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 EbtBool: mangledName += 'b'; break;
66 case EbtAtomicUint: mangledName += "au"; break;
68 switch (sampler.type) {
69 case EbtInt: mangledName += "i"; break;
70 case EbtUint: mangledName += "u"; break;
71 default: break; // some compilers want this
83 switch (sampler.dim) {
84 case Esd1D: mangledName += "1"; break;
85 case Esd2D: mangledName += "2"; break;
86 case Esd3D: mangledName += "3"; break;
87 case EsdCube: mangledName += "C"; break;
88 case EsdRect: mangledName += "R2"; break;
89 case EsdBuffer: mangledName += "B"; break;
90 case EsdSubpass: mangledName += "P"; break;
91 default: break; // some compilers want this
97 mangledName += "struct-";
99 mangledName += *typeName;
100 for (unsigned int i = 0; i < structure->size(); ++i) {
102 (*structure)[i].type->buildMangledName(mangledName);
108 if (getVectorSize() > 0)
109 mangledName += static_cast<char>('0' + getVectorSize());
111 mangledName += static_cast<char>('0' + getMatrixCols());
112 mangledName += static_cast<char>('0' + getMatrixRows());
116 const int maxSize = 11;
118 for (int i = 0; i < arraySizes->getNumDims(); ++i) {
119 if (arraySizes->getDimNode(i)) {
120 if (arraySizes->getDimNode(i)->getAsSymbolNode())
121 snprintf(buf, maxSize, "s%d", arraySizes->getDimNode(i)->getAsSymbolNode()->getId());
123 snprintf(buf, maxSize, "s%x", arraySizes->getDimNode(i));
125 snprintf(buf, maxSize, "%d", arraySizes->getDimSize(i));
137 void TVariable::dump(TInfoSink& infoSink) const
139 infoSink.debug << getName().c_str() << ": " << type.getStorageQualifierString() << " " << type.getBasicTypeString();
140 if (type.isArray()) {
141 infoSink.debug << "[0]";
143 infoSink.debug << "\n";
146 void TFunction::dump(TInfoSink& infoSink) const
148 infoSink.debug << getName().c_str() << ": " << returnType.getBasicTypeString() << " " << getMangledName().c_str() << "\n";
151 void TAnonMember::dump(TInfoSink& TInfoSink) const
153 TInfoSink.debug << "anonymous member " << getMemberNumber() << " of " << getAnonContainer().getName().c_str() << "\n";
156 void TSymbolTableLevel::dump(TInfoSink &infoSink) const
158 tLevel::const_iterator it;
159 for (it = level.begin(); it != level.end(); ++it)
160 (*it).second->dump(infoSink);
163 void TSymbolTable::dump(TInfoSink &infoSink) const
165 for (int level = currentLevel(); level >= 0; --level) {
166 infoSink.debug << "LEVEL " << level << "\n";
167 table[level]->dump(infoSink);
172 // Functions have buried pointers to delete.
174 TFunction::~TFunction()
176 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
181 // Symbol table levels are a map of pointers to symbols that have to be deleted.
183 TSymbolTableLevel::~TSymbolTableLevel()
185 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
188 delete [] defaultPrecision;
192 // Change all function entries in the table with the non-mangled name
193 // to be related to the provided built-in operation.
195 void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
197 tLevel::const_iterator candidate = level.lower_bound(name);
198 while (candidate != level.end()) {
199 const TString& candidateName = (*candidate).first;
200 TString::size_type parenAt = candidateName.find_first_of('(');
201 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
202 TFunction* function = (*candidate).second->getAsFunction();
203 function->relateToOperator(op);
210 // Make all function overloads of the given name require an extension(s).
211 // Should only be used for a version/profile that actually needs the extension(s).
212 void TSymbolTableLevel::setFunctionExtensions(const char* name, int num, const char* const extensions[])
214 tLevel::const_iterator candidate = level.lower_bound(name);
215 while (candidate != level.end()) {
216 const TString& candidateName = (*candidate).first;
217 TString::size_type parenAt = candidateName.find_first_of('(');
218 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
219 TSymbol* symbol = candidate->second;
220 symbol->setExtensions(num, extensions);
228 // Make all symbols in this table level read only.
230 void TSymbolTableLevel::readOnly()
232 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
233 (*it).second->makeReadOnly();
237 // Copy a symbol, but the copy is writable; call readOnly() afterward if that's not desired.
239 TSymbol::TSymbol(const TSymbol& copyOf)
241 name = NewPoolTString(copyOf.name->c_str());
242 uniqueId = copyOf.uniqueId;
246 TVariable::TVariable(const TVariable& copyOf) : TSymbol(copyOf)
248 type.deepCopy(copyOf.type);
249 userType = copyOf.userType;
252 if (copyOf.numExtensions != 0)
253 setExtensions(copyOf.numExtensions, copyOf.extensions);
255 if (! copyOf.unionArray.empty()) {
256 assert(! copyOf.type.isStruct());
257 TConstUnionArray newArray(copyOf.unionArray, 0, copyOf.unionArray.size());
258 unionArray = newArray;
262 TVariable* TVariable::clone() const
264 TVariable *variable = new TVariable(*this);
269 TFunction::TFunction(const TFunction& copyOf) : TSymbol(copyOf)
271 for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
273 parameters.push_back(param);
274 parameters.back().copyParam(copyOf.parameters[i]);
279 if (copyOf.extensions != 0)
280 setExtensions(copyOf.numExtensions, copyOf.extensions);
281 returnType.deepCopy(copyOf.returnType);
282 mangledName = copyOf.mangledName;
284 defined = copyOf.defined;
285 prototyped = copyOf.prototyped;
288 TFunction* TFunction::clone() const
290 TFunction *function = new TFunction(*this);
295 TAnonMember* TAnonMember::clone() const
297 // Anonymous members of a given block should be cloned at a higher level,
298 // where they can all be assured to still end up pointing to a single
299 // copy of the original container.
305 TSymbolTableLevel* TSymbolTableLevel::clone() const
307 TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
308 symTableLevel->anonId = anonId;
309 std::vector<bool> containerCopied(anonId, false);
310 tLevel::const_iterator iter;
311 for (iter = level.begin(); iter != level.end(); ++iter) {
312 const TAnonMember* anon = iter->second->getAsAnonMember();
314 // Insert all the anonymous members of this same container at once,
315 // avoid inserting the other members in the future, once this has been done,
316 // allowing them to all be part of the same new container.
317 if (! containerCopied[anon->getAnonId()]) {
318 TVariable* container = anon->getAnonContainer().clone();
319 container->changeName(NewPoolTString(""));
320 // insert the whole container
321 symTableLevel->insert(*container, false);
322 containerCopied[anon->getAnonId()] = true;
325 symTableLevel->insert(*iter->second->clone(), false);
328 return symTableLevel;
331 void TSymbolTable::copyTable(const TSymbolTable& copyOf)
333 assert(adoptedLevels == copyOf.adoptedLevels);
335 uniqueId = copyOf.uniqueId;
336 noBuiltInRedeclarations = copyOf.noBuiltInRedeclarations;
337 separateNameSpaces = copyOf.separateNameSpaces;
338 for (unsigned int i = copyOf.adoptedLevels; i < copyOf.table.size(); ++i)
339 table.push_back(copyOf.table[i]->clone());
342 } // end namespace glslang