87f987682cd304d5c55a44fc8efe7d889cea3e7e
[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 /**
37 * \defgroup AST Abstract syntax tree node definitions
38 *
39 * An abstract syntax tree is generated by the parser. This is a fairly
40 * direct representation of the gramma derivation for the source program.
41 * No symantic checking is done during the generation of the AST. Only
42 * syntactic checking is done. Symantic checking is performed by a later
43 * stage that converts the AST to a more generic intermediate representation.
44 *
45 *@{
46 */
47 /**
48 * Base class of all abstract syntax tree nodes
49 */
50 class ast_node {
51 public:
52 /* Callers of this ralloc-based new need not call delete. It's
53 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
54 static void* operator new(size_t size, void *ctx)
55 {
56 void *node;
57
58 node = rzalloc_size(ctx, size);
59 assert(node != NULL);
60
61 return node;
62 }
63
64 /* If the user *does* call delete, that's OK, we will just
65 * ralloc_free in that case. */
66 static void operator delete(void *table)
67 {
68 ralloc_free(table);
69 }
70
71 /**
72 * Print an AST node in something approximating the original GLSL code
73 */
74 virtual void print(void) const;
75
76 /**
77 * Convert the AST node to the high-level intermediate representation
78 */
79 virtual ir_rvalue *hir(exec_list *instructions,
80 struct _mesa_glsl_parse_state *state);
81
82 /**
83 * Retrieve the source location of an AST node
84 *
85 * This function is primarily used to get the source position of an AST node
86 * into a form that can be passed to \c _mesa_glsl_error.
87 *
88 * \sa _mesa_glsl_error, ast_node::set_location
89 */
90 struct YYLTYPE get_location(void) const
91 {
92 struct YYLTYPE locp;
93
94 locp.source = this->location.source;
95 locp.first_line = this->location.line;
96 locp.first_column = this->location.column;
97 locp.last_line = locp.first_line;
98 locp.last_column = locp.first_column;
99
100 return locp;
101 }
102
103 /**
104 * Set the source location of an AST node from a parser location
105 *
106 * \sa ast_node::get_location
107 */
108 void set_location(const struct YYLTYPE &locp)
109 {
110 this->location.source = locp.source;
111 this->location.line = locp.first_line;
112 this->location.column = locp.first_column;
113 }
114
115 /**
116 * Source location of the AST node.
117 */
118 struct {
119 unsigned source; /**< GLSL source number. */
120 unsigned line; /**< Line number within the source string. */
121 unsigned column; /**< Column in the line. */
122 } location;
123
124 exec_node link;
125
126 protected:
127 /**
128 * The only constructor is protected so that only derived class objects can
129 * be created.
130 */
131 ast_node(void);
132 };
133
134
135 /**
136 * Operators for AST expression nodes.
137 */
138 enum ast_operators {
139 ast_assign,
140 ast_plus, /**< Unary + operator. */
141 ast_neg,
142 ast_add,
143 ast_sub,
144 ast_mul,
145 ast_div,
146 ast_mod,
147 ast_lshift,
148 ast_rshift,
149 ast_less,
150 ast_greater,
151 ast_lequal,
152 ast_gequal,
153 ast_equal,
154 ast_nequal,
155 ast_bit_and,
156 ast_bit_xor,
157 ast_bit_or,
158 ast_bit_not,
159 ast_logic_and,
160 ast_logic_xor,
161 ast_logic_or,
162 ast_logic_not,
163
164 ast_mul_assign,
165 ast_div_assign,
166 ast_mod_assign,
167 ast_add_assign,
168 ast_sub_assign,
169 ast_ls_assign,
170 ast_rs_assign,
171 ast_and_assign,
172 ast_xor_assign,
173 ast_or_assign,
174
175 ast_conditional,
176
177 ast_pre_inc,
178 ast_pre_dec,
179 ast_post_inc,
180 ast_post_dec,
181 ast_field_selection,
182 ast_array_index,
183
184 ast_function_call,
185
186 ast_identifier,
187 ast_int_constant,
188 ast_uint_constant,
189 ast_float_constant,
190 ast_bool_constant,
191
192 ast_sequence
193 };
194
195 /**
196 * Representation of any sort of expression.
197 */
198 class ast_expression : public ast_node {
199 public:
200 ast_expression(int oper, ast_expression *,
201 ast_expression *, ast_expression *);
202
203 ast_expression(const char *identifier) :
204 oper(ast_identifier)
205 {
206 subexpressions[0] = NULL;
207 subexpressions[1] = NULL;
208 subexpressions[2] = NULL;
209 primary_expression.identifier = identifier;
210 this->non_lvalue_description = NULL;
211 }
212
213 static const char *operator_string(enum ast_operators op);
214
215 virtual ir_rvalue *hir(exec_list *instructions,
216 struct _mesa_glsl_parse_state *state);
217
218 virtual void print(void) const;
219
220 enum ast_operators oper;
221
222 ast_expression *subexpressions[3];
223
224 union {
225 const char *identifier;
226 int int_constant;
227 float float_constant;
228 unsigned uint_constant;
229 int bool_constant;
230 } primary_expression;
231
232
233 /**
234 * List of expressions for an \c ast_sequence or parameters for an
235 * \c ast_function_call
236 */
237 exec_list expressions;
238
239 /**
240 * For things that can't be l-values, this describes what it is.
241 *
242 * This text is used by the code that generates IR for assignments to
243 * detect and emit useful messages for assignments to some things that
244 * can't be l-values. For example, pre- or post-incerement expressions.
245 *
246 * \note
247 * This pointer may be \c NULL.
248 */
249 const char *non_lvalue_description;
250 };
251
252 class ast_expression_bin : public ast_expression {
253 public:
254 ast_expression_bin(int oper, ast_expression *, ast_expression *);
255
256 virtual void print(void) const;
257 };
258
259 /**
260 * Subclass of expressions for function calls
261 */
262 class ast_function_expression : public ast_expression {
263 public:
264 ast_function_expression(ast_expression *callee)
265 : ast_expression(ast_function_call, callee,
266 NULL, NULL),
267 cons(false)
268 {
269 /* empty */
270 }
271
272 ast_function_expression(class ast_type_specifier *type)
273 : ast_expression(ast_function_call, (ast_expression *) type,
274 NULL, NULL),
275 cons(true)
276 {
277 /* empty */
278 }
279
280 bool is_constructor() const
281 {
282 return cons;
283 }
284
285 virtual ir_rvalue *hir(exec_list *instructions,
286 struct _mesa_glsl_parse_state *state);
287
288 private:
289 /**
290 * Is this function call actually a constructor?
291 */
292 bool cons;
293 };
294
295
296 /**
297 * Number of possible operators for an ast_expression
298 *
299 * This is done as a define instead of as an additional value in the enum so
300 * that the compiler won't generate spurious messages like "warning:
301 * enumeration value ‘ast_num_operators’ not handled in switch"
302 */
303 #define AST_NUM_OPERATORS (ast_sequence + 1)
304
305
306 class ast_compound_statement : public ast_node {
307 public:
308 ast_compound_statement(int new_scope, ast_node *statements);
309 virtual void print(void) const;
310
311 virtual ir_rvalue *hir(exec_list *instructions,
312 struct _mesa_glsl_parse_state *state);
313
314 int new_scope;
315 exec_list statements;
316 };
317
318 class ast_declaration : public ast_node {
319 public:
320 ast_declaration(const char *identifier, bool is_array, ast_expression *array_size,
321 ast_expression *initializer);
322 virtual void print(void) const;
323
324 const char *identifier;
325
326 bool is_array;
327 ast_expression *array_size;
328
329 ast_expression *initializer;
330 };
331
332
333 enum {
334 ast_precision_none = 0, /**< Absence of precision qualifier. */
335 ast_precision_high,
336 ast_precision_medium,
337 ast_precision_low
338 };
339
340 struct ast_type_qualifier {
341 /* Callers of this ralloc-based new need not call delete. It's
342 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
343 static void* operator new(size_t size, void *ctx)
344 {
345 void *node;
346
347 node = rzalloc_size(ctx, size);
348 assert(node != NULL);
349
350 return node;
351 }
352
353 /* If the user *does* call delete, that's OK, we will just
354 * ralloc_free in that case. */
355 static void operator delete(void *table)
356 {
357 ralloc_free(table);
358 }
359
360 union {
361 struct {
362 unsigned invariant:1;
363 unsigned constant:1;
364 unsigned attribute:1;
365 unsigned varying:1;
366 unsigned in:1;
367 unsigned out:1;
368 unsigned centroid:1;
369 unsigned uniform:1;
370 unsigned smooth:1;
371 unsigned flat:1;
372 unsigned noperspective:1;
373
374 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
375 /*@{*/
376 unsigned origin_upper_left:1;
377 unsigned pixel_center_integer:1;
378 /*@}*/
379
380 /**
381 * Flag set if GL_ARB_explicit_attrib_location "location" layout
382 * qualifier is used.
383 */
384 unsigned explicit_location:1;
385 /**
386 * Flag set if GL_ARB_explicit_attrib_location "index" layout
387 * qualifier is used.
388 */
389 unsigned explicit_index:1;
390
391 /** \name Layout qualifiers for GL_AMD_conservative_depth */
392 /** \{ */
393 unsigned depth_any:1;
394 unsigned depth_greater:1;
395 unsigned depth_less:1;
396 unsigned depth_unchanged:1;
397 /** \} */
398
399 /** \name Layout qualifiers for GL_ARB_uniform_buffer_object */
400 /** \{ */
401 unsigned std140:1;
402 unsigned shared:1;
403 unsigned packed:1;
404 unsigned column_major:1;
405 unsigned row_major:1;
406 /** \} */
407 }
408 /** \brief Set of flags, accessed by name. */
409 q;
410
411 /** \brief Set of flags, accessed as a bitmask. */
412 unsigned i;
413 } flags;
414
415 /**
416 * Location specified via GL_ARB_explicit_attrib_location layout
417 *
418 * \note
419 * This field is only valid if \c explicit_location is set.
420 */
421 int location;
422 /**
423 * Index specified via GL_ARB_explicit_attrib_location layout
424 *
425 * \note
426 * This field is only valid if \c explicit_index is set.
427 */
428 int index;
429
430 /**
431 * Return true if and only if an interpolation qualifier is present.
432 */
433 bool has_interpolation() const;
434
435 /**
436 * \brief Return string representation of interpolation qualifier.
437 *
438 * If an interpolation qualifier is present, then return that qualifier's
439 * string representation. Otherwise, return null. For example, if the
440 * noperspective bit is set, then this returns "noperspective".
441 *
442 * If multiple interpolation qualifiers are somehow present, then the
443 * returned string is undefined but not null.
444 */
445 const char *interpolation_string() const;
446
447 bool merge_qualifier(YYLTYPE *loc,
448 _mesa_glsl_parse_state *state,
449 ast_type_qualifier q);
450 };
451
452 class ast_declarator_list;
453
454 class ast_struct_specifier : public ast_node {
455 public:
456 /**
457 * \brief Make a shallow copy of an ast_struct_specifier.
458 *
459 * Use only if the objects are allocated from the same context and will not
460 * be modified. Zeros the inherited ast_node's fields.
461 */
462 ast_struct_specifier(const ast_struct_specifier& that):
463 ast_node(), name(that.name), declarations(that.declarations)
464 {
465 /* empty */
466 }
467
468 ast_struct_specifier(const char *identifier,
469 ast_declarator_list *declarator_list);
470 virtual void print(void) const;
471
472 virtual ir_rvalue *hir(exec_list *instructions,
473 struct _mesa_glsl_parse_state *state);
474
475 const char *name;
476 /* List of ast_declarator_list * */
477 exec_list declarations;
478 };
479
480
481
482 class ast_type_specifier : public ast_node {
483 public:
484 /**
485 * \brief Make a shallow copy of an ast_type_specifier, specifying array
486 * fields.
487 *
488 * Use only if the objects are allocated from the same context and will not
489 * be modified. Zeros the inherited ast_node's fields.
490 */
491 ast_type_specifier(const ast_type_specifier *that, bool is_array,
492 ast_expression *array_size)
493 : ast_node(), type_name(that->type_name), structure(that->structure),
494 is_array(is_array), array_size(array_size), precision(that->precision),
495 is_precision_statement(that->is_precision_statement)
496 {
497 /* empty */
498 }
499
500 /** Construct a type specifier from a type name */
501 ast_type_specifier(const char *name)
502 : type_name(name), structure(NULL),
503 is_array(false), array_size(NULL), precision(ast_precision_none),
504 is_precision_statement(false)
505 {
506 /* empty */
507 }
508
509 /** Construct a type specifier from a structure definition */
510 ast_type_specifier(ast_struct_specifier *s)
511 : type_name(s->name), structure(s),
512 is_array(false), array_size(NULL), precision(ast_precision_none),
513 is_precision_statement(false)
514 {
515 /* empty */
516 }
517
518 const struct glsl_type *glsl_type(const char **name,
519 struct _mesa_glsl_parse_state *state)
520 const;
521
522 virtual void print(void) const;
523
524 ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
525
526 const char *type_name;
527 ast_struct_specifier *structure;
528
529 bool is_array;
530 ast_expression *array_size;
531
532 unsigned precision:2;
533
534 bool is_precision_statement;
535 };
536
537
538 class ast_fully_specified_type : public ast_node {
539 public:
540 virtual void print(void) const;
541 bool has_qualifiers() const;
542
543 ast_type_qualifier qualifier;
544 ast_type_specifier *specifier;
545 };
546
547
548 class ast_declarator_list : public ast_node {
549 public:
550 ast_declarator_list(ast_fully_specified_type *);
551 virtual void print(void) const;
552
553 virtual ir_rvalue *hir(exec_list *instructions,
554 struct _mesa_glsl_parse_state *state);
555
556 ast_fully_specified_type *type;
557 /** List of 'ast_declaration *' */
558 exec_list declarations;
559
560 /**
561 * Special flag for vertex shader "invariant" declarations.
562 *
563 * Vertex shaders can contain "invariant" variable redeclarations that do
564 * not include a type. For example, "invariant gl_Position;". This flag
565 * is used to note these cases when no type is specified.
566 */
567 int invariant;
568
569 /**
570 * Flag indicating that these declarators are in a uniform block,
571 * allowing UBO type qualifiers.
572 */
573 bool ubo_qualifiers_valid;
574 };
575
576
577 class ast_parameter_declarator : public ast_node {
578 public:
579 ast_parameter_declarator() :
580 type(NULL),
581 identifier(NULL),
582 is_array(false),
583 array_size(NULL),
584 formal_parameter(false),
585 is_void(false)
586 {
587 /* empty */
588 }
589
590 virtual void print(void) const;
591
592 virtual ir_rvalue *hir(exec_list *instructions,
593 struct _mesa_glsl_parse_state *state);
594
595 ast_fully_specified_type *type;
596 const char *identifier;
597 bool is_array;
598 ast_expression *array_size;
599
600 static void parameters_to_hir(exec_list *ast_parameters,
601 bool formal, exec_list *ir_parameters,
602 struct _mesa_glsl_parse_state *state);
603
604 private:
605 /** Is this parameter declaration part of a formal parameter list? */
606 bool formal_parameter;
607
608 /**
609 * Is this parameter 'void' type?
610 *
611 * This field is set by \c ::hir.
612 */
613 bool is_void;
614 };
615
616
617 class ast_function : public ast_node {
618 public:
619 ast_function(void);
620
621 virtual void print(void) const;
622
623 virtual ir_rvalue *hir(exec_list *instructions,
624 struct _mesa_glsl_parse_state *state);
625
626 ast_fully_specified_type *return_type;
627 const char *identifier;
628
629 exec_list parameters;
630
631 private:
632 /**
633 * Is this prototype part of the function definition?
634 *
635 * Used by ast_function_definition::hir to process the parameters, etc.
636 * of the function.
637 *
638 * \sa ::hir
639 */
640 bool is_definition;
641
642 /**
643 * Function signature corresponding to this function prototype instance
644 *
645 * Used by ast_function_definition::hir to process the parameters, etc.
646 * of the function.
647 *
648 * \sa ::hir
649 */
650 class ir_function_signature *signature;
651
652 friend class ast_function_definition;
653 };
654
655
656 class ast_expression_statement : public ast_node {
657 public:
658 ast_expression_statement(ast_expression *);
659 virtual void print(void) const;
660
661 virtual ir_rvalue *hir(exec_list *instructions,
662 struct _mesa_glsl_parse_state *state);
663
664 ast_expression *expression;
665 };
666
667
668 class ast_case_label : public ast_node {
669 public:
670 ast_case_label(ast_expression *test_value);
671 virtual void print(void) const;
672
673 virtual ir_rvalue *hir(exec_list *instructions,
674 struct _mesa_glsl_parse_state *state);
675
676 /**
677 * An test value of NULL means 'default'.
678 */
679 ast_expression *test_value;
680 };
681
682
683 class ast_case_label_list : public ast_node {
684 public:
685 ast_case_label_list(void);
686 virtual void print(void) const;
687
688 virtual ir_rvalue *hir(exec_list *instructions,
689 struct _mesa_glsl_parse_state *state);
690
691 /**
692 * A list of case labels.
693 */
694 exec_list labels;
695 };
696
697
698 class ast_case_statement : public ast_node {
699 public:
700 ast_case_statement(ast_case_label_list *labels);
701 virtual void print(void) const;
702
703 virtual ir_rvalue *hir(exec_list *instructions,
704 struct _mesa_glsl_parse_state *state);
705
706 ast_case_label_list *labels;
707
708 /**
709 * A list of statements.
710 */
711 exec_list stmts;
712 };
713
714
715 class ast_case_statement_list : public ast_node {
716 public:
717 ast_case_statement_list(void);
718 virtual void print(void) const;
719
720 virtual ir_rvalue *hir(exec_list *instructions,
721 struct _mesa_glsl_parse_state *state);
722
723 /**
724 * A list of cases.
725 */
726 exec_list cases;
727 };
728
729
730 class ast_switch_body : public ast_node {
731 public:
732 ast_switch_body(ast_case_statement_list *stmts);
733 virtual void print(void) const;
734
735 virtual ir_rvalue *hir(exec_list *instructions,
736 struct _mesa_glsl_parse_state *state);
737
738 ast_case_statement_list *stmts;
739 };
740
741
742 class ast_selection_statement : public ast_node {
743 public:
744 ast_selection_statement(ast_expression *condition,
745 ast_node *then_statement,
746 ast_node *else_statement);
747 virtual void print(void) const;
748
749 virtual ir_rvalue *hir(exec_list *instructions,
750 struct _mesa_glsl_parse_state *state);
751
752 ast_expression *condition;
753 ast_node *then_statement;
754 ast_node *else_statement;
755 };
756
757
758 class ast_switch_statement : public ast_node {
759 public:
760 ast_switch_statement(ast_expression *test_expression,
761 ast_node *body);
762 virtual void print(void) const;
763
764 virtual ir_rvalue *hir(exec_list *instructions,
765 struct _mesa_glsl_parse_state *state);
766
767 ast_expression *test_expression;
768 ast_node *body;
769
770 protected:
771 void test_to_hir(exec_list *, struct _mesa_glsl_parse_state *);
772 };
773
774 class ast_iteration_statement : public ast_node {
775 public:
776 ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
777 ast_expression *rest_expression, ast_node *body);
778
779 virtual void print(void) const;
780
781 virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
782
783 enum ast_iteration_modes {
784 ast_for,
785 ast_while,
786 ast_do_while
787 } mode;
788
789
790 ast_node *init_statement;
791 ast_node *condition;
792 ast_expression *rest_expression;
793
794 ast_node *body;
795
796 private:
797 /**
798 * Generate IR from the condition of a loop
799 *
800 * This is factored out of ::hir because some loops have the condition
801 * test at the top (for and while), and others have it at the end (do-while).
802 */
803 void condition_to_hir(class ir_loop *, struct _mesa_glsl_parse_state *);
804 };
805
806
807 class ast_jump_statement : public ast_node {
808 public:
809 ast_jump_statement(int mode, ast_expression *return_value);
810 virtual void print(void) const;
811
812 virtual ir_rvalue *hir(exec_list *instructions,
813 struct _mesa_glsl_parse_state *state);
814
815 enum ast_jump_modes {
816 ast_continue,
817 ast_break,
818 ast_return,
819 ast_discard
820 } mode;
821
822 ast_expression *opt_return_value;
823 };
824
825
826 class ast_function_definition : public ast_node {
827 public:
828 virtual void print(void) const;
829
830 virtual ir_rvalue *hir(exec_list *instructions,
831 struct _mesa_glsl_parse_state *state);
832
833 ast_function *prototype;
834 ast_compound_statement *body;
835 };
836
837 class ast_interface_block : public ast_node {
838 public:
839 ast_interface_block(ast_type_qualifier layout,
840 const char *instance_name,
841 ast_expression *array_size)
842 : layout(layout), block_name(NULL), instance_name(instance_name),
843 array_size(array_size)
844 {
845 /* empty */
846 }
847
848 virtual ir_rvalue *hir(exec_list *instructions,
849 struct _mesa_glsl_parse_state *state);
850
851 ast_type_qualifier layout;
852 const char *block_name;
853
854 /**
855 * Declared name of the block instance, if specified.
856 *
857 * If the block does not have an instance name, this field will be
858 * \c NULL.
859 */
860 const char *instance_name;
861
862 /** List of ast_declarator_list * */
863 exec_list declarations;
864
865 /**
866 * Declared array size of the block instance
867 *
868 * If the block is not declared as an array, this field will be \c NULL.
869 *
870 * \note
871 * A block can only be an array if it also has an instance name. If this
872 * field is not \c NULL, ::instance_name must also not be \c NULL.
873 */
874 ast_expression *array_size;
875 };
876 /*@}*/
877
878 extern void
879 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
880
881 extern ir_rvalue *
882 _mesa_ast_field_selection_to_hir(const ast_expression *expr,
883 exec_list *instructions,
884 struct _mesa_glsl_parse_state *state);
885
886 extern ir_rvalue *
887 _mesa_ast_array_index_to_hir(void *mem_ctx,
888 struct _mesa_glsl_parse_state *state,
889 ir_rvalue *array, ir_rvalue *idx,
890 YYLTYPE &loc, YYLTYPE &idx_loc);
891
892 void
893 emit_function(_mesa_glsl_parse_state *state, ir_function *f);
894
895 extern void
896 check_builtin_array_max_size(const char *name, unsigned size,
897 YYLTYPE loc, struct _mesa_glsl_parse_state *state);
898
899 #endif /* AST_H */