blob: 4b2c19f9d1a7b0518968e9fa81206609fa966214 [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
Zhenyao Moe740add2014-07-18 17:01:01 -070060TSymbol *TSymbolTable::find(const TString &name, int shaderVersion,
61 bool *builtIn, bool *sameScope) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000062{
63 int level = currentLevel();
64 TSymbol *symbol;
65
66 do
67 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -070068 if (level == ESSL3_BUILTINS && shaderVersion != 300)
69 level--;
70 if (level == ESSL1_BUILTINS && shaderVersion != 100)
71 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000072
73 symbol = table[level]->find(name);
74 }
75 while (symbol == 0 && --level >= 0);
76
77 if (builtIn)
78 *builtIn = (level <= LAST_BUILTIN_LEVEL);
79 if (sameScope)
80 *sameScope = (level == currentLevel());
81
82 return symbol;
83}
84
Zhenyao Moe740add2014-07-18 17:01:01 -070085TSymbol *TSymbolTable::findBuiltIn(
86 const TString &name, int shaderVersion) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000087{
88 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
89 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -070090 if (level == ESSL3_BUILTINS && shaderVersion != 300)
91 level--;
92 if (level == ESSL1_BUILTINS && shaderVersion != 100)
93 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000094
95 TSymbol *symbol = table[level]->find(name);
96
97 if (symbol)
98 return symbol;
99 }
100
101 return 0;
102}
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400103
104TSymbolTable::~TSymbolTable()
105{
106 while (table.size() > 0)
107 pop();
108}
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700109
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500110bool IsGenType(const TType *type)
111{
112 if (type)
113 {
114 TBasicType basicType = type->getBasicType();
115 return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType || basicType == EbtGenBType;
116 }
117
118 return false;
119}
120
121bool IsVecType(const TType *type)
122{
123 if (type)
124 {
125 TBasicType basicType = type->getBasicType();
126 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec || basicType == EbtBVec;
127 }
128
129 return false;
130}
131
132TType *SpecificType(TType *type, int size)
133{
134 ASSERT(size >= 1 && size <= 4);
135
136 if (!type)
137 {
138 return nullptr;
139 }
140
141 ASSERT(!IsVecType(type));
142
143 switch(type->getBasicType())
144 {
145 case EbtGenType: return new TType(EbtFloat, size);
146 case EbtGenIType: return new TType(EbtInt, size);
147 case EbtGenUType: return new TType(EbtUInt, size);
148 case EbtGenBType: return new TType(EbtBool, size);
149 default: return type;
150 }
151}
152
153TType *VectorType(TType *type, int size)
154{
155 ASSERT(size >= 2 && size <= 4);
156
157 if (!type)
158 {
159 return nullptr;
160 }
161
162 ASSERT(!IsGenType(type));
163
164 switch(type->getBasicType())
165 {
166 case EbtVec: return new TType(EbtFloat, size);
167 case EbtIVec: return new TType(EbtInt, size);
168 case EbtUVec: return new TType(EbtUInt, size);
169 case EbtBVec: return new TType(EbtBool, size);
170 default: return type;
171 }
172}
173
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500174void TSymbolTable::insertBuiltIn(ESymbolLevel level, TOperator op, const char *ext, TType *rvalue, const char *name,
175 TType *ptype1, TType *ptype2, TType *ptype3, TType *ptype4, TType *ptype5)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700176{
177 if (ptype1->getBasicType() == EbtGSampler2D)
178 {
179 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500180 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5);
181 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5);
182 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700183 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500184 else if (ptype1->getBasicType() == EbtGSampler3D)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700185 {
186 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500187 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler3D), ptype2, ptype3, ptype4, ptype5);
188 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5);
189 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700190 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500191 else if (ptype1->getBasicType() == EbtGSamplerCube)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700192 {
193 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500194 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5);
195 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5);
196 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700197 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500198 else if (ptype1->getBasicType() == EbtGSampler2DArray)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700199 {
200 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500201 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5);
202 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5);
203 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700204 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500205 else if (IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3))
206 {
207 ASSERT(!ptype4 && !ptype5);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500208 insertBuiltIn(level, op, ext, SpecificType(rvalue, 1), name, SpecificType(ptype1, 1), SpecificType(ptype2, 1), SpecificType(ptype3, 1));
209 insertBuiltIn(level, op, ext, SpecificType(rvalue, 2), name, SpecificType(ptype1, 2), SpecificType(ptype2, 2), SpecificType(ptype3, 2));
210 insertBuiltIn(level, op, ext, SpecificType(rvalue, 3), name, SpecificType(ptype1, 3), SpecificType(ptype2, 3), SpecificType(ptype3, 3));
211 insertBuiltIn(level, op, ext, SpecificType(rvalue, 4), name, SpecificType(ptype1, 4), SpecificType(ptype2, 4), SpecificType(ptype3, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500212 }
213 else if (IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
214 {
215 ASSERT(!ptype4 && !ptype5);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500216 insertBuiltIn(level, op, ext, VectorType(rvalue, 2), name, VectorType(ptype1, 2), VectorType(ptype2, 2), VectorType(ptype3, 2));
217 insertBuiltIn(level, op, ext, VectorType(rvalue, 3), name, VectorType(ptype1, 3), VectorType(ptype2, 3), VectorType(ptype3, 3));
218 insertBuiltIn(level, op, ext, VectorType(rvalue, 4), name, VectorType(ptype1, 4), VectorType(ptype2, 4), VectorType(ptype3, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500219 }
220 else
221 {
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500222 TFunction *function = new TFunction(NewPoolTString(name), *rvalue, op, ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700223
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500224 TParameter param1 = {0, ptype1};
225 function->addParameter(param1);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700226
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500227 if (ptype2)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700228 {
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500229 TParameter param2 = {0, ptype2};
230 function->addParameter(param2);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700231 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700232
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500233 if (ptype3)
234 {
235 TParameter param3 = {0, ptype3};
236 function->addParameter(param3);
237 }
238
239 if (ptype4)
240 {
241 TParameter param4 = {0, ptype4};
242 function->addParameter(param4);
243 }
244
245 if (ptype5)
246 {
247 TParameter param5 = {0, ptype5};
248 function->addParameter(param5);
249 }
250
251 insert(level, function);
252 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700253}
254
Zhenyao Moe740add2014-07-18 17:01:01 -0700255TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700256{
257 if (!SupportsPrecision(type))
258 return EbpUndefined;
259
260 // unsigned integers use the same precision as signed
261 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
262
263 int level = static_cast<int>(precisionStack.size()) - 1;
264 assert(level >= 0); // Just to be safe. Should not happen.
265 // If we dont find anything we return this. Should we error check this?
266 TPrecision prec = EbpUndefined;
267 while (level >= 0)
268 {
269 PrecisionStackLevel::iterator it = precisionStack[level]->find(baseType);
270 if (it != precisionStack[level]->end())
271 {
272 prec = (*it).second;
273 break;
274 }
275 level--;
276 }
277 return prec;
278}