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