1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
import SwiftSyntax
import SwiftSyntaxMacros
import SwiftDiagnostics
import SwiftSyntaxBuilder
extension VariableDeclSyntax {
var identifierPattern: IdentifierPatternSyntax? {
bindings.first?.pattern.as(IdentifierPatternSyntax.self)
}
var isInstance: Bool {
for modifier in modifiers {
for token in modifier.tokens(viewMode: .all) {
if token.tokenKind == .keyword(.static) || token.tokenKind == .keyword(.class) {
return false
}
}
}
return true
}
var identifier: TokenSyntax? {
identifierPattern?.identifier
}
var type: TypeSyntax? {
bindings.first?.typeAnnotation?.type
}
func accessorsMatching(_ predicate: (TokenKind) -> Bool) -> [AccessorDeclSyntax] {
let accessors: [AccessorDeclListSyntax.Element] = bindings.compactMap { patternBinding in
switch patternBinding.accessorBlock?.accessors {
case .accessors(let accessors):
return accessors
default:
return nil
}
}.flatMap { $0 }
return accessors.compactMap { accessor in
if predicate(accessor.accessorSpecifier.tokenKind) {
return accessor
} else {
return nil
}
}
}
var willSetAccessors: [AccessorDeclSyntax] {
accessorsMatching { $0 == .keyword(.willSet) }
}
var didSetAccessors: [AccessorDeclSyntax] {
accessorsMatching { $0 == .keyword(.didSet) }
}
var isComputed: Bool {
if accessorsMatching({ $0 == .keyword(.get) }).count > 0 {
return true
} else {
return bindings.contains { binding in
if case .getter = binding.accessorBlock?.accessors {
return true
} else {
return false
}
}
}
}
var isImmutable: Bool {
return bindingSpecifier.tokenKind == .keyword(.let)
}
func isEquivalent(to other: VariableDeclSyntax) -> Bool {
if isInstance != other.isInstance {
return false
}
return identifier?.text == other.identifier?.text
}
var initializer: InitializerClauseSyntax? {
bindings.first?.initializer
}
func hasMacroApplication(_ name: String) -> Bool {
for attribute in attributes {
switch attribute {
case .attribute(let attr):
if attr.attributeName.tokens(viewMode: .all).map({ $0.tokenKind }) == [.identifier(name)] {
return true
}
default:
break
}
}
return false
}
}
extension TypeSyntax {
var identifier: String? {
for token in tokens(viewMode: .all) {
switch token.tokenKind {
case .identifier(let identifier):
return identifier
default:
break
}
}
return nil
}
func genericSubstitution(_ parameters: GenericParameterListSyntax?) -> String? {
var genericParameters = [String : TypeSyntax?]()
if let parameters {
for parameter in parameters {
genericParameters[parameter.name.text] = parameter.inheritedType
}
}
var iterator = self.asProtocol(TypeSyntaxProtocol.self).tokens(viewMode: .sourceAccurate).makeIterator()
guard let base = iterator.next() else {
return nil
}
if let genericBase = genericParameters[base.text] {
if let text = genericBase?.identifier {
return "some " + text
} else {
return nil
}
}
var substituted = base.text
while let token = iterator.next() {
switch token.tokenKind {
case .leftAngle:
substituted += "<"
case .rightAngle:
substituted += ">"
case .comma:
substituted += ","
case .identifier(let identifier):
let type: TypeSyntax = "\(raw: identifier)"
guard let substituedType = type.genericSubstitution(parameters) else {
return nil
}
substituted += substituedType
break
default:
// ignore?
break
}
}
return substituted
}
}
extension FunctionDeclSyntax {
var isInstance: Bool {
for modifier in modifiers {
for token in modifier.tokens(viewMode: .all) {
if token.tokenKind == .keyword(.static) || token.tokenKind == .keyword(.class) {
return false
}
}
}
return true
}
struct SignatureStandin: Equatable {
var isInstance: Bool
var identifier: String
var parameters: [String]
var returnType: String
}
var signatureStandin: SignatureStandin {
var parameters = [String]()
for parameter in signature.parameterClause.parameters {
parameters.append(parameter.firstName.text + ":" + (parameter.type.genericSubstitution(genericParameterClause?.parameters) ?? "" ))
}
let returnType = signature.returnClause?.type.genericSubstitution(genericParameterClause?.parameters) ?? "Void"
return SignatureStandin(isInstance: isInstance, identifier: name.text, parameters: parameters, returnType: returnType)
}
func isEquivalent(to other: FunctionDeclSyntax) -> Bool {
return signatureStandin == other.signatureStandin
}
}
extension DeclGroupSyntax {
var memberFunctionStandins: [FunctionDeclSyntax.SignatureStandin] {
var standins = [FunctionDeclSyntax.SignatureStandin]()
for member in memberBlock.members {
if let function = member.decl.as(FunctionDeclSyntax.self) {
standins.append(function.signatureStandin)
}
}
return standins
}
func hasMemberFunction(equvalentTo other: FunctionDeclSyntax) -> Bool {
for member in memberBlock.members {
if let function = member.decl.as(FunctionDeclSyntax.self) {
if function.isEquivalent(to: other) {
return true
}
}
}
return false
}
func hasMemberProperty(equivalentTo other: VariableDeclSyntax) -> Bool {
for member in memberBlock.members {
if let variable = member.decl.as(VariableDeclSyntax.self) {
if variable.isEquivalent(to: other) {
return true
}
}
}
return false
}
var definedVariables: [VariableDeclSyntax] {
memberBlock.members.compactMap { member in
if let variableDecl = member.decl.as(VariableDeclSyntax.self) {
return variableDecl
}
return nil
}
}
func addIfNeeded(_ decl: DeclSyntax?, to declarations: inout [DeclSyntax]) {
guard let decl else { return }
if let fn = decl.as(FunctionDeclSyntax.self) {
if !hasMemberFunction(equvalentTo: fn) {
declarations.append(decl)
}
} else if let property = decl.as(VariableDeclSyntax.self) {
if !hasMemberProperty(equivalentTo: property) {
declarations.append(decl)
}
}
}
var isClass: Bool {
return self.is(ClassDeclSyntax.self)
}
var isActor: Bool {
return self.is(ActorDeclSyntax.self)
}
var isEnum: Bool {
return self.is(EnumDeclSyntax.self)
}
var isStruct: Bool {
return self.is(StructDeclSyntax.self)
}
}
|