blob: 228aa128ee6ee6276f865d74b6926d9390d12f9c [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 Etuaho2d8e4322018-01-22 14:12:46 +020020#include "compiler/translator/ImmutableString.h"
Olli Etuaho01d0ad02017-01-22 14:51:23 -080021#include "compiler/translator/IntermNode.h"
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020022#include "compiler/translator/StaticType.h"
kbr@chromium.org476541f2011-10-27 21:14:51 +000023
Jamie Madill45bcc782016-11-07 13:58:48 -050024namespace sh
25{
26
Olli Etuahodd21ecf2018-01-10 12:42:09 +020027class TSymbolTable::TSymbolTableLevel
28{
29 public:
30 TSymbolTableLevel() : mGlobalInvariant(false) {}
Olli Etuahodd21ecf2018-01-10 12:42:09 +020031
32 bool insert(TSymbol *symbol);
33
34 // Insert a function using its unmangled name as the key.
Olli Etuahob92f92a2018-02-15 19:14:59 +020035 void insertUnmangled(TFunction *function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +020036
Olli Etuahofbb1c792018-01-19 16:26:59 +020037 TSymbol *find(const ImmutableString &name) const;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020038
Olli Etuahodefe3932018-02-13 11:56:09 +020039 void addInvariantVarying(const ImmutableString &name) { mInvariantVaryings.insert(name); }
Olli Etuahodd21ecf2018-01-10 12:42:09 +020040
Olli Etuahodefe3932018-02-13 11:56:09 +020041 bool isVaryingInvariant(const ImmutableString &name)
Olli Etuahodd21ecf2018-01-10 12:42:09 +020042 {
43 return (mGlobalInvariant || mInvariantVaryings.count(name) > 0);
44 }
45
46 void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; }
47
Olli Etuahodd21ecf2018-01-10 12:42:09 +020048 private:
Olli Etuahofbb1c792018-01-19 16:26:59 +020049 using tLevel = TUnorderedMap<ImmutableString,
50 TSymbol *,
51 ImmutableString::FowlerNollVoHash<sizeof(size_t)>>;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020052 using tLevelPair = const tLevel::value_type;
53 using tInsertResult = std::pair<tLevel::iterator, bool>;
54
55 tLevel level;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020056
Olli Etuahodefe3932018-02-13 11:56:09 +020057 std::set<ImmutableString> mInvariantVaryings;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020058 bool mGlobalInvariant;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020059};
60
Olli Etuahodd21ecf2018-01-10 12:42:09 +020061bool TSymbolTable::TSymbolTableLevel::insert(TSymbol *symbol)
Jamie Madillbfa91f42014-06-05 15:45:18 -040062{
Jamie Madillbfa91f42014-06-05 15:45:18 -040063 // returning true means symbol was added to the table
Nicolas Capensadfffe42014-06-17 02:13:36 -040064 tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol));
Jamie Madillbfa91f42014-06-05 15:45:18 -040065 return result.second;
66}
67
Olli Etuahob92f92a2018-02-15 19:14:59 +020068void TSymbolTable::TSymbolTableLevel::insertUnmangled(TFunction *function)
Olli Etuahob2983c92015-03-18 14:02:46 +020069{
Olli Etuahob92f92a2018-02-15 19:14:59 +020070 level.insert(tLevelPair(function->name(), function));
Olli Etuahob2983c92015-03-18 14:02:46 +020071}
72
Olli Etuahofbb1c792018-01-19 16:26:59 +020073TSymbol *TSymbolTable::TSymbolTableLevel::find(const ImmutableString &name) const
Jamie Madillbfa91f42014-06-05 15:45:18 -040074{
75 tLevel::const_iterator it = level.find(name);
76 if (it == level.end())
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020077 return nullptr;
Jamie Madillbfa91f42014-06-05 15:45:18 -040078 else
79 return (*it).second;
80}
81
Olli Etuaho94bbed12018-03-20 14:44:53 +020082TSymbolTable::TSymbolTable()
83 : mUniqueIdCounter(0), mShaderType(GL_FRAGMENT_SHADER), mGlInVariableWithArraySize(nullptr)
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 Etuaho94bbed12018-03-20 14:44:53 +0200141bool TSymbolTable::setGlInArraySize(unsigned int inputArraySize)
142{
143 if (mGlInVariableWithArraySize)
144 {
145 return mGlInVariableWithArraySize->getType().getOutermostArraySize() == inputArraySize;
146 }
147 const TInterfaceBlock *glPerVertex = mVar_gl_PerVertex;
148 TType *glInType = new TType(glPerVertex, EvqPerVertexIn, TLayoutQualifier::Create());
149 glInType->makeArray(inputArraySize);
150 mGlInVariableWithArraySize =
151 new TVariable(this, ImmutableString("gl_in"), glInType, SymbolType::BuiltIn,
152 TExtension::EXT_geometry_shader);
153 return true;
154}
155
156TVariable *TSymbolTable::getGlInVariableWithArraySize() const
157{
158 return mGlInVariableWithArraySize;
159}
160
161void TSymbolTable::markStaticWrite(const TVariable &variable)
162{
163 int id = variable.uniqueId().get();
164 auto iter = mVariableMetadata.find(id);
165 if (iter == mVariableMetadata.end())
166 {
167 iter = mVariableMetadata.insert(std::make_pair(id, VariableMetadata())).first;
168 }
169 iter->second.staticWrite = true;
170}
171
172void TSymbolTable::markStaticRead(const TVariable &variable)
173{
174 int id = variable.uniqueId().get();
175 auto iter = mVariableMetadata.find(id);
176 if (iter == mVariableMetadata.end())
177 {
178 iter = mVariableMetadata.insert(std::make_pair(id, VariableMetadata())).first;
179 }
180 iter->second.staticRead = true;
181}
182
183bool TSymbolTable::isStaticallyUsed(const TVariable &variable) const
184{
185 ASSERT(!variable.getConstPointer());
186 int id = variable.uniqueId().get();
187 auto iter = mVariableMetadata.find(id);
188 return iter != mVariableMetadata.end();
189}
190
Olli Etuahofbb1c792018-01-19 16:26:59 +0200191const TSymbol *TSymbolTable::find(const ImmutableString &name, int shaderVersion) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200192{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200193 int userDefinedLevel = static_cast<int>(mTable.size()) - 1;
194 while (userDefinedLevel >= 0)
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000195 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200196 const TSymbol *symbol = mTable[userDefinedLevel]->find(name);
197 if (symbol)
198 {
199 return symbol;
200 }
201 userDefinedLevel--;
202 }
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000203
Olli Etuaho115b2c42018-03-15 17:46:29 +0200204 return findBuiltIn(name, shaderVersion);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000205}
206
Olli Etuahofbb1c792018-01-19 16:26:59 +0200207TFunction *TSymbolTable::findUserDefinedFunction(const ImmutableString &name) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200208{
209 // User-defined functions are always declared at the global level.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200210 ASSERT(!mTable.empty());
211 return static_cast<TFunction *>(mTable[0]->find(name));
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200212}
213
Olli Etuahofbb1c792018-01-19 16:26:59 +0200214const TSymbol *TSymbolTable::findGlobal(const ImmutableString &name) const
Zhenyao Mod7490962016-11-09 15:49:51 -0800215{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200216 ASSERT(!mTable.empty());
217 return mTable[0]->find(name);
Zhenyao Mod7490962016-11-09 15:49:51 -0800218}
219
Olli Etuaho437664b2018-02-28 15:38:14 +0200220bool TSymbolTable::declare(TSymbol *symbol)
Olli Etuaho0f684632017-07-13 12:42:15 +0300221{
Olli Etuaho437664b2018-02-28 15:38:14 +0200222 ASSERT(!mTable.empty());
223 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
224 ASSERT(!symbol->isFunction());
225 return mTable.back()->insert(symbol);
Jiawei Shaod8105a02017-08-08 09:54:36 +0800226}
227
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200228void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUnmangledName)
229{
Olli Etuaho437664b2018-02-28 15:38:14 +0200230 ASSERT(!mTable.empty());
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200231 if (insertUnmangledName)
232 {
233 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200234 mTable[0]->insertUnmangled(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200235 }
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200236 mTable[0]->insert(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200237}
238
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200239void TSymbolTable::setDefaultPrecision(TBasicType type, TPrecision prec)
240{
241 int indexOfLastElement = static_cast<int>(mPrecisionStack.size()) - 1;
242 // Uses map operator [], overwrites the current value
243 (*mPrecisionStack[indexOfLastElement])[type] = prec;
244}
245
Zhenyao Moe740add2014-07-18 17:01:01 -0700246TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700247{
248 if (!SupportsPrecision(type))
249 return EbpUndefined;
250
251 // unsigned integers use the same precision as signed
252 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
253
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200254 int level = static_cast<int>(mPrecisionStack.size()) - 1;
255 ASSERT(level >= 0); // Just to be safe. Should not happen.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200256 // If we dont find anything we return this. Some types don't have predefined default precision.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700257 TPrecision prec = EbpUndefined;
258 while (level >= 0)
259 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200260 PrecisionStackLevel::iterator it = mPrecisionStack[level]->find(baseType);
261 if (it != mPrecisionStack[level]->end())
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700262 {
263 prec = (*it).second;
264 break;
265 }
266 level--;
267 }
268 return prec;
269}
Jamie Madill45bcc782016-11-07 13:58:48 -0500270
Olli Etuahodefe3932018-02-13 11:56:09 +0200271void TSymbolTable::addInvariantVarying(const ImmutableString &originalName)
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200272{
273 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200274 mTable.back()->addInvariantVarying(originalName);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200275}
276
Olli Etuahodefe3932018-02-13 11:56:09 +0200277bool TSymbolTable::isVaryingInvariant(const ImmutableString &originalName) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200278{
279 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200280 return mTable.back()->isVaryingInvariant(originalName);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200281}
282
283void TSymbolTable::setGlobalInvariant(bool invariant)
284{
285 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200286 mTable.back()->setGlobalInvariant(invariant);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200287}
288
Olli Etuaho5d69db12017-11-24 16:51:15 +0200289void TSymbolTable::clearCompilationResults()
290{
Olli Etuaho391bda22018-02-23 11:43:14 +0200291 mUniqueIdCounter = kLastBuiltInId + 1;
Olli Etuaho94bbed12018-03-20 14:44:53 +0200292 mVariableMetadata.clear();
293 mGlInVariableWithArraySize = nullptr;
Olli Etuaho5d69db12017-11-24 16:51:15 +0200294
295 // User-defined scopes should have already been cleared when the compilation finished.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200296 ASSERT(mTable.size() == 0u);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200297}
298
299int TSymbolTable::nextUniqueIdValue()
300{
301 ASSERT(mUniqueIdCounter < std::numeric_limits<int>::max());
302 return ++mUniqueIdCounter;
303}
304
Olli Etuaho29bda812018-01-26 17:37:36 +0200305void TSymbolTable::initializeBuiltIns(sh::GLenum type,
306 ShShaderSpec spec,
307 const ShBuiltInResources &resources)
308{
Olli Etuaho065aa862018-02-22 15:30:27 +0200309 mShaderType = type;
Olli Etuahob391ec42018-03-12 17:04:59 +0200310 mResources = resources;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200311
312 // We need just one precision stack level for predefined precisions.
313 mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel));
Olli Etuaho29bda812018-01-26 17:37:36 +0200314
315 switch (type)
316 {
317 case GL_FRAGMENT_SHADER:
318 setDefaultPrecision(EbtInt, EbpMedium);
319 break;
320 case GL_VERTEX_SHADER:
321 case GL_COMPUTE_SHADER:
322 case GL_GEOMETRY_SHADER_EXT:
323 setDefaultPrecision(EbtInt, EbpHigh);
324 setDefaultPrecision(EbtFloat, EbpHigh);
325 break;
326 default:
327 UNREACHABLE();
328 }
329 // Set defaults for sampler types that have default precision, even those that are
330 // only available if an extension exists.
331 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
332 initSamplerDefaultPrecision(EbtSampler2D);
333 initSamplerDefaultPrecision(EbtSamplerCube);
334 // SamplerExternalOES is specified in the extension to have default precision.
335 initSamplerDefaultPrecision(EbtSamplerExternalOES);
336 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
337 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
338 // It isn't specified whether Sampler2DRect has default precision.
339 initSamplerDefaultPrecision(EbtSampler2DRect);
340
341 setDefaultPrecision(EbtAtomicCounter, EbpHigh);
342
Olli Etuahob391ec42018-03-12 17:04:59 +0200343 initializeBuiltInVariables(type, spec, resources);
Olli Etuaho391bda22018-02-23 11:43:14 +0200344 mUniqueIdCounter = kLastBuiltInId + 1;
Olli Etuaho29bda812018-01-26 17:37:36 +0200345}
346
347void TSymbolTable::initSamplerDefaultPrecision(TBasicType samplerType)
348{
Olli Etuahoe600c0a2018-03-02 11:23:29 +0200349 ASSERT(samplerType >= EbtGuardSamplerBegin && samplerType <= EbtGuardSamplerEnd);
Olli Etuaho29bda812018-01-26 17:37:36 +0200350 setDefaultPrecision(samplerType, EbpLow);
351}
352
Olli Etuaho94bbed12018-03-20 14:44:53 +0200353TSymbolTable::VariableMetadata::VariableMetadata() : staticRead(false), staticWrite(false)
354{
355}
356
Jamie Madill45bcc782016-11-07 13:58:48 -0500357} // namespace sh