| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#ifndef SKSL_SYMBOLTABLE |
| 9 |
#define SKSL_SYMBOLTABLE |
| 10 |
|
| 11 |
#include "include/core/SkTypes.h" |
| 12 |
#include "src/core/SkChecksum.h" |
| 13 |
#include "src/core/SkTHash.h" |
| 14 |
#include "src/sksl/ir/SkSLSymbol.h" |
| 15 |
|
| 16 |
#include <cstddef> |
| 17 |
#include <cstdint> |
| 18 |
#include <forward_list> |
| 19 |
#include <memory> |
| 20 |
#include <string> |
| 21 |
#include <string_view> |
| 22 |
#include <utility> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
namespace SkSL { |
| 26 |
|
| 27 |
class Context; |
| 28 |
class Expression; |
| 29 |
class Position; |
| 30 |
class Type; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class SymbolTable { |
| 36 |
public: |
| 37 |
explicit SymbolTable(bool builtin) |
| 38 |
: fBuiltin(builtin) {} |
| 39 |
|
| 40 |
explicit SymbolTable(SymbolTable* parent, bool builtin) |
| 41 |
: fParent(parent) |
| 42 |
, fBuiltin(builtin) {} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
std::unique_ptr<SymbolTable> insertNewParent(); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
const Symbol* find(std::string_view name) const { |
| 55 |
return this->lookup(MakeSymbolKey(name)); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
const Symbol* findBuiltinSymbol(std::string_view name) const; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
Symbol* findMutable(std::string_view name) const { |
| 68 |
return this->lookup(MakeSymbolKey(name)); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
std::unique_ptr<Expression> instantiateSymbolRef(const Context& context, |
| 76 |
std::string_view name, |
| 77 |
Position pos); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
void renameSymbol(const Context& context, Symbol* symbol, std::string_view newName); |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
std::unique_ptr<Symbol> removeSymbol(const Symbol* symbol); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
void moveSymbolTo(SymbolTable* otherTable, Symbol* sym, const Context& context); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
bool isType(std::string_view name) const; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
bool isBuiltinType(std::string_view name) const; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
void addWithoutOwnershipOrDie(Symbol* symbol); |
| 114 |
void addWithoutOwnership(const Context& context, Symbol* symbol); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
template <typename T> |
| 121 |
T* add(const Context& context, std::unique_ptr<T> symbol) { |
| 122 |
T* ptr = symbol.get(); |
| 123 |
this->addWithoutOwnership(context, this->takeOwnershipOfSymbol(std::move(symbol))); |
| 124 |
return ptr; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
template <typename T> |
| 132 |
T* addOrDie(std::unique_ptr<T> symbol) { |
| 133 |
T* ptr = symbol.get(); |
| 134 |
this->addWithoutOwnershipOrDie(this->takeOwnershipOfSymbol(std::move(symbol))); |
| 135 |
return ptr; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
void injectWithoutOwnership(Symbol* symbol); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
template <typename T> |
| 149 |
T* inject(std::unique_ptr<T> symbol) { |
| 150 |
T* ptr = symbol.get(); |
| 151 |
this->injectWithoutOwnership(this->takeOwnershipOfSymbol(std::move(symbol))); |
| 152 |
return ptr; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
template <typename T> |
| 159 |
T* takeOwnershipOfSymbol(std::unique_ptr<T> symbol) { |
| 160 |
T* ptr = symbol.get(); |
| 161 |
fOwnedSymbols.push_back(std::move(symbol)); |
| 162 |
return ptr; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
const Type* addArrayDimension(const Context& context, const Type* type, int arraySize); |
| 171 |
|
| 172 |
|
| 173 |
template <typename Fn> |
| 174 |
void foreach(Fn&& fn) const { |
| 175 |
fSymbols.foreach( |
| 176 |
[&fn](const SymbolKey& key, const Symbol* symbol) { fn(key.fName, symbol); }); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
bool wouldShadowSymbolsFrom(const SymbolTable* other) const; |
| 182 |
|
| 183 |
size_t count() const { |
| 184 |
return fSymbols.count(); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
bool isBuiltin() const { |
| 189 |
return fBuiltin; |
| 190 |
} |
| 191 |
|
| 192 |
const std::string* takeOwnershipOfString(std::string n); |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
void markModuleBoundary() { |
| 198 |
fAtModuleBoundary = true; |
| 199 |
} |
| 200 |
|
| 201 |
SymbolTable* fParent = nullptr; |
| 202 |
|
| 203 |
std::vector<std::unique_ptr<Symbol>> fOwnedSymbols; |
| 204 |
|
| 205 |
private: |
| 206 |
struct SymbolKey { |
| 207 |
std::string_view fName; |
| 208 |
uint32_t fHash; |
| 209 |
|
| 210 |
bool operator==(const SymbolKey& that) const { return fName == that.fName; } |
| 211 |
bool operator!=(const SymbolKey& that) const { return fName != that.fName; } |
| 212 |
struct Hash { |
| 213 |
uint32_t operator()(const SymbolKey& key) const { return key.fHash; } |
| 214 |
}; |
| 215 |
}; |
| 216 |
|
| 217 |
static SymbolKey MakeSymbolKey(std::string_view name) { |
| 218 |
return SymbolKey{name, SkChecksum::Hash32(name.data(), name.size())}; |
| 219 |
} |
| 220 |
|
| 221 |
Symbol* lookup(const SymbolKey& key) const; |
| 222 |
bool addWithoutOwnership(Symbol* symbol); |
| 223 |
|
| 224 |
bool fBuiltin = false; |
| 225 |
bool fAtModuleBoundary = false; |
| 226 |
std::forward_list<std::string> fOwnedStrings; |
| 227 |
skia_private::THashMap<SymbolKey, Symbol*, SymbolKey::Hash> fSymbols; |
| 228 |
}; |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
#endif |