blob: 96a21bcf985ba9486f71c9be75ab43b7ab7e5fd4 [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 {
Alexis Hetuad6b8752015-06-09 16:15:30 -0400196 symbol.setUniqueId(nextUniqueId());
John Baumand4ae8632014-05-06 16:18:33 -0400197
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
Alexis Hetuad6b8752015-06-09 16:15:30 -0400216 static int nextUniqueId()
217 {
218 return ++uniqueId;
219 }
220
John Bauman66b8ab22014-05-06 15:57:45 -0400221protected:
222 tLevel level;
John Baumand4ae8632014-05-06 16:18:33 -0400223 static int uniqueId; // for unique identification in code generation
John Bauman66b8ab22014-05-06 15:57:45 -0400224};
225
Nicolas Capense2858652015-02-18 15:30:51 -0500226enum ESymbolLevel
227{
Nicolas Capens3d7f6ed2015-02-18 16:34:50 -0500228 COMMON_BUILTINS,
229 ESSL1_BUILTINS,
230 ESSL3_BUILTINS,
231 LAST_BUILTIN_LEVEL = ESSL3_BUILTINS,
232 GLOBAL_LEVEL
Nicolas Capense2858652015-02-18 15:30:51 -0500233};
234
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500235inline bool IsGenType(const TType *type)
236{
237 if(type)
238 {
239 TBasicType basicType = type->getBasicType();
240 return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType || basicType == EbtGenBType;
241 }
242
243 return false;
244}
245
246inline bool IsVecType(const TType *type)
247{
248 if(type)
249 {
250 TBasicType basicType = type->getBasicType();
251 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec || basicType == EbtBVec;
252 }
253
254 return false;
255}
256
257inline TType *GenType(TType *type, int size)
258{
259 ASSERT(size >= 1 && size <= 4);
260
261 if(!type)
262 {
263 return nullptr;
264 }
265
266 ASSERT(!IsVecType(type));
267
268 switch(type->getBasicType())
269 {
270 case EbtGenType: return new TType(EbtFloat, size);
271 case EbtGenIType: return new TType(EbtInt, size);
272 case EbtGenUType: return new TType(EbtUInt, size);
273 case EbtGenBType: return new TType(EbtBool, size);
274 default: return type;
275 }
276}
277
278inline TType *VecType(TType *type, int size)
279{
280 ASSERT(size >= 2 && size <= 4);
281
282 if(!type)
283 {
284 return nullptr;
285 }
286
287 ASSERT(!IsGenType(type));
288
289 switch(type->getBasicType())
290 {
291 case EbtVec: return new TType(EbtFloat, size);
292 case EbtIVec: return new TType(EbtInt, size);
293 case EbtUVec: return new TType(EbtUInt, size);
294 case EbtBVec: return new TType(EbtBool, size);
295 default: return type;
296 }
297}
298
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500299class TSymbolTable
300{
John Bauman66b8ab22014-05-06 15:57:45 -0400301public:
John Baumand4ae8632014-05-06 16:18:33 -0400302 TSymbolTable()
John Bauman66b8ab22014-05-06 15:57:45 -0400303 {
304 //
305 // The symbol table cannot be used until push() is called, but
306 // the lack of an initial call to push() can be used to detect
307 // that the symbol table has not been preloaded with built-ins.
308 //
309 }
310
311 ~TSymbolTable()
312 {
Nicolas Capense2858652015-02-18 15:30:51 -0500313 while(currentLevel() > LAST_BUILTIN_LEVEL)
314 {
315 pop();
316 }
John Bauman66b8ab22014-05-06 15:57:45 -0400317 }
318
Nicolas Capense2858652015-02-18 15:30:51 -0500319 bool isEmpty() { return table.empty(); }
320 bool atBuiltInLevel() { return currentLevel() <= LAST_BUILTIN_LEVEL; }
321 bool atGlobalLevel() { return currentLevel() <= GLOBAL_LEVEL; }
John Bauman66b8ab22014-05-06 15:57:45 -0400322 void push()
323 {
324 table.push_back(new TSymbolTableLevel);
325 precisionStack.push_back( PrecisionStackLevel() );
326 }
327
328 void pop()
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500329 {
330 delete table[currentLevel()];
331 table.pop_back();
John Bauman66b8ab22014-05-06 15:57:45 -0400332 precisionStack.pop_back();
333 }
334
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500335 bool declare(TSymbol &symbol)
John Bauman66b8ab22014-05-06 15:57:45 -0400336 {
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500337 return insert(currentLevel(), symbol);
338 }
339
Nicolas Capense2858652015-02-18 15:30:51 -0500340 bool insert(ESymbolLevel level, TSymbol &symbol)
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500341 {
342 return table[level]->insert(symbol);
John Bauman66b8ab22014-05-06 15:57:45 -0400343 }
344
Nicolas Capense2858652015-02-18 15:30:51 -0500345 bool insertConstInt(ESymbolLevel level, const char *name, int value)
John Baumand4ae8632014-05-06 16:18:33 -0400346 {
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500347 TVariable *constant = new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConstExpr, 1));
John Baumand4ae8632014-05-06 16:18:33 -0400348 constant->getConstPointer()->setIConst(value);
Nicolas Capense2858652015-02-18 15:30:51 -0500349 return insert(level, *constant);
John Baumand4ae8632014-05-06 16:18:33 -0400350 }
351
Alexis Hetub5332c52015-06-04 13:04:17 -0400352 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 -0400353 {
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500354 if(ptype1->getBasicType() == EbtGSampler2D)
355 {
356 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Alexis Hetub5332c52015-06-04 13:04:17 -0400357 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5);
358 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5);
359 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500360 }
361 else if(ptype1->getBasicType() == EbtGSampler3D)
362 {
363 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Alexis Hetub5332c52015-06-04 13:04:17 -0400364 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler3D), ptype2, ptype3, ptype4, ptype5);
365 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5);
366 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500367 }
368 else if(ptype1->getBasicType() == EbtGSamplerCube)
369 {
370 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Alexis Hetub5332c52015-06-04 13:04:17 -0400371 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5);
372 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5);
373 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500374 }
375 else if(ptype1->getBasicType() == EbtGSampler2DArray)
376 {
377 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Alexis Hetub5332c52015-06-04 13:04:17 -0400378 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5);
379 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5);
380 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500381 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500382 else if(IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3))
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500383 {
Nicolas Capens33cda112015-02-24 17:35:47 -0500384 ASSERT(!ptype4);
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500385 insertBuiltIn(level, op, ext, GenType(rvalue, 1), name, GenType(ptype1, 1), GenType(ptype2, 1), GenType(ptype3, 1));
386 insertBuiltIn(level, op, ext, GenType(rvalue, 2), name, GenType(ptype1, 2), GenType(ptype2, 2), GenType(ptype3, 2));
387 insertBuiltIn(level, op, ext, GenType(rvalue, 3), name, GenType(ptype1, 3), GenType(ptype2, 3), GenType(ptype3, 3));
388 insertBuiltIn(level, op, ext, GenType(rvalue, 4), name, GenType(ptype1, 4), GenType(ptype2, 4), GenType(ptype3, 4));
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500389 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500390 else if(IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500391 {
Nicolas Capens33cda112015-02-24 17:35:47 -0500392 ASSERT(!ptype4);
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500393 insertBuiltIn(level, op, ext, VecType(rvalue, 2), name, VecType(ptype1, 2), VecType(ptype2, 2), VecType(ptype3, 2));
394 insertBuiltIn(level, op, ext, VecType(rvalue, 3), name, VecType(ptype1, 3), VecType(ptype2, 3), VecType(ptype3, 3));
395 insertBuiltIn(level, op, ext, VecType(rvalue, 4), name, VecType(ptype1, 4), VecType(ptype2, 4), VecType(ptype3, 4));
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500396 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500397 else
John Baumand4ae8632014-05-06 16:18:33 -0400398 {
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500399 TFunction *function = new TFunction(NewPoolTString(name), *rvalue, op, ext);
John Baumand4ae8632014-05-06 16:18:33 -0400400
Nicolas Capens33cda112015-02-24 17:35:47 -0500401 TParameter param1 = {0, ptype1};
402 function->addParameter(param1);
John Baumand4ae8632014-05-06 16:18:33 -0400403
Nicolas Capens33cda112015-02-24 17:35:47 -0500404 if(ptype2)
405 {
406 TParameter param2 = {0, ptype2};
407 function->addParameter(param2);
408 }
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500409
Nicolas Capens33cda112015-02-24 17:35:47 -0500410 if(ptype3)
411 {
412 TParameter param3 = {0, ptype3};
413 function->addParameter(param3);
414 }
415
416 if(ptype4)
417 {
418 TParameter param4 = {0, ptype4};
419 function->addParameter(param4);
420 }
421
Alexis Hetub5332c52015-06-04 13:04:17 -0400422 if(ptype5)
423 {
424 TParameter param5 = {0, ptype5};
425 function->addParameter(param5);
426 }
427
Nicolas Capens33cda112015-02-24 17:35:47 -0500428 insert(level, *function);
429 }
430 }
431
Alexis Hetub5332c52015-06-04 13:04:17 -0400432 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 -0500433 {
Alexis Hetub5332c52015-06-04 13:04:17 -0400434 insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500435 }
436
Alexis Hetub5332c52015-06-04 13:04:17 -0400437 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 -0500438 {
Alexis Hetub5332c52015-06-04 13:04:17 -0400439 insertBuiltIn(level, EOpNull, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
John Bauman66b8ab22014-05-06 15:57:45 -0400440 }
441
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500442 TSymbol *find(const TString &name, int shaderVersion, bool *builtIn = nullptr, bool *sameScope = nullptr) const;
Nicolas Capense2858652015-02-18 15:30:51 -0500443 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
John Bauman66b8ab22014-05-06 15:57:45 -0400444
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500445 TSymbolTableLevel *getOuterLevel() const
446 {
Nicolas Capense2858652015-02-18 15:30:51 -0500447 assert(currentLevel() >= 1);
John Bauman66b8ab22014-05-06 15:57:45 -0400448 return table[currentLevel() - 1];
449 }
450
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500451 bool setDefaultPrecision(const TPublicType &type, TPrecision prec)
452 {
John Baumand4ae8632014-05-06 16:18:33 -0400453 if (IsSampler(type.type))
454 return true; // Skip sampler types for the time being
455 if (type.type != EbtFloat && type.type != EbtInt)
456 return false; // Only set default precision for int/float
Alexis Hetub14178b2015-04-13 13:23:20 -0400457 if (type.primarySize > 1 || type.secondarySize > 1 || type.array)
John Baumand4ae8632014-05-06 16:18:33 -0400458 return false; // Not allowed to set for aggregate types
John Bauman66b8ab22014-05-06 15:57:45 -0400459 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
John Baumand4ae8632014-05-06 16:18:33 -0400460 precisionStack[indexOfLastElement][type.type] = prec; // Uses map operator [], overwrites the current value
461 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400462 }
463
464 // Searches down the precisionStack for a precision qualifier for the specified TBasicType
Nicolas Capens3c20f802015-02-17 17:17:20 -0500465 TPrecision getDefaultPrecision( TBasicType type)
466 {
467 // unsigned integers use the same precision as signed
468 if (type == EbtUInt) type = EbtInt;
469
John Bauman66b8ab22014-05-06 15:57:45 -0400470 if( type != EbtFloat && type != EbtInt ) return EbpUndefined;
471 int level = static_cast<int>(precisionStack.size()) - 1;
472 assert( level >= 0); // Just to be safe. Should not happen.
473 PrecisionStackLevel::iterator it;
474 TPrecision prec = EbpUndefined; // If we dont find anything we return this. Should we error check this?
475 while( level >= 0 ){
476 it = precisionStack[level].find( type );
477 if( it != precisionStack[level].end() ){
478 prec = (*it).second;
479 break;
480 }
481 level--;
482 }
483 return prec;
484 }
485
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500486protected:
Nicolas Capense2858652015-02-18 15:30:51 -0500487 ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
John Bauman66b8ab22014-05-06 15:57:45 -0400488
489 std::vector<TSymbolTableLevel*> table;
490 typedef std::map< TBasicType, TPrecision > PrecisionStackLevel;
491 std::vector< PrecisionStackLevel > precisionStack;
John Bauman66b8ab22014-05-06 15:57:45 -0400492};
493
494#endif // _SYMBOL_TABLE_INCLUDED_