14 #ifndef OPENVDB_AX_AST_VISITOR_HAS_BEEN_INCLUDED 15 #define OPENVDB_AX_AST_VISITOR_HAS_BEEN_INCLUDED 20 #include <openvdb/version.h> 22 #include <type_traits> 94 template <
typename Derived,
bool ConstVisit=true>
99 template <
typename NodeT>
100 using NodeType =
typename std::conditional<ConstVisit, const NodeT, NodeT>::type;
105 return *
static_cast<Derived*
>(
this);
162 return this->defaultTraversal<ast::Tree>(tree);
166 return this->defaultTraversal<ast::StatementList>(cond);
170 return this->defaultTraversal<ast::Block>(block);
174 return this->defaultTraversal<ast::CommaOperator>(comma);
178 return this->defaultTraversal<ast::Loop>(loop);
182 return this->defaultTraversal<ast::Keyword>(keyw);
186 return this->defaultTraversal<ast::ConditionalStatement>(cond);
190 return this->defaultTraversal<ast::AssignExpression>(asgn);
194 return this->defaultTraversal<ast::Crement>(crmt);
198 return this->defaultTraversal<ast::UnaryOperator>(unry);
202 return this->defaultTraversal<ast::BinaryOperator>(bin);
206 return this->defaultTraversal<ast::TernaryOperator>(tern);
210 return this->defaultTraversal<ast::Cast>(cast);
214 return this->defaultTraversal<ast::FunctionCall>(call);
218 return this->defaultTraversal<ast::Attribute>(attr);
222 return this->defaultTraversal<ast::ExternalVariable>(ext);
226 return this->defaultTraversal<ast::DeclareLocal>(decl);
230 return this->defaultTraversal<ast::Local>(loc);
234 return this->defaultTraversal<ast::ArrayPack>(pack);
238 return this->defaultTraversal<ast::ArrayUnpack>(pack);
242 return this->defaultTraversal<ast::Value<bool>>(val);
246 return this->defaultTraversal<ast::Value<int16_t>>(val);
250 return this->defaultTraversal<ast::Value<int32_t>>(val);
254 return this->defaultTraversal<ast::Value<int64_t>>(val);
258 return this->defaultTraversal<ast::Value<float>>(val);
262 return this->defaultTraversal<ast::Value<double>>(val);
266 return this->defaultTraversal<ast::Value<std::string>>(val);
274 if (!node)
return true;
275 switch (node->nodetype()) {
276 case Node::TreeNode :
return this->derived().traverse(
static_cast<NodeType<ast::Tree>*
>(node));
278 case Node::BlockNode :
return this->derived().traverse(
static_cast<NodeType<ast::Block>*
>(node));
280 case Node::LoopNode :
return this->derived().traverse(
static_cast<NodeType<ast::Loop>*
>(node));
288 case Node::CastNode :
return this->derived().traverse(
static_cast<NodeType<ast::Cast>*
>(node));
295 case Node::LocalNode :
return this->derived().traverse(
static_cast<NodeType<ast::Local>*
>(node));
303 default :
return true;
359 template <
bool V,
typename NodeT>
360 inline typename std::enable_if<V, const NodeT*>::type
361 strip(
const NodeT* node) {
369 template <
bool V,
typename NodeT>
370 inline typename std::enable_if<!V, typename std::remove_const<NodeT>::type*>::type
371 strip(
const NodeT* node) {
372 return const_cast<NodeT*
>(node);
379 template <
typename NodeT>
380 bool hierarchyVisits(NodeT& node)
382 if (this->derived().reverseHierarchyVisits()) {
383 if (
auto base = node.NodeT::basetype()) {
384 if (!hierarchyVisits(*base))
return false;
386 if (!this->derived().visit(this->strip<ConstVisit>(&node)))
return false;
389 if (!this->derived().visit(this->strip<ConstVisit>(&node)))
return false;
390 if (
auto base = node.NodeT::basetype()) {
391 return hierarchyVisits(*base);
402 template <
typename NodeT>
405 if (!node)
return true;
406 const size_t children = node->children();
408 if (this->derived().postOrderNodes()) {
409 if (this->derived().reverseChildVisits()) {
411 for (int64_t i = static_cast<int64_t>(children - 1); i >= 0; --i) {
412 auto child = this->strip<ConstVisit>(node->child(i));
413 if (!this->derived().traverse(
child)) {
420 for (
size_t i = 0; i < children; ++i) {
421 auto child = this->strip<ConstVisit>(node->child(i));
422 if (!this->derived().traverse(
child)) {
427 if (this->derived().visitNodeHierarchies()) {
428 return this->hierarchyVisits(*node);
431 return this->derived().visit(node);
435 if (this->derived().visitNodeHierarchies()) {
436 if (!this->hierarchyVisits(*node))
return false;
439 if (!this->derived().visit(node))
return false;
441 if (this->derived().reverseChildVisits()) {
443 for (int64_t i = static_cast<int64_t>(children - 1); i >= 0; --i) {
444 auto child = this->strip<ConstVisit>(node->child(i));
445 if (!this->derived().traverse(
child)) {
452 for (
size_t i = 0; i < children; ++i) {
453 auto child = this->strip<ConstVisit>(node->child(i));
454 if (!this->derived().traverse(
child)) {
470 #endif // OPENVDB_AX_AST_VISITOR_HAS_BEEN_INCLUDED bool traverse(NodeType< ast::Block > *block)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:169
bool visit(NodeType< ast::Value< bool >> *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:344
ChildT * child
Definition: GridBuilder.h:1286
Specialization of Values for strings.
Definition: AST.h:2334
bool traverse(NodeType< ast::Value< int32_t >> *val)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:249
Provides the definition for every abstract and concrete derived class which represent a particular ab...
bool traverse(NodeType< ast::TernaryOperator > *tern)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:205
bool visit(NodeType< ast::Value< int64_t >> *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:347
bool visit(NodeType< ast::Tree > *)
Visits for concrete Node types.
Definition: Visitor.h:324
bool visit(NodeType< ast::Value< double >> *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:349
bool visit(NodeType< ast::StatementList > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:325
typename std::conditional< true, const NodeT, NodeT >::type NodeType
Templated conditional which resolves to a const NodeT if ConstVisit is true, or a non-const NodeT if ...
Definition: Visitor.h:100
bool traverse(NodeType< ast::Keyword > *keyw)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:181
bool traverse(NodeType< ast::ExternalVariable > *ext)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:221
bool traverse(NodeType< ast::ArrayPack > *pack)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:233
bool visit(NodeType< ast::ArrayUnpack > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:343
bool traverse(NodeType< ast::DeclareLocal > *decl)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:225
bool visit(NodeType< ast::Variable > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:319
bool traverse(NodeType< ast::CommaOperator > *comma)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:173
A Value (literal) AST node holds either literal text or absolute value information on all numerical...
Definition: AST.h:2252
bool visit(NodeType< ast::Expression > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:318
bool visit(NodeType< ast::Attribute > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:338
bool visit(NodeType< ast::Statement > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:317
bool visit(NodeType< ast::BinaryOperator > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:334
bool visit(NodeType< ast::Value< int16_t >> *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:345
bool visit(NodeType< ast::TernaryOperator > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:335
bool traverse(NodeType< ast::BinaryOperator > *bin)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:201
The Visitor class uses the Curiously Recursive Template Pattern (CRTP) to provide a customizable inte...
Definition: Visitor.h:95
bool visitNodeHierarchies() const
Default behavior option. Controls whether nodes visit themselves at each stage of their class hierarc...
Definition: Visitor.h:141
bool traverse(NodeType< ast::Node > *node)
The default traversal method which is hit for all child traversals. The correct derived traversal sch...
Definition: Visitor.h:273
bool traverse(NodeType< ast::Attribute > *attr)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:217
bool visit(NodeType< ast::ValueBase > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:320
bool visit(NodeType< ast::Loop > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:328
bool visit(NodeType< ast::AssignExpression > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:331
bool visit(NodeType< ast::Value< int32_t >> *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:346
bool traverse(NodeType< ast::ConditionalStatement > *cond)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:185
bool traverse(NodeType< ast::AssignExpression > *asgn)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:189
Derived & derived()
Accesses the derived class by static casting the current object. Assumes use of the Curiously Recursi...
Definition: Visitor.h:104
bool reverseChildVisits() const
Default behavior option. Reverses the traversal order of child nodes. If true, child nodes are access...
Definition: Visitor.h:128
Definition: Exceptions.h:13
bool visit(NodeType< ast::CommaOperator > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:327
bool reverseHierarchyVisits() const
Default behavior option. Reverses the traversal order of node hierarchies. If true, hierarchical visits start at the very top of their inheritance structure (always a Node AST node) and visit downwards until the lowest derived concrete node is reached. If false, hierarchical visits start at the lowest derived concrete node and visit upwards until the very top of their inheritance structure (always a Node AST node) is reached.
Definition: Visitor.h:151
bool traverse(NodeType< ast::Value< int64_t >> *val)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:253
bool traverse(NodeType< ast::Loop > *loop)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:177
bool traverse(NodeType< ast::Value< bool >> *val)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:241
bool visit(NodeType< ast::Keyword > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:329
bool visit(NodeType< ast::ExternalVariable > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:339
bool visit(NodeType< ast::Block > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:326
bool traverse(NodeType< ast::StatementList > *cond)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:165
bool visit(NodeType< ast::Value< std::string >> *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:350
bool traverse(NodeType< ast::FunctionCall > *call)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:213
bool traverse(NodeType< ast::Value< double >> *val)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:261
bool traverse(NodeType< ast::Cast > *cast)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:209
bool visit(NodeType< ast::DeclareLocal > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:340
bool visit(NodeType< ast::Cast > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:336
bool traverse(NodeType< ast::Value< int16_t >> *val)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:245
bool visit(NodeType< ast::Local > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:341
bool traverse(NodeType< ast::Local > *loc)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:229
Various function and operator tokens used throughout the AST and code generation. ...
bool traverse(NodeType< ast::Value< std::string >> *val)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:265
bool visit(NodeType< ast::Crement > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:332
bool traverse(NodeType< ast::ArrayUnpack > *pack)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:237
bool visit(NodeType< ast::UnaryOperator > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:333
bool visit(NodeType< ast::Node > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:316
bool traverse(NodeType< ast::UnaryOperator > *unry)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:197
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
bool visit(NodeType< ast::FunctionCall > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:337
bool postOrderNodes() const
Default behavior option. If true, this results in post-order traversal, where node children are trave...
Definition: Visitor.h:122
bool visit(NodeType< ast::Value< float >> *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:348
bool traverse(NodeType< ast::Tree > *tree)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:161
bool traverse(NodeType< ast::Value< float >> *val)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:257
bool visit(NodeType< ast::ConditionalStatement > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:330
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202
bool visit(NodeType< ast::ArrayPack > *)
Visits for abstract (pure-virtual) Node types.
Definition: Visitor.h:342
bool traverse(NodeType< ast::Crement > *crmt)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:193