Add override where needed by clang++
[platform/upstream/glslang.git] / glslang / MachineIndependent / SymbolTable.h
1 //
2 // Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
3 // Copyright (C) 2013 LunarG, Inc.
4 //
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //    Redistributions of source code must retain the above copyright
12 //    notice, this list of conditions and the following disclaimer.
13 //
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.
18 //
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.
22 //
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.
35 //
36
37 #ifndef _SYMBOL_TABLE_INCLUDED_
38 #define _SYMBOL_TABLE_INCLUDED_
39
40 //
41 // Symbol table for parsing.  Has these design characteristics:
42 //
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
45 //   symbols.
46 //
47 // -->  This requires a copy mechanism, so initial pools used to create
48 //   the shared information can be popped.  Done through "clone"
49 //   methods.
50 //
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.
54 //
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
57 //   the top.
58 //
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.
62 //
63 // * No temporaries:  Temporaries made from operations (+, --, .xy, etc.)
64 //   are tracked in the intermediate representation, not the symbol table.
65 //
66
67 #include "../Include/Common.h"
68 #include "../Include/intermediate.h"
69 #include "../Include/InfoSink.h"
70
71 namespace glslang {
72
73 //
74 // Symbol base class.  (Can build functions or variables out of these...)
75 //
76
77 class TVariable;
78 class TFunction;
79 class TAnonMember;
80
81 class TSymbol {
82 public:
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
87
88     virtual const TString& getName() const { return *name; }
89     virtual void changeName(const TString* newName) { name = newName; }
90     virtual void addPrefix(const char* prefix)
91     {
92         TString newName(prefix);
93         newName.append(*name);
94         changeName(NewPoolTString(newName.c_str()));
95     }
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[])
107     {
108         assert(extensions == 0);
109         assert(num > 0);
110         numExtensions = num;
111         extensions = NewPoolObject(exts[0], num);
112         for (int e = 0; e < num; ++e)
113             extensions[e] = exts[e];
114     }
115     virtual int getNumExtensions() const { return numExtensions; }
116     virtual const char** getExtensions() const { return extensions; }
117     virtual void dump(TInfoSink &infoSink) const = 0;
118
119     virtual bool isReadOnly() const { return ! writable; }
120     virtual void makeReadOnly() { writable = false; }
121
122 protected:
123     explicit TSymbol(const TSymbol&);
124     TSymbol& operator=(const TSymbol&);
125
126     const TString *name;
127     unsigned int uniqueId;      // For cross-scope comparing during code generation
128
129     // For tracking what extensions must be present
130     // (don't use if correct version/profile is present).
131     int numExtensions;
132     const char** extensions; // an array of pointers to existing constant char strings
133
134     //
135     // N.B.: Non-const functions that will be generally used should assert on this,
136     // to avoid overwriting shared symbol-table information.
137     //
138     bool writable;
139 };
140
141 //
142 // Variable class, meaning a symbol that's not a function.
143 //
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.
150 //
151 class TVariable : public TSymbol {
152 public:
153     TVariable(const TString *name, const TType& t, bool uT = false )
154         : TSymbol(name),
155           userType(uT),
156           constSubtree(nullptr),
157           anonId(-1) { type.shallowCopy(t); }
158     virtual TVariable* clone() const;
159     virtual ~TVariable() { }
160
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; }
173
174     virtual void dump(TInfoSink &infoSink) const;
175
176 protected:
177     explicit TVariable(const TVariable&);
178     TVariable& operator=(const TVariable&);
179
180     TType type;
181     bool userType;
182     // we are assuming that Pool Allocator will free the memory allocated to unionArray
183     // when this object is destroyed
184
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
191 };
192
193 //
194 // The function sub-class of symbols and the parser will need to
195 // share this definition of a function parameter.
196 //
197 struct TParameter {
198     TString *name;
199     TType* type;
200     TIntermTyped* defaultValue;
201     TBuiltInVariable declaredBuiltIn;
202     void copyParam(const TParameter& param)
203     {
204         if (param.name)
205             name = NewPoolTString(param.name->c_str());
206         else
207             name = 0;
208         type = param.type->clone();
209         defaultValue = param.defaultValue;
210         declaredBuiltIn = param.declaredBuiltIn;
211     }
212 };
213
214 //
215 // The function sub-class of a symbol.
216 //
217 class TFunction : public TSymbol {
218 public:
219     explicit TFunction(TOperator o) :
220         TSymbol(0),
221         op(o),
222         defined(false), prototyped(false), defaultParamCount(0) { }
223     TFunction(const TString *name, const TType& retType, TOperator tOp = EOpNull) :
224         TSymbol(name),
225         mangledName(*name + '('),
226         op(tOp),
227         defined(false), prototyped(false), defaultParamCount(0)
228     {
229         returnType.shallowCopy(retType);
230         declaredBuiltIn = retType.getQualifier().builtIn;
231     }
232     virtual TFunction* clone() const override;
233     virtual ~TFunction();
234
235     virtual TFunction* getAsFunction() override { return this; }
236     virtual const TFunction* getAsFunction() const override { return this; }
237
238     virtual void addParameter(TParameter& p)
239     {
240         assert(writable);
241         p.declaredBuiltIn = p.type->getQualifier().builtIn;
242         parameters.push_back(p);
243         p.type->appendMangledName(mangledName);
244
245         if (p.defaultValue != nullptr)
246             defaultParamCount++;
247     }
248     virtual void addPrefix(const char* prefix) override
249     {
250         TSymbol::addPrefix(prefix);
251         mangledName.insert(0, prefix);
252     }
253
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; }
264
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(); }
271
272     virtual TParameter& operator[](int i) { assert(writable); return parameters[i]; }
273     virtual const TParameter& operator[](int i) const { return parameters[i]; }
274
275     virtual void dump(TInfoSink &infoSink) const override;
276
277 protected:
278     explicit TFunction(const TFunction&);
279     TFunction& operator=(const TFunction&);
280
281     typedef TVector<TParameter> TParamList;
282     TParamList parameters;
283     TType returnType;
284     TBuiltInVariable declaredBuiltIn;
285
286     TString mangledName;
287     TOperator op;
288     bool defined;
289     bool prototyped;
290     int  defaultParamCount;
291 };
292
293 //
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.)
298 //
299 class TAnonMember : public TSymbol {
300 public:
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() { }
304
305     virtual const TAnonMember* getAsAnonMember() const { return this; }
306     virtual const TVariable& getAnonContainer() const { return anonContainer; }
307     virtual unsigned int getMemberNumber() const { return memberNumber; }
308
309     virtual const TType& getType() const
310     {
311         const TTypeList& types = *anonContainer.getType().getStruct();
312         return *types[memberNumber].type;
313     }
314
315     virtual TType& getWritableType()
316     {
317         assert(writable);
318         const TTypeList& types = *anonContainer.getType().getStruct();
319         return *types[memberNumber].type;
320     }
321
322     virtual int getAnonId() const { return anonId; }
323     virtual void dump(TInfoSink &infoSink) const;
324
325 protected:
326     explicit TAnonMember(const TAnonMember&);
327     TAnonMember& operator=(const TAnonMember&);
328
329     const TVariable& anonContainer;
330     unsigned int memberNumber;
331     int anonId;
332 };
333
334 class TSymbolTableLevel {
335 public:
336     POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
337     TSymbolTableLevel() : defaultPrecision(0), anonId(0) { }
338     ~TSymbolTableLevel();
339
340     bool insert(TSymbol& symbol, bool separateNameSpaces)
341     {
342         //
343         // returning true means symbol was added to the table with no semantic errors
344         //
345         const TString& name = symbol.getName();
346         if (name == "") {
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.
350             char buf[20];
351             snprintf(buf, 20, "%s%d", AnonymousPrefix, symbol.getAsVariable()->getAnonId());
352             symbol.changeName(NewPoolTString(buf));
353
354             return insertAnonymousMembers(symbol, 0);
355         } else {
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())
363                     return false;
364
365                 // insert, and whatever happens is okay
366                 level.insert(tLevelPair(insertName, &symbol));
367
368                 return true;
369             } else
370                 return level.insert(tLevelPair(insertName, &symbol)).second;
371         }
372     }
373
374     // Add more members to an already inserted aggregate object
375     bool amend(TSymbol& symbol, int firstNewMember)
376     {
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);
382         else
383             return false;
384     }
385
386     bool insertAnonymousMembers(TSymbol& symbol, int firstMember)
387     {
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)
392                 return false;
393         }
394
395         return true;
396     }
397
398     TSymbol* find(const TString& name) const
399     {
400         tLevel::const_iterator it = level.find(name);
401         if (it == level.end())
402             return 0;
403         else
404             return (*it).second;
405     }
406
407     void findFunctionNameList(const TString& name, TVector<const TFunction*>& list)
408     {
409         size_t parenAt = name.find_first_of('(');
410         TString base(name, 0, parenAt + 1);
411
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());
417     }
418
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
421     {
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)
427
428                 return true;
429         }
430
431         return false;
432     }
433
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
437     {
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
446                     variable = true;
447                     return true;
448                 }
449             } else {
450                 // a mangled name
451                 if (candidateName.compare(0, parenAt, name) == 0) {
452                     // found a function name match
453                     variable = false;
454                     return true;
455                 }
456             }
457         }
458
459         return false;
460     }
461
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)
467     {
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)
471             return;
472
473         defaultPrecision = new TPrecisionQualifier[EbtNumTypes];
474         for (int t = 0; t < EbtNumTypes; ++t)
475             defaultPrecision[t] = p[t];
476     }
477
478     void getPreviousDefaultPrecisions(TPrecisionQualifier *p)
479     {
480         // can be called for table level pops that didn't set the
481         // defaults
482         if (defaultPrecision == 0 || p == 0)
483             return;
484
485         for (int t = 0; t < EbtNumTypes; ++t)
486             p[t] = defaultPrecision[t];
487     }
488
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;
493     void readOnly();
494
495 protected:
496     explicit TSymbolTableLevel(TSymbolTableLevel&);
497     TSymbolTableLevel& operator=(TSymbolTableLevel&);
498
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;
502
503     tLevel level;  // named mappings
504     TPrecisionQualifier *defaultPrecision;
505     int anonId;
506 };
507
508 class TSymbolTable {
509 public:
510     TSymbolTable() : uniqueId(0), noBuiltInRedeclarations(false), separateNameSpaces(false), adoptedLevels(0)
511     {
512         //
513         // This symbol table cannot be used until push() is called.
514         //
515     }
516     ~TSymbolTable()
517     {
518         // this can be called explicitly; safest to code it so it can be called multiple times
519
520         // don't deallocate levels passed in from elsewhere
521         while (table.size() > adoptedLevels)
522             pop(0);
523     }
524
525     void adoptLevels(TSymbolTable& symTable)
526     {
527         for (unsigned int level = 0; level < symTable.table.size(); ++level) {
528             table.push_back(symTable.table[level]);
529             ++adoptedLevels;
530         }
531         uniqueId = symTable.uniqueId;
532         noBuiltInRedeclarations = symTable.noBuiltInRedeclarations;
533         separateNameSpaces = symTable.separateNameSpaces;
534     }
535
536     //
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
543     //
544 protected:
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
549 public:
550     bool isEmpty() { return table.size() == 0; }
551     bool atBuiltInLevel() { return isBuiltInLevel(currentLevel()); }
552     bool atGlobalLevel()  { return isGlobalLevel(currentLevel()); }
553
554     void setNoBuiltInRedeclarations() { noBuiltInRedeclarations = true; }
555     void setSeparateNameSpaces() { separateNameSpaces = true; }
556
557     void push()
558     {
559         table.push_back(new TSymbolTableLevel);
560     }
561
562     void pop(TPrecisionQualifier *p)
563     {
564         table[currentLevel()]->getPreviousDefaultPrecisions(p);
565         delete table.back();
566         table.pop_back();
567     }
568
569     //
570     // Insert a visible symbol into the symbol table so it can
571     // be found later by name.
572     //
573     // Returns false if the was a name collision.
574     //
575     bool insert(TSymbol& symbol)
576     {
577         symbol.setUniqueId(++uniqueId);
578
579         // make sure there isn't a function of this variable name
580         if (! separateNameSpaces && ! symbol.getAsFunction() && table[currentLevel()]->hasFunctionName(symbol.getName()))
581             return false;
582
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()))
587                     return false;
588                 if (currentLevel() > 1 && table[1]->hasFunctionName(symbol.getName()))
589                     return false;
590             }
591         }
592
593         return table[currentLevel()]->insert(symbol, separateNameSpaces);
594     }
595
596     // Add more members to an already inserted aggregate object
597     bool amend(TSymbol& symbol, int firstNewMember)
598     {
599         // See insert() for comments on basic explanation of insert.
600         // This operates similarly, but more simply.
601         return table[currentLevel()]->amend(symbol, firstNewMember);
602     }
603
604     //
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.
609     //
610     void makeInternalVariable(TSymbol& symbol)
611     {
612         symbol.setUniqueId(++uniqueId);
613     }
614
615     //
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.
619     //
620     TSymbol* copyUpDeferredInsert(TSymbol* shared)
621     {
622         if (shared->getAsVariable()) {
623             TSymbol* copy = shared->clone();
624             copy->setUniqueId(shared->getUniqueId());
625             return copy;
626         } else {
627             const TAnonMember* anon = shared->getAsAnonMember();
628             assert(anon);
629             TVariable* container = anon->getAnonContainer().clone();
630             container->changeName(NewPoolTString(""));
631             container->setUniqueId(anon->getAnonContainer().getUniqueId());
632             return container;
633         }
634     }
635
636     TSymbol* copyUp(TSymbol* shared)
637     {
638         TSymbol* copy = copyUpDeferredInsert(shared);
639         table[globalLevel]->insert(*copy, separateNameSpaces);
640         if (shared->getAsVariable())
641             return copy;
642         else {
643             // return the copy of the anonymous member
644             return table[globalLevel]->find(shared->getName());
645         }
646     }
647
648     TSymbol* find(const TString& name, bool* builtIn = 0, bool *currentScope = 0)
649     {
650         int level = currentLevel();
651         TSymbol* symbol;
652         do {
653             symbol = table[level]->find(name);
654             --level;
655         } while (symbol == 0 && level >= 0);
656         level++;
657         if (builtIn)
658             *builtIn = isBuiltInLevel(level);
659         if (currentScope)
660             *currentScope = isGlobalLevel(currentLevel()) || level == currentLevel();  // consider shared levels as "current scope" WRT user globals
661
662         return symbol;
663     }
664
665     bool isFunctionNameVariable(const TString& name) const
666     {
667         if (separateNameSpaces)
668             return false;
669
670         int level = currentLevel();
671         do {
672             bool variable;
673             bool found = table[level]->findFunctionVariableName(name, variable);
674             if (found)
675                 return variable;
676             --level;
677         } while (level >= 0);
678
679         return false;
680     }
681
682     void findFunctionNameList(const TString& name, TVector<const TFunction*>& list, bool& builtIn)
683     {
684         // For user levels, return the set found in the first scope with a match
685         builtIn = false;
686         int level = currentLevel();
687         do {
688             table[level]->findFunctionNameList(name, list);
689             --level;
690         } while (list.empty() && level >= globalLevel);
691
692         if (! list.empty())
693             return;
694
695         // Gather across all built-in levels; they don't hide each other
696         builtIn = true;
697         do {
698             table[level]->findFunctionNameList(name, list);
699             --level;
700         } while (level >= 0);
701     }
702
703     void relateToOperator(const char* name, TOperator op)
704     {
705         for (unsigned int level = 0; level < table.size(); ++level)
706             table[level]->relateToOperator(name, op);
707     }
708
709     void setFunctionExtensions(const char* name, int num, const char* const extensions[])
710     {
711         for (unsigned int level = 0; level < table.size(); ++level)
712             table[level]->setFunctionExtensions(name, num, extensions);
713     }
714
715     void setVariableExtensions(const char* name, int num, const char* const extensions[])
716     {
717         TSymbol* symbol = find(TString(name));
718         if (symbol)
719             symbol->setExtensions(num, extensions);
720     }
721
722     int getMaxSymbolId() { return uniqueId; }
723     void dump(TInfoSink &infoSink) const;
724     void copyTable(const TSymbolTable& copyOf);
725
726     void setPreviousDefaultPrecisions(TPrecisionQualifier *p) { table[currentLevel()]->setPreviousDefaultPrecisions(p); }
727
728     void readOnly()
729     {
730         for (unsigned int level = 0; level < table.size(); ++level)
731             table[level]->readOnly();
732     }
733
734 protected:
735     TSymbolTable(TSymbolTable&);
736     TSymbolTable& operator=(TSymbolTableLevel&);
737
738     int currentLevel() const { return static_cast<int>(table.size()) - 1; }
739
740     std::vector<TSymbolTableLevel*> table;
741     int uniqueId;     // for unique identification in code generation
742     bool noBuiltInRedeclarations;
743     bool separateNameSpaces;
744     unsigned int adoptedLevels;
745 };
746
747 } // end namespace glslang
748
749 #endif // _SYMBOL_TABLE_INCLUDED_