blob: 68f029d0e63ccdd8de7846d32a39dc9a5059c08d [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// 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.com4f39fd92010-03-08 20:26:45 +000023//
24// TType helper function needs a place to live.
25//
26
27//
28// Recursively generate mangled names.
29//
30void TType::buildMangledName(TString& mangledName)
31{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000032 if (isMatrix())
33 mangledName += 'm';
34 else if (isVector())
35 mangledName += 'v';
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036
daniel@transgaming.com0578f812010-05-17 09:58:39 +000037 switch (type) {
38 case EbtFloat: mangledName += 'f'; break;
39 case EbtInt: mangledName += 'i'; break;
40 case EbtBool: mangledName += 'b'; break;
41 case EbtSampler2D: mangledName += "s2"; break;
42 case EbtSamplerCube: mangledName += "sC"; break;
43 case EbtStruct:
44 mangledName += "struct-";
45 if (typeName)
46 mangledName += *typeName;
47 {// support MSVC++6.0
48 for (unsigned int i = 0; i < structure->size(); ++i) {
49 mangledName += '-';
50 (*structure)[i].type->buildMangledName(mangledName);
51 }
52 }
53 default:
54 break;
55 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000056
daniel@transgaming.com0578f812010-05-17 09:58:39 +000057 mangledName += static_cast<char>('0' + getNominalSize());
58 if (isArray()) {
59 char buf[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000060 snprintf(buf, sizeof(buf), "%d", arraySize);
daniel@transgaming.com0578f812010-05-17 09:58:39 +000061 mangledName += '[';
62 mangledName += buf;
63 mangledName += ']';
64 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000065}
66
67int TType::getStructSize() const
68{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000069 if (!getStruct()) {
70 assert(false && "Not a struct");
71 return 0;
72 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000073
daniel@transgaming.com0578f812010-05-17 09:58:39 +000074 if (structureSize == 0)
alokp@chromium.org58e54292010-08-24 21:40:03 +000075 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); tl++)
daniel@transgaming.com0578f812010-05-17 09:58:39 +000076 structureSize += ((*tl).type)->getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077
daniel@transgaming.com0578f812010-05-17 09:58:39 +000078 return structureSize;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079}
80
kbr@chromium.org476541f2011-10-27 21:14:51 +000081void TType::computeDeepestStructNesting()
82{
83 if (!getStruct()) {
84 return;
85 }
86
87 int maxNesting = 0;
88 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); ++tl) {
89 maxNesting = std::max(maxNesting, ((*tl).type)->getDeepestStructNesting());
90 }
91
92 deepestStructNesting = 1 + maxNesting;
93}
94
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095//
96// Dump functions.
97//
98
99void TVariable::dump(TInfoSink& infoSink) const
100{
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000101 infoSink.debug << getName().c_str() << ": " << type.getQualifierString() << " " << type.getPrecisionString() << " " << type.getBasicString();
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000102 if (type.isArray()) {
103 infoSink.debug << "[0]";
104 }
105 infoSink.debug << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106}
107
108void TFunction::dump(TInfoSink &infoSink) const
109{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000110 infoSink.debug << getName().c_str() << ": " << returnType.getBasicString() << " " << getMangledName().c_str() << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111}
112
113void TSymbolTableLevel::dump(TInfoSink &infoSink) const
114{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000115 tLevel::const_iterator it;
116 for (it = level.begin(); it != level.end(); ++it)
117 (*it).second->dump(infoSink);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118}
119
120void TSymbolTable::dump(TInfoSink &infoSink) const
121{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000122 for (int level = currentLevel(); level >= 0; --level) {
123 infoSink.debug << "LEVEL " << level << "\n";
124 table[level]->dump(infoSink);
125 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000126}
127
128//
129// Functions have buried pointers to delete.
130//
131TFunction::~TFunction()
132{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000133 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
134 delete (*i).type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135}
136
137//
138// Symbol table levels are a map of pointers to symbols that have to be deleted.
139//
140TSymbolTableLevel::~TSymbolTableLevel()
141{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000142 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
143 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144}
145
146//
147// Change all function entries in the table with the non-mangled name
148// to be related to the provided built-in operation. This is a low
149// performance operation, and only intended for symbol tables that
150// live across a large number of compiles.
151//
152void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
153{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000154 tLevel::iterator it;
155 for (it = level.begin(); it != level.end(); ++it) {
156 if ((*it).second->isFunction()) {
157 TFunction* function = static_cast<TFunction*>((*it).second);
158 if (function->getName() == name)
159 function->relateToOperator(op);
160 }
161 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000162}
163
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000164//
165// Change all function entries in the table with the non-mangled name
166// to be related to the provided built-in extension. This is a low
167// performance operation, and only intended for symbol tables that
168// live across a large number of compiles.
169//
170void TSymbolTableLevel::relateToExtension(const char* name, const TString& ext)
171{
172 for (tLevel::iterator it = level.begin(); it != level.end(); ++it) {
173 if (it->second->isFunction()) {
174 TFunction* function = static_cast<TFunction*>(it->second);
175 if (function->getName() == name)
176 function->relateToExtension(ext);
177 }
178 }
179}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180
181TSymbol::TSymbol(const TSymbol& copyOf)
182{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000183 name = NewPoolTString(copyOf.name->c_str());
184 uniqueId = copyOf.uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000185}
186
187TVariable::TVariable(const TVariable& copyOf, TStructureMap& remapper) : TSymbol(copyOf)
188{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000189 type.copyType(copyOf.type, remapper);
190 userType = copyOf.userType;
191 // for builtIn symbol table level, unionArray and arrayInformation pointers should be NULL
192 assert(copyOf.arrayInformationType == 0);
193 arrayInformationType = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000195 if (copyOf.unionArray) {
196 assert(!copyOf.type.getStruct());
197 assert(copyOf.type.getObjectSize() == 1);
198 unionArray = new ConstantUnion[1];
199 unionArray[0] = copyOf.unionArray[0];
200 } else
201 unionArray = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000202}
203
204TVariable* TVariable::clone(TStructureMap& remapper)
205{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000206 TVariable *variable = new TVariable(*this, remapper);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000208 return variable;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209}
210
211TFunction::TFunction(const TFunction& copyOf, TStructureMap& remapper) : TSymbol(copyOf)
212{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000213 for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
214 TParameter param;
215 parameters.push_back(param);
216 parameters.back().copyParam(copyOf.parameters[i], remapper);
217 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000219 returnType.copyType(copyOf.returnType, remapper);
220 mangledName = copyOf.mangledName;
221 op = copyOf.op;
222 defined = copyOf.defined;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223}
224
225TFunction* TFunction::clone(TStructureMap& remapper)
226{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000227 TFunction *function = new TFunction(*this, remapper);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000229 return function;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230}
231
232TSymbolTableLevel* TSymbolTableLevel::clone(TStructureMap& remapper)
233{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000234 TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
235 tLevel::iterator iter;
236 for (iter = level.begin(); iter != level.end(); ++iter) {
237 symTableLevel->insert(*iter->second->clone(remapper));
238 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000239
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000240 return symTableLevel;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000241}
242
243void TSymbolTable::copyTable(const TSymbolTable& copyOf)
244{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000245 TStructureMap remapper;
246 uniqueId = copyOf.uniqueId;
247 for (unsigned int i = 0; i < copyOf.table.size(); ++i) {
248 table.push_back(copyOf.table[i]->clone(remapper));
249 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000250 for( unsigned int i = 0; i < copyOf.precisionStack.size(); i++) {
251 precisionStack.push_back( copyOf.precisionStack[i] );
252 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000253}