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