1
1
/*
2
2
This source file is part of the Swift.org open source project
3
3
4
- Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
4
+ Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
5
5
Licensed under Apache License v2.0 with Runtime Library Exception
6
6
7
7
See https://swift.org/LICENSE.txt for license information
@@ -39,27 +39,29 @@ struct SymbolGraphRelationshipsBuilder {
39
39
selector: UnifiedSymbolGraph . Selector ,
40
40
in bundle: DocumentationBundle ,
41
41
context: DocumentationContext ,
42
- symbolIndex: inout [ String : DocumentationNode ] ,
42
+ symbolIndex: inout [ String : ResolvedTopicReference ] ,
43
+ documentationCache: [ ResolvedTopicReference : DocumentationNode ] ,
43
44
engine: DiagnosticEngine
44
45
) {
45
46
// Resolve source symbol
46
- guard let implementorNode = symbolIndex [ edge. source] ,
47
- let implementorSymbol = implementorNode. semantic as? Symbol else {
47
+ guard let implementorNode = symbolIndex [ edge. source] . flatMap ( { documentationCache [ $0] } ) ,
48
+ let implementorSymbol = implementorNode. semantic as? Symbol
49
+ else {
48
50
// The source node for implementation relationship not found.
49
51
engine. emit ( NodeProblem . notFound ( edge. source) )
50
52
return
51
53
}
52
54
53
55
// Resolve target symbol if possible
54
- let optionalInterfaceNode = symbolIndex [ edge. target]
56
+ let optionalInterfaceNode = symbolIndex [ edge. target] . flatMap { documentationCache [ $0 ] }
55
57
56
58
if optionalInterfaceNode == nil {
57
59
// Take the interface language of the target symbol
58
60
// or if external - default to the language of the current symbol.
59
- let language = symbolIndex [ edge. target] ? . reference . sourceLanguage
61
+ let language = symbolIndex [ edge. target] ? . sourceLanguage
60
62
?? implementorNode. reference. sourceLanguage
61
63
62
- let symbolReference = SymbolReference ( edge. target, interfaceLanguage: language, symbol: symbolIndex [ edge. target] ? . symbol)
64
+ let symbolReference = SymbolReference ( edge. target, interfaceLanguage: language, symbol: symbolIndex [ edge. target] . flatMap { documentationCache [ $0 ] ? . symbol } )
63
65
guard let unresolved = UnresolvedTopicReference ( symbolReference: symbolReference, bundle: bundle) else {
64
66
// The symbol reference format is invalid.
65
67
engine. emit ( NodeProblem . invalidReference ( symbolReference. path) )
@@ -74,9 +76,10 @@ struct SymbolGraphRelationshipsBuilder {
74
76
// Find out the parent's title
75
77
let parentName : String ?
76
78
77
- if let reference = symbolIndex [ edge. source] ? . reference,
78
- let parentNode = try ? context. entity ( with: reference. removingLastPathComponent ( ) ) ,
79
- let title = ( parentNode. semantic as? Symbol ) ? . title {
79
+ if let reference = symbolIndex [ edge. source] ,
80
+ let parentNode = try ? context. entity ( with: reference. removingLastPathComponent ( ) ) ,
81
+ let title = ( parentNode. semantic as? Symbol ) ? . title
82
+ {
80
83
parentName = title
81
84
} else {
82
85
parentName = nil
@@ -93,14 +96,14 @@ struct SymbolGraphRelationshipsBuilder {
93
96
)
94
97
95
98
// Make the implementation a child of the requirement
96
- guard let childReference = symbolIndex [ edge. source] ? . reference else {
99
+ guard let childReference = symbolIndex [ edge. source] else {
97
100
// The child wasn't found, invalid reference in relationship.
98
101
engine. emit ( SymbolGraphRelationshipsBuilder . NodeProblem. notFound ( edge. source) )
99
102
return
100
103
}
101
104
102
105
if let child = context. topicGraph. nodeWithReference ( childReference) ,
103
- let targetReference = symbolIndex [ edge. target] ? . reference ,
106
+ let targetReference = symbolIndex [ edge. target] ,
104
107
let parent = context. topicGraph. nodeWithReference ( targetReference) {
105
108
context. topicGraph. addEdge ( from: parent, to: child)
106
109
}
@@ -119,30 +122,32 @@ struct SymbolGraphRelationshipsBuilder {
119
122
edge: SymbolGraph . Relationship ,
120
123
selector: UnifiedSymbolGraph . Selector ,
121
124
in bundle: DocumentationBundle ,
122
- symbolIndex: inout [ String : DocumentationNode ] ,
125
+ symbolIndex: inout [ String : ResolvedTopicReference ] ,
126
+ documentationCache: [ ResolvedTopicReference : DocumentationNode ] ,
123
127
engine: DiagnosticEngine
124
128
) {
125
129
// Resolve source symbol
126
- guard let conformingNode = symbolIndex [ edge. source] ,
127
- let conformingSymbol = conformingNode. semantic as? Symbol else {
128
- // The source node for coformance relationship not found.
130
+ guard let conformingNode = symbolIndex [ edge. source] . flatMap ( { documentationCache [ $0] } ) ,
131
+ let conformingSymbol = conformingNode. semantic as? Symbol
132
+ else {
133
+ // The source node for conformance relationship not found.
129
134
engine. emit ( NodeProblem . notFound ( edge. source) )
130
135
return
131
136
}
132
137
133
138
// Resolve target symbol if possible
134
- let optionalConformanceNode = symbolIndex [ edge. target]
139
+ let optionalConformanceNode = symbolIndex [ edge. target] . flatMap { documentationCache [ $0 ] }
135
140
let conformanceNodeReference : TopicReference
136
141
137
142
if let conformanceNode = optionalConformanceNode {
138
143
conformanceNodeReference = . successfullyResolved( conformanceNode. reference)
139
144
} else {
140
145
// Take the interface language of the target symbol
141
146
// or if external - default to the language of the current symbol.
142
- let language = symbolIndex [ edge. target] ? . reference . sourceLanguage
147
+ let language = symbolIndex [ edge. target] ? . sourceLanguage
143
148
?? conformingNode. reference. sourceLanguage
144
149
145
- let symbolReference = SymbolReference ( edge. target, interfaceLanguage: language, symbol: symbolIndex [ edge. target] ? . symbol)
150
+ let symbolReference = SymbolReference ( edge. target, interfaceLanguage: language, symbol: symbolIndex [ edge. target] . flatMap { documentationCache [ $0 ] ? . symbol } )
146
151
guard let unresolved = UnresolvedTopicReference ( symbolReference: symbolReference, bundle: bundle) else {
147
152
// The symbol reference format is invalid.
148
153
engine. emit ( NodeProblem . invalidReference ( symbolReference. path) )
@@ -203,29 +208,32 @@ struct SymbolGraphRelationshipsBuilder {
203
208
edge: SymbolGraph . Relationship ,
204
209
selector: UnifiedSymbolGraph . Selector ,
205
210
in bundle: DocumentationBundle ,
206
- symbolIndex: inout [ String : DocumentationNode ] ,
211
+ symbolIndex: inout [ String : ResolvedTopicReference ] ,
212
+ documentationCache: [ ResolvedTopicReference : DocumentationNode ] ,
207
213
engine: DiagnosticEngine
208
214
) {
209
215
// Resolve source symbol
210
- guard let childNode = symbolIndex [ edge. source] ,
211
- let childSymbol = childNode. semantic as? Symbol else {
216
+ guard let childNode = symbolIndex [ edge. source] . flatMap ( { documentationCache [ $0] } ) ,
217
+ let childSymbol = childNode. semantic as? Symbol
218
+ else {
212
219
// The source node for inheritance relationship not found.
213
220
engine. emit ( NodeProblem . notFound ( edge. source) )
214
221
return
215
222
}
216
223
217
224
// Resolve target symbol if possible
218
- let optionalParentNode = symbolIndex [ edge. target]
225
+ let optionalParentNode = symbolIndex [ edge. target] . flatMap { documentationCache [ $0 ] }
219
226
let parentNodeReference : TopicReference
220
227
221
228
if let parentNode = optionalParentNode {
222
229
parentNodeReference = . successfullyResolved( parentNode. reference)
223
230
} else {
224
- // Use the target symbol language, if external - fallback on child symbol's langauge
225
- let language = symbolIndex [ edge. target] ? . symbol. map ( { SourceLanguage ( id: $0. identifier. interfaceLanguage) } )
226
- ?? childNode. reference. sourceLanguage
231
+ // Use the target symbol language, if external - fallback on child symbol's language
232
+ let language : SourceLanguage = symbolIndex [ edge. target] . flatMap {
233
+ documentationCache [ $0] ? . symbol. map ( { SourceLanguage ( id: $0. identifier. interfaceLanguage) } )
234
+ } ?? childNode. reference. sourceLanguage
227
235
228
- let symbolReference = SymbolReference ( edge. target, interfaceLanguage: language, symbol: symbolIndex [ edge. target] ? . symbol)
236
+ let symbolReference = SymbolReference ( edge. target, interfaceLanguage: language, symbol: symbolIndex [ edge. target] . flatMap { documentationCache [ $0 ] ? . symbol } )
229
237
guard let unresolved = UnresolvedTopicReference ( symbolReference: symbolReference, bundle: bundle) else {
230
238
// The symbol reference format is invalid.
231
239
engine. emit ( NodeProblem . invalidReference ( symbolReference. path) )
@@ -267,14 +275,16 @@ struct SymbolGraphRelationshipsBuilder {
267
275
edge: SymbolGraph . Relationship ,
268
276
selector: UnifiedSymbolGraph . Selector ,
269
277
in bundle: DocumentationBundle ,
270
- symbolIndex: inout [ String : DocumentationNode ] ,
278
+ symbolIndex: inout [ String : ResolvedTopicReference ] ,
279
+ documentationCache: [ ResolvedTopicReference : DocumentationNode ] ,
271
280
engine: DiagnosticEngine
272
281
) {
273
282
addProtocolRelationship (
274
283
edge: edge,
275
284
selector: selector,
276
285
in: bundle,
277
286
symbolIndex: & symbolIndex,
287
+ documentationCache: documentationCache,
278
288
engine: engine,
279
289
required: true
280
290
)
@@ -291,14 +301,16 @@ struct SymbolGraphRelationshipsBuilder {
291
301
edge: SymbolGraph . Relationship ,
292
302
selector: UnifiedSymbolGraph . Selector ,
293
303
in bundle: DocumentationBundle ,
294
- symbolIndex: inout [ String : DocumentationNode ] ,
304
+ symbolIndex: inout [ String : ResolvedTopicReference ] ,
305
+ documentationCache: [ ResolvedTopicReference : DocumentationNode ] ,
295
306
engine: DiagnosticEngine
296
307
) {
297
308
addProtocolRelationship (
298
309
edge: edge,
299
310
selector: selector,
300
311
in: bundle,
301
312
symbolIndex: & symbolIndex,
313
+ documentationCache: documentationCache,
302
314
engine: engine,
303
315
required: false
304
316
)
@@ -316,12 +328,15 @@ struct SymbolGraphRelationshipsBuilder {
316
328
edge: SymbolGraph . Relationship ,
317
329
selector: UnifiedSymbolGraph . Selector ,
318
330
in bundle: DocumentationBundle ,
319
- symbolIndex: inout [ String : DocumentationNode ] ,
331
+ symbolIndex: inout [ String : ResolvedTopicReference ] ,
332
+ documentationCache: [ ResolvedTopicReference : DocumentationNode ] ,
320
333
engine: DiagnosticEngine , required: Bool
321
334
) {
322
335
// Resolve source symbol
323
- guard let requiredNode = symbolIndex [ edge. source] ,
324
- let requiredSymbol = requiredNode. semantic as? Symbol else {
336
+ guard let requiredNodeRef = symbolIndex [ edge. source] ,
337
+ let requiredNode = documentationCache [ requiredNodeRef] ,
338
+ let requiredSymbol = requiredNode. semantic as? Symbol
339
+ else {
325
340
// The source node for requirement relationship not found.
326
341
engine. emit ( NodeProblem . notFound ( edge. source) )
327
342
return
@@ -341,8 +356,8 @@ struct SymbolGraphRelationshipsBuilder {
341
356
static func addInheritedDefaultImplementation(
342
357
edge: SymbolGraph . Relationship ,
343
358
context: DocumentationContext ,
344
- symbolIndex: inout [ String : DocumentationNode ] ,
345
- moduleName: String ,
359
+ symbolIndex: inout [ String : ResolvedTopicReference ] ,
360
+ moduleName: String ,
346
361
engine: DiagnosticEngine
347
362
) {
348
363
func setAsInheritedSymbol( origin: SymbolGraph . Relationship . SourceOrigin , for node: inout DocumentationNode , originNode: DocumentationNode ? ) {
@@ -374,13 +389,11 @@ struct SymbolGraphRelationshipsBuilder {
374
389
// verify we have the matching data in symbolIndex and documentationCache
375
390
// and add the origin data to the symbol.
376
391
if let origin = edge. mixins [ SymbolGraph . Relationship. SourceOrigin. mixinKey] as? SymbolGraph . Relationship . SourceOrigin ,
377
- let node = symbolIndex [ edge. source] ,
378
- node. semantic is Symbol ,
379
- context. documentationCache. keys. contains ( node. reference) {
380
-
392
+ let reference = symbolIndex [ edge. source] ,
393
+ context. documentationCache [ reference] ? . semantic is Symbol
394
+ {
381
395
// OK to unwrap - we've verified the existence of the key above.
382
- setAsInheritedSymbol ( origin: origin, for: & symbolIndex[ edge. source] !, originNode: symbolIndex [ origin. identifier] )
383
- setAsInheritedSymbol ( origin: origin, for: & context. documentationCache [ node. reference] !, originNode: symbolIndex [ origin. identifier] )
396
+ setAsInheritedSymbol ( origin: origin, for: & context. documentationCache [ reference] !, originNode: symbolIndex [ origin. identifier] . flatMap { context. documentationCache [ $0] } )
384
397
}
385
398
}
386
399
}
0 commit comments