@@ -1079,38 +1079,28 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
1079
1079
if (!RD->isExternallyVisible ())
1080
1080
return llvm::GlobalVariable::InternalLinkage;
1081
1081
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>())) {
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>()) {
1089
1086
// If this class has a key function, use that to determine the
1090
1087
// linkage of the vtable.
1091
1088
const FunctionDecl *def = nullptr ;
1092
- if (keyFunction && keyFunction ->hasBody (def))
1089
+ if (keyFunction->hasBody (def))
1093
1090
keyFunction = cast<CXXMethodDecl>(def);
1094
1091
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:
1092
+ switch (keyFunction->getTemplateSpecializationKind ()) {
1093
+ case TSK_Undeclared:
1094
+ case TSK_ExplicitSpecialization:
1105
1095
assert (
1106
- (IsInNamedModule || def || CodeGenOpts.OptimizationLevel > 0 ||
1096
+ (def || CodeGenOpts.OptimizationLevel > 0 ||
1107
1097
CodeGenOpts.getDebugInfo () != llvm::codegenoptions::NoDebugInfo) &&
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 )
1098
+ " Shouldn't query vtable linkage without key function , "
1099
+ " optimizations, or debug info" );
1100
+ if (!def && CodeGenOpts.OptimizationLevel > 0 )
1111
1101
return llvm::GlobalVariable::AvailableExternallyLinkage;
1112
1102
1113
- if (keyFunction && keyFunction ->isInlined ())
1103
+ if (keyFunction->isInlined ())
1114
1104
return !Context.getLangOpts ().AppleKext
1115
1105
? llvm::GlobalVariable::LinkOnceODRLinkage
1116
1106
: llvm::Function::InternalLinkage;
@@ -1129,7 +1119,7 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
1129
1119
1130
1120
case TSK_ExplicitInstantiationDeclaration:
1131
1121
llvm_unreachable (" Should not have been asked to emit this" );
1132
- }
1122
+ }
1133
1123
}
1134
1124
1135
1125
// -fapple-kext mode does not support weak linkage, so we must use
@@ -1223,21 +1213,6 @@ bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
1223
1213
TSK == TSK_ExplicitInstantiationDefinition)
1224
1214
return false ;
1225
1215
1226
- // Itanium C++ ABI [5.2.3]:
1227
- // Virtual tables for dynamic classes are emitted as follows:
1228
- //
1229
- // - If the class is templated, the tables are emitted in every object that
1230
- // references any of them.
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
- // - Otherwise, if the class has a key function (see below), the tables are
1234
- // emitted in the object for the translation unit containing the definition of
1235
- // the key function. This is unique if the key function is not inline.
1236
- // - Otherwise, the tables are emitted in every object that references any of
1237
- // them.
1238
- if (RD->isInNamedModule ())
1239
- return RD->shouldEmitInExternalSource ();
1240
-
1241
1216
// Otherwise, if the class doesn't have a key function (possibly
1242
1217
// anymore), the vtable must be defined here.
1243
1218
const CXXMethodDecl *keyFunction = CGM.getContext ().getCurrentKeyFunction (RD);
@@ -1247,7 +1222,13 @@ bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
1247
1222
const FunctionDecl *Def;
1248
1223
// Otherwise, if we don't have a definition of the key function, the
1249
1224
// vtable must be defined somewhere else.
1250
- return !keyFunction->hasBody (Def);
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 ();
1251
1232
}
1252
1233
1253
1234
// / Given that we're currently at the end of the translation unit, and
0 commit comments