2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // Copyright (C) 2013 LunarG, Inc.
5 // All rights reserved.
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 void addPrefix(const char* prefix)
92 TString newName(prefix);
93 newName.append(*name);
94 changeName(NewPoolTString(newName.c_str()));
96 virtual const TString& getMangledName() const { return getName(); }
97 virtual TFunction* getAsFunction() { return 0; }
98 virtual const TFunction* getAsFunction() const { return 0; }
99 virtual TVariable* getAsVariable() { return 0; }
100 virtual const TVariable* getAsVariable() const { return 0; }
101 virtual const TAnonMember* getAsAnonMember() const { return 0; }
102 virtual const TType& getType() const = 0;
103 virtual TType& getWritableType() = 0;
104 virtual void setUniqueId(int id) { uniqueId = id; }
105 virtual int getUniqueId() const { return uniqueId; }
106 virtual void setExtensions(int num, const char* const exts[])
108 assert(extensions == 0);
111 extensions = NewPoolObject(exts[0], num);
112 for (int e = 0; e < num; ++e)
113 extensions[e] = exts[e];
115 virtual int getNumExtensions() const { return numExtensions; }
116 virtual const char** getExtensions() const { return extensions; }
117 virtual void dump(TInfoSink &infoSink) const = 0;
119 virtual bool isReadOnly() const { return ! writable; }
120 virtual void makeReadOnly() { writable = false; }
123 explicit TSymbol(const TSymbol&);
124 TSymbol& operator=(const TSymbol&);
127 unsigned int uniqueId; // For cross-scope comparing during code generation
129 // For tracking what extensions must be present
130 // (don't use if correct version/profile is present).
132 const char** extensions; // an array of pointers to existing constant char strings
135 // N.B.: Non-const functions that will be generally used should assert on this,
136 // to avoid overwriting shared symbol-table information.
142 // Variable class, meaning a symbol that's not a function.
144 // There could be a separate class hierarchy for Constant variables;
145 // Only one of int, bool, or float, (or none) is correct for
146 // any particular use, but it's easy to do this way, and doesn't
147 // seem worth having separate classes, and "getConst" can't simply return
148 // different values for different types polymorphically, so this is
149 // just simple and pragmatic.
151 class TVariable : public TSymbol {
153 TVariable(const TString *name, const TType& t, bool uT = false )
156 constSubtree(nullptr),
157 anonId(-1) { type.shallowCopy(t); }
158 virtual TVariable* clone() const;
159 virtual ~TVariable() { }
161 virtual TVariable* getAsVariable() { return this; }
162 virtual const TVariable* getAsVariable() const { return this; }
163 virtual const TType& getType() const { return type; }
164 virtual TType& getWritableType() { assert(writable); return type; }
165 virtual bool isUserType() const { return userType; }
166 virtual const TConstUnionArray& getConstArray() const { return constArray; }
167 virtual TConstUnionArray& getWritableConstArray() { assert(writable); return constArray; }
168 virtual void setConstArray(const TConstUnionArray& array) { constArray = array; }
169 virtual void setConstSubtree(TIntermTyped* subtree) { constSubtree = subtree; }
170 virtual TIntermTyped* getConstSubtree() const { return constSubtree; }
171 virtual void setAnonId(int i) { anonId = i; }
172 virtual int getAnonId() const { return anonId; }
174 virtual void dump(TInfoSink &infoSink) const;
177 explicit TVariable(const TVariable&);
178 TVariable& operator=(const TVariable&);
182 // we are assuming that Pool Allocator will free the memory allocated to unionArray
183 // when this object is destroyed
185 // TODO: these two should be a union
186 // A variable could be a compile-time constant, or a specialization
187 // constant, or neither, but never both.
188 TConstUnionArray constArray; // for compile-time constant value
189 TIntermTyped* constSubtree; // for specialization constant computation
190 int anonId; // the ID used for anonymous blocks: TODO: see if uniqueId could serve a dual purpose
194 // The function sub-class of symbols and the parser will need to
195 // share this definition of a function parameter.
200 TIntermTyped* defaultValue;
201 TBuiltInVariable declaredBuiltIn;
202 void copyParam(const TParameter& param)
205 name = NewPoolTString(param.name->c_str());
208 type = param.type->clone();
209 defaultValue = param.defaultValue;
210 declaredBuiltIn = param.declaredBuiltIn;
215 // The function sub-class of a symbol.
217 class TFunction : public TSymbol {
219 explicit TFunction(TOperator o) :
222 defined(false), prototyped(false), defaultParamCount(0) { }
223 TFunction(const TString *name, const TType& retType, TOperator tOp = EOpNull) :
225 mangledName(*name + '('),
227 defined(false), prototyped(false), defaultParamCount(0)
229 returnType.shallowCopy(retType);
230 declaredBuiltIn = retType.getQualifier().builtIn;
232 virtual TFunction* clone() const override;
233 virtual ~TFunction();
235 virtual TFunction* getAsFunction() override { return this; }
236 virtual const TFunction* getAsFunction() const override { return this; }
238 virtual void addParameter(TParameter& p)
241 p.declaredBuiltIn = p.type->getQualifier().builtIn;
242 parameters.push_back(p);
243 p.type->appendMangledName(mangledName);
245 if (p.defaultValue != nullptr)
248 virtual void addPrefix(const char* prefix) override
250 TSymbol::addPrefix(prefix);
251 mangledName.insert(0, prefix);
254 virtual const TString& getMangledName() const override { return mangledName; }
255 virtual const TType& getType() const override { return returnType; }
256 virtual TBuiltInVariable getDeclaredBuiltInType() const { return declaredBuiltIn; }
257 virtual TType& getWritableType() override { return returnType; }
258 virtual void relateToOperator(TOperator o) { assert(writable); op = o; }
259 virtual TOperator getBuiltInOp() const { return op; }
260 virtual void setDefined() { assert(writable); defined = true; }
261 virtual bool isDefined() const { return defined; }
262 virtual void setPrototyped() { assert(writable); prototyped = true; }
263 virtual bool isPrototyped() const { return prototyped; }
265 // Return total number of parameters
266 virtual int getParamCount() const { return static_cast<int>(parameters.size()); }
267 // Return number of parameters with default values.
268 virtual int getDefaultParamCount() const { return defaultParamCount; }
269 // Return number of fixed parameters (without default values)
270 virtual int getFixedParamCount() const { return getParamCount() - getDefaultParamCount(); }
272 virtual TParameter& operator[](int i) { assert(writable); return parameters[i]; }
273 virtual const TParameter& operator[](int i) const { return parameters[i]; }
275 virtual void dump(TInfoSink &infoSink) const override;
278 explicit TFunction(const TFunction&);
279 TFunction& operator=(const TFunction&);
281 typedef TVector<TParameter> TParamList;
282 TParamList parameters;
284 TBuiltInVariable declaredBuiltIn;
290 int defaultParamCount;
294 // Members of anonymous blocks are a kind of TSymbol. They are not hidden in
295 // the symbol table behind a container; rather they are visible and point to
296 // their anonymous container. (The anonymous container is found through the
297 // member, not the other way around.)
299 class TAnonMember : public TSymbol {
301 TAnonMember(const TString* n, unsigned int m, const TVariable& a, int an) : TSymbol(n), anonContainer(a), memberNumber(m), anonId(an) { }
302 virtual TAnonMember* clone() const;
303 virtual ~TAnonMember() { }
305 virtual const TAnonMember* getAsAnonMember() const { return this; }
306 virtual const TVariable& getAnonContainer() const { return anonContainer; }
307 virtual unsigned int getMemberNumber() const { return memberNumber; }
309 virtual const TType& getType() const
311 const TTypeList& types = *anonContainer.getType().getStruct();
312 return *types[memberNumber].type;
315 virtual TType& getWritableType()
318 const TTypeList& types = *anonContainer.getType().getStruct();
319 return *types[memberNumber].type;
322 virtual int getAnonId() const { return anonId; }
323 virtual void dump(TInfoSink &infoSink) const;
326 explicit TAnonMember(const TAnonMember&);
327 TAnonMember& operator=(const TAnonMember&);
329 const TVariable& anonContainer;
330 unsigned int memberNumber;
334 class TSymbolTableLevel {
336 POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
337 TSymbolTableLevel() : defaultPrecision(0), anonId(0) { }
338 ~TSymbolTableLevel();
340 bool insert(TSymbol& symbol, bool separateNameSpaces)
343 // returning true means symbol was added to the table with no semantic errors
345 const TString& name = symbol.getName();
347 symbol.getAsVariable()->setAnonId(anonId++);
348 // An empty name means an anonymous container, exposing its members to the external scope.
349 // Give it a name and insert its members in the symbol table, pointing to the container.
351 snprintf(buf, 20, "%s%d", AnonymousPrefix, symbol.getAsVariable()->getAnonId());
352 symbol.changeName(NewPoolTString(buf));
354 return insertAnonymousMembers(symbol, 0);
356 // Check for redefinition errors:
357 // - STL itself will tell us if there is a direct name collision, with name mangling, at this level
358 // - additionally, check for function-redefining-variable name collisions
359 const TString& insertName = symbol.getMangledName();
360 if (symbol.getAsFunction()) {
361 // make sure there isn't a variable of this name
362 if (! separateNameSpaces && level.find(name) != level.end())
365 // insert, and whatever happens is okay
366 level.insert(tLevelPair(insertName, &symbol));
370 return level.insert(tLevelPair(insertName, &symbol)).second;
374 // Add more members to an already inserted aggregate object
375 bool amend(TSymbol& symbol, int firstNewMember)
377 // See insert() for comments on basic explanation of insert.
378 // This operates similarly, but more simply.
379 // Only supporting amend of anonymous blocks so far.
380 if (IsAnonymous(symbol.getName()))
381 return insertAnonymousMembers(symbol, firstNewMember);
386 bool insertAnonymousMembers(TSymbol& symbol, int firstMember)
388 const TTypeList& types = *symbol.getAsVariable()->getType().getStruct();
389 for (unsigned int m = firstMember; m < types.size(); ++m) {
390 TAnonMember* member = new TAnonMember(&types[m].type->getFieldName(), m, *symbol.getAsVariable(), symbol.getAsVariable()->getAnonId());
391 if (! level.insert(tLevelPair(member->getMangledName(), member)).second)
398 TSymbol* find(const TString& name) const
400 tLevel::const_iterator it = level.find(name);
401 if (it == level.end())
407 void findFunctionNameList(const TString& name, TVector<const TFunction*>& list)
409 size_t parenAt = name.find_first_of('(');
410 TString base(name, 0, parenAt + 1);
412 tLevel::const_iterator begin = level.lower_bound(base);
413 base[parenAt] = ')'; // assume ')' is lexically after '('
414 tLevel::const_iterator end = level.upper_bound(base);
415 for (tLevel::const_iterator it = begin; it != end; ++it)
416 list.push_back(it->second->getAsFunction());
419 // See if there is already a function in the table having the given non-function-style name.
420 bool hasFunctionName(const TString& name) const
422 tLevel::const_iterator candidate = level.lower_bound(name);
423 if (candidate != level.end()) {
424 const TString& candidateName = (*candidate).first;
425 TString::size_type parenAt = candidateName.find_first_of('(');
426 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0)
434 // See if there is a variable at this level having the given non-function-style name.
435 // Return true if name is found, and set variable to true if the name was a variable.
436 bool findFunctionVariableName(const TString& name, bool& variable) const
438 tLevel::const_iterator candidate = level.lower_bound(name);
439 if (candidate != level.end()) {
440 const TString& candidateName = (*candidate).first;
441 TString::size_type parenAt = candidateName.find_first_of('(');
442 if (parenAt == candidateName.npos) {
443 // not a mangled name
444 if (candidateName == name) {
445 // found a variable name match
451 if (candidateName.compare(0, parenAt, name) == 0) {
452 // found a function name match
462 // Use this to do a lazy 'push' of precision defaults the first time
463 // a precision statement is seen in a new scope. Leave it at 0 for
464 // when no push was needed. Thus, it is not the current defaults,
465 // it is what to restore the defaults to when popping a level.
466 void setPreviousDefaultPrecisions(const TPrecisionQualifier *p)
468 // can call multiple times at one scope, will only latch on first call,
469 // as we're tracking the previous scope's values, not the current values
470 if (defaultPrecision != 0)
473 defaultPrecision = new TPrecisionQualifier[EbtNumTypes];
474 for (int t = 0; t < EbtNumTypes; ++t)
475 defaultPrecision[t] = p[t];
478 void getPreviousDefaultPrecisions(TPrecisionQualifier *p)
480 // can be called for table level pops that didn't set the
482 if (defaultPrecision == 0 || p == 0)
485 for (int t = 0; t < EbtNumTypes; ++t)
486 p[t] = defaultPrecision[t];
489 void relateToOperator(const char* name, TOperator op);
490 void setFunctionExtensions(const char* name, int num, const char* const extensions[]);
491 void dump(TInfoSink &infoSink) const;
492 TSymbolTableLevel* clone() const;
496 explicit TSymbolTableLevel(TSymbolTableLevel&);
497 TSymbolTableLevel& operator=(TSymbolTableLevel&);
499 typedef std::map<TString, TSymbol*, std::less<TString>, pool_allocator<std::pair<const TString, TSymbol*> > > tLevel;
500 typedef const tLevel::value_type tLevelPair;
501 typedef std::pair<tLevel::iterator, bool> tInsertResult;
503 tLevel level; // named mappings
504 TPrecisionQualifier *defaultPrecision;
510 TSymbolTable() : uniqueId(0), noBuiltInRedeclarations(false), separateNameSpaces(false), adoptedLevels(0)
513 // This symbol table cannot be used until push() is called.
518 // this can be called explicitly; safest to code it so it can be called multiple times
520 // don't deallocate levels passed in from elsewhere
521 while (table.size() > adoptedLevels)
525 void adoptLevels(TSymbolTable& symTable)
527 for (unsigned int level = 0; level < symTable.table.size(); ++level) {
528 table.push_back(symTable.table[level]);
531 uniqueId = symTable.uniqueId;
532 noBuiltInRedeclarations = symTable.noBuiltInRedeclarations;
533 separateNameSpaces = symTable.separateNameSpaces;
537 // While level adopting is generic, the methods below enact a the following
538 // convention for levels:
539 // 0: common built-ins shared across all stages, all compiles, only one copy for all symbol tables
540 // 1: per-stage built-ins, shared across all compiles, but a different copy per stage
541 // 2: built-ins specific to a compile, like resources that are context-dependent, or redeclared built-ins
542 // 3: user-shader globals
545 static const int globalLevel = 3;
546 bool isSharedLevel(int level) { return level <= 1; } // exclude all per-compile levels
547 bool isBuiltInLevel(int level) { return level <= 2; } // exclude user globals
548 bool isGlobalLevel(int level) { return level <= globalLevel; } // include user globals
550 bool isEmpty() { return table.size() == 0; }
551 bool atBuiltInLevel() { return isBuiltInLevel(currentLevel()); }
552 bool atGlobalLevel() { return isGlobalLevel(currentLevel()); }
554 void setNoBuiltInRedeclarations() { noBuiltInRedeclarations = true; }
555 void setSeparateNameSpaces() { separateNameSpaces = true; }
559 table.push_back(new TSymbolTableLevel);
562 void pop(TPrecisionQualifier *p)
564 table[currentLevel()]->getPreviousDefaultPrecisions(p);
570 // Insert a visible symbol into the symbol table so it can
571 // be found later by name.
573 // Returns false if the was a name collision.
575 bool insert(TSymbol& symbol)
577 symbol.setUniqueId(++uniqueId);
579 // make sure there isn't a function of this variable name
580 if (! separateNameSpaces && ! symbol.getAsFunction() && table[currentLevel()]->hasFunctionName(symbol.getName()))
583 // check for not overloading or redefining a built-in function
584 if (noBuiltInRedeclarations) {
585 if (atGlobalLevel() && currentLevel() > 0) {
586 if (table[0]->hasFunctionName(symbol.getName()))
588 if (currentLevel() > 1 && table[1]->hasFunctionName(symbol.getName()))
593 return table[currentLevel()]->insert(symbol, separateNameSpaces);
596 // Add more members to an already inserted aggregate object
597 bool amend(TSymbol& symbol, int firstNewMember)
599 // See insert() for comments on basic explanation of insert.
600 // This operates similarly, but more simply.
601 return table[currentLevel()]->amend(symbol, firstNewMember);
605 // To allocate an internal temporary, which will need to be uniquely
606 // identified by the consumer of the AST, but never need to
607 // found by doing a symbol table search by name, hence allowed an
608 // arbitrary name in the symbol with no worry of collision.
610 void makeInternalVariable(TSymbol& symbol)
612 symbol.setUniqueId(++uniqueId);
616 // Copy a variable or anonymous member's structure from a shared level so that
617 // it can be added (soon after return) to the symbol table where it can be
618 // modified without impacting other users of the shared table.
620 TSymbol* copyUpDeferredInsert(TSymbol* shared)
622 if (shared->getAsVariable()) {
623 TSymbol* copy = shared->clone();
624 copy->setUniqueId(shared->getUniqueId());
627 const TAnonMember* anon = shared->getAsAnonMember();
629 TVariable* container = anon->getAnonContainer().clone();
630 container->changeName(NewPoolTString(""));
631 container->setUniqueId(anon->getAnonContainer().getUniqueId());
636 TSymbol* copyUp(TSymbol* shared)
638 TSymbol* copy = copyUpDeferredInsert(shared);
639 table[globalLevel]->insert(*copy, separateNameSpaces);
640 if (shared->getAsVariable())
643 // return the copy of the anonymous member
644 return table[globalLevel]->find(shared->getName());
648 TSymbol* find(const TString& name, bool* builtIn = 0, bool *currentScope = 0)
650 int level = currentLevel();
653 symbol = table[level]->find(name);
655 } while (symbol == 0 && level >= 0);
658 *builtIn = isBuiltInLevel(level);
660 *currentScope = isGlobalLevel(currentLevel()) || level == currentLevel(); // consider shared levels as "current scope" WRT user globals
665 bool isFunctionNameVariable(const TString& name) const
667 if (separateNameSpaces)
670 int level = currentLevel();
673 bool found = table[level]->findFunctionVariableName(name, variable);
677 } while (level >= 0);
682 void findFunctionNameList(const TString& name, TVector<const TFunction*>& list, bool& builtIn)
684 // For user levels, return the set found in the first scope with a match
686 int level = currentLevel();
688 table[level]->findFunctionNameList(name, list);
690 } while (list.empty() && level >= globalLevel);
695 // Gather across all built-in levels; they don't hide each other
698 table[level]->findFunctionNameList(name, list);
700 } while (level >= 0);
703 void relateToOperator(const char* name, TOperator op)
705 for (unsigned int level = 0; level < table.size(); ++level)
706 table[level]->relateToOperator(name, op);
709 void setFunctionExtensions(const char* name, int num, const char* const extensions[])
711 for (unsigned int level = 0; level < table.size(); ++level)
712 table[level]->setFunctionExtensions(name, num, extensions);
715 void setVariableExtensions(const char* name, int num, const char* const extensions[])
717 TSymbol* symbol = find(TString(name));
719 symbol->setExtensions(num, extensions);
722 int getMaxSymbolId() { return uniqueId; }
723 void dump(TInfoSink &infoSink) const;
724 void copyTable(const TSymbolTable& copyOf);
726 void setPreviousDefaultPrecisions(TPrecisionQualifier *p) { table[currentLevel()]->setPreviousDefaultPrecisions(p); }
730 for (unsigned int level = 0; level < table.size(); ++level)
731 table[level]->readOnly();
735 TSymbolTable(TSymbolTable&);
736 TSymbolTable& operator=(TSymbolTableLevel&);
738 int currentLevel() const { return static_cast<int>(table.size()) - 1; }
740 std::vector<TSymbolTableLevel*> table;
741 int uniqueId; // for unique identification in code generation
742 bool noBuiltInRedeclarations;
743 bool separateNameSpaces;
744 unsigned int adoptedLevels;
747 } // end namespace glslang
749 #endif // _SYMBOL_TABLE_INCLUDED_