1 //===- ParentMapContext.h - Map of parents using DynTypedNode -------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Similar to ParentMap.h, but generalizes to non-Stmt nodes, which can have
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_PARENTMAPCONTEXT_H
15 #define LLVM_CLANG_AST_PARENTMAPCONTEXT_H
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/ASTTypeTraits.h"
21 class DynTypedNodeList;
23 class ParentMapContext {
25 ParentMapContext(ASTContext &Ctx);
29 /// Returns the parents of the given node (within the traversal scope).
31 /// Note that this will lazily compute the parents of all nodes
32 /// and store them for later retrieval. Thus, the first call is O(n)
33 /// in the number of AST nodes.
35 /// Caveats and FIXMEs:
36 /// Calculating the parent map over all AST nodes will need to load the
37 /// full AST. This can be undesirable in the case where the full AST is
38 /// expensive to create (for example, when using precompiled header
39 /// preambles). Thus, there are good opportunities for optimization here.
40 /// One idea is to walk the given node downwards, looking for references
41 /// to declaration contexts - once a declaration context is found, compute
42 /// the parent map for the declaration context; if that can satisfy the
43 /// request, loading the whole AST can be avoided. Note that this is made
44 /// more complex by statements in templates having multiple parents - those
45 /// problems can be solved by building closure over the templated parts of
46 /// the AST, which also avoids touching large parts of the AST.
47 /// Additionally, we will want to add an interface to already give a hint
48 /// where to search for the parents, for example when looking at a statement
49 /// inside a certain function.
51 /// 'NodeT' can be one of Decl, Stmt, Type, TypeLoc,
52 /// NestedNameSpecifier or NestedNameSpecifierLoc.
53 template <typename NodeT> DynTypedNodeList getParents(const NodeT &Node);
55 DynTypedNodeList getParents(const DynTypedNode &Node);
57 /// Clear parent maps.
60 TraversalKind getTraversalKind() const { return Traversal; }
61 void setTraversalKind(TraversalKind TK) { Traversal = TK; }
63 const Expr *traverseIgnored(const Expr *E) const;
64 Expr *traverseIgnored(Expr *E) const;
65 DynTypedNode traverseIgnored(const DynTypedNode &N) const;
70 TraversalKind Traversal = TK_AsIs;
71 std::unique_ptr<ParentMap> Parents;
74 class TraversalKindScope {
75 ParentMapContext &Ctx;
76 TraversalKind TK = TK_AsIs;
79 TraversalKindScope(ASTContext &ASTCtx, llvm::Optional<TraversalKind> ScopeTK)
80 : Ctx(ASTCtx.getParentMapContext()) {
81 TK = Ctx.getTraversalKind();
83 Ctx.setTraversalKind(*ScopeTK);
86 ~TraversalKindScope() { Ctx.setTraversalKind(TK); }
89 /// Container for either a single DynTypedNode or for an ArrayRef to
90 /// DynTypedNode. For use with ParentMap.
91 class DynTypedNodeList {
92 llvm::AlignedCharArrayUnion<DynTypedNode, ArrayRef<DynTypedNode>> Storage;
96 DynTypedNodeList(const DynTypedNode &N) : IsSingleNode(true) {
97 new (Storage.buffer) DynTypedNode(N);
100 DynTypedNodeList(ArrayRef<DynTypedNode> A) : IsSingleNode(false) {
101 new (Storage.buffer) ArrayRef<DynTypedNode>(A);
104 const DynTypedNode *begin() const {
106 return reinterpret_cast<const ArrayRef<DynTypedNode> *>(Storage.buffer)
108 return reinterpret_cast<const DynTypedNode *>(Storage.buffer);
111 const DynTypedNode *end() const {
113 return reinterpret_cast<const ArrayRef<DynTypedNode> *>(Storage.buffer)
115 return reinterpret_cast<const DynTypedNode *>(Storage.buffer) + 1;
118 size_t size() const { return end() - begin(); }
119 bool empty() const { return begin() == end(); }
121 const DynTypedNode &operator[](size_t N) const {
122 assert(N < size() && "Out of bounds!");
123 return *(begin() + N);
127 template <typename NodeT>
128 inline DynTypedNodeList ParentMapContext::getParents(const NodeT &Node) {
129 return getParents(DynTypedNode::create(Node));
132 template <typename NodeT>
133 inline DynTypedNodeList ASTContext::getParents(const NodeT &Node) {
134 return getParentMapContext().getParents(Node);
138 inline DynTypedNodeList ASTContext::getParents(const DynTypedNode &Node) {
139 return getParentMapContext().getParents(Node);