+
+ // Check to see if the type is abstract. If so, it might be refined in the
+ // future, which would cause the plane of the old type to get merged into
+ // a new type plane.
+ //
+ if (VTy->isAbstract()) {
+ cast<DerivedType>(VTy)->addAbstractTypeUser(this);
+#if DEBUG_ABSTYPE
+ cerr << "Added abstract type value: " << VTy->getDescription() << "\n";
+#endif
+ }
+ }
+
+ I->second.insert(make_pair(Name, V));
+
+ // If we are adding an abstract type, add the symbol table to it's use list.
+ if (VTy == Type::TypeTy) {
+ const Type *T = cast<const Type>(V);
+ if (T->isAbstract()) {
+ cast<DerivedType>(T)->addAbstractTypeUser(this);
+#if DEBUG_ABSTYPE
+ cerr << "Added abstract type to ST: " << T->getDescription() << "\n";
+#endif
+ }
+ }
+}
+
+// This function is called when one of the types in the type plane are refined
+void SymbolTable::refineAbstractType(const DerivedType *OldType,
+ const Type *NewType) {
+ if (OldType == NewType && OldType->isAbstract())
+ return; // Noop, don't waste time dinking around
+
+ // Search to see if we have any values of the type oldtype. If so, we need to
+ // move them into the newtype plane...
+ iterator TPI = find(OldType);
+ if (OldType != NewType && TPI != end()) {
+ // Get a handle to the new type plane...
+ iterator NewTypeIt = find(NewType);
+ if (NewTypeIt == super::end()) { // If no plane exists, add one
+ NewTypeIt = super::insert(make_pair(NewType, VarMap())).first;
+
+ if (NewType->isAbstract()) {
+ cast<DerivedType>(NewType)->addAbstractTypeUser(this);
+#if DEBUG_ABSTYPE
+ cerr << "[Added] refined to abstype: "<<NewType->getDescription()<<"\n";
+#endif
+ }
+ }
+
+ VarMap &NewPlane = NewTypeIt->second;
+ VarMap &OldPlane = TPI->second;
+ while (!OldPlane.empty()) {
+ pair<const string, Value*> V = *OldPlane.begin();
+
+ // Check to see if there is already a value in the symbol table that this
+ // would collide with.
+ type_iterator TI = NewPlane.find(V.first);
+ if (TI != NewPlane.end() && TI->second == V.second) {
+ // No action
+
+ } else if (TI != NewPlane.end()) {
+ // The only thing we are allowing for now is two method prototypes being
+ // folded into one.
+ //
+ Function *ExistM = dyn_cast<Function>(TI->second);
+ Function *NewM = dyn_cast<Function>(V.second);
+
+ if (ExistM && NewM && ExistM->isExternal() && NewM->isExternal()) {
+ // Ok we have two external methods. Make all uses of the new one
+ // use the old one...
+ //
+ NewM->replaceAllUsesWith(ExistM);
+
+ // Now we just convert it to an unnamed method... which won't get
+ // added to our symbol table. The problem is that if we call
+ // setName on the method that it will try to remove itself from
+ // the symbol table and die... because it's not in the symtab
+ // right now. To fix this, we have an internally consistent flag
+ // that turns remove into a noop. Thus the name will get null'd
+ // out, but the symbol table won't get upset.
+ //
+ assert(InternallyInconsistent == false &&
+ "Symbol table already inconsistent!");
+ InternallyInconsistent = true;
+
+ // Remove newM from the symtab
+ NewM->setName("");
+ InternallyInconsistent = false;
+
+ // Now we can remove this method from the module entirely...
+ NewM->getParent()->getFunctionList().remove(NewM);
+ delete NewM;
+
+ } else {
+ assert(0 && "Two ploanes folded together with overlapping "
+ "value names!");
+ }
+ } else {
+ insertEntry(V.first, NewType, V.second);
+
+ }
+ // Remove the item from the old type plane
+ OldPlane.erase(OldPlane.begin());
+ }
+
+ // Ok, now we are not referencing the type anymore... take me off your user
+ // list please!
+#if DEBUG_ABSTYPE
+ cerr << "Removing type " << OldType->getDescription() << "\n";
+#endif
+ OldType->removeAbstractTypeUser(this);
+
+ // Remove the plane that is no longer used
+ erase(TPI);
+ } else if (TPI != end()) {
+ assert(OldType == NewType);
+#if DEBUG_ABSTYPE
+ cerr << "Removing SELF type " << OldType->getDescription() << "\n";
+#endif
+ OldType->removeAbstractTypeUser(this);
+ }
+
+ TPI = find(Type::TypeTy);
+ if (TPI != end()) {
+ // Loop over all of the types in the symbol table, replacing any references
+ // to OldType with references to NewType. Note that there may be multiple
+ // occurances, and although we only need to remove one at a time, it's
+ // faster to remove them all in one pass.
+ //
+ VarMap &TyPlane = TPI->second;
+ for (VarMap::iterator I = TyPlane.begin(), E = TyPlane.end(); I != E; ++I)
+ if (I->second == (Value*)OldType) { // FIXME when Types aren't const.
+#if DEBUG_ABSTYPE
+ cerr << "Removing type " << OldType->getDescription() << "\n";
+#endif
+ OldType->removeAbstractTypeUser(this);
+
+ I->second = (Value*)NewType; // TODO FIXME when types aren't const
+ if (NewType->isAbstract()) {
+#if DEBUG_ABSTYPE
+ cerr << "Added type " << NewType->getDescription() << "\n";
+#endif
+ cast<const DerivedType>(NewType)->addAbstractTypeUser(this);
+ }
+ }