2 //Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 //Copyright (C) 2012-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.
38 // Symbol table for parsing. Most functionaliy and main ideas
39 // are documented in the header file.
42 #include "SymbolTable.h"
47 // TType helper function needs a place to live.
51 // Recursively generate mangled names.
53 void TType::buildMangledName(TString& mangledName)
61 case EbtFloat: mangledName += 'f'; break;
62 case EbtDouble: mangledName += 'd'; break;
63 case EbtInt: mangledName += 'i'; break;
64 case EbtUint: mangledName += 'u'; break;
65 case EbtInt64: mangledName += "i64"; break;
66 case EbtUint64: mangledName += "u64"; break;
67 case EbtBool: mangledName += 'b'; break;
68 case EbtAtomicUint: mangledName += "au"; break;
70 switch (sampler.type) {
71 case EbtInt: mangledName += "i"; break;
72 case EbtUint: mangledName += "u"; break;
73 default: break; // some compilers want this
76 mangledName += "I"; // a normal image
77 else if (sampler.sampler)
78 mangledName += "p"; // a "pure" sampler
79 else if (!sampler.combined)
80 mangledName += "t"; // a "pure" texture
82 mangledName += "s"; // traditional combined sampler
89 switch (sampler.dim) {
90 case Esd1D: mangledName += "1"; break;
91 case Esd2D: mangledName += "2"; break;
92 case Esd3D: mangledName += "3"; break;
93 case EsdCube: mangledName += "C"; break;
94 case EsdRect: mangledName += "R2"; break;
95 case EsdBuffer: mangledName += "B"; break;
96 case EsdSubpass: mangledName += "P"; break;
97 default: break; // some compilers want this
103 mangledName += "struct-";
105 mangledName += *typeName;
106 for (unsigned int i = 0; i < structure->size(); ++i) {
108 (*structure)[i].type->buildMangledName(mangledName);
114 if (getVectorSize() > 0)
115 mangledName += static_cast<char>('0' + getVectorSize());
117 mangledName += static_cast<char>('0' + getMatrixCols());
118 mangledName += static_cast<char>('0' + getMatrixRows());
122 const int maxSize = 11;
124 for (int i = 0; i < arraySizes->getNumDims(); ++i) {
125 if (arraySizes->getDimNode(i)) {
126 if (arraySizes->getDimNode(i)->getAsSymbolNode())
127 snprintf(buf, maxSize, "s%d", arraySizes->getDimNode(i)->getAsSymbolNode()->getId());
129 snprintf(buf, maxSize, "s%p", arraySizes->getDimNode(i));
131 snprintf(buf, maxSize, "%d", arraySizes->getDimSize(i));
143 void TVariable::dump(TInfoSink& infoSink) const
145 infoSink.debug << getName().c_str() << ": " << type.getStorageQualifierString() << " " << type.getBasicTypeString();
146 if (type.isArray()) {
147 infoSink.debug << "[0]";
149 infoSink.debug << "\n";
152 void TFunction::dump(TInfoSink& infoSink) const
154 infoSink.debug << getName().c_str() << ": " << returnType.getBasicTypeString() << " " << getMangledName().c_str() << "\n";
157 void TAnonMember::dump(TInfoSink& TInfoSink) const
159 TInfoSink.debug << "anonymous member " << getMemberNumber() << " of " << getAnonContainer().getName().c_str() << "\n";
162 void TSymbolTableLevel::dump(TInfoSink &infoSink) const
164 tLevel::const_iterator it;
165 for (it = level.begin(); it != level.end(); ++it)
166 (*it).second->dump(infoSink);
169 void TSymbolTable::dump(TInfoSink &infoSink) const
171 for (int level = currentLevel(); level >= 0; --level) {
172 infoSink.debug << "LEVEL " << level << "\n";
173 table[level]->dump(infoSink);
178 // Functions have buried pointers to delete.
180 TFunction::~TFunction()
182 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
187 // Symbol table levels are a map of pointers to symbols that have to be deleted.
189 TSymbolTableLevel::~TSymbolTableLevel()
191 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
194 delete [] defaultPrecision;
198 // Change all function entries in the table with the non-mangled name
199 // to be related to the provided built-in operation.
201 void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
203 tLevel::const_iterator candidate = level.lower_bound(name);
204 while (candidate != level.end()) {
205 const TString& candidateName = (*candidate).first;
206 TString::size_type parenAt = candidateName.find_first_of('(');
207 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
208 TFunction* function = (*candidate).second->getAsFunction();
209 function->relateToOperator(op);
216 // Make all function overloads of the given name require an extension(s).
217 // Should only be used for a version/profile that actually needs the extension(s).
218 void TSymbolTableLevel::setFunctionExtensions(const char* name, int num, const char* const extensions[])
220 tLevel::const_iterator candidate = level.lower_bound(name);
221 while (candidate != level.end()) {
222 const TString& candidateName = (*candidate).first;
223 TString::size_type parenAt = candidateName.find_first_of('(');
224 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
225 TSymbol* symbol = candidate->second;
226 symbol->setExtensions(num, extensions);
234 // Make all symbols in this table level read only.
236 void TSymbolTableLevel::readOnly()
238 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
239 (*it).second->makeReadOnly();
243 // Copy a symbol, but the copy is writable; call readOnly() afterward if that's not desired.
245 TSymbol::TSymbol(const TSymbol& copyOf)
247 name = NewPoolTString(copyOf.name->c_str());
248 uniqueId = copyOf.uniqueId;
252 TVariable::TVariable(const TVariable& copyOf) : TSymbol(copyOf)
254 type.deepCopy(copyOf.type);
255 userType = copyOf.userType;
258 if (copyOf.numExtensions != 0)
259 setExtensions(copyOf.numExtensions, copyOf.extensions);
261 if (! copyOf.constArray.empty()) {
262 assert(! copyOf.type.isStruct());
263 TConstUnionArray newArray(copyOf.constArray, 0, copyOf.constArray.size());
264 constArray = newArray;
267 // don't support specialization-constant subtrees in cloned tables
268 constSubtree = nullptr;
271 TVariable* TVariable::clone() const
273 TVariable *variable = new TVariable(*this);
278 TFunction::TFunction(const TFunction& copyOf) : TSymbol(copyOf)
280 for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
282 parameters.push_back(param);
283 parameters.back().copyParam(copyOf.parameters[i]);
288 if (copyOf.extensions != 0)
289 setExtensions(copyOf.numExtensions, copyOf.extensions);
290 returnType.deepCopy(copyOf.returnType);
291 mangledName = copyOf.mangledName;
293 defined = copyOf.defined;
294 prototyped = copyOf.prototyped;
297 TFunction* TFunction::clone() const
299 TFunction *function = new TFunction(*this);
304 TAnonMember* TAnonMember::clone() const
306 // Anonymous members of a given block should be cloned at a higher level,
307 // where they can all be assured to still end up pointing to a single
308 // copy of the original container.
314 TSymbolTableLevel* TSymbolTableLevel::clone() const
316 TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
317 symTableLevel->anonId = anonId;
318 std::vector<bool> containerCopied(anonId, false);
319 tLevel::const_iterator iter;
320 for (iter = level.begin(); iter != level.end(); ++iter) {
321 const TAnonMember* anon = iter->second->getAsAnonMember();
323 // Insert all the anonymous members of this same container at once,
324 // avoid inserting the other members in the future, once this has been done,
325 // allowing them to all be part of the same new container.
326 if (! containerCopied[anon->getAnonId()]) {
327 TVariable* container = anon->getAnonContainer().clone();
328 container->changeName(NewPoolTString(""));
329 // insert the whole container
330 symTableLevel->insert(*container, false);
331 containerCopied[anon->getAnonId()] = true;
334 symTableLevel->insert(*iter->second->clone(), false);
337 return symTableLevel;
340 void TSymbolTable::copyTable(const TSymbolTable& copyOf)
342 assert(adoptedLevels == copyOf.adoptedLevels);
344 uniqueId = copyOf.uniqueId;
345 noBuiltInRedeclarations = copyOf.noBuiltInRedeclarations;
346 separateNameSpaces = copyOf.separateNameSpaces;
347 for (unsigned int i = copyOf.adoptedLevels; i < copyOf.table.size(); ++i)
348 table.push_back(copyOf.table[i]->clone());
351 } // end namespace glslang