@@ -1081,29 +1081,41 @@ llvm::GlobalVariable::LinkageTypes
10811081CodeGenModule::getVTableLinkage (const CXXRecordDecl *RD) {
10821082 if (!RD->isExternallyVisible ())
10831083 return llvm::GlobalVariable::InternalLinkage;
1084-
1085- // We're at the end of the translation unit, so the current key
1086- // function is fully correct.
1087- const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction (RD);
1088- if (keyFunction && !RD->hasAttr <DLLImportAttr>()) {
1084+
1085+ // In windows, the linkage of vtable is not related to modules.
1086+ bool IsInNamedModule = !getTarget ().getCXXABI ().isMicrosoft () &&
1087+ RD->isInNamedModule ();
1088+ // If the CXXRecordDecl is not in a module unit, we need to get
1089+ // its key function. We're at the end of the translation unit, so the current
1090+ // key function is fully correct.
1091+ const CXXMethodDecl *keyFunction =
1092+ IsInNamedModule ? nullptr : Context.getCurrentKeyFunction (RD);
1093+ if (IsInNamedModule || (keyFunction && !RD->hasAttr <DLLImportAttr>())) {
10891094 // If this class has a key function, use that to determine the
10901095 // linkage of the vtable.
10911096 const FunctionDecl *def = nullptr ;
1092- if (keyFunction->hasBody (def))
1097+ if (keyFunction && keyFunction ->hasBody (def))
10931098 keyFunction = cast<CXXMethodDecl>(def);
10941099
1095- switch (keyFunction->getTemplateSpecializationKind ()) {
1096- case TSK_Undeclared:
1097- case TSK_ExplicitSpecialization:
1100+ bool IsExternalDefinition =
1101+ IsInNamedModule ? RD->shouldEmitInExternalSource () : !def;
1102+
1103+ TemplateSpecializationKind Kind =
1104+ IsInNamedModule ? RD->getTemplateSpecializationKind ()
1105+ : keyFunction->getTemplateSpecializationKind ();
1106+
1107+ switch (Kind) {
1108+ case TSK_Undeclared:
1109+ case TSK_ExplicitSpecialization:
10981110 assert (
1099- (def || CodeGenOpts.OptimizationLevel > 0 ||
1111+ (IsInNamedModule || def || CodeGenOpts.OptimizationLevel > 0 ||
11001112 CodeGenOpts.getDebugInfo () != llvm::codegenoptions::NoDebugInfo) &&
1101- " Shouldn't query vtable linkage without key function , "
1102- " optimizations, or debug info" );
1103- if (!def && CodeGenOpts.OptimizationLevel > 0 )
1113+ " Shouldn't query vtable linkage without the class in module units , "
1114+ " key function, optimizations, or debug info" );
1115+ if (IsExternalDefinition && CodeGenOpts.OptimizationLevel > 0 )
11041116 return llvm::GlobalVariable::AvailableExternallyLinkage;
11051117
1106- if (keyFunction->isInlined ())
1118+ if (keyFunction && keyFunction ->isInlined ())
11071119 return !Context.getLangOpts ().AppleKext
11081120 ? llvm::GlobalVariable::LinkOnceODRLinkage
11091121 : llvm::Function::InternalLinkage;
@@ -1122,7 +1134,7 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
11221134
11231135 case TSK_ExplicitInstantiationDeclaration:
11241136 llvm_unreachable (" Should not have been asked to emit this" );
1125- }
1137+ }
11261138 }
11271139
11281140 // -fapple-kext mode does not support weak linkage, so we must use
@@ -1216,22 +1228,20 @@ bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
12161228 TSK == TSK_ExplicitInstantiationDefinition)
12171229 return false ;
12181230
1231+ // Otherwise, if the class is attached to a module, the tables are uniquely
1232+ // emitted in the object for the module unit in which it is defined.
1233+ if (RD->isInNamedModule ())
1234+ return RD->shouldEmitInExternalSource ();
1235+
12191236 // Otherwise, if the class doesn't have a key function (possibly
12201237 // anymore), the vtable must be defined here.
12211238 const CXXMethodDecl *keyFunction = CGM.getContext ().getCurrentKeyFunction (RD);
12221239 if (!keyFunction)
12231240 return false ;
12241241
1225- const FunctionDecl *Def;
12261242 // Otherwise, if we don't have a definition of the key function, the
12271243 // vtable must be defined somewhere else.
1228- if (!keyFunction->hasBody (Def))
1229- return true ;
1230-
1231- assert (Def && " The body of the key function is not assigned to Def?" );
1232- // If the non-inline key function comes from another module unit, the vtable
1233- // must be defined there.
1234- return Def->isInAnotherModuleUnit () && !Def->isInlineSpecified ();
1244+ return !keyFunction->hasBody ();
12351245}
12361246
12371247// / Given that we're currently at the end of the translation unit, and
0 commit comments