blob: ae1d7f2d603e98c2d21747c9cbf31ba44914ad4d [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7//
8// Symbol table for parsing. Most functionaliy and main ideas
9// are documented in the header file.
10//
11
apatrick@chromium.orge057c5d2012-01-26 19:18:24 +000012#if defined(_MSC_VER)
13#pragma warning(disable: 4718)
14#endif
15
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000016#include "compiler/SymbolTable.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000017
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000018#include <stdio.h>
kbr@chromium.org476541f2011-10-27 21:14:51 +000019#include <algorithm>
20
alokp@chromium.org79fb1012012-04-26 21:07:39 +000021#include "common/angleutils.h"
22
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +000023TType::TType(const TPublicType &p) :
24 type(p.type), precision(p.precision), qualifier(p.qualifier), size(p.size), matrix(p.matrix), array(p.array), arraySize(p.arraySize),
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +000025 maxArraySize(0), arrayInformationType(0), interfaceBlockType(0), structure(0), structureSize(0), deepestStructNesting(0), fieldName(0), mangled(0), typeName(0)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +000026{
27 if (p.userDef) {
28 structure = p.userDef->getStruct();
29 typeName = NewPoolTString(p.userDef->getTypeName().c_str());
30 computeDeepestStructNesting();
31 }
32}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033
34//
35// Recursively generate mangled names.
36//
37void TType::buildMangledName(TString& mangledName)
38{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000039 if (isMatrix())
40 mangledName += 'm';
41 else if (isVector())
42 mangledName += 'v';
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000043
daniel@transgaming.com0578f812010-05-17 09:58:39 +000044 switch (type) {
45 case EbtFloat: mangledName += 'f'; break;
46 case EbtInt: mangledName += 'i'; break;
47 case EbtBool: mangledName += 'b'; break;
48 case EbtSampler2D: mangledName += "s2"; break;
49 case EbtSamplerCube: mangledName += "sC"; break;
50 case EbtStruct:
51 mangledName += "struct-";
52 if (typeName)
53 mangledName += *typeName;
54 {// support MSVC++6.0
55 for (unsigned int i = 0; i < structure->size(); ++i) {
56 mangledName += '-';
57 (*structure)[i].type->buildMangledName(mangledName);
58 }
59 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +000060 break;
61 case EbtInterfaceBlock:
62 {
63 mangledName += "interface-block-";
64 if (typeName)
65 {
66 mangledName += *typeName;
67 }
68 for (unsigned int i = 0; i < structure->size(); ++i)
69 {
70 mangledName += '-';
71 (*structure)[i].type->buildMangledName(mangledName);
72 }
73 }
74 break;
daniel@transgaming.com0578f812010-05-17 09:58:39 +000075 default:
76 break;
77 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000078
daniel@transgaming.com0578f812010-05-17 09:58:39 +000079 mangledName += static_cast<char>('0' + getNominalSize());
80 if (isArray()) {
81 char buf[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000082 snprintf(buf, sizeof(buf), "%d", arraySize);
daniel@transgaming.com0578f812010-05-17 09:58:39 +000083 mangledName += '[';
84 mangledName += buf;
85 mangledName += ']';
86 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087}
88
89int TType::getStructSize() const
90{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000091 if (!getStruct()) {
92 assert(false && "Not a struct");
93 return 0;
94 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
daniel@transgaming.com0578f812010-05-17 09:58:39 +000096 if (structureSize == 0)
alokp@chromium.org58e54292010-08-24 21:40:03 +000097 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); tl++)
daniel@transgaming.com0578f812010-05-17 09:58:39 +000098 structureSize += ((*tl).type)->getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000100 return structureSize;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101}
102
kbr@chromium.org476541f2011-10-27 21:14:51 +0000103void TType::computeDeepestStructNesting()
104{
105 if (!getStruct()) {
106 return;
107 }
108
109 int maxNesting = 0;
110 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); ++tl) {
111 maxNesting = std::max(maxNesting, ((*tl).type)->getDeepestStructNesting());
112 }
113
114 deepestStructNesting = 1 + maxNesting;
115}
116
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000117bool TType::isStructureContainingArrays() const
118{
119 if (!structure)
120 {
121 return false;
122 }
123
124 for (TTypeList::const_iterator member = structure->begin(); member != structure->end(); member++)
125 {
126 if (member->type->isArray() ||
127 member->type->isStructureContainingArrays())
128 {
129 return true;
130 }
131 }
132
133 return false;
134}
135
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136//
137// Dump functions.
138//
139
140void TVariable::dump(TInfoSink& infoSink) const
141{
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000142 infoSink.debug << getName().c_str() << ": " << type.getQualifierString() << " " << type.getPrecisionString() << " " << type.getBasicString();
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000143 if (type.isArray()) {
144 infoSink.debug << "[0]";
145 }
146 infoSink.debug << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147}
148
149void TFunction::dump(TInfoSink &infoSink) const
150{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000151 infoSink.debug << getName().c_str() << ": " << returnType.getBasicString() << " " << getMangledName().c_str() << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152}
153
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000154void TInterfaceBlockName::dump(TInfoSink &infoSink) const
155{
156 infoSink.debug << "interface block " << getName().c_str() << "\n";
157}
158
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000159void TSymbolTableLevel::dump(TInfoSink &infoSink) const
160{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000161 tLevel::const_iterator it;
162 for (it = level.begin(); it != level.end(); ++it)
163 (*it).second->dump(infoSink);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164}
165
166void TSymbolTable::dump(TInfoSink &infoSink) const
167{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000168 for (int level = currentLevel(); level >= 0; --level) {
169 infoSink.debug << "LEVEL " << level << "\n";
170 table[level]->dump(infoSink);
171 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000172}
173
174//
175// Functions have buried pointers to delete.
176//
177TFunction::~TFunction()
178{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000179 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
180 delete (*i).type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181}
182
183//
184// Symbol table levels are a map of pointers to symbols that have to be deleted.
185//
186TSymbolTableLevel::~TSymbolTableLevel()
187{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000188 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
189 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000190}
191
192//
193// Change all function entries in the table with the non-mangled name
194// to be related to the provided built-in operation. This is a low
195// performance operation, and only intended for symbol tables that
196// live across a large number of compiles.
197//
198void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
199{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000200 tLevel::iterator it;
201 for (it = level.begin(); it != level.end(); ++it) {
202 if ((*it).second->isFunction()) {
203 TFunction* function = static_cast<TFunction*>((*it).second);
204 if (function->getName() == name)
205 function->relateToOperator(op);
206 }
207 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000208}
209
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000210//
211// Change all function entries in the table with the non-mangled name
212// to be related to the provided built-in extension. This is a low
213// performance operation, and only intended for symbol tables that
214// live across a large number of compiles.
215//
216void TSymbolTableLevel::relateToExtension(const char* name, const TString& ext)
217{
218 for (tLevel::iterator it = level.begin(); it != level.end(); ++it) {
219 if (it->second->isFunction()) {
220 TFunction* function = static_cast<TFunction*>(it->second);
221 if (function->getName() == name)
222 function->relateToExtension(ext);
223 }
224 }
225}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226
227TSymbol::TSymbol(const TSymbol& copyOf)
228{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000229 name = NewPoolTString(copyOf.name->c_str());
230 uniqueId = copyOf.uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000231}
232
233TVariable::TVariable(const TVariable& copyOf, TStructureMap& remapper) : TSymbol(copyOf)
234{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000235 type.copyType(copyOf.type, remapper);
236 userType = copyOf.userType;
237 // for builtIn symbol table level, unionArray and arrayInformation pointers should be NULL
238 assert(copyOf.arrayInformationType == 0);
239 arrayInformationType = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000240
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000241 if (copyOf.unionArray) {
242 assert(!copyOf.type.getStruct());
243 assert(copyOf.type.getObjectSize() == 1);
244 unionArray = new ConstantUnion[1];
245 unionArray[0] = copyOf.unionArray[0];
246 } else
247 unionArray = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000248}
249
250TVariable* TVariable::clone(TStructureMap& remapper)
251{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000252 TVariable *variable = new TVariable(*this, remapper);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000253
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000254 return variable;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000255}
256
257TFunction::TFunction(const TFunction& copyOf, TStructureMap& remapper) : TSymbol(copyOf)
258{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000259 for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
260 TParameter param;
261 parameters.push_back(param);
262 parameters.back().copyParam(copyOf.parameters[i], remapper);
263 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000264
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000265 returnType.copyType(copyOf.returnType, remapper);
266 mangledName = copyOf.mangledName;
267 op = copyOf.op;
268 defined = copyOf.defined;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269}
270
271TFunction* TFunction::clone(TStructureMap& remapper)
272{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000273 TFunction *function = new TFunction(*this, remapper);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000274
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000275 return function;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000276}
277
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000278TInterfaceBlockName* TInterfaceBlockName::clone(TStructureMap& remapper)
279{
280 return new TInterfaceBlockName(this->name);
281}
282
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283TSymbolTableLevel* TSymbolTableLevel::clone(TStructureMap& remapper)
284{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000285 TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
286 tLevel::iterator iter;
287 for (iter = level.begin(); iter != level.end(); ++iter) {
288 symTableLevel->insert(*iter->second->clone(remapper));
289 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000291 return symTableLevel;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292}
293
294void TSymbolTable::copyTable(const TSymbolTable& copyOf)
295{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000296 TStructureMap remapper;
297 uniqueId = copyOf.uniqueId;
298 for (unsigned int i = 0; i < copyOf.table.size(); ++i) {
299 table.push_back(copyOf.table[i]->clone(remapper));
300 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000301 for( unsigned int i = 0; i < copyOf.precisionStack.size(); i++) {
302 precisionStack.push_back( copyOf.precisionStack[i] );
303 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304}
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000305
306TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, bool *builtIn, bool *sameScope)
307{
308 int level = currentLevel();
309 TSymbol *symbol;
310
311 do
312 {
313 if (level == ESSL3_BUILTINS && shaderVersion != 300) level--;
314 if (level == ESSL1_BUILTINS && shaderVersion != 100) level--;
315
316 symbol = table[level]->find(name);
317 }
318 while (symbol == 0 && --level >= 0);
319
320 if (builtIn)
321 *builtIn = (level <= LAST_BUILTIN_LEVEL);
322 if (sameScope)
323 *sameScope = (level == currentLevel());
324
325 return symbol;
326}
327
328TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion)
329{
330 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
331 {
332 if (level == ESSL3_BUILTINS && shaderVersion != 300) level--;
333 if (level == ESSL1_BUILTINS && shaderVersion != 100) level--;
334
335 TSymbol *symbol = table[level]->find(name);
336
337 if (symbol)
338 return symbol;
339 }
340
341 return 0;
342}