-// LinkFunctionProtos - Link the functions together between the two modules,
-// without doing function bodies... this just adds external function prototypes
-// to the Dest function...
-//
-static bool LinkFunctionProtos(Module *Dest, const Module *Src,
- std::map<const Value*, Value*> &ValueMap,
- std::map<std::string, GlobalValue*> &GlobalsByName,
- std::string *Err) {
- SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable();
-
- // Loop over all of the functions in the src module, mapping them over as we
- // go
- for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
- const Function *SF = I; // SrcFunction
- Function *DF = 0;
- if (SF->hasName() && !SF->hasInternalLinkage()) {
- // Check to see if may have to link the function.
- if (!(DF = Dest->getFunction(SF->getName(), SF->getFunctionType()))) {
- std::map<std::string, GlobalValue*>::iterator EF =
- GlobalsByName.find(SF->getName());
- if (EF != GlobalsByName.end())
- DF = dyn_cast<Function>(EF->second);
- if (DF && RecursiveResolveTypes(SF->getType(), DF->getType(), ST, ""))
- DF = 0; // FIXME: gross.
- }
- }
-
- if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
- // Function does not already exist, simply insert an function signature
- // identical to SF into the dest module...
- Function *NewDF = new Function(SF->getFunctionType(), SF->getLinkage(),
- SF->getName(), Dest);
- NewDF->setCallingConv(SF->getCallingConv());
-
- // If the LLVM runtime renamed the function, but it is an externally
- // visible symbol, DF must be an existing function with internal linkage.
- // Rename it.
- if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage())
- ForceRenaming(NewDF, SF->getName());
-
- // ... and remember this mapping...
- ValueMap.insert(std::make_pair(SF, NewDF));
- } else if (SF->isExternal()) {
- // If SF is external or if both SF & DF are external.. Just link the
- // external functions, we aren't adding anything.
- if (SF->hasDLLImportLinkage()) {
- if (DF->isExternal()) {
- ValueMap.insert(std::make_pair(SF, DF));
- DF->setLinkage(SF->getLinkage());
- }
- } else {
- ValueMap.insert(std::make_pair(SF, DF));
- }
- } else if (DF->isExternal() && !DF->hasDLLImportLinkage()) {
- // If DF is external but SF is not...
- // Link the external functions, update linkage qualifiers
- ValueMap.insert(std::make_pair(SF, DF));
- DF->setLinkage(SF->getLinkage());
- } else if (SF->hasWeakLinkage() || SF->hasLinkOnceLinkage()) {
- // At this point we know that DF has LinkOnce, Weak, or External* linkage.
- ValueMap.insert(std::make_pair(SF, DF));
-
- // Linkonce+Weak = Weak
- // *+External Weak = *
- if ((DF->hasLinkOnceLinkage() && SF->hasWeakLinkage()) ||
- DF->hasExternalWeakLinkage())
- DF->setLinkage(SF->getLinkage());
-
-
- } else if (DF->hasWeakLinkage() || DF->hasLinkOnceLinkage()) {
- // At this point we know that SF has LinkOnce or External* linkage.
- ValueMap.insert(std::make_pair(SF, DF));
- if (!SF->hasLinkOnceLinkage() && !SF->hasExternalWeakLinkage())
- // Don't inherit linkonce & external weak linkage
- DF->setLinkage(SF->getLinkage());
- } else if (SF->getLinkage() != DF->getLinkage()) {
- return Error(Err, "Functions named '" + SF->getName() +
- "' have different linkage specifiers!");
- } else if (SF->hasExternalLinkage()) {
- // The function is defined in both modules!!
- return Error(Err, "Function '" +
- ToStr(SF->getFunctionType(), Src) + "':\"" +
- SF->getName() + "\" - Function is already defined!");
- } else {
- assert(0 && "Unknown linkage configuration found!");