daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 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 | // |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 6 | // Symbol table for parsing. The design principles and most of the functionality are documented in |
| 7 | // the header file. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 8 | // |
| 9 | |
apatrick@chromium.org | e057c5d | 2012-01-26 19:18:24 +0000 | [diff] [blame] | 10 | #if defined(_MSC_VER) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 11 | #pragma warning(disable : 4718) |
apatrick@chromium.org | e057c5d | 2012-01-26 19:18:24 +0000 | [diff] [blame] | 12 | #endif |
| 13 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 14 | #include "compiler/translator/SymbolTable.h" |
Olli Etuaho | 01d0ad0 | 2017-01-22 14:51:23 -0800 | [diff] [blame] | 15 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | #include <set> |
| 18 | |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 19 | #include "angle_gl.h" |
Olli Etuaho | 391bda2 | 2018-02-23 11:43:14 +0200 | [diff] [blame] | 20 | #include "compiler/translator/BuiltIn_autogen.h" |
Olli Etuaho | 2d8e432 | 2018-01-22 14:12:46 +0200 | [diff] [blame] | 21 | #include "compiler/translator/ImmutableString.h" |
Olli Etuaho | 01d0ad0 | 2017-01-22 14:51:23 -0800 | [diff] [blame] | 22 | #include "compiler/translator/IntermNode.h" |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 23 | #include "compiler/translator/StaticType.h" |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 24 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 25 | namespace sh |
| 26 | { |
| 27 | |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 28 | class TSymbolTable::TSymbolTableLevel |
| 29 | { |
| 30 | public: |
| 31 | TSymbolTableLevel() : mGlobalInvariant(false) {} |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 32 | |
| 33 | bool insert(TSymbol *symbol); |
| 34 | |
| 35 | // Insert a function using its unmangled name as the key. |
Olli Etuaho | b92f92a | 2018-02-15 19:14:59 +0200 | [diff] [blame] | 36 | void insertUnmangled(TFunction *function); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 37 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 38 | TSymbol *find(const ImmutableString &name) const; |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 39 | |
Olli Etuaho | defe393 | 2018-02-13 11:56:09 +0200 | [diff] [blame] | 40 | void addInvariantVarying(const ImmutableString &name) { mInvariantVaryings.insert(name); } |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 41 | |
Olli Etuaho | defe393 | 2018-02-13 11:56:09 +0200 | [diff] [blame] | 42 | bool isVaryingInvariant(const ImmutableString &name) |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 43 | { |
| 44 | return (mGlobalInvariant || mInvariantVaryings.count(name) > 0); |
| 45 | } |
| 46 | |
| 47 | void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; } |
| 48 | |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 49 | private: |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 50 | using tLevel = TUnorderedMap<ImmutableString, |
| 51 | TSymbol *, |
| 52 | ImmutableString::FowlerNollVoHash<sizeof(size_t)>>; |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 53 | using tLevelPair = const tLevel::value_type; |
| 54 | using tInsertResult = std::pair<tLevel::iterator, bool>; |
| 55 | |
| 56 | tLevel level; |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 57 | |
Olli Etuaho | defe393 | 2018-02-13 11:56:09 +0200 | [diff] [blame] | 58 | std::set<ImmutableString> mInvariantVaryings; |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 59 | bool mGlobalInvariant; |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | class TSymbolTable::TSymbolTableBuiltInLevel |
| 63 | { |
| 64 | public: |
| 65 | TSymbolTableBuiltInLevel() = default; |
| 66 | |
Olli Etuaho | 065aa86 | 2018-02-22 15:30:27 +0200 | [diff] [blame] | 67 | void insert(const TSymbol *symbol); |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 68 | const TSymbol *find(const ImmutableString &name) const; |
| 69 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 70 | private: |
| 71 | using tLevel = TUnorderedMap<ImmutableString, |
| 72 | const TSymbol *, |
| 73 | ImmutableString::FowlerNollVoHash<sizeof(size_t)>>; |
| 74 | using tLevelPair = const tLevel::value_type; |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 75 | tLevel mLevel; |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 76 | }; |
| 77 | |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 78 | bool TSymbolTable::TSymbolTableLevel::insert(TSymbol *symbol) |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 79 | { |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 80 | // returning true means symbol was added to the table |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 81 | tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol)); |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 82 | return result.second; |
| 83 | } |
| 84 | |
Olli Etuaho | b92f92a | 2018-02-15 19:14:59 +0200 | [diff] [blame] | 85 | void TSymbolTable::TSymbolTableLevel::insertUnmangled(TFunction *function) |
Olli Etuaho | b2983c9 | 2015-03-18 14:02:46 +0200 | [diff] [blame] | 86 | { |
Olli Etuaho | b92f92a | 2018-02-15 19:14:59 +0200 | [diff] [blame] | 87 | level.insert(tLevelPair(function->name(), function)); |
Olli Etuaho | b2983c9 | 2015-03-18 14:02:46 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 90 | TSymbol *TSymbolTable::TSymbolTableLevel::find(const ImmutableString &name) const |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 91 | { |
| 92 | tLevel::const_iterator it = level.find(name); |
| 93 | if (it == level.end()) |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 94 | return nullptr; |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 95 | else |
| 96 | return (*it).second; |
| 97 | } |
| 98 | |
Olli Etuaho | 065aa86 | 2018-02-22 15:30:27 +0200 | [diff] [blame] | 99 | void TSymbolTable::TSymbolTableBuiltInLevel::insert(const TSymbol *symbol) |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 100 | { |
Olli Etuaho | 065aa86 | 2018-02-22 15:30:27 +0200 | [diff] [blame] | 101 | mLevel.insert(tLevelPair(symbol->getMangledName(), symbol)); |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 104 | const TSymbol *TSymbolTable::TSymbolTableBuiltInLevel::find(const ImmutableString &name) const |
| 105 | { |
| 106 | tLevel::const_iterator it = mLevel.find(name); |
| 107 | if (it == mLevel.end()) |
| 108 | return nullptr; |
| 109 | else |
| 110 | return (*it).second; |
| 111 | } |
| 112 | |
Olli Etuaho | 391bda2 | 2018-02-23 11:43:14 +0200 | [diff] [blame] | 113 | TSymbolTable::TSymbolTable() : mUniqueIdCounter(0), mShaderType(GL_FRAGMENT_SHADER) |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 114 | { |
| 115 | } |
| 116 | |
| 117 | TSymbolTable::~TSymbolTable() = default; |
| 118 | |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 119 | bool TSymbolTable::isEmpty() const |
| 120 | { |
| 121 | return mTable.empty(); |
| 122 | } |
| 123 | |
| 124 | bool TSymbolTable::atGlobalLevel() const |
| 125 | { |
| 126 | return mTable.size() == 1u; |
| 127 | } |
| 128 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 129 | void TSymbolTable::pushBuiltInLevel() |
| 130 | { |
| 131 | mBuiltInTable.push_back( |
| 132 | std::unique_ptr<TSymbolTableBuiltInLevel>(new TSymbolTableBuiltInLevel)); |
| 133 | } |
| 134 | |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 135 | void TSymbolTable::push() |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 136 | { |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 137 | mTable.push_back(std::unique_ptr<TSymbolTableLevel>(new TSymbolTableLevel)); |
| 138 | mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel)); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 139 | } |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 140 | |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 141 | void TSymbolTable::pop() |
| 142 | { |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 143 | mTable.pop_back(); |
| 144 | mPrecisionStack.pop_back(); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 147 | const TFunction *TSymbolTable::markFunctionHasPrototypeDeclaration( |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 148 | const ImmutableString &mangledName, |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 149 | bool *hadPrototypeDeclarationOut) |
| 150 | { |
| 151 | TFunction *function = findUserDefinedFunction(mangledName); |
| 152 | *hadPrototypeDeclarationOut = function->hasPrototypeDeclaration(); |
| 153 | function->setHasPrototypeDeclaration(); |
| 154 | return function; |
| 155 | } |
| 156 | |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 157 | const TFunction *TSymbolTable::setFunctionParameterNamesFromDefinition(const TFunction *function, |
| 158 | bool *wasDefinedOut) |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 159 | { |
| 160 | TFunction *firstDeclaration = findUserDefinedFunction(function->getMangledName()); |
| 161 | ASSERT(firstDeclaration); |
| 162 | // Note: 'firstDeclaration' could be 'function' if this is the first time we've seen function as |
| 163 | // it would have just been put in the symbol table. Otherwise, we're looking up an earlier |
| 164 | // occurance. |
| 165 | if (function != firstDeclaration) |
| 166 | { |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame] | 167 | // The previous declaration should have the same parameters as the function definition |
| 168 | // (parameter names may differ). |
| 169 | firstDeclaration->shareParameters(*function); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | *wasDefinedOut = firstDeclaration->isDefined(); |
| 173 | firstDeclaration->setDefined(); |
| 174 | return firstDeclaration; |
| 175 | } |
| 176 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 177 | const TSymbol *TSymbolTable::find(const ImmutableString &name, int shaderVersion) const |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 178 | { |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 179 | int userDefinedLevel = static_cast<int>(mTable.size()) - 1; |
| 180 | while (userDefinedLevel >= 0) |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 181 | { |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 182 | const TSymbol *symbol = mTable[userDefinedLevel]->find(name); |
| 183 | if (symbol) |
| 184 | { |
| 185 | return symbol; |
| 186 | } |
| 187 | userDefinedLevel--; |
| 188 | } |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 189 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 190 | return findBuiltIn(name, shaderVersion, false); |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 193 | TFunction *TSymbolTable::findUserDefinedFunction(const ImmutableString &name) const |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 194 | { |
| 195 | // User-defined functions are always declared at the global level. |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 196 | ASSERT(!mTable.empty()); |
| 197 | return static_cast<TFunction *>(mTable[0]->find(name)); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 200 | const TSymbol *TSymbolTable::findGlobal(const ImmutableString &name) const |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 201 | { |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 202 | ASSERT(!mTable.empty()); |
| 203 | return mTable[0]->find(name); |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 206 | const TSymbol *TSymbolTable::findBuiltIn(const ImmutableString &name, int shaderVersion) const |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 207 | { |
Olli Etuaho | 977ee7e | 2017-07-21 11:38:27 +0300 | [diff] [blame] | 208 | return findBuiltIn(name, shaderVersion, false); |
| 209 | } |
| 210 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 211 | const TSymbol *TSymbolTable::findBuiltIn(const ImmutableString &name, |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 212 | int shaderVersion, |
| 213 | bool includeGLSLBuiltins) const |
Olli Etuaho | 977ee7e | 2017-07-21 11:38:27 +0300 | [diff] [blame] | 214 | { |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 215 | for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--) |
| 216 | { |
Olli Etuaho | 977ee7e | 2017-07-21 11:38:27 +0300 | [diff] [blame] | 217 | if (level == GLSL_BUILTINS && !includeGLSLBuiltins) |
| 218 | level--; |
Martin Radev | e93d24e | 2016-07-28 12:06:05 +0300 | [diff] [blame] | 219 | if (level == ESSL3_1_BUILTINS && shaderVersion != 310) |
| 220 | level--; |
| 221 | if (level == ESSL3_BUILTINS && shaderVersion < 300) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 222 | level--; |
| 223 | if (level == ESSL1_BUILTINS && shaderVersion != 100) |
| 224 | level--; |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 225 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 226 | const TSymbol *symbol = mBuiltInTable[level]->find(name); |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 227 | |
| 228 | if (symbol) |
| 229 | return symbol; |
| 230 | } |
| 231 | |
Olli Etuaho | 977ee7e | 2017-07-21 11:38:27 +0300 | [diff] [blame] | 232 | return nullptr; |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 233 | } |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 234 | |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 235 | |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 236 | bool TSymbolTable::declare(TSymbol *symbol) |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 237 | { |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 238 | ASSERT(!mTable.empty()); |
| 239 | ASSERT(symbol->symbolType() == SymbolType::UserDefined); |
| 240 | ASSERT(!symbol->isFunction()); |
| 241 | return mTable.back()->insert(symbol); |
Jiawei Shao | d8105a0 | 2017-08-08 09:54:36 +0800 | [diff] [blame] | 242 | } |
| 243 | |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 244 | void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUnmangledName) |
| 245 | { |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 246 | ASSERT(!mTable.empty()); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 247 | if (insertUnmangledName) |
| 248 | { |
| 249 | // Insert the unmangled name to detect potential future redefinition as a variable. |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 250 | mTable[0]->insertUnmangled(function); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 251 | } |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 252 | mTable[0]->insert(function); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 255 | void TSymbolTable::insertBuiltIn(ESymbolLevel level, const TSymbol *symbol) |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame] | 256 | { |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 257 | ASSERT(symbol); |
| 258 | ASSERT(level <= LAST_BUILTIN_LEVEL); |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame] | 259 | |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 260 | mBuiltInTable[level]->insert(symbol); |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 261 | } |
| 262 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 263 | void TSymbolTable::setDefaultPrecision(TBasicType type, TPrecision prec) |
| 264 | { |
| 265 | int indexOfLastElement = static_cast<int>(mPrecisionStack.size()) - 1; |
| 266 | // Uses map operator [], overwrites the current value |
| 267 | (*mPrecisionStack[indexOfLastElement])[type] = prec; |
| 268 | } |
| 269 | |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 270 | TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 271 | { |
| 272 | if (!SupportsPrecision(type)) |
| 273 | return EbpUndefined; |
| 274 | |
| 275 | // unsigned integers use the same precision as signed |
| 276 | TBasicType baseType = (type == EbtUInt) ? EbtInt : type; |
| 277 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 278 | int level = static_cast<int>(mPrecisionStack.size()) - 1; |
| 279 | ASSERT(level >= 0); // Just to be safe. Should not happen. |
Olli Etuaho | 183d7e2 | 2015-11-20 15:59:09 +0200 | [diff] [blame] | 280 | // If we dont find anything we return this. Some types don't have predefined default precision. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 281 | TPrecision prec = EbpUndefined; |
| 282 | while (level >= 0) |
| 283 | { |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 284 | PrecisionStackLevel::iterator it = mPrecisionStack[level]->find(baseType); |
| 285 | if (it != mPrecisionStack[level]->end()) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 286 | { |
| 287 | prec = (*it).second; |
| 288 | break; |
| 289 | } |
| 290 | level--; |
| 291 | } |
| 292 | return prec; |
| 293 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 294 | |
Olli Etuaho | defe393 | 2018-02-13 11:56:09 +0200 | [diff] [blame] | 295 | void TSymbolTable::addInvariantVarying(const ImmutableString &originalName) |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 296 | { |
| 297 | ASSERT(atGlobalLevel()); |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 298 | mTable.back()->addInvariantVarying(originalName); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Olli Etuaho | defe393 | 2018-02-13 11:56:09 +0200 | [diff] [blame] | 301 | bool TSymbolTable::isVaryingInvariant(const ImmutableString &originalName) const |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 302 | { |
| 303 | ASSERT(atGlobalLevel()); |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 304 | return mTable.back()->isVaryingInvariant(originalName); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void TSymbolTable::setGlobalInvariant(bool invariant) |
| 308 | { |
| 309 | ASSERT(atGlobalLevel()); |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 310 | mTable.back()->setGlobalInvariant(invariant); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Olli Etuaho | 5d69db1 | 2017-11-24 16:51:15 +0200 | [diff] [blame] | 313 | void TSymbolTable::clearCompilationResults() |
| 314 | { |
Olli Etuaho | 391bda2 | 2018-02-23 11:43:14 +0200 | [diff] [blame] | 315 | mUniqueIdCounter = kLastBuiltInId + 1; |
Olli Etuaho | 5d69db1 | 2017-11-24 16:51:15 +0200 | [diff] [blame] | 316 | |
| 317 | // User-defined scopes should have already been cleared when the compilation finished. |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 318 | ASSERT(mTable.size() == 0u); |
Olli Etuaho | 5d69db1 | 2017-11-24 16:51:15 +0200 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | int TSymbolTable::nextUniqueIdValue() |
| 322 | { |
| 323 | ASSERT(mUniqueIdCounter < std::numeric_limits<int>::max()); |
| 324 | return ++mUniqueIdCounter; |
| 325 | } |
| 326 | |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 327 | void TSymbolTable::initializeBuiltIns(sh::GLenum type, |
| 328 | ShShaderSpec spec, |
| 329 | const ShBuiltInResources &resources) |
| 330 | { |
Olli Etuaho | 065aa86 | 2018-02-22 15:30:27 +0200 | [diff] [blame] | 331 | mShaderType = type; |
| 332 | |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 333 | ASSERT(isEmpty()); |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 334 | pushBuiltInLevel(); // COMMON_BUILTINS |
| 335 | pushBuiltInLevel(); // ESSL1_BUILTINS |
| 336 | pushBuiltInLevel(); // ESSL3_BUILTINS |
| 337 | pushBuiltInLevel(); // ESSL3_1_BUILTINS |
| 338 | pushBuiltInLevel(); // GLSL_BUILTINS |
| 339 | |
| 340 | // We need just one precision stack level for predefined precisions. |
| 341 | mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel)); |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 342 | |
| 343 | switch (type) |
| 344 | { |
| 345 | case GL_FRAGMENT_SHADER: |
| 346 | setDefaultPrecision(EbtInt, EbpMedium); |
| 347 | break; |
| 348 | case GL_VERTEX_SHADER: |
| 349 | case GL_COMPUTE_SHADER: |
| 350 | case GL_GEOMETRY_SHADER_EXT: |
| 351 | setDefaultPrecision(EbtInt, EbpHigh); |
| 352 | setDefaultPrecision(EbtFloat, EbpHigh); |
| 353 | break; |
| 354 | default: |
| 355 | UNREACHABLE(); |
| 356 | } |
| 357 | // Set defaults for sampler types that have default precision, even those that are |
| 358 | // only available if an extension exists. |
| 359 | // New sampler types in ESSL3 don't have default precision. ESSL1 types do. |
| 360 | initSamplerDefaultPrecision(EbtSampler2D); |
| 361 | initSamplerDefaultPrecision(EbtSamplerCube); |
| 362 | // SamplerExternalOES is specified in the extension to have default precision. |
| 363 | initSamplerDefaultPrecision(EbtSamplerExternalOES); |
| 364 | // SamplerExternal2DY2YEXT is specified in the extension to have default precision. |
| 365 | initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT); |
| 366 | // It isn't specified whether Sampler2DRect has default precision. |
| 367 | initSamplerDefaultPrecision(EbtSampler2DRect); |
| 368 | |
| 369 | setDefaultPrecision(EbtAtomicCounter, EbpHigh); |
| 370 | |
Olli Etuaho | 140152e | 2018-02-08 14:46:44 +0200 | [diff] [blame] | 371 | insertBuiltInFunctions(type); |
Olli Etuaho | 391bda2 | 2018-02-23 11:43:14 +0200 | [diff] [blame] | 372 | insertBuiltInVariables(type, spec, resources); |
| 373 | mUniqueIdCounter = kLastBuiltInId + 1; |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | void TSymbolTable::initSamplerDefaultPrecision(TBasicType samplerType) |
| 377 | { |
Olli Etuaho | e600c0a | 2018-03-02 11:23:29 +0200 | [diff] [blame^] | 378 | ASSERT(samplerType >= EbtGuardSamplerBegin && samplerType <= EbtGuardSamplerEnd); |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 379 | setDefaultPrecision(samplerType, EbpLow); |
| 380 | } |
| 381 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 382 | } // namespace sh |