blob: 782045a811cd2d71711ac6c123b269cae158558d [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 Etuaho29bda812018-01-26 17:37:36 +020016#include "angle_gl.h"
Olli Etuaho2d8e4322018-01-22 14:12:46 +020017#include "compiler/translator/ImmutableString.h"
Olli Etuaho01d0ad02017-01-22 14:51:23 -080018#include "compiler/translator/IntermNode.h"
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020019#include "compiler/translator/StaticType.h"
kbr@chromium.org476541f2011-10-27 21:14:51 +000020
Jamie Madill45bcc782016-11-07 13:58:48 -050021namespace sh
22{
23
Olli Etuahodd21ecf2018-01-10 12:42:09 +020024class TSymbolTable::TSymbolTableLevel
25{
26 public:
Olli Etuaho76b2c382018-03-19 15:51:29 +020027 TSymbolTableLevel() {}
Olli Etuahodd21ecf2018-01-10 12:42:09 +020028
29 bool insert(TSymbol *symbol);
30
31 // Insert a function using its unmangled name as the key.
Olli Etuahob92f92a2018-02-15 19:14:59 +020032 void insertUnmangled(TFunction *function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +020033
Olli Etuahofbb1c792018-01-19 16:26:59 +020034 TSymbol *find(const ImmutableString &name) const;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020035
Olli Etuahodd21ecf2018-01-10 12:42:09 +020036 private:
Olli Etuahofbb1c792018-01-19 16:26:59 +020037 using tLevel = TUnorderedMap<ImmutableString,
38 TSymbol *,
39 ImmutableString::FowlerNollVoHash<sizeof(size_t)>>;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020040 using tLevelPair = const tLevel::value_type;
41 using tInsertResult = std::pair<tLevel::iterator, bool>;
42
43 tLevel level;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020044};
45
Olli Etuahodd21ecf2018-01-10 12:42:09 +020046bool TSymbolTable::TSymbolTableLevel::insert(TSymbol *symbol)
Jamie Madillbfa91f42014-06-05 15:45:18 -040047{
Jamie Madillbfa91f42014-06-05 15:45:18 -040048 // returning true means symbol was added to the table
Nicolas Capensadfffe42014-06-17 02:13:36 -040049 tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol));
Jamie Madillbfa91f42014-06-05 15:45:18 -040050 return result.second;
51}
52
Olli Etuahob92f92a2018-02-15 19:14:59 +020053void TSymbolTable::TSymbolTableLevel::insertUnmangled(TFunction *function)
Olli Etuahob2983c92015-03-18 14:02:46 +020054{
Olli Etuahob92f92a2018-02-15 19:14:59 +020055 level.insert(tLevelPair(function->name(), function));
Olli Etuahob2983c92015-03-18 14:02:46 +020056}
57
Olli Etuahofbb1c792018-01-19 16:26:59 +020058TSymbol *TSymbolTable::TSymbolTableLevel::find(const ImmutableString &name) const
Jamie Madillbfa91f42014-06-05 15:45:18 -040059{
60 tLevel::const_iterator it = level.find(name);
61 if (it == level.end())
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020062 return nullptr;
Jamie Madillbfa91f42014-06-05 15:45:18 -040063 else
64 return (*it).second;
65}
66
Olli Etuaho94bbed12018-03-20 14:44:53 +020067TSymbolTable::TSymbolTable()
Olli Etuaho76b2c382018-03-19 15:51:29 +020068 : mGlobalInvariant(false),
69 mUniqueIdCounter(0),
70 mShaderType(GL_FRAGMENT_SHADER),
71 mGlInVariableWithArraySize(nullptr)
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020072{
73}
74
75TSymbolTable::~TSymbolTable() = default;
76
Olli Etuaho437664b2018-02-28 15:38:14 +020077bool TSymbolTable::isEmpty() const
78{
79 return mTable.empty();
80}
81
82bool TSymbolTable::atGlobalLevel() const
83{
84 return mTable.size() == 1u;
85}
86
Olli Etuahodd21ecf2018-01-10 12:42:09 +020087void TSymbolTable::push()
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000088{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020089 mTable.push_back(std::unique_ptr<TSymbolTableLevel>(new TSymbolTableLevel));
90 mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel));
Olli Etuahodd21ecf2018-01-10 12:42:09 +020091}
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000092
Olli Etuahodd21ecf2018-01-10 12:42:09 +020093void TSymbolTable::pop()
94{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020095 mTable.pop_back();
96 mPrecisionStack.pop_back();
Olli Etuahodd21ecf2018-01-10 12:42:09 +020097}
98
Olli Etuaho7c8567a2018-02-20 15:44:07 +020099const TFunction *TSymbolTable::markFunctionHasPrototypeDeclaration(
Olli Etuahofbb1c792018-01-19 16:26:59 +0200100 const ImmutableString &mangledName,
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200101 bool *hadPrototypeDeclarationOut)
102{
103 TFunction *function = findUserDefinedFunction(mangledName);
104 *hadPrototypeDeclarationOut = function->hasPrototypeDeclaration();
105 function->setHasPrototypeDeclaration();
106 return function;
107}
108
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200109const TFunction *TSymbolTable::setFunctionParameterNamesFromDefinition(const TFunction *function,
110 bool *wasDefinedOut)
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200111{
112 TFunction *firstDeclaration = findUserDefinedFunction(function->getMangledName());
113 ASSERT(firstDeclaration);
114 // Note: 'firstDeclaration' could be 'function' if this is the first time we've seen function as
115 // it would have just been put in the symbol table. Otherwise, we're looking up an earlier
116 // occurance.
117 if (function != firstDeclaration)
118 {
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200119 // The previous declaration should have the same parameters as the function definition
120 // (parameter names may differ).
121 firstDeclaration->shareParameters(*function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200122 }
123
124 *wasDefinedOut = firstDeclaration->isDefined();
125 firstDeclaration->setDefined();
126 return firstDeclaration;
127}
128
Olli Etuaho94bbed12018-03-20 14:44:53 +0200129bool TSymbolTable::setGlInArraySize(unsigned int inputArraySize)
130{
131 if (mGlInVariableWithArraySize)
132 {
133 return mGlInVariableWithArraySize->getType().getOutermostArraySize() == inputArraySize;
134 }
135 const TInterfaceBlock *glPerVertex = mVar_gl_PerVertex;
136 TType *glInType = new TType(glPerVertex, EvqPerVertexIn, TLayoutQualifier::Create());
137 glInType->makeArray(inputArraySize);
138 mGlInVariableWithArraySize =
139 new TVariable(this, ImmutableString("gl_in"), glInType, SymbolType::BuiltIn,
140 TExtension::EXT_geometry_shader);
141 return true;
142}
143
144TVariable *TSymbolTable::getGlInVariableWithArraySize() const
145{
146 return mGlInVariableWithArraySize;
147}
148
Olli Etuaho59c5b892018-04-03 11:44:50 +0300149const TVariable *TSymbolTable::gl_FragData() const
150{
151 return mVar_gl_FragData;
152}
153
154const TVariable *TSymbolTable::gl_SecondaryFragDataEXT() const
155{
156 return mVar_gl_SecondaryFragDataEXT;
157}
158
Olli Etuaho76b2c382018-03-19 15:51:29 +0200159TSymbolTable::VariableMetadata *TSymbolTable::getOrCreateVariableMetadata(const TVariable &variable) {
160 int id = variable.uniqueId().get();
Olli Etuaho94bbed12018-03-20 14:44:53 +0200161 auto iter = mVariableMetadata.find(id);
162 if (iter == mVariableMetadata.end())
163 {
164 iter = mVariableMetadata.insert(std::make_pair(id, VariableMetadata())).first;
165 }
Olli Etuaho76b2c382018-03-19 15:51:29 +0200166 return &iter->second;
167}
168
169void TSymbolTable::markStaticWrite(const TVariable &variable)
170{
171 auto metadata = getOrCreateVariableMetadata(variable);
172 metadata->staticWrite = true;
Olli Etuaho94bbed12018-03-20 14:44:53 +0200173}
174
175void TSymbolTable::markStaticRead(const TVariable &variable)
176{
Olli Etuaho76b2c382018-03-19 15:51:29 +0200177 auto metadata = getOrCreateVariableMetadata(variable);
178 metadata->staticRead = true;
Olli Etuaho94bbed12018-03-20 14:44:53 +0200179}
180
181bool TSymbolTable::isStaticallyUsed(const TVariable &variable) const
182{
183 ASSERT(!variable.getConstPointer());
184 int id = variable.uniqueId().get();
185 auto iter = mVariableMetadata.find(id);
Olli Etuaho76b2c382018-03-19 15:51:29 +0200186 return iter != mVariableMetadata.end() && (iter->second.staticRead || iter->second.staticWrite);
187}
188
189void TSymbolTable::addInvariantVarying(const TVariable &variable)
190{
191 ASSERT(atGlobalLevel());
192 auto metadata = getOrCreateVariableMetadata(variable);
193 metadata->invariant = true;
194}
195
196bool TSymbolTable::isVaryingInvariant(const TVariable &variable) const
197{
198 ASSERT(atGlobalLevel());
199 if (mGlobalInvariant)
200 {
201 return true;
202 }
203 int id = variable.uniqueId().get();
204 auto iter = mVariableMetadata.find(id);
205 return iter != mVariableMetadata.end() && iter->second.invariant;
206}
207
208void TSymbolTable::setGlobalInvariant(bool invariant)
209{
210 ASSERT(atGlobalLevel());
211 mGlobalInvariant = invariant;
Olli Etuaho94bbed12018-03-20 14:44:53 +0200212}
213
Olli Etuahofbb1c792018-01-19 16:26:59 +0200214const TSymbol *TSymbolTable::find(const ImmutableString &name, int shaderVersion) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200215{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200216 int userDefinedLevel = static_cast<int>(mTable.size()) - 1;
217 while (userDefinedLevel >= 0)
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000218 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200219 const TSymbol *symbol = mTable[userDefinedLevel]->find(name);
220 if (symbol)
221 {
222 return symbol;
223 }
224 userDefinedLevel--;
225 }
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000226
Olli Etuaho115b2c42018-03-15 17:46:29 +0200227 return findBuiltIn(name, shaderVersion);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000228}
229
Olli Etuahofbb1c792018-01-19 16:26:59 +0200230TFunction *TSymbolTable::findUserDefinedFunction(const ImmutableString &name) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200231{
232 // User-defined functions are always declared at the global level.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200233 ASSERT(!mTable.empty());
234 return static_cast<TFunction *>(mTable[0]->find(name));
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200235}
236
Olli Etuahofbb1c792018-01-19 16:26:59 +0200237const TSymbol *TSymbolTable::findGlobal(const ImmutableString &name) const
Zhenyao Mod7490962016-11-09 15:49:51 -0800238{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200239 ASSERT(!mTable.empty());
240 return mTable[0]->find(name);
Zhenyao Mod7490962016-11-09 15:49:51 -0800241}
242
Olli Etuaho437664b2018-02-28 15:38:14 +0200243bool TSymbolTable::declare(TSymbol *symbol)
Olli Etuaho0f684632017-07-13 12:42:15 +0300244{
Olli Etuaho437664b2018-02-28 15:38:14 +0200245 ASSERT(!mTable.empty());
246 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
247 ASSERT(!symbol->isFunction());
248 return mTable.back()->insert(symbol);
Jiawei Shaod8105a02017-08-08 09:54:36 +0800249}
250
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200251void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUnmangledName)
252{
Olli Etuaho437664b2018-02-28 15:38:14 +0200253 ASSERT(!mTable.empty());
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200254 if (insertUnmangledName)
255 {
256 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200257 mTable[0]->insertUnmangled(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200258 }
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200259 mTable[0]->insert(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200260}
261
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200262void TSymbolTable::setDefaultPrecision(TBasicType type, TPrecision prec)
263{
264 int indexOfLastElement = static_cast<int>(mPrecisionStack.size()) - 1;
265 // Uses map operator [], overwrites the current value
266 (*mPrecisionStack[indexOfLastElement])[type] = prec;
267}
268
Zhenyao Moe740add2014-07-18 17:01:01 -0700269TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700270{
271 if (!SupportsPrecision(type))
272 return EbpUndefined;
273
274 // unsigned integers use the same precision as signed
275 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
276
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200277 int level = static_cast<int>(mPrecisionStack.size()) - 1;
278 ASSERT(level >= 0); // Just to be safe. Should not happen.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200279 // If we dont find anything we return this. Some types don't have predefined default precision.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700280 TPrecision prec = EbpUndefined;
281 while (level >= 0)
282 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200283 PrecisionStackLevel::iterator it = mPrecisionStack[level]->find(baseType);
284 if (it != mPrecisionStack[level]->end())
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700285 {
286 prec = (*it).second;
287 break;
288 }
289 level--;
290 }
291 return prec;
292}
Jamie Madill45bcc782016-11-07 13:58:48 -0500293
Olli Etuaho5d69db12017-11-24 16:51:15 +0200294void TSymbolTable::clearCompilationResults()
295{
Olli Etuaho76b2c382018-03-19 15:51:29 +0200296 mGlobalInvariant = false;
Olli Etuaho391bda22018-02-23 11:43:14 +0200297 mUniqueIdCounter = kLastBuiltInId + 1;
Olli Etuaho94bbed12018-03-20 14:44:53 +0200298 mVariableMetadata.clear();
299 mGlInVariableWithArraySize = nullptr;
Olli Etuaho5d69db12017-11-24 16:51:15 +0200300
301 // User-defined scopes should have already been cleared when the compilation finished.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200302 ASSERT(mTable.size() == 0u);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200303}
304
305int TSymbolTable::nextUniqueIdValue()
306{
307 ASSERT(mUniqueIdCounter < std::numeric_limits<int>::max());
308 return ++mUniqueIdCounter;
309}
310
Olli Etuaho29bda812018-01-26 17:37:36 +0200311void TSymbolTable::initializeBuiltIns(sh::GLenum type,
312 ShShaderSpec spec,
313 const ShBuiltInResources &resources)
314{
Olli Etuaho065aa862018-02-22 15:30:27 +0200315 mShaderType = type;
Olli Etuahob391ec42018-03-12 17:04:59 +0200316 mResources = resources;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200317
318 // We need just one precision stack level for predefined precisions.
319 mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel));
Olli Etuaho29bda812018-01-26 17:37:36 +0200320
321 switch (type)
322 {
323 case GL_FRAGMENT_SHADER:
324 setDefaultPrecision(EbtInt, EbpMedium);
325 break;
326 case GL_VERTEX_SHADER:
327 case GL_COMPUTE_SHADER:
328 case GL_GEOMETRY_SHADER_EXT:
329 setDefaultPrecision(EbtInt, EbpHigh);
330 setDefaultPrecision(EbtFloat, EbpHigh);
331 break;
332 default:
333 UNREACHABLE();
334 }
335 // Set defaults for sampler types that have default precision, even those that are
336 // only available if an extension exists.
337 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
338 initSamplerDefaultPrecision(EbtSampler2D);
339 initSamplerDefaultPrecision(EbtSamplerCube);
340 // SamplerExternalOES is specified in the extension to have default precision.
341 initSamplerDefaultPrecision(EbtSamplerExternalOES);
342 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
343 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
344 // It isn't specified whether Sampler2DRect has default precision.
345 initSamplerDefaultPrecision(EbtSampler2DRect);
346
347 setDefaultPrecision(EbtAtomicCounter, EbpHigh);
348
Olli Etuahob391ec42018-03-12 17:04:59 +0200349 initializeBuiltInVariables(type, spec, resources);
Olli Etuaho391bda22018-02-23 11:43:14 +0200350 mUniqueIdCounter = kLastBuiltInId + 1;
Olli Etuaho29bda812018-01-26 17:37:36 +0200351}
352
353void TSymbolTable::initSamplerDefaultPrecision(TBasicType samplerType)
354{
Olli Etuahoe600c0a2018-03-02 11:23:29 +0200355 ASSERT(samplerType >= EbtGuardSamplerBegin && samplerType <= EbtGuardSamplerEnd);
Olli Etuaho29bda812018-01-26 17:37:36 +0200356 setDefaultPrecision(samplerType, EbpLow);
357}
358
Olli Etuaho76b2c382018-03-19 15:51:29 +0200359TSymbolTable::VariableMetadata::VariableMetadata() : staticRead(false), staticWrite(false), invariant(false)
Olli Etuaho94bbed12018-03-20 14:44:53 +0200360{
361}
362
Jamie Madill45bcc782016-11-07 13:58:48 -0500363} // namespace sh