Simplify ResolveTypes by pulling the null case out into the one

client that cares and simplifying its control flow.

Remove the DestST argument to ResolveTypes and RecursiveResolveTypes*
which are dead now.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@52340 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2008-06-16 18:19:05 +00:00
parent 69d5053f9c
commit bc1c82a0f7

View File

@ -60,21 +60,17 @@ static std::string ToStr(const Type *Ty, const Module *M) {
// true - There is an error and the types cannot yet be linked. // true - There is an error and the types cannot yet be linked.
// false - No errors. // false - No errors.
// //
static bool ResolveTypes(const Type *DestTy, const Type *SrcTy, static bool ResolveTypes(const Type *DestTy, const Type *SrcTy) {
TypeSymbolTable *DestST) {
if (DestTy == SrcTy) return false; // If already equal, noop if (DestTy == SrcTy) return false; // If already equal, noop
assert(DestTy && SrcTy && "Can't handle null types");
// Does the type already exist in the module? if (const OpaqueType *OT = dyn_cast<OpaqueType>(DestTy)) {
if (DestTy && !isa<OpaqueType>(DestTy)) { // Yup, the type already exists... // Type _is_ in module, just opaque...
if (const OpaqueType *OT = dyn_cast<OpaqueType>(SrcTy)) { const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(SrcTy);
const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(DestTy); } else if (const OpaqueType *OT = dyn_cast<OpaqueType>(SrcTy)) {
} else { const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(DestTy);
return true; // Cannot link types... neither is opaque and not-equal } else {
} return true; // Cannot link types... not-equal and neither is opaque.
} else { // Type not in dest module. Add it now.
if (DestTy) // Type _is_ in module, just opaque...
const_cast<OpaqueType*>(cast<OpaqueType>(DestTy))
->refineAbstractTypeTo(SrcTy);
} }
return false; return false;
} }
@ -91,7 +87,6 @@ static const StructType *getST(const PATypeHolder &TH) {
// are compatible. // are compatible.
static bool RecursiveResolveTypesI(const PATypeHolder &DestTy, static bool RecursiveResolveTypesI(const PATypeHolder &DestTy,
const PATypeHolder &SrcTy, const PATypeHolder &SrcTy,
TypeSymbolTable *DestST,
std::vector<std::pair<PATypeHolder, PATypeHolder> > &Pointers) { std::vector<std::pair<PATypeHolder, PATypeHolder> > &Pointers) {
const Type *SrcTyT = SrcTy.get(); const Type *SrcTyT = SrcTy.get();
const Type *DestTyT = DestTy.get(); const Type *DestTyT = DestTy.get();
@ -99,7 +94,7 @@ static bool RecursiveResolveTypesI(const PATypeHolder &DestTy,
// If we found our opaque type, resolve it now! // If we found our opaque type, resolve it now!
if (isa<OpaqueType>(DestTyT) || isa<OpaqueType>(SrcTyT)) if (isa<OpaqueType>(DestTyT) || isa<OpaqueType>(SrcTyT))
return ResolveTypes(DestTyT, SrcTyT, DestST); return ResolveTypes(DestTyT, SrcTyT);
// Two types cannot be resolved together if they are of different primitive // Two types cannot be resolved together if they are of different primitive
// type. For example, we cannot resolve an int to a float. // type. For example, we cannot resolve an int to a float.
@ -121,8 +116,7 @@ static bool RecursiveResolveTypesI(const PATypeHolder &DestTy,
return true; return true;
for (unsigned i = 0, e = getFT(DestTy)->getNumContainedTypes(); i != e; ++i) for (unsigned i = 0, e = getFT(DestTy)->getNumContainedTypes(); i != e; ++i)
if (RecursiveResolveTypesI(getFT(DestTy)->getContainedType(i), if (RecursiveResolveTypesI(getFT(DestTy)->getContainedType(i),
getFT(SrcTy)->getContainedType(i), DestST, getFT(SrcTy)->getContainedType(i), Pointers))
Pointers))
return true; return true;
return false; return false;
} }
@ -131,8 +125,7 @@ static bool RecursiveResolveTypesI(const PATypeHolder &DestTy,
getST(SrcTy)->getNumContainedTypes()) return 1; getST(SrcTy)->getNumContainedTypes()) return 1;
for (unsigned i = 0, e = getST(DestTy)->getNumContainedTypes(); i != e; ++i) for (unsigned i = 0, e = getST(DestTy)->getNumContainedTypes(); i != e; ++i)
if (RecursiveResolveTypesI(getST(DestTy)->getContainedType(i), if (RecursiveResolveTypesI(getST(DestTy)->getContainedType(i),
getST(SrcTy)->getContainedType(i), DestST, getST(SrcTy)->getContainedType(i), Pointers))
Pointers))
return true; return true;
return false; return false;
} }
@ -141,7 +134,7 @@ static bool RecursiveResolveTypesI(const PATypeHolder &DestTy,
const ArrayType *SAT = cast<ArrayType>(SrcTy.get()); const ArrayType *SAT = cast<ArrayType>(SrcTy.get());
if (DAT->getNumElements() != SAT->getNumElements()) return true; if (DAT->getNumElements() != SAT->getNumElements()) return true;
return RecursiveResolveTypesI(DAT->getElementType(), SAT->getElementType(), return RecursiveResolveTypesI(DAT->getElementType(), SAT->getElementType(),
DestST, Pointers); Pointers);
} }
case Type::PointerTyID: { case Type::PointerTyID: {
// If this is a pointer type, check to see if we have already seen it. If // If this is a pointer type, check to see if we have already seen it. If
@ -158,7 +151,7 @@ static bool RecursiveResolveTypesI(const PATypeHolder &DestTy,
bool Result = bool Result =
RecursiveResolveTypesI(cast<PointerType>(DestTy.get())->getElementType(), RecursiveResolveTypesI(cast<PointerType>(DestTy.get())->getElementType(),
cast<PointerType>(SrcTy.get())->getElementType(), cast<PointerType>(SrcTy.get())->getElementType(),
DestST, Pointers); Pointers);
Pointers.pop_back(); Pointers.pop_back();
return Result; return Result;
} }
@ -167,10 +160,9 @@ static bool RecursiveResolveTypesI(const PATypeHolder &DestTy,
} }
static bool RecursiveResolveTypes(const PATypeHolder &DestTy, static bool RecursiveResolveTypes(const PATypeHolder &DestTy,
const PATypeHolder &SrcTy, const PATypeHolder &SrcTy) {
TypeSymbolTable *DestST){
std::vector<std::pair<PATypeHolder, PATypeHolder> > PointerTypes; std::vector<std::pair<PATypeHolder, PATypeHolder> > PointerTypes;
return RecursiveResolveTypesI(DestTy, SrcTy, DestST, PointerTypes); return RecursiveResolveTypesI(DestTy, SrcTy, PointerTypes);
} }
@ -195,12 +187,14 @@ static bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) {
const std::string &Name = TI->first; const std::string &Name = TI->first;
const Type *RHS = TI->second; const Type *RHS = TI->second;
// Check to see if this type name is already in the dest module... // Check to see if this type name is already in the dest module.
Type *Entry = DestST->lookup(Name); Type *Entry = DestST->lookup(Name);
if (Entry == 0 && !Name.empty()) // If the name is just in the source module, bring it over to the dest.
DestST->insert(Name, const_cast<Type*>(RHS)); if (Entry == 0) {
else if (ResolveTypes(Entry, RHS, DestST)) { if (!Name.empty())
DestST->insert(Name, const_cast<Type*>(RHS));
} else if (ResolveTypes(Entry, RHS)) {
// They look different, save the types 'till later to resolve. // They look different, save the types 'till later to resolve.
DelayedTypesToResolve.push_back(Name); DelayedTypesToResolve.push_back(Name);
} }
@ -216,7 +210,7 @@ static bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) {
const std::string &Name = DelayedTypesToResolve[i]; const std::string &Name = DelayedTypesToResolve[i];
Type *T1 = SrcST->lookup(Name); Type *T1 = SrcST->lookup(Name);
Type *T2 = DestST->lookup(Name); Type *T2 = DestST->lookup(Name);
if (!ResolveTypes(T2, T1, DestST)) { if (!ResolveTypes(T2, T1)) {
// We are making progress! // We are making progress!
DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i); DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i);
--i; --i;
@ -232,7 +226,7 @@ static bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) {
PATypeHolder T1(SrcST->lookup(Name)); PATypeHolder T1(SrcST->lookup(Name));
PATypeHolder T2(DestST->lookup(Name)); PATypeHolder T2(DestST->lookup(Name));
if (!RecursiveResolveTypes(T2, T1, DestST)) { if (!RecursiveResolveTypes(T2, T1)) {
// We are making progress! // We are making progress!
DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i); DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i);
@ -459,8 +453,7 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
DGV = Dest->getGlobalVariable(SGV->getName()); DGV = Dest->getGlobalVariable(SGV->getName());
if (DGV && DGV->getType() != SGV->getType()) if (DGV && DGV->getType() != SGV->getType())
// If types don't agree due to opaque types, try to resolve them. // If types don't agree due to opaque types, try to resolve them.
RecursiveResolveTypes(SGV->getType(), DGV->getType(), RecursiveResolveTypes(SGV->getType(), DGV->getType());
&Dest->getTypeSymbolTable());
} }
// Check to see if may have to link the global with the alias // Check to see if may have to link the global with the alias
@ -468,8 +461,7 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
DGV = Dest->getNamedAlias(SGV->getName()); DGV = Dest->getNamedAlias(SGV->getName());
if (DGV && DGV->getType() != SGV->getType()) if (DGV && DGV->getType() != SGV->getType())
// If types don't agree due to opaque types, try to resolve them. // If types don't agree due to opaque types, try to resolve them.
RecursiveResolveTypes(SGV->getType(), DGV->getType(), RecursiveResolveTypes(SGV->getType(), DGV->getType());
&Dest->getTypeSymbolTable());
} }
if (DGV && DGV->hasInternalLinkage()) if (DGV && DGV->hasInternalLinkage())
@ -629,8 +621,7 @@ static bool LinkAlias(Module *Dest, const Module *Src,
// If types don't agree due to opaque types, try to resolve them. // If types don't agree due to opaque types, try to resolve them.
if (DGV && DGV->getType() != SGA->getType()) if (DGV && DGV->getType() != SGA->getType())
if (RecursiveResolveTypes(SGA->getType(), DGV->getType(), if (RecursiveResolveTypes(SGA->getType(), DGV->getType()))
&Dest->getTypeSymbolTable()))
return Error(Err, "Alias Collision on '" + SGA->getName()+ return Error(Err, "Alias Collision on '" + SGA->getName()+
"': aliases have different types"); "': aliases have different types");
} }
@ -640,8 +631,7 @@ static bool LinkAlias(Module *Dest, const Module *Src,
// If types don't agree due to opaque types, try to resolve them. // If types don't agree due to opaque types, try to resolve them.
if (DGV && DGV->getType() != SGA->getType()) if (DGV && DGV->getType() != SGA->getType())
if (RecursiveResolveTypes(SGA->getType(), DGV->getType(), if (RecursiveResolveTypes(SGA->getType(), DGV->getType()))
&Dest->getTypeSymbolTable()))
return Error(Err, "Alias Collision on '" + SGA->getName()+ return Error(Err, "Alias Collision on '" + SGA->getName()+
"': aliases have different types"); "': aliases have different types");
} }
@ -651,8 +641,7 @@ static bool LinkAlias(Module *Dest, const Module *Src,
// If types don't agree due to opaque types, try to resolve them. // If types don't agree due to opaque types, try to resolve them.
if (DGV && DGV->getType() != SGA->getType()) if (DGV && DGV->getType() != SGA->getType())
if (RecursiveResolveTypes(SGA->getType(), DGV->getType(), if (RecursiveResolveTypes(SGA->getType(), DGV->getType()))
&Dest->getTypeSymbolTable()))
return Error(Err, "Alias Collision on '" + SGA->getName()+ return Error(Err, "Alias Collision on '" + SGA->getName()+
"': aliases have different types"); "': aliases have different types");
} }
@ -850,8 +839,7 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
// If types don't agree because of opaque, try to resolve them. // If types don't agree because of opaque, try to resolve them.
if (SF->getType() != DF->getType()) if (SF->getType() != DF->getType())
RecursiveResolveTypes(SF->getType(), DF->getType(), RecursiveResolveTypes(SF->getType(), DF->getType());
&Dest->getTypeSymbolTable());
// Check visibility, merging if a definition overrides a prototype. // Check visibility, merging if a definition overrides a prototype.
if (SF->getVisibility() != DF->getVisibility()) { if (SF->getVisibility() != DF->getVisibility()) {