Skip to content

[ASTGen] Generate ForceValueExpr and BindOptionalExpr #76985

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
Oct 13, 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 @@ -1254,6 +1254,12 @@ BridgedAwaitExpr BridgedAwaitExpr_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cAwaitLoc,
BridgedExpr cSubExpr);

SWIFT_NAME("BridgedBindOptionalExpr.createParsed(_:subExpr:questionLoc:)")
BridgedBindOptionalExpr
BridgedBindOptionalExpr_createParsed(BridgedASTContext cContext,
BridgedExpr cSubExpr,
BridgedSourceLoc cQuestionLoc);

SWIFT_NAME("BridgedBooleanLiteralExpr.createParsed(_:value:loc:)")
BridgedBooleanLiteralExpr
BridgedBooleanLiteralExpr_createParsed(BridgedASTContext cContext, bool value,
Expand Down Expand Up @@ -1344,6 +1350,12 @@ BridgedForceTryExpr_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cTryLoc, BridgedExpr cSubExpr,
BridgedSourceLoc cExclaimLoc);

SWIFT_NAME("BridgedForceValueExpr.createParsed(_:subExpr:exclaimLoc:)")
BridgedForceValueExpr
BridgedForceValueExpr_createParsed(BridgedASTContext cContext,
BridgedExpr cSubExpr,
BridgedSourceLoc cExclaimLoc);

SWIFT_NAME(
"BridgedForcedCheckedCastExpr.createParsed(_:asLoc:exclaimLoc:type:)")
BridgedForcedCheckedCastExpr BridgedForcedCheckedCastExpr_createParsed(
Expand Down
18 changes: 18 additions & 0 deletions lib/AST/Bridging/ExprBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ BridgedAwaitExpr BridgedAwaitExpr_createParsed(BridgedASTContext cContext,
AwaitExpr(cAwaitLoc.unbridged(), cSubExpr.unbridged());
}

BridgedBindOptionalExpr
BridgedBindOptionalExpr_createParsed(BridgedASTContext cContext,
BridgedExpr cSubExpr,
BridgedSourceLoc cQuestionLoc) {
ASTContext &context = cContext.unbridged();
return new (context) BindOptionalExpr(cSubExpr.unbridged(),
cQuestionLoc.unbridged(), /*depth=*/0);
}

BridgedBooleanLiteralExpr
BridgedBooleanLiteralExpr_createParsed(BridgedASTContext cContext, bool value,
BridgedSourceLoc cTokenLoc) {
Expand Down Expand Up @@ -220,6 +229,15 @@ BridgedForceTryExpr_createParsed(BridgedASTContext cContext,
cTryLoc.unbridged(), cSubExpr.unbridged(), cExclaimLoc.unbridged());
}

BridgedForceValueExpr
BridgedForceValueExpr_createParsed(BridgedASTContext cContext,
BridgedExpr cSubExpr,
BridgedSourceLoc cExclaimLoc) {
ASTContext &context = cContext.unbridged();
return new (context)
ForceValueExpr(cSubExpr.unbridged(), cExclaimLoc.unbridged());
}

BridgedFloatLiteralExpr
BridgedFloatLiteralExpr_createParsed(BridgedASTContext cContext,
BridgedStringRef cStr,
Expand Down
33 changes: 25 additions & 8 deletions lib/ASTGen/Sources/ASTGen/Exprs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,18 @@ func isExprMigrated(_ node: ExprSyntax) -> Bool {
case .asExpr, .arrayExpr, .arrowExpr, .assignmentExpr, .awaitExpr, .binaryOperatorExpr,
.booleanLiteralExpr, .borrowExpr, .closureExpr, .consumeExpr, .copyExpr,
.discardAssignmentExpr, .declReferenceExpr, .dictionaryExpr, .doExpr,
.editorPlaceholderExpr, .floatLiteralExpr, .functionCallExpr, .genericSpecializationExpr,
.ifExpr, .infixOperatorExpr, .inOutExpr, .integerLiteralExpr, .isExpr, .memberAccessExpr,
.nilLiteralExpr, .packElementExpr, .packExpansionExpr, .patternExpr,
.editorPlaceholderExpr, .floatLiteralExpr, .forceUnwrapExpr, .functionCallExpr,
.genericSpecializationExpr, .ifExpr, .infixOperatorExpr, .inOutExpr,
.integerLiteralExpr, .isExpr, .memberAccessExpr, .nilLiteralExpr, .optionalChainingExpr,
.packElementExpr, .packExpansionExpr, .patternExpr,
.postfixOperatorExpr, .prefixOperatorExpr, .regexLiteralExpr, .sequenceExpr,
.simpleStringLiteralExpr, .subscriptCallExpr, .stringLiteralExpr, .superExpr,
.switchExpr, .tryExpr, .tupleExpr, .typeExpr, .unresolvedAsExpr, .unresolvedIsExpr,
.unresolvedTernaryExpr, .ternaryExpr:
break

// Known unimplemented kinds.
case .forceUnwrapExpr, .keyPathExpr, .macroExpansionExpr, .optionalChainingExpr, .postfixIfConfigExpr:
case .keyPathExpr, .macroExpansionExpr, .postfixIfConfigExpr:
return false

// Unknown expr kinds.
Expand Down Expand Up @@ -112,8 +113,8 @@ extension ASTGenVisitor {
preconditionFailure("EditorPlaceholderExpr is no longer generated by the parser")
case .floatLiteralExpr(let node):
return self.generate(floatLiteralExpr: node).asExpr
case .forceUnwrapExpr:
break
case .forceUnwrapExpr(let node):
return self.generate(forceUnwrapExpr: node).asExpr
case .functionCallExpr(let node):
return self.generate(functionCallExpr: node).asExpr
case .genericSpecializationExpr(let node):
Expand All @@ -138,8 +139,8 @@ extension ASTGenVisitor {
break
case .nilLiteralExpr(let node):
return self.generate(nilLiteralExpr: node).asExpr
case .optionalChainingExpr:
// Need special care to wrap the entire postfix chain with OptionalEvaluationExpr.
case .optionalChainingExpr(let node):
return self.generate(optionalChainingExpr: node).asExpr
break
case .packElementExpr(let node):
return self.generate(packElementExpr: node).asExpr
Expand Down Expand Up @@ -291,6 +292,14 @@ extension ASTGenVisitor {
)
}

func generate(forceUnwrapExpr node: ForceUnwrapExprSyntax) -> BridgedForceValueExpr {
return .createParsed(
self.ctx,
subExpr: self.generate(expr: node.expression),
exclaimLoc: self.generateSourceLoc(node.exclamationMark)
)
}

func generateArgumentList(
leftParen: TokenSyntax?,
labeledExprList: LabeledExprListSyntax,
Expand Down Expand Up @@ -539,6 +548,14 @@ extension ASTGenVisitor {
)
}

func generate(optionalChainingExpr node: OptionalChainingExprSyntax) -> BridgedBindOptionalExpr {
return .createParsed(
self.ctx,
subExpr: self.generate(expr: node.expression),
questionLoc: self.generateSourceLoc(node.questionMark)
)
}

func generate(packElementExpr node: PackElementExprSyntax) -> BridgedPackElementExpr {
return .createParsed(
self.ctx,
Expand Down
11 changes: 11 additions & 0 deletions test/ASTGen/exprs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ struct TestStruct {
_ = self.method(arg:_:).self
_ = Ty.`Self` == Ty.`self`
}

var optSelf: Self? { self }
}

func testSequence(arg1: Int, arg2: () -> Int, arg3: Any) {
Expand Down Expand Up @@ -178,3 +180,12 @@ func testSpecializeExpr() {
_ = Generic<Int>.self
_ = Generic<Int>()
}

func testOptionalChain(value: TestStruct) {
let _: TestStruct? = value.optSelf?.optSelf!
let _: TestStruct = value.optSelf!
let _: TestStruct = value.optSelf.self!

var value: Int? = 1
value? += 1
}