blob: 64c43fdc7bf505d3a5b95ff39e02aac3a315dc29 [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"
John Bauman66b8ab22014-05-06 15:57:45 -040041
42//
43// Symbol base class. (Can build functions or variables out of these...)
44//
Nicolas Capens0a7f0c22015-02-18 14:47:31 -050045class TSymbol
46{
John Bauman66b8ab22014-05-06 15:57:45 -040047public:
Nicolas Capens978ddc52014-11-11 12:42:08 -050048 POOL_ALLOCATOR_NEW_DELETE();
John Bauman66b8ab22014-05-06 15:57:45 -040049 TSymbol(const TString *n) : name(n) { }
50 virtual ~TSymbol() { /* don't delete name, it's from the pool */ }
Nicolas Capens978ddc52014-11-11 12:42:08 -050051
John Bauman66b8ab22014-05-06 15:57:45 -040052 const TString& getName() const { return *name; }
53 virtual const TString& getMangledName() const { return getName(); }
54 virtual bool isFunction() const { return false; }
55 virtual bool isVariable() const { return false; }
56 void setUniqueId(int id) { uniqueId = id; }
57 int getUniqueId() const { return uniqueId; }
John Bauman66b8ab22014-05-06 15:57:45 -040058 TSymbol(const TSymbol&);
John Bauman66b8ab22014-05-06 15:57:45 -040059
60protected:
61 const TString *name;
62 unsigned int uniqueId; // For real comparing during code generation
63};
64
65//
66// Variable class, meaning a symbol that's not a function.
Nicolas Capenscc1e9962015-03-02 11:17:58 -050067//
John Bauman66b8ab22014-05-06 15:57:45 -040068// There could be a separate class heirarchy for Constant variables;
69// Only one of int, bool, or float, (or none) is correct for
70// any particular use, but it's easy to do this way, and doesn't
71// seem worth having separate classes, and "getConst" can't simply return
Nicolas Capenscc1e9962015-03-02 11:17:58 -050072// different values for different types polymorphically, so this is
John Bauman66b8ab22014-05-06 15:57:45 -040073// just simple and pragmatic.
74//
Nicolas Capens0a7f0c22015-02-18 14:47:31 -050075class TVariable : public TSymbol
76{
John Bauman66b8ab22014-05-06 15:57:45 -040077public:
78 TVariable(const TString *name, const TType& t, bool uT = false ) : TSymbol(name), type(t), userType(uT), unionArray(0), arrayInformationType(0) { }
79 virtual ~TVariable() { }
Nicolas Capenscc1e9962015-03-02 11:17:58 -050080 virtual bool isVariable() const { return true; }
81 TType& getType() { return type; }
John Bauman66b8ab22014-05-06 15:57:45 -040082 const TType& getType() const { return type; }
83 bool isUserType() const { return userType; }
84 void setQualifier(TQualifier qualifier) { type.setQualifier(qualifier); }
85 void updateArrayInformationType(TType *t) { arrayInformationType = t; }
86 TType* getArrayInformationType() { return arrayInformationType; }
87
John Bauman66b8ab22014-05-06 15:57:45 -040088 ConstantUnion* getConstPointer()
Nicolas Capenscc1e9962015-03-02 11:17:58 -050089 {
John Bauman66b8ab22014-05-06 15:57:45 -040090 if (!unionArray)
91 unionArray = new ConstantUnion[type.getObjectSize()];
92
93 return unionArray;
94 }
95
96 ConstantUnion* getConstPointer() const { return unionArray; }
97
98 void shareConstPointer( ConstantUnion *constArray)
99 {
100 if (unionArray == constArray)
101 return;
102
103 delete[] unionArray;
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500104 unionArray = constArray;
John Bauman66b8ab22014-05-06 15:57:45 -0400105 }
John Bauman66b8ab22014-05-06 15:57:45 -0400106
107protected:
108 TType type;
109 bool userType;
110 // we are assuming that Pool Allocator will free the memory allocated to unionArray
111 // when this object is destroyed
112 ConstantUnion *unionArray;
113 TType *arrayInformationType; // this is used for updating maxArraySize in all the references to a given symbol
114};
115
116//
117// The function sub-class of symbols and the parser will need to
118// share this definition of a function parameter.
119//
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500120struct TParameter
121{
John Bauman66b8ab22014-05-06 15:57:45 -0400122 TString *name;
John Baumand4ae8632014-05-06 16:18:33 -0400123 TType *type;
John Bauman66b8ab22014-05-06 15:57:45 -0400124};
125
126//
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500127// The function sub-class of a symbol.
John Bauman66b8ab22014-05-06 15:57:45 -0400128//
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500129class TFunction : public TSymbol
130{
John Bauman66b8ab22014-05-06 15:57:45 -0400131public:
132 TFunction(TOperator o) :
133 TSymbol(0),
134 returnType(TType(EbtVoid, EbpUndefined)),
135 op(o),
136 defined(false) { }
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500137 TFunction(const TString *name, TType& retType, TOperator tOp = EOpNull, const char *ext = "") :
138 TSymbol(name),
John Bauman66b8ab22014-05-06 15:57:45 -0400139 returnType(retType),
140 mangledName(TFunction::mangleName(*name)),
141 op(tOp),
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500142 extension(ext),
John Bauman66b8ab22014-05-06 15:57:45 -0400143 defined(false) { }
144 virtual ~TFunction();
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500145 virtual bool isFunction() const { return true; }
John Bauman66b8ab22014-05-06 15:57:45 -0400146
147 static TString mangleName(const TString& name) { return name + '('; }
148 static TString unmangleName(const TString& mangledName)
149 {
150 return TString(mangledName.c_str(), mangledName.find_first_of('('));
151 }
152
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500153 void addParameter(TParameter& p)
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500154 {
John Bauman66b8ab22014-05-06 15:57:45 -0400155 parameters.push_back(p);
156 mangledName = mangledName + p.type->getMangledName();
157 }
158
159 const TString& getMangledName() const { return mangledName; }
160 const TType& getReturnType() const { return returnType; }
161
John Bauman66b8ab22014-05-06 15:57:45 -0400162 TOperator getBuiltInOp() const { return op; }
John Bauman66b8ab22014-05-06 15:57:45 -0400163 const TString& getExtension() const { return extension; }
164
165 void setDefined() { defined = true; }
166 bool isDefined() { return defined; }
167
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500168 int getParamCount() const { return static_cast<int>(parameters.size()); }
John Bauman66b8ab22014-05-06 15:57:45 -0400169 const TParameter& getParam(int i) const { return parameters[i]; }
170
John Bauman66b8ab22014-05-06 15:57:45 -0400171protected:
172 typedef TVector<TParameter> TParamList;
173 TParamList parameters;
174 TType returnType;
175 TString mangledName;
176 TOperator op;
177 TString extension;
178 bool defined;
179};
180
181
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500182class TSymbolTableLevel
183{
John Bauman66b8ab22014-05-06 15:57:45 -0400184public:
185 typedef TMap<TString, TSymbol*> tLevel;
186 typedef tLevel::const_iterator const_iterator;
187 typedef const tLevel::value_type tLevelPair;
188 typedef std::pair<tLevel::iterator, bool> tInsertResult;
189
Nicolas Capens978ddc52014-11-11 12:42:08 -0500190 POOL_ALLOCATOR_NEW_DELETE();
John Bauman66b8ab22014-05-06 15:57:45 -0400191 TSymbolTableLevel() { }
192 ~TSymbolTableLevel();
193
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500194 bool insert(TSymbol &symbol)
John Bauman66b8ab22014-05-06 15:57:45 -0400195 {
John Baumand4ae8632014-05-06 16:18:33 -0400196 symbol.setUniqueId(++uniqueId);
197
John Bauman66b8ab22014-05-06 15:57:45 -0400198 //
199 // returning true means symbol was added to the table
200 //
201 tInsertResult result;
202 result = level.insert(tLevelPair(symbol.getMangledName(), &symbol));
203
204 return result.second;
205 }
206
207 TSymbol* find(const TString& name) const
208 {
209 tLevel::const_iterator it = level.find(name);
210 if (it == level.end())
211 return 0;
212 else
213 return (*it).second;
214 }
215
John Bauman66b8ab22014-05-06 15:57:45 -0400216protected:
217 tLevel level;
John Baumand4ae8632014-05-06 16:18:33 -0400218 static int uniqueId; // for unique identification in code generation
John Bauman66b8ab22014-05-06 15:57:45 -0400219};
220
Nicolas Capense2858652015-02-18 15:30:51 -0500221enum ESymbolLevel
222{
Nicolas Capens3d7f6ed2015-02-18 16:34:50 -0500223 COMMON_BUILTINS,
224 ESSL1_BUILTINS,
225 ESSL3_BUILTINS,
226 LAST_BUILTIN_LEVEL = ESSL3_BUILTINS,
227 GLOBAL_LEVEL
Nicolas Capense2858652015-02-18 15:30:51 -0500228};
229
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500230inline bool IsGenType(const TType *type)
231{
232 if(type)
233 {
234 TBasicType basicType = type->getBasicType();
235 return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType || basicType == EbtGenBType;
236 }
237
238 return false;
239}
240
241inline bool IsVecType(const TType *type)
242{
243 if(type)
244 {
245 TBasicType basicType = type->getBasicType();
246 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec || basicType == EbtBVec;
247 }
248
249 return false;
250}
251
252inline TType *GenType(TType *type, int size)
253{
254 ASSERT(size >= 1 && size <= 4);
255
256 if(!type)
257 {
258 return nullptr;
259 }
260
261 ASSERT(!IsVecType(type));
262
263 switch(type->getBasicType())
264 {
265 case EbtGenType: return new TType(EbtFloat, size);
266 case EbtGenIType: return new TType(EbtInt, size);
267 case EbtGenUType: return new TType(EbtUInt, size);
268 case EbtGenBType: return new TType(EbtBool, size);
269 default: return type;
270 }
271}
272
273inline TType *VecType(TType *type, int size)
274{
275 ASSERT(size >= 2 && size <= 4);
276
277 if(!type)
278 {
279 return nullptr;
280 }
281
282 ASSERT(!IsGenType(type));
283
284 switch(type->getBasicType())
285 {
286 case EbtVec: return new TType(EbtFloat, size);
287 case EbtIVec: return new TType(EbtInt, size);
288 case EbtUVec: return new TType(EbtUInt, size);
289 case EbtBVec: return new TType(EbtBool, size);
290 default: return type;
291 }
292}
293
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500294class TSymbolTable
295{
John Bauman66b8ab22014-05-06 15:57:45 -0400296public:
John Baumand4ae8632014-05-06 16:18:33 -0400297 TSymbolTable()
John Bauman66b8ab22014-05-06 15:57:45 -0400298 {
299 //
300 // The symbol table cannot be used until push() is called, but
301 // the lack of an initial call to push() can be used to detect
302 // that the symbol table has not been preloaded with built-ins.
303 //
304 }
305
306 ~TSymbolTable()
307 {
Nicolas Capense2858652015-02-18 15:30:51 -0500308 while(currentLevel() > LAST_BUILTIN_LEVEL)
309 {
310 pop();
311 }
John Bauman66b8ab22014-05-06 15:57:45 -0400312 }
313
Nicolas Capense2858652015-02-18 15:30:51 -0500314 bool isEmpty() { return table.empty(); }
315 bool atBuiltInLevel() { return currentLevel() <= LAST_BUILTIN_LEVEL; }
316 bool atGlobalLevel() { return currentLevel() <= GLOBAL_LEVEL; }
John Bauman66b8ab22014-05-06 15:57:45 -0400317 void push()
318 {
319 table.push_back(new TSymbolTableLevel);
320 precisionStack.push_back( PrecisionStackLevel() );
321 }
322
323 void pop()
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500324 {
325 delete table[currentLevel()];
326 table.pop_back();
John Bauman66b8ab22014-05-06 15:57:45 -0400327 precisionStack.pop_back();
328 }
329
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500330 bool declare(TSymbol &symbol)
John Bauman66b8ab22014-05-06 15:57:45 -0400331 {
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500332 return insert(currentLevel(), symbol);
333 }
334
Nicolas Capense2858652015-02-18 15:30:51 -0500335 bool insert(ESymbolLevel level, TSymbol &symbol)
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500336 {
337 return table[level]->insert(symbol);
John Bauman66b8ab22014-05-06 15:57:45 -0400338 }
339
Nicolas Capense2858652015-02-18 15:30:51 -0500340 bool insertConstInt(ESymbolLevel level, const char *name, int value)
John Baumand4ae8632014-05-06 16:18:33 -0400341 {
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500342 TVariable *constant = new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConstExpr, 1));
John Baumand4ae8632014-05-06 16:18:33 -0400343 constant->getConstPointer()->setIConst(value);
Nicolas Capense2858652015-02-18 15:30:51 -0500344 return insert(level, *constant);
John Baumand4ae8632014-05-06 16:18:33 -0400345 }
346
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500347 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)
John Baumand4ae8632014-05-06 16:18:33 -0400348 {
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500349 if(ptype1->getBasicType() == EbtGSampler2D)
350 {
351 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
352 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2D), ptype2, ptype3, ptype4);
353 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2D), ptype2, ptype3, ptype4);
354 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2D), ptype2, ptype3, ptype4);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500355 }
356 else if(ptype1->getBasicType() == EbtGSampler3D)
357 {
358 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
359 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler3D), ptype2, ptype3, ptype4);
360 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler3D), ptype2, ptype3, ptype4);
361 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler3D), ptype2, ptype3, ptype4);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500362 }
363 else if(ptype1->getBasicType() == EbtGSamplerCube)
364 {
365 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
366 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSamplerCube), ptype2, ptype3, ptype4);
367 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISamplerCube), ptype2, ptype3, ptype4);
368 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSamplerCube), ptype2, ptype3, ptype4);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500369 }
370 else if(ptype1->getBasicType() == EbtGSampler2DArray)
371 {
372 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
373 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2DArray), ptype2, ptype3, ptype4);
374 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2DArray), ptype2, ptype3, ptype4);
375 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2DArray), ptype2, ptype3, ptype4);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500376 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500377 else if(IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3))
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500378 {
Nicolas Capens33cda112015-02-24 17:35:47 -0500379 ASSERT(!ptype4);
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500380 insertBuiltIn(level, op, ext, GenType(rvalue, 1), name, GenType(ptype1, 1), GenType(ptype2, 1), GenType(ptype3, 1));
381 insertBuiltIn(level, op, ext, GenType(rvalue, 2), name, GenType(ptype1, 2), GenType(ptype2, 2), GenType(ptype3, 2));
382 insertBuiltIn(level, op, ext, GenType(rvalue, 3), name, GenType(ptype1, 3), GenType(ptype2, 3), GenType(ptype3, 3));
383 insertBuiltIn(level, op, ext, GenType(rvalue, 4), name, GenType(ptype1, 4), GenType(ptype2, 4), GenType(ptype3, 4));
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500384 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500385 else if(IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500386 {
Nicolas Capens33cda112015-02-24 17:35:47 -0500387 ASSERT(!ptype4);
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500388 insertBuiltIn(level, op, ext, VecType(rvalue, 2), name, VecType(ptype1, 2), VecType(ptype2, 2), VecType(ptype3, 2));
389 insertBuiltIn(level, op, ext, VecType(rvalue, 3), name, VecType(ptype1, 3), VecType(ptype2, 3), VecType(ptype3, 3));
390 insertBuiltIn(level, op, ext, VecType(rvalue, 4), name, VecType(ptype1, 4), VecType(ptype2, 4), VecType(ptype3, 4));
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500391 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500392 else
John Baumand4ae8632014-05-06 16:18:33 -0400393 {
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500394 TFunction *function = new TFunction(NewPoolTString(name), *rvalue, op, ext);
John Baumand4ae8632014-05-06 16:18:33 -0400395
Nicolas Capens33cda112015-02-24 17:35:47 -0500396 TParameter param1 = {0, ptype1};
397 function->addParameter(param1);
John Baumand4ae8632014-05-06 16:18:33 -0400398
Nicolas Capens33cda112015-02-24 17:35:47 -0500399 if(ptype2)
400 {
401 TParameter param2 = {0, ptype2};
402 function->addParameter(param2);
403 }
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500404
Nicolas Capens33cda112015-02-24 17:35:47 -0500405 if(ptype3)
406 {
407 TParameter param3 = {0, ptype3};
408 function->addParameter(param3);
409 }
410
411 if(ptype4)
412 {
413 TParameter param4 = {0, ptype4};
414 function->addParameter(param4);
415 }
416
417 insert(level, *function);
418 }
419 }
420
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500421 void insertBuiltIn(ESymbolLevel level, TOperator op, TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0)
422 {
423 insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4);
424 }
425
Nicolas Capens33cda112015-02-24 17:35:47 -0500426 void insertBuiltIn(ESymbolLevel level, TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0)
427 {
428 insertBuiltIn(level, EOpNull, rvalue, name, ptype1, ptype2, ptype3, ptype4);
John Bauman66b8ab22014-05-06 15:57:45 -0400429 }
430
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500431 TSymbol *find(const TString &name, int shaderVersion, bool *builtIn = nullptr, bool *sameScope = nullptr) const;
Nicolas Capense2858652015-02-18 15:30:51 -0500432 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
John Bauman66b8ab22014-05-06 15:57:45 -0400433
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500434 TSymbolTableLevel *getOuterLevel() const
435 {
Nicolas Capense2858652015-02-18 15:30:51 -0500436 assert(currentLevel() >= 1);
John Bauman66b8ab22014-05-06 15:57:45 -0400437 return table[currentLevel() - 1];
438 }
439
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500440 bool setDefaultPrecision(const TPublicType &type, TPrecision prec)
441 {
John Baumand4ae8632014-05-06 16:18:33 -0400442 if (IsSampler(type.type))
443 return true; // Skip sampler types for the time being
444 if (type.type != EbtFloat && type.type != EbtInt)
445 return false; // Only set default precision for int/float
Alexis Hetub14178b2015-04-13 13:23:20 -0400446 if (type.primarySize > 1 || type.secondarySize > 1 || type.array)
John Baumand4ae8632014-05-06 16:18:33 -0400447 return false; // Not allowed to set for aggregate types
John Bauman66b8ab22014-05-06 15:57:45 -0400448 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
John Baumand4ae8632014-05-06 16:18:33 -0400449 precisionStack[indexOfLastElement][type.type] = prec; // Uses map operator [], overwrites the current value
450 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400451 }
452
453 // Searches down the precisionStack for a precision qualifier for the specified TBasicType
Nicolas Capens3c20f802015-02-17 17:17:20 -0500454 TPrecision getDefaultPrecision( TBasicType type)
455 {
456 // unsigned integers use the same precision as signed
457 if (type == EbtUInt) type = EbtInt;
458
John Bauman66b8ab22014-05-06 15:57:45 -0400459 if( type != EbtFloat && type != EbtInt ) return EbpUndefined;
460 int level = static_cast<int>(precisionStack.size()) - 1;
461 assert( level >= 0); // Just to be safe. Should not happen.
462 PrecisionStackLevel::iterator it;
463 TPrecision prec = EbpUndefined; // If we dont find anything we return this. Should we error check this?
464 while( level >= 0 ){
465 it = precisionStack[level].find( type );
466 if( it != precisionStack[level].end() ){
467 prec = (*it).second;
468 break;
469 }
470 level--;
471 }
472 return prec;
473 }
474
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500475protected:
Nicolas Capense2858652015-02-18 15:30:51 -0500476 ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
John Bauman66b8ab22014-05-06 15:57:45 -0400477
478 std::vector<TSymbolTableLevel*> table;
479 typedef std::map< TBasicType, TPrecision > PrecisionStackLevel;
480 std::vector< PrecisionStackLevel > precisionStack;
John Bauman66b8ab22014-05-06 15:57:45 -0400481};
482
483#endif // _SYMBOL_TABLE_INCLUDED_