|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Foundation |
| 14 | +import SwiftSyntax |
| 15 | + |
| 16 | +extension SyntaxProtocol { |
| 17 | + /// Scope at the syntax node. Could be inherited from parent or introduced at the node. |
| 18 | + var scope: Scope? { |
| 19 | + switch self.syntaxNodeType { |
| 20 | + case is SourceFileSyntax.Type: |
| 21 | + FileScope(syntax: self) |
| 22 | + default: |
| 23 | + parent?.scope |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +protocol Scope { |
| 29 | + var parent: Scope? { get } |
| 30 | + |
| 31 | + var sourceSyntax: SyntaxProtocol { get } |
| 32 | + |
| 33 | + func getDeclarationFor(name: String, at syntax: SyntaxProtocol) -> Syntax? |
| 34 | +} |
| 35 | + |
| 36 | +extension Scope { |
| 37 | + var parent: Scope? { |
| 38 | + getParentScope(forSyntax: sourceSyntax) |
| 39 | + } |
| 40 | + |
| 41 | + private func getParentScope(forSyntax syntax: SyntaxProtocol?) -> Scope? { |
| 42 | + if let lookedUpScope = syntax?.scope, lookedUpScope.sourceSyntax.id == syntax?.id { |
| 43 | + return getParentScope(forSyntax: sourceSyntax.parent) |
| 44 | + } else { |
| 45 | + return syntax?.scope |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // MARK: - lookupLabeledStmts |
| 50 | + |
| 51 | + func lookupLabeledStmts(at syntax: SyntaxProtocol) -> [LabeledStmtSyntax] { |
| 52 | + var result = [LabeledStmtSyntax]() |
| 53 | + lookupLabeledStmtsHelper(at: syntax.parent, accumulator: &result) |
| 54 | + return result |
| 55 | + } |
| 56 | + |
| 57 | + private func lookupLabeledStmtsHelper(at syntax: Syntax?, accumulator: inout [LabeledStmtSyntax]) { |
| 58 | + guard let syntax, !syntax.is(MemberBlockSyntax.self) else { return } |
| 59 | + if let labeledStmtSyntax = syntax.as(LabeledStmtSyntax.self) { |
| 60 | + accumulator.append(labeledStmtSyntax) |
| 61 | + lookupLabeledStmtsHelper(at: labeledStmtSyntax.parent, accumulator: &accumulator) |
| 62 | + } else { |
| 63 | + lookupLabeledStmtsHelper(at: syntax.parent, accumulator: &accumulator) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // MARK: - lookupFallthroughSourceAndDest |
| 68 | + |
| 69 | + func lookupFallthroughSourceAndDest(at syntax: SyntaxProtocol) -> (SwitchCaseSyntax?, SwitchCaseSyntax?) { |
| 70 | + guard let originalSwitchCase = lookupClosestSwitchCaseSyntaxAncestor(at: syntax) else { return (nil, nil) } |
| 71 | + |
| 72 | + let nextSwitchCase = lookupNextSwitchCase(at: originalSwitchCase) |
| 73 | + |
| 74 | + return (originalSwitchCase, nextSwitchCase) |
| 75 | + } |
| 76 | + |
| 77 | + private func lookupClosestSwitchCaseSyntaxAncestor(at syntax: SyntaxProtocol?) -> SwitchCaseSyntax? { |
| 78 | + guard let syntax else { return nil } |
| 79 | + |
| 80 | + if let switchCaseSyntax = syntax.as(SwitchCaseSyntax.self) { |
| 81 | + return switchCaseSyntax |
| 82 | + } else { |
| 83 | + return lookupClosestSwitchCaseSyntaxAncestor(at: syntax.parent) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private func lookupNextSwitchCase(at switchCaseSyntax: SwitchCaseSyntax) -> SwitchCaseSyntax? { |
| 88 | + guard let switchCaseListSyntax = switchCaseSyntax.parent?.as(SwitchCaseListSyntax.self) else { return nil } |
| 89 | + |
| 90 | + var visitedOriginalCase = false |
| 91 | + |
| 92 | + for child in switchCaseListSyntax.children(viewMode: .sourceAccurate) { |
| 93 | + if let thisCase = child.as(SwitchCaseSyntax.self) { |
| 94 | + if thisCase.id == switchCaseSyntax.id { |
| 95 | + visitedOriginalCase = true |
| 96 | + } else if visitedOriginalCase { |
| 97 | + return thisCase |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return nil |
| 103 | + } |
| 104 | + |
| 105 | + // MARK: - lookupCatchNode |
| 106 | + |
| 107 | + func lookupCatchNode(at syntax: Syntax) -> Syntax? { |
| 108 | + return lookupCatchNodeHelper(at: syntax, traversedCatchClause: false) |
| 109 | + } |
| 110 | + |
| 111 | + private func lookupCatchNodeHelper(at syntax: Syntax?, traversedCatchClause: Bool) -> Syntax? { |
| 112 | + guard let syntax else { return nil } |
| 113 | + |
| 114 | + switch syntax.syntaxNodeType { |
| 115 | + case is DoStmtSyntax.Type: |
| 116 | + if traversedCatchClause { |
| 117 | + return lookupCatchNodeHelper(at: syntax.parent, traversedCatchClause: false) |
| 118 | + } else { |
| 119 | + return syntax |
| 120 | + } |
| 121 | + case is CatchClauseSyntax.Type: |
| 122 | + return lookupCatchNodeHelper(at: syntax.parent, traversedCatchClause: true) |
| 123 | + case is TryExprSyntax.Type: |
| 124 | + if syntax.as(TryExprSyntax.self)!.questionOrExclamationMark != nil { |
| 125 | + return syntax |
| 126 | + } else { |
| 127 | + return lookupCatchNodeHelper(at: syntax.parent, traversedCatchClause: traversedCatchClause) |
| 128 | + } |
| 129 | + case is FunctionDeclSyntax.Type: |
| 130 | + if syntax.as(FunctionDeclSyntax.self)!.signature.effectSpecifiers?.throwsClause != nil { |
| 131 | + return syntax |
| 132 | + } else { |
| 133 | + return lookupCatchNodeHelper(at: syntax.parent, traversedCatchClause: traversedCatchClause) |
| 134 | + } |
| 135 | + default: |
| 136 | + return lookupCatchNodeHelper(at: syntax.parent, traversedCatchClause: traversedCatchClause) |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments