nixd
Loading...
Searching...
No Matches
libnixf/src/Sema/ParentMap.cpp
Go to the documentation of this file.
2
3using namespace nixf;
4
5void ParentMapAnalysis::dfs(const Node *N, const Node *Parent) {
6 if (!N)
7 return;
8 ParentMap.insert({N, Parent});
9 for (const Node *Ch : N->children())
10 dfs(Ch, N);
11}
12
13const Node *ParentMapAnalysis::query(const Node &N) const {
14 return ParentMap.contains(&N) ? ParentMap.at(&N) : nullptr;
15}
16
17const Node *ParentMapAnalysis::upExpr(const Node &N) const {
18
19 if (Expr::isExpr(N.kind()))
20 return &N;
21 const Node *Up = query(N);
22 if (isRoot(Up, N) || !Up)
23 return nullptr;
24 return upExpr(*Up);
25}
26
27const Node *ParentMapAnalysis::upTo(const Node &N, Node::NodeKind Kind) const {
28
29 if (N.kind() == Kind)
30 return &N;
31 const Node *Up = query(N);
32 if (isRoot(Up, N) || !Up)
33 return nullptr;
34 return upTo(*Up, Kind);
35}
36
38 // Special case. Root node has itself as "parent".
39 dfs(&Root, &Root);
40}
41
42bool nixf::ParentMapAnalysis::isRoot(const Node *Up, const Node &N) {
43 return Up == &N;
44}
45
47 return isRoot(query(N), N);
48}
static bool isExpr(NodeKind Kind)
Definition Basic.h:79
NodeKind kind() const
Definition Basic.h:34
const Node * upExpr(const Node &N) const
Search up until the node becomes a concrete expression. a ^<--— ID -> ExprVar.
static bool isRoot(const Node *Up, const Node &N)
const Node * upTo(const Node &N, Node::NodeKind Kind) const
Search up until some kind of node is found.
const Node * query(const Node &N) const
ParentMap analysis.