blob: 51a82f724ca0674858dda8b1492adb0903911967 [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();
Olli Etuaho476197f2016-10-11 13:59:08 +010046 TSymbol(const TString *n);
47
Zhenyao Mo9eedea02014-05-12 16:02:35 -070048 virtual ~TSymbol()
49 {
50 // don't delete name, it's from the pool
51 }
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040052
Zhenyao Mo9eedea02014-05-12 16:02:35 -070053 const TString &getName() const
54 {
55 return *name;
56 }
57 virtual const TString &getMangledName() const
58 {
59 return getName();
60 }
61 virtual bool isFunction() const
62 {
63 return false;
64 }
65 virtual bool isVariable() const
66 {
67 return false;
68 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -070069 int getUniqueId() const
70 {
71 return uniqueId;
72 }
73 void relateToExtension(const TString &ext)
74 {
75 extension = ext;
76 }
77 const TString &getExtension() const
78 {
79 return extension;
80 }
Nicolas Capensba60ad32013-06-04 15:55:47 -040081
Zhenyao Mo9eedea02014-05-12 16:02:35 -070082 private:
Olli Etuaho476197f2016-10-11 13:59:08 +010083 const int uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000084 const TString *name;
Jamie Madill2aeb26a2013-07-08 14:02:55 -040085 TString extension;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086};
87
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088// Variable class, meaning a symbol that's not a function.
89//
90// There could be a separate class heirarchy for Constant variables;
91// Only one of int, bool, or float, (or none) is correct for
92// any particular use, but it's easy to do this way, and doesn't
93// seem worth having separate classes, and "getConst" can't simply return
94// different values for different types polymorphically, so this is
95// just simple and pragmatic.
Zhenyao Mo9eedea02014-05-12 16:02:35 -070096class TVariable : public TSymbol
97{
98 public:
99 TVariable(const TString *name, const TType &t, bool uT = false)
100 : TSymbol(name),
101 type(t),
102 userType(uT),
103 unionArray(0)
104 {
105 }
Corentin Walleze5a1f272015-08-21 02:58:25 +0200106 ~TVariable() override {}
107 bool isVariable() const override { return true; }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700108 TType &getType()
109 {
110 return type;
111 }
112 const TType &getType() const
113 {
114 return type;
115 }
116 bool isUserType() const
117 {
118 return userType;
119 }
120 void setQualifier(TQualifier qualifier)
121 {
122 type.setQualifier(qualifier);
123 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200125 const TConstantUnion *getConstPointer() const { return unionArray; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000126
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200127 void shareConstPointer(const TConstantUnion *constArray) { unionArray = constArray; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000128
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700129 private:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130 TType type;
131 bool userType;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700132 // we are assuming that Pool Allocator will free the memory
133 // allocated to unionArray when this object is destroyed.
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200134 const TConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135};
136
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700137// Immutable version of TParameter.
138struct TConstParameter
139{
140 TConstParameter()
141 : name(nullptr),
142 type(nullptr)
143 {
144 }
145 explicit TConstParameter(const TString *n)
146 : name(n),
147 type(nullptr)
148 {
149 }
150 explicit TConstParameter(const TType *t)
151 : name(nullptr),
152 type(t)
153 {
154 }
155 TConstParameter(const TString *n, const TType *t)
156 : name(n),
157 type(t)
158 {
159 }
160
161 // Both constructor arguments must be const.
162 TConstParameter(TString *n, TType *t) = delete;
163 TConstParameter(const TString *n, TType *t) = delete;
164 TConstParameter(TString *n, const TType *t) = delete;
165
166 const TString *name;
167 const TType *type;
168};
169
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000170// The function sub-class of symbols and the parser will need to
171// share this definition of a function parameter.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700172struct TParameter
173{
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700174 // Destructively converts to TConstParameter.
175 // This method resets name and type to nullptrs to make sure
176 // their content cannot be modified after the call.
177 TConstParameter turnToConst()
178 {
179 const TString *constName = name;
180 const TType *constType = type;
181 name = nullptr;
182 type = nullptr;
183 return TConstParameter(constName, constType);
184 }
185
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186 TString *name;
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400187 TType *type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188};
189
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000190// The function sub-class of a symbol.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700191class TFunction : public TSymbol
192{
193 public:
Olli Etuaho5d653182016-01-04 14:43:28 +0200194 TFunction(const TString *name,
195 const TType *retType,
196 TOperator tOp = EOpNull,
197 const char *ext = "")
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700198 : TSymbol(name),
199 returnType(retType),
Dmitry Skiba58832202015-07-06 16:11:13 -0700200 mangledName(nullptr),
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700201 op(tOp),
Olli Etuaho5d653182016-01-04 14:43:28 +0200202 defined(false),
203 mHasPrototypeDeclaration(false)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700204 {
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500205 relateToExtension(ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700206 }
Corentin Walleze5a1f272015-08-21 02:58:25 +0200207 ~TFunction() override;
208 bool isFunction() const override { return true; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000209
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700210 static TString mangleName(const TString &name)
211 {
212 return name + '(';
213 }
214 static TString unmangleName(const TString &mangledName)
alokp@chromium.org43884872010-03-30 00:08:52 +0000215 {
216 return TString(mangledName.c_str(), mangledName.find_first_of('('));
217 }
218
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700219 void addParameter(const TConstParameter &p)
Dmitry Skiba58832202015-07-06 16:11:13 -0700220 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221 parameters.push_back(p);
Dmitry Skiba58832202015-07-06 16:11:13 -0700222 mangledName = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000224
Olli Etuaho476197f2016-10-11 13:59:08 +0100225 void swapParameters(const TFunction &parametersSource);
226
Corentin Walleze5a1f272015-08-21 02:58:25 +0200227 const TString &getMangledName() const override
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700228 {
Dmitry Skiba58832202015-07-06 16:11:13 -0700229 if (mangledName == nullptr)
230 {
231 mangledName = buildMangledName();
232 }
233 return *mangledName;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700234 }
235 const TType &getReturnType() const
236 {
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700237 return *returnType;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700238 }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000239
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700240 TOperator getBuiltInOp() const
241 {
242 return op;
243 }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000244
Olli Etuaho5d653182016-01-04 14:43:28 +0200245 void setDefined() { defined = true; }
246 bool isDefined() { return defined; }
247 void setHasPrototypeDeclaration() { mHasPrototypeDeclaration = true; }
248 bool hasPrototypeDeclaration() const { return mHasPrototypeDeclaration; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000249
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700250 size_t getParamCount() const
251 {
252 return parameters.size();
253 }
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700254 const TConstParameter &getParam(size_t i) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700255 {
256 return parameters[i];
257 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000258
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700259 private:
Olli Etuaho476197f2016-10-11 13:59:08 +0100260 void clearParameters();
261
Dmitry Skiba58832202015-07-06 16:11:13 -0700262 const TString *buildMangledName() const;
263
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700264 typedef TVector<TConstParameter> TParamList;
alokp@chromium.org43884872010-03-30 00:08:52 +0000265 TParamList parameters;
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700266 const TType *returnType;
Dmitry Skiba58832202015-07-06 16:11:13 -0700267 mutable const TString *mangledName;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268 TOperator op;
269 bool defined;
Olli Etuaho5d653182016-01-04 14:43:28 +0200270 bool mHasPrototypeDeclaration;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000271};
272
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000273// Interface block name sub-symbol
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000274class TInterfaceBlockName : public TSymbol
275{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700276 public:
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000277 TInterfaceBlockName(const TString *name)
278 : TSymbol(name)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700279 {
280 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000281
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700282 virtual ~TInterfaceBlockName()
283 {
284 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000285};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700287class TSymbolTableLevel
288{
289 public:
290 typedef TMap<TString, TSymbol *> tLevel;
alokp@chromium.org43884872010-03-30 00:08:52 +0000291 typedef tLevel::const_iterator const_iterator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292 typedef const tLevel::value_type tLevelPair;
293 typedef std::pair<tLevel::iterator, bool> tInsertResult;
294
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700295 TSymbolTableLevel()
Qiankun Miaof69682b2016-08-16 14:50:42 +0800296 : mGlobalInvariant(false)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700297 {
298 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000299 ~TSymbolTableLevel();
300
Nicolas Capensadfffe42014-06-17 02:13:36 -0400301 bool insert(TSymbol *symbol);
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400302
Olli Etuahob2983c92015-03-18 14:02:46 +0200303 // Insert a function using its unmangled name as the key.
304 bool insertUnmangled(TFunction *function);
305
Jamie Madillbfa91f42014-06-05 15:45:18 -0400306 TSymbol *find(const TString &name) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307
Qiankun Miaof69682b2016-08-16 14:50:42 +0800308 void addInvariantVarying(const std::string &name)
309 {
310 mInvariantVaryings.insert(name);
311 }
312
313 bool isVaryingInvariant(const std::string &name)
314 {
315 return (mGlobalInvariant || mInvariantVaryings.count(name) > 0);
316 }
317
318 void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; }
319
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700320 protected:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000321 tLevel level;
Qiankun Miaof69682b2016-08-16 14:50:42 +0800322 std::set<std::string> mInvariantVaryings;
323 bool mGlobalInvariant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324};
325
Gus Fernandez964df492014-10-13 11:54:39 -0700326// Define ESymbolLevel as int rather than an enum since level can go
327// above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the
328// compiler optimizes the >= of the last element to ==.
329typedef int ESymbolLevel;
330const int COMMON_BUILTINS = 0;
331const int ESSL1_BUILTINS = 1;
332const int ESSL3_BUILTINS = 2;
Martin Radeve93d24e2016-07-28 12:06:05 +0300333const int ESSL3_1_BUILTINS = 3;
334const int LAST_BUILTIN_LEVEL = ESSL3_1_BUILTINS;
335const int GLOBAL_LEVEL = 4;
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000336
Jamie Madillf0d10f82015-03-31 12:56:52 -0400337class TSymbolTable : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700338{
339 public:
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400340 TSymbolTable()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342 // The symbol table cannot be used until push() is called, but
343 // the lack of an initial call to push() can be used to detect
344 // that the symbol table has not been preloaded with built-ins.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345 }
346
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400347 ~TSymbolTable();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349 // When the symbol table is initialized with the built-ins, there should
350 // 'push' calls, so that built-ins are at level 0 and the shader
351 // globals are at level 1.
Zhenyao Moe740add2014-07-18 17:01:01 -0700352 bool isEmpty() const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700353 {
354 return table.empty();
355 }
Zhenyao Moe740add2014-07-18 17:01:01 -0700356 bool atBuiltInLevel() const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700357 {
358 return currentLevel() <= LAST_BUILTIN_LEVEL;
359 }
Zhenyao Moe740add2014-07-18 17:01:01 -0700360 bool atGlobalLevel() const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700361 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800362 return currentLevel() == GLOBAL_LEVEL;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700363 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000364 void push()
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000365 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366 table.push_back(new TSymbolTableLevel);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400367 precisionStack.push_back(new PrecisionStackLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368 }
369
alokp@chromium.org43884872010-03-30 00:08:52 +0000370 void pop()
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400371 {
372 delete table.back();
373 table.pop_back();
374
375 delete precisionStack.back();
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000376 precisionStack.pop_back();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000377 }
378
Nicolas Capensadfffe42014-06-17 02:13:36 -0400379 bool declare(TSymbol *symbol)
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000380 {
381 return insert(currentLevel(), symbol);
382 }
383
Nicolas Capensadfffe42014-06-17 02:13:36 -0400384 bool insert(ESymbolLevel level, TSymbol *symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385 {
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000386 return table[level]->insert(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000388
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500389 bool insert(ESymbolLevel level, const char *ext, TSymbol *symbol)
390 {
391 symbol->relateToExtension(ext);
392 return table[level]->insert(symbol);
393 }
394
Martin Radeve93d24e2016-07-28 12:06:05 +0300395 bool insertConstInt(ESymbolLevel level, const char *name, int value, TPrecision precision)
Nicolas Capens49a88872013-06-20 09:54:03 -0400396 {
Martin Radeve93d24e2016-07-28 12:06:05 +0300397 TVariable *constant =
398 new TVariable(NewPoolTString(name), TType(EbtInt, precision, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200399 TConstantUnion *unionArray = new TConstantUnion[1];
400 unionArray[0].setIConst(value);
401 constant->shareConstPointer(unionArray);
Nicolas Capensadfffe42014-06-17 02:13:36 -0400402 return insert(level, constant);
Nicolas Capens49a88872013-06-20 09:54:03 -0400403 }
404
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300405 bool insertConstIntExt(ESymbolLevel level, const char *ext, const char *name, int value)
406 {
407 TVariable *constant =
408 new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200409 TConstantUnion *unionArray = new TConstantUnion[1];
410 unionArray[0].setIConst(value);
411 constant->shareConstPointer(unionArray);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300412 return insert(level, ext, constant);
413 }
414
Martin Radeve93d24e2016-07-28 12:06:05 +0300415 bool insertConstIvec3(ESymbolLevel level,
416 const char *name,
417 const std::array<int, 3> &values,
418 TPrecision precision)
419 {
420 TVariable *constantIvec3 =
421 new TVariable(NewPoolTString(name), TType(EbtInt, precision, EvqConst, 3));
422
423 TConstantUnion *unionArray = new TConstantUnion[3];
424 for (size_t index = 0u; index < 3u; ++index)
425 {
426 unionArray[index].setIConst(values[index]);
427 }
428 constantIvec3->shareConstPointer(unionArray);
429
430 return insert(level, constantIvec3);
431 }
432
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700433 void insertBuiltIn(ESymbolLevel level, TOperator op, const char *ext, const TType *rvalue, const char *name,
434 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 -0500435
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700436 void insertBuiltIn(ESymbolLevel level, 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 Capens482907e2015-02-23 16:56:33 -0500438 {
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530439 insertUnmangledBuiltIn(name);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500440 insertBuiltIn(level, EOpNull, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
441 }
442
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700443 void insertBuiltIn(ESymbolLevel level, const char *ext, const TType *rvalue, const char *name,
444 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 -0500445 {
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530446 insertUnmangledBuiltIn(name);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500447 insertBuiltIn(level, EOpNull, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
448 }
449
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700450 void insertBuiltIn(ESymbolLevel level, TOperator op, const TType *rvalue, const char *name,
451 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 -0500452 {
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530453 insertUnmangledBuiltIn(name);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500454 insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
Nicolas Capens482907e2015-02-23 16:56:33 -0500455 }
456
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700457 TSymbol *find(const TString &name, int shaderVersion,
Zhenyao Moe740add2014-07-18 17:01:01 -0700458 bool *builtIn = NULL, bool *sameScope = NULL) const;
459 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000460
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700461 TSymbolTableLevel *getOuterLevel()
462 {
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000463 assert(currentLevel() >= 1);
daniel@transgaming.com5dd6d092012-03-20 20:10:28 +0000464 return table[currentLevel() - 1];
465 }
466
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400467 void dump(TInfoSink &infoSink) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000468
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700469 bool setDefaultPrecision(const TPublicType &type, TPrecision prec)
470 {
Martin Radev4a9cd802016-09-01 16:51:51 +0300471 if (!SupportsPrecision(type.getBasicType()))
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400472 return false;
Martin Radev4a9cd802016-09-01 16:51:51 +0300473 if (type.getBasicType() == EbtUInt)
Olli Etuaho0980e292015-11-20 14:57:34 +0200474 return false; // ESSL 3.00.4 section 4.5.4
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000475 if (type.isAggregate())
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000476 return false; // Not allowed to set for aggregate types
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000477 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700478 // Uses map operator [], overwrites the current value
Martin Radev4a9cd802016-09-01 16:51:51 +0300479 (*precisionStack[indexOfLastElement])[type.getBasicType()] = prec;
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000480 return true;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000481 }
482
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700483 // Searches down the precisionStack for a precision qualifier
484 // for the specified TBasicType
Zhenyao Moe740add2014-07-18 17:01:01 -0700485 TPrecision getDefaultPrecision(TBasicType type) const;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000486
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700487 // This records invariant varyings declared through
488 // "invariant varying_name;".
Jamie Madill2c433252014-12-03 12:36:54 -0500489 void addInvariantVarying(const std::string &originalName)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700490 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800491 ASSERT(atGlobalLevel());
492 table[currentLevel()]->addInvariantVarying(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700493 }
494 // If this returns false, the varying could still be invariant
495 // if it is set as invariant during the varying variable
496 // declaration - this piece of information is stored in the
497 // variable's type, not here.
Jamie Madill2c433252014-12-03 12:36:54 -0500498 bool isVaryingInvariant(const std::string &originalName) const
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700499 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800500 ASSERT(atGlobalLevel());
501 return table[currentLevel()]->isVaryingInvariant(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700502 }
503
Qiankun Miaof69682b2016-08-16 14:50:42 +0800504 void setGlobalInvariant(bool invariant)
505 {
506 ASSERT(atGlobalLevel());
507 table[currentLevel()]->setGlobalInvariant(invariant);
508 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700509
Jamie Madillbfa91f42014-06-05 15:45:18 -0400510 static int nextUniqueId()
511 {
512 return ++uniqueIdCounter;
513 }
514
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530515 bool hasUnmangledBuiltIn(const char *name)
516 {
517 return mUnmangledBuiltinNames.count(std::string(name)) > 0;
518 }
519
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700520 private:
521 ESymbolLevel currentLevel() const
522 {
523 return static_cast<ESymbolLevel>(table.size() - 1);
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000524 }
525
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530526 // Used to insert unmangled functions to check redeclaration of built-ins in ESSL 3.00.
527 void insertUnmangledBuiltIn(const char *name)
528 {
529 mUnmangledBuiltinNames.insert(std::string(name));
530 }
531
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700532 std::vector<TSymbolTableLevel *> table;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400533 typedef TMap<TBasicType, TPrecision> PrecisionStackLevel;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700534 std::vector< PrecisionStackLevel *> precisionStack;
Jamie Madillbfa91f42014-06-05 15:45:18 -0400535
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530536 std::set<std::string> mUnmangledBuiltinNames;
537
Jamie Madillbfa91f42014-06-05 15:45:18 -0400538 static int uniqueIdCounter;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000539};
540
Geoff Lang0a73dd82014-11-19 16:18:08 -0500541#endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_