2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // Copyright (C) 2012-2013 LunarG, Inc.
4 // Copyright (C) 2017 ARM Limited.
6 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
12 // Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
15 // Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following
17 // disclaimer in the documentation and/or other materials provided
18 // with the distribution.
20 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
21 // contributors may be used to endorse or promote products derived
22 // from this software without specific prior written permission.
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 // POSSIBILITY OF SUCH DAMAGE.
39 // Symbol table for parsing. Most functionality and main ideas
40 // are documented in the header file.
43 #include "SymbolTable.h"
48 // TType helper function needs a place to live.
52 // Recursively generate mangled names.
54 void TType::buildMangledName(TString& mangledName) const
62 case EbtFloat: mangledName += 'f'; break;
63 case EbtDouble: mangledName += 'd'; break;
64 case EbtFloat16: mangledName += "f16"; break;
65 case EbtInt: mangledName += 'i'; break;
66 case EbtUint: mangledName += 'u'; break;
67 case EbtInt8: mangledName += "i8"; break;
68 case EbtUint8: mangledName += "u8"; break;
69 case EbtInt16: mangledName += "i16"; break;
70 case EbtUint16: mangledName += "u16"; break;
71 case EbtInt64: mangledName += "i64"; break;
72 case EbtUint64: mangledName += "u64"; break;
73 case EbtBool: mangledName += 'b'; break;
74 case EbtAtomicUint: mangledName += "au"; break;
76 case EbtAccStructNV: mangledName += "asnv"; break;
79 switch (sampler.type) {
81 case EbtFloat16: mangledName += "f16"; break;
83 case EbtInt: mangledName += "i"; break;
84 case EbtUint: mangledName += "u"; break;
85 default: break; // some compilers want this
88 mangledName += "I"; // a normal image
89 else if (sampler.sampler)
90 mangledName += "p"; // a "pure" sampler
91 else if (!sampler.combined)
92 mangledName += "t"; // a "pure" texture
94 mangledName += "s"; // traditional combined sampler
101 switch (sampler.dim) {
102 case Esd1D: mangledName += "1"; break;
103 case Esd2D: mangledName += "2"; break;
104 case Esd3D: mangledName += "3"; break;
105 case EsdCube: mangledName += "C"; break;
106 case EsdRect: mangledName += "R2"; break;
107 case EsdBuffer: mangledName += "B"; break;
108 case EsdSubpass: mangledName += "P"; break;
109 default: break; // some compilers want this
112 if (sampler.hasReturnStruct()) {
113 // Name mangle for sampler return struct uses struct table index.
114 mangledName += "-tx-struct";
116 char text[16]; // plenty enough space for the small integers.
117 snprintf(text, sizeof(text), "%d-", sampler.structReturnIndex);
120 switch (sampler.getVectorSize()) {
121 case 1: mangledName += "1"; break;
122 case 2: mangledName += "2"; break;
123 case 3: mangledName += "3"; break;
124 case 4: break; // default to prior name mangle behavior
133 if (basicType == EbtStruct)
134 mangledName += "struct-";
136 mangledName += "block-";
138 mangledName += *typeName;
139 for (unsigned int i = 0; i < structure->size(); ++i) {
141 (*structure)[i].type->buildMangledName(mangledName);
147 if (getVectorSize() > 0)
148 mangledName += static_cast<char>('0' + getVectorSize());
150 mangledName += static_cast<char>('0' + getMatrixCols());
151 mangledName += static_cast<char>('0' + getMatrixRows());
155 const int maxSize = 11;
157 for (int i = 0; i < arraySizes->getNumDims(); ++i) {
158 if (arraySizes->getDimNode(i)) {
159 if (arraySizes->getDimNode(i)->getAsSymbolNode())
160 snprintf(buf, maxSize, "s%d", arraySizes->getDimNode(i)->getAsSymbolNode()->getId());
162 snprintf(buf, maxSize, "s%p", arraySizes->getDimNode(i));
164 snprintf(buf, maxSize, "%d", arraySizes->getDimSize(i));
176 void TVariable::dump(TInfoSink& infoSink) const
178 infoSink.debug << getName().c_str() << ": " << type.getStorageQualifierString() << " " << type.getBasicTypeString();
179 if (type.isArray()) {
180 infoSink.debug << "[0]";
182 infoSink.debug << "\n";
185 void TFunction::dump(TInfoSink& infoSink) const
187 infoSink.debug << getName().c_str() << ": " << returnType.getBasicTypeString() << " " << getMangledName().c_str() << "\n";
190 void TAnonMember::dump(TInfoSink& TInfoSink) const
192 TInfoSink.debug << "anonymous member " << getMemberNumber() << " of " << getAnonContainer().getName().c_str() << "\n";
195 void TSymbolTableLevel::dump(TInfoSink &infoSink) const
197 tLevel::const_iterator it;
198 for (it = level.begin(); it != level.end(); ++it)
199 (*it).second->dump(infoSink);
202 void TSymbolTable::dump(TInfoSink &infoSink) const
204 for (int level = currentLevel(); level >= 0; --level) {
205 infoSink.debug << "LEVEL " << level << "\n";
206 table[level]->dump(infoSink);
211 // Functions have buried pointers to delete.
213 TFunction::~TFunction()
215 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
220 // Symbol table levels are a map of pointers to symbols that have to be deleted.
222 TSymbolTableLevel::~TSymbolTableLevel()
224 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
227 delete [] defaultPrecision;
231 // Change all function entries in the table with the non-mangled name
232 // to be related to the provided built-in operation.
234 void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
236 tLevel::const_iterator candidate = level.lower_bound(name);
237 while (candidate != level.end()) {
238 const TString& candidateName = (*candidate).first;
239 TString::size_type parenAt = candidateName.find_first_of('(');
240 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
241 TFunction* function = (*candidate).second->getAsFunction();
242 function->relateToOperator(op);
249 // Make all function overloads of the given name require an extension(s).
250 // Should only be used for a version/profile that actually needs the extension(s).
251 void TSymbolTableLevel::setFunctionExtensions(const char* name, int num, const char* const extensions[])
253 tLevel::const_iterator candidate = level.lower_bound(name);
254 while (candidate != level.end()) {
255 const TString& candidateName = (*candidate).first;
256 TString::size_type parenAt = candidateName.find_first_of('(');
257 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
258 TSymbol* symbol = candidate->second;
259 symbol->setExtensions(num, extensions);
267 // Make all symbols in this table level read only.
269 void TSymbolTableLevel::readOnly()
271 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
272 (*it).second->makeReadOnly();
276 // Copy a symbol, but the copy is writable; call readOnly() afterward if that's not desired.
278 TSymbol::TSymbol(const TSymbol& copyOf)
280 name = NewPoolTString(copyOf.name->c_str());
281 uniqueId = copyOf.uniqueId;
285 TVariable::TVariable(const TVariable& copyOf) : TSymbol(copyOf)
287 type.deepCopy(copyOf.type);
288 userType = copyOf.userType;
291 if (copyOf.numExtensions != 0)
292 setExtensions(copyOf.numExtensions, copyOf.extensions);
294 if (! copyOf.constArray.empty()) {
295 assert(! copyOf.type.isStruct());
296 TConstUnionArray newArray(copyOf.constArray, 0, copyOf.constArray.size());
297 constArray = newArray;
300 // don't support specialization-constant subtrees in cloned tables
301 constSubtree = nullptr;
304 TVariable* TVariable::clone() const
306 TVariable *variable = new TVariable(*this);
311 TFunction::TFunction(const TFunction& copyOf) : TSymbol(copyOf)
313 for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
315 parameters.push_back(param);
316 parameters.back().copyParam(copyOf.parameters[i]);
321 if (copyOf.extensions != 0)
322 setExtensions(copyOf.numExtensions, copyOf.extensions);
323 returnType.deepCopy(copyOf.returnType);
324 mangledName = copyOf.mangledName;
326 defined = copyOf.defined;
327 prototyped = copyOf.prototyped;
328 implicitThis = copyOf.implicitThis;
329 illegalImplicitThis = copyOf.illegalImplicitThis;
330 defaultParamCount = copyOf.defaultParamCount;
333 TFunction* TFunction::clone() const
335 TFunction *function = new TFunction(*this);
340 TAnonMember* TAnonMember::clone() const
342 // Anonymous members of a given block should be cloned at a higher level,
343 // where they can all be assured to still end up pointing to a single
344 // copy of the original container.
350 TSymbolTableLevel* TSymbolTableLevel::clone() const
352 TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
353 symTableLevel->anonId = anonId;
354 symTableLevel->thisLevel = thisLevel;
355 std::vector<bool> containerCopied(anonId, false);
356 tLevel::const_iterator iter;
357 for (iter = level.begin(); iter != level.end(); ++iter) {
358 const TAnonMember* anon = iter->second->getAsAnonMember();
360 // Insert all the anonymous members of this same container at once,
361 // avoid inserting the other members in the future, once this has been done,
362 // allowing them to all be part of the same new container.
363 if (! containerCopied[anon->getAnonId()]) {
364 TVariable* container = anon->getAnonContainer().clone();
365 container->changeName(NewPoolTString(""));
366 // insert the whole container
367 symTableLevel->insert(*container, false);
368 containerCopied[anon->getAnonId()] = true;
371 symTableLevel->insert(*iter->second->clone(), false);
374 return symTableLevel;
377 void TSymbolTable::copyTable(const TSymbolTable& copyOf)
379 assert(adoptedLevels == copyOf.adoptedLevels);
381 uniqueId = copyOf.uniqueId;
382 noBuiltInRedeclarations = copyOf.noBuiltInRedeclarations;
383 separateNameSpaces = copyOf.separateNameSpaces;
384 for (unsigned int i = copyOf.adoptedLevels; i < copyOf.table.size(); ++i)
385 table.push_back(copyOf.table[i]->clone());
388 } // end namespace glslang