Go to the documentation of this file.
12 #ifndef OPENVDB_AX_CODEGEN_SYMBOL_TABLE_HAS_BEEN_INCLUDED
13 #define OPENVDB_AX_CODEGEN_SYMBOL_TABLE_HAS_BEEN_INCLUDED
17 #include <llvm/IR/Value.h>
20 #include <unordered_map>
36 using MapType = std::unordered_map<std::string, llvm::Value*>;
45 inline llvm::Value*
get(
const std::string& name)
const
47 const auto iter = mMap.find(name);
48 if (iter == mMap.end())
return nullptr;
56 inline bool exists(
const std::string& name)
const
58 const auto iter = mMap.find(name);
59 return (iter != mMap.end());
68 inline bool insert(
const std::string& name, llvm::Value* value)
70 if (exists(name))
return false;
81 inline bool replace(
const std::string& name, llvm::Value* value)
83 const bool existed = exists(name);
90 inline void clear() { mMap.clear(); }
113 using MapType = std::map<size_t, SymbolTable>;
129 inline bool erase(
const size_t index)
132 throw std::runtime_error(
"Attempted to erase global variables which is disallowed.");
135 const bool existed = (mTables.find(index) != mTables.end());
136 mTables.erase(index);
146 return &(mTables[index]);
156 auto iter = mTables.find(index);
157 if (iter != mTables.end())
return iter->second;
158 throw std::runtime_error(
"Attempted to access invalid symbol table with index "
159 + std::to_string(index));
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;
182 assert(it != mTables.end());
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);
230 #endif // OPENVDB_AX_CODEGEN_SYMBOL_TABLE_HAS_BEEN_INCLUDED
SymbolTable * getOrInsert(const size_t index)
Get or insert and get a SymbolTable with a unique index.
Definition: SymbolTable.h:144
~SymbolTableBlocks()=default
void clear()
Clear all symbols in this table.
Definition: SymbolTable.h:90
A symbol table which can be used to represent a single scoped set of a programs variables....
Definition: SymbolTable.h:35
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:68
std::map< size_t, SymbolTable > MapType
Definition: SymbolTable.h:113
bool exists(const std::string &name) const
Returns true if a variable exists in this symbol table with the given name.
Definition: SymbolTable.h:56
SymbolTableBlocks()
Definition: SymbolTable.h:115
Library and file format version numbers.
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:45
SymbolTable & globals()
Access to the list of global variables which are always accessible.
Definition: SymbolTable.h:120
SymbolTable & get(const size_t index)
Get a SymbolTable with a unique index. If the symbol table does not exist, this function throws a run...
Definition: SymbolTable.h:154
std::unordered_map< std::string, llvm::Value * > MapType
Definition: SymbolTable.h:36
bool replace(const std::string &name, llvm::Value *value)
Replace the first occurance of a variable with a given name with a replacement value....
Definition: SymbolTable.h:209
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
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:147
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:129
SymbolTable()
Definition: SymbolTable.h:38
A map of unique ids to symbol tables which can be used to represent local variables within a program....
Definition: SymbolTable.h:112
const MapType & map() const
Access to the underlying map.
Definition: SymbolTable.h:94
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:95
const SymbolTable & globals() const
Definition: SymbolTable.h:121
Definition: openvdb/Exceptions.h:13
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 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:81