OpenVDB: Scanners.h Source File

OpenVDB 8.1.0

Scanners.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
11
12 #ifndef OPENVDB_AX_COMPILER_AST_SCANNERS_HAS_BEEN_INCLUDED
13 #define OPENVDB_AX_COMPILER_AST_SCANNERS_HAS_BEEN_INCLUDED
14
15 #include "AST.h"
16 #include "Visitor.h"
17
18 #include <openvdb/version.h>
19
20 #include <string>
21
22 namespace openvdb {
23 OPENVDB_USE_VERSION_NAMESPACE
24 namespace OPENVDB_VERSION_NAME {
25
26 namespace ax {
27 namespace ast {
28
37 bool usesAttribute(const ast::Node& node,
38 const std::string& name,
39 const tokens::CoreType type = tokens::UNKNOWN);
40
49 bool writesToAttribute(const ast::Node& node,
50 const std::string& name,
51 const tokens::CoreType type = tokens::UNKNOWN);
52
58 bool callsFunction(const ast::Node& node, const std::string& name);
59
61 void catalogueVariables(const ast::Node& node,
62 std::vector<const ast::Variable*>* readOnly,
63 std::vector<const ast::Variable*>* writeOnly,
64 std::vector<const ast::Variable*>* readWrite,
65 const bool locals = true,
66 const bool attributes = true);
67
81 void catalogueAttributeTokens(const ast::Node& node,
82 std::vector<std::string>* readOnly,
83 std::vector<std::string>* writeOnly,
84 std::vector<std::string>* readWrite);
85
87 void attributeDependencyTokens(const ast::Tree& tree,
88 const std::string& name,
89 const tokens::CoreType type,
90 std::vector<std::string>& dependencies);
91
99 template <typename NodeT, typename OpT>
100 inline void visitNodeType(const ast::Node& node, const OpT& op);
101
104 template<typename NodeT, typename ContainerType = std::vector<const NodeT*>>
105 inline void collectNodeType(const ast::Node& node, ContainerType& array);
106
117 template <typename NodeTypeList, typename ContainerType = std::vector<const Node*>>
118 inline void collectNodeTypes(const ast::Node& node, ContainerType& array);
119
122 void linearize(const ast::Node& node, std::vector<const ast::Node*>& list);
123
124 const ast::Variable* firstUse(const ast::Node& node, const std::string& token);
125 const ast::Variable* lastUse(const ast::Node& node, const std::string& token);
126
127
130
131
132 namespace internal {
133 template<typename ContainerType, typename T, typename ...Ts>
134 struct CollectForEach {
135 static void exec(const ast::Node&, ContainerType&) {}
136 };
137
138 template<typename ContainerType, typename T, typename ...Ts>
139 struct CollectForEach<ContainerType, TypeList<T, Ts...>> {
140 static void exec(const ast::Node& node, ContainerType& C) {
141 collectNodeType<T, ContainerType>(node, C);
142 CollectForEach<ContainerType, TypeList<Ts...>>::exec(node, C);
143 }
144 };
145 }
146
147 template<typename NodeT, typename ContainerType>
148 inline void collectNodeType(const ast::Node& node, ContainerType& array)
149 {
150 visitNodeType<NodeT>(node, [&](const NodeT& node) -> bool {
151 array.push_back(&node);
152 return true;
153 });
154 }
155
156 template <typename NodeTypeList, typename ContainerType>
157 inline void collectNodeTypes(const ast::Node& node, ContainerType& array)
158 {
159 internal::CollectForEach<ContainerType, NodeTypeList>::exec(node, array);
160 }
161
162 template <typename NodeT, typename OpT, typename Derived = void>
163 struct VisitNodeType :
164 public ast::Visitor<typename std::conditional<
165 std::is_same<Derived, void>::value,
166 VisitNodeType<NodeT, OpT>,
167 Derived>::type>
168 {
169 using VisitorT = typename std::conditional<
170 std::is_same<Derived, void>::value,
171 VisitNodeType<NodeT, OpT>,
172 Derived>::type;
173
174 using ast::Visitor<VisitorT>::traverse;
175 using ast::Visitor<VisitorT>::visit;
176
177 inline bool visitNodeHierarchies() const {
178 return std::is_abstract<NodeT>::value;
179 }
180
181 VisitNodeType(const OpT& op) : mOp(op) {}
182 ~VisitNodeType() = default;
183 inline bool visit(const NodeT* node) {
184 if (node) return mOp(*node);
185 return true;
186 }
187 private:
188 const OpT& mOp;
189 };
190
191 template <typename NodeT, typename OpT>
192 inline void visitNodeType(const ast::Node& node, const OpT& op)
193 {
194 VisitNodeType<NodeT, OpT> visitOp(op);
195 visitOp.traverse(&node);
196 }
197
198 } // namespace ast
199 } // namespace ax
200 } // namespace OPENVDB_VERSION_NAME
201 } // namespace openvdb
202
203 #endif // OPENVDB_AX_COMPILER_AST_SCANNERS_HAS_BEEN_INCLUDED
204
205
AST.h
Provides the definition for every abstract and concrete derived class which represent a particular ab...
Visitor.h
Contains the AX AST Node Visitor, providing default and customizable traversal and visitation methods...
openvdb::v8_1::ax::ast::tokens::CoreType
CoreType
Definition: Tokens.h:32
openvdb::v8_1::ax::ast::tokens::UNKNOWN
@ UNKNOWN
Definition: Tokens.h:63
openvdb::v8_1::ax::ast::usesAttribute
bool usesAttribute(const ast::Node &node, const std::string &name, const tokens::CoreType type=tokens::UNKNOWN)
Returns whether or not a given branch of an AST reads from or writes to a given attribute.
openvdb::v8_1::ax::ast::callsFunction
bool callsFunction(const ast::Node &node, const std::string &name)
Returns whether or not a given branch of an AST calls a function.
openvdb::v8_1::ax::ast::linearize
void linearize(const ast::Node &node, std::vector< const ast::Node * > &list)
Flatten the provided AST branch into a linear list using post order traversal.
openvdb::v8_1::ax::ast::catalogueVariables
void catalogueVariables(const ast::Node &node, std::vector< const ast::Variable * > *readOnly, std::vector< const ast::Variable * > *writeOnly, std::vector< const ast::Variable * > *readWrite, const bool locals=true, const bool attributes=true)
todo
openvdb::v8_1::ax::ast::collectNodeType
void collectNodeType(const ast::Node &node, ContainerType &array)
Visit all nodes of a given type and store pointers to them in a provided compatible container.
Definition: Scanners.h:148
openvdb::v8_1::ax::ast::collectNodeTypes
void collectNodeTypes(const ast::Node &node, ContainerType &array)
Visit all nodes of the given types and store pointers to them in a container of base ast::Node pointe...
Definition: Scanners.h:157
openvdb::v8_1::ax::ast::attributeDependencyTokens
void attributeDependencyTokens(const ast::Tree &tree, const std::string &name, const tokens::CoreType type, std::vector< std::string > &dependencies)
Populate a list of attribute names which the given attribute depends on.
openvdb::v8_1::ax::ast::lastUse
const ast::Variable * lastUse(const ast::Node &node, const std::string &token)
openvdb::v8_1::ax::ast::visitNodeType
void visitNodeType(const ast::Node &node, const OpT &op)
For an AST node of a given type, search for and call a custom const operator() which takes a const re...
Definition: Scanners.h:192
openvdb::v8_1::ax::ast::firstUse
const ast::Variable * firstUse(const ast::Node &node, const std::string &token)
openvdb::v8_1::ax::ast::catalogueAttributeTokens
void catalogueAttributeTokens(const ast::Node &node, std::vector< std::string > *readOnly, std::vector< std::string > *writeOnly, std::vector< std::string > *readWrite)
Parse all attributes into three unique vectors which represent how they are accessed within the synta...
openvdb::v8_1::ax::ast::writesToAttribute
bool writesToAttribute(const ast::Node &node, const std::string &name, const tokens::CoreType type=tokens::UNKNOWN)
Returns whether or not a given branch of an AST writes to a given attribute.
openvdb
Definition: openvdb/Exceptions.h:13
openvdb::v8_1::TypeList
A list of types (not necessarily unique)
Definition: TypeList.h:366
openvdb::v8_1::ax::ast::Node
The base abstract node which determines the interface and required methods for all derived concrete n...
Definition: AST.h:102
openvdb::v8_1::ax::ast::Tree
A Tree is the highest concrete (non-abstract) node in the entire AX AST hierarchy....
Definition: AST.h:562
openvdb::v8_1::ax::ast::Variable
Variables are a base type for Locals, Attributes and ExternalVariables. Unlike other abstract types,...
Definition: AST.h:337
openvdb::v8_1::ax::ast::VisitNodeType
Definition: Scanners.h:168
openvdb::v8_1::ax::ast::VisitNodeType::~VisitNodeType
~VisitNodeType()=default
openvdb::v8_1::ax::ast::VisitNodeType::visit
bool visit(const NodeT *node)
Definition: Scanners.h:183
openvdb::v8_1::ax::ast::VisitNodeType::visitNodeHierarchies
bool visitNodeHierarchies() const
Definition: Scanners.h:177
openvdb::v8_1::ax::ast::VisitNodeType::VisitorT
typename std::conditional< std::is_same< Derived, void >::value, VisitNodeType< NodeT, OpT >, Derived >::type VisitorT
Definition: Scanners.h:172
openvdb::v8_1::ax::ast::VisitNodeType::VisitNodeType
VisitNodeType(const OpT &op)
Definition: Scanners.h:181
openvdb::v8_1::ax::ast::Visitor
The Visitor class uses the Curiously Recursive Template Pattern (CRTP) to provide a customizable inte...
Definition: Visitor.h:96
openvdb::v8_1::ax::ast::Visitor::traverse
bool traverse(NodeType< ast::Tree > *tree)
Default traversals for a given concrete AST node type.
Definition: Visitor.h:161
openvdb::v8_1::ax::ast::internal::CollectForEach< ContainerType, TypeList< T, Ts... > >::exec
static void exec(const ast::Node &node, ContainerType &C)
Definition: Scanners.h:140
openvdb::v8_1::ax::ast::internal::CollectForEach
Definition: Scanners.h:134
openvdb::v8_1::ax::ast::internal::CollectForEach::exec
static void exec(const ast::Node &, ContainerType &)
Definition: Scanners.h:135
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:178