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 case EbtInt64: mangledName += "i64"; break;
89 case EbtUint64: mangledName += "u64"; break;
90 default: break; // some compilers want this
91 }
92 if (sampler.isImageClass())
93 mangledName += "I"; // a normal image or subpass
94 else if (sampler.isPureSampler())
95 mangledName += "p"; // a "pure" sampler
96 else if (!sampler.isCombined())
97 mangledName += "t"; // a "pure" texture
98 else
99 mangledName += "s"; // traditional combined sampler
100 if (sampler.isArrayed())
101 mangledName += "A";
102 if (sampler.isShadow())
103 mangledName += "S";
104 if (sampler.isExternal())
105 mangledName += "E";
106 if (sampler.isYuv())
107 mangledName += "Y";
108 switch (sampler.dim) {
109 case Esd2D: mangledName += "2"; break;
110 case Esd3D: mangledName += "3"; break;
111 case EsdCube: mangledName += "C"; break;
112 #ifndef GLSLANG_WEB
113 case Esd1D: mangledName += "1"; break;
114 case EsdRect: mangledName += "R2"; break;
115 case EsdBuffer: mangledName += "B"; break;
116 case EsdSubpass: mangledName += "P"; break;
117 #endif
118 default: break; // some compilers want this
119 }
120
121 #ifdef ENABLE_HLSL
122 if (sampler.hasReturnStruct()) {
123 // Name mangle for sampler return struct uses struct table index.
124 mangledName += "-tx-struct";
125
126 char text[16]; // plenty enough space for the small integers.
127 snprintf(text, sizeof(text), "%u-", sampler.getStructReturnIndex());
128 mangledName += text;
129 } else {
130 switch (sampler.getVectorSize()) {
131 case 1: mangledName += "1"; break;
132 case 2: mangledName += "2"; break;
133 case 3: mangledName += "3"; break;
134 case 4: break; // default to prior name mangle behavior
135 }
136 }
137 #endif
138
139 if (sampler.isMultiSample())
140 mangledName += "M";
141 break;
142 case EbtStruct:
143 case EbtBlock:
144 if (basicType == EbtStruct)
145 mangledName += "struct-";
146 else
147 mangledName += "block-";
148 if (typeName)
149 mangledName += *typeName;
150 for (unsigned int i = 0; i < structure->size(); ++i) {
151 if ((*structure)[i].type->getBasicType() == EbtVoid)
152 continue;
153 mangledName += '-';
154 (*structure)[i].type->buildMangledName(mangledName);
155 }
156 default:
157 break;
158 }
159
160 if (getVectorSize() > 0)
161 mangledName += static_cast<char>('0' + getVectorSize());
162 else {
163 mangledName += static_cast<char>('0' + getMatrixCols());
164 mangledName += static_cast<char>('0' + getMatrixRows());
165 }
166
167 if (arraySizes) {
168 const int maxSize = 11;
169 char buf[maxSize];
170 for (int i = 0; i < arraySizes->getNumDims(); ++i) {
171 if (arraySizes->getDimNode(i)) {
172 if (arraySizes->getDimNode(i)->getAsSymbolNode())
173 snprintf(buf, maxSize, "s%d", arraySizes->getDimNode(i)->getAsSymbolNode()->getId());
174 else
175 snprintf(buf, maxSize, "s%p", arraySizes->getDimNode(i));
176 } else
177 snprintf(buf, maxSize, "%d", arraySizes->getDimSize(i));
178 mangledName += '[';
179 mangledName += buf;
180 mangledName += ']';
181 }
182 }
183 }
184
185 #if !defined(GLSLANG_WEB) && !defined(GLSLANG_ANGLE)
186
187 //
188 // Dump functions.
189 //
190
dumpExtensions(TInfoSink & infoSink) const191 void TSymbol::dumpExtensions(TInfoSink& infoSink) const
192 {
193 int numExtensions = getNumExtensions();
194 if (numExtensions) {
195 infoSink.debug << " <";
196
197 for (int i = 0; i < numExtensions; i++)
198 infoSink.debug << getExtensions()[i] << ",";
199
200 infoSink.debug << ">";
201 }
202 }
203
dump(TInfoSink & infoSink,bool complete) const204 void TVariable::dump(TInfoSink& infoSink, bool complete) const
205 {
206 if (complete) {
207 infoSink.debug << getName().c_str() << ": " << type.getCompleteString();
208 dumpExtensions(infoSink);
209 } else {
210 infoSink.debug << getName().c_str() << ": " << type.getStorageQualifierString() << " "
211 << type.getBasicTypeString();
212
213 if (type.isArray())
214 infoSink.debug << "[0]";
215 }
216
217 infoSink.debug << "\n";
218 }
219
dump(TInfoSink & infoSink,bool complete) const220 void TFunction::dump(TInfoSink& infoSink, bool complete) const
221 {
222 if (complete) {
223 infoSink.debug << getName().c_str() << ": " << returnType.getCompleteString() << " " << getName().c_str()
224 << "(";
225
226 int numParams = getParamCount();
227 for (int i = 0; i < numParams; i++) {
228 const TParameter ¶m = parameters[i];
229 infoSink.debug << param.type->getCompleteString() << " "
230 << (param.type->isStruct() ? "of " + param.type->getTypeName() + " " : "")
231 << (param.name ? *param.name : "") << (i < numParams - 1 ? "," : "");
232 }
233
234 infoSink.debug << ")";
235 dumpExtensions(infoSink);
236 } else {
237 infoSink.debug << getName().c_str() << ": " << returnType.getBasicTypeString() << " "
238 << getMangledName().c_str() << "n";
239 }
240
241 infoSink.debug << "\n";
242 }
243
dump(TInfoSink & TInfoSink,bool) const244 void TAnonMember::dump(TInfoSink& TInfoSink, bool) const
245 {
246 TInfoSink.debug << "anonymous member " << getMemberNumber() << " of " << getAnonContainer().getName().c_str()
247 << "\n";
248 }
249
dump(TInfoSink & infoSink,bool complete) const250 void TSymbolTableLevel::dump(TInfoSink& infoSink, bool complete) const
251 {
252 tLevel::const_iterator it;
253 for (it = level.begin(); it != level.end(); ++it)
254 (*it).second->dump(infoSink, complete);
255 }
256
dump(TInfoSink & infoSink,bool complete) const257 void TSymbolTable::dump(TInfoSink& infoSink, bool complete) const
258 {
259 for (int level = currentLevel(); level >= 0; --level) {
260 infoSink.debug << "LEVEL " << level << "\n";
261 table[level]->dump(infoSink, complete);
262 }
263 }
264
265 #endif
266
267 //
268 // Functions have buried pointers to delete.
269 //
~TFunction()270 TFunction::~TFunction()
271 {
272 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
273 delete (*i).type;
274 }
275
276 //
277 // Symbol table levels are a map of pointers to symbols that have to be deleted.
278 //
~TSymbolTableLevel()279 TSymbolTableLevel::~TSymbolTableLevel()
280 {
281 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
282 delete (*it).second;
283
284 delete [] defaultPrecision;
285 }
286
287 //
288 // Change all function entries in the table with the non-mangled name
289 // to be related to the provided built-in operation.
290 //
relateToOperator(const char * name,TOperator op)291 void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
292 {
293 tLevel::const_iterator candidate = level.lower_bound(name);
294 while (candidate != level.end()) {
295 const TString& candidateName = (*candidate).first;
296 TString::size_type parenAt = candidateName.find_first_of('(');
297 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
298 TFunction* function = (*candidate).second->getAsFunction();
299 function->relateToOperator(op);
300 } else
301 break;
302 ++candidate;
303 }
304 }
305
306 // Make all function overloads of the given name require an extension(s).
307 // Should only be used for a version/profile that actually needs the extension(s).
setFunctionExtensions(const char * name,int num,const char * const extensions[])308 void TSymbolTableLevel::setFunctionExtensions(const char* name, int num, const char* const extensions[])
309 {
310 tLevel::const_iterator candidate = level.lower_bound(name);
311 while (candidate != level.end()) {
312 const TString& candidateName = (*candidate).first;
313 TString::size_type parenAt = candidateName.find_first_of('(');
314 if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
315 TSymbol* symbol = candidate->second;
316 symbol->setExtensions(num, extensions);
317 } else
318 break;
319 ++candidate;
320 }
321 }
322
323 //
324 // Make all symbols in this table level read only.
325 //
readOnly()326 void TSymbolTableLevel::readOnly()
327 {
328 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
329 (*it).second->makeReadOnly();
330 }
331
332 //
333 // Copy a symbol, but the copy is writable; call readOnly() afterward if that's not desired.
334 //
TSymbol(const TSymbol & copyOf)335 TSymbol::TSymbol(const TSymbol& copyOf)
336 {
337 name = NewPoolTString(copyOf.name->c_str());
338 uniqueId = copyOf.uniqueId;
339 writable = true;
340 }
341
TVariable(const TVariable & copyOf)342 TVariable::TVariable(const TVariable& copyOf) : TSymbol(copyOf)
343 {
344 type.deepCopy(copyOf.type);
345 userType = copyOf.userType;
346
347 // we don't support specialization-constant subtrees in cloned tables, only extensions
348 constSubtree = nullptr;
349 extensions = nullptr;
350 memberExtensions = nullptr;
351 if (copyOf.getNumExtensions() > 0)
352 setExtensions(copyOf.getNumExtensions(), copyOf.getExtensions());
353 if (copyOf.hasMemberExtensions()) {
354 for (int m = 0; m < (int)copyOf.type.getStruct()->size(); ++m) {
355 if (copyOf.getNumMemberExtensions(m) > 0)
356 setMemberExtensions(m, copyOf.getNumMemberExtensions(m), copyOf.getMemberExtensions(m));
357 }
358 }
359
360 if (! copyOf.constArray.empty()) {
361 assert(! copyOf.type.isStruct());
362 TConstUnionArray newArray(copyOf.constArray, 0, copyOf.constArray.size());
363 constArray = newArray;
364 }
365 }
366
clone() const367 TVariable* TVariable::clone() const
368 {
369 TVariable *variable = new TVariable(*this);
370
371 return variable;
372 }
373
TFunction(const TFunction & copyOf)374 TFunction::TFunction(const TFunction& copyOf) : TSymbol(copyOf)
375 {
376 for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
377 TParameter param;
378 parameters.push_back(param);
379 parameters.back().copyParam(copyOf.parameters[i]);
380 }
381
382 extensions = nullptr;
383 if (copyOf.getNumExtensions() > 0)
384 setExtensions(copyOf.getNumExtensions(), copyOf.getExtensions());
385 returnType.deepCopy(copyOf.returnType);
386 mangledName = copyOf.mangledName;
387 op = copyOf.op;
388 defined = copyOf.defined;
389 prototyped = copyOf.prototyped;
390 implicitThis = copyOf.implicitThis;
391 illegalImplicitThis = copyOf.illegalImplicitThis;
392 defaultParamCount = copyOf.defaultParamCount;
393 }
394
clone() const395 TFunction* TFunction::clone() const
396 {
397 TFunction *function = new TFunction(*this);
398
399 return function;
400 }
401
clone() const402 TAnonMember* TAnonMember::clone() const
403 {
404 // Anonymous members of a given block should be cloned at a higher level,
405 // where they can all be assured to still end up pointing to a single
406 // copy of the original container.
407 assert(0);
408
409 return 0;
410 }
411
clone() const412 TSymbolTableLevel* TSymbolTableLevel::clone() const
413 {
414 TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
415 symTableLevel->anonId = anonId;
416 symTableLevel->thisLevel = thisLevel;
417 std::vector<bool> containerCopied(anonId, false);
418 tLevel::const_iterator iter;
419 for (iter = level.begin(); iter != level.end(); ++iter) {
420 const TAnonMember* anon = iter->second->getAsAnonMember();
421 if (anon) {
422 // Insert all the anonymous members of this same container at once,
423 // avoid inserting the remaining members in the future, once this has been done,
424 // allowing them to all be part of the same new container.
425 if (! containerCopied[anon->getAnonId()]) {
426 TVariable* container = anon->getAnonContainer().clone();
427 container->changeName(NewPoolTString(""));
428 // insert the container and all its members
429 symTableLevel->insert(*container, false);
430 containerCopied[anon->getAnonId()] = true;
431 }
432 } else
433 symTableLevel->insert(*iter->second->clone(), false);
434 }
435
436 return symTableLevel;
437 }
438
copyTable(const TSymbolTable & copyOf)439 void TSymbolTable::copyTable(const TSymbolTable& copyOf)
440 {
441 assert(adoptedLevels == copyOf.adoptedLevels);
442
443 uniqueId = copyOf.uniqueId;
444 noBuiltInRedeclarations = copyOf.noBuiltInRedeclarations;
445 separateNameSpaces = copyOf.separateNameSpaces;
446 for (unsigned int i = copyOf.adoptedLevels; i < copyOf.table.size(); ++i)
447 table.push_back(copyOf.table[i]->clone());
448 }
449
450 } // end namespace glslang
451