Repository navigation
Expand file tree
/
Copy pathSwiftParsedModuleSymbolTableBuilder.swift
More file actions
304 lines (273 loc) · 10.2 KB
/
Copy pathSwiftParsedModuleSymbolTableBuilder.swift
File metadata and controls
304 lines (273 loc) · 10.2 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Logging
import SwiftIfConfig
import SwiftSyntax
package struct SwiftParsedModuleSymbolTableBuilder {
package let log: Logger?
/// The symbol table being built.
package var symbolTable: SwiftModuleSymbolTable
/// Imported modules to resolve type syntax.
package let importedModules: [String: SwiftModuleSymbolTable]
/// The build configuration used to resolve #if conditional compilation blocks.
package let buildConfig: any BuildConfiguration
/// Extension decls their extended type hasn't been resolved.
package var unresolvedExtensions: [ExtensionDeclSyntax]
package init(
moduleName: String,
requiredAvailablityOfModuleWithName: String? = nil,
alternativeModules: SwiftModuleSymbolTable.AlternativeModuleNamesData? = nil,
importedModules: [String: SwiftModuleSymbolTable],
buildConfig: any BuildConfiguration = .swiftExtractDefault,
log: Logger? = nil
) {
self.log = log
self.symbolTable = .init(
moduleName: moduleName,
requiredAvailablityOfModuleWithName: requiredAvailablityOfModuleWithName,
alternativeModules: alternativeModules
)
self.importedModules = importedModules
self.buildConfig = buildConfig
self.unresolvedExtensions = []
}
package var moduleName: String {
symbolTable.moduleName
}
}
extension SwiftParsedModuleSymbolTableBuilder {
package mutating func handle(
sourceFile: SourceFileSyntax,
sourceFilePath: String
) {
// Find top-level type declarations.
for statement in sourceFile.statements {
self.handle(codeBlockItem: statement.item, sourceFilePath: sourceFilePath)
}
}
package mutating func handle(
codeBlockItem node: CodeBlockItemSyntax.Item,
sourceFilePath: String
) {
// We only care about declarations.
guard case .decl(let decl) = node else {
return
}
if let nominalTypeNode = decl.asNominal {
self.handle(sourceFilePath: sourceFilePath, nominalTypeDecl: nominalTypeNode, parent: nil)
} else if let typeAliasNode = decl.as(TypeAliasDeclSyntax.self) {
self.handle(sourceFilePath: sourceFilePath, typeAliasDecl: typeAliasNode, parent: nil)
} else if let extensionNode = decl.as(ExtensionDeclSyntax.self) {
self.handle(extensionDecl: extensionNode, sourceFilePath: sourceFilePath)
} else if let ifConfigNode = decl.as(IfConfigDeclSyntax.self) {
self.handle(ifConfig: ifConfigNode, sourceFilePath: sourceFilePath)
} else if let typeAliasNode = decl.as(TypeAliasDeclSyntax.self) {
self.handle(typeAliasDecl: typeAliasNode, sourceFilePath: sourceFilePath)
}
}
package mutating func handle(
typeAliasDecl node: TypeAliasDeclSyntax,
sourceFilePath: String
) {
let name = node.name.text
if symbolTable.topLevelTypeAliases[name] != nil
|| symbolTable.lookupTopLevelNominalType(name) != nil
{
log?.debug("Failed to add a typealias into symbol table: redeclaration; \(name)")
return
}
symbolTable.topLevelTypeAliases[name] = SwiftTypeAliasDeclaration(
sourceFilePath: sourceFilePath,
moduleName: moduleName,
node: node
)
}
/// Add a nominal type declaration and all of the nested types within it to the symbol
/// table.
package mutating func handle(
sourceFilePath: String,
nominalTypeDecl node: NominalTypeDeclSyntaxNode,
parent: SwiftNominalTypeDeclaration?
) {
// If we have already recorded a nominal type with the name in this module,
// it's an invalid redeclaration.
if symbolTable.lookupType(node.name.text, parent: parent) != nil
|| symbolTable.lookupTypealias(node.name.text, parent: parent) != nil
{
log?.debug("Failed to add a decl into symbol table: redeclaration; \(node.nameForDebug)")
return
}
// Otherwise, create the nominal type declaration.
let nominalTypeDecl = SwiftNominalTypeDeclaration(
name: node.name.text,
sourceFilePath: sourceFilePath,
moduleName: moduleName,
parent: parent,
node: node
)
if let parent {
// For nested types, make them discoverable from the parent type.
symbolTable.nestedTypes[parent, default: [:]][nominalTypeDecl.name] = nominalTypeDecl
} else {
// For top-level types, make them discoverable by name.
symbolTable.topLevelTypes[nominalTypeDecl.name] = nominalTypeDecl
}
self.handle(sourceFilePath: sourceFilePath, memberBlock: node.memberBlock, parent: nominalTypeDecl)
}
package mutating func handle(
sourceFilePath: String,
typeAliasDecl node: TypeAliasDeclSyntax,
parent: SwiftNominalTypeDeclaration?
) {
if symbolTable.lookupType(node.name.text, parent: parent) != nil
|| symbolTable.lookupTypealias(node.name.text, parent: parent) != nil
{
log?.debug("Failed to add a decl into symbol table: redeclaration; \(node.nameForDebug)")
return
}
let typeAliasDecl = SwiftTypeAliasDeclaration(
sourceFilePath: sourceFilePath,
moduleName: moduleName,
node: node
)
if let parent {
symbolTable.nestedTypeAliases[parent, default: [:]][typeAliasDecl.name] = typeAliasDecl
} else {
symbolTable.topLevelTypeAliases[typeAliasDecl.name] = typeAliasDecl
}
}
package mutating func handle(
sourceFilePath: String,
memberBlock node: MemberBlockSyntax,
parent: SwiftNominalTypeDeclaration
) {
for member in node.members {
self.handle(sourceFilePath: sourceFilePath, memberDecl: member.decl, parent: parent)
}
}
/// Add a nested type declaration to the symbol table.
package mutating func handle(
sourceFilePath: String,
memberDecl decl: DeclSyntax,
parent: SwiftNominalTypeDeclaration
) {
if let nominalMember = decl.asNominal {
self.handle(sourceFilePath: sourceFilePath, nominalTypeDecl: nominalMember, parent: parent)
} else if let typeAliasMember = decl.as(TypeAliasDeclSyntax.self) {
self.handle(sourceFilePath: sourceFilePath, typeAliasDecl: typeAliasMember, parent: parent)
} else if let ifConfigNode = decl.as(IfConfigDeclSyntax.self) {
let (clause, _) = ifConfigNode.activeClause(in: buildConfig)
if let clause, let elements = clause.elements {
switch elements {
case .decls(let memberBlock):
for memberItem in memberBlock {
self.handle(sourceFilePath: sourceFilePath, memberDecl: memberItem.decl, parent: parent)
}
case .statements(let codeBlock):
for codeItem in codeBlock {
if let declNode = codeItem.item.as(DeclSyntax.self) {
self.handle(sourceFilePath: sourceFilePath, memberDecl: declNode, parent: parent)
}
}
default:
break
}
}
}
}
package mutating func handle(
extensionDecl node: ExtensionDeclSyntax,
sourceFilePath: String
) {
if !self.tryHandle(extension: node, sourceFilePath: sourceFilePath) {
self.unresolvedExtensions.append(node)
}
}
/// Add any nested types within the given extension to the symbol table.
/// If the extended nominal type can't be resolved, returns false.
package mutating func tryHandle(
extension node: ExtensionDeclSyntax,
sourceFilePath: String
) -> Bool {
// Try to resolve the type referenced by this extension declaration.
// If it fails, we'll try again later.
let table = SwiftSymbolTable(
parsedModule: symbolTable,
importedModules: importedModules
)
let lookupContext = SwiftTypeLookupContext(symbolTable: table)
guard let extendedType = try? SwiftType(node.extendedType, lookupContext: lookupContext) else {
return false
}
guard let extendedNominal = extendedType.asNominalTypeDeclaration else {
// Extending type was not a nominal type. Ignore it.
return true
}
// Find any nested types within this extension and add them.
self.handle(sourceFilePath: sourceFilePath, memberBlock: node.memberBlock, parent: extendedNominal)
return true
}
package mutating func handle(
ifConfig node: IfConfigDeclSyntax,
sourceFilePath: String
) {
let (clause, _) = node.activeClause(in: buildConfig)
if let clause, let elements = clause.elements {
switch elements {
case .statements(let codeBlock):
for codeItem in codeBlock {
self.handle(codeBlockItem: codeItem.item, sourceFilePath: sourceFilePath)
}
default:
break
}
}
}
/// Finalize the symbol table and return it.
package mutating func finalize() -> SwiftModuleSymbolTable {
// Handle the unresolved extensions.
// The work queue is required because, the extending type might be declared
// in another extension that hasn't been processed. E.g.:
//
// extension Outer.Inner { struct Deeper {} }
// extension Outer { struct Inner {} }
// struct Outer {}
//
while !unresolvedExtensions.isEmpty {
var extensions = self.unresolvedExtensions
extensions.removeAll(where: {
self.tryHandle(extension: $0, sourceFilePath: "FIXME_MISSING_FILEPATH.swift") // FIXME: missing filepath here in finalize
})
// If we didn't resolve anything, we're done.
if extensions.count == unresolvedExtensions.count {
break
}
assert(extensions.count < unresolvedExtensions.count)
self.unresolvedExtensions = extensions
}
return symbolTable
}
}
extension DeclSyntaxProtocol {
package var asNominal: NominalTypeDeclSyntaxNode? {
switch DeclSyntax(self).as(DeclSyntaxEnum.self) {
case .actorDecl(let actorDecl): actorDecl
case .classDecl(let classDecl): classDecl
case .enumDecl(let enumDecl): enumDecl
case .protocolDecl(let protocolDecl): protocolDecl
case .structDecl(let structDecl): structDecl
default: nil
}
}
}