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 | // |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 21 | // * Pushing and popping of scope, so symbol table will really be a stack |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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" |
Olli Etuaho | 2a1e8f9 | 2017-07-14 11:49:36 +0300 | [diff] [blame] | 38 | #include "compiler/translator/ExtensionBehavior.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 39 | #include "compiler/translator/InfoSink.h" |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 40 | #include "compiler/translator/IntermNode.h" |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 41 | #include "compiler/translator/StaticType.h" |
Olli Etuaho | d4529f3 | 2017-12-12 13:06:40 +0200 | [diff] [blame] | 42 | #include "compiler/translator/Symbol.h" |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 43 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 44 | namespace sh |
| 45 | { |
| 46 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 47 | class TSymbolTableLevel |
| 48 | { |
| 49 | public: |
Kai Ninomiya | d4556df | 2017-09-27 16:45:22 -0700 | [diff] [blame] | 50 | typedef TUnorderedMap<TString, TSymbol *> tLevel; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 51 | typedef tLevel::const_iterator const_iterator; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 52 | typedef const tLevel::value_type tLevelPair; |
| 53 | typedef std::pair<tLevel::iterator, bool> tInsertResult; |
| 54 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 55 | TSymbolTableLevel() : mGlobalInvariant(false) {} |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 56 | ~TSymbolTableLevel(); |
| 57 | |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 58 | bool insert(TSymbol *symbol); |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 59 | |
Olli Etuaho | b2983c9 | 2015-03-18 14:02:46 +0200 | [diff] [blame] | 60 | // Insert a function using its unmangled name as the key. |
| 61 | bool insertUnmangled(TFunction *function); |
| 62 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 63 | TSymbol *find(const TString &name) const; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 64 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 65 | void addInvariantVarying(const std::string &name) { mInvariantVaryings.insert(name); } |
Qiankun Miao | f69682b | 2016-08-16 14:50:42 +0800 | [diff] [blame] | 66 | |
| 67 | bool isVaryingInvariant(const std::string &name) |
| 68 | { |
| 69 | return (mGlobalInvariant || mInvariantVaryings.count(name) > 0); |
| 70 | } |
| 71 | |
| 72 | void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; } |
| 73 | |
Olli Etuaho | 342b83d | 2018-01-10 13:24:01 +0200 | [diff] [blame] | 74 | void insertUnmangledBuiltInName(const char *name); |
| 75 | bool hasUnmangledBuiltIn(const char *name) const; |
Martin Radev | da6254b | 2016-12-14 17:00:36 +0200 | [diff] [blame] | 76 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 77 | protected: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 78 | tLevel level; |
Qiankun Miao | f69682b | 2016-08-16 14:50:42 +0800 | [diff] [blame] | 79 | std::set<std::string> mInvariantVaryings; |
| 80 | bool mGlobalInvariant; |
Martin Radev | da6254b | 2016-12-14 17:00:36 +0200 | [diff] [blame] | 81 | |
| 82 | private: |
Olli Etuaho | 342b83d | 2018-01-10 13:24:01 +0200 | [diff] [blame] | 83 | struct CharArrayComparator |
| 84 | { |
| 85 | bool operator()(const char *a, const char *b) const { return strcmp(a, b) < 0; } |
| 86 | }; |
| 87 | std::set<const char *, CharArrayComparator> mUnmangledBuiltInNames; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Gus Fernandez | 964df49 | 2014-10-13 11:54:39 -0700 | [diff] [blame] | 90 | // Define ESymbolLevel as int rather than an enum since level can go |
| 91 | // above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the |
| 92 | // compiler optimizes the >= of the last element to ==. |
| 93 | typedef int ESymbolLevel; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 94 | const int COMMON_BUILTINS = 0; |
| 95 | const int ESSL1_BUILTINS = 1; |
| 96 | const int ESSL3_BUILTINS = 2; |
Martin Radev | e93d24e | 2016-07-28 12:06:05 +0300 | [diff] [blame] | 97 | const int ESSL3_1_BUILTINS = 3; |
Olli Etuaho | 977ee7e | 2017-07-21 11:38:27 +0300 | [diff] [blame] | 98 | // GLSL_BUILTINS are desktop GLSL builtins that don't exist in ESSL but are used to implement |
| 99 | // features in ANGLE's GLSL backend. They're not visible to the parser. |
| 100 | const int GLSL_BUILTINS = 4; |
| 101 | const int LAST_BUILTIN_LEVEL = GLSL_BUILTINS; |
| 102 | const int GLOBAL_LEVEL = 5; |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 103 | |
Jamie Madill | f0d10f8 | 2015-03-31 12:56:52 -0400 | [diff] [blame] | 104 | class TSymbolTable : angle::NonCopyable |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 105 | { |
| 106 | public: |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame] | 107 | TSymbolTable() : mUniqueIdCounter(0), mUserDefinedUniqueIdsStart(-1) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 108 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 109 | // The symbol table cannot be used until push() is called, but |
| 110 | // the lack of an initial call to push() can be used to detect |
| 111 | // that the symbol table has not been preloaded with built-ins. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 114 | ~TSymbolTable(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 115 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 116 | // When the symbol table is initialized with the built-ins, there should |
| 117 | // 'push' calls, so that built-ins are at level 0 and the shader |
| 118 | // globals are at level 1. |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 119 | bool isEmpty() const { return table.empty(); } |
| 120 | bool atBuiltInLevel() const { return currentLevel() <= LAST_BUILTIN_LEVEL; } |
| 121 | bool atGlobalLevel() const { return currentLevel() == GLOBAL_LEVEL; } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 122 | void push() |
alokp@chromium.org | e4249f0 | 2010-07-26 18:13:52 +0000 | [diff] [blame] | 123 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 124 | table.push_back(new TSymbolTableLevel); |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 125 | precisionStack.push_back(new PrecisionStackLevel); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 126 | } |
| 127 | |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 128 | void pop() |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 129 | { |
| 130 | delete table.back(); |
| 131 | table.pop_back(); |
| 132 | |
| 133 | delete precisionStack.back(); |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 134 | precisionStack.pop_back(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 137 | // The declare* entry points are used when parsing and declare symbols at the current scope. |
Olli Etuaho | 035419f | 2017-11-28 14:27:15 +0200 | [diff] [blame] | 138 | // They return the created symbol / true in case the declaration was successful, and nullptr / |
| 139 | // false if the declaration failed due to redefinition. |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame] | 140 | bool declareVariable(TVariable *variable); |
Olli Etuaho | 035419f | 2017-11-28 14:27:15 +0200 | [diff] [blame] | 141 | bool declareStructType(TStructure *str); |
Olli Etuaho | 378c3a5 | 2017-12-04 11:32:13 +0200 | [diff] [blame] | 142 | bool declareInterfaceBlock(TInterfaceBlock *interfaceBlock); |
shannonwoods@chromium.org | 1c84809 | 2013-05-30 00:02:34 +0000 | [diff] [blame] | 143 | |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 144 | // The insert* entry points are used when initializing the symbol table with built-ins. |
Olli Etuaho | 035419f | 2017-11-28 14:27:15 +0200 | [diff] [blame] | 145 | // They return the created symbol / true in case the declaration was successful, and nullptr / |
| 146 | // false if the declaration failed due to redefinition. |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 147 | TVariable *insertVariable(ESymbolLevel level, const char *name, const TType *type); |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 148 | TVariable *insertVariableExt(ESymbolLevel level, |
Olli Etuaho | 2a1e8f9 | 2017-07-14 11:49:36 +0300 | [diff] [blame] | 149 | TExtension ext, |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 150 | const char *name, |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 151 | const TType *type); |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame] | 152 | bool insertVariable(ESymbolLevel level, TVariable *variable); |
Olli Etuaho | 035419f | 2017-11-28 14:27:15 +0200 | [diff] [blame] | 153 | bool insertStructType(ESymbolLevel level, TStructure *str); |
Olli Etuaho | 378c3a5 | 2017-12-04 11:32:13 +0200 | [diff] [blame] | 154 | bool insertInterfaceBlock(ESymbolLevel level, TInterfaceBlock *interfaceBlock); |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 155 | |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 156 | template <TPrecision precision> |
| 157 | bool insertConstInt(ESymbolLevel level, const char *name, int value); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 158 | |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 159 | template <TPrecision precision> |
| 160 | bool insertConstIntExt(ESymbolLevel level, TExtension ext, const char *name, int value); |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 161 | |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 162 | template <TPrecision precision> |
| 163 | bool insertConstIvec3(ESymbolLevel level, const char *name, const std::array<int, 3> &values); |
Martin Radev | e93d24e | 2016-07-28 12:06:05 +0300 | [diff] [blame] | 164 | |
Olli Etuaho | 342b83d | 2018-01-10 13:24:01 +0200 | [diff] [blame] | 165 | // Note that for inserted built-in functions the const char *name needs to remain valid for the |
| 166 | // lifetime of the SymbolTable. SymbolTable does not allocate a copy of it. |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 167 | void insertBuiltIn(ESymbolLevel level, |
| 168 | TOperator op, |
Olli Etuaho | 2a1e8f9 | 2017-07-14 11:49:36 +0300 | [diff] [blame] | 169 | TExtension ext, |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 170 | const TType *rvalue, |
| 171 | const char *name, |
| 172 | const TType *ptype1, |
| 173 | const TType *ptype2 = 0, |
| 174 | const TType *ptype3 = 0, |
| 175 | const TType *ptype4 = 0, |
| 176 | const TType *ptype5 = 0); |
Nicolas Capens | 759b994 | 2014-02-14 17:57:14 -0500 | [diff] [blame] | 177 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 178 | void insertBuiltIn(ESymbolLevel level, |
| 179 | const TType *rvalue, |
| 180 | const char *name, |
| 181 | const TType *ptype1, |
| 182 | const TType *ptype2 = 0, |
| 183 | const TType *ptype3 = 0, |
| 184 | const TType *ptype4 = 0, |
| 185 | const TType *ptype5 = 0) |
Nicolas Capens | 482907e | 2015-02-23 16:56:33 -0500 | [diff] [blame] | 186 | { |
Martin Radev | da6254b | 2016-12-14 17:00:36 +0200 | [diff] [blame] | 187 | insertUnmangledBuiltInName(name, level); |
Olli Etuaho | 2a1e8f9 | 2017-07-14 11:49:36 +0300 | [diff] [blame] | 188 | insertBuiltIn(level, EOpNull, TExtension::UNDEFINED, rvalue, name, ptype1, ptype2, ptype3, |
| 189 | ptype4, ptype5); |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 190 | } |
| 191 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 192 | void insertBuiltIn(ESymbolLevel level, |
Olli Etuaho | 2a1e8f9 | 2017-07-14 11:49:36 +0300 | [diff] [blame] | 193 | TExtension ext, |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 194 | const TType *rvalue, |
| 195 | const char *name, |
| 196 | const TType *ptype1, |
| 197 | const TType *ptype2 = 0, |
| 198 | const TType *ptype3 = 0, |
| 199 | const TType *ptype4 = 0, |
| 200 | const TType *ptype5 = 0) |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 201 | { |
Martin Radev | da6254b | 2016-12-14 17:00:36 +0200 | [diff] [blame] | 202 | insertUnmangledBuiltInName(name, level); |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 203 | insertBuiltIn(level, EOpNull, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5); |
| 204 | } |
| 205 | |
Olli Etuaho | 492cfab | 2017-01-20 21:18:29 +0000 | [diff] [blame] | 206 | void insertBuiltInOp(ESymbolLevel level, |
| 207 | TOperator op, |
| 208 | const TType *rvalue, |
| 209 | const TType *ptype1, |
| 210 | const TType *ptype2 = 0, |
| 211 | const TType *ptype3 = 0, |
| 212 | const TType *ptype4 = 0, |
| 213 | const TType *ptype5 = 0); |
| 214 | |
| 215 | void insertBuiltInOp(ESymbolLevel level, |
| 216 | TOperator op, |
Olli Etuaho | 2a1e8f9 | 2017-07-14 11:49:36 +0300 | [diff] [blame] | 217 | TExtension ext, |
Olli Etuaho | 492cfab | 2017-01-20 21:18:29 +0000 | [diff] [blame] | 218 | const TType *rvalue, |
| 219 | const TType *ptype1, |
| 220 | const TType *ptype2 = 0, |
| 221 | const TType *ptype3 = 0, |
| 222 | const TType *ptype4 = 0, |
| 223 | const TType *ptype5 = 0); |
Nicolas Capens | 482907e | 2015-02-23 16:56:33 -0500 | [diff] [blame] | 224 | |
Martin Radev | d7c5b0a | 2016-07-27 14:04:43 +0300 | [diff] [blame] | 225 | void insertBuiltInFunctionNoParameters(ESymbolLevel level, |
| 226 | TOperator op, |
| 227 | const TType *rvalue, |
| 228 | const char *name); |
| 229 | |
Jiawei Shao | d27f5c8 | 2017-08-23 09:38:08 +0800 | [diff] [blame] | 230 | void insertBuiltInFunctionNoParametersExt(ESymbolLevel level, |
Olli Etuaho | 2a1e8f9 | 2017-07-14 11:49:36 +0300 | [diff] [blame] | 231 | TExtension ext, |
Jiawei Shao | d27f5c8 | 2017-08-23 09:38:08 +0800 | [diff] [blame] | 232 | TOperator op, |
| 233 | const TType *rvalue, |
| 234 | const char *name); |
| 235 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 236 | TSymbol *find(const TString &name, |
| 237 | int shaderVersion, |
Yunchao He | 4f28544 | 2017-04-21 12:15:49 +0800 | [diff] [blame] | 238 | bool *builtIn = nullptr, |
| 239 | bool *sameScope = nullptr) const; |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 240 | |
| 241 | TSymbol *findGlobal(const TString &name) const; |
| 242 | |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 243 | TSymbol *findBuiltIn(const TString &name, int shaderVersion) const; |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 244 | |
Olli Etuaho | 977ee7e | 2017-07-21 11:38:27 +0300 | [diff] [blame] | 245 | TSymbol *findBuiltIn(const TString &name, int shaderVersion, bool includeGLSLBuiltins) const; |
| 246 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 247 | TSymbolTableLevel *getOuterLevel() |
| 248 | { |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 249 | assert(currentLevel() >= 1); |
daniel@transgaming.com | 5dd6d09 | 2012-03-20 20:10:28 +0000 | [diff] [blame] | 250 | return table[currentLevel() - 1]; |
| 251 | } |
| 252 | |
Olli Etuaho | cce8965 | 2017-06-19 16:04:09 +0300 | [diff] [blame] | 253 | void setDefaultPrecision(TBasicType type, TPrecision prec) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 254 | { |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 255 | int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 256 | // Uses map operator [], overwrites the current value |
Olli Etuaho | cce8965 | 2017-06-19 16:04:09 +0300 | [diff] [blame] | 257 | (*precisionStack[indexOfLastElement])[type] = prec; |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 260 | // Searches down the precisionStack for a precision qualifier |
| 261 | // for the specified TBasicType |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 262 | TPrecision getDefaultPrecision(TBasicType type) const; |
shannonwoods@chromium.org | 6b70991 | 2013-05-30 00:20:04 +0000 | [diff] [blame] | 263 | |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 264 | // This records invariant varyings declared through |
| 265 | // "invariant varying_name;". |
Jamie Madill | 2c43325 | 2014-12-03 12:36:54 -0500 | [diff] [blame] | 266 | void addInvariantVarying(const std::string &originalName) |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 267 | { |
Qiankun Miao | f69682b | 2016-08-16 14:50:42 +0800 | [diff] [blame] | 268 | ASSERT(atGlobalLevel()); |
| 269 | table[currentLevel()]->addInvariantVarying(originalName); |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 270 | } |
| 271 | // If this returns false, the varying could still be invariant |
| 272 | // if it is set as invariant during the varying variable |
| 273 | // declaration - this piece of information is stored in the |
| 274 | // variable's type, not here. |
Jamie Madill | 2c43325 | 2014-12-03 12:36:54 -0500 | [diff] [blame] | 275 | bool isVaryingInvariant(const std::string &originalName) const |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 276 | { |
Qiankun Miao | f69682b | 2016-08-16 14:50:42 +0800 | [diff] [blame] | 277 | ASSERT(atGlobalLevel()); |
| 278 | return table[currentLevel()]->isVaryingInvariant(originalName); |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Qiankun Miao | f69682b | 2016-08-16 14:50:42 +0800 | [diff] [blame] | 281 | void setGlobalInvariant(bool invariant) |
| 282 | { |
| 283 | ASSERT(atGlobalLevel()); |
| 284 | table[currentLevel()]->setGlobalInvariant(invariant); |
| 285 | } |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 286 | |
Olli Etuaho | 2d88e9b | 2017-07-21 16:52:03 +0300 | [diff] [blame] | 287 | const TSymbolUniqueId nextUniqueId() { return TSymbolUniqueId(this); } |
| 288 | |
Martin Radev | da6254b | 2016-12-14 17:00:36 +0200 | [diff] [blame] | 289 | // Checks whether there is a built-in accessible by a shader with the specified version. |
| 290 | bool hasUnmangledBuiltInForShaderVersion(const char *name, int shaderVersion); |
Olli Etuaho | c4a96d6 | 2015-07-23 17:37:39 +0530 | [diff] [blame] | 291 | |
Olli Etuaho | 5d69db1 | 2017-11-24 16:51:15 +0200 | [diff] [blame] | 292 | void markBuiltInInitializationFinished(); |
| 293 | void clearCompilationResults(); |
| 294 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 295 | private: |
Olli Etuaho | 2d88e9b | 2017-07-21 16:52:03 +0300 | [diff] [blame] | 296 | friend class TSymbolUniqueId; |
Olli Etuaho | 5d69db1 | 2017-11-24 16:51:15 +0200 | [diff] [blame] | 297 | int nextUniqueIdValue(); |
Olli Etuaho | 2d88e9b | 2017-07-21 16:52:03 +0300 | [diff] [blame] | 298 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 299 | ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); } |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 300 | |
Olli Etuaho | 9d4d7f0 | 2017-12-07 17:11:41 +0100 | [diff] [blame] | 301 | TVariable *insertVariable(ESymbolLevel level, |
| 302 | const TString *name, |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 303 | const TType *type, |
Olli Etuaho | 9d4d7f0 | 2017-12-07 17:11:41 +0100 | [diff] [blame] | 304 | SymbolType symbolType); |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 305 | |
Olli Etuaho | 5d69db1 | 2017-11-24 16:51:15 +0200 | [diff] [blame] | 306 | bool insert(ESymbolLevel level, TSymbol *symbol) |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 307 | { |
Olli Etuaho | 5d69db1 | 2017-11-24 16:51:15 +0200 | [diff] [blame] | 308 | ASSERT(level > LAST_BUILTIN_LEVEL || mUserDefinedUniqueIdsStart == -1); |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 309 | return table[level]->insert(symbol); |
| 310 | } |
| 311 | |
Martin Radev | da6254b | 2016-12-14 17:00:36 +0200 | [diff] [blame] | 312 | // Used to insert unmangled functions to check redeclaration of built-ins in ESSL 3.00 and |
| 313 | // above. |
| 314 | void insertUnmangledBuiltInName(const char *name, ESymbolLevel level); |
| 315 | |
| 316 | bool hasUnmangledBuiltInAtLevel(const char *name, ESymbolLevel level); |
Olli Etuaho | c4a96d6 | 2015-07-23 17:37:39 +0530 | [diff] [blame] | 317 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 318 | std::vector<TSymbolTableLevel *> table; |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 319 | typedef TMap<TBasicType, TPrecision> PrecisionStackLevel; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 320 | std::vector<PrecisionStackLevel *> precisionStack; |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 321 | |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame] | 322 | int mUniqueIdCounter; |
Olli Etuaho | 2d88e9b | 2017-07-21 16:52:03 +0300 | [diff] [blame] | 323 | |
Olli Etuaho | 5d69db1 | 2017-11-24 16:51:15 +0200 | [diff] [blame] | 324 | // -1 before built-in init has finished, one past the last built-in id afterwards. |
| 325 | // TODO(oetuaho): Make this a compile-time constant once the symbol table is initialized at |
| 326 | // compile time. http://anglebug.com/1432 |
| 327 | int mUserDefinedUniqueIdsStart; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 328 | }; |
| 329 | |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 330 | template <TPrecision precision> |
| 331 | bool TSymbolTable::insertConstInt(ESymbolLevel level, const char *name, int value) |
| 332 | { |
| 333 | TVariable *constant = |
| 334 | new TVariable(this, NewPoolTString(name), |
| 335 | StaticType::Get<EbtInt, precision, EvqConst, 1, 1>(), SymbolType::BuiltIn); |
| 336 | TConstantUnion *unionArray = new TConstantUnion[1]; |
| 337 | unionArray[0].setIConst(value); |
| 338 | constant->shareConstPointer(unionArray); |
| 339 | return insert(level, constant); |
| 340 | } |
| 341 | |
| 342 | template <TPrecision precision> |
| 343 | bool TSymbolTable::insertConstIntExt(ESymbolLevel level, |
| 344 | TExtension ext, |
| 345 | const char *name, |
| 346 | int value) |
| 347 | { |
| 348 | TVariable *constant = new TVariable(this, NewPoolTString(name), |
| 349 | StaticType::Get<EbtInt, precision, EvqConst, 1, 1>(), |
| 350 | SymbolType::BuiltIn, ext); |
| 351 | TConstantUnion *unionArray = new TConstantUnion[1]; |
| 352 | unionArray[0].setIConst(value); |
| 353 | constant->shareConstPointer(unionArray); |
| 354 | return insert(level, constant); |
| 355 | } |
| 356 | |
| 357 | template <TPrecision precision> |
| 358 | bool TSymbolTable::insertConstIvec3(ESymbolLevel level, |
| 359 | const char *name, |
| 360 | const std::array<int, 3> &values) |
| 361 | { |
| 362 | TVariable *constantIvec3 = |
| 363 | new TVariable(this, NewPoolTString(name), |
| 364 | StaticType::Get<EbtInt, precision, EvqConst, 3, 1>(), SymbolType::BuiltIn); |
| 365 | |
| 366 | TConstantUnion *unionArray = new TConstantUnion[3]; |
| 367 | for (size_t index = 0u; index < 3u; ++index) |
| 368 | { |
| 369 | unionArray[index].setIConst(values[index]); |
| 370 | } |
| 371 | constantIvec3->shareConstPointer(unionArray); |
| 372 | |
| 373 | return insert(level, constantIvec3); |
| 374 | } |
| 375 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 376 | } // namespace sh |
| 377 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 378 | #endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_ |