@@ -1079,33 +1079,38 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
10791079 if (!RD->isExternallyVisible ())
10801080 return llvm::GlobalVariable::InternalLinkage;
10811081
1082- // V-tables for non-template classes with an owning module are always
1083- // uniquely emitted in that module.
1084- if (RD->isInNamedModule ())
1085- return llvm::GlobalVariable::ExternalLinkage;
1086-
1087- // We're at the end of the translation unit, so the current key
1088- // function is fully correct.
1089- const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction (RD);
1090- if (keyFunction && !RD->hasAttr <DLLImportAttr>()) {
1082+ bool IsInNamedModule = RD->isInNamedModule ();
1083+ // If the CXXRecordDecl are not in a module unit, we need to get
1084+ // its key function. We're at the end of the translation unit, so the current
1085+ // key function is fully correct.
1086+ const CXXMethodDecl *keyFunction =
1087+ IsInNamedModule ? nullptr : Context.getCurrentKeyFunction (RD);
1088+ if (IsInNamedModule || (keyFunction && !RD->hasAttr <DLLImportAttr>())) {
10911089 // If this class has a key function, use that to determine the
10921090 // linkage of the vtable.
10931091 const FunctionDecl *def = nullptr ;
1094- if (keyFunction->hasBody (def))
1092+ if (keyFunction && keyFunction ->hasBody (def))
10951093 keyFunction = cast<CXXMethodDecl>(def);
10961094
1097- switch (keyFunction->getTemplateSpecializationKind ()) {
1098- case TSK_Undeclared:
1099- case TSK_ExplicitSpecialization:
1095+ bool IsExternalDefinition =
1096+ IsInNamedModule ? RD->shouldEmitInExternalSource () : !def;
1097+
1098+ TemplateSpecializationKind Kind =
1099+ IsInNamedModule ? RD->getTemplateSpecializationKind ()
1100+ : keyFunction->getTemplateSpecializationKind ();
1101+
1102+ switch (Kind) {
1103+ case TSK_Undeclared:
1104+ case TSK_ExplicitSpecialization:
11001105 assert (
1101- (def || CodeGenOpts.OptimizationLevel > 0 ||
1106+ (IsInNamedModule || def || CodeGenOpts.OptimizationLevel > 0 ||
11021107 CodeGenOpts.getDebugInfo () != llvm::codegenoptions::NoDebugInfo) &&
1103- " Shouldn't query vtable linkage without key function , "
1104- " optimizations, or debug info" );
1105- if (!def && CodeGenOpts.OptimizationLevel > 0 )
1108+ " Shouldn't query vtable linkage without the class in module units , "
1109+ " key function, optimizations, or debug info" );
1110+ if (IsExternalDefinition && CodeGenOpts.OptimizationLevel > 0 )
11061111 return llvm::GlobalVariable::AvailableExternallyLinkage;
11071112
1108- if (keyFunction->isInlined ())
1113+ if (keyFunction && keyFunction ->isInlined ())
11091114 return !Context.getLangOpts ().AppleKext
11101115 ? llvm::GlobalVariable::LinkOnceODRLinkage
11111116 : llvm::Function::InternalLinkage;
@@ -1124,7 +1129,7 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
11241129
11251130 case TSK_ExplicitInstantiationDeclaration:
11261131 llvm_unreachable (" Should not have been asked to emit this" );
1127- }
1132+ }
11281133 }
11291134
11301135 // -fapple-kext mode does not support weak linkage, so we must use
0 commit comments