Skip to content

Commit 09f49b0

Browse files
committed
[ASTGen] Generate FloatLiteralExpr
1 parent 7d9ba2d commit 09f49b0

File tree

6 files changed

+71
-27
lines changed

6 files changed

+71
-27
lines changed

include/swift/AST/ASTBridging.h

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

203-
SWIFT_NAME("BridgedASTContext.copy(self:string:)")
203+
SWIFT_NAME("BridgedASTContext.allocate(self:size:alignment:)")
204204
BRIDGED_INLINE
205-
BridgedStringRef BridgedASTContext_copyString(BridgedASTContext cContext,
206-
BridgedStringRef cStr);
205+
void *_Nullable BridgedASTContext_allocate(BridgedASTContext bridged,
206+
size_t size, size_t alignment);
207+
208+
SWIFT_NAME("BridgedASTContext.allocateCopy(self:string:)")
209+
BRIDGED_INLINE
210+
BridgedStringRef
211+
BridgedASTContext_allocateCopyString(BridgedASTContext cContext,
212+
BridgedStringRef cStr);
207213

208214
SWIFT_NAME("BridgedASTContext.getIdentifier(self:_:)")
209215
BridgedIdentifier BridgedASTContext_getIdentifier(BridgedASTContext cContext,
@@ -1318,6 +1324,12 @@ SWIFT_NAME("BridgedErrorExpr.create(_:loc:)")
13181324
BridgedErrorExpr BridgedErrorExpr_create(BridgedASTContext cContext,
13191325
BridgedSourceRange cRange);
13201326

1327+
SWIFT_NAME("BridgedFloatLiteralExpr.createParsed(_:value:loc:)")
1328+
BridgedFloatLiteralExpr
1329+
BridgedFloatLiteralExpr_createParsed(BridgedASTContext cContext,
1330+
BridgedStringRef cStr,
1331+
BridgedSourceLoc cTokenLoc);
1332+
13211333
SWIFT_NAME("BridgedForceTryExpr.createParsed(_:tryLoc:subExpr:exclaimLoc:)")
13221334
BridgedForceTryExpr
13231335
BridgedForceTryExpr_createParsed(BridgedASTContext cContext,

include/swift/AST/ASTBridgingImpl.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ BridgedASTContext BridgedASTContext_fromRaw(void * _Nonnull ptr) {
3232
return *static_cast<swift::ASTContext *>(ptr);
3333
}
3434

35-
BridgedStringRef BridgedASTContext_copyString(BridgedASTContext bridged,
36-
BridgedStringRef cStr) {
35+
BRIDGED_INLINE
36+
void *_Nullable BridgedASTContext_allocate(BridgedASTContext bridged,
37+
size_t size, size_t alignment) {
38+
return bridged.unbridged().Allocate(size, alignment);
39+
}
40+
41+
BridgedStringRef BridgedASTContext_allocateCopyString(BridgedASTContext bridged,
42+
BridgedStringRef cStr) {
3743
return bridged.unbridged().AllocateCopy(cStr.unbridged());
3844
}
3945

lib/AST/Bridging/ExprBridging.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ BridgedForceTryExpr_createParsed(BridgedASTContext cContext,
211211
cTryLoc.unbridged(), cSubExpr.unbridged(), cExclaimLoc.unbridged());
212212
}
213213

214+
BridgedFloatLiteralExpr
215+
BridgedFloatLiteralExpr_createParsed(BridgedASTContext cContext,
216+
BridgedStringRef cStr,
217+
BridgedSourceLoc cTokenLoc) {
218+
ASTContext &context = cContext.unbridged();
219+
return new (context)
220+
FloatLiteralExpr(cStr.unbridged(), cTokenLoc.unbridged());
221+
}
222+
214223
BridgedForcedCheckedCastExpr BridgedForcedCheckedCastExpr_createParsed(
215224
BridgedASTContext cContext, BridgedSourceLoc cAsLoc,
216225
BridgedSourceLoc cExclaimLoc, BridgedTypeRepr cType) {
@@ -224,8 +233,8 @@ BridgedIntegerLiteralExpr_createParsed(BridgedASTContext cContext,
224233
BridgedStringRef cStr,
225234
BridgedSourceLoc cTokenLoc) {
226235
ASTContext &context = cContext.unbridged();
227-
auto str = context.AllocateCopy(cStr.unbridged());
228-
return new (context) IntegerLiteralExpr(str, cTokenLoc.unbridged());
236+
return new (context)
237+
IntegerLiteralExpr(cStr.unbridged(), cTokenLoc.unbridged());
229238
}
230239

231240
BridgedSuperRefExpr

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func isExprMigrated(_ node: ExprSyntax) -> Bool {
4242
// Known implemented kinds.
4343
case .arrayExpr, .arrowExpr, .assignmentExpr, .awaitExpr, .binaryOperatorExpr,
4444
.booleanLiteralExpr, .borrowExpr, .closureExpr, .consumeExpr, .copyExpr,
45-
.discardAssignmentExpr, .declReferenceExpr, .dictionaryExpr,
45+
.discardAssignmentExpr, .declReferenceExpr, .dictionaryExpr, .floatLiteralExpr,
4646
.functionCallExpr, .ifExpr, .integerLiteralExpr, .memberAccessExpr,
4747
.nilLiteralExpr, .packElementExpr, .packExpansionExpr, .patternExpr,
4848
.postfixOperatorExpr, .prefixOperatorExpr, .regexLiteralExpr, .sequenceExpr,
@@ -53,11 +53,10 @@ func isExprMigrated(_ node: ExprSyntax) -> Bool {
5353

5454
// Known unimplemented kinds.
5555
case .asExpr,
56-
.doExpr, .editorPlaceholderExpr, .floatLiteralExpr, .forceUnwrapExpr,
57-
.inOutExpr, .infixOperatorExpr, .isExpr, .keyPathExpr,
58-
.macroExpansionExpr, .optionalChainingExpr,
59-
.postfixIfConfigExpr, .genericSpecializationExpr, .switchExpr,
60-
.ternaryExpr:
56+
.doExpr, .editorPlaceholderExpr, .forceUnwrapExpr, .inOutExpr,
57+
.infixOperatorExpr, .isExpr, .keyPathExpr, .macroExpansionExpr,
58+
.optionalChainingExpr, .postfixIfConfigExpr, .genericSpecializationExpr,
59+
.switchExpr, .ternaryExpr:
6160
return false
6261

6362
// Unknown expr kinds.
@@ -113,8 +112,8 @@ extension ASTGenVisitor {
113112
break
114113
case .editorPlaceholderExpr:
115114
break
116-
case .floatLiteralExpr:
117-
break
115+
case .floatLiteralExpr(let node):
116+
return self.generate(floatLiteralExpr: node).asExpr
118117
case .forceUnwrapExpr:
119118
break
120119
case .functionCallExpr(let node):

lib/ASTGen/Sources/ASTGen/Literals.swift

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import ASTBridging
1414
@_spi(Compiler) import SwiftParser
15-
import SwiftSyntax
15+
@_spi(RawSyntax) import SwiftSyntax
1616
@_spi(CompilerInterface) import _CompilerRegexParser
1717

1818
extension ASTGenVisitor {
@@ -215,17 +215,35 @@ extension ASTGenVisitor {
215215
)
216216
}
217217

218-
func generate(integerLiteralExpr node: IntegerLiteralExprSyntax) -> BridgedIntegerLiteralExpr {
219-
// FIXME: Avoid 'String' instantiation
220-
// FIXME: Strip '_'.
221-
var segment = node.literal.text
222-
return segment.withBridgedString { bridgedSegment in
223-
return .createParsed(
224-
ctx,
225-
value: bridgedSegment,
226-
loc: self.generateSourceLoc(node.literal)
227-
)
218+
func copyAndStripUnderscores(text: SyntaxText) -> BridgedStringRef {
219+
assert(!text.isEmpty)
220+
let start = self.ctx.allocate(size: text.count, alignment: 1)!
221+
.bindMemory(to: UInt8.self, capacity: text.count)
222+
var ptr = start
223+
for chr in text where chr != UInt8(ascii: "_") {
224+
ptr.initialize(to: chr)
225+
ptr += 1
228226
}
227+
return BridgedStringRef(
228+
data: start,
229+
count: start.distance(to: ptr)
230+
)
231+
}
232+
233+
func generate(integerLiteralExpr node: IntegerLiteralExprSyntax) -> BridgedIntegerLiteralExpr {
234+
return .createParsed(
235+
self.ctx,
236+
value: self.copyAndStripUnderscores(text: node.literal.rawText),
237+
loc: self.generateSourceLoc(node)
238+
)
239+
}
240+
241+
func generate(floatLiteralExpr node: FloatLiteralExprSyntax) -> BridgedFloatLiteralExpr {
242+
return .createParsed(
243+
self.ctx,
244+
value: self.copyAndStripUnderscores(text: node.literal.rawText),
245+
loc: self.generateSourceLoc(node)
246+
)
229247
}
230248

231249
func generate(booleanLiteralExpr node: BooleanLiteralExprSyntax) -> BridgedBooleanLiteralExpr {

lib/ASTGen/Sources/ASTGen/Regex.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension ASTGenVisitor {
4141
)
4242
// Copy the regex string to the ASTContext.
4343
let regexToEmitStr = regexToEmit.withBridgedString {
44-
self.ctx.copy(string: $0)
44+
self.ctx.allocateCopy(string: $0)
4545
}
4646

4747
return BridgedRegexLiteralExpr.createParsed(

0 commit comments

Comments
 (0)