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