Skip to content

Commit f91b0d4

Browse files
authored
Merge pull request #535 from ahoppen/pr/missing-base-nodes
Introduce a new syntax kind to represent missing base nodes
2 parents c3b5493 + 0a9864b commit f91b0d4

27 files changed

+947
-287
lines changed

Sources/SwiftSyntax/Misc.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extension Syntax {
5454
return node
5555
case .unknown(let node):
5656
return node
57-
% for node in SYNTAX_NODES:
57+
% for node in NON_BASE_SYNTAX_NODES:
5858
case .${node.swift_syntax_kind}(let node):
5959
return node
6060
% end

Sources/SwiftSyntax/SyntaxEnum.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public enum SyntaxEnum {
2424
case unknown(UnknownSyntax)
2525
case token(TokenSyntax)
26-
% for node in SYNTAX_NODES:
26+
% for node in NON_BASE_SYNTAX_NODES:
2727
% if node.is_base():
2828
case ${node.swift_syntax_kind}(Unknown${node.name})
2929
% else:
@@ -40,7 +40,7 @@ public extension Syntax {
4040
return .token(TokenSyntax(self)!)
4141
case .unknown:
4242
return .unknown(UnknownSyntax(self)!)
43-
% for node in SYNTAX_NODES:
43+
% for node in NON_BASE_SYNTAX_NODES:
4444
case .${node.swift_syntax_kind}:
4545
% if node.is_base():
4646
return .${node.swift_syntax_kind}(Unknown${node.name}(self)!)

Sources/SwiftSyntax/SyntaxFactory.swift.gyb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ public enum SyntaxFactory {
8383
% end
8484

8585
% if not node.is_base():
86-
public static func makeBlank${node.syntax_kind}(presence: SourcePresence = .present) -> ${node.name} {
86+
% default_presence = 'missing' if node.is_missing() else 'present'
87+
public static func makeBlank${node.syntax_kind}(presence: SourcePresence = .${default_presence}) -> ${node.name} {
8788
let data = SyntaxData.forRoot(RawSyntax.create(kind: .${node.swift_syntax_kind},
8889
layout: [
8990
% for child in node.children:

Sources/SwiftSyntax/SyntaxKind.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
internal enum SyntaxKind: CSyntaxKind {
2323
case token = 0
2424
case unknown = 1
25-
% for node in SYNTAX_NODES:
25+
% for node in NON_BASE_SYNTAX_NODES:
2626
case ${node.swift_syntax_kind} = ${SYNTAX_NODE_SERIALIZATION_CODES[node.syntax_kind]}
2727
% end
2828

Sources/SwiftSyntax/SyntaxRewriter.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ open class SyntaxRewriter {
157157
return visitImplTokenSyntax
158158
case .unknown:
159159
return visitImplUnknownSyntax
160-
% for node in SYNTAX_NODES:
160+
% for node in NON_BASE_SYNTAX_NODES:
161161
case .${node.swift_syntax_kind}:
162162
return visitImpl${node.name}
163163
% end
@@ -176,7 +176,7 @@ open class SyntaxRewriter {
176176
return visitImplTokenSyntax(data)
177177
case .unknown:
178178
return visitImplUnknownSyntax(data)
179-
% for node in SYNTAX_NODES:
179+
% for node in NON_BASE_SYNTAX_NODES:
180180
case .${node.swift_syntax_kind}:
181181
return visitImpl${node.name}(data)
182182
% end

Sources/SwiftSyntax/SyntaxVisitor.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ open class SyntaxVisitor {
126126
// circumvents an issue where the compiler allocates stack space for every
127127
// case statement next to each other in debug builds, causing it to allocate
128128
// ~50KB per call to this function. rdar://55929175
129-
% for node in SYNTAX_NODES:
129+
% for node in NON_BASE_SYNTAX_NODES:
130130
case .${node.swift_syntax_kind}:
131131
visitImpl${node.name}(data)
132132
% end

Sources/SwiftSyntax/gyb_generated/Misc.swift

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,42 @@ extension SyntaxNode {
4949
return UnknownPatternSyntax(asSyntaxData)
5050
}
5151

52+
public var isMissing: Bool { return raw.kind == .missing }
53+
public var asMissing: MissingSyntax? {
54+
guard isMissing else { return nil }
55+
return MissingSyntax(asSyntaxData)
56+
}
57+
58+
public var isMissingDecl: Bool { return raw.kind == .missingDecl }
59+
public var asMissingDecl: MissingDeclSyntax? {
60+
guard isMissingDecl else { return nil }
61+
return MissingDeclSyntax(asSyntaxData)
62+
}
63+
64+
public var isMissingExpr: Bool { return raw.kind == .missingExpr }
65+
public var asMissingExpr: MissingExprSyntax? {
66+
guard isMissingExpr else { return nil }
67+
return MissingExprSyntax(asSyntaxData)
68+
}
69+
70+
public var isMissingStmt: Bool { return raw.kind == .missingStmt }
71+
public var asMissingStmt: MissingStmtSyntax? {
72+
guard isMissingStmt else { return nil }
73+
return MissingStmtSyntax(asSyntaxData)
74+
}
75+
76+
public var isMissingType: Bool { return raw.kind == .missingType }
77+
public var asMissingType: MissingTypeSyntax? {
78+
guard isMissingType else { return nil }
79+
return MissingTypeSyntax(asSyntaxData)
80+
}
81+
82+
public var isMissingPattern: Bool { return raw.kind == .missingPattern }
83+
public var asMissingPattern: MissingPatternSyntax? {
84+
guard isMissingPattern else { return nil }
85+
return MissingPatternSyntax(asSyntaxData)
86+
}
87+
5288
public var isCodeBlockItem: Bool { return raw.kind == .codeBlockItem }
5389
public var asCodeBlockItem: CodeBlockItemSyntax? {
5490
guard isCodeBlockItem else { return nil }
@@ -1531,16 +1567,6 @@ extension Syntax {
15311567
return node
15321568
case .unknown(let node):
15331569
return node
1534-
case .decl(let node):
1535-
return node
1536-
case .expr(let node):
1537-
return node
1538-
case .stmt(let node):
1539-
return node
1540-
case .type(let node):
1541-
return node
1542-
case .pattern(let node):
1543-
return node
15441570
case .unknownDecl(let node):
15451571
return node
15461572
case .unknownExpr(let node):
@@ -1551,6 +1577,18 @@ extension Syntax {
15511577
return node
15521578
case .unknownPattern(let node):
15531579
return node
1580+
case .missing(let node):
1581+
return node
1582+
case .missingDecl(let node):
1583+
return node
1584+
case .missingExpr(let node):
1585+
return node
1586+
case .missingStmt(let node):
1587+
return node
1588+
case .missingType(let node):
1589+
return node
1590+
case .missingPattern(let node):
1591+
return node
15541592
case .codeBlockItem(let node):
15551593
return node
15561594
case .codeBlockItemList(let node):

Sources/SwiftSyntax/gyb_generated/SyntaxAnyVisitor.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,48 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
9999
override open func visitPost(_ node: UnknownPatternSyntax) {
100100
visitAnyPost(node._syntaxNode)
101101
}
102+
override open func visit(_ node: MissingSyntax) -> SyntaxVisitorContinueKind {
103+
return visitAny(node._syntaxNode)
104+
}
105+
106+
override open func visitPost(_ node: MissingSyntax) {
107+
visitAnyPost(node._syntaxNode)
108+
}
109+
override open func visit(_ node: MissingDeclSyntax) -> SyntaxVisitorContinueKind {
110+
return visitAny(node._syntaxNode)
111+
}
112+
113+
override open func visitPost(_ node: MissingDeclSyntax) {
114+
visitAnyPost(node._syntaxNode)
115+
}
116+
override open func visit(_ node: MissingExprSyntax) -> SyntaxVisitorContinueKind {
117+
return visitAny(node._syntaxNode)
118+
}
119+
120+
override open func visitPost(_ node: MissingExprSyntax) {
121+
visitAnyPost(node._syntaxNode)
122+
}
123+
override open func visit(_ node: MissingStmtSyntax) -> SyntaxVisitorContinueKind {
124+
return visitAny(node._syntaxNode)
125+
}
126+
127+
override open func visitPost(_ node: MissingStmtSyntax) {
128+
visitAnyPost(node._syntaxNode)
129+
}
130+
override open func visit(_ node: MissingTypeSyntax) -> SyntaxVisitorContinueKind {
131+
return visitAny(node._syntaxNode)
132+
}
133+
134+
override open func visitPost(_ node: MissingTypeSyntax) {
135+
visitAnyPost(node._syntaxNode)
136+
}
137+
override open func visit(_ node: MissingPatternSyntax) -> SyntaxVisitorContinueKind {
138+
return visitAny(node._syntaxNode)
139+
}
140+
141+
override open func visitPost(_ node: MissingPatternSyntax) {
142+
visitAnyPost(node._syntaxNode)
143+
}
102144
override open func visit(_ node: CodeBlockItemSyntax) -> SyntaxVisitorContinueKind {
103145
return visitAny(node._syntaxNode)
104146
}

Sources/SwiftSyntax/gyb_generated/SyntaxBaseNodes.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
5656
/// `nil` if the conversion is not possible.
5757
public init?(_ syntax: Syntax) {
5858
switch syntax.raw.kind {
59-
case .unknownDecl, .typealiasDecl, .associatedtypeDecl, .ifConfigDecl, .poundErrorDecl, .poundWarningDecl, .poundSourceLocation, .classDecl, .actorDecl, .structDecl, .protocolDecl, .extensionDecl, .functionDecl, .initializerDecl, .deinitializerDecl, .subscriptDecl, .importDecl, .accessorDecl, .variableDecl, .enumCaseDecl, .enumDecl, .operatorDecl, .precedenceGroupDecl:
59+
case .unknownDecl, .missingDecl, .typealiasDecl, .associatedtypeDecl, .ifConfigDecl, .poundErrorDecl, .poundWarningDecl, .poundSourceLocation, .classDecl, .actorDecl, .structDecl, .protocolDecl, .extensionDecl, .functionDecl, .initializerDecl, .deinitializerDecl, .subscriptDecl, .importDecl, .accessorDecl, .variableDecl, .enumCaseDecl, .enumDecl, .operatorDecl, .precedenceGroupDecl:
6060
self._syntaxNode = syntax
6161
default:
6262
return nil
@@ -70,7 +70,7 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
7070
// Assert that the kind of the given data matches in debug builds.
7171
#if DEBUG
7272
switch data.raw.kind {
73-
case .unknownDecl, .typealiasDecl, .associatedtypeDecl, .ifConfigDecl, .poundErrorDecl, .poundWarningDecl, .poundSourceLocation, .classDecl, .actorDecl, .structDecl, .protocolDecl, .extensionDecl, .functionDecl, .initializerDecl, .deinitializerDecl, .subscriptDecl, .importDecl, .accessorDecl, .variableDecl, .enumCaseDecl, .enumDecl, .operatorDecl, .precedenceGroupDecl:
73+
case .unknownDecl, .missingDecl, .typealiasDecl, .associatedtypeDecl, .ifConfigDecl, .poundErrorDecl, .poundWarningDecl, .poundSourceLocation, .classDecl, .actorDecl, .structDecl, .protocolDecl, .extensionDecl, .functionDecl, .initializerDecl, .deinitializerDecl, .subscriptDecl, .importDecl, .accessorDecl, .variableDecl, .enumCaseDecl, .enumDecl, .operatorDecl, .precedenceGroupDecl:
7474
break
7575
default:
7676
fatalError("Unable to create DeclSyntax from \(data.raw.kind)")
@@ -164,7 +164,7 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
164164
/// `nil` if the conversion is not possible.
165165
public init?(_ syntax: Syntax) {
166166
switch syntax.raw.kind {
167-
case .unknownExpr, .inOutExpr, .poundColumnExpr, .tryExpr, .awaitExpr, .identifierExpr, .superRefExpr, .nilLiteralExpr, .discardAssignmentExpr, .assignmentExpr, .sequenceExpr, .poundLineExpr, .poundFileExpr, .poundFileIDExpr, .poundFilePathExpr, .poundFunctionExpr, .poundDsohandleExpr, .symbolicReferenceExpr, .prefixOperatorExpr, .binaryOperatorExpr, .arrowExpr, .floatLiteralExpr, .tupleExpr, .arrayExpr, .dictionaryExpr, .integerLiteralExpr, .booleanLiteralExpr, .ternaryExpr, .memberAccessExpr, .isExpr, .asExpr, .typeExpr, .closureExpr, .unresolvedPatternExpr, .functionCallExpr, .subscriptExpr, .optionalChainingExpr, .forcedValueExpr, .postfixUnaryExpr, .specializeExpr, .stringLiteralExpr, .regexLiteralExpr, .keyPathExpr, .keyPathBaseExpr, .objcKeyPathExpr, .objcSelectorExpr, .postfixIfConfigExpr, .editorPlaceholderExpr, .objectLiteralExpr:
167+
case .unknownExpr, .missingExpr, .inOutExpr, .poundColumnExpr, .tryExpr, .awaitExpr, .identifierExpr, .superRefExpr, .nilLiteralExpr, .discardAssignmentExpr, .assignmentExpr, .sequenceExpr, .poundLineExpr, .poundFileExpr, .poundFileIDExpr, .poundFilePathExpr, .poundFunctionExpr, .poundDsohandleExpr, .symbolicReferenceExpr, .prefixOperatorExpr, .binaryOperatorExpr, .arrowExpr, .floatLiteralExpr, .tupleExpr, .arrayExpr, .dictionaryExpr, .integerLiteralExpr, .booleanLiteralExpr, .ternaryExpr, .memberAccessExpr, .isExpr, .asExpr, .typeExpr, .closureExpr, .unresolvedPatternExpr, .functionCallExpr, .subscriptExpr, .optionalChainingExpr, .forcedValueExpr, .postfixUnaryExpr, .specializeExpr, .stringLiteralExpr, .regexLiteralExpr, .keyPathExpr, .keyPathBaseExpr, .objcKeyPathExpr, .objcSelectorExpr, .postfixIfConfigExpr, .editorPlaceholderExpr, .objectLiteralExpr:
168168
self._syntaxNode = syntax
169169
default:
170170
return nil
@@ -178,7 +178,7 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
178178
// Assert that the kind of the given data matches in debug builds.
179179
#if DEBUG
180180
switch data.raw.kind {
181-
case .unknownExpr, .inOutExpr, .poundColumnExpr, .tryExpr, .awaitExpr, .identifierExpr, .superRefExpr, .nilLiteralExpr, .discardAssignmentExpr, .assignmentExpr, .sequenceExpr, .poundLineExpr, .poundFileExpr, .poundFileIDExpr, .poundFilePathExpr, .poundFunctionExpr, .poundDsohandleExpr, .symbolicReferenceExpr, .prefixOperatorExpr, .binaryOperatorExpr, .arrowExpr, .floatLiteralExpr, .tupleExpr, .arrayExpr, .dictionaryExpr, .integerLiteralExpr, .booleanLiteralExpr, .ternaryExpr, .memberAccessExpr, .isExpr, .asExpr, .typeExpr, .closureExpr, .unresolvedPatternExpr, .functionCallExpr, .subscriptExpr, .optionalChainingExpr, .forcedValueExpr, .postfixUnaryExpr, .specializeExpr, .stringLiteralExpr, .regexLiteralExpr, .keyPathExpr, .keyPathBaseExpr, .objcKeyPathExpr, .objcSelectorExpr, .postfixIfConfigExpr, .editorPlaceholderExpr, .objectLiteralExpr:
181+
case .unknownExpr, .missingExpr, .inOutExpr, .poundColumnExpr, .tryExpr, .awaitExpr, .identifierExpr, .superRefExpr, .nilLiteralExpr, .discardAssignmentExpr, .assignmentExpr, .sequenceExpr, .poundLineExpr, .poundFileExpr, .poundFileIDExpr, .poundFilePathExpr, .poundFunctionExpr, .poundDsohandleExpr, .symbolicReferenceExpr, .prefixOperatorExpr, .binaryOperatorExpr, .arrowExpr, .floatLiteralExpr, .tupleExpr, .arrayExpr, .dictionaryExpr, .integerLiteralExpr, .booleanLiteralExpr, .ternaryExpr, .memberAccessExpr, .isExpr, .asExpr, .typeExpr, .closureExpr, .unresolvedPatternExpr, .functionCallExpr, .subscriptExpr, .optionalChainingExpr, .forcedValueExpr, .postfixUnaryExpr, .specializeExpr, .stringLiteralExpr, .regexLiteralExpr, .keyPathExpr, .keyPathBaseExpr, .objcKeyPathExpr, .objcSelectorExpr, .postfixIfConfigExpr, .editorPlaceholderExpr, .objectLiteralExpr:
182182
break
183183
default:
184184
fatalError("Unable to create ExprSyntax from \(data.raw.kind)")
@@ -272,7 +272,7 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
272272
/// `nil` if the conversion is not possible.
273273
public init?(_ syntax: Syntax) {
274274
switch syntax.raw.kind {
275-
case .unknownStmt, .continueStmt, .whileStmt, .deferStmt, .expressionStmt, .repeatWhileStmt, .guardStmt, .forInStmt, .switchStmt, .doStmt, .returnStmt, .yieldStmt, .fallthroughStmt, .breakStmt, .declarationStmt, .throwStmt, .ifStmt, .poundAssertStmt:
275+
case .unknownStmt, .missingStmt, .continueStmt, .whileStmt, .deferStmt, .expressionStmt, .repeatWhileStmt, .guardStmt, .forInStmt, .switchStmt, .doStmt, .returnStmt, .yieldStmt, .fallthroughStmt, .breakStmt, .declarationStmt, .throwStmt, .ifStmt, .poundAssertStmt:
276276
self._syntaxNode = syntax
277277
default:
278278
return nil
@@ -286,7 +286,7 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
286286
// Assert that the kind of the given data matches in debug builds.
287287
#if DEBUG
288288
switch data.raw.kind {
289-
case .unknownStmt, .continueStmt, .whileStmt, .deferStmt, .expressionStmt, .repeatWhileStmt, .guardStmt, .forInStmt, .switchStmt, .doStmt, .returnStmt, .yieldStmt, .fallthroughStmt, .breakStmt, .declarationStmt, .throwStmt, .ifStmt, .poundAssertStmt:
289+
case .unknownStmt, .missingStmt, .continueStmt, .whileStmt, .deferStmt, .expressionStmt, .repeatWhileStmt, .guardStmt, .forInStmt, .switchStmt, .doStmt, .returnStmt, .yieldStmt, .fallthroughStmt, .breakStmt, .declarationStmt, .throwStmt, .ifStmt, .poundAssertStmt:
290290
break
291291
default:
292292
fatalError("Unable to create StmtSyntax from \(data.raw.kind)")
@@ -380,7 +380,7 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
380380
/// `nil` if the conversion is not possible.
381381
public init?(_ syntax: Syntax) {
382382
switch syntax.raw.kind {
383-
case .unknownType, .simpleTypeIdentifier, .memberTypeIdentifier, .classRestrictionType, .arrayType, .dictionaryType, .metatypeType, .optionalType, .constrainedSugarType, .implicitlyUnwrappedOptionalType, .compositionType, .tupleType, .functionType, .attributedType:
383+
case .unknownType, .missingType, .simpleTypeIdentifier, .memberTypeIdentifier, .classRestrictionType, .arrayType, .dictionaryType, .metatypeType, .optionalType, .constrainedSugarType, .implicitlyUnwrappedOptionalType, .compositionType, .tupleType, .functionType, .attributedType:
384384
self._syntaxNode = syntax
385385
default:
386386
return nil
@@ -394,7 +394,7 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
394394
// Assert that the kind of the given data matches in debug builds.
395395
#if DEBUG
396396
switch data.raw.kind {
397-
case .unknownType, .simpleTypeIdentifier, .memberTypeIdentifier, .classRestrictionType, .arrayType, .dictionaryType, .metatypeType, .optionalType, .constrainedSugarType, .implicitlyUnwrappedOptionalType, .compositionType, .tupleType, .functionType, .attributedType:
397+
case .unknownType, .missingType, .simpleTypeIdentifier, .memberTypeIdentifier, .classRestrictionType, .arrayType, .dictionaryType, .metatypeType, .optionalType, .constrainedSugarType, .implicitlyUnwrappedOptionalType, .compositionType, .tupleType, .functionType, .attributedType:
398398
break
399399
default:
400400
fatalError("Unable to create TypeSyntax from \(data.raw.kind)")
@@ -488,7 +488,7 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
488488
/// `nil` if the conversion is not possible.
489489
public init?(_ syntax: Syntax) {
490490
switch syntax.raw.kind {
491-
case .unknownPattern, .enumCasePattern, .isTypePattern, .optionalPattern, .identifierPattern, .asTypePattern, .tuplePattern, .wildcardPattern, .expressionPattern, .valueBindingPattern:
491+
case .unknownPattern, .missingPattern, .enumCasePattern, .isTypePattern, .optionalPattern, .identifierPattern, .asTypePattern, .tuplePattern, .wildcardPattern, .expressionPattern, .valueBindingPattern:
492492
self._syntaxNode = syntax
493493
default:
494494
return nil
@@ -502,7 +502,7 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
502502
// Assert that the kind of the given data matches in debug builds.
503503
#if DEBUG
504504
switch data.raw.kind {
505-
case .unknownPattern, .enumCasePattern, .isTypePattern, .optionalPattern, .identifierPattern, .asTypePattern, .tuplePattern, .wildcardPattern, .expressionPattern, .valueBindingPattern:
505+
case .unknownPattern, .missingPattern, .enumCasePattern, .isTypePattern, .optionalPattern, .identifierPattern, .asTypePattern, .tuplePattern, .wildcardPattern, .expressionPattern, .valueBindingPattern:
506506
break
507507
default:
508508
fatalError("Unable to create PatternSyntax from \(data.raw.kind)")

0 commit comments

Comments
 (0)