blob: f7d213bb753f4b81f7b2acd5fdd2c936d62450ab [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
Olli Etuaho01d0ad02017-01-22 14:51:23 -080016#include "compiler/translator/IntermNode.h"
Kai Ninomiya614dd0f2017-11-22 14:04:48 -080017#include "compiler/translator/StaticType.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
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025//
26// Symbol table levels are a map of pointers to symbols that have to be deleted.
27//
28TSymbolTableLevel::~TSymbolTableLevel()
29{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000030 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
31 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032}
33
Nicolas Capensadfffe42014-06-17 02:13:36 -040034bool TSymbolTableLevel::insert(TSymbol *symbol)
Jamie Madillbfa91f42014-06-05 15:45:18 -040035{
Jamie Madillbfa91f42014-06-05 15:45:18 -040036 // returning true means symbol was added to the table
Nicolas Capensadfffe42014-06-17 02:13:36 -040037 tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol));
Jamie Madillbfa91f42014-06-05 15:45:18 -040038
39 return result.second;
40}
41
Olli Etuahob2983c92015-03-18 14:02:46 +020042bool TSymbolTableLevel::insertUnmangled(TFunction *function)
43{
Olli Etuahob2983c92015-03-18 14:02:46 +020044 // returning true means symbol was added to the table
Olli Etuahobed35d72017-12-20 16:36:26 +020045 tInsertResult result = level.insert(tLevelPair(function->name(), function));
Olli Etuahob2983c92015-03-18 14:02:46 +020046
47 return result.second;
48}
49
Jamie Madillbfa91f42014-06-05 15:45:18 -040050TSymbol *TSymbolTableLevel::find(const TString &name) const
51{
52 tLevel::const_iterator it = level.find(name);
53 if (it == level.end())
54 return 0;
55 else
56 return (*it).second;
57}
58
Jamie Madilld7b1ab52016-12-12 14:42:19 -050059TSymbol *TSymbolTable::find(const TString &name,
60 int shaderVersion,
61 bool *builtIn,
62 bool *sameScope) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000063{
64 int level = currentLevel();
65 TSymbol *symbol;
66
67 do
68 {
Olli Etuaho977ee7e2017-07-21 11:38:27 +030069 if (level == GLSL_BUILTINS)
70 level--;
Martin Radeve93d24e2016-07-28 12:06:05 +030071 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
72 level--;
73 if (level == ESSL3_BUILTINS && shaderVersion < 300)
Zhenyao Mo9eedea02014-05-12 16:02:35 -070074 level--;
75 if (level == ESSL1_BUILTINS && shaderVersion != 100)
76 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000077
78 symbol = table[level]->find(name);
Jamie Madilld7b1ab52016-12-12 14:42:19 -050079 } while (symbol == 0 && --level >= 0);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000080
81 if (builtIn)
82 *builtIn = (level <= LAST_BUILTIN_LEVEL);
83 if (sameScope)
84 *sameScope = (level == currentLevel());
85
86 return symbol;
87}
88
Zhenyao Mod7490962016-11-09 15:49:51 -080089TSymbol *TSymbolTable::findGlobal(const TString &name) const
90{
91 ASSERT(table.size() > GLOBAL_LEVEL);
92 return table[GLOBAL_LEVEL]->find(name);
93}
94
Jamie Madilld7b1ab52016-12-12 14:42:19 -050095TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000096{
Olli Etuaho977ee7e2017-07-21 11:38:27 +030097 return findBuiltIn(name, shaderVersion, false);
98}
99
100TSymbol *TSymbolTable::findBuiltIn(const TString &name,
101 int shaderVersion,
102 bool includeGLSLBuiltins) const
103{
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000104 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
105 {
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300106 if (level == GLSL_BUILTINS && !includeGLSLBuiltins)
107 level--;
Martin Radeve93d24e2016-07-28 12:06:05 +0300108 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
109 level--;
110 if (level == ESSL3_BUILTINS && shaderVersion < 300)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700111 level--;
112 if (level == ESSL1_BUILTINS && shaderVersion != 100)
113 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000114
115 TSymbol *symbol = table[level]->find(name);
116
117 if (symbol)
118 return symbol;
119 }
120
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300121 return nullptr;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000122}
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400123
124TSymbolTable::~TSymbolTable()
125{
126 while (table.size() > 0)
127 pop();
128}
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700129
Kai Ninomiya030017a2017-12-06 14:06:53 -0800130constexpr bool IsGenType(const TType *type)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500131{
132 if (type)
133 {
134 TBasicType basicType = type->getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500135 return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType ||
136 basicType == EbtGenBType;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500137 }
138
139 return false;
140}
141
Kai Ninomiya030017a2017-12-06 14:06:53 -0800142constexpr bool IsVecType(const TType *type)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500143{
144 if (type)
145 {
146 TBasicType basicType = type->getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500147 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec ||
148 basicType == EbtBVec;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500149 }
150
151 return false;
152}
153
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800154constexpr const TType *SpecificType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500155{
156 ASSERT(size >= 1 && size <= 4);
157
158 if (!type)
159 {
160 return nullptr;
161 }
162
163 ASSERT(!IsVecType(type));
164
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500165 switch (type->getBasicType())
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500166 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500167 case EbtGenType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800168 return StaticType::GetForVec<EbtFloat>(type->getQualifier(),
169 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500170 case EbtGenIType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800171 return StaticType::GetForVec<EbtInt>(type->getQualifier(),
172 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500173 case EbtGenUType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800174 return StaticType::GetForVec<EbtUInt>(type->getQualifier(),
175 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500176 case EbtGenBType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800177 return StaticType::GetForVec<EbtBool>(type->getQualifier(),
178 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500179 default:
180 return type;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500181 }
182}
183
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800184constexpr const TType *VectorType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500185{
186 ASSERT(size >= 2 && size <= 4);
187
188 if (!type)
189 {
190 return nullptr;
191 }
192
193 ASSERT(!IsGenType(type));
194
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500195 switch (type->getBasicType())
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500196 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500197 case EbtVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800198 return StaticType::GetForVecMat<EbtFloat>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500199 case EbtIVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800200 return StaticType::GetForVecMat<EbtInt>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500201 case EbtUVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800202 return StaticType::GetForVecMat<EbtUInt>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500203 case EbtBVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800204 return StaticType::GetForVecMat<EbtBool>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500205 default:
206 return type;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500207 }
208}
209
Olli Etuaho195be942017-12-04 23:40:14 +0200210bool TSymbolTable::declareVariable(TVariable *variable)
Olli Etuaho0f684632017-07-13 12:42:15 +0300211{
Olli Etuaho195be942017-12-04 23:40:14 +0200212 ASSERT(variable->symbolType() == SymbolType::UserDefined);
213 return insertVariable(currentLevel(), variable);
Olli Etuaho0f684632017-07-13 12:42:15 +0300214}
215
Olli Etuaho035419f2017-11-28 14:27:15 +0200216bool TSymbolTable::declareStructType(TStructure *str)
Olli Etuaho0f684632017-07-13 12:42:15 +0300217{
218 return insertStructType(currentLevel(), str);
219}
220
Olli Etuaho378c3a52017-12-04 11:32:13 +0200221bool TSymbolTable::declareInterfaceBlock(TInterfaceBlock *interfaceBlock)
Olli Etuaho0f684632017-07-13 12:42:15 +0300222{
Olli Etuaho378c3a52017-12-04 11:32:13 +0200223 return insert(currentLevel(), interfaceBlock);
Jiawei Shaod8105a02017-08-08 09:54:36 +0800224}
225
Olli Etuaho0f684632017-07-13 12:42:15 +0300226TVariable *TSymbolTable::insertVariable(ESymbolLevel level, const char *name, const TType &type)
227{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100228 ASSERT(level <= LAST_BUILTIN_LEVEL);
229 return insertVariable(level, NewPoolTString(name), type, SymbolType::BuiltIn);
Olli Etuaho0f684632017-07-13 12:42:15 +0300230}
231
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100232TVariable *TSymbolTable::insertVariable(ESymbolLevel level,
233 const TString *name,
234 const TType &type,
235 SymbolType symbolType)
Olli Etuaho0f684632017-07-13 12:42:15 +0300236{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100237 TVariable *var = new TVariable(this, name, type, symbolType);
Olli Etuaho0f684632017-07-13 12:42:15 +0300238 if (insert(level, var))
239 {
240 // Do lazy initialization for struct types, so we allocate to the current scope.
241 if (var->getType().getBasicType() == EbtStruct)
242 {
243 var->getType().realize();
244 }
245 return var;
246 }
247 return nullptr;
248}
249
250TVariable *TSymbolTable::insertVariableExt(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300251 TExtension ext,
Olli Etuaho0f684632017-07-13 12:42:15 +0300252 const char *name,
253 const TType &type)
254{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100255 TVariable *var = new TVariable(this, NewPoolTString(name), type, SymbolType::BuiltIn, ext);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200256 if (insert(level, var))
Olli Etuaho0f684632017-07-13 12:42:15 +0300257 {
258 if (var->getType().getBasicType() == EbtStruct)
259 {
260 var->getType().realize();
261 }
262 return var;
263 }
264 return nullptr;
265}
266
Olli Etuaho195be942017-12-04 23:40:14 +0200267bool TSymbolTable::insertVariable(ESymbolLevel level, TVariable *variable)
268{
269 ASSERT(variable);
270 return insert(level, variable);
271}
272
Olli Etuaho035419f2017-11-28 14:27:15 +0200273bool TSymbolTable::insertStructType(ESymbolLevel level, TStructure *str)
Olli Etuaho0f684632017-07-13 12:42:15 +0300274{
Olli Etuaho035419f2017-11-28 14:27:15 +0200275 ASSERT(str);
Olli Etuaho378c3a52017-12-04 11:32:13 +0200276 return insert(level, str);
277}
278
279bool TSymbolTable::insertInterfaceBlock(ESymbolLevel level, TInterfaceBlock *interfaceBlock)
280{
281 ASSERT(interfaceBlock);
282 return insert(level, interfaceBlock);
Olli Etuaho0f684632017-07-13 12:42:15 +0300283}
284
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500285void TSymbolTable::insertBuiltIn(ESymbolLevel level,
286 TOperator op,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300287 TExtension ext,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500288 const TType *rvalue,
289 const char *name,
290 const TType *ptype1,
291 const TType *ptype2,
292 const TType *ptype3,
293 const TType *ptype4,
294 const TType *ptype5)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700295{
296 if (ptype1->getBasicType() == EbtGSampler2D)
297 {
Martin Radevda6254b2016-12-14 17:00:36 +0200298 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700299 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800300 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
301 StaticType::GetBasic<EbtSampler2D>(), ptype2, ptype3, ptype4, ptype5);
302 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
303 StaticType::GetBasic<EbtISampler2D>(), ptype2, ptype3, ptype4, ptype5);
304 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
305 StaticType::GetBasic<EbtUSampler2D>(), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700306 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500307 else if (ptype1->getBasicType() == EbtGSampler3D)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700308 {
Martin Radevda6254b2016-12-14 17:00:36 +0200309 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700310 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800311 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
312 StaticType::GetBasic<EbtSampler3D>(), ptype2, ptype3, ptype4, ptype5);
313 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
314 StaticType::GetBasic<EbtISampler3D>(), ptype2, ptype3, ptype4, ptype5);
315 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
316 StaticType::GetBasic<EbtUSampler3D>(), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700317 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500318 else if (ptype1->getBasicType() == EbtGSamplerCube)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700319 {
Martin Radevda6254b2016-12-14 17:00:36 +0200320 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700321 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800322 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
323 StaticType::GetBasic<EbtSamplerCube>(), ptype2, ptype3, ptype4, ptype5);
324 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
325 StaticType::GetBasic<EbtISamplerCube>(), ptype2, ptype3, ptype4, ptype5);
326 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
327 StaticType::GetBasic<EbtUSamplerCube>(), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700328 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500329 else if (ptype1->getBasicType() == EbtGSampler2DArray)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700330 {
Martin Radevda6254b2016-12-14 17:00:36 +0200331 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700332 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800333 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
334 StaticType::GetBasic<EbtSampler2DArray>(), ptype2, ptype3, ptype4,
335 ptype5);
336 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
337 StaticType::GetBasic<EbtISampler2DArray>(), ptype2, ptype3, ptype4,
338 ptype5);
339 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
340 StaticType::GetBasic<EbtUSampler2DArray>(), ptype2, ptype3, ptype4,
341 ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700342 }
JiangYizhou40219322016-12-09 09:50:51 +0800343 else if (ptype1->getBasicType() == EbtGSampler2DMS)
344 {
345 insertUnmangledBuiltInName(name, level);
346 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800347 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
348 StaticType::GetBasic<EbtSampler2DMS>(), ptype2, ptype3, ptype4, ptype5);
349 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
350 StaticType::GetBasic<EbtISampler2DMS>(), ptype2, ptype3, ptype4, ptype5);
351 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
352 StaticType::GetBasic<EbtUSampler2DMS>(), ptype2, ptype3, ptype4, ptype5);
JiangYizhou40219322016-12-09 09:50:51 +0800353 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300354 else if (IsGImage(ptype1->getBasicType()))
355 {
Martin Radevda6254b2016-12-14 17:00:36 +0200356 insertUnmangledBuiltInName(name, level);
Martin Radev2cc85b32016-08-05 16:22:53 +0300357
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800358 const TType *floatType = StaticType::GetBasic<EbtFloat, 4>();
359 const TType *intType = StaticType::GetBasic<EbtInt, 4>();
360 const TType *unsignedType = StaticType::GetBasic<EbtUInt, 4>();
Martin Radev2cc85b32016-08-05 16:22:53 +0300361
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800362 const TType *floatImage = StaticType::GetForFloatImage(ptype1->getBasicType());
363 const TType *intImage = StaticType::GetForIntImage(ptype1->getBasicType());
364 const TType *unsignedImage = StaticType::GetForUintImage(ptype1->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300365
366 // GLSL ES 3.10, Revision 4, 8.12 Image Functions
367 if (rvalue->getBasicType() == EbtGVec4)
368 {
369 // imageLoad
370 insertBuiltIn(level, floatType, name, floatImage, ptype2, ptype3, ptype4, ptype5);
371 insertBuiltIn(level, intType, name, intImage, ptype2, ptype3, ptype4, ptype5);
372 insertBuiltIn(level, unsignedType, name, unsignedImage, ptype2, ptype3, ptype4, ptype5);
373 }
374 else if (rvalue->getBasicType() == EbtVoid)
375 {
376 // imageStore
377 insertBuiltIn(level, rvalue, name, floatImage, ptype2, floatType, ptype4, ptype5);
378 insertBuiltIn(level, rvalue, name, intImage, ptype2, intType, ptype4, ptype5);
379 insertBuiltIn(level, rvalue, name, unsignedImage, ptype2, unsignedType, ptype4, ptype5);
380 }
381 else
382 {
383 // imageSize
384 insertBuiltIn(level, rvalue, name, floatImage, ptype2, ptype3, ptype4, ptype5);
385 insertBuiltIn(level, rvalue, name, intImage, ptype2, ptype3, ptype4, ptype5);
386 insertBuiltIn(level, rvalue, name, unsignedImage, ptype2, ptype3, ptype4, ptype5);
387 }
388 }
Olli Etuaho9250cb22017-01-21 10:51:27 +0000389 else if (IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3) ||
390 IsGenType(ptype4))
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500391 {
Olli Etuaho9250cb22017-01-21 10:51:27 +0000392 ASSERT(!ptype5);
Martin Radevda6254b2016-12-14 17:00:36 +0200393 insertUnmangledBuiltInName(name, level);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500394 insertBuiltIn(level, op, ext, SpecificType(rvalue, 1), name, SpecificType(ptype1, 1),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000395 SpecificType(ptype2, 1), SpecificType(ptype3, 1), SpecificType(ptype4, 1));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500396 insertBuiltIn(level, op, ext, SpecificType(rvalue, 2), name, SpecificType(ptype1, 2),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000397 SpecificType(ptype2, 2), SpecificType(ptype3, 2), SpecificType(ptype4, 2));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500398 insertBuiltIn(level, op, ext, SpecificType(rvalue, 3), name, SpecificType(ptype1, 3),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000399 SpecificType(ptype2, 3), SpecificType(ptype3, 3), SpecificType(ptype4, 3));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500400 insertBuiltIn(level, op, ext, SpecificType(rvalue, 4), name, SpecificType(ptype1, 4),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000401 SpecificType(ptype2, 4), SpecificType(ptype3, 4), SpecificType(ptype4, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500402 }
403 else if (IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
404 {
405 ASSERT(!ptype4 && !ptype5);
Martin Radevda6254b2016-12-14 17:00:36 +0200406 insertUnmangledBuiltInName(name, level);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500407 insertBuiltIn(level, op, ext, VectorType(rvalue, 2), name, VectorType(ptype1, 2),
408 VectorType(ptype2, 2), VectorType(ptype3, 2));
409 insertBuiltIn(level, op, ext, VectorType(rvalue, 3), name, VectorType(ptype1, 3),
410 VectorType(ptype2, 3), VectorType(ptype3, 3));
411 insertBuiltIn(level, op, ext, VectorType(rvalue, 4), name, VectorType(ptype1, 4),
412 VectorType(ptype2, 4), VectorType(ptype3, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500413 }
414 else
415 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100416 TFunction *function =
Olli Etuaho0c371002017-12-13 17:00:25 +0400417 new TFunction(this, NewPoolTString(name), rvalue, SymbolType::BuiltIn, false, op, ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700418
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700419 function->addParameter(TConstParameter(ptype1));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700420
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500421 if (ptype2)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700422 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700423 function->addParameter(TConstParameter(ptype2));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700424 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700425
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500426 if (ptype3)
427 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700428 function->addParameter(TConstParameter(ptype3));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500429 }
430
431 if (ptype4)
432 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700433 function->addParameter(TConstParameter(ptype4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500434 }
435
436 if (ptype5)
437 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700438 function->addParameter(TConstParameter(ptype5));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500439 }
440
Martin Radevda6254b2016-12-14 17:00:36 +0200441 ASSERT(hasUnmangledBuiltInAtLevel(name, level));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500442 insert(level, function);
443 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700444}
445
Olli Etuaho492cfab2017-01-20 21:18:29 +0000446void TSymbolTable::insertBuiltInOp(ESymbolLevel level,
447 TOperator op,
448 const TType *rvalue,
449 const TType *ptype1,
450 const TType *ptype2,
451 const TType *ptype3,
452 const TType *ptype4,
453 const TType *ptype5)
454{
455 const char *name = GetOperatorString(op);
456 ASSERT(strlen(name) > 0);
457 insertUnmangledBuiltInName(name, level);
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300458 insertBuiltIn(level, op, TExtension::UNDEFINED, rvalue, name, ptype1, ptype2, ptype3, ptype4,
459 ptype5);
Olli Etuaho492cfab2017-01-20 21:18:29 +0000460}
461
462void TSymbolTable::insertBuiltInOp(ESymbolLevel level,
463 TOperator op,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300464 TExtension ext,
Olli Etuaho492cfab2017-01-20 21:18:29 +0000465 const TType *rvalue,
466 const TType *ptype1,
467 const TType *ptype2,
468 const TType *ptype3,
469 const TType *ptype4,
470 const TType *ptype5)
471{
472 const char *name = GetOperatorString(op);
473 insertUnmangledBuiltInName(name, level);
474 insertBuiltIn(level, op, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
475}
476
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300477void TSymbolTable::insertBuiltInFunctionNoParameters(ESymbolLevel level,
478 TOperator op,
479 const TType *rvalue,
480 const char *name)
481{
482 insertUnmangledBuiltInName(name, level);
Olli Etuaho0c371002017-12-13 17:00:25 +0400483 insert(level,
484 new TFunction(this, NewPoolTString(name), rvalue, SymbolType::BuiltIn, false, op));
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300485}
486
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800487void TSymbolTable::insertBuiltInFunctionNoParametersExt(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300488 TExtension ext,
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800489 TOperator op,
490 const TType *rvalue,
491 const char *name)
492{
493 insertUnmangledBuiltInName(name, level);
Olli Etuaho0c371002017-12-13 17:00:25 +0400494 insert(level,
495 new TFunction(this, NewPoolTString(name), rvalue, SymbolType::BuiltIn, false, op, ext));
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800496}
497
Zhenyao Moe740add2014-07-18 17:01:01 -0700498TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700499{
500 if (!SupportsPrecision(type))
501 return EbpUndefined;
502
503 // unsigned integers use the same precision as signed
504 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
505
506 int level = static_cast<int>(precisionStack.size()) - 1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500507 assert(level >= 0); // Just to be safe. Should not happen.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200508 // If we dont find anything we return this. Some types don't have predefined default precision.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700509 TPrecision prec = EbpUndefined;
510 while (level >= 0)
511 {
512 PrecisionStackLevel::iterator it = precisionStack[level]->find(baseType);
513 if (it != precisionStack[level]->end())
514 {
515 prec = (*it).second;
516 break;
517 }
518 level--;
519 }
520 return prec;
521}
Jamie Madill45bcc782016-11-07 13:58:48 -0500522
Martin Radevda6254b2016-12-14 17:00:36 +0200523void TSymbolTable::insertUnmangledBuiltInName(const char *name, ESymbolLevel level)
524{
525 ASSERT(level >= 0 && level < static_cast<ESymbolLevel>(table.size()));
Olli Etuaho5d69db12017-11-24 16:51:15 +0200526 ASSERT(mUserDefinedUniqueIdsStart == -1);
Martin Radevda6254b2016-12-14 17:00:36 +0200527 table[level]->insertUnmangledBuiltInName(std::string(name));
528}
529
530bool TSymbolTable::hasUnmangledBuiltInAtLevel(const char *name, ESymbolLevel level)
531{
532 ASSERT(level >= 0 && level < static_cast<ESymbolLevel>(table.size()));
533 return table[level]->hasUnmangledBuiltIn(std::string(name));
534}
535
536bool TSymbolTable::hasUnmangledBuiltInForShaderVersion(const char *name, int shaderVersion)
537{
538 ASSERT(static_cast<ESymbolLevel>(table.size()) > LAST_BUILTIN_LEVEL);
539
540 for (int level = LAST_BUILTIN_LEVEL; level >= 0; --level)
541 {
542 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
543 {
544 --level;
545 }
546 if (level == ESSL3_BUILTINS && shaderVersion < 300)
547 {
548 --level;
549 }
550 if (level == ESSL1_BUILTINS && shaderVersion != 100)
551 {
552 --level;
553 }
554
555 if (table[level]->hasUnmangledBuiltIn(name))
556 {
557 return true;
558 }
559 }
560 return false;
561}
562
Olli Etuaho5d69db12017-11-24 16:51:15 +0200563void TSymbolTable::markBuiltInInitializationFinished()
564{
565 mUserDefinedUniqueIdsStart = mUniqueIdCounter;
566}
567
568void TSymbolTable::clearCompilationResults()
569{
570 mUniqueIdCounter = mUserDefinedUniqueIdsStart;
571
572 // User-defined scopes should have already been cleared when the compilation finished.
573 ASSERT(table.size() == LAST_BUILTIN_LEVEL + 1u);
574}
575
576int TSymbolTable::nextUniqueIdValue()
577{
578 ASSERT(mUniqueIdCounter < std::numeric_limits<int>::max());
579 return ++mUniqueIdCounter;
580}
581
Jamie Madill45bcc782016-11-07 13:58:48 -0500582} // namespace sh