Skip to content

Introduce a new syntax kind to represent missing base nodes #535

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
Aug 2, 2022
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
2 changes: 1 addition & 1 deletion Sources/SwiftSyntax/Misc.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extension Syntax {
return node
case .unknown(let node):
return node
% for node in SYNTAX_NODES:
% for node in NON_BASE_SYNTAX_NODES:
case .${node.swift_syntax_kind}(let node):
return node
% end
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftSyntax/SyntaxEnum.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public enum SyntaxEnum {
case unknown(UnknownSyntax)
case token(TokenSyntax)
% for node in SYNTAX_NODES:
% for node in NON_BASE_SYNTAX_NODES:
% if node.is_base():
case ${node.swift_syntax_kind}(Unknown${node.name})
% else:
Expand All @@ -40,7 +40,7 @@ public extension Syntax {
return .token(TokenSyntax(self)!)
case .unknown:
return .unknown(UnknownSyntax(self)!)
% for node in SYNTAX_NODES:
% for node in NON_BASE_SYNTAX_NODES:
case .${node.swift_syntax_kind}:
% if node.is_base():
return .${node.swift_syntax_kind}(Unknown${node.name}(self)!)
Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftSyntax/SyntaxFactory.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public enum SyntaxFactory {
% end

% if not node.is_base():
public static func makeBlank${node.syntax_kind}(presence: SourcePresence = .present) -> ${node.name} {
% default_presence = 'missing' if node.is_missing() else 'present'
public static func makeBlank${node.syntax_kind}(presence: SourcePresence = .${default_presence}) -> ${node.name} {
let data = SyntaxData.forRoot(RawSyntax.create(kind: .${node.swift_syntax_kind},
layout: [
% for child in node.children:
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftSyntax/SyntaxKind.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
internal enum SyntaxKind: CSyntaxKind {
case token = 0
case unknown = 1
% for node in SYNTAX_NODES:
% for node in NON_BASE_SYNTAX_NODES:
case ${node.swift_syntax_kind} = ${SYNTAX_NODE_SERIALIZATION_CODES[node.syntax_kind]}
% end

Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftSyntax/SyntaxRewriter.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ open class SyntaxRewriter {
return visitImplTokenSyntax
case .unknown:
return visitImplUnknownSyntax
% for node in SYNTAX_NODES:
% for node in NON_BASE_SYNTAX_NODES:
case .${node.swift_syntax_kind}:
return visitImpl${node.name}
% end
Expand All @@ -176,7 +176,7 @@ open class SyntaxRewriter {
return visitImplTokenSyntax(data)
case .unknown:
return visitImplUnknownSyntax(data)
% for node in SYNTAX_NODES:
% for node in NON_BASE_SYNTAX_NODES:
case .${node.swift_syntax_kind}:
return visitImpl${node.name}(data)
% end
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftSyntax/SyntaxVisitor.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ open class SyntaxVisitor {
// circumvents an issue where the compiler allocates stack space for every
// case statement next to each other in debug builds, causing it to allocate
// ~50KB per call to this function. rdar://55929175
% for node in SYNTAX_NODES:
% for node in NON_BASE_SYNTAX_NODES:
case .${node.swift_syntax_kind}:
visitImpl${node.name}(data)
% end
Expand Down
58 changes: 48 additions & 10 deletions Sources/SwiftSyntax/gyb_generated/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,42 @@ extension SyntaxNode {
return UnknownPatternSyntax(asSyntaxData)
}

public var isMissing: Bool { return raw.kind == .missing }
public var asMissing: MissingSyntax? {
guard isMissing else { return nil }
return MissingSyntax(asSyntaxData)
}

public var isMissingDecl: Bool { return raw.kind == .missingDecl }
public var asMissingDecl: MissingDeclSyntax? {
guard isMissingDecl else { return nil }
return MissingDeclSyntax(asSyntaxData)
}

public var isMissingExpr: Bool { return raw.kind == .missingExpr }
public var asMissingExpr: MissingExprSyntax? {
guard isMissingExpr else { return nil }
return MissingExprSyntax(asSyntaxData)
}

public var isMissingStmt: Bool { return raw.kind == .missingStmt }
public var asMissingStmt: MissingStmtSyntax? {
guard isMissingStmt else { return nil }
return MissingStmtSyntax(asSyntaxData)
}

public var isMissingType: Bool { return raw.kind == .missingType }
public var asMissingType: MissingTypeSyntax? {
guard isMissingType else { return nil }
return MissingTypeSyntax(asSyntaxData)
}

public var isMissingPattern: Bool { return raw.kind == .missingPattern }
public var asMissingPattern: MissingPatternSyntax? {
guard isMissingPattern else { return nil }
return MissingPatternSyntax(asSyntaxData)
}

public var isCodeBlockItem: Bool { return raw.kind == .codeBlockItem }
public var asCodeBlockItem: CodeBlockItemSyntax? {
guard isCodeBlockItem else { return nil }
Expand Down Expand Up @@ -1531,16 +1567,6 @@ extension Syntax {
return node
case .unknown(let node):
return node
case .decl(let node):
return node
case .expr(let node):
return node
case .stmt(let node):
return node
case .type(let node):
return node
case .pattern(let node):
return node
case .unknownDecl(let node):
return node
case .unknownExpr(let node):
Expand All @@ -1551,6 +1577,18 @@ extension Syntax {
return node
case .unknownPattern(let node):
return node
case .missing(let node):
return node
case .missingDecl(let node):
return node
case .missingExpr(let node):
return node
case .missingStmt(let node):
return node
case .missingType(let node):
return node
case .missingPattern(let node):
return node
case .codeBlockItem(let node):
return node
case .codeBlockItemList(let node):
Expand Down
42 changes: 42 additions & 0 deletions Sources/SwiftSyntax/gyb_generated/SyntaxAnyVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,48 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
override open func visitPost(_ node: UnknownPatternSyntax) {
visitAnyPost(node._syntaxNode)
}
override open func visit(_ node: MissingSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}

override open func visitPost(_ node: MissingSyntax) {
visitAnyPost(node._syntaxNode)
}
override open func visit(_ node: MissingDeclSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}

override open func visitPost(_ node: MissingDeclSyntax) {
visitAnyPost(node._syntaxNode)
}
override open func visit(_ node: MissingExprSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}

override open func visitPost(_ node: MissingExprSyntax) {
visitAnyPost(node._syntaxNode)
}
override open func visit(_ node: MissingStmtSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}

override open func visitPost(_ node: MissingStmtSyntax) {
visitAnyPost(node._syntaxNode)
}
override open func visit(_ node: MissingTypeSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}

override open func visitPost(_ node: MissingTypeSyntax) {
visitAnyPost(node._syntaxNode)
}
override open func visit(_ node: MissingPatternSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}

override open func visitPost(_ node: MissingPatternSyntax) {
visitAnyPost(node._syntaxNode)
}
override open func visit(_ node: CodeBlockItemSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}
Expand Down
20 changes: 10 additions & 10 deletions Sources/SwiftSyntax/gyb_generated/SyntaxBaseNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
/// `nil` if the conversion is not possible.
public init?(_ syntax: Syntax) {
switch syntax.raw.kind {
case .unknownDecl, .typealiasDecl, .associatedtypeDecl, .ifConfigDecl, .poundErrorDecl, .poundWarningDecl, .poundSourceLocation, .classDecl, .actorDecl, .structDecl, .protocolDecl, .extensionDecl, .functionDecl, .initializerDecl, .deinitializerDecl, .subscriptDecl, .importDecl, .accessorDecl, .variableDecl, .enumCaseDecl, .enumDecl, .operatorDecl, .precedenceGroupDecl:
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:
self._syntaxNode = syntax
default:
return nil
Expand All @@ -70,7 +70,7 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
// Assert that the kind of the given data matches in debug builds.
#if DEBUG
switch data.raw.kind {
case .unknownDecl, .typealiasDecl, .associatedtypeDecl, .ifConfigDecl, .poundErrorDecl, .poundWarningDecl, .poundSourceLocation, .classDecl, .actorDecl, .structDecl, .protocolDecl, .extensionDecl, .functionDecl, .initializerDecl, .deinitializerDecl, .subscriptDecl, .importDecl, .accessorDecl, .variableDecl, .enumCaseDecl, .enumDecl, .operatorDecl, .precedenceGroupDecl:
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:
break
default:
fatalError("Unable to create DeclSyntax from \(data.raw.kind)")
Expand Down Expand Up @@ -164,7 +164,7 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
/// `nil` if the conversion is not possible.
public init?(_ syntax: Syntax) {
switch syntax.raw.kind {
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:
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:
self._syntaxNode = syntax
default:
return nil
Expand All @@ -178,7 +178,7 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
// Assert that the kind of the given data matches in debug builds.
#if DEBUG
switch data.raw.kind {
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:
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:
break
default:
fatalError("Unable to create ExprSyntax from \(data.raw.kind)")
Expand Down Expand Up @@ -272,7 +272,7 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
/// `nil` if the conversion is not possible.
public init?(_ syntax: Syntax) {
switch syntax.raw.kind {
case .unknownStmt, .continueStmt, .whileStmt, .deferStmt, .expressionStmt, .repeatWhileStmt, .guardStmt, .forInStmt, .switchStmt, .doStmt, .returnStmt, .yieldStmt, .fallthroughStmt, .breakStmt, .declarationStmt, .throwStmt, .ifStmt, .poundAssertStmt:
case .unknownStmt, .missingStmt, .continueStmt, .whileStmt, .deferStmt, .expressionStmt, .repeatWhileStmt, .guardStmt, .forInStmt, .switchStmt, .doStmt, .returnStmt, .yieldStmt, .fallthroughStmt, .breakStmt, .declarationStmt, .throwStmt, .ifStmt, .poundAssertStmt:
self._syntaxNode = syntax
default:
return nil
Expand All @@ -286,7 +286,7 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
// Assert that the kind of the given data matches in debug builds.
#if DEBUG
switch data.raw.kind {
case .unknownStmt, .continueStmt, .whileStmt, .deferStmt, .expressionStmt, .repeatWhileStmt, .guardStmt, .forInStmt, .switchStmt, .doStmt, .returnStmt, .yieldStmt, .fallthroughStmt, .breakStmt, .declarationStmt, .throwStmt, .ifStmt, .poundAssertStmt:
case .unknownStmt, .missingStmt, .continueStmt, .whileStmt, .deferStmt, .expressionStmt, .repeatWhileStmt, .guardStmt, .forInStmt, .switchStmt, .doStmt, .returnStmt, .yieldStmt, .fallthroughStmt, .breakStmt, .declarationStmt, .throwStmt, .ifStmt, .poundAssertStmt:
break
default:
fatalError("Unable to create StmtSyntax from \(data.raw.kind)")
Expand Down Expand Up @@ -380,7 +380,7 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
/// `nil` if the conversion is not possible.
public init?(_ syntax: Syntax) {
switch syntax.raw.kind {
case .unknownType, .simpleTypeIdentifier, .memberTypeIdentifier, .classRestrictionType, .arrayType, .dictionaryType, .metatypeType, .optionalType, .constrainedSugarType, .implicitlyUnwrappedOptionalType, .compositionType, .tupleType, .functionType, .attributedType:
case .unknownType, .missingType, .simpleTypeIdentifier, .memberTypeIdentifier, .classRestrictionType, .arrayType, .dictionaryType, .metatypeType, .optionalType, .constrainedSugarType, .implicitlyUnwrappedOptionalType, .compositionType, .tupleType, .functionType, .attributedType:
self._syntaxNode = syntax
default:
return nil
Expand All @@ -394,7 +394,7 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
// Assert that the kind of the given data matches in debug builds.
#if DEBUG
switch data.raw.kind {
case .unknownType, .simpleTypeIdentifier, .memberTypeIdentifier, .classRestrictionType, .arrayType, .dictionaryType, .metatypeType, .optionalType, .constrainedSugarType, .implicitlyUnwrappedOptionalType, .compositionType, .tupleType, .functionType, .attributedType:
case .unknownType, .missingType, .simpleTypeIdentifier, .memberTypeIdentifier, .classRestrictionType, .arrayType, .dictionaryType, .metatypeType, .optionalType, .constrainedSugarType, .implicitlyUnwrappedOptionalType, .compositionType, .tupleType, .functionType, .attributedType:
break
default:
fatalError("Unable to create TypeSyntax from \(data.raw.kind)")
Expand Down Expand Up @@ -488,7 +488,7 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
/// `nil` if the conversion is not possible.
public init?(_ syntax: Syntax) {
switch syntax.raw.kind {
case .unknownPattern, .enumCasePattern, .isTypePattern, .optionalPattern, .identifierPattern, .asTypePattern, .tuplePattern, .wildcardPattern, .expressionPattern, .valueBindingPattern:
case .unknownPattern, .missingPattern, .enumCasePattern, .isTypePattern, .optionalPattern, .identifierPattern, .asTypePattern, .tuplePattern, .wildcardPattern, .expressionPattern, .valueBindingPattern:
self._syntaxNode = syntax
default:
return nil
Expand All @@ -502,7 +502,7 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
// Assert that the kind of the given data matches in debug builds.
#if DEBUG
switch data.raw.kind {
case .unknownPattern, .enumCasePattern, .isTypePattern, .optionalPattern, .identifierPattern, .asTypePattern, .tuplePattern, .wildcardPattern, .expressionPattern, .valueBindingPattern:
case .unknownPattern, .missingPattern, .enumCasePattern, .isTypePattern, .optionalPattern, .identifierPattern, .asTypePattern, .tuplePattern, .wildcardPattern, .expressionPattern, .valueBindingPattern:
break
default:
fatalError("Unable to create PatternSyntax from \(data.raw.kind)")
Expand Down
Loading