Skip to content

Commit b1411b3

Browse files
authored
Merge pull request #79427 from rintaro/astgen-lifetimeattr
[ASTGen] Generate LifetimeAttr
2 parents 1a42a0c + b729746 commit b1411b3

File tree

10 files changed

+304
-22
lines changed

10 files changed

+304
-22
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Fingerprint;
4848
class Identifier;
4949
class IfConfigClauseRangeInfo;
5050
struct LabeledStmtInfo;
51+
struct LifetimeDescriptor;
5152
enum class MacroRole : uint32_t;
5253
class MacroIntroducedDeclName;
5354
enum class MacroIntroducedDeclNameKind;
@@ -964,6 +965,79 @@ BridgedInlineAttr BridgedInlineAttr_createParsed(BridgedASTContext cContext,
964965
BridgedSourceRange cRange,
965966
BridgedInlineKind cKind);
966967

968+
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedParsedLifetimeDependenceKind {
969+
BridgedParsedLifetimeDependenceKindDefault,
970+
BridgedParsedLifetimeDependenceKindScope,
971+
BridgedParsedLifetimeDependenceKindInherit,
972+
};
973+
974+
class BridgedLifetimeDescriptor {
975+
union Value {
976+
BridgedIdentifier name;
977+
unsigned index;
978+
979+
Value(BridgedIdentifier name) : name(name) {}
980+
Value(unsigned index) : index(index) {}
981+
Value() : name() {}
982+
} value;
983+
984+
enum DescriptorKind {
985+
Named,
986+
Ordered,
987+
Self,
988+
} kind;
989+
990+
BridgedParsedLifetimeDependenceKind dependenceKind;
991+
BridgedSourceLoc loc;
992+
993+
BridgedLifetimeDescriptor(Value value, DescriptorKind kind,
994+
BridgedParsedLifetimeDependenceKind dependenceKind,
995+
BridgedSourceLoc loc)
996+
: value(value), kind(kind), dependenceKind(dependenceKind), loc(loc) {}
997+
998+
public:
999+
SWIFT_NAME("forNamed(_:dependenceKind:loc:)")
1000+
static BridgedLifetimeDescriptor
1001+
forNamed(BridgedIdentifier name,
1002+
BridgedParsedLifetimeDependenceKind dependenceKind,
1003+
BridgedSourceLoc loc) {
1004+
return BridgedLifetimeDescriptor(name, DescriptorKind::Named,
1005+
dependenceKind, loc);
1006+
}
1007+
SWIFT_NAME("forOrdered(_:dependenceKind:loc:)")
1008+
static BridgedLifetimeDescriptor
1009+
forOrdered(size_t index, BridgedParsedLifetimeDependenceKind dependenceKind,
1010+
BridgedSourceLoc loc) {
1011+
return BridgedLifetimeDescriptor(index, DescriptorKind::Ordered,
1012+
dependenceKind, loc);
1013+
}
1014+
SWIFT_NAME("forSelf(dependenceKind:loc:)")
1015+
static BridgedLifetimeDescriptor
1016+
forSelf(BridgedParsedLifetimeDependenceKind dependenceKind,
1017+
BridgedSourceLoc loc) {
1018+
return BridgedLifetimeDescriptor({}, DescriptorKind::Self, dependenceKind,
1019+
loc);
1020+
}
1021+
1022+
swift::LifetimeDescriptor unbridged();
1023+
};
1024+
1025+
SWIFT_NAME("BridgedLifetimeEntry.createParsed(_:range:sources:)")
1026+
BridgedLifetimeEntry
1027+
BridgedLifetimeEntry_createParsed(BridgedASTContext cContext,
1028+
BridgedSourceRange cRange,
1029+
BridgedArrayRef cSources);
1030+
1031+
SWIFT_NAME("BridgedLifetimeEntry.createParsed(_:range:sources:target:)")
1032+
BridgedLifetimeEntry BridgedLifetimeEntry_createParsed(
1033+
BridgedASTContext cContext, BridgedSourceRange cRange,
1034+
BridgedArrayRef cSources, BridgedLifetimeDescriptor cTarget);
1035+
1036+
SWIFT_NAME("BridgedLifetimeAttr.createParsed(_:atLoc:range:entry:)")
1037+
BridgedLifetimeAttr BridgedLifetimeAttr_createParsed(
1038+
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
1039+
BridgedSourceRange cRange, BridgedLifetimeEntry cEntry);
1040+
9671041
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedMacroSyntax {
9681042
BridgedMacroSyntaxFreestanding,
9691043
BridgedMacroSyntaxAttached,

include/swift/AST/ASTBridgingWrappers.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ AST_BRIDGING_WRAPPER_NULLABLE(ArgumentList)
115115
AST_BRIDGING_WRAPPER_NULLABLE(AvailabilitySpec)
116116
AST_BRIDGING_WRAPPER_CONST_NONNULL(AvailabilityMacroMap)
117117
AST_BRIDGING_WRAPPER_NONNULL(PoundAvailableInfo)
118+
AST_BRIDGING_WRAPPER_NONNULL(LifetimeEntry)
118119

119120
// Non-AST types to generate wrappers for.
120121
AST_BRIDGING_WRAPPER_NULLABLE(DiagnosticEngine)

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8035,7 +8035,7 @@ ERROR(pack_iteration_where_clause_not_supported, none,
80358035
//------------------------------------------------------------------------------
80368036

80378037
ERROR(lifetime_dependence_invalid_param_name, none,
8038-
"invalid parameter name specified '%0'", (StringRef))
8038+
"invalid parameter name specified %0", (Identifier))
80398039
ERROR(lifetime_dependence_invalid_param_index, none,
80408040
"invalid parameter index specified '%0'", (unsigned))
80418041
ERROR(lifetime_dependence_invalid_self_in_static, none,

include/swift/AST/LifetimeDependence.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ enum class LifetimeDependenceKind : uint8_t { Inherit = 0, Scope };
4747
struct LifetimeDescriptor {
4848
union Value {
4949
struct {
50-
StringRef name;
50+
Identifier name;
5151
} Named;
5252
struct {
5353
unsigned index;
5454
bool isAddress;
5555
} Ordered;
5656
struct {
5757
} Self;
58-
Value(StringRef name) : Named({name}) {}
58+
Value(Identifier name) : Named({name}) {}
5959
Value(unsigned index, bool isAddress) : Ordered({index, isAddress}) {}
6060
Value() : Self() {}
6161
} value;
@@ -67,7 +67,7 @@ struct LifetimeDescriptor {
6767
SourceLoc loc;
6868

6969
private:
70-
LifetimeDescriptor(StringRef name,
70+
LifetimeDescriptor(Identifier name,
7171
ParsedLifetimeDependenceKind parsedLifetimeDependenceKind,
7272
SourceLoc loc)
7373
: value{name}, kind(DescriptorKind::Named),
@@ -84,7 +84,7 @@ struct LifetimeDescriptor {
8484

8585
public:
8686
static LifetimeDescriptor
87-
forNamed(StringRef name,
87+
forNamed(Identifier name,
8888
ParsedLifetimeDependenceKind parsedLifetimeDependenceKind,
8989
SourceLoc loc) {
9090
return {name, parsedLifetimeDependenceKind, loc};
@@ -106,7 +106,7 @@ struct LifetimeDescriptor {
106106
return parsedLifetimeDependenceKind;
107107
}
108108

109-
StringRef getName() const {
109+
Identifier getName() const {
110110
assert(kind == DescriptorKind::Named);
111111
return value.Named.name;
112112
}
@@ -130,13 +130,13 @@ struct LifetimeDescriptor {
130130
if (getDescriptorKind() != LifetimeDescriptor::DescriptorKind::Named) {
131131
return false;
132132
}
133-
return getName() == "immortal";
133+
return getName().str() == "immortal";
134134
}
135135

136136
std::string getString() const {
137137
switch (kind) {
138138
case DescriptorKind::Named:
139-
return getName().str();
139+
return getName().str().str();
140140
case DescriptorKind::Ordered:
141141
return std::to_string(getIndex());
142142
case DescriptorKind::Self:

lib/AST/ASTDumper.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5120,7 +5120,12 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
51205120
}
51215121
void visitLifetimeAttr(LifetimeAttr *Attr, Label label) {
51225122
printCommon(Attr, "lifetime_attr", label);
5123-
// TODO: Implement.
5123+
// FIXME: Improve, more detailed info.
5124+
printFieldRaw(
5125+
[&](raw_ostream &out) {
5126+
out << " " << Attr->getLifetimeEntry()->getString() << " ";
5127+
},
5128+
Label::optional("lifetime_entry"));
51245129
printFoot();
51255130
}
51265131
void visitMacroRoleAttr(MacroRoleAttr *Attr, Label label) {

lib/AST/Bridging/DeclAttributeBridging.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,72 @@ BridgedInlineAttr BridgedInlineAttr_createParsed(BridgedASTContext cContext,
360360
InlineAttr(cAtLoc.unbridged(), cRange.unbridged(), unbridged(cKind));
361361
}
362362

363+
static swift::ParsedLifetimeDependenceKind
364+
unbridged(BridgedParsedLifetimeDependenceKind kind) {
365+
switch (kind) {
366+
case BridgedParsedLifetimeDependenceKindDefault:
367+
return swift::ParsedLifetimeDependenceKind::Default;
368+
case BridgedParsedLifetimeDependenceKindScope:
369+
return swift::ParsedLifetimeDependenceKind::Scope;
370+
case BridgedParsedLifetimeDependenceKindInherit:
371+
return swift::ParsedLifetimeDependenceKind::Inherit;
372+
}
373+
llvm_unreachable("unhandled enum value");
374+
}
375+
376+
swift::LifetimeDescriptor BridgedLifetimeDescriptor::unbridged() {
377+
switch (kind) {
378+
case DescriptorKind::Named:
379+
return LifetimeDescriptor::forNamed(
380+
value.name.unbridged(), ::unbridged(dependenceKind), loc.unbridged());
381+
case DescriptorKind::Ordered:
382+
return LifetimeDescriptor::forOrdered(
383+
value.index, ::unbridged(dependenceKind), loc.unbridged());
384+
case DescriptorKind::Self:
385+
return LifetimeDescriptor::forSelf(::unbridged(dependenceKind),
386+
loc.unbridged());
387+
}
388+
llvm_unreachable("unhandled enum value");
389+
}
390+
391+
static BridgedLifetimeEntry BridgedLifetimeEntry_createParsedImpl(
392+
BridgedASTContext cContext, BridgedSourceRange cRange,
393+
BridgedArrayRef cSources,
394+
std::optional<BridgedLifetimeDescriptor> cTarget) {
395+
SmallVector<LifetimeDescriptor> sources;
396+
for (auto cSource : cSources.unbridged<BridgedLifetimeDescriptor>())
397+
sources.push_back(cSource.unbridged());
398+
std::optional<LifetimeDescriptor> target;
399+
if (cTarget)
400+
target = cTarget->unbridged();
401+
402+
return LifetimeEntry::create(cContext.unbridged(), cRange.Start.unbridged(),
403+
cRange.End.unbridged(), sources, target);
404+
}
405+
406+
BridgedLifetimeEntry
407+
BridgedLifetimeEntry_createParsed(BridgedASTContext cContext,
408+
BridgedSourceRange cRange,
409+
BridgedArrayRef cSources) {
410+
return BridgedLifetimeEntry_createParsedImpl(cContext, cRange, cSources,
411+
std::nullopt);
412+
}
413+
414+
BridgedLifetimeEntry BridgedLifetimeEntry_createParsed(
415+
BridgedASTContext cContext, BridgedSourceRange cRange,
416+
BridgedArrayRef cSources, BridgedLifetimeDescriptor cTarget) {
417+
return BridgedLifetimeEntry_createParsedImpl(cContext, cRange, cSources,
418+
cTarget);
419+
}
420+
421+
BridgedLifetimeAttr BridgedLifetimeAttr_createParsed(
422+
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
423+
BridgedSourceRange cRange, BridgedLifetimeEntry cEntry) {
424+
return LifetimeAttr::create(cContext.unbridged(), cAtLoc.unbridged(),
425+
cRange.unbridged(), /*implicit=*/false,
426+
cEntry.unbridged());
427+
}
428+
363429
BridgedMacroRole BridgedMacroRole_fromString(BridgedStringRef str) {
364430
// Match the role string to the known set of roles.
365431
auto role =

lib/AST/LifetimeDependence.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ getParamDeclFromDescriptor(AbstractFunctionDecl *afd,
287287
unsigned paramIndex = 0;
288288
ParamDecl *candidateParam = nullptr;
289289
for (auto *param : *afd->getParameters()) {
290-
if (param->getParameterName().str() == descriptor.getName()) {
290+
if (param->getParameterName() == descriptor.getName()) {
291291
candidateParam = param;
292292
break;
293293
}

0 commit comments

Comments
 (0)