This repository was archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathInterface.swift
220 lines (174 loc) · 9.5 KB
/
Interface.swift
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
import Foundation
import SwiftSemantics
import struct SwiftSemantics.Protocol
public final class Interface {
public let imports: [Import]
public let symbols: [Symbol]
public required init(imports: [Import], symbols: [Symbol]) {
self.imports = imports
self.symbols = symbols
let symbolsGroupedByIdentifier = Dictionary(grouping: symbols, by: { $0.id })
let symbolsGroupedByQualifiedName = Dictionary(grouping: symbols, by: { $0.id.description })
self.symbolsGroupedByIdentifier = symbolsGroupedByIdentifier
self.symbolsGroupedByQualifiedName = symbolsGroupedByQualifiedName
self.topLevelSymbols = symbols.filter { symbol in
if symbol.api is Type || symbol.api is Operator {
return true
}
if let function = symbol.api as? Function, function.isOperator {
return false
}
return symbol.id.context.isEmpty
}
self.relationships = {
let extensionsByExtendedType: [String: [Extension]] = Dictionary(grouping: symbols.flatMap { $0.context.compactMap { $0 as? Extension } }, by: { $0.extendedType })
var relationships: Set<Relationship> = []
for symbol in symbols {
let lastDeclarationScope = symbol.context.last(where: { $0 is Extension || $0 is Symbol })
if let container = lastDeclarationScope as? Symbol {
let predicate: Relationship.Predicate
switch container.api {
case is Protocol:
if symbol.api.modifiers.contains(where: { $0.name == "optional" }) {
predicate = .optionalRequirementOf
} else {
predicate = .requirementOf
}
default:
predicate = .memberOf
}
relationships.insert(Relationship(subject: symbol, predicate: predicate, object: container))
}
if let `extension` = lastDeclarationScope as? Extension {
for extended in symbolsGroupedByIdentifier.named(`extension`.extendedType, resolvingTypealiases: true) {
let predicate: Relationship.Predicate
switch extended.api {
case is Protocol:
predicate = .defaultImplementationOf
default:
predicate = .memberOf
}
relationships.insert(Relationship(subject: symbol, predicate: predicate, object: extended))
}
}
if let type = symbol.api as? Type {
var inheritedTypeNames: Set<String> = []
inheritedTypeNames.formUnion(type.inheritance.flatMap { $0.split(separator: "&").map { $0.trimmingCharacters(in: .whitespaces) }
})
for `extension` in extensionsByExtendedType[symbol.id.description] ?? [] {
inheritedTypeNames.formUnion(`extension`.inheritance)
}
inheritedTypeNames = Set(inheritedTypeNames.flatMap { $0.split(separator: "&").map { $0.trimmingCharacters(in: .whitespaces) } })
for name in inheritedTypeNames {
let inheritedTypes = symbolsGroupedByIdentifier.named(name, resolvingTypealiases: true).filter({ ($0.api is Class || $0.api is Protocol) && $0.id.description == name })
if inheritedTypes.isEmpty {
let inherited = Symbol(api: Unknown(name: name), context: [], declaration: [], documentation: nil, sourceRange: nil)
relationships.insert(Relationship(subject: symbol, predicate: .conformsTo, object: inherited))
} else {
for inherited in inheritedTypes {
let predicate: Relationship.Predicate
if symbol.api is Class, inherited.api is Class {
predicate = .inheritsFrom
} else {
predicate = .conformsTo
}
relationships.insert(Relationship(subject: symbol, predicate: predicate, object: inherited))
}
}
}
}
}
return Array(relationships)
}()
self.functionsByOperator = {
var functionsByOperator: [Symbol: Set<Symbol>] = [:]
let functionsGroupedByName = Dictionary(grouping: symbols.filter { $0.api is Function},
by: { $0.api.name })
for `operator` in symbols.filter({ $0.api is Operator }) {
let functions = functionsGroupedByName[`operator`.name] ?? []
functionsByOperator[`operator`] = Set(functions)
}
return functionsByOperator
}()
self.relationshipsBySubject = Dictionary(grouping: relationships, by: { $0.subject.id })
self.relationshipsByObject = Dictionary(grouping: relationships, by: { $0.object.id })
}
// MARK: -
public let symbolsGroupedByIdentifier: [Symbol.ID: [Symbol]]
public let symbolsGroupedByQualifiedName: [String: [Symbol]]
public let topLevelSymbols: [Symbol]
public var functionsByOperator: [Symbol: Set<Symbol>]
public var baseClasses: [Symbol] {
symbols.filter { $0.api is Class && typesInherited(by: $0).isEmpty }
}
public var classHierarchies: [Symbol: Set<Symbol>] {
var classClusters: [Symbol: Set<Symbol>] = [:]
for baseClass in baseClasses {
var superclasses = Set(CollectionOfOne(baseClass))
while !superclasses.isEmpty {
let subclasses = Set(superclasses.flatMap { typesInheriting(from: $0) })
defer { superclasses = subclasses }
classClusters[baseClass, default: []].formUnion(subclasses)
}
}
return classClusters
}
public let relationships: [Relationship]
public let relationshipsBySubject: [Symbol.ID: [Relationship]]
public let relationshipsByObject: [Symbol.ID: [Relationship]]
// MARK: -
public func members(of symbol: Symbol) -> [Symbol] {
return relationshipsByObject[symbol.id]?.filter { $0.predicate == .memberOf }.map { $0.subject }.sorted() ?? []
}
public func requirements(of symbol: Symbol) -> [Symbol] {
return relationshipsByObject[symbol.id]?.filter { $0.predicate == .requirementOf }.map { $0.subject }.sorted() ?? []
}
public func optionalRequirements(of symbol: Symbol) -> [Symbol] {
return relationshipsByObject[symbol.id]?.filter { $0.predicate == .optionalRequirementOf }.map { $0.subject }.sorted() ?? []
}
public func typesInherited(by symbol: Symbol) -> [Symbol] {
return relationshipsBySubject[symbol.id]?.filter { $0.predicate == .inheritsFrom }.map { $0.object }.sorted() ?? []
}
public func typesInheriting(from symbol: Symbol) -> [Symbol] {
return relationshipsByObject[symbol.id]?.filter { $0.predicate == .inheritsFrom }.map { $0.subject }.sorted() ?? []
}
public func typesConformed(by symbol: Symbol) -> [Symbol] {
return relationshipsBySubject[symbol.id]?.filter { $0.predicate == .conformsTo }.map { $0.object }.sorted() ?? []
}
public func typesConforming(to symbol: Symbol) -> [Symbol] {
return relationshipsByObject[symbol.id]?.filter { $0.predicate == .conformsTo }.map { $0.subject }.sorted() ?? []
}
public func conditionalCounterparts(of symbol: Symbol) -> [Symbol] {
return symbolsGroupedByIdentifier[symbol.id]?.filter { $0 != symbol }.sorted() ?? []
}
public func defaultImplementations(of symbol: Symbol) -> [Symbol] {
return relationshipsByObject[symbol.id]?.filter { $0.predicate == .defaultImplementationOf }.map { $0.subject }.sorted() ?? []
}
// MARK: -
public func symbols(named name: String, resolvingTypealiases: Bool) -> [Symbol] {
symbolsGroupedByIdentifier.named(name, resolvingTypealiases: resolvingTypealiases)
}
}
fileprivate extension Dictionary where Key == Identifier, Value == [Symbol] {
func named(_ name: String, resolvingTypealiases: Bool) -> [Symbol] {
var pathComponents: [String] = []
for component in name.split(separator: ".") {
pathComponents.append("\(component)")
guard resolvingTypealiases else { continue }
if let symbols = first(where: { $0.key.pathComponents == pathComponents })?.value,
let symbol = symbols.first(where: { $0.api is Typealias }),
let `typealias` = symbol.api as? Typealias,
let initializedType = `typealias`.initializedType
{
let initializedTypePathComponents = initializedType.split(separator: ".")
let candidates = keys.filter { $0.matches(initializedTypePathComponents) }
if let id = candidates.max(by: { $0.pathComponents.count > $1.pathComponents.count }) {
pathComponents = id.pathComponents
} else {
return []
}
}
}
return first(where: { $0.key.pathComponents == pathComponents })?.value ?? []
}
}