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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021-2024 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
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import SymbolKit
/// A set of functions that add relationship information to a topic graph.
struct SymbolGraphRelationshipsBuilder {
/// A namespace for creation of topic-graph related problems.
enum NodeProblem {
/// Returns a problem about a node with the given precise identifier not being found.
static func notFound(_ identifier: String) -> Problem {
return Problem(diagnostic: Diagnostic(source: nil, severity: .error, range: nil, identifier: "org.swift.docc.SymbolNodeNotFound", summary: "Symbol with identifier \(identifier.singleQuoted) couldn't be found"), possibleSolutions: [])
}
/// Returns a problem about a node with the given reference not being found.
static func invalidReference(_ reference: String) -> Problem {
return Problem(diagnostic: Diagnostic(source: nil, severity: .error, range: nil, identifier: "org.swift.docc.InvalidSymbolIdentifier", summary: "Relationship symbol path \(reference.singleQuoted) isn't valid"), possibleSolutions: [])
}
}
/// Adds a two-way relationship from a default implementation to a protocol requirement.
///
/// The target is optional, because the protocol might be from a different symbol graph.
/// - Parameters:
/// - edge: A symbol graph relationship with a source and a target.
/// - selector: The symbol graph selector in which the relationship is relevant.
/// - bundle: A documentation bundle.
/// - context: A documentation context.
/// - localCache: A cache of local documentation content.
/// - engine: A diagnostic collecting engine.
static func addImplementationRelationship(
edge: SymbolGraph.Relationship,
selector: UnifiedSymbolGraph.Selector,
in bundle: DocumentationBundle,
context: DocumentationContext,
localCache: DocumentationContext.LocalCache,
engine: DiagnosticEngine
) {
// Resolve source symbol
guard let implementorNode = localCache[edge.source],
let implementorSymbol = implementorNode.semantic as? Symbol
else {
// The source node for implementation relationship not found.
engine.emit(NodeProblem.notFound(edge.source))
return
}
// Resolve target symbol if possible
let optionalInterfaceNode = localCache[edge.target]
if optionalInterfaceNode == nil {
// Take the interface language of the target symbol
// or if external - default to the language of the current symbol.
let language = localCache[edge.target]?.sourceLanguage
?? implementorNode.reference.sourceLanguage
let symbolReference = SymbolReference(edge.target, interfaceLanguage: language, symbol: localCache[edge.target]?.symbol)
guard let unresolved = UnresolvedTopicReference(symbolReference: symbolReference, bundle: bundle) else {
// The symbol reference format is invalid.
engine.emit(NodeProblem.invalidReference(symbolReference.path))
return
}
if let targetFallback = edge.targetFallback {
implementorSymbol.defaultImplementations.targetFallbacks[.unresolved(unresolved)] = targetFallback
}
}
// Find out the parent's title
let parentName: String?
if let reference = localCache.reference(symbolID: edge.source),
let parentNode = try? context.entity(with: reference.removingLastPathComponent()),
let title = (parentNode.semantic as? Symbol)?.title
{
parentName = title
} else {
parentName = nil
}
// Add default implementations to the requirement symbol.
if let interfaceSymbol = optionalInterfaceNode?.semantic as? Symbol {
// Add a default implementation
interfaceSymbol.defaultImplementationsVariants[
DocumentationDataVariantsTrait(for: selector),
default: DefaultImplementationsSection()
].addImplementation(
Implementation(reference: .successfullyResolved(implementorNode.reference), parent: parentName, fallbackName: edge.targetFallback)
)
// Make the implementation a child of the requirement
guard let childReference = localCache.reference(symbolID: edge.source) else {
// The child wasn't found, invalid reference in relationship.
engine.emit(SymbolGraphRelationshipsBuilder.NodeProblem.notFound(edge.source))
return
}
if let child = context.topicGraph.nodeWithReference(childReference),
let targetReference = localCache.reference(symbolID: edge.target),
let parent = context.topicGraph.nodeWithReference(targetReference) {
context.topicGraph.addEdge(from: parent, to: child)
}
}
}
/// Adds a two-way relationship from a conforming type to a protocol.
///
/// The target is optional, because the protocol might be from a different module.
/// - Parameters:
/// - edge: A symbol-graph relationship with a source and a target.
/// - selector: The symbol graph selector in which the relationship is relevant.
/// - bundle: A documentation bundle.
/// - localCache: A cache of local documentation content.
/// - externalCache: A cache of external documentation content.
/// - engine: A diagnostic collecting engine.
static func addConformanceRelationship(
edge: SymbolGraph.Relationship,
selector: UnifiedSymbolGraph.Selector,
in bundle: DocumentationBundle,
localCache: DocumentationContext.LocalCache,
externalCache: DocumentationContext.ExternalCache,
engine: DiagnosticEngine
) {
// Resolve source symbol
guard let conformingNode = localCache[edge.source],
let conformingSymbol = conformingNode.semantic as? Symbol
else {
// The source node for conformance relationship not found.
engine.emit(NodeProblem.notFound(edge.source))
return
}
// Resolve target symbol if possible
let optionalConformanceNode = localCache[edge.target]
let conformanceNodeReference: TopicReference
if let conformanceNode = optionalConformanceNode {
conformanceNodeReference = .successfullyResolved(conformanceNode.reference)
} else if let resolved = externalCache.reference(symbolID: edge.target) {
conformanceNodeReference = .successfullyResolved(resolved)
} else {
// Take the interface language of the target symbol
// or if external - default to the language of the current symbol.
let language = localCache[edge.target]?.sourceLanguage
?? conformingNode.reference.sourceLanguage
let symbolReference = SymbolReference(edge.target, interfaceLanguage: language, symbol: localCache[edge.target]?.symbol)
guard let unresolved = UnresolvedTopicReference(symbolReference: symbolReference, bundle: bundle) else {
// The symbol reference format is invalid.
engine.emit(NodeProblem.invalidReference(symbolReference.path))
return
}
conformanceNodeReference = .unresolved(unresolved)
if let targetFallback = edge.targetFallback {
conformingSymbol.relationshipsVariants[
DocumentationDataVariantsTrait(for: selector),
default: RelationshipsSection()
].targetFallbacks[.unresolved(unresolved)] = targetFallback
}
}
// Conditional conformance constraints, if any
let relationshipConstraints = edge[mixin: SymbolGraph.Relationship.Swift.GenericConstraints.self]
// Add relationships depending whether it's class inheritance or protocol conformance
if conformingSymbol.kind.identifier == .protocol {
conformingSymbol.relationshipsVariants[
DocumentationDataVariantsTrait(for: selector),
default: RelationshipsSection()
].addRelationship(.inheritsFrom(conformanceNodeReference))
} else {
conformingSymbol.relationshipsVariants[
DocumentationDataVariantsTrait(for: selector),
default: RelationshipsSection()
].addRelationship(.conformsTo(conformanceNodeReference, relationshipConstraints?.constraints))
}
if let conformanceNode = optionalConformanceNode, let conformanceSymbol = conformanceNode.semantic as? Symbol {
if let rawSymbol = conformingNode.symbol, rawSymbol.kind.identifier == .protocol {
conformanceSymbol.relationshipsVariants[
DocumentationDataVariantsTrait(for: selector),
default: RelationshipsSection()
].addRelationship(.inheritedBy(.successfullyResolved(conformingNode.reference)))
} else {
conformanceSymbol.relationshipsVariants[
DocumentationDataVariantsTrait(for: selector),
default: RelationshipsSection()
].addRelationship(.conformingType(.successfullyResolved(conformingNode.reference), relationshipConstraints?.constraints))
}
}
}
/// Adds a two-way relationship from a child class to a parent class *or*
/// a conforming protocol to a parent protocol.
///
/// The target is optional, because the protocol or class might be from a different module.
/// - Parameters:
/// - edge: A symbol graph relationship with a source and a target.
/// - selector: The symbol graph selector in which the relationship is relevant.
/// - bundle: A documentation bundle.
/// - localCache: A cache of local documentation content.
/// - externalCache: A cache of external documentation content.
/// - engine: A diagnostic collecting engine.
static func addInheritanceRelationship(
edge: SymbolGraph.Relationship,
selector: UnifiedSymbolGraph.Selector,
in bundle: DocumentationBundle,
localCache: DocumentationContext.LocalCache,
externalCache: DocumentationContext.ExternalCache,
engine: DiagnosticEngine
) {
// Resolve source symbol
guard let childNode = localCache[edge.source],
let childSymbol = childNode.semantic as? Symbol
else {
// The source node for inheritance relationship not found.
engine.emit(NodeProblem.notFound(edge.source))
return
}
// Resolve target symbol if possible
let optionalParentNode = localCache[edge.target]
let parentNodeReference: TopicReference
if let parentNode = optionalParentNode {
parentNodeReference = .successfullyResolved(parentNode.reference)
} else if let resolved = externalCache.reference(symbolID: edge.target) {
parentNodeReference = .successfullyResolved(resolved)
} else {
// Fallback on child symbol's language
let language = childNode.reference.sourceLanguage
let symbolReference = SymbolReference(edge.target, interfaceLanguage: language, symbol: nil)
guard let unresolved = UnresolvedTopicReference(symbolReference: symbolReference, bundle: bundle) else {
// The symbol reference format is invalid.
engine.emit(NodeProblem.invalidReference(symbolReference.path))
return
}
parentNodeReference = .unresolved(unresolved)
// At this point the parent node we are inheriting from is unresolved, so let's add a fallback in case we can not resolve it later.
if let targetFallback = edge.targetFallback {
childSymbol.relationshipsVariants[
DocumentationDataVariantsTrait(for: selector),
default: RelationshipsSection()
].targetFallbacks[.unresolved(unresolved)] = targetFallback
}
}
// Add relationships
childSymbol.relationshipsVariants[
DocumentationDataVariantsTrait(for: selector),
default: RelationshipsSection()
].addRelationship(.inheritsFrom(parentNodeReference))
if let parentNode = optionalParentNode, let parentSymbol = parentNode.semantic as? Symbol {
parentSymbol.relationshipsVariants[
DocumentationDataVariantsTrait(for: selector),
default: RelationshipsSection()
].addRelationship(.inheritedBy(.successfullyResolved(childNode.reference)))
}
}
/// Adds a required relationship from a type member to a protocol requirement.
/// - Parameters:
/// - edge: A symbol graph relationship with a source and a target.
/// - localCache: A cache of local documentation content.
/// - engine: A diagnostic collecting engine.
static func addRequirementRelationship(
edge: SymbolGraph.Relationship,
localCache: DocumentationContext.LocalCache,
engine: DiagnosticEngine
) {
addProtocolRelationship(
edge: edge,
localCache: localCache,
engine: engine,
required: true
)
}
/// Adds an optional relationship from a type member to a protocol requirement.
/// - Parameters:
/// - edge: A symbol graph relationship with a source and a target.
/// - localCache: A cache of local documentation content.
/// - engine: A diagnostic collecting engine.
static func addOptionalRequirementRelationship(
edge: SymbolGraph.Relationship,
localCache: DocumentationContext.LocalCache,
engine: DiagnosticEngine
) {
addProtocolRelationship(
edge: edge,
localCache: localCache,
engine: engine,
required: false
)
}
/// Adds a relationship from a type member to a protocol requirement.
/// - Parameters:
/// - edge: A symbol graph relationship with a source and a target.
/// - localCache: A cache of local documentation content.
/// - engine: A diagnostic collecting engine.
/// - required: A bool value indicating whether the protocol requirement is required or optional
private static func addProtocolRelationship(
edge: SymbolGraph.Relationship,
localCache: DocumentationContext.LocalCache,
engine: DiagnosticEngine,
required: Bool
) {
// Resolve source symbol
guard let requiredNode = localCache[edge.source],
let requiredSymbol = requiredNode.semantic as? Symbol
else {
// The source node for requirement relationship not found.
engine.emit(NodeProblem.notFound(edge.source))
return
}
requiredSymbol.isRequired = required
}
/// Sets a node in the context as an inherited symbol.
///
/// - Parameters:
/// - sourceOrigin: The symbol's source origin.
/// - inheritedSymbolID: The precise identifier of the inherited symbol.
/// - context: A documentation context.
/// - localCache: A cache of local documentation content.
/// - moduleName: The symbol name of the current module.
static func addInheritedDefaultImplementation(
sourceOrigin: SymbolGraph.Relationship.SourceOrigin,
inheritedSymbolID: String,
context: DocumentationContext,
localCache: DocumentationContext.LocalCache,
moduleName: String
) {
guard let inherited = localCache[inheritedSymbolID], let inheritedSymbolSemantic = inherited.semantic as? Symbol else {
return
}
// If this a local inherited symbol, update the origin data of that symbol.
inheritedSymbolSemantic.origin = sourceOrigin
// Check if the origin symbol is also local. Always inherit the documentation from other local symbols.
if let parentSymbolSemantic = localCache[sourceOrigin.identifier]?.semantic as? Symbol,
inheritedSymbolSemantic.moduleReference == parentSymbolSemantic.moduleReference
{
return
}
// Remove any inherited docs from the original symbol if the feature is disabled.
// However, when the docs are inherited from within the same module, its content can be resolved in
// the local context, so keeping those inherited docs provide a better user experience.
if !context.externalMetadata.inheritDocs, let unifiedSymbol = inherited.unifiedSymbol, unifiedSymbol.documentedSymbol?.isDocCommentFromSameModule(symbolModuleName: moduleName) == false {
unifiedSymbol.docComment.removeAll()
}
}
/// Add a new generic constraint: "Self is SomeProtocol" to members of
/// protocol extensions of protocols from external modules. When a protocol
/// is defined in a different module it's not clear which protocol the
/// extension is for since we don't otherwise display that, unless implied
/// by curation.
///
/// - Parameters:
/// - edge: A symbol graph relationship with a source and a target.
/// - extendedModuleRelationships: Source->target dictionary for external module relationships.
/// - localCache: A cache of local documentation content.
static func addProtocolExtensionMemberConstraint(
edge: SymbolGraph.Relationship,
extendedModuleRelationships: [String : String],
localCache: DocumentationContext.LocalCache
) {
// Utility function to look up a symbol identifier in the
// symbol index, returning its documentation node and semantic symbol
func nodeAndSymbolFor(identifier: String) -> (DocumentationNode, Symbol)? {
if let node = localCache[identifier], let symbol = node.semantic as? Symbol {
return (node, symbol)
}
return nil
}
// Is this symbol a member of some type from an extended module?
guard let extendedModuleRelationship = extendedModuleRelationships[edge.target] else {
return
}
// Return unless the target symbol is a protocol. The "Self is ..."
// constraint only makes sense for protocol extensions.
guard let (targetNode, targetSymbol) = nodeAndSymbolFor(identifier: edge.target) else {
return
}
guard targetNode.kind == .extendedProtocol else {
return
}
// Obtain the source symbol
guard let (_, sourceSymbol) = nodeAndSymbolFor(identifier: edge.source) else {
return
}
// Obtain the extended module documentation node.
guard let (_, extendedModuleSymbol) = nodeAndSymbolFor(identifier: extendedModuleRelationship) else {
return
}
// Create a new generic constraint: "Self is SomeProtocol" to show which
// protocol this function's extension is extending. When the protocol is
// defined in a different module it's not clear at all which protocol it
// is, especially if the curation doesn't indicate that.
let newConstraint = SymbolGraph.Symbol.Swift.GenericConstraint(
kind: SymbolGraph.Symbol.Swift.GenericConstraint.Kind.sameType,
leftTypeName: "Self",
rightTypeName: targetSymbol.title
)
// Add the constraint to the source symbol, the member of the protocol
// extension.
sourceSymbol.addSwiftExtensionConstraint(
extendedModule: extendedModuleSymbol.title,
extendedSymbolKind: .protocol,
constraint: newConstraint
)
}
static func addOverloadRelationship(
edge: SymbolGraph.Relationship,
context: DocumentationContext,
localCache: DocumentationContext.LocalCache,
engine: DiagnosticEngine
) {
guard let overloadNode = localCache[edge.source] else {
engine.emit(NodeProblem.notFound(edge.source))
return
}
guard let overloadGroupNode = localCache[edge.target] else {
engine.emit(NodeProblem.notFound(edge.target))
return
}
guard let overloadTopicGraphNode = context.topicGraph.nodes[overloadNode.reference],
let overloadGroupTopicGraphNode = context.topicGraph.nodes[overloadGroupNode.reference]
else {
return
}
overloadGroupTopicGraphNode.isOverloadGroup = true
context.topicGraph.addEdge(from: overloadGroupTopicGraphNode, to: overloadTopicGraphNode)
}
}
|