nixd
Loading...
Searching...
No Matches
libnixt/lib/ParentMap.cpp
Go to the documentation of this file.
1#include "nixt/ParentMap.h"
2
3namespace nixt {
4
5ParentMap parentMap(const nix::Expr *Root) {
6 ParentMap Ret;
7 struct VisitorClass : RecursiveASTVisitor<VisitorClass> {
8 // The parent before traverseExpr
9 const nix::Expr *ParentExpr;
10 ParentMap *CapturedRet;
11
12 bool traverseExpr(const nix::Expr *E) {
13 CapturedRet->insert({E, ParentExpr});
14 const auto *OldParent = ParentExpr;
15 ParentExpr = E; // Set the parent into the visitor, it should be the
16 // parent when we are traversing child nodes.
18 return false;
19
20 // After traversing on childrens finished, set parent expr to previous
21 // parent.
22 ParentExpr = OldParent;
23 return true;
24 }
25
26 } Visitor;
27
28 Visitor.ParentExpr = Root;
29 Visitor.CapturedRet = &Ret;
30
31 Visitor.traverseExpr(Root);
32
33 return Ret;
34}
35
36} // namespace nixt
Construct child -> parent relations of nix::Expr nodes.
Access EvalCache in nix::EvalState.
Definition ArrayRef.h:7
std::map< const nix::Expr *, const nix::Expr * > ParentMap
The parent map. The key is "child", the value is "parent".
ParentMap parentMap(const nix::Expr *Root)
Construct child -> parent relations of nix::Expr nodes.
A CRTP base class for traversing nix::Expr * nodes.
Definition Visitor.h:48
bool traverseExpr(const nix::Expr *E)
Definition Visitor.h:65