From 66138e8395ebb7e011e69b664329e65763ab600c Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Fri, 11 Oct 2024 14:26:34 -0700 Subject: [PATCH] [ASTGen] Generate ForceValueExpr and BindOptionalExpr --- include/swift/AST/ASTBridging.h | 12 ++++++++++ lib/AST/Bridging/ExprBridging.cpp | 18 +++++++++++++++ lib/ASTGen/Sources/ASTGen/Exprs.swift | 33 ++++++++++++++++++++------- test/ASTGen/exprs.swift | 11 +++++++++ 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/include/swift/AST/ASTBridging.h b/include/swift/AST/ASTBridging.h index eff06caefd82a..3f2992942867b 100644 --- a/include/swift/AST/ASTBridging.h +++ b/include/swift/AST/ASTBridging.h @@ -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, @@ -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( diff --git a/lib/AST/Bridging/ExprBridging.cpp b/lib/AST/Bridging/ExprBridging.cpp index 87b061e6eee52..150c75204173c 100644 --- a/lib/AST/Bridging/ExprBridging.cpp +++ b/lib/AST/Bridging/ExprBridging.cpp @@ -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) { @@ -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, diff --git a/lib/ASTGen/Sources/ASTGen/Exprs.swift b/lib/ASTGen/Sources/ASTGen/Exprs.swift index 32f21f9104af6..4637ecd8a2c2c 100644 --- a/lib/ASTGen/Sources/ASTGen/Exprs.swift +++ b/lib/ASTGen/Sources/ASTGen/Exprs.swift @@ -44,9 +44,10 @@ 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, @@ -54,7 +55,7 @@ func isExprMigrated(_ node: ExprSyntax) -> Bool { break // Known unimplemented kinds. - case .forceUnwrapExpr, .keyPathExpr, .macroExpansionExpr, .optionalChainingExpr, .postfixIfConfigExpr: + case .keyPathExpr, .macroExpansionExpr, .postfixIfConfigExpr: return false // Unknown expr kinds. @@ -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): @@ -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 @@ -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, @@ -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, diff --git a/test/ASTGen/exprs.swift b/test/ASTGen/exprs.swift index 4ab7d2e256e3c..b67d74cdc886b 100644 --- a/test/ASTGen/exprs.swift +++ b/test/ASTGen/exprs.swift @@ -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) { @@ -178,3 +180,12 @@ func testSpecializeExpr() { _ = Generic.self _ = Generic() } + +func testOptionalChain(value: TestStruct) { + let _: TestStruct? = value.optSelf?.optSelf! + let _: TestStruct = value.optSelf! + let _: TestStruct = value.optSelf.self! + + var value: Int? = 1 + value? += 1 +}