blob: efc1d8d72d986a42659e237143aa9a81db443a7b [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 Etuaho391bda22018-02-23 11:43:14 +020082TSymbolTable::TSymbolTable() : mUniqueIdCounter(0), mShaderType(GL_FRAGMENT_SHADER)
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020083{
84}
85
86TSymbolTable::~TSymbolTable() = default;
87
Olli Etuaho437664b2018-02-28 15:38:14 +020088bool TSymbolTable::isEmpty() const
89{
90 return mTable.empty();
91}
92
93bool TSymbolTable::atGlobalLevel() const
94{
95 return mTable.size() == 1u;
96}
97
Olli Etuahodd21ecf2018-01-10 12:42:09 +020098void TSymbolTable::push()
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000099{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200100 mTable.push_back(std::unique_ptr<TSymbolTableLevel>(new TSymbolTableLevel));
101 mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel));
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200102}
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000103
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200104void TSymbolTable::pop()
105{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200106 mTable.pop_back();
107 mPrecisionStack.pop_back();
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200108}
109
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200110const TFunction *TSymbolTable::markFunctionHasPrototypeDeclaration(
Olli Etuahofbb1c792018-01-19 16:26:59 +0200111 const ImmutableString &mangledName,
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200112 bool *hadPrototypeDeclarationOut)
113{
114 TFunction *function = findUserDefinedFunction(mangledName);
115 *hadPrototypeDeclarationOut = function->hasPrototypeDeclaration();
116 function->setHasPrototypeDeclaration();
117 return function;
118}
119
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200120const TFunction *TSymbolTable::setFunctionParameterNamesFromDefinition(const TFunction *function,
121 bool *wasDefinedOut)
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200122{
123 TFunction *firstDeclaration = findUserDefinedFunction(function->getMangledName());
124 ASSERT(firstDeclaration);
125 // Note: 'firstDeclaration' could be 'function' if this is the first time we've seen function as
126 // it would have just been put in the symbol table. Otherwise, we're looking up an earlier
127 // occurance.
128 if (function != firstDeclaration)
129 {
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200130 // The previous declaration should have the same parameters as the function definition
131 // (parameter names may differ).
132 firstDeclaration->shareParameters(*function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200133 }
134
135 *wasDefinedOut = firstDeclaration->isDefined();
136 firstDeclaration->setDefined();
137 return firstDeclaration;
138}
139
Olli Etuahofbb1c792018-01-19 16:26:59 +0200140const TSymbol *TSymbolTable::find(const ImmutableString &name, int shaderVersion) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200141{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200142 int userDefinedLevel = static_cast<int>(mTable.size()) - 1;
143 while (userDefinedLevel >= 0)
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000144 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200145 const TSymbol *symbol = mTable[userDefinedLevel]->find(name);
146 if (symbol)
147 {
148 return symbol;
149 }
150 userDefinedLevel--;
151 }
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000152
Olli Etuaho115b2c42018-03-15 17:46:29 +0200153 return findBuiltIn(name, shaderVersion);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000154}
155
Olli Etuahofbb1c792018-01-19 16:26:59 +0200156TFunction *TSymbolTable::findUserDefinedFunction(const ImmutableString &name) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200157{
158 // User-defined functions are always declared at the global level.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200159 ASSERT(!mTable.empty());
160 return static_cast<TFunction *>(mTable[0]->find(name));
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200161}
162
Olli Etuahofbb1c792018-01-19 16:26:59 +0200163const TSymbol *TSymbolTable::findGlobal(const ImmutableString &name) const
Zhenyao Mod7490962016-11-09 15:49:51 -0800164{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200165 ASSERT(!mTable.empty());
166 return mTable[0]->find(name);
Zhenyao Mod7490962016-11-09 15:49:51 -0800167}
168
Olli Etuaho437664b2018-02-28 15:38:14 +0200169bool TSymbolTable::declare(TSymbol *symbol)
Olli Etuaho0f684632017-07-13 12:42:15 +0300170{
Olli Etuaho437664b2018-02-28 15:38:14 +0200171 ASSERT(!mTable.empty());
172 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
173 ASSERT(!symbol->isFunction());
174 return mTable.back()->insert(symbol);
Jiawei Shaod8105a02017-08-08 09:54:36 +0800175}
176
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200177void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUnmangledName)
178{
Olli Etuaho437664b2018-02-28 15:38:14 +0200179 ASSERT(!mTable.empty());
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200180 if (insertUnmangledName)
181 {
182 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200183 mTable[0]->insertUnmangled(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200184 }
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200185 mTable[0]->insert(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200186}
187
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200188void TSymbolTable::setDefaultPrecision(TBasicType type, TPrecision prec)
189{
190 int indexOfLastElement = static_cast<int>(mPrecisionStack.size()) - 1;
191 // Uses map operator [], overwrites the current value
192 (*mPrecisionStack[indexOfLastElement])[type] = prec;
193}
194
Zhenyao Moe740add2014-07-18 17:01:01 -0700195TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700196{
197 if (!SupportsPrecision(type))
198 return EbpUndefined;
199
200 // unsigned integers use the same precision as signed
201 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
202
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200203 int level = static_cast<int>(mPrecisionStack.size()) - 1;
204 ASSERT(level >= 0); // Just to be safe. Should not happen.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200205 // If we dont find anything we return this. Some types don't have predefined default precision.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700206 TPrecision prec = EbpUndefined;
207 while (level >= 0)
208 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200209 PrecisionStackLevel::iterator it = mPrecisionStack[level]->find(baseType);
210 if (it != mPrecisionStack[level]->end())
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700211 {
212 prec = (*it).second;
213 break;
214 }
215 level--;
216 }
217 return prec;
218}
Jamie Madill45bcc782016-11-07 13:58:48 -0500219
Olli Etuahodefe3932018-02-13 11:56:09 +0200220void TSymbolTable::addInvariantVarying(const ImmutableString &originalName)
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200221{
222 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200223 mTable.back()->addInvariantVarying(originalName);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200224}
225
Olli Etuahodefe3932018-02-13 11:56:09 +0200226bool TSymbolTable::isVaryingInvariant(const ImmutableString &originalName) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200227{
228 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200229 return mTable.back()->isVaryingInvariant(originalName);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200230}
231
232void TSymbolTable::setGlobalInvariant(bool invariant)
233{
234 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200235 mTable.back()->setGlobalInvariant(invariant);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200236}
237
Olli Etuaho5d69db12017-11-24 16:51:15 +0200238void TSymbolTable::clearCompilationResults()
239{
Olli Etuaho391bda22018-02-23 11:43:14 +0200240 mUniqueIdCounter = kLastBuiltInId + 1;
Olli Etuaho5d69db12017-11-24 16:51:15 +0200241
242 // User-defined scopes should have already been cleared when the compilation finished.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200243 ASSERT(mTable.size() == 0u);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200244}
245
246int TSymbolTable::nextUniqueIdValue()
247{
248 ASSERT(mUniqueIdCounter < std::numeric_limits<int>::max());
249 return ++mUniqueIdCounter;
250}
251
Olli Etuaho29bda812018-01-26 17:37:36 +0200252void TSymbolTable::initializeBuiltIns(sh::GLenum type,
253 ShShaderSpec spec,
254 const ShBuiltInResources &resources)
255{
Olli Etuaho065aa862018-02-22 15:30:27 +0200256 mShaderType = type;
Olli Etuahob391ec42018-03-12 17:04:59 +0200257 mResources = resources;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200258
259 // We need just one precision stack level for predefined precisions.
260 mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel));
Olli Etuaho29bda812018-01-26 17:37:36 +0200261
262 switch (type)
263 {
264 case GL_FRAGMENT_SHADER:
265 setDefaultPrecision(EbtInt, EbpMedium);
266 break;
267 case GL_VERTEX_SHADER:
268 case GL_COMPUTE_SHADER:
269 case GL_GEOMETRY_SHADER_EXT:
270 setDefaultPrecision(EbtInt, EbpHigh);
271 setDefaultPrecision(EbtFloat, EbpHigh);
272 break;
273 default:
274 UNREACHABLE();
275 }
276 // Set defaults for sampler types that have default precision, even those that are
277 // only available if an extension exists.
278 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
279 initSamplerDefaultPrecision(EbtSampler2D);
280 initSamplerDefaultPrecision(EbtSamplerCube);
281 // SamplerExternalOES is specified in the extension to have default precision.
282 initSamplerDefaultPrecision(EbtSamplerExternalOES);
283 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
284 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
285 // It isn't specified whether Sampler2DRect has default precision.
286 initSamplerDefaultPrecision(EbtSampler2DRect);
287
288 setDefaultPrecision(EbtAtomicCounter, EbpHigh);
289
Olli Etuahob391ec42018-03-12 17:04:59 +0200290 initializeBuiltInVariables(type, spec, resources);
Olli Etuaho391bda22018-02-23 11:43:14 +0200291 mUniqueIdCounter = kLastBuiltInId + 1;
Olli Etuaho29bda812018-01-26 17:37:36 +0200292}
293
294void TSymbolTable::initSamplerDefaultPrecision(TBasicType samplerType)
295{
Olli Etuahoe600c0a2018-03-02 11:23:29 +0200296 ASSERT(samplerType >= EbtGuardSamplerBegin && samplerType <= EbtGuardSamplerEnd);
Olli Etuaho29bda812018-01-26 17:37:36 +0200297 setDefaultPrecision(samplerType, EbpLow);
298}
299
Jamie Madill45bcc782016-11-07 13:58:48 -0500300} // namespace sh