blob: ebe2113804f2992cfd1e87319bd7a52d2dd7cf91 [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),
25 maxArraySize(0), arrayInformationType(0), structure(0), structureSize(0), deepestStructNesting(0), fieldName(0), mangled(0), typeName(0)
26{
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 }
60 default:
61 break;
62 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000063
daniel@transgaming.com0578f812010-05-17 09:58:39 +000064 mangledName += static_cast<char>('0' + getNominalSize());
65 if (isArray()) {
66 char buf[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000067 snprintf(buf, sizeof(buf), "%d", arraySize);
daniel@transgaming.com0578f812010-05-17 09:58:39 +000068 mangledName += '[';
69 mangledName += buf;
70 mangledName += ']';
71 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000072}
73
74int TType::getStructSize() const
75{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000076 if (!getStruct()) {
77 assert(false && "Not a struct");
78 return 0;
79 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080
daniel@transgaming.com0578f812010-05-17 09:58:39 +000081 if (structureSize == 0)
alokp@chromium.org58e54292010-08-24 21:40:03 +000082 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); tl++)
daniel@transgaming.com0578f812010-05-17 09:58:39 +000083 structureSize += ((*tl).type)->getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000084
daniel@transgaming.com0578f812010-05-17 09:58:39 +000085 return structureSize;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086}
87
kbr@chromium.org476541f2011-10-27 21:14:51 +000088void TType::computeDeepestStructNesting()
89{
90 if (!getStruct()) {
91 return;
92 }
93
94 int maxNesting = 0;
95 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); ++tl) {
96 maxNesting = std::max(maxNesting, ((*tl).type)->getDeepestStructNesting());
97 }
98
99 deepestStructNesting = 1 + maxNesting;
100}
101
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000102bool TType::isStructureContainingArrays() const
103{
104 if (!structure)
105 {
106 return false;
107 }
108
109 for (TTypeList::const_iterator member = structure->begin(); member != structure->end(); member++)
110 {
111 if (member->type->isArray() ||
112 member->type->isStructureContainingArrays())
113 {
114 return true;
115 }
116 }
117
118 return false;
119}
120
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121//
122// Dump functions.
123//
124
125void TVariable::dump(TInfoSink& infoSink) const
126{
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000127 infoSink.debug << getName().c_str() << ": " << type.getQualifierString() << " " << type.getPrecisionString() << " " << type.getBasicString();
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000128 if (type.isArray()) {
129 infoSink.debug << "[0]";
130 }
131 infoSink.debug << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132}
133
134void TFunction::dump(TInfoSink &infoSink) const
135{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000136 infoSink.debug << getName().c_str() << ": " << returnType.getBasicString() << " " << getMangledName().c_str() << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137}
138
139void TSymbolTableLevel::dump(TInfoSink &infoSink) const
140{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000141 tLevel::const_iterator it;
142 for (it = level.begin(); it != level.end(); ++it)
143 (*it).second->dump(infoSink);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144}
145
146void TSymbolTable::dump(TInfoSink &infoSink) const
147{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000148 for (int level = currentLevel(); level >= 0; --level) {
149 infoSink.debug << "LEVEL " << level << "\n";
150 table[level]->dump(infoSink);
151 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152}
153
154//
155// Functions have buried pointers to delete.
156//
157TFunction::~TFunction()
158{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000159 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
160 delete (*i).type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161}
162
163//
164// Symbol table levels are a map of pointers to symbols that have to be deleted.
165//
166TSymbolTableLevel::~TSymbolTableLevel()
167{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000168 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
169 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000170}
171
172//
173// Change all function entries in the table with the non-mangled name
174// to be related to the provided built-in operation. This is a low
175// performance operation, and only intended for symbol tables that
176// live across a large number of compiles.
177//
178void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
179{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000180 tLevel::iterator it;
181 for (it = level.begin(); it != level.end(); ++it) {
182 if ((*it).second->isFunction()) {
183 TFunction* function = static_cast<TFunction*>((*it).second);
184 if (function->getName() == name)
185 function->relateToOperator(op);
186 }
187 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188}
189
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000190//
191// Change all function entries in the table with the non-mangled name
192// to be related to the provided built-in extension. This is a low
193// performance operation, and only intended for symbol tables that
194// live across a large number of compiles.
195//
196void TSymbolTableLevel::relateToExtension(const char* name, const TString& ext)
197{
198 for (tLevel::iterator it = level.begin(); it != level.end(); ++it) {
199 if (it->second->isFunction()) {
200 TFunction* function = static_cast<TFunction*>(it->second);
201 if (function->getName() == name)
202 function->relateToExtension(ext);
203 }
204 }
205}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000206
207TSymbol::TSymbol(const TSymbol& copyOf)
208{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000209 name = NewPoolTString(copyOf.name->c_str());
210 uniqueId = copyOf.uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211}
212
213TVariable::TVariable(const TVariable& copyOf, TStructureMap& remapper) : TSymbol(copyOf)
214{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000215 type.copyType(copyOf.type, remapper);
216 userType = copyOf.userType;
217 // for builtIn symbol table level, unionArray and arrayInformation pointers should be NULL
218 assert(copyOf.arrayInformationType == 0);
219 arrayInformationType = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000220
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000221 if (copyOf.unionArray) {
222 assert(!copyOf.type.getStruct());
223 assert(copyOf.type.getObjectSize() == 1);
224 unionArray = new ConstantUnion[1];
225 unionArray[0] = copyOf.unionArray[0];
226 } else
227 unionArray = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228}
229
230TVariable* TVariable::clone(TStructureMap& remapper)
231{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000232 TVariable *variable = new TVariable(*this, remapper);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000234 return variable;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000235}
236
237TFunction::TFunction(const TFunction& copyOf, TStructureMap& remapper) : TSymbol(copyOf)
238{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000239 for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
240 TParameter param;
241 parameters.push_back(param);
242 parameters.back().copyParam(copyOf.parameters[i], remapper);
243 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000245 returnType.copyType(copyOf.returnType, remapper);
246 mangledName = copyOf.mangledName;
247 op = copyOf.op;
248 defined = copyOf.defined;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000249}
250
251TFunction* TFunction::clone(TStructureMap& remapper)
252{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000253 TFunction *function = new TFunction(*this, remapper);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000254
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000255 return function;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000256}
257
258TSymbolTableLevel* TSymbolTableLevel::clone(TStructureMap& remapper)
259{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000260 TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
261 tLevel::iterator iter;
262 for (iter = level.begin(); iter != level.end(); ++iter) {
263 symTableLevel->insert(*iter->second->clone(remapper));
264 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000266 return symTableLevel;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000267}
268
269void TSymbolTable::copyTable(const TSymbolTable& copyOf)
270{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000271 TStructureMap remapper;
272 uniqueId = copyOf.uniqueId;
273 for (unsigned int i = 0; i < copyOf.table.size(); ++i) {
274 table.push_back(copyOf.table[i]->clone(remapper));
275 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000276 for( unsigned int i = 0; i < copyOf.precisionStack.size(); i++) {
277 precisionStack.push_back( copyOf.precisionStack[i] );
278 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000279}
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000280
281TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, bool *builtIn, bool *sameScope)
282{
283 int level = currentLevel();
284 TSymbol *symbol;
285
286 do
287 {
288 if (level == ESSL3_BUILTINS && shaderVersion != 300) level--;
289 if (level == ESSL1_BUILTINS && shaderVersion != 100) level--;
290
291 symbol = table[level]->find(name);
292 }
293 while (symbol == 0 && --level >= 0);
294
295 if (builtIn)
296 *builtIn = (level <= LAST_BUILTIN_LEVEL);
297 if (sameScope)
298 *sameScope = (level == currentLevel());
299
300 return symbol;
301}
302
303TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion)
304{
305 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
306 {
307 if (level == ESSL3_BUILTINS && shaderVersion != 300) level--;
308 if (level == ESSL1_BUILTINS && shaderVersion != 100) level--;
309
310 TSymbol *symbol = table[level]->find(name);
311
312 if (symbol)
313 return symbol;
314 }
315
316 return 0;
317}