12#ifndef OPENVDB_AX_CODEGEN_SYMBOL_TABLE_HAS_BEEN_INCLUDED
13#define OPENVDB_AX_CODEGEN_SYMBOL_TABLE_HAS_BEEN_INCLUDED
15#include <openvdb/version.h>
18#include <llvm/IR/Value.h>
22#include <unordered_map>
38 using MapType = std::unordered_map<std::string, llvm::Value*>;
47 inline llvm::Value*
get(
const std::string& name)
const
49 const auto iter = mMap.find(name);
50 if (iter == mMap.end())
return nullptr;
58 inline bool exists(
const std::string& name)
const
60 const auto iter = mMap.find(name);
61 return (iter != mMap.end());
70 inline bool insert(
const std::string& name, llvm::Value* value)
72 if (
exists(name))
return false;
83 inline bool replace(
const std::string& name, llvm::Value* value)
85 const bool existed =
exists(name);
92 inline void clear() { mMap.clear(); }
115 using MapType = std::map<size_t, SymbolTable>;
131 inline bool erase(
const size_t index)
134 throw std::runtime_error(
"Attempted to erase global variables which is disallowed.");
137 const bool existed = (mTables.find(index) != mTables.end());
138 mTables.erase(index);
148 return &(mTables[index]);
157 auto iter = mTables.find(index);
158 if (iter == mTables.end())
return nullptr;
159 return &(iter->second);
170 inline llvm::Value*
find(
const std::string& name,
const size_t startIndex)
const
176 auto it = mTables.lower_bound(startIndex);
177 if (it == mTables.end() || it->first != startIndex) --it;
183 MapType::const_reverse_iterator iter(++it);
185 for (; iter != mTables.crend(); ++iter) {
186 llvm::Value* value = iter->second.get(name);
187 if (value)
return value;
198 inline llvm::Value*
find(
const std::string& name)
const
200 return this->
find(name, mTables.crbegin()->first);
209 inline bool replace(
const std::string& name, llvm::Value* value)
211 for (
auto it = mTables.rbegin(); it != mTables.rend(); ++it) {
212 if (it->second.get(name)) {
213 it->second.replace(name, value);
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
Definition Exceptions.h:13
SymbolTable * get(const size_t index)
Get a SymbolTable with a unique index. If it doesn't exist, nullptr is returned.
Definition SymbolTable.h:155
SymbolTable & globals()
Access to the list of global variables which are always accessible.
Definition SymbolTable.h:122
SymbolTableBlocks()
Definition SymbolTable.h:117
~SymbolTableBlocks()=default
std::map< size_t, SymbolTable > MapType
Definition SymbolTable.h:115
llvm::Value * find(const std::string &name) const
Find a variable within the program starting at the lowest level SymbolTable.
Definition SymbolTable.h:198
bool erase(const size_t index)
Erase a given scoped indexed SymbolTable from the list of held SymbolTables. Returns true if the tabl...
Definition SymbolTable.h:131
llvm::Value * find(const std::string &name, const size_t startIndex) const
Find a variable within the program starting at a given table index. If the given index does not exist...
Definition SymbolTable.h:170
SymbolTable * getOrInsert(const size_t index)
Get or insert and get a SymbolTable with a unique index.
Definition SymbolTable.h:146
const SymbolTable & globals() const
Definition SymbolTable.h:123
bool replace(const std::string &name, llvm::Value *value)
Replace the first occurrance of a variable with a given name with a replacement value....
Definition SymbolTable.h:209
A symbol table which can be used to represent a single scoped set of a programs variables....
Definition SymbolTable.h:37
bool insert(const std::string &name, llvm::Value *value)
Insert a variable to this symbol table if it does not exist. Returns true if successfully,...
Definition SymbolTable.h:70
const MapType & map() const
Access to the underlying map.
Definition SymbolTable.h:96
bool exists(const std::string &name) const
Returns true if a variable exists in this symbol table with the given name.
Definition SymbolTable.h:58
std::unordered_map< std::string, llvm::Value * > MapType
Definition SymbolTable.h:38
SymbolTable()
Definition SymbolTable.h:40
void clear()
Clear all symbols in this table.
Definition SymbolTable.h:92
llvm::Value * get(const std::string &name) const
Get a llvm::Value from this symbol table with the given name mapping. It it does not exist,...
Definition SymbolTable.h:47
bool replace(const std::string &name, llvm::Value *value)
Replace a variable in this symbol table. Returns true if the variable previously existed and false if...
Definition SymbolTable.h:83
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:218