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