@@ -1078,29 +1078,41 @@ llvm::GlobalVariable::LinkageTypes
1078
1078
CodeGenModule::getVTableLinkage (const CXXRecordDecl *RD) {
1079
1079
if (!RD->isExternallyVisible ())
1080
1080
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>())) {
1086
1091
// If this class has a key function, use that to determine the
1087
1092
// linkage of the vtable.
1088
1093
const FunctionDecl *def = nullptr ;
1089
- if (keyFunction->hasBody (def))
1094
+ if (keyFunction && keyFunction ->hasBody (def))
1090
1095
keyFunction = cast<CXXMethodDecl>(def);
1091
1096
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:
1095
1107
assert (
1096
- (def || CodeGenOpts.OptimizationLevel > 0 ||
1108
+ (IsInNamedModule || def || CodeGenOpts.OptimizationLevel > 0 ||
1097
1109
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 )
1101
1113
return llvm::GlobalVariable::AvailableExternallyLinkage;
1102
1114
1103
- if (keyFunction->isInlined ())
1115
+ if (keyFunction && keyFunction ->isInlined ())
1104
1116
return !Context.getLangOpts ().AppleKext
1105
1117
? llvm::GlobalVariable::LinkOnceODRLinkage
1106
1118
: llvm::Function::InternalLinkage;
@@ -1119,7 +1131,7 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
1119
1131
1120
1132
case TSK_ExplicitInstantiationDeclaration:
1121
1133
llvm_unreachable (" Should not have been asked to emit this" );
1122
- }
1134
+ }
1123
1135
}
1124
1136
1125
1137
// -fapple-kext mode does not support weak linkage, so we must use
@@ -1213,22 +1225,20 @@ bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
1213
1225
TSK == TSK_ExplicitInstantiationDefinition)
1214
1226
return false ;
1215
1227
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
+
1216
1233
// Otherwise, if the class doesn't have a key function (possibly
1217
1234
// anymore), the vtable must be defined here.
1218
1235
const CXXMethodDecl *keyFunction = CGM.getContext ().getCurrentKeyFunction (RD);
1219
1236
if (!keyFunction)
1220
1237
return false ;
1221
1238
1222
- const FunctionDecl *Def;
1223
1239
// Otherwise, if we don't have a definition of the key function, the
1224
1240
// 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 ();
1232
1242
}
1233
1243
1234
1244
// / Given that we're currently at the end of the translation unit, and
0 commit comments