blob: 396edc6a130ea4ee285b13d098d06556c9170c80 [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"
Dmitry Skiba01971112015-07-10 14:54:00 -040017#include "compiler/translator/Cache.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000019#include <stdio.h>
kbr@chromium.org476541f2011-10-27 21:14:51 +000020#include <algorithm>
21
Jamie Madillbfa91f42014-06-05 15:45:18 -040022int TSymbolTable::uniqueIdCounter = 0;
Nicolas Capensbd10cf52013-06-20 09:51:51 -040023
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000024//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025// Functions have buried pointers to delete.
26//
27TFunction::~TFunction()
28{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000029 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
30 delete (*i).type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031}
32
Dmitry Skiba58832202015-07-06 16:11:13 -070033const TString *TFunction::buildMangledName() const
34{
Cooper Partin149e6e62015-08-07 16:18:18 -070035 std::string newName = mangleName(getName()).c_str();
Dmitry Skiba58832202015-07-06 16:11:13 -070036
37 for (const auto &p : parameters)
38 {
Cooper Partin149e6e62015-08-07 16:18:18 -070039 newName += p.type->getMangledName().c_str();
Dmitry Skiba58832202015-07-06 16:11:13 -070040 }
41
Cooper Partin149e6e62015-08-07 16:18:18 -070042 return NewPoolTString(newName.c_str());
Dmitry Skiba58832202015-07-06 16:11:13 -070043}
44
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045//
46// Symbol table levels are a map of pointers to symbols that have to be deleted.
47//
48TSymbolTableLevel::~TSymbolTableLevel()
49{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000050 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
51 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052}
53
Nicolas Capensadfffe42014-06-17 02:13:36 -040054bool TSymbolTableLevel::insert(TSymbol *symbol)
Jamie Madillbfa91f42014-06-05 15:45:18 -040055{
Nicolas Capensadfffe42014-06-17 02:13:36 -040056 symbol->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madillbfa91f42014-06-05 15:45:18 -040057
58 // returning true means symbol was added to the table
Nicolas Capensadfffe42014-06-17 02:13:36 -040059 tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol));
Jamie Madillbfa91f42014-06-05 15:45:18 -040060
61 return result.second;
62}
63
Olli Etuahob2983c92015-03-18 14:02:46 +020064bool TSymbolTableLevel::insertUnmangled(TFunction *function)
65{
66 function->setUniqueId(TSymbolTable::nextUniqueId());
67
68 // returning true means symbol was added to the table
69 tInsertResult result = level.insert(tLevelPair(function->getName(), function));
70
71 return result.second;
72}
73
Jamie Madillbfa91f42014-06-05 15:45:18 -040074TSymbol *TSymbolTableLevel::find(const TString &name) const
75{
76 tLevel::const_iterator it = level.find(name);
77 if (it == level.end())
78 return 0;
79 else
80 return (*it).second;
81}
82
Zhenyao Moe740add2014-07-18 17:01:01 -070083TSymbol *TSymbolTable::find(const TString &name, int shaderVersion,
84 bool *builtIn, bool *sameScope) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000085{
86 int level = currentLevel();
87 TSymbol *symbol;
88
89 do
90 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -070091 if (level == ESSL3_BUILTINS && shaderVersion != 300)
92 level--;
93 if (level == ESSL1_BUILTINS && shaderVersion != 100)
94 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000095
96 symbol = table[level]->find(name);
97 }
98 while (symbol == 0 && --level >= 0);
99
100 if (builtIn)
101 *builtIn = (level <= LAST_BUILTIN_LEVEL);
102 if (sameScope)
103 *sameScope = (level == currentLevel());
104
105 return symbol;
106}
107
Zhenyao Moe740add2014-07-18 17:01:01 -0700108TSymbol *TSymbolTable::findBuiltIn(
109 const TString &name, int shaderVersion) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000110{
111 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
112 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700113 if (level == ESSL3_BUILTINS && shaderVersion != 300)
114 level--;
115 if (level == ESSL1_BUILTINS && shaderVersion != 100)
116 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000117
118 TSymbol *symbol = table[level]->find(name);
119
120 if (symbol)
121 return symbol;
122 }
123
124 return 0;
125}
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400126
127TSymbolTable::~TSymbolTable()
128{
129 while (table.size() > 0)
130 pop();
131}
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700132
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500133bool IsGenType(const TType *type)
134{
135 if (type)
136 {
137 TBasicType basicType = type->getBasicType();
138 return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType || basicType == EbtGenBType;
139 }
140
141 return false;
142}
143
144bool IsVecType(const TType *type)
145{
146 if (type)
147 {
148 TBasicType basicType = type->getBasicType();
149 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec || basicType == EbtBVec;
150 }
151
152 return false;
153}
154
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700155const TType *SpecificType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500156{
157 ASSERT(size >= 1 && size <= 4);
158
159 if (!type)
160 {
161 return nullptr;
162 }
163
164 ASSERT(!IsVecType(type));
165
166 switch(type->getBasicType())
167 {
Dmitry Skiba01971112015-07-10 14:54:00 -0400168 case EbtGenType: return TCache::getType(EbtFloat, static_cast<unsigned char>(size));
169 case EbtGenIType: return TCache::getType(EbtInt, static_cast<unsigned char>(size));
170 case EbtGenUType: return TCache::getType(EbtUInt, static_cast<unsigned char>(size));
171 case EbtGenBType: return TCache::getType(EbtBool, static_cast<unsigned char>(size));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500172 default: return type;
173 }
174}
175
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700176const TType *VectorType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500177{
178 ASSERT(size >= 2 && size <= 4);
179
180 if (!type)
181 {
182 return nullptr;
183 }
184
185 ASSERT(!IsGenType(type));
186
187 switch(type->getBasicType())
188 {
Dmitry Skiba01971112015-07-10 14:54:00 -0400189 case EbtVec: return TCache::getType(EbtFloat, static_cast<unsigned char>(size));
190 case EbtIVec: return TCache::getType(EbtInt, static_cast<unsigned char>(size));
191 case EbtUVec: return TCache::getType(EbtUInt, static_cast<unsigned char>(size));
192 case EbtBVec: return TCache::getType(EbtBool, static_cast<unsigned char>(size));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500193 default: return type;
194 }
195}
196
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700197void TSymbolTable::insertBuiltIn(ESymbolLevel level, TOperator op, const char *ext, const TType *rvalue, const char *name,
198 const TType *ptype1, const TType *ptype2, const TType *ptype3, const TType *ptype4, const TType *ptype5)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700199{
200 if (ptype1->getBasicType() == EbtGSampler2D)
201 {
202 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Dmitry Skiba01971112015-07-10 14:54:00 -0400203 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name, TCache::getType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5);
204 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name, TCache::getType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5);
205 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name, TCache::getType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700206 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500207 else if (ptype1->getBasicType() == EbtGSampler3D)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700208 {
209 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Dmitry Skiba01971112015-07-10 14:54:00 -0400210 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name, TCache::getType(EbtSampler3D), ptype2, ptype3, ptype4, ptype5);
211 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name, TCache::getType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5);
212 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name, TCache::getType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700213 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500214 else if (ptype1->getBasicType() == EbtGSamplerCube)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700215 {
216 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Dmitry Skiba01971112015-07-10 14:54:00 -0400217 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name, TCache::getType(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5);
218 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name, TCache::getType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5);
219 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name, TCache::getType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700220 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500221 else if (ptype1->getBasicType() == EbtGSampler2DArray)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700222 {
223 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Dmitry Skiba01971112015-07-10 14:54:00 -0400224 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name, TCache::getType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5);
225 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name, TCache::getType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5);
226 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name, TCache::getType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700227 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500228 else if (IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3))
229 {
230 ASSERT(!ptype4 && !ptype5);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500231 insertBuiltIn(level, op, ext, SpecificType(rvalue, 1), name, SpecificType(ptype1, 1), SpecificType(ptype2, 1), SpecificType(ptype3, 1));
232 insertBuiltIn(level, op, ext, SpecificType(rvalue, 2), name, SpecificType(ptype1, 2), SpecificType(ptype2, 2), SpecificType(ptype3, 2));
233 insertBuiltIn(level, op, ext, SpecificType(rvalue, 3), name, SpecificType(ptype1, 3), SpecificType(ptype2, 3), SpecificType(ptype3, 3));
234 insertBuiltIn(level, op, ext, SpecificType(rvalue, 4), name, SpecificType(ptype1, 4), SpecificType(ptype2, 4), SpecificType(ptype3, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500235 }
236 else if (IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
237 {
238 ASSERT(!ptype4 && !ptype5);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500239 insertBuiltIn(level, op, ext, VectorType(rvalue, 2), name, VectorType(ptype1, 2), VectorType(ptype2, 2), VectorType(ptype3, 2));
240 insertBuiltIn(level, op, ext, VectorType(rvalue, 3), name, VectorType(ptype1, 3), VectorType(ptype2, 3), VectorType(ptype3, 3));
241 insertBuiltIn(level, op, ext, VectorType(rvalue, 4), name, VectorType(ptype1, 4), VectorType(ptype2, 4), VectorType(ptype3, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500242 }
243 else
244 {
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700245 TFunction *function = new TFunction(NewPoolTString(name), rvalue, op, ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700246
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700247 function->addParameter(TConstParameter(ptype1));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700248
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500249 if (ptype2)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700250 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700251 function->addParameter(TConstParameter(ptype2));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700252 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700253
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500254 if (ptype3)
255 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700256 function->addParameter(TConstParameter(ptype3));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500257 }
258
259 if (ptype4)
260 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700261 function->addParameter(TConstParameter(ptype4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500262 }
263
264 if (ptype5)
265 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700266 function->addParameter(TConstParameter(ptype5));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500267 }
268
269 insert(level, function);
270 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700271}
272
Zhenyao Moe740add2014-07-18 17:01:01 -0700273TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700274{
275 if (!SupportsPrecision(type))
276 return EbpUndefined;
277
278 // unsigned integers use the same precision as signed
279 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
280
281 int level = static_cast<int>(precisionStack.size()) - 1;
282 assert(level >= 0); // Just to be safe. Should not happen.
283 // If we dont find anything we return this. Should we error check this?
284 TPrecision prec = EbpUndefined;
285 while (level >= 0)
286 {
287 PrecisionStackLevel::iterator it = precisionStack[level]->find(baseType);
288 if (it != precisionStack[level]->end())
289 {
290 prec = (*it).second;
291 break;
292 }
293 level--;
294 }
295 return prec;
296}