2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // Copyright (C) 2012-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.
38 // Symbol table for parsing. Most functionality 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;
64 case EbtFloat16: mangledName += "f16"; break;
66 case EbtInt: mangledName += 'i'; break;
67 case EbtUint: mangledName += 'u'; break;
68 case EbtInt64: mangledName += "i64"; break;
69 case EbtUint64: mangledName += "u64"; break;
70 case EbtBool: mangledName += 'b'; break;
71 case EbtAtomicUint: mangledName += "au"; break;
73 switch (sampler.type) {
74 case EbtInt: mangledName += "i"; break;
75 case EbtUint: mangledName += "u"; break;
76 default: break; // some compilers want this
79 mangledName += "I"; // a normal image
80 else if (sampler.sampler)
81 mangledName += "p"; // a "pure" sampler
82 else if (!sampler.combined)
83 mangledName += "t"; // a "pure" texture
85 mangledName += "s"; // traditional combined sampler
92 switch (sampler.dim) {
93 case Esd1D: mangledName += "1"; break;
94 case Esd2D: mangledName += "2"; break;
95 case Esd3D: mangledName += "3"; break;
96 case EsdCube: mangledName += "C"; break;
97 case EsdRect: mangledName += "R2"; break;
98 case EsdBuffer: mangledName += "B"; break;
99 case EsdSubpass: mangledName += "P"; break;
100 default: break; // some compilers want this
106 mangledName += "struct-";
108 mangledName += *typeName;
109 for (unsigned int i = 0; i < structure->size(); ++i) {
111 (*structure)[i].type->buildMangledName(mangledName);
117 if (getVectorSize() > 0)
118 mangledName += static_cast<char>('0' + getVectorSize());
120 mangledName += static_cast<char>('0' + getMatrixCols());
121 mangledName += static_cast<char>('0' + getMatrixRows());
125 const int maxSize = 11;
127 for (int i = 0; i < arraySizes->getNumDims(); ++i) {
128 if (arraySizes->getDimNode(i)) {
129 if (arraySizes->getDimNode(i)->getAsSymbolNode())
130 snprintf(buf, maxSize, "s%d", arraySizes->getDimNode(i)->getAsSymbolNode()->getId());
132 snprintf(buf, maxSize, "s%p", arraySizes->getDimNode(i));
134 snprintf(buf, maxSize, "%d", arraySizes->getDimSize(i));
146 void TVariable::dump(TInfoSink& infoSink) const
148 infoSink.debug << getName().c_str() << ": " << type.getStorageQualifierString() << " " << type.getBasicTypeString();
149 if (type.isArray()) {
150 infoSink.debug << "[0]";
152 infoSink.debug << "\n";
155 void TFunction::dump(TInfoSink& infoSink) const
157 infoSink.debug << getName().c_str() << ": " << returnType.getBasicTypeString() << " " << getMangledName().c_str() << "\n";
160 void TAnonMember::dump(TInfoSink& TInfoSink) const
162 TInfoSink.debug << "anonymous member " << getMemberNumber() << " of " << getAnonContainer().getName().c_str() << "\n";
165 void TSymbolTableLevel::dump(TInfoSink &infoSink) const
167 tLevel::const_iterator it;
168 for (it = level.begin(); it != level.end(); ++it)
169 (*it).second->dump(infoSink);
172 void TSymbolTable::dump(TInfoSink &infoSink) const
174 for (int level = currentLevel(); level >= 0; --level) {
175 infoSink.debug << "LEVEL " << level << "\n";
176 table[level]->dump(infoSink);
181 // Functions have buried pointers to delete.
183 TFunction::~TFunction()
185 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
190 // Symbol table levels are a map of pointers to symbols that have to be deleted.
192 TSymbolTableLevel::~TSymbolTableLevel()
194 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
197 delete [] defaultPrecision;
201 // Change all function entries in the table with the non-mangled name
202 // to be related to the provided built-in operation.
204 void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
206 tLevel::const_iterator candidate = level.lower_bound(name);
207 while (candidate != level.end()) {
208 const TString& candidateName = (*candidate).first;
209 TString::size_type parenAt = candidateName.find_first_of('(');
210 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
211 TFunction* function = (*candidate).second->getAsFunction();
212 function->relateToOperator(op);
219 // Make all function overloads of the given name require an extension(s).
220 // Should only be used for a version/profile that actually needs the extension(s).
221 void TSymbolTableLevel::setFunctionExtensions(const char* name, int num, const char* const extensions[])
223 tLevel::const_iterator candidate = level.lower_bound(name);
224 while (candidate != level.end()) {
225 const TString& candidateName = (*candidate).first;
226 TString::size_type parenAt = candidateName.find_first_of('(');
227 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
228 TSymbol* symbol = candidate->second;
229 symbol->setExtensions(num, extensions);
237 // Make all symbols in this table level read only.
239 void TSymbolTableLevel::readOnly()
241 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
242 (*it).second->makeReadOnly();
246 // Copy a symbol, but the copy is writable; call readOnly() afterward if that's not desired.
248 TSymbol::TSymbol(const TSymbol& copyOf)
250 name = NewPoolTString(copyOf.name->c_str());
251 uniqueId = copyOf.uniqueId;
255 TVariable::TVariable(const TVariable& copyOf) : TSymbol(copyOf)
257 type.deepCopy(copyOf.type);
258 userType = copyOf.userType;
261 if (copyOf.numExtensions != 0)
262 setExtensions(copyOf.numExtensions, copyOf.extensions);
264 if (! copyOf.constArray.empty()) {
265 assert(! copyOf.type.isStruct());
266 TConstUnionArray newArray(copyOf.constArray, 0, copyOf.constArray.size());
267 constArray = newArray;
270 // don't support specialization-constant subtrees in cloned tables
271 constSubtree = nullptr;
274 TVariable* TVariable::clone() const
276 TVariable *variable = new TVariable(*this);
281 TFunction::TFunction(const TFunction& copyOf) : TSymbol(copyOf)
283 for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
285 parameters.push_back(param);
286 parameters.back().copyParam(copyOf.parameters[i]);
291 if (copyOf.extensions != 0)
292 setExtensions(copyOf.numExtensions, copyOf.extensions);
293 returnType.deepCopy(copyOf.returnType);
294 mangledName = copyOf.mangledName;
296 defined = copyOf.defined;
297 prototyped = copyOf.prototyped;
298 defaultParamCount = copyOf.defaultParamCount;
301 TFunction* TFunction::clone() const
303 TFunction *function = new TFunction(*this);
308 TAnonMember* TAnonMember::clone() const
310 // Anonymous members of a given block should be cloned at a higher level,
311 // where they can all be assured to still end up pointing to a single
312 // copy of the original container.
318 TSymbolTableLevel* TSymbolTableLevel::clone() const
320 TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
321 symTableLevel->anonId = anonId;
322 std::vector<bool> containerCopied(anonId, false);
323 tLevel::const_iterator iter;
324 for (iter = level.begin(); iter != level.end(); ++iter) {
325 const TAnonMember* anon = iter->second->getAsAnonMember();
327 // Insert all the anonymous members of this same container at once,
328 // avoid inserting the other members in the future, once this has been done,
329 // allowing them to all be part of the same new container.
330 if (! containerCopied[anon->getAnonId()]) {
331 TVariable* container = anon->getAnonContainer().clone();
332 container->changeName(NewPoolTString(""));
333 // insert the whole container
334 symTableLevel->insert(*container, false);
335 containerCopied[anon->getAnonId()] = true;
338 symTableLevel->insert(*iter->second->clone(), false);
341 return symTableLevel;
344 void TSymbolTable::copyTable(const TSymbolTable& copyOf)
346 assert(adoptedLevels == copyOf.adoptedLevels);
348 uniqueId = copyOf.uniqueId;
349 noBuiltInRedeclarations = copyOf.noBuiltInRedeclarations;
350 separateNameSpaces = copyOf.separateNameSpaces;
351 for (unsigned int i = copyOf.adoptedLevels; i < copyOf.table.size(); ++i)
352 table.push_back(copyOf.table[i]->clone());
355 } // end namespace glslang