daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
Nicolas Capens | 1fbc287 | 2014-01-03 14:12:09 -0500 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 7 | #ifndef COMPILER_TRANSLATOR_SYMBOLTABLE_H_ |
| 8 | #define COMPILER_TRANSLATOR_SYMBOLTABLE_H_ |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 9 | |
| 10 | // |
| 11 | // Symbol table for parsing. Has these design characteristics: |
| 12 | // |
| 13 | // * Same symbol table can be used to compile many shaders, to preserve |
| 14 | // effort of creating and loading with the large numbers of built-in |
| 15 | // symbols. |
| 16 | // |
| 17 | // * Name mangling will be used to give each function a unique name |
| 18 | // so that symbol table lookups are never ambiguous. This allows |
| 19 | // a simpler symbol table structure. |
| 20 | // |
| 21 | // * Pushing and popping of scope, so symbol table will really be a stack |
| 22 | // of symbol tables. Searched from the top, with new inserts going into |
| 23 | // the top. |
| 24 | // |
| 25 | // * Constants: Compile time constant symbols will keep their values |
| 26 | // in the symbol table. The parser can substitute constants at parse |
| 27 | // time, including doing constant folding and constant propagation. |
| 28 | // |
| 29 | // * No temporaries: Temporaries made from operations (+, --, .xy, etc.) |
| 30 | // are tracked in the intermediate representation, not the symbol table. |
| 31 | // |
| 32 | |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 33 | #include <assert.h> |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 34 | #include <set> |
alokp@chromium.org | e4249f0 | 2010-07-26 18:13:52 +0000 | [diff] [blame] | 35 | |
Jamie Madill | 703cdd6 | 2013-07-08 15:07:30 -0400 | [diff] [blame] | 36 | #include "common/angleutils.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 37 | #include "compiler/translator/InfoSink.h" |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 38 | #include "compiler/translator/IntermNode.h" |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 39 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 40 | // Symbol base class. (Can build functions or variables out of these...) |
Jamie Madill | f0d10f8 | 2015-03-31 12:56:52 -0400 | [diff] [blame] | 41 | class TSymbol : angle::NonCopyable |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 42 | { |
| 43 | public: |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 44 | POOL_ALLOCATOR_NEW_DELETE(); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 45 | TSymbol(const TString *n) |
| 46 | : uniqueId(0), |
| 47 | name(n) |
| 48 | { |
| 49 | } |
| 50 | virtual ~TSymbol() |
| 51 | { |
| 52 | // don't delete name, it's from the pool |
| 53 | } |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 54 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 55 | const TString &getName() const |
| 56 | { |
| 57 | return *name; |
| 58 | } |
| 59 | virtual const TString &getMangledName() const |
| 60 | { |
| 61 | return getName(); |
| 62 | } |
| 63 | virtual bool isFunction() const |
| 64 | { |
| 65 | return false; |
| 66 | } |
| 67 | virtual bool isVariable() const |
| 68 | { |
| 69 | return false; |
| 70 | } |
| 71 | void setUniqueId(int id) |
| 72 | { |
| 73 | uniqueId = id; |
| 74 | } |
| 75 | int getUniqueId() const |
| 76 | { |
| 77 | return uniqueId; |
| 78 | } |
| 79 | void relateToExtension(const TString &ext) |
| 80 | { |
| 81 | extension = ext; |
| 82 | } |
| 83 | const TString &getExtension() const |
| 84 | { |
| 85 | return extension; |
| 86 | } |
Nicolas Capens | ba60ad3 | 2013-06-04 15:55:47 -0400 | [diff] [blame] | 87 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 88 | private: |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 89 | int uniqueId; // For real comparing during code generation |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 90 | const TString *name; |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 91 | TString extension; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 94 | // Variable class, meaning a symbol that's not a function. |
| 95 | // |
| 96 | // There could be a separate class heirarchy for Constant variables; |
| 97 | // Only one of int, bool, or float, (or none) is correct for |
| 98 | // any particular use, but it's easy to do this way, and doesn't |
| 99 | // seem worth having separate classes, and "getConst" can't simply return |
| 100 | // different values for different types polymorphically, so this is |
| 101 | // just simple and pragmatic. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 102 | class TVariable : public TSymbol |
| 103 | { |
| 104 | public: |
| 105 | TVariable(const TString *name, const TType &t, bool uT = false) |
| 106 | : TSymbol(name), |
| 107 | type(t), |
| 108 | userType(uT), |
| 109 | unionArray(0) |
| 110 | { |
| 111 | } |
Corentin Wallez | e5a1f27 | 2015-08-21 02:58:25 +0200 | [diff] [blame] | 112 | ~TVariable() override {} |
| 113 | bool isVariable() const override { return true; } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 114 | TType &getType() |
| 115 | { |
| 116 | return type; |
| 117 | } |
| 118 | const TType &getType() const |
| 119 | { |
| 120 | return type; |
| 121 | } |
| 122 | bool isUserType() const |
| 123 | { |
| 124 | return userType; |
| 125 | } |
| 126 | void setQualifier(TQualifier qualifier) |
| 127 | { |
| 128 | type.setQualifier(qualifier); |
| 129 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 130 | |
Olli Etuaho | 5c0e023 | 2015-11-11 15:55:59 +0200 | [diff] [blame] | 131 | const TConstantUnion *getConstPointer() const { return unionArray; } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 132 | |
Olli Etuaho | 5c0e023 | 2015-11-11 15:55:59 +0200 | [diff] [blame] | 133 | void shareConstPointer(const TConstantUnion *constArray) { unionArray = constArray; } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 134 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 135 | private: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 136 | TType type; |
| 137 | bool userType; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 138 | // we are assuming that Pool Allocator will free the memory |
| 139 | // allocated to unionArray when this object is destroyed. |
Olli Etuaho | 5c0e023 | 2015-11-11 15:55:59 +0200 | [diff] [blame] | 140 | const TConstantUnion *unionArray; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 143 | // Immutable version of TParameter. |
| 144 | struct TConstParameter |
| 145 | { |
| 146 | TConstParameter() |
| 147 | : name(nullptr), |
| 148 | type(nullptr) |
| 149 | { |
| 150 | } |
| 151 | explicit TConstParameter(const TString *n) |
| 152 | : name(n), |
| 153 | type(nullptr) |
| 154 | { |
| 155 | } |
| 156 | explicit TConstParameter(const TType *t) |
| 157 | : name(nullptr), |
| 158 | type(t) |
| 159 | { |
| 160 | } |
| 161 | TConstParameter(const TString *n, const TType *t) |
| 162 | : name(n), |
| 163 | type(t) |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | // Both constructor arguments must be const. |
| 168 | TConstParameter(TString *n, TType *t) = delete; |
| 169 | TConstParameter(const TString *n, TType *t) = delete; |
| 170 | TConstParameter(TString *n, const TType *t) = delete; |
| 171 | |
| 172 | const TString *name; |
| 173 | const TType *type; |
| 174 | }; |
| 175 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 176 | // The function sub-class of symbols and the parser will need to |
| 177 | // share this definition of a function parameter. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 178 | struct TParameter |
| 179 | { |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 180 | // Destructively converts to TConstParameter. |
| 181 | // This method resets name and type to nullptrs to make sure |
| 182 | // their content cannot be modified after the call. |
| 183 | TConstParameter turnToConst() |
| 184 | { |
| 185 | const TString *constName = name; |
| 186 | const TType *constType = type; |
| 187 | name = nullptr; |
| 188 | type = nullptr; |
| 189 | return TConstParameter(constName, constType); |
| 190 | } |
| 191 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 192 | TString *name; |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 193 | TType *type; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 196 | // The function sub-class of a symbol. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 197 | class TFunction : public TSymbol |
| 198 | { |
| 199 | public: |
Olli Etuaho | 5d65318 | 2016-01-04 14:43:28 +0200 | [diff] [blame] | 200 | TFunction(const TString *name, |
| 201 | const TType *retType, |
| 202 | TOperator tOp = EOpNull, |
| 203 | const char *ext = "") |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 204 | : TSymbol(name), |
| 205 | returnType(retType), |
Dmitry Skiba | 5883220 | 2015-07-06 16:11:13 -0700 | [diff] [blame] | 206 | mangledName(nullptr), |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 207 | op(tOp), |
Olli Etuaho | 5d65318 | 2016-01-04 14:43:28 +0200 | [diff] [blame] | 208 | defined(false), |
| 209 | mHasPrototypeDeclaration(false) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 210 | { |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 211 | relateToExtension(ext); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 212 | } |
Corentin Wallez | e5a1f27 | 2015-08-21 02:58:25 +0200 | [diff] [blame] | 213 | ~TFunction() override; |
| 214 | bool isFunction() const override { return true; } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 215 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 216 | static TString mangleName(const TString &name) |
| 217 | { |
| 218 | return name + '('; |
| 219 | } |
| 220 | static TString unmangleName(const TString &mangledName) |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 221 | { |
| 222 | return TString(mangledName.c_str(), mangledName.find_first_of('(')); |
| 223 | } |
| 224 | |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 225 | void addParameter(const TConstParameter &p) |
Dmitry Skiba | 5883220 | 2015-07-06 16:11:13 -0700 | [diff] [blame] | 226 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 227 | parameters.push_back(p); |
Dmitry Skiba | 5883220 | 2015-07-06 16:11:13 -0700 | [diff] [blame] | 228 | mangledName = nullptr; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 229 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 230 | |
Corentin Wallez | e5a1f27 | 2015-08-21 02:58:25 +0200 | [diff] [blame] | 231 | const TString &getMangledName() const override |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 232 | { |
Dmitry Skiba | 5883220 | 2015-07-06 16:11:13 -0700 | [diff] [blame] | 233 | if (mangledName == nullptr) |
| 234 | { |
| 235 | mangledName = buildMangledName(); |
| 236 | } |
| 237 | return *mangledName; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 238 | } |
| 239 | const TType &getReturnType() const |
| 240 | { |
Dmitry Skiba | 7f17a50 | 2015-06-22 15:08:39 -0700 | [diff] [blame] | 241 | return *returnType; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 242 | } |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 243 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 244 | TOperator getBuiltInOp() const |
| 245 | { |
| 246 | return op; |
| 247 | } |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 248 | |
Olli Etuaho | 5d65318 | 2016-01-04 14:43:28 +0200 | [diff] [blame] | 249 | void setDefined() { defined = true; } |
| 250 | bool isDefined() { return defined; } |
| 251 | void setHasPrototypeDeclaration() { mHasPrototypeDeclaration = true; } |
| 252 | bool hasPrototypeDeclaration() const { return mHasPrototypeDeclaration; } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 253 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 254 | size_t getParamCount() const |
| 255 | { |
| 256 | return parameters.size(); |
| 257 | } |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 258 | const TConstParameter &getParam(size_t i) const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 259 | { |
| 260 | return parameters[i]; |
| 261 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 262 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 263 | private: |
Dmitry Skiba | 5883220 | 2015-07-06 16:11:13 -0700 | [diff] [blame] | 264 | const TString *buildMangledName() const; |
| 265 | |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 266 | typedef TVector<TConstParameter> TParamList; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 267 | TParamList parameters; |
Dmitry Skiba | 7f17a50 | 2015-06-22 15:08:39 -0700 | [diff] [blame] | 268 | const TType *returnType; |
Dmitry Skiba | 5883220 | 2015-07-06 16:11:13 -0700 | [diff] [blame] | 269 | mutable const TString *mangledName; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 270 | TOperator op; |
| 271 | bool defined; |
Olli Etuaho | 5d65318 | 2016-01-04 14:43:28 +0200 | [diff] [blame] | 272 | bool mHasPrototypeDeclaration; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 275 | // Interface block name sub-symbol |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 276 | class TInterfaceBlockName : public TSymbol |
| 277 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 278 | public: |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 279 | TInterfaceBlockName(const TString *name) |
| 280 | : TSymbol(name) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 281 | { |
| 282 | } |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 283 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 284 | virtual ~TInterfaceBlockName() |
| 285 | { |
| 286 | } |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 287 | }; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 288 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 289 | class TSymbolTableLevel |
| 290 | { |
| 291 | public: |
| 292 | typedef TMap<TString, TSymbol *> tLevel; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 293 | typedef tLevel::const_iterator const_iterator; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 294 | typedef const tLevel::value_type tLevelPair; |
| 295 | typedef std::pair<tLevel::iterator, bool> tInsertResult; |
| 296 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 297 | TSymbolTableLevel() |
| 298 | { |
| 299 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 300 | ~TSymbolTableLevel(); |
| 301 | |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 302 | bool insert(TSymbol *symbol); |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 303 | |
Olli Etuaho | b2983c9 | 2015-03-18 14:02:46 +0200 | [diff] [blame] | 304 | // Insert a function using its unmangled name as the key. |
| 305 | bool insertUnmangled(TFunction *function); |
| 306 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 307 | TSymbol *find(const TString &name) const; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 308 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 309 | protected: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 310 | tLevel level; |
| 311 | }; |
| 312 | |
Gus Fernandez | 964df49 | 2014-10-13 11:54:39 -0700 | [diff] [blame] | 313 | // Define ESymbolLevel as int rather than an enum since level can go |
| 314 | // above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the |
| 315 | // compiler optimizes the >= of the last element to ==. |
| 316 | typedef int ESymbolLevel; |
| 317 | const int COMMON_BUILTINS = 0; |
| 318 | const int ESSL1_BUILTINS = 1; |
| 319 | const int ESSL3_BUILTINS = 2; |
| 320 | const int LAST_BUILTIN_LEVEL = ESSL3_BUILTINS; |
| 321 | const int GLOBAL_LEVEL = 3; |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 322 | |
Jamie Madill | f0d10f8 | 2015-03-31 12:56:52 -0400 | [diff] [blame] | 323 | class TSymbolTable : angle::NonCopyable |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 324 | { |
| 325 | public: |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 326 | TSymbolTable() |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 327 | : mGlobalInvariant(false) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 328 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 329 | // The symbol table cannot be used until push() is called, but |
| 330 | // the lack of an initial call to push() can be used to detect |
| 331 | // that the symbol table has not been preloaded with built-ins. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 334 | ~TSymbolTable(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 335 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 336 | // When the symbol table is initialized with the built-ins, there should |
| 337 | // 'push' calls, so that built-ins are at level 0 and the shader |
| 338 | // globals are at level 1. |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 339 | bool isEmpty() const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 340 | { |
| 341 | return table.empty(); |
| 342 | } |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 343 | bool atBuiltInLevel() const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 344 | { |
| 345 | return currentLevel() <= LAST_BUILTIN_LEVEL; |
| 346 | } |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 347 | bool atGlobalLevel() const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 348 | { |
| 349 | return currentLevel() <= GLOBAL_LEVEL; |
| 350 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 351 | void push() |
alokp@chromium.org | e4249f0 | 2010-07-26 18:13:52 +0000 | [diff] [blame] | 352 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 353 | table.push_back(new TSymbolTableLevel); |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 354 | precisionStack.push_back(new PrecisionStackLevel); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 355 | } |
| 356 | |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 357 | void pop() |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 358 | { |
| 359 | delete table.back(); |
| 360 | table.pop_back(); |
| 361 | |
| 362 | delete precisionStack.back(); |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 363 | precisionStack.pop_back(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 366 | bool declare(TSymbol *symbol) |
shannonwoods@chromium.org | 1c84809 | 2013-05-30 00:02:34 +0000 | [diff] [blame] | 367 | { |
| 368 | return insert(currentLevel(), symbol); |
| 369 | } |
| 370 | |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 371 | bool insert(ESymbolLevel level, TSymbol *symbol) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 372 | { |
shannonwoods@chromium.org | 1c84809 | 2013-05-30 00:02:34 +0000 | [diff] [blame] | 373 | return table[level]->insert(symbol); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 374 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 375 | |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 376 | bool insert(ESymbolLevel level, const char *ext, TSymbol *symbol) |
| 377 | { |
| 378 | symbol->relateToExtension(ext); |
| 379 | return table[level]->insert(symbol); |
| 380 | } |
| 381 | |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 382 | bool insertConstInt(ESymbolLevel level, const char *name, int value) |
| 383 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 384 | TVariable *constant = new TVariable( |
| 385 | NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1)); |
Olli Etuaho | 5c0e023 | 2015-11-11 15:55:59 +0200 | [diff] [blame] | 386 | TConstantUnion *unionArray = new TConstantUnion[1]; |
| 387 | unionArray[0].setIConst(value); |
| 388 | constant->shareConstPointer(unionArray); |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 389 | return insert(level, constant); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 390 | } |
| 391 | |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 392 | bool insertConstIntExt(ESymbolLevel level, const char *ext, const char *name, int value) |
| 393 | { |
| 394 | TVariable *constant = |
| 395 | new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1)); |
Olli Etuaho | 5c0e023 | 2015-11-11 15:55:59 +0200 | [diff] [blame] | 396 | TConstantUnion *unionArray = new TConstantUnion[1]; |
| 397 | unionArray[0].setIConst(value); |
| 398 | constant->shareConstPointer(unionArray); |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 399 | return insert(level, ext, constant); |
| 400 | } |
| 401 | |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 402 | void insertBuiltIn(ESymbolLevel level, TOperator op, const char *ext, const TType *rvalue, const char *name, |
| 403 | const TType *ptype1, const TType *ptype2 = 0, const TType *ptype3 = 0, const TType *ptype4 = 0, const TType *ptype5 = 0); |
Nicolas Capens | 759b994 | 2014-02-14 17:57:14 -0500 | [diff] [blame] | 404 | |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 405 | void insertBuiltIn(ESymbolLevel level, const TType *rvalue, const char *name, |
| 406 | const TType *ptype1, const TType *ptype2 = 0, const TType *ptype3 = 0, const TType *ptype4 = 0, const TType *ptype5 = 0) |
Nicolas Capens | 482907e | 2015-02-23 16:56:33 -0500 | [diff] [blame] | 407 | { |
Olli Etuaho | c4a96d6 | 2015-07-23 17:37:39 +0530 | [diff] [blame^] | 408 | insertUnmangledBuiltIn(name); |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 409 | insertBuiltIn(level, EOpNull, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5); |
| 410 | } |
| 411 | |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 412 | void insertBuiltIn(ESymbolLevel level, const char *ext, const TType *rvalue, const char *name, |
| 413 | const TType *ptype1, const TType *ptype2 = 0, const TType *ptype3 = 0, const TType *ptype4 = 0, const TType *ptype5 = 0) |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 414 | { |
Olli Etuaho | c4a96d6 | 2015-07-23 17:37:39 +0530 | [diff] [blame^] | 415 | insertUnmangledBuiltIn(name); |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 416 | insertBuiltIn(level, EOpNull, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5); |
| 417 | } |
| 418 | |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 419 | void insertBuiltIn(ESymbolLevel level, TOperator op, const TType *rvalue, const char *name, |
| 420 | const TType *ptype1, const TType *ptype2 = 0, const TType *ptype3 = 0, const TType *ptype4 = 0, const TType *ptype5 = 0) |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 421 | { |
Olli Etuaho | c4a96d6 | 2015-07-23 17:37:39 +0530 | [diff] [blame^] | 422 | insertUnmangledBuiltIn(name); |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 423 | insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5); |
Nicolas Capens | 482907e | 2015-02-23 16:56:33 -0500 | [diff] [blame] | 424 | } |
| 425 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 426 | TSymbol *find(const TString &name, int shaderVersion, |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 427 | bool *builtIn = NULL, bool *sameScope = NULL) const; |
| 428 | TSymbol *findBuiltIn(const TString &name, int shaderVersion) const; |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 429 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 430 | TSymbolTableLevel *getOuterLevel() |
| 431 | { |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 432 | assert(currentLevel() >= 1); |
daniel@transgaming.com | 5dd6d09 | 2012-03-20 20:10:28 +0000 | [diff] [blame] | 433 | return table[currentLevel() - 1]; |
| 434 | } |
| 435 | |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 436 | void dump(TInfoSink &infoSink) const; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 437 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 438 | bool setDefaultPrecision(const TPublicType &type, TPrecision prec) |
| 439 | { |
Zhenyao Mo | ed14b79 | 2014-05-08 11:21:07 -0700 | [diff] [blame] | 440 | if (!SupportsPrecision(type.type)) |
Zhenyao Mo | a5a1dfc | 2013-09-23 14:57:03 -0400 | [diff] [blame] | 441 | return false; |
Olli Etuaho | 0980e29 | 2015-11-20 14:57:34 +0200 | [diff] [blame] | 442 | if (type.type == EbtUInt) |
| 443 | return false; // ESSL 3.00.4 section 4.5.4 |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 444 | if (type.isAggregate()) |
shannon.woods@transgaming.com | d25a6b3 | 2013-02-28 23:19:13 +0000 | [diff] [blame] | 445 | return false; // Not allowed to set for aggregate types |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 446 | int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 447 | // Uses map operator [], overwrites the current value |
| 448 | (*precisionStack[indexOfLastElement])[type.type] = prec; |
shannon.woods@transgaming.com | d25a6b3 | 2013-02-28 23:19:13 +0000 | [diff] [blame] | 449 | return true; |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 452 | // Searches down the precisionStack for a precision qualifier |
| 453 | // for the specified TBasicType |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 454 | TPrecision getDefaultPrecision(TBasicType type) const; |
shannonwoods@chromium.org | 6b70991 | 2013-05-30 00:20:04 +0000 | [diff] [blame] | 455 | |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 456 | // This records invariant varyings declared through |
| 457 | // "invariant varying_name;". |
Jamie Madill | 2c43325 | 2014-12-03 12:36:54 -0500 | [diff] [blame] | 458 | void addInvariantVarying(const std::string &originalName) |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 459 | { |
| 460 | mInvariantVaryings.insert(originalName); |
| 461 | } |
| 462 | // If this returns false, the varying could still be invariant |
| 463 | // if it is set as invariant during the varying variable |
| 464 | // declaration - this piece of information is stored in the |
| 465 | // variable's type, not here. |
Jamie Madill | 2c43325 | 2014-12-03 12:36:54 -0500 | [diff] [blame] | 466 | bool isVaryingInvariant(const std::string &originalName) const |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 467 | { |
| 468 | return (mGlobalInvariant || |
| 469 | mInvariantVaryings.count(originalName) > 0); |
| 470 | } |
| 471 | |
| 472 | void setGlobalInvariant() { mGlobalInvariant = true; } |
| 473 | bool getGlobalInvariant() const { return mGlobalInvariant; } |
| 474 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 475 | static int nextUniqueId() |
| 476 | { |
| 477 | return ++uniqueIdCounter; |
| 478 | } |
| 479 | |
Olli Etuaho | c4a96d6 | 2015-07-23 17:37:39 +0530 | [diff] [blame^] | 480 | bool hasUnmangledBuiltIn(const char *name) |
| 481 | { |
| 482 | return mUnmangledBuiltinNames.count(std::string(name)) > 0; |
| 483 | } |
| 484 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 485 | private: |
| 486 | ESymbolLevel currentLevel() const |
| 487 | { |
| 488 | return static_cast<ESymbolLevel>(table.size() - 1); |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Olli Etuaho | c4a96d6 | 2015-07-23 17:37:39 +0530 | [diff] [blame^] | 491 | // Used to insert unmangled functions to check redeclaration of built-ins in ESSL 3.00. |
| 492 | void insertUnmangledBuiltIn(const char *name) |
| 493 | { |
| 494 | mUnmangledBuiltinNames.insert(std::string(name)); |
| 495 | } |
| 496 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 497 | std::vector<TSymbolTableLevel *> table; |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 498 | typedef TMap<TBasicType, TPrecision> PrecisionStackLevel; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 499 | std::vector< PrecisionStackLevel *> precisionStack; |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 500 | |
Olli Etuaho | c4a96d6 | 2015-07-23 17:37:39 +0530 | [diff] [blame^] | 501 | std::set<std::string> mUnmangledBuiltinNames; |
| 502 | |
Jamie Madill | 2c43325 | 2014-12-03 12:36:54 -0500 | [diff] [blame] | 503 | std::set<std::string> mInvariantVaryings; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 504 | bool mGlobalInvariant; |
| 505 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 506 | static int uniqueIdCounter; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 507 | }; |
| 508 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 509 | #endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_ |