blob: d97f0eccbb58e78a646cdbd06058a2a0aaf67516 [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
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000012#include "compiler/SymbolTable.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000013
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000014#include <stdio.h>
15
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000016//
17// TType helper function needs a place to live.
18//
19
20//
21// Recursively generate mangled names.
22//
23void TType::buildMangledName(TString& mangledName)
24{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000025 if (isMatrix())
26 mangledName += 'm';
27 else if (isVector())
28 mangledName += 'v';
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029
daniel@transgaming.com0578f812010-05-17 09:58:39 +000030 switch (type) {
31 case EbtFloat: mangledName += 'f'; break;
32 case EbtInt: mangledName += 'i'; break;
33 case EbtBool: mangledName += 'b'; break;
34 case EbtSampler2D: mangledName += "s2"; break;
35 case EbtSamplerCube: mangledName += "sC"; break;
36 case EbtStruct:
37 mangledName += "struct-";
38 if (typeName)
39 mangledName += *typeName;
40 {// support MSVC++6.0
41 for (unsigned int i = 0; i < structure->size(); ++i) {
42 mangledName += '-';
43 (*structure)[i].type->buildMangledName(mangledName);
44 }
45 }
46 default:
47 break;
48 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000049
daniel@transgaming.com0578f812010-05-17 09:58:39 +000050 mangledName += static_cast<char>('0' + getNominalSize());
51 if (isArray()) {
52 char buf[20];
53 sprintf(buf, "%d", arraySize);
54 mangledName += '[';
55 mangledName += buf;
56 mangledName += ']';
57 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000058}
59
60int TType::getStructSize() const
61{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000062 if (!getStruct()) {
63 assert(false && "Not a struct");
64 return 0;
65 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000066
daniel@transgaming.com0578f812010-05-17 09:58:39 +000067 if (structureSize == 0)
68 for (TTypeList::iterator tl = getStruct()->begin(); tl != getStruct()->end(); tl++)
69 structureSize += ((*tl).type)->getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000070
daniel@transgaming.com0578f812010-05-17 09:58:39 +000071 return structureSize;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000072}
73
74//
75// Dump functions.
76//
77
78void TVariable::dump(TInfoSink& infoSink) const
79{
daniel@transgaming.coma5d76232010-05-17 09:58:47 +000080 infoSink.debug << getName().c_str() << ": " << type.getQualifierString() << " " << type.getPrecisionString() << " " << type.getBasicString();
daniel@transgaming.com0578f812010-05-17 09:58:39 +000081 if (type.isArray()) {
82 infoSink.debug << "[0]";
83 }
84 infoSink.debug << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000085}
86
87void TFunction::dump(TInfoSink &infoSink) const
88{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000089 infoSink.debug << getName().c_str() << ": " << returnType.getBasicString() << " " << getMangledName().c_str() << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000090}
91
92void TSymbolTableLevel::dump(TInfoSink &infoSink) const
93{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000094 tLevel::const_iterator it;
95 for (it = level.begin(); it != level.end(); ++it)
96 (*it).second->dump(infoSink);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097}
98
99void TSymbolTable::dump(TInfoSink &infoSink) const
100{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000101 for (int level = currentLevel(); level >= 0; --level) {
102 infoSink.debug << "LEVEL " << level << "\n";
103 table[level]->dump(infoSink);
104 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105}
106
107//
108// Functions have buried pointers to delete.
109//
110TFunction::~TFunction()
111{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000112 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
113 delete (*i).type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114}
115
116//
117// Symbol table levels are a map of pointers to symbols that have to be deleted.
118//
119TSymbolTableLevel::~TSymbolTableLevel()
120{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000121 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
122 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123}
124
125//
126// Change all function entries in the table with the non-mangled name
127// to be related to the provided built-in operation. This is a low
128// performance operation, and only intended for symbol tables that
129// live across a large number of compiles.
130//
131void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
132{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000133 tLevel::iterator it;
134 for (it = level.begin(); it != level.end(); ++it) {
135 if ((*it).second->isFunction()) {
136 TFunction* function = static_cast<TFunction*>((*it).second);
137 if (function->getName() == name)
138 function->relateToOperator(op);
139 }
140 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141}
142
143
144TSymbol::TSymbol(const TSymbol& copyOf)
145{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000146 name = NewPoolTString(copyOf.name->c_str());
147 uniqueId = copyOf.uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000148}
149
150TVariable::TVariable(const TVariable& copyOf, TStructureMap& remapper) : TSymbol(copyOf)
151{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000152 type.copyType(copyOf.type, remapper);
153 userType = copyOf.userType;
154 // for builtIn symbol table level, unionArray and arrayInformation pointers should be NULL
155 assert(copyOf.arrayInformationType == 0);
156 arrayInformationType = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000158 if (copyOf.unionArray) {
159 assert(!copyOf.type.getStruct());
160 assert(copyOf.type.getObjectSize() == 1);
161 unionArray = new ConstantUnion[1];
162 unionArray[0] = copyOf.unionArray[0];
163 } else
164 unionArray = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000165}
166
167TVariable* TVariable::clone(TStructureMap& remapper)
168{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000169 TVariable *variable = new TVariable(*this, remapper);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000170
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000171 return variable;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000172}
173
174TFunction::TFunction(const TFunction& copyOf, TStructureMap& remapper) : TSymbol(copyOf)
175{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000176 for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
177 TParameter param;
178 parameters.push_back(param);
179 parameters.back().copyParam(copyOf.parameters[i], remapper);
180 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000182 returnType.copyType(copyOf.returnType, remapper);
183 mangledName = copyOf.mangledName;
184 op = copyOf.op;
185 defined = copyOf.defined;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186}
187
188TFunction* TFunction::clone(TStructureMap& remapper)
189{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000190 TFunction *function = new TFunction(*this, remapper);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000191
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000192 return function;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193}
194
195TSymbolTableLevel* TSymbolTableLevel::clone(TStructureMap& remapper)
196{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000197 TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
198 tLevel::iterator iter;
199 for (iter = level.begin(); iter != level.end(); ++iter) {
200 symTableLevel->insert(*iter->second->clone(remapper));
201 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000202
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000203 return symTableLevel;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000204}
205
206void TSymbolTable::copyTable(const TSymbolTable& copyOf)
207{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000208 TStructureMap remapper;
209 uniqueId = copyOf.uniqueId;
210 for (unsigned int i = 0; i < copyOf.table.size(); ++i) {
211 table.push_back(copyOf.table[i]->clone(remapper));
212 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000213 for( unsigned int i = 0; i < copyOf.precisionStack.size(); i++) {
214 precisionStack.push_back( copyOf.precisionStack[i] );
215 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000216}