+// 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
+ std::cerr << "[Added] refined to abstype: " << NewType->getDescription()
+ << "\n";
+#endif
+ }
+ }
+
+ VarMap &NewPlane = NewTypeIt->second;
+ VarMap &OldPlane = TPI->second;
+ while (!OldPlane.empty()) {
+ std::pair<const std::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 external global values
+ // folded into one.
+ //
+ GlobalValue *ExistGV = dyn_cast<GlobalValue>(TI->second);
+ GlobalValue *NewGV = dyn_cast<GlobalValue>(V.second);
+
+ if (ExistGV && NewGV && ExistGV->isExternal() && NewGV->isExternal()) {
+ // Ok we have two external global values. Make all uses of the new
+ // one use the old one...
+ //
+ assert(ExistGV->use_empty() && "No uses allowed on untyped value!");
+ //NewGV->replaceAllUsesWith(ExistGV);
+
+ // 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
+ NewGV->setName("");
+ InternallyInconsistent = false;
+
+ // Now we can remove this global from the module entirely...
+ Module *M = NewGV->getParent();
+ if (Function *F = dyn_cast<Function>(NewGV))
+ M->getFunctionList().remove(F);
+ else
+ M->getGlobalList().remove(cast<GlobalVariable>(NewGV));
+ delete NewGV;
+
+ } else {
+ assert(0 && "Two planes 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
+ std::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
+ std::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
+ std::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
+ std::cerr << "Added type " << NewType->getDescription() << "\n";
+#endif
+ cast<DerivedType>(NewType)->addAbstractTypeUser(this);
+ }
+ }
+ }
+}
+
+static void DumpVal(const std::pair<const std::string, Value *> &V) {
+ std::cout << " '" << V.first << "' = ";
+ V.second->dump();
+ std::cout << "\n";
+}
+
+static void DumpPlane(const std::pair<const Type *,
+ std::map<const std::string, Value *> >&P){
+ std::cout << " Plane: ";
+ P.first->dump();
+ std::cout << "\n";
+ for_each(P.second.begin(), P.second.end(), DumpVal);
+}
+
+void SymbolTable::dump() const {
+ std::cout << "Symbol table dump:\n";
+ for_each(begin(), end(), DumpPlane);
+}