blob: c3ca92826d52e06a65796056068b43ab96f687e9 [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//
Olli Etuaho0f684632017-07-13 12:42:15 +03006// Symbol table for parsing. The design principles and most of the functionality are documented in
7// the header file.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008//
9
apatrick@chromium.orge057c5d2012-01-26 19:18:24 +000010#if defined(_MSC_VER)
Jamie Madilld7b1ab52016-12-12 14:42:19 -050011#pragma warning(disable : 4718)
apatrick@chromium.orge057c5d2012-01-26 19:18:24 +000012#endif
13
Geoff Lang17732822013-08-29 13:46:49 -040014#include "compiler/translator/SymbolTable.h"
Olli Etuaho01d0ad02017-01-22 14:51:23 -080015
Dmitry Skiba01971112015-07-10 14:54:00 -040016#include "compiler/translator/Cache.h"
Olli Etuaho01d0ad02017-01-22 14:51:23 -080017#include "compiler/translator/IntermNode.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 Madill45bcc782016-11-07 13:58:48 -050022namespace sh
23{
24
Olli Etuahof2209f72017-04-01 12:45:55 +030025namespace
26{
27
28static const char kFunctionMangledNameSeparator = '(';
29
30} // anonymous namespace
31
Olli Etuahoa5e693a2017-07-13 16:07:26 +030032TSymbolUniqueId::TSymbolUniqueId(TSymbolTable *symbolTable) : mId(symbolTable->nextUniqueId())
Olli Etuahofe486322017-03-21 09:30:54 +000033{
34}
35
36TSymbolUniqueId::TSymbolUniqueId(const TSymbol &symbol) : mId(symbol.getUniqueId())
37{
38}
39
40int TSymbolUniqueId::get() const
41{
42 return mId;
43}
44
Olli Etuahoa5e693a2017-07-13 16:07:26 +030045TSymbol::TSymbol(TSymbolTable *symbolTable, const TString *n)
46 : uniqueId(symbolTable->nextUniqueId()), name(n)
Olli Etuaho476197f2016-10-11 13:59:08 +010047{
48}
49
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000051// Functions have buried pointers to delete.
52//
53TFunction::~TFunction()
54{
Olli Etuaho476197f2016-10-11 13:59:08 +010055 clearParameters();
56}
57
58void TFunction::clearParameters()
59{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000060 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
61 delete (*i).type;
Olli Etuaho476197f2016-10-11 13:59:08 +010062 parameters.clear();
63 mangledName = nullptr;
64}
65
66void TFunction::swapParameters(const TFunction &parametersSource)
67{
68 clearParameters();
69 for (auto parameter : parametersSource.parameters)
70 {
71 addParameter(parameter);
72 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000073}
74
Dmitry Skiba58832202015-07-06 16:11:13 -070075const TString *TFunction::buildMangledName() const
76{
Olli Etuahof2209f72017-04-01 12:45:55 +030077 std::string newName = getName().c_str();
78 newName += kFunctionMangledNameSeparator;
Dmitry Skiba58832202015-07-06 16:11:13 -070079
80 for (const auto &p : parameters)
81 {
Cooper Partin149e6e62015-08-07 16:18:18 -070082 newName += p.type->getMangledName().c_str();
Dmitry Skiba58832202015-07-06 16:11:13 -070083 }
Cooper Partin149e6e62015-08-07 16:18:18 -070084 return NewPoolTString(newName.c_str());
Dmitry Skiba58832202015-07-06 16:11:13 -070085}
86
Olli Etuahof2209f72017-04-01 12:45:55 +030087const TString &TFunction::GetMangledNameFromCall(const TString &functionName,
88 const TIntermSequence &arguments)
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -080089{
Olli Etuahof2209f72017-04-01 12:45:55 +030090 std::string newName = functionName.c_str();
91 newName += kFunctionMangledNameSeparator;
92
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -080093 for (TIntermNode *argument : arguments)
94 {
95 newName += argument->getAsTyped()->getType().getMangledName().c_str();
96 }
97 return *NewPoolTString(newName.c_str());
98}
99
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100//
101// Symbol table levels are a map of pointers to symbols that have to be deleted.
102//
103TSymbolTableLevel::~TSymbolTableLevel()
104{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000105 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
106 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107}
108
Nicolas Capensadfffe42014-06-17 02:13:36 -0400109bool TSymbolTableLevel::insert(TSymbol *symbol)
Jamie Madillbfa91f42014-06-05 15:45:18 -0400110{
Jamie Madillbfa91f42014-06-05 15:45:18 -0400111 // returning true means symbol was added to the table
Nicolas Capensadfffe42014-06-17 02:13:36 -0400112 tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol));
Jamie Madillbfa91f42014-06-05 15:45:18 -0400113
114 return result.second;
115}
116
Olli Etuahob2983c92015-03-18 14:02:46 +0200117bool TSymbolTableLevel::insertUnmangled(TFunction *function)
118{
Olli Etuahob2983c92015-03-18 14:02:46 +0200119 // returning true means symbol was added to the table
120 tInsertResult result = level.insert(tLevelPair(function->getName(), function));
121
122 return result.second;
123}
124
Jamie Madillbfa91f42014-06-05 15:45:18 -0400125TSymbol *TSymbolTableLevel::find(const TString &name) const
126{
127 tLevel::const_iterator it = level.find(name);
128 if (it == level.end())
129 return 0;
130 else
131 return (*it).second;
132}
133
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500134TSymbol *TSymbolTable::find(const TString &name,
135 int shaderVersion,
136 bool *builtIn,
137 bool *sameScope) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000138{
139 int level = currentLevel();
140 TSymbol *symbol;
141
142 do
143 {
Martin Radeve93d24e2016-07-28 12:06:05 +0300144 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
145 level--;
146 if (level == ESSL3_BUILTINS && shaderVersion < 300)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700147 level--;
148 if (level == ESSL1_BUILTINS && shaderVersion != 100)
149 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000150
151 symbol = table[level]->find(name);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500152 } while (symbol == 0 && --level >= 0);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000153
154 if (builtIn)
155 *builtIn = (level <= LAST_BUILTIN_LEVEL);
156 if (sameScope)
157 *sameScope = (level == currentLevel());
158
159 return symbol;
160}
161
Zhenyao Mod7490962016-11-09 15:49:51 -0800162TSymbol *TSymbolTable::findGlobal(const TString &name) const
163{
164 ASSERT(table.size() > GLOBAL_LEVEL);
165 return table[GLOBAL_LEVEL]->find(name);
166}
167
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500168TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000169{
170 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
171 {
Martin Radeve93d24e2016-07-28 12:06:05 +0300172 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
173 level--;
174 if (level == ESSL3_BUILTINS && shaderVersion < 300)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700175 level--;
176 if (level == ESSL1_BUILTINS && shaderVersion != 100)
177 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000178
179 TSymbol *symbol = table[level]->find(name);
180
181 if (symbol)
182 return symbol;
183 }
184
185 return 0;
186}
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400187
188TSymbolTable::~TSymbolTable()
189{
190 while (table.size() > 0)
191 pop();
192}
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700193
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500194bool IsGenType(const TType *type)
195{
196 if (type)
197 {
198 TBasicType basicType = type->getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500199 return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType ||
200 basicType == EbtGenBType;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500201 }
202
203 return false;
204}
205
206bool IsVecType(const TType *type)
207{
208 if (type)
209 {
210 TBasicType basicType = type->getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500211 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec ||
212 basicType == EbtBVec;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500213 }
214
215 return false;
216}
217
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700218const TType *SpecificType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500219{
220 ASSERT(size >= 1 && size <= 4);
221
222 if (!type)
223 {
224 return nullptr;
225 }
226
227 ASSERT(!IsVecType(type));
228
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500229 switch (type->getBasicType())
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500230 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500231 case EbtGenType:
Olli Etuaho9250cb22017-01-21 10:51:27 +0000232 return TCache::getType(EbtFloat, type->getQualifier(),
233 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500234 case EbtGenIType:
Olli Etuaho9250cb22017-01-21 10:51:27 +0000235 return TCache::getType(EbtInt, type->getQualifier(), static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500236 case EbtGenUType:
Olli Etuaho9250cb22017-01-21 10:51:27 +0000237 return TCache::getType(EbtUInt, type->getQualifier(), static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500238 case EbtGenBType:
Olli Etuaho9250cb22017-01-21 10:51:27 +0000239 return TCache::getType(EbtBool, type->getQualifier(), static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500240 default:
241 return type;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500242 }
243}
244
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700245const TType *VectorType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500246{
247 ASSERT(size >= 2 && size <= 4);
248
249 if (!type)
250 {
251 return nullptr;
252 }
253
254 ASSERT(!IsGenType(type));
255
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500256 switch (type->getBasicType())
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500257 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500258 case EbtVec:
259 return TCache::getType(EbtFloat, static_cast<unsigned char>(size));
260 case EbtIVec:
261 return TCache::getType(EbtInt, static_cast<unsigned char>(size));
262 case EbtUVec:
263 return TCache::getType(EbtUInt, static_cast<unsigned char>(size));
264 case EbtBVec:
265 return TCache::getType(EbtBool, static_cast<unsigned char>(size));
266 default:
267 return type;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500268 }
269}
270
Olli Etuaho0f684632017-07-13 12:42:15 +0300271TVariable *TSymbolTable::declareVariable(const TString *name, const TType &type)
272{
273 return insertVariable(currentLevel(), name, type);
274}
275
276TVariable *TSymbolTable::declareStructType(TStructure *str)
277{
278 return insertStructType(currentLevel(), str);
279}
280
281TInterfaceBlockName *TSymbolTable::declareInterfaceBlockName(const TString *name)
282{
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300283 TInterfaceBlockName *blockNameSymbol = new TInterfaceBlockName(this, name);
Olli Etuaho0f684632017-07-13 12:42:15 +0300284 if (insert(currentLevel(), blockNameSymbol))
285 {
286 return blockNameSymbol;
287 }
288 return nullptr;
289}
290
291TVariable *TSymbolTable::insertVariable(ESymbolLevel level, const char *name, const TType &type)
292{
293 return insertVariable(level, NewPoolTString(name), type);
294}
295
296TVariable *TSymbolTable::insertVariable(ESymbolLevel level, const TString *name, const TType &type)
297{
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300298 TVariable *var = new TVariable(this, name, type);
Olli Etuaho0f684632017-07-13 12:42:15 +0300299 if (insert(level, var))
300 {
301 // Do lazy initialization for struct types, so we allocate to the current scope.
302 if (var->getType().getBasicType() == EbtStruct)
303 {
304 var->getType().realize();
305 }
306 return var;
307 }
308 return nullptr;
309}
310
311TVariable *TSymbolTable::insertVariableExt(ESymbolLevel level,
312 const char *ext,
313 const char *name,
314 const TType &type)
315{
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300316 TVariable *var = new TVariable(this, NewPoolTString(name), type);
Olli Etuaho0f684632017-07-13 12:42:15 +0300317 if (insert(level, ext, var))
318 {
319 if (var->getType().getBasicType() == EbtStruct)
320 {
321 var->getType().realize();
322 }
323 return var;
324 }
325 return nullptr;
326}
327
328TVariable *TSymbolTable::insertStructType(ESymbolLevel level, TStructure *str)
329{
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300330 TVariable *var = new TVariable(this, &str->name(), TType(str), true);
Olli Etuaho0f684632017-07-13 12:42:15 +0300331 if (insert(level, var))
332 {
333 var->getType().realize();
334 return var;
335 }
336 return nullptr;
337}
338
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500339void TSymbolTable::insertBuiltIn(ESymbolLevel level,
340 TOperator op,
341 const char *ext,
342 const TType *rvalue,
343 const char *name,
344 const TType *ptype1,
345 const TType *ptype2,
346 const TType *ptype3,
347 const TType *ptype4,
348 const TType *ptype5)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700349{
350 if (ptype1->getBasicType() == EbtGSampler2D)
351 {
Martin Radevda6254b2016-12-14 17:00:36 +0200352 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700353 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500354 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name,
355 TCache::getType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5);
356 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name,
357 TCache::getType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5);
358 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name,
359 TCache::getType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700360 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500361 else if (ptype1->getBasicType() == EbtGSampler3D)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700362 {
Martin Radevda6254b2016-12-14 17:00:36 +0200363 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700364 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500365 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name,
366 TCache::getType(EbtSampler3D), ptype2, ptype3, ptype4, ptype5);
367 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name,
368 TCache::getType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5);
369 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name,
370 TCache::getType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700371 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500372 else if (ptype1->getBasicType() == EbtGSamplerCube)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700373 {
Martin Radevda6254b2016-12-14 17:00:36 +0200374 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700375 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500376 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name,
377 TCache::getType(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5);
378 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name,
379 TCache::getType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5);
380 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name,
381 TCache::getType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700382 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500383 else if (ptype1->getBasicType() == EbtGSampler2DArray)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700384 {
Martin Radevda6254b2016-12-14 17:00:36 +0200385 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700386 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500387 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name,
388 TCache::getType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5);
389 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name,
390 TCache::getType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5);
391 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name,
392 TCache::getType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700393 }
JiangYizhou40219322016-12-09 09:50:51 +0800394 else if (ptype1->getBasicType() == EbtGSampler2DMS)
395 {
396 insertUnmangledBuiltInName(name, level);
397 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
398 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name,
399 TCache::getType(EbtSampler2DMS), ptype2, ptype3, ptype4, ptype5);
400 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name,
401 TCache::getType(EbtISampler2DMS), ptype2, ptype3, ptype4, ptype5);
402 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name,
403 TCache::getType(EbtUSampler2DMS), ptype2, ptype3, ptype4, ptype5);
404 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300405 else if (IsGImage(ptype1->getBasicType()))
406 {
Martin Radevda6254b2016-12-14 17:00:36 +0200407 insertUnmangledBuiltInName(name, level);
Martin Radev2cc85b32016-08-05 16:22:53 +0300408
409 const TType *floatType = TCache::getType(EbtFloat, 4);
410 const TType *intType = TCache::getType(EbtInt, 4);
411 const TType *unsignedType = TCache::getType(EbtUInt, 4);
412
413 const TType *floatImage =
414 TCache::getType(convertGImageToFloatImage(ptype1->getBasicType()));
415 const TType *intImage = TCache::getType(convertGImageToIntImage(ptype1->getBasicType()));
416 const TType *unsignedImage =
417 TCache::getType(convertGImageToUnsignedImage(ptype1->getBasicType()));
418
419 // GLSL ES 3.10, Revision 4, 8.12 Image Functions
420 if (rvalue->getBasicType() == EbtGVec4)
421 {
422 // imageLoad
423 insertBuiltIn(level, floatType, name, floatImage, ptype2, ptype3, ptype4, ptype5);
424 insertBuiltIn(level, intType, name, intImage, ptype2, ptype3, ptype4, ptype5);
425 insertBuiltIn(level, unsignedType, name, unsignedImage, ptype2, ptype3, ptype4, ptype5);
426 }
427 else if (rvalue->getBasicType() == EbtVoid)
428 {
429 // imageStore
430 insertBuiltIn(level, rvalue, name, floatImage, ptype2, floatType, ptype4, ptype5);
431 insertBuiltIn(level, rvalue, name, intImage, ptype2, intType, ptype4, ptype5);
432 insertBuiltIn(level, rvalue, name, unsignedImage, ptype2, unsignedType, ptype4, ptype5);
433 }
434 else
435 {
436 // imageSize
437 insertBuiltIn(level, rvalue, name, floatImage, ptype2, ptype3, ptype4, ptype5);
438 insertBuiltIn(level, rvalue, name, intImage, ptype2, ptype3, ptype4, ptype5);
439 insertBuiltIn(level, rvalue, name, unsignedImage, ptype2, ptype3, ptype4, ptype5);
440 }
441 }
Olli Etuaho9250cb22017-01-21 10:51:27 +0000442 else if (IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3) ||
443 IsGenType(ptype4))
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500444 {
Olli Etuaho9250cb22017-01-21 10:51:27 +0000445 ASSERT(!ptype5);
Martin Radevda6254b2016-12-14 17:00:36 +0200446 insertUnmangledBuiltInName(name, level);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500447 insertBuiltIn(level, op, ext, SpecificType(rvalue, 1), name, SpecificType(ptype1, 1),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000448 SpecificType(ptype2, 1), SpecificType(ptype3, 1), SpecificType(ptype4, 1));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500449 insertBuiltIn(level, op, ext, SpecificType(rvalue, 2), name, SpecificType(ptype1, 2),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000450 SpecificType(ptype2, 2), SpecificType(ptype3, 2), SpecificType(ptype4, 2));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500451 insertBuiltIn(level, op, ext, SpecificType(rvalue, 3), name, SpecificType(ptype1, 3),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000452 SpecificType(ptype2, 3), SpecificType(ptype3, 3), SpecificType(ptype4, 3));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500453 insertBuiltIn(level, op, ext, SpecificType(rvalue, 4), name, SpecificType(ptype1, 4),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000454 SpecificType(ptype2, 4), SpecificType(ptype3, 4), SpecificType(ptype4, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500455 }
456 else if (IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
457 {
458 ASSERT(!ptype4 && !ptype5);
Martin Radevda6254b2016-12-14 17:00:36 +0200459 insertUnmangledBuiltInName(name, level);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500460 insertBuiltIn(level, op, ext, VectorType(rvalue, 2), name, VectorType(ptype1, 2),
461 VectorType(ptype2, 2), VectorType(ptype3, 2));
462 insertBuiltIn(level, op, ext, VectorType(rvalue, 3), name, VectorType(ptype1, 3),
463 VectorType(ptype2, 3), VectorType(ptype3, 3));
464 insertBuiltIn(level, op, ext, VectorType(rvalue, 4), name, VectorType(ptype1, 4),
465 VectorType(ptype2, 4), VectorType(ptype3, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500466 }
467 else
468 {
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300469 TFunction *function = new TFunction(this, NewPoolTString(name), rvalue, op, ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700470
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700471 function->addParameter(TConstParameter(ptype1));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700472
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500473 if (ptype2)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700474 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700475 function->addParameter(TConstParameter(ptype2));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700476 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700477
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500478 if (ptype3)
479 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700480 function->addParameter(TConstParameter(ptype3));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500481 }
482
483 if (ptype4)
484 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700485 function->addParameter(TConstParameter(ptype4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500486 }
487
488 if (ptype5)
489 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700490 function->addParameter(TConstParameter(ptype5));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500491 }
492
Martin Radevda6254b2016-12-14 17:00:36 +0200493 ASSERT(hasUnmangledBuiltInAtLevel(name, level));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500494 insert(level, function);
495 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700496}
497
Olli Etuaho492cfab2017-01-20 21:18:29 +0000498void TSymbolTable::insertBuiltInOp(ESymbolLevel level,
499 TOperator op,
500 const TType *rvalue,
501 const TType *ptype1,
502 const TType *ptype2,
503 const TType *ptype3,
504 const TType *ptype4,
505 const TType *ptype5)
506{
507 const char *name = GetOperatorString(op);
508 ASSERT(strlen(name) > 0);
509 insertUnmangledBuiltInName(name, level);
510 insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
511}
512
513void TSymbolTable::insertBuiltInOp(ESymbolLevel level,
514 TOperator op,
515 const char *ext,
516 const TType *rvalue,
517 const TType *ptype1,
518 const TType *ptype2,
519 const TType *ptype3,
520 const TType *ptype4,
521 const TType *ptype5)
522{
523 const char *name = GetOperatorString(op);
524 insertUnmangledBuiltInName(name, level);
525 insertBuiltIn(level, op, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
526}
527
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300528void TSymbolTable::insertBuiltInFunctionNoParameters(ESymbolLevel level,
529 TOperator op,
530 const TType *rvalue,
531 const char *name)
532{
533 insertUnmangledBuiltInName(name, level);
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300534 insert(level, new TFunction(this, NewPoolTString(name), rvalue, op));
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300535}
536
Zhenyao Moe740add2014-07-18 17:01:01 -0700537TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700538{
539 if (!SupportsPrecision(type))
540 return EbpUndefined;
541
542 // unsigned integers use the same precision as signed
543 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
544
545 int level = static_cast<int>(precisionStack.size()) - 1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500546 assert(level >= 0); // Just to be safe. Should not happen.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200547 // If we dont find anything we return this. Some types don't have predefined default precision.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700548 TPrecision prec = EbpUndefined;
549 while (level >= 0)
550 {
551 PrecisionStackLevel::iterator it = precisionStack[level]->find(baseType);
552 if (it != precisionStack[level]->end())
553 {
554 prec = (*it).second;
555 break;
556 }
557 level--;
558 }
559 return prec;
560}
Jamie Madill45bcc782016-11-07 13:58:48 -0500561
Martin Radevda6254b2016-12-14 17:00:36 +0200562void TSymbolTable::insertUnmangledBuiltInName(const char *name, ESymbolLevel level)
563{
564 ASSERT(level >= 0 && level < static_cast<ESymbolLevel>(table.size()));
565 table[level]->insertUnmangledBuiltInName(std::string(name));
566}
567
568bool TSymbolTable::hasUnmangledBuiltInAtLevel(const char *name, ESymbolLevel level)
569{
570 ASSERT(level >= 0 && level < static_cast<ESymbolLevel>(table.size()));
571 return table[level]->hasUnmangledBuiltIn(std::string(name));
572}
573
574bool TSymbolTable::hasUnmangledBuiltInForShaderVersion(const char *name, int shaderVersion)
575{
576 ASSERT(static_cast<ESymbolLevel>(table.size()) > LAST_BUILTIN_LEVEL);
577
578 for (int level = LAST_BUILTIN_LEVEL; level >= 0; --level)
579 {
580 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
581 {
582 --level;
583 }
584 if (level == ESSL3_BUILTINS && shaderVersion < 300)
585 {
586 --level;
587 }
588 if (level == ESSL1_BUILTINS && shaderVersion != 100)
589 {
590 --level;
591 }
592
593 if (table[level]->hasUnmangledBuiltIn(name))
594 {
595 return true;
596 }
597 }
598 return false;
599}
600
Jamie Madill45bcc782016-11-07 13:58:48 -0500601} // namespace sh