return true;
}
+// ResolveTypes - Attempt to link the two specified types together. Return true
+// if there is an error and they cannot yet be linked.
+//
+static bool ResolveTypes(const Type *DestTy, const Type *SrcTy,
+ SymbolTable *DestST, const std::string &Name) {
+ if (DestTy == SrcTy) return false; // If already equal, noop
+
+ // Does the type already exist in the module?
+ if (DestTy && !isa<OpaqueType>(DestTy)) { // Yup, the type already exists...
+ if (const OpaqueType *OT = dyn_cast<OpaqueType>(SrcTy)) {
+ const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(DestTy);
+ } else {
+ return true; // Cannot link types... neither is opaque and not-equal
+ }
+ } 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);
+ else if (!Name.empty())
+ DestST->insert(Name, const_cast<Type*>(SrcTy));
+ }
+ return false;
+}
+
+static const FunctionType *getFT(const PATypeHolder &TH) {
+ return cast<FunctionType>(TH.get());
+}
+static const StructType *getST(const PATypeHolder &TH) {
+ return cast<StructType>(TH.get());
+}
+
+// RecursiveResolveTypes - This is just like ResolveTypes, except that it
+// recurses down into derived types, merging the used types if the parent types
+// are compatible.
+//
+static bool RecursiveResolveTypesI(const PATypeHolder &DestTy,
+ const PATypeHolder &SrcTy,
+ SymbolTable *DestST, const std::string &Name,
+ std::vector<std::pair<PATypeHolder, PATypeHolder> > &Pointers) {
+ const Type *SrcTyT = SrcTy.get();
+ const Type *DestTyT = DestTy.get();
+ if (DestTyT == SrcTyT) return false; // If already equal, noop
+
+ // If we found our opaque type, resolve it now!
+ if (isa<OpaqueType>(DestTyT) || isa<OpaqueType>(SrcTyT))
+ return ResolveTypes(DestTyT, SrcTyT, DestST, Name);
+
+ // Two types cannot be resolved together if they are of different primitive
+ // type. For example, we cannot resolve an int to a float.
+ if (DestTyT->getPrimitiveID() != SrcTyT->getPrimitiveID()) return true;
+
+ // Otherwise, resolve the used type used by this derived type...
+ switch (DestTyT->getPrimitiveID()) {
+ case Type::FunctionTyID: {
+ if (cast<FunctionType>(DestTyT)->isVarArg() !=
+ cast<FunctionType>(SrcTyT)->isVarArg() ||
+ cast<FunctionType>(DestTyT)->getNumContainedTypes() !=
+ cast<FunctionType>(SrcTyT)->getNumContainedTypes())
+ return true;
+ for (unsigned i = 0, e = getFT(DestTy)->getNumContainedTypes(); i != e; ++i)
+ if (RecursiveResolveTypesI(getFT(DestTy)->getContainedType(i),
+ getFT(SrcTy)->getContainedType(i), DestST, "",
+ Pointers))
+ return true;
+ return false;
+ }
+ case Type::StructTyID: {
+ if (getST(DestTy)->getNumContainedTypes() !=
+ getST(SrcTy)->getNumContainedTypes()) return 1;
+ for (unsigned i = 0, e = getST(DestTy)->getNumContainedTypes(); i != e; ++i)
+ if (RecursiveResolveTypesI(getST(DestTy)->getContainedType(i),
+ getST(SrcTy)->getContainedType(i), DestST, "",
+ Pointers))
+ return true;
+ return false;
+ }
+ case Type::ArrayTyID: {
+ const ArrayType *DAT = cast<ArrayType>(DestTy.get());
+ const ArrayType *SAT = cast<ArrayType>(SrcTy.get());
+ if (DAT->getNumElements() != SAT->getNumElements()) return true;
+ return RecursiveResolveTypesI(DAT->getElementType(), SAT->getElementType(),
+ DestST, "", Pointers);
+ }
+ case Type::PointerTyID: {
+ // If this is a pointer type, check to see if we have already seen it. If
+ // so, we are in a recursive branch. Cut off the search now. We cannot use
+ // an associative container for this search, because the type pointers (keys
+ // in the container) change whenever types get resolved...
+ //
+ for (unsigned i = 0, e = Pointers.size(); i != e; ++i)
+ if (Pointers[i].first == DestTy)
+ return Pointers[i].second != SrcTy;
+
+ // Otherwise, add the current pointers to the vector to stop recursion on
+ // this pair.
+ Pointers.push_back(std::make_pair(DestTyT, SrcTyT));
+ bool Result =
+ RecursiveResolveTypesI(cast<PointerType>(DestTy.get())->getElementType(),
+ cast<PointerType>(SrcTy.get())->getElementType(),
+ DestST, "", Pointers);
+ Pointers.pop_back();
+ return Result;
+ }
+ default: assert(0 && "Unexpected type!"); return true;
+ }
+}
+
+static bool RecursiveResolveTypes(const PATypeHolder &DestTy,
+ const PATypeHolder &SrcTy,
+ SymbolTable *DestST, const std::string &Name){
+ std::vector<std::pair<PATypeHolder, PATypeHolder> > PointerTypes;
+ return RecursiveResolveTypesI(DestTy, SrcTy, DestST, Name, PointerTypes);
+}
+
+
// LinkTypes - Go through the symbol table of the Src module and see if any
// types are named in the src module that are not named in the Dst module.
// Make sure there are no type name conflicts.
SymbolTable::const_iterator PI = SrcST->find(Type::TypeTy);
if (PI == SrcST->end()) return false; // No named types, do nothing.
+ // Some types cannot be resolved immediately becuse they depend on other types
+ // being resolved to each other first. This contains a list of types we are
+ // waiting to recheck.
+ std::vector<std::string> DelayedTypesToResolve;
+
const SymbolTable::VarMap &VM = PI->second;
for (SymbolTable::type_const_iterator I = VM.begin(), E = VM.end();
I != E; ++I) {
const std::string &Name = I->first;
- const Type *RHS = cast<Type>(I->second);
+ Type *RHS = cast<Type>(I->second);
// Check to see if this type name is already in the dest module...
- const Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name));
- if (Entry && !isa<OpaqueType>(Entry)) { // Yup, the value already exists...
- if (Entry != RHS) {
- if (OpaqueType *OT = dyn_cast<OpaqueType>(const_cast<Type*>(RHS))) {
- OT->refineAbstractTypeTo(Entry);
- } else {
- // If it's the same, noop. Otherwise, error.
- return Error(Err, "Type named '" + Name +
- "' of different shape in modules.\n Src='" +
- Entry->getDescription() + "'.\n Dst='" +
- RHS->getDescription() + "'");
- }
+ Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name));
+
+ if (ResolveTypes(Entry, RHS, DestST, Name)) {
+ // They look different, save the types 'till later to resolve.
+ DelayedTypesToResolve.push_back(Name);
+ }
+ }
+
+ // Iteratively resolve types while we can...
+ while (!DelayedTypesToResolve.empty()) {
+ // Loop over all of the types, attempting to resolve them if possible...
+ unsigned OldSize = DelayedTypesToResolve.size();
+
+ // Try direct resolution by name...
+ for (unsigned i = 0; i != DelayedTypesToResolve.size(); ++i) {
+ const std::string &Name = DelayedTypesToResolve[i];
+ Type *T1 = cast<Type>(VM.find(Name)->second);
+ Type *T2 = cast<Type>(DestST->lookup(Type::TypeTy, Name));
+ if (!ResolveTypes(T2, T1, DestST, Name)) {
+ // We are making progress!
+ DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i);
+ --i;
}
- } else { // Type not in dest module. Add it now.
- if (Entry) {
- OpaqueType *OT = cast<OpaqueType>(const_cast<Type*>(Entry));
- OT->refineAbstractTypeTo(RHS);
+ }
+
+ // Did we not eliminate any types?
+ if (DelayedTypesToResolve.size() == OldSize) {
+ // Attempt to resolve subelements of types. This allows us to merge these
+ // two types: { int* } and { opaque* }
+ for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) {
+ const std::string &Name = DelayedTypesToResolve[i];
+ PATypeHolder T1(cast<Type>(VM.find(Name)->second));
+ PATypeHolder T2(cast<Type>(DestST->lookup(Type::TypeTy, Name)));
+
+ if (!RecursiveResolveTypes(T2, T1, DestST, Name)) {
+ // We are making progress!
+ DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i);
+
+ // Go back to the main loop, perhaps we can resolve directly by name
+ // now...
+ break;
+ }
}
- // TODO: FIXME WHEN TYPES AREN'T CONST
- DestST->insert(Name, const_cast<Type*>(RHS));
+ // If we STILL cannot resolve the types, then there is something wrong.
+ // Report the error.
+ if (DelayedTypesToResolve.size() == OldSize) {
+ // Build up an error message of all of the mismatched types.
+ std::string ErrorMessage;
+ for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) {
+ const std::string &Name = DelayedTypesToResolve[i];
+ const Type *T1 = cast<Type>(VM.find(Name)->second);
+ const Type *T2 = cast<Type>(DestST->lookup(Type::TypeTy, Name));
+ ErrorMessage += " Type named '" + Name +
+ "' conflicts.\n Src='" + T1->getDescription() +
+ "'.\n Dest='" + T2->getDescription() + "'\n";
+ }
+ return Error(Err, "Type conflict between types in modules:\n" +
+ ErrorMessage);
+ }
}
}
+
+
return false;
}
return 0;
}
+/// FindGlobalNamed - Look in the specified symbol table for a global with the
+/// specified name and type. If an exactly matching global does not exist, see
+/// if there is a global which is "type compatible" with the specified
+/// name/type. This allows us to resolve things like '%x = global int*' with
+/// '%x = global opaque*'.
+///
+static GlobalValue *FindGlobalNamed(const std::string &Name, const Type *Ty,
+ SymbolTable *ST) {
+ // See if an exact match exists in the symbol table...
+ if (Value *V = ST->lookup(Ty, Name)) return cast<GlobalValue>(V);
+
+ // It doesn't exist exactly, scan through all of the type planes in the symbol
+ // table, checking each of them for a type-compatible version.
+ //
+ for (SymbolTable::iterator I = ST->begin(), E = ST->end(); I != E; ++I)
+ if (I->first != Type::TypeTy) {
+ SymbolTable::VarMap &VM = I->second;
+ // Does this type plane contain an entry with the specified name?
+ SymbolTable::type_iterator TI = VM.find(Name);
+ if (TI != VM.end()) {
+ // Determine whether we can fold the two types together, resolving them.
+ // If so, we can use this value.
+ if (!RecursiveResolveTypes(Ty, I->first, ST, ""))
+ return cast<GlobalValue>(TI->second);
+ }
+ }
+ return 0; // Otherwise, nothing could be found.
+}
+
// LinkGlobals - Loop through the global variables in the src module and merge
// them into the dest module.
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
- DGV = cast_or_null<GlobalVariable>(ST->lookup(SGV->getType(),
- SGV->getName()));
+ DGV = cast_or_null<GlobalVariable>(FindGlobalNamed(SGV->getName(),
+ SGV->getType(), ST));
}
assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
- DF = cast_or_null<Function>(ST->lookup(SF->getType(), SF->getName()));
+ DF = cast_or_null<Function>(FindGlobalNamed(SF->getName(), SF->getType(),
+ ST));
if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
// Function does not already exist, simply insert an function signature
// shouldn't be relied on to be consistent.
//
bool LinkModules(Module *Dest, const Module *Src, std::string *ErrorMsg) {
- if (Dest->getEndianness() != Src->getEndianness())
+ if (Dest->getEndianness() == Module::AnyEndianness)
+ Dest->setEndianness(Src->getEndianness());
+ if (Dest->getPointerSize() == Module::AnyPointerSize)
+ Dest->setPointerSize(Src->getPointerSize());
+
+ if (Src->getEndianness() != Module::AnyEndianness &&
+ Dest->getEndianness() != Src->getEndianness())
std::cerr << "WARNING: Linking two modules of different endianness!\n";
- if (Dest->getPointerSize() != Src->getPointerSize())
+ if (Src->getPointerSize() != Module::AnyPointerSize &&
+ Dest->getPointerSize() != Src->getPointerSize())
std::cerr << "WARNING: Linking two modules of different pointer size!\n";
// LinkTypes - Go through the symbol table of the Src module and see if any