OpenVDB: Compiler.h Source File

OpenVDB 8.0.0

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 {
36 OPENVDB_USE_VERSION_NAMESPACE
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
58 Compiler(const CompilerOptions& options = CompilerOptions());
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
179 private:
180
181 std::shared_ptr<llvm::LLVMContext> mContext;
182 const CompilerOptions mCompilerOptions;
183 std::shared_ptr<codegen::FunctionRegistry> mFunctionRegistry;
184 };
185
186
187 } // namespace ax
188 } // namespace OPENVDB_VERSION_NAME
189 } // namespace openvdb
190
191 #endif // OPENVDB_AX_COMPILER_HAS_BEEN_INCLUDED
192
openvdb::v8_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
llvm
Definition: Compiler.h:31
openvdb::v8_2::ax::CompilerOptions
Settings which control how a Compiler class object behaves.
Definition: CompilerOptions.h:48
openvdb::v8_2::ax::Compiler::~Compiler
~Compiler()=default
openvdb::v8_2::AXCompilerError
Definition: ax/openvdb_ax/Exceptions.h:37
openvdb::v8_2::ax::ast::Tree
A Tree is the highest concrete (non-abstract) node in the entire AX AST hierarchy....
Definition: AST.h:562
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:74
openvdb::v8_2::ax::Compiler::setFunctionRegistry
void setFunctionRegistry(std::unique_ptr< codegen::FunctionRegistry > &&functionRegistry)
Sets the compiler's function registry object.
openvdb::v8_2::ax::ast::Tree::ConstPtr
std::shared_ptr< const Tree > ConstPtr
Definition: AST.h:564
Logger.h
Logging system to collect errors and warnings throughout the different stages of parsing and compilat...
openvdb::v8_2::ax::Compiler
The compiler class. This holds an llvm context and set of compiler options, and constructs executable...
Definition: Compiler.h:50
openvdb::v8_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::v8_2::ax::Compiler::Ptr
std::shared_ptr< Compiler > Ptr
Definition: Compiler.h:53
openvdb::v8_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::v8_2::ax::Compiler::UniquePtr
std::unique_ptr< Compiler > UniquePtr
Definition: Compiler.h:54
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:153
CompilerOptions.h
OpenVDB AX Compiler Options.
openvdb::v8_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::v8_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_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:101
openvdb::v8_2::ax::Logger
Logger for collecting errors and warnings that occur during AX compilation.
Definition: Logger.h:55
openvdb::v8_2::ax::Compiler::create
static UniquePtr create(const CompilerOptions &options=CompilerOptions())
Static method for creating Compiler objects.
openvdb
Definition: openvdb/Exceptions.h:13
openvdb::v8_2::ax::Logger::clear
void clear()
Clear the tree-code mapping and reset the number of errors/warnings.
openvdb::v8_2::ax::CustomData::Ptr
std::shared_ptr< CustomData > Ptr
Definition: CustomData.h:46
openvdb::v8_2::ax::Compiler::Compiler
Compiler(const CompilerOptions &options=CompilerOptions())
Construct a compiler object with given settings.