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...) |
| 41 | class TSymbol |
| 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: |
Jamie Madill | 703cdd6 | 2013-07-08 15:07:30 -0400 | [diff] [blame] | 89 | DISALLOW_COPY_AND_ASSIGN(TSymbol); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 90 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 91 | int uniqueId; // For real comparing during code generation |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 92 | const TString *name; |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 93 | TString extension; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 96 | // Variable class, meaning a symbol that's not a function. |
| 97 | // |
| 98 | // There could be a separate class heirarchy for Constant variables; |
| 99 | // Only one of int, bool, or float, (or none) is correct for |
| 100 | // any particular use, but it's easy to do this way, and doesn't |
| 101 | // seem worth having separate classes, and "getConst" can't simply return |
| 102 | // different values for different types polymorphically, so this is |
| 103 | // just simple and pragmatic. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 104 | class TVariable : public TSymbol |
| 105 | { |
| 106 | public: |
| 107 | TVariable(const TString *name, const TType &t, bool uT = false) |
| 108 | : TSymbol(name), |
| 109 | type(t), |
| 110 | userType(uT), |
| 111 | unionArray(0) |
| 112 | { |
| 113 | } |
| 114 | virtual ~TVariable() |
| 115 | { |
| 116 | } |
| 117 | virtual bool isVariable() const |
| 118 | { |
| 119 | return true; |
| 120 | } |
| 121 | TType &getType() |
| 122 | { |
| 123 | return type; |
| 124 | } |
| 125 | const TType &getType() const |
| 126 | { |
| 127 | return type; |
| 128 | } |
| 129 | bool isUserType() const |
| 130 | { |
| 131 | return userType; |
| 132 | } |
| 133 | void setQualifier(TQualifier qualifier) |
| 134 | { |
| 135 | type.setQualifier(qualifier); |
| 136 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 137 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 138 | ConstantUnion *getConstPointer() |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 139 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 140 | if (!unionArray) |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 141 | unionArray = new ConstantUnion[type.getObjectSize()]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 142 | |
| 143 | return unionArray; |
| 144 | } |
| 145 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 146 | ConstantUnion *getConstPointer() const |
| 147 | { |
| 148 | return unionArray; |
| 149 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 150 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 151 | void shareConstPointer(ConstantUnion *constArray) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 152 | { |
alokp@chromium.org | 6c82caf | 2010-10-14 16:08:56 +0000 | [diff] [blame] | 153 | if (unionArray == constArray) |
| 154 | return; |
| 155 | |
| 156 | delete[] unionArray; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 157 | unionArray = constArray; |
| 158 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 159 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 160 | private: |
Jamie Madill | 703cdd6 | 2013-07-08 15:07:30 -0400 | [diff] [blame] | 161 | DISALLOW_COPY_AND_ASSIGN(TVariable); |
| 162 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 163 | TType type; |
| 164 | bool userType; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 165 | // we are assuming that Pool Allocator will free the memory |
| 166 | // allocated to unionArray when this object is destroyed. |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 167 | ConstantUnion *unionArray; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 170 | // The function sub-class of symbols and the parser will need to |
| 171 | // share this definition of a function parameter. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 172 | struct TParameter |
| 173 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 174 | TString *name; |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 175 | TType *type; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 178 | // The function sub-class of a symbol. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 179 | class TFunction : public TSymbol |
| 180 | { |
| 181 | public: |
| 182 | TFunction(TOperator o) |
| 183 | : TSymbol(0), |
| 184 | returnType(TType(EbtVoid, EbpUndefined)), |
| 185 | op(o), |
| 186 | defined(false) |
| 187 | { |
| 188 | } |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 189 | TFunction(const TString *name, const TType &retType, TOperator tOp = EOpNull, const char *ext = "") |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 190 | : TSymbol(name), |
| 191 | returnType(retType), |
| 192 | mangledName(TFunction::mangleName(*name)), |
| 193 | op(tOp), |
| 194 | defined(false) |
| 195 | { |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 196 | relateToExtension(ext); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 197 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 198 | virtual ~TFunction(); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 199 | virtual bool isFunction() const |
| 200 | { |
| 201 | return true; |
| 202 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 203 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 204 | static TString mangleName(const TString &name) |
| 205 | { |
| 206 | return name + '('; |
| 207 | } |
| 208 | static TString unmangleName(const TString &mangledName) |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 209 | { |
| 210 | return TString(mangledName.c_str(), mangledName.find_first_of('(')); |
| 211 | } |
| 212 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 213 | void addParameter(TParameter &p) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 214 | { |
| 215 | parameters.push_back(p); |
| 216 | mangledName = mangledName + p.type->getMangledName(); |
| 217 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 218 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 219 | const TString &getMangledName() const |
| 220 | { |
| 221 | return mangledName; |
| 222 | } |
| 223 | const TType &getReturnType() const |
| 224 | { |
| 225 | return returnType; |
| 226 | } |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 227 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 228 | TOperator getBuiltInOp() const |
| 229 | { |
| 230 | return op; |
| 231 | } |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 232 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 233 | void setDefined() |
| 234 | { |
| 235 | defined = true; |
| 236 | } |
| 237 | bool isDefined() |
| 238 | { |
| 239 | return defined; |
| 240 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 241 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 242 | size_t getParamCount() const |
| 243 | { |
| 244 | return parameters.size(); |
| 245 | } |
| 246 | const TParameter &getParam(size_t i) const |
| 247 | { |
| 248 | return parameters[i]; |
| 249 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 250 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 251 | private: |
Jamie Madill | 703cdd6 | 2013-07-08 15:07:30 -0400 | [diff] [blame] | 252 | DISALLOW_COPY_AND_ASSIGN(TFunction); |
| 253 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 254 | typedef TVector<TParameter> TParamList; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 255 | TParamList parameters; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 256 | TType returnType; |
| 257 | TString mangledName; |
| 258 | TOperator op; |
| 259 | bool defined; |
| 260 | }; |
| 261 | |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 262 | // Interface block name sub-symbol |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 263 | class TInterfaceBlockName : public TSymbol |
| 264 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 265 | public: |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 266 | TInterfaceBlockName(const TString *name) |
| 267 | : TSymbol(name) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 268 | { |
| 269 | } |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 270 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 271 | virtual ~TInterfaceBlockName() |
| 272 | { |
| 273 | } |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 274 | }; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 275 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 276 | class TSymbolTableLevel |
| 277 | { |
| 278 | public: |
| 279 | typedef TMap<TString, TSymbol *> tLevel; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 280 | typedef tLevel::const_iterator const_iterator; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 281 | typedef const tLevel::value_type tLevelPair; |
| 282 | typedef std::pair<tLevel::iterator, bool> tInsertResult; |
| 283 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 284 | TSymbolTableLevel() |
| 285 | { |
| 286 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 287 | ~TSymbolTableLevel(); |
| 288 | |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 289 | bool insert(TSymbol *symbol); |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 290 | |
Olli Etuaho | b2983c9 | 2015-03-18 14:02:46 +0200 | [diff] [blame^] | 291 | // Insert a function using its unmangled name as the key. |
| 292 | bool insertUnmangled(TFunction *function); |
| 293 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 294 | TSymbol *find(const TString &name) const; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 295 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 296 | protected: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 297 | tLevel level; |
| 298 | }; |
| 299 | |
Gus Fernandez | 964df49 | 2014-10-13 11:54:39 -0700 | [diff] [blame] | 300 | // Define ESymbolLevel as int rather than an enum since level can go |
| 301 | // above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the |
| 302 | // compiler optimizes the >= of the last element to ==. |
| 303 | typedef int ESymbolLevel; |
| 304 | const int COMMON_BUILTINS = 0; |
| 305 | const int ESSL1_BUILTINS = 1; |
| 306 | const int ESSL3_BUILTINS = 2; |
| 307 | const int LAST_BUILTIN_LEVEL = ESSL3_BUILTINS; |
| 308 | const int GLOBAL_LEVEL = 3; |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 309 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 310 | class TSymbolTable |
| 311 | { |
| 312 | public: |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 313 | TSymbolTable() |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 314 | : mGlobalInvariant(false) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 315 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 316 | // The symbol table cannot be used until push() is called, but |
| 317 | // the lack of an initial call to push() can be used to detect |
| 318 | // that the symbol table has not been preloaded with built-ins. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 321 | ~TSymbolTable(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 322 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 323 | // When the symbol table is initialized with the built-ins, there should |
| 324 | // 'push' calls, so that built-ins are at level 0 and the shader |
| 325 | // globals are at level 1. |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 326 | bool isEmpty() const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 327 | { |
| 328 | return table.empty(); |
| 329 | } |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 330 | bool atBuiltInLevel() const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 331 | { |
| 332 | return currentLevel() <= LAST_BUILTIN_LEVEL; |
| 333 | } |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 334 | bool atGlobalLevel() const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 335 | { |
| 336 | return currentLevel() <= GLOBAL_LEVEL; |
| 337 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 338 | void push() |
alokp@chromium.org | e4249f0 | 2010-07-26 18:13:52 +0000 | [diff] [blame] | 339 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 340 | table.push_back(new TSymbolTableLevel); |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 341 | precisionStack.push_back(new PrecisionStackLevel); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 342 | } |
| 343 | |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 344 | void pop() |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 345 | { |
| 346 | delete table.back(); |
| 347 | table.pop_back(); |
| 348 | |
| 349 | delete precisionStack.back(); |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 350 | precisionStack.pop_back(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 353 | bool declare(TSymbol *symbol) |
shannonwoods@chromium.org | 1c84809 | 2013-05-30 00:02:34 +0000 | [diff] [blame] | 354 | { |
| 355 | return insert(currentLevel(), symbol); |
| 356 | } |
| 357 | |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 358 | bool insert(ESymbolLevel level, TSymbol *symbol) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 359 | { |
shannonwoods@chromium.org | 1c84809 | 2013-05-30 00:02:34 +0000 | [diff] [blame] | 360 | return table[level]->insert(symbol); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 361 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 362 | |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 363 | bool insert(ESymbolLevel level, const char *ext, TSymbol *symbol) |
| 364 | { |
| 365 | symbol->relateToExtension(ext); |
| 366 | return table[level]->insert(symbol); |
| 367 | } |
| 368 | |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 369 | bool insertConstInt(ESymbolLevel level, const char *name, int value) |
| 370 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 371 | TVariable *constant = new TVariable( |
| 372 | NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1)); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 373 | constant->getConstPointer()->setIConst(value); |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 374 | return insert(level, constant); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 375 | } |
| 376 | |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 377 | void insertBuiltIn(ESymbolLevel level, TOperator op, const char *ext, TType *rvalue, const char *name, |
| 378 | TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0, TType *ptype5 = 0); |
Nicolas Capens | 759b994 | 2014-02-14 17:57:14 -0500 | [diff] [blame] | 379 | |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 380 | void insertBuiltIn(ESymbolLevel level, TType *rvalue, const char *name, |
| 381 | TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0, TType *ptype5 = 0) |
Nicolas Capens | 482907e | 2015-02-23 16:56:33 -0500 | [diff] [blame] | 382 | { |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 383 | insertBuiltIn(level, EOpNull, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5); |
| 384 | } |
| 385 | |
| 386 | void insertBuiltIn(ESymbolLevel level, const char *ext, TType *rvalue, const char *name, |
| 387 | TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0, TType *ptype5 = 0) |
| 388 | { |
| 389 | insertBuiltIn(level, EOpNull, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5); |
| 390 | } |
| 391 | |
| 392 | void insertBuiltIn(ESymbolLevel level, TOperator op, TType *rvalue, const char *name, |
| 393 | TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0, TType *ptype5 = 0) |
| 394 | { |
| 395 | insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5); |
Nicolas Capens | 482907e | 2015-02-23 16:56:33 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 398 | TSymbol *find(const TString &name, int shaderVersion, |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 399 | bool *builtIn = NULL, bool *sameScope = NULL) const; |
| 400 | TSymbol *findBuiltIn(const TString &name, int shaderVersion) const; |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 401 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 402 | TSymbolTableLevel *getOuterLevel() |
| 403 | { |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 404 | assert(currentLevel() >= 1); |
daniel@transgaming.com | 5dd6d09 | 2012-03-20 20:10:28 +0000 | [diff] [blame] | 405 | return table[currentLevel() - 1]; |
| 406 | } |
| 407 | |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 408 | void dump(TInfoSink &infoSink) const; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 409 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 410 | bool setDefaultPrecision(const TPublicType &type, TPrecision prec) |
| 411 | { |
Zhenyao Mo | ed14b79 | 2014-05-08 11:21:07 -0700 | [diff] [blame] | 412 | if (!SupportsPrecision(type.type)) |
Zhenyao Mo | a5a1dfc | 2013-09-23 14:57:03 -0400 | [diff] [blame] | 413 | return false; |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 414 | if (type.isAggregate()) |
shannon.woods@transgaming.com | d25a6b3 | 2013-02-28 23:19:13 +0000 | [diff] [blame] | 415 | return false; // Not allowed to set for aggregate types |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 416 | int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 417 | // Uses map operator [], overwrites the current value |
| 418 | (*precisionStack[indexOfLastElement])[type.type] = prec; |
shannon.woods@transgaming.com | d25a6b3 | 2013-02-28 23:19:13 +0000 | [diff] [blame] | 419 | return true; |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 422 | // Searches down the precisionStack for a precision qualifier |
| 423 | // for the specified TBasicType |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 424 | TPrecision getDefaultPrecision(TBasicType type) const; |
shannonwoods@chromium.org | 6b70991 | 2013-05-30 00:20:04 +0000 | [diff] [blame] | 425 | |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 426 | // This records invariant varyings declared through |
| 427 | // "invariant varying_name;". |
Jamie Madill | 2c43325 | 2014-12-03 12:36:54 -0500 | [diff] [blame] | 428 | void addInvariantVarying(const std::string &originalName) |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 429 | { |
| 430 | mInvariantVaryings.insert(originalName); |
| 431 | } |
| 432 | // If this returns false, the varying could still be invariant |
| 433 | // if it is set as invariant during the varying variable |
| 434 | // declaration - this piece of information is stored in the |
| 435 | // variable's type, not here. |
Jamie Madill | 2c43325 | 2014-12-03 12:36:54 -0500 | [diff] [blame] | 436 | bool isVaryingInvariant(const std::string &originalName) const |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 437 | { |
| 438 | return (mGlobalInvariant || |
| 439 | mInvariantVaryings.count(originalName) > 0); |
| 440 | } |
| 441 | |
| 442 | void setGlobalInvariant() { mGlobalInvariant = true; } |
| 443 | bool getGlobalInvariant() const { return mGlobalInvariant; } |
| 444 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 445 | static int nextUniqueId() |
| 446 | { |
| 447 | return ++uniqueIdCounter; |
| 448 | } |
| 449 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 450 | private: |
| 451 | ESymbolLevel currentLevel() const |
| 452 | { |
| 453 | return static_cast<ESymbolLevel>(table.size() - 1); |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 456 | std::vector<TSymbolTableLevel *> table; |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 457 | typedef TMap<TBasicType, TPrecision> PrecisionStackLevel; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 458 | std::vector< PrecisionStackLevel *> precisionStack; |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 459 | |
Jamie Madill | 2c43325 | 2014-12-03 12:36:54 -0500 | [diff] [blame] | 460 | std::set<std::string> mInvariantVaryings; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 461 | bool mGlobalInvariant; |
| 462 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 463 | static int uniqueIdCounter; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 464 | }; |
| 465 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 466 | #endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_ |