blob: 2a4cfffb64c9bcad8620cdc976f009a8e3683b87 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens1fbc2872014-01-03 14:12:09 -05002// Copyright (c) 2002-2014 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
Geoff Lang0a73dd82014-11-19 16:18:08 -05007#ifndef COMPILER_TRANSLATOR_SYMBOLTABLE_H_
8#define COMPILER_TRANSLATOR_SYMBOLTABLE_H_
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009
10//
11// Symbol table for parsing. Has these design characteristics:
12//
13// * Same symbol table can be used to compile many shaders, to preserve
14// effort of creating and loading with the large numbers of built-in
15// symbols.
16//
17// * Name mangling will be used to give each function a unique name
18// so that symbol table lookups are never ambiguous. This allows
19// a simpler symbol table structure.
20//
Jamie Madilld7b1ab52016-12-12 14:42:19 -050021// * Pushing and popping of scope, so symbol table will really be a stack
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000022// of symbol tables. Searched from the top, with new inserts going into
23// the top.
24//
25// * Constants: Compile time constant symbols will keep their values
26// in the symbol table. The parser can substitute constants at parse
27// time, including doing constant folding and constant propagation.
28//
29// * No temporaries: Temporaries made from operations (+, --, .xy, etc.)
30// are tracked in the intermediate representation, not the symbol table.
31//
32
Martin Radeve93d24e2016-07-28 12:06:05 +030033#include <array>
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000034#include <assert.h>
Zhenyao Mo94ac7b72014-10-15 18:22:08 -070035#include <set>
alokp@chromium.orge4249f02010-07-26 18:13:52 +000036
Jamie Madill703cdd62013-07-08 15:07:30 -040037#include "common/angleutils.h"
Geoff Lang17732822013-08-29 13:46:49 -040038#include "compiler/translator/InfoSink.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040039#include "compiler/translator/IntermNode.h"
alokp@chromium.org43884872010-03-30 00:08:52 +000040
Jamie Madill45bcc782016-11-07 13:58:48 -050041namespace sh
42{
43
Olli Etuaho01d0ad02017-01-22 14:51:23 -080044class TIntermAggregate;
45
Zhenyao Mo9eedea02014-05-12 16:02:35 -070046// Symbol base class. (Can build functions or variables out of these...)
Jamie Madillf0d10f82015-03-31 12:56:52 -040047class TSymbol : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -070048{
49 public:
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040050 POOL_ALLOCATOR_NEW_DELETE();
Olli Etuaho476197f2016-10-11 13:59:08 +010051 TSymbol(const TString *n);
52
Zhenyao Mo9eedea02014-05-12 16:02:35 -070053 virtual ~TSymbol()
54 {
55 // don't delete name, it's from the pool
56 }
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040057
Jamie Madilld7b1ab52016-12-12 14:42:19 -050058 const TString &getName() const { return *name; }
59 virtual const TString &getMangledName() const { return getName(); }
60 virtual bool isFunction() const { return false; }
61 virtual bool isVariable() const { return false; }
62 int getUniqueId() const { return uniqueId; }
63 void relateToExtension(const TString &ext) { extension = ext; }
64 const TString &getExtension() const { return extension; }
Nicolas Capensba60ad32013-06-04 15:55:47 -040065
Zhenyao Mo9eedea02014-05-12 16:02:35 -070066 private:
Olli Etuaho476197f2016-10-11 13:59:08 +010067 const int uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000068 const TString *name;
Jamie Madill2aeb26a2013-07-08 14:02:55 -040069 TString extension;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000070};
71
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000072// Variable class, meaning a symbol that's not a function.
Jamie Madilld7b1ab52016-12-12 14:42:19 -050073//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000074// There could be a separate class heirarchy for Constant variables;
75// Only one of int, bool, or float, (or none) is correct for
76// any particular use, but it's easy to do this way, and doesn't
77// seem worth having separate classes, and "getConst" can't simply return
Jamie Madilld7b1ab52016-12-12 14:42:19 -050078// different values for different types polymorphically, so this is
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079// just simple and pragmatic.
Zhenyao Mo9eedea02014-05-12 16:02:35 -070080class TVariable : public TSymbol
81{
82 public:
83 TVariable(const TString *name, const TType &t, bool uT = false)
Jamie Madilld7b1ab52016-12-12 14:42:19 -050084 : TSymbol(name), type(t), userType(uT), unionArray(0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -070085 {
86 }
Corentin Walleze5a1f272015-08-21 02:58:25 +020087 ~TVariable() override {}
88 bool isVariable() const override { return true; }
Jamie Madilld7b1ab52016-12-12 14:42:19 -050089 TType &getType() { return type; }
90 const TType &getType() const { return type; }
91 bool isUserType() const { return userType; }
92 void setQualifier(TQualifier qualifier) { type.setQualifier(qualifier); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093
Olli Etuaho5c0e0232015-11-11 15:55:59 +020094 const TConstantUnion *getConstPointer() const { return unionArray; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
Olli Etuaho5c0e0232015-11-11 15:55:59 +020096 void shareConstPointer(const TConstantUnion *constArray) { unionArray = constArray; }
alokp@chromium.org43884872010-03-30 00:08:52 +000097
Zhenyao Mo9eedea02014-05-12 16:02:35 -070098 private:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099 TType type;
100 bool userType;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700101 // we are assuming that Pool Allocator will free the memory
102 // allocated to unionArray when this object is destroyed.
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200103 const TConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000104};
105
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700106// Immutable version of TParameter.
107struct TConstParameter
108{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500109 TConstParameter() : name(nullptr), type(nullptr) {}
110 explicit TConstParameter(const TString *n) : name(n), type(nullptr) {}
111 explicit TConstParameter(const TType *t) : name(nullptr), type(t) {}
112 TConstParameter(const TString *n, const TType *t) : name(n), type(t) {}
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700113
114 // Both constructor arguments must be const.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500115 TConstParameter(TString *n, TType *t) = delete;
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700116 TConstParameter(const TString *n, TType *t) = delete;
117 TConstParameter(TString *n, const TType *t) = delete;
118
119 const TString *name;
120 const TType *type;
121};
122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123// The function sub-class of symbols and the parser will need to
124// share this definition of a function parameter.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700125struct TParameter
126{
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700127 // Destructively converts to TConstParameter.
128 // This method resets name and type to nullptrs to make sure
129 // their content cannot be modified after the call.
130 TConstParameter turnToConst()
131 {
132 const TString *constName = name;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500133 const TType *constType = type;
134 name = nullptr;
135 type = nullptr;
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700136 return TConstParameter(constName, constType);
137 }
138
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 TString *name;
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400140 TType *type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141};
142
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500143// The function sub-class of a symbol.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700144class TFunction : public TSymbol
145{
146 public:
Olli Etuaho5d653182016-01-04 14:43:28 +0200147 TFunction(const TString *name,
148 const TType *retType,
149 TOperator tOp = EOpNull,
150 const char *ext = "")
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700151 : TSymbol(name),
152 returnType(retType),
Dmitry Skiba58832202015-07-06 16:11:13 -0700153 mangledName(nullptr),
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700154 op(tOp),
Olli Etuaho5d653182016-01-04 14:43:28 +0200155 defined(false),
156 mHasPrototypeDeclaration(false)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700157 {
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500158 relateToExtension(ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700159 }
Corentin Walleze5a1f272015-08-21 02:58:25 +0200160 ~TFunction() override;
161 bool isFunction() const override { return true; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000162
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500163 static TString mangleName(const TString &name) { return name + '('; }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700164 static TString unmangleName(const TString &mangledName)
alokp@chromium.org43884872010-03-30 00:08:52 +0000165 {
166 return TString(mangledName.c_str(), mangledName.find_first_of('('));
167 }
168
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700169 void addParameter(const TConstParameter &p)
Dmitry Skiba58832202015-07-06 16:11:13 -0700170 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000171 parameters.push_back(p);
Dmitry Skiba58832202015-07-06 16:11:13 -0700172 mangledName = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000174
Olli Etuaho476197f2016-10-11 13:59:08 +0100175 void swapParameters(const TFunction &parametersSource);
176
Corentin Walleze5a1f272015-08-21 02:58:25 +0200177 const TString &getMangledName() const override
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700178 {
Dmitry Skiba58832202015-07-06 16:11:13 -0700179 if (mangledName == nullptr)
180 {
181 mangledName = buildMangledName();
182 }
183 return *mangledName;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700184 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500185 const TType &getReturnType() const { return *returnType; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000186
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500187 TOperator getBuiltInOp() const { return op; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000188
Olli Etuaho5d653182016-01-04 14:43:28 +0200189 void setDefined() { defined = true; }
190 bool isDefined() { return defined; }
191 void setHasPrototypeDeclaration() { mHasPrototypeDeclaration = true; }
192 bool hasPrototypeDeclaration() const { return mHasPrototypeDeclaration; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500194 size_t getParamCount() const { return parameters.size(); }
195 const TConstParameter &getParam(size_t i) const { return parameters[i]; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000196
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700197 private:
Olli Etuaho476197f2016-10-11 13:59:08 +0100198 void clearParameters();
199
Dmitry Skiba58832202015-07-06 16:11:13 -0700200 const TString *buildMangledName() const;
201
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700202 typedef TVector<TConstParameter> TParamList;
alokp@chromium.org43884872010-03-30 00:08:52 +0000203 TParamList parameters;
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700204 const TType *returnType;
Dmitry Skiba58832202015-07-06 16:11:13 -0700205 mutable const TString *mangledName;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000206 TOperator op;
207 bool defined;
Olli Etuaho5d653182016-01-04 14:43:28 +0200208 bool mHasPrototypeDeclaration;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209};
210
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000211// Interface block name sub-symbol
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000212class TInterfaceBlockName : public TSymbol
213{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700214 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500215 TInterfaceBlockName(const TString *name) : TSymbol(name) {}
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000216
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500217 virtual ~TInterfaceBlockName() {}
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000218};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000219
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700220class TSymbolTableLevel
221{
222 public:
223 typedef TMap<TString, TSymbol *> tLevel;
alokp@chromium.org43884872010-03-30 00:08:52 +0000224 typedef tLevel::const_iterator const_iterator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225 typedef const tLevel::value_type tLevelPair;
226 typedef std::pair<tLevel::iterator, bool> tInsertResult;
227
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500228 TSymbolTableLevel() : mGlobalInvariant(false) {}
alokp@chromium.org43884872010-03-30 00:08:52 +0000229 ~TSymbolTableLevel();
230
Nicolas Capensadfffe42014-06-17 02:13:36 -0400231 bool insert(TSymbol *symbol);
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400232
Olli Etuahob2983c92015-03-18 14:02:46 +0200233 // Insert a function using its unmangled name as the key.
234 bool insertUnmangled(TFunction *function);
235
Jamie Madillbfa91f42014-06-05 15:45:18 -0400236 TSymbol *find(const TString &name) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000237
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500238 void addInvariantVarying(const std::string &name) { mInvariantVaryings.insert(name); }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800239
240 bool isVaryingInvariant(const std::string &name)
241 {
242 return (mGlobalInvariant || mInvariantVaryings.count(name) > 0);
243 }
244
245 void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; }
246
Martin Radevda6254b2016-12-14 17:00:36 +0200247 void insertUnmangledBuiltInName(const std::string &name)
248 {
249 mUnmangledBuiltInNames.insert(name);
250 }
251
252 bool hasUnmangledBuiltIn(const std::string &name)
253 {
254 return mUnmangledBuiltInNames.count(name) > 0;
255 }
256
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700257 protected:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258 tLevel level;
Qiankun Miaof69682b2016-08-16 14:50:42 +0800259 std::set<std::string> mInvariantVaryings;
260 bool mGlobalInvariant;
Martin Radevda6254b2016-12-14 17:00:36 +0200261
262 private:
263 std::set<std::string> mUnmangledBuiltInNames;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000264};
265
Gus Fernandez964df492014-10-13 11:54:39 -0700266// Define ESymbolLevel as int rather than an enum since level can go
267// above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the
268// compiler optimizes the >= of the last element to ==.
269typedef int ESymbolLevel;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500270const int COMMON_BUILTINS = 0;
271const int ESSL1_BUILTINS = 1;
272const int ESSL3_BUILTINS = 2;
Martin Radeve93d24e2016-07-28 12:06:05 +0300273const int ESSL3_1_BUILTINS = 3;
274const int LAST_BUILTIN_LEVEL = ESSL3_1_BUILTINS;
275const int GLOBAL_LEVEL = 4;
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000276
Jamie Madillf0d10f82015-03-31 12:56:52 -0400277class TSymbolTable : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700278{
279 public:
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400280 TSymbolTable()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000281 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000282 // The symbol table cannot be used until push() is called, but
283 // the lack of an initial call to push() can be used to detect
284 // that the symbol table has not been preloaded with built-ins.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000285 }
286
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400287 ~TSymbolTable();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000288
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000289 // When the symbol table is initialized with the built-ins, there should
290 // 'push' calls, so that built-ins are at level 0 and the shader
291 // globals are at level 1.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500292 bool isEmpty() const { return table.empty(); }
293 bool atBuiltInLevel() const { return currentLevel() <= LAST_BUILTIN_LEVEL; }
294 bool atGlobalLevel() const { return currentLevel() == GLOBAL_LEVEL; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000295 void push()
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000296 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297 table.push_back(new TSymbolTableLevel);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400298 precisionStack.push_back(new PrecisionStackLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299 }
300
alokp@chromium.org43884872010-03-30 00:08:52 +0000301 void pop()
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400302 {
303 delete table.back();
304 table.pop_back();
305
306 delete precisionStack.back();
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000307 precisionStack.pop_back();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308 }
309
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500310 bool declare(TSymbol *symbol) { return insert(currentLevel(), symbol); }
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000311
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500312 bool insert(ESymbolLevel level, TSymbol *symbol) { return table[level]->insert(symbol); }
alokp@chromium.org43884872010-03-30 00:08:52 +0000313
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500314 bool insert(ESymbolLevel level, const char *ext, TSymbol *symbol)
315 {
316 symbol->relateToExtension(ext);
317 return table[level]->insert(symbol);
318 }
319
Martin Radeve93d24e2016-07-28 12:06:05 +0300320 bool insertConstInt(ESymbolLevel level, const char *name, int value, TPrecision precision)
Nicolas Capens49a88872013-06-20 09:54:03 -0400321 {
Martin Radeve93d24e2016-07-28 12:06:05 +0300322 TVariable *constant =
323 new TVariable(NewPoolTString(name), TType(EbtInt, precision, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200324 TConstantUnion *unionArray = new TConstantUnion[1];
325 unionArray[0].setIConst(value);
326 constant->shareConstPointer(unionArray);
Nicolas Capensadfffe42014-06-17 02:13:36 -0400327 return insert(level, constant);
Nicolas Capens49a88872013-06-20 09:54:03 -0400328 }
329
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300330 bool insertConstIntExt(ESymbolLevel level, const char *ext, const char *name, int value)
331 {
332 TVariable *constant =
333 new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200334 TConstantUnion *unionArray = new TConstantUnion[1];
335 unionArray[0].setIConst(value);
336 constant->shareConstPointer(unionArray);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300337 return insert(level, ext, constant);
338 }
339
Martin Radeve93d24e2016-07-28 12:06:05 +0300340 bool insertConstIvec3(ESymbolLevel level,
341 const char *name,
342 const std::array<int, 3> &values,
343 TPrecision precision)
344 {
345 TVariable *constantIvec3 =
346 new TVariable(NewPoolTString(name), TType(EbtInt, precision, EvqConst, 3));
347
348 TConstantUnion *unionArray = new TConstantUnion[3];
349 for (size_t index = 0u; index < 3u; ++index)
350 {
351 unionArray[index].setIConst(values[index]);
352 }
353 constantIvec3->shareConstPointer(unionArray);
354
355 return insert(level, constantIvec3);
356 }
357
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500358 void insertBuiltIn(ESymbolLevel level,
359 TOperator op,
360 const char *ext,
361 const TType *rvalue,
362 const char *name,
363 const TType *ptype1,
364 const TType *ptype2 = 0,
365 const TType *ptype3 = 0,
366 const TType *ptype4 = 0,
367 const TType *ptype5 = 0);
Nicolas Capens759b9942014-02-14 17:57:14 -0500368
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500369 void insertBuiltIn(ESymbolLevel level,
370 const TType *rvalue,
371 const char *name,
372 const TType *ptype1,
373 const TType *ptype2 = 0,
374 const TType *ptype3 = 0,
375 const TType *ptype4 = 0,
376 const TType *ptype5 = 0)
Nicolas Capens482907e2015-02-23 16:56:33 -0500377 {
Martin Radevda6254b2016-12-14 17:00:36 +0200378 insertUnmangledBuiltInName(name, level);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500379 insertBuiltIn(level, EOpNull, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
380 }
381
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500382 void insertBuiltIn(ESymbolLevel level,
383 const char *ext,
384 const TType *rvalue,
385 const char *name,
386 const TType *ptype1,
387 const TType *ptype2 = 0,
388 const TType *ptype3 = 0,
389 const TType *ptype4 = 0,
390 const TType *ptype5 = 0)
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500391 {
Martin Radevda6254b2016-12-14 17:00:36 +0200392 insertUnmangledBuiltInName(name, level);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500393 insertBuiltIn(level, EOpNull, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
394 }
395
Olli Etuaho492cfab2017-01-20 21:18:29 +0000396 void insertBuiltInOp(ESymbolLevel level,
397 TOperator op,
398 const TType *rvalue,
399 const TType *ptype1,
400 const TType *ptype2 = 0,
401 const TType *ptype3 = 0,
402 const TType *ptype4 = 0,
403 const TType *ptype5 = 0);
404
405 void insertBuiltInOp(ESymbolLevel level,
406 TOperator op,
407 const char *ext,
408 const TType *rvalue,
409 const TType *ptype1,
410 const TType *ptype2 = 0,
411 const TType *ptype3 = 0,
412 const TType *ptype4 = 0,
413 const TType *ptype5 = 0);
Nicolas Capens482907e2015-02-23 16:56:33 -0500414
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300415 void insertBuiltInFunctionNoParameters(ESymbolLevel level,
416 TOperator op,
417 const TType *rvalue,
418 const char *name);
419
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500420 TSymbol *find(const TString &name,
421 int shaderVersion,
422 bool *builtIn = NULL,
423 bool *sameScope = NULL) const;
Zhenyao Mod7490962016-11-09 15:49:51 -0800424
425 TSymbol *findGlobal(const TString &name) const;
426
Zhenyao Moe740add2014-07-18 17:01:01 -0700427 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
Zhenyao Mod7490962016-11-09 15:49:51 -0800428
Olli Etuaho01d0ad02017-01-22 14:51:23 -0800429 // Helper front-end for regular findBuiltIn that constructs the mangled function name from
430 // callNode.
431 TFunction *findBuiltInOp(TIntermAggregate *callNode, int shaderVersion) const;
432
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700433 TSymbolTableLevel *getOuterLevel()
434 {
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000435 assert(currentLevel() >= 1);
daniel@transgaming.com5dd6d092012-03-20 20:10:28 +0000436 return table[currentLevel() - 1];
437 }
438
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400439 void dump(TInfoSink &infoSink) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000440
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700441 bool setDefaultPrecision(const TPublicType &type, TPrecision prec)
442 {
Martin Radev4a9cd802016-09-01 16:51:51 +0300443 if (!SupportsPrecision(type.getBasicType()))
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400444 return false;
Martin Radev4a9cd802016-09-01 16:51:51 +0300445 if (type.getBasicType() == EbtUInt)
Olli Etuaho0980e292015-11-20 14:57:34 +0200446 return false; // ESSL 3.00.4 section 4.5.4
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000447 if (type.isAggregate())
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500448 return false; // Not allowed to set for aggregate types
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000449 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700450 // Uses map operator [], overwrites the current value
Martin Radev4a9cd802016-09-01 16:51:51 +0300451 (*precisionStack[indexOfLastElement])[type.getBasicType()] = prec;
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000452 return true;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000453 }
454
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700455 // Searches down the precisionStack for a precision qualifier
456 // for the specified TBasicType
Zhenyao Moe740add2014-07-18 17:01:01 -0700457 TPrecision getDefaultPrecision(TBasicType type) const;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000458
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700459 // This records invariant varyings declared through
460 // "invariant varying_name;".
Jamie Madill2c433252014-12-03 12:36:54 -0500461 void addInvariantVarying(const std::string &originalName)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700462 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800463 ASSERT(atGlobalLevel());
464 table[currentLevel()]->addInvariantVarying(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700465 }
466 // If this returns false, the varying could still be invariant
467 // if it is set as invariant during the varying variable
468 // declaration - this piece of information is stored in the
469 // variable's type, not here.
Jamie Madill2c433252014-12-03 12:36:54 -0500470 bool isVaryingInvariant(const std::string &originalName) const
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700471 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800472 ASSERT(atGlobalLevel());
473 return table[currentLevel()]->isVaryingInvariant(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700474 }
475
Qiankun Miaof69682b2016-08-16 14:50:42 +0800476 void setGlobalInvariant(bool invariant)
477 {
478 ASSERT(atGlobalLevel());
479 table[currentLevel()]->setGlobalInvariant(invariant);
480 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700481
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500482 static int nextUniqueId() { return ++uniqueIdCounter; }
Jamie Madillbfa91f42014-06-05 15:45:18 -0400483
Martin Radevda6254b2016-12-14 17:00:36 +0200484 // Checks whether there is a built-in accessible by a shader with the specified version.
485 bool hasUnmangledBuiltInForShaderVersion(const char *name, int shaderVersion);
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530486
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700487 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500488 ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000489
Martin Radevda6254b2016-12-14 17:00:36 +0200490 // Used to insert unmangled functions to check redeclaration of built-ins in ESSL 3.00 and
491 // above.
492 void insertUnmangledBuiltInName(const char *name, ESymbolLevel level);
493
494 bool hasUnmangledBuiltInAtLevel(const char *name, ESymbolLevel level);
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530495
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700496 std::vector<TSymbolTableLevel *> table;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400497 typedef TMap<TBasicType, TPrecision> PrecisionStackLevel;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500498 std::vector<PrecisionStackLevel *> precisionStack;
Jamie Madillbfa91f42014-06-05 15:45:18 -0400499
500 static int uniqueIdCounter;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000501};
502
Jamie Madill45bcc782016-11-07 13:58:48 -0500503} // namespace sh
504
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500505#endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_