@@ -1079,33 +1079,38 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
1079
1079
if (!RD->isExternallyVisible ())
1080
1080
return llvm::GlobalVariable::InternalLinkage;
1081
1081
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>())) {
1091
1089
// If this class has a key function, use that to determine the
1092
1090
// linkage of the vtable.
1093
1091
const FunctionDecl *def = nullptr ;
1094
- if (keyFunction->hasBody (def))
1092
+ if (keyFunction && keyFunction ->hasBody (def))
1095
1093
keyFunction = cast<CXXMethodDecl>(def);
1096
1094
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:
1100
1105
assert (
1101
- (def || CodeGenOpts.OptimizationLevel > 0 ||
1106
+ (IsInNamedModule || def || CodeGenOpts.OptimizationLevel > 0 ||
1102
1107
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 )
1106
1111
return llvm::GlobalVariable::AvailableExternallyLinkage;
1107
1112
1108
- if (keyFunction->isInlined ())
1113
+ if (keyFunction && keyFunction ->isInlined ())
1109
1114
return !Context.getLangOpts ().AppleKext
1110
1115
? llvm::GlobalVariable::LinkOnceODRLinkage
1111
1116
: llvm::Function::InternalLinkage;
@@ -1124,7 +1129,7 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
1124
1129
1125
1130
case TSK_ExplicitInstantiationDeclaration:
1126
1131
llvm_unreachable (" Should not have been asked to emit this" );
1127
- }
1132
+ }
1128
1133
}
1129
1134
1130
1135
// -fapple-kext mode does not support weak linkage, so we must use
0 commit comments