LinkModules.cpp revision e9bb2df410f7a22decad9a883f7139d5857c1520
152f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//===- Linker.cpp - Module Linker Implementation --------------------------===//
252f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//
352f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// This file implements the LLVM module linker.
452f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//
552f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// Specifically, this:
68d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner//  * Merges global variables between the two modules
78d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner//    * Uninit + Uninit = Init, Init + Uninit = Init, Init + Init = Error if !=
88d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner//  * Merges methods between two modules
952f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//
1052f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//===----------------------------------------------------------------------===//
1152f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
1252f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner#include "llvm/Transforms/Linker.h"
135c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/Module.h"
145c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/Method.h"
155c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/GlobalVariable.h"
165c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/SymbolTable.h"
175c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/DerivedTypes.h"
185c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/iOther.h"
19e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner#include "llvm/ConstantVals.h"
205c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
215c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// Error - Simple wrapper function to conditionally assign to E and return true.
225c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// This just makes error return conditions a little bit simpler...
235c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
245c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattnerstatic inline bool Error(string *E, string Message) {
255c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (E) *E = Message;
265c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return true;
275c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
285c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
295c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/Assembly/Writer.h" // TODO: REMOVE
305c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
312c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
322c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// LinkTypes - Go through the symbol table of the Src module and see if any
332c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// types are named in the src module that are not named in the Dst module.
342c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// Make sure there are no type name conflicts.
352c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner//
362c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattnerstatic bool LinkTypes(Module *Dest, const Module *Src, string *Err = 0) {
372c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // No symbol table?  Can't have named types.
382c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  if (!Src->hasSymbolTable()) return false;
392c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
402c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  SymbolTable       *DestST = Dest->getSymbolTableSure();
412c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  const SymbolTable *SrcST  = Src->getSymbolTable();
422c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
432c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // Look for a type plane for Type's...
442c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  SymbolTable::const_iterator PI = SrcST->find(Type::TypeTy);
452c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  if (PI == SrcST->end()) return false;  // No named types, do nothing.
462c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
472c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  const SymbolTable::VarMap &VM = PI->second;
482c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  for (SymbolTable::type_const_iterator I = VM.begin(), E = VM.end();
492c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner       I != E; ++I) {
502c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    const string &Name = I->first;
512c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    const Type *RHS = cast<Type>(I->second);
522c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
532c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    // Check to see if this type name is already in the dest module...
542c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    const Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name));
552c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    if (Entry) {     // Yup, the value already exists...
562c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner      if (Entry != RHS)            // If it's the same, noop.  Otherwise, error.
572c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner        return Error(Err, "Type named '" + Name +
582c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner                     "' of different shape in modules.\n  Src='" +
592c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner                     Entry->getDescription() + "'.  Dest='" +
602c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner                     RHS->getDescription() + "'");
612c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    } else {                       // Type not in dest module.  Add it now.
622c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner      // TODO: FIXME WHEN TYPES AREN'T CONST
632c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner      DestST->insert(Name, const_cast<Type*>(RHS));
642c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    }
652c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  }
662c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  return false;
672c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner}
682c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
692d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattnerstatic void PrintMap(const map<const Value*, Value*> &M) {
702d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  for (map<const Value*, Value*>::const_iterator I = M.begin(), E = M.end();
712d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner       I != E; ++I) {
722d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner    cerr << " Fr: " << (void*)I->first << " " << I->first
732d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner         << " To: " << (void*)I->second << " " << I->second << endl;
742d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  }
752d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner}
762d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
772d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
785c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// RemapOperand - Use LocalMap and GlobalMap to convert references from one
795c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// module to another.  This is somewhat sophisticated in that it can
805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// automatically handle constant references correctly as well...
815c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
828d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattnerstatic Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
835c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner                           const map<const Value*, Value*> *GlobalMap = 0) {
845c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  map<const Value*,Value*>::const_iterator I = LocalMap.find(In);
855c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (I != LocalMap.end()) return I->second;
865c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
875c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (GlobalMap) {
885c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    I = GlobalMap->find(In);
895c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    if (I != GlobalMap->end()) return I->second;
905c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
915c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
928d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // Check to see if it's a constant that we are interesting in transforming...
93e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner  if (Constant *CPV = dyn_cast<Constant>(In)) {
948d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    if (!isa<DerivedType>(CPV->getType()))
958d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      return CPV;              // Simple constants stay identical...
968d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
97e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    Constant *Result = 0;
988d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
99e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    if (ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
1008d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      const vector<Use> &Ops = CPA->getValues();
101e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      vector<Constant*> Operands(Ops.size());
1028d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      for (unsigned i = 0; i < Ops.size(); ++i)
1038d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        Operands[i] =
104e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner          cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
105e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
106e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    } else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
1078d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      const vector<Use> &Ops = CPS->getValues();
108e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      vector<Constant*> Operands(Ops.size());
1098d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      for (unsigned i = 0; i < Ops.size(); ++i)
1108d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        Operands[i] =
111e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner          cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
112e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
113e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    } else if (isa<ConstantPointerNull>(CPV)) {
1148d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      Result = CPV;
115e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
1168d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
117e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Result = ConstantPointerRef::get(cast<GlobalValue>(V));
1188d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    } else {
1198d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      assert(0 && "Unknown type of derived type constant value!");
1208d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    }
1218d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
1228d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    // Cache the mapping in our local map structure...
1238d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    LocalMap.insert(make_pair(In, CPV));
1248d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    return Result;
1258d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  }
1262d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
1272d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  cerr << "XXX LocalMap: \n";
1282d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  PrintMap(LocalMap);
1292d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
1302d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  if (GlobalMap) {
1312d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner    cerr << "XXX GlobalMap: \n";
1322d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner    PrintMap(*GlobalMap);
1332d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  }
1342d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
1352d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  cerr << "Couldn't remap value: " << (void*)In << " " << In << endl;
1368d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  assert(0 && "Couldn't remap value!");
1378d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  return 0;
1385c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
1395c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1405c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1415c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// LinkGlobals - Loop through the global variables in the src module and merge
1425c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// them into the dest module...
1435c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
1445c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattnerstatic bool LinkGlobals(Module *Dest, const Module *Src,
1455c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner                        map<const Value*, Value*> &ValueMap, string *Err = 0) {
1465c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // We will need a module level symbol table if the src module has a module
1475c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // level symbol table...
1485c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0;
1495c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1505c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Loop over all of the globals in the src module, mapping them over as we go
1515c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
1525c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
1538d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    const GlobalVariable *SGV = *I;
1545c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    Value *V;
1555c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1565c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // If the global variable has a name, and that name is already in use in the
1575c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Dest module, make sure that the name is a compatible global variable...
1585c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    //
159e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner    if (SGV->hasExternalLinkage() && SGV->hasName() &&
160e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner	(V = ST->lookup(SGV->getType(), SGV->getName())) &&
161e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner	cast<GlobalVariable>(V)->hasExternalLinkage()) {
1625c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // The same named thing is a global variable, because the only two things
1635c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // that may be in a module level symbol table are Global Vars and Methods,
1645c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // and they both have distinct, nonoverlapping, possible types.
1655c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      //
1665c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      GlobalVariable *DGV = cast<GlobalVariable>(V);
1675c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1685c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Check to see if the two GV's have the same Const'ness...
1698d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      if (SGV->isConstant() != DGV->isConstant())
1705c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner        return Error(Err, "Global Variable Collision on '" +
1718d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner                     SGV->getType()->getDescription() + "':%" + SGV->getName() +
1725c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner                     " - Global variables differ in const'ness");
1735c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1748d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // Okay, everything is cool, remember the mapping...
1758d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      ValueMap.insert(make_pair(SGV, DGV));
1765c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    } else {
1775c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // No linking to be performed, simply create an identical version of the
1788d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // symbol over in the dest module... the initializer will be filled in
1798d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // later by LinkGlobalInits...
1808d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      //
1818d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      GlobalVariable *DGV =
1828d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        new GlobalVariable(SGV->getType()->getValueType(), SGV->isConstant(),
183e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner                           SGV->hasInternalLinkage(), 0, SGV->getName());
1845c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1855c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Add the new global to the dest module
1868d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      Dest->getGlobalList().push_back(DGV);
1875c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1885c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Make sure to remember this mapping...
1898d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      ValueMap.insert(make_pair(SGV, DGV));
1905c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
1915c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
1925c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
1935c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
1945c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1955c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1968d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// LinkGlobalInits - Update the initializers in the Dest module now that all
1978d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// globals that may be referenced are in Dest.
1988d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner//
1998d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattnerstatic bool LinkGlobalInits(Module *Dest, const Module *Src,
2008d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner                            map<const Value*, Value*> &ValueMap,
2018d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner                            string *Err = 0) {
2028d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
2038d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // Loop over all of the globals in the src module, mapping them over as we go
2048d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  //
2058d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
2068d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    const GlobalVariable *SGV = *I;
2078d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
2088d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    if (SGV->hasInitializer()) {      // Only process initialized GV's
2098d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // Figure out what the initializer looks like in the dest module...
210e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Constant *DInit =
211e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner        cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap));
2128d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
2138d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
214e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner      if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
215e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner	  DGV->hasExternalLinkage()) {
2168d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        if (DGV->getInitializer() != DInit)
2178d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner          return Error(Err, "Global Variable Collision on '" +
2188d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner                       SGV->getType()->getDescription() + "':%" +SGV->getName()+
2198d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner                       " - Global variables have different initializers");
2208d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      } else {
2218d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        // Copy the initializer over now...
2228d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        DGV->setInitializer(DInit);
2238d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      }
2248d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    }
2258d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  }
2268d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  return false;
2278d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner}
2285c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2295c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// LinkMethodProtos - Link the methods together between the two modules, without
2305c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// doing method bodies... this just adds external method prototypes to the Dest
2315c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// method...
2325c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
2335c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattnerstatic bool LinkMethodProtos(Module *Dest, const Module *Src,
2345c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner                             map<const Value*, Value*> &ValueMap,
2355c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner                             string *Err = 0) {
2365c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // We will need a module level symbol table if the src module has a module
2375c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // level symbol table...
2385c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0;
2395c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2405c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Loop over all of the methods in the src module, mapping them over as we go
2415c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
2425c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
2435c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    const Method *SM = *I;   // SrcMethod
2445c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    Value *V;
2455c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2465c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // If the method has a name, and that name is already in use in the
2475c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Dest module, make sure that the name is a compatible method...
2485c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    //
249e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner    if (SM->hasExternalLinkage() && SM->hasName() &&
250e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner	(V = ST->lookup(SM->getType(), SM->getName())) &&
251e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner	cast<Method>(V)->hasExternalLinkage()) {
2525c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // The same named thing is a Method, because the only two things
2535c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // that may be in a module level symbol table are Global Vars and Methods,
2545c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // and they both have distinct, nonoverlapping, possible types.
2555c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      //
2565c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      Method *DM = cast<Method>(V);   // DestMethod
2575c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2585c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Check to make sure the method is not defined in both modules...
2595c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      if (!SM->isExternal() && !DM->isExternal())
2605c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner        return Error(Err, "Method '" +
2615c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner                     SM->getMethodType()->getDescription() + "':\"" +
2625c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner                     SM->getName() + "\" - Method is already defined!");
2635c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2645c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Otherwise, just remember this mapping...
2655c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      ValueMap.insert(make_pair(SM, DM));
2665c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    } else {
2675c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Method does not already exist, simply insert an external method
2685c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // signature identical to SM into the dest module...
269e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner      Method *DM = new Method(SM->getMethodType(), SM->hasInternalLinkage(),
270e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner			      SM->getName());
2715c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2725c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Add the method signature to the dest module...
2735c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      Dest->getMethodList().push_back(DM);
2745c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2755c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // ... and remember this mapping...
2765c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      ValueMap.insert(make_pair(SM, DM));
2775c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
2785c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
2795c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
2805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
2815c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2825c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// LinkMethodBody - Copy the source method over into the dest method and fix up
2835c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// references to values.  At this point we know that Dest is an external method,
2845c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// and that Src is not.
2855c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
2865c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattnerstatic bool LinkMethodBody(Method *Dest, const Method *Src,
2875c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner                           const map<const Value*, Value*> &GlobalMap,
2885c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner                           string *Err = 0) {
2895c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  assert(Src && Dest && Dest->isExternal() && !Src->isExternal());
2905c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  map<const Value*, Value*> LocalMap;   // Map for method local values
2915c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2925c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Go through and convert method arguments over...
2935c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  for (Method::ArgumentListType::const_iterator
2945c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner         I = Src->getArgumentList().begin(),
2955c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner         E = Src->getArgumentList().end(); I != E; ++I) {
2965c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    const MethodArgument *SMA = *I;
2975c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2985c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Create the new method argument and add to the dest method...
2995c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    MethodArgument *DMA = new MethodArgument(SMA->getType(), SMA->getName());
3005c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    Dest->getArgumentList().push_back(DMA);
3015c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3025c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Add a mapping to our local map
3035c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    LocalMap.insert(make_pair(SMA, DMA));
3045c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
3055c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3065c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Loop over all of the basic blocks, copying the instructions over...
3075c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
3085c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  for (Method::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
3095c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    const BasicBlock *SBB = *I;
3105c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3115c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Create new basic block and add to mapping and the Dest method...
3125c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    BasicBlock *DBB = new BasicBlock(SBB->getName(), Dest);
3135c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    LocalMap.insert(make_pair(SBB, DBB));
3145c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3155c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Loop over all of the instructions in the src basic block, copying them
3165c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // over.  Note that this is broken in a strict sense because the cloned
3175c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // instructions will still be referencing values in the Src module, not
3185c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // the remapped values.  In our case, however, we will not get caught and
3195c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // so we can delay patching the values up until later...
3205c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    //
3215c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    for (BasicBlock::const_iterator II = SBB->begin(), IE = SBB->end();
3225c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner         II != IE; ++II) {
3235c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      const Instruction *SI = *II;
3245c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      Instruction *DI = SI->clone();
32543a6f2e332eb54796a2e85188aa89c0dfee03ef3Chris Lattner      DI->setName(SI->getName());
3265c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      DBB->getInstList().push_back(DI);
3275c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      LocalMap.insert(make_pair(SI, DI));
3285c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
3295c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
3305c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3315c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // At this point, all of the instructions and values of the method are now
3325c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // copied over.  The only problem is that they are still referencing values
3335c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // in the Source method as operands.  Loop through all of the operands of the
3345c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // methods and patch them up to point to the local versions...
3355c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
3365c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  for (Method::inst_iterator I = Dest->inst_begin(), E = Dest->inst_end();
3375c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner       I != E; ++I) {
3385c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    Instruction *Inst = *I;
3395c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3405c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    for (Instruction::op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
3415c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner         OI != OE; ++OI)
3425c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      *OI = RemapOperand(*OI, LocalMap, &GlobalMap);
3435c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
3445c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3455c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
3465c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
3475c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3485c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3495c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// LinkMethodBodies - Link in the method bodies that are defined in the source
3505c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// module into the DestModule.  This consists basically of copying the method
3515c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// over and fixing up references to values.
3525c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
3535c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattnerstatic bool LinkMethodBodies(Module *Dest, const Module *Src,
3545c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner                             map<const Value*, Value*> &ValueMap,
3555c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner                             string *Err = 0) {
3565c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3575c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Loop over all of the methods in the src module, mapping them over as we go
3585c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
3595c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
360c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner    const Method *SM = *I;                     // Source Method
361c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner    if (!SM->isExternal()) {                   // No body if method is external
362c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner      Method *DM = cast<Method>(ValueMap[SM]); // Destination method
363c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner
364c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner      // DM not external SM external?
365c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner      if (!DM->isExternal()) {
366c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner        if (Err)
367c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner          *Err = "Method '" + (SM->hasName() ? SM->getName() : string("")) +
368c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner                 "' body multiply defined!";
369c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner        return true;
370c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner      }
3715c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3725c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      if (LinkMethodBody(DM, SM, ValueMap, Err)) return true;
373c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner    }
3745c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
3755c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
3765c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
3775c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
37852f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
37952f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
38052f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// LinkModules - This function links two modules together, with the resulting
38152f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// left module modified to be the composite of the two input modules.  If an
38252f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
3835c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// the problem.  Upon failure, the Dest module could be in a modified state, and
3845c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// shouldn't be relied on to be consistent.
38552f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//
38652f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattnerbool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) {
3872c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
3882c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // LinkTypes - Go through the symbol table of the Src module and see if any
3892c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // types are named in the src module that are not named in the Dst module.
3902c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // Make sure there are no type name conflicts.
3912c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  //
3922c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  if (LinkTypes(Dest, Src, ErrorMsg)) return true;
3932c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
3945c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // ValueMap - Mapping of values from what they used to be in Src, to what they
3955c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // are now in Dest.
3965c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
3975c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  map<const Value*, Value*> ValueMap;
3985c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3998d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // Insert all of the globals in src into the Dest module... without
4008d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // initializers
4015c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true;
4025c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4038d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // Update the initializers in the Dest module now that all globals that may
4048d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // be referenced are in Dest.
4058d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  //
4068d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
4078d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
4085c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Link the methods together between the two modules, without doing method
4095c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // bodies... this just adds external method prototypes to the Dest method...
4105c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // We do this so that when we begin processing method bodies, all of the
4115c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // global values that may be referenced are available in our ValueMap.
4125c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
4135c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (LinkMethodProtos(Dest, Src, ValueMap, ErrorMsg)) return true;
4145c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4155c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Link in the method bodies that are defined in the source module into the
4165c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // DestModule.  This consists basically of copying the method over and fixing
4175c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // up references to values.
4185c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
4195c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (LinkMethodBodies(Dest, Src, ValueMap, ErrorMsg)) return true;
42052f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
42152f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner  return false;
42252f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner}
4239466f5113ba859677a28887c3c7143065e702019Vikram S. Adve
424