IR print visitor: Add some support for printing types and constants
[mesa.git] / ast.h
1 /*
2 * Copyright © 2009 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 AST_H
26 #define AST_H
27
28 #include "main/simple_list.h"
29 #include "list.h"
30 #include "glsl_parser_extras.h"
31
32 struct ir_instruction;
33 struct _mesa_glsl_parse_state;
34
35 struct YYLTYPE;
36
37 class ast_node : public simple_node {
38 public:
39 virtual ~ast_node();
40 virtual void print(void) const;
41 virtual ir_instruction *hir(exec_list *instructions,
42 struct _mesa_glsl_parse_state *state);
43
44 /**
45 * Retrieve the source location of an AST node
46 *
47 * This function is primarily used to get the source position of an AST node
48 * into a form that can be passed to \c _mesa_glsl_error.
49 *
50 * \sa _mesa_glsl_error, ast_node::set_location
51 */
52 struct YYLTYPE get_location(void) const
53 {
54 struct YYLTYPE locp;
55
56 locp.source = this->location.source;
57 locp.first_line = this->location.line;
58 locp.first_column = this->location.column;
59 locp.last_line = locp.first_line;
60 locp.last_column = locp.first_column;
61
62 return locp;
63 }
64
65 /**
66 * Set the source location of an AST node from a parser location
67 *
68 * \sa ast_node::get_location
69 */
70 void set_location(const struct YYLTYPE *locp)
71 {
72 this->location.source = locp->source;
73 this->location.line = locp->first_line;
74 this->location.column = locp->first_column;
75 }
76
77
78 int type;
79
80 struct {
81 unsigned source;
82 unsigned line;
83 unsigned column;
84 } location;
85
86 protected:
87 ast_node(void);
88 };
89
90
91 enum ast_operators {
92 ast_assign,
93 ast_plus, /**< Unary + operator. */
94 ast_neg,
95 ast_add,
96 ast_sub,
97 ast_mul,
98 ast_div,
99 ast_mod,
100 ast_lshift,
101 ast_rshift,
102 ast_less,
103 ast_greater,
104 ast_lequal,
105 ast_gequal,
106 ast_equal,
107 ast_nequal,
108 ast_bit_and,
109 ast_bit_xor,
110 ast_bit_or,
111 ast_bit_not,
112 ast_logic_and,
113 ast_logic_xor,
114 ast_logic_or,
115 ast_logic_not,
116
117 ast_mul_assign,
118 ast_div_assign,
119 ast_mod_assign,
120 ast_add_assign,
121 ast_sub_assign,
122 ast_ls_assign,
123 ast_rs_assign,
124 ast_and_assign,
125 ast_xor_assign,
126 ast_or_assign,
127
128 ast_conditional,
129
130 ast_pre_inc,
131 ast_pre_dec,
132 ast_post_inc,
133 ast_post_dec,
134 ast_field_selection,
135 ast_array_index,
136
137 ast_function_call,
138
139 ast_identifier,
140 ast_int_constant,
141 ast_uint_constant,
142 ast_float_constant,
143 ast_bool_constant,
144
145 ast_sequence
146 };
147
148 class ast_expression : public ast_node {
149 public:
150 ast_expression(int oper, ast_expression *,
151 ast_expression *, ast_expression *);
152
153 static const char *operator_string(enum ast_operators op);
154
155 virtual ir_instruction *hir(exec_list *instructions,
156 struct _mesa_glsl_parse_state *state);
157
158 virtual void print(void) const;
159
160 enum ast_operators oper;
161
162 ast_expression *subexpressions[3];
163
164 union {
165 char *identifier;
166 int int_constant;
167 float float_constant;
168 unsigned uint_constant;
169 int bool_constant;
170 } primary_expression;
171
172
173 /**
174 * List of expressions for an \c ast_sequence.
175 */
176 struct simple_node expressions;
177 };
178
179 class ast_expression_bin : public ast_expression {
180 public:
181 ast_expression_bin(int oper, ast_expression *, ast_expression *);
182
183 virtual void print(void) const;
184 };
185
186
187 /**
188 * Number of possible operators for an ast_expression
189 *
190 * This is done as a define instead of as an additional value in the enum so
191 * that the compiler won't generate spurious messages like "warning:
192 * enumeration value ‘ast_num_operators’ not handled in switch"
193 */
194 #define AST_NUM_OPERATORS (ast_sequence + 1)
195
196
197 class ast_compound_statement : public ast_node {
198 public:
199 ast_compound_statement(int new_scope, ast_node *statements);
200 virtual void print(void) const;
201
202 virtual ir_instruction *hir(exec_list *instructions,
203 struct _mesa_glsl_parse_state *state);
204
205 int new_scope;
206 struct simple_node statements;
207 };
208
209 class ast_declaration : public ast_node {
210 public:
211 ast_declaration(char *identifier, int is_array, ast_expression *array_size,
212 ast_expression *initializer);
213 virtual void print(void) const;
214
215 char *identifier;
216
217 int is_array;
218 ast_expression *array_size;
219
220 ast_expression *initializer;
221 };
222
223
224 enum {
225 ast_precision_high = 0, /**< Default precision. */
226 ast_precision_medium,
227 ast_precision_low
228 };
229
230 struct ast_type_qualifier {
231 unsigned invariant:1;
232 unsigned constant:1;
233 unsigned attribute:1;
234 unsigned varying:1;
235 unsigned in:1;
236 unsigned out:1;
237 unsigned centroid:1;
238 unsigned uniform:1;
239 unsigned smooth:1;
240 unsigned flat:1;
241 unsigned noperspective:1;
242 };
243
244 class ast_struct_specifier : public ast_node {
245 public:
246 ast_struct_specifier(char *identifier, ast_node *declarator_list);
247 virtual void print(void) const;
248
249 char *name;
250 struct simple_node declarations;
251 };
252
253
254 enum ast_types {
255 ast_void,
256 ast_float,
257 ast_int,
258 ast_uint,
259 ast_bool,
260 ast_vec2,
261 ast_vec3,
262 ast_vec4,
263 ast_bvec2,
264 ast_bvec3,
265 ast_bvec4,
266 ast_ivec2,
267 ast_ivec3,
268 ast_ivec4,
269 ast_uvec2,
270 ast_uvec3,
271 ast_uvec4,
272 ast_mat2,
273 ast_mat2x3,
274 ast_mat2x4,
275 ast_mat3x2,
276 ast_mat3,
277 ast_mat3x4,
278 ast_mat4x2,
279 ast_mat4x3,
280 ast_mat4,
281 ast_sampler1d,
282 ast_sampler2d,
283 ast_sampler3d,
284 ast_samplercube,
285 ast_sampler1dshadow,
286 ast_sampler2dshadow,
287 ast_samplercubeshadow,
288 ast_sampler1darray,
289 ast_sampler2darray,
290 ast_sampler1darrayshadow,
291 ast_sampler2darrayshadow,
292 ast_isampler1d,
293 ast_isampler2d,
294 ast_isampler3d,
295 ast_isamplercube,
296 ast_isampler1darray,
297 ast_isampler2darray,
298 ast_usampler1d,
299 ast_usampler2d,
300 ast_usampler3d,
301 ast_usamplercube,
302 ast_usampler1darray,
303 ast_usampler2darray,
304
305 ast_struct,
306 ast_type_name
307 };
308
309
310 class ast_type_specifier : public ast_node {
311 public:
312 ast_type_specifier(int specifier);
313
314 virtual void print(void) const;
315
316 enum ast_types type_specifier;
317
318 char *type_name;
319 ast_struct_specifier *structure;
320
321 int is_array;
322 ast_expression *array_size;
323
324 unsigned precision:2;
325 };
326
327
328 class ast_fully_specified_type : public ast_node {
329 public:
330 virtual void print(void) const;
331
332 ast_type_qualifier qualifier;
333 ast_type_specifier *specifier;
334 };
335
336
337 class ast_declarator_list : public ast_node {
338 public:
339 ast_declarator_list(ast_fully_specified_type *);
340 virtual void print(void) const;
341
342 virtual ir_instruction *hir(exec_list *instructions,
343 struct _mesa_glsl_parse_state *state);
344
345 ast_fully_specified_type *type;
346 struct simple_node declarations;
347
348 /**
349 * Special flag for vertex shader "invariant" declarations.
350 *
351 * Vertex shaders can contain "invariant" variable redeclarations that do
352 * not include a type. For example, "invariant gl_Position;". This flag
353 * is used to note these cases when no type is specified.
354 */
355 int invariant;
356 };
357
358
359 class ast_parameter_declarator : public ast_node {
360 public:
361 virtual void print(void) const;
362
363 virtual ir_instruction *hir(exec_list *instructions,
364 struct _mesa_glsl_parse_state *state);
365
366 ast_fully_specified_type *type;
367 char *identifier;
368 int is_array;
369 ast_expression *array_size;
370 };
371
372
373 class ast_function : public ast_node {
374 public:
375 ast_function(void);
376
377 virtual void print(void) const;
378
379 ast_fully_specified_type *return_type;
380 char *identifier;
381
382 struct simple_node parameters;
383 };
384
385
386 class ast_declaration_statement : public ast_node {
387 public:
388 ast_declaration_statement(void);
389
390 enum {
391 ast_function,
392 ast_declaration,
393 ast_precision
394 } mode;
395
396 union {
397 class ast_function *function;
398 ast_declarator_list *declarator;
399 ast_type_specifier *type;
400 ast_node *node;
401 } declaration;
402 };
403
404
405 class ast_expression_statement : public ast_node {
406 public:
407 ast_expression_statement(ast_expression *);
408 virtual void print(void) const;
409
410 virtual ir_instruction *hir(exec_list *instructions,
411 struct _mesa_glsl_parse_state *state);
412
413 ast_expression *expression;
414 };
415
416
417 class ast_case_label : public ast_node {
418 public:
419
420 /**
421 * An expression of NULL means 'default'.
422 */
423 ast_expression *expression;
424 };
425
426 class ast_selection_statement : public ast_node {
427 public:
428 ast_selection_statement(ast_expression *condition,
429 ast_node *then_statement,
430 ast_node *else_statement);
431 virtual void print(void) const;
432
433 ast_expression *condition;
434 ast_node *then_statement;
435 ast_node *else_statement;
436 };
437
438
439 class ast_switch_statement : public ast_node {
440 public:
441 ast_expression *expression;
442 struct simple_node statements;
443 };
444
445 class ast_iteration_statement : public ast_node {
446 public:
447 ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
448 ast_expression *rest_expression, ast_node *body);
449
450 virtual void print(void) const;
451
452 enum ast_iteration_modes {
453 ast_for,
454 ast_while,
455 ast_do_while
456 } mode;
457
458
459 ast_node *init_statement;
460 ast_node *condition;
461 ast_expression *rest_expression;
462
463 ast_node *body;
464 };
465
466
467 class ast_jump_statement : public ast_node {
468 public:
469 ast_jump_statement(int mode, ast_expression *return_value);
470 virtual void print(void) const;
471
472 enum ast_jump_modes {
473 ast_continue,
474 ast_break,
475 ast_return,
476 ast_discard
477 } mode;
478
479 ast_expression *opt_return_value;
480 };
481
482
483 class ast_function_definition : public ast_node {
484 public:
485 virtual void print(void) const;
486
487 virtual ir_instruction *hir(exec_list *instructions,
488 struct _mesa_glsl_parse_state *state);
489
490 ast_function *prototype;
491 ast_compound_statement *body;
492 };
493
494
495 extern struct ir_instruction *
496 _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
497 exec_list *instructions,
498 struct _mesa_glsl_parse_state *state);
499
500 #endif /* AST_H */