Skip to content

[ASTGen] Start using 'TokenSyntax.rawText' #70201

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
Dec 5, 2023
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
8 changes: 7 additions & 1 deletion lib/ASTGen/Sources/ASTGen/Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import ASTBridging
import BasicBridging
import SwiftSyntax
@_spi(RawSyntax) import SwiftSyntax

protocol BridgedNullable: ExpressibleByNilLiteral {
associatedtype RawPtr
Expand Down Expand Up @@ -100,6 +100,12 @@ extension String {
}
}

extension SyntaxText {
var bridged: BridgedStringRef {
BridgedStringRef(data: self.baseAddress, count: self.count)
}
}

/// Allocate a copy of the given string as a null-terminated UTF-8 string.
func allocateBridgedString(
_ string: String
Expand Down
3 changes: 3 additions & 0 deletions lib/ASTGen/Sources/ASTGen/Literals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ extension ASTGenVisitor {
let openDelimiterOrQuoteLoc = (node.openingPounds ?? node.openingQuote).bridgedSourceLoc(in: self)

// FIXME: Handle interpolated strings.
// FIXME: Avoid 'String' instantiation
var segment = node.segments.first!.as(StringSegmentSyntax.self)!.content.text
return segment.withBridgedString { bridgedSegment in
return .createParsed(self.ctx, value: bridgedSegment, loc: openDelimiterOrQuoteLoc)
}
}

public func generate(integerLiteralExpr node: IntegerLiteralExprSyntax) -> BridgedIntegerLiteralExpr {
// FIXME: Avoid 'String' instantiation
// FIXME: Strip '_'.
var segment = node.literal.text
return segment.withBridgedString { bridgedSegment in
return .createParsed(ctx, value: bridgedSegment, loc: node.literal.bridgedSourceLoc(in: self))
Expand Down
13 changes: 5 additions & 8 deletions lib/ASTGen/Sources/ASTGen/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import ASTBridging
import BasicBridging
import SwiftDiagnostics
@_spi(ExperimentalLanguageFeatures) import SwiftSyntax
@_spi(ExperimentalLanguageFeatures) @_spi(RawSyntax) import SwiftSyntax

extension EffectSpecifiersSyntax {
var thrownError: TypeSyntax? {
Expand Down Expand Up @@ -203,14 +203,14 @@ extension ASTGenVisitor {
public func generate(metatypeType node: MetatypeTypeSyntax) -> BridgedTypeRepr {
let baseType = generate(type: node.baseType)
let tyLoc = node.metatypeSpecifier.bridgedSourceLoc(in: self)
if node.metatypeSpecifier.text == "Type" {
if node.metatypeSpecifier.rawText == "Type" {
return BridgedMetatypeTypeRepr.createParsed(
self.ctx,
base: baseType,
typeKeywordLoc: tyLoc
)
} else {
assert(node.metatypeSpecifier.text == "Protocol")
assert(node.metatypeSpecifier.rawText == "Protocol")
return BridgedProtocolTypeRepr.createParsed(
self.ctx,
base: baseType,
Expand Down Expand Up @@ -298,7 +298,7 @@ extension ASTGenVisitor {
public func generate(someOrAnyType node: SomeOrAnyTypeSyntax) -> BridgedTypeRepr {
let someOrAnyLoc = node.someOrAnySpecifier.bridgedSourceLoc(in: self)
let baseTy = generate(type: node.constraint)
if node.someOrAnySpecifier.text == "some" {
if node.someOrAnySpecifier.rawText == "some" {
return BridgedOpaqueReturnTypeRepr.createParsed(
self.ctx,
someKeywordLoc: someOrAnyLoc,
Expand Down Expand Up @@ -368,10 +368,7 @@ extension ASTGenVisitor {
}

let nameSyntax = identType.name
var name = nameSyntax.text
let typeAttrKind = name.withBridgedString { bridgedName in
BridgedTypeAttrKind(from: bridgedName)
}
let typeAttrKind = BridgedTypeAttrKind(from: nameSyntax.rawText.bridged)
let atLoc = attribute.atSign.bridgedSourceLoc(in: self)
let attrLoc = nameSyntax.bridgedSourceLoc(in: self)
switch typeAttrKind {
Expand Down