|
| 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 SwiftSyntax |
| 14 | + |
| 15 | +@_spi(Experimental) public enum LookupName { |
| 16 | + /// Identifier associated with the name. |
| 17 | + /// Could be an identifier of a variable, function or closure parameter and more |
| 18 | + case identifier(String, SyntaxProtocol) |
| 19 | + /// Declaration associated with the name. |
| 20 | + /// Could be class, struct, actor, protocol, function and more |
| 21 | + case declaration(String, DeclSyntaxProtocol) |
| 22 | + |
| 23 | + /// Syntax associated with this name. |
| 24 | + @_spi(Experimental) public var syntax: SyntaxProtocol { |
| 25 | + switch self { |
| 26 | + case .identifier(_, let syntax): |
| 27 | + syntax |
| 28 | + case .declaration(_, let syntax): |
| 29 | + syntax |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /// Introduced name. |
| 34 | + @_spi(Experimental) public var name: String { |
| 35 | + switch self { |
| 36 | + case .identifier(let name, _): |
| 37 | + name |
| 38 | + case .declaration(let name, _): |
| 39 | + name |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Checks if this name was introduced before the syntax used for lookup. |
| 44 | + func isBefore(_ lookedUpSyntax: SyntaxProtocol) -> Bool { |
| 45 | + syntax.position < lookedUpSyntax.position |
| 46 | + } |
| 47 | + |
| 48 | + /// Checks if this name refers to the looked up phrase. |
| 49 | + func refersTo(_ lookedUpName: String) -> Bool { |
| 50 | + name == lookedUpName |
| 51 | + } |
| 52 | + |
| 53 | + /// Extracts names introduced by the given `from` structure. |
| 54 | + static func getNames(from syntax: SyntaxProtocol) -> [LookupName] { |
| 55 | + switch Syntax(syntax).as(SyntaxEnum.self) { |
| 56 | + case .variableDecl(let variableDecl): |
| 57 | + variableDecl.bindings.flatMap { binding in |
| 58 | + getNames(from: binding.pattern) |
| 59 | + } |
| 60 | + case .tuplePattern(let tuplePattern): |
| 61 | + tuplePattern.elements.flatMap { tupleElement in |
| 62 | + getNames(from: tupleElement.pattern) |
| 63 | + } |
| 64 | + case .valueBindingPattern(let valueBindingPattern): |
| 65 | + getNames(from: valueBindingPattern.pattern) |
| 66 | + case .expressionPattern(let expressionPattern): |
| 67 | + getNames(from: expressionPattern.expression) |
| 68 | + case .sequenceExpr(let sequenceExpr): |
| 69 | + sequenceExpr.elements.flatMap { expression in |
| 70 | + getNames(from: expression) |
| 71 | + } |
| 72 | + case .patternExpr(let patternExpr): |
| 73 | + getNames(from: patternExpr.pattern) |
| 74 | + case .optionalBindingCondition(let optionalBinding): |
| 75 | + getNames(from: optionalBinding.pattern) |
| 76 | + case .identifierPattern(let identifierPattern): |
| 77 | + handle(identifierPattern: identifierPattern) |
| 78 | + case .closureShorthandParameter(let closureShorthandParameter): |
| 79 | + handle(closureShorthandParameter: closureShorthandParameter) |
| 80 | + case .closureParameter(let closureParameter): |
| 81 | + handle(closureParameter: closureParameter) |
| 82 | + case .functionDecl(let functionDecl): |
| 83 | + handle(functionDecl: functionDecl) |
| 84 | + case .classDecl(let classDecl): |
| 85 | + handle(classDecl: classDecl) |
| 86 | + case .structDecl(let structDecl): |
| 87 | + handle(structDecl: structDecl) |
| 88 | + case .actorDecl(let actorDecl): |
| 89 | + handle(actorDecl: actorDecl) |
| 90 | + case .protocolDecl(let protocolDecl): |
| 91 | + handle(protocolDecl: protocolDecl) |
| 92 | + default: |
| 93 | + [] |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /// Extracts name introduced by `identifierPattern`. |
| 98 | + private static func handle(identifierPattern: IdentifierPatternSyntax) -> [LookupName] { |
| 99 | + [.identifier(identifierPattern.identifier.text, identifierPattern)] |
| 100 | + } |
| 101 | + |
| 102 | + /// Extracts name introduced by `closureParameter`. |
| 103 | + private static func handle(closureParameter: ClosureParameterSyntax) -> [LookupName] { |
| 104 | + [.identifier(closureParameter.secondName?.text ?? closureParameter.firstName.text, closureParameter)] |
| 105 | + } |
| 106 | + |
| 107 | + /// Extracts name introduced by `closureShorthandParameter`. |
| 108 | + private static func handle(closureShorthandParameter: ClosureShorthandParameterSyntax) -> [LookupName] { |
| 109 | + let name = closureShorthandParameter.name.text |
| 110 | + if name != "_" { |
| 111 | + return [.identifier(name, closureShorthandParameter)] |
| 112 | + } else { |
| 113 | + return [] |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /// Extracts name introduced by `functionDecl`. |
| 118 | + private static func handle(functionDecl: FunctionDeclSyntax) -> [LookupName] { |
| 119 | + [.declaration(functionDecl.name.text, functionDecl)] |
| 120 | + } |
| 121 | + |
| 122 | + /// Extracts name introduced by `classDecl`. |
| 123 | + private static func handle(classDecl: ClassDeclSyntax) -> [LookupName] { |
| 124 | + [.declaration(classDecl.name.text, classDecl)] |
| 125 | + } |
| 126 | + |
| 127 | + /// Extracts name introduced by `structDecl`. |
| 128 | + private static func handle(structDecl: StructDeclSyntax) -> [LookupName] { |
| 129 | + [.declaration(structDecl.name.text, structDecl)] |
| 130 | + } |
| 131 | + |
| 132 | + /// Extracts name introduced by `actorDecl`. |
| 133 | + private static func handle(actorDecl: ActorDeclSyntax) -> [LookupName] { |
| 134 | + [.declaration(actorDecl.name.text, actorDecl)] |
| 135 | + } |
| 136 | + |
| 137 | + /// Extracts name introduced by `protocolDecl`. |
| 138 | + private static func handle(protocolDecl: ProtocolDeclSyntax) -> [LookupName] { |
| 139 | + [.declaration(protocolDecl.name.text, protocolDecl)] |
| 140 | + } |
| 141 | +} |
0 commit comments