blob: 671b183d1106b0ee4d286f372cc3db025eb60fa2 [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
Zhenyao Mo9eedea02014-05-12 16:02:35 -070044// Symbol base class. (Can build functions or variables out of these...)
Jamie Madillf0d10f82015-03-31 12:56:52 -040045class TSymbol : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -070046{
47 public:
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040048 POOL_ALLOCATOR_NEW_DELETE();
Olli Etuaho476197f2016-10-11 13:59:08 +010049 TSymbol(const TString *n);
50
Zhenyao Mo9eedea02014-05-12 16:02:35 -070051 virtual ~TSymbol()
52 {
53 // don't delete name, it's from the pool
54 }
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040055
Jamie Madilld7b1ab52016-12-12 14:42:19 -050056 const TString &getName() const { return *name; }
57 virtual const TString &getMangledName() const { return getName(); }
58 virtual bool isFunction() const { return false; }
59 virtual bool isVariable() const { return false; }
60 int getUniqueId() const { return uniqueId; }
61 void relateToExtension(const TString &ext) { extension = ext; }
62 const TString &getExtension() const { return extension; }
Nicolas Capensba60ad32013-06-04 15:55:47 -040063
Zhenyao Mo9eedea02014-05-12 16:02:35 -070064 private:
Olli Etuaho476197f2016-10-11 13:59:08 +010065 const int uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000066 const TString *name;
Jamie Madill2aeb26a2013-07-08 14:02:55 -040067 TString extension;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000068};
69
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000070// Variable class, meaning a symbol that's not a function.
Jamie Madilld7b1ab52016-12-12 14:42:19 -050071//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000072// There could be a separate class heirarchy for Constant variables;
73// Only one of int, bool, or float, (or none) is correct for
74// any particular use, but it's easy to do this way, and doesn't
75// seem worth having separate classes, and "getConst" can't simply return
Jamie Madilld7b1ab52016-12-12 14:42:19 -050076// different values for different types polymorphically, so this is
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077// just simple and pragmatic.
Zhenyao Mo9eedea02014-05-12 16:02:35 -070078class TVariable : public TSymbol
79{
80 public:
81 TVariable(const TString *name, const TType &t, bool uT = false)
Jamie Madilld7b1ab52016-12-12 14:42:19 -050082 : TSymbol(name), type(t), userType(uT), unionArray(0)
Zhenyao Mo9eedea02014-05-12 16:02:35 -070083 {
84 }
Corentin Walleze5a1f272015-08-21 02:58:25 +020085 ~TVariable() override {}
86 bool isVariable() const override { return true; }
Jamie Madilld7b1ab52016-12-12 14:42:19 -050087 TType &getType() { return type; }
88 const TType &getType() const { return type; }
89 bool isUserType() const { return userType; }
90 void setQualifier(TQualifier qualifier) { type.setQualifier(qualifier); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000091
Olli Etuaho5c0e0232015-11-11 15:55:59 +020092 const TConstantUnion *getConstPointer() const { return unionArray; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093
Olli Etuaho5c0e0232015-11-11 15:55:59 +020094 void shareConstPointer(const TConstantUnion *constArray) { unionArray = constArray; }
alokp@chromium.org43884872010-03-30 00:08:52 +000095
Zhenyao Mo9eedea02014-05-12 16:02:35 -070096 private:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097 TType type;
98 bool userType;
Zhenyao Mo9eedea02014-05-12 16:02:35 -070099 // we are assuming that Pool Allocator will free the memory
100 // allocated to unionArray when this object is destroyed.
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200101 const TConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102};
103
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700104// Immutable version of TParameter.
105struct TConstParameter
106{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500107 TConstParameter() : name(nullptr), type(nullptr) {}
108 explicit TConstParameter(const TString *n) : name(n), type(nullptr) {}
109 explicit TConstParameter(const TType *t) : name(nullptr), type(t) {}
110 TConstParameter(const TString *n, const TType *t) : name(n), type(t) {}
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700111
112 // Both constructor arguments must be const.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500113 TConstParameter(TString *n, TType *t) = delete;
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700114 TConstParameter(const TString *n, TType *t) = delete;
115 TConstParameter(TString *n, const TType *t) = delete;
116
117 const TString *name;
118 const TType *type;
119};
120
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121// The function sub-class of symbols and the parser will need to
122// share this definition of a function parameter.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700123struct TParameter
124{
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700125 // Destructively converts to TConstParameter.
126 // This method resets name and type to nullptrs to make sure
127 // their content cannot be modified after the call.
128 TConstParameter turnToConst()
129 {
130 const TString *constName = name;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500131 const TType *constType = type;
132 name = nullptr;
133 type = nullptr;
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700134 return TConstParameter(constName, constType);
135 }
136
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137 TString *name;
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400138 TType *type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139};
140
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500141// The function sub-class of a symbol.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700142class TFunction : public TSymbol
143{
144 public:
Olli Etuaho5d653182016-01-04 14:43:28 +0200145 TFunction(const TString *name,
146 const TType *retType,
147 TOperator tOp = EOpNull,
148 const char *ext = "")
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700149 : TSymbol(name),
150 returnType(retType),
Dmitry Skiba58832202015-07-06 16:11:13 -0700151 mangledName(nullptr),
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700152 op(tOp),
Olli Etuaho5d653182016-01-04 14:43:28 +0200153 defined(false),
154 mHasPrototypeDeclaration(false)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700155 {
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500156 relateToExtension(ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700157 }
Corentin Walleze5a1f272015-08-21 02:58:25 +0200158 ~TFunction() override;
159 bool isFunction() const override { return true; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000160
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500161 static TString mangleName(const TString &name) { return name + '('; }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700162 static TString unmangleName(const TString &mangledName)
alokp@chromium.org43884872010-03-30 00:08:52 +0000163 {
164 return TString(mangledName.c_str(), mangledName.find_first_of('('));
165 }
166
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700167 void addParameter(const TConstParameter &p)
Dmitry Skiba58832202015-07-06 16:11:13 -0700168 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169 parameters.push_back(p);
Dmitry Skiba58832202015-07-06 16:11:13 -0700170 mangledName = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000171 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000172
Olli Etuaho476197f2016-10-11 13:59:08 +0100173 void swapParameters(const TFunction &parametersSource);
174
Corentin Walleze5a1f272015-08-21 02:58:25 +0200175 const TString &getMangledName() const override
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700176 {
Dmitry Skiba58832202015-07-06 16:11:13 -0700177 if (mangledName == nullptr)
178 {
179 mangledName = buildMangledName();
180 }
181 return *mangledName;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700182 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800183
184 static const TString &GetMangledNameFromCall(const TString &unmangledFunctionName,
185 TIntermSequence &arguments);
186
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500187 const TType &getReturnType() const { return *returnType; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000188
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500189 TOperator getBuiltInOp() const { return op; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000190
Olli Etuaho5d653182016-01-04 14:43:28 +0200191 void setDefined() { defined = true; }
192 bool isDefined() { return defined; }
193 void setHasPrototypeDeclaration() { mHasPrototypeDeclaration = true; }
194 bool hasPrototypeDeclaration() const { return mHasPrototypeDeclaration; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000195
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500196 size_t getParamCount() const { return parameters.size(); }
197 const TConstParameter &getParam(size_t i) const { return parameters[i]; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000198
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700199 private:
Olli Etuaho476197f2016-10-11 13:59:08 +0100200 void clearParameters();
201
Dmitry Skiba58832202015-07-06 16:11:13 -0700202 const TString *buildMangledName() const;
203
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700204 typedef TVector<TConstParameter> TParamList;
alokp@chromium.org43884872010-03-30 00:08:52 +0000205 TParamList parameters;
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700206 const TType *returnType;
Dmitry Skiba58832202015-07-06 16:11:13 -0700207 mutable const TString *mangledName;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000208 TOperator op;
209 bool defined;
Olli Etuaho5d653182016-01-04 14:43:28 +0200210 bool mHasPrototypeDeclaration;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211};
212
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000213// Interface block name sub-symbol
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000214class TInterfaceBlockName : public TSymbol
215{
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700216 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500217 TInterfaceBlockName(const TString *name) : TSymbol(name) {}
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000218
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500219 virtual ~TInterfaceBlockName() {}
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000220};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700222class TSymbolTableLevel
223{
224 public:
225 typedef TMap<TString, TSymbol *> tLevel;
alokp@chromium.org43884872010-03-30 00:08:52 +0000226 typedef tLevel::const_iterator const_iterator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000227 typedef const tLevel::value_type tLevelPair;
228 typedef std::pair<tLevel::iterator, bool> tInsertResult;
229
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500230 TSymbolTableLevel() : mGlobalInvariant(false) {}
alokp@chromium.org43884872010-03-30 00:08:52 +0000231 ~TSymbolTableLevel();
232
Nicolas Capensadfffe42014-06-17 02:13:36 -0400233 bool insert(TSymbol *symbol);
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400234
Olli Etuahob2983c92015-03-18 14:02:46 +0200235 // Insert a function using its unmangled name as the key.
236 bool insertUnmangled(TFunction *function);
237
Jamie Madillbfa91f42014-06-05 15:45:18 -0400238 TSymbol *find(const TString &name) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000239
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500240 void addInvariantVarying(const std::string &name) { mInvariantVaryings.insert(name); }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800241
242 bool isVaryingInvariant(const std::string &name)
243 {
244 return (mGlobalInvariant || mInvariantVaryings.count(name) > 0);
245 }
246
247 void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; }
248
Martin Radevda6254b2016-12-14 17:00:36 +0200249 void insertUnmangledBuiltInName(const std::string &name)
250 {
251 mUnmangledBuiltInNames.insert(name);
252 }
253
254 bool hasUnmangledBuiltIn(const std::string &name)
255 {
256 return mUnmangledBuiltInNames.count(name) > 0;
257 }
258
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700259 protected:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260 tLevel level;
Qiankun Miaof69682b2016-08-16 14:50:42 +0800261 std::set<std::string> mInvariantVaryings;
262 bool mGlobalInvariant;
Martin Radevda6254b2016-12-14 17:00:36 +0200263
264 private:
265 std::set<std::string> mUnmangledBuiltInNames;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266};
267
Gus Fernandez964df492014-10-13 11:54:39 -0700268// Define ESymbolLevel as int rather than an enum since level can go
269// above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the
270// compiler optimizes the >= of the last element to ==.
271typedef int ESymbolLevel;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500272const int COMMON_BUILTINS = 0;
273const int ESSL1_BUILTINS = 1;
274const int ESSL3_BUILTINS = 2;
Martin Radeve93d24e2016-07-28 12:06:05 +0300275const int ESSL3_1_BUILTINS = 3;
276const int LAST_BUILTIN_LEVEL = ESSL3_1_BUILTINS;
277const int GLOBAL_LEVEL = 4;
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000278
Jamie Madillf0d10f82015-03-31 12:56:52 -0400279class TSymbolTable : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700280{
281 public:
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400282 TSymbolTable()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284 // The symbol table cannot be used until push() is called, but
285 // the lack of an initial call to push() can be used to detect
286 // that the symbol table has not been preloaded with built-ins.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287 }
288
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400289 ~TSymbolTable();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291 // When the symbol table is initialized with the built-ins, there should
292 // 'push' calls, so that built-ins are at level 0 and the shader
293 // globals are at level 1.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500294 bool isEmpty() const { return table.empty(); }
295 bool atBuiltInLevel() const { return currentLevel() <= LAST_BUILTIN_LEVEL; }
296 bool atGlobalLevel() const { return currentLevel() == GLOBAL_LEVEL; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000297 void push()
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000298 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299 table.push_back(new TSymbolTableLevel);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400300 precisionStack.push_back(new PrecisionStackLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301 }
302
alokp@chromium.org43884872010-03-30 00:08:52 +0000303 void pop()
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400304 {
305 delete table.back();
306 table.pop_back();
307
308 delete precisionStack.back();
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000309 precisionStack.pop_back();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310 }
311
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500312 bool declare(TSymbol *symbol) { return insert(currentLevel(), symbol); }
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000313
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500314 bool insert(ESymbolLevel level, TSymbol *symbol) { return table[level]->insert(symbol); }
alokp@chromium.org43884872010-03-30 00:08:52 +0000315
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500316 bool insert(ESymbolLevel level, const char *ext, TSymbol *symbol)
317 {
318 symbol->relateToExtension(ext);
319 return table[level]->insert(symbol);
320 }
321
Martin Radeve93d24e2016-07-28 12:06:05 +0300322 bool insertConstInt(ESymbolLevel level, const char *name, int value, TPrecision precision)
Nicolas Capens49a88872013-06-20 09:54:03 -0400323 {
Martin Radeve93d24e2016-07-28 12:06:05 +0300324 TVariable *constant =
325 new TVariable(NewPoolTString(name), TType(EbtInt, precision, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200326 TConstantUnion *unionArray = new TConstantUnion[1];
327 unionArray[0].setIConst(value);
328 constant->shareConstPointer(unionArray);
Nicolas Capensadfffe42014-06-17 02:13:36 -0400329 return insert(level, constant);
Nicolas Capens49a88872013-06-20 09:54:03 -0400330 }
331
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300332 bool insertConstIntExt(ESymbolLevel level, const char *ext, const char *name, int value)
333 {
334 TVariable *constant =
335 new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1));
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200336 TConstantUnion *unionArray = new TConstantUnion[1];
337 unionArray[0].setIConst(value);
338 constant->shareConstPointer(unionArray);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300339 return insert(level, ext, constant);
340 }
341
Martin Radeve93d24e2016-07-28 12:06:05 +0300342 bool insertConstIvec3(ESymbolLevel level,
343 const char *name,
344 const std::array<int, 3> &values,
345 TPrecision precision)
346 {
347 TVariable *constantIvec3 =
348 new TVariable(NewPoolTString(name), TType(EbtInt, precision, EvqConst, 3));
349
350 TConstantUnion *unionArray = new TConstantUnion[3];
351 for (size_t index = 0u; index < 3u; ++index)
352 {
353 unionArray[index].setIConst(values[index]);
354 }
355 constantIvec3->shareConstPointer(unionArray);
356
357 return insert(level, constantIvec3);
358 }
359
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500360 void insertBuiltIn(ESymbolLevel level,
361 TOperator op,
362 const char *ext,
363 const TType *rvalue,
364 const char *name,
365 const TType *ptype1,
366 const TType *ptype2 = 0,
367 const TType *ptype3 = 0,
368 const TType *ptype4 = 0,
369 const TType *ptype5 = 0);
Nicolas Capens759b9942014-02-14 17:57:14 -0500370
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500371 void insertBuiltIn(ESymbolLevel level,
372 const TType *rvalue,
373 const char *name,
374 const TType *ptype1,
375 const TType *ptype2 = 0,
376 const TType *ptype3 = 0,
377 const TType *ptype4 = 0,
378 const TType *ptype5 = 0)
Nicolas Capens482907e2015-02-23 16:56:33 -0500379 {
Martin Radevda6254b2016-12-14 17:00:36 +0200380 insertUnmangledBuiltInName(name, level);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500381 insertBuiltIn(level, EOpNull, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
382 }
383
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500384 void insertBuiltIn(ESymbolLevel level,
385 const char *ext,
386 const TType *rvalue,
387 const char *name,
388 const TType *ptype1,
389 const TType *ptype2 = 0,
390 const TType *ptype3 = 0,
391 const TType *ptype4 = 0,
392 const TType *ptype5 = 0)
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500393 {
Martin Radevda6254b2016-12-14 17:00:36 +0200394 insertUnmangledBuiltInName(name, level);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500395 insertBuiltIn(level, EOpNull, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
396 }
397
Olli Etuaho492cfab2017-01-20 21:18:29 +0000398 void insertBuiltInOp(ESymbolLevel level,
399 TOperator op,
400 const TType *rvalue,
401 const TType *ptype1,
402 const TType *ptype2 = 0,
403 const TType *ptype3 = 0,
404 const TType *ptype4 = 0,
405 const TType *ptype5 = 0);
406
407 void insertBuiltInOp(ESymbolLevel level,
408 TOperator op,
409 const char *ext,
410 const TType *rvalue,
411 const TType *ptype1,
412 const TType *ptype2 = 0,
413 const TType *ptype3 = 0,
414 const TType *ptype4 = 0,
415 const TType *ptype5 = 0);
Nicolas Capens482907e2015-02-23 16:56:33 -0500416
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300417 void insertBuiltInFunctionNoParameters(ESymbolLevel level,
418 TOperator op,
419 const TType *rvalue,
420 const char *name);
421
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500422 TSymbol *find(const TString &name,
423 int shaderVersion,
424 bool *builtIn = NULL,
425 bool *sameScope = NULL) const;
Zhenyao Mod7490962016-11-09 15:49:51 -0800426
427 TSymbol *findGlobal(const TString &name) const;
428
Zhenyao Moe740add2014-07-18 17:01:01 -0700429 TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
Zhenyao Mod7490962016-11-09 15:49:51 -0800430
Olli Etuaho01d0ad02017-01-22 14:51:23 -0800431 // Helper front-end for regular findBuiltIn that constructs the mangled function name from
432 // callNode.
433 TFunction *findBuiltInOp(TIntermAggregate *callNode, int shaderVersion) const;
434
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700435 TSymbolTableLevel *getOuterLevel()
436 {
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000437 assert(currentLevel() >= 1);
daniel@transgaming.com5dd6d092012-03-20 20:10:28 +0000438 return table[currentLevel() - 1];
439 }
440
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400441 void dump(TInfoSink &infoSink) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000442
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700443 bool setDefaultPrecision(const TPublicType &type, TPrecision prec)
444 {
Martin Radev4a9cd802016-09-01 16:51:51 +0300445 if (!SupportsPrecision(type.getBasicType()))
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400446 return false;
Martin Radev4a9cd802016-09-01 16:51:51 +0300447 if (type.getBasicType() == EbtUInt)
Olli Etuaho0980e292015-11-20 14:57:34 +0200448 return false; // ESSL 3.00.4 section 4.5.4
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000449 if (type.isAggregate())
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500450 return false; // Not allowed to set for aggregate types
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000451 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700452 // Uses map operator [], overwrites the current value
Martin Radev4a9cd802016-09-01 16:51:51 +0300453 (*precisionStack[indexOfLastElement])[type.getBasicType()] = prec;
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000454 return true;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000455 }
456
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700457 // Searches down the precisionStack for a precision qualifier
458 // for the specified TBasicType
Zhenyao Moe740add2014-07-18 17:01:01 -0700459 TPrecision getDefaultPrecision(TBasicType type) const;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000460
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700461 // This records invariant varyings declared through
462 // "invariant varying_name;".
Jamie Madill2c433252014-12-03 12:36:54 -0500463 void addInvariantVarying(const std::string &originalName)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700464 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800465 ASSERT(atGlobalLevel());
466 table[currentLevel()]->addInvariantVarying(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700467 }
468 // If this returns false, the varying could still be invariant
469 // if it is set as invariant during the varying variable
470 // declaration - this piece of information is stored in the
471 // variable's type, not here.
Jamie Madill2c433252014-12-03 12:36:54 -0500472 bool isVaryingInvariant(const std::string &originalName) const
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700473 {
Qiankun Miaof69682b2016-08-16 14:50:42 +0800474 ASSERT(atGlobalLevel());
475 return table[currentLevel()]->isVaryingInvariant(originalName);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700476 }
477
Qiankun Miaof69682b2016-08-16 14:50:42 +0800478 void setGlobalInvariant(bool invariant)
479 {
480 ASSERT(atGlobalLevel());
481 table[currentLevel()]->setGlobalInvariant(invariant);
482 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700483
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500484 static int nextUniqueId() { return ++uniqueIdCounter; }
Jamie Madillbfa91f42014-06-05 15:45:18 -0400485
Martin Radevda6254b2016-12-14 17:00:36 +0200486 // Checks whether there is a built-in accessible by a shader with the specified version.
487 bool hasUnmangledBuiltInForShaderVersion(const char *name, int shaderVersion);
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530488
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700489 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500490 ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000491
Martin Radevda6254b2016-12-14 17:00:36 +0200492 // Used to insert unmangled functions to check redeclaration of built-ins in ESSL 3.00 and
493 // above.
494 void insertUnmangledBuiltInName(const char *name, ESymbolLevel level);
495
496 bool hasUnmangledBuiltInAtLevel(const char *name, ESymbolLevel level);
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530497
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700498 std::vector<TSymbolTableLevel *> table;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400499 typedef TMap<TBasicType, TPrecision> PrecisionStackLevel;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500500 std::vector<PrecisionStackLevel *> precisionStack;
Jamie Madillbfa91f42014-06-05 15:45:18 -0400501
502 static int uniqueIdCounter;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000503};
504
Jamie Madill45bcc782016-11-07 13:58:48 -0500505} // namespace sh
506
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500507#endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_