@@ -1078,29 +1078,41 @@ llvm::GlobalVariable::LinkageTypes
10781078CodeGenModule::getVTableLinkage (const CXXRecordDecl *RD) {
10791079 if (!RD->isExternallyVisible ())
10801080 return llvm::GlobalVariable::InternalLinkage;
1081-
1082- // We're at the end of the translation unit, so the current key
1083- // function is fully correct.
1084- const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction (RD);
1085- if (keyFunction && !RD->hasAttr <DLLImportAttr>()) {
1081+
1082+ // In windows, the linkage of vtable is not related to modules.
1083+ bool IsInNamedModule = !getTarget ().getCXXABI ().isMicrosoft () &&
1084+ RD->isInNamedModule ();
1085+ // If the CXXRecordDecl is not in a module unit, we need to get
1086+ // its key function. We're at the end of the translation unit, so the current
1087+ // key function is fully correct.
1088+ const CXXMethodDecl *keyFunction =
1089+ IsInNamedModule ? nullptr : Context.getCurrentKeyFunction (RD);
1090+ if (IsInNamedModule || (keyFunction && !RD->hasAttr <DLLImportAttr>())) {
10861091 // If this class has a key function, use that to determine the
10871092 // linkage of the vtable.
10881093 const FunctionDecl *def = nullptr ;
1089- if (keyFunction->hasBody (def))
1094+ if (keyFunction && keyFunction ->hasBody (def))
10901095 keyFunction = cast<CXXMethodDecl>(def);
10911096
1092- switch (keyFunction->getTemplateSpecializationKind ()) {
1093- case TSK_Undeclared:
1094- case TSK_ExplicitSpecialization:
1097+ bool IsExternalDefinition =
1098+ IsInNamedModule ? RD->shouldEmitInExternalSource () : !def;
1099+
1100+ TemplateSpecializationKind Kind =
1101+ IsInNamedModule ? RD->getTemplateSpecializationKind ()
1102+ : keyFunction->getTemplateSpecializationKind ();
1103+
1104+ switch (Kind) {
1105+ case TSK_Undeclared:
1106+ case TSK_ExplicitSpecialization:
10951107 assert (
1096- (def || CodeGenOpts.OptimizationLevel > 0 ||
1108+ (IsInNamedModule || def || CodeGenOpts.OptimizationLevel > 0 ||
10971109 CodeGenOpts.getDebugInfo () != llvm::codegenoptions::NoDebugInfo) &&
1098- " Shouldn't query vtable linkage without key function , "
1099- " optimizations, or debug info" );
1100- if (!def && CodeGenOpts.OptimizationLevel > 0 )
1110+ " Shouldn't query vtable linkage without the class in module units , "
1111+ " key function, optimizations, or debug info" );
1112+ if (IsExternalDefinition && CodeGenOpts.OptimizationLevel > 0 )
11011113 return llvm::GlobalVariable::AvailableExternallyLinkage;
11021114
1103- if (keyFunction->isInlined ())
1115+ if (keyFunction && keyFunction ->isInlined ())
11041116 return !Context.getLangOpts ().AppleKext
11051117 ? llvm::GlobalVariable::LinkOnceODRLinkage
11061118 : llvm::Function::InternalLinkage;
@@ -1119,7 +1131,7 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
11191131
11201132 case TSK_ExplicitInstantiationDeclaration:
11211133 llvm_unreachable (" Should not have been asked to emit this" );
1122- }
1134+ }
11231135 }
11241136
11251137 // -fapple-kext mode does not support weak linkage, so we must use
@@ -1213,22 +1225,20 @@ bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
12131225 TSK == TSK_ExplicitInstantiationDefinition)
12141226 return false ;
12151227
1228+ // Otherwise, if the class is attached to a module, the tables are uniquely
1229+ // emitted in the object for the module unit in which it is defined.
1230+ if (RD->isInNamedModule ())
1231+ return RD->shouldEmitInExternalSource ();
1232+
12161233 // Otherwise, if the class doesn't have a key function (possibly
12171234 // anymore), the vtable must be defined here.
12181235 const CXXMethodDecl *keyFunction = CGM.getContext ().getCurrentKeyFunction (RD);
12191236 if (!keyFunction)
12201237 return false ;
12211238
1222- const FunctionDecl *Def;
12231239 // Otherwise, if we don't have a definition of the key function, the
12241240 // vtable must be defined somewhere else.
1225- if (!keyFunction->hasBody (Def))
1226- return true ;
1227-
1228- assert (Def && " The body of the key function is not assigned to Def?" );
1229- // If the non-inline key function comes from another module unit, the vtable
1230- // must be defined there.
1231- return Def->isInAnotherModuleUnit () && !Def->isInlineSpecified ();
1241+ return !keyFunction->hasBody ();
12321242}
12331243
12341244// / Given that we're currently at the end of the translation unit, and
0 commit comments