blob: 153e8145c417df6085fb9a5f2bc3b61b5cc2f536 [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; }
Nicolas Capens0863f0d2016-04-10 00:30:02 -040098 bool isConstant() const { return unionArray != nullptr; }
John Bauman66b8ab22014-05-06 15:57:45 -040099
100 void shareConstPointer( ConstantUnion *constArray)
101 {
102 if (unionArray == constArray)
103 return;
104
105 delete[] unionArray;
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500106 unionArray = constArray;
John Bauman66b8ab22014-05-06 15:57:45 -0400107 }
John Bauman66b8ab22014-05-06 15:57:45 -0400108
109protected:
110 TType type;
111 bool userType;
112 // we are assuming that Pool Allocator will free the memory allocated to unionArray
113 // when this object is destroyed
114 ConstantUnion *unionArray;
115 TType *arrayInformationType; // this is used for updating maxArraySize in all the references to a given symbol
116};
117
118//
119// The function sub-class of symbols and the parser will need to
120// share this definition of a function parameter.
121//
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500122struct TParameter
123{
John Bauman66b8ab22014-05-06 15:57:45 -0400124 TString *name;
John Baumand4ae8632014-05-06 16:18:33 -0400125 TType *type;
John Bauman66b8ab22014-05-06 15:57:45 -0400126};
127
128//
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500129// The function sub-class of a symbol.
John Bauman66b8ab22014-05-06 15:57:45 -0400130//
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500131class TFunction : public TSymbol
132{
John Bauman66b8ab22014-05-06 15:57:45 -0400133public:
134 TFunction(TOperator o) :
135 TSymbol(0),
136 returnType(TType(EbtVoid, EbpUndefined)),
137 op(o),
Alexis Hetu407813b2016-02-24 16:46:13 -0500138 defined(false),
139 prototypeDeclaration(false) { }
140 TFunction(const TString *name, const TType& retType, TOperator tOp = EOpNull, const char *ext = "") :
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500141 TSymbol(name),
John Bauman66b8ab22014-05-06 15:57:45 -0400142 returnType(retType),
143 mangledName(TFunction::mangleName(*name)),
144 op(tOp),
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500145 extension(ext),
Alexis Hetu407813b2016-02-24 16:46:13 -0500146 defined(false),
147 prototypeDeclaration(false) { }
John Bauman66b8ab22014-05-06 15:57:45 -0400148 virtual ~TFunction();
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500149 virtual bool isFunction() const { return true; }
John Bauman66b8ab22014-05-06 15:57:45 -0400150
151 static TString mangleName(const TString& name) { return name + '('; }
152 static TString unmangleName(const TString& mangledName)
153 {
154 return TString(mangledName.c_str(), mangledName.find_first_of('('));
155 }
156
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500157 void addParameter(TParameter& p)
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500158 {
John Bauman66b8ab22014-05-06 15:57:45 -0400159 parameters.push_back(p);
160 mangledName = mangledName + p.type->getMangledName();
161 }
162
163 const TString& getMangledName() const { return mangledName; }
164 const TType& getReturnType() const { return returnType; }
165
John Bauman66b8ab22014-05-06 15:57:45 -0400166 TOperator getBuiltInOp() const { return op; }
John Bauman66b8ab22014-05-06 15:57:45 -0400167 const TString& getExtension() const { return extension; }
168
169 void setDefined() { defined = true; }
170 bool isDefined() { return defined; }
Alexis Hetu407813b2016-02-24 16:46:13 -0500171 void setHasPrototypeDeclaration() { prototypeDeclaration = true; }
172 bool hasPrototypeDeclaration() const { return prototypeDeclaration; }
John Bauman66b8ab22014-05-06 15:57:45 -0400173
Alexis Hetua818c452015-06-11 13:06:58 -0400174 size_t getParamCount() const { return parameters.size(); }
John Bauman66b8ab22014-05-06 15:57:45 -0400175 const TParameter& getParam(int i) const { return parameters[i]; }
176
John Bauman66b8ab22014-05-06 15:57:45 -0400177protected:
178 typedef TVector<TParameter> TParamList;
179 TParamList parameters;
180 TType returnType;
181 TString mangledName;
182 TOperator op;
183 TString extension;
184 bool defined;
Alexis Hetu407813b2016-02-24 16:46:13 -0500185 bool prototypeDeclaration;
John Bauman66b8ab22014-05-06 15:57:45 -0400186};
187
188
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500189class TSymbolTableLevel
190{
John Bauman66b8ab22014-05-06 15:57:45 -0400191public:
192 typedef TMap<TString, TSymbol*> tLevel;
193 typedef tLevel::const_iterator const_iterator;
194 typedef const tLevel::value_type tLevelPair;
195 typedef std::pair<tLevel::iterator, bool> tInsertResult;
196
Nicolas Capens978ddc52014-11-11 12:42:08 -0500197 POOL_ALLOCATOR_NEW_DELETE();
John Bauman66b8ab22014-05-06 15:57:45 -0400198 TSymbolTableLevel() { }
199 ~TSymbolTableLevel();
200
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500201 bool insert(TSymbol &symbol)
John Bauman66b8ab22014-05-06 15:57:45 -0400202 {
Alexis Hetuad6b8752015-06-09 16:15:30 -0400203 symbol.setUniqueId(nextUniqueId());
John Baumand4ae8632014-05-06 16:18:33 -0400204
John Bauman66b8ab22014-05-06 15:57:45 -0400205 //
206 // returning true means symbol was added to the table
207 //
208 tInsertResult result;
209 result = level.insert(tLevelPair(symbol.getMangledName(), &symbol));
210
211 return result.second;
212 }
213
214 TSymbol* find(const TString& name) const
215 {
216 tLevel::const_iterator it = level.find(name);
217 if (it == level.end())
218 return 0;
219 else
220 return (*it).second;
221 }
222
Alexis Hetuad6b8752015-06-09 16:15:30 -0400223 static int nextUniqueId()
224 {
225 return ++uniqueId;
226 }
227
John Bauman66b8ab22014-05-06 15:57:45 -0400228protected:
229 tLevel level;
John Baumand4ae8632014-05-06 16:18:33 -0400230 static int uniqueId; // for unique identification in code generation
John Bauman66b8ab22014-05-06 15:57:45 -0400231};
232
Nicolas Capense2858652015-02-18 15:30:51 -0500233enum ESymbolLevel
234{
Nicolas Capens3d7f6ed2015-02-18 16:34:50 -0500235 COMMON_BUILTINS,
236 ESSL1_BUILTINS,
237 ESSL3_BUILTINS,
238 LAST_BUILTIN_LEVEL = ESSL3_BUILTINS,
239 GLOBAL_LEVEL
Nicolas Capense2858652015-02-18 15:30:51 -0500240};
241
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500242inline bool IsGenType(const TType *type)
243{
244 if(type)
245 {
246 TBasicType basicType = type->getBasicType();
247 return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType || basicType == EbtGenBType;
248 }
249
250 return false;
251}
252
253inline bool IsVecType(const TType *type)
254{
255 if(type)
256 {
257 TBasicType basicType = type->getBasicType();
258 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec || basicType == EbtBVec;
259 }
260
261 return false;
262}
263
264inline TType *GenType(TType *type, int size)
265{
266 ASSERT(size >= 1 && size <= 4);
267
268 if(!type)
269 {
270 return nullptr;
271 }
272
273 ASSERT(!IsVecType(type));
274
275 switch(type->getBasicType())
276 {
277 case EbtGenType: return new TType(EbtFloat, size);
278 case EbtGenIType: return new TType(EbtInt, size);
279 case EbtGenUType: return new TType(EbtUInt, size);
280 case EbtGenBType: return new TType(EbtBool, size);
281 default: return type;
282 }
283}
284
285inline TType *VecType(TType *type, int size)
286{
287 ASSERT(size >= 2 && size <= 4);
288
289 if(!type)
290 {
291 return nullptr;
292 }
293
294 ASSERT(!IsGenType(type));
295
296 switch(type->getBasicType())
297 {
298 case EbtVec: return new TType(EbtFloat, size);
299 case EbtIVec: return new TType(EbtInt, size);
300 case EbtUVec: return new TType(EbtUInt, size);
301 case EbtBVec: return new TType(EbtBool, size);
302 default: return type;
303 }
304}
305
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500306class TSymbolTable
307{
John Bauman66b8ab22014-05-06 15:57:45 -0400308public:
John Baumand4ae8632014-05-06 16:18:33 -0400309 TSymbolTable()
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400310 : mGlobalInvariant(false)
John Bauman66b8ab22014-05-06 15:57:45 -0400311 {
312 //
313 // The symbol table cannot be used until push() is called, but
314 // the lack of an initial call to push() can be used to detect
315 // that the symbol table has not been preloaded with built-ins.
316 //
317 }
318
319 ~TSymbolTable()
320 {
Nicolas Capense2858652015-02-18 15:30:51 -0500321 while(currentLevel() > LAST_BUILTIN_LEVEL)
322 {
323 pop();
324 }
John Bauman66b8ab22014-05-06 15:57:45 -0400325 }
326
Nicolas Capense2858652015-02-18 15:30:51 -0500327 bool isEmpty() { return table.empty(); }
328 bool atBuiltInLevel() { return currentLevel() <= LAST_BUILTIN_LEVEL; }
329 bool atGlobalLevel() { return currentLevel() <= GLOBAL_LEVEL; }
John Bauman66b8ab22014-05-06 15:57:45 -0400330 void push()
331 {
332 table.push_back(new TSymbolTableLevel);
333 precisionStack.push_back( PrecisionStackLevel() );
334 }
335
336 void pop()
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500337 {
338 delete table[currentLevel()];
339 table.pop_back();
John Bauman66b8ab22014-05-06 15:57:45 -0400340 precisionStack.pop_back();
341 }
342
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500343 bool declare(TSymbol &symbol)
John Bauman66b8ab22014-05-06 15:57:45 -0400344 {
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500345 return insert(currentLevel(), symbol);
346 }
347
Nicolas Capense2858652015-02-18 15:30:51 -0500348 bool insert(ESymbolLevel level, TSymbol &symbol)
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500349 {
350 return table[level]->insert(symbol);
John Bauman66b8ab22014-05-06 15:57:45 -0400351 }
352
Nicolas Capense2858652015-02-18 15:30:51 -0500353 bool insertConstInt(ESymbolLevel level, const char *name, int value)
John Baumand4ae8632014-05-06 16:18:33 -0400354 {
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500355 TVariable *constant = new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConstExpr, 1));
John Baumand4ae8632014-05-06 16:18:33 -0400356 constant->getConstPointer()->setIConst(value);
Nicolas Capense2858652015-02-18 15:30:51 -0500357 return insert(level, *constant);
John Baumand4ae8632014-05-06 16:18:33 -0400358 }
359
Alexis Hetub5332c52015-06-04 13:04:17 -0400360 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 -0400361 {
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500362 if(ptype1->getBasicType() == EbtGSampler2D)
363 {
364 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Alexis Hetub5332c52015-06-04 13:04:17 -0400365 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5);
366 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5);
367 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500368 }
369 else if(ptype1->getBasicType() == EbtGSampler3D)
370 {
371 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Alexis Hetub5332c52015-06-04 13:04:17 -0400372 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler3D), ptype2, ptype3, ptype4, ptype5);
373 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5);
374 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500375 }
376 else if(ptype1->getBasicType() == EbtGSamplerCube)
377 {
378 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Alexis Hetub5332c52015-06-04 13:04:17 -0400379 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5);
380 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5);
381 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500382 }
383 else if(ptype1->getBasicType() == EbtGSampler2DArray)
384 {
385 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Alexis Hetub5332c52015-06-04 13:04:17 -0400386 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5);
387 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5);
388 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5);
Nicolas Capensf8a4cba2015-02-18 17:30:46 -0500389 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500390 else if(IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(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, GenType(rvalue, 1), name, GenType(ptype1, 1), GenType(ptype2, 1), GenType(ptype3, 1));
394 insertBuiltIn(level, op, ext, GenType(rvalue, 2), name, GenType(ptype1, 2), GenType(ptype2, 2), GenType(ptype3, 2));
395 insertBuiltIn(level, op, ext, GenType(rvalue, 3), name, GenType(ptype1, 3), GenType(ptype2, 3), GenType(ptype3, 3));
396 insertBuiltIn(level, op, ext, GenType(rvalue, 4), name, GenType(ptype1, 4), GenType(ptype2, 4), GenType(ptype3, 4));
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500397 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500398 else if(IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500399 {
Nicolas Capens33cda112015-02-24 17:35:47 -0500400 ASSERT(!ptype4);
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500401 insertBuiltIn(level, op, ext, VecType(rvalue, 2), name, VecType(ptype1, 2), VecType(ptype2, 2), VecType(ptype3, 2));
402 insertBuiltIn(level, op, ext, VecType(rvalue, 3), name, VecType(ptype1, 3), VecType(ptype2, 3), VecType(ptype3, 3));
403 insertBuiltIn(level, op, ext, VecType(rvalue, 4), name, VecType(ptype1, 4), VecType(ptype2, 4), VecType(ptype3, 4));
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500404 }
Nicolas Capens33cda112015-02-24 17:35:47 -0500405 else
John Baumand4ae8632014-05-06 16:18:33 -0400406 {
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500407 TFunction *function = new TFunction(NewPoolTString(name), *rvalue, op, ext);
John Baumand4ae8632014-05-06 16:18:33 -0400408
Nicolas Capens33cda112015-02-24 17:35:47 -0500409 TParameter param1 = {0, ptype1};
410 function->addParameter(param1);
John Baumand4ae8632014-05-06 16:18:33 -0400411
Nicolas Capens33cda112015-02-24 17:35:47 -0500412 if(ptype2)
413 {
414 TParameter param2 = {0, ptype2};
415 function->addParameter(param2);
416 }
Nicolas Capens8fa9d232015-02-19 15:53:16 -0500417
Nicolas Capens33cda112015-02-24 17:35:47 -0500418 if(ptype3)
419 {
420 TParameter param3 = {0, ptype3};
421 function->addParameter(param3);
422 }
423
424 if(ptype4)
425 {
426 TParameter param4 = {0, ptype4};
427 function->addParameter(param4);
428 }
429
Alexis Hetub5332c52015-06-04 13:04:17 -0400430 if(ptype5)
431 {
432 TParameter param5 = {0, ptype5};
433 function->addParameter(param5);
434 }
435
Nicolas Capens33cda112015-02-24 17:35:47 -0500436 insert(level, *function);
437 }
438 }
439
Alexis Hetub5332c52015-06-04 13:04:17 -0400440 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 -0500441 {
Alexis Hetub5332c52015-06-04 13:04:17 -0400442 insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
Nicolas Capens18d5ef92015-02-24 17:38:08 -0500443 }
444
Alexis Hetub5332c52015-06-04 13:04:17 -0400445 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 -0500446 {
Alexis Hetub5332c52015-06-04 13:04:17 -0400447 insertBuiltIn(level, EOpNull, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
John Bauman66b8ab22014-05-06 15:57:45 -0400448 }
449
Nicolas Capenscc1e9962015-03-02 11:17:58 -0500450 TSymbol *find(const TString &name, int shaderVersion, bool *builtIn = nullptr, bool *sameScope = nullptr) const;
Nicolas Capense2858652015-02-18 15:30:51 -0500451 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
John Bauman66b8ab22014-05-06 15:57:45 -0400452
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500453 TSymbolTableLevel *getOuterLevel() const
454 {
Nicolas Capense2858652015-02-18 15:30:51 -0500455 assert(currentLevel() >= 1);
John Bauman66b8ab22014-05-06 15:57:45 -0400456 return table[currentLevel() - 1];
457 }
458
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500459 bool setDefaultPrecision(const TPublicType &type, TPrecision prec)
460 {
John Baumand4ae8632014-05-06 16:18:33 -0400461 if (IsSampler(type.type))
462 return true; // Skip sampler types for the time being
463 if (type.type != EbtFloat && type.type != EbtInt)
464 return false; // Only set default precision for int/float
Alexis Hetub14178b2015-04-13 13:23:20 -0400465 if (type.primarySize > 1 || type.secondarySize > 1 || type.array)
John Baumand4ae8632014-05-06 16:18:33 -0400466 return false; // Not allowed to set for aggregate types
John Bauman66b8ab22014-05-06 15:57:45 -0400467 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
John Baumand4ae8632014-05-06 16:18:33 -0400468 precisionStack[indexOfLastElement][type.type] = prec; // Uses map operator [], overwrites the current value
469 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400470 }
471
472 // Searches down the precisionStack for a precision qualifier for the specified TBasicType
Nicolas Capens3c20f802015-02-17 17:17:20 -0500473 TPrecision getDefaultPrecision( TBasicType type)
474 {
475 // unsigned integers use the same precision as signed
476 if (type == EbtUInt) type = EbtInt;
477
John Bauman66b8ab22014-05-06 15:57:45 -0400478 if( type != EbtFloat && type != EbtInt ) return EbpUndefined;
479 int level = static_cast<int>(precisionStack.size()) - 1;
480 assert( level >= 0); // Just to be safe. Should not happen.
481 PrecisionStackLevel::iterator it;
482 TPrecision prec = EbpUndefined; // If we dont find anything we return this. Should we error check this?
483 while( level >= 0 ){
484 it = precisionStack[level].find( type );
485 if( it != precisionStack[level].end() ){
486 prec = (*it).second;
487 break;
488 }
489 level--;
490 }
491 return prec;
492 }
493
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400494 // This records invariant varyings declared through
495 // "invariant varying_name;".
496 void addInvariantVarying(const std::string &originalName)
497 {
498 mInvariantVaryings.insert(originalName);
499 }
500 // If this returns false, the varying could still be invariant
501 // if it is set as invariant during the varying variable
502 // declaration - this piece of information is stored in the
503 // variable's type, not here.
504 bool isVaryingInvariant(const std::string &originalName) const
505 {
506 return (mGlobalInvariant ||
507 mInvariantVaryings.count(originalName) > 0);
508 }
509
510 void setGlobalInvariant() { mGlobalInvariant = true; }
511 bool getGlobalInvariant() const { return mGlobalInvariant; }
512
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500513protected:
Nicolas Capense2858652015-02-18 15:30:51 -0500514 ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
John Bauman66b8ab22014-05-06 15:57:45 -0400515
516 std::vector<TSymbolTableLevel*> table;
517 typedef std::map< TBasicType, TPrecision > PrecisionStackLevel;
518 std::vector< PrecisionStackLevel > precisionStack;
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400519
520 std::set<std::string> mInvariantVaryings;
521 bool mGlobalInvariant;
John Bauman66b8ab22014-05-06 15:57:45 -0400522};
523
524#endif // _SYMBOL_TABLE_INCLUDED_