Skip to content

Commit 7d9ba2d

Browse files
committed
[ASTGen] Generate RegexLiteralExpr, SubscriptExpr, and SuperRefExpr
1 parent 23b721f commit 7d9ba2d

File tree

7 files changed

+173
-14
lines changed

7 files changed

+173
-14
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ SWIFT_NAME("BridgedASTContext.init(raw:)")
200200
BRIDGED_INLINE
201201
BridgedASTContext BridgedASTContext_fromRaw(void * _Nonnull ptr);
202202

203+
SWIFT_NAME("BridgedASTContext.copy(self:string:)")
204+
BRIDGED_INLINE
205+
BridgedStringRef BridgedASTContext_copyString(BridgedASTContext cContext,
206+
BridgedStringRef cStr);
207+
203208
SWIFT_NAME("BridgedASTContext.getIdentifier(self:_:)")
204209
BridgedIdentifier BridgedASTContext_getIdentifier(BridgedASTContext cContext,
205210
BridgedStringRef cStr);
@@ -1309,6 +1314,10 @@ BridgedDotSelfExpr BridgedDotSelfExpr_createParsed(BridgedASTContext cContext,
13091314
BridgedSourceLoc cDotLoc,
13101315
BridgedSourceLoc cSelfLoc);
13111316

1317+
SWIFT_NAME("BridgedErrorExpr.create(_:loc:)")
1318+
BridgedErrorExpr BridgedErrorExpr_create(BridgedASTContext cContext,
1319+
BridgedSourceRange cRange);
1320+
13121321
SWIFT_NAME("BridgedForceTryExpr.createParsed(_:tryLoc:subExpr:exclaimLoc:)")
13131322
BridgedForceTryExpr
13141323
BridgedForceTryExpr_createParsed(BridgedASTContext cContext,
@@ -1371,6 +1380,18 @@ BridgedPrefixUnaryExpr
13711380
BridgedPrefixUnaryExpr_createParsed(BridgedASTContext cContext,
13721381
BridgedExpr oper, BridgedExpr operand);
13731382

1383+
SWIFT_NAME("BridgedRegexLiteralExpr."
1384+
"allocateCaptureStructureSerializationBuffer(_:size:)")
1385+
BridgedData BridgedRegexLiteralExpr_allocateCaptureStructureSerializationBuffer(
1386+
BridgedASTContext cContext, SwiftInt size);
1387+
1388+
SWIFT_NAME("BridgedRegexLiteralExpr.createParsed(_:loc:regexText:version:"
1389+
"captureStructure:)")
1390+
BridgedRegexLiteralExpr BridgedRegexLiteralExpr_createParsed(
1391+
BridgedASTContext cContext, BridgedSourceLoc cLoc,
1392+
BridgedStringRef cRegexText, SwiftInt version,
1393+
BridgedData cCaptureStructure);
1394+
13741395
SWIFT_NAME("BridgedSequenceExpr.createParsed(_:exprs:)")
13751396
BridgedSequenceExpr BridgedSequenceExpr_createParsed(BridgedASTContext cContext,
13761397
BridgedArrayRef exprs);
@@ -1387,6 +1408,17 @@ BridgedStringLiteralExpr_createParsed(BridgedASTContext cContext,
13871408
BridgedStringRef cStr,
13881409
BridgedSourceLoc cTokenLoc);
13891410

1411+
SWIFT_NAME("BridgedSuperRefExpr.createParsed(_:superLoc:)")
1412+
BridgedSuperRefExpr
1413+
BridgedSuperRefExpr_createParsed(BridgedASTContext cContext,
1414+
BridgedSourceLoc cSuperLoc);
1415+
1416+
SWIFT_NAME("BridgedSubscriptExpr.createParsed(_:baseExpr:args:)")
1417+
BridgedSubscriptExpr
1418+
BridgedSubscriptExpr_createParsed(BridgedASTContext cContext,
1419+
BridgedExpr cBaseExpr,
1420+
BridgedArgumentList cArgs);
1421+
13901422
SWIFT_NAME("BridgedTapExpr.create(_:body:)")
13911423
BridgedTapExpr BridgedTapExpr_create(BridgedASTContext cContext,
13921424
BridgedBraceStmt cBody);

include/swift/AST/ASTBridgingImpl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_AST_ASTBRIDGINGIMPL_H
1414
#define SWIFT_AST_ASTBRIDGINGIMPL_H
1515

16+
#include "swift/AST/ASTContext.h"
1617
#include "swift/AST/Decl.h"
1718
#include "swift/AST/ProtocolConformance.h"
1819
#include "swift/AST/ProtocolConformanceRef.h"
@@ -31,6 +32,11 @@ BridgedASTContext BridgedASTContext_fromRaw(void * _Nonnull ptr) {
3132
return *static_cast<swift::ASTContext *>(ptr);
3233
}
3334

35+
BridgedStringRef BridgedASTContext_copyString(BridgedASTContext bridged,
36+
BridgedStringRef cStr) {
37+
return bridged.unbridged().AllocateCopy(cStr.unbridged());
38+
}
39+
3440
//===----------------------------------------------------------------------===//
3541
// MARK: BridgedDeclObj
3642
//===----------------------------------------------------------------------===//

lib/AST/Bridging/ExprBridging.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ BridgedDotSelfExpr BridgedDotSelfExpr_createParsed(BridgedASTContext cContext,
198198
cSubExpr.unbridged(), cDotLoc.unbridged(), cSelfLoc.unbridged());
199199
}
200200

201+
BridgedErrorExpr BridgedErrorExpr_create(BridgedASTContext cContext,
202+
BridgedSourceRange cRange) {
203+
return new (cContext.unbridged()) ErrorExpr(cRange.unbridged());
204+
}
205+
201206
BridgedForceTryExpr
202207
BridgedForceTryExpr_createParsed(BridgedASTContext cContext,
203208
BridgedSourceLoc cTryLoc, BridgedExpr cSubExpr,
@@ -223,6 +228,22 @@ BridgedIntegerLiteralExpr_createParsed(BridgedASTContext cContext,
223228
return new (context) IntegerLiteralExpr(str, cTokenLoc.unbridged());
224229
}
225230

231+
BridgedSuperRefExpr
232+
BridgedSuperRefExpr_createParsed(BridgedASTContext cContext,
233+
BridgedSourceLoc cSuperLoc) {
234+
ASTContext &context = cContext.unbridged();
235+
return new (context)
236+
SuperRefExpr(/*Self=*/nullptr, cSuperLoc.unbridged(), /*Implicit=*/false);
237+
}
238+
239+
BridgedSubscriptExpr
240+
BridgedSubscriptExpr_createParsed(BridgedASTContext cContext,
241+
BridgedExpr cBaseExpr,
242+
BridgedArgumentList cArgs) {
243+
return SubscriptExpr::create(cContext.unbridged(), cBaseExpr.unbridged(),
244+
cArgs.unbridged());
245+
}
246+
226247
BridgedInterpolatedStringLiteralExpr
227248
BridgedInterpolatedStringLiteralExpr_createParsed(
228249
BridgedASTContext cContext, BridgedSourceLoc cLoc, size_t literalCapacity,
@@ -283,6 +304,27 @@ BridgedPrefixUnaryExpr_createParsed(BridgedASTContext cContext,
283304
operand.unbridged());
284305
}
285306

307+
BridgedData BridgedRegexLiteralExpr_allocateCaptureStructureSerializationBuffer(
308+
BridgedASTContext cContext, SwiftInt size) {
309+
auto buf = cContext.unbridged().AllocateUninitialized<uint8_t>(
310+
RegexLiteralExpr::getCaptureStructureSerializationAllocationSize(
311+
unsigned(size)));
312+
return BridgedData(reinterpret_cast<const char *>(buf.data()), buf.size());
313+
}
314+
315+
BridgedRegexLiteralExpr BridgedRegexLiteralExpr_createParsed(
316+
BridgedASTContext cContext, BridgedSourceLoc cLoc,
317+
BridgedStringRef cRegexText, SwiftInt version,
318+
BridgedData cCaptureStructure) {
319+
ArrayRef<uint8_t> captures(
320+
reinterpret_cast<const uint8_t *>(cCaptureStructure.BaseAddress),
321+
cCaptureStructure.Length);
322+
323+
return RegexLiteralExpr::createParsed(cContext.unbridged(), cLoc.unbridged(),
324+
cRegexText.unbridged(),
325+
unsigned(version), captures);
326+
}
327+
286328
BridgedSequenceExpr BridgedSequenceExpr_createParsed(BridgedASTContext cContext,
287329
BridgedArrayRef exprs) {
288330
return SequenceExpr::create(cContext.unbridged(), exprs.unbridged<Expr *>());

lib/ASTGen/Sources/ASTGen/Diagnostics.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,12 @@ struct UnknownAccessorSpecifierError: ASTGenError {
110110
"unknown accessor specifier '\(specifier.text)'"
111111
}
112112
}
113+
114+
struct RegexParserError: ASTGenError {
115+
var message: String
116+
init(_ message: String) {
117+
self.message = message
118+
}
119+
120+
var severity: DiagnosticSeverity { .error }
121+
}

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ func isExprMigrated(_ node: ExprSyntax) -> Bool {
4545
.discardAssignmentExpr, .declReferenceExpr, .dictionaryExpr,
4646
.functionCallExpr, .ifExpr, .integerLiteralExpr, .memberAccessExpr,
4747
.nilLiteralExpr, .packElementExpr, .packExpansionExpr, .patternExpr,
48-
.postfixOperatorExpr, .prefixOperatorExpr, .sequenceExpr,
49-
.stringLiteralExpr, .tryExpr, .tupleExpr, .typeExpr, .unresolvedAsExpr,
50-
.unresolvedIsExpr, .unresolvedTernaryExpr:
48+
.postfixOperatorExpr, .prefixOperatorExpr, .regexLiteralExpr, .sequenceExpr,
49+
.simpleStringLiteralExpr, .subscriptCallExpr, .stringLiteralExpr, .superExpr,
50+
.tryExpr, .tupleExpr, .typeExpr, .unresolvedAsExpr, .unresolvedIsExpr,
51+
.unresolvedTernaryExpr:
5152
break
5253

5354
// Known unimplemented kinds.
5455
case .asExpr,
5556
.doExpr, .editorPlaceholderExpr, .floatLiteralExpr, .forceUnwrapExpr,
5657
.inOutExpr, .infixOperatorExpr, .isExpr, .keyPathExpr,
5758
.macroExpansionExpr, .optionalChainingExpr,
58-
.postfixIfConfigExpr, .regexLiteralExpr, .genericSpecializationExpr,
59-
.simpleStringLiteralExpr, .subscriptCallExpr, .superExpr, .switchExpr,
59+
.postfixIfConfigExpr, .genericSpecializationExpr, .switchExpr,
6060
.ternaryExpr:
6161
return false
6262

@@ -156,22 +156,22 @@ extension ASTGenVisitor {
156156
return self.generate(postfixOperatorExpr: node).asExpr
157157
case .prefixOperatorExpr(let node):
158158
return self.generate(prefixOperatorExpr: node).asExpr
159-
case .regexLiteralExpr:
160-
break
159+
case .regexLiteralExpr(let node):
160+
return self.generate(regexLiteralExpr: node)
161161
case .sequenceExpr(let node):
162162
return self.generate(sequenceExpr: node)
163163
case .simpleStringLiteralExpr:
164-
break
164+
preconditionFailure("SimpleStringLiteral expression only appear in attributes")
165165
case .stringLiteralExpr(let node):
166166
return self.generate(stringLiteralExpr: node)
167-
case .subscriptCallExpr:
168-
break
169-
case .superExpr:
170-
break
167+
case .subscriptCallExpr(let node):
168+
return self.generate(subscriptCallExpr: node).asExpr
169+
case .superExpr(let node):
170+
return self.generate(superExpr: node).asExpr
171171
case .switchExpr:
172172
break
173-
case .ternaryExpr:
174-
break
173+
case .ternaryExpr(let node):
174+
preconditionFailure("Ternary expression only appear after operator folding")
175175
case .tryExpr(let node):
176176
return self.generate(tryExpr: node)
177177
case .tupleExpr(let node):
@@ -591,6 +591,23 @@ extension ASTGenVisitor {
591591
).asExpr
592592
}
593593

594+
func generate(subscriptCallExpr node: SubscriptCallExprSyntax) -> BridgedSubscriptExpr {
595+
let callee = generate(expr: node.calledExpression)
596+
let arguments = generateArgumentList(
597+
leftParen: node.leftSquare,
598+
labeledExprList: node.arguments,
599+
rightParen: node.rightSquare,
600+
trailingClosure: node.trailingClosure,
601+
additionalTrailingClosures: node.additionalTrailingClosures
602+
)
603+
604+
return .createParsed(self.ctx, baseExpr: callee, args: arguments)
605+
}
606+
607+
func generate(superExpr node: SuperExprSyntax) -> BridgedSuperRefExpr {
608+
return .createParsed(self.ctx, superLoc: self.generateSourceLoc(node))
609+
}
610+
594611
func generate(tryExpr node: TryExprSyntax) -> BridgedExpr {
595612
let tryLoc = self.generateSourceLoc(node.tryKeyword)
596613
let subExpr = self.generate(expr: node.expression)

lib/ASTGen/Sources/ASTGen/Literals.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import ASTBridging
1414
@_spi(Compiler) import SwiftParser
1515
import SwiftSyntax
16+
@_spi(CompilerInterface) import _CompilerRegexParser
1617

1718
extension ASTGenVisitor {
1819
func generate(stringLiteralExpr node: StringLiteralExprSyntax) -> BridgedExpr {

lib/ASTGen/Sources/ASTGen/Regex.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,62 @@
1212

1313
import ASTBridging
1414
import BasicBridging
15+
import SwiftDiagnostics
16+
import SwiftSyntax
1517

1618
#if canImport(_CompilerRegexParser)
1719
@_spi(CompilerInterface) import _CompilerRegexParser
1820

21+
extension ASTGenVisitor {
22+
func generate(regexLiteralExpr node: RegexLiteralExprSyntax) -> BridgedExpr {
23+
let str = node.trimmedDescription
24+
let captureBuffer = BridgedRegexLiteralExpr
25+
.allocateCaptureStructureSerializationBuffer(self.ctx, size: str.utf8.count)
26+
let captureBufferOut = UnsafeMutableRawBufferPointer(
27+
start: UnsafeMutableRawPointer(mutating: captureBuffer.baseAddress),
28+
count: captureBuffer.count
29+
)
30+
31+
let loc = self.generateSourceLoc(node);
32+
33+
do {
34+
// FIXME: We need to plumb through the 'regexToEmit' result to the caller.
35+
// For now, it is the same as the input.
36+
var regexToEmit: String
37+
let version: Int
38+
(regexToEmit, version) = try swiftCompilerParseRegexLiteral(
39+
str,
40+
captureBufferOut: captureBufferOut
41+
)
42+
// Copy the regex string to the ASTContext.
43+
let regexToEmitStr = regexToEmit.withBridgedString {
44+
self.ctx.copy(string: $0)
45+
}
46+
47+
return BridgedRegexLiteralExpr.createParsed(
48+
self.ctx,
49+
loc: loc,
50+
regexText: regexToEmitStr,
51+
version: version,
52+
captureStructure: captureBuffer
53+
).asExpr
54+
} catch let error as _CompilerRegexParser.CompilerParseError {
55+
let offset = error.location != nil ? str.utf8.offset(of: error.location!) : 0
56+
let position = node.positionAfterSkippingLeadingTrivia.advanced(by: offset)
57+
self.diagnose(
58+
Diagnostic(
59+
node: node.regex,
60+
position: position,
61+
message: RegexParserError(error.message)
62+
)
63+
)
64+
return BridgedErrorExpr.create(self.ctx, loc: BridgedSourceRange(start: loc, end: loc)).asExpr
65+
} catch {
66+
fatalError("Expected CompilerParseError")
67+
}
68+
}
69+
}
70+
1971
/// Bridging between C++ lexer and swiftCompilerLexRegexLiteral.
2072
///
2173
/// Attempt to lex a regex literal string.

0 commit comments

Comments
 (0)