blob: 5ff31a676072653f7f4c3bb8a79b5ee252a8a992 [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"
Olli Etuaho2a1e8f92017-07-14 11:49:36 +030038#include "compiler/translator/ExtensionBehavior.h"
Olli Etuahofbb1c792018-01-19 16:26:59 +020039#include "compiler/translator/ImmutableString.h"
Geoff Lang17732822013-08-29 13:46:49 -040040#include "compiler/translator/InfoSink.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040041#include "compiler/translator/IntermNode.h"
Olli Etuahob60d30f2018-01-16 12:31:06 +020042#include "compiler/translator/StaticType.h"
Olli Etuahod4529f32017-12-12 13:06:40 +020043#include "compiler/translator/Symbol.h"
alokp@chromium.org43884872010-03-30 00:08:52 +000044
Jamie Madill45bcc782016-11-07 13:58:48 -050045namespace sh
46{
47
Gus Fernandez964df492014-10-13 11:54:39 -070048// Define ESymbolLevel as int rather than an enum since level can go
49// above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the
50// compiler optimizes the >= of the last element to ==.
51typedef int ESymbolLevel;
Jamie Madilld7b1ab52016-12-12 14:42:19 -050052const int COMMON_BUILTINS = 0;
53const int ESSL1_BUILTINS = 1;
54const int ESSL3_BUILTINS = 2;
Martin Radeve93d24e2016-07-28 12:06:05 +030055const int ESSL3_1_BUILTINS = 3;
Olli Etuaho977ee7e2017-07-21 11:38:27 +030056// GLSL_BUILTINS are desktop GLSL builtins that don't exist in ESSL but are used to implement
57// features in ANGLE's GLSL backend. They're not visible to the parser.
58const int GLSL_BUILTINS = 4;
59const int LAST_BUILTIN_LEVEL = GLSL_BUILTINS;
60const int GLOBAL_LEVEL = 5;
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +000061
Jamie Madillf0d10f82015-03-31 12:56:52 -040062class TSymbolTable : angle::NonCopyable
Zhenyao Mo9eedea02014-05-12 16:02:35 -070063{
64 public:
Olli Etuaho195be942017-12-04 23:40:14 +020065 TSymbolTable() : mUniqueIdCounter(0), mUserDefinedUniqueIdsStart(-1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000066 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000067 // The symbol table cannot be used until push() is called, but
68 // the lack of an initial call to push() can be used to detect
69 // that the symbol table has not been preloaded with built-ins.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000070 }
71
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040072 ~TSymbolTable();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000073
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000074 // When the symbol table is initialized with the built-ins, there should
75 // 'push' calls, so that built-ins are at level 0 and the shader
76 // globals are at level 1.
Jamie Madilld7b1ab52016-12-12 14:42:19 -050077 bool isEmpty() const { return table.empty(); }
78 bool atBuiltInLevel() const { return currentLevel() <= LAST_BUILTIN_LEVEL; }
79 bool atGlobalLevel() const { return currentLevel() == GLOBAL_LEVEL; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080
Olli Etuahodd21ecf2018-01-10 12:42:09 +020081 void push();
82 void pop();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000083
Olli Etuaho0f684632017-07-13 12:42:15 +030084 // The declare* entry points are used when parsing and declare symbols at the current scope.
Olli Etuahodd21ecf2018-01-10 12:42:09 +020085 // They return the created true in case the declaration was successful, and false if the
86 // declaration failed due to redefinition.
Olli Etuaho195be942017-12-04 23:40:14 +020087 bool declareVariable(TVariable *variable);
Olli Etuaho035419f2017-11-28 14:27:15 +020088 bool declareStructType(TStructure *str);
Olli Etuaho378c3a52017-12-04 11:32:13 +020089 bool declareInterfaceBlock(TInterfaceBlock *interfaceBlock);
Olli Etuahodd21ecf2018-01-10 12:42:09 +020090 // Functions are always declared at global scope.
91 void declareUserDefinedFunction(TFunction *function, bool insertUnmangledName);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +000092
Olli Etuaho29bda812018-01-26 17:37:36 +020093 // These return the TFunction pointer to keep using to refer to this function.
94 const TFunction *markUserDefinedFunctionHasPrototypeDeclaration(
95 const ImmutableString &mangledName,
96 bool *hadPrototypeDeclarationOut);
97 const TFunction *setUserDefinedFunctionParameterNamesFromDefinition(const TFunction *function,
98 bool *wasDefinedOut);
99
100 // find() is guaranteed not to retain a reference to the ImmutableString, so an ImmutableString
101 // with a reference to a short-lived char * is fine to pass here.
102 const TSymbol *find(const ImmutableString &name, int shaderVersion) const;
103
104 const TSymbol *findGlobal(const ImmutableString &name) const;
105
106 const TSymbol *findBuiltIn(const ImmutableString &name, int shaderVersion) const;
107
108 const TSymbol *findBuiltIn(const ImmutableString &name,
109 int shaderVersion,
110 bool includeGLSLBuiltins) const;
111
112 void setDefaultPrecision(TBasicType type, TPrecision prec)
113 {
114 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
115 // Uses map operator [], overwrites the current value
116 (*precisionStack[indexOfLastElement])[type] = prec;
117 }
118
119 // Searches down the precisionStack for a precision qualifier
120 // for the specified TBasicType
121 TPrecision getDefaultPrecision(TBasicType type) const;
122
123 // This records invariant varyings declared through
124 // "invariant varying_name;".
125 void addInvariantVarying(const std::string &originalName);
126
127 // If this returns false, the varying could still be invariant
128 // if it is set as invariant during the varying variable
129 // declaration - this piece of information is stored in the
130 // variable's type, not here.
131 bool isVaryingInvariant(const std::string &originalName) const;
132
133 void setGlobalInvariant(bool invariant);
134
135 const TSymbolUniqueId nextUniqueId() { return TSymbolUniqueId(this); }
136
137 // Checks whether there is a built-in accessible by a shader with the specified version.
138 bool hasUnmangledBuiltInForShaderVersion(const char *name, int shaderVersion);
139
140 void initializeBuiltIns(sh::GLenum type,
141 ShShaderSpec spec,
142 const ShBuiltInResources &resources);
143 void clearCompilationResults();
144
145 private:
146 friend class TSymbolUniqueId;
147 int nextUniqueIdValue();
148
149 class TSymbolTableLevel;
150
151 ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
152
Olli Etuaho0f684632017-07-13 12:42:15 +0300153 // The insert* entry points are used when initializing the symbol table with built-ins.
Olli Etuaho035419f2017-11-28 14:27:15 +0200154 // They return the created symbol / true in case the declaration was successful, and nullptr /
155 // false if the declaration failed due to redefinition.
Olli Etuahofbb1c792018-01-19 16:26:59 +0200156 TVariable *insertVariable(ESymbolLevel level, const ImmutableString &name, const TType *type);
Olli Etuaho0f684632017-07-13 12:42:15 +0300157 TVariable *insertVariableExt(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300158 TExtension ext,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200159 const ImmutableString &name,
Olli Etuahob60d30f2018-01-16 12:31:06 +0200160 const TType *type);
Olli Etuaho195be942017-12-04 23:40:14 +0200161 bool insertVariable(ESymbolLevel level, TVariable *variable);
Olli Etuaho035419f2017-11-28 14:27:15 +0200162 bool insertStructType(ESymbolLevel level, TStructure *str);
Olli Etuaho378c3a52017-12-04 11:32:13 +0200163 bool insertInterfaceBlock(ESymbolLevel level, TInterfaceBlock *interfaceBlock);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500164
Olli Etuahob60d30f2018-01-16 12:31:06 +0200165 template <TPrecision precision>
Olli Etuahofbb1c792018-01-19 16:26:59 +0200166 bool insertConstInt(ESymbolLevel level, const ImmutableString &name, int value);
Nicolas Capens49a88872013-06-20 09:54:03 -0400167
Olli Etuahob60d30f2018-01-16 12:31:06 +0200168 template <TPrecision precision>
Olli Etuahofbb1c792018-01-19 16:26:59 +0200169 bool insertConstIntExt(ESymbolLevel level,
170 TExtension ext,
171 const ImmutableString &name,
172 int value);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300173
Olli Etuahob60d30f2018-01-16 12:31:06 +0200174 template <TPrecision precision>
Olli Etuahofbb1c792018-01-19 16:26:59 +0200175 bool insertConstIvec3(ESymbolLevel level,
176 const ImmutableString &name,
177 const std::array<int, 3> &values);
Martin Radeve93d24e2016-07-28 12:06:05 +0300178
Olli Etuaho342b83d2018-01-10 13:24:01 +0200179 // Note that for inserted built-in functions the const char *name needs to remain valid for the
180 // lifetime of the SymbolTable. SymbolTable does not allocate a copy of it.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500181 void insertBuiltIn(ESymbolLevel level,
182 TOperator op,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300183 TExtension ext,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500184 const TType *rvalue,
185 const char *name,
186 const TType *ptype1,
187 const TType *ptype2 = 0,
188 const TType *ptype3 = 0,
189 const TType *ptype4 = 0,
190 const TType *ptype5 = 0);
Nicolas Capens759b9942014-02-14 17:57:14 -0500191
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500192 void insertBuiltIn(ESymbolLevel level,
193 const TType *rvalue,
194 const char *name,
195 const TType *ptype1,
196 const TType *ptype2 = 0,
197 const TType *ptype3 = 0,
198 const TType *ptype4 = 0,
199 const TType *ptype5 = 0)
Nicolas Capens482907e2015-02-23 16:56:33 -0500200 {
Martin Radevda6254b2016-12-14 17:00:36 +0200201 insertUnmangledBuiltInName(name, level);
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300202 insertBuiltIn(level, EOpNull, TExtension::UNDEFINED, rvalue, name, ptype1, ptype2, ptype3,
203 ptype4, ptype5);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500204 }
205
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500206 void insertBuiltIn(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300207 TExtension ext,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500208 const TType *rvalue,
209 const char *name,
210 const TType *ptype1,
211 const TType *ptype2 = 0,
212 const TType *ptype3 = 0,
213 const TType *ptype4 = 0,
214 const TType *ptype5 = 0)
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500215 {
Martin Radevda6254b2016-12-14 17:00:36 +0200216 insertUnmangledBuiltInName(name, level);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500217 insertBuiltIn(level, EOpNull, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
218 }
219
Olli Etuaho492cfab2017-01-20 21:18:29 +0000220 void insertBuiltInOp(ESymbolLevel level,
221 TOperator op,
222 const TType *rvalue,
223 const TType *ptype1,
224 const TType *ptype2 = 0,
225 const TType *ptype3 = 0,
226 const TType *ptype4 = 0,
227 const TType *ptype5 = 0);
228
229 void insertBuiltInOp(ESymbolLevel level,
230 TOperator op,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300231 TExtension ext,
Olli Etuaho492cfab2017-01-20 21:18:29 +0000232 const TType *rvalue,
233 const TType *ptype1,
234 const TType *ptype2 = 0,
235 const TType *ptype3 = 0,
236 const TType *ptype4 = 0,
237 const TType *ptype5 = 0);
Nicolas Capens482907e2015-02-23 16:56:33 -0500238
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300239 void insertBuiltInFunctionNoParameters(ESymbolLevel level,
240 TOperator op,
241 const TType *rvalue,
242 const char *name);
243
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800244 void insertBuiltInFunctionNoParametersExt(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300245 TExtension ext,
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800246 TOperator op,
247 const TType *rvalue,
248 const char *name);
249
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100250 TVariable *insertVariable(ESymbolLevel level,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200251 const ImmutableString &name,
Olli Etuahob60d30f2018-01-16 12:31:06 +0200252 const TType *type,
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100253 SymbolType symbolType);
Olli Etuaho0f684632017-07-13 12:42:15 +0300254
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200255 bool insert(ESymbolLevel level, TSymbol *symbol);
256
Olli Etuahofbb1c792018-01-19 16:26:59 +0200257 TFunction *findUserDefinedFunction(const ImmutableString &name) const;
Olli Etuaho0f684632017-07-13 12:42:15 +0300258
Martin Radevda6254b2016-12-14 17:00:36 +0200259 // Used to insert unmangled functions to check redeclaration of built-ins in ESSL 3.00 and
260 // above.
261 void insertUnmangledBuiltInName(const char *name, ESymbolLevel level);
262
263 bool hasUnmangledBuiltInAtLevel(const char *name, ESymbolLevel level);
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530264
Olli Etuaho29bda812018-01-26 17:37:36 +0200265 void initSamplerDefaultPrecision(TBasicType samplerType);
266
267 void initializeBuiltInFunctions(sh::GLenum type,
268 ShShaderSpec spec,
269 const ShBuiltInResources &resources);
270 void initializeBuiltInVariables(sh::GLenum type,
271 ShShaderSpec spec,
272 const ShBuiltInResources &resources);
273 void markBuiltInInitializationFinished();
274
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700275 std::vector<TSymbolTableLevel *> table;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400276 typedef TMap<TBasicType, TPrecision> PrecisionStackLevel;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500277 std::vector<PrecisionStackLevel *> precisionStack;
Jamie Madillbfa91f42014-06-05 15:45:18 -0400278
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300279 int mUniqueIdCounter;
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300280
Olli Etuaho5d69db12017-11-24 16:51:15 +0200281 // -1 before built-in init has finished, one past the last built-in id afterwards.
282 // TODO(oetuaho): Make this a compile-time constant once the symbol table is initialized at
283 // compile time. http://anglebug.com/1432
284 int mUserDefinedUniqueIdsStart;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000285};
286
Jamie Madill45bcc782016-11-07 13:58:48 -0500287} // namespace sh
288
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500289#endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_