+//
+// Function: ResolveTypes()
+//
+// Description:
+// Attempt to link the two specified types together.
+//
+// Inputs:
+// DestTy - The type to which we wish to resolve.
+// SrcTy - The original type which we want to resolve.
+// Name - The name of the type.
+//
+// Outputs:
+// DestST - The symbol table in which the new type should be placed.
+//
+// Return value:
+// true - There is an error and the types cannot yet be linked.
+// false - No errors.
+//
+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);
+}