21 using llvm::itanium_demangle::ForwardTemplateReference;
22 using llvm::itanium_demangle::Node;
24 using llvm::itanium_demangle::StringView;
27 struct FoldingSetNodeIDBuilder {
29 void operator()(
const Node *
P) { ID.
AddPointer(P); }
30 void operator()(StringView Str) {
34 typename std::enable_if<std::is_integral<T>::value ||
35 std::is_enum<T>::value>
::type 39 void operator()(itanium_demangle::NodeArray A) {
41 for (
const Node *
N : A)
46 template<
typename ...T>
48 FoldingSetNodeIDBuilder Builder = {ID};
50 int VisitInOrder[] = {
58 template<
typename NodeT>
struct ProfileSpecificNode {
60 template<
typename ...T>
void operator()(
T ...V) {
67 template<
typename NodeT>
void operator()(
const NodeT *
N) {
68 N->match(ProfileSpecificNode<NodeT>{ID});
72 template<>
void ProfileNode::operator()(
const ForwardTemplateReference *
N) {
77 N->visit(ProfileNode{ID});
80 class FoldingNodeAllocator {
84 itanium_demangle::Node *getNode() {
85 return reinterpret_cast<itanium_demangle::Node *
>(
this + 1);
96 template <
typename T,
typename...
Args>
97 std::pair<Node *, bool> getOrCreateNode(
bool CreateNewNodes,
Args &&... As) {
101 if (std::is_same<T, ForwardTemplateReference>::value) {
104 return {
new (RawAlloc.
Allocate(
sizeof(
T),
alignof(
T)))
105 T(std::forward<Args>(As)...),
114 return {
static_cast<T*
>(Existing->getNode()),
false};
117 return {
nullptr,
true};
119 static_assert(
alignof(
T) <=
alignof(NodeHeader),
120 "underaligned node header for specific node kind");
122 RawAlloc.
Allocate(
sizeof(NodeHeader) +
sizeof(
T),
alignof(NodeHeader));
123 NodeHeader *
New =
new (Storage) NodeHeader;
124 T *
Result =
new (New->getNode())
T(std::forward<Args>(As)...);
126 return {Result,
true};
129 template<
typename T,
typename...
Args>
130 Node *makeNode(
Args &&...As) {
131 return getOrCreateNode<T>(
true, std::forward<Args>(As)...).first;
134 void *allocateNodeArray(
size_t sz) {
135 return RawAlloc.
Allocate(
sizeof(Node *) * sz,
alignof(Node *));
139 class CanonicalizerAllocator :
public FoldingNodeAllocator {
140 Node *MostRecentlyCreated =
nullptr;
141 Node *TrackedNode =
nullptr;
142 bool TrackedNodeIsUsed =
false;
143 bool CreateNewNodes =
true;
146 template<
typename T,
typename ...Args> Node *makeNodeSimple(
Args &&...As) {
147 std::pair<Node *, bool> Result =
148 getOrCreateNode<T>(CreateNewNodes, std::forward<Args>(As)...);
151 MostRecentlyCreated = Result.first;
152 }
else if (Result.first) {
154 if (
auto *N = Remappings.
lookup(Result.first)) {
157 "should never need multiple remap steps");
159 if (Result.first == TrackedNode)
160 TrackedNodeIsUsed =
true;
166 template<
typename T>
struct MakeNodeImpl {
167 CanonicalizerAllocator &Self;
168 template<
typename ...Args> Node *
make(
Args &&...As) {
169 return Self.makeNodeSimple<
T>(std::forward<Args>(As)...);
174 template<
typename T,
typename ...Args> Node *makeNode(
Args &&...As) {
175 return MakeNodeImpl<T>{*
this}.make(std::forward<Args>(As)...);
178 void reset() { MostRecentlyCreated =
nullptr; }
180 void setCreateNewNodes(
bool CNN) { CreateNewNodes = CNN; }
182 void addRemapping(Node *A, Node *
B) {
185 Remappings.
insert(std::make_pair(A, B));
188 bool isMostRecentlyCreated(Node *N)
const {
return MostRecentlyCreated ==
N; }
190 void trackUsesOf(Node *N) {
192 TrackedNodeIsUsed =
false;
194 bool trackedNodeIsUsed()
const {
return TrackedNodeIsUsed; }
200 struct CanonicalizerAllocator::MakeNodeImpl<
202 CanonicalizerAllocator &Self;
203 Node *
make(Node *Child) {
204 Node *StdNamespace = Self.makeNode<itanium_demangle::NameType>(
"std");
207 return Self.makeNode<itanium_demangle::NestedName>(StdNamespace, Child);
213 using CanonicalizingDemangler =
214 itanium_demangle::ManglingParser<CanonicalizerAllocator>;
227 auto &Alloc = P->Demangler.ASTAllocator;
228 Alloc.setCreateNewNodes(
true);
231 P->Demangler.reset(Str.begin(), Str.end());
240 if (Str.size() == 2 && P->Demangler.consumeIf(
"St"))
241 N = P->Demangler.make<itanium_demangle::NameType>(
"std");
246 else if (Str.startswith(
"S"))
248 N = P->Demangler.parseType();
250 N = P->Demangler.parseName();
255 N = P->Demangler.parseType();
260 N = P->Demangler.parseEncoding();
265 if (P->Demangler.numLeft() != 0)
270 return std::make_pair(N, Alloc.isMostRecentlyCreated(N));
273 Node *FirstNode, *SecondNode;
274 bool FirstIsNew, SecondIsNew;
276 std::tie(FirstNode, FirstIsNew) = Parse(First);
280 Alloc.trackUsesOf(FirstNode);
281 std::tie(SecondNode, SecondIsNew) = Parse(Second);
286 if (FirstNode == SecondNode)
289 if (FirstIsNew && !Alloc.trackedNodeIsUsed())
290 Alloc.addRemapping(FirstNode, SecondNode);
291 else if (SecondIsNew)
292 Alloc.addRemapping(SecondNode, FirstNode);
301 bool CreateNewNodes) {
302 Demangler.ASTAllocator.setCreateNewNodes(CreateNewNodes);
303 Demangler.reset(Mangling.
begin(), Mangling.
end());
312 N = Demangler.parse();
314 N = Demangler.make<itanium_demangle::NameType>(
315 StringView(Mangling.
data(), Mangling.
size()));
The first equivalent mangling is invalid.
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
This class represents lattice values for constants.
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
T * FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos)
FindNodeOrInsertPos - Look up the node specified by ID.
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
void AddInteger(signed I)
The second equivalent mangling is invalid.
void InsertNode(T *N, void *InsertPos)
InsertNode - Insert the specified node into the folding set, knowing that it is not already in the fo...
LLVM_NODISCARD size_t size() const
size - Get the string size.
iterator find(const_arg_type_t< KeyT > Val)
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Allocate memory in an ever growing pool, as if by bump-pointer.
itanium_demangle::ManglingParser< DefaultAllocator > Demangler
FoldingSet - This template class is used to instantiate a specialized implementation of the folding s...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static ItaniumManglingCanonicalizer::Key parseMaybeMangledName(CanonicalizingDemangler &Demangler, StringRef Mangling, bool CreateNewNodes)
The mangling fragment is an <encoding>.
Key lookup(StringRef Mangling)
Find a canonical key for the specified mangling, if one has already been formed.
Both the equivalent manglings have already been used as components of some other mangling we've looke...
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
~ItaniumManglingCanonicalizer()
ItaniumManglingCanonicalizer()
Node - This class is used to maintain the singly linked bucket list in a folding set.
void AddString(StringRef String)
The mangling fragment is a <name> (or a predefined <substitution>).
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
EquivalenceError addEquivalence(FragmentKind Kind, StringRef First, StringRef Second)
Add an equivalence between First and Second.
StringRef - Represent a constant reference to a string, i.e.
The mangling fragment is a <type>.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Key canonicalize(StringRef Mangling)
Form a canonical key for the specified mangling.