blob: 60e0b9929609a99069dee7db3fbcfc9181819e9c [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
Zhenyao Mo9eedea02014-05-12 16:02:35 -070041// Symbol base class. (Can build functions or variables out of these...)
Jamie Madillf0d10f82015-03-31 12:56:52 -040042class TSymbol : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -070043{
44 public:
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040045 POOL_ALLOCATOR_NEW_DELETE();
Zhenyao Mo9eedea02014-05-12 16:02:35 -070046 TSymbol(const TString *n)
47 : uniqueId(0),
48 name(n)
49 {
50 }
51 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 }
72 void setUniqueId(int id)
73 {
74 uniqueId = id;
75 }
76 int getUniqueId() const
77 {
78 return uniqueId;
79 }
80 void relateToExtension(const TString &ext)
81 {
82 extension = ext;
83 }
84 const TString &getExtension() const
85 {
86 return extension;
87 }
Nicolas Capensba60ad32013-06-04 15:55:47 -040088
Zhenyao Mo9eedea02014-05-12 16:02:35 -070089 private:
Zhenyao Mo9eedea02014-05-12 16:02:35 -070090 int uniqueId; // For real comparing during code generation
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000091 const TString *name;
Jamie Madill2aeb26a2013-07-08 14:02:55 -040092 TString extension;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093};
94
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095// Variable class, meaning a symbol that's not a function.
96//
97// There could be a separate class heirarchy for Constant variables;
98// Only one of int, bool, or float, (or none) is correct for
99// any particular use, but it's easy to do this way, and doesn't
100// seem worth having separate classes, and "getConst" can't simply return
101// different values for different types polymorphically, so this is
102// just simple and pragmatic.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700103class TVariable : public TSymbol
104{
105 public:
106 TVariable(const TString *name, const TType &t, bool uT = false)
107 : TSymbol(name),
108 type(t),
109 userType(uT),
110 unionArray(0)
111 {
112 }
Corentin Walleze5a1f272015-08-21 02:58:25 +0200113 ~TVariable() override {}
114 bool isVariable() const override { return true; }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700115 TType &getType()
116 {
117 return type;
118 }
119 const TType &getType() const
120 {
121 return type;
122 }
123 bool isUserType() const
124 {
125 return userType;
126 }
127 void setQualifier(TQualifier qualifier)
128 {
129 type.setQualifier(qualifier);
130 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200132 const TConstantUnion *getConstPointer() const { return unionArray; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000133
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200134 void shareConstPointer(const TConstantUnion *constArray) { unionArray = constArray; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000135
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700136 private:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137 TType type;
138 bool userType;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700139 // we are assuming that Pool Allocator will free the memory
140 // allocated to unionArray when this object is destroyed.
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200141 const TConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142};
143
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700144// Immutable version of TParameter.
145struct TConstParameter
146{
147 TConstParameter()
148 : name(nullptr),
149 type(nullptr)
150 {
151 }
152 explicit TConstParameter(const TString *n)
153 : name(n),
154 type(nullptr)
155 {
156 }
157 explicit TConstParameter(const TType *t)
158 : name(nullptr),
159 type(t)
160 {
161 }
162 TConstParameter(const TString *n, const TType *t)
163 : name(n),
164 type(t)
165 {
166 }
167
168 // Both constructor arguments must be const.
169 TConstParameter(TString *n, TType *t) = delete;
170 TConstParameter(const TString *n, TType *t) = delete;
171 TConstParameter(TString *n, const TType *t) = delete;
172
173 const TString *name;
174 const TType *type;
175};
176
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000177// The function sub-class of symbols and the parser will need to
178// share this definition of a function parameter.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700179struct TParameter
180{
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700181 // Destructively converts to TConstParameter.
182 // This method resets name and type to nullptrs to make sure
183 // their content cannot be modified after the call.
184 TConstParameter turnToConst()
185 {
186 const TString *constName = name;
187 const TType *constType = type;
188 name = nullptr;
189 type = nullptr;
190 return TConstParameter(constName, constType);
191 }
192
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193 TString *name;
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400194 TType *type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000195};
196
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000197// The function sub-class of a symbol.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700198class TFunction : public TSymbol
199{
200 public:
Olli Etuaho5d653182016-01-04 14:43:28 +0200201 TFunction(const TString *name,
202 const TType *retType,
203 TOperator tOp = EOpNull,
204 const char *ext = "")
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700205 : TSymbol(name),
206 returnType(retType),
Dmitry Skiba58832202015-07-06 16:11:13 -0700207 mangledName(nullptr),
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700208 op(tOp),
Olli Etuaho5d653182016-01-04 14:43:28 +0200209 defined(false),
210 mHasPrototypeDeclaration(false)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700211 {
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500212 relateToExtension(ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700213 }
Corentin Walleze5a1f272015-08-21 02:58:25 +0200214 ~TFunction() override;
215 bool isFunction() const override { return true; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000216
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700217 static TString mangleName(const TString &name)
218 {
219 return name + '(';
220 }
221 static TString unmangleName(const TString &mangledName)
alokp@chromium.org43884872010-03-30 00:08:52 +0000222 {
223 return TString(mangledName.c_str(), mangledName.find_first_of('('));
224 }
225
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700226 void addParameter(const TConstParameter &p)
Dmitry Skiba58832202015-07-06 16:11:13 -0700227 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228 parameters.push_back(p);
Dmitry Skiba58832202015-07-06 16:11:13 -0700229 mangledName = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000231
Corentin Walleze5a1f272015-08-21 02:58:25 +0200232 const TString &getMangledName() const override
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700233 {
Dmitry Skiba58832202015-07-06 16:11:13 -0700234 if (mangledName == nullptr)
235 {
236 mangledName = buildMangledName();
237 }
238 return *mangledName;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700239 }
240 const TType &getReturnType() const
241 {
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700242 return *returnType;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700243 }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000244
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700245 TOperator getBuiltInOp() const
246 {
247 return op;
248 }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000249
Olli Etuaho5d653182016-01-04 14:43:28 +0200250 void setDefined() { defined = true; }
251 bool isDefined() { return defined; }
252 void setHasPrototypeDeclaration() { mHasPrototypeDeclaration = true; }
253 bool hasPrototypeDeclaration() const { return mHasPrototypeDeclaration; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000254
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700255 size_t getParamCount() const
256 {
257 return parameters.size();
258 }
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700259 const TConstParameter &getParam(size_t i) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700260 {
261 return parameters[i];
262 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000263
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700264 private:
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;
462 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000463
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700464 TSymbolTableLevel *getOuterLevel()
465 {
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000466 assert(currentLevel() >= 1);
daniel@transgaming.com5dd6d092012-03-20 20:10:28 +0000467 return table[currentLevel() - 1];
468 }
469
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400470 void dump(TInfoSink &infoSink) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700472 bool setDefaultPrecision(const TPublicType &type, TPrecision prec)
473 {
Zhenyao Moed14b792014-05-08 11:21:07 -0700474 if (!SupportsPrecision(type.type))
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400475 return false;
Olli Etuaho0980e292015-11-20 14:57:34 +0200476 if (type.type == EbtUInt)
477 return false; // ESSL 3.00.4 section 4.5.4
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000478 if (type.isAggregate())
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000479 return false; // Not allowed to set for aggregate types
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000480 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700481 // Uses map operator [], overwrites the current value
482 (*precisionStack[indexOfLastElement])[type.type] = prec;
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000483 return true;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000484 }
485
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700486 // Searches down the precisionStack for a precision qualifier
487 // for the specified TBasicType
Zhenyao Moe740add2014-07-18 17:01:01 -0700488 TPrecision getDefaultPrecision(TBasicType type) const;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000489
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700490 // This records invariant varyings declared through
491 // "invariant varying_name;".
Jamie Madill2c433252014-12-03 12:36:54 -0500492 void addInvariantVarying(const std::string &originalName)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700493 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800494 ASSERT(atGlobalLevel());
495 table[currentLevel()]->addInvariantVarying(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700496 }
497 // If this returns false, the varying could still be invariant
498 // if it is set as invariant during the varying variable
499 // declaration - this piece of information is stored in the
500 // variable's type, not here.
Jamie Madill2c433252014-12-03 12:36:54 -0500501 bool isVaryingInvariant(const std::string &originalName) const
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700502 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800503 ASSERT(atGlobalLevel());
504 return table[currentLevel()]->isVaryingInvariant(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700505 }
506
Qiankun Miaof69682b2016-08-16 14:50:42 +0800507 void setGlobalInvariant(bool invariant)
508 {
509 ASSERT(atGlobalLevel());
510 table[currentLevel()]->setGlobalInvariant(invariant);
511 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700512
Jamie Madillbfa91f42014-06-05 15:45:18 -0400513 static int nextUniqueId()
514 {
515 return ++uniqueIdCounter;
516 }
517
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530518 bool hasUnmangledBuiltIn(const char *name)
519 {
520 return mUnmangledBuiltinNames.count(std::string(name)) > 0;
521 }
522
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700523 private:
524 ESymbolLevel currentLevel() const
525 {
526 return static_cast<ESymbolLevel>(table.size() - 1);
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000527 }
528
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530529 // Used to insert unmangled functions to check redeclaration of built-ins in ESSL 3.00.
530 void insertUnmangledBuiltIn(const char *name)
531 {
532 mUnmangledBuiltinNames.insert(std::string(name));
533 }
534
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700535 std::vector<TSymbolTableLevel *> table;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400536 typedef TMap<TBasicType, TPrecision> PrecisionStackLevel;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700537 std::vector< PrecisionStackLevel *> precisionStack;
Jamie Madillbfa91f42014-06-05 15:45:18 -0400538
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530539 std::set<std::string> mUnmangledBuiltinNames;
540
Jamie Madillbfa91f42014-06-05 15:45:18 -0400541 static int uniqueIdCounter;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000542};
543
Geoff Lang0a73dd82014-11-19 16:18:08 -0500544#endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_