+/// Loop through the global variables in the src module and merge them into the
+/// dest module.
+static GlobalVariable *copyGlobalVariableProto(TypeMapTy &TypeMap, Module &DstM,
+ const GlobalVariable *SGVar) {
+ // No linking to be performed or linking from the source: simply create an
+ // identical version of the symbol over in the dest module... the
+ // initializer will be filled in later by LinkGlobalInits.
+ GlobalVariable *NewDGV = new GlobalVariable(
+ DstM, TypeMap.get(SGVar->getType()->getElementType()),
+ SGVar->isConstant(), SGVar->getLinkage(), /*init*/ nullptr,
+ SGVar->getName(), /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
+ SGVar->getType()->getAddressSpace());
+
+ return NewDGV;
+}
+
+/// Link the function in the source module into the destination module if
+/// needed, setting up mapping information.
+static Function *copyFunctionProto(TypeMapTy &TypeMap, Module &DstM,
+ const Function *SF) {
+ // If there is no linkage to be performed or we are linking from the source,
+ // bring SF over.
+ return Function::Create(TypeMap.get(SF->getFunctionType()), SF->getLinkage(),
+ SF->getName(), &DstM);
+}
+
+/// Set up prototypes for any aliases that come over from the source module.
+static GlobalAlias *copyGlobalAliasProto(TypeMapTy &TypeMap, Module &DstM,
+ const GlobalAlias *SGA) {
+ // If there is no linkage to be performed or we're linking from the source,
+ // bring over SGA.
+ auto *PTy = cast<PointerType>(TypeMap.get(SGA->getType()));
+ return GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
+ SGA->getLinkage(), SGA->getName(), &DstM);
+}
+
+static GlobalValue *copyGlobalValueProto(TypeMapTy &TypeMap, Module &DstM,
+ const GlobalValue *SGV) {
+ GlobalValue *NewGV;
+ if (auto *SGVar = dyn_cast<GlobalVariable>(SGV))
+ NewGV = copyGlobalVariableProto(TypeMap, DstM, SGVar);
+ else if (auto *SF = dyn_cast<Function>(SGV))
+ NewGV = copyFunctionProto(TypeMap, DstM, SF);
+ else
+ NewGV = copyGlobalAliasProto(TypeMap, DstM, cast<GlobalAlias>(SGV));
+ copyGVAttributes(NewGV, SGV);
+ return NewGV;
+}
+