blob: 7c4d3aab353aa438a9b552cfb7efc0cf012d60c5 [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//
21// * Pushing and popping of scope, so symbol table will really be a stack
22// 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
Zhenyao Mo9eedea02014-05-12 16:02:35 -070044// Symbol base class. (Can build functions or variables out of these...)
Jamie Madillf0d10f82015-03-31 12:56:52 -040045class TSymbol : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -070046{
47 public:
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040048 POOL_ALLOCATOR_NEW_DELETE();
Olli Etuaho476197f2016-10-11 13:59:08 +010049 TSymbol(const TString *n);
50
Zhenyao Mo9eedea02014-05-12 16:02:35 -070051 virtual ~TSymbol()
52 {
53 // don't delete name, it's from the pool
54 }
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040055
Zhenyao Mo9eedea02014-05-12 16:02:35 -070056 const TString &getName() const
57 {
58 return *name;
59 }
60 virtual const TString &getMangledName() const
61 {
62 return getName();
63 }
64 virtual bool isFunction() const
65 {
66 return false;
67 }
68 virtual bool isVariable() const
69 {
70 return false;
71 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -070072 int getUniqueId() const
73 {
74 return uniqueId;
75 }
76 void relateToExtension(const TString &ext)
77 {
78 extension = ext;
79 }
80 const TString &getExtension() const
81 {
82 return extension;
83 }
Nicolas Capensba60ad32013-06-04 15:55:47 -040084
Zhenyao Mo9eedea02014-05-12 16:02:35 -070085 private:
Olli Etuaho476197f2016-10-11 13:59:08 +010086 const int uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087 const TString *name;
Jamie Madill2aeb26a2013-07-08 14:02:55 -040088 TString extension;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089};
90
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000091// Variable class, meaning a symbol that's not a function.
92//
93// There could be a separate class heirarchy for Constant variables;
94// Only one of int, bool, or float, (or none) is correct for
95// any particular use, but it's easy to do this way, and doesn't
96// seem worth having separate classes, and "getConst" can't simply return
97// different values for different types polymorphically, so this is
98// just simple and pragmatic.
Zhenyao Mo9eedea02014-05-12 16:02:35 -070099class TVariable : public TSymbol
100{
101 public:
102 TVariable(const TString *name, const TType &t, bool uT = false)
103 : TSymbol(name),
104 type(t),
105 userType(uT),
106 unionArray(0)
107 {
108 }
Corentin Walleze5a1f272015-08-21 02:58:25 +0200109 ~TVariable() override {}
110 bool isVariable() const override { return true; }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700111 TType &getType()
112 {
113 return type;
114 }
115 const TType &getType() const
116 {
117 return type;
118 }
119 bool isUserType() const
120 {
121 return userType;
122 }
123 void setQualifier(TQualifier qualifier)
124 {
125 type.setQualifier(qualifier);
126 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200128 const TConstantUnion *getConstPointer() const { return unionArray; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200130 void shareConstPointer(const TConstantUnion *constArray) { unionArray = constArray; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000131
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700132 private:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000133 TType type;
134 bool userType;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700135 // we are assuming that Pool Allocator will free the memory
136 // allocated to unionArray when this object is destroyed.
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200137 const TConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138};
139
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700140// Immutable version of TParameter.
141struct TConstParameter
142{
143 TConstParameter()
144 : name(nullptr),
145 type(nullptr)
146 {
147 }
148 explicit TConstParameter(const TString *n)
149 : name(n),
150 type(nullptr)
151 {
152 }
153 explicit TConstParameter(const TType *t)
154 : name(nullptr),
155 type(t)
156 {
157 }
158 TConstParameter(const TString *n, const TType *t)
159 : name(n),
160 type(t)
161 {
162 }
163
164 // Both constructor arguments must be const.
165 TConstParameter(TString *n, TType *t) = delete;
166 TConstParameter(const TString *n, TType *t) = delete;
167 TConstParameter(TString *n, const TType *t) = delete;
168
169 const TString *name;
170 const TType *type;
171};
172
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173// The function sub-class of symbols and the parser will need to
174// share this definition of a function parameter.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700175struct TParameter
176{
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700177 // Destructively converts to TConstParameter.
178 // This method resets name and type to nullptrs to make sure
179 // their content cannot be modified after the call.
180 TConstParameter turnToConst()
181 {
182 const TString *constName = name;
183 const TType *constType = type;
184 name = nullptr;
185 type = nullptr;
186 return TConstParameter(constName, constType);
187 }
188
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189 TString *name;
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400190 TType *type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000191};
192
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193// The function sub-class of a symbol.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700194class TFunction : public TSymbol
195{
196 public:
Olli Etuaho5d653182016-01-04 14:43:28 +0200197 TFunction(const TString *name,
198 const TType *retType,
199 TOperator tOp = EOpNull,
200 const char *ext = "")
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700201 : TSymbol(name),
202 returnType(retType),
Dmitry Skiba58832202015-07-06 16:11:13 -0700203 mangledName(nullptr),
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700204 op(tOp),
Olli Etuaho5d653182016-01-04 14:43:28 +0200205 defined(false),
206 mHasPrototypeDeclaration(false)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700207 {
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500208 relateToExtension(ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700209 }
Corentin Walleze5a1f272015-08-21 02:58:25 +0200210 ~TFunction() override;
211 bool isFunction() const override { return true; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000212
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700213 static TString mangleName(const TString &name)
214 {
215 return name + '(';
216 }
217 static TString unmangleName(const TString &mangledName)
alokp@chromium.org43884872010-03-30 00:08:52 +0000218 {
219 return TString(mangledName.c_str(), mangledName.find_first_of('('));
220 }
221
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700222 void addParameter(const TConstParameter &p)
Dmitry Skiba58832202015-07-06 16:11:13 -0700223 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224 parameters.push_back(p);
Dmitry Skiba58832202015-07-06 16:11:13 -0700225 mangledName = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000227
Olli Etuaho476197f2016-10-11 13:59:08 +0100228 void swapParameters(const TFunction &parametersSource);
229
Corentin Walleze5a1f272015-08-21 02:58:25 +0200230 const TString &getMangledName() const override
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700231 {
Dmitry Skiba58832202015-07-06 16:11:13 -0700232 if (mangledName == nullptr)
233 {
234 mangledName = buildMangledName();
235 }
236 return *mangledName;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700237 }
238 const TType &getReturnType() const
239 {
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700240 return *returnType;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700241 }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000242
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700243 TOperator getBuiltInOp() const
244 {
245 return op;
246 }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000247
Olli Etuaho5d653182016-01-04 14:43:28 +0200248 void setDefined() { defined = true; }
249 bool isDefined() { return defined; }
250 void setHasPrototypeDeclaration() { mHasPrototypeDeclaration = true; }
251 bool hasPrototypeDeclaration() const { return mHasPrototypeDeclaration; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000252
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700253 size_t getParamCount() const
254 {
255 return parameters.size();
256 }
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700257 const TConstParameter &getParam(size_t i) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700258 {
259 return parameters[i];
260 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000261
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700262 private:
Olli Etuaho476197f2016-10-11 13:59:08 +0100263 void clearParameters();
264
Dmitry Skiba58832202015-07-06 16:11:13 -0700265 const TString *buildMangledName() const;
266
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700267 typedef TVector<TConstParameter> TParamList;
alokp@chromium.org43884872010-03-30 00:08:52 +0000268 TParamList parameters;
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700269 const TType *returnType;
Dmitry Skiba58832202015-07-06 16:11:13 -0700270 mutable const TString *mangledName;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000271 TOperator op;
272 bool defined;
Olli Etuaho5d653182016-01-04 14:43:28 +0200273 bool mHasPrototypeDeclaration;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000274};
275
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000276// Interface block name sub-symbol
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000277class TInterfaceBlockName : public TSymbol
278{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700279 public:
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000280 TInterfaceBlockName(const TString *name)
281 : TSymbol(name)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700282 {
283 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000284
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700285 virtual ~TInterfaceBlockName()
286 {
287 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000288};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000289
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700290class TSymbolTableLevel
291{
292 public:
293 typedef TMap<TString, TSymbol *> tLevel;
alokp@chromium.org43884872010-03-30 00:08:52 +0000294 typedef tLevel::const_iterator const_iterator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295 typedef const tLevel::value_type tLevelPair;
296 typedef std::pair<tLevel::iterator, bool> tInsertResult;
297
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700298 TSymbolTableLevel()
Qiankun Miaof69682b2016-08-16 14:50:42 +0800299 : mGlobalInvariant(false)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700300 {
301 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000302 ~TSymbolTableLevel();
303
Nicolas Capensadfffe42014-06-17 02:13:36 -0400304 bool insert(TSymbol *symbol);
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400305
Olli Etuahob2983c92015-03-18 14:02:46 +0200306 // Insert a function using its unmangled name as the key.
307 bool insertUnmangled(TFunction *function);
308
Jamie Madillbfa91f42014-06-05 15:45:18 -0400309 TSymbol *find(const TString &name) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
Qiankun Miaof69682b2016-08-16 14:50:42 +0800311 void addInvariantVarying(const std::string &name)
312 {
313 mInvariantVaryings.insert(name);
314 }
315
316 bool isVaryingInvariant(const std::string &name)
317 {
318 return (mGlobalInvariant || mInvariantVaryings.count(name) > 0);
319 }
320
321 void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; }
322
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700323 protected:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324 tLevel level;
Qiankun Miaof69682b2016-08-16 14:50:42 +0800325 std::set<std::string> mInvariantVaryings;
326 bool mGlobalInvariant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327};
328
Gus Fernandez964df492014-10-13 11:54:39 -0700329// Define ESymbolLevel as int rather than an enum since level can go
330// above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the
331// compiler optimizes the >= of the last element to ==.
332typedef int ESymbolLevel;
333const int COMMON_BUILTINS = 0;
334const int ESSL1_BUILTINS = 1;
335const int ESSL3_BUILTINS = 2;
Martin Radeve93d24e2016-07-28 12:06:05 +0300336const int ESSL3_1_BUILTINS = 3;
337const int LAST_BUILTIN_LEVEL = ESSL3_1_BUILTINS;
338const int GLOBAL_LEVEL = 4;
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000339
Jamie Madillf0d10f82015-03-31 12:56:52 -0400340class TSymbolTable : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700341{
342 public:
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400343 TSymbolTable()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345 // The symbol table cannot be used until push() is called, but
346 // the lack of an initial call to push() can be used to detect
347 // that the symbol table has not been preloaded with built-ins.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348 }
349
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400350 ~TSymbolTable();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352 // When the symbol table is initialized with the built-ins, there should
353 // 'push' calls, so that built-ins are at level 0 and the shader
354 // globals are at level 1.
Zhenyao Moe740add2014-07-18 17:01:01 -0700355 bool isEmpty() const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700356 {
357 return table.empty();
358 }
Zhenyao Moe740add2014-07-18 17:01:01 -0700359 bool atBuiltInLevel() const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700360 {
361 return currentLevel() <= LAST_BUILTIN_LEVEL;
362 }
Zhenyao Moe740add2014-07-18 17:01:01 -0700363 bool atGlobalLevel() const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700364 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800365 return currentLevel() == GLOBAL_LEVEL;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700366 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000367 void push()
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000368 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369 table.push_back(new TSymbolTableLevel);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400370 precisionStack.push_back(new PrecisionStackLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371 }
372
alokp@chromium.org43884872010-03-30 00:08:52 +0000373 void pop()
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400374 {
375 delete table.back();
376 table.pop_back();
377
378 delete precisionStack.back();
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000379 precisionStack.pop_back();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000380 }
381
Nicolas Capensadfffe42014-06-17 02:13:36 -0400382 bool declare(TSymbol *symbol)
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000383 {
384 return insert(currentLevel(), symbol);
385 }
386
Nicolas Capensadfffe42014-06-17 02:13:36 -0400387 bool insert(ESymbolLevel level, TSymbol *symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388 {
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000389 return table[level]->insert(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000391
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500392 bool insert(ESymbolLevel level, const char *ext, TSymbol *symbol)
393 {
394 symbol->relateToExtension(ext);
395 return table[level]->insert(symbol);
396 }
397
Martin Radeve93d24e2016-07-28 12:06:05 +0300398 bool insertConstInt(ESymbolLevel level, const char *name, int value, TPrecision precision)
Nicolas Capens49a88872013-06-20 09:54:03 -0400399 {
Martin Radeve93d24e2016-07-28 12:06:05 +0300400 TVariable *constant =
401 new TVariable(NewPoolTString(name), TType(EbtInt, precision, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200402 TConstantUnion *unionArray = new TConstantUnion[1];
403 unionArray[0].setIConst(value);
404 constant->shareConstPointer(unionArray);
Nicolas Capensadfffe42014-06-17 02:13:36 -0400405 return insert(level, constant);
Nicolas Capens49a88872013-06-20 09:54:03 -0400406 }
407
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300408 bool insertConstIntExt(ESymbolLevel level, const char *ext, const char *name, int value)
409 {
410 TVariable *constant =
411 new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200412 TConstantUnion *unionArray = new TConstantUnion[1];
413 unionArray[0].setIConst(value);
414 constant->shareConstPointer(unionArray);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300415 return insert(level, ext, constant);
416 }
417
Martin Radeve93d24e2016-07-28 12:06:05 +0300418 bool insertConstIvec3(ESymbolLevel level,
419 const char *name,
420 const std::array<int, 3> &values,
421 TPrecision precision)
422 {
423 TVariable *constantIvec3 =
424 new TVariable(NewPoolTString(name), TType(EbtInt, precision, EvqConst, 3));
425
426 TConstantUnion *unionArray = new TConstantUnion[3];
427 for (size_t index = 0u; index < 3u; ++index)
428 {
429 unionArray[index].setIConst(values[index]);
430 }
431 constantIvec3->shareConstPointer(unionArray);
432
433 return insert(level, constantIvec3);
434 }
435
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700436 void insertBuiltIn(ESymbolLevel level, TOperator op, const char *ext, const TType *rvalue, const char *name,
437 const TType *ptype1, const TType *ptype2 = 0, const TType *ptype3 = 0, const TType *ptype4 = 0, const TType *ptype5 = 0);
Nicolas Capens759b9942014-02-14 17:57:14 -0500438
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700439 void insertBuiltIn(ESymbolLevel level, const TType *rvalue, const char *name,
440 const TType *ptype1, const TType *ptype2 = 0, const TType *ptype3 = 0, const TType *ptype4 = 0, const TType *ptype5 = 0)
Nicolas Capens482907e2015-02-23 16:56:33 -0500441 {
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530442 insertUnmangledBuiltIn(name);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500443 insertBuiltIn(level, EOpNull, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
444 }
445
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700446 void insertBuiltIn(ESymbolLevel level, const char *ext, const TType *rvalue, const char *name,
447 const TType *ptype1, const TType *ptype2 = 0, const TType *ptype3 = 0, const TType *ptype4 = 0, const TType *ptype5 = 0)
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500448 {
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530449 insertUnmangledBuiltIn(name);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500450 insertBuiltIn(level, EOpNull, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
451 }
452
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700453 void insertBuiltIn(ESymbolLevel level, TOperator op, const TType *rvalue, const char *name,
454 const TType *ptype1, const TType *ptype2 = 0, const TType *ptype3 = 0, const TType *ptype4 = 0, const TType *ptype5 = 0)
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500455 {
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530456 insertUnmangledBuiltIn(name);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500457 insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
Nicolas Capens482907e2015-02-23 16:56:33 -0500458 }
459
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700460 TSymbol *find(const TString &name, int shaderVersion,
Zhenyao Moe740add2014-07-18 17:01:01 -0700461 bool *builtIn = NULL, bool *sameScope = NULL) const;
Zhenyao Mod7490962016-11-09 15:49:51 -0800462
463 TSymbol *findGlobal(const TString &name) const;
464
Zhenyao Moe740add2014-07-18 17:01:01 -0700465 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
Zhenyao Mod7490962016-11-09 15:49:51 -0800466
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700467 TSymbolTableLevel *getOuterLevel()
468 {
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000469 assert(currentLevel() >= 1);
daniel@transgaming.com5dd6d092012-03-20 20:10:28 +0000470 return table[currentLevel() - 1];
471 }
472
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400473 void dump(TInfoSink &infoSink) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700475 bool setDefaultPrecision(const TPublicType &type, TPrecision prec)
476 {
Martin Radev4a9cd802016-09-01 16:51:51 +0300477 if (!SupportsPrecision(type.getBasicType()))
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400478 return false;
Martin Radev4a9cd802016-09-01 16:51:51 +0300479 if (type.getBasicType() == EbtUInt)
Olli Etuaho0980e292015-11-20 14:57:34 +0200480 return false; // ESSL 3.00.4 section 4.5.4
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000481 if (type.isAggregate())
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000482 return false; // Not allowed to set for aggregate types
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000483 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700484 // Uses map operator [], overwrites the current value
Martin Radev4a9cd802016-09-01 16:51:51 +0300485 (*precisionStack[indexOfLastElement])[type.getBasicType()] = prec;
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000486 return true;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000487 }
488
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700489 // Searches down the precisionStack for a precision qualifier
490 // for the specified TBasicType
Zhenyao Moe740add2014-07-18 17:01:01 -0700491 TPrecision getDefaultPrecision(TBasicType type) const;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000492
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700493 // This records invariant varyings declared through
494 // "invariant varying_name;".
Jamie Madill2c433252014-12-03 12:36:54 -0500495 void addInvariantVarying(const std::string &originalName)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700496 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800497 ASSERT(atGlobalLevel());
498 table[currentLevel()]->addInvariantVarying(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700499 }
500 // If this returns false, the varying could still be invariant
501 // if it is set as invariant during the varying variable
502 // declaration - this piece of information is stored in the
503 // variable's type, not here.
Jamie Madill2c433252014-12-03 12:36:54 -0500504 bool isVaryingInvariant(const std::string &originalName) const
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700505 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800506 ASSERT(atGlobalLevel());
507 return table[currentLevel()]->isVaryingInvariant(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700508 }
509
Qiankun Miaof69682b2016-08-16 14:50:42 +0800510 void setGlobalInvariant(bool invariant)
511 {
512 ASSERT(atGlobalLevel());
513 table[currentLevel()]->setGlobalInvariant(invariant);
514 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700515
Jamie Madillbfa91f42014-06-05 15:45:18 -0400516 static int nextUniqueId()
517 {
518 return ++uniqueIdCounter;
519 }
520
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530521 bool hasUnmangledBuiltIn(const char *name)
522 {
523 return mUnmangledBuiltinNames.count(std::string(name)) > 0;
524 }
525
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700526 private:
527 ESymbolLevel currentLevel() const
528 {
529 return static_cast<ESymbolLevel>(table.size() - 1);
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000530 }
531
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530532 // Used to insert unmangled functions to check redeclaration of built-ins in ESSL 3.00.
533 void insertUnmangledBuiltIn(const char *name)
534 {
535 mUnmangledBuiltinNames.insert(std::string(name));
536 }
537
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700538 std::vector<TSymbolTableLevel *> table;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400539 typedef TMap<TBasicType, TPrecision> PrecisionStackLevel;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700540 std::vector< PrecisionStackLevel *> precisionStack;
Jamie Madillbfa91f42014-06-05 15:45:18 -0400541
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530542 std::set<std::string> mUnmangledBuiltinNames;
543
Jamie Madillbfa91f42014-06-05 15:45:18 -0400544 static int uniqueIdCounter;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000545};
546
Jamie Madill45bcc782016-11-07 13:58:48 -0500547} // namespace sh
548
Geoff Lang0a73dd82014-11-19 16:18:08 -0500549#endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_