2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // Copyright (C) 2012-2013 LunarG, Inc.
4 // Copyright (C) 2017 ARM Limited.
5 // Copyright (C) 2015-2018 Google, Inc.
7 // All rights reserved.
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions
13 // Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
16 // Redistributions in binary form must reproduce the above
17 // copyright notice, this list of conditions and the following
18 // disclaimer in the documentation and/or other materials provided
19 // with the distribution.
21 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
22 // contributors may be used to endorse or promote products derived
23 // from this software without specific prior written permission.
25 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 // POSSIBILITY OF SUCH DAMAGE.
40 // Symbol table for parsing. Most functionality and main ideas
41 // are documented in the header file.
44 #include "SymbolTable.h"
49 // TType helper function needs a place to live.
53 // Recursively generate mangled names.
55 void TType::buildMangledName(TString& mangledName) const
63 case EbtFloat: mangledName += 'f'; break;
64 case EbtDouble: mangledName += 'd'; break;
65 case EbtFloat16: mangledName += "f16"; break;
66 case EbtInt: mangledName += 'i'; break;
67 case EbtUint: mangledName += 'u'; break;
68 case EbtInt8: mangledName += "i8"; break;
69 case EbtUint8: mangledName += "u8"; break;
70 case EbtInt16: mangledName += "i16"; break;
71 case EbtUint16: mangledName += "u16"; break;
72 case EbtInt64: mangledName += "i64"; break;
73 case EbtUint64: mangledName += "u64"; break;
74 case EbtBool: mangledName += 'b'; break;
75 case EbtAtomicUint: mangledName += "au"; break;
77 case EbtAccStructNV: mangledName += "asnv"; break;
80 switch (sampler.type) {
82 case EbtFloat16: mangledName += "f16"; break;
84 case EbtInt: mangledName += "i"; break;
85 case EbtUint: mangledName += "u"; break;
86 default: break; // some compilers want this
89 mangledName += "I"; // a normal image
90 else if (sampler.sampler)
91 mangledName += "p"; // a "pure" sampler
92 else if (!sampler.combined)
93 mangledName += "t"; // a "pure" texture
95 mangledName += "s"; // traditional combined sampler
100 if (sampler.external)
104 switch (sampler.dim) {
105 case Esd1D: mangledName += "1"; break;
106 case Esd2D: mangledName += "2"; break;
107 case Esd3D: mangledName += "3"; break;
108 case EsdCube: mangledName += "C"; break;
109 case EsdRect: mangledName += "R2"; break;
110 case EsdBuffer: mangledName += "B"; break;
111 case EsdSubpass: mangledName += "P"; break;
112 default: break; // some compilers want this
115 if (sampler.hasReturnStruct()) {
116 // Name mangle for sampler return struct uses struct table index.
117 mangledName += "-tx-struct";
119 char text[16]; // plenty enough space for the small integers.
120 snprintf(text, sizeof(text), "%d-", sampler.structReturnIndex);
123 switch (sampler.getVectorSize()) {
124 case 1: mangledName += "1"; break;
125 case 2: mangledName += "2"; break;
126 case 3: mangledName += "3"; break;
127 case 4: break; // default to prior name mangle behavior
136 if (basicType == EbtStruct)
137 mangledName += "struct-";
139 mangledName += "block-";
141 mangledName += *typeName;
142 for (unsigned int i = 0; i < structure->size(); ++i) {
144 (*structure)[i].type->buildMangledName(mangledName);
150 if (getVectorSize() > 0)
151 mangledName += static_cast<char>('0' + getVectorSize());
153 mangledName += static_cast<char>('0' + getMatrixCols());
154 mangledName += static_cast<char>('0' + getMatrixRows());
158 const int maxSize = 11;
160 for (int i = 0; i < arraySizes->getNumDims(); ++i) {
161 if (arraySizes->getDimNode(i)) {
162 if (arraySizes->getDimNode(i)->getAsSymbolNode())
163 snprintf(buf, maxSize, "s%d", arraySizes->getDimNode(i)->getAsSymbolNode()->getId());
165 snprintf(buf, maxSize, "s%p", arraySizes->getDimNode(i));
167 snprintf(buf, maxSize, "%d", arraySizes->getDimSize(i));
179 void TSymbol::dumpExtensions(TInfoSink &infoSink) const
181 int numExtensions = getNumExtensions();
184 infoSink.debug << " <";
185 for (int i = 0; i < numExtensions; i++)
187 infoSink.debug << getExtensions()[i] << ",";
189 infoSink.debug << ">";
193 void TVariable::dump(TInfoSink &infoSink, bool complete) const
197 infoSink.debug << getName().c_str() << ": " << type.getCompleteString();
198 dumpExtensions(infoSink);
201 infoSink.debug << getName().c_str() << ": " << type.getStorageQualifierString() << " "
202 << type.getBasicTypeString();
205 infoSink.debug << "[0]";
209 infoSink.debug << "\n";
212 void TFunction::dump(TInfoSink &infoSink, bool complete) const
216 infoSink.debug << getName().c_str() << ": " << returnType.getCompleteString() << " " << getName().c_str() << "(";
217 int numParams = getParamCount();
218 for (int i = 0; i < numParams; i++){
219 const TParameter& param = parameters[i];
220 infoSink.debug << param.type->getCompleteString() << " " << (param.name ? param.name->c_str() : "") << (i < numParams-1 ? "," : "");
222 infoSink.debug << ")";
223 dumpExtensions(infoSink);
226 infoSink.debug << getName().c_str() << ": " << returnType.getBasicTypeString() << " "
227 << getMangledName().c_str() << "n";
230 infoSink.debug << "\n";
233 void TAnonMember::dump(TInfoSink &TInfoSink, bool complete) const
235 TInfoSink.debug << "anonymous member " << getMemberNumber() << " of " << getAnonContainer().getName().c_str()
239 void TSymbolTableLevel::dump(TInfoSink &infoSink, bool complete) const
241 tLevel::const_iterator it;
242 for (it = level.begin(); it != level.end(); ++it)
243 (*it).second->dump(infoSink, complete);
246 void TSymbolTable::dump(TInfoSink &infoSink, bool complete) const
248 for (int level = currentLevel(); level >= 0; --level) {
249 infoSink.debug << "LEVEL " << level << "\n";
250 table[level]->dump(infoSink, complete);
255 // Functions have buried pointers to delete.
257 TFunction::~TFunction()
259 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
264 // Symbol table levels are a map of pointers to symbols that have to be deleted.
266 TSymbolTableLevel::~TSymbolTableLevel()
268 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
271 delete [] defaultPrecision;
275 // Change all function entries in the table with the non-mangled name
276 // to be related to the provided built-in operation.
278 void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
280 tLevel::const_iterator candidate = level.lower_bound(name);
281 while (candidate != level.end()) {
282 const TString& candidateName = (*candidate).first;
283 TString::size_type parenAt = candidateName.find_first_of('(');
284 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
285 TFunction* function = (*candidate).second->getAsFunction();
286 function->relateToOperator(op);
293 // Make all function overloads of the given name require an extension(s).
294 // Should only be used for a version/profile that actually needs the extension(s).
295 void TSymbolTableLevel::setFunctionExtensions(const char* name, int num, const char* const extensions[])
297 tLevel::const_iterator candidate = level.lower_bound(name);
298 while (candidate != level.end()) {
299 const TString& candidateName = (*candidate).first;
300 TString::size_type parenAt = candidateName.find_first_of('(');
301 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
302 TSymbol* symbol = candidate->second;
303 symbol->setExtensions(num, extensions);
311 // Make all symbols in this table level read only.
313 void TSymbolTableLevel::readOnly()
315 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
316 (*it).second->makeReadOnly();
320 // Copy a symbol, but the copy is writable; call readOnly() afterward if that's not desired.
322 TSymbol::TSymbol(const TSymbol& copyOf)
324 name = NewPoolTString(copyOf.name->c_str());
325 uniqueId = copyOf.uniqueId;
329 TVariable::TVariable(const TVariable& copyOf) : TSymbol(copyOf)
331 type.deepCopy(copyOf.type);
332 userType = copyOf.userType;
334 // we don't support specialization-constant subtrees in cloned tables, only extensions
335 constSubtree = nullptr;
336 extensions = nullptr;
337 memberExtensions = nullptr;
338 if (copyOf.getNumExtensions() > 0)
339 setExtensions(copyOf.getNumExtensions(), copyOf.getExtensions());
340 if (copyOf.hasMemberExtensions()) {
341 for (int m = 0; m < (int)copyOf.type.getStruct()->size(); ++m) {
342 if (copyOf.getNumMemberExtensions(m) > 0)
343 setMemberExtensions(m, copyOf.getNumMemberExtensions(m), copyOf.getMemberExtensions(m));
347 if (! copyOf.constArray.empty()) {
348 assert(! copyOf.type.isStruct());
349 TConstUnionArray newArray(copyOf.constArray, 0, copyOf.constArray.size());
350 constArray = newArray;
354 TVariable* TVariable::clone() const
356 TVariable *variable = new TVariable(*this);
361 TFunction::TFunction(const TFunction& copyOf) : TSymbol(copyOf)
363 for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
365 parameters.push_back(param);
366 parameters.back().copyParam(copyOf.parameters[i]);
369 extensions = nullptr;
370 if (copyOf.getNumExtensions() > 0)
371 setExtensions(copyOf.getNumExtensions(), copyOf.getExtensions());
372 returnType.deepCopy(copyOf.returnType);
373 mangledName = copyOf.mangledName;
375 defined = copyOf.defined;
376 prototyped = copyOf.prototyped;
377 implicitThis = copyOf.implicitThis;
378 illegalImplicitThis = copyOf.illegalImplicitThis;
379 defaultParamCount = copyOf.defaultParamCount;
382 TFunction* TFunction::clone() const
384 TFunction *function = new TFunction(*this);
389 TAnonMember* TAnonMember::clone() const
391 // Anonymous members of a given block should be cloned at a higher level,
392 // where they can all be assured to still end up pointing to a single
393 // copy of the original container.
399 TSymbolTableLevel* TSymbolTableLevel::clone() const
401 TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
402 symTableLevel->anonId = anonId;
403 symTableLevel->thisLevel = thisLevel;
404 std::vector<bool> containerCopied(anonId, false);
405 tLevel::const_iterator iter;
406 for (iter = level.begin(); iter != level.end(); ++iter) {
407 const TAnonMember* anon = iter->second->getAsAnonMember();
409 // Insert all the anonymous members of this same container at once,
410 // avoid inserting the remaining members in the future, once this has been done,
411 // allowing them to all be part of the same new container.
412 if (! containerCopied[anon->getAnonId()]) {
413 TVariable* container = anon->getAnonContainer().clone();
414 container->changeName(NewPoolTString(""));
415 // insert the container and all its members
416 symTableLevel->insert(*container, false);
417 containerCopied[anon->getAnonId()] = true;
420 symTableLevel->insert(*iter->second->clone(), false);
423 return symTableLevel;
426 void TSymbolTable::copyTable(const TSymbolTable& copyOf)
428 assert(adoptedLevels == copyOf.adoptedLevels);
430 uniqueId = copyOf.uniqueId;
431 noBuiltInRedeclarations = copyOf.noBuiltInRedeclarations;
432 separateNameSpaces = copyOf.separateNameSpaces;
433 for (unsigned int i = copyOf.adoptedLevels; i < copyOf.table.size(); ++i)
434 table.push_back(copyOf.table[i]->clone());
437 } // end namespace glslang