2 //Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 //Copyright (C) 2013 LunarG, Inc.
7 //Redistribution and use in source and binary forms, with or without
8 //modification, are permitted provided that the following conditions
11 // Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
14 // Redistributions in binary form must reproduce the above
15 // copyright notice, this list of conditions and the following
16 // disclaimer in the documentation and/or other materials provided
17 // with the distribution.
19 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20 // contributors may be used to endorse or promote products derived
21 // from this software without specific prior written permission.
23 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 //POSSIBILITY OF SUCH DAMAGE.
37 #ifndef _SYMBOL_TABLE_INCLUDED_
38 #define _SYMBOL_TABLE_INCLUDED_
41 // Symbol table for parsing. Has these design characteristics:
43 // * Same symbol table can be used to compile many shaders, to preserve
44 // effort of creating and loading with the large numbers of built-in
47 // --> This requires a copy mechanism, so initial pools used to create
48 // the shared information can be popped. Done through "clone"
51 // * Name mangling will be used to give each function a unique name
52 // so that symbol table lookups are never ambiguous. This allows
53 // a simpler symbol table structure.
55 // * Pushing and popping of scope, so symbol table will really be a stack
56 // of symbol tables. Searched from the top, with new inserts going into
59 // * Constants: Compile time constant symbols will keep their values
60 // in the symbol table. The parser can substitute constants at parse
61 // time, including doing constant folding and constant propagation.
63 // * No temporaries: Temporaries made from operations (+, --, .xy, etc.)
64 // are tracked in the intermediate representation, not the symbol table.
67 #include "../Include/Common.h"
68 #include "../Include/intermediate.h"
69 #include "../Include/InfoSink.h"
74 // Symbol base class. (Can build functions or variables out of these...)
83 POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
84 explicit TSymbol(const TString *n) : name(n), numExtensions(0), extensions(0), writable(true) { }
85 virtual TSymbol* clone() const = 0;
86 virtual ~TSymbol() { } // rely on all symbol owned memory coming from the pool
88 virtual const TString& getName() const { return *name; }
89 virtual void changeName(const TString* newName) { name = newName; }
90 virtual const TString& getMangledName() const { return getName(); }
91 virtual TFunction* getAsFunction() { return 0; }
92 virtual const TFunction* getAsFunction() const { return 0; }
93 virtual TVariable* getAsVariable() { return 0; }
94 virtual const TVariable* getAsVariable() const { return 0; }
95 virtual const TAnonMember* getAsAnonMember() const { return 0; }
96 virtual const TType& getType() const = 0;
97 virtual TType& getWritableType() = 0;
98 virtual void setUniqueId(int id) { uniqueId = id; }
99 virtual int getUniqueId() const { return uniqueId; }
100 virtual void setExtensions(int num, const char* const exts[])
102 assert(extensions == 0);
105 extensions = NewPoolObject(exts[0], num);
106 for (int e = 0; e < num; ++e)
107 extensions[e] = exts[e];
109 virtual int getNumExtensions() const { return numExtensions; }
110 virtual const char** getExtensions() const { return extensions; }
111 virtual void dump(TInfoSink &infoSink) const = 0;
113 virtual bool isReadOnly() const { return ! writable; }
114 virtual void makeReadOnly() { writable = false; }
117 explicit TSymbol(const TSymbol&);
118 TSymbol& operator=(const TSymbol&);
121 unsigned int uniqueId; // For cross-scope comparing during code generation
123 // For tracking what extensions must be present
124 // (don't use if correct version/profile is present).
126 const char** extensions; // an array of pointers to existing constant char strings
129 // N.B.: Non-const functions that will be generally used should assert on this,
130 // to avoid overwriting shared symbol-table information.
136 // Variable class, meaning a symbol that's not a function.
138 // There could be a separate class hierarchy for Constant variables;
139 // Only one of int, bool, or float, (or none) is correct for
140 // any particular use, but it's easy to do this way, and doesn't
141 // seem worth having separate classes, and "getConst" can't simply return
142 // different values for different types polymorphically, so this is
143 // just simple and pragmatic.
145 class TVariable : public TSymbol {
147 TVariable(const TString *name, const TType& t, bool uT = false )
150 constSubtree(nullptr) { type.shallowCopy(t); }
151 virtual TVariable* clone() const;
152 virtual ~TVariable() { }
154 virtual TVariable* getAsVariable() { return this; }
155 virtual const TVariable* getAsVariable() const { return this; }
156 virtual const TType& getType() const { return type; }
157 virtual TType& getWritableType() { assert(writable); return type; }
158 virtual bool isUserType() const { return userType; }
159 virtual const TConstUnionArray& getConstArray() const { return constArray; }
160 virtual TConstUnionArray& getWritableConstArray() { assert(writable); return constArray; }
161 virtual void setConstArray(const TConstUnionArray& array) { constArray = array; }
162 virtual void setConstSubtree(TIntermTyped* subtree) { constSubtree = subtree; }
163 virtual TIntermTyped* getConstSubtree() const { return constSubtree; }
165 virtual void dump(TInfoSink &infoSink) const;
168 explicit TVariable(const TVariable&);
169 TVariable& operator=(const TVariable&);
173 // we are assuming that Pool Allocator will free the memory allocated to unionArray
174 // when this object is destroyed
176 // TODO: these two should be a union
177 // A variable could be a compile-time constant, or a specialization
178 // constant, or neither, but never both.
179 TConstUnionArray constArray; // for compile-time constant value
180 TIntermTyped* constSubtree; // for specialization constant computation
184 // The function sub-class of symbols and the parser will need to
185 // share this definition of a function parameter.
190 void copyParam(const TParameter& param)
193 name = NewPoolTString(param.name->c_str());
196 type = param.type->clone();
201 // The function sub-class of a symbol.
203 class TFunction : public TSymbol {
205 explicit TFunction(TOperator o) :
208 defined(false), prototyped(false) { }
209 TFunction(const TString *name, const TType& retType, TOperator tOp = EOpNull) :
211 mangledName(*name + '('),
213 defined(false), prototyped(false) { returnType.shallowCopy(retType); }
214 virtual TFunction* clone() const;
215 virtual ~TFunction();
217 virtual TFunction* getAsFunction() { return this; }
218 virtual const TFunction* getAsFunction() const { return this; }
220 virtual void addParameter(TParameter& p)
223 parameters.push_back(p);
224 p.type->appendMangledName(mangledName);
227 virtual const TString& getMangledName() const { return mangledName; }
228 virtual const TType& getType() const { return returnType; }
229 virtual TType& getWritableType() { return returnType; }
230 virtual void relateToOperator(TOperator o) { assert(writable); op = o; }
231 virtual TOperator getBuiltInOp() const { return op; }
232 virtual void setDefined() { assert(writable); defined = true; }
233 virtual bool isDefined() const { return defined; }
234 virtual void setPrototyped() { assert(writable); prototyped = true; }
235 virtual bool isPrototyped() const { return prototyped; }
237 virtual int getParamCount() const { return static_cast<int>(parameters.size()); }
238 virtual TParameter& operator[](int i) { assert(writable); return parameters[i]; }
239 virtual const TParameter& operator[](int i) const { return parameters[i]; }
241 virtual void dump(TInfoSink &infoSink) const;
244 explicit TFunction(const TFunction&);
245 TFunction& operator=(const TFunction&);
247 typedef TVector<TParameter> TParamList;
248 TParamList parameters;
257 // Members of anonymous blocks are a kind of TSymbol. They are not hidden in
258 // the symbol table behind a container; rather they are visible and point to
259 // their anonymous container. (The anonymous container is found through the
260 // member, not the other way around.)
262 class TAnonMember : public TSymbol {
264 TAnonMember(const TString* n, unsigned int m, const TVariable& a, int an) : TSymbol(n), anonContainer(a), memberNumber(m), anonId(an) { }
265 virtual TAnonMember* clone() const;
266 virtual ~TAnonMember() { }
268 virtual const TAnonMember* getAsAnonMember() const { return this; }
269 virtual const TVariable& getAnonContainer() const { return anonContainer; }
270 virtual unsigned int getMemberNumber() const { return memberNumber; }
272 virtual const TType& getType() const
274 const TTypeList& types = *anonContainer.getType().getStruct();
275 return *types[memberNumber].type;
278 virtual TType& getWritableType()
281 const TTypeList& types = *anonContainer.getType().getStruct();
282 return *types[memberNumber].type;
285 virtual int getAnonId() const { return anonId; }
286 virtual void dump(TInfoSink &infoSink) const;
289 explicit TAnonMember(const TAnonMember&);
290 TAnonMember& operator=(const TAnonMember&);
292 const TVariable& anonContainer;
293 unsigned int memberNumber;
297 class TSymbolTableLevel {
299 POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
300 TSymbolTableLevel() : defaultPrecision(0), anonId(0) { }
301 ~TSymbolTableLevel();
303 bool insert(TSymbol& symbol, bool separateNameSpaces)
306 // returning true means symbol was added to the table with no semantic errors
308 tInsertResult result;
309 const TString& name = symbol.getName();
311 // An empty name means an anonymous container, exposing its members to the external scope.
312 // Give it a name and insert its members in the symbol table, pointing to the container.
314 snprintf(buf, 20, "%s%d", AnonymousPrefix, anonId);
315 symbol.changeName(NewPoolTString(buf));
318 const TTypeList& types = *symbol.getAsVariable()->getType().getStruct();
319 for (unsigned int m = 0; m < types.size(); ++m) {
320 TAnonMember* member = new TAnonMember(&types[m].type->getFieldName(), m, *symbol.getAsVariable(), anonId);
321 result = level.insert(tLevelPair(member->getMangledName(), member));
330 // Check for redefinition errors:
331 // - STL itself will tell us if there is a direct name collision, with name mangling, at this level
332 // - additionally, check for function-redefining-variable name collisions
333 const TString& insertName = symbol.getMangledName();
334 if (symbol.getAsFunction()) {
335 // make sure there isn't a variable of this name
336 if (! separateNameSpaces && level.find(name) != level.end())
339 // insert, and whatever happens is okay
340 level.insert(tLevelPair(insertName, &symbol));
344 result = level.insert(tLevelPair(insertName, &symbol));
346 return result.second;
351 TSymbol* find(const TString& name) const
353 tLevel::const_iterator it = level.find(name);
354 if (it == level.end())
360 void findFunctionNameList(const TString& name, TVector<TFunction*>& list)
362 size_t parenAt = name.find_first_of('(');
363 TString base(name, 0, parenAt + 1);
365 tLevel::const_iterator begin = level.lower_bound(base);
366 base[parenAt] = ')'; // assume ')' is lexically after '('
367 tLevel::const_iterator end = level.upper_bound(base);
368 for (tLevel::const_iterator it = begin; it != end; ++it)
369 list.push_back(it->second->getAsFunction());
372 // See if there is already a function in the table having the given non-function-style name.
373 bool hasFunctionName(const TString& name) const
375 tLevel::const_iterator candidate = level.lower_bound(name);
376 if (candidate != level.end()) {
377 const TString& candidateName = (*candidate).first;
378 TString::size_type parenAt = candidateName.find_first_of('(');
379 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0)
387 // See if there is a variable at this level having the given non-function-style name.
388 // Return true if name is found, and set variable to true if the name was a variable.
389 bool findFunctionVariableName(const TString& name, bool& variable) const
391 tLevel::const_iterator candidate = level.lower_bound(name);
392 if (candidate != level.end()) {
393 const TString& candidateName = (*candidate).first;
394 TString::size_type parenAt = candidateName.find_first_of('(');
395 if (parenAt == candidateName.npos) {
396 // not a mangled name
397 if (candidateName == name) {
398 // found a variable name match
404 if (candidateName.compare(0, parenAt, name) == 0) {
405 // found a function name match
415 // Use this to do a lazy 'push' of precision defaults the first time
416 // a precision statement is seen in a new scope. Leave it at 0 for
417 // when no push was needed. Thus, it is not the current defaults,
418 // it is what to restore the defaults to when popping a level.
419 void setPreviousDefaultPrecisions(const TPrecisionQualifier *p)
421 // can call multiple times at one scope, will only latch on first call,
422 // as we're tracking the previous scope's values, not the current values
423 if (defaultPrecision != 0)
426 defaultPrecision = new TPrecisionQualifier[EbtNumTypes];
427 for (int t = 0; t < EbtNumTypes; ++t)
428 defaultPrecision[t] = p[t];
431 void getPreviousDefaultPrecisions(TPrecisionQualifier *p)
433 // can be called for table level pops that didn't set the
435 if (defaultPrecision == 0 || p == 0)
438 for (int t = 0; t < EbtNumTypes; ++t)
439 p[t] = defaultPrecision[t];
442 void relateToOperator(const char* name, TOperator op);
443 void setFunctionExtensions(const char* name, int num, const char* const extensions[]);
444 void dump(TInfoSink &infoSink) const;
445 TSymbolTableLevel* clone() const;
449 explicit TSymbolTableLevel(TSymbolTableLevel&);
450 TSymbolTableLevel& operator=(TSymbolTableLevel&);
452 typedef std::map<TString, TSymbol*, std::less<TString>, pool_allocator<std::pair<const TString, TSymbol*> > > tLevel;
453 typedef const tLevel::value_type tLevelPair;
454 typedef std::pair<tLevel::iterator, bool> tInsertResult;
456 tLevel level; // named mappings
457 TPrecisionQualifier *defaultPrecision;
463 TSymbolTable() : uniqueId(0), noBuiltInRedeclarations(false), separateNameSpaces(false), adoptedLevels(0)
466 // This symbol table cannot be used until push() is called.
471 // this can be called explicitly; safest to code it so it can be called multiple times
473 // don't deallocate levels passed in from elsewhere
474 while (table.size() > adoptedLevels)
478 void adoptLevels(TSymbolTable& symTable)
480 for (unsigned int level = 0; level < symTable.table.size(); ++level) {
481 table.push_back(symTable.table[level]);
484 uniqueId = symTable.uniqueId;
485 noBuiltInRedeclarations = symTable.noBuiltInRedeclarations;
486 separateNameSpaces = symTable.separateNameSpaces;
490 // While level adopting is generic, the methods below enact a the following
491 // convention for levels:
492 // 0: common built-ins shared across all stages, all compiles, only one copy for all symbol tables
493 // 1: per-stage built-ins, shared across all compiles, but a different copy per stage
494 // 2: built-ins specific to a compile, like resources that are context-dependent, or redeclared built-ins
495 // 3: user-shader globals
498 static const int globalLevel = 3;
499 bool isSharedLevel(int level) { return level <= 1; } // exclude all per-compile levels
500 bool isBuiltInLevel(int level) { return level <= 2; } // exclude user globals
501 bool isGlobalLevel(int level) { return level <= globalLevel; } // include user globals
503 bool isEmpty() { return table.size() == 0; }
504 bool atBuiltInLevel() { return isBuiltInLevel(currentLevel()); }
505 bool atGlobalLevel() { return isGlobalLevel(currentLevel()); }
507 void setNoBuiltInRedeclarations() { noBuiltInRedeclarations = true; }
508 void setSeparateNameSpaces() { separateNameSpaces = true; }
512 table.push_back(new TSymbolTableLevel);
515 void pop(TPrecisionQualifier *p)
517 table[currentLevel()]->getPreviousDefaultPrecisions(p);
523 // Insert a visible symbol into the symbol table so it can
524 // be found later by name.
526 // Returns false if the was a name collision.
528 bool insert(TSymbol& symbol)
530 symbol.setUniqueId(++uniqueId);
532 // make sure there isn't a function of this variable name
533 if (! separateNameSpaces && ! symbol.getAsFunction() && table[currentLevel()]->hasFunctionName(symbol.getName()))
536 // check for not overloading or redefining a built-in function
537 if (noBuiltInRedeclarations) {
538 if (atGlobalLevel() && currentLevel() > 0) {
539 if (table[0]->hasFunctionName(symbol.getName()))
541 if (currentLevel() > 1 && table[1]->hasFunctionName(symbol.getName()))
546 return table[currentLevel()]->insert(symbol, separateNameSpaces);
550 // To allocate an internal temporary, which will need to be uniquely
551 // identified by the consumer of the AST, but never need to
552 // found by doing a symbol table search by name, hence allowed an
553 // arbitrary name in the symbol with no worry of collision.
555 void makeInternalVariable(TSymbol& symbol)
557 symbol.setUniqueId(++uniqueId);
561 // Copy a variable or anonymous member's structure from a shared level so that
562 // it can be added (soon after return) to the symbol table where it can be
563 // modified without impacting other users of the shared table.
565 TSymbol* copyUpDeferredInsert(TSymbol* shared)
567 if (shared->getAsVariable()) {
568 TSymbol* copy = shared->clone();
569 copy->setUniqueId(shared->getUniqueId());
572 const TAnonMember* anon = shared->getAsAnonMember();
574 TVariable* container = anon->getAnonContainer().clone();
575 container->changeName(NewPoolTString(""));
576 container->setUniqueId(anon->getAnonContainer().getUniqueId());
581 TSymbol* copyUp(TSymbol* shared)
583 TSymbol* copy = copyUpDeferredInsert(shared);
584 table[globalLevel]->insert(*copy, separateNameSpaces);
585 if (shared->getAsVariable())
588 // return the copy of the anonymous member
589 return table[globalLevel]->find(shared->getName());
593 TSymbol* find(const TString& name, bool* builtIn = 0, bool *currentScope = 0)
595 int level = currentLevel();
598 symbol = table[level]->find(name);
600 } while (symbol == 0 && level >= 0);
603 *builtIn = isBuiltInLevel(level);
605 *currentScope = isGlobalLevel(currentLevel()) || level == currentLevel(); // consider shared levels as "current scope" WRT user globals
610 bool isFunctionNameVariable(const TString& name) const
612 if (separateNameSpaces)
615 int level = currentLevel();
618 bool found = table[level]->findFunctionVariableName(name, variable);
622 } while (level >= 0);
627 void findFunctionNameList(const TString& name, TVector<TFunction*>& list, bool& builtIn)
629 // For user levels, return the set found in the first scope with a match
631 int level = currentLevel();
633 table[level]->findFunctionNameList(name, list);
635 } while (list.empty() && level >= globalLevel);
640 // Gather across all built-in levels; they don't hide each other
643 table[level]->findFunctionNameList(name, list);
645 } while (level >= 0);
648 void relateToOperator(const char* name, TOperator op)
650 for (unsigned int level = 0; level < table.size(); ++level)
651 table[level]->relateToOperator(name, op);
654 void setFunctionExtensions(const char* name, int num, const char* const extensions[])
656 for (unsigned int level = 0; level < table.size(); ++level)
657 table[level]->setFunctionExtensions(name, num, extensions);
660 void setVariableExtensions(const char* name, int num, const char* const extensions[])
662 TSymbol* symbol = find(TString(name));
664 symbol->setExtensions(num, extensions);
667 int getMaxSymbolId() { return uniqueId; }
668 void dump(TInfoSink &infoSink) const;
669 void copyTable(const TSymbolTable& copyOf);
671 void setPreviousDefaultPrecisions(TPrecisionQualifier *p) { table[currentLevel()]->setPreviousDefaultPrecisions(p); }
675 for (unsigned int level = 0; level < table.size(); ++level)
676 table[level]->readOnly();
680 TSymbolTable(TSymbolTable&);
681 TSymbolTable& operator=(TSymbolTableLevel&);
683 int currentLevel() const { return static_cast<int>(table.size()) - 1; }
685 std::vector<TSymbolTableLevel*> table;
686 int uniqueId; // for unique identification in code generation
687 bool noBuiltInRedeclarations;
688 bool separateNameSpaces;
689 unsigned int adoptedLevels;
692 } // end namespace glslang
694 #endif // _SYMBOL_TABLE_INCLUDED_