forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwiftParsedModuleSymbolTableBuilder.swift
More file actions
226 lines (199 loc) · 7.24 KB
/
SwiftParsedModuleSymbolTableBuilder.swift
File metadata and controls
226 lines (199 loc) · 7.24 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
//===----------------------------------------------------------------------===//
//
// 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 SwiftIfConfig
import SwiftSyntax
struct SwiftParsedModuleSymbolTableBuilder {
let log: Logger?
/// The symbol table being built.
var symbolTable: SwiftModuleSymbolTable
/// Imported modules to resolve type syntax.
let importedModules: [String: SwiftModuleSymbolTable]
/// The build configuration used to resolve #if conditional compilation blocks.
let buildConfig: any BuildConfiguration
/// Extension decls their extended type hasn't been resolved.
var unresolvedExtensions: [ExtensionDeclSyntax]
init(
moduleName: String,
requiredAvailablityOfModuleWithName: String? = nil,
alternativeModules: SwiftModuleSymbolTable.AlternativeModuleNamesData? = nil,
importedModules: [String: SwiftModuleSymbolTable],
buildConfig: any BuildConfiguration = .jextractDefault,
log: Logger? = nil
) {
self.log = log
self.symbolTable = .init(
moduleName: moduleName,
requiredAvailablityOfModuleWithName: requiredAvailablityOfModuleWithName,
alternativeModules: alternativeModules
)
self.importedModules = importedModules
self.buildConfig = buildConfig
self.unresolvedExtensions = []
}
var moduleName: String {
symbolTable.moduleName
}
}
extension SwiftParsedModuleSymbolTableBuilder {
mutating func handle(
sourceFile: SourceFileSyntax,
sourceFilePath: String
) {
// Find top-level type declarations.
for statement in sourceFile.statements {
self.handle(codeBlockItem: statement.item, sourceFilePath: sourceFilePath)
}
}
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 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)
}
}
/// Add a nominal type declaration and all of the nested types within it to the symbol
/// table.
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 let _ = symbolTable.lookupType(node.name.text, parent: parent) {
log?.debug("Failed to add a decl into symbol table: redeclaration; " + node.nameForDebug)
return
}
// Otherwise, create the nominal type declaration.
let nominalTypeDecl = SwiftNominalTypeDeclaration(
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)
}
mutating func handle(
sourceFilePath: String,
memberBlock node: MemberBlockSyntax,
parent: SwiftNominalTypeDeclaration
) {
for member in node.members {
// Find any nested types within this nominal type and add them.
if let nominalMember = member.decl.asNominal {
self.handle(sourceFilePath: sourceFilePath, nominalTypeDecl: nominalMember, parent: parent)
}
}
}
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.
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
}
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.
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 {
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
}
}
}