blob: 8d3d76a32780c54c10013631cdedd48353737479 [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
Nicolas Capensbd10cf52013-06-20 09:51:51 -040023int TSymbolTableLevel::uniqueId = 0;
24
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +000025TType::TType(const TPublicType &p) :
Jamie Madilla5efff92013-06-06 11:56:47 -040026 type(p.type), precision(p.precision), qualifier(p.qualifier), primarySize(p.primarySize), secondarySize(p.secondarySize), array(p.array), layoutQualifier(p.layoutQualifier), arraySize(p.arraySize),
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +000027 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 +000028{
29 if (p.userDef) {
30 structure = p.userDef->getStruct();
31 typeName = NewPoolTString(p.userDef->getTypeName().c_str());
32 computeDeepestStructNesting();
33 }
34}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035
36//
37// Recursively generate mangled names.
38//
39void TType::buildMangledName(TString& mangledName)
40{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000041 if (isMatrix())
42 mangledName += 'm';
43 else if (isVector())
44 mangledName += 'v';
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045
daniel@transgaming.com0578f812010-05-17 09:58:39 +000046 switch (type) {
47 case EbtFloat: mangledName += 'f'; break;
48 case EbtInt: mangledName += 'i'; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +000049 case EbtUInt: mangledName += 'u'; break;
daniel@transgaming.com0578f812010-05-17 09:58:39 +000050 case EbtBool: mangledName += 'b'; break;
51 case EbtSampler2D: mangledName += "s2"; break;
Nicolas Capensda075352013-06-24 16:02:56 -040052 case EbtSampler3D: mangledName += "s3"; break;
daniel@transgaming.com0578f812010-05-17 09:58:39 +000053 case EbtSamplerCube: mangledName += "sC"; break;
Nicolas Capens344e7142013-06-24 15:39:21 -040054 case EbtISampler2D: mangledName += "is2"; break;
Nicolas Capensda075352013-06-24 16:02:56 -040055 case EbtISampler3D: mangledName += "is3"; break;
Nicolas Capens344e7142013-06-24 15:39:21 -040056 case EbtISamplerCube: mangledName += "isC"; break;
Nicolas Capens2ffe0bb2013-06-24 15:56:19 -040057 case EbtUSampler2D: mangledName += "us2"; break;
Nicolas Capensda075352013-06-24 16:02:56 -040058 case EbtUSampler3D: mangledName += "us3"; break;
Nicolas Capens2ffe0bb2013-06-24 15:56:19 -040059 case EbtUSamplerCube: mangledName += "usC"; break;
daniel@transgaming.com0578f812010-05-17 09:58:39 +000060 case EbtStruct:
61 mangledName += "struct-";
62 if (typeName)
63 mangledName += *typeName;
64 {// support MSVC++6.0
65 for (unsigned int i = 0; i < structure->size(); ++i) {
66 mangledName += '-';
67 (*structure)[i].type->buildMangledName(mangledName);
68 }
69 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +000070 break;
71 case EbtInterfaceBlock:
72 {
73 mangledName += "interface-block-";
74 if (typeName)
75 {
76 mangledName += *typeName;
77 }
78 for (unsigned int i = 0; i < structure->size(); ++i)
79 {
80 mangledName += '-';
81 (*structure)[i].type->buildMangledName(mangledName);
82 }
83 }
84 break;
daniel@transgaming.com0578f812010-05-17 09:58:39 +000085 default:
86 break;
87 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +000089 if (isMatrix())
90 {
91 mangledName += static_cast<char>('0' + getCols());
92 mangledName += static_cast<char>('x');
93 mangledName += static_cast<char>('0' + getRows());
94 }
95 else
96 {
97 mangledName += static_cast<char>('0' + getNominalSize());
98 }
daniel@transgaming.com0578f812010-05-17 09:58:39 +000099 if (isArray()) {
100 char buf[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +0000101 snprintf(buf, sizeof(buf), "%d", arraySize);
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000102 mangledName += '[';
103 mangledName += buf;
104 mangledName += ']';
105 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106}
107
108int TType::getStructSize() const
109{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000110 if (!getStruct()) {
111 assert(false && "Not a struct");
112 return 0;
113 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000115 if (structureSize == 0)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000116 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); tl++)
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000117 structureSize += ((*tl).type)->getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000119 return structureSize;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120}
121
kbr@chromium.org476541f2011-10-27 21:14:51 +0000122void TType::computeDeepestStructNesting()
123{
124 if (!getStruct()) {
125 return;
126 }
127
128 int maxNesting = 0;
129 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); ++tl) {
130 maxNesting = std::max(maxNesting, ((*tl).type)->getDeepestStructNesting());
131 }
132
133 deepestStructNesting = 1 + maxNesting;
134}
135
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000136bool TType::isStructureContainingArrays() const
137{
138 if (!structure)
139 {
140 return false;
141 }
142
143 for (TTypeList::const_iterator member = structure->begin(); member != structure->end(); member++)
144 {
145 if (member->type->isArray() ||
146 member->type->isStructureContainingArrays())
147 {
148 return true;
149 }
150 }
151
152 return false;
153}
154
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000155//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156// Functions have buried pointers to delete.
157//
158TFunction::~TFunction()
159{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000160 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
161 delete (*i).type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000162}
163
164//
165// Symbol table levels are a map of pointers to symbols that have to be deleted.
166//
167TSymbolTableLevel::~TSymbolTableLevel()
168{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000169 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
170 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000171}
172
173//
174// Change all function entries in the table with the non-mangled name
175// to be related to the provided built-in operation. This is a low
176// performance operation, and only intended for symbol tables that
177// live across a large number of compiles.
178//
179void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
180{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000181 tLevel::iterator it;
182 for (it = level.begin(); it != level.end(); ++it) {
183 if ((*it).second->isFunction()) {
184 TFunction* function = static_cast<TFunction*>((*it).second);
185 if (function->getName() == name)
186 function->relateToOperator(op);
187 }
188 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189}
190
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000191//
192// Change all function entries in the table with the non-mangled name
193// to be related to the provided built-in extension. This is a low
194// performance operation, and only intended for symbol tables that
195// live across a large number of compiles.
196//
197void TSymbolTableLevel::relateToExtension(const char* name, const TString& ext)
198{
199 for (tLevel::iterator it = level.begin(); it != level.end(); ++it) {
200 if (it->second->isFunction()) {
201 TFunction* function = static_cast<TFunction*>(it->second);
202 if (function->getName() == name)
203 function->relateToExtension(ext);
204 }
205 }
206}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207
208TSymbol::TSymbol(const TSymbol& copyOf)
209{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000210 name = NewPoolTString(copyOf.name->c_str());
211 uniqueId = copyOf.uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000212}
213
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000214TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, bool *builtIn, bool *sameScope)
215{
216 int level = currentLevel();
217 TSymbol *symbol;
218
219 do
220 {
221 if (level == ESSL3_BUILTINS && shaderVersion != 300) level--;
222 if (level == ESSL1_BUILTINS && shaderVersion != 100) level--;
223
224 symbol = table[level]->find(name);
225 }
226 while (symbol == 0 && --level >= 0);
227
228 if (builtIn)
229 *builtIn = (level <= LAST_BUILTIN_LEVEL);
230 if (sameScope)
231 *sameScope = (level == currentLevel());
232
233 return symbol;
234}
235
236TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion)
237{
238 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
239 {
240 if (level == ESSL3_BUILTINS && shaderVersion != 300) level--;
241 if (level == ESSL1_BUILTINS && shaderVersion != 100) level--;
242
243 TSymbol *symbol = table[level]->find(name);
244
245 if (symbol)
246 return symbol;
247 }
248
249 return 0;
250}