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 | |
| 7 | #ifndef _SYMBOL_TABLE_INCLUDED_ |
| 8 | #define _SYMBOL_TABLE_INCLUDED_ |
| 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> |
alokp@chromium.org | e4249f0 | 2010-07-26 18:13:52 +0000 | [diff] [blame] | 34 | |
Jamie Madill | 703cdd6 | 2013-07-08 15:07:30 -0400 | [diff] [blame] | 35 | #include "common/angleutils.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 36 | #include "compiler/translator/InfoSink.h" |
| 37 | #include "compiler/translator/intermediate.h" |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 38 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 39 | // Symbol base class. (Can build functions or variables out of these...) |
| 40 | class TSymbol |
| 41 | { |
| 42 | public: |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 43 | POOL_ALLOCATOR_NEW_DELETE(); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 44 | TSymbol(const TString *n) |
| 45 | : uniqueId(0), |
| 46 | name(n) |
| 47 | { |
| 48 | } |
| 49 | virtual ~TSymbol() |
| 50 | { |
| 51 | // don't delete name, it's from the pool |
| 52 | } |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 53 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 54 | const TString &getName() const |
| 55 | { |
| 56 | return *name; |
| 57 | } |
| 58 | virtual const TString &getMangledName() const |
| 59 | { |
| 60 | return getName(); |
| 61 | } |
| 62 | virtual bool isFunction() const |
| 63 | { |
| 64 | return false; |
| 65 | } |
| 66 | virtual bool isVariable() const |
| 67 | { |
| 68 | return false; |
| 69 | } |
| 70 | void setUniqueId(int id) |
| 71 | { |
| 72 | uniqueId = id; |
| 73 | } |
| 74 | int getUniqueId() const |
| 75 | { |
| 76 | return uniqueId; |
| 77 | } |
| 78 | void relateToExtension(const TString &ext) |
| 79 | { |
| 80 | extension = ext; |
| 81 | } |
| 82 | const TString &getExtension() const |
| 83 | { |
| 84 | return extension; |
| 85 | } |
Nicolas Capens | ba60ad3 | 2013-06-04 15:55:47 -0400 | [diff] [blame] | 86 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 87 | private: |
Jamie Madill | 703cdd6 | 2013-07-08 15:07:30 -0400 | [diff] [blame] | 88 | DISALLOW_COPY_AND_ASSIGN(TSymbol); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 89 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 90 | int uniqueId; // For real comparing during code generation |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 91 | const TString *name; |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 92 | TString extension; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 95 | // Variable class, meaning a symbol that's not a function. |
| 96 | // |
| 97 | // There could be a separate class heirarchy for Constant variables; |
| 98 | // Only one of int, bool, or float, (or none) is correct for |
| 99 | // any particular use, but it's easy to do this way, and doesn't |
| 100 | // seem worth having separate classes, and "getConst" can't simply return |
| 101 | // different values for different types polymorphically, so this is |
| 102 | // just simple and pragmatic. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 103 | class TVariable : public TSymbol |
| 104 | { |
| 105 | public: |
| 106 | TVariable(const TString *name, const TType &t, bool uT = false) |
| 107 | : TSymbol(name), |
| 108 | type(t), |
| 109 | userType(uT), |
| 110 | unionArray(0) |
| 111 | { |
| 112 | } |
| 113 | virtual ~TVariable() |
| 114 | { |
| 115 | } |
| 116 | virtual bool isVariable() const |
| 117 | { |
| 118 | return true; |
| 119 | } |
| 120 | TType &getType() |
| 121 | { |
| 122 | return type; |
| 123 | } |
| 124 | const TType &getType() const |
| 125 | { |
| 126 | return type; |
| 127 | } |
| 128 | bool isUserType() const |
| 129 | { |
| 130 | return userType; |
| 131 | } |
| 132 | void setQualifier(TQualifier qualifier) |
| 133 | { |
| 134 | type.setQualifier(qualifier); |
| 135 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 136 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 137 | ConstantUnion *getConstPointer() |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 138 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 139 | if (!unionArray) |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 140 | unionArray = new ConstantUnion[type.getObjectSize()]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 141 | |
| 142 | return unionArray; |
| 143 | } |
| 144 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 145 | ConstantUnion *getConstPointer() const |
| 146 | { |
| 147 | return unionArray; |
| 148 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 149 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 150 | void shareConstPointer(ConstantUnion *constArray) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 151 | { |
alokp@chromium.org | 6c82caf | 2010-10-14 16:08:56 +0000 | [diff] [blame] | 152 | if (unionArray == constArray) |
| 153 | return; |
| 154 | |
| 155 | delete[] unionArray; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 156 | unionArray = constArray; |
| 157 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 158 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 159 | private: |
Jamie Madill | 703cdd6 | 2013-07-08 15:07:30 -0400 | [diff] [blame] | 160 | DISALLOW_COPY_AND_ASSIGN(TVariable); |
| 161 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 162 | TType type; |
| 163 | bool userType; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 164 | // we are assuming that Pool Allocator will free the memory |
| 165 | // allocated to unionArray when this object is destroyed. |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 166 | ConstantUnion *unionArray; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 169 | // The function sub-class of symbols and the parser will need to |
| 170 | // share this definition of a function parameter. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 171 | struct TParameter |
| 172 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 173 | TString *name; |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 174 | TType *type; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 177 | // The function sub-class of a symbol. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 178 | class TFunction : public TSymbol |
| 179 | { |
| 180 | public: |
| 181 | TFunction(TOperator o) |
| 182 | : TSymbol(0), |
| 183 | returnType(TType(EbtVoid, EbpUndefined)), |
| 184 | op(o), |
| 185 | defined(false) |
| 186 | { |
| 187 | } |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 188 | TFunction(const TString *name, const TType &retType, TOperator tOp = EOpNull) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 189 | : TSymbol(name), |
| 190 | returnType(retType), |
| 191 | mangledName(TFunction::mangleName(*name)), |
| 192 | op(tOp), |
| 193 | defined(false) |
| 194 | { |
| 195 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 196 | virtual ~TFunction(); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 197 | virtual bool isFunction() const |
| 198 | { |
| 199 | return true; |
| 200 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 201 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 202 | static TString mangleName(const TString &name) |
| 203 | { |
| 204 | return name + '('; |
| 205 | } |
| 206 | static TString unmangleName(const TString &mangledName) |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 207 | { |
| 208 | return TString(mangledName.c_str(), mangledName.find_first_of('(')); |
| 209 | } |
| 210 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 211 | void addParameter(TParameter &p) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 212 | { |
| 213 | parameters.push_back(p); |
| 214 | mangledName = mangledName + p.type->getMangledName(); |
| 215 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 216 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 217 | const TString &getMangledName() const |
| 218 | { |
| 219 | return mangledName; |
| 220 | } |
| 221 | const TType &getReturnType() const |
| 222 | { |
| 223 | return returnType; |
| 224 | } |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 225 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 226 | void relateToOperator(TOperator o) |
| 227 | { |
| 228 | op = o; |
| 229 | } |
| 230 | TOperator getBuiltInOp() const |
| 231 | { |
| 232 | return op; |
| 233 | } |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 234 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 235 | void setDefined() |
| 236 | { |
| 237 | defined = true; |
| 238 | } |
| 239 | bool isDefined() |
| 240 | { |
| 241 | return defined; |
| 242 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 243 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 244 | size_t getParamCount() const |
| 245 | { |
| 246 | return parameters.size(); |
| 247 | } |
| 248 | const TParameter &getParam(size_t i) const |
| 249 | { |
| 250 | return parameters[i]; |
| 251 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 252 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 253 | private: |
Jamie Madill | 703cdd6 | 2013-07-08 15:07:30 -0400 | [diff] [blame] | 254 | DISALLOW_COPY_AND_ASSIGN(TFunction); |
| 255 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 256 | typedef TVector<TParameter> TParamList; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 257 | TParamList parameters; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 258 | TType returnType; |
| 259 | TString mangledName; |
| 260 | TOperator op; |
| 261 | bool defined; |
| 262 | }; |
| 263 | |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 264 | // Interface block name sub-symbol |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 265 | class TInterfaceBlockName : public TSymbol |
| 266 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 267 | public: |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 268 | TInterfaceBlockName(const TString *name) |
| 269 | : TSymbol(name) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 270 | { |
| 271 | } |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 272 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 273 | virtual ~TInterfaceBlockName() |
| 274 | { |
| 275 | } |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 276 | }; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 277 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 278 | class TSymbolTableLevel |
| 279 | { |
| 280 | public: |
| 281 | typedef TMap<TString, TSymbol *> tLevel; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 282 | typedef tLevel::const_iterator const_iterator; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 283 | typedef const tLevel::value_type tLevelPair; |
| 284 | typedef std::pair<tLevel::iterator, bool> tInsertResult; |
| 285 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 286 | TSymbolTableLevel() |
| 287 | { |
| 288 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 289 | ~TSymbolTableLevel(); |
| 290 | |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 291 | bool insert(TSymbol *symbol); |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 292 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 293 | TSymbol *find(const TString &name) const; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 294 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 295 | void relateToOperator(const char *name, TOperator op); |
| 296 | void relateToExtension(const char *name, const TString &ext); |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 297 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 298 | protected: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 299 | tLevel level; |
| 300 | }; |
| 301 | |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 302 | enum ESymbolLevel |
| 303 | { |
| 304 | COMMON_BUILTINS = 0, |
| 305 | ESSL1_BUILTINS = 1, |
| 306 | ESSL3_BUILTINS = 2, |
| 307 | LAST_BUILTIN_LEVEL = ESSL3_BUILTINS, |
| 308 | GLOBAL_LEVEL = 3 |
| 309 | }; |
| 310 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 311 | class TSymbolTable |
| 312 | { |
| 313 | public: |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 314 | TSymbolTable() |
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 | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 363 | bool insertConstInt(ESymbolLevel level, const char *name, int value) |
| 364 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 365 | TVariable *constant = new TVariable( |
| 366 | NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1)); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 367 | constant->getConstPointer()->setIConst(value); |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 368 | return insert(level, constant); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 369 | } |
| 370 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 371 | void insertBuiltIn(ESymbolLevel level, TType *rvalue, const char *name, |
| 372 | TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, |
| 373 | TType *ptype4 = 0, TType *ptype5 = 0); |
Nicolas Capens | 759b994 | 2014-02-14 17:57:14 -0500 | [diff] [blame] | 374 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 375 | TSymbol *find(const TString &name, int shaderVersion, |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame^] | 376 | bool *builtIn = NULL, bool *sameScope = NULL) const; |
| 377 | TSymbol *findBuiltIn(const TString &name, int shaderVersion) const; |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 378 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 379 | TSymbolTableLevel *getOuterLevel() |
| 380 | { |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 381 | assert(currentLevel() >= 1); |
daniel@transgaming.com | 5dd6d09 | 2012-03-20 20:10:28 +0000 | [diff] [blame] | 382 | return table[currentLevel() - 1]; |
| 383 | } |
| 384 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 385 | void relateToOperator(ESymbolLevel level, const char *name, TOperator op) |
| 386 | { |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 387 | table[level]->relateToOperator(name, op); |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 388 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 389 | void relateToExtension(ESymbolLevel level, const char *name, const TString &ext) |
| 390 | { |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 391 | table[level]->relateToExtension(name, ext); |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 392 | } |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 393 | void dump(TInfoSink &infoSink) const; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 394 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 395 | bool setDefaultPrecision(const TPublicType &type, TPrecision prec) |
| 396 | { |
Zhenyao Mo | ed14b79 | 2014-05-08 11:21:07 -0700 | [diff] [blame] | 397 | if (!SupportsPrecision(type.type)) |
Zhenyao Mo | a5a1dfc | 2013-09-23 14:57:03 -0400 | [diff] [blame] | 398 | return false; |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 399 | if (type.isAggregate()) |
shannon.woods@transgaming.com | d25a6b3 | 2013-02-28 23:19:13 +0000 | [diff] [blame] | 400 | return false; // Not allowed to set for aggregate types |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 401 | int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 402 | // Uses map operator [], overwrites the current value |
| 403 | (*precisionStack[indexOfLastElement])[type.type] = prec; |
shannon.woods@transgaming.com | d25a6b3 | 2013-02-28 23:19:13 +0000 | [diff] [blame] | 404 | return true; |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 407 | // Searches down the precisionStack for a precision qualifier |
| 408 | // for the specified TBasicType |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame^] | 409 | TPrecision getDefaultPrecision(TBasicType type) const; |
shannonwoods@chromium.org | 6b70991 | 2013-05-30 00:20:04 +0000 | [diff] [blame] | 410 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 411 | static int nextUniqueId() |
| 412 | { |
| 413 | return ++uniqueIdCounter; |
| 414 | } |
| 415 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 416 | private: |
| 417 | ESymbolLevel currentLevel() const |
| 418 | { |
| 419 | return static_cast<ESymbolLevel>(table.size() - 1); |
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 | std::vector<TSymbolTableLevel *> table; |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 423 | typedef TMap<TBasicType, TPrecision> PrecisionStackLevel; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 424 | std::vector< PrecisionStackLevel *> precisionStack; |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 425 | |
| 426 | static int uniqueIdCounter; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | #endif // _SYMBOL_TABLE_INCLUDED_ |