blob: 2b749d3f66e4b8ac8d00b726f2b0ac5d5d33cc18 [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
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700125// Immutable version of TParameter.
126struct TConstParameter
127{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500128 TConstParameter() : name(nullptr), type(nullptr) {}
129 explicit TConstParameter(const TString *n) : name(n), type(nullptr) {}
130 explicit TConstParameter(const TType *t) : name(nullptr), type(t) {}
131 TConstParameter(const TString *n, const TType *t) : name(n), type(t) {}
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700132
133 // Both constructor arguments must be const.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500134 TConstParameter(TString *n, TType *t) = delete;
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700135 TConstParameter(const TString *n, TType *t) = delete;
136 TConstParameter(TString *n, const TType *t) = delete;
137
Olli Etuahocce89652017-06-19 16:04:09 +0300138 const TString *const name;
139 const TType *const type;
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700140};
141
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142// The function sub-class of symbols and the parser will need to
143// share this definition of a function parameter.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700144struct TParameter
145{
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700146 // Destructively converts to TConstParameter.
147 // This method resets name and type to nullptrs to make sure
148 // their content cannot be modified after the call.
149 TConstParameter turnToConst()
150 {
151 const TString *constName = name;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500152 const TType *constType = type;
153 name = nullptr;
154 type = nullptr;
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700155 return TConstParameter(constName, constType);
156 }
157
Olli Etuahocce89652017-06-19 16:04:09 +0300158 const TString *name;
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400159 TType *type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160};
161
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500162// The function sub-class of a symbol.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700163class TFunction : public TSymbol
164{
165 public:
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300166 TFunction(TSymbolTable *symbolTable,
167 const TString *name,
Olli Etuaho5d653182016-01-04 14:43:28 +0200168 const TType *retType,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300169 TOperator tOp = EOpNull,
170 TExtension ext = TExtension::UNDEFINED)
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300171 : TSymbol(symbolTable, name),
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700172 returnType(retType),
Dmitry Skiba58832202015-07-06 16:11:13 -0700173 mangledName(nullptr),
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700174 op(tOp),
Olli Etuaho5d653182016-01-04 14:43:28 +0200175 defined(false),
176 mHasPrototypeDeclaration(false)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700177 {
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500178 relateToExtension(ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700179 }
Corentin Walleze5a1f272015-08-21 02:58:25 +0200180 ~TFunction() override;
181 bool isFunction() const override { return true; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000182
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700183 void addParameter(const TConstParameter &p)
Dmitry Skiba58832202015-07-06 16:11:13 -0700184 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000185 parameters.push_back(p);
Dmitry Skiba58832202015-07-06 16:11:13 -0700186 mangledName = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000187 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000188
Olli Etuaho476197f2016-10-11 13:59:08 +0100189 void swapParameters(const TFunction &parametersSource);
190
Corentin Walleze5a1f272015-08-21 02:58:25 +0200191 const TString &getMangledName() const override
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700192 {
Dmitry Skiba58832202015-07-06 16:11:13 -0700193 if (mangledName == nullptr)
194 {
195 mangledName = buildMangledName();
196 }
197 return *mangledName;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700198 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800199
Olli Etuahof2209f72017-04-01 12:45:55 +0300200 static const TString &GetMangledNameFromCall(const TString &functionName,
201 const TIntermSequence &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800202
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500203 const TType &getReturnType() const { return *returnType; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000204
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500205 TOperator getBuiltInOp() const { return op; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000206
Olli Etuaho5d653182016-01-04 14:43:28 +0200207 void setDefined() { defined = true; }
208 bool isDefined() { return defined; }
209 void setHasPrototypeDeclaration() { mHasPrototypeDeclaration = true; }
210 bool hasPrototypeDeclaration() const { return mHasPrototypeDeclaration; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500212 size_t getParamCount() const { return parameters.size(); }
213 const TConstParameter &getParam(size_t i) const { return parameters[i]; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000214
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700215 private:
Olli Etuaho476197f2016-10-11 13:59:08 +0100216 void clearParameters();
217
Dmitry Skiba58832202015-07-06 16:11:13 -0700218 const TString *buildMangledName() const;
219
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700220 typedef TVector<TConstParameter> TParamList;
alokp@chromium.org43884872010-03-30 00:08:52 +0000221 TParamList parameters;
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700222 const TType *returnType;
Dmitry Skiba58832202015-07-06 16:11:13 -0700223 mutable const TString *mangledName;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224 TOperator op;
225 bool defined;
Olli Etuaho5d653182016-01-04 14:43:28 +0200226 bool mHasPrototypeDeclaration;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000227};
228
Olli Etuaho035419f2017-11-28 14:27:15 +0200229// Reserved interface block name. Note that this simply reserves the block name, not the instance
230// name. Interface block instances are stored as TVariable.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000231class TInterfaceBlockName : public TSymbol
232{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700233 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500234 virtual ~TInterfaceBlockName() {}
Olli Etuaho0f684632017-07-13 12:42:15 +0300235
236 private:
237 friend class TSymbolTable;
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300238 TInterfaceBlockName(TSymbolTable *symbolTable, const TString *name) : TSymbol(symbolTable, name)
239 {
240 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000241};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000242
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700243class TSymbolTableLevel
244{
245 public:
Kai Ninomiyad4556df2017-09-27 16:45:22 -0700246 typedef TUnorderedMap<TString, TSymbol *> tLevel;
alokp@chromium.org43884872010-03-30 00:08:52 +0000247 typedef tLevel::const_iterator const_iterator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000248 typedef const tLevel::value_type tLevelPair;
249 typedef std::pair<tLevel::iterator, bool> tInsertResult;
250
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500251 TSymbolTableLevel() : mGlobalInvariant(false) {}
alokp@chromium.org43884872010-03-30 00:08:52 +0000252 ~TSymbolTableLevel();
253
Nicolas Capensadfffe42014-06-17 02:13:36 -0400254 bool insert(TSymbol *symbol);
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400255
Olli Etuahob2983c92015-03-18 14:02:46 +0200256 // Insert a function using its unmangled name as the key.
257 bool insertUnmangled(TFunction *function);
258
Jamie Madillbfa91f42014-06-05 15:45:18 -0400259 TSymbol *find(const TString &name) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500261 void addInvariantVarying(const std::string &name) { mInvariantVaryings.insert(name); }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800262
263 bool isVaryingInvariant(const std::string &name)
264 {
265 return (mGlobalInvariant || mInvariantVaryings.count(name) > 0);
266 }
267
268 void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; }
269
Martin Radevda6254b2016-12-14 17:00:36 +0200270 void insertUnmangledBuiltInName(const std::string &name)
271 {
272 mUnmangledBuiltInNames.insert(name);
273 }
274
275 bool hasUnmangledBuiltIn(const std::string &name)
276 {
277 return mUnmangledBuiltInNames.count(name) > 0;
278 }
279
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700280 protected:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000281 tLevel level;
Qiankun Miaof69682b2016-08-16 14:50:42 +0800282 std::set<std::string> mInvariantVaryings;
283 bool mGlobalInvariant;
Martin Radevda6254b2016-12-14 17:00:36 +0200284
285 private:
286 std::set<std::string> mUnmangledBuiltInNames;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287};
288
Gus Fernandez964df492014-10-13 11:54:39 -0700289// Define ESymbolLevel as int rather than an enum since level can go
290// above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the
291// compiler optimizes the >= of the last element to ==.
292typedef int ESymbolLevel;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500293const int COMMON_BUILTINS = 0;
294const int ESSL1_BUILTINS = 1;
295const int ESSL3_BUILTINS = 2;
Martin Radeve93d24e2016-07-28 12:06:05 +0300296const int ESSL3_1_BUILTINS = 3;
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300297// GLSL_BUILTINS are desktop GLSL builtins that don't exist in ESSL but are used to implement
298// features in ANGLE's GLSL backend. They're not visible to the parser.
299const int GLSL_BUILTINS = 4;
300const int LAST_BUILTIN_LEVEL = GLSL_BUILTINS;
301const int GLOBAL_LEVEL = 5;
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000302
Jamie Madillf0d10f82015-03-31 12:56:52 -0400303class TSymbolTable : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700304{
305 public:
Olli Etuaho5d69db12017-11-24 16:51:15 +0200306 TSymbolTable() : mUniqueIdCounter(0), mUserDefinedUniqueIdsStart(-1), mEmptySymbolId(this)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308 // The symbol table cannot be used until push() is called, but
309 // the lack of an initial call to push() can be used to detect
310 // that the symbol table has not been preloaded with built-ins.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311 }
312
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400313 ~TSymbolTable();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000314
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315 // When the symbol table is initialized with the built-ins, there should
316 // 'push' calls, so that built-ins are at level 0 and the shader
317 // globals are at level 1.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500318 bool isEmpty() const { return table.empty(); }
319 bool atBuiltInLevel() const { return currentLevel() <= LAST_BUILTIN_LEVEL; }
320 bool atGlobalLevel() const { return currentLevel() == GLOBAL_LEVEL; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000321 void push()
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000322 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323 table.push_back(new TSymbolTableLevel);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400324 precisionStack.push_back(new PrecisionStackLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325 }
326
alokp@chromium.org43884872010-03-30 00:08:52 +0000327 void pop()
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400328 {
329 delete table.back();
330 table.pop_back();
331
332 delete precisionStack.back();
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000333 precisionStack.pop_back();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000334 }
335
Olli Etuaho0f684632017-07-13 12:42:15 +0300336 // The declare* entry points are used when parsing and declare symbols at the current scope.
Olli Etuaho035419f2017-11-28 14:27:15 +0200337 // They return the created symbol / true in case the declaration was successful, and nullptr /
338 // false if the declaration failed due to redefinition.
Olli Etuaho0f684632017-07-13 12:42:15 +0300339 TVariable *declareVariable(const TString *name, const TType &type);
Olli Etuaho035419f2017-11-28 14:27:15 +0200340 bool declareStructType(TStructure *str);
Olli Etuaho0f684632017-07-13 12:42:15 +0300341 TInterfaceBlockName *declareInterfaceBlockName(const TString *name);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000342
Olli Etuaho0f684632017-07-13 12:42:15 +0300343 // The insert* entry points are used when initializing the symbol table with built-ins.
Olli Etuaho035419f2017-11-28 14:27:15 +0200344 // They return the created symbol / true in case the declaration was successful, and nullptr /
345 // false if the declaration failed due to redefinition.
Olli Etuaho0f684632017-07-13 12:42:15 +0300346 TVariable *insertVariable(ESymbolLevel level, const char *name, const TType &type);
347 TVariable *insertVariableExt(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300348 TExtension ext,
Olli Etuaho0f684632017-07-13 12:42:15 +0300349 const char *name,
350 const TType &type);
Olli Etuaho035419f2017-11-28 14:27:15 +0200351 bool insertStructType(ESymbolLevel level, TStructure *str);
Jiawei Shaod8105a02017-08-08 09:54:36 +0800352 TInterfaceBlockName *insertInterfaceBlockNameExt(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300353 TExtension ext,
Jiawei Shaod8105a02017-08-08 09:54:36 +0800354 const TString *name);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500355
Martin Radeve93d24e2016-07-28 12:06:05 +0300356 bool insertConstInt(ESymbolLevel level, const char *name, int value, TPrecision precision)
Nicolas Capens49a88872013-06-20 09:54:03 -0400357 {
Martin Radeve93d24e2016-07-28 12:06:05 +0300358 TVariable *constant =
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300359 new TVariable(this, NewPoolTString(name), TType(EbtInt, precision, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200360 TConstantUnion *unionArray = new TConstantUnion[1];
361 unionArray[0].setIConst(value);
362 constant->shareConstPointer(unionArray);
Nicolas Capensadfffe42014-06-17 02:13:36 -0400363 return insert(level, constant);
Nicolas Capens49a88872013-06-20 09:54:03 -0400364 }
365
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800366 bool insertConstIntExt(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300367 TExtension ext,
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800368 const char *name,
369 int value,
370 TPrecision precision)
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300371 {
372 TVariable *constant =
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800373 new TVariable(this, NewPoolTString(name), TType(EbtInt, precision, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200374 TConstantUnion *unionArray = new TConstantUnion[1];
375 unionArray[0].setIConst(value);
376 constant->shareConstPointer(unionArray);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200377 constant->relateToExtension(ext);
378 return insert(level, constant);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300379 }
380
Martin Radeve93d24e2016-07-28 12:06:05 +0300381 bool insertConstIvec3(ESymbolLevel level,
382 const char *name,
383 const std::array<int, 3> &values,
384 TPrecision precision)
385 {
386 TVariable *constantIvec3 =
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300387 new TVariable(this, NewPoolTString(name), TType(EbtInt, precision, EvqConst, 3));
Martin Radeve93d24e2016-07-28 12:06:05 +0300388
389 TConstantUnion *unionArray = new TConstantUnion[3];
390 for (size_t index = 0u; index < 3u; ++index)
391 {
392 unionArray[index].setIConst(values[index]);
393 }
394 constantIvec3->shareConstPointer(unionArray);
395
396 return insert(level, constantIvec3);
397 }
398
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500399 void insertBuiltIn(ESymbolLevel level,
400 TOperator op,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300401 TExtension ext,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500402 const TType *rvalue,
403 const char *name,
404 const TType *ptype1,
405 const TType *ptype2 = 0,
406 const TType *ptype3 = 0,
407 const TType *ptype4 = 0,
408 const TType *ptype5 = 0);
Nicolas Capens759b9942014-02-14 17:57:14 -0500409
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500410 void insertBuiltIn(ESymbolLevel level,
411 const TType *rvalue,
412 const char *name,
413 const TType *ptype1,
414 const TType *ptype2 = 0,
415 const TType *ptype3 = 0,
416 const TType *ptype4 = 0,
417 const TType *ptype5 = 0)
Nicolas Capens482907e2015-02-23 16:56:33 -0500418 {
Martin Radevda6254b2016-12-14 17:00:36 +0200419 insertUnmangledBuiltInName(name, level);
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300420 insertBuiltIn(level, EOpNull, TExtension::UNDEFINED, rvalue, name, ptype1, ptype2, ptype3,
421 ptype4, ptype5);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500422 }
423
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500424 void insertBuiltIn(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300425 TExtension ext,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500426 const TType *rvalue,
427 const char *name,
428 const TType *ptype1,
429 const TType *ptype2 = 0,
430 const TType *ptype3 = 0,
431 const TType *ptype4 = 0,
432 const TType *ptype5 = 0)
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500433 {
Martin Radevda6254b2016-12-14 17:00:36 +0200434 insertUnmangledBuiltInName(name, level);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500435 insertBuiltIn(level, EOpNull, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
436 }
437
Olli Etuaho492cfab2017-01-20 21:18:29 +0000438 void insertBuiltInOp(ESymbolLevel level,
439 TOperator op,
440 const TType *rvalue,
441 const TType *ptype1,
442 const TType *ptype2 = 0,
443 const TType *ptype3 = 0,
444 const TType *ptype4 = 0,
445 const TType *ptype5 = 0);
446
447 void insertBuiltInOp(ESymbolLevel level,
448 TOperator op,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300449 TExtension ext,
Olli Etuaho492cfab2017-01-20 21:18:29 +0000450 const TType *rvalue,
451 const TType *ptype1,
452 const TType *ptype2 = 0,
453 const TType *ptype3 = 0,
454 const TType *ptype4 = 0,
455 const TType *ptype5 = 0);
Nicolas Capens482907e2015-02-23 16:56:33 -0500456
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300457 void insertBuiltInFunctionNoParameters(ESymbolLevel level,
458 TOperator op,
459 const TType *rvalue,
460 const char *name);
461
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800462 void insertBuiltInFunctionNoParametersExt(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300463 TExtension ext,
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800464 TOperator op,
465 const TType *rvalue,
466 const char *name);
467
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500468 TSymbol *find(const TString &name,
469 int shaderVersion,
Yunchao He4f285442017-04-21 12:15:49 +0800470 bool *builtIn = nullptr,
471 bool *sameScope = nullptr) const;
Zhenyao Mod7490962016-11-09 15:49:51 -0800472
473 TSymbol *findGlobal(const TString &name) const;
474
Zhenyao Moe740add2014-07-18 17:01:01 -0700475 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
Zhenyao Mod7490962016-11-09 15:49:51 -0800476
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300477 TSymbol *findBuiltIn(const TString &name, int shaderVersion, bool includeGLSLBuiltins) const;
478
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700479 TSymbolTableLevel *getOuterLevel()
480 {
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000481 assert(currentLevel() >= 1);
daniel@transgaming.com5dd6d092012-03-20 20:10:28 +0000482 return table[currentLevel() - 1];
483 }
484
Olli Etuahocce89652017-06-19 16:04:09 +0300485 void setDefaultPrecision(TBasicType type, TPrecision prec)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700486 {
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000487 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700488 // Uses map operator [], overwrites the current value
Olli Etuahocce89652017-06-19 16:04:09 +0300489 (*precisionStack[indexOfLastElement])[type] = prec;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000490 }
491
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700492 // Searches down the precisionStack for a precision qualifier
493 // for the specified TBasicType
Zhenyao Moe740add2014-07-18 17:01:01 -0700494 TPrecision getDefaultPrecision(TBasicType type) const;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000495
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700496 // This records invariant varyings declared through
497 // "invariant varying_name;".
Jamie Madill2c433252014-12-03 12:36:54 -0500498 void addInvariantVarying(const std::string &originalName)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700499 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800500 ASSERT(atGlobalLevel());
501 table[currentLevel()]->addInvariantVarying(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700502 }
503 // If this returns false, the varying could still be invariant
504 // if it is set as invariant during the varying variable
505 // declaration - this piece of information is stored in the
506 // variable's type, not here.
Jamie Madill2c433252014-12-03 12:36:54 -0500507 bool isVaryingInvariant(const std::string &originalName) const
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700508 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800509 ASSERT(atGlobalLevel());
510 return table[currentLevel()]->isVaryingInvariant(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700511 }
512
Qiankun Miaof69682b2016-08-16 14:50:42 +0800513 void setGlobalInvariant(bool invariant)
514 {
515 ASSERT(atGlobalLevel());
516 table[currentLevel()]->setGlobalInvariant(invariant);
517 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700518
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300519 const TSymbolUniqueId nextUniqueId() { return TSymbolUniqueId(this); }
520
521 // The empty symbol id is shared between all empty string ("") symbols. They are used in the
522 // AST for unused function parameters and struct type declarations that don't declare a
523 // variable, for example.
524 const TSymbolUniqueId &getEmptySymbolId() { return mEmptySymbolId; }
Jamie Madillbfa91f42014-06-05 15:45:18 -0400525
Martin Radevda6254b2016-12-14 17:00:36 +0200526 // Checks whether there is a built-in accessible by a shader with the specified version.
527 bool hasUnmangledBuiltInForShaderVersion(const char *name, int shaderVersion);
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530528
Olli Etuaho5d69db12017-11-24 16:51:15 +0200529 void markBuiltInInitializationFinished();
530 void clearCompilationResults();
531
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700532 private:
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300533 friend class TSymbolUniqueId;
Olli Etuaho5d69db12017-11-24 16:51:15 +0200534 int nextUniqueIdValue();
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300535
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500536 ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000537
Olli Etuaho0f684632017-07-13 12:42:15 +0300538 TVariable *insertVariable(ESymbolLevel level, const TString *name, const TType &type);
539
Olli Etuaho5d69db12017-11-24 16:51:15 +0200540 bool insert(ESymbolLevel level, TSymbol *symbol)
Olli Etuaho0f684632017-07-13 12:42:15 +0300541 {
Olli Etuaho5d69db12017-11-24 16:51:15 +0200542 ASSERT(level > LAST_BUILTIN_LEVEL || mUserDefinedUniqueIdsStart == -1);
Olli Etuaho0f684632017-07-13 12:42:15 +0300543 return table[level]->insert(symbol);
544 }
545
Martin Radevda6254b2016-12-14 17:00:36 +0200546 // Used to insert unmangled functions to check redeclaration of built-ins in ESSL 3.00 and
547 // above.
548 void insertUnmangledBuiltInName(const char *name, ESymbolLevel level);
549
550 bool hasUnmangledBuiltInAtLevel(const char *name, ESymbolLevel level);
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530551
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700552 std::vector<TSymbolTableLevel *> table;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400553 typedef TMap<TBasicType, TPrecision> PrecisionStackLevel;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500554 std::vector<PrecisionStackLevel *> precisionStack;
Jamie Madillbfa91f42014-06-05 15:45:18 -0400555
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300556 int mUniqueIdCounter;
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300557
Olli Etuaho5d69db12017-11-24 16:51:15 +0200558 // -1 before built-in init has finished, one past the last built-in id afterwards.
559 // TODO(oetuaho): Make this a compile-time constant once the symbol table is initialized at
560 // compile time. http://anglebug.com/1432
561 int mUserDefinedUniqueIdsStart;
562
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300563 const TSymbolUniqueId mEmptySymbolId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564};
565
Jamie Madill45bcc782016-11-07 13:58:48 -0500566} // namespace sh
567
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500568#endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_