glsl: Move constant expression handling from calls to signatures.
[mesa.git] / src / glsl / glsl_parser_extras.h
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #pragma once
25 #ifndef GLSL_PARSER_EXTRAS_H
26 #define GLSL_PARSER_EXTRAS_H
27
28 /*
29 * Most of the definitions here only apply to C++
30 */
31 #ifdef __cplusplus
32
33
34 #include <stdlib.h>
35 #include "glsl_symbol_table.h"
36
37 enum _mesa_glsl_parser_targets {
38 vertex_shader,
39 geometry_shader,
40 fragment_shader
41 };
42
43 struct gl_context;
44
45 struct glsl_switch_state {
46 /** Temporary variables needed for switch statement. */
47 ir_variable *test_var;
48 ir_variable *is_fallthru_var;
49 ir_variable *is_break_var;
50 class ast_switch_statement *switch_nesting_ast;
51
52 /** Table of constant values already used in case labels */
53 struct hash_table *labels_ht;
54 class ast_case_label *previous_default;
55
56 bool is_switch_innermost; // if switch stmt is closest to break, ...
57 };
58
59 struct _mesa_glsl_parse_state {
60 _mesa_glsl_parse_state(struct gl_context *ctx, GLenum target,
61 void *mem_ctx);
62
63 /* Callers of this ralloc-based new need not call delete. It's
64 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
65 static void* operator new(size_t size, void *ctx)
66 {
67 void *mem = rzalloc_size(ctx, size);
68 assert(mem != NULL);
69
70 return mem;
71 }
72
73 /* If the user *does* call delete, that's OK, we will just
74 * ralloc_free in that case. */
75 static void operator delete(void *mem)
76 {
77 ralloc_free(mem);
78 }
79
80 void *scanner;
81 exec_list translation_unit;
82 glsl_symbol_table *symbols;
83
84 bool es_shader;
85 unsigned language_version;
86 const char *version_string;
87 enum _mesa_glsl_parser_targets target;
88
89 /**
90 * Printable list of GLSL versions supported by the current context
91 *
92 * \note
93 * This string should probably be generated per-context instead of per
94 * invokation of the compiler. This should be changed when the method of
95 * tracking supported GLSL versions changes.
96 */
97 const char *supported_version_string;
98
99 /**
100 * Implementation defined limits that affect built-in variables, etc.
101 *
102 * \sa struct gl_constants (in mtypes.h)
103 */
104 struct {
105 /* 1.10 */
106 unsigned MaxLights;
107 unsigned MaxClipPlanes;
108 unsigned MaxTextureUnits;
109 unsigned MaxTextureCoords;
110 unsigned MaxVertexAttribs;
111 unsigned MaxVertexUniformComponents;
112 unsigned MaxVaryingFloats;
113 unsigned MaxVertexTextureImageUnits;
114 unsigned MaxCombinedTextureImageUnits;
115 unsigned MaxTextureImageUnits;
116 unsigned MaxFragmentUniformComponents;
117
118 /* ARB_draw_buffers */
119 unsigned MaxDrawBuffers;
120
121 /**
122 * Set of GLSL versions supported by the current context
123 *
124 * Knowing that version X is supported doesn't mean that versions before
125 * X are also supported. Version 1.00 is only supported in an ES2
126 * context or when GL_ARB_ES2_compatibility is supported. In an OpenGL
127 * 3.0 "forward compatible" context, GLSL 1.10 and 1.20 are \b not
128 * supported.
129 */
130 /*@{*/
131 unsigned GLSL_100ES:1;
132 unsigned GLSL_110:1;
133 unsigned GLSL_120:1;
134 unsigned GLSL_130:1;
135 unsigned GLSL_140:1;
136 /*@}*/
137 } Const;
138
139 /**
140 * During AST to IR conversion, pointer to current IR function
141 *
142 * Will be \c NULL whenever the AST to IR conversion is not inside a
143 * function definition.
144 */
145 class ir_function_signature *current_function;
146
147 /**
148 * During AST to IR conversion, pointer to the toplevel IR
149 * instruction list being generated.
150 */
151 exec_list *toplevel_ir;
152
153 /** Have we found a return statement in this function? */
154 bool found_return;
155
156 /** Was there an error during compilation? */
157 bool error;
158
159 /**
160 * Are all shader inputs / outputs invariant?
161 *
162 * This is set when the 'STDGL invariant(all)' pragma is used.
163 */
164 bool all_invariant;
165
166 /** Loop or switch statement containing the current instructions. */
167 class ast_iteration_statement *loop_nesting_ast;
168
169 struct glsl_switch_state switch_state;
170
171 /** List of structures defined in user code. */
172 const glsl_type **user_structures;
173 unsigned num_user_structures;
174
175 char *info_log;
176
177 /**
178 * \name Enable bits for GLSL extensions
179 */
180 /*@{*/
181 bool ARB_draw_buffers_enable;
182 bool ARB_draw_buffers_warn;
183 bool ARB_draw_instanced_enable;
184 bool ARB_draw_instanced_warn;
185 bool ARB_explicit_attrib_location_enable;
186 bool ARB_explicit_attrib_location_warn;
187 bool ARB_fragment_coord_conventions_enable;
188 bool ARB_fragment_coord_conventions_warn;
189 bool ARB_texture_rectangle_enable;
190 bool ARB_texture_rectangle_warn;
191 bool EXT_texture_array_enable;
192 bool EXT_texture_array_warn;
193 bool ARB_shader_texture_lod_enable;
194 bool ARB_shader_texture_lod_warn;
195 bool ARB_shader_stencil_export_enable;
196 bool ARB_shader_stencil_export_warn;
197 bool AMD_conservative_depth_enable;
198 bool AMD_conservative_depth_warn;
199 bool ARB_conservative_depth_enable;
200 bool ARB_conservative_depth_warn;
201 bool AMD_shader_stencil_export_enable;
202 bool AMD_shader_stencil_export_warn;
203 bool OES_texture_3D_enable;
204 bool OES_texture_3D_warn;
205 bool OES_EGL_image_external_enable;
206 bool OES_EGL_image_external_warn;
207 /*@}*/
208
209 /** Extensions supported by the OpenGL implementation. */
210 const struct gl_extensions *extensions;
211
212 /** Shaders containing built-in functions that are used for linking. */
213 struct gl_shader *builtins_to_link[16];
214 unsigned num_builtins_to_link;
215 };
216
217 typedef struct YYLTYPE {
218 int first_line;
219 int first_column;
220 int last_line;
221 int last_column;
222 unsigned source;
223 } YYLTYPE;
224 # define YYLTYPE_IS_DECLARED 1
225 # define YYLTYPE_IS_TRIVIAL 1
226
227 # define YYLLOC_DEFAULT(Current, Rhs, N) \
228 do { \
229 if (N) \
230 { \
231 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
232 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
233 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
234 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
235 } \
236 else \
237 { \
238 (Current).first_line = (Current).last_line = \
239 YYRHSLOC(Rhs, 0).last_line; \
240 (Current).first_column = (Current).last_column = \
241 YYRHSLOC(Rhs, 0).last_column; \
242 } \
243 (Current).source = 0; \
244 } while (0)
245
246 extern void _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
247 const char *fmt, ...);
248
249 /**
250 * Emit a warning to the shader log
251 *
252 * \sa _mesa_glsl_error
253 */
254 extern void _mesa_glsl_warning(const YYLTYPE *locp,
255 _mesa_glsl_parse_state *state,
256 const char *fmt, ...);
257
258 extern void _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state,
259 const char *string);
260
261 extern void _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state);
262
263 union YYSTYPE;
264 extern int _mesa_glsl_lex(union YYSTYPE *yylval, YYLTYPE *yylloc,
265 void *scanner);
266
267 extern int _mesa_glsl_parse(struct _mesa_glsl_parse_state *);
268
269 /**
270 * Process elements of the #extension directive
271 *
272 * \return
273 * If \c name and \c behavior are valid, \c true is returned. Otherwise
274 * \c false is returned.
275 */
276 extern bool _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
277 const char *behavior,
278 YYLTYPE *behavior_locp,
279 _mesa_glsl_parse_state *state);
280
281 /**
282 * Get the textual name of the specified shader target
283 */
284 extern const char *
285 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target);
286
287
288 #endif /* __cplusplus */
289
290
291 /*
292 * These definitions apply to C and C++
293 */
294 #ifdef __cplusplus
295 extern "C" {
296 #endif
297
298 extern int preprocess(void *ctx, const char **shader, char **info_log,
299 const struct gl_extensions *extensions, int api);
300
301 extern void _mesa_destroy_shader_compiler(void);
302 extern void _mesa_destroy_shader_compiler_caches(void);
303
304 #ifdef __cplusplus
305 }
306 #endif
307
308
309 #endif /* GLSL_PARSER_EXTRAS_H */