Skip to content

[ASTGen] Generate LifetimeAttr #79427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Fingerprint;
class Identifier;
class IfConfigClauseRangeInfo;
struct LabeledStmtInfo;
struct LifetimeDescriptor;
enum class MacroRole : uint32_t;
class MacroIntroducedDeclName;
enum class MacroIntroducedDeclNameKind;
Expand Down Expand Up @@ -964,6 +965,79 @@ BridgedInlineAttr BridgedInlineAttr_createParsed(BridgedASTContext cContext,
BridgedSourceRange cRange,
BridgedInlineKind cKind);

enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedParsedLifetimeDependenceKind {
BridgedParsedLifetimeDependenceKindDefault,
BridgedParsedLifetimeDependenceKindScope,
BridgedParsedLifetimeDependenceKindInherit,
};

class BridgedLifetimeDescriptor {
union Value {
BridgedIdentifier name;
unsigned index;

Value(BridgedIdentifier name) : name(name) {}
Value(unsigned index) : index(index) {}
Value() : name() {}
} value;

enum DescriptorKind {
Named,
Ordered,
Self,
} kind;

BridgedParsedLifetimeDependenceKind dependenceKind;
BridgedSourceLoc loc;

BridgedLifetimeDescriptor(Value value, DescriptorKind kind,
BridgedParsedLifetimeDependenceKind dependenceKind,
BridgedSourceLoc loc)
: value(value), kind(kind), dependenceKind(dependenceKind), loc(loc) {}

public:
SWIFT_NAME("forNamed(_:dependenceKind:loc:)")
static BridgedLifetimeDescriptor
forNamed(BridgedIdentifier name,
BridgedParsedLifetimeDependenceKind dependenceKind,
BridgedSourceLoc loc) {
return BridgedLifetimeDescriptor(name, DescriptorKind::Named,
dependenceKind, loc);
}
SWIFT_NAME("forOrdered(_:dependenceKind:loc:)")
static BridgedLifetimeDescriptor
forOrdered(size_t index, BridgedParsedLifetimeDependenceKind dependenceKind,
BridgedSourceLoc loc) {
return BridgedLifetimeDescriptor(index, DescriptorKind::Ordered,
dependenceKind, loc);
}
SWIFT_NAME("forSelf(dependenceKind:loc:)")
static BridgedLifetimeDescriptor
forSelf(BridgedParsedLifetimeDependenceKind dependenceKind,
BridgedSourceLoc loc) {
return BridgedLifetimeDescriptor({}, DescriptorKind::Self, dependenceKind,
loc);
}

swift::LifetimeDescriptor unbridged();
};

SWIFT_NAME("BridgedLifetimeEntry.createParsed(_:range:sources:)")
BridgedLifetimeEntry
BridgedLifetimeEntry_createParsed(BridgedASTContext cContext,
BridgedSourceRange cRange,
BridgedArrayRef cSources);

SWIFT_NAME("BridgedLifetimeEntry.createParsed(_:range:sources:target:)")
BridgedLifetimeEntry BridgedLifetimeEntry_createParsed(
BridgedASTContext cContext, BridgedSourceRange cRange,
BridgedArrayRef cSources, BridgedLifetimeDescriptor cTarget);

SWIFT_NAME("BridgedLifetimeAttr.createParsed(_:atLoc:range:entry:)")
BridgedLifetimeAttr BridgedLifetimeAttr_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
BridgedSourceRange cRange, BridgedLifetimeEntry cEntry);

enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedMacroSyntax {
BridgedMacroSyntaxFreestanding,
BridgedMacroSyntaxAttached,
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/ASTBridgingWrappers.def
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ AST_BRIDGING_WRAPPER_NULLABLE(ArgumentList)
AST_BRIDGING_WRAPPER_NULLABLE(AvailabilitySpec)
AST_BRIDGING_WRAPPER_CONST_NONNULL(AvailabilityMacroMap)
AST_BRIDGING_WRAPPER_NONNULL(PoundAvailableInfo)
AST_BRIDGING_WRAPPER_NONNULL(LifetimeEntry)

// Non-AST types to generate wrappers for.
AST_BRIDGING_WRAPPER_NULLABLE(DiagnosticEngine)
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -8035,7 +8035,7 @@ ERROR(pack_iteration_where_clause_not_supported, none,
//------------------------------------------------------------------------------

ERROR(lifetime_dependence_invalid_param_name, none,
"invalid parameter name specified '%0'", (StringRef))
"invalid parameter name specified %0", (Identifier))
ERROR(lifetime_dependence_invalid_param_index, none,
"invalid parameter index specified '%0'", (unsigned))
ERROR(lifetime_dependence_invalid_self_in_static, none,
Expand Down
14 changes: 7 additions & 7 deletions include/swift/AST/LifetimeDependence.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ enum class LifetimeDependenceKind : uint8_t { Inherit = 0, Scope };
struct LifetimeDescriptor {
union Value {
struct {
StringRef name;
Identifier name;
} Named;
struct {
unsigned index;
bool isAddress;
} Ordered;
struct {
} Self;
Value(StringRef name) : Named({name}) {}
Value(Identifier name) : Named({name}) {}
Value(unsigned index, bool isAddress) : Ordered({index, isAddress}) {}
Value() : Self() {}
} value;
Expand All @@ -67,7 +67,7 @@ struct LifetimeDescriptor {
SourceLoc loc;

private:
LifetimeDescriptor(StringRef name,
LifetimeDescriptor(Identifier name,
ParsedLifetimeDependenceKind parsedLifetimeDependenceKind,
SourceLoc loc)
: value{name}, kind(DescriptorKind::Named),
Expand All @@ -84,7 +84,7 @@ struct LifetimeDescriptor {

public:
static LifetimeDescriptor
forNamed(StringRef name,
forNamed(Identifier name,
ParsedLifetimeDependenceKind parsedLifetimeDependenceKind,
SourceLoc loc) {
return {name, parsedLifetimeDependenceKind, loc};
Expand All @@ -106,7 +106,7 @@ struct LifetimeDescriptor {
return parsedLifetimeDependenceKind;
}

StringRef getName() const {
Identifier getName() const {
assert(kind == DescriptorKind::Named);
return value.Named.name;
}
Expand All @@ -130,13 +130,13 @@ struct LifetimeDescriptor {
if (getDescriptorKind() != LifetimeDescriptor::DescriptorKind::Named) {
return false;
}
return getName() == "immortal";
return getName().str() == "immortal";
}

std::string getString() const {
switch (kind) {
case DescriptorKind::Named:
return getName().str();
return getName().str().str();
case DescriptorKind::Ordered:
return std::to_string(getIndex());
case DescriptorKind::Self:
Expand Down
7 changes: 6 additions & 1 deletion lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5120,7 +5120,12 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
}
void visitLifetimeAttr(LifetimeAttr *Attr, Label label) {
printCommon(Attr, "lifetime_attr", label);
// TODO: Implement.
// FIXME: Improve, more detailed info.
printFieldRaw(
[&](raw_ostream &out) {
out << " " << Attr->getLifetimeEntry()->getString() << " ";
},
Label::optional("lifetime_entry"));
printFoot();
}
void visitMacroRoleAttr(MacroRoleAttr *Attr, Label label) {
Expand Down
66 changes: 66 additions & 0 deletions lib/AST/Bridging/DeclAttributeBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,72 @@ BridgedInlineAttr BridgedInlineAttr_createParsed(BridgedASTContext cContext,
InlineAttr(cAtLoc.unbridged(), cRange.unbridged(), unbridged(cKind));
}

static swift::ParsedLifetimeDependenceKind
unbridged(BridgedParsedLifetimeDependenceKind kind) {
switch (kind) {
case BridgedParsedLifetimeDependenceKindDefault:
return swift::ParsedLifetimeDependenceKind::Default;
case BridgedParsedLifetimeDependenceKindScope:
return swift::ParsedLifetimeDependenceKind::Scope;
case BridgedParsedLifetimeDependenceKindInherit:
return swift::ParsedLifetimeDependenceKind::Inherit;
}
llvm_unreachable("unhandled enum value");
}

swift::LifetimeDescriptor BridgedLifetimeDescriptor::unbridged() {
switch (kind) {
case DescriptorKind::Named:
return LifetimeDescriptor::forNamed(
value.name.unbridged(), ::unbridged(dependenceKind), loc.unbridged());
case DescriptorKind::Ordered:
return LifetimeDescriptor::forOrdered(
value.index, ::unbridged(dependenceKind), loc.unbridged());
case DescriptorKind::Self:
return LifetimeDescriptor::forSelf(::unbridged(dependenceKind),
loc.unbridged());
}
llvm_unreachable("unhandled enum value");
}

static BridgedLifetimeEntry BridgedLifetimeEntry_createParsedImpl(
BridgedASTContext cContext, BridgedSourceRange cRange,
BridgedArrayRef cSources,
std::optional<BridgedLifetimeDescriptor> cTarget) {
SmallVector<LifetimeDescriptor> sources;
for (auto cSource : cSources.unbridged<BridgedLifetimeDescriptor>())
sources.push_back(cSource.unbridged());
std::optional<LifetimeDescriptor> target;
if (cTarget)
target = cTarget->unbridged();

return LifetimeEntry::create(cContext.unbridged(), cRange.Start.unbridged(),
cRange.End.unbridged(), sources, target);
}

BridgedLifetimeEntry
BridgedLifetimeEntry_createParsed(BridgedASTContext cContext,
BridgedSourceRange cRange,
BridgedArrayRef cSources) {
return BridgedLifetimeEntry_createParsedImpl(cContext, cRange, cSources,
std::nullopt);
}

BridgedLifetimeEntry BridgedLifetimeEntry_createParsed(
BridgedASTContext cContext, BridgedSourceRange cRange,
BridgedArrayRef cSources, BridgedLifetimeDescriptor cTarget) {
return BridgedLifetimeEntry_createParsedImpl(cContext, cRange, cSources,
cTarget);
}

BridgedLifetimeAttr BridgedLifetimeAttr_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
BridgedSourceRange cRange, BridgedLifetimeEntry cEntry) {
return LifetimeAttr::create(cContext.unbridged(), cAtLoc.unbridged(),
cRange.unbridged(), /*implicit=*/false,
cEntry.unbridged());
}

BridgedMacroRole BridgedMacroRole_fromString(BridgedStringRef str) {
// Match the role string to the known set of roles.
auto role =
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/LifetimeDependence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ getParamDeclFromDescriptor(AbstractFunctionDecl *afd,
unsigned paramIndex = 0;
ParamDecl *candidateParam = nullptr;
for (auto *param : *afd->getParameters()) {
if (param->getParameterName().str() == descriptor.getName()) {
if (param->getParameterName() == descriptor.getName()) {
candidateParam = param;
break;
}
Expand Down
Loading