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