blob: f7486a2ecd304f48b37171fee35e624ab2d44abe [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens1fbc2872014-01-03 14:12:09 -05002// Copyright (c) 2002-2014 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//
6
Geoff Lang0a73dd82014-11-19 16:18:08 -05007#ifndef COMPILER_TRANSLATOR_SYMBOLTABLE_H_
8#define COMPILER_TRANSLATOR_SYMBOLTABLE_H_
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009
10//
11// Symbol table for parsing. Has these design characteristics:
12//
13// * Same symbol table can be used to compile many shaders, to preserve
14// effort of creating and loading with the large numbers of built-in
15// symbols.
16//
17// * Name mangling will be used to give each function a unique name
18// so that symbol table lookups are never ambiguous. This allows
19// a simpler symbol table structure.
20//
Jamie Madilld7b1ab52016-12-12 14:42:19 -050021// * Pushing and popping of scope, so symbol table will really be a stack
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000022// of symbol tables. Searched from the top, with new inserts going into
23// the top.
24//
25// * Constants: Compile time constant symbols will keep their values
26// in the symbol table. The parser can substitute constants at parse
27// time, including doing constant folding and constant propagation.
28//
29// * No temporaries: Temporaries made from operations (+, --, .xy, etc.)
30// are tracked in the intermediate representation, not the symbol table.
31//
32
Martin Radeve93d24e2016-07-28 12:06:05 +030033#include <array>
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000034#include <assert.h>
Zhenyao Mo94ac7b72014-10-15 18:22:08 -070035#include <set>
alokp@chromium.orge4249f02010-07-26 18:13:52 +000036
Jamie Madill703cdd62013-07-08 15:07:30 -040037#include "common/angleutils.h"
Olli Etuaho2a1e8f92017-07-14 11:49:36 +030038#include "compiler/translator/ExtensionBehavior.h"
Geoff Lang17732822013-08-29 13:46:49 -040039#include "compiler/translator/InfoSink.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040040#include "compiler/translator/IntermNode.h"
Olli Etuaho2d88e9b2017-07-21 16:52:03 +030041#include "compiler/translator/SymbolUniqueId.h"
alokp@chromium.org43884872010-03-30 00:08:52 +000042
Jamie Madill45bcc782016-11-07 13:58:48 -050043namespace sh
44{
45
Zhenyao Mo9eedea02014-05-12 16:02:35 -070046// Symbol base class. (Can build functions or variables out of these...)
Jamie Madillf0d10f82015-03-31 12:56:52 -040047class TSymbol : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -070048{
49 public:
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040050 POOL_ALLOCATOR_NEW_DELETE();
Olli Etuaho54a29ff2017-11-28 17:35:20 +020051 TSymbol(TSymbolTable *symbolTable, const TString *name);
Olli Etuaho476197f2016-10-11 13:59:08 +010052
Zhenyao Mo9eedea02014-05-12 16:02:35 -070053 virtual ~TSymbol()
54 {
55 // don't delete name, it's from the pool
56 }
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040057
Olli Etuaho54a29ff2017-11-28 17:35:20 +020058 const TString &name() const { return *mName; }
59 virtual const TString &getMangledName() const { return name(); }
Jamie Madilld7b1ab52016-12-12 14:42:19 -050060 virtual bool isFunction() const { return false; }
61 virtual bool isVariable() const { return false; }
Olli Etuaho035419f2017-11-28 14:27:15 +020062 virtual bool isStruct() const { return false; }
Olli Etuaho54a29ff2017-11-28 17:35:20 +020063 const TSymbolUniqueId &uniqueId() const { return mUniqueId; }
64 void relateToExtension(TExtension ext) { mExtension = ext; }
65 TExtension extension() const { return mExtension; }
Nicolas Capensba60ad32013-06-04 15:55:47 -040066
Olli Etuaho035419f2017-11-28 14:27:15 +020067 protected:
68 const TString *mName;
69
Zhenyao Mo9eedea02014-05-12 16:02:35 -070070 private:
Olli Etuaho54a29ff2017-11-28 17:35:20 +020071 const TSymbolUniqueId mUniqueId;
Olli Etuaho54a29ff2017-11-28 17:35:20 +020072 TExtension mExtension;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000073};
74
Olli Etuaho035419f2017-11-28 14:27:15 +020075// Variable.
Olli Etuaho0f684632017-07-13 12:42:15 +030076// May store the value of a constant variable of any type (float, int, bool or struct).
Zhenyao Mo9eedea02014-05-12 16:02:35 -070077class TVariable : public TSymbol
78{
79 public:
Corentin Walleze5a1f272015-08-21 02:58:25 +020080 ~TVariable() override {}
81 bool isVariable() const override { return true; }
Jamie Madilld7b1ab52016-12-12 14:42:19 -050082 TType &getType() { return type; }
83 const TType &getType() const { return type; }
Jamie Madilld7b1ab52016-12-12 14:42:19 -050084 void setQualifier(TQualifier qualifier) { type.setQualifier(qualifier); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000085
Olli Etuaho5c0e0232015-11-11 15:55:59 +020086 const TConstantUnion *getConstPointer() const { return unionArray; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087
Olli Etuaho5c0e0232015-11-11 15:55:59 +020088 void shareConstPointer(const TConstantUnion *constArray) { unionArray = constArray; }
alokp@chromium.org43884872010-03-30 00:08:52 +000089
Zhenyao Mo9eedea02014-05-12 16:02:35 -070090 private:
Olli Etuaho0f684632017-07-13 12:42:15 +030091 friend class TSymbolTable;
Olli Etuaho035419f2017-11-28 14:27:15 +020092 TVariable(TSymbolTable *symbolTable, const TString *name, const TType &t);
Olli Etuaho0f684632017-07-13 12:42:15 +030093
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094 TType type;
Olli Etuaho5c0e0232015-11-11 15:55:59 +020095 const TConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096};
97
Olli Etuaho035419f2017-11-28 14:27:15 +020098// Struct type.
99class TStructure : public TSymbol, public TFieldListCollection
100{
101 public:
102 TStructure(TSymbolTable *symbolTable, const TString *name, const TFieldList *fields);
103
104 bool isStruct() const override { return true; }
105
106 void createSamplerSymbols(const TString &namePrefix,
107 const TString &apiNamePrefix,
108 TVector<TIntermSymbol *> *outputSymbols,
109 TMap<TIntermSymbol *, TString> *outputSymbolsToAPINames,
110 TSymbolTable *symbolTable) const;
111
112 void setAtGlobalScope(bool atGlobalScope) { mAtGlobalScope = atGlobalScope; }
113 bool atGlobalScope() const { return mAtGlobalScope; }
114
115 private:
116 // TODO(zmo): Find a way to get rid of the const_cast in function
117 // setName(). At the moment keep this function private so only
118 // friend class RegenerateStructNames may call it.
119 friend class RegenerateStructNames;
120 void setName(const TString &name);
121
122 bool mAtGlobalScope;
123};
124
Olli Etuaho378c3a52017-12-04 11:32:13 +0200125// Interface block. Note that this contains the block name, not the instance name. Interface block
126// instances are stored as TVariable.
127class TInterfaceBlock : public TSymbol, public TFieldListCollection
128{
129 public:
130 TInterfaceBlock(TSymbolTable *symbolTable,
131 const TString *name,
132 const TFieldList *fields,
133 const TLayoutQualifier &layoutQualifier);
134
135 TLayoutBlockStorage blockStorage() const { return mBlockStorage; }
136 int blockBinding() const { return mBinding; }
137
138 private:
139 TLayoutBlockStorage mBlockStorage;
140 int mBinding;
141
142 // Note that we only record matrix packing on a per-field granularity.
143};
144
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700145// Immutable version of TParameter.
146struct TConstParameter
147{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500148 TConstParameter() : name(nullptr), type(nullptr) {}
149 explicit TConstParameter(const TString *n) : name(n), type(nullptr) {}
150 explicit TConstParameter(const TType *t) : name(nullptr), type(t) {}
151 TConstParameter(const TString *n, const TType *t) : name(n), type(t) {}
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700152
153 // Both constructor arguments must be const.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500154 TConstParameter(TString *n, TType *t) = delete;
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700155 TConstParameter(const TString *n, TType *t) = delete;
156 TConstParameter(TString *n, const TType *t) = delete;
157
Olli Etuahocce89652017-06-19 16:04:09 +0300158 const TString *const name;
159 const TType *const type;
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700160};
161
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000162// The function sub-class of symbols and the parser will need to
163// share this definition of a function parameter.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700164struct TParameter
165{
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700166 // Destructively converts to TConstParameter.
167 // This method resets name and type to nullptrs to make sure
168 // their content cannot be modified after the call.
169 TConstParameter turnToConst()
170 {
171 const TString *constName = name;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500172 const TType *constType = type;
173 name = nullptr;
174 type = nullptr;
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700175 return TConstParameter(constName, constType);
176 }
177
Olli Etuahocce89652017-06-19 16:04:09 +0300178 const TString *name;
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400179 TType *type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180};
181
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500182// The function sub-class of a symbol.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700183class TFunction : public TSymbol
184{
185 public:
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300186 TFunction(TSymbolTable *symbolTable,
187 const TString *name,
Olli Etuaho5d653182016-01-04 14:43:28 +0200188 const TType *retType,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300189 TOperator tOp = EOpNull,
190 TExtension ext = TExtension::UNDEFINED)
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300191 : TSymbol(symbolTable, name),
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700192 returnType(retType),
Dmitry Skiba58832202015-07-06 16:11:13 -0700193 mangledName(nullptr),
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700194 op(tOp),
Olli Etuaho5d653182016-01-04 14:43:28 +0200195 defined(false),
196 mHasPrototypeDeclaration(false)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700197 {
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500198 relateToExtension(ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700199 }
Corentin Walleze5a1f272015-08-21 02:58:25 +0200200 ~TFunction() override;
201 bool isFunction() const override { return true; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000202
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700203 void addParameter(const TConstParameter &p)
Dmitry Skiba58832202015-07-06 16:11:13 -0700204 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205 parameters.push_back(p);
Dmitry Skiba58832202015-07-06 16:11:13 -0700206 mangledName = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000208
Olli Etuaho476197f2016-10-11 13:59:08 +0100209 void swapParameters(const TFunction &parametersSource);
210
Corentin Walleze5a1f272015-08-21 02:58:25 +0200211 const TString &getMangledName() const override
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700212 {
Dmitry Skiba58832202015-07-06 16:11:13 -0700213 if (mangledName == nullptr)
214 {
215 mangledName = buildMangledName();
216 }
217 return *mangledName;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700218 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800219
Olli Etuahof2209f72017-04-01 12:45:55 +0300220 static const TString &GetMangledNameFromCall(const TString &functionName,
221 const TIntermSequence &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800222
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500223 const TType &getReturnType() const { return *returnType; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000224
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500225 TOperator getBuiltInOp() const { return op; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000226
Olli Etuaho5d653182016-01-04 14:43:28 +0200227 void setDefined() { defined = true; }
228 bool isDefined() { return defined; }
229 void setHasPrototypeDeclaration() { mHasPrototypeDeclaration = true; }
230 bool hasPrototypeDeclaration() const { return mHasPrototypeDeclaration; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000231
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500232 size_t getParamCount() const { return parameters.size(); }
233 const TConstParameter &getParam(size_t i) const { return parameters[i]; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000234
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700235 private:
Olli Etuaho476197f2016-10-11 13:59:08 +0100236 void clearParameters();
237
Dmitry Skiba58832202015-07-06 16:11:13 -0700238 const TString *buildMangledName() const;
239
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700240 typedef TVector<TConstParameter> TParamList;
alokp@chromium.org43884872010-03-30 00:08:52 +0000241 TParamList parameters;
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700242 const TType *returnType;
Dmitry Skiba58832202015-07-06 16:11:13 -0700243 mutable const TString *mangledName;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244 TOperator op;
245 bool defined;
Olli Etuaho5d653182016-01-04 14:43:28 +0200246 bool mHasPrototypeDeclaration;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000247};
248
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700249class TSymbolTableLevel
250{
251 public:
Kai Ninomiyad4556df2017-09-27 16:45:22 -0700252 typedef TUnorderedMap<TString, TSymbol *> tLevel;
alokp@chromium.org43884872010-03-30 00:08:52 +0000253 typedef tLevel::const_iterator const_iterator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000254 typedef const tLevel::value_type tLevelPair;
255 typedef std::pair<tLevel::iterator, bool> tInsertResult;
256
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500257 TSymbolTableLevel() : mGlobalInvariant(false) {}
alokp@chromium.org43884872010-03-30 00:08:52 +0000258 ~TSymbolTableLevel();
259
Nicolas Capensadfffe42014-06-17 02:13:36 -0400260 bool insert(TSymbol *symbol);
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400261
Olli Etuahob2983c92015-03-18 14:02:46 +0200262 // Insert a function using its unmangled name as the key.
263 bool insertUnmangled(TFunction *function);
264
Jamie Madillbfa91f42014-06-05 15:45:18 -0400265 TSymbol *find(const TString &name) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500267 void addInvariantVarying(const std::string &name) { mInvariantVaryings.insert(name); }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800268
269 bool isVaryingInvariant(const std::string &name)
270 {
271 return (mGlobalInvariant || mInvariantVaryings.count(name) > 0);
272 }
273
274 void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; }
275
Martin Radevda6254b2016-12-14 17:00:36 +0200276 void insertUnmangledBuiltInName(const std::string &name)
277 {
278 mUnmangledBuiltInNames.insert(name);
279 }
280
281 bool hasUnmangledBuiltIn(const std::string &name)
282 {
283 return mUnmangledBuiltInNames.count(name) > 0;
284 }
285
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700286 protected:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287 tLevel level;
Qiankun Miaof69682b2016-08-16 14:50:42 +0800288 std::set<std::string> mInvariantVaryings;
289 bool mGlobalInvariant;
Martin Radevda6254b2016-12-14 17:00:36 +0200290
291 private:
292 std::set<std::string> mUnmangledBuiltInNames;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293};
294
Gus Fernandez964df492014-10-13 11:54:39 -0700295// Define ESymbolLevel as int rather than an enum since level can go
296// above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the
297// compiler optimizes the >= of the last element to ==.
298typedef int ESymbolLevel;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500299const int COMMON_BUILTINS = 0;
300const int ESSL1_BUILTINS = 1;
301const int ESSL3_BUILTINS = 2;
Martin Radeve93d24e2016-07-28 12:06:05 +0300302const int ESSL3_1_BUILTINS = 3;
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300303// GLSL_BUILTINS are desktop GLSL builtins that don't exist in ESSL but are used to implement
304// features in ANGLE's GLSL backend. They're not visible to the parser.
305const int GLSL_BUILTINS = 4;
306const int LAST_BUILTIN_LEVEL = GLSL_BUILTINS;
307const int GLOBAL_LEVEL = 5;
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000308
Jamie Madillf0d10f82015-03-31 12:56:52 -0400309class TSymbolTable : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700310{
311 public:
Olli Etuaho5d69db12017-11-24 16:51:15 +0200312 TSymbolTable() : mUniqueIdCounter(0), mUserDefinedUniqueIdsStart(-1), mEmptySymbolId(this)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000314 // The symbol table cannot be used until push() is called, but
315 // the lack of an initial call to push() can be used to detect
316 // that the symbol table has not been preloaded with built-ins.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317 }
318
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400319 ~TSymbolTable();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000321 // When the symbol table is initialized with the built-ins, there should
322 // 'push' calls, so that built-ins are at level 0 and the shader
323 // globals are at level 1.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500324 bool isEmpty() const { return table.empty(); }
325 bool atBuiltInLevel() const { return currentLevel() <= LAST_BUILTIN_LEVEL; }
326 bool atGlobalLevel() const { return currentLevel() == GLOBAL_LEVEL; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000327 void push()
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000328 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329 table.push_back(new TSymbolTableLevel);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400330 precisionStack.push_back(new PrecisionStackLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331 }
332
alokp@chromium.org43884872010-03-30 00:08:52 +0000333 void pop()
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400334 {
335 delete table.back();
336 table.pop_back();
337
338 delete precisionStack.back();
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000339 precisionStack.pop_back();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340 }
341
Olli Etuaho0f684632017-07-13 12:42:15 +0300342 // The declare* entry points are used when parsing and declare symbols at the current scope.
Olli Etuaho035419f2017-11-28 14:27:15 +0200343 // They return the created symbol / true in case the declaration was successful, and nullptr /
344 // false if the declaration failed due to redefinition.
Olli Etuaho0f684632017-07-13 12:42:15 +0300345 TVariable *declareVariable(const TString *name, const TType &type);
Olli Etuaho035419f2017-11-28 14:27:15 +0200346 bool declareStructType(TStructure *str);
Olli Etuaho378c3a52017-12-04 11:32:13 +0200347 bool declareInterfaceBlock(TInterfaceBlock *interfaceBlock);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000348
Olli Etuaho0f684632017-07-13 12:42:15 +0300349 // The insert* entry points are used when initializing the symbol table with built-ins.
Olli Etuaho035419f2017-11-28 14:27:15 +0200350 // They return the created symbol / true in case the declaration was successful, and nullptr /
351 // false if the declaration failed due to redefinition.
Olli Etuaho0f684632017-07-13 12:42:15 +0300352 TVariable *insertVariable(ESymbolLevel level, const char *name, const TType &type);
353 TVariable *insertVariableExt(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300354 TExtension ext,
Olli Etuaho0f684632017-07-13 12:42:15 +0300355 const char *name,
356 const TType &type);
Olli Etuaho035419f2017-11-28 14:27:15 +0200357 bool insertStructType(ESymbolLevel level, TStructure *str);
Olli Etuaho378c3a52017-12-04 11:32:13 +0200358 bool insertInterfaceBlock(ESymbolLevel level, TInterfaceBlock *interfaceBlock);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500359
Martin Radeve93d24e2016-07-28 12:06:05 +0300360 bool insertConstInt(ESymbolLevel level, const char *name, int value, TPrecision precision)
Nicolas Capens49a88872013-06-20 09:54:03 -0400361 {
Martin Radeve93d24e2016-07-28 12:06:05 +0300362 TVariable *constant =
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300363 new TVariable(this, NewPoolTString(name), TType(EbtInt, precision, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200364 TConstantUnion *unionArray = new TConstantUnion[1];
365 unionArray[0].setIConst(value);
366 constant->shareConstPointer(unionArray);
Nicolas Capensadfffe42014-06-17 02:13:36 -0400367 return insert(level, constant);
Nicolas Capens49a88872013-06-20 09:54:03 -0400368 }
369
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800370 bool insertConstIntExt(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300371 TExtension ext,
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800372 const char *name,
373 int value,
374 TPrecision precision)
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300375 {
376 TVariable *constant =
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800377 new TVariable(this, NewPoolTString(name), TType(EbtInt, precision, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200378 TConstantUnion *unionArray = new TConstantUnion[1];
379 unionArray[0].setIConst(value);
380 constant->shareConstPointer(unionArray);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200381 constant->relateToExtension(ext);
382 return insert(level, constant);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300383 }
384
Martin Radeve93d24e2016-07-28 12:06:05 +0300385 bool insertConstIvec3(ESymbolLevel level,
386 const char *name,
387 const std::array<int, 3> &values,
388 TPrecision precision)
389 {
390 TVariable *constantIvec3 =
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300391 new TVariable(this, NewPoolTString(name), TType(EbtInt, precision, EvqConst, 3));
Martin Radeve93d24e2016-07-28 12:06:05 +0300392
393 TConstantUnion *unionArray = new TConstantUnion[3];
394 for (size_t index = 0u; index < 3u; ++index)
395 {
396 unionArray[index].setIConst(values[index]);
397 }
398 constantIvec3->shareConstPointer(unionArray);
399
400 return insert(level, constantIvec3);
401 }
402
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500403 void insertBuiltIn(ESymbolLevel level,
404 TOperator op,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300405 TExtension ext,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500406 const TType *rvalue,
407 const char *name,
408 const TType *ptype1,
409 const TType *ptype2 = 0,
410 const TType *ptype3 = 0,
411 const TType *ptype4 = 0,
412 const TType *ptype5 = 0);
Nicolas Capens759b9942014-02-14 17:57:14 -0500413
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500414 void insertBuiltIn(ESymbolLevel level,
415 const TType *rvalue,
416 const char *name,
417 const TType *ptype1,
418 const TType *ptype2 = 0,
419 const TType *ptype3 = 0,
420 const TType *ptype4 = 0,
421 const TType *ptype5 = 0)
Nicolas Capens482907e2015-02-23 16:56:33 -0500422 {
Martin Radevda6254b2016-12-14 17:00:36 +0200423 insertUnmangledBuiltInName(name, level);
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300424 insertBuiltIn(level, EOpNull, TExtension::UNDEFINED, rvalue, name, ptype1, ptype2, ptype3,
425 ptype4, ptype5);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500426 }
427
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500428 void insertBuiltIn(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300429 TExtension ext,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500430 const TType *rvalue,
431 const char *name,
432 const TType *ptype1,
433 const TType *ptype2 = 0,
434 const TType *ptype3 = 0,
435 const TType *ptype4 = 0,
436 const TType *ptype5 = 0)
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500437 {
Martin Radevda6254b2016-12-14 17:00:36 +0200438 insertUnmangledBuiltInName(name, level);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500439 insertBuiltIn(level, EOpNull, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
440 }
441
Olli Etuaho492cfab2017-01-20 21:18:29 +0000442 void insertBuiltInOp(ESymbolLevel level,
443 TOperator op,
444 const TType *rvalue,
445 const TType *ptype1,
446 const TType *ptype2 = 0,
447 const TType *ptype3 = 0,
448 const TType *ptype4 = 0,
449 const TType *ptype5 = 0);
450
451 void insertBuiltInOp(ESymbolLevel level,
452 TOperator op,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300453 TExtension ext,
Olli Etuaho492cfab2017-01-20 21:18:29 +0000454 const TType *rvalue,
455 const TType *ptype1,
456 const TType *ptype2 = 0,
457 const TType *ptype3 = 0,
458 const TType *ptype4 = 0,
459 const TType *ptype5 = 0);
Nicolas Capens482907e2015-02-23 16:56:33 -0500460
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300461 void insertBuiltInFunctionNoParameters(ESymbolLevel level,
462 TOperator op,
463 const TType *rvalue,
464 const char *name);
465
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800466 void insertBuiltInFunctionNoParametersExt(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300467 TExtension ext,
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800468 TOperator op,
469 const TType *rvalue,
470 const char *name);
471
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500472 TSymbol *find(const TString &name,
473 int shaderVersion,
Yunchao He4f285442017-04-21 12:15:49 +0800474 bool *builtIn = nullptr,
475 bool *sameScope = nullptr) const;
Zhenyao Mod7490962016-11-09 15:49:51 -0800476
477 TSymbol *findGlobal(const TString &name) const;
478
Zhenyao Moe740add2014-07-18 17:01:01 -0700479 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
Zhenyao Mod7490962016-11-09 15:49:51 -0800480
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300481 TSymbol *findBuiltIn(const TString &name, int shaderVersion, bool includeGLSLBuiltins) const;
482
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700483 TSymbolTableLevel *getOuterLevel()
484 {
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000485 assert(currentLevel() >= 1);
daniel@transgaming.com5dd6d092012-03-20 20:10:28 +0000486 return table[currentLevel() - 1];
487 }
488
Olli Etuahocce89652017-06-19 16:04:09 +0300489 void setDefaultPrecision(TBasicType type, TPrecision prec)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700490 {
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000491 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700492 // Uses map operator [], overwrites the current value
Olli Etuahocce89652017-06-19 16:04:09 +0300493 (*precisionStack[indexOfLastElement])[type] = prec;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000494 }
495
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700496 // Searches down the precisionStack for a precision qualifier
497 // for the specified TBasicType
Zhenyao Moe740add2014-07-18 17:01:01 -0700498 TPrecision getDefaultPrecision(TBasicType type) const;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000499
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700500 // This records invariant varyings declared through
501 // "invariant varying_name;".
Jamie Madill2c433252014-12-03 12:36:54 -0500502 void addInvariantVarying(const std::string &originalName)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700503 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800504 ASSERT(atGlobalLevel());
505 table[currentLevel()]->addInvariantVarying(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700506 }
507 // If this returns false, the varying could still be invariant
508 // if it is set as invariant during the varying variable
509 // declaration - this piece of information is stored in the
510 // variable's type, not here.
Jamie Madill2c433252014-12-03 12:36:54 -0500511 bool isVaryingInvariant(const std::string &originalName) const
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700512 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800513 ASSERT(atGlobalLevel());
514 return table[currentLevel()]->isVaryingInvariant(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700515 }
516
Qiankun Miaof69682b2016-08-16 14:50:42 +0800517 void setGlobalInvariant(bool invariant)
518 {
519 ASSERT(atGlobalLevel());
520 table[currentLevel()]->setGlobalInvariant(invariant);
521 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700522
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300523 const TSymbolUniqueId nextUniqueId() { return TSymbolUniqueId(this); }
524
525 // The empty symbol id is shared between all empty string ("") symbols. They are used in the
526 // AST for unused function parameters and struct type declarations that don't declare a
527 // variable, for example.
528 const TSymbolUniqueId &getEmptySymbolId() { return mEmptySymbolId; }
Jamie Madillbfa91f42014-06-05 15:45:18 -0400529
Martin Radevda6254b2016-12-14 17:00:36 +0200530 // Checks whether there is a built-in accessible by a shader with the specified version.
531 bool hasUnmangledBuiltInForShaderVersion(const char *name, int shaderVersion);
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530532
Olli Etuaho5d69db12017-11-24 16:51:15 +0200533 void markBuiltInInitializationFinished();
534 void clearCompilationResults();
535
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700536 private:
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300537 friend class TSymbolUniqueId;
Olli Etuaho5d69db12017-11-24 16:51:15 +0200538 int nextUniqueIdValue();
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300539
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500540 ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000541
Olli Etuaho0f684632017-07-13 12:42:15 +0300542 TVariable *insertVariable(ESymbolLevel level, const TString *name, const TType &type);
543
Olli Etuaho5d69db12017-11-24 16:51:15 +0200544 bool insert(ESymbolLevel level, TSymbol *symbol)
Olli Etuaho0f684632017-07-13 12:42:15 +0300545 {
Olli Etuaho5d69db12017-11-24 16:51:15 +0200546 ASSERT(level > LAST_BUILTIN_LEVEL || mUserDefinedUniqueIdsStart == -1);
Olli Etuaho0f684632017-07-13 12:42:15 +0300547 return table[level]->insert(symbol);
548 }
549
Martin Radevda6254b2016-12-14 17:00:36 +0200550 // Used to insert unmangled functions to check redeclaration of built-ins in ESSL 3.00 and
551 // above.
552 void insertUnmangledBuiltInName(const char *name, ESymbolLevel level);
553
554 bool hasUnmangledBuiltInAtLevel(const char *name, ESymbolLevel level);
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530555
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700556 std::vector<TSymbolTableLevel *> table;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400557 typedef TMap<TBasicType, TPrecision> PrecisionStackLevel;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500558 std::vector<PrecisionStackLevel *> precisionStack;
Jamie Madillbfa91f42014-06-05 15:45:18 -0400559
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300560 int mUniqueIdCounter;
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300561
Olli Etuaho5d69db12017-11-24 16:51:15 +0200562 // -1 before built-in init has finished, one past the last built-in id afterwards.
563 // TODO(oetuaho): Make this a compile-time constant once the symbol table is initialized at
564 // compile time. http://anglebug.com/1432
565 int mUserDefinedUniqueIdsStart;
566
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300567 const TSymbolUniqueId mEmptySymbolId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000568};
569
Jamie Madill45bcc782016-11-07 13:58:48 -0500570} // namespace sh
571
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500572#endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_