blob: cf6f150fd518993f5a8c3a1fd346d323ebebe3b2 [file] [log] [blame]
John Bauman66b8ab22014-05-06 15:57:45 -04001//
John Baumand4ae8632014-05-06 16:18:33 -04002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
John Bauman66b8ab22014-05-06 15:57:45 -04003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#ifndef _SYMBOL_TABLE_INCLUDED_
8#define _SYMBOL_TABLE_INCLUDED_
9
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//
Nicolas Capenscc1e9962015-03-02 11:17:58 -050021// * Pushing and popping of scope, so symbol table will really be a stack
John Bauman66b8ab22014-05-06 15:57:45 -040022// 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
Greg Hartmand61ac5f2015-04-09 18:48:53 -070033#ifndef __ANDROID__
John Bauman66b8ab22014-05-06 15:57:45 -040034#include <assert.h>
Greg Hartmand61ac5f2015-04-09 18:48:53 -070035#else
36#include "../../Common/DebugAndroid.hpp"
37#endif
John Bauman66b8ab22014-05-06 15:57:45 -040038
Nicolas Capenscc863da2015-01-21 15:50:55 -050039#include "InfoSink.h"
40#include "intermediate.h"
Alexis Hetudd7ff7a2015-06-11 08:25:30 -040041#include <set>
John Bauman66b8ab22014-05-06 15:57:45 -040042
43//
44// Symbol base class. (Can build functions or variables out of these...)
45//
Nicolas Capens0a7f0c22015-02-18 14:47:31 -050046class TSymbol
47{
John Bauman66b8ab22014-05-06 15:57:45 -040048public:
Nicolas Capens978ddc52014-11-11 12:42:08 -050049 POOL_ALLOCATOR_NEW_DELETE();
John Bauman66b8ab22014-05-06 15:57:45 -040050 TSymbol(const TString *n) : name(n) { }
51 virtual ~TSymbol() { /* don't delete name, it's from the pool */ }
Nicolas Capens978ddc52014-11-11 12:42:08 -050052
John Bauman66b8ab22014-05-06 15:57:45 -040053 const TString& getName() const { return *name; }
54 virtual const TString& getMangledName() const { return getName(); }
55 virtual bool isFunction() const { return false; }
56 virtual bool isVariable() const { return false; }
57 void setUniqueId(int id) { uniqueId = id; }
58 int getUniqueId() const { return uniqueId; }
John Bauman66b8ab22014-05-06 15:57:45 -040059 TSymbol(const TSymbol&);
John Bauman66b8ab22014-05-06 15:57:45 -040060
61protected:
62 const TString *name;
63 unsigned int uniqueId; // For real comparing during code generation
64};
65
66//
67// Variable class, meaning a symbol that's not a function.
Nicolas Capenscc1e9962015-03-02 11:17:58 -050068//
John Bauman66b8ab22014-05-06 15:57:45 -040069// There could be a separate class heirarchy for Constant variables;
70// Only one of int, bool, or float, (or none) is correct for
71// any particular use, but it's easy to do this way, and doesn't
72// seem worth having separate classes, and "getConst" can't simply return
Nicolas Capenscc1e9962015-03-02 11:17:58 -050073// different values for different types polymorphically, so this is
John Bauman66b8ab22014-05-06 15:57:45 -040074// just simple and pragmatic.
75//
Nicolas Capens0a7f0c22015-02-18 14:47:31 -050076class TVariable : public TSymbol
77{
John Bauman66b8ab22014-05-06 15:57:45 -040078public:
79 TVariable(const TString *name, const TType& t, bool uT = false ) : TSymbol(name), type(t), userType(uT), unionArray(0), arrayInformationType(0) { }
80 virtual ~TVariable() { }
Nicolas Capenscc1e9962015-03-02 11:17:58 -050081 virtual bool isVariable() const { return true; }
82 TType& getType() { return type; }
John Bauman66b8ab22014-05-06 15:57:45 -040083 const TType& getType() const { return type; }
84 bool isUserType() const { return userType; }
85 void setQualifier(TQualifier qualifier) { type.setQualifier(qualifier); }
86 void updateArrayInformationType(TType *t) { arrayInformationType = t; }
87 TType* getArrayInformationType() { return arrayInformationType; }
88
John Bauman66b8ab22014-05-06 15:57:45 -040089 ConstantUnion* getConstPointer()
Nicolas Capenscc1e9962015-03-02 11:17:58 -050090 {
John Bauman66b8ab22014-05-06 15:57:45 -040091 if (!unionArray)
92 unionArray = new ConstantUnion[type.getObjectSize()];
93
94 return unionArray;
95 }
96
97 ConstantUnion* getConstPointer() const { return unionArray; }
98
99 void shareConstPointer( ConstantUnion *constArray)
100 {
101 if (unionArray == constArray)
102 return;
103
104 delete[] unionArray;
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500105 unionArray = constArray;
John Bauman66b8ab22014-05-06 15:57:45 -0400106 }
John Bauman66b8ab22014-05-06 15:57:45 -0400107
108protected:
109 TType type;
110 bool userType;
111 // we are assuming that Pool Allocator will free the memory allocated to unionArray
112 // when this object is destroyed
113 ConstantUnion *unionArray;
114 TType *arrayInformationType; // this is used for updating maxArraySize in all the references to a given symbol
115};
116
117//
118// The function sub-class of symbols and the parser will need to
119// share this definition of a function parameter.
120//
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500121struct TParameter
122{
John Bauman66b8ab22014-05-06 15:57:45 -0400123 TString *name;
John Baumand4ae8632014-05-06 16:18:33 -0400124 TType *type;
John Bauman66b8ab22014-05-06 15:57:45 -0400125};
126
127//
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500128// The function sub-class of a symbol.
John Bauman66b8ab22014-05-06 15:57:45 -0400129//
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500130class TFunction : public TSymbol
131{
John Bauman66b8ab22014-05-06 15:57:45 -0400132public:
133 TFunction(TOperator o) :
134 TSymbol(0),
135 returnType(TType(EbtVoid, EbpUndefined)),
136 op(o),
137 defined(false) { }
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500138 TFunction(const TString *name, TType& retType, TOperator tOp = EOpNull, const char *ext = "") :
139 TSymbol(name),
John Bauman66b8ab22014-05-06 15:57:45 -0400140 returnType(retType),
141 mangledName(TFunction::mangleName(*name)),
142 op(tOp),
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500143 extension(ext),
John Bauman66b8ab22014-05-06 15:57:45 -0400144 defined(false) { }
145 virtual ~TFunction();
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500146 virtual bool isFunction() const { return true; }
John Bauman66b8ab22014-05-06 15:57:45 -0400147
148 static TString mangleName(const TString& name) { return name + '('; }
149 static TString unmangleName(const TString& mangledName)
150 {
151 return TString(mangledName.c_str(), mangledName.find_first_of('('));
152 }
153
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500154 void addParameter(TParameter& p)
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500155 {
John Bauman66b8ab22014-05-06 15:57:45 -0400156 parameters.push_back(p);
157 mangledName = mangledName + p.type->getMangledName();
158 }
159
160 const TString& getMangledName() const { return mangledName; }
161 const TType& getReturnType() const { return returnType; }
162
John Bauman66b8ab22014-05-06 15:57:45 -0400163 TOperator getBuiltInOp() const { return op; }
John Bauman66b8ab22014-05-06 15:57:45 -0400164 const TString& getExtension() const { return extension; }
165
166 void setDefined() { defined = true; }
167 bool isDefined() { return defined; }
168
Alexis Hetua818c452015-06-11 13:06:58 -0400169 size_t getParamCount() const { return parameters.size(); }
John Bauman66b8ab22014-05-06 15:57:45 -0400170 const TParameter& getParam(int i) const { return parameters[i]; }
171
John Bauman66b8ab22014-05-06 15:57:45 -0400172protected:
173 typedef TVector<TParameter> TParamList;
174 TParamList parameters;
175 TType returnType;
176 TString mangledName;
177 TOperator op;
178 TString extension;
179 bool defined;
180};
181
182
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500183class TSymbolTableLevel
184{
John Bauman66b8ab22014-05-06 15:57:45 -0400185public:
186 typedef TMap<TString, TSymbol*> tLevel;
187 typedef tLevel::const_iterator const_iterator;
188 typedef const tLevel::value_type tLevelPair;
189 typedef std::pair<tLevel::iterator, bool> tInsertResult;
190
Nicolas Capens978ddc52014-11-11 12:42:08 -0500191 POOL_ALLOCATOR_NEW_DELETE();
John Bauman66b8ab22014-05-06 15:57:45 -0400192 TSymbolTableLevel() { }
193 ~TSymbolTableLevel();
194
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500195 bool insert(TSymbol &symbol)
John Bauman66b8ab22014-05-06 15:57:45 -0400196 {
Alexis Hetuad6b8752015-06-09 16:15:30 -0400197 symbol.setUniqueId(nextUniqueId());
John Baumand4ae8632014-05-06 16:18:33 -0400198
John Bauman66b8ab22014-05-06 15:57:45 -0400199 //
200 // returning true means symbol was added to the table
201 //
202 tInsertResult result;
203 result = level.insert(tLevelPair(symbol.getMangledName(), &symbol));
204
205 return result.second;
206 }
207
208 TSymbol* find(const TString& name) const
209 {
210 tLevel::const_iterator it = level.find(name);
211 if (it == level.end())
212 return 0;
213 else
214 return (*it).second;
215 }
216
Alexis Hetuad6b8752015-06-09 16:15:30 -0400217 static int nextUniqueId()
218 {
219 return ++uniqueId;
220 }
221
John Bauman66b8ab22014-05-06 15:57:45 -0400222protected:
223 tLevel level;
John Baumand4ae8632014-05-06 16:18:33 -0400224 static int uniqueId; // for unique identification in code generation
John Bauman66b8ab22014-05-06 15:57:45 -0400225};
226
Nicolas Capense2858652015-02-18 15:30:51 -0500227enum ESymbolLevel
228{
Nicolas Capens3d7f6ed2015-02-18 16:34:50 -0500229 COMMON_BUILTINS,
230 ESSL1_BUILTINS,
231 ESSL3_BUILTINS,
232 LAST_BUILTIN_LEVEL = ESSL3_BUILTINS,
233 GLOBAL_LEVEL
Nicolas Capense2858652015-02-18 15:30:51 -0500234};
235
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500236inline bool IsGenType(const TType *type)
237{
238 if(type)
239 {
240 TBasicType basicType = type->getBasicType();
241 return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType || basicType == EbtGenBType;
242 }
243
244 return false;
245}
246
247inline bool IsVecType(const TType *type)
248{
249 if(type)
250 {
251 TBasicType basicType = type->getBasicType();
252 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec || basicType == EbtBVec;
253 }
254
255 return false;
256}
257
258inline TType *GenType(TType *type, int size)
259{
260 ASSERT(size >= 1 && size <= 4);
261
262 if(!type)
263 {
264 return nullptr;
265 }
266
267 ASSERT(!IsVecType(type));
268
269 switch(type->getBasicType())
270 {
271 case EbtGenType: return new TType(EbtFloat, size);
272 case EbtGenIType: return new TType(EbtInt, size);
273 case EbtGenUType: return new TType(EbtUInt, size);
274 case EbtGenBType: return new TType(EbtBool, size);
275 default: return type;
276 }
277}
278
279inline TType *VecType(TType *type, int size)
280{
281 ASSERT(size >= 2 && size <= 4);
282
283 if(!type)
284 {
285 return nullptr;
286 }
287
288 ASSERT(!IsGenType(type));
289
290 switch(type->getBasicType())
291 {
292 case EbtVec: return new TType(EbtFloat, size);
293 case EbtIVec: return new TType(EbtInt, size);
294 case EbtUVec: return new TType(EbtUInt, size);
295 case EbtBVec: return new TType(EbtBool, size);
296 default: return type;
297 }
298}
299
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500300class TSymbolTable
301{
John Bauman66b8ab22014-05-06 15:57:45 -0400302public:
John Baumand4ae8632014-05-06 16:18:33 -0400303 TSymbolTable()
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400304 : mGlobalInvariant(false)
John Bauman66b8ab22014-05-06 15:57:45 -0400305 {
306 //
307 // The symbol table cannot be used until push() is called, but
308 // the lack of an initial call to push() can be used to detect
309 // that the symbol table has not been preloaded with built-ins.
310 //
311 }
312
313 ~TSymbolTable()
314 {
Nicolas Capense2858652015-02-18 15:30:51 -0500315 while(currentLevel() > LAST_BUILTIN_LEVEL)
316 {
317 pop();
318 }
John Bauman66b8ab22014-05-06 15:57:45 -0400319 }
320
Nicolas Capense2858652015-02-18 15:30:51 -0500321 bool isEmpty() { return table.empty(); }
322 bool atBuiltInLevel() { return currentLevel() <= LAST_BUILTIN_LEVEL; }
323 bool atGlobalLevel() { return currentLevel() <= GLOBAL_LEVEL; }
John Bauman66b8ab22014-05-06 15:57:45 -0400324 void push()
325 {
326 table.push_back(new TSymbolTableLevel);
327 precisionStack.push_back( PrecisionStackLevel() );
328 }
329
330 void pop()
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500331 {
332 delete table[currentLevel()];
333 table.pop_back();
John Bauman66b8ab22014-05-06 15:57:45 -0400334 precisionStack.pop_back();
335 }
336
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500337 bool declare(TSymbol &symbol)
John Bauman66b8ab22014-05-06 15:57:45 -0400338 {
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500339 return insert(currentLevel(), symbol);
340 }
341
Nicolas Capense2858652015-02-18 15:30:51 -0500342 bool insert(ESymbolLevel level, TSymbol &symbol)
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500343 {
344 return table[level]->insert(symbol);
John Bauman66b8ab22014-05-06 15:57:45 -0400345 }
346
Nicolas Capense2858652015-02-18 15:30:51 -0500347 bool insertConstInt(ESymbolLevel level, const char *name, int value)
John Baumand4ae8632014-05-06 16:18:33 -0400348 {
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500349 TVariable *constant = new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConstExpr, 1));
John Baumand4ae8632014-05-06 16:18:33 -0400350 constant->getConstPointer()->setIConst(value);
Nicolas Capense2858652015-02-18 15:30:51 -0500351 return insert(level, *constant);
John Baumand4ae8632014-05-06 16:18:33 -0400352 }
353
Alexis Hetub5332c52015-06-04 13:04:17 -0400354 void insertBuiltIn(ESymbolLevel level, TOperator op, const char *ext, TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0, TType *ptype5 = 0)
John Baumand4ae8632014-05-06 16:18:33 -0400355 {
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500356 if(ptype1->getBasicType() == EbtGSampler2D)
357 {
358 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Alexis Hetub5332c52015-06-04 13:04:17 -0400359 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5);
360 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5);
361 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500362 }
363 else if(ptype1->getBasicType() == EbtGSampler3D)
364 {
365 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Alexis Hetub5332c52015-06-04 13:04:17 -0400366 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler3D), ptype2, ptype3, ptype4, ptype5);
367 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5);
368 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500369 }
370 else if(ptype1->getBasicType() == EbtGSamplerCube)
371 {
372 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Alexis Hetub5332c52015-06-04 13:04:17 -0400373 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5);
374 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5);
375 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500376 }
377 else if(ptype1->getBasicType() == EbtGSampler2DArray)
378 {
379 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Alexis Hetub5332c52015-06-04 13:04:17 -0400380 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5);
381 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5);
382 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500383 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500384 else if(IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3))
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500385 {
Nicolas Capens33cda112015-02-24 17:35:47 -0500386 ASSERT(!ptype4);
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500387 insertBuiltIn(level, op, ext, GenType(rvalue, 1), name, GenType(ptype1, 1), GenType(ptype2, 1), GenType(ptype3, 1));
388 insertBuiltIn(level, op, ext, GenType(rvalue, 2), name, GenType(ptype1, 2), GenType(ptype2, 2), GenType(ptype3, 2));
389 insertBuiltIn(level, op, ext, GenType(rvalue, 3), name, GenType(ptype1, 3), GenType(ptype2, 3), GenType(ptype3, 3));
390 insertBuiltIn(level, op, ext, GenType(rvalue, 4), name, GenType(ptype1, 4), GenType(ptype2, 4), GenType(ptype3, 4));
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500391 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500392 else if(IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500393 {
Nicolas Capens33cda112015-02-24 17:35:47 -0500394 ASSERT(!ptype4);
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500395 insertBuiltIn(level, op, ext, VecType(rvalue, 2), name, VecType(ptype1, 2), VecType(ptype2, 2), VecType(ptype3, 2));
396 insertBuiltIn(level, op, ext, VecType(rvalue, 3), name, VecType(ptype1, 3), VecType(ptype2, 3), VecType(ptype3, 3));
397 insertBuiltIn(level, op, ext, VecType(rvalue, 4), name, VecType(ptype1, 4), VecType(ptype2, 4), VecType(ptype3, 4));
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500398 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500399 else
John Baumand4ae8632014-05-06 16:18:33 -0400400 {
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500401 TFunction *function = new TFunction(NewPoolTString(name), *rvalue, op, ext);
John Baumand4ae8632014-05-06 16:18:33 -0400402
Nicolas Capens33cda112015-02-24 17:35:47 -0500403 TParameter param1 = {0, ptype1};
404 function->addParameter(param1);
John Baumand4ae8632014-05-06 16:18:33 -0400405
Nicolas Capens33cda112015-02-24 17:35:47 -0500406 if(ptype2)
407 {
408 TParameter param2 = {0, ptype2};
409 function->addParameter(param2);
410 }
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500411
Nicolas Capens33cda112015-02-24 17:35:47 -0500412 if(ptype3)
413 {
414 TParameter param3 = {0, ptype3};
415 function->addParameter(param3);
416 }
417
418 if(ptype4)
419 {
420 TParameter param4 = {0, ptype4};
421 function->addParameter(param4);
422 }
423
Alexis Hetub5332c52015-06-04 13:04:17 -0400424 if(ptype5)
425 {
426 TParameter param5 = {0, ptype5};
427 function->addParameter(param5);
428 }
429
Nicolas Capens33cda112015-02-24 17:35:47 -0500430 insert(level, *function);
431 }
432 }
433
Alexis Hetub5332c52015-06-04 13:04:17 -0400434 void insertBuiltIn(ESymbolLevel level, TOperator op, TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0, TType *ptype5 = 0)
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500435 {
Alexis Hetub5332c52015-06-04 13:04:17 -0400436 insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500437 }
438
Alexis Hetub5332c52015-06-04 13:04:17 -0400439 void insertBuiltIn(ESymbolLevel level, TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0, TType *ptype5 = 0)
Nicolas Capens33cda112015-02-24 17:35:47 -0500440 {
Alexis Hetub5332c52015-06-04 13:04:17 -0400441 insertBuiltIn(level, EOpNull, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
John Bauman66b8ab22014-05-06 15:57:45 -0400442 }
443
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500444 TSymbol *find(const TString &name, int shaderVersion, bool *builtIn = nullptr, bool *sameScope = nullptr) const;
Nicolas Capense2858652015-02-18 15:30:51 -0500445 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
John Bauman66b8ab22014-05-06 15:57:45 -0400446
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500447 TSymbolTableLevel *getOuterLevel() const
448 {
Nicolas Capense2858652015-02-18 15:30:51 -0500449 assert(currentLevel() >= 1);
John Bauman66b8ab22014-05-06 15:57:45 -0400450 return table[currentLevel() - 1];
451 }
452
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500453 bool setDefaultPrecision(const TPublicType &type, TPrecision prec)
454 {
John Baumand4ae8632014-05-06 16:18:33 -0400455 if (IsSampler(type.type))
456 return true; // Skip sampler types for the time being
457 if (type.type != EbtFloat && type.type != EbtInt)
458 return false; // Only set default precision for int/float
Alexis Hetub14178b2015-04-13 13:23:20 -0400459 if (type.primarySize > 1 || type.secondarySize > 1 || type.array)
John Baumand4ae8632014-05-06 16:18:33 -0400460 return false; // Not allowed to set for aggregate types
John Bauman66b8ab22014-05-06 15:57:45 -0400461 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
John Baumand4ae8632014-05-06 16:18:33 -0400462 precisionStack[indexOfLastElement][type.type] = prec; // Uses map operator [], overwrites the current value
463 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400464 }
465
466 // Searches down the precisionStack for a precision qualifier for the specified TBasicType
Nicolas Capens3c20f802015-02-17 17:17:20 -0500467 TPrecision getDefaultPrecision( TBasicType type)
468 {
469 // unsigned integers use the same precision as signed
470 if (type == EbtUInt) type = EbtInt;
471
John Bauman66b8ab22014-05-06 15:57:45 -0400472 if( type != EbtFloat && type != EbtInt ) return EbpUndefined;
473 int level = static_cast<int>(precisionStack.size()) - 1;
474 assert( level >= 0); // Just to be safe. Should not happen.
475 PrecisionStackLevel::iterator it;
476 TPrecision prec = EbpUndefined; // If we dont find anything we return this. Should we error check this?
477 while( level >= 0 ){
478 it = precisionStack[level].find( type );
479 if( it != precisionStack[level].end() ){
480 prec = (*it).second;
481 break;
482 }
483 level--;
484 }
485 return prec;
486 }
487
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400488 // This records invariant varyings declared through
489 // "invariant varying_name;".
490 void addInvariantVarying(const std::string &originalName)
491 {
492 mInvariantVaryings.insert(originalName);
493 }
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.
498 bool isVaryingInvariant(const std::string &originalName) const
499 {
500 return (mGlobalInvariant ||
501 mInvariantVaryings.count(originalName) > 0);
502 }
503
504 void setGlobalInvariant() { mGlobalInvariant = true; }
505 bool getGlobalInvariant() const { return mGlobalInvariant; }
506
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500507protected:
Nicolas Capense2858652015-02-18 15:30:51 -0500508 ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
John Bauman66b8ab22014-05-06 15:57:45 -0400509
510 std::vector<TSymbolTableLevel*> table;
511 typedef std::map< TBasicType, TPrecision > PrecisionStackLevel;
512 std::vector< PrecisionStackLevel > precisionStack;
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400513
514 std::set<std::string> mInvariantVaryings;
515 bool mGlobalInvariant;
John Bauman66b8ab22014-05-06 15:57:45 -0400516};
517
518#endif // _SYMBOL_TABLE_INCLUDED_