1//
2// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
3// 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 COMPILER_TRANSLATOR_SYMBOLTABLE_H_
8#define COMPILER_TRANSLATOR_SYMBOLTABLE_H_
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//
21// * Pushing and popping of scope, so symbol table will really be a stack
22// 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
33#include <assert.h>
34#include <set>
35
36#include "common/angleutils.h"
37#include "compiler/translator/InfoSink.h"
38#include "compiler/translator/IntermNode.h"
39
40// Symbol base class. (Can build functions or variables out of these...)
41class TSymbol : angle::NonCopyable
42{
43 public:
44 POOL_ALLOCATOR_NEW_DELETE();
45 TSymbol(const TString *n)
46 : uniqueId(0),
47 name(n)
48 {
49 }
50 virtual ~TSymbol()
51 {
52 // don't delete name, it's from the pool
53 }
54
55 const TString &getName() const
56 {
57 return *name;
58 }
59 virtual const TString &getMangledName() const
60 {
61 return getName();
62 }
63 virtual bool isFunction() const
64 {
65 return false;
66 }
67 virtual bool isVariable() const
68 {
69 return false;
70 }
71 void setUniqueId(int id)
72 {
73 uniqueId = id;
74 }
75 int getUniqueId() const
76 {
77 return uniqueId;
78 }
79 void relateToExtension(const TString &ext)
80 {
81 extension = ext;
82 }
83 const TString &getExtension() const
84 {
85 return extension;
86 }
87
88 private:
89 int uniqueId; // For real comparing during code generation
90 const TString *name;
91 TString extension;
92};
93
94// Variable class, meaning a symbol that's not a function.
95//
96// There could be a separate class heirarchy for Constant variables;
97// Only one of int, bool, or float, (or none) is correct for
98// any particular use, but it's easy to do this way, and doesn't
99// seem worth having separate classes, and "getConst" can't simply return
100// different values for different types polymorphically, so this is
101// just simple and pragmatic.
102class TVariable : public TSymbol
103{
104 public:
105 TVariable(const TString *name, const TType &t, bool uT = false)
106 : TSymbol(name),
107 type(t),
108 userType(uT),
109 unionArray(0)
110 {
111 }
112 virtual ~TVariable()
113 {
114 }
115 virtual bool isVariable() const
116 {
117 return true;
118 }
119 TType &getType()
120 {
121 return type;
122 }
123 const TType &getType() const
124 {
125 return type;
126 }
127 bool isUserType() const
128 {
129 return userType;
130 }
131 void setQualifier(TQualifier qualifier)
132 {
133 type.setQualifier(qualifier);
134 }
135
136 TConstantUnion *getConstPointer()
137 {
138 if (!unionArray)
139 unionArray = new TConstantUnion[type.getObjectSize()];
140
141 return unionArray;
142 }
143
144 TConstantUnion *getConstPointer() const
145 {
146 return unionArray;
147 }
148
149 void shareConstPointer(TConstantUnion *constArray)
150 {
151 if (unionArray == constArray)
152 return;
153
154 delete[] unionArray;
155 unionArray = constArray;
156 }
157
158 private:
159 TType type;
160 bool userType;
161 // we are assuming that Pool Allocator will free the memory
162 // allocated to unionArray when this object is destroyed.
163 TConstantUnion *unionArray;
164};
165
166// The function sub-class of symbols and the parser will need to
167// share this definition of a function parameter.
168struct TParameter
169{
170 TString *name;
171 TType *type;
172};
173
174// The function sub-class of a symbol.
175class TFunction : public TSymbol
176{
177 public:
178 TFunction(TOperator o)
179 : TSymbol(0),
180 returnType(TType(EbtVoid, EbpUndefined)),
181 op(o),
182 defined(false)
183 {
184 }
185 TFunction(const TString *name, const TType &retType, TOperator tOp = EOpNull, const char *ext = "")
186 : TSymbol(name),
187 returnType(retType),
188 mangledName(TFunction::mangleName(*name)),
189 op(tOp),
190 defined(false)
191 {
192 relateToExtension(ext);
193 }
194 virtual ~TFunction();
195 virtual bool isFunction() const
196 {
197 return true;
198 }
199
200 static TString mangleName(const TString &name)
201 {
202 return name + '(';
203 }
204 static TString unmangleName(const TString &mangledName)
205 {
206 return TString(mangledName.c_str(), mangledName.find_first_of('('));
207 }
208
209 void addParameter(TParameter &p)
210 {
211 parameters.push_back(p);
212 mangledName = mangledName + p.type->getMangledName();
213 }
214
215 const TString &getMangledName() const
216 {
217 return mangledName;
218 }
219 const TType &getReturnType() const
220 {
221 return returnType;
222 }
223
224 TOperator getBuiltInOp() const
225 {
226 return op;
227 }
228
229 void setDefined()
230 {
231 defined = true;
232 }
233 bool isDefined()
234 {
235 return defined;
236 }
237
238 size_t getParamCount() const
239 {
240 return parameters.size();
241 }
242 const TParameter &getParam(size_t i) const
243 {
244 return parameters[i];
245 }
246
247 private:
248 typedef TVector<TParameter> TParamList;
249 TParamList parameters;
250 TType returnType;
251 TString mangledName;
252 TOperator op;
253 bool defined;
254};
255
256// Interface block name sub-symbol
257class TInterfaceBlockName : public TSymbol
258{
259 public:
260 TInterfaceBlockName(const TString *name)
261 : TSymbol(name)
262 {
263 }
264
265 virtual ~TInterfaceBlockName()
266 {
267 }
268};
269
270class TSymbolTableLevel
271{
272 public:
273 typedef TMap<TString, TSymbol *> tLevel;
274 typedef tLevel::const_iterator const_iterator;
275 typedef const tLevel::value_type tLevelPair;
276 typedef std::pair<tLevel::iterator, bool> tInsertResult;
277
278 TSymbolTableLevel()
279 {
280 }
281 ~TSymbolTableLevel();
282
283 bool insert(TSymbol *symbol);
284
285 // Insert a function using its unmangled name as the key.
286 bool insertUnmangled(TFunction *function);
287
288 TSymbol *find(const TString &name) const;
289
290 protected:
291 tLevel level;
292};
293
294// Define ESymbolLevel as int rather than an enum since level can go
295// above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the
296// compiler optimizes the >= of the last element to ==.
297typedef int ESymbolLevel;
298const int COMMON_BUILTINS = 0;
299const int ESSL1_BUILTINS = 1;
300const int ESSL3_BUILTINS = 2;
301const int LAST_BUILTIN_LEVEL = ESSL3_BUILTINS;
302const int GLOBAL_LEVEL = 3;
303
304class TSymbolTable : angle::NonCopyable
305{
306 public:
307 TSymbolTable()
308 : mGlobalInvariant(false)
309 {
310 // The symbol table cannot be used until push() is called, but
311 // the lack of an initial call to push() can be used to detect
312 // that the symbol table has not been preloaded with built-ins.
313 }
314
315 ~TSymbolTable();
316
317 // When the symbol table is initialized with the built-ins, there should
318 // 'push' calls, so that built-ins are at level 0 and the shader
319 // globals are at level 1.
320 bool isEmpty() const
321 {
322 return table.empty();
323 }
324 bool atBuiltInLevel() const
325 {
326 return currentLevel() <= LAST_BUILTIN_LEVEL;
327 }
328 bool atGlobalLevel() const
329 {
330 return currentLevel() <= GLOBAL_LEVEL;
331 }
332 void push()
333 {
334 table.push_back(new TSymbolTableLevel);
335 precisionStack.push_back(new PrecisionStackLevel);
336 }
337
338 void pop()
339 {
340 delete table.back();
341 table.pop_back();
342
343 delete precisionStack.back();
344 precisionStack.pop_back();
345 }
346
347 bool declare(TSymbol *symbol)
348 {
349 return insert(currentLevel(), symbol);
350 }
351
352 bool insert(ESymbolLevel level, TSymbol *symbol)
353 {
354 return table[level]->insert(symbol);
355 }
356
357 bool insert(ESymbolLevel level, const char *ext, TSymbol *symbol)
358 {
359 symbol->relateToExtension(ext);
360 return table[level]->insert(symbol);
361 }
362
363 bool insertConstInt(ESymbolLevel level, const char *name, int value)
364 {
365 TVariable *constant = new TVariable(
366 NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1));
367 constant->getConstPointer()->setIConst(value);
368 return insert(level, constant);
369 }
370
371 void insertBuiltIn(ESymbolLevel level, TOperator op, const char *ext, TType *rvalue, const char *name,
372 TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0, TType *ptype5 = 0);
373
374 void insertBuiltIn(ESymbolLevel level, TType *rvalue, const char *name,
375 TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0, TType *ptype5 = 0)
376 {
377 insertBuiltIn(level, EOpNull, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
378 }
379
380 void insertBuiltIn(ESymbolLevel level, const char *ext, TType *rvalue, const char *name,
381 TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0, TType *ptype5 = 0)
382 {
383 insertBuiltIn(level, EOpNull, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
384 }
385
386 void insertBuiltIn(ESymbolLevel level, TOperator op, TType *rvalue, const char *name,
387 TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0, TType *ptype5 = 0)
388 {
389 insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
390 }
391
392 TSymbol *find(const TString &name, int shaderVersion,
393 bool *builtIn = NULL, bool *sameScope = NULL) const;
394 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
395
396 TSymbolTableLevel *getOuterLevel()
397 {
398 assert(currentLevel() >= 1);
399 return table[currentLevel() - 1];
400 }
401
402 void dump(TInfoSink &infoSink) const;
403
404 bool setDefaultPrecision(const TPublicType &type, TPrecision prec)
405 {
406 if (!SupportsPrecision(type.type))
407 return false;
408 if (type.isAggregate())
409 return false; // Not allowed to set for aggregate types
410 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
411 // Uses map operator [], overwrites the current value
412 (*precisionStack[indexOfLastElement])[type.type] = prec;
413 return true;
414 }
415
416 // Searches down the precisionStack for a precision qualifier
417 // for the specified TBasicType
418 TPrecision getDefaultPrecision(TBasicType type) const;
419
420 // This records invariant varyings declared through
421 // "invariant varying_name;".
422 void addInvariantVarying(const std::string &originalName)
423 {
424 mInvariantVaryings.insert(originalName);
425 }
426 // If this returns false, the varying could still be invariant
427 // if it is set as invariant during the varying variable
428 // declaration - this piece of information is stored in the
429 // variable's type, not here.
430 bool isVaryingInvariant(const std::string &originalName) const
431 {
432 return (mGlobalInvariant ||
433 mInvariantVaryings.count(originalName) > 0);
434 }
435
436 void setGlobalInvariant() { mGlobalInvariant = true; }
437 bool getGlobalInvariant() const { return mGlobalInvariant; }
438
439 static int nextUniqueId()
440 {
441 return ++uniqueIdCounter;
442 }
443
444 private:
445 ESymbolLevel currentLevel() const
446 {
447 return static_cast<ESymbolLevel>(table.size() - 1);
448 }
449
450 std::vector<TSymbolTableLevel *> table;
451 typedef TMap<TBasicType, TPrecision> PrecisionStackLevel;
452 std::vector< PrecisionStackLevel *> precisionStack;
453
454 std::set<std::string> mInvariantVaryings;
455 bool mGlobalInvariant;
456
457 static int uniqueIdCounter;
458};
459
460#endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_
461