blob: b323fa56ea4c80aaac5fe86797e080b1d8255c67 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
Olli Etuaho0f684632017-07-13 12:42:15 +03006// Symbol table for parsing. The design principles and most of the functionality are documented in
7// the header file.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008//
9
apatrick@chromium.orge057c5d2012-01-26 19:18:24 +000010#if defined(_MSC_VER)
Jamie Madilld7b1ab52016-12-12 14:42:19 -050011#pragma warning(disable : 4718)
apatrick@chromium.orge057c5d2012-01-26 19:18:24 +000012#endif
13
Geoff Lang17732822013-08-29 13:46:49 -040014#include "compiler/translator/SymbolTable.h"
Olli Etuaho01d0ad02017-01-22 14:51:23 -080015
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020016#include <algorithm>
17#include <set>
18
Olli Etuaho29bda812018-01-26 17:37:36 +020019#include "angle_gl.h"
Olli Etuaho391bda22018-02-23 11:43:14 +020020#include "compiler/translator/BuiltIn_autogen.h"
Olli Etuaho2d8e4322018-01-22 14:12:46 +020021#include "compiler/translator/ImmutableString.h"
Olli Etuaho01d0ad02017-01-22 14:51:23 -080022#include "compiler/translator/IntermNode.h"
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020023#include "compiler/translator/StaticType.h"
kbr@chromium.org476541f2011-10-27 21:14:51 +000024
Jamie Madill45bcc782016-11-07 13:58:48 -050025namespace sh
26{
27
Olli Etuahodd21ecf2018-01-10 12:42:09 +020028class TSymbolTable::TSymbolTableLevel
29{
30 public:
31 TSymbolTableLevel() : mGlobalInvariant(false) {}
Olli Etuahodd21ecf2018-01-10 12:42:09 +020032
33 bool insert(TSymbol *symbol);
34
35 // Insert a function using its unmangled name as the key.
Olli Etuahob92f92a2018-02-15 19:14:59 +020036 void insertUnmangled(TFunction *function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +020037
Olli Etuahofbb1c792018-01-19 16:26:59 +020038 TSymbol *find(const ImmutableString &name) const;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020039
Olli Etuahodefe3932018-02-13 11:56:09 +020040 void addInvariantVarying(const ImmutableString &name) { mInvariantVaryings.insert(name); }
Olli Etuahodd21ecf2018-01-10 12:42:09 +020041
Olli Etuahodefe3932018-02-13 11:56:09 +020042 bool isVaryingInvariant(const ImmutableString &name)
Olli Etuahodd21ecf2018-01-10 12:42:09 +020043 {
44 return (mGlobalInvariant || mInvariantVaryings.count(name) > 0);
45 }
46
47 void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; }
48
Olli Etuahodd21ecf2018-01-10 12:42:09 +020049 private:
Olli Etuahofbb1c792018-01-19 16:26:59 +020050 using tLevel = TUnorderedMap<ImmutableString,
51 TSymbol *,
52 ImmutableString::FowlerNollVoHash<sizeof(size_t)>>;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020053 using tLevelPair = const tLevel::value_type;
54 using tInsertResult = std::pair<tLevel::iterator, bool>;
55
56 tLevel level;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020057
Olli Etuahodefe3932018-02-13 11:56:09 +020058 std::set<ImmutableString> mInvariantVaryings;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020059 bool mGlobalInvariant;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020060};
61
Olli Etuahodd21ecf2018-01-10 12:42:09 +020062bool TSymbolTable::TSymbolTableLevel::insert(TSymbol *symbol)
Jamie Madillbfa91f42014-06-05 15:45:18 -040063{
Jamie Madillbfa91f42014-06-05 15:45:18 -040064 // returning true means symbol was added to the table
Nicolas Capensadfffe42014-06-17 02:13:36 -040065 tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol));
Jamie Madillbfa91f42014-06-05 15:45:18 -040066 return result.second;
67}
68
Olli Etuahob92f92a2018-02-15 19:14:59 +020069void TSymbolTable::TSymbolTableLevel::insertUnmangled(TFunction *function)
Olli Etuahob2983c92015-03-18 14:02:46 +020070{
Olli Etuahob92f92a2018-02-15 19:14:59 +020071 level.insert(tLevelPair(function->name(), function));
Olli Etuahob2983c92015-03-18 14:02:46 +020072}
73
Olli Etuahofbb1c792018-01-19 16:26:59 +020074TSymbol *TSymbolTable::TSymbolTableLevel::find(const ImmutableString &name) const
Jamie Madillbfa91f42014-06-05 15:45:18 -040075{
76 tLevel::const_iterator it = level.find(name);
77 if (it == level.end())
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020078 return nullptr;
Jamie Madillbfa91f42014-06-05 15:45:18 -040079 else
80 return (*it).second;
81}
82
Olli Etuaho391bda22018-02-23 11:43:14 +020083TSymbolTable::TSymbolTable() : mUniqueIdCounter(0), mShaderType(GL_FRAGMENT_SHADER)
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020084{
85}
86
87TSymbolTable::~TSymbolTable() = default;
88
Olli Etuaho437664b2018-02-28 15:38:14 +020089bool TSymbolTable::isEmpty() const
90{
91 return mTable.empty();
92}
93
94bool TSymbolTable::atGlobalLevel() const
95{
96 return mTable.size() == 1u;
97}
98
Olli Etuahodd21ecf2018-01-10 12:42:09 +020099void TSymbolTable::push()
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000100{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200101 mTable.push_back(std::unique_ptr<TSymbolTableLevel>(new TSymbolTableLevel));
102 mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel));
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200103}
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000104
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200105void TSymbolTable::pop()
106{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200107 mTable.pop_back();
108 mPrecisionStack.pop_back();
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200109}
110
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200111const TFunction *TSymbolTable::markFunctionHasPrototypeDeclaration(
Olli Etuahofbb1c792018-01-19 16:26:59 +0200112 const ImmutableString &mangledName,
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200113 bool *hadPrototypeDeclarationOut)
114{
115 TFunction *function = findUserDefinedFunction(mangledName);
116 *hadPrototypeDeclarationOut = function->hasPrototypeDeclaration();
117 function->setHasPrototypeDeclaration();
118 return function;
119}
120
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200121const TFunction *TSymbolTable::setFunctionParameterNamesFromDefinition(const TFunction *function,
122 bool *wasDefinedOut)
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200123{
124 TFunction *firstDeclaration = findUserDefinedFunction(function->getMangledName());
125 ASSERT(firstDeclaration);
126 // Note: 'firstDeclaration' could be 'function' if this is the first time we've seen function as
127 // it would have just been put in the symbol table. Otherwise, we're looking up an earlier
128 // occurance.
129 if (function != firstDeclaration)
130 {
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200131 // The previous declaration should have the same parameters as the function definition
132 // (parameter names may differ).
133 firstDeclaration->shareParameters(*function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200134 }
135
136 *wasDefinedOut = firstDeclaration->isDefined();
137 firstDeclaration->setDefined();
138 return firstDeclaration;
139}
140
Olli Etuahofbb1c792018-01-19 16:26:59 +0200141const TSymbol *TSymbolTable::find(const ImmutableString &name, int shaderVersion) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200142{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200143 int userDefinedLevel = static_cast<int>(mTable.size()) - 1;
144 while (userDefinedLevel >= 0)
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000145 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200146 const TSymbol *symbol = mTable[userDefinedLevel]->find(name);
147 if (symbol)
148 {
149 return symbol;
150 }
151 userDefinedLevel--;
152 }
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000153
Olli Etuaho115b2c42018-03-15 17:46:29 +0200154 return findBuiltIn(name, shaderVersion);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000155}
156
Olli Etuahofbb1c792018-01-19 16:26:59 +0200157TFunction *TSymbolTable::findUserDefinedFunction(const ImmutableString &name) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200158{
159 // User-defined functions are always declared at the global level.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200160 ASSERT(!mTable.empty());
161 return static_cast<TFunction *>(mTable[0]->find(name));
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200162}
163
Olli Etuahofbb1c792018-01-19 16:26:59 +0200164const TSymbol *TSymbolTable::findGlobal(const ImmutableString &name) const
Zhenyao Mod7490962016-11-09 15:49:51 -0800165{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200166 ASSERT(!mTable.empty());
167 return mTable[0]->find(name);
Zhenyao Mod7490962016-11-09 15:49:51 -0800168}
169
Olli Etuaho437664b2018-02-28 15:38:14 +0200170bool TSymbolTable::declare(TSymbol *symbol)
Olli Etuaho0f684632017-07-13 12:42:15 +0300171{
Olli Etuaho437664b2018-02-28 15:38:14 +0200172 ASSERT(!mTable.empty());
173 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
174 ASSERT(!symbol->isFunction());
175 return mTable.back()->insert(symbol);
Jiawei Shaod8105a02017-08-08 09:54:36 +0800176}
177
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200178void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUnmangledName)
179{
Olli Etuaho437664b2018-02-28 15:38:14 +0200180 ASSERT(!mTable.empty());
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200181 if (insertUnmangledName)
182 {
183 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200184 mTable[0]->insertUnmangled(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200185 }
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200186 mTable[0]->insert(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200187}
188
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200189void TSymbolTable::setDefaultPrecision(TBasicType type, TPrecision prec)
190{
191 int indexOfLastElement = static_cast<int>(mPrecisionStack.size()) - 1;
192 // Uses map operator [], overwrites the current value
193 (*mPrecisionStack[indexOfLastElement])[type] = prec;
194}
195
Zhenyao Moe740add2014-07-18 17:01:01 -0700196TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700197{
198 if (!SupportsPrecision(type))
199 return EbpUndefined;
200
201 // unsigned integers use the same precision as signed
202 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
203
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200204 int level = static_cast<int>(mPrecisionStack.size()) - 1;
205 ASSERT(level >= 0); // Just to be safe. Should not happen.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200206 // If we dont find anything we return this. Some types don't have predefined default precision.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700207 TPrecision prec = EbpUndefined;
208 while (level >= 0)
209 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200210 PrecisionStackLevel::iterator it = mPrecisionStack[level]->find(baseType);
211 if (it != mPrecisionStack[level]->end())
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700212 {
213 prec = (*it).second;
214 break;
215 }
216 level--;
217 }
218 return prec;
219}
Jamie Madill45bcc782016-11-07 13:58:48 -0500220
Olli Etuahodefe3932018-02-13 11:56:09 +0200221void TSymbolTable::addInvariantVarying(const ImmutableString &originalName)
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200222{
223 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200224 mTable.back()->addInvariantVarying(originalName);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200225}
226
Olli Etuahodefe3932018-02-13 11:56:09 +0200227bool TSymbolTable::isVaryingInvariant(const ImmutableString &originalName) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200228{
229 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200230 return mTable.back()->isVaryingInvariant(originalName);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200231}
232
233void TSymbolTable::setGlobalInvariant(bool invariant)
234{
235 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200236 mTable.back()->setGlobalInvariant(invariant);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200237}
238
Olli Etuaho5d69db12017-11-24 16:51:15 +0200239void TSymbolTable::clearCompilationResults()
240{
Olli Etuaho391bda22018-02-23 11:43:14 +0200241 mUniqueIdCounter = kLastBuiltInId + 1;
Olli Etuaho5d69db12017-11-24 16:51:15 +0200242
243 // User-defined scopes should have already been cleared when the compilation finished.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200244 ASSERT(mTable.size() == 0u);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200245}
246
247int TSymbolTable::nextUniqueIdValue()
248{
249 ASSERT(mUniqueIdCounter < std::numeric_limits<int>::max());
250 return ++mUniqueIdCounter;
251}
252
Olli Etuaho29bda812018-01-26 17:37:36 +0200253void TSymbolTable::initializeBuiltIns(sh::GLenum type,
254 ShShaderSpec spec,
255 const ShBuiltInResources &resources)
256{
Olli Etuaho065aa862018-02-22 15:30:27 +0200257 mShaderType = type;
Olli Etuahob391ec42018-03-12 17:04:59 +0200258 mResources = resources;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200259
260 // We need just one precision stack level for predefined precisions.
261 mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel));
Olli Etuaho29bda812018-01-26 17:37:36 +0200262
263 switch (type)
264 {
265 case GL_FRAGMENT_SHADER:
266 setDefaultPrecision(EbtInt, EbpMedium);
267 break;
268 case GL_VERTEX_SHADER:
269 case GL_COMPUTE_SHADER:
270 case GL_GEOMETRY_SHADER_EXT:
271 setDefaultPrecision(EbtInt, EbpHigh);
272 setDefaultPrecision(EbtFloat, EbpHigh);
273 break;
274 default:
275 UNREACHABLE();
276 }
277 // Set defaults for sampler types that have default precision, even those that are
278 // only available if an extension exists.
279 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
280 initSamplerDefaultPrecision(EbtSampler2D);
281 initSamplerDefaultPrecision(EbtSamplerCube);
282 // SamplerExternalOES is specified in the extension to have default precision.
283 initSamplerDefaultPrecision(EbtSamplerExternalOES);
284 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
285 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
286 // It isn't specified whether Sampler2DRect has default precision.
287 initSamplerDefaultPrecision(EbtSampler2DRect);
288
289 setDefaultPrecision(EbtAtomicCounter, EbpHigh);
290
Olli Etuahob391ec42018-03-12 17:04:59 +0200291 initializeBuiltInVariables(type, spec, resources);
Olli Etuaho391bda22018-02-23 11:43:14 +0200292 mUniqueIdCounter = kLastBuiltInId + 1;
Olli Etuaho29bda812018-01-26 17:37:36 +0200293}
294
295void TSymbolTable::initSamplerDefaultPrecision(TBasicType samplerType)
296{
Olli Etuahoe600c0a2018-03-02 11:23:29 +0200297 ASSERT(samplerType >= EbtGuardSamplerBegin && samplerType <= EbtGuardSamplerEnd);
Olli Etuaho29bda812018-01-26 17:37:36 +0200298 setDefaultPrecision(samplerType, EbpLow);
299}
300
Jamie Madill45bcc782016-11-07 13:58:48 -0500301} // namespace sh