Merge pull request #1499 from chaoc/nvidia_turing
[platform/upstream/glslang.git] / glslang / MachineIndependent / SymbolTable.cpp
1 //
2 // Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
3 // Copyright (C) 2012-2013 LunarG, Inc.
4 // Copyright (C) 2017 ARM Limited.
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 //    Redistributions of source code must retain the above copyright
13 //    notice, this list of conditions and the following disclaimer.
14 //
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.
19 //
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.
23 //
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.
36 //
37
38 //
39 // Symbol table for parsing.  Most functionality and main ideas
40 // are documented in the header file.
41 //
42
43 #include "SymbolTable.h"
44
45 namespace glslang {
46
47 //
48 // TType helper function needs a place to live.
49 //
50
51 //
52 // Recursively generate mangled names.
53 //
54 void TType::buildMangledName(TString& mangledName) const
55 {
56     if (isMatrix())
57         mangledName += 'm';
58     else if (isVector())
59         mangledName += 'v';
60
61     switch (basicType) {
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;
75 #ifdef NV_EXTENSIONS
76     case EbtAccStructNV:        mangledName += "asnv";   break;
77 #endif
78     case EbtSampler:
79         switch (sampler.type) {
80 #ifdef AMD_EXTENSIONS
81         case EbtFloat16: mangledName += "f16"; break;
82 #endif
83         case EbtInt:   mangledName += "i"; break;
84         case EbtUint:  mangledName += "u"; break;
85         default: break; // some compilers want this
86         }
87         if (sampler.image)
88             mangledName += "I";  // a normal image
89         else if (sampler.sampler)
90             mangledName += "p";  // a "pure" sampler
91         else if (!sampler.combined)
92             mangledName += "t";  // a "pure" texture
93         else
94             mangledName += "s";  // traditional combined sampler
95         if (sampler.arrayed)
96             mangledName += "A";
97         if (sampler.shadow)
98             mangledName += "S";
99         if (sampler.external)
100             mangledName += "E";
101         switch (sampler.dim) {
102         case Esd1D:       mangledName += "1";  break;
103         case Esd2D:       mangledName += "2";  break;
104         case Esd3D:       mangledName += "3";  break;
105         case EsdCube:     mangledName += "C";  break;
106         case EsdRect:     mangledName += "R2"; break;
107         case EsdBuffer:   mangledName += "B";  break;
108         case EsdSubpass:  mangledName += "P";  break;
109         default: break; // some compilers want this
110         }
111
112         if (sampler.hasReturnStruct()) {
113             // Name mangle for sampler return struct uses struct table index.
114             mangledName += "-tx-struct";
115
116             char text[16]; // plenty enough space for the small integers.
117             snprintf(text, sizeof(text), "%d-", sampler.structReturnIndex);
118             mangledName += text;
119         } else {
120             switch (sampler.getVectorSize()) {
121             case 1: mangledName += "1"; break;
122             case 2: mangledName += "2"; break;
123             case 3: mangledName += "3"; break;
124             case 4: break; // default to prior name mangle behavior
125             }
126         }
127
128         if (sampler.ms)
129             mangledName += "M";
130         break;
131     case EbtStruct:
132     case EbtBlock:
133         if (basicType == EbtStruct)
134             mangledName += "struct-";
135         else
136             mangledName += "block-";
137         if (typeName)
138             mangledName += *typeName;
139         for (unsigned int i = 0; i < structure->size(); ++i) {
140             mangledName += '-';
141             (*structure)[i].type->buildMangledName(mangledName);
142         }
143     default:
144         break;
145     }
146
147     if (getVectorSize() > 0)
148         mangledName += static_cast<char>('0' + getVectorSize());
149     else {
150         mangledName += static_cast<char>('0' + getMatrixCols());
151         mangledName += static_cast<char>('0' + getMatrixRows());
152     }
153
154     if (arraySizes) {
155         const int maxSize = 11;
156         char buf[maxSize];
157         for (int i = 0; i < arraySizes->getNumDims(); ++i) {
158             if (arraySizes->getDimNode(i)) {
159                 if (arraySizes->getDimNode(i)->getAsSymbolNode())
160                     snprintf(buf, maxSize, "s%d", arraySizes->getDimNode(i)->getAsSymbolNode()->getId());
161                 else
162                     snprintf(buf, maxSize, "s%p", arraySizes->getDimNode(i));
163             } else
164                 snprintf(buf, maxSize, "%d", arraySizes->getDimSize(i));
165             mangledName += '[';
166             mangledName += buf;
167             mangledName += ']';
168         }
169     }
170 }
171
172 //
173 // Dump functions.
174 //
175
176 void TVariable::dump(TInfoSink& infoSink) const
177 {
178     infoSink.debug << getName().c_str() << ": " << type.getStorageQualifierString() << " " << type.getBasicTypeString();
179     if (type.isArray()) {
180         infoSink.debug << "[0]";
181     }
182     infoSink.debug << "\n";
183 }
184
185 void TFunction::dump(TInfoSink& infoSink) const
186 {
187     infoSink.debug << getName().c_str() << ": " <<  returnType.getBasicTypeString() << " " << getMangledName().c_str() << "\n";
188 }
189
190 void TAnonMember::dump(TInfoSink& TInfoSink) const
191 {
192     TInfoSink.debug << "anonymous member " << getMemberNumber() << " of " << getAnonContainer().getName().c_str() << "\n";
193 }
194
195 void TSymbolTableLevel::dump(TInfoSink &infoSink) const
196 {
197     tLevel::const_iterator it;
198     for (it = level.begin(); it != level.end(); ++it)
199         (*it).second->dump(infoSink);
200 }
201
202 void TSymbolTable::dump(TInfoSink &infoSink) const
203 {
204     for (int level = currentLevel(); level >= 0; --level) {
205         infoSink.debug << "LEVEL " << level << "\n";
206         table[level]->dump(infoSink);
207     }
208 }
209
210 //
211 // Functions have buried pointers to delete.
212 //
213 TFunction::~TFunction()
214 {
215     for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
216         delete (*i).type;
217 }
218
219 //
220 // Symbol table levels are a map of pointers to symbols that have to be deleted.
221 //
222 TSymbolTableLevel::~TSymbolTableLevel()
223 {
224     for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
225         delete (*it).second;
226
227     delete [] defaultPrecision;
228 }
229
230 //
231 // Change all function entries in the table with the non-mangled name
232 // to be related to the provided built-in operation.
233 //
234 void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
235 {
236     tLevel::const_iterator candidate = level.lower_bound(name);
237     while (candidate != level.end()) {
238         const TString& candidateName = (*candidate).first;
239         TString::size_type parenAt = candidateName.find_first_of('(');
240         if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
241             TFunction* function = (*candidate).second->getAsFunction();
242             function->relateToOperator(op);
243         } else
244             break;
245         ++candidate;
246     }
247 }
248
249 // Make all function overloads of the given name require an extension(s).
250 // Should only be used for a version/profile that actually needs the extension(s).
251 void TSymbolTableLevel::setFunctionExtensions(const char* name, int num, const char* const extensions[])
252 {
253     tLevel::const_iterator candidate = level.lower_bound(name);
254     while (candidate != level.end()) {
255         const TString& candidateName = (*candidate).first;
256         TString::size_type parenAt = candidateName.find_first_of('(');
257         if (parenAt != candidateName.npos && candidateName.compare(0, parenAt, name) == 0) {
258             TSymbol* symbol = candidate->second;
259             symbol->setExtensions(num, extensions);
260         } else
261             break;
262         ++candidate;
263     }
264 }
265
266 //
267 // Make all symbols in this table level read only.
268 //
269 void TSymbolTableLevel::readOnly()
270 {
271     for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
272         (*it).second->makeReadOnly();
273 }
274
275 //
276 // Copy a symbol, but the copy is writable; call readOnly() afterward if that's not desired.
277 //
278 TSymbol::TSymbol(const TSymbol& copyOf)
279 {
280     name = NewPoolTString(copyOf.name->c_str());
281     uniqueId = copyOf.uniqueId;
282     writable = true;
283 }
284
285 TVariable::TVariable(const TVariable& copyOf) : TSymbol(copyOf)
286 {
287     type.deepCopy(copyOf.type);
288     userType = copyOf.userType;
289     numExtensions = 0;
290     extensions = 0;
291     if (copyOf.numExtensions != 0)
292         setExtensions(copyOf.numExtensions, copyOf.extensions);
293
294     if (! copyOf.constArray.empty()) {
295         assert(! copyOf.type.isStruct());
296         TConstUnionArray newArray(copyOf.constArray, 0, copyOf.constArray.size());
297         constArray = newArray;
298     }
299
300     // don't support specialization-constant subtrees in cloned tables
301     constSubtree = nullptr;
302 }
303
304 TVariable* TVariable::clone() const
305 {
306     TVariable *variable = new TVariable(*this);
307
308     return variable;
309 }
310
311 TFunction::TFunction(const TFunction& copyOf) : TSymbol(copyOf)
312 {
313     for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
314         TParameter param;
315         parameters.push_back(param);
316         parameters.back().copyParam(copyOf.parameters[i]);
317     }
318
319     numExtensions = 0;
320     extensions = 0;
321     if (copyOf.extensions != 0)
322         setExtensions(copyOf.numExtensions, copyOf.extensions);
323     returnType.deepCopy(copyOf.returnType);
324     mangledName = copyOf.mangledName;
325     op = copyOf.op;
326     defined = copyOf.defined;
327     prototyped = copyOf.prototyped;
328     implicitThis = copyOf.implicitThis;
329     illegalImplicitThis = copyOf.illegalImplicitThis;
330     defaultParamCount = copyOf.defaultParamCount;
331 }
332
333 TFunction* TFunction::clone() const
334 {
335     TFunction *function = new TFunction(*this);
336
337     return function;
338 }
339
340 TAnonMember* TAnonMember::clone() const
341 {
342     // Anonymous members of a given block should be cloned at a higher level,
343     // where they can all be assured to still end up pointing to a single
344     // copy of the original container.
345     assert(0);
346
347     return 0;
348 }
349
350 TSymbolTableLevel* TSymbolTableLevel::clone() const
351 {
352     TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
353     symTableLevel->anonId = anonId;
354     symTableLevel->thisLevel = thisLevel;
355     std::vector<bool> containerCopied(anonId, false);
356     tLevel::const_iterator iter;
357     for (iter = level.begin(); iter != level.end(); ++iter) {
358         const TAnonMember* anon = iter->second->getAsAnonMember();
359         if (anon) {
360             // Insert all the anonymous members of this same container at once,
361             // avoid inserting the other members in the future, once this has been done,
362             // allowing them to all be part of the same new container.
363             if (! containerCopied[anon->getAnonId()]) {
364                 TVariable* container = anon->getAnonContainer().clone();
365                 container->changeName(NewPoolTString(""));
366                 // insert the whole container
367                 symTableLevel->insert(*container, false);
368                 containerCopied[anon->getAnonId()] = true;
369             }
370         } else
371             symTableLevel->insert(*iter->second->clone(), false);
372     }
373
374     return symTableLevel;
375 }
376
377 void TSymbolTable::copyTable(const TSymbolTable& copyOf)
378 {
379     assert(adoptedLevels == copyOf.adoptedLevels);
380
381     uniqueId = copyOf.uniqueId;
382     noBuiltInRedeclarations = copyOf.noBuiltInRedeclarations;
383     separateNameSpaces = copyOf.separateNameSpaces;
384     for (unsigned int i = copyOf.adoptedLevels; i < copyOf.table.size(); ++i)
385         table.push_back(copyOf.table[i]->clone());
386 }
387
388 } // end namespace glslang