blob: 2f5eb369f8aeb314a1bc079796ad09ab424b314e [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
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000033#include <assert.h>
Zhenyao Mo94ac7b72014-10-15 18:22:08 -070034#include <set>
alokp@chromium.orge4249f02010-07-26 18:13:52 +000035
Jamie Madill703cdd62013-07-08 15:07:30 -040036#include "common/angleutils.h"
Geoff Lang17732822013-08-29 13:46:49 -040037#include "compiler/translator/InfoSink.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040038#include "compiler/translator/IntermNode.h"
alokp@chromium.org43884872010-03-30 00:08:52 +000039
Zhenyao Mo9eedea02014-05-12 16:02:35 -070040// Symbol base class. (Can build functions or variables out of these...)
Jamie Madillf0d10f82015-03-31 12:56:52 -040041class TSymbol : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -070042{
43 public:
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040044 POOL_ALLOCATOR_NEW_DELETE();
Zhenyao Mo9eedea02014-05-12 16:02:35 -070045 TSymbol(const TString *n)
46 : uniqueId(0),
47 name(n)
48 {
49 }
50 virtual ~TSymbol()
51 {
52 // don't delete name, it's from the pool
53 }
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040054
Zhenyao Mo9eedea02014-05-12 16:02:35 -070055 const TString &getName() const
56 {
57 return *name;
58 }
59 virtual const TString &getMangledName() const
60 {
61 return getName();
62 }
63 virtual bool isFunction() const
64 {
65 return false;
66 }
67 virtual bool isVariable() const
68 {
69 return false;
70 }
71 void setUniqueId(int id)
72 {
73 uniqueId = id;
74 }
75 int getUniqueId() const
76 {
77 return uniqueId;
78 }
79 void relateToExtension(const TString &ext)
80 {
81 extension = ext;
82 }
83 const TString &getExtension() const
84 {
85 return extension;
86 }
Nicolas Capensba60ad32013-06-04 15:55:47 -040087
Zhenyao Mo9eedea02014-05-12 16:02:35 -070088 private:
Zhenyao Mo9eedea02014-05-12 16:02:35 -070089 int uniqueId; // For real comparing during code generation
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000090 const TString *name;
Jamie Madill2aeb26a2013-07-08 14:02:55 -040091 TString extension;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092};
93
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094// Variable class, meaning a symbol that's not a function.
95//
96// There could be a separate class heirarchy for Constant variables;
97// Only one of int, bool, or float, (or none) is correct for
98// any particular use, but it's easy to do this way, and doesn't
99// seem worth having separate classes, and "getConst" can't simply return
100// different values for different types polymorphically, so this is
101// just simple and pragmatic.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700102class TVariable : public TSymbol
103{
104 public:
105 TVariable(const TString *name, const TType &t, bool uT = false)
106 : TSymbol(name),
107 type(t),
108 userType(uT),
109 unionArray(0)
110 {
111 }
Corentin Walleze5a1f272015-08-21 02:58:25 +0200112 ~TVariable() override {}
113 bool isVariable() const override { return true; }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700114 TType &getType()
115 {
116 return type;
117 }
118 const TType &getType() const
119 {
120 return type;
121 }
122 bool isUserType() const
123 {
124 return userType;
125 }
126 void setQualifier(TQualifier qualifier)
127 {
128 type.setQualifier(qualifier);
129 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200131 const TConstantUnion *getConstPointer() const { return unionArray; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200133 void shareConstPointer(const TConstantUnion *constArray) { unionArray = constArray; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000134
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700135 private:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136 TType type;
137 bool userType;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700138 // we are assuming that Pool Allocator will free the memory
139 // allocated to unionArray when this object is destroyed.
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200140 const TConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141};
142
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700143// Immutable version of TParameter.
144struct TConstParameter
145{
146 TConstParameter()
147 : name(nullptr),
148 type(nullptr)
149 {
150 }
151 explicit TConstParameter(const TString *n)
152 : name(n),
153 type(nullptr)
154 {
155 }
156 explicit TConstParameter(const TType *t)
157 : name(nullptr),
158 type(t)
159 {
160 }
161 TConstParameter(const TString *n, const TType *t)
162 : name(n),
163 type(t)
164 {
165 }
166
167 // Both constructor arguments must be const.
168 TConstParameter(TString *n, TType *t) = delete;
169 TConstParameter(const TString *n, TType *t) = delete;
170 TConstParameter(TString *n, const TType *t) = delete;
171
172 const TString *name;
173 const TType *type;
174};
175
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000176// The function sub-class of symbols and the parser will need to
177// share this definition of a function parameter.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700178struct TParameter
179{
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700180 // Destructively converts to TConstParameter.
181 // This method resets name and type to nullptrs to make sure
182 // their content cannot be modified after the call.
183 TConstParameter turnToConst()
184 {
185 const TString *constName = name;
186 const TType *constType = type;
187 name = nullptr;
188 type = nullptr;
189 return TConstParameter(constName, constType);
190 }
191
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000192 TString *name;
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400193 TType *type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194};
195
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000196// The function sub-class of a symbol.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700197class TFunction : public TSymbol
198{
199 public:
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700200 TFunction(const TString *name, const TType *retType, TOperator tOp = EOpNull, 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),
205 defined(false)
206 {
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500207 relateToExtension(ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700208 }
Corentin Walleze5a1f272015-08-21 02:58:25 +0200209 ~TFunction() override;
210 bool isFunction() const override { return true; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000211
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700212 static TString mangleName(const TString &name)
213 {
214 return name + '(';
215 }
216 static TString unmangleName(const TString &mangledName)
alokp@chromium.org43884872010-03-30 00:08:52 +0000217 {
218 return TString(mangledName.c_str(), mangledName.find_first_of('('));
219 }
220
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700221 void addParameter(const TConstParameter &p)
Dmitry Skiba58832202015-07-06 16:11:13 -0700222 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223 parameters.push_back(p);
Dmitry Skiba58832202015-07-06 16:11:13 -0700224 mangledName = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000226
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
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700245 void setDefined()
246 {
247 defined = true;
248 }
249 bool isDefined()
250 {
251 return defined;
252 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000253
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700254 size_t getParamCount() const
255 {
256 return parameters.size();
257 }
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700258 const TConstParameter &getParam(size_t i) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700259 {
260 return parameters[i];
261 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000262
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700263 private:
Dmitry Skiba58832202015-07-06 16:11:13 -0700264 const TString *buildMangledName() const;
265
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700266 typedef TVector<TConstParameter> TParamList;
alokp@chromium.org43884872010-03-30 00:08:52 +0000267 TParamList parameters;
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700268 const TType *returnType;
Dmitry Skiba58832202015-07-06 16:11:13 -0700269 mutable const TString *mangledName;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270 TOperator op;
271 bool defined;
272};
273
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000274// Interface block name sub-symbol
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000275class TInterfaceBlockName : public TSymbol
276{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700277 public:
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000278 TInterfaceBlockName(const TString *name)
279 : TSymbol(name)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700280 {
281 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000282
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700283 virtual ~TInterfaceBlockName()
284 {
285 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000286};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700288class TSymbolTableLevel
289{
290 public:
291 typedef TMap<TString, TSymbol *> tLevel;
alokp@chromium.org43884872010-03-30 00:08:52 +0000292 typedef tLevel::const_iterator const_iterator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293 typedef const tLevel::value_type tLevelPair;
294 typedef std::pair<tLevel::iterator, bool> tInsertResult;
295
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700296 TSymbolTableLevel()
297 {
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
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700308 protected:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309 tLevel level;
310};
311
Gus Fernandez964df492014-10-13 11:54:39 -0700312// Define ESymbolLevel as int rather than an enum since level can go
313// above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the
314// compiler optimizes the >= of the last element to ==.
315typedef int ESymbolLevel;
316const int COMMON_BUILTINS = 0;
317const int ESSL1_BUILTINS = 1;
318const int ESSL3_BUILTINS = 2;
319const int LAST_BUILTIN_LEVEL = ESSL3_BUILTINS;
320const int GLOBAL_LEVEL = 3;
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000321
Jamie Madillf0d10f82015-03-31 12:56:52 -0400322class TSymbolTable : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700323{
324 public:
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400325 TSymbolTable()
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700326 : mGlobalInvariant(false)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328 // The symbol table cannot be used until push() is called, but
329 // the lack of an initial call to push() can be used to detect
330 // that the symbol table has not been preloaded with built-ins.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331 }
332
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400333 ~TSymbolTable();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000334
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335 // When the symbol table is initialized with the built-ins, there should
336 // 'push' calls, so that built-ins are at level 0 and the shader
337 // globals are at level 1.
Zhenyao Moe740add2014-07-18 17:01:01 -0700338 bool isEmpty() const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700339 {
340 return table.empty();
341 }
Zhenyao Moe740add2014-07-18 17:01:01 -0700342 bool atBuiltInLevel() const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700343 {
344 return currentLevel() <= LAST_BUILTIN_LEVEL;
345 }
Zhenyao Moe740add2014-07-18 17:01:01 -0700346 bool atGlobalLevel() const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700347 {
348 return currentLevel() <= GLOBAL_LEVEL;
349 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000350 void push()
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000351 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352 table.push_back(new TSymbolTableLevel);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400353 precisionStack.push_back(new PrecisionStackLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354 }
355
alokp@chromium.org43884872010-03-30 00:08:52 +0000356 void pop()
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400357 {
358 delete table.back();
359 table.pop_back();
360
361 delete precisionStack.back();
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000362 precisionStack.pop_back();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363 }
364
Nicolas Capensadfffe42014-06-17 02:13:36 -0400365 bool declare(TSymbol *symbol)
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000366 {
367 return insert(currentLevel(), symbol);
368 }
369
Nicolas Capensadfffe42014-06-17 02:13:36 -0400370 bool insert(ESymbolLevel level, TSymbol *symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371 {
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000372 return table[level]->insert(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000373 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000374
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500375 bool insert(ESymbolLevel level, const char *ext, TSymbol *symbol)
376 {
377 symbol->relateToExtension(ext);
378 return table[level]->insert(symbol);
379 }
380
Nicolas Capens49a88872013-06-20 09:54:03 -0400381 bool insertConstInt(ESymbolLevel level, const char *name, int value)
382 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700383 TVariable *constant = new TVariable(
384 NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200385 TConstantUnion *unionArray = new TConstantUnion[1];
386 unionArray[0].setIConst(value);
387 constant->shareConstPointer(unionArray);
Nicolas Capensadfffe42014-06-17 02:13:36 -0400388 return insert(level, constant);
Nicolas Capens49a88872013-06-20 09:54:03 -0400389 }
390
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300391 bool insertConstIntExt(ESymbolLevel level, const char *ext, const char *name, int value)
392 {
393 TVariable *constant =
394 new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200395 TConstantUnion *unionArray = new TConstantUnion[1];
396 unionArray[0].setIConst(value);
397 constant->shareConstPointer(unionArray);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300398 return insert(level, ext, constant);
399 }
400
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700401 void insertBuiltIn(ESymbolLevel level, TOperator op, const char *ext, const TType *rvalue, const char *name,
402 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 -0500403
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700404 void insertBuiltIn(ESymbolLevel level, const TType *rvalue, const char *name,
405 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 -0500406 {
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500407 insertBuiltIn(level, EOpNull, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
408 }
409
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700410 void insertBuiltIn(ESymbolLevel level, const char *ext, const TType *rvalue, const char *name,
411 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 -0500412 {
413 insertBuiltIn(level, EOpNull, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
414 }
415
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700416 void insertBuiltIn(ESymbolLevel level, TOperator op, const TType *rvalue, const char *name,
417 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 -0500418 {
419 insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
Nicolas Capens482907e2015-02-23 16:56:33 -0500420 }
421
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700422 TSymbol *find(const TString &name, int shaderVersion,
Zhenyao Moe740add2014-07-18 17:01:01 -0700423 bool *builtIn = NULL, bool *sameScope = NULL) const;
424 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000425
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700426 TSymbolTableLevel *getOuterLevel()
427 {
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000428 assert(currentLevel() >= 1);
daniel@transgaming.com5dd6d092012-03-20 20:10:28 +0000429 return table[currentLevel() - 1];
430 }
431
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400432 void dump(TInfoSink &infoSink) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700434 bool setDefaultPrecision(const TPublicType &type, TPrecision prec)
435 {
Zhenyao Moed14b792014-05-08 11:21:07 -0700436 if (!SupportsPrecision(type.type))
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400437 return false;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000438 if (type.isAggregate())
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000439 return false; // Not allowed to set for aggregate types
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000440 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700441 // Uses map operator [], overwrites the current value
442 (*precisionStack[indexOfLastElement])[type.type] = prec;
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000443 return true;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000444 }
445
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700446 // Searches down the precisionStack for a precision qualifier
447 // for the specified TBasicType
Zhenyao Moe740add2014-07-18 17:01:01 -0700448 TPrecision getDefaultPrecision(TBasicType type) const;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000449
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700450 // This records invariant varyings declared through
451 // "invariant varying_name;".
Jamie Madill2c433252014-12-03 12:36:54 -0500452 void addInvariantVarying(const std::string &originalName)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700453 {
454 mInvariantVaryings.insert(originalName);
455 }
456 // If this returns false, the varying could still be invariant
457 // if it is set as invariant during the varying variable
458 // declaration - this piece of information is stored in the
459 // variable's type, not here.
Jamie Madill2c433252014-12-03 12:36:54 -0500460 bool isVaryingInvariant(const std::string &originalName) const
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700461 {
462 return (mGlobalInvariant ||
463 mInvariantVaryings.count(originalName) > 0);
464 }
465
466 void setGlobalInvariant() { mGlobalInvariant = true; }
467 bool getGlobalInvariant() const { return mGlobalInvariant; }
468
Jamie Madillbfa91f42014-06-05 15:45:18 -0400469 static int nextUniqueId()
470 {
471 return ++uniqueIdCounter;
472 }
473
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700474 private:
475 ESymbolLevel currentLevel() const
476 {
477 return static_cast<ESymbolLevel>(table.size() - 1);
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000478 }
479
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700480 std::vector<TSymbolTableLevel *> table;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400481 typedef TMap<TBasicType, TPrecision> PrecisionStackLevel;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700482 std::vector< PrecisionStackLevel *> precisionStack;
Jamie Madillbfa91f42014-06-05 15:45:18 -0400483
Jamie Madill2c433252014-12-03 12:36:54 -0500484 std::set<std::string> mInvariantVaryings;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700485 bool mGlobalInvariant;
486
Jamie Madillbfa91f42014-06-05 15:45:18 -0400487 static int uniqueIdCounter;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488};
489
Geoff Lang0a73dd82014-11-19 16:18:08 -0500490#endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_