blob: 48c2dfe82b56676432d82537043ba70080993439 [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"
Geoff Lang17732822013-08-29 13:46:49 -040038#include "compiler/translator/InfoSink.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040039#include "compiler/translator/IntermNode.h"
alokp@chromium.org43884872010-03-30 00:08:52 +000040
Jamie Madill45bcc782016-11-07 13:58:48 -050041namespace sh
42{
43
Olli Etuahofe486322017-03-21 09:30:54 +000044// Encapsulates a unique id for a symbol.
45class TSymbolUniqueId
46{
47 public:
48 POOL_ALLOCATOR_NEW_DELETE();
49 TSymbolUniqueId();
50 TSymbolUniqueId(const TSymbol &symbol);
51 TSymbolUniqueId(const TSymbolUniqueId &) = default;
52 TSymbolUniqueId &operator=(const TSymbolUniqueId &) = default;
53
54 int get() const;
55
56 private:
57 int mId;
58};
59
Zhenyao Mo9eedea02014-05-12 16:02:35 -070060// Symbol base class. (Can build functions or variables out of these...)
Jamie Madillf0d10f82015-03-31 12:56:52 -040061class TSymbol : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -070062{
63 public:
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040064 POOL_ALLOCATOR_NEW_DELETE();
Olli Etuaho476197f2016-10-11 13:59:08 +010065 TSymbol(const TString *n);
66
Zhenyao Mo9eedea02014-05-12 16:02:35 -070067 virtual ~TSymbol()
68 {
69 // don't delete name, it's from the pool
70 }
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040071
Jamie Madilld7b1ab52016-12-12 14:42:19 -050072 const TString &getName() const { return *name; }
73 virtual const TString &getMangledName() const { return getName(); }
74 virtual bool isFunction() const { return false; }
75 virtual bool isVariable() const { return false; }
76 int getUniqueId() const { return uniqueId; }
77 void relateToExtension(const TString &ext) { extension = ext; }
78 const TString &getExtension() const { return extension; }
Nicolas Capensba60ad32013-06-04 15:55:47 -040079
Zhenyao Mo9eedea02014-05-12 16:02:35 -070080 private:
Olli Etuaho476197f2016-10-11 13:59:08 +010081 const int uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000082 const TString *name;
Jamie Madill2aeb26a2013-07-08 14:02:55 -040083 TString extension;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000084};
85
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086// Variable class, meaning a symbol that's not a function.
Jamie Madilld7b1ab52016-12-12 14:42:19 -050087//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088// There could be a separate class heirarchy for Constant variables;
89// Only one of int, bool, or float, (or none) is correct for
90// any particular use, but it's easy to do this way, and doesn't
91// seem worth having separate classes, and "getConst" can't simply return
Jamie Madilld7b1ab52016-12-12 14:42:19 -050092// different values for different types polymorphically, so this is
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093// just simple and pragmatic.
Zhenyao Mo9eedea02014-05-12 16:02:35 -070094class TVariable : public TSymbol
95{
96 public:
97 TVariable(const TString *name, const TType &t, bool uT = false)
Jamie Madilld7b1ab52016-12-12 14:42:19 -050098 : TSymbol(name), type(t), userType(uT), unionArray(0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -070099 {
100 }
Corentin Walleze5a1f272015-08-21 02:58:25 +0200101 ~TVariable() override {}
102 bool isVariable() const override { return true; }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500103 TType &getType() { return type; }
104 const TType &getType() const { return type; }
105 bool isUserType() const { return userType; }
106 void setQualifier(TQualifier qualifier) { type.setQualifier(qualifier); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200108 const TConstantUnion *getConstPointer() const { return unionArray; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200110 void shareConstPointer(const TConstantUnion *constArray) { unionArray = constArray; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000111
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700112 private:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113 TType type;
114 bool userType;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700115 // we are assuming that Pool Allocator will free the memory
116 // allocated to unionArray when this object is destroyed.
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200117 const TConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118};
119
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700120// Immutable version of TParameter.
121struct TConstParameter
122{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500123 TConstParameter() : name(nullptr), type(nullptr) {}
124 explicit TConstParameter(const TString *n) : name(n), type(nullptr) {}
125 explicit TConstParameter(const TType *t) : name(nullptr), type(t) {}
126 TConstParameter(const TString *n, const TType *t) : name(n), type(t) {}
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700127
128 // Both constructor arguments must be const.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500129 TConstParameter(TString *n, TType *t) = delete;
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700130 TConstParameter(const TString *n, TType *t) = delete;
131 TConstParameter(TString *n, const TType *t) = delete;
132
Olli Etuahocce89652017-06-19 16:04:09 +0300133 const TString *const name;
134 const TType *const type;
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700135};
136
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137// The function sub-class of symbols and the parser will need to
138// share this definition of a function parameter.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700139struct TParameter
140{
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700141 // Destructively converts to TConstParameter.
142 // This method resets name and type to nullptrs to make sure
143 // their content cannot be modified after the call.
144 TConstParameter turnToConst()
145 {
146 const TString *constName = name;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500147 const TType *constType = type;
148 name = nullptr;
149 type = nullptr;
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700150 return TConstParameter(constName, constType);
151 }
152
Olli Etuahocce89652017-06-19 16:04:09 +0300153 const TString *name;
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400154 TType *type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000155};
156
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500157// The function sub-class of a symbol.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700158class TFunction : public TSymbol
159{
160 public:
Olli Etuaho5d653182016-01-04 14:43:28 +0200161 TFunction(const TString *name,
162 const TType *retType,
163 TOperator tOp = EOpNull,
164 const char *ext = "")
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700165 : TSymbol(name),
166 returnType(retType),
Dmitry Skiba58832202015-07-06 16:11:13 -0700167 mangledName(nullptr),
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700168 op(tOp),
Olli Etuaho5d653182016-01-04 14:43:28 +0200169 defined(false),
170 mHasPrototypeDeclaration(false)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700171 {
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500172 relateToExtension(ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700173 }
Corentin Walleze5a1f272015-08-21 02:58:25 +0200174 ~TFunction() override;
175 bool isFunction() const override { return true; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000176
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700177 void addParameter(const TConstParameter &p)
Dmitry Skiba58832202015-07-06 16:11:13 -0700178 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000179 parameters.push_back(p);
Dmitry Skiba58832202015-07-06 16:11:13 -0700180 mangledName = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000182
Olli Etuaho476197f2016-10-11 13:59:08 +0100183 void swapParameters(const TFunction &parametersSource);
184
Corentin Walleze5a1f272015-08-21 02:58:25 +0200185 const TString &getMangledName() const override
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700186 {
Dmitry Skiba58832202015-07-06 16:11:13 -0700187 if (mangledName == nullptr)
188 {
189 mangledName = buildMangledName();
190 }
191 return *mangledName;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700192 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800193
Olli Etuahof2209f72017-04-01 12:45:55 +0300194 static const TString &GetMangledNameFromCall(const TString &functionName,
195 const TIntermSequence &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800196
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500197 const TType &getReturnType() const { return *returnType; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000198
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500199 TOperator getBuiltInOp() const { return op; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000200
Olli Etuaho5d653182016-01-04 14:43:28 +0200201 void setDefined() { defined = true; }
202 bool isDefined() { return defined; }
203 void setHasPrototypeDeclaration() { mHasPrototypeDeclaration = true; }
204 bool hasPrototypeDeclaration() const { return mHasPrototypeDeclaration; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500206 size_t getParamCount() const { return parameters.size(); }
207 const TConstParameter &getParam(size_t i) const { return parameters[i]; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000208
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700209 private:
Olli Etuaho476197f2016-10-11 13:59:08 +0100210 void clearParameters();
211
Dmitry Skiba58832202015-07-06 16:11:13 -0700212 const TString *buildMangledName() const;
213
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700214 typedef TVector<TConstParameter> TParamList;
alokp@chromium.org43884872010-03-30 00:08:52 +0000215 TParamList parameters;
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700216 const TType *returnType;
Dmitry Skiba58832202015-07-06 16:11:13 -0700217 mutable const TString *mangledName;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218 TOperator op;
219 bool defined;
Olli Etuaho5d653182016-01-04 14:43:28 +0200220 bool mHasPrototypeDeclaration;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221};
222
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000223// Interface block name sub-symbol
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000224class TInterfaceBlockName : public TSymbol
225{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700226 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500227 TInterfaceBlockName(const TString *name) : TSymbol(name) {}
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000228
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500229 virtual ~TInterfaceBlockName() {}
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000230};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000231
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700232class TSymbolTableLevel
233{
234 public:
235 typedef TMap<TString, TSymbol *> tLevel;
alokp@chromium.org43884872010-03-30 00:08:52 +0000236 typedef tLevel::const_iterator const_iterator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000237 typedef const tLevel::value_type tLevelPair;
238 typedef std::pair<tLevel::iterator, bool> tInsertResult;
239
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500240 TSymbolTableLevel() : mGlobalInvariant(false) {}
alokp@chromium.org43884872010-03-30 00:08:52 +0000241 ~TSymbolTableLevel();
242
Nicolas Capensadfffe42014-06-17 02:13:36 -0400243 bool insert(TSymbol *symbol);
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400244
Olli Etuahob2983c92015-03-18 14:02:46 +0200245 // Insert a function using its unmangled name as the key.
246 bool insertUnmangled(TFunction *function);
247
Jamie Madillbfa91f42014-06-05 15:45:18 -0400248 TSymbol *find(const TString &name) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000249
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500250 void addInvariantVarying(const std::string &name) { mInvariantVaryings.insert(name); }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800251
252 bool isVaryingInvariant(const std::string &name)
253 {
254 return (mGlobalInvariant || mInvariantVaryings.count(name) > 0);
255 }
256
257 void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; }
258
Martin Radevda6254b2016-12-14 17:00:36 +0200259 void insertUnmangledBuiltInName(const std::string &name)
260 {
261 mUnmangledBuiltInNames.insert(name);
262 }
263
264 bool hasUnmangledBuiltIn(const std::string &name)
265 {
266 return mUnmangledBuiltInNames.count(name) > 0;
267 }
268
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700269 protected:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270 tLevel level;
Qiankun Miaof69682b2016-08-16 14:50:42 +0800271 std::set<std::string> mInvariantVaryings;
272 bool mGlobalInvariant;
Martin Radevda6254b2016-12-14 17:00:36 +0200273
274 private:
275 std::set<std::string> mUnmangledBuiltInNames;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000276};
277
Gus Fernandez964df492014-10-13 11:54:39 -0700278// Define ESymbolLevel as int rather than an enum since level can go
279// above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the
280// compiler optimizes the >= of the last element to ==.
281typedef int ESymbolLevel;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500282const int COMMON_BUILTINS = 0;
283const int ESSL1_BUILTINS = 1;
284const int ESSL3_BUILTINS = 2;
Martin Radeve93d24e2016-07-28 12:06:05 +0300285const int ESSL3_1_BUILTINS = 3;
286const int LAST_BUILTIN_LEVEL = ESSL3_1_BUILTINS;
287const int GLOBAL_LEVEL = 4;
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000288
Jamie Madillf0d10f82015-03-31 12:56:52 -0400289class TSymbolTable : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700290{
291 public:
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400292 TSymbolTable()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294 // The symbol table cannot be used until push() is called, but
295 // the lack of an initial call to push() can be used to detect
296 // that the symbol table has not been preloaded with built-ins.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297 }
298
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400299 ~TSymbolTable();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301 // When the symbol table is initialized with the built-ins, there should
302 // 'push' calls, so that built-ins are at level 0 and the shader
303 // globals are at level 1.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500304 bool isEmpty() const { return table.empty(); }
305 bool atBuiltInLevel() const { return currentLevel() <= LAST_BUILTIN_LEVEL; }
306 bool atGlobalLevel() const { return currentLevel() == GLOBAL_LEVEL; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000307 void push()
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000308 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309 table.push_back(new TSymbolTableLevel);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400310 precisionStack.push_back(new PrecisionStackLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311 }
312
alokp@chromium.org43884872010-03-30 00:08:52 +0000313 void pop()
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400314 {
315 delete table.back();
316 table.pop_back();
317
318 delete precisionStack.back();
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000319 precisionStack.pop_back();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320 }
321
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500322 bool declare(TSymbol *symbol) { return insert(currentLevel(), symbol); }
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000323
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500324 bool insert(ESymbolLevel level, TSymbol *symbol) { return table[level]->insert(symbol); }
alokp@chromium.org43884872010-03-30 00:08:52 +0000325
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500326 bool insert(ESymbolLevel level, const char *ext, TSymbol *symbol)
327 {
328 symbol->relateToExtension(ext);
329 return table[level]->insert(symbol);
330 }
331
Martin Radeve93d24e2016-07-28 12:06:05 +0300332 bool insertConstInt(ESymbolLevel level, const char *name, int value, TPrecision precision)
Nicolas Capens49a88872013-06-20 09:54:03 -0400333 {
Martin Radeve93d24e2016-07-28 12:06:05 +0300334 TVariable *constant =
335 new TVariable(NewPoolTString(name), TType(EbtInt, precision, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200336 TConstantUnion *unionArray = new TConstantUnion[1];
337 unionArray[0].setIConst(value);
338 constant->shareConstPointer(unionArray);
Nicolas Capensadfffe42014-06-17 02:13:36 -0400339 return insert(level, constant);
Nicolas Capens49a88872013-06-20 09:54:03 -0400340 }
341
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300342 bool insertConstIntExt(ESymbolLevel level, const char *ext, const char *name, int value)
343 {
344 TVariable *constant =
345 new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200346 TConstantUnion *unionArray = new TConstantUnion[1];
347 unionArray[0].setIConst(value);
348 constant->shareConstPointer(unionArray);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300349 return insert(level, ext, constant);
350 }
351
Martin Radeve93d24e2016-07-28 12:06:05 +0300352 bool insertConstIvec3(ESymbolLevel level,
353 const char *name,
354 const std::array<int, 3> &values,
355 TPrecision precision)
356 {
357 TVariable *constantIvec3 =
358 new TVariable(NewPoolTString(name), TType(EbtInt, precision, EvqConst, 3));
359
360 TConstantUnion *unionArray = new TConstantUnion[3];
361 for (size_t index = 0u; index < 3u; ++index)
362 {
363 unionArray[index].setIConst(values[index]);
364 }
365 constantIvec3->shareConstPointer(unionArray);
366
367 return insert(level, constantIvec3);
368 }
369
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500370 void insertBuiltIn(ESymbolLevel level,
371 TOperator op,
372 const char *ext,
373 const TType *rvalue,
374 const char *name,
375 const TType *ptype1,
376 const TType *ptype2 = 0,
377 const TType *ptype3 = 0,
378 const TType *ptype4 = 0,
379 const TType *ptype5 = 0);
Nicolas Capens759b9942014-02-14 17:57:14 -0500380
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500381 void insertBuiltIn(ESymbolLevel level,
382 const TType *rvalue,
383 const char *name,
384 const TType *ptype1,
385 const TType *ptype2 = 0,
386 const TType *ptype3 = 0,
387 const TType *ptype4 = 0,
388 const TType *ptype5 = 0)
Nicolas Capens482907e2015-02-23 16:56:33 -0500389 {
Martin Radevda6254b2016-12-14 17:00:36 +0200390 insertUnmangledBuiltInName(name, level);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500391 insertBuiltIn(level, EOpNull, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
392 }
393
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500394 void insertBuiltIn(ESymbolLevel level,
395 const char *ext,
396 const TType *rvalue,
397 const char *name,
398 const TType *ptype1,
399 const TType *ptype2 = 0,
400 const TType *ptype3 = 0,
401 const TType *ptype4 = 0,
402 const TType *ptype5 = 0)
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500403 {
Martin Radevda6254b2016-12-14 17:00:36 +0200404 insertUnmangledBuiltInName(name, level);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500405 insertBuiltIn(level, EOpNull, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
406 }
407
Olli Etuaho492cfab2017-01-20 21:18:29 +0000408 void insertBuiltInOp(ESymbolLevel level,
409 TOperator op,
410 const TType *rvalue,
411 const TType *ptype1,
412 const TType *ptype2 = 0,
413 const TType *ptype3 = 0,
414 const TType *ptype4 = 0,
415 const TType *ptype5 = 0);
416
417 void insertBuiltInOp(ESymbolLevel level,
418 TOperator op,
419 const char *ext,
420 const TType *rvalue,
421 const TType *ptype1,
422 const TType *ptype2 = 0,
423 const TType *ptype3 = 0,
424 const TType *ptype4 = 0,
425 const TType *ptype5 = 0);
Nicolas Capens482907e2015-02-23 16:56:33 -0500426
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300427 void insertBuiltInFunctionNoParameters(ESymbolLevel level,
428 TOperator op,
429 const TType *rvalue,
430 const char *name);
431
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500432 TSymbol *find(const TString &name,
433 int shaderVersion,
Yunchao He4f285442017-04-21 12:15:49 +0800434 bool *builtIn = nullptr,
435 bool *sameScope = nullptr) const;
Zhenyao Mod7490962016-11-09 15:49:51 -0800436
437 TSymbol *findGlobal(const TString &name) const;
438
Zhenyao Moe740add2014-07-18 17:01:01 -0700439 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
Zhenyao Mod7490962016-11-09 15:49:51 -0800440
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700441 TSymbolTableLevel *getOuterLevel()
442 {
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000443 assert(currentLevel() >= 1);
daniel@transgaming.com5dd6d092012-03-20 20:10:28 +0000444 return table[currentLevel() - 1];
445 }
446
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400447 void dump(TInfoSink &infoSink) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448
Olli Etuahocce89652017-06-19 16:04:09 +0300449 void setDefaultPrecision(TBasicType type, TPrecision prec)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700450 {
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000451 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700452 // Uses map operator [], overwrites the current value
Olli Etuahocce89652017-06-19 16:04:09 +0300453 (*precisionStack[indexOfLastElement])[type] = prec;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000454 }
455
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700456 // Searches down the precisionStack for a precision qualifier
457 // for the specified TBasicType
Zhenyao Moe740add2014-07-18 17:01:01 -0700458 TPrecision getDefaultPrecision(TBasicType type) const;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000459
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700460 // This records invariant varyings declared through
461 // "invariant varying_name;".
Jamie Madill2c433252014-12-03 12:36:54 -0500462 void addInvariantVarying(const std::string &originalName)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700463 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800464 ASSERT(atGlobalLevel());
465 table[currentLevel()]->addInvariantVarying(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700466 }
467 // If this returns false, the varying could still be invariant
468 // if it is set as invariant during the varying variable
469 // declaration - this piece of information is stored in the
470 // variable's type, not here.
Jamie Madill2c433252014-12-03 12:36:54 -0500471 bool isVaryingInvariant(const std::string &originalName) const
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700472 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800473 ASSERT(atGlobalLevel());
474 return table[currentLevel()]->isVaryingInvariant(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700475 }
476
Qiankun Miaof69682b2016-08-16 14:50:42 +0800477 void setGlobalInvariant(bool invariant)
478 {
479 ASSERT(atGlobalLevel());
480 table[currentLevel()]->setGlobalInvariant(invariant);
481 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700482
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500483 static int nextUniqueId() { return ++uniqueIdCounter; }
Jamie Madillbfa91f42014-06-05 15:45:18 -0400484
Martin Radevda6254b2016-12-14 17:00:36 +0200485 // Checks whether there is a built-in accessible by a shader with the specified version.
486 bool hasUnmangledBuiltInForShaderVersion(const char *name, int shaderVersion);
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530487
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700488 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500489 ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000490
Martin Radevda6254b2016-12-14 17:00:36 +0200491 // Used to insert unmangled functions to check redeclaration of built-ins in ESSL 3.00 and
492 // above.
493 void insertUnmangledBuiltInName(const char *name, ESymbolLevel level);
494
495 bool hasUnmangledBuiltInAtLevel(const char *name, ESymbolLevel level);
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530496
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700497 std::vector<TSymbolTableLevel *> table;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400498 typedef TMap<TBasicType, TPrecision> PrecisionStackLevel;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500499 std::vector<PrecisionStackLevel *> precisionStack;
Jamie Madillbfa91f42014-06-05 15:45:18 -0400500
501 static int uniqueIdCounter;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502};
503
Jamie Madill45bcc782016-11-07 13:58:48 -0500504} // namespace sh
505
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500506#endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_