1 //
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.
6 // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
7 //
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions
12 // are met:
13 //
14 // Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // Redistributions in binary form must reproduce the above
18 // copyright notice, this list of conditions and the following
19 // disclaimer in the documentation and/or other materials provided
20 // with the distribution.
21 //
22 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
23 // contributors may be used to endorse or promote products derived
24 // from this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 // POSSIBILITY OF SUCH DAMAGE.
38 //
39
40 //
41 // Symbol table for parsing. Most functionality and main ideas
42 // are documented in the header file.
43 //
44
45 #include "SymbolTable.h"
46
47 namespace glslang {
48
49 //
50 // TType helper function needs a place to live.
51 //
52
53 //
54 // Recursively generate mangled names.
55 //
buildMangledName(TString & mangledName) const56 void TType::buildMangledName(TString& mangledName) const
57 {
58 if (isMatrix())
59 mangledName += 'm';
60 else if (isVector())
61 mangledName += 'v';
62
63 switch (basicType) {
64 case EbtFloat: mangledName += 'f'; break;
65 case EbtInt: mangledName += 'i'; break;
66 case EbtUint: mangledName += 'u'; break;
67 case EbtBool: mangledName += 'b'; break;
68 #ifndef GLSLANG_WEB
69 case EbtDouble: mangledName += 'd'; break;
70 case EbtFloat16: mangledName += "f16"; break;
71 case EbtInt8: mangledName += "i8"; break;
72 case EbtUint8: mangledName += "u8"; break;
73 case EbtInt16: mangledName += "i16"; break;
74 case EbtUint16: mangledName += "u16"; break;
75 case EbtInt64: mangledName += "i64"; break;
76 case EbtUint64: mangledName += "u64"; break;
77 case EbtAtomicUint: mangledName += "au"; break;
78 case EbtAccStruct: mangledName += "as"; break;
79 case EbtRayQuery: mangledName += "rq"; break;
80 #endif
81 case EbtSampler:
82 switch (sampler.type) {
83 #ifndef GLSLANG_WEB
84 case EbtFloat16: mangledName += "f16"; break;
85 #endif
86 case EbtInt: mangledName += "i"; break;
87 case EbtUint: mangledName += "u"; break;
88 default: break; // some compilers want this
89 }
90 if (sampler.isImageClass())
91 mangledName += "I"; // a normal image or subpass
92 else if (sampler.isPureSampler())
93 mangledName += "p"; // a "pure" sampler
94 else if (!sampler.isCombined())
95 mangledName += "t"; // a "pure" texture
96 else
97 mangledName += "s"; // traditional combined sampler
98 if (sampler.isArrayed())
99 mangledName += "A";
100 if (sampler.isShadow())
101 mangledName += "S";
102 if (sampler.isExternal())
103 mangledName += "E";
104 if (sampler.isYuv())
105 mangledName += "Y";
106 switch (sampler.dim) {
107 case Esd2D: mangledName += "2"; break;
108 case Esd3D: mangledName += "3"; break;
109 case EsdCube: mangledName += "C"; break;
110 #ifndef GLSLANG_WEB
111 case Esd1D: mangledName += "1"; break;
112 case EsdRect: mangledName += "R2"; break;
113 case EsdBuffer: mangledName += "B"; break;
114 case EsdSubpass: mangledName += "P"; break;
115 #endif
116 default: break; // some compilers want this
117 }
118
119 #ifdef ENABLE_HLSL
120 if (sampler.hasReturnStruct()) {
121 // Name mangle for sampler return struct uses struct table index.
122 mangledName += "-tx-struct";
123
124 char text[16]; // plenty enough space for the small integers.
125 snprintf(text, sizeof(text), "%u-", sampler.getStructReturnIndex());
126 mangledName += text;
127 } else {
128 switch (sampler.getVectorSize()) {
129 case 1: mangledName += "1"; break;
130 case 2: mangledName += "2"; break;
131 case 3: mangledName += "3"; break;
132 case 4: break; // default to prior name mangle behavior
133 }
134 }
135 #endif
136
137 if (sampler.isMultiSample())
138 mangledName += "M";
139 break;
140 case EbtStruct:
141 case EbtBlock:
142 if (basicType == EbtStruct)
143 mangledName += "struct-";
144 else
145 mangledName += "block-";
146 if (typeName)
147 mangledName += *typeName;
148 for (unsigned int i = 0; i < structure->size(); ++i) {
149 mangledName += '-';
150 (*structure)[i].type->buildMangledName(mangledName);
151 }
152 default:
153 break;
154 }
155
156 if (getVectorSize() > 0)
157 mangledName += static_cast<char>('0' + getVectorSize());
158 else {
159 mangledName += static_cast<char>('0' + getMatrixCols());
160 mangledName += static_cast<char>('0' + getMatrixRows());
161 }
162
163 if (arraySizes) {
164 const int maxSize = 11;
165 char buf[maxSize];
166 for (int i = 0; i < arraySizes->getNumDims(); ++i) {
167 if (arraySizes->getDimNode(i)) {
168 if (arraySizes->getDimNode(i)->getAsSymbolNode())
169 snprintf(buf, maxSize, "s%d", arraySizes->getDimNode(i)->getAsSymbolNode()->getId());
170 else
171 snprintf(buf, maxSize, "s%p", arraySizes->getDimNode(i));
172 } else
173 snprintf(buf, maxSize, "%d", arraySizes->getDimSize(i));
174 mangledName += '[';
175 mangledName += buf;
176 mangledName += ']';
177 }
178 }
179 }
180
181 #if !defined(GLSLANG_WEB) && !defined(GLSLANG_ANGLE)
182
183 //
184 // Dump functions.
185 //
186
dumpExtensions(TInfoSink & infoSink) const187 void TSymbol::dumpExtensions(TInfoSink& infoSink) const
188 {
189 int numExtensions = getNumExtensions();
190 if (numExtensions) {
191 infoSink.debug << " <";
192
193 for (int i = 0; i < numExtensions; i++)
194 infoSink.debug << getExtensions()[i] << ",";
195
196 infoSink.debug << ">";
197 }
198 }
199
dump(TInfoSink & infoSink,bool complete) const200 void TVariable::dump(TInfoSink& infoSink, bool complete) const
201 {
202 if (complete) {
203 infoSink.debug << getName().c_str() << ": " << type.getCompleteString();
204 dumpExtensions(infoSink);
205 } else {
206 infoSink.debug << getName().c_str() << ": " << type.getStorageQualifierString() << " "
207 << type.getBasicTypeString();
208
209 if (type.isArray())
210 infoSink.debug << "[0]";
211 }
212
213 infoSink.debug << "\n";
214 }
215
dump(TInfoSink & infoSink,bool complete) const216 void TFunction::dump(TInfoSink& infoSink, bool complete) const
217 {
218 if (complete) {
219 infoSink.debug << getName().c_str() << ": " << returnType.getCompleteString() << " " << getName().c_str()
220 << "(";
221
222 int numParams = getParamCount();
223 for (int i = 0; i < numParams; i++) {
224 const TParameter ¶m = parameters[i];
225 infoSink.debug << param.type->getCompleteString() << " "
226 << (param.type->isStruct() ? "of " + param.type->getTypeName() + " " : "")
227 << (param.name ? *param.name : "") << (i < numParams - 1 ? "," : "");
228 }
229
230 infoSink.debug << ")";
231 dumpExtensions(infoSink);
232 } else {
233 infoSink.debug << getName().c_str() << ": " << returnType.getBasicTypeString() << " "
234 << getMangledName().c_str() << "n";
235 }
236
237 infoSink.debug << "\n";
238 }
239
dump(TInfoSink & TInfoSink,bool) const240 void TAnonMember::dump(TInfoSink& TInfoSink, bool) const
241 {
242 TInfoSink.debug << "anonymous member " << getMemberNumber() << " of " << getAnonContainer().getName().c_str()
243 << "\n";
244 }
245
dump(TInfoSink & infoSink,bool complete) const246 void TSymbolTableLevel::dump(TInfoSink& infoSink, bool complete) const
247 {
248 tLevel::const_iterator it;
249 for (it = level.begin(); it != level.end(); ++it)
250 (*it).second->dump(infoSink, complete);
251 }
252
dump(TInfoSink & infoSink,bool complete) const253 void TSymbolTable::dump(TInfoSink& infoSink, bool complete) const
254 {
255 for (int level = currentLevel(); level >= 0; --level) {
256 infoSink.debug << "LEVEL " << level << "\n";
257 table[level]->dump(infoSink, complete);
258 }
259 }
260
261 #endif
262
263 //
264 // Functions have buried pointers to delete.
265 //
~TFunction()266 TFunction::~TFunction()
267 {
268 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
269 delete (*i).type;
270 }
271
272 //
273 // Symbol table levels are a map of pointers to symbols that have to be deleted.
274 //
~TSymbolTableLevel()275 TSymbolTableLevel::~TSymbolTableLevel()
276 {
277 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
278 delete (*it).second;
279
280 delete [] defaultPrecision;
281 }
282
283 //
284 // Change all function entries in the table with the non-mangled name
285 // to be related to the provided built-in operation.
286 //
relateToOperator(const char * name,TOperator op)287 void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
288 {
289 tLevel::const_iterator candidate = level.lower_bound(name);
290 while (candidate != level.end()) {
291 const TString& candidateName = (*candidate).first;
292 TString::size_type parenAt = candidateName.find_first_of('(');
293 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
294 TFunction* function = (*candidate).second->getAsFunction();
295 function->relateToOperator(op);
296 } else
297 break;
298 ++candidate;
299 }
300 }
301
302 // Make all function overloads of the given name require an extension(s).
303 // Should only be used for a version/profile that actually needs the extension(s).
setFunctionExtensions(const char * name,int num,const char * const extensions[])304 void TSymbolTableLevel::setFunctionExtensions(const char* name, int num, const char* const extensions[])
305 {
306 tLevel::const_iterator candidate = level.lower_bound(name);
307 while (candidate != level.end()) {
308 const TString& candidateName = (*candidate).first;
309 TString::size_type parenAt = candidateName.find_first_of('(');
310 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
311 TSymbol* symbol = candidate->second;
312 symbol->setExtensions(num, extensions);
313 } else
314 break;
315 ++candidate;
316 }
317 }
318
319 //
320 // Make all symbols in this table level read only.
321 //
readOnly()322 void TSymbolTableLevel::readOnly()
323 {
324 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
325 (*it).second->makeReadOnly();
326 }
327
328 //
329 // Copy a symbol, but the copy is writable; call readOnly() afterward if that's not desired.
330 //
TSymbol(const TSymbol & copyOf)331 TSymbol::TSymbol(const TSymbol& copyOf)
332 {
333 name = NewPoolTString(copyOf.name->c_str());
334 uniqueId = copyOf.uniqueId;
335 writable = true;
336 }
337
TVariable(const TVariable & copyOf)338 TVariable::TVariable(const TVariable& copyOf) : TSymbol(copyOf)
339 {
340 type.deepCopy(copyOf.type);
341 userType = copyOf.userType;
342
343 // we don't support specialization-constant subtrees in cloned tables, only extensions
344 constSubtree = nullptr;
345 extensions = nullptr;
346 memberExtensions = nullptr;
347 if (copyOf.getNumExtensions() > 0)
348 setExtensions(copyOf.getNumExtensions(), copyOf.getExtensions());
349 if (copyOf.hasMemberExtensions()) {
350 for (int m = 0; m < (int)copyOf.type.getStruct()->size(); ++m) {
351 if (copyOf.getNumMemberExtensions(m) > 0)
352 setMemberExtensions(m, copyOf.getNumMemberExtensions(m), copyOf.getMemberExtensions(m));
353 }
354 }
355
356 if (! copyOf.constArray.empty()) {
357 assert(! copyOf.type.isStruct());
358 TConstUnionArray newArray(copyOf.constArray, 0, copyOf.constArray.size());
359 constArray = newArray;
360 }
361 }
362
clone() const363 TVariable* TVariable::clone() const
364 {
365 TVariable *variable = new TVariable(*this);
366
367 return variable;
368 }
369
TFunction(const TFunction & copyOf)370 TFunction::TFunction(const TFunction& copyOf) : TSymbol(copyOf)
371 {
372 for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
373 TParameter param;
374 parameters.push_back(param);
375 parameters.back().copyParam(copyOf.parameters[i]);
376 }
377
378 extensions = nullptr;
379 if (copyOf.getNumExtensions() > 0)
380 setExtensions(copyOf.getNumExtensions(), copyOf.getExtensions());
381 returnType.deepCopy(copyOf.returnType);
382 mangledName = copyOf.mangledName;
383 op = copyOf.op;
384 defined = copyOf.defined;
385 prototyped = copyOf.prototyped;
386 implicitThis = copyOf.implicitThis;
387 illegalImplicitThis = copyOf.illegalImplicitThis;
388 defaultParamCount = copyOf.defaultParamCount;
389 }
390
clone() const391 TFunction* TFunction::clone() const
392 {
393 TFunction *function = new TFunction(*this);
394
395 return function;
396 }
397
clone() const398 TAnonMember* TAnonMember::clone() const
399 {
400 // Anonymous members of a given block should be cloned at a higher level,
401 // where they can all be assured to still end up pointing to a single
402 // copy of the original container.
403 assert(0);
404
405 return 0;
406 }
407
clone() const408 TSymbolTableLevel* TSymbolTableLevel::clone() const
409 {
410 TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
411 symTableLevel->anonId = anonId;
412 symTableLevel->thisLevel = thisLevel;
413 std::vector<bool> containerCopied(anonId, false);
414 tLevel::const_iterator iter;
415 for (iter = level.begin(); iter != level.end(); ++iter) {
416 const TAnonMember* anon = iter->second->getAsAnonMember();
417 if (anon) {
418 // Insert all the anonymous members of this same container at once,
419 // avoid inserting the remaining members in the future, once this has been done,
420 // allowing them to all be part of the same new container.
421 if (! containerCopied[anon->getAnonId()]) {
422 TVariable* container = anon->getAnonContainer().clone();
423 container->changeName(NewPoolTString(""));
424 // insert the container and all its members
425 symTableLevel->insert(*container, false);
426 containerCopied[anon->getAnonId()] = true;
427 }
428 } else
429 symTableLevel->insert(*iter->second->clone(), false);
430 }
431
432 return symTableLevel;
433 }
434
copyTable(const TSymbolTable & copyOf)435 void TSymbolTable::copyTable(const TSymbolTable& copyOf)
436 {
437 assert(adoptedLevels == copyOf.adoptedLevels);
438
439 uniqueId = copyOf.uniqueId;
440 noBuiltInRedeclarations = copyOf.noBuiltInRedeclarations;
441 separateNameSpaces = copyOf.separateNameSpaces;
442 for (unsigned int i = copyOf.adoptedLevels; i < copyOf.table.size(); ++i)
443 table.push_back(copyOf.table[i]->clone());
444 }
445
446 } // end namespace glslang
447