@@ -573,6 +573,44 @@ static bool isSingleLineLanguageLinkage(const Decl &D) {
573
573
return false ;
574
574
}
575
575
576
+ static bool isExportedFromModuleIntefaceUnit (const NamedDecl *D) {
577
+ switch (D->getModuleOwnershipKind ()) {
578
+ case Decl::ModuleOwnershipKind::Unowned:
579
+ case Decl::ModuleOwnershipKind::ModulePrivate:
580
+ return false ;
581
+ case Decl::ModuleOwnershipKind::Visible:
582
+ case Decl::ModuleOwnershipKind::VisibleWhenImported:
583
+ if (auto *M = D->getOwningModule ())
584
+ return M->Kind == Module::ModuleInterfaceUnit;
585
+ }
586
+ llvm_unreachable (" unexpected module ownership kind" );
587
+ }
588
+
589
+ static LinkageInfo getInternalLinkageFor (const NamedDecl *D) {
590
+ // Internal linkage declarations within a module interface unit are modeled
591
+ // as "module-internal linkage", which means that they have internal linkage
592
+ // formally but can be indirectly accessed from outside the module via inline
593
+ // functions and templates defined within the module.
594
+ if (auto *M = D->getOwningModule ())
595
+ if (M->Kind == Module::ModuleInterfaceUnit)
596
+ return LinkageInfo (ModuleInternalLinkage, DefaultVisibility, false );
597
+
598
+ return LinkageInfo::internal ();
599
+ }
600
+
601
+ static LinkageInfo getExternalLinkageFor (const NamedDecl *D) {
602
+ // C++ Modules TS [basic.link]/6.8:
603
+ // - A name declared at namespace scope that does not have internal linkage
604
+ // by the previous rules and that is introduced by a non-exported
605
+ // declaration has module linkage.
606
+ if (auto *M = D->getOwningModule ())
607
+ if (M->Kind == Module::ModuleInterfaceUnit)
608
+ if (!isExportedFromModuleIntefaceUnit (D))
609
+ return LinkageInfo (ModuleLinkage, DefaultVisibility, false );
610
+
611
+ return LinkageInfo::external ();
612
+ }
613
+
576
614
static LinkageInfo getLVForNamespaceScopeDecl (const NamedDecl *D,
577
615
LVComputationKind computation) {
578
616
assert (D->getDeclContext ()->getRedeclContext ()->isFileContext () &&
@@ -588,24 +626,26 @@ static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
588
626
if (const auto *Var = dyn_cast<VarDecl>(D)) {
589
627
// Explicitly declared static.
590
628
if (Var->getStorageClass () == SC_Static)
591
- return LinkageInfo::internal ( );
629
+ return getInternalLinkageFor (Var );
592
630
593
631
// - a non-inline, non-volatile object or reference that is explicitly
594
632
// declared const or constexpr and neither explicitly declared extern
595
633
// nor previously declared to have external linkage; or (there is no
596
634
// equivalent in C99)
635
+ // The C++ modules TS adds "non-exported" to this list.
597
636
if (Context.getLangOpts ().CPlusPlus &&
598
637
Var->getType ().isConstQualified () &&
599
638
!Var->getType ().isVolatileQualified () &&
600
- !Var->isInline ()) {
639
+ !Var->isInline () &&
640
+ !isExportedFromModuleIntefaceUnit (Var)) {
601
641
const VarDecl *PrevVar = Var->getPreviousDecl ();
602
642
if (PrevVar)
603
643
return getLVForDecl (PrevVar, computation);
604
644
605
645
if (Var->getStorageClass () != SC_Extern &&
606
646
Var->getStorageClass () != SC_PrivateExtern &&
607
647
!isSingleLineLanguageLinkage (*Var))
608
- return LinkageInfo::internal ( );
648
+ return getInternalLinkageFor (Var );
609
649
}
610
650
611
651
for (const VarDecl *PrevVar = Var->getPreviousDecl (); PrevVar;
@@ -615,7 +655,7 @@ static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
615
655
return PrevVar->getLinkageAndVisibility ();
616
656
// Explicitly declared static.
617
657
if (PrevVar->getStorageClass () == SC_Static)
618
- return LinkageInfo::internal ( );
658
+ return getInternalLinkageFor (Var );
619
659
}
620
660
} else if (const FunctionDecl *Function = D->getAsFunction ()) {
621
661
// C++ [temp]p4:
@@ -624,7 +664,7 @@ static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
624
664
625
665
// Explicitly declared static.
626
666
if (Function->getCanonicalDecl ()->getStorageClass () == SC_Static)
627
- return LinkageInfo (InternalLinkage, DefaultVisibility, false );
667
+ return getInternalLinkageFor (Function );
628
668
} else if (const auto *IFD = dyn_cast<IndirectFieldDecl>(D)) {
629
669
// - a data member of an anonymous union.
630
670
const VarDecl *VD = IFD->getVarDecl ();
@@ -637,7 +677,12 @@ static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
637
677
const auto *Var = dyn_cast<VarDecl>(D);
638
678
const auto *Func = dyn_cast<FunctionDecl>(D);
639
679
// FIXME: In C++11 onwards, anonymous namespaces should give decls
640
- // within them internal linkage, not unique external linkage.
680
+ // within them (including those inside extern "C" contexts) internal
681
+ // linkage, not unique external linkage:
682
+ //
683
+ // C++11 [basic.link]p4:
684
+ // An unnamed namespace or a namespace declared directly or indirectly
685
+ // within an unnamed namespace has internal linkage.
641
686
if ((!Var || !isFirstInExternCContext (Var)) &&
642
687
(!Func || !isFirstInExternCContext (Func)))
643
688
return LinkageInfo::uniqueExternal ();
@@ -718,7 +763,8 @@ static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
718
763
// because of this, but unique-external linkage suits us.
719
764
if (Context.getLangOpts ().CPlusPlus && !isFirstInExternCContext (Var)) {
720
765
LinkageInfo TypeLV = getLVForType (*Var->getType (), computation);
721
- if (TypeLV.getLinkage () != ExternalLinkage)
766
+ if (TypeLV.getLinkage () != ExternalLinkage &&
767
+ TypeLV.getLinkage () != ModuleLinkage)
722
768
return LinkageInfo::uniqueExternal ();
723
769
if (!LV.isVisibilityExplicit ())
724
770
LV.mergeVisibility (TypeLV);
@@ -816,7 +862,9 @@ static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
816
862
817
863
// - a namespace (7.3), unless it is declared within an unnamed
818
864
// namespace.
819
- } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace ()) {
865
+ //
866
+ // We handled names in anonymous namespaces above.
867
+ } else if (isa<NamespaceDecl>(D)) {
820
868
return LV;
821
869
822
870
// By extension, we assign external linkage to Objective-C
@@ -1125,6 +1173,8 @@ static LinkageInfo getLVForClosure(const DeclContext *DC, Decl *ContextDecl,
1125
1173
if (const auto *ND = dyn_cast<NamedDecl>(DC))
1126
1174
return getLVForDecl (ND, computation);
1127
1175
1176
+ // FIXME: We have a closure at TU scope with no context declaration. This
1177
+ // should probably have no linkage.
1128
1178
return LinkageInfo::external ();
1129
1179
}
1130
1180
@@ -1137,7 +1187,7 @@ static LinkageInfo getLVForLocalDecl(const NamedDecl *D,
1137
1187
1138
1188
// This is a "void f();" which got merged with a file static.
1139
1189
if (Function->getCanonicalDecl ()->getStorageClass () == SC_Static)
1140
- return LinkageInfo::internal ( );
1190
+ return getInternalLinkageFor (Function );
1141
1191
1142
1192
LinkageInfo LV;
1143
1193
if (!hasExplicitVisibilityAlready (computation)) {
@@ -1226,7 +1276,7 @@ static LinkageInfo computeLVForDecl(const NamedDecl *D,
1226
1276
LVComputationKind computation) {
1227
1277
// Internal_linkage attribute overrides other considerations.
1228
1278
if (D->hasAttr <InternalLinkageAttr>())
1229
- return LinkageInfo::internal ( );
1279
+ return getInternalLinkageFor (D );
1230
1280
1231
1281
// Objective-C: treat all Objective-C declarations as having external
1232
1282
// linkage.
@@ -1275,14 +1325,14 @@ static LinkageInfo computeLVForDecl(const NamedDecl *D,
1275
1325
case Decl::ObjCProperty:
1276
1326
case Decl::ObjCPropertyImpl:
1277
1327
case Decl::ObjCProtocol:
1278
- return LinkageInfo::external ( );
1328
+ return getExternalLinkageFor (D );
1279
1329
1280
1330
case Decl::CXXRecord: {
1281
1331
const auto *Record = cast<CXXRecordDecl>(D);
1282
1332
if (Record->isLambda ()) {
1283
1333
if (!Record->getLambdaManglingNumber ()) {
1284
1334
// This lambda has no mangling number, so it's internal.
1285
- return LinkageInfo::internal ( );
1335
+ return getInternalLinkageFor (D );
1286
1336
}
1287
1337
1288
1338
// This lambda has its linkage/visibility determined:
@@ -1298,7 +1348,7 @@ static LinkageInfo computeLVForDecl(const NamedDecl *D,
1298
1348
const CXXRecordDecl *OuterMostLambda =
1299
1349
getOutermostEnclosingLambda (Record);
1300
1350
if (!OuterMostLambda->getLambdaManglingNumber ())
1301
- return LinkageInfo::internal ( );
1351
+ return getInternalLinkageFor (D );
1302
1352
1303
1353
return getLVForClosure (
1304
1354
OuterMostLambda->getDeclContext ()->getRedeclContext (),
@@ -1349,7 +1399,7 @@ class LinkageComputer {
1349
1399
LVComputationKind computation) {
1350
1400
// Internal_linkage attribute overrides other considerations.
1351
1401
if (D->hasAttr <InternalLinkageAttr>())
1352
- return LinkageInfo::internal ( );
1402
+ return getInternalLinkageFor (D );
1353
1403
1354
1404
if (computation == LVForLinkageOnly && D->hasCachedLinkage ())
1355
1405
return LinkageInfo (D->getCachedLinkage (), DefaultVisibility, false );
0 commit comments