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.
7 #ifndef _SYMBOL_TABLE_INCLUDED_
8 #define _SYMBOL_TABLE_INCLUDED_
11 // Symbol table for parsing. Has these design characteristics:
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
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.
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
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.
29 // * No temporaries: Temporaries made from operations (+, --, .xy, etc.)
30 // are tracked in the intermediate representation, not the symbol table.
35 #include "common/angleutils.h"
36 #include "compiler/translator/InfoSink.h"
37 #include "compiler/translator/IntermNode.h"
39 // Symbol base class. (Can build functions or variables out of these...)
43 POOL_ALLOCATOR_NEW_DELETE();
44 TSymbol(const TString *n)
51 // don't delete name, it's from the pool
54 const TString &getName() const
58 virtual const TString &getMangledName() const
62 virtual bool isFunction() const
66 virtual bool isVariable() const
70 void setUniqueId(int id)
74 int getUniqueId() const
78 void relateToExtension(const TString &ext)
82 const TString &getExtension() const
88 DISALLOW_COPY_AND_ASSIGN(TSymbol);
90 int uniqueId; // For real comparing during code generation
95 // Variable class, meaning a symbol that's not a function.
97 // There could be a separate class heirarchy for Constant variables;
98 // Only one of int, bool, or float, (or none) is correct for
99 // any particular use, but it's easy to do this way, and doesn't
100 // seem worth having separate classes, and "getConst" can't simply return
101 // different values for different types polymorphically, so this is
102 // just simple and pragmatic.
103 class TVariable : public TSymbol
106 TVariable(const TString *name, const TType &t, bool uT = false)
116 virtual bool isVariable() const
124 const TType &getType() const
128 bool isUserType() const
132 void setQualifier(TQualifier qualifier)
134 type.setQualifier(qualifier);
137 ConstantUnion *getConstPointer()
140 unionArray = new ConstantUnion[type.getObjectSize()];
145 ConstantUnion *getConstPointer() const
150 void shareConstPointer(ConstantUnion *constArray)
152 if (unionArray == constArray)
156 unionArray = constArray;
160 DISALLOW_COPY_AND_ASSIGN(TVariable);
164 // we are assuming that Pool Allocator will free the memory
165 // allocated to unionArray when this object is destroyed.
166 ConstantUnion *unionArray;
169 // The function sub-class of symbols and the parser will need to
170 // share this definition of a function parameter.
177 // The function sub-class of a symbol.
178 class TFunction : public TSymbol
181 TFunction(TOperator o)
183 returnType(TType(EbtVoid, EbpUndefined)),
188 TFunction(const TString *name, const TType &retType, TOperator tOp = EOpNull)
191 mangledName(TFunction::mangleName(*name)),
196 virtual ~TFunction();
197 virtual bool isFunction() const
202 static TString mangleName(const TString &name)
206 static TString unmangleName(const TString &mangledName)
208 return TString(mangledName.c_str(), mangledName.find_first_of('('));
211 void addParameter(TParameter &p)
213 parameters.push_back(p);
214 mangledName = mangledName + p.type->getMangledName();
217 const TString &getMangledName() const
221 const TType &getReturnType() const
226 void relateToOperator(TOperator o)
230 TOperator getBuiltInOp() const
244 size_t getParamCount() const
246 return parameters.size();
248 const TParameter &getParam(size_t i) const
250 return parameters[i];
254 DISALLOW_COPY_AND_ASSIGN(TFunction);
256 typedef TVector<TParameter> TParamList;
257 TParamList parameters;
264 // Interface block name sub-symbol
265 class TInterfaceBlockName : public TSymbol
268 TInterfaceBlockName(const TString *name)
273 virtual ~TInterfaceBlockName()
278 class TSymbolTableLevel
281 typedef TMap<TString, TSymbol *> tLevel;
282 typedef tLevel::const_iterator const_iterator;
283 typedef const tLevel::value_type tLevelPair;
284 typedef std::pair<tLevel::iterator, bool> tInsertResult;
289 ~TSymbolTableLevel();
291 bool insert(TSymbol *symbol);
293 TSymbol *find(const TString &name) const;
295 void relateToOperator(const char *name, TOperator op);
296 void relateToExtension(const char *name, const TString &ext);
307 LAST_BUILTIN_LEVEL = ESSL3_BUILTINS,
316 // The symbol table cannot be used until push() is called, but
317 // the lack of an initial call to push() can be used to detect
318 // that the symbol table has not been preloaded with built-ins.
323 // When the symbol table is initialized with the built-ins, there should
324 // 'push' calls, so that built-ins are at level 0 and the shader
325 // globals are at level 1.
328 return table.empty();
330 bool atBuiltInLevel() const
332 return currentLevel() <= LAST_BUILTIN_LEVEL;
334 bool atGlobalLevel() const
336 return currentLevel() <= GLOBAL_LEVEL;
340 table.push_back(new TSymbolTableLevel);
341 precisionStack.push_back(new PrecisionStackLevel);
349 delete precisionStack.back();
350 precisionStack.pop_back();
353 bool declare(TSymbol *symbol)
355 return insert(currentLevel(), symbol);
358 bool insert(ESymbolLevel level, TSymbol *symbol)
360 return table[level]->insert(symbol);
363 bool insertConstInt(ESymbolLevel level, const char *name, int value)
365 TVariable *constant = new TVariable(
366 NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1));
367 constant->getConstPointer()->setIConst(value);
368 return insert(level, constant);
371 void insertBuiltIn(ESymbolLevel level, TType *rvalue, const char *name,
372 TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0,
373 TType *ptype4 = 0, TType *ptype5 = 0);
375 TSymbol *find(const TString &name, int shaderVersion,
376 bool *builtIn = NULL, bool *sameScope = NULL) const;
377 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
379 TSymbolTableLevel *getOuterLevel()
381 assert(currentLevel() >= 1);
382 return table[currentLevel() - 1];
385 void relateToOperator(ESymbolLevel level, const char *name, TOperator op)
387 table[level]->relateToOperator(name, op);
389 void relateToExtension(ESymbolLevel level, const char *name, const TString &ext)
391 table[level]->relateToExtension(name, ext);
393 void dump(TInfoSink &infoSink) const;
395 bool setDefaultPrecision(const TPublicType &type, TPrecision prec)
397 if (!SupportsPrecision(type.type))
399 if (type.isAggregate())
400 return false; // Not allowed to set for aggregate types
401 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
402 // Uses map operator [], overwrites the current value
403 (*precisionStack[indexOfLastElement])[type.type] = prec;
407 // Searches down the precisionStack for a precision qualifier
408 // for the specified TBasicType
409 TPrecision getDefaultPrecision(TBasicType type) const;
411 static int nextUniqueId()
413 return ++uniqueIdCounter;
417 ESymbolLevel currentLevel() const
419 return static_cast<ESymbolLevel>(table.size() - 1);
422 std::vector<TSymbolTableLevel *> table;
423 typedef TMap<TBasicType, TPrecision> PrecisionStackLevel;
424 std::vector< PrecisionStackLevel *> precisionStack;
426 static int uniqueIdCounter;
429 #endif // _SYMBOL_TABLE_INCLUDED_