Skip to content

[ASTGen] Generate Swift key-path expressions #77398

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 1 commit into from
Nov 6, 2024
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
12 changes: 12 additions & 0 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,18 @@ BridgedIsExpr BridgedIsExpr_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cIsLoc,
BridgedTypeRepr cType);

SWIFT_NAME("BridgedKeyPathDotExpr.createParsed(_:loc:)")
BridgedKeyPathDotExpr
BridgedKeyPathDotExpr_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cLoc);

SWIFT_NAME("BridgedKeyPathExpr.createParsed(_:backslashLoc:parsedRoot:"
"parsedPath:hasLeadingDot:)")
BridgedKeyPathExpr BridgedKeyPathExpr_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cBackslashLoc,
BridgedNullableExpr cParsedRoot, BridgedNullableExpr cParsedPath,
bool hasLeadingDot);

SWIFT_NAME("BridgedMacroExpansionExpr.createParsed(_:poundLoc:macroNameRef:"
"macroNameLoc:leftAngleLoc:genericArgs:rightAngleLoc:args:)")
BridgedMacroExpansionExpr BridgedMacroExpansionExpr_createParsed(
Expand Down
2 changes: 2 additions & 0 deletions lib/AST/Bridging/DeclBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ static void setParsedMembers(IterableDeclContext *IDC,
}
}

IDC->setMaybeHasOperatorDeclarations();
IDC->setMaybeHasNestedClassDeclarations();
ctx.evaluator.cacheOutput(
ParseMembersRequest{IDC},
FingerprintAndMembers{std::nullopt, ctx.AllocateCopy(members)});
Expand Down
15 changes: 15 additions & 0 deletions lib/AST/Bridging/ExprBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,21 @@ BridgedIntegerLiteralExpr_createParsed(BridgedASTContext cContext,
IntegerLiteralExpr(cStr.unbridged(), cTokenLoc.unbridged());
}

BridgedKeyPathDotExpr
BridgedKeyPathDotExpr_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cLoc) {
return new (cContext.unbridged()) KeyPathDotExpr(cLoc.unbridged());
}

BridgedKeyPathExpr BridgedKeyPathExpr_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cBackslashLoc,
BridgedNullableExpr cParsedRoot, BridgedNullableExpr cParsedPath,
bool hasLeadingDot) {
return KeyPathExpr::createParsed(
cContext.unbridged(), cBackslashLoc.unbridged(), cParsedRoot.unbridged(),
cParsedPath.unbridged(), hasLeadingDot);
}

BridgedSuperRefExpr
BridgedSuperRefExpr_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cSuperLoc) {
Expand Down
9 changes: 8 additions & 1 deletion lib/ASTGen/Sources/ASTGen/Decls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,13 @@ extension ASTGenVisitor {
accessorDecl node: AccessorDeclSyntax,
for storage: BridgedAbstractStorageDecl
) -> BridgedAccessorDecl? {
// TODO: Attributes and modifier.
// FIXME: Attributes.
var attrs = BridgedDeclAttributes()
if
let modifier = node.modifier,
let attr = self.generate(declModifier: modifier) {
attrs.add(attr)
}

guard let kind = self.generate(accessorSpecifier: node.accessorSpecifier) else {
// TODO: We could potentially recover if this is the first accessor by treating
Expand All @@ -368,6 +374,7 @@ extension ASTGenVisitor {
throwsSpecifierLoc: self.generateSourceLoc(node.effectSpecifiers?.throwsClause),
thrownType: self.generate(type: node.effectSpecifiers?.thrownError)
)
accessor.asDecl.setAttrs(attrs)
if let body = node.body {
self.withDeclContext(accessor.asDeclContext) {
accessor.setParsedBody(self.generate(codeBlock: body))
Expand Down
122 changes: 115 additions & 7 deletions lib/ASTGen/Sources/ASTGen/Exprs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,14 @@ func isExprMigrated(_ node: ExprSyntax) -> Bool {
.discardAssignmentExpr, .declReferenceExpr, .dictionaryExpr, .doExpr,
.editorPlaceholderExpr, .floatLiteralExpr, .forceUnwrapExpr, .functionCallExpr,
.genericSpecializationExpr, .ifExpr, .infixOperatorExpr, .inOutExpr,
.integerLiteralExpr, .isExpr, .macroExpansionExpr, .memberAccessExpr, .nilLiteralExpr,
.integerLiteralExpr, .isExpr, .keyPathExpr, .macroExpansionExpr, .memberAccessExpr, .nilLiteralExpr,
.optionalChainingExpr, .packElementExpr, .packExpansionExpr, .patternExpr, .postfixIfConfigExpr,
.postfixOperatorExpr, .prefixOperatorExpr, .regexLiteralExpr, .sequenceExpr,
.simpleStringLiteralExpr, .subscriptCallExpr, .stringLiteralExpr, .superExpr,
.switchExpr, .tryExpr, .tupleExpr, .typeExpr, .unresolvedAsExpr, .unresolvedIsExpr,
.unresolvedTernaryExpr, .ternaryExpr:
break

// Known unimplemented kinds.
case .keyPathExpr:
return false

// Unknown expr kinds.
case _ where current.is(ExprSyntax.self):
return false
Expand Down Expand Up @@ -129,8 +125,8 @@ extension ASTGenVisitor {
return self.generate(integerLiteralExpr: node).asExpr
case .isExpr:
preconditionFailure("IsExprSyntax only appear after operator folding")
case .keyPathExpr:
break
case .keyPathExpr(let node):
return self.generate(keyPathExpr: node)
case .macroExpansionExpr(let node):
return self.generate(macroExpansionExpr: node).asExpr
case .memberAccessExpr(let node):
Expand Down Expand Up @@ -535,6 +531,118 @@ extension ASTGenVisitor {
)
}

func generate(keyPathComponent node: KeyPathComponentSyntax, baseExpr: BridgedExpr) -> BridgedExpr {
switch node.component {
case .property(let prop):
let dotLoc = self.generateSourceLoc(node.period)
if prop.declName.baseName.presence == .missing {
return BridgedErrorExpr.create(
self.ctx,
loc: BridgedSourceRange(start: dotLoc, end: dotLoc)
).asExpr
} else if prop.declName.baseName.keywordKind == .`self` {
// TODO: Diagnose if there's arguments
assert(prop.declName.argumentNames == nil)

return BridgedDotSelfExpr.createParsed(
self.ctx,
subExpr: baseExpr,
dotLoc: dotLoc,
selfLoc: self.generateSourceLoc(prop.declName)
).asExpr
} else {
let declNameRef = self.generateDeclNameRef(declReferenceExpr: prop.declName)
return BridgedUnresolvedDotExpr.createParsed(
self.ctx,
base: baseExpr,
dotLoc: dotLoc,
name: declNameRef.name,
nameLoc: declNameRef.loc
).asExpr
}

case .optional(let comp):
if comp.questionOrExclamationMark.rawText == "!" {
return BridgedForceValueExpr.createParsed(
self.ctx,
subExpr: baseExpr,
exclaimLoc: self.generateSourceLoc(comp.questionOrExclamationMark)
).asExpr
} else {
return BridgedBindOptionalExpr.createParsed(
self.ctx,
subExpr: baseExpr,
questionLoc: self.generateSourceLoc(comp.questionOrExclamationMark)
).asExpr
}

case .subscript(let comp):
return BridgedSubscriptExpr.createParsed(
self.ctx,
baseExpr: baseExpr,
args: self.generateArgumentList(
leftParen: comp.leftSquare,
labeledExprList: comp.arguments,
rightParen: comp.rightSquare,
trailingClosure: nil,
additionalTrailingClosures: nil
)
).asExpr
}
}

func generate(keyPathExpr node: KeyPathExprSyntax) -> BridgedExpr {
guard !node.components.isEmpty else {
// FIXME: Diagnostics KeyPath expression without any component.
return BridgedErrorExpr.create(self.ctx, loc: self.generateSourceRange(node)).asExpr
}

var rootExpr: BridgedExpr?
if let parsedType = node.root, !parsedType.is(MissingTypeSyntax.self) {
let rootType = self.generate(type: parsedType)
rootExpr = BridgedTypeExpr.createParsed(self.ctx, type: rootType).asExpr
} else {
rootExpr = nil
}

var inRoot = rootExpr != nil
var pathExpr: BridgedExpr? = nil

for component in node.components {
if inRoot {
switch component.component {
case // "root" expression is separated by '.?' or '.[idx]'
.optional(_) where component.period != nil,
.subscript(_) where component.period != nil:
inRoot = false
default:
rootExpr = self.generate(keyPathComponent: component, baseExpr: rootExpr!)
continue
}
}

if pathExpr == nil {
// 'KeyPathDotExpr' is a dummy base expression.
pathExpr = BridgedKeyPathDotExpr.createParsed(
self.ctx,
// Use 'component' instead of 'component.period' because period can
// be nil (e.g. '\?'), but 'loc' must be a valid location.
loc: self.generateSourceLoc(component)
).asExpr
}

pathExpr = self.generate(keyPathComponent: component, baseExpr: pathExpr!)
}

return BridgedKeyPathExpr.createParsed(
self.ctx,
backslashLoc: self.generateSourceLoc(node.backslash),
parsedRoot: rootExpr.asNullable,
parsedPath: pathExpr.asNullable,
hasLeadingDot: rootExpr == nil
).asExpr
}

struct FreestandingMacroExpansionInfo {
var poundLoc: BridgedSourceLoc
var macroNameRef: BridgedDeclNameRef
Expand Down
Loading