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 default: break; // some compilers want this
96 mangledName += "struct-";
98 mangledName += *typeName;
99 for (unsigned int i = 0; i < structure->size(); ++i) {
101 (*structure)[i].type->buildMangledName(mangledName);
107 if (getVectorSize() > 0)
108 mangledName += static_cast<char>('0' + getVectorSize());
110 mangledName += static_cast<char>('0' + getMatrixCols());
111 mangledName += static_cast<char>('0' + getMatrixRows());
115 const int maxSize = 11;
117 for (int i = 0; i < arraySizes->getNumDims(); ++i) {
118 snprintf(buf, maxSize, "%d", arraySizes->getDimSize(i));
130 void TVariable::dump(TInfoSink& infoSink) const
132 infoSink.debug << getName().c_str() << ": " << type.getStorageQualifierString() << " " << type.getBasicTypeString();
133 if (type.isArray()) {
134 infoSink.debug << "[0]";
136 infoSink.debug << "\n";
139 void TFunction::dump(TInfoSink& infoSink) const
141 infoSink.debug << getName().c_str() << ": " << returnType.getBasicTypeString() << " " << getMangledName().c_str() << "\n";
144 void TAnonMember::dump(TInfoSink& TInfoSink) const
146 TInfoSink.debug << "anonymous member " << getMemberNumber() << " of " << getAnonContainer().getName().c_str() << "\n";
149 void TSymbolTableLevel::dump(TInfoSink &infoSink) const
151 tLevel::const_iterator it;
152 for (it = level.begin(); it != level.end(); ++it)
153 (*it).second->dump(infoSink);
156 void TSymbolTable::dump(TInfoSink &infoSink) const
158 for (int level = currentLevel(); level >= 0; --level) {
159 infoSink.debug << "LEVEL " << level << "\n";
160 table[level]->dump(infoSink);
165 // Functions have buried pointers to delete.
167 TFunction::~TFunction()
169 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
174 // Symbol table levels are a map of pointers to symbols that have to be deleted.
176 TSymbolTableLevel::~TSymbolTableLevel()
178 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
181 delete [] defaultPrecision;
185 // Change all function entries in the table with the non-mangled name
186 // to be related to the provided built-in operation.
188 void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
190 tLevel::const_iterator candidate = level.lower_bound(name);
191 while (candidate != level.end()) {
192 const TString& candidateName = (*candidate).first;
193 TString::size_type parenAt = candidateName.find_first_of('(');
194 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
195 TFunction* function = (*candidate).second->getAsFunction();
196 function->relateToOperator(op);
203 // Make all function overloads of the given name require an extension(s).
204 // Should only be used for a version/profile that actually needs the extension(s).
205 void TSymbolTableLevel::setFunctionExtensions(const char* name, int num, const char* const extensions[])
207 tLevel::const_iterator candidate = level.lower_bound(name);
208 while (candidate != level.end()) {
209 const TString& candidateName = (*candidate).first;
210 TString::size_type parenAt = candidateName.find_first_of('(');
211 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
212 TSymbol* symbol = candidate->second;
213 symbol->setExtensions(num, extensions);
221 // Make all symbols in this table level read only.
223 void TSymbolTableLevel::readOnly()
225 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
226 (*it).second->makeReadOnly();
230 // Copy a symbol, but the copy is writable; call readOnly() afterward if that's not desired.
232 TSymbol::TSymbol(const TSymbol& copyOf)
234 name = NewPoolTString(copyOf.name->c_str());
235 uniqueId = copyOf.uniqueId;
239 TVariable::TVariable(const TVariable& copyOf) : TSymbol(copyOf)
241 type.deepCopy(copyOf.type);
242 userType = copyOf.userType;
245 if (copyOf.numExtensions != 0)
246 setExtensions(copyOf.numExtensions, copyOf.extensions);
248 if (! copyOf.unionArray.empty()) {
249 assert(! copyOf.type.isStruct());
250 TConstUnionArray newArray(copyOf.unionArray, 0, copyOf.unionArray.size());
251 unionArray = newArray;
255 TVariable* TVariable::clone() const
257 TVariable *variable = new TVariable(*this);
262 TFunction::TFunction(const TFunction& copyOf) : TSymbol(copyOf)
264 for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
266 parameters.push_back(param);
267 parameters.back().copyParam(copyOf.parameters[i]);
272 if (copyOf.extensions != 0)
273 setExtensions(copyOf.numExtensions, copyOf.extensions);
274 returnType.deepCopy(copyOf.returnType);
275 mangledName = copyOf.mangledName;
277 defined = copyOf.defined;
278 prototyped = copyOf.prototyped;
281 TFunction* TFunction::clone() const
283 TFunction *function = new TFunction(*this);
288 TAnonMember* TAnonMember::clone() const
290 // Anonymous members of a given block should be cloned at a higher level,
291 // where they can all be assured to still end up pointing to a single
292 // copy of the original container.
298 TSymbolTableLevel* TSymbolTableLevel::clone() const
300 TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
301 symTableLevel->anonId = anonId;
302 std::vector<bool> containerCopied(anonId, false);
303 tLevel::const_iterator iter;
304 for (iter = level.begin(); iter != level.end(); ++iter) {
305 const TAnonMember* anon = iter->second->getAsAnonMember();
307 // Insert all the anonymous members of this same container at once,
308 // avoid inserting the other members in the future, once this has been done,
309 // allowing them to all be part of the same new container.
310 if (! containerCopied[anon->getAnonId()]) {
311 TVariable* container = anon->getAnonContainer().clone();
312 container->changeName(NewPoolTString(""));
313 // insert the whole container
314 symTableLevel->insert(*container, false);
315 containerCopied[anon->getAnonId()] = true;
318 symTableLevel->insert(*iter->second->clone(), false);
321 return symTableLevel;
324 void TSymbolTable::copyTable(const TSymbolTable& copyOf)
326 assert(adoptedLevels == copyOf.adoptedLevels);
328 uniqueId = copyOf.uniqueId;
329 noBuiltInRedeclarations = copyOf.noBuiltInRedeclarations;
330 separateNameSpaces = copyOf.separateNameSpaces;
331 for (unsigned int i = copyOf.adoptedLevels; i < copyOf.table.size(); ++i)
332 table.push_back(copyOf.table[i]->clone());
335 } // end namespace glslang