@@ -438,6 +438,7 @@ class CXXNameMangler {
438
438
void mangleType (QualType T);
439
439
void mangleNameOrStandardSubstitution (const NamedDecl *ND);
440
440
void mangleLambdaSig (const CXXRecordDecl *Lambda);
441
+ void mangleModuleNamePrefix (StringRef Name);
441
442
442
443
private:
443
444
@@ -473,22 +474,21 @@ class CXXNameMangler {
473
474
474
475
void mangleNameWithAbiTags (GlobalDecl GD,
475
476
const AbiTagList *AdditionalAbiTags);
476
- void mangleModuleName (const Module *M);
477
- void mangleModuleNamePrefix (StringRef Name);
477
+ void mangleModuleName (const NamedDecl *ND);
478
478
void mangleTemplateName (const TemplateDecl *TD,
479
479
const TemplateArgument *TemplateArgs,
480
480
unsigned NumTemplateArgs);
481
- void mangleUnqualifiedName (GlobalDecl GD,
481
+ void mangleUnqualifiedName (GlobalDecl GD, const DeclContext *DC,
482
482
const AbiTagList *AdditionalAbiTags) {
483
- mangleUnqualifiedName (GD, cast<NamedDecl>(GD.getDecl ())->getDeclName (), UnknownArity ,
484
- AdditionalAbiTags);
483
+ mangleUnqualifiedName (GD, cast<NamedDecl>(GD.getDecl ())->getDeclName (), DC ,
484
+ UnknownArity, AdditionalAbiTags);
485
485
}
486
486
void mangleUnqualifiedName (GlobalDecl GD, DeclarationName Name,
487
- unsigned KnownArity,
487
+ const DeclContext *DC, unsigned KnownArity,
488
488
const AbiTagList *AdditionalAbiTags);
489
- void mangleUnscopedName (GlobalDecl GD,
489
+ void mangleUnscopedName (GlobalDecl GD, const DeclContext *DC,
490
490
const AbiTagList *AdditionalAbiTags);
491
- void mangleUnscopedTemplateName (GlobalDecl GD,
491
+ void mangleUnscopedTemplateName (GlobalDecl GD, const DeclContext *DC,
492
492
const AbiTagList *AdditionalAbiTags);
493
493
void mangleSourceName (const IdentifierInfo *II);
494
494
void mangleRegCallName (const IdentifierInfo *II);
@@ -733,15 +733,17 @@ bool ItaniumMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) {
733
733
if (VD->isExternC ())
734
734
return false ;
735
735
736
- // Variables at global scope with non-internal linkage are not mangled.
736
+ // Variables at global scope are not mangled unless they have internal
737
+ // linkage or are specializations or are attached to a named module.
737
738
const DeclContext *DC = getEffectiveDeclContext (D);
738
739
// Check for extern variable declared locally.
739
740
if (DC->isFunctionOrMethod () && D->hasLinkage ())
740
741
while (!DC->isFileContext ())
741
742
DC = getEffectiveParentContext (DC);
742
743
if (DC->isTranslationUnit () && D->getFormalLinkage () != InternalLinkage &&
743
744
!CXXNameMangler::shouldHaveAbiTags (*this , VD) &&
744
- !isa<VarTemplateSpecializationDecl>(VD))
745
+ !isa<VarTemplateSpecializationDecl>(VD) &&
746
+ !VD->getOwningModuleForLinkage ())
745
747
return false ;
746
748
}
747
749
@@ -1016,14 +1018,6 @@ void CXXNameMangler::mangleNameWithAbiTags(GlobalDecl GD,
1016
1018
return ;
1017
1019
}
1018
1020
1019
- // Do not mangle the owning module for an external linkage declaration.
1020
- // This enables backwards-compatibility with non-modular code, and is
1021
- // a valid choice since conflicts are not permitted by C++ Modules TS
1022
- // [basic.def.odr]/6.2.
1023
- if (!ND->hasExternalFormalLinkage ())
1024
- if (Module *M = ND->getOwningModuleForLinkage ())
1025
- mangleModuleName (M);
1026
-
1027
1021
// Closures can require a nested-name mangling even if they're semantically
1028
1022
// in the global namespace.
1029
1023
if (const NamedDecl *PrefixND = getClosurePrefix (ND)) {
@@ -1035,38 +1029,35 @@ void CXXNameMangler::mangleNameWithAbiTags(GlobalDecl GD,
1035
1029
// Check if we have a template.
1036
1030
const TemplateArgumentList *TemplateArgs = nullptr ;
1037
1031
if (GlobalDecl TD = isTemplate (GD, TemplateArgs)) {
1038
- mangleUnscopedTemplateName (TD, AdditionalAbiTags);
1032
+ mangleUnscopedTemplateName (TD, DC, AdditionalAbiTags);
1039
1033
mangleTemplateArgs (asTemplateName (TD), *TemplateArgs);
1040
1034
return ;
1041
1035
}
1042
1036
1043
- mangleUnscopedName (GD, AdditionalAbiTags);
1037
+ mangleUnscopedName (GD, DC, AdditionalAbiTags);
1044
1038
return ;
1045
1039
}
1046
1040
1047
1041
mangleNestedName (GD, DC, AdditionalAbiTags);
1048
1042
}
1049
1043
1050
- void CXXNameMangler::mangleModuleName (const Module *M) {
1051
- // Implement the C++ Modules TS name mangling proposal; see
1052
- // https://gcc.gnu.org/wiki/cxx-modules?action=AttachFile
1053
- //
1054
- // <module-name> ::= W <unscoped-name>+ E
1055
- // ::= W <module-subst> <unscoped-name>* E
1056
- Out << ' W' ;
1057
- mangleModuleNamePrefix (M->Name );
1058
- Out << ' E' ;
1044
+ void CXXNameMangler::mangleModuleName (const NamedDecl *ND) {
1045
+ if (ND->isExternallyVisible ())
1046
+ if (Module *M = ND->getOwningModuleForLinkage ())
1047
+ mangleModuleNamePrefix (M->getPrimaryModuleInterfaceName ());
1059
1048
}
1060
1049
1050
+ // <module-name> ::= <module-subname>
1051
+ // ::= <module-name> <module-subname>
1052
+ // ::= <substitution>
1053
+ // <module-subname> ::= W <source-name>
1054
+ // ::= W P <source-name> # not (yet) needed
1061
1055
void CXXNameMangler::mangleModuleNamePrefix (StringRef Name) {
1062
- // <module-subst> ::= _ <seq-id> # 0 < seq-id < 10
1063
- // ::= W <seq-id - 10> _ # otherwise
1056
+ // <substitution> ::= S <seq-id> _
1064
1057
auto It = ModuleSubstitutions.find (Name);
1065
1058
if (It != ModuleSubstitutions.end ()) {
1066
- if (It->second < 10 )
1067
- Out << ' _' << static_cast <char >(' 0' + It->second );
1068
- else
1069
- Out << ' W' << (It->second - 10 ) << ' _' ;
1059
+ Out << ' S' ;
1060
+ mangleSeqID (It->second );
1070
1061
return ;
1071
1062
}
1072
1063
@@ -1078,8 +1069,9 @@ void CXXNameMangler::mangleModuleNamePrefix(StringRef Name) {
1078
1069
else
1079
1070
mangleModuleNamePrefix (Parts.first );
1080
1071
1072
+ Out << ' W' ;
1081
1073
Out << Parts.second .size () << Parts.second ;
1082
- ModuleSubstitutions.insert ({Name, ModuleSubstitutions. size () });
1074
+ ModuleSubstitutions.insert ({Name, SeqID++ });
1083
1075
}
1084
1076
1085
1077
void CXXNameMangler::mangleTemplateName (const TemplateDecl *TD,
@@ -1088,27 +1080,27 @@ void CXXNameMangler::mangleTemplateName(const TemplateDecl *TD,
1088
1080
const DeclContext *DC = Context.getEffectiveDeclContext (TD);
1089
1081
1090
1082
if (DC->isTranslationUnit () || isStdNamespace (DC)) {
1091
- mangleUnscopedTemplateName (TD, nullptr );
1083
+ mangleUnscopedTemplateName (TD, DC, nullptr );
1092
1084
mangleTemplateArgs (asTemplateName (TD), TemplateArgs, NumTemplateArgs);
1093
1085
} else {
1094
1086
mangleNestedName (TD, TemplateArgs, NumTemplateArgs);
1095
1087
}
1096
1088
}
1097
1089
1098
- void CXXNameMangler::mangleUnscopedName (GlobalDecl GD,
1090
+ void CXXNameMangler::mangleUnscopedName (GlobalDecl GD, const DeclContext *DC,
1099
1091
const AbiTagList *AdditionalAbiTags) {
1100
- const NamedDecl *ND = cast<NamedDecl>(GD.getDecl ());
1101
1092
// <unscoped-name> ::= <unqualified-name>
1102
1093
// ::= St <unqualified-name> # ::std::
1103
1094
1104
- if (isStdNamespace (Context.getEffectiveDeclContext (ND)))
1095
+ assert (!isa<LinkageSpecDecl>(DC) && " unskipped LinkageSpecDecl" );
1096
+ if (isStdNamespace (DC))
1105
1097
Out << " St" ;
1106
1098
1107
- mangleUnqualifiedName (GD, AdditionalAbiTags);
1099
+ mangleUnqualifiedName (GD, DC, AdditionalAbiTags);
1108
1100
}
1109
1101
1110
1102
void CXXNameMangler::mangleUnscopedTemplateName (
1111
- GlobalDecl GD, const AbiTagList *AdditionalAbiTags) {
1103
+ GlobalDecl GD, const DeclContext *DC, const AbiTagList *AdditionalAbiTags) {
1112
1104
const TemplateDecl *ND = cast<TemplateDecl>(GD.getDecl ());
1113
1105
// <unscoped-template-name> ::= <unscoped-name>
1114
1106
// ::= <substitution>
@@ -1121,9 +1113,10 @@ void CXXNameMangler::mangleUnscopedTemplateName(
1121
1113
" template template param cannot have abi tags" );
1122
1114
mangleTemplateParameter (TTP->getDepth (), TTP->getIndex ());
1123
1115
} else if (isa<BuiltinTemplateDecl>(ND) || isa<ConceptDecl>(ND)) {
1124
- mangleUnscopedName (GD, AdditionalAbiTags);
1116
+ mangleUnscopedName (GD, DC, AdditionalAbiTags);
1125
1117
} else {
1126
- mangleUnscopedName (GD.getWithDecl (ND->getTemplatedDecl ()), AdditionalAbiTags);
1118
+ mangleUnscopedName (GD.getWithDecl (ND->getTemplatedDecl ()), DC,
1119
+ AdditionalAbiTags);
1127
1120
}
1128
1121
1129
1122
addSubstitution (ND);
@@ -1399,15 +1392,19 @@ void CXXNameMangler::mangleUnresolvedName(
1399
1392
mangleTemplateArgs (TemplateName (), TemplateArgs, NumTemplateArgs);
1400
1393
}
1401
1394
1402
- void CXXNameMangler::mangleUnqualifiedName (GlobalDecl GD,
1403
- DeclarationName Name,
1404
- unsigned KnownArity,
1405
- const AbiTagList *AdditionalAbiTags) {
1395
+ void CXXNameMangler::mangleUnqualifiedName (
1396
+ GlobalDecl GD, DeclarationName Name, const DeclContext *DC,
1397
+ unsigned KnownArity, const AbiTagList *AdditionalAbiTags) {
1406
1398
const NamedDecl *ND = cast_or_null<NamedDecl>(GD.getDecl ());
1407
- unsigned Arity = KnownArity;
1408
- // <unqualified-name> ::= <operator-name>
1399
+ // <unqualified-name> ::= [<module-name>] <operator-name>
1409
1400
// ::= <ctor-dtor-name>
1410
- // ::= <source-name>
1401
+ // ::= [<module-name>] <source-name>
1402
+ // ::= [<module-name>] DC <source-name>* E
1403
+
1404
+ if (ND && DC && DC->isFileContext ())
1405
+ mangleModuleName (ND);
1406
+
1407
+ unsigned Arity = KnownArity;
1411
1408
switch (Name.getNameKind ()) {
1412
1409
case DeclarationName::Identifier: {
1413
1410
const IdentifierInfo *II = Name.getAsIdentifierInfo ();
@@ -1418,8 +1415,6 @@ void CXXNameMangler::mangleUnqualifiedName(GlobalDecl GD,
1418
1415
//
1419
1416
// <unqualified-name> ::= DC <source-name>* E
1420
1417
//
1421
- // These can never be referenced across translation units, so we do
1422
- // not need a cross-vendor mangling for anything other than demanglers.
1423
1418
// Proposed on cxx-abi-dev on 2016-08-12
1424
1419
Out << " DC" ;
1425
1420
for (auto *BD : DD->bindings ())
@@ -1716,7 +1711,7 @@ void CXXNameMangler::mangleNestedName(GlobalDecl GD,
1716
1711
mangleTemplateArgs (asTemplateName (TD), *TemplateArgs);
1717
1712
} else {
1718
1713
manglePrefix (DC, NoFunction);
1719
- mangleUnqualifiedName (GD, AdditionalAbiTags);
1714
+ mangleUnqualifiedName (GD, DC, AdditionalAbiTags);
1720
1715
}
1721
1716
1722
1717
Out << ' E' ;
@@ -1746,7 +1741,7 @@ void CXXNameMangler::mangleNestedNameWithClosurePrefix(
1746
1741
Out << ' N' ;
1747
1742
1748
1743
mangleClosurePrefix (PrefixND);
1749
- mangleUnqualifiedName (GD, AdditionalAbiTags);
1744
+ mangleUnqualifiedName (GD, nullptr , AdditionalAbiTags);
1750
1745
1751
1746
Out << ' E' ;
1752
1747
}
@@ -1824,7 +1819,7 @@ void CXXNameMangler::mangleLocalName(GlobalDecl GD,
1824
1819
// Mangle the name relative to the closest enclosing function.
1825
1820
// equality ok because RD derived from ND above
1826
1821
if (D == RD) {
1827
- mangleUnqualifiedName (RD, AdditionalAbiTags);
1822
+ mangleUnqualifiedName (RD, DC, AdditionalAbiTags);
1828
1823
} else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1829
1824
if (const NamedDecl *PrefixND = getClosurePrefix (BD))
1830
1825
mangleClosurePrefix (PrefixND, true /* NoFunction*/ );
@@ -1855,7 +1850,7 @@ void CXXNameMangler::mangleLocalName(GlobalDecl GD,
1855
1850
assert (!AdditionalAbiTags && " Block cannot have additional abi tags" );
1856
1851
mangleUnqualifiedBlock (BD);
1857
1852
} else {
1858
- mangleUnqualifiedName (GD, AdditionalAbiTags);
1853
+ mangleUnqualifiedName (GD, DC, AdditionalAbiTags);
1859
1854
}
1860
1855
1861
1856
if (const NamedDecl *ND = dyn_cast<NamedDecl>(RD ? RD : D)) {
@@ -2082,10 +2077,11 @@ void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
2082
2077
mangleTemplateArgs (asTemplateName (TD), *TemplateArgs);
2083
2078
} else if (const NamedDecl *PrefixND = getClosurePrefix (ND)) {
2084
2079
mangleClosurePrefix (PrefixND, NoFunction);
2085
- mangleUnqualifiedName (ND, nullptr );
2080
+ mangleUnqualifiedName (ND, nullptr , nullptr );
2086
2081
} else {
2087
- manglePrefix (Context.getEffectiveDeclContext (ND), NoFunction);
2088
- mangleUnqualifiedName (ND, nullptr );
2082
+ const DeclContext *DC = Context.getEffectiveDeclContext (ND);
2083
+ manglePrefix (DC, NoFunction);
2084
+ mangleUnqualifiedName (ND, DC, nullptr );
2089
2085
}
2090
2086
2091
2087
addSubstitution (ND);
@@ -2138,11 +2134,13 @@ void CXXNameMangler::mangleTemplatePrefix(GlobalDecl GD,
2138
2134
if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(ND)) {
2139
2135
mangleTemplateParameter (TTP->getDepth (), TTP->getIndex ());
2140
2136
} else {
2141
- manglePrefix (Context.getEffectiveDeclContext (ND), NoFunction);
2137
+ const DeclContext *DC = Context.getEffectiveDeclContext (ND);
2138
+ manglePrefix (DC, NoFunction);
2142
2139
if (isa<BuiltinTemplateDecl>(ND) || isa<ConceptDecl>(ND))
2143
- mangleUnqualifiedName (GD, nullptr );
2140
+ mangleUnqualifiedName (GD, DC, nullptr );
2144
2141
else
2145
- mangleUnqualifiedName (GD.getWithDecl (ND->getTemplatedDecl ()), nullptr );
2142
+ mangleUnqualifiedName (GD.getWithDecl (ND->getTemplatedDecl ()), DC,
2143
+ nullptr );
2146
2144
}
2147
2145
2148
2146
addSubstitution (ND);
@@ -2183,8 +2181,9 @@ void CXXNameMangler::mangleClosurePrefix(const NamedDecl *ND, bool NoFunction) {
2183
2181
mangleTemplatePrefix (TD, NoFunction);
2184
2182
mangleTemplateArgs (asTemplateName (TD), *TemplateArgs);
2185
2183
} else {
2186
- manglePrefix (Context.getEffectiveDeclContext (ND), NoFunction);
2187
- mangleUnqualifiedName (ND, nullptr );
2184
+ const auto *DC = Context.getEffectiveDeclContext (ND);
2185
+ manglePrefix (DC, NoFunction);
2186
+ mangleUnqualifiedName (ND, DC, nullptr );
2188
2187
}
2189
2188
2190
2189
Out << ' M' ;
@@ -6027,6 +6026,9 @@ bool CXXNameMangler::isSpecializedAs(QualType S, llvm::StringRef Name,
6027
6026
if (TemplateArgs[0 ].getAsType () != A)
6028
6027
return false ;
6029
6028
6029
+ if (SD->getSpecializedTemplate ()->getOwningModuleForLinkage ())
6030
+ return false ;
6031
+
6030
6032
return true ;
6031
6033
}
6032
6034
@@ -6058,6 +6060,9 @@ bool CXXNameMangler::isStdCharSpecialization(
6058
6060
!isSpecializedAs (TemplateArgs[2 ].getAsType (), " allocator" , A))
6059
6061
return false ;
6060
6062
6063
+ if (SD->getSpecializedTemplate ()->getOwningModuleForLinkage ())
6064
+ return false ;
6065
+
6061
6066
return true ;
6062
6067
}
6063
6068
@@ -6075,6 +6080,9 @@ bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
6075
6080
if (!isStdNamespace (Context.getEffectiveDeclContext (TD)))
6076
6081
return false ;
6077
6082
6083
+ if (TD->getOwningModuleForLinkage ())
6084
+ return false ;
6085
+
6078
6086
// <substitution> ::= Sa # ::std::allocator
6079
6087
if (TD->getIdentifier ()->isStr (" allocator" )) {
6080
6088
Out << " Sa" ;
@@ -6094,6 +6102,9 @@ bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
6094
6102
if (!isStdNamespace (Context.getEffectiveDeclContext (SD)))
6095
6103
return false ;
6096
6104
6105
+ if (SD->getSpecializedTemplate ()->getOwningModuleForLinkage ())
6106
+ return false ;
6107
+
6097
6108
// <substitution> ::= Ss # ::std::basic_string<char,
6098
6109
// ::std::char_traits<char>,
6099
6110
// ::std::allocator<char> >
0 commit comments