The accept function that act as a nodes accept method in the Visitor pattern.
Helper to dispatch the node if the method exist otherwise to a fallback.
Joakim Brännström (joakim.brannstrom@gmx.com)
This module contains the basic building blocks for an AST.
TODO consider if disabling postblit for visitors is a good recommendation.
## Enable different tree visit algorithms The intension of the design is to separate the algorithm for _how_ to visit the tree from the structural requirements _of_ the tree.
This is achieve by the _accept_ function in this module. It correspond with the _accept_ method in the Visitor pattern. It dispatch the call to the composite visitors implAccept. _How_ to visit the specific part of the tree is implemented in implAccept.
## Generic Structural Design It is desired to avoid boilerplate code and dynamic dispatch. This is achieved via compile time introspection in the maybeCallVisit template.
A pro side effect of this is that the runtime performance is _probably_ improved. A con side effect is that the compile time may be adversely affected.
## Potential Problem The recommended implementation of using mixins for the implAccept makes it so that when a new node type is added to a subtree all the parents to that part of the tree must update their visitors.
This is deemed OK because adding new nodes is assumed to be rare. This design problem do not affect the use override of visit functions.
# Usage This is a minimal example using the building blocks of this module.
Separate the implAccept in mixin templates to allow reuse of them in a supertree to allow visiting a full tree.
1 mixin template NodeA_Accept(VisitorT, UserT) { 2 // to avoid problems of missing imports when using this mixin 3 import my.subtree; 4 void implAccept(ref NodeA n) { 5 static void fallback(ref VisitorT!UserT self, ref UserT user, ref NodeA node) { 6 auto parent_node = cast(Node) node; 7 maybeCallVisit(self, user, parent_node); 8 } 9 foreach (child; n.children) { 10 maybeCallVisit(this, user, child, &fallback); 11 } 12 } 13 }
Make a subtree visitor for NodeA. Add a convenient visit function for the user to use if the top node isn't of interest in the user implementation. But it is important to still allow an override to be handled correctly.
1 struct NodeAVisitor(UserT) { 2 UserT user; 3 4 void visit(ref NodeA n) { 5 import llvm_hiwrap.ast.tree; 6 static void fallback(ref this self, ref UserT user, ref NodeA node) { 7 accept(n, self); 8 } 9 maybeCallVisit(this, user, n); 10 } 11 mixin NodeA_Accept!(NodeAVisitor, UserT); 12 }
Make a supertree visitor. Assume that there exist a NodeB with the same implementation as NodeA.
1 mixin template TopNode_Accept(VisitorT, UserT) { 2 import my.supertree; 3 void implAccept(ref TopNode n) { 4 static void fallback(T)(ref VisitorT!UserT self, ref UserT user, ref T node) { 5 auto parent_node = cast(Node) node; 6 maybeCallVisit(self, user, parent_node); 7 } 8 foreach (child; n.childrenA) { 9 maybeCallVisit(this, user, child, &fallback!NodeA); 10 } 11 foreach (child; n.childrenB) { 12 maybeCallVisit(this, user, child, &fallback!NodeB); 13 } 14 } 15 } 16 17 struct SuperVisitor(UserT) { 18 UserT user; 19 20 void visit(ref TopNode n) { 21 import llvm_hiwrap.ast.tree; 22 static void fallback(ref this self, ref UserT user, ref TopNode node) { 23 accept(n, self); 24 } 25 maybeCallVisit(this, user, n); 26 } 27 28 mixin NodeA_Accept!(NodeAVisitor, UserT); 29 mixin NodeB_Accept!(NodeAVisitor, UserT); 30 mixin TopNode_Accept!(NodeAVisitor, UserT); 31 }
Copyright (c) 2017, Joakim Brännström. All rights reserved.