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