blob: 08dbccaee565d53bd4a6ee6e1720b88a8186b99e [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
Geoff Lang17732822013-08-29 13:46:49 -040016#include "compiler/translator/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
Jamie Madillbfa91f42014-06-05 15:45:18 -040021int TSymbolTable::uniqueIdCounter = 0;
Nicolas Capensbd10cf52013-06-20 09:51:51 -040022
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000023//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000024// Functions have buried pointers to delete.
25//
26TFunction::~TFunction()
27{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000028 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
29 delete (*i).type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030}
31
32//
33// Symbol table levels are a map of pointers to symbols that have to be deleted.
34//
35TSymbolTableLevel::~TSymbolTableLevel()
36{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000037 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
38 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039}
40
Nicolas Capensadfffe42014-06-17 02:13:36 -040041bool TSymbolTableLevel::insert(TSymbol *symbol)
Jamie Madillbfa91f42014-06-05 15:45:18 -040042{
Nicolas Capensadfffe42014-06-17 02:13:36 -040043 symbol->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madillbfa91f42014-06-05 15:45:18 -040044
45 // returning true means symbol was added to the table
Nicolas Capensadfffe42014-06-17 02:13:36 -040046 tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol));
Jamie Madillbfa91f42014-06-05 15:45:18 -040047
48 return result.second;
49}
50
Jamie Madillbfa91f42014-06-05 15:45:18 -040051TSymbol *TSymbolTableLevel::find(const TString &name) const
52{
53 tLevel::const_iterator it = level.find(name);
54 if (it == level.end())
55 return 0;
56 else
57 return (*it).second;
58}
59
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000060//
61// Change all function entries in the table with the non-mangled name
62// to be related to the provided built-in operation. This is a low
63// performance operation, and only intended for symbol tables that
64// live across a large number of compiles.
65//
Zhenyao Mo9eedea02014-05-12 16:02:35 -070066void TSymbolTableLevel::relateToOperator(const char *name, TOperator op)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000067{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070068 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
69 {
70 if ((*it).second->isFunction())
71 {
72 TFunction *function = static_cast<TFunction*>((*it).second);
daniel@transgaming.com0578f812010-05-17 09:58:39 +000073 if (function->getName() == name)
74 function->relateToOperator(op);
75 }
76 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077}
78
alokp@chromium.org8815d7f2010-09-09 17:30:03 +000079//
80// Change all function entries in the table with the non-mangled name
81// to be related to the provided built-in extension. This is a low
82// performance operation, and only intended for symbol tables that
83// live across a large number of compiles.
84//
Zhenyao Mo9eedea02014-05-12 16:02:35 -070085void TSymbolTableLevel::relateToExtension(const char *name, const TString &ext)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +000086{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070087 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
88 {
89 TSymbol *symbol = it->second;
90 if (symbol->getName() == name)
Jamie Madill2aeb26a2013-07-08 14:02:55 -040091 symbol->relateToExtension(ext);
alokp@chromium.org8815d7f2010-09-09 17:30:03 +000092 }
93}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094
Zhenyao Moe740add2014-07-18 17:01:01 -070095TSymbol *TSymbolTable::find(const TString &name, int shaderVersion,
96 bool *builtIn, bool *sameScope) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000097{
98 int level = currentLevel();
99 TSymbol *symbol;
100
101 do
102 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700103 if (level == ESSL3_BUILTINS && shaderVersion != 300)
104 level--;
105 if (level == ESSL1_BUILTINS && shaderVersion != 100)
106 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000107
108 symbol = table[level]->find(name);
109 }
110 while (symbol == 0 && --level >= 0);
111
112 if (builtIn)
113 *builtIn = (level <= LAST_BUILTIN_LEVEL);
114 if (sameScope)
115 *sameScope = (level == currentLevel());
116
117 return symbol;
118}
119
Zhenyao Moe740add2014-07-18 17:01:01 -0700120TSymbol *TSymbolTable::findBuiltIn(
121 const TString &name, int shaderVersion) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000122{
123 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
124 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700125 if (level == ESSL3_BUILTINS && shaderVersion != 300)
126 level--;
127 if (level == ESSL1_BUILTINS && shaderVersion != 100)
128 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000129
130 TSymbol *symbol = table[level]->find(name);
131
132 if (symbol)
133 return symbol;
134 }
135
136 return 0;
137}
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400138
139TSymbolTable::~TSymbolTable()
140{
141 while (table.size() > 0)
142 pop();
143}
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700144
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500145bool IsGenType(const TType *type)
146{
147 if (type)
148 {
149 TBasicType basicType = type->getBasicType();
150 return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType || basicType == EbtGenBType;
151 }
152
153 return false;
154}
155
156bool IsVecType(const TType *type)
157{
158 if (type)
159 {
160 TBasicType basicType = type->getBasicType();
161 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec || basicType == EbtBVec;
162 }
163
164 return false;
165}
166
167TType *SpecificType(TType *type, int size)
168{
169 ASSERT(size >= 1 && size <= 4);
170
171 if (!type)
172 {
173 return nullptr;
174 }
175
176 ASSERT(!IsVecType(type));
177
178 switch(type->getBasicType())
179 {
180 case EbtGenType: return new TType(EbtFloat, size);
181 case EbtGenIType: return new TType(EbtInt, size);
182 case EbtGenUType: return new TType(EbtUInt, size);
183 case EbtGenBType: return new TType(EbtBool, size);
184 default: return type;
185 }
186}
187
188TType *VectorType(TType *type, int size)
189{
190 ASSERT(size >= 2 && size <= 4);
191
192 if (!type)
193 {
194 return nullptr;
195 }
196
197 ASSERT(!IsGenType(type));
198
199 switch(type->getBasicType())
200 {
201 case EbtVec: return new TType(EbtFloat, size);
202 case EbtIVec: return new TType(EbtInt, size);
203 case EbtUVec: return new TType(EbtUInt, size);
204 case EbtBVec: return new TType(EbtBool, size);
205 default: return type;
206 }
207}
208
209void TSymbolTable::insertBuiltIn(ESymbolLevel level, TType *rvalue, const char *name, TType *ptype1, TType *ptype2, TType *ptype3, TType *ptype4, TType *ptype5)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700210{
211 if (ptype1->getBasicType() == EbtGSampler2D)
212 {
213 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500214 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5);
215 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5);
216 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700217 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500218 else if (ptype1->getBasicType() == EbtGSampler3D)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700219 {
220 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500221 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler3D), ptype2, ptype3, ptype4, ptype5);
222 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5);
223 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700224 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500225 else if (ptype1->getBasicType() == EbtGSamplerCube)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700226 {
227 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500228 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5);
229 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5);
230 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700231 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500232 else if (ptype1->getBasicType() == EbtGSampler2DArray)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700233 {
234 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500235 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5);
236 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5);
237 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700238 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500239 else if (IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3))
240 {
241 ASSERT(!ptype4 && !ptype5);
242 insertBuiltIn(level, SpecificType(rvalue, 1), name, SpecificType(ptype1, 1), SpecificType(ptype2, 1), SpecificType(ptype3, 1));
243 insertBuiltIn(level, SpecificType(rvalue, 2), name, SpecificType(ptype1, 2), SpecificType(ptype2, 2), SpecificType(ptype3, 2));
244 insertBuiltIn(level, SpecificType(rvalue, 3), name, SpecificType(ptype1, 3), SpecificType(ptype2, 3), SpecificType(ptype3, 3));
245 insertBuiltIn(level, SpecificType(rvalue, 4), name, SpecificType(ptype1, 4), SpecificType(ptype2, 4), SpecificType(ptype3, 4));
246 }
247 else if (IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
248 {
249 ASSERT(!ptype4 && !ptype5);
250 insertBuiltIn(level, VectorType(rvalue, 2), name, VectorType(ptype1, 2), VectorType(ptype2, 2), VectorType(ptype3, 2));
251 insertBuiltIn(level, VectorType(rvalue, 3), name, VectorType(ptype1, 3), VectorType(ptype2, 3), VectorType(ptype3, 3));
252 insertBuiltIn(level, VectorType(rvalue, 4), name, VectorType(ptype1, 4), VectorType(ptype2, 4), VectorType(ptype3, 4));
253 }
254 else
255 {
256 TFunction *function = new TFunction(NewPoolTString(name), *rvalue);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700257
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500258 TParameter param1 = {0, ptype1};
259 function->addParameter(param1);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700260
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500261 if (ptype2)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700262 {
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500263 TParameter param2 = {0, ptype2};
264 function->addParameter(param2);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700265 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700266
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500267 if (ptype3)
268 {
269 TParameter param3 = {0, ptype3};
270 function->addParameter(param3);
271 }
272
273 if (ptype4)
274 {
275 TParameter param4 = {0, ptype4};
276 function->addParameter(param4);
277 }
278
279 if (ptype5)
280 {
281 TParameter param5 = {0, ptype5};
282 function->addParameter(param5);
283 }
284
285 insert(level, function);
286 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700287}
288
Zhenyao Moe740add2014-07-18 17:01:01 -0700289TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700290{
291 if (!SupportsPrecision(type))
292 return EbpUndefined;
293
294 // unsigned integers use the same precision as signed
295 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
296
297 int level = static_cast<int>(precisionStack.size()) - 1;
298 assert(level >= 0); // Just to be safe. Should not happen.
299 // If we dont find anything we return this. Should we error check this?
300 TPrecision prec = EbpUndefined;
301 while (level >= 0)
302 {
303 PrecisionStackLevel::iterator it = precisionStack[level]->find(baseType);
304 if (it != precisionStack[level]->end())
305 {
306 prec = (*it).second;
307 break;
308 }
309 level--;
310 }
311 return prec;
312}