X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/9610ce88f0b753db81014a053bd3c6d79471c90c..HEAD:/src/visit.h diff --git a/src/visit.h b/src/visit.h index 1175a86..279a4fc 100644 --- a/src/visit.h +++ b/src/visit.h @@ -4,22 +4,26 @@ #include +// struct with callback functions for all relevant types +// tree can be walked selectively by providing only +// needed callbacks struct Visitor { - std::function ctx)> onType = [](auto, auto){}; - std::function ctx)> onExpr = [](auto, auto){}; - std::function ctx)> onStmt = [](auto, auto){}; - std::function ctx)> onBody = [](auto, auto){}; - std::function ctx)> onFunction = [](auto, auto){}; - std::function ctx)> onVariable = [](auto, auto){}; + std::function ctx)> onType = [](auto, auto){}; + std::function ctx)> onExpr = [](auto, auto){}; + std::function ctx)> onStmt = [](auto, auto){}; + std::function ctx)> onBody = [](auto, auto){}; + std::function ctx)> onFunction = [](auto, auto){}; + std::function ctx)> onVariable = [](auto, auto){}; std::function &, const std::shared_ptr ctx)> onStructMethod = [](auto, auto){}; std::function &, const std::shared_ptr ctx)> onStructMember = [](auto, auto){}; - std::function ctx)> onStruct = [](auto, auto){}; - std::function ctx)> onNamespace = [](auto, auto){}; - std::function ctx)> onProgram = [](auto, auto){}; + std::function ctx)> onStruct = [](auto, auto){}; + std::function ctx)> onNamespace = [](auto, auto){}; + std::function ctx)> onProgram = [](auto, auto){}; }; #define VISIT(XS) for (auto x : XS) visit(x); +// simply walk IR by recursively calling functions for all children struct Visit { private: Visitor v; @@ -158,9 +162,9 @@ public: v.onFunction(x, ctx); if (x.defined) { - visit(x.body); for (auto v : x.parameters) visit(v.type); + visit(x.body); } } void visit(const StructMember & x)