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