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
|
//===--- Edge.h - Symbol Graph Edge ---------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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 the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_SYMBOLGRAPHGEN_EDGE_H
#define SWIFT_SYMBOLGRAPHGEN_EDGE_H
#include "llvm/Support/JSON.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Type.h"
#include "swift/Basic/LLVM.h"
#include "JSON.h"
#include "Symbol.h"
namespace swift {
namespace symbolgraphgen {
struct SymbolGraph;
/// The kind of relationship, tagging an edge in the graph.
struct RelationshipKind {
StringRef Name;
RelationshipKind(llvm::StringRef Name) : Name(Name) {}
/**
A symbol A is a member of another symbol B.
For example, a method or field of a class would be a member of that class.
The implied inverse of this relationship is a symbol B is the owner
of a member symbol A.
*/
static inline RelationshipKind MemberOf() {
return RelationshipKind { "memberOf" };
}
/**
A symbol A conforms to an interface/protocol symbol B.
For example, a class `C` that conforms to protocol `P` in Swift would use
this relationship.
The implied inverse of this relationship is a symbol B that has
a conformer A.
*/
static inline RelationshipKind ConformsTo() {
return RelationshipKind { "conformsTo" };
}
/**
A symbol A inherits from another symbol B.
For example, a derived class inherits from a base class, or a protocol
that refines another protocol would use this relationship.
The implied inverse of this relationship is a symbol B is a base
of another symbol A.
*/
static inline RelationshipKind InheritsFrom() {
return RelationshipKind { "inheritsFrom" };
}
/**
A symbol A serves as a default implementation of an interface requirement B.
The implied inverse of this relationship is an interface requirement B
has a default implementation of A.
*/
static inline RelationshipKind DefaultImplementationOf() {
return RelationshipKind { "defaultImplementationOf" };
}
/**
A symbol A overrides another symbol B, such as through inheritance.
The implied inverse of this relationship is a symbol A is the base
of symbol B.
*/
static inline RelationshipKind Overrides() {
return RelationshipKind { "overrides" };
}
/**
A symbol A is a requirement of interface B.
The implied inverse of this relationship is an interface B
has a requirement of A.
*/
static inline RelationshipKind RequirementOf() {
return RelationshipKind { "requirementOf" };
}
/**
A symbol A is an optional requirement of interface B.
The implied inverse of this relationship is an interface B
has an optional requirement of A.
*/
static inline RelationshipKind OptionalRequirementOf() {
return RelationshipKind { "optionalRequirementOf" };
}
/**
A symbol A extends a symbol B with members or conformances.
This relationship describes the connection between extension blocks
(swift.extension symbols) and the type they extend.
The implied inverse of this relationship is a symbol B that is extended
by an extension block symbol A.
*/
static inline RelationshipKind ExtensionTo() {
return RelationshipKind{"extensionTo"};
}
bool operator==(const RelationshipKind &Other) const {
return Name == Other.Name;
}
bool operator<(const RelationshipKind &Other) const {
return Name < Other.Name;
}
};
/// A relationship between two symbols: an edge in a directed graph.
struct Edge {
SymbolGraph *Graph;
/// The kind of relationship this edge represents.
RelationshipKind Kind;
/// The precise identifier of the source symbol node.
Symbol Source;
/// The precise identifier of the target symbol node.
Symbol Target;
/// If this is a conformsTo relationship, the extension that defined
/// the conformance.
const ExtensionDecl *ConformanceExtension;
void serialize(llvm::json::OStream &OS) const;
};
} // end namespace symbolgraphgen
} // end namespace swift
namespace llvm {
using SymbolGraph = swift::symbolgraphgen::SymbolGraph;
using Symbol = swift::symbolgraphgen::Symbol;
using Edge = swift::symbolgraphgen::Edge;
using ExtensionDecl = swift::ExtensionDecl;
template <> struct DenseMapInfo<Edge> {
static inline Edge getEmptyKey() {
return {
DenseMapInfo<SymbolGraph *>::getEmptyKey(),
{ "Empty" },
DenseMapInfo<Symbol>::getEmptyKey(),
DenseMapInfo<Symbol>::getEmptyKey(),
DenseMapInfo<const ExtensionDecl *>::getEmptyKey(),
};
}
static inline Edge getTombstoneKey() {
return {
nullptr,
{ "Tombstone" },
DenseMapInfo<Symbol>::getTombstoneKey(),
DenseMapInfo<Symbol>::getTombstoneKey(),
DenseMapInfo<const ExtensionDecl *>::getTombstoneKey(),
};
}
static unsigned getHashValue(const Edge E) {
unsigned H = 0;
H ^= DenseMapInfo<StringRef>::getHashValue(E.Kind.Name);
H ^= DenseMapInfo<Symbol>::getHashValue(E.Source);
H ^= DenseMapInfo<Symbol>::getHashValue(E.Target);
H ^= DenseMapInfo<const ExtensionDecl *>::
getHashValue(E.ConformanceExtension);
return H;
}
static bool isEqual(const Edge LHS, const Edge RHS) {
return LHS.Kind == RHS.Kind &&
DenseMapInfo<Symbol>::isEqual(LHS.Source, RHS.Source) &&
DenseMapInfo<Symbol>::isEqual(LHS.Target, RHS.Target) &&
DenseMapInfo<const ExtensionDecl *>::isEqual(LHS.ConformanceExtension,
RHS.ConformanceExtension);
}
};
} // end namespace llvm
#endif // SWIFT_SYMBOLGRAPHGEN_EDGE_H
|