Files
swift-mirror/lib/SymbolGraphGen/Edge.h
Max Obermeier 453fd2231b Allow for emission of swift.extension symbols for extensions to external types in swiftSymbolGraphGen (#59047)
This includes:
 - bumping the SWIFT_SYMBOLGRAPH_FORMAT_MINOR version
 - introduction of the "swift.extension" symbol and "extensionTo" relationship
 - adding support for ExtensionDecl to the Symbol class
 - adding a "typeKind" field to the symbol's extension mixin which indicates what kind
   of symbol was extended
 - intoduction of the -emit-extension-block-symbols flag, which enables the behavior
   outlined below
 - adaptions to SymbolGraphASTWalker that ensure a swift.extension symbol is emitted
   for each extension to a type that does not exist in the local symbol graph
 - adaptions to SymbolGraph and SymbolGraphASTWalker that ensure member and conformance
   relationships are correctly associated with the swift.extension symbol instead of
   the original type declaration's (extended nominal's) symbol where applicable
 - adaptions to SymbolGraphASTWalker that ensure swift.extension symbols are connected
   to their respective extended nominal's symbol using an extensionTo relationship

Testing:
- adds SymbolGraph tests that test behavior only relevant in
  -emit-extension-block-symbols mode
- adapts some SymbolGraph tests to additionally test similar behavior for
  extensions to external types in -emit-extension-block-symbols mode
- adapts some SymbolGraph tests to (additionally or exclusively) test the
  behavior with -emit-extension-block-symbols mode enabled

Bugfixes:
- fixes a bug where some conformsTo relationships implicated by the conformances
  declared on an extension to an external type were not emitted
  (see test/SymbolGraph/Relationships/ConformsTo/Indirect.swift)

Further changes:
- documents the strategy for naming and associating children declared in extensions
  to typealiases (see test/SymbolGraph/Relationships/MemberOf/Typealias.swift,
  test/SymbolGraph/Symbols/Names.swift)
2022-09-16 12:02:40 -06:00

197 lines
5.8 KiB
C++

//===--- 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