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 switch (sampler.type) {
78 case EbtFloat16: mangledName += "f16"; break;
80 case EbtInt: mangledName += "i"; break;
81 case EbtUint: mangledName += "u"; break;
82 default: break; // some compilers want this
85 mangledName += "I"; // a normal image
86 else if (sampler.sampler)
87 mangledName += "p"; // a "pure" sampler
88 else if (!sampler.combined)
89 mangledName += "t"; // a "pure" texture
91 mangledName += "s"; // traditional combined sampler
98 switch (sampler.dim) {
99 case Esd1D: mangledName += "1"; break;
100 case Esd2D: mangledName += "2"; break;
101 case Esd3D: mangledName += "3"; break;
102 case EsdCube: mangledName += "C"; break;
103 case EsdRect: mangledName += "R2"; break;
104 case EsdBuffer: mangledName += "B"; break;
105 case EsdSubpass: mangledName += "P"; break;
106 default: break; // some compilers want this
109 if (sampler.hasReturnStruct()) {
110 // Name mangle for sampler return struct uses struct table index.
111 mangledName += "-tx-struct";
113 char text[16]; // plenty enough space for the small integers.
114 snprintf(text, sizeof(text), "%d-", sampler.structReturnIndex);
117 switch (sampler.getVectorSize()) {
118 case 1: mangledName += "1"; break;
119 case 2: mangledName += "2"; break;
120 case 3: mangledName += "3"; break;
121 case 4: break; // default to prior name mangle behavior
130 if (basicType == EbtStruct)
131 mangledName += "struct-";
133 mangledName += "block-";
135 mangledName += *typeName;
136 for (unsigned int i = 0; i < structure->size(); ++i) {
138 (*structure)[i].type->buildMangledName(mangledName);
144 if (getVectorSize() > 0)
145 mangledName += static_cast<char>('0' + getVectorSize());
147 mangledName += static_cast<char>('0' + getMatrixCols());
148 mangledName += static_cast<char>('0' + getMatrixRows());
152 const int maxSize = 11;
154 for (int i = 0; i < arraySizes->getNumDims(); ++i) {
155 if (arraySizes->getDimNode(i)) {
156 if (arraySizes->getDimNode(i)->getAsSymbolNode())
157 snprintf(buf, maxSize, "s%d", arraySizes->getDimNode(i)->getAsSymbolNode()->getId());
159 snprintf(buf, maxSize, "s%p", arraySizes->getDimNode(i));
161 snprintf(buf, maxSize, "%d", arraySizes->getDimSize(i));
173 void TVariable::dump(TInfoSink& infoSink) const
175 infoSink.debug << getName().c_str() << ": " << type.getStorageQualifierString() << " " << type.getBasicTypeString();
176 if (type.isArray()) {
177 infoSink.debug << "[0]";
179 infoSink.debug << "\n";
182 void TFunction::dump(TInfoSink& infoSink) const
184 infoSink.debug << getName().c_str() << ": " << returnType.getBasicTypeString() << " " << getMangledName().c_str() << "\n";
187 void TAnonMember::dump(TInfoSink& TInfoSink) const
189 TInfoSink.debug << "anonymous member " << getMemberNumber() << " of " << getAnonContainer().getName().c_str() << "\n";
192 void TSymbolTableLevel::dump(TInfoSink &infoSink) const
194 tLevel::const_iterator it;
195 for (it = level.begin(); it != level.end(); ++it)
196 (*it).second->dump(infoSink);
199 void TSymbolTable::dump(TInfoSink &infoSink) const
201 for (int level = currentLevel(); level >= 0; --level) {
202 infoSink.debug << "LEVEL " << level << "\n";
203 table[level]->dump(infoSink);
208 // Functions have buried pointers to delete.
210 TFunction::~TFunction()
212 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
217 // Symbol table levels are a map of pointers to symbols that have to be deleted.
219 TSymbolTableLevel::~TSymbolTableLevel()
221 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
224 delete [] defaultPrecision;
228 // Change all function entries in the table with the non-mangled name
229 // to be related to the provided built-in operation.
231 void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
233 tLevel::const_iterator candidate = level.lower_bound(name);
234 while (candidate != level.end()) {
235 const TString& candidateName = (*candidate).first;
236 TString::size_type parenAt = candidateName.find_first_of('(');
237 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
238 TFunction* function = (*candidate).second->getAsFunction();
239 function->relateToOperator(op);
246 // Make all function overloads of the given name require an extension(s).
247 // Should only be used for a version/profile that actually needs the extension(s).
248 void TSymbolTableLevel::setFunctionExtensions(const char* name, int num, const char* const extensions[])
250 tLevel::const_iterator candidate = level.lower_bound(name);
251 while (candidate != level.end()) {
252 const TString& candidateName = (*candidate).first;
253 TString::size_type parenAt = candidateName.find_first_of('(');
254 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
255 TSymbol* symbol = candidate->second;
256 symbol->setExtensions(num, extensions);
264 // Make all symbols in this table level read only.
266 void TSymbolTableLevel::readOnly()
268 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
269 (*it).second->makeReadOnly();
273 // Copy a symbol, but the copy is writable; call readOnly() afterward if that's not desired.
275 TSymbol::TSymbol(const TSymbol& copyOf)
277 name = NewPoolTString(copyOf.name->c_str());
278 uniqueId = copyOf.uniqueId;
282 TVariable::TVariable(const TVariable& copyOf) : TSymbol(copyOf)
284 type.deepCopy(copyOf.type);
285 userType = copyOf.userType;
288 if (copyOf.numExtensions != 0)
289 setExtensions(copyOf.numExtensions, copyOf.extensions);
291 if (! copyOf.constArray.empty()) {
292 assert(! copyOf.type.isStruct());
293 TConstUnionArray newArray(copyOf.constArray, 0, copyOf.constArray.size());
294 constArray = newArray;
297 // don't support specialization-constant subtrees in cloned tables
298 constSubtree = nullptr;
301 TVariable* TVariable::clone() const
303 TVariable *variable = new TVariable(*this);
308 TFunction::TFunction(const TFunction& copyOf) : TSymbol(copyOf)
310 for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
312 parameters.push_back(param);
313 parameters.back().copyParam(copyOf.parameters[i]);
318 if (copyOf.extensions != 0)
319 setExtensions(copyOf.numExtensions, copyOf.extensions);
320 returnType.deepCopy(copyOf.returnType);
321 mangledName = copyOf.mangledName;
323 defined = copyOf.defined;
324 prototyped = copyOf.prototyped;
325 implicitThis = copyOf.implicitThis;
326 illegalImplicitThis = copyOf.illegalImplicitThis;
327 defaultParamCount = copyOf.defaultParamCount;
330 TFunction* TFunction::clone() const
332 TFunction *function = new TFunction(*this);
337 TAnonMember* TAnonMember::clone() const
339 // Anonymous members of a given block should be cloned at a higher level,
340 // where they can all be assured to still end up pointing to a single
341 // copy of the original container.
347 TSymbolTableLevel* TSymbolTableLevel::clone() const
349 TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
350 symTableLevel->anonId = anonId;
351 symTableLevel->thisLevel = thisLevel;
352 std::vector<bool> containerCopied(anonId, false);
353 tLevel::const_iterator iter;
354 for (iter = level.begin(); iter != level.end(); ++iter) {
355 const TAnonMember* anon = iter->second->getAsAnonMember();
357 // Insert all the anonymous members of this same container at once,
358 // avoid inserting the other members in the future, once this has been done,
359 // allowing them to all be part of the same new container.
360 if (! containerCopied[anon->getAnonId()]) {
361 TVariable* container = anon->getAnonContainer().clone();
362 container->changeName(NewPoolTString(""));
363 // insert the whole container
364 symTableLevel->insert(*container, false);
365 containerCopied[anon->getAnonId()] = true;
368 symTableLevel->insert(*iter->second->clone(), false);
371 return symTableLevel;
374 void TSymbolTable::copyTable(const TSymbolTable& copyOf)
376 assert(adoptedLevels == copyOf.adoptedLevels);
378 uniqueId = copyOf.uniqueId;
379 noBuiltInRedeclarations = copyOf.noBuiltInRedeclarations;
380 separateNameSpaces = copyOf.separateNameSpaces;
381 for (unsigned int i = copyOf.adoptedLevels; i < copyOf.table.size(); ++i)
382 table.push_back(copyOf.table[i]->clone());
385 } // end namespace glslang