Skip to content
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
7 changes: 7 additions & 0 deletions unified/extractor/ast_types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ supertypes:
- bulk_importing_pattern
- ignore_pattern
- generic_type_expr
- modified_type_expr
- inferred_type_expr
# An element of an `unresolved_operator_sequence`: either an operand (`expr`)
# or one of the infix operators separating the operands.
Expand Down Expand Up @@ -553,5 +554,11 @@ named:
base: expr
type_argument*: expr

# A type with a modifier. In Swift this can be specifiers or attributes, such
# as `inout T`, `sending T` or `@escaping () -> Void`.
modified_type_expr:
modifier*: modifier
type: expr

# A modifier such as 'static', 'public', or 'async'. For now this is just a leaf node with a string value.
modifier:
19 changes: 18 additions & 1 deletion unified/extractor/src/languages/swift/swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,24 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
// becomes a `modifier`; its source text is the modifier spelling.
rule!((attribute) @m => (modifier #{m})),
rule!((declModifier) @m => (modifier #{m})),
// Type specifiers (`inout`, `sending`, ...) are mapped to modifiers.
rule!((simpleTypeSpecifier) @m => (modifier #{m})),
rule!((lifetimeTypeSpecifier) @m => (modifier #{m})),
rule!((nonisolatedTypeSpecifier) @m => (modifier #{m})),
// An attributed type is mapped to a modified type of its base type.
rule!(
(attributedType
specifiers: _* @specs
attributes: _* @attrs
lateSpecifiers: _* @late
baseType: @base)
=>
(modified_type_expr
modifier: {specs}
modifier: {attrs}
modifier: {late}
type: {base})
),
// Preserve the `super` keyword as a dedicated expression, normally used
// as the base of a member access (`super.foo`).
rule!((superExpr superKeyword: @@keyword) => (super_expr #{keyword})),
Expand Down Expand Up @@ -1475,7 +1493,6 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
// currently map them to the unified AST. Syntax nested inside one of
// these roots is discarded with its parent and needs no separate rule.
rule!((actorDecl) => (unsupported_node)),
rule!((attributedType) => (unsupported_node)),
rule!((borrowExpr) => (unsupported_node)),
rule!((classRestrictionType) => (unsupported_node)),
rule!((compositionType) => (unsupported_node)),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
func f<D, T>(
a: inout D,
b: sending T,
c: borrowing T,
d: consuming T,
e: @escaping (T) -> Void,
g: @Sendable @escaping () -> D
) {}

---

sourceFile
endOfFileToken: endOfFile
statements:
codeBlockItem
item:
functionDecl
attributes:
body:
codeBlock
leftBrace: {
rightBrace: }
statements:
name: identifier "f"
genericParameterClause:
genericParameterClause
parameters:
genericParameter
attributes:
name: identifier "D"
trailingComma: ,
genericParameter
attributes:
name: identifier "T"
leftAngle: <
rightAngle: >
modifiers:
signature:
functionSignature
parameterClause:
functionParameterClause
leftParen: (
rightParen: )
parameters:
functionParameter
colon: :
attributes:
modifiers:
trailingComma: ,
type:
attributedType
attributes:
baseType:
identifierType
name: identifier "D"
lateSpecifiers:
specifiers:
simpleTypeSpecifier
specifier: inout
firstName: identifier "a"
functionParameter
colon: :
attributes:
modifiers:
trailingComma: ,
type:
attributedType
attributes:
baseType:
identifierType
name: identifier "T"
lateSpecifiers:
specifiers:
simpleTypeSpecifier
specifier: sending
firstName: identifier "b"
functionParameter
colon: :
attributes:
modifiers:
trailingComma: ,
type:
attributedType
attributes:
baseType:
identifierType
name: identifier "T"
lateSpecifiers:
specifiers:
simpleTypeSpecifier
specifier: borrowing
firstName: identifier "c"
functionParameter
colon: :
attributes:
modifiers:
trailingComma: ,
type:
attributedType
attributes:
baseType:
identifierType
name: identifier "T"
lateSpecifiers:
specifiers:
simpleTypeSpecifier
specifier: consuming
firstName: identifier "d"
functionParameter
colon: :
attributes:
modifiers:
trailingComma: ,
type:
attributedType
attributes:
attribute
atSign: @
attributeName:
identifierType
name: identifier "escaping"
baseType:
functionType
leftParen: (
rightParen: )
parameters:
tupleTypeElement
type:
identifierType
name: identifier "T"
returnClause:
returnClause
arrow: ->
type:
identifierType
name: identifier "Void"
lateSpecifiers:
specifiers:
firstName: identifier "e"
functionParameter
colon: :
attributes:
modifiers:
type:
attributedType
attributes:
attribute
atSign: @
attributeName:
identifierType
name: identifier "Sendable"
attribute
atSign: @
attributeName:
identifierType
name: identifier "escaping"
baseType:
functionType
leftParen: (
rightParen: )
parameters:
returnClause:
returnClause
arrow: ->
type:
identifierType
name: identifier "D"
lateSpecifiers:
specifiers:
firstName: identifier "g"
funcKeyword: func

---

top_level source="⟨body⟩"
body:
block source="⟨stmt⟩"
stmt:
function_declaration source="func ⟨name_node⟩<⟨type_parameter⟩, ⟨type_parameter⟩>(\n ⟨parameter⟩,\n ⟨parameter⟩,\n ⟨parameter⟩,\n ⟨parameter⟩,\n ⟨parameter⟩,\n ⟨parameter⟩\n) ⟨body⟩"
name_node: identifier "f" source="f"
type_parameter:
type_parameter source="⟨name_node⟩"
name_node: identifier "D" source="D"
type_parameter source="⟨name_node⟩"
name_node: identifier "T" source="T"
parameter:
parameter source="⟨external_name_node⟩⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "a" source="a"
type:
modified_type_expr source="⟨modifier⟩ ⟨type⟩"
modifier: modifier "inout" source="inout"
type: identifier "D" source="D"
pattern: identifier "a" source="a"
parameter source="⟨external_name_node⟩⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "b" source="b"
type:
modified_type_expr source="⟨modifier⟩ ⟨type⟩"
modifier: modifier "sending" source="sending"
type: identifier "T" source="T"
pattern: identifier "b" source="b"
parameter source="⟨external_name_node⟩⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "c" source="c"
type:
modified_type_expr source="⟨modifier⟩ ⟨type⟩"
modifier: modifier "borrowing" source="borrowing"
type: identifier "T" source="T"
pattern: identifier "c" source="c"
parameter source="⟨external_name_node⟩⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "d" source="d"
type:
modified_type_expr source="⟨modifier⟩ ⟨type⟩"
modifier: modifier "consuming" source="consuming"
type: identifier "T" source="T"
pattern: identifier "d" source="d"
parameter source="⟨external_name_node⟩⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "e" source="e"
type:
modified_type_expr source="⟨modifier⟩ ⟨type⟩"
modifier: modifier "@escaping" source="@escaping"
type:
function_expr source="(⟨parameter⟩) -> ⟨return_type⟩"
parameter:
parameter source="⟨type⟩"
type: identifier "T" source="T"
return_type: identifier "Void" source="Void"
pattern: identifier "e" source="e"
parameter source="⟨external_name_node⟩⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "g" source="g"
type:
modified_type_expr source="⟨modifier⟩ ⟨modifier⟩ ⟨type⟩"
modifier:
modifier "@Sendable" source="@Sendable"
modifier "@escaping" source="@escaping"
type:
function_expr source="() -> ⟨return_type⟩"
return_type: identifier "D" source="D"
pattern: identifier "g" source="g"
body: block "{}" source="{}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
func f<D, T>(
a: inout D,
b: sending T,
c: borrowing T,
d: consuming T,
e: @escaping (T) -> Void,
g: @Sendable @escaping () -> D
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ top_level source="⟨body⟩"
parameter:
parameter source="⟨external_name_node⟩ ⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "_" source="_"
type: unsupported_node "inout Int" source="inout Int"
type:
modified_type_expr source="⟨modifier⟩ ⟨type⟩"
modifier: modifier "inout" source="inout"
type: identifier "Int" source="Int"
pattern: identifier "x" source="x"
body:
block source="{\n ⟨stmt⟩\n}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ top_level source="⟨body⟩"
variable_declaration source="⟨modifier⟩ ⟨pattern⟩: ⟨type⟩ = ⟨value⟩"
modifier: modifier "let" source="let"
pattern: identifier "callback" source="callback"
type: unsupported_node "@convention(c) () -> Void" source="@convention(c) () -> Void"
type:
modified_type_expr source="⟨modifier⟩ ⟨type⟩"
modifier: modifier "@convention(c)" source="@convention(c)"
type:
function_expr source="() -> ⟨return_type⟩"
return_type: identifier "Void" source="Void"
value:
function_expr source="{}"
body: block source=""
Loading
Loading