blob: 53b926f5e7c0a5fe8fd658cab8885c35151d6c96 [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),
Alexis Hetu407813b2016-02-24 16:46:13 -0500137 defined(false),
138 prototypeDeclaration(false) { }
139 TFunction(const TString *name, const TType& retType, TOperator tOp = EOpNull, const char *ext = "") :
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500140 TSymbol(name),
John Bauman66b8ab22014-05-06 15:57:45 -0400141 returnType(retType),
142 mangledName(TFunction::mangleName(*name)),
143 op(tOp),
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500144 extension(ext),
Alexis Hetu407813b2016-02-24 16:46:13 -0500145 defined(false),
146 prototypeDeclaration(false) { }
John Bauman66b8ab22014-05-06 15:57:45 -0400147 virtual ~TFunction();
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500148 virtual bool isFunction() const { return true; }
John Bauman66b8ab22014-05-06 15:57:45 -0400149
150 static TString mangleName(const TString& name) { return name + '('; }
151 static TString unmangleName(const TString& mangledName)
152 {
153 return TString(mangledName.c_str(), mangledName.find_first_of('('));
154 }
155
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500156 void addParameter(TParameter& p)
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500157 {
John Bauman66b8ab22014-05-06 15:57:45 -0400158 parameters.push_back(p);
159 mangledName = mangledName + p.type->getMangledName();
160 }
161
162 const TString& getMangledName() const { return mangledName; }
163 const TType& getReturnType() const { return returnType; }
164
John Bauman66b8ab22014-05-06 15:57:45 -0400165 TOperator getBuiltInOp() const { return op; }
John Bauman66b8ab22014-05-06 15:57:45 -0400166 const TString& getExtension() const { return extension; }
167
168 void setDefined() { defined = true; }
169 bool isDefined() { return defined; }
Alexis Hetu407813b2016-02-24 16:46:13 -0500170 void setHasPrototypeDeclaration() { prototypeDeclaration = true; }
171 bool hasPrototypeDeclaration() const { return prototypeDeclaration; }
John Bauman66b8ab22014-05-06 15:57:45 -0400172
Alexis Hetua818c452015-06-11 13:06:58 -0400173 size_t getParamCount() const { return parameters.size(); }
John Bauman66b8ab22014-05-06 15:57:45 -0400174 const TParameter& getParam(int i) const { return parameters[i]; }
175
John Bauman66b8ab22014-05-06 15:57:45 -0400176protected:
177 typedef TVector<TParameter> TParamList;
178 TParamList parameters;
179 TType returnType;
180 TString mangledName;
181 TOperator op;
182 TString extension;
183 bool defined;
Alexis Hetu407813b2016-02-24 16:46:13 -0500184 bool prototypeDeclaration;
John Bauman66b8ab22014-05-06 15:57:45 -0400185};
186
187
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500188class TSymbolTableLevel
189{
John Bauman66b8ab22014-05-06 15:57:45 -0400190public:
191 typedef TMap<TString, TSymbol*> tLevel;
192 typedef tLevel::const_iterator const_iterator;
193 typedef const tLevel::value_type tLevelPair;
194 typedef std::pair<tLevel::iterator, bool> tInsertResult;
195
Nicolas Capens978ddc52014-11-11 12:42:08 -0500196 POOL_ALLOCATOR_NEW_DELETE();
John Bauman66b8ab22014-05-06 15:57:45 -0400197 TSymbolTableLevel() { }
198 ~TSymbolTableLevel();
199
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500200 bool insert(TSymbol &symbol)
John Bauman66b8ab22014-05-06 15:57:45 -0400201 {
Alexis Hetuad6b8752015-06-09 16:15:30 -0400202 symbol.setUniqueId(nextUniqueId());
John Baumand4ae8632014-05-06 16:18:33 -0400203
John Bauman66b8ab22014-05-06 15:57:45 -0400204 //
205 // returning true means symbol was added to the table
206 //
207 tInsertResult result;
208 result = level.insert(tLevelPair(symbol.getMangledName(), &symbol));
209
210 return result.second;
211 }
212
213 TSymbol* find(const TString& name) const
214 {
215 tLevel::const_iterator it = level.find(name);
216 if (it == level.end())
217 return 0;
218 else
219 return (*it).second;
220 }
221
Alexis Hetuad6b8752015-06-09 16:15:30 -0400222 static int nextUniqueId()
223 {
224 return ++uniqueId;
225 }
226
John Bauman66b8ab22014-05-06 15:57:45 -0400227protected:
228 tLevel level;
John Baumand4ae8632014-05-06 16:18:33 -0400229 static int uniqueId; // for unique identification in code generation
John Bauman66b8ab22014-05-06 15:57:45 -0400230};
231
Nicolas Capense2858652015-02-18 15:30:51 -0500232enum ESymbolLevel
233{
Nicolas Capens3d7f6ed2015-02-18 16:34:50 -0500234 COMMON_BUILTINS,
235 ESSL1_BUILTINS,
236 ESSL3_BUILTINS,
237 LAST_BUILTIN_LEVEL = ESSL3_BUILTINS,
238 GLOBAL_LEVEL
Nicolas Capense2858652015-02-18 15:30:51 -0500239};
240
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500241inline bool IsGenType(const TType *type)
242{
243 if(type)
244 {
245 TBasicType basicType = type->getBasicType();
246 return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType || basicType == EbtGenBType;
247 }
248
249 return false;
250}
251
252inline bool IsVecType(const TType *type)
253{
254 if(type)
255 {
256 TBasicType basicType = type->getBasicType();
257 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec || basicType == EbtBVec;
258 }
259
260 return false;
261}
262
263inline TType *GenType(TType *type, int size)
264{
265 ASSERT(size >= 1 && size <= 4);
266
267 if(!type)
268 {
269 return nullptr;
270 }
271
272 ASSERT(!IsVecType(type));
273
274 switch(type->getBasicType())
275 {
276 case EbtGenType: return new TType(EbtFloat, size);
277 case EbtGenIType: return new TType(EbtInt, size);
278 case EbtGenUType: return new TType(EbtUInt, size);
279 case EbtGenBType: return new TType(EbtBool, size);
280 default: return type;
281 }
282}
283
284inline TType *VecType(TType *type, int size)
285{
286 ASSERT(size >= 2 && size <= 4);
287
288 if(!type)
289 {
290 return nullptr;
291 }
292
293 ASSERT(!IsGenType(type));
294
295 switch(type->getBasicType())
296 {
297 case EbtVec: return new TType(EbtFloat, size);
298 case EbtIVec: return new TType(EbtInt, size);
299 case EbtUVec: return new TType(EbtUInt, size);
300 case EbtBVec: return new TType(EbtBool, size);
301 default: return type;
302 }
303}
304
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500305class TSymbolTable
306{
John Bauman66b8ab22014-05-06 15:57:45 -0400307public:
John Baumand4ae8632014-05-06 16:18:33 -0400308 TSymbolTable()
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400309 : mGlobalInvariant(false)
John Bauman66b8ab22014-05-06 15:57:45 -0400310 {
311 //
312 // The symbol table cannot be used until push() is called, but
313 // the lack of an initial call to push() can be used to detect
314 // that the symbol table has not been preloaded with built-ins.
315 //
316 }
317
318 ~TSymbolTable()
319 {
Nicolas Capense2858652015-02-18 15:30:51 -0500320 while(currentLevel() > LAST_BUILTIN_LEVEL)
321 {
322 pop();
323 }
John Bauman66b8ab22014-05-06 15:57:45 -0400324 }
325
Nicolas Capense2858652015-02-18 15:30:51 -0500326 bool isEmpty() { return table.empty(); }
327 bool atBuiltInLevel() { return currentLevel() <= LAST_BUILTIN_LEVEL; }
328 bool atGlobalLevel() { return currentLevel() <= GLOBAL_LEVEL; }
John Bauman66b8ab22014-05-06 15:57:45 -0400329 void push()
330 {
331 table.push_back(new TSymbolTableLevel);
332 precisionStack.push_back( PrecisionStackLevel() );
333 }
334
335 void pop()
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500336 {
337 delete table[currentLevel()];
338 table.pop_back();
John Bauman66b8ab22014-05-06 15:57:45 -0400339 precisionStack.pop_back();
340 }
341
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500342 bool declare(TSymbol &symbol)
John Bauman66b8ab22014-05-06 15:57:45 -0400343 {
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500344 return insert(currentLevel(), symbol);
345 }
346
Nicolas Capense2858652015-02-18 15:30:51 -0500347 bool insert(ESymbolLevel level, TSymbol &symbol)
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500348 {
349 return table[level]->insert(symbol);
John Bauman66b8ab22014-05-06 15:57:45 -0400350 }
351
Nicolas Capense2858652015-02-18 15:30:51 -0500352 bool insertConstInt(ESymbolLevel level, const char *name, int value)
John Baumand4ae8632014-05-06 16:18:33 -0400353 {
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500354 TVariable *constant = new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConstExpr, 1));
John Baumand4ae8632014-05-06 16:18:33 -0400355 constant->getConstPointer()->setIConst(value);
Nicolas Capense2858652015-02-18 15:30:51 -0500356 return insert(level, *constant);
John Baumand4ae8632014-05-06 16:18:33 -0400357 }
358
Alexis Hetub5332c52015-06-04 13:04:17 -0400359 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 -0400360 {
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500361 if(ptype1->getBasicType() == EbtGSampler2D)
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(EbtSampler2D), ptype2, ptype3, ptype4, ptype5);
365 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5);
366 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500367 }
368 else if(ptype1->getBasicType() == EbtGSampler3D)
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(EbtSampler3D), ptype2, ptype3, ptype4, ptype5);
372 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5);
373 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500374 }
375 else if(ptype1->getBasicType() == EbtGSamplerCube)
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(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5);
379 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5);
380 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500381 }
382 else if(ptype1->getBasicType() == EbtGSampler2DArray)
383 {
384 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Alexis Hetub5332c52015-06-04 13:04:17 -0400385 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5);
386 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5);
387 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500388 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500389 else if(IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3))
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500390 {
Nicolas Capens33cda112015-02-24 17:35:47 -0500391 ASSERT(!ptype4);
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500392 insertBuiltIn(level, op, ext, GenType(rvalue, 1), name, GenType(ptype1, 1), GenType(ptype2, 1), GenType(ptype3, 1));
393 insertBuiltIn(level, op, ext, GenType(rvalue, 2), name, GenType(ptype1, 2), GenType(ptype2, 2), GenType(ptype3, 2));
394 insertBuiltIn(level, op, ext, GenType(rvalue, 3), name, GenType(ptype1, 3), GenType(ptype2, 3), GenType(ptype3, 3));
395 insertBuiltIn(level, op, ext, GenType(rvalue, 4), name, GenType(ptype1, 4), GenType(ptype2, 4), GenType(ptype3, 4));
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500396 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500397 else if(IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500398 {
Nicolas Capens33cda112015-02-24 17:35:47 -0500399 ASSERT(!ptype4);
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500400 insertBuiltIn(level, op, ext, VecType(rvalue, 2), name, VecType(ptype1, 2), VecType(ptype2, 2), VecType(ptype3, 2));
401 insertBuiltIn(level, op, ext, VecType(rvalue, 3), name, VecType(ptype1, 3), VecType(ptype2, 3), VecType(ptype3, 3));
402 insertBuiltIn(level, op, ext, VecType(rvalue, 4), name, VecType(ptype1, 4), VecType(ptype2, 4), VecType(ptype3, 4));
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500403 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500404 else
John Baumand4ae8632014-05-06 16:18:33 -0400405 {
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500406 TFunction *function = new TFunction(NewPoolTString(name), *rvalue, op, ext);
John Baumand4ae8632014-05-06 16:18:33 -0400407
Nicolas Capens33cda112015-02-24 17:35:47 -0500408 TParameter param1 = {0, ptype1};
409 function->addParameter(param1);
John Baumand4ae8632014-05-06 16:18:33 -0400410
Nicolas Capens33cda112015-02-24 17:35:47 -0500411 if(ptype2)
412 {
413 TParameter param2 = {0, ptype2};
414 function->addParameter(param2);
415 }
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500416
Nicolas Capens33cda112015-02-24 17:35:47 -0500417 if(ptype3)
418 {
419 TParameter param3 = {0, ptype3};
420 function->addParameter(param3);
421 }
422
423 if(ptype4)
424 {
425 TParameter param4 = {0, ptype4};
426 function->addParameter(param4);
427 }
428
Alexis Hetub5332c52015-06-04 13:04:17 -0400429 if(ptype5)
430 {
431 TParameter param5 = {0, ptype5};
432 function->addParameter(param5);
433 }
434
Nicolas Capens33cda112015-02-24 17:35:47 -0500435 insert(level, *function);
436 }
437 }
438
Alexis Hetub5332c52015-06-04 13:04:17 -0400439 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 -0500440 {
Alexis Hetub5332c52015-06-04 13:04:17 -0400441 insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500442 }
443
Alexis Hetub5332c52015-06-04 13:04:17 -0400444 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 -0500445 {
Alexis Hetub5332c52015-06-04 13:04:17 -0400446 insertBuiltIn(level, EOpNull, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
John Bauman66b8ab22014-05-06 15:57:45 -0400447 }
448
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500449 TSymbol *find(const TString &name, int shaderVersion, bool *builtIn = nullptr, bool *sameScope = nullptr) const;
Nicolas Capense2858652015-02-18 15:30:51 -0500450 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
John Bauman66b8ab22014-05-06 15:57:45 -0400451
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500452 TSymbolTableLevel *getOuterLevel() const
453 {
Nicolas Capense2858652015-02-18 15:30:51 -0500454 assert(currentLevel() >= 1);
John Bauman66b8ab22014-05-06 15:57:45 -0400455 return table[currentLevel() - 1];
456 }
457
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500458 bool setDefaultPrecision(const TPublicType &type, TPrecision prec)
459 {
John Baumand4ae8632014-05-06 16:18:33 -0400460 if (IsSampler(type.type))
461 return true; // Skip sampler types for the time being
462 if (type.type != EbtFloat && type.type != EbtInt)
463 return false; // Only set default precision for int/float
Alexis Hetub14178b2015-04-13 13:23:20 -0400464 if (type.primarySize > 1 || type.secondarySize > 1 || type.array)
John Baumand4ae8632014-05-06 16:18:33 -0400465 return false; // Not allowed to set for aggregate types
John Bauman66b8ab22014-05-06 15:57:45 -0400466 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
John Baumand4ae8632014-05-06 16:18:33 -0400467 precisionStack[indexOfLastElement][type.type] = prec; // Uses map operator [], overwrites the current value
468 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400469 }
470
471 // Searches down the precisionStack for a precision qualifier for the specified TBasicType
Nicolas Capens3c20f802015-02-17 17:17:20 -0500472 TPrecision getDefaultPrecision( TBasicType type)
473 {
474 // unsigned integers use the same precision as signed
475 if (type == EbtUInt) type = EbtInt;
476
John Bauman66b8ab22014-05-06 15:57:45 -0400477 if( type != EbtFloat && type != EbtInt ) return EbpUndefined;
478 int level = static_cast<int>(precisionStack.size()) - 1;
479 assert( level >= 0); // Just to be safe. Should not happen.
480 PrecisionStackLevel::iterator it;
481 TPrecision prec = EbpUndefined; // If we dont find anything we return this. Should we error check this?
482 while( level >= 0 ){
483 it = precisionStack[level].find( type );
484 if( it != precisionStack[level].end() ){
485 prec = (*it).second;
486 break;
487 }
488 level--;
489 }
490 return prec;
491 }
492
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400493 // This records invariant varyings declared through
494 // "invariant varying_name;".
495 void addInvariantVarying(const std::string &originalName)
496 {
497 mInvariantVaryings.insert(originalName);
498 }
499 // If this returns false, the varying could still be invariant
500 // if it is set as invariant during the varying variable
501 // declaration - this piece of information is stored in the
502 // variable's type, not here.
503 bool isVaryingInvariant(const std::string &originalName) const
504 {
505 return (mGlobalInvariant ||
506 mInvariantVaryings.count(originalName) > 0);
507 }
508
509 void setGlobalInvariant() { mGlobalInvariant = true; }
510 bool getGlobalInvariant() const { return mGlobalInvariant; }
511
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500512protected:
Nicolas Capense2858652015-02-18 15:30:51 -0500513 ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
John Bauman66b8ab22014-05-06 15:57:45 -0400514
515 std::vector<TSymbolTableLevel*> table;
516 typedef std::map< TBasicType, TPrecision > PrecisionStackLevel;
517 std::vector< PrecisionStackLevel > precisionStack;
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400518
519 std::set<std::string> mInvariantVaryings;
520 bool mGlobalInvariant;
John Bauman66b8ab22014-05-06 15:57:45 -0400521};
522
523#endif // _SYMBOL_TABLE_INCLUDED_