-
-// PatchUpMethodReferences - Go over the methods that are in the module and
-// look for methods that have the same name. More often than not, there will
-// be things like:
-// void "foo"(...)
-// void "foo"(int, int)
-// because of the way things are declared in C. If this is the case, patch
-// things up.
-//
-static bool PatchUpMethodReferences(SymbolTable *ST) {
- map<string, vector<Method*> > Methods;
-
- // Loop over the entries in the symbol table. If an entry is a method pointer,
- // then add it to the Methods map. We do a two pass algorithm here to avoid
- // problems with iterators getting invalidated if we did a one pass scheme.
- //
- for (SymbolTable::iterator I = ST->begin(), E = ST->end(); I != E; ++I)
- if (const PointerType *PT = dyn_cast<PointerType>(I->first))
- if (const MethodType *MT = dyn_cast<MethodType>(PT->getValueType())) {
- SymbolTable::VarMap &Plane = I->second;
- for (SymbolTable::type_iterator PI = Plane.begin(), PE = Plane.end();
- PI != PE; ++PI) {
- const string &Name = PI->first;
- Method *M = cast<Method>(PI->second);
- Methods[Name].push_back(M);
- }
- }
-
- bool Changed = false;
-
- // Now we have a list of all methods with a particular name. If there is more
- // than one entry in a list, merge the methods together.
- //
- for (map<string, vector<Method*> >::iterator I = Methods.begin(),
- E = Methods.end(); I != E; ++I) {
- vector<Method*> &Methods = I->second;
- if (Methods.size() > 1) { // Found a multiply defined method.
- Method *Implementation = 0; // Find the implementation
- Method *Concrete = 0;
- for (unsigned i = 0; i < Methods.size(); ++i) {
- if (!Methods[i]->isExternal()) { // Found an implementation
- assert(Implementation == 0 && "Multiple definitions of the same"
- " method. Case not handled yet!");
- Implementation = Methods[i];
- }
-
- if (!Methods[i]->getMethodType()->isVarArg() ||
- Methods[i]->getMethodType()->getParamTypes().size()) {
- if (Concrete) { // Found two different methods types. Can't choose
- Concrete = 0;
- break;
- }
- Concrete = Methods[i];
- }
- }
-
- // We should find exactly one non-vararg method definition, which is
- // probably the implementation. Change all of the method definitions
- // and uses to use it instead.
- //
- if (!Concrete) {
- cerr << "Warning: Found methods types that are not compatible:\n";
- for (unsigned i = 0; i < Methods.size(); ++i) {
- cerr << "\t" << Methods[i]->getType()->getDescription() << " %"
- << Methods[i]->getName() << endl;
- }
- cerr << " No linkage of methods named '" << Methods[0]->getName()
- << "' performed!\n";
- } else {
- for (unsigned i = 0; i < Methods.size(); ++i)
- if (Methods[i] != Concrete) {
- Method *Old = Methods[i];
- assert(Old->getReturnType() == Concrete->getReturnType() &&
- "Differing return types not handled yet!");
- assert(Old->getMethodType()->getParamTypes().size() == 0 &&
- "Cannot handle varargs fn's with specified element types!");
-
- // Attempt to convert all of the uses of the old method to the
- // concrete form of the method. If there is a use of the method
- // that we don't understand here we punt to avoid making a bad
- // transformation.
- //
- // At this point, we know that the return values are the same for
- // our two functions and that the Old method has no varargs methods
- // specified. In otherwords it's just <retty> (...)
- //
- for (unsigned i = 0; i < Old->use_size(); ) {
- User *U = *(Old->use_begin()+i);
- if (CastInst *CI = dyn_cast<CastInst>(U)) {
- // Convert casts directly
- assert(CI->getOperand(0) == Old);
- CI->setOperand(0, Concrete);
- Changed = true;
- } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
- // Can only fix up calls TO the argument, not args passed in.
- if (CI->getCalledValue() == Old) {
- ConvertCallTo(CI, Concrete);
- Changed = true;
- } else {
- cerr << "Couldn't cleanup this function call, must be an"
- << " argument or something!" << CI;
- ++i;
- }
- } else {
- cerr << "Cannot convert use of method: " << U << endl;
- ++i;
- }
- }
- }
- }
- }
- }
-
- return Changed;