glsl: Refactor variable declaration handling.
[mesa.git] / src / glsl / ast.h
1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2009 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #pragma once
26 #ifndef AST_H
27 #define AST_H
28
29 #include "list.h"
30 #include "glsl_parser_extras.h"
31
32 struct _mesa_glsl_parse_state;
33
34 struct YYLTYPE;
35
36 class ast_node {
37 public:
38 /* Callers of this talloc-based new need not call delete. It's
39 * easier to just talloc_free 'ctx' (or any of its ancestors). */
40 static void* operator new(size_t size, void *ctx)
41 {
42 void *node;
43
44 node = talloc_zero_size(ctx, size);
45 assert(node != NULL);
46
47 return node;
48 }
49
50 /* If the user *does* call delete, that's OK, we will just
51 * talloc_free in that case. */
52 static void operator delete(void *table)
53 {
54 talloc_free(table);
55 }
56
57 virtual void print(void) const;
58 virtual ir_rvalue *hir(exec_list *instructions,
59 struct _mesa_glsl_parse_state *state);
60
61 /**
62 * Retrieve the source location of an AST node
63 *
64 * This function is primarily used to get the source position of an AST node
65 * into a form that can be passed to \c _mesa_glsl_error.
66 *
67 * \sa _mesa_glsl_error, ast_node::set_location
68 */
69 struct YYLTYPE get_location(void) const
70 {
71 struct YYLTYPE locp;
72
73 locp.source = this->location.source;
74 locp.first_line = this->location.line;
75 locp.first_column = this->location.column;
76 locp.last_line = locp.first_line;
77 locp.last_column = locp.first_column;
78
79 return locp;
80 }
81
82 /**
83 * Set the source location of an AST node from a parser location
84 *
85 * \sa ast_node::get_location
86 */
87 void set_location(const struct YYLTYPE &locp)
88 {
89 this->location.source = locp.source;
90 this->location.line = locp.first_line;
91 this->location.column = locp.first_column;
92 }
93
94 struct {
95 unsigned source;
96 unsigned line;
97 unsigned column;
98 } location;
99
100 exec_node link;
101
102 protected:
103 ast_node(void);
104 };
105
106
107 enum ast_operators {
108 ast_assign,
109 ast_plus, /**< Unary + operator. */
110 ast_neg,
111 ast_add,
112 ast_sub,
113 ast_mul,
114 ast_div,
115 ast_mod,
116 ast_lshift,
117 ast_rshift,
118 ast_less,
119 ast_greater,
120 ast_lequal,
121 ast_gequal,
122 ast_equal,
123 ast_nequal,
124 ast_bit_and,
125 ast_bit_xor,
126 ast_bit_or,
127 ast_bit_not,
128 ast_logic_and,
129 ast_logic_xor,
130 ast_logic_or,
131 ast_logic_not,
132
133 ast_mul_assign,
134 ast_div_assign,
135 ast_mod_assign,
136 ast_add_assign,
137 ast_sub_assign,
138 ast_ls_assign,
139 ast_rs_assign,
140 ast_and_assign,
141 ast_xor_assign,
142 ast_or_assign,
143
144 ast_conditional,
145
146 ast_pre_inc,
147 ast_pre_dec,
148 ast_post_inc,
149 ast_post_dec,
150 ast_field_selection,
151 ast_array_index,
152
153 ast_function_call,
154
155 ast_identifier,
156 ast_int_constant,
157 ast_uint_constant,
158 ast_float_constant,
159 ast_bool_constant,
160
161 ast_sequence
162 };
163
164 class ast_expression : public ast_node {
165 public:
166 ast_expression(int oper, ast_expression *,
167 ast_expression *, ast_expression *);
168
169 ast_expression(const char *identifier) :
170 oper(ast_identifier)
171 {
172 subexpressions[0] = NULL;
173 subexpressions[1] = NULL;
174 subexpressions[2] = NULL;
175 primary_expression.identifier = (char *) identifier;
176 }
177
178 static const char *operator_string(enum ast_operators op);
179
180 virtual ir_rvalue *hir(exec_list *instructions,
181 struct _mesa_glsl_parse_state *state);
182
183 virtual void print(void) const;
184
185 enum ast_operators oper;
186
187 ast_expression *subexpressions[3];
188
189 union {
190 char *identifier;
191 int int_constant;
192 float float_constant;
193 unsigned uint_constant;
194 int bool_constant;
195 } primary_expression;
196
197
198 /**
199 * List of expressions for an \c ast_sequence or parameters for an
200 * \c ast_function_call
201 */
202 exec_list expressions;
203 };
204
205 class ast_expression_bin : public ast_expression {
206 public:
207 ast_expression_bin(int oper, ast_expression *, ast_expression *);
208
209 virtual void print(void) const;
210 };
211
212 /**
213 * Subclass of expressions for function calls
214 */
215 class ast_function_expression : public ast_expression {
216 public:
217 ast_function_expression(ast_expression *callee)
218 : ast_expression(ast_function_call, callee,
219 NULL, NULL),
220 cons(false)
221 {
222 /* empty */
223 }
224
225 ast_function_expression(class ast_type_specifier *type)
226 : ast_expression(ast_function_call, (ast_expression *) type,
227 NULL, NULL),
228 cons(true)
229 {
230 /* empty */
231 }
232
233 bool is_constructor() const
234 {
235 return cons;
236 }
237
238 virtual ir_rvalue *hir(exec_list *instructions,
239 struct _mesa_glsl_parse_state *state);
240
241 private:
242 /**
243 * Is this function call actually a constructor?
244 */
245 bool cons;
246 };
247
248
249 /**
250 * Number of possible operators for an ast_expression
251 *
252 * This is done as a define instead of as an additional value in the enum so
253 * that the compiler won't generate spurious messages like "warning:
254 * enumeration value ‘ast_num_operators’ not handled in switch"
255 */
256 #define AST_NUM_OPERATORS (ast_sequence + 1)
257
258
259 class ast_compound_statement : public ast_node {
260 public:
261 ast_compound_statement(int new_scope, ast_node *statements);
262 virtual void print(void) const;
263
264 virtual ir_rvalue *hir(exec_list *instructions,
265 struct _mesa_glsl_parse_state *state);
266
267 int new_scope;
268 exec_list statements;
269 };
270
271 class ast_declaration : public ast_node {
272 public:
273 ast_declaration(char *identifier, int is_array, ast_expression *array_size,
274 ast_expression *initializer);
275 virtual void print(void) const;
276
277 char *identifier;
278
279 int is_array;
280 ast_expression *array_size;
281
282 ast_expression *initializer;
283 };
284
285
286 enum {
287 ast_precision_high = 0, /**< Default precision. */
288 ast_precision_medium,
289 ast_precision_low
290 };
291
292 struct ast_type_qualifier {
293 unsigned invariant:1;
294 unsigned constant:1;
295 unsigned attribute:1;
296 unsigned varying:1;
297 unsigned in:1;
298 unsigned out:1;
299 unsigned centroid:1;
300 unsigned uniform:1;
301 unsigned smooth:1;
302 unsigned flat:1;
303 unsigned noperspective:1;
304
305 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
306 /*@{*/
307 unsigned origin_upper_left:1;
308 unsigned pixel_center_integer:1;
309 /*@}*/
310 };
311
312 class ast_struct_specifier : public ast_node {
313 public:
314 ast_struct_specifier(char *identifier, ast_node *declarator_list);
315 virtual void print(void) const;
316
317 virtual ir_rvalue *hir(exec_list *instructions,
318 struct _mesa_glsl_parse_state *state);
319
320 char *name;
321 exec_list declarations;
322 };
323
324
325 enum ast_types {
326 ast_void,
327 ast_float,
328 ast_int,
329 ast_uint,
330 ast_bool,
331 ast_vec2,
332 ast_vec3,
333 ast_vec4,
334 ast_bvec2,
335 ast_bvec3,
336 ast_bvec4,
337 ast_ivec2,
338 ast_ivec3,
339 ast_ivec4,
340 ast_uvec2,
341 ast_uvec3,
342 ast_uvec4,
343 ast_mat2,
344 ast_mat2x3,
345 ast_mat2x4,
346 ast_mat3x2,
347 ast_mat3,
348 ast_mat3x4,
349 ast_mat4x2,
350 ast_mat4x3,
351 ast_mat4,
352 ast_sampler1d,
353 ast_sampler2d,
354 ast_sampler2drect,
355 ast_sampler3d,
356 ast_samplercube,
357 ast_sampler1dshadow,
358 ast_sampler2dshadow,
359 ast_sampler2drectshadow,
360 ast_samplercubeshadow,
361 ast_sampler1darray,
362 ast_sampler2darray,
363 ast_sampler1darrayshadow,
364 ast_sampler2darrayshadow,
365 ast_isampler1d,
366 ast_isampler2d,
367 ast_isampler3d,
368 ast_isamplercube,
369 ast_isampler1darray,
370 ast_isampler2darray,
371 ast_usampler1d,
372 ast_usampler2d,
373 ast_usampler3d,
374 ast_usamplercube,
375 ast_usampler1darray,
376 ast_usampler2darray,
377
378 ast_struct,
379 ast_type_name
380 };
381
382
383 class ast_type_specifier : public ast_node {
384 public:
385 ast_type_specifier(int specifier);
386
387 /** Construct a type specifier from a type name */
388 ast_type_specifier(const char *name)
389 : type_specifier(ast_type_name), type_name(name), structure(NULL),
390 is_array(false), array_size(NULL), precision(ast_precision_high)
391 {
392 /* empty */
393 }
394
395 /** Construct a type specifier from a structure definition */
396 ast_type_specifier(ast_struct_specifier *s)
397 : type_specifier(ast_struct), type_name(s->name), structure(s),
398 is_array(false), array_size(NULL), precision(ast_precision_high)
399 {
400 /* empty */
401 }
402
403 const struct glsl_type *glsl_type(const char **name,
404 struct _mesa_glsl_parse_state *state)
405 const;
406
407 virtual void print(void) const;
408
409 ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
410
411 enum ast_types type_specifier;
412
413 const char *type_name;
414 ast_struct_specifier *structure;
415
416 int is_array;
417 ast_expression *array_size;
418
419 unsigned precision:2;
420 };
421
422
423 class ast_fully_specified_type : public ast_node {
424 public:
425 virtual void print(void) const;
426 bool has_qualifiers() const;
427
428 ast_type_qualifier qualifier;
429 ast_type_specifier *specifier;
430 };
431
432
433 class ast_declarator_list : public ast_node {
434 public:
435 ast_declarator_list(ast_fully_specified_type *);
436 virtual void print(void) const;
437
438 virtual ir_rvalue *hir(exec_list *instructions,
439 struct _mesa_glsl_parse_state *state);
440
441 ast_fully_specified_type *type;
442 exec_list declarations;
443
444 /**
445 * Special flag for vertex shader "invariant" declarations.
446 *
447 * Vertex shaders can contain "invariant" variable redeclarations that do
448 * not include a type. For example, "invariant gl_Position;". This flag
449 * is used to note these cases when no type is specified.
450 */
451 int invariant;
452 };
453
454
455 class ast_parameter_declarator : public ast_node {
456 public:
457 ast_parameter_declarator()
458 {
459 this->identifier = NULL;
460 this->is_array = false;
461 this->array_size = 0;
462 }
463
464 virtual void print(void) const;
465
466 virtual ir_rvalue *hir(exec_list *instructions,
467 struct _mesa_glsl_parse_state *state);
468
469 ast_fully_specified_type *type;
470 char *identifier;
471 int is_array;
472 ast_expression *array_size;
473
474 static void parameters_to_hir(exec_list *ast_parameters,
475 bool formal, exec_list *ir_parameters,
476 struct _mesa_glsl_parse_state *state);
477
478 private:
479 /** Is this parameter declaration part of a formal parameter list? */
480 bool formal_parameter;
481
482 /**
483 * Is this parameter 'void' type?
484 *
485 * This field is set by \c ::hir.
486 */
487 bool is_void;
488 };
489
490
491 class ast_function : public ast_node {
492 public:
493 ast_function(void);
494
495 virtual void print(void) const;
496
497 virtual ir_rvalue *hir(exec_list *instructions,
498 struct _mesa_glsl_parse_state *state);
499
500 ast_fully_specified_type *return_type;
501 char *identifier;
502
503 exec_list parameters;
504
505 private:
506 /**
507 * Is this prototype part of the function definition?
508 *
509 * Used by ast_function_definition::hir to process the parameters, etc.
510 * of the function.
511 *
512 * \sa ::hir
513 */
514 bool is_definition;
515
516 /**
517 * Function signature corresponding to this function prototype instance
518 *
519 * Used by ast_function_definition::hir to process the parameters, etc.
520 * of the function.
521 *
522 * \sa ::hir
523 */
524 class ir_function_signature *signature;
525
526 friend class ast_function_definition;
527 };
528
529
530 class ast_declaration_statement : public ast_node {
531 public:
532 ast_declaration_statement(void);
533
534 enum {
535 ast_function,
536 ast_declaration,
537 ast_precision
538 } mode;
539
540 union {
541 class ast_function *function;
542 ast_declarator_list *declarator;
543 ast_type_specifier *type;
544 ast_node *node;
545 } declaration;
546 };
547
548
549 class ast_expression_statement : public ast_node {
550 public:
551 ast_expression_statement(ast_expression *);
552 virtual void print(void) const;
553
554 virtual ir_rvalue *hir(exec_list *instructions,
555 struct _mesa_glsl_parse_state *state);
556
557 ast_expression *expression;
558 };
559
560
561 class ast_case_label : public ast_node {
562 public:
563
564 /**
565 * An expression of NULL means 'default'.
566 */
567 ast_expression *expression;
568 };
569
570 class ast_selection_statement : public ast_node {
571 public:
572 ast_selection_statement(ast_expression *condition,
573 ast_node *then_statement,
574 ast_node *else_statement);
575 virtual void print(void) const;
576
577 virtual ir_rvalue *hir(exec_list *instructions,
578 struct _mesa_glsl_parse_state *state);
579
580 ast_expression *condition;
581 ast_node *then_statement;
582 ast_node *else_statement;
583 };
584
585
586 class ast_switch_statement : public ast_node {
587 public:
588 ast_expression *expression;
589 exec_list statements;
590 };
591
592 class ast_iteration_statement : public ast_node {
593 public:
594 ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
595 ast_expression *rest_expression, ast_node *body);
596
597 virtual void print(void) const;
598
599 virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
600
601 enum ast_iteration_modes {
602 ast_for,
603 ast_while,
604 ast_do_while
605 } mode;
606
607
608 ast_node *init_statement;
609 ast_node *condition;
610 ast_expression *rest_expression;
611
612 ast_node *body;
613
614 private:
615 /**
616 * Generate IR from the condition of a loop
617 *
618 * This is factored out of ::hir because some loops have the condition
619 * test at the top (for and while), and others have it at the end (do-while).
620 */
621 void condition_to_hir(class ir_loop *, struct _mesa_glsl_parse_state *);
622 };
623
624
625 class ast_jump_statement : public ast_node {
626 public:
627 ast_jump_statement(int mode, ast_expression *return_value);
628 virtual void print(void) const;
629
630 virtual ir_rvalue *hir(exec_list *instructions,
631 struct _mesa_glsl_parse_state *state);
632
633 enum ast_jump_modes {
634 ast_continue,
635 ast_break,
636 ast_return,
637 ast_discard
638 } mode;
639
640 ast_expression *opt_return_value;
641 };
642
643
644 class ast_function_definition : public ast_node {
645 public:
646 virtual void print(void) const;
647
648 virtual ir_rvalue *hir(exec_list *instructions,
649 struct _mesa_glsl_parse_state *state);
650
651 ast_function *prototype;
652 ast_compound_statement *body;
653 };
654
655
656 extern void
657 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
658
659 extern ir_rvalue *
660 _mesa_ast_field_selection_to_hir(const ast_expression *expr,
661 exec_list *instructions,
662 struct _mesa_glsl_parse_state *state);
663
664 #endif /* AST_H */