blob: e3dd234966ea757f627068ba1fe562422855aeac [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 Etuahoe5fe7aa2018-01-29 12:06:11 +0200154 return findBuiltIn(name, shaderVersion, false);
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 Etuahofbb1c792018-01-19 16:26:59 +0200170const TSymbol *TSymbolTable::findBuiltIn(const ImmutableString &name, int shaderVersion) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000171{
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300172 return findBuiltIn(name, shaderVersion, false);
173}
174
Olli Etuaho437664b2018-02-28 15:38:14 +0200175bool TSymbolTable::declare(TSymbol *symbol)
Olli Etuaho0f684632017-07-13 12:42:15 +0300176{
Olli Etuaho437664b2018-02-28 15:38:14 +0200177 ASSERT(!mTable.empty());
178 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
179 ASSERT(!symbol->isFunction());
180 return mTable.back()->insert(symbol);
Jiawei Shaod8105a02017-08-08 09:54:36 +0800181}
182
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200183void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUnmangledName)
184{
Olli Etuaho437664b2018-02-28 15:38:14 +0200185 ASSERT(!mTable.empty());
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200186 if (insertUnmangledName)
187 {
188 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200189 mTable[0]->insertUnmangled(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200190 }
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200191 mTable[0]->insert(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200192}
193
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200194void TSymbolTable::setDefaultPrecision(TBasicType type, TPrecision prec)
195{
196 int indexOfLastElement = static_cast<int>(mPrecisionStack.size()) - 1;
197 // Uses map operator [], overwrites the current value
198 (*mPrecisionStack[indexOfLastElement])[type] = prec;
199}
200
Zhenyao Moe740add2014-07-18 17:01:01 -0700201TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700202{
203 if (!SupportsPrecision(type))
204 return EbpUndefined;
205
206 // unsigned integers use the same precision as signed
207 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
208
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200209 int level = static_cast<int>(mPrecisionStack.size()) - 1;
210 ASSERT(level >= 0); // Just to be safe. Should not happen.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200211 // If we dont find anything we return this. Some types don't have predefined default precision.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700212 TPrecision prec = EbpUndefined;
213 while (level >= 0)
214 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200215 PrecisionStackLevel::iterator it = mPrecisionStack[level]->find(baseType);
216 if (it != mPrecisionStack[level]->end())
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700217 {
218 prec = (*it).second;
219 break;
220 }
221 level--;
222 }
223 return prec;
224}
Jamie Madill45bcc782016-11-07 13:58:48 -0500225
Olli Etuahodefe3932018-02-13 11:56:09 +0200226void TSymbolTable::addInvariantVarying(const ImmutableString &originalName)
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200227{
228 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200229 mTable.back()->addInvariantVarying(originalName);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200230}
231
Olli Etuahodefe3932018-02-13 11:56:09 +0200232bool TSymbolTable::isVaryingInvariant(const ImmutableString &originalName) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200233{
234 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200235 return mTable.back()->isVaryingInvariant(originalName);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200236}
237
238void TSymbolTable::setGlobalInvariant(bool invariant)
239{
240 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200241 mTable.back()->setGlobalInvariant(invariant);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200242}
243
Olli Etuaho5d69db12017-11-24 16:51:15 +0200244void TSymbolTable::clearCompilationResults()
245{
Olli Etuaho391bda22018-02-23 11:43:14 +0200246 mUniqueIdCounter = kLastBuiltInId + 1;
Olli Etuaho5d69db12017-11-24 16:51:15 +0200247
248 // User-defined scopes should have already been cleared when the compilation finished.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200249 ASSERT(mTable.size() == 0u);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200250}
251
252int TSymbolTable::nextUniqueIdValue()
253{
254 ASSERT(mUniqueIdCounter < std::numeric_limits<int>::max());
255 return ++mUniqueIdCounter;
256}
257
Olli Etuaho29bda812018-01-26 17:37:36 +0200258void TSymbolTable::initializeBuiltIns(sh::GLenum type,
259 ShShaderSpec spec,
260 const ShBuiltInResources &resources)
261{
Olli Etuaho065aa862018-02-22 15:30:27 +0200262 mShaderType = type;
Olli Etuahob391ec42018-03-12 17:04:59 +0200263 mResources = resources;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200264
265 // We need just one precision stack level for predefined precisions.
266 mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel));
Olli Etuaho29bda812018-01-26 17:37:36 +0200267
268 switch (type)
269 {
270 case GL_FRAGMENT_SHADER:
271 setDefaultPrecision(EbtInt, EbpMedium);
272 break;
273 case GL_VERTEX_SHADER:
274 case GL_COMPUTE_SHADER:
275 case GL_GEOMETRY_SHADER_EXT:
276 setDefaultPrecision(EbtInt, EbpHigh);
277 setDefaultPrecision(EbtFloat, EbpHigh);
278 break;
279 default:
280 UNREACHABLE();
281 }
282 // Set defaults for sampler types that have default precision, even those that are
283 // only available if an extension exists.
284 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
285 initSamplerDefaultPrecision(EbtSampler2D);
286 initSamplerDefaultPrecision(EbtSamplerCube);
287 // SamplerExternalOES is specified in the extension to have default precision.
288 initSamplerDefaultPrecision(EbtSamplerExternalOES);
289 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
290 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
291 // It isn't specified whether Sampler2DRect has default precision.
292 initSamplerDefaultPrecision(EbtSampler2DRect);
293
294 setDefaultPrecision(EbtAtomicCounter, EbpHigh);
295
Olli Etuahob391ec42018-03-12 17:04:59 +0200296 initializeBuiltInVariables(type, spec, resources);
Olli Etuaho391bda22018-02-23 11:43:14 +0200297 mUniqueIdCounter = kLastBuiltInId + 1;
Olli Etuaho29bda812018-01-26 17:37:36 +0200298}
299
300void TSymbolTable::initSamplerDefaultPrecision(TBasicType samplerType)
301{
Olli Etuahoe600c0a2018-03-02 11:23:29 +0200302 ASSERT(samplerType >= EbtGuardSamplerBegin && samplerType <= EbtGuardSamplerEnd);
Olli Etuaho29bda812018-01-26 17:37:36 +0200303 setDefaultPrecision(samplerType, EbpLow);
304}
305
Jamie Madill45bcc782016-11-07 13:58:48 -0500306} // namespace sh