Skip to content

Commit 910989c

Browse files
committed
AST: Store parsed version in OriginallyDefinedInAttr instead of canonical.
Canonicalize the version on-demand instead. NFC, part of rdar://155558161.
1 parent e03cf96 commit 910989c

14 files changed

+164
-43
lines changed

include/swift/AST/Attr.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,6 +2399,11 @@ class ProjectedValuePropertyAttr : public DeclAttribute {
23992399
/// The variable \p foo was originally defined in another module called
24002400
/// \p Original prior to OSX 10.15
24012401
class OriginallyDefinedInAttr: public DeclAttribute {
2402+
const StringRef ManglingModuleName;
2403+
const StringRef LinkerModuleName;
2404+
const PlatformKind Platform;
2405+
const llvm::VersionTuple MovedVersion;
2406+
24022407
public:
24032408
OriginallyDefinedInAttr(SourceLoc AtLoc, SourceRange Range,
24042409
StringRef OriginalModuleName,
@@ -2420,16 +2425,22 @@ class OriginallyDefinedInAttr: public DeclAttribute {
24202425
OriginallyDefinedInAttr *clone(ASTContext &C, bool implicit) const;
24212426

24222427
// The original module name for mangling.
2423-
const StringRef ManglingModuleName;
2428+
StringRef getManglingModuleName() const { return ManglingModuleName; }
24242429

24252430
// The original module name for linker directives.
2426-
const StringRef LinkerModuleName;
2431+
StringRef getLinkerModuleName() const { return LinkerModuleName; }
24272432

24282433
/// The platform of the symbol.
2429-
const PlatformKind Platform;
2434+
PlatformKind getPlatform() const { return Platform; }
24302435

2431-
/// Indicates when the symbol was moved here.
2432-
const llvm::VersionTuple MovedVersion;
2436+
/// The version of the platform that the symbol was moved in, as it was
2437+
/// written in source.
2438+
llvm::VersionTuple getParsedMovedVersion() const { return MovedVersion; }
2439+
2440+
/// The version of the platform that the symbol was moved in. This may be
2441+
/// different than the version that was written in source due to
2442+
/// canonicalization.
2443+
llvm::VersionTuple getMovedVersion() const;
24332444

24342445
struct ActiveVersion {
24352446
StringRef ManglingModuleName;

lib/AST/ASTDumper.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5347,11 +5347,12 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
53475347
void visitOriginallyDefinedInAttr(OriginallyDefinedInAttr *Attr,
53485348
Label label) {
53495349
printCommon(Attr, "originally_defined_in_attr", label);
5350-
printField(Attr->ManglingModuleName, Label::always("mangling_module"));
5351-
printField(Attr->LinkerModuleName, Label::always("linker_module"));
5352-
printField(Attr->Platform, Label::always("platform"));
5353-
printFieldRaw([&](auto &out) { out << Attr->MovedVersion.getAsString(); },
5354-
Label::always("moved_version"));
5350+
printField(Attr->getManglingModuleName(), Label::always("mangling_module"));
5351+
printField(Attr->getLinkerModuleName(), Label::always("linker_module"));
5352+
printField(Attr->getPlatform(), Label::always("platform"));
5353+
printFieldRaw(
5354+
[&](auto &out) { out << Attr->getParsedMovedVersion().getAsString(); },
5355+
Label::always("moved_version"));
53555356
printFoot();
53565357
}
53575358
void visitPrivateImportAttr(PrivateImportAttr *Attr, Label label) {

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6058,9 +6058,9 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
60586058
if (Options.UseOriginallyDefinedInModuleNames) {
60596059
Decl *D = Ty->getDecl();
60606060
for (auto attr: D->getAttrs().getAttributes<OriginallyDefinedInAttr>()) {
6061-
Name = Mod->getASTContext()
6062-
.getIdentifier(const_cast<OriginallyDefinedInAttr*>(attr)
6063-
->ManglingModuleName);
6061+
Name = Mod->getASTContext().getIdentifier(
6062+
const_cast<OriginallyDefinedInAttr *>(attr)
6063+
->getManglingModuleName());
60646064
break;
60656065
}
60666066
}

lib/AST/Attr.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ static std::optional<PlatformKind> referencedPlatform(const DeclAttribute *attr,
773773
case DeclAttrKind::BackDeployed:
774774
return static_cast<const BackDeployedAttr *>(attr)->Platform;
775775
case DeclAttrKind::OriginallyDefinedIn:
776-
return static_cast<const OriginallyDefinedInAttr *>(attr)->Platform;
776+
return static_cast<const OriginallyDefinedInAttr *>(attr)->getPlatform();
777777
default:
778778
return std::nullopt;
779779
}
@@ -1094,7 +1094,7 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
10941094
case DeclAttrKind::OriginallyDefinedIn: {
10951095
auto Attr = cast<OriginallyDefinedInAttr>(this);
10961096
auto Name = D->getDeclContext()->getParentModule()->getName().str();
1097-
if (Options.IsForSwiftInterface && Attr->ManglingModuleName == Name)
1097+
if (Options.IsForSwiftInterface && Attr->getManglingModuleName() == Name)
10981098
return false;
10991099
break;
11001100
}
@@ -1195,13 +1195,15 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
11951195
Printer.printAttrName("@_originallyDefinedIn");
11961196
Printer << "(module: ";
11971197
auto Attr = cast<OriginallyDefinedInAttr>(this);
1198-
Printer << "\"" << Attr->ManglingModuleName;
1199-
ASSERT(!Attr->ManglingModuleName.empty());
1200-
ASSERT(!Attr->LinkerModuleName.empty());
1201-
if (Attr->LinkerModuleName != Attr->ManglingModuleName)
1202-
Printer << ";" << Attr->LinkerModuleName;
1203-
Printer << "\", " << platformString(Attr->Platform) << " " <<
1204-
Attr->MovedVersion.getAsString();
1198+
auto ManglingModuleName = Attr->getManglingModuleName();
1199+
auto LinkerModuleName = Attr->getLinkerModuleName();
1200+
Printer << "\"" << ManglingModuleName;
1201+
ASSERT(!ManglingModuleName.empty());
1202+
ASSERT(!LinkerModuleName.empty());
1203+
if (LinkerModuleName != ManglingModuleName)
1204+
Printer << ";" << LinkerModuleName;
1205+
Printer << "\", " << platformString(Attr->getPlatform()) << " "
1206+
<< Attr->getMovedVersion().getAsString();
12051207
Printer << ")";
12061208
break;
12071209
}
@@ -2375,14 +2377,14 @@ bool AvailableAttr::isEquivalent(const AvailableAttr *other,
23752377
== attachedTo->getSemanticAvailableAttr(other)->getDomain();
23762378
}
23772379

2378-
static StringRef getManglingModuleName(StringRef OriginalModuleName) {
2380+
static StringRef parseManglingModuleName(StringRef OriginalModuleName) {
23792381
auto index = OriginalModuleName.find(";");
23802382
return index == StringRef::npos
23812383
? OriginalModuleName
23822384
: OriginalModuleName.slice(0, index);
23832385
}
23842386

2385-
static StringRef getLinkerModuleName(StringRef OriginalModuleName) {
2387+
static StringRef parseLinkerModuleName(StringRef OriginalModuleName) {
23862388
auto index = OriginalModuleName.find(";");
23872389
return index == StringRef::npos
23882390
? OriginalModuleName
@@ -2393,16 +2395,19 @@ OriginallyDefinedInAttr::OriginallyDefinedInAttr(
23932395
SourceLoc AtLoc, SourceRange Range, StringRef OriginalModuleName,
23942396
PlatformKind Platform, const llvm::VersionTuple MovedVersion, bool Implicit)
23952397
: DeclAttribute(DeclAttrKind::OriginallyDefinedIn, AtLoc, Range, Implicit),
2396-
ManglingModuleName(getManglingModuleName(OriginalModuleName)),
2397-
LinkerModuleName(getLinkerModuleName(OriginalModuleName)),
2398-
Platform(Platform),
2399-
MovedVersion(canonicalizePlatformVersion(Platform, MovedVersion)) {}
2398+
ManglingModuleName(parseManglingModuleName(OriginalModuleName)),
2399+
LinkerModuleName(parseLinkerModuleName(OriginalModuleName)),
2400+
Platform(Platform), MovedVersion(MovedVersion) {}
2401+
2402+
llvm::VersionTuple OriginallyDefinedInAttr::getMovedVersion() const {
2403+
return canonicalizePlatformVersion(getPlatform(), getParsedMovedVersion());
2404+
}
24002405

24012406
std::optional<OriginallyDefinedInAttr::ActiveVersion>
24022407
OriginallyDefinedInAttr::isActivePlatform(const ASTContext &ctx) const {
24032408
OriginallyDefinedInAttr::ActiveVersion Result;
24042409
Result.Platform = Platform;
2405-
Result.Version = MovedVersion;
2410+
Result.Version = getMovedVersion();
24062411
Result.ManglingModuleName = ManglingModuleName;
24072412
Result.LinkerModuleName = LinkerModuleName;
24082413
if (isPlatformActive(Platform, ctx.LangOpts, /*TargetVariant*/false)) {

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ StringRef Decl::getAlternateModuleName() const {
11421142
for (auto *Att: Attrs) {
11431143
if (auto *OD = dyn_cast<OriginallyDefinedInAttr>(Att)) {
11441144
if (!OD->isInvalid() && OD->isActivePlatform(getASTContext())) {
1145-
return OD->ManglingModuleName;
1145+
return OD->getManglingModuleName();
11461146
}
11471147
}
11481148
}

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,10 +2540,10 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
25402540
if (auto Attribute =
25412541
ND->getAttrs().getAttribute<OriginallyDefinedInAttr>()) {
25422542
auto Identifier = IGM.getSILModule().getASTContext().getIdentifier(
2543-
Attribute->ManglingModuleName);
2543+
Attribute->getManglingModuleName());
25442544
void *Key = (void *)Identifier.get();
2545-
Scope = getOrCreateModule(Key, TheCU, Attribute->ManglingModuleName, {},
2546-
{});
2545+
Scope = getOrCreateModule(Key, TheCU,
2546+
Attribute->getManglingModuleName(), {}, {});
25472547
} else {
25482548
Context = ND->getParent();
25492549
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4923,7 +4923,7 @@ void AttributeChecker::checkOriginalDefinedInAttrs(
49234923
continue;
49244924

49254925
auto AtLoc = Attr->AtLoc;
4926-
auto Platform = Attr->Platform;
4926+
auto Platform = Attr->getPlatform();
49274927
if (!seenPlatforms.insert({Platform, AtLoc}).second) {
49284928
// We've seen the platform before, emit error to the previous one which
49294929
// comes later in the source order.
@@ -4941,7 +4941,7 @@ void AttributeChecker::checkOriginalDefinedInAttrs(
49414941
return;
49424942

49434943
auto IntroVer = D->getIntroducedOSVersion(Platform);
4944-
if (IntroVer.value() > Attr->MovedVersion) {
4944+
if (IntroVer.value() > Attr->getMovedVersion()) {
49454945
diagnose(AtLoc,
49464946
diag::originally_definedin_must_not_before_available_version);
49474947
return;

lib/Serialization/Serialization.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3086,19 +3086,19 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
30863086

30873087
case DeclAttrKind::OriginallyDefinedIn: {
30883088
auto *theAttr = cast<OriginallyDefinedInAttr>(DA);
3089-
ENCODE_VER_TUPLE(
3090-
Moved, std::optional<llvm::VersionTuple>(theAttr->MovedVersion));
3089+
ENCODE_VER_TUPLE(Moved, std::optional<llvm::VersionTuple>(
3090+
theAttr->getParsedMovedVersion()));
30913091
auto abbrCode = S.DeclTypeAbbrCodes[OriginallyDefinedInDeclAttrLayout::Code];
30923092
llvm::SmallString<32> blob;
3093-
blob.append(theAttr->ManglingModuleName.str());
3093+
blob.append(theAttr->getManglingModuleName().str());
30943094
blob.push_back('\0');
3095-
blob.append(theAttr->LinkerModuleName.str());
3095+
blob.append(theAttr->getLinkerModuleName().str());
30963096
blob.push_back('\0');
30973097
OriginallyDefinedInDeclAttrLayout::emitRecord(
30983098
S.Out, S.ScratchRecord, abbrCode,
30993099
theAttr->isImplicit(),
31003100
LIST_VER_TUPLE_PIECES(Moved),
3101-
static_cast<unsigned>(theAttr->Platform),
3101+
static_cast<unsigned>(theAttr->getPlatform()),
31023102
blob);
31033103
return;
31043104
}

test/ModuleInterface/originally-defined-attr.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// RUN: %FileCheck %s < %t/printed-module.txt
1515

1616
// CHECK: @_originallyDefinedIn(module: "another", macOS 13.13)
17+
// CHECK-LABEL: protocol SimpleProto
1718
@available(OSX 10.8, *)
1819
@_originallyDefinedIn(module: "another", OSX 13.13)
1920
public protocol SimpleProto { }
@@ -22,30 +23,65 @@ public protocol SimpleProto { }
2223
// CHECK: @_originallyDefinedIn(module: "another_original", macOS 2.0)
2324
// CHECK: @_originallyDefinedIn(module: "another_original", iOS 3.0)
2425
// CHECK: @_originallyDefinedIn(module: "another_original", watchOS 4.0)
26+
// CHECK-LABEL: struct SimpleStruct
2527
@available(tvOS 0.7, OSX 1.1, iOS 2.1, watchOS 3.2, *)
2628
@_originallyDefinedIn(module: "original", tvOS 1.0)
2729
@_originallyDefinedIn(module: "another_original", OSX 2.0, iOS 3.0, watchOS 4.0)
2830
public struct SimpleStruct {}
2931

3032
// CHECK: @_originallyDefinedIn(module: "other0", macOS 10.10)
3133
// CHECK: @_originallyDefinedIn(module: "other0", iOS 8.0)
34+
// CHECK-LABEL: struct SimpleThingInAlphabeticalOrderForMacros0
3235
@available(tvOS 0.7, OSX 1.1, iOS 2.1, watchOS 3.2, *)
3336
@_originallyDefinedIn(module: "other0", _iOS8Aligned)
3437
public struct SimpleThingInAlphabeticalOrderForMacros0 {}
3538

3639
// CHECK: @_originallyDefinedIn(module: "other1", iOS 9.0)
3740
// CHECK: @_originallyDefinedIn(module: "other1", macOS 10.11)
41+
// CHECK-LABEL: struct SimpleThingInAlphabeticalOrderForMacros1
3842
@available(tvOS 0.7, OSX 1.1, iOS 2.1, watchOS 3.2, *)
3943
@_originallyDefinedIn(module: "other1", _iOS9, _macOS10_11)
4044
public struct SimpleThingInAlphabeticalOrderForMacros1 {}
4145

4246
// CHECK: @_originallyDefinedIn(module: "other2", macOS 10.11)
47+
// CHECK-LABEL: struct SimpleThingInAlphabeticalOrderForMacros2
4348
@available(tvOS 0.7, OSX 1.1, iOS 2.1, watchOS 3.2, *)
4449
@_originallyDefinedIn(module: "other2", _myProject 1.0)
4550
public struct SimpleThingInAlphabeticalOrderForMacros2 {}
4651

4752
// CHECK: @_originallyDefinedIn(module: "another", macOS 13.13)
53+
// CHECK-LABEL: struct SimpleThingInAlphabeticalOrderForMacros3_UsableFromInline
4854
@available(OSX 10.8, *)
4955
@_originallyDefinedIn(module: "another", OSX 13.13)
5056
@usableFromInline
5157
internal struct SimpleThingInAlphabeticalOrderForMacros3_UsableFromInline {}
58+
59+
// CHECK: @_originallyDefinedIn(module: "pre26", macOS 26.0)
60+
// CHECK: @_originallyDefinedIn(module: "pre26", iOS 26.0)
61+
// CHECK: @_originallyDefinedIn(module: "pre26", watchOS 26.0)
62+
// CHECK: @_originallyDefinedIn(module: "pre26", tvOS 26.0)
63+
// CHECK: @_originallyDefinedIn(module: "pre26", visionOS 26.0)
64+
// CHECK-LABEL: struct SimpleThingInAlphabeticalOrderForMacros4_VersionsMappingTo26
65+
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
66+
@_originallyDefinedIn(module: "pre26", macOS 16, iOS 19, watchOS 12, tvOS 19, visionOS 3)
67+
public struct SimpleThingInAlphabeticalOrderForMacros4_VersionsMappingTo26 {}
68+
69+
// CHECK: @_originallyDefinedIn(module: "pre27", macOS 27)
70+
// CHECK: @_originallyDefinedIn(module: "pre27", iOS 27)
71+
// CHECK: @_originallyDefinedIn(module: "pre27", watchOS 27)
72+
// CHECK: @_originallyDefinedIn(module: "pre27", tvOS 27)
73+
// CHECK: @_originallyDefinedIn(module: "pre27", visionOS 27)
74+
// CHECK-LABEL: struct SimpleThingInAlphabeticalOrderForMacros5_VersionsMappingTo27
75+
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
76+
@_originallyDefinedIn(module: "pre27", macOS 17, iOS 20, watchOS 13, tvOS 20, visionOS 4)
77+
public struct SimpleThingInAlphabeticalOrderForMacros5_VersionsMappingTo27 {}
78+
79+
// CHECK: @_originallyDefinedIn(module: "pre26", macOS 26)
80+
// CHECK: @_originallyDefinedIn(module: "pre26", iOS 26)
81+
// CHECK: @_originallyDefinedIn(module: "pre26", watchOS 26)
82+
// CHECK: @_originallyDefinedIn(module: "pre26", tvOS 26)
83+
// CHECK: @_originallyDefinedIn(module: "pre26", visionOS 26)
84+
// CHECK-LABEL: struct SimpleThingInAlphabeticalOrderForMacros6_Version26
85+
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
86+
@_originallyDefinedIn(module: "pre26", macOS 26, iOS 26, watchOS 26, tvOS 26, visionOS 26)
87+
public struct SimpleThingInAlphabeticalOrderForMacros6_Version26 {}

test/Parse/original_defined_in_attr.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,15 @@ fileprivate class ToplevelClass7 {}
5050
@available(OSX 13.10, *)
5151
@_originallyDefinedIn(module: "foo", OSX 13.13, iOS 7.0) // expected-warning {{'@_originallyDefinedIn' does not have any effect on internal declarations}}
5252
internal class ToplevelClass8 {}
53+
54+
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
55+
@_originallyDefinedIn(module: "foo", macOS 16, iOS 19, watchOS 12, tvOS 19, visionOS 3)
56+
public class ToplevelClass9 {}
57+
58+
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
59+
@_originallyDefinedIn(module: "foo", macOS 17, iOS 20, watchOS 13, tvOS 20, visionOS 4) // FIXME: Should be diagnosed
60+
public class ToplevelClass10 {}
61+
62+
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
63+
@_originallyDefinedIn(module: "foo", macOS 26, iOS 26, watchOS 26, tvOS 26, visionOS 26)
64+
public class ToplevelClass11 {}

0 commit comments

Comments
 (0)