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
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021 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
*/
/**
A symbol graph is a set of *nodes* that represent the symbols in a module and
a set of directed *edges* that represent the relationships between symbols.
*/
public struct SymbolGraph: Codable {
/// Metadata about the symbol graph.
public var metadata: Metadata
/// The module that this symbol graph represents.
public var module: Module
/// The symbols in a module: the nodes in a graph, mapped by precise identifier.
public var symbols: [String: Symbol]
/// The relationships between symbols: the edges in a graph.
public var relationships: [Relationship]
public init(metadata: Metadata, module: Module, symbols: [Symbol], relationships: [Relationship]) {
self.metadata = metadata
self.module = module
self.symbols = [String: Symbol](symbols.lazy.map({ ($0.identifier.precise, $0) }), uniquingKeysWith: { old, new in
SymbolGraph._symbolToKeepInCaseOfPreciseIdentifierConflict(old, new)
})
self.relationships = relationships
}
// MARK: - Codable
public enum CodingKeys: String, CodingKey {
case metadata
case module
case symbols
case relationships
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let metadata = try container.decode(Metadata.self, forKey: .metadata)
let module = try container.decode(Module.self, forKey: .module)
let symbols = try container.decode([Symbol].self, forKey: .symbols)
let relationships = try container.decode([Relationship].self, forKey: .relationships)
self.init(metadata: metadata, module: module, symbols: symbols, relationships: relationships)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(metadata, forKey: .metadata)
try container.encode(module, forKey: .module)
try container.encode(Array(symbols.values), forKey: .symbols)
try container.encode(relationships, forKey: .relationships)
}
public static func _symbolToKeepInCaseOfPreciseIdentifierConflict(_ lhs: Symbol, _ rhs: Symbol) -> Symbol {
if lhs.declarationContainsAsyncKeyword() {
var result = rhs
result.addAlternateDeclaration(from: lhs)
return result
} else if rhs.declarationContainsAsyncKeyword() {
var result = lhs
result.addAlternateDeclaration(from: rhs)
return result
} else {
// It's not expected to ever end up here, but if we do, we return the symbol with the longer name
// to have consistent results.
var result: Symbol
let other: Symbol
if lhs.names.title.count < rhs.names.title.count {
result = rhs
other = lhs
} else if rhs.names.title.count < lhs.names.title.count {
result = lhs
other = rhs
} else {
// If, by total coincidence, both symbols have the same length, try a lexicographic
// sort and pick the first one.
if lhs.names.title <= rhs.names.title {
result = lhs
other = rhs
} else {
result = rhs
other = lhs
}
}
result.addAlternateDeclaration(from: other)
return result
}
}
}
extension SymbolGraph.Symbol {
fileprivate func declarationContainsAsyncKeyword() -> Bool {
return (mixins[DeclarationFragments.mixinKey] as? DeclarationFragments)?.declarationFragments.contains(where: { fragment in
fragment.kind == .keyword && fragment.spelling == "async"
}) == true
}
}
|