Skip to content

Commit 2774987

Browse files
authored
Merge pull request #1556 from ahoppen/ahoppen/5.9/cherry-pick-2
Cherry-pick multiple PRs to `release/5.9`
2 parents f73d080 + 8d68708 commit 2774987

File tree

140 files changed

+7998
-13200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+7998
-13200
lines changed

BUILD.bazel

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,6 @@ swift_syntax_library(
9595
],
9696
)
9797

98-
swift_syntax_library(
99-
name = "SwiftSyntaxParser",
100-
deps = [
101-
":SwiftParser",
102-
":SwiftSyntax",
103-
],
104-
)
105-
10698
swift_syntax_library(
10799
name = "SwiftRefactor",
108100
deps = [

CONTRIBUTING.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ If you can’t find it in your Schemes, you need to manually add it using Produc
4343
4444
### XCTests
4545
46-
The `SwiftSyntaxParser` module (the legacy parser) of this repository depends on the C++ parser library (`_InternalSwiftSyntaxParser.dylib`) to parse source code.
47-
The syntax node definitions of that parser library need to match those used by your SwiftSyntax checkout.
48-
Most of the time, the parser library included in the latest Swift Development Snapshot will fulfill this requirement.
49-
50-
To run the tests in Xcode, select the latest Swift Development Snapshot in Xcode -> Toolchains, select the SwiftSyntax-Package scheme and hit Product -> Test.
46+
To run the tests in Xcode, select the SwiftSyntax-Package scheme and hit Product -> Test.
5147
5248
You can also run the tests from the command line using
5349
```bash
@@ -68,7 +64,7 @@ Tip: Running SwiftSyntax’s self-parse tests takes the majority of testing time
6864

6965
### `lit`-based tests
7066

71-
A few tests of the `SwiftSyntaxParser` module (the legacy parser), which test the interaction between SwiftSyntax and the C++ parser library (`_InternalSwiftSyntaxParser.dylib`) are based LLVM’s `lit` and `FileCheck` tools.
67+
A few tests are based LLVM’s `lit` and `FileCheck` tools.
7268
To run these, build `FileCheck`, e.g. by building the Swift compiler and run the tests using the following command:
7369
```bash
7470
./build-script.py test --toolchain /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-<recent date>.xctoolchain/usr --skip-lit-tests --filecheck-exec /path/to/build/Release+Asserts/llvm-macosx-x86_64/bin/FileCheck

CodeGeneration/Sources/SyntaxSupport/Classification.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@ public class SyntaxClassification {
2828
public class ChildClassification {
2929
public let parent: Node
3030
public let childIndex: Int
31-
public let isToken: Bool
32-
public let classification: SyntaxClassification?
33-
public let force: Bool
31+
public let child: Child
32+
public var isToken: Bool { child.isToken }
33+
public var classification: SyntaxClassification? { child.classification }
34+
public var force: Bool { child.forceClassification }
3435

3536
public init(node: Node, childIndex: Int, child: Child) {
3637
self.parent = node
3738
self.childIndex = childIndex
38-
self.isToken = child.syntaxKind.hasSuffix("Token")
39-
self.classification = child.classification
40-
self.force = child.forceClassification
39+
self.child = child
4140
}
4241
}
4342

CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,31 @@ public let COMMON_NODES: [Node] = [
8383
]
8484
),
8585

86-
// decl-effect-specifiers -> (async | reasync)? (throws | rethrows)?
86+
// accessor-effect-specifiers -> (async)? (throws)?
8787
Node(
88-
name: "DeclEffectSpecifiers",
88+
name: "AccessorEffectSpecifiers",
89+
nameForDiagnostics: "accessor specifiers",
90+
kind: "Syntax",
91+
traits: [
92+
"EffectSpecifiers"
93+
],
94+
children: [
95+
Child(
96+
name: "AsyncSpecifier",
97+
kind: .token(choices: [.keyword(text: "async")]),
98+
isOptional: true
99+
),
100+
Child(
101+
name: "ThrowsSpecifier",
102+
kind: .token(choices: [.keyword(text: "throws")]),
103+
isOptional: true
104+
),
105+
]
106+
),
107+
108+
// funtion-effect-specifiers -> (async | reasync)? (throws | rethrows)?
109+
Node(
110+
name: "FunctionEffectSpecifiers",
89111
nameForDiagnostics: "effect specifiers",
90112
kind: "Syntax",
91113
traits: [

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public let DECL_NODES: [Node] = [
9696
),
9797
Child(
9898
name: "EffectSpecifiers",
99-
kind: .node(kind: "DeclEffectSpecifiers"),
99+
kind: .node(kind: "AccessorEffectSpecifiers"),
100100
isOptional: true
101101
),
102102
Child(
@@ -196,7 +196,7 @@ public let DECL_NODES: [Node] = [
196196
isOptional: true
197197
),
198198
Child(
199-
name: "Members",
199+
name: "MemberBlock",
200200
kind: .node(kind: "MemberDeclBlock")
201201
),
202202
]
@@ -312,7 +312,7 @@ public let DECL_NODES: [Node] = [
312312
isOptional: true
313313
),
314314
Child(
315-
name: "Members",
315+
name: "MemberBlock",
316316
kind: .node(kind: "MemberDeclBlock")
317317
),
318318
]
@@ -332,7 +332,7 @@ public let DECL_NODES: [Node] = [
332332
),
333333
Child(
334334
name: "Detail",
335-
kind: .token(choices: [.token(tokenKind: "IdentifierToken"), .keyword(text: "set")])
335+
kind: .token(choices: [.token(tokenKind: "IdentifierToken")])
336336
),
337337
Child(
338338
name: "RightParen",
@@ -348,7 +348,44 @@ public let DECL_NODES: [Node] = [
348348
children: [
349349
Child(
350350
name: "Name",
351-
kind: .token(choices: [.keyword(text: "class"), .keyword(text: "convenience"), .keyword(text: "dynamic"), .keyword(text: "final"), .keyword(text: "infix"), .keyword(text: "lazy"), .keyword(text: "optional"), .keyword(text: "override"), .keyword(text: "postfix"), .keyword(text: "prefix"), .keyword(text: "required"), .keyword(text: "static"), .keyword(text: "unowned"), .keyword(text: "weak"), .keyword(text: "private"), .keyword(text: "fileprivate"), .keyword(text: "internal"), .keyword(text: "public"), .keyword(text: "open"), .keyword(text: "mutating"), .keyword(text: "nonmutating"), .keyword(text: "indirect"), .keyword(text: "__consuming"), .keyword(text: "borrowing"), .keyword(text: "consuming"), .keyword(text: "actor"), .keyword(text: "async"), .keyword(text: "distributed"), .keyword(text: "isolated"), .keyword(text: "nonisolated"), .keyword(text: "_const"), .keyword(text: "_local"), .keyword(text: "package")]),
351+
kind: .token(choices: [
352+
.keyword(text: "__consuming"),
353+
.keyword(text: "__setter_access"),
354+
.keyword(text: "_const"),
355+
.keyword(text: "_local"),
356+
.keyword(text: "actor"),
357+
.keyword(text: "async"),
358+
.keyword(text: "borrowing"),
359+
.keyword(text: "class"),
360+
.keyword(text: "consuming"),
361+
.keyword(text: "convenience"),
362+
.keyword(text: "distributed"),
363+
.keyword(text: "dynamic"),
364+
.keyword(text: "fileprivate"),
365+
.keyword(text: "final"),
366+
.keyword(text: "indirect"),
367+
.keyword(text: "infix"),
368+
.keyword(text: "internal"),
369+
.keyword(text: "isolated"),
370+
.keyword(text: "lazy"),
371+
.keyword(text: "mutating"),
372+
.keyword(text: "nonisolated"),
373+
.keyword(text: "nonmutating"),
374+
.keyword(text: "open"),
375+
.keyword(text: "optional"),
376+
.keyword(text: "override"),
377+
.keyword(text: "package"),
378+
.keyword(text: "postfix"),
379+
.keyword(text: "prefix"),
380+
.keyword(text: "private"),
381+
.keyword(text: "public"),
382+
.keyword(text: "reasync"),
383+
.keyword(text: "required"),
384+
.keyword(text: "setter_access"),
385+
.keyword(text: "static"),
386+
.keyword(text: "unowned"),
387+
.keyword(text: "weak"),
388+
]),
352389
classification: "Attribute"
353390
),
354391
Child(
@@ -654,7 +691,7 @@ public let DECL_NODES: [Node] = [
654691
isOptional: true
655692
),
656693
Child(
657-
name: "Members",
694+
name: "MemberBlock",
658695
kind: .node(kind: "MemberDeclBlock"),
659696
description: "The cases and other members of this enum."
660697
),
@@ -709,7 +746,7 @@ public let DECL_NODES: [Node] = [
709746
isOptional: true
710747
),
711748
Child(
712-
name: "Members",
749+
name: "MemberBlock",
713750
kind: .node(kind: "MemberDeclBlock")
714751
),
715752
]
@@ -842,7 +879,7 @@ public let DECL_NODES: [Node] = [
842879
),
843880

844881
// function-signature ->
845-
// '(' parameter-list? ')' decl-effect-specifiers? return-clause?
882+
// '(' parameter-list? ')' function-effect-specifiers? return-clause?
846883
Node(
847884
name: "FunctionSignature",
848885
nameForDiagnostics: "function signature",
@@ -854,7 +891,7 @@ public let DECL_NODES: [Node] = [
854891
),
855892
Child(
856893
name: "EffectSpecifiers",
857-
kind: .node(kind: "DeclEffectSpecifiers"),
894+
kind: .node(kind: "FunctionEffectSpecifiers"),
858895
isOptional: true
859896
),
860897
Child(
@@ -1024,6 +1061,16 @@ public let DECL_NODES: [Node] = [
10241061
Node(
10251062
name: "InitializerDecl",
10261063
nameForDiagnostics: "initializer",
1064+
description: """
1065+
An initializer declaration like the following.
1066+
1067+
```swift
1068+
init(someParameter: Int) {
1069+
}
1070+
```
1071+
1072+
The body is optional because this node also represents initializer requirements inside protocols.
1073+
""",
10271074
kind: "Decl",
10281075
traits: [
10291076
"Attributed"
@@ -1033,43 +1080,51 @@ public let DECL_NODES: [Node] = [
10331080
name: "Attributes",
10341081
kind: .collection(kind: "AttributeList", collectionElementName: "Attribute"),
10351082
nameForDiagnostics: "attributes",
1083+
description: "Attributes that are attached to the initializer.",
10361084
isOptional: true
10371085
),
10381086
Child(
10391087
name: "Modifiers",
10401088
kind: .collection(kind: "ModifierList", collectionElementName: "Modifier"),
10411089
nameForDiagnostics: "modifiers",
1090+
description: "Modifiers attached to the initializer",
10421091
isOptional: true
10431092
),
10441093
Child(
10451094
name: "InitKeyword",
1046-
kind: .token(choices: [.keyword(text: "init")])
1095+
kind: .token(choices: [.keyword(text: "init")]),
1096+
description: "The init keyword"
10471097
),
10481098
Child(
10491099
name: "OptionalMark",
10501100
kind: .token(choices: [.token(tokenKind: "PostfixQuestionMarkToken"), .token(tokenKind: "InfixQuestionMarkToken"), .token(tokenKind: "ExclamationMarkToken")]),
1101+
description: "If the initializer is failable, a question mark to indicate that.",
10511102
isOptional: true
10521103
),
10531104
Child(
10541105
name: "GenericParameterClause",
10551106
kind: .node(kind: "GenericParameterClause"),
10561107
nameForDiagnostics: "generic parameter clause",
1108+
description: "Generic parameters of the initializer.",
10571109
isOptional: true
10581110
),
10591111
Child(
10601112
name: "Signature",
10611113
kind: .node(kind: "FunctionSignature"),
1062-
nameForDiagnostics: "function signature"
1114+
nameForDiagnostics: "function signature",
1115+
description: "The arguments of the initializer. While the function signature allows specifying an return clause, doing so is not semantically valid."
10631116
),
10641117
Child(
10651118
name: "GenericWhereClause",
10661119
kind: .node(kind: "GenericWhereClause"),
10671120
nameForDiagnostics: "generic where clause",
1121+
description: "If the initializer had generic parameters, a where clause that can restrict those",
10681122
isOptional: true
10691123
),
10701124
Child(
10711125
name: "Body",
10721126
kind: .node(kind: "CodeBlock"),
1127+
description: "The initializer’s body. Missing if the initialier is a requirement of a protocol declaration.",
10731128
isOptional: true
10741129
),
10751130
]
@@ -1696,7 +1751,7 @@ public let DECL_NODES: [Node] = [
16961751
isOptional: true
16971752
),
16981753
Child(
1699-
name: "Members",
1754+
name: "MemberBlock",
17001755
kind: .node(kind: "MemberDeclBlock")
17011756
),
17021757
]
@@ -1798,7 +1853,7 @@ public let DECL_NODES: [Node] = [
17981853
isOptional: true
17991854
),
18001855
Child(
1801-
name: "Members",
1856+
name: "MemberBlock",
18021857
kind: .node(kind: "MemberDeclBlock")
18031858
),
18041859
]

CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public let KEYWORDS: [KeywordSpec] = [
9696
KeywordSpec("autoclosure"),
9797
KeywordSpec("availability"),
9898
KeywordSpec("available"),
99-
KeywordSpec("await"),
99+
KeywordSpec("await", requiresTrailingSpace: true),
100100
KeywordSpec("backDeployed"),
101101
KeywordSpec("before"),
102102
KeywordSpec("block"),
@@ -122,7 +122,7 @@ public let KEYWORDS: [KeywordSpec] = [
122122
KeywordSpec("do", isLexerClassified: true),
123123
KeywordSpec("dynamic"),
124124
KeywordSpec("each"),
125-
KeywordSpec("else", isLexerClassified: true, requiresTrailingSpace: true),
125+
KeywordSpec("else", isLexerClassified: true, requiresLeadingSpace: true, requiresTrailingSpace: true),
126126
KeywordSpec("enum", isLexerClassified: true, requiresTrailingSpace: true),
127127
KeywordSpec("escaping"),
128128
KeywordSpec("exclusivity"),

CodeGeneration/Sources/SyntaxSupport/StmtNodes.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,17 @@ public let STMT_NODES: [Node] = [
290290
),
291291
Child(
292292
name: "TryKeyword",
293-
kind: .node(kind: "TryToken"),
293+
kind: .token(choices: [.keyword(text: "try")]),
294294
isOptional: true
295295
),
296296
Child(
297297
name: "AwaitKeyword",
298298
kind: .token(choices: [.keyword(text: "await")]),
299-
isOptional: true,
300-
classification: "Keyword"
299+
isOptional: true
301300
),
302301
Child(
303302
name: "CaseKeyword",
304-
kind: .node(kind: "CaseToken"),
303+
kind: .token(choices: [.keyword(text: "case")]),
305304
isOptional: true
306305
),
307306
Child(

CodeGeneration/Sources/SyntaxSupport/Traits.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public let TRAITS: [Trait] = [
4141
children: [
4242
Child(name: "Attributes", kind: .node(kind: "AttributeList"), isOptional: true),
4343
Child(name: "Modifiers", kind: .node(kind: "ModifierList"), isOptional: true),
44-
Child(name: "Members", kind: .node(kind: "MemberDeclBlock")),
44+
Child(name: "MemberBlock", kind: .node(kind: "MemberDeclBlock")),
4545
]
4646
),
4747
Trait(

CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,34 @@ public extension Child {
4545
flattened(indentedDocumentation: description ?? "")
4646
}
4747

48-
var defaultInitialization: ExprSyntax? {
49-
switch kind {
50-
case .token(choices: let choices, requiresLeadingSpace: _, requiresTrailingSpace: _):
51-
if choices.count == 1, case .keyword(text: let text) = choices.first {
52-
var textChoice = text
53-
if textChoice == "init" {
54-
textChoice = "`init`"
55-
}
56-
return ExprSyntax(".keyword(.\(raw: textChoice))")
57-
} else {
58-
return type.defaultInitialization
59-
}
60-
default:
61-
return type.defaultInitialization
48+
/// If the child node has a default value, return an expression of the form
49+
/// ` = default_value` that can be used as the default value to for a
50+
/// function parameter. Otherwise, return `nil`.
51+
var defaultInitialization: InitializerClauseSyntax? {
52+
if isOptional || isUnexpectedNodes {
53+
return InitializerClauseSyntax(value: NilLiteralExprSyntax())
54+
}
55+
guard let token = token, isToken else {
56+
return type.defaultValue.map { InitializerClauseSyntax(value: $0) }
57+
}
58+
if token.isKeyword {
59+
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)()"))
60+
}
61+
if token.text != nil {
62+
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)Token()"))
63+
}
64+
guard case .token(let choices, _, _) = kind, choices.count == 1, token.associatedValueClass != nil else {
65+
return nil
66+
}
67+
var textChoice: String
68+
switch choices[0] {
69+
case .keyword(let text), .token(let text):
70+
textChoice = text
71+
}
72+
if textChoice == "init" {
73+
textChoice = "`init`"
6274
}
75+
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)(.\(raw: textChoice))"))
6376
}
6477

6578
/// If this node is a token that can't contain arbitrary text, generate a Swift

0 commit comments

Comments
 (0)