OpenVDB  7.2.1
Compiler.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
14 
15 #ifndef OPENVDB_AX_COMPILER_HAS_BEEN_INCLUDED
16 #define OPENVDB_AX_COMPILER_HAS_BEEN_INCLUDED
17 
18 #include "CompilerOptions.h"
19 #include "CustomData.h"
20 #include "Logger.h"
21 
22 #include "../ax.h" // backward compat support for initialize()
23 #include "../ast/Parse.h"
24 
25 #include <openvdb/version.h>
26 
27 #include <memory>
28 #include <sstream>
29 
30 // forward
31 namespace llvm {
32 class LLVMContext;
33 }
34 
35 namespace openvdb {
37 namespace OPENVDB_VERSION_NAME {
38 
39 namespace ax {
40 
41 namespace codegen {
42 // forward
43 class FunctionRegistry;
44 }
45 
49 class Compiler
50 {
51 public:
52 
53  using Ptr = std::shared_ptr<Compiler>;
54  using UniquePtr = std::unique_ptr<Compiler>;
55 
59 
60  ~Compiler() = default;
61 
63  static UniquePtr create(const CompilerOptions& options = CompilerOptions());
64 
77  template <typename ExecutableT>
78  typename ExecutableT::Ptr
79  compile(const ast::Tree& syntaxTree,
80  Logger& logger,
81  const CustomData::Ptr data = CustomData::Ptr());
82 
93  template <typename ExecutableT>
94  typename ExecutableT::Ptr
95  compile(const std::string& code,
96  Logger& logger,
97  const CustomData::Ptr data = CustomData::Ptr())
98  {
99  logger.clear();
100  const ast::Tree::ConstPtr syntaxTree = ast::parse(code.c_str(), logger);
101  if (syntaxTree) return compile<ExecutableT>(*syntaxTree, logger, data);
102  else return nullptr;
103  }
104 
113  template <typename ExecutableT>
114  typename ExecutableT::Ptr
115  compile(const std::string& code,
116  const CustomData::Ptr data = CustomData::Ptr())
117  {
118  std::vector<std::string> errors;
119  openvdb::ax::Logger logger(
120  [&errors] (const std::string& error) {
121  errors.emplace_back(error + "\n");
122  },
123  // ignore warnings
124  [] (const std::string&) {}
125  );
126  const ast::Tree::ConstPtr syntaxTree = ast::parse(code.c_str(), logger);
127  typename ExecutableT::Ptr exe;
128  if (syntaxTree) {
129  exe = this->compile<ExecutableT>(*syntaxTree, logger, data);
130  }
131  if (!errors.empty()) {
132  std::ostringstream os;
133  for (const auto& e : errors) os << e << "\n";
134  OPENVDB_THROW(AXCompilerError, os.str());
135  }
136  assert(exe);
137  return exe;
138  }
139 
146  template <typename ExecutableT>
147  typename ExecutableT::Ptr
148  compile(const ast::Tree& syntaxTree,
149  const CustomData::Ptr data = CustomData::Ptr())
150  {
151  std::vector<std::string> errors;
152  openvdb::ax::Logger logger(
153  [&errors] (const std::string& error) {
154  errors.emplace_back(error + "\n");
155  },
156  // ignore warnings
157  [] (const std::string&) {}
158  );
159  auto exe = compile<ExecutableT>(syntaxTree, logger, data);
160  if (!errors.empty()) {
161  std::ostringstream os;
162  for (const auto& e : errors) os << e << "\n";
163  OPENVDB_THROW(AXCompilerError, os.str());
164  }
165  assert(exe);
166  return exe;
167  }
168 
175  void setFunctionRegistry(std::unique_ptr<codegen::FunctionRegistry>&& functionRegistry);
176 
178 
180  template <typename ExecutableT>
182  typename ExecutableT::Ptr
183  compile(const ast::Tree& syntaxTree,
184  const CustomData::Ptr data,
185  std::vector<std::string>* warnings) {
186  openvdb::ax::Logger logger(
187  // throw immediately on first error
188  [] (const std::string& error) {
190  },
191  // collect warnings in vector
192  [&warnings] (const std::string& warn) {
193  if (warnings) warnings->emplace_back(warn);
194  }
195  );
196  return compile<ExecutableT>(syntaxTree, logger, data);
197  }
198 
199  template <typename ExecutableT>
201  typename ExecutableT::Ptr
202  compile(const std::string& code,
203  const CustomData::Ptr data,
204  std::vector<std::string>* warnings) {
205  openvdb::ax::Logger logger(
206  // throw immediately on first error
207  [] (const std::string& error) {
209  },
210  // collect warnings in vector
211  [&warnings] (const std::string& warn) {
212  if (warnings) warnings->emplace_back(warn);
213  }
214  );
215  return compile<ExecutableT>(code, logger, data);
216  }
217 
219 
220 private:
221 
222  std::shared_ptr<llvm::LLVMContext> mContext;
223  const CompilerOptions mCompilerOptions;
224  std::shared_ptr<codegen::FunctionRegistry> mFunctionRegistry;
225 };
226 
227 
228 } // namespace ax
229 } // namespace OPENVDB_VERSION_NAME
230 } // namespace openvdb
231 
232 #endif // OPENVDB_AX_COMPILER_HAS_BEEN_INCLUDED
233 
openvdb::v7_2::ax::ast::Tree
A Tree is the highest concrete (non-abstract) node in the entire AX AST hierarchy....
Definition: AST.h:562
openvdb::v7_2::ax::Logger
Logger for collecting errors and warnings that occur during AX compilation.
Definition: Logger.h:55
llvm
Definition: Compiler.h:31
openvdb::v7_2::AXCompilerError
Definition: ax/openvdb_ax/Exceptions.h:37
openvdb::v7_2::ax::CustomData::Ptr
std::shared_ptr< CustomData > Ptr
Definition: CustomData.h:46
openvdb::v7_2::ax::Compiler::compile
ExecutableT::Ptr compile(const std::string &code, const CustomData::Ptr data=CustomData::Ptr())
Compile a given snippet of AX code into an executable object of the given type.
Definition: Compiler.h:115
CustomData.h
Access to the CustomData class which can provide custom user user data to the OpenVDB AX Compiler.
version.h
Library and file format version numbers.
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: openvdb/Exceptions.h:82
openvdb::v7_2::ax::Compiler::Compiler
Compiler(const CompilerOptions &options=CompilerOptions())
Construct a compiler object with given settings.
openvdb::v7_2::ax::Compiler::compile
OPENVDB_DEPRECATED ExecutableT::Ptr compile(const ast::Tree &syntaxTree, const CustomData::Ptr data, std::vector< std::string > *warnings)
deprecated methods
Definition: Compiler.h:183
openvdb::v7_2::ax::Compiler::compile
ExecutableT::Ptr compile(const ast::Tree &syntaxTree, Logger &logger, const CustomData::Ptr data=CustomData::Ptr())
Compile a given AST into an executable object of the given type.
OPENVDB_DEPRECATED
#define OPENVDB_DEPRECATED
Definition: Platform.h:42
openvdb::v7_2::ax::ast::Tree::ConstPtr
std::shared_ptr< const Tree > ConstPtr
Definition: AST.h:564
openvdb::v7_2::ax::Compiler::UniquePtr
std::unique_ptr< Compiler > UniquePtr
Definition: Compiler.h:54
Logger.h
Logging system to collect errors and warnings throughout the different stages of parsing and compilat...
openvdb::v7_2::ax::Compiler
The compiler class. This holds an llvm context and set of compiler options, and constructs executable...
Definition: Compiler.h:50
openvdb::v7_2::ax::Compiler::compile
ExecutableT::Ptr compile(const ast::Tree &syntaxTree, const CustomData::Ptr data=CustomData::Ptr())
Compile a given AST into an executable object of the given type.
Definition: Compiler.h:148
openvdb::v7_2::ax::Compiler::compile
OPENVDB_DEPRECATED ExecutableT::Ptr compile(const std::string &code, const CustomData::Ptr data, std::vector< std::string > *warnings)
Definition: Compiler.h:202
openvdb::v7_2::AXSyntaxError
Definition: ax/openvdb_ax/Exceptions.h:35
openvdb::v7_2::ax::CompilerOptions
Settings which control how a Compiler class object behaves.
Definition: CompilerOptions.h:48
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:147
openvdb::v7_2::ax::Logger::clear
void clear()
Clear the tree-code mapping and reset the number of errors/warnings.
CompilerOptions.h
OpenVDB AX Compiler Options.
openvdb::v7_2::ax::ast::parse
openvdb::ax::ast::Tree::Ptr parse(const char *code)
Construct an abstract syntax tree from a code snippet. A runtime exception will be thrown with the fi...
openvdb::v7_2::ax::Compiler::~Compiler
~Compiler()=default
openvdb::v7_2::ax::Compiler::Ptr
std::shared_ptr< Compiler > Ptr
Definition: Compiler.h:53
openvdb::v7_2::ax::Compiler::compile
ExecutableT::Ptr compile(const std::string &code, Logger &logger, const CustomData::Ptr data=CustomData::Ptr())
Compile a given snippet of AX code into an executable object of the given type.
Definition: Compiler.h:95
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:95
openvdb::v7_2::ax::Compiler::create
static UniquePtr create(const CompilerOptions &options=CompilerOptions())
Static method for creating Compiler objects.
openvdb::v7_2::ax::Compiler::setFunctionRegistry
void setFunctionRegistry(std::unique_ptr< codegen::FunctionRegistry > &&functionRegistry)
Sets the compiler's function registry object.
openvdb
Definition: openvdb/Exceptions.h:13