- CurModule.CurrentModule->getFunctionList().remove(Fn);
- CurModule.CurrentModule->getFunctionList().push_back(Fn);
- } else if (!FunctionName.empty() && // Merge with an earlier prototype?
- (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
- if (Fn->getFunctionType() != FT ) {
- // The existing function doesn't have the same type. Previously this was
- // permitted because the symbol tables had "type planes" and names were
- // distinct within a type plane. After PR411 was fixed, this is no
- // longer the case. To resolve this we must rename this function.
- // However, renaming it can cause problems if its linkage is external
- // because it could cause a link failure. We warn about this.
- std::string NewName = makeNameUnique(FunctionName);
- warning("Renaming function '" + FunctionName + "' as '" + NewName +
- "' may cause linkage errors");
-
- Fn = new Function(FT, GlobalValue::ExternalLinkage, NewName,
- CurModule.CurrentModule);
- InsertValue(Fn, CurModule.Values);
- RenameMapKey Key = std::make_pair(FunctionName,PFT);
- CurModule.RenameMap[Key] = NewName;
- } else if (Fn->hasInternalLinkage()) {
- // The function we are creating conflicts in name with another function
- // that has internal linkage. We'll rename that one quietly to get rid
- // of the conflict.
- Fn->setName(makeNameUnique(Fn->getName()));
- RenameMapKey Key = std::make_pair(FunctionName,PFT);
- CurModule.RenameMap[Key] = Fn->getName();
-
- Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
- CurModule.CurrentModule);
-
- InsertValue(Fn, CurModule.Values);
- } else if (CurFun.Linkage == GlobalValue::InternalLinkage) {
- // The function we are creating has internal linkage and conflicts with
- // another function of the same name. We'll just rename this one
- // quietly because its internal linkage can't conflict with anything
- // else.
- std::string NewName = makeNameUnique(FunctionName);
- Fn = new Function(FT, GlobalValue::ExternalLinkage, NewName,
- CurModule.CurrentModule);
- InsertValue(Fn, CurModule.Values);
- RenameMapKey Key = std::make_pair(FunctionName,PFT);
- CurModule.RenameMap[Key] = NewName;
+ M->getFunctionList().remove(Fn);
+ M->getFunctionList().push_back(Fn);
+ } else if (!FunctionName.empty()) {
+ GlobalValue *Conflict = M->getFunction(FunctionName);
+ if (!Conflict)
+ Conflict = M->getNamedGlobal(FunctionName);
+ if (Conflict && PFT == Conflict->getType()) {
+ if (!CurFun.isDeclare && !Conflict->isDeclaration()) {
+ // We have two function definitions that conflict, same type, same
+ // name. This wasn't allowed in 1.9, its not allowed here either
+ error("Redefinition of function '" + FunctionName + "' of type '" +
+ PFT->getDescription() + "'");
+
+ } else {
+ // If they are not both definitions, then just use the function we
+ // found since the types are the same.
+ Fn = cast<Function>(Conflict);
+
+ // Make sure to strip off any argument names so we can't get
+ // conflicts.
+ if (Fn->isDeclaration())
+ for (Function::arg_iterator AI = Fn->arg_begin(),
+ AE = Fn->arg_end(); AI != AE; ++AI)
+ AI->setName("");
+ }
+ } else if (Conflict) {
+ // We have two globals with the same name and different types.
+ // Previously, this was permitted because the symbol table had
+ // "type planes" and names only needed to be distinct within a
+ // type plane. After PR411 was fixed, this is no loner the case.
+ // To resolve this we must rename one of the two.
+ if (Conflict->hasInternalLinkage()) {
+ // We can safely renamed the Conflict.
+ Conflict->setName(makeNameUnique(Conflict->getName()));
+ RenameMapKey Key = std::make_pair(FunctionName,Conflict->getType());
+ CurModule.RenameMap[Key] = Conflict->getName();
+ Fn = new Function(FT, CurFun.Linkage, FunctionName, M);
+ InsertValue(Fn, CurModule.Values);
+ } else if (CurFun.Linkage == GlobalValue::InternalLinkage) {
+ // We can safely rename the function we're defining
+ std::string NewName = makeNameUnique(FunctionName);
+ Fn = new Function(FT, CurFun.Linkage, NewName, M);
+ InsertValue(Fn, CurModule.Values);
+ RenameMapKey Key = std::make_pair(FunctionName,PFT);
+ CurModule.RenameMap[Key] = NewName;
+ } else {
+ // We can't quietly rename either of these things, but we must
+ // rename one of them. Generate a warning about the renaming and
+ // elect to rename the thing we're now defining.
+ std::string NewName = makeNameUnique(FunctionName);
+ warning("Renaming function '" + FunctionName + "' as '" + NewName +
+ "' may cause linkage errors");
+ Fn = new Function(FT, CurFun.Linkage, NewName, M);
+ InsertValue(Fn, CurModule.Values);
+ RenameMapKey Key = std::make_pair(FunctionName,PFT);
+ CurModule.RenameMap[Key] = NewName;
+ }