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