blob: d225b39073c1172be3e062a80f848a855dbb8b33 [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;
53 case EbtStruct:
54 mangledName += "struct-";
55 if (typeName)
56 mangledName += *typeName;
57 {// support MSVC++6.0
58 for (unsigned int i = 0; i < structure->size(); ++i) {
59 mangledName += '-';
60 (*structure)[i].type->buildMangledName(mangledName);
61 }
62 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +000063 break;
64 case EbtInterfaceBlock:
65 {
66 mangledName += "interface-block-";
67 if (typeName)
68 {
69 mangledName += *typeName;
70 }
71 for (unsigned int i = 0; i < structure->size(); ++i)
72 {
73 mangledName += '-';
74 (*structure)[i].type->buildMangledName(mangledName);
75 }
76 }
77 break;
daniel@transgaming.com0578f812010-05-17 09:58:39 +000078 default:
79 break;
80 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000081
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +000082 if (isMatrix())
83 {
84 mangledName += static_cast<char>('0' + getCols());
85 mangledName += static_cast<char>('x');
86 mangledName += static_cast<char>('0' + getRows());
87 }
88 else
89 {
90 mangledName += static_cast<char>('0' + getNominalSize());
91 }
daniel@transgaming.com0578f812010-05-17 09:58:39 +000092 if (isArray()) {
93 char buf[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000094 snprintf(buf, sizeof(buf), "%d", arraySize);
daniel@transgaming.com0578f812010-05-17 09:58:39 +000095 mangledName += '[';
96 mangledName += buf;
97 mangledName += ']';
98 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099}
100
101int TType::getStructSize() const
102{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000103 if (!getStruct()) {
104 assert(false && "Not a struct");
105 return 0;
106 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000108 if (structureSize == 0)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000109 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); tl++)
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000110 structureSize += ((*tl).type)->getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000112 return structureSize;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113}
114
kbr@chromium.org476541f2011-10-27 21:14:51 +0000115void TType::computeDeepestStructNesting()
116{
117 if (!getStruct()) {
118 return;
119 }
120
121 int maxNesting = 0;
122 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); ++tl) {
123 maxNesting = std::max(maxNesting, ((*tl).type)->getDeepestStructNesting());
124 }
125
126 deepestStructNesting = 1 + maxNesting;
127}
128
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000129bool TType::isStructureContainingArrays() const
130{
131 if (!structure)
132 {
133 return false;
134 }
135
136 for (TTypeList::const_iterator member = structure->begin(); member != structure->end(); member++)
137 {
138 if (member->type->isArray() ||
139 member->type->isStructureContainingArrays())
140 {
141 return true;
142 }
143 }
144
145 return false;
146}
147
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000148//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149// Functions have buried pointers to delete.
150//
151TFunction::~TFunction()
152{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000153 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
154 delete (*i).type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000155}
156
157//
158// Symbol table levels are a map of pointers to symbols that have to be deleted.
159//
160TSymbolTableLevel::~TSymbolTableLevel()
161{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000162 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
163 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164}
165
166//
167// Change all function entries in the table with the non-mangled name
168// to be related to the provided built-in operation. This is a low
169// performance operation, and only intended for symbol tables that
170// live across a large number of compiles.
171//
172void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
173{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000174 tLevel::iterator it;
175 for (it = level.begin(); it != level.end(); ++it) {
176 if ((*it).second->isFunction()) {
177 TFunction* function = static_cast<TFunction*>((*it).second);
178 if (function->getName() == name)
179 function->relateToOperator(op);
180 }
181 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000182}
183
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000184//
185// Change all function entries in the table with the non-mangled name
186// to be related to the provided built-in extension. This is a low
187// performance operation, and only intended for symbol tables that
188// live across a large number of compiles.
189//
190void TSymbolTableLevel::relateToExtension(const char* name, const TString& ext)
191{
192 for (tLevel::iterator it = level.begin(); it != level.end(); ++it) {
193 if (it->second->isFunction()) {
194 TFunction* function = static_cast<TFunction*>(it->second);
195 if (function->getName() == name)
196 function->relateToExtension(ext);
197 }
198 }
199}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000200
201TSymbol::TSymbol(const TSymbol& copyOf)
202{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000203 name = NewPoolTString(copyOf.name->c_str());
204 uniqueId = copyOf.uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205}
206
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000207TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, bool *builtIn, bool *sameScope)
208{
209 int level = currentLevel();
210 TSymbol *symbol;
211
212 do
213 {
214 if (level == ESSL3_BUILTINS && shaderVersion != 300) level--;
215 if (level == ESSL1_BUILTINS && shaderVersion != 100) level--;
216
217 symbol = table[level]->find(name);
218 }
219 while (symbol == 0 && --level >= 0);
220
221 if (builtIn)
222 *builtIn = (level <= LAST_BUILTIN_LEVEL);
223 if (sameScope)
224 *sameScope = (level == currentLevel());
225
226 return symbol;
227}
228
229TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion)
230{
231 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
232 {
233 if (level == ESSL3_BUILTINS && shaderVersion != 300) level--;
234 if (level == ESSL1_BUILTINS && shaderVersion != 100) level--;
235
236 TSymbol *symbol = table[level]->find(name);
237
238 if (symbol)
239 return symbol;
240 }
241
242 return 0;
243}