glsl: Refactor variable declaration handling.
[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 #include <cstdlib>
29 #include "glsl_symbol_table.h"
30
31 enum _mesa_glsl_parser_targets {
32 vertex_shader,
33 geometry_shader,
34 fragment_shader,
35 ir_shader
36 };
37
38 struct __GLcontextRec;
39
40 struct _mesa_glsl_parse_state {
41 _mesa_glsl_parse_state(struct __GLcontextRec *ctx, GLenum target,
42 void *mem_ctx);
43
44 /* Callers of this talloc-based new need not call delete. It's
45 * easier to just talloc_free 'ctx' (or any of its ancestors). */
46 static void* operator new(size_t size, void *ctx)
47 {
48 void *mem = talloc_zero_size(ctx, size);
49 assert(mem != NULL);
50
51 return mem;
52 }
53
54 /* If the user *does* call delete, that's OK, we will just
55 * talloc_free in that case. */
56 static void operator delete(void *mem)
57 {
58 talloc_free(mem);
59 }
60
61 void *scanner;
62 exec_list translation_unit;
63 glsl_symbol_table *symbols;
64
65 unsigned language_version;
66 enum _mesa_glsl_parser_targets target;
67
68 /**
69 * Implementation defined limits that affect built-in variables, etc.
70 *
71 * \sa struct gl_constants (in mtypes.h)
72 */
73 struct {
74 /* 1.10 */
75 unsigned MaxLights;
76 unsigned MaxClipPlanes;
77 unsigned MaxTextureUnits;
78 unsigned MaxTextureCoords;
79 unsigned MaxVertexAttribs;
80 unsigned MaxVertexUniformComponents;
81 unsigned MaxVaryingFloats;
82 unsigned MaxVertexTextureImageUnits;
83 unsigned MaxCombinedTextureImageUnits;
84 unsigned MaxTextureImageUnits;
85 unsigned MaxFragmentUniformComponents;
86
87 /* ARB_draw_buffers */
88 unsigned MaxDrawBuffers;
89 } Const;
90
91 /**
92 * During AST to IR conversion, pointer to current IR function
93 *
94 * Will be \c NULL whenever the AST to IR conversion is not inside a
95 * function definition.
96 */
97 class ir_function_signature *current_function;
98
99 /** Have we found a return statement in this function? */
100 bool found_return;
101
102 /** Was there an error during compilation? */
103 bool error;
104
105 /** Loop or switch statement containing the current instructions. */
106 class ir_instruction *loop_or_switch_nesting;
107 class ast_iteration_statement *loop_or_switch_nesting_ast;
108
109 /** List of structures defined in user code. */
110 const glsl_type **user_structures;
111 unsigned num_user_structures;
112
113 char *info_log;
114
115 /**
116 * \name Enable bits for GLSL extensions
117 */
118 /*@{*/
119 unsigned ARB_draw_buffers_enable:1;
120 unsigned ARB_draw_buffers_warn:1;
121 unsigned ARB_fragment_coord_conventions_enable:1;
122 unsigned ARB_fragment_coord_conventions_warn:1;
123 unsigned ARB_texture_rectangle_enable:1;
124 unsigned ARB_texture_rectangle_warn:1;
125 unsigned EXT_texture_array_enable:1;
126 unsigned EXT_texture_array_warn:1;
127 /*@}*/
128
129 /** Extensions supported by the OpenGL implementation. */
130 const struct gl_extensions *extensions;
131
132 /** Shaders containing built-in functions that are used for linking. */
133 struct gl_shader *builtins_to_link[16];
134 unsigned num_builtins_to_link;
135 };
136
137 typedef struct YYLTYPE {
138 int first_line;
139 int first_column;
140 int last_line;
141 int last_column;
142 unsigned source;
143 } YYLTYPE;
144 # define YYLTYPE_IS_DECLARED 1
145 # define YYLTYPE_IS_TRIVIAL 1
146
147 # define YYLLOC_DEFAULT(Current, Rhs, N) \
148 do { \
149 if (N) \
150 { \
151 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
152 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
153 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
154 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
155 } \
156 else \
157 { \
158 (Current).first_line = (Current).last_line = \
159 YYRHSLOC(Rhs, 0).last_line; \
160 (Current).first_column = (Current).last_column = \
161 YYRHSLOC(Rhs, 0).last_column; \
162 } \
163 (Current).source = 0; \
164 } while (0)
165
166 extern void _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
167 const char *fmt, ...);
168
169 /**
170 * Emit a warning to the shader log
171 *
172 * \sa _mesa_glsl_error
173 */
174 extern void _mesa_glsl_warning(const YYLTYPE *locp,
175 _mesa_glsl_parse_state *state,
176 const char *fmt, ...);
177
178 extern "C" {
179 extern int preprocess(void *ctx, const char **shader, char **info_log,
180 const struct gl_extensions *extensions);
181
182 extern void _mesa_destroy_shader_compiler();
183 extern void _mesa_destroy_shader_compiler_caches();
184 }
185
186 extern void _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state,
187 const char *string);
188
189 extern void _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state);
190
191 union YYSTYPE;
192 extern int _mesa_glsl_lex(union YYSTYPE *yylval, YYLTYPE *yylloc,
193 void *scanner);
194
195 extern int _mesa_glsl_parse(struct _mesa_glsl_parse_state *);
196
197 /**
198 * Process elements of the #extension directive
199 *
200 * \return
201 * If \c name and \c behavior are valid, \c true is returned. Otherwise
202 * \c false is returned.
203 */
204 extern bool _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
205 const char *behavior,
206 YYLTYPE *behavior_locp,
207 _mesa_glsl_parse_state *state);
208
209 /**
210 * Get the textual name of the specified shader target
211 */
212 extern const char *
213 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target);
214
215 #endif /* GLSL_PARSER_EXTRAS_H */