X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/17860defa84c6d8bc0e8bc088a7e09361f17db07..c6ad2948bb98d42f8e0883ef82cd14cd2d5eda60:/src/visit.h diff --git a/src/visit.h b/src/visit.h index 3fe2cb2..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; @@ -141,7 +145,7 @@ public: ctx = x.ctx; - VISIT(x.namespaces) + VISIT(x.ctx->namespaces) VISIT(x.ctx->variables) VISIT(x.ctx->structs) VISIT(x.ctx->functions) @@ -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) @@ -188,7 +192,7 @@ public: ctx = x.ctx; - VISIT(x.namespaces) + VISIT(x.ctx->namespaces) VISIT(x.ctx->variables) VISIT(x.ctx->structs) VISIT(x.ctx->functions)