blob: b246a0f692d33782dea5f0558dd4efd4060aa6f1 [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)
Jamie Madilld7b1ab52016-12-12 14:42:19 -050013#pragma warning(disable : 4718)
apatrick@chromium.orge057c5d2012-01-26 19:18:24 +000014#endif
15
Geoff Lang17732822013-08-29 13:46:49 -040016#include "compiler/translator/SymbolTable.h"
Olli Etuaho01d0ad02017-01-22 14:51:23 -080017
Dmitry Skiba01971112015-07-10 14:54:00 -040018#include "compiler/translator/Cache.h"
Olli Etuaho01d0ad02017-01-22 14:51:23 -080019#include "compiler/translator/IntermNode.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000021#include <stdio.h>
kbr@chromium.org476541f2011-10-27 21:14:51 +000022#include <algorithm>
23
Jamie Madill45bcc782016-11-07 13:58:48 -050024namespace sh
25{
26
Jamie Madillbfa91f42014-06-05 15:45:18 -040027int TSymbolTable::uniqueIdCounter = 0;
Nicolas Capensbd10cf52013-06-20 09:51:51 -040028
Olli Etuaho476197f2016-10-11 13:59:08 +010029TSymbol::TSymbol(const TString *n) : uniqueId(TSymbolTable::nextUniqueId()), name(n)
30{
31}
32
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034// Functions have buried pointers to delete.
35//
36TFunction::~TFunction()
37{
Olli Etuaho476197f2016-10-11 13:59:08 +010038 clearParameters();
39}
40
41void TFunction::clearParameters()
42{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000043 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
44 delete (*i).type;
Olli Etuaho476197f2016-10-11 13:59:08 +010045 parameters.clear();
46 mangledName = nullptr;
47}
48
49void TFunction::swapParameters(const TFunction &parametersSource)
50{
51 clearParameters();
52 for (auto parameter : parametersSource.parameters)
53 {
54 addParameter(parameter);
55 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000056}
57
Dmitry Skiba58832202015-07-06 16:11:13 -070058const TString *TFunction::buildMangledName() const
59{
Cooper Partin149e6e62015-08-07 16:18:18 -070060 std::string newName = mangleName(getName()).c_str();
Dmitry Skiba58832202015-07-06 16:11:13 -070061
62 for (const auto &p : parameters)
63 {
Cooper Partin149e6e62015-08-07 16:18:18 -070064 newName += p.type->getMangledName().c_str();
Dmitry Skiba58832202015-07-06 16:11:13 -070065 }
66
Cooper Partin149e6e62015-08-07 16:18:18 -070067 return NewPoolTString(newName.c_str());
Dmitry Skiba58832202015-07-06 16:11:13 -070068}
69
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -080070const TString &TFunction::GetMangledNameFromCall(const TString &unmangledFunctionName,
71 TIntermSequence &arguments)
72{
73 std::string newName = mangleName(unmangledFunctionName).c_str();
74 for (TIntermNode *argument : arguments)
75 {
76 newName += argument->getAsTyped()->getType().getMangledName().c_str();
77 }
78 return *NewPoolTString(newName.c_str());
79}
80
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000081//
82// Symbol table levels are a map of pointers to symbols that have to be deleted.
83//
84TSymbolTableLevel::~TSymbolTableLevel()
85{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000086 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
87 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088}
89
Nicolas Capensadfffe42014-06-17 02:13:36 -040090bool TSymbolTableLevel::insert(TSymbol *symbol)
Jamie Madillbfa91f42014-06-05 15:45:18 -040091{
Jamie Madillbfa91f42014-06-05 15:45:18 -040092 // returning true means symbol was added to the table
Nicolas Capensadfffe42014-06-17 02:13:36 -040093 tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol));
Jamie Madillbfa91f42014-06-05 15:45:18 -040094
95 return result.second;
96}
97
Olli Etuahob2983c92015-03-18 14:02:46 +020098bool TSymbolTableLevel::insertUnmangled(TFunction *function)
99{
Olli Etuahob2983c92015-03-18 14:02:46 +0200100 // returning true means symbol was added to the table
101 tInsertResult result = level.insert(tLevelPair(function->getName(), function));
102
103 return result.second;
104}
105
Jamie Madillbfa91f42014-06-05 15:45:18 -0400106TSymbol *TSymbolTableLevel::find(const TString &name) const
107{
108 tLevel::const_iterator it = level.find(name);
109 if (it == level.end())
110 return 0;
111 else
112 return (*it).second;
113}
114
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500115TSymbol *TSymbolTable::find(const TString &name,
116 int shaderVersion,
117 bool *builtIn,
118 bool *sameScope) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000119{
120 int level = currentLevel();
121 TSymbol *symbol;
122
123 do
124 {
Martin Radeve93d24e2016-07-28 12:06:05 +0300125 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
126 level--;
127 if (level == ESSL3_BUILTINS && shaderVersion < 300)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700128 level--;
129 if (level == ESSL1_BUILTINS && shaderVersion != 100)
130 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000131
132 symbol = table[level]->find(name);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500133 } while (symbol == 0 && --level >= 0);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000134
135 if (builtIn)
136 *builtIn = (level <= LAST_BUILTIN_LEVEL);
137 if (sameScope)
138 *sameScope = (level == currentLevel());
139
140 return symbol;
141}
142
Zhenyao Mod7490962016-11-09 15:49:51 -0800143TSymbol *TSymbolTable::findGlobal(const TString &name) const
144{
145 ASSERT(table.size() > GLOBAL_LEVEL);
146 return table[GLOBAL_LEVEL]->find(name);
147}
148
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500149TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000150{
151 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
152 {
Martin Radeve93d24e2016-07-28 12:06:05 +0300153 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
154 level--;
155 if (level == ESSL3_BUILTINS && shaderVersion < 300)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700156 level--;
157 if (level == ESSL1_BUILTINS && shaderVersion != 100)
158 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000159
160 TSymbol *symbol = table[level]->find(name);
161
162 if (symbol)
163 return symbol;
164 }
165
166 return 0;
167}
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400168
Olli Etuaho01d0ad02017-01-22 14:51:23 -0800169TFunction *TSymbolTable::findBuiltInOp(TIntermAggregate *callNode, int shaderVersion) const
170{
171 ASSERT(!callNode->isConstructor());
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800172 ASSERT(!callNode->isFunctionCall());
Olli Etuaho01d0ad02017-01-22 14:51:23 -0800173 TString opString = GetOperatorString(callNode->getOp());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800174 TSymbol *sym = findBuiltIn(
175 TFunction::GetMangledNameFromCall(opString, *callNode->getSequence()), shaderVersion);
Olli Etuaho01d0ad02017-01-22 14:51:23 -0800176 ASSERT(sym != nullptr && sym->isFunction());
177
178 TFunction *builtInFunc = static_cast<TFunction *>(sym);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800179 ASSERT(builtInFunc->getParamCount() == callNode->getSequence()->size());
Olli Etuaho01d0ad02017-01-22 14:51:23 -0800180 return builtInFunc;
181}
182
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400183TSymbolTable::~TSymbolTable()
184{
185 while (table.size() > 0)
186 pop();
187}
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700188
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500189bool IsGenType(const TType *type)
190{
191 if (type)
192 {
193 TBasicType basicType = type->getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500194 return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType ||
195 basicType == EbtGenBType;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500196 }
197
198 return false;
199}
200
201bool IsVecType(const TType *type)
202{
203 if (type)
204 {
205 TBasicType basicType = type->getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500206 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec ||
207 basicType == EbtBVec;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500208 }
209
210 return false;
211}
212
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700213const TType *SpecificType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500214{
215 ASSERT(size >= 1 && size <= 4);
216
217 if (!type)
218 {
219 return nullptr;
220 }
221
222 ASSERT(!IsVecType(type));
223
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500224 switch (type->getBasicType())
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500225 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500226 case EbtGenType:
Olli Etuaho9250cb22017-01-21 10:51:27 +0000227 return TCache::getType(EbtFloat, type->getQualifier(),
228 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500229 case EbtGenIType:
Olli Etuaho9250cb22017-01-21 10:51:27 +0000230 return TCache::getType(EbtInt, type->getQualifier(), static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500231 case EbtGenUType:
Olli Etuaho9250cb22017-01-21 10:51:27 +0000232 return TCache::getType(EbtUInt, type->getQualifier(), static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500233 case EbtGenBType:
Olli Etuaho9250cb22017-01-21 10:51:27 +0000234 return TCache::getType(EbtBool, type->getQualifier(), static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500235 default:
236 return type;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500237 }
238}
239
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700240const TType *VectorType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500241{
242 ASSERT(size >= 2 && size <= 4);
243
244 if (!type)
245 {
246 return nullptr;
247 }
248
249 ASSERT(!IsGenType(type));
250
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500251 switch (type->getBasicType())
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500252 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500253 case EbtVec:
254 return TCache::getType(EbtFloat, static_cast<unsigned char>(size));
255 case EbtIVec:
256 return TCache::getType(EbtInt, static_cast<unsigned char>(size));
257 case EbtUVec:
258 return TCache::getType(EbtUInt, static_cast<unsigned char>(size));
259 case EbtBVec:
260 return TCache::getType(EbtBool, static_cast<unsigned char>(size));
261 default:
262 return type;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500263 }
264}
265
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500266void TSymbolTable::insertBuiltIn(ESymbolLevel level,
267 TOperator op,
268 const char *ext,
269 const TType *rvalue,
270 const char *name,
271 const TType *ptype1,
272 const TType *ptype2,
273 const TType *ptype3,
274 const TType *ptype4,
275 const TType *ptype5)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700276{
277 if (ptype1->getBasicType() == EbtGSampler2D)
278 {
Martin Radevda6254b2016-12-14 17:00:36 +0200279 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700280 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500281 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name,
282 TCache::getType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5);
283 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name,
284 TCache::getType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5);
285 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name,
286 TCache::getType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700287 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500288 else if (ptype1->getBasicType() == EbtGSampler3D)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700289 {
Martin Radevda6254b2016-12-14 17:00:36 +0200290 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700291 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500292 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name,
293 TCache::getType(EbtSampler3D), ptype2, ptype3, ptype4, ptype5);
294 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name,
295 TCache::getType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5);
296 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name,
297 TCache::getType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700298 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500299 else if (ptype1->getBasicType() == EbtGSamplerCube)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700300 {
Martin Radevda6254b2016-12-14 17:00:36 +0200301 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700302 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500303 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name,
304 TCache::getType(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5);
305 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name,
306 TCache::getType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5);
307 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name,
308 TCache::getType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700309 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500310 else if (ptype1->getBasicType() == EbtGSampler2DArray)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700311 {
Martin Radevda6254b2016-12-14 17:00:36 +0200312 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700313 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500314 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name,
315 TCache::getType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5);
316 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name,
317 TCache::getType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5);
318 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name,
319 TCache::getType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700320 }
JiangYizhou40219322016-12-09 09:50:51 +0800321 else if (ptype1->getBasicType() == EbtGSampler2DMS)
322 {
323 insertUnmangledBuiltInName(name, level);
324 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
325 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name,
326 TCache::getType(EbtSampler2DMS), ptype2, ptype3, ptype4, ptype5);
327 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name,
328 TCache::getType(EbtISampler2DMS), ptype2, ptype3, ptype4, ptype5);
329 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name,
330 TCache::getType(EbtUSampler2DMS), ptype2, ptype3, ptype4, ptype5);
331 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300332 else if (IsGImage(ptype1->getBasicType()))
333 {
Martin Radevda6254b2016-12-14 17:00:36 +0200334 insertUnmangledBuiltInName(name, level);
Martin Radev2cc85b32016-08-05 16:22:53 +0300335
336 const TType *floatType = TCache::getType(EbtFloat, 4);
337 const TType *intType = TCache::getType(EbtInt, 4);
338 const TType *unsignedType = TCache::getType(EbtUInt, 4);
339
340 const TType *floatImage =
341 TCache::getType(convertGImageToFloatImage(ptype1->getBasicType()));
342 const TType *intImage = TCache::getType(convertGImageToIntImage(ptype1->getBasicType()));
343 const TType *unsignedImage =
344 TCache::getType(convertGImageToUnsignedImage(ptype1->getBasicType()));
345
346 // GLSL ES 3.10, Revision 4, 8.12 Image Functions
347 if (rvalue->getBasicType() == EbtGVec4)
348 {
349 // imageLoad
350 insertBuiltIn(level, floatType, name, floatImage, ptype2, ptype3, ptype4, ptype5);
351 insertBuiltIn(level, intType, name, intImage, ptype2, ptype3, ptype4, ptype5);
352 insertBuiltIn(level, unsignedType, name, unsignedImage, ptype2, ptype3, ptype4, ptype5);
353 }
354 else if (rvalue->getBasicType() == EbtVoid)
355 {
356 // imageStore
357 insertBuiltIn(level, rvalue, name, floatImage, ptype2, floatType, ptype4, ptype5);
358 insertBuiltIn(level, rvalue, name, intImage, ptype2, intType, ptype4, ptype5);
359 insertBuiltIn(level, rvalue, name, unsignedImage, ptype2, unsignedType, ptype4, ptype5);
360 }
361 else
362 {
363 // imageSize
364 insertBuiltIn(level, rvalue, name, floatImage, ptype2, ptype3, ptype4, ptype5);
365 insertBuiltIn(level, rvalue, name, intImage, ptype2, ptype3, ptype4, ptype5);
366 insertBuiltIn(level, rvalue, name, unsignedImage, ptype2, ptype3, ptype4, ptype5);
367 }
368 }
Olli Etuaho9250cb22017-01-21 10:51:27 +0000369 else if (IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3) ||
370 IsGenType(ptype4))
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500371 {
Olli Etuaho9250cb22017-01-21 10:51:27 +0000372 ASSERT(!ptype5);
Martin Radevda6254b2016-12-14 17:00:36 +0200373 insertUnmangledBuiltInName(name, level);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500374 insertBuiltIn(level, op, ext, SpecificType(rvalue, 1), name, SpecificType(ptype1, 1),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000375 SpecificType(ptype2, 1), SpecificType(ptype3, 1), SpecificType(ptype4, 1));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500376 insertBuiltIn(level, op, ext, SpecificType(rvalue, 2), name, SpecificType(ptype1, 2),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000377 SpecificType(ptype2, 2), SpecificType(ptype3, 2), SpecificType(ptype4, 2));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500378 insertBuiltIn(level, op, ext, SpecificType(rvalue, 3), name, SpecificType(ptype1, 3),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000379 SpecificType(ptype2, 3), SpecificType(ptype3, 3), SpecificType(ptype4, 3));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500380 insertBuiltIn(level, op, ext, SpecificType(rvalue, 4), name, SpecificType(ptype1, 4),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000381 SpecificType(ptype2, 4), SpecificType(ptype3, 4), SpecificType(ptype4, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500382 }
383 else if (IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
384 {
385 ASSERT(!ptype4 && !ptype5);
Martin Radevda6254b2016-12-14 17:00:36 +0200386 insertUnmangledBuiltInName(name, level);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500387 insertBuiltIn(level, op, ext, VectorType(rvalue, 2), name, VectorType(ptype1, 2),
388 VectorType(ptype2, 2), VectorType(ptype3, 2));
389 insertBuiltIn(level, op, ext, VectorType(rvalue, 3), name, VectorType(ptype1, 3),
390 VectorType(ptype2, 3), VectorType(ptype3, 3));
391 insertBuiltIn(level, op, ext, VectorType(rvalue, 4), name, VectorType(ptype1, 4),
392 VectorType(ptype2, 4), VectorType(ptype3, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500393 }
394 else
395 {
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700396 TFunction *function = new TFunction(NewPoolTString(name), rvalue, op, ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700397
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700398 function->addParameter(TConstParameter(ptype1));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700399
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500400 if (ptype2)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700401 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700402 function->addParameter(TConstParameter(ptype2));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700403 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700404
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500405 if (ptype3)
406 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700407 function->addParameter(TConstParameter(ptype3));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500408 }
409
410 if (ptype4)
411 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700412 function->addParameter(TConstParameter(ptype4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500413 }
414
415 if (ptype5)
416 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700417 function->addParameter(TConstParameter(ptype5));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500418 }
419
Martin Radevda6254b2016-12-14 17:00:36 +0200420 ASSERT(hasUnmangledBuiltInAtLevel(name, level));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500421 insert(level, function);
422 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700423}
424
Olli Etuaho492cfab2017-01-20 21:18:29 +0000425void TSymbolTable::insertBuiltInOp(ESymbolLevel level,
426 TOperator op,
427 const TType *rvalue,
428 const TType *ptype1,
429 const TType *ptype2,
430 const TType *ptype3,
431 const TType *ptype4,
432 const TType *ptype5)
433{
434 const char *name = GetOperatorString(op);
435 ASSERT(strlen(name) > 0);
436 insertUnmangledBuiltInName(name, level);
437 insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
438}
439
440void TSymbolTable::insertBuiltInOp(ESymbolLevel level,
441 TOperator op,
442 const char *ext,
443 const TType *rvalue,
444 const TType *ptype1,
445 const TType *ptype2,
446 const TType *ptype3,
447 const TType *ptype4,
448 const TType *ptype5)
449{
450 const char *name = GetOperatorString(op);
451 insertUnmangledBuiltInName(name, level);
452 insertBuiltIn(level, op, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
453}
454
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300455void TSymbolTable::insertBuiltInFunctionNoParameters(ESymbolLevel level,
456 TOperator op,
457 const TType *rvalue,
458 const char *name)
459{
460 insertUnmangledBuiltInName(name, level);
461 insert(level, new TFunction(NewPoolTString(name), rvalue, op));
462}
463
Zhenyao Moe740add2014-07-18 17:01:01 -0700464TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700465{
466 if (!SupportsPrecision(type))
467 return EbpUndefined;
468
469 // unsigned integers use the same precision as signed
470 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
471
472 int level = static_cast<int>(precisionStack.size()) - 1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500473 assert(level >= 0); // Just to be safe. Should not happen.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200474 // If we dont find anything we return this. Some types don't have predefined default precision.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700475 TPrecision prec = EbpUndefined;
476 while (level >= 0)
477 {
478 PrecisionStackLevel::iterator it = precisionStack[level]->find(baseType);
479 if (it != precisionStack[level]->end())
480 {
481 prec = (*it).second;
482 break;
483 }
484 level--;
485 }
486 return prec;
487}
Jamie Madill45bcc782016-11-07 13:58:48 -0500488
Martin Radevda6254b2016-12-14 17:00:36 +0200489void TSymbolTable::insertUnmangledBuiltInName(const char *name, ESymbolLevel level)
490{
491 ASSERT(level >= 0 && level < static_cast<ESymbolLevel>(table.size()));
492 table[level]->insertUnmangledBuiltInName(std::string(name));
493}
494
495bool TSymbolTable::hasUnmangledBuiltInAtLevel(const char *name, ESymbolLevel level)
496{
497 ASSERT(level >= 0 && level < static_cast<ESymbolLevel>(table.size()));
498 return table[level]->hasUnmangledBuiltIn(std::string(name));
499}
500
501bool TSymbolTable::hasUnmangledBuiltInForShaderVersion(const char *name, int shaderVersion)
502{
503 ASSERT(static_cast<ESymbolLevel>(table.size()) > LAST_BUILTIN_LEVEL);
504
505 for (int level = LAST_BUILTIN_LEVEL; level >= 0; --level)
506 {
507 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
508 {
509 --level;
510 }
511 if (level == ESSL3_BUILTINS && shaderVersion < 300)
512 {
513 --level;
514 }
515 if (level == ESSL1_BUILTINS && shaderVersion != 100)
516 {
517 --level;
518 }
519
520 if (table[level]->hasUnmangledBuiltIn(name))
521 {
522 return true;
523 }
524 }
525 return false;
526}
527
Jamie Madill45bcc782016-11-07 13:58:48 -0500528} // namespace sh