Loading...
Searching...
No Matches
by::visitor Class Reference

Base visitor class for AST traversal. More...

#include <visitor.hpp>

Inheritance diagram for by::visitor:
by::tworker< void, node > by::expander by::generalizer by::graphVisitor by::verifier

Public Member Functions

 visitor (nbool isReturnable)
 
void setReturnable (nbool isReturnable)
 
nbool isReturnable () const
 
virtual void visit (const visitInfo &i, node &me)
 
virtual nbool onVisit (const visitInfo &i, node &me, nbool alreadyVisited)
 
virtual void onLeave (const visitInfo &i, node &me, nbool alreadyVisited)
 
virtual void onTraverse (const visitInfo &i, node &me)
 
virtual void onTraverse (const visitInfo &i, asExpr &a)
 
virtual void onTraverse (const visitInfo &i, assignExpr &a)
 
virtual void onTraverse (const visitInfo &i, blockExpr &b)
 
virtual void onTraverse (const visitInfo &i, defVarExpr &d)
 
virtual void onTraverse (const visitInfo &i, FBOExpr &f)
 
virtual void onTraverse (const visitInfo &i, getExpr &e)
 
virtual void onTraverse (const visitInfo &i, retExpr &b)
 
virtual void onTraverse (const visitInfo &i, evalExpr &e)
 
virtual void onTraverse (evalExpr &e, node &subject)
 
virtual void onTraverse (const visitInfo &i, func &f)
 
virtual void onTraverse (const visitInfo &i, frame &f)
 
virtual void onTraverse (const visitInfo &i, forExpr &f)
 
virtual void onTraverse (const visitInfo &i, retStateExpr &r)
 
virtual void onTraverse (const visitInfo &i, ifExpr &f)
 
virtual void onTraverse (const visitInfo &i, whileExpr &w)
 
virtual void onTraverse (const visitInfo &i, defArrayExpr &d)
 
virtual void onTraverse (const visitInfo &i, defNestedFuncExpr &e)
 
virtual void onTraverse (const visitInfo &i, genericOrigin &g)
 
virtual void onTraverse (const visitInfo &i, obj &o)
 
- Public Member Functions inherited from by::tworker< void, node >
 tworker (const errReport &rpt)
 
errReportgetReport ()
 
const errReportgetReport () const BY_CONST_FUNC(getReport()) me &setReport(errReport &rpt)
 
me & setFlag (nint newFlag)
 
me & addFlag (nint flag)
 
me & delFlag (nint clear)
 
nbool isFlag (nint flag) const
 
nint getFlag () const
 
me & setTask (const node &root)
 
me & setTask (const node *it) BY_SIDE_FUNC(it
 
nodegetTask ()
 
const nodegetTask () const BY_CONST_FUNC(getTask()) virtual void rel()
 
void work ()
 
const areagetArea () const BY_CONST_FUNC(_getArea()) nbool isOk() const
 

Protected Member Functions

void _onWork () override
 
void _prepare () override
 Protected virtual method for preparation before starting the work.
 
- Protected Member Functions inherited from by::tworker< void, node >
void _report (baseErr *e)
 
virtual void _onEndWork ()
 Protected virtual method called after the work is completed.
 
area_getArea ()
 
void _onEndErrReport (const errReport &rpt) const
 

Additional Inherited Members

- Public Types inherited from by::tworker< void, node >
enum  logFlag
 
- Public Attributes inherited from by::tworker< void, node >
me setTask it
 
me setTask * this
 

Detailed Description

Base visitor class for AST traversal.

Since byeol focuses on AST, visitor is used frequently. visitor is actively utilized to separate the traversal method from the actions taken when visiting node during traversal.

Traversal

Always follows preorder traversal. Changing to postorder traversal is not possible. visit() consists of 3 stages:

  1. Visit the currently found node (onVisit())
  2. Traverse next child nodes (onTraverse())
  3. Leave the currently found node (onLeave())

Downcasting Through accept

visitor has many virtual functions that represent visits to many concrete types like onVisit(T&). On the other hand, when searching in onTraverse, the node type is mainly used because it uses the tbicontainable interface through node's subs(). So somewhere the node type must be downcast to concrete types like nInt or defNestedFuncExpr. For this purpose, node's virtual function accept() is called. See the example:

void defNestedFuncExpr::accept(const visitInfo& i, visitor& v) {
v.visit(i, *this); // calls visitor::visit(const visitInfo&, defNestedFuncExpr&)
}
Definition tnarr.hpp:9
Visitor context information.
Definition visitInfo.hpp:11
Base visitor class for AST traversal.
Definition visitor.hpp:61

When the virtual function accept() is called, it reversely calls visitor's visit() as a concrete type through *this. For this, all node-derived classes participating in visitation must override the virtual function accept(), and the VISIT macro is used to make this process easier. You will often see declarations like this:

class _nout pod: public node {
BY(CLASS(pod, node), VISIT()) // <---
Base class for all AST nodes in the byeol language.
Definition node.hpp:195
Pod.
Definition pod.hpp:21

If a node-derived class does not override accept(), onTraverse(node&) is used instead, which is sufficient for those cases.

Duplicate Visit Elimination

AST sometimes has mutual references between nodes. In this case, traversing without any exception handling revisits already visited nodes and enters an infinite loop. visitor owns a map called _visited. Through this, when visit() is called, it determines if it's an already visited node and provides exception handling. This visit history information is reset right before visitor starts visiting each time. If you want to enable revisiting, change the value with setReturnable(true).

Member Function Documentation

◆ _onWork()

void by::visitor::_onWork ( )
overrideprotectedvirtual

◆ _prepare()

void by::visitor::_prepare ( )
overrideprotectedvirtual

Protected virtual method for preparation before starting the work.

This hook allows derived classes to perform any necessary setup or initialization before _onWork() is invoked.

Reimplemented from by::tworker< void, node >.

◆ setReturnable()

void by::visitor::setReturnable ( nbool isReturnable)

if you set the visitor as returnable, nodes you have already been visited, will be visited again if it's refered by different nodes. in default, this value is false.


The documentation for this class was generated from the following file:
to Top