blob: 300c5093b6e95ff5dc97c63b8b0d32d5e9935002 [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;
52 case EbtSamplerCube: mangledName += "sC"; break;
Nicolas Capens344e7142013-06-24 15:39:21 -040053 case EbtISampler2D: mangledName += "is2"; break;
54 case EbtISamplerCube: mangledName += "isC"; break;
daniel@transgaming.com0578f812010-05-17 09:58:39 +000055 case EbtStruct:
56 mangledName += "struct-";
57 if (typeName)
58 mangledName += *typeName;
59 {// support MSVC++6.0
60 for (unsigned int i = 0; i < structure->size(); ++i) {
61 mangledName += '-';
62 (*structure)[i].type->buildMangledName(mangledName);
63 }
64 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +000065 break;
66 case EbtInterfaceBlock:
67 {
68 mangledName += "interface-block-";
69 if (typeName)
70 {
71 mangledName += *typeName;
72 }
73 for (unsigned int i = 0; i < structure->size(); ++i)
74 {
75 mangledName += '-';
76 (*structure)[i].type->buildMangledName(mangledName);
77 }
78 }
79 break;
daniel@transgaming.com0578f812010-05-17 09:58:39 +000080 default:
81 break;
82 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000083
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +000084 if (isMatrix())
85 {
86 mangledName += static_cast<char>('0' + getCols());
87 mangledName += static_cast<char>('x');
88 mangledName += static_cast<char>('0' + getRows());
89 }
90 else
91 {
92 mangledName += static_cast<char>('0' + getNominalSize());
93 }
daniel@transgaming.com0578f812010-05-17 09:58:39 +000094 if (isArray()) {
95 char buf[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000096 snprintf(buf, sizeof(buf), "%d", arraySize);
daniel@transgaming.com0578f812010-05-17 09:58:39 +000097 mangledName += '[';
98 mangledName += buf;
99 mangledName += ']';
100 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101}
102
103int TType::getStructSize() const
104{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000105 if (!getStruct()) {
106 assert(false && "Not a struct");
107 return 0;
108 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000110 if (structureSize == 0)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000111 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); tl++)
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000112 structureSize += ((*tl).type)->getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000114 return structureSize;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000115}
116
kbr@chromium.org476541f2011-10-27 21:14:51 +0000117void TType::computeDeepestStructNesting()
118{
119 if (!getStruct()) {
120 return;
121 }
122
123 int maxNesting = 0;
124 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); ++tl) {
125 maxNesting = std::max(maxNesting, ((*tl).type)->getDeepestStructNesting());
126 }
127
128 deepestStructNesting = 1 + maxNesting;
129}
130
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000131bool TType::isStructureContainingArrays() const
132{
133 if (!structure)
134 {
135 return false;
136 }
137
138 for (TTypeList::const_iterator member = structure->begin(); member != structure->end(); member++)
139 {
140 if (member->type->isArray() ||
141 member->type->isStructureContainingArrays())
142 {
143 return true;
144 }
145 }
146
147 return false;
148}
149
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151// Functions have buried pointers to delete.
152//
153TFunction::~TFunction()
154{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000155 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
156 delete (*i).type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157}
158
159//
160// Symbol table levels are a map of pointers to symbols that have to be deleted.
161//
162TSymbolTableLevel::~TSymbolTableLevel()
163{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000164 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
165 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000166}
167
168//
169// Change all function entries in the table with the non-mangled name
170// to be related to the provided built-in operation. This is a low
171// performance operation, and only intended for symbol tables that
172// live across a large number of compiles.
173//
174void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
175{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000176 tLevel::iterator it;
177 for (it = level.begin(); it != level.end(); ++it) {
178 if ((*it).second->isFunction()) {
179 TFunction* function = static_cast<TFunction*>((*it).second);
180 if (function->getName() == name)
181 function->relateToOperator(op);
182 }
183 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000184}
185
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000186//
187// Change all function entries in the table with the non-mangled name
188// to be related to the provided built-in extension. This is a low
189// performance operation, and only intended for symbol tables that
190// live across a large number of compiles.
191//
192void TSymbolTableLevel::relateToExtension(const char* name, const TString& ext)
193{
194 for (tLevel::iterator it = level.begin(); it != level.end(); ++it) {
195 if (it->second->isFunction()) {
196 TFunction* function = static_cast<TFunction*>(it->second);
197 if (function->getName() == name)
198 function->relateToExtension(ext);
199 }
200 }
201}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000202
203TSymbol::TSymbol(const TSymbol& copyOf)
204{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000205 name = NewPoolTString(copyOf.name->c_str());
206 uniqueId = copyOf.uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207}
208
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000209TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, bool *builtIn, bool *sameScope)
210{
211 int level = currentLevel();
212 TSymbol *symbol;
213
214 do
215 {
216 if (level == ESSL3_BUILTINS && shaderVersion != 300) level--;
217 if (level == ESSL1_BUILTINS && shaderVersion != 100) level--;
218
219 symbol = table[level]->find(name);
220 }
221 while (symbol == 0 && --level >= 0);
222
223 if (builtIn)
224 *builtIn = (level <= LAST_BUILTIN_LEVEL);
225 if (sameScope)
226 *sameScope = (level == currentLevel());
227
228 return symbol;
229}
230
231TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion)
232{
233 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
234 {
235 if (level == ESSL3_BUILTINS && shaderVersion != 300) level--;
236 if (level == ESSL1_BUILTINS && shaderVersion != 100) level--;
237
238 TSymbol *symbol = table[level]->find(name);
239
240 if (symbol)
241 return symbol;
242 }
243
244 return 0;
245}