Skip to content

Commit 2056d72

Browse files
committed
AST: Introduce @_originallyDefinedIn(..., _linkerModule: "Foo", ...)
1 parent dc3b0e6 commit 2056d72

File tree

13 files changed

+110
-40
lines changed

13 files changed

+110
-40
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,10 +1087,12 @@ BridgedMacroRoleAttr BridgedMacroRoleAttr_createParsed(
10871087
BridgedArrayRef cConformances, BridgedSourceLoc cRParenLoc);
10881088

10891089
SWIFT_NAME("BridgedOriginallyDefinedInAttr.createParsed(_:atLoc:range:"
1090-
"moduleName:platform:version:)")
1090+
"manglingModuleName:linkerModuleName:platform:version:)")
10911091
BridgedOriginallyDefinedInAttr BridgedOriginallyDefinedInAttr_createParsed(
10921092
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
1093-
BridgedSourceRange cRange, BridgedStringRef cModuleName,
1093+
BridgedSourceRange cRange,
1094+
BridgedStringRef cManglingModuleName,
1095+
BridgedStringRef cLinkerModuleName,
10941096
BridgedPlatformKind cPlatform, BridgedVersionTuple cVersion);
10951097

10961098
SWIFT_NAME("BridgedStorageRestrictionsAttr.createParsed(_:atLoc:range:"

include/swift/AST/Attr.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,17 +2029,24 @@ class ProjectedValuePropertyAttr : public DeclAttribute {
20292029
class OriginallyDefinedInAttr: public DeclAttribute {
20302030
public:
20312031
OriginallyDefinedInAttr(SourceLoc AtLoc, SourceRange Range,
2032-
StringRef OriginalModuleName, PlatformKind Platform,
2032+
StringRef ManglingModuleName,
2033+
StringRef LinkerModuleName,
2034+
PlatformKind Platform,
20332035
const llvm::VersionTuple MovedVersion, bool Implicit)
20342036
: DeclAttribute(DeclAttrKind::OriginallyDefinedIn, AtLoc, Range,
20352037
Implicit),
2036-
OriginalModuleName(OriginalModuleName), Platform(Platform),
2038+
ManglingModuleName(ManglingModuleName),
2039+
LinkerModuleName(LinkerModuleName),
2040+
Platform(Platform),
20372041
MovedVersion(MovedVersion) {}
20382042

20392043
OriginallyDefinedInAttr *clone(ASTContext &C, bool implicit) const;
20402044

2041-
// The original module name.
2042-
const StringRef OriginalModuleName;
2045+
// The original module name for mangling.
2046+
const StringRef ManglingModuleName;
2047+
2048+
// The original module name for linker directives.
2049+
const StringRef LinkerModuleName;
20432050

20442051
/// The platform of the symbol.
20452052
const PlatformKind Platform;
@@ -2048,7 +2055,8 @@ class OriginallyDefinedInAttr: public DeclAttribute {
20482055
const llvm::VersionTuple MovedVersion;
20492056

20502057
struct ActiveVersion {
2051-
StringRef ModuleName;
2058+
StringRef ManglingModuleName;
2059+
StringRef LinkerModuleName;
20522060
PlatformKind Platform;
20532061
llvm::VersionTuple Version;
20542062
bool ForTargetVariant = false;
@@ -2064,7 +2072,8 @@ class OriginallyDefinedInAttr: public DeclAttribute {
20642072
/// Create a copy of this attribute.
20652073
OriginallyDefinedInAttr *clone(ASTContext &ctx) const {
20662074
return new (ctx) OriginallyDefinedInAttr(
2067-
AtLoc, Range, OriginalModuleName, Platform, MovedVersion, isImplicit());
2075+
AtLoc, Range, ManglingModuleName, LinkerModuleName,
2076+
Platform, MovedVersion, isImplicit());
20682077
}
20692078
};
20702079

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5196,7 +5196,8 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
51965196
void visitOriginallyDefinedInAttr(OriginallyDefinedInAttr *Attr,
51975197
Label label) {
51985198
printCommon(Attr, "originally_defined_in_attr", label);
5199-
printField(Attr->OriginalModuleName, Label::always("original_module"));
5199+
printField(Attr->ManglingModuleName, Label::always("mangling_module"));
5200+
printField(Attr->LinkerModuleName, Label::always("linker_module"));
52005201
printField(Attr->Platform, Label::always("platform"));
52015202
printFieldRaw([&](auto &out) { out << Attr->MovedVersion.getAsString(); },
52025203
Label::always("moved_version"));

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5934,7 +5934,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
59345934
for (auto attr: D->getAttrs().getAttributes<OriginallyDefinedInAttr>()) {
59355935
Name = Mod->getASTContext()
59365936
.getIdentifier(const_cast<OriginallyDefinedInAttr*>(attr)
5937-
->OriginalModuleName);
5937+
->ManglingModuleName);
59385938
break;
59395939
}
59405940
}

lib/AST/Attr.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,10 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
11861186
Printer.printAttrName("@_originallyDefinedIn");
11871187
Printer << "(module: ";
11881188
auto Attr = cast<OriginallyDefinedInAttr>(this);
1189-
Printer << "\"" << Attr->OriginalModuleName << "\", ";
1189+
Printer << "\"" << Attr->ManglingModuleName << "\", ";
1190+
if (Attr->LinkerModuleName != Attr->ManglingModuleName) {
1191+
Printer << ", _linkerModule: \"" << Attr->LinkerModuleName << "\", ";
1192+
}
11901193
Printer << platformString(Attr->Platform) << " " <<
11911194
Attr->MovedVersion.getAsString();
11921195
Printer << ")";
@@ -2227,7 +2230,8 @@ OriginallyDefinedInAttr::isActivePlatform(const ASTContext &ctx) const {
22272230
OriginallyDefinedInAttr::ActiveVersion Result;
22282231
Result.Platform = Platform;
22292232
Result.Version = MovedVersion;
2230-
Result.ModuleName = OriginalModuleName;
2233+
Result.ManglingModuleName = ManglingModuleName;
2234+
Result.LinkerModuleName = LinkerModuleName;
22312235
if (isPlatformActive(Platform, ctx.LangOpts, /*TargetVariant*/false)) {
22322236
return Result;
22332237
}
@@ -2247,7 +2251,7 @@ OriginallyDefinedInAttr *OriginallyDefinedInAttr::clone(ASTContext &C,
22472251
bool implicit) const {
22482252
return new (C) OriginallyDefinedInAttr(
22492253
implicit ? SourceLoc() : AtLoc, implicit ? SourceRange() : getRange(),
2250-
OriginalModuleName, Platform, MovedVersion, implicit);
2254+
ManglingModuleName, LinkerModuleName, Platform, MovedVersion, implicit);
22512255
}
22522256

22532257
bool AvailableAttr::isUnconditionallyUnavailable() const {

lib/AST/Bridging/DeclAttributeBridging.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,13 @@ BridgedMacroRoleAttr BridgedMacroRoleAttr_createParsed(
473473

474474
BridgedOriginallyDefinedInAttr BridgedOriginallyDefinedInAttr_createParsed(
475475
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
476-
BridgedSourceRange cRange, BridgedStringRef cModuleName,
476+
BridgedSourceRange cRange,
477+
BridgedStringRef cManglingModuleName,
478+
BridgedStringRef cLinkerModuleName,
477479
BridgedPlatformKind cPlatform, BridgedVersionTuple cVersion) {
478480
return new (cContext.unbridged()) OriginallyDefinedInAttr(
479-
cAtLoc.unbridged(), cRange.unbridged(), cModuleName.unbridged(),
481+
cAtLoc.unbridged(), cRange.unbridged(),
482+
cManglingModuleName.unbridged(), cLinkerModuleName.unbridged(),
480483
unbridge(cPlatform), cVersion.unbridged(),
481484
/*Implicit=*/false);
482485
}

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ StringRef Decl::getAlternateModuleName() const {
10381038
for (auto *Att: Attrs) {
10391039
if (auto *OD = dyn_cast<OriginallyDefinedInAttr>(Att)) {
10401040
if (!OD->isInvalid() && OD->isActivePlatform(getASTContext())) {
1041-
return OD->OriginalModuleName;
1041+
return OD->ManglingModuleName;
10421042
}
10431043
}
10441044
}

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,16 +1374,20 @@ extension ASTGenVisitor {
13741374
}
13751375

13761376
guard
1377-
let moduleName = self.generateStringLiteralTextIfNotInterpolated(expr: args.moduleName),
1378-
!moduleName.isEmpty
1377+
let manglingModuleName = self.generateStringLiteralTextIfNotInterpolated(expr: args.moduleName),
1378+
!manglingModuleName.isEmpty
13791379
else {
13801380
// TODO: Diagnose
13811381
fatalError("expected non-empty string literal without interpolations")
13821382
}
13831383

1384+
// FIXME
1385+
let linkerModuleName = manglingModuleName
1386+
13841387
let atLoc = self.generateSourceLoc(node.atSign)
13851388
let range = self.generateAttrSourceRange(node)
1386-
let moduleNameInCtx = self.ctx.allocateCopy(string: moduleName)
1389+
let manglingModuleNameInCtx = self.ctx.allocateCopy(string: manglingModuleName)
1390+
let linkerModuleNameInCtx = self.ctx.allocateCopy(string: linkerModuleName)
13871391

13881392
let platformVersions = self.generate(platformVersionList: args.platforms)
13891393
var result: [BridgedOriginallyDefinedInAttr] = []
@@ -1392,7 +1396,8 @@ extension ASTGenVisitor {
13921396
ctx,
13931397
atLoc: atLoc,
13941398
range: range,
1395-
moduleName: moduleNameInCtx,
1399+
manglingModuleName: manglingModuleNameInCtx,
1400+
linkerModuleName: linkerModuleNameInCtx,
13961401
platform: platformVersion.platform,
13971402
version: platformVersion.version
13981403
)

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,10 +2490,10 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
24902490
if (auto Attribute =
24912491
ND->getAttrs().getAttribute<OriginallyDefinedInAttr>()) {
24922492
auto Identifier = IGM.getSILModule().getASTContext().getIdentifier(
2493-
Attribute->OriginalModuleName);
2493+
Attribute->ManglingModuleName);
24942494
void *Key = (void *)Identifier.get();
24952495
Scope =
2496-
getOrCreateModule(Key, TheCU, Attribute->OriginalModuleName, {});
2496+
getOrCreateModule(Key, TheCU, Attribute->ManglingModuleName, {});
24972497
} else {
24982498
Context = ND->getParent();
24992499
}

lib/IRGen/TBDGen.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,20 @@ void TBDGenVisitor::addSymbolInternal(StringRef name, EncodeKind kind,
8686
DeclStack.empty() ? nullptr : DeclStack.back(), flags);
8787
}
8888

89-
static std::vector<OriginallyDefinedInAttr::ActiveVersion>
90-
getAllMovedPlatformVersions(Decl *D) {
89+
std::vector<OriginallyDefinedInAttr::ActiveVersion>
90+
TBDGenVisitor::getAllMovedPlatformVersions(Decl *D) {
91+
StringRef Name = D->getDeclContext()->getParentModule()->getName().str();
92+
9193
std::vector<OriginallyDefinedInAttr::ActiveVersion> Results;
9294
for (auto *attr: D->getAttrs()) {
9395
if (auto *ODA = dyn_cast<OriginallyDefinedInAttr>(attr)) {
9496
auto Active = ODA->isActivePlatform(D->getASTContext());
95-
if (Active.has_value()) {
97+
if (Active.has_value() && Active->LinkerModuleName != Name) {
9698
Results.push_back(*Active);
9799
}
98100
}
99101
}
102+
100103
return Results;
101104
}
102105

@@ -324,16 +327,16 @@ void TBDGenVisitor::addLinkerDirectiveSymbolsLdPrevious(
324327
if (*IntroVer >= Ver.Version)
325328
continue;
326329
auto PlatformNumber = getLinkerPlatformId(Ver, Ctx);
327-
auto It = previousInstallNameMap->find(Ver.ModuleName.str());
330+
auto It = previousInstallNameMap->find(Ver.LinkerModuleName.str());
328331
if (It == previousInstallNameMap->end()) {
329332
Ctx.Diags.diagnose(SourceLoc(), diag::cannot_find_install_name,
330-
Ver.ModuleName, getLinkerPlatformName(Ver, Ctx));
333+
Ver.LinkerModuleName, getLinkerPlatformName(Ver, Ctx));
331334
continue;
332335
}
333336
auto InstallName = It->second.getInstallName(PlatformNumber);
334337
if (InstallName.empty()) {
335338
Ctx.Diags.diagnose(SourceLoc(), diag::cannot_find_install_name,
336-
Ver.ModuleName, getLinkerPlatformName(Ver, Ctx));
339+
Ver.LinkerModuleName, getLinkerPlatformName(Ver, Ctx));
337340
continue;
338341
}
339342
llvm::SmallString<64> Buffer;

lib/Parse/ParseDecl.cpp

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3207,10 +3207,12 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
32073207
SourceLoc RightLoc;
32083208
enum class NextSegmentKind: uint8_t {
32093209
ModuleName = 0,
3210+
LinkerModuleName,
32103211
PlatformVersion,
32113212
};
32123213
NextSegmentKind NK = NextSegmentKind::ModuleName;
3213-
StringRef OriginalModuleName;
3214+
StringRef ManglingModuleName;
3215+
StringRef LinkerModuleName;
32143216
llvm::SmallVector<std::pair<PlatformKind, llvm::VersionTuple>, 4>
32153217
PlatformAndVersions;
32163218

@@ -3229,7 +3231,8 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
32293231
// Parse 'module: "original_module_name"'.
32303232
case NextSegmentKind::ModuleName: {
32313233
// Parse 'module' ':'.
3232-
if (!Tok.is(tok::identifier) || Tok.getText() != "module" ||
3234+
if (!Tok.is(tok::identifier) ||
3235+
(Tok.getText() != "module") ||
32333236
!peekToken().is(tok::colon)) {
32343237
diagnose(Tok, diag::originally_defined_in_need_original_module_name);
32353238
SuppressLaterDiags = true;
@@ -3243,10 +3246,36 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
32433246
auto NameOp = getStringLiteralIfNotInterpolated(Tok.getLoc(),
32443247
"original module name");
32453248
if (NameOp.has_value())
3246-
OriginalModuleName = *NameOp;
3249+
ManglingModuleName = *NameOp;
32473250
consumeToken();
32483251
}
3249-
if (OriginalModuleName.empty()) {
3252+
if (ManglingModuleName.empty()) {
3253+
diagnose(ModuleNameLoc,
3254+
diag::originally_defined_in_need_nonempty_module_name);
3255+
SuppressLaterDiags = true;
3256+
return makeParserError();
3257+
}
3258+
return makeParserSuccess();
3259+
}
3260+
case NextSegmentKind::LinkerModuleName: {
3261+
// Parse optional '_linkerModule' ':'.
3262+
if (!Tok.is(tok::identifier) ||
3263+
(Tok.getText() != "_linkerModule") ||
3264+
!peekToken().is(tok::colon)) {
3265+
return makeParserSuccess();
3266+
}
3267+
consumeToken(tok::identifier);
3268+
consumeToken(tok::colon);
3269+
// Parse the next string literal as the original module name.
3270+
auto ModuleNameLoc = Tok.getLoc();
3271+
if (Tok.is(tok::string_literal)) {
3272+
auto NameOp = getStringLiteralIfNotInterpolated(Tok.getLoc(),
3273+
"linker module name");
3274+
if (NameOp.has_value())
3275+
LinkerModuleName = *NameOp;
3276+
consumeToken();
3277+
}
3278+
if (LinkerModuleName.empty()) {
32503279
diagnose(ModuleNameLoc,
32513280
diag::originally_defined_in_need_nonempty_module_name);
32523281
SuppressLaterDiags = true;
@@ -3267,7 +3296,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
32673296
}).isErrorOrHasCompletion() || SuppressLaterDiags) {
32683297
return makeParserSuccess();
32693298
}
3270-
if (OriginalModuleName.empty()) {
3299+
if (ManglingModuleName.empty()) {
32713300
diagnose(AtLoc, diag::originally_defined_in_need_nonempty_module_name);
32723301
return makeParserSuccess();
32733302
}
@@ -3277,12 +3306,15 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
32773306
return makeParserSuccess();
32783307
}
32793308

3280-
assert(!OriginalModuleName.empty());
3309+
if (LinkerModuleName.empty())
3310+
LinkerModuleName = ManglingModuleName;
3311+
assert(!ManglingModuleName.empty());
32813312
assert(NK == NextSegmentKind::PlatformVersion);
32823313
AttrRange = SourceRange(Loc, RightLoc);
32833314
for (auto &Item: PlatformAndVersions) {
32843315
Attributes.add(new (Context) OriginallyDefinedInAttr(AtLoc, AttrRange,
3285-
OriginalModuleName,
3316+
ManglingModuleName,
3317+
LinkerModuleName,
32863318
Item.first,
32873319
Item.second,
32883320
/*IsImplicit*/false));

lib/Serialization/Deserialization.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5933,11 +5933,20 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
59335933
Platform);
59345934
llvm::VersionTuple MovedVer;
59355935
DECODE_VER_TUPLE(MovedVer)
5936-
auto ModuleNameEnd = blobData.find('\0');
5937-
assert(ModuleNameEnd != StringRef::npos);
5938-
auto ModuleName = blobData.slice(0, ModuleNameEnd);
5936+
5937+
auto ManglingModuleNameEnd = blobData.find('\0');
5938+
assert(ManglingModuleNameEnd != StringRef::npos);
5939+
auto ManglingModuleName = blobData.slice(0, ManglingModuleNameEnd);
5940+
5941+
blobData = blobData.slice(ManglingModuleNameEnd, blobData.size());
5942+
5943+
auto LinkerModuleNameEnd = blobData.find('\0');
5944+
assert(LinkerModuleNameEnd != StringRef::npos);
5945+
auto LinkerModuleName = blobData.slice(0, LinkerModuleNameEnd);
5946+
59395947
Attr = new (ctx) OriginallyDefinedInAttr(SourceLoc(), SourceRange(),
5940-
ModuleName,
5948+
ManglingModuleName,
5949+
LinkerModuleName,
59415950
(PlatformKind)Platform,
59425951
MovedVer,
59435952
isImplicit);

lib/Serialization/Serialization.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3064,7 +3064,9 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
30643064
Moved, std::optional<llvm::VersionTuple>(theAttr->MovedVersion));
30653065
auto abbrCode = S.DeclTypeAbbrCodes[OriginallyDefinedInDeclAttrLayout::Code];
30663066
llvm::SmallString<32> blob;
3067-
blob.append(theAttr->OriginalModuleName.str());
3067+
blob.append(theAttr->ManglingModuleName.str());
3068+
blob.push_back('\0');
3069+
blob.append(theAttr->LinkerModuleName.str());
30683070
blob.push_back('\0');
30693071
OriginallyDefinedInDeclAttrLayout::emitRecord(
30703072
S.Out, S.ScratchRecord, abbrCode,

0 commit comments

Comments
 (0)