- if (DF && DF->getType() != SF->getType()) {
- if (DF->isDeclaration() && !SF->isDeclaration()) {
- // We have a definition of the same name but different type in the
- // source module. Copy the prototype to the destination and replace
- // uses of the destination's prototype with the new prototype.
- Function *NewDF = new Function(SF->getFunctionType(), SF->getLinkage(),
- SF->getName(), Dest);
- CopyGVAttributes(NewDF, SF);
-
- // Any uses of DF need to change to NewDF, with cast
- DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DF->getType()));
-
- // DF will conflict with NewDF because they both had the same. We must
- // erase this now so ForceRenaming doesn't assert because DF might
- // not have internal linkage.
- DF->eraseFromParent();
-
- // If the symbol table 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());
-
- // Remember this mapping so uses in the source module get remapped
- // later by RemapOperand.
- ValueMap[SF] = NewDF;
- } else if (SF->isDeclaration()) {
- // We have two functions of the same name but different type and the
- // source is a declaration while the destination is not. Any use of
- // the source must be mapped to the destination, with a cast.
- ValueMap[SF] = ConstantExpr::getBitCast(DF, SF->getType());
- } else {
- // We have two functions of the same name but different types and they
- // are both definitions. This is an error.
- return Error(Err, "Function '" + DF->getName() + "' defined as both '" +
- ToStr(SF->getFunctionType(), Src) + "' and '" +
- ToStr(DF->getFunctionType(), Dest) + "'");
- }
- } else 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);
- CopyGVAttributes(NewDF, SF);
-
- // 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->isDeclaration()) {
- // If SF is a declaration or if both SF & DF are declarations, just link
- // the declarations, we aren't adding anything.
- if (SF->hasDLLImportLinkage()) {
- if (DF->isDeclaration()) {
- ValueMap.insert(std::make_pair(SF, DF));
- DF->setLinkage(SF->getLinkage());
- }
- } else {
- ValueMap.insert(std::make_pair(SF, DF));
- }
- } else if (DF->isDeclaration() && !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 identically in both modules!!
- return Error(Err, "Function '" +
- ToStr(SF->getFunctionType(), Src) + "':\"" +
- SF->getName() + "\" - Function is already defined!");
- } else {
- assert(0 && "Unknown linkage configuration found!");