ae70b003cfeba94cf5a587f3556a61255fa2e319
[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 DECLARE_RALLOC_CXX_OPERATORS(ast_node);
53
54 /**
55 * Print an AST node in something approximating the original GLSL code
56 */
57 virtual void print(void) const;
58
59 /**
60 * Convert the AST node to the high-level intermediate representation
61 */
62 virtual ir_rvalue *hir(exec_list *instructions,
63 struct _mesa_glsl_parse_state *state);
64
65 /**
66 * Retrieve the source location of an AST node
67 *
68 * This function is primarily used to get the source position of an AST node
69 * into a form that can be passed to \c _mesa_glsl_error.
70 *
71 * \sa _mesa_glsl_error, ast_node::set_location
72 */
73 struct YYLTYPE get_location(void) const
74 {
75 struct YYLTYPE locp;
76
77 locp.source = this->location.source;
78 locp.first_line = this->location.first_line;
79 locp.first_column = this->location.first_column;
80 locp.last_line = this->location.last_line;
81 locp.last_column = this->location.last_column;
82
83 return locp;
84 }
85
86 /**
87 * Set the source location of an AST node from a parser location
88 *
89 * \sa ast_node::get_location
90 */
91 void set_location(const struct YYLTYPE &locp)
92 {
93 this->location.source = locp.source;
94 this->location.first_line = locp.first_line;
95 this->location.first_column = locp.first_column;
96 this->location.last_line = locp.last_line;
97 this->location.last_column = locp.last_column;
98 }
99
100 /**
101 * Set the source location range of an AST node using two location nodes
102 *
103 * \sa ast_node::set_location
104 */
105 void set_location_range(const struct YYLTYPE &begin, const struct YYLTYPE &end)
106 {
107 this->location.source = begin.source;
108 this->location.first_line = begin.first_line;
109 this->location.last_line = end.last_line;
110 this->location.first_column = begin.first_column;
111 this->location.last_column = end.last_column;
112 }
113
114 /**
115 * Source location of the AST node.
116 */
117 struct {
118 unsigned source; /**< GLSL source number. */
119 unsigned first_line; /**< First line number within the source string. */
120 unsigned first_column; /**< First column in the first line. */
121 unsigned last_line; /**< Last line number within the source string. */
122 unsigned last_column; /**< Last column in the last line. */
123 } location;
124
125 exec_node link;
126
127 protected:
128 /**
129 * The only constructor is protected so that only derived class objects can
130 * be created.
131 */
132 ast_node(void);
133 };
134
135
136 /**
137 * Operators for AST expression nodes.
138 */
139 enum ast_operators {
140 ast_assign,
141 ast_plus, /**< Unary + operator. */
142 ast_neg,
143 ast_add,
144 ast_sub,
145 ast_mul,
146 ast_div,
147 ast_mod,
148 ast_lshift,
149 ast_rshift,
150 ast_less,
151 ast_greater,
152 ast_lequal,
153 ast_gequal,
154 ast_equal,
155 ast_nequal,
156 ast_bit_and,
157 ast_bit_xor,
158 ast_bit_or,
159 ast_bit_not,
160 ast_logic_and,
161 ast_logic_xor,
162 ast_logic_or,
163 ast_logic_not,
164
165 ast_mul_assign,
166 ast_div_assign,
167 ast_mod_assign,
168 ast_add_assign,
169 ast_sub_assign,
170 ast_ls_assign,
171 ast_rs_assign,
172 ast_and_assign,
173 ast_xor_assign,
174 ast_or_assign,
175
176 ast_conditional,
177
178 ast_pre_inc,
179 ast_pre_dec,
180 ast_post_inc,
181 ast_post_dec,
182 ast_field_selection,
183 ast_array_index,
184
185 ast_function_call,
186
187 ast_identifier,
188 ast_int_constant,
189 ast_uint_constant,
190 ast_float_constant,
191 ast_bool_constant,
192
193 ast_sequence,
194 ast_aggregate
195 };
196
197 /**
198 * Representation of any sort of expression.
199 */
200 class ast_expression : public ast_node {
201 public:
202 ast_expression(int oper, ast_expression *,
203 ast_expression *, ast_expression *);
204
205 ast_expression(const char *identifier) :
206 oper(ast_identifier)
207 {
208 subexpressions[0] = NULL;
209 subexpressions[1] = NULL;
210 subexpressions[2] = NULL;
211 primary_expression.identifier = identifier;
212 this->non_lvalue_description = NULL;
213 }
214
215 static const char *operator_string(enum ast_operators op);
216
217 virtual ir_rvalue *hir(exec_list *instructions,
218 struct _mesa_glsl_parse_state *state);
219
220 virtual void print(void) const;
221
222 enum ast_operators oper;
223
224 ast_expression *subexpressions[3];
225
226 union {
227 const char *identifier;
228 int int_constant;
229 float float_constant;
230 unsigned uint_constant;
231 int bool_constant;
232 } primary_expression;
233
234
235 /**
236 * List of expressions for an \c ast_sequence or parameters for an
237 * \c ast_function_call
238 */
239 exec_list expressions;
240
241 /**
242 * For things that can't be l-values, this describes what it is.
243 *
244 * This text is used by the code that generates IR for assignments to
245 * detect and emit useful messages for assignments to some things that
246 * can't be l-values. For example, pre- or post-incerement expressions.
247 *
248 * \note
249 * This pointer may be \c NULL.
250 */
251 const char *non_lvalue_description;
252 };
253
254 class ast_expression_bin : public ast_expression {
255 public:
256 ast_expression_bin(int oper, ast_expression *, ast_expression *);
257
258 virtual void print(void) const;
259 };
260
261 /**
262 * Subclass of expressions for function calls
263 */
264 class ast_function_expression : public ast_expression {
265 public:
266 ast_function_expression(ast_expression *callee)
267 : ast_expression(ast_function_call, callee,
268 NULL, NULL),
269 cons(false)
270 {
271 /* empty */
272 }
273
274 ast_function_expression(class ast_type_specifier *type)
275 : ast_expression(ast_function_call, (ast_expression *) type,
276 NULL, NULL),
277 cons(true)
278 {
279 /* empty */
280 }
281
282 bool is_constructor() const
283 {
284 return cons;
285 }
286
287 virtual ir_rvalue *hir(exec_list *instructions,
288 struct _mesa_glsl_parse_state *state);
289
290 private:
291 /**
292 * Is this function call actually a constructor?
293 */
294 bool cons;
295 };
296
297 class ast_array_specifier : public ast_node {
298 public:
299 /** Unsized array specifier ([]) */
300 explicit ast_array_specifier(const struct YYLTYPE &locp)
301 : is_unsized_array(true)
302 {
303 set_location(locp);
304 }
305
306 /** Sized array specifier ([dim]) */
307 ast_array_specifier(const struct YYLTYPE &locp, ast_expression *dim)
308 : is_unsized_array(false)
309 {
310 set_location(locp);
311 array_dimensions.push_tail(&dim->link);
312 }
313
314 void add_dimension(ast_expression *dim)
315 {
316 array_dimensions.push_tail(&dim->link);
317 }
318
319 virtual void print(void) const;
320
321 /* If true, this means that the array has an unsized outermost dimension. */
322 bool is_unsized_array;
323
324 /* This list contains objects of type ast_node containing the
325 * sized dimensions only, in outermost-to-innermost order.
326 */
327 exec_list array_dimensions;
328 };
329
330 /**
331 * C-style aggregate initialization class
332 *
333 * Represents C-style initializers of vectors, matrices, arrays, and
334 * structures. E.g., vec3 pos = {1.0, 0.0, -1.0} is equivalent to
335 * vec3 pos = vec3(1.0, 0.0, -1.0).
336 *
337 * Specified in GLSL 4.20 and GL_ARB_shading_language_420pack.
338 *
339 * \sa _mesa_ast_set_aggregate_type
340 */
341 class ast_aggregate_initializer : public ast_expression {
342 public:
343 ast_aggregate_initializer()
344 : ast_expression(ast_aggregate, NULL, NULL, NULL),
345 constructor_type(NULL)
346 {
347 /* empty */
348 }
349
350 /**
351 * glsl_type of the aggregate, which is inferred from the LHS of whatever
352 * the aggregate is being used to initialize. This can't be inferred at
353 * parse time (since the parser deals with ast_type_specifiers, not
354 * glsl_types), so the parser leaves it NULL. However, the ast-to-hir
355 * conversion code makes sure to fill it in with the appropriate type
356 * before hir() is called.
357 */
358 const glsl_type *constructor_type;
359
360 virtual ir_rvalue *hir(exec_list *instructions,
361 struct _mesa_glsl_parse_state *state);
362 };
363
364 /**
365 * Number of possible operators for an ast_expression
366 *
367 * This is done as a define instead of as an additional value in the enum so
368 * that the compiler won't generate spurious messages like "warning:
369 * enumeration value ‘ast_num_operators’ not handled in switch"
370 */
371 #define AST_NUM_OPERATORS (ast_sequence + 1)
372
373
374 class ast_compound_statement : public ast_node {
375 public:
376 ast_compound_statement(int new_scope, ast_node *statements);
377 virtual void print(void) const;
378
379 virtual ir_rvalue *hir(exec_list *instructions,
380 struct _mesa_glsl_parse_state *state);
381
382 int new_scope;
383 exec_list statements;
384 };
385
386 class ast_declaration : public ast_node {
387 public:
388 ast_declaration(const char *identifier,
389 ast_array_specifier *array_specifier,
390 ast_expression *initializer);
391 virtual void print(void) const;
392
393 const char *identifier;
394
395 ast_array_specifier *array_specifier;
396
397 ast_expression *initializer;
398 };
399
400
401 enum {
402 ast_precision_none = 0, /**< Absence of precision qualifier. */
403 ast_precision_high,
404 ast_precision_medium,
405 ast_precision_low
406 };
407
408 struct ast_type_qualifier {
409 DECLARE_RALLOC_CXX_OPERATORS(ast_type_qualifier);
410
411 union {
412 struct {
413 unsigned invariant:1;
414 unsigned constant:1;
415 unsigned attribute:1;
416 unsigned varying:1;
417 unsigned in:1;
418 unsigned out:1;
419 unsigned centroid:1;
420 unsigned sample:1;
421 unsigned uniform:1;
422 unsigned smooth:1;
423 unsigned flat:1;
424 unsigned noperspective:1;
425
426 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
427 /*@{*/
428 unsigned origin_upper_left:1;
429 unsigned pixel_center_integer:1;
430 /*@}*/
431
432 /**
433 * Flag set if GL_ARB_explicit_attrib_location "location" layout
434 * qualifier is used.
435 */
436 unsigned explicit_location:1;
437 /**
438 * Flag set if GL_ARB_explicit_attrib_location "index" layout
439 * qualifier is used.
440 */
441 unsigned explicit_index:1;
442
443 /**
444 * Flag set if GL_ARB_shading_language_420pack "binding" layout
445 * qualifier is used.
446 */
447 unsigned explicit_binding:1;
448
449 /**
450 * Flag set if GL_ARB_shader_atomic counter "offset" layout
451 * qualifier is used.
452 */
453 unsigned explicit_offset:1;
454
455 /** \name Layout qualifiers for GL_AMD_conservative_depth */
456 /** \{ */
457 unsigned depth_any:1;
458 unsigned depth_greater:1;
459 unsigned depth_less:1;
460 unsigned depth_unchanged:1;
461 /** \} */
462
463 /** \name Layout qualifiers for GL_ARB_uniform_buffer_object */
464 /** \{ */
465 unsigned std140:1;
466 unsigned shared:1;
467 unsigned packed:1;
468 unsigned column_major:1;
469 unsigned row_major:1;
470 /** \} */
471
472 /** \name Layout qualifiers for GLSL 1.50 geometry shaders */
473 /** \{ */
474 unsigned prim_type:1;
475 unsigned max_vertices:1;
476 /** \} */
477
478 /**
479 * local_size_{x,y,z} flags for compute shaders. Bit 0 represents
480 * local_size_x, and so on.
481 */
482 unsigned local_size:3;
483
484 /** \name Layout and memory qualifiers for ARB_shader_image_load_store. */
485 /** \{ */
486 unsigned early_fragment_tests:1;
487 unsigned explicit_image_format:1;
488 unsigned coherent:1;
489 unsigned _volatile:1;
490 unsigned restrict_flag:1;
491 unsigned read_only:1; /**< "readonly" qualifier. */
492 unsigned write_only:1; /**< "writeonly" qualifier. */
493 /** \} */
494
495 /** \name Layout qualifiers for GL_ARB_gpu_shader5 */
496 /** \{ */
497 unsigned invocations:1;
498 /** \} */
499 }
500 /** \brief Set of flags, accessed by name. */
501 q;
502
503 /** \brief Set of flags, accessed as a bitmask. */
504 uint64_t i;
505 } flags;
506
507 /** Precision of the type (highp/medium/lowp). */
508 unsigned precision:2;
509
510 /** Geometry shader invocations for GL_ARB_gpu_shader5. */
511 int invocations;
512
513 /**
514 * Location specified via GL_ARB_explicit_attrib_location layout
515 *
516 * \note
517 * This field is only valid if \c explicit_location is set.
518 */
519 int location;
520 /**
521 * Index specified via GL_ARB_explicit_attrib_location layout
522 *
523 * \note
524 * This field is only valid if \c explicit_index is set.
525 */
526 int index;
527
528 /** Maximum output vertices in GLSL 1.50 geometry shaders. */
529 int max_vertices;
530
531 /** Input or output primitive type in GLSL 1.50 geometry shaders */
532 GLenum prim_type;
533
534 /**
535 * Binding specified via GL_ARB_shading_language_420pack's "binding" keyword.
536 *
537 * \note
538 * This field is only valid if \c explicit_binding is set.
539 */
540 int binding;
541
542 /**
543 * Offset specified via GL_ARB_shader_atomic_counter's "offset"
544 * keyword.
545 *
546 * \note
547 * This field is only valid if \c explicit_offset is set.
548 */
549 int offset;
550
551 /**
552 * Local size specified via GL_ARB_compute_shader's "local_size_{x,y,z}"
553 * layout qualifier. Element i of this array is only valid if
554 * flags.q.local_size & (1 << i) is set.
555 */
556 int local_size[3];
557
558 /**
559 * Image format specified with an ARB_shader_image_load_store
560 * layout qualifier.
561 *
562 * \note
563 * This field is only valid if \c explicit_image_format is set.
564 */
565 GLenum image_format;
566
567 /**
568 * Base type of the data read from or written to this image. Only
569 * the following enumerants are allowed: GLSL_TYPE_UINT,
570 * GLSL_TYPE_INT, GLSL_TYPE_FLOAT.
571 *
572 * \note
573 * This field is only valid if \c explicit_image_format is set.
574 */
575 glsl_base_type image_base_type;
576
577 /**
578 * Return true if and only if an interpolation qualifier is present.
579 */
580 bool has_interpolation() const;
581
582 /**
583 * Return whether a layout qualifier is present.
584 */
585 bool has_layout() const;
586
587 /**
588 * Return whether a storage qualifier is present.
589 */
590 bool has_storage() const;
591
592 /**
593 * Return whether an auxiliary storage qualifier is present.
594 */
595 bool has_auxiliary_storage() const;
596
597 /**
598 * \brief Return string representation of interpolation qualifier.
599 *
600 * If an interpolation qualifier is present, then return that qualifier's
601 * string representation. Otherwise, return null. For example, if the
602 * noperspective bit is set, then this returns "noperspective".
603 *
604 * If multiple interpolation qualifiers are somehow present, then the
605 * returned string is undefined but not null.
606 */
607 const char *interpolation_string() const;
608
609 bool merge_qualifier(YYLTYPE *loc,
610 _mesa_glsl_parse_state *state,
611 ast_type_qualifier q);
612
613 bool merge_in_qualifier(YYLTYPE *loc,
614 _mesa_glsl_parse_state *state,
615 ast_type_qualifier q,
616 ast_node* &node);
617
618 };
619
620 class ast_declarator_list;
621
622 class ast_struct_specifier : public ast_node {
623 public:
624 /**
625 * \brief Make a shallow copy of an ast_struct_specifier.
626 *
627 * Use only if the objects are allocated from the same context and will not
628 * be modified. Zeros the inherited ast_node's fields.
629 */
630 ast_struct_specifier(const ast_struct_specifier& that):
631 ast_node(), name(that.name), declarations(that.declarations),
632 is_declaration(that.is_declaration)
633 {
634 /* empty */
635 }
636
637 ast_struct_specifier(const char *identifier,
638 ast_declarator_list *declarator_list);
639 virtual void print(void) const;
640
641 virtual ir_rvalue *hir(exec_list *instructions,
642 struct _mesa_glsl_parse_state *state);
643
644 const char *name;
645 /* List of ast_declarator_list * */
646 exec_list declarations;
647 bool is_declaration;
648 };
649
650
651
652 class ast_type_specifier : public ast_node {
653 public:
654 /**
655 * \brief Make a shallow copy of an ast_type_specifier, specifying array
656 * fields.
657 *
658 * Use only if the objects are allocated from the same context and will not
659 * be modified. Zeros the inherited ast_node's fields.
660 */
661 ast_type_specifier(const ast_type_specifier *that,
662 ast_array_specifier *array_specifier)
663 : ast_node(), type_name(that->type_name), structure(that->structure),
664 array_specifier(array_specifier),
665 default_precision(that->default_precision)
666 {
667 /* empty */
668 }
669
670 /** Construct a type specifier from a type name */
671 ast_type_specifier(const char *name)
672 : type_name(name), structure(NULL), array_specifier(NULL),
673 default_precision(ast_precision_none)
674 {
675 /* empty */
676 }
677
678 /** Construct a type specifier from a structure definition */
679 ast_type_specifier(ast_struct_specifier *s)
680 : type_name(s->name), structure(s), array_specifier(NULL),
681 default_precision(ast_precision_none)
682 {
683 /* empty */
684 }
685
686 const struct glsl_type *glsl_type(const char **name,
687 struct _mesa_glsl_parse_state *state)
688 const;
689
690 virtual void print(void) const;
691
692 ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
693
694 const char *type_name;
695 ast_struct_specifier *structure;
696
697 ast_array_specifier *array_specifier;
698
699 /** For precision statements, this is the given precision; otherwise none. */
700 unsigned default_precision:2;
701 };
702
703
704 class ast_fully_specified_type : public ast_node {
705 public:
706 virtual void print(void) const;
707 bool has_qualifiers() const;
708
709 ast_fully_specified_type() : qualifier(), specifier(NULL)
710 {
711 }
712
713 const struct glsl_type *glsl_type(const char **name,
714 struct _mesa_glsl_parse_state *state)
715 const;
716
717 ast_type_qualifier qualifier;
718 ast_type_specifier *specifier;
719 };
720
721
722 class ast_declarator_list : public ast_node {
723 public:
724 ast_declarator_list(ast_fully_specified_type *);
725 virtual void print(void) const;
726
727 virtual ir_rvalue *hir(exec_list *instructions,
728 struct _mesa_glsl_parse_state *state);
729
730 ast_fully_specified_type *type;
731 /** List of 'ast_declaration *' */
732 exec_list declarations;
733
734 /**
735 * Special flag for vertex shader "invariant" declarations.
736 *
737 * Vertex shaders can contain "invariant" variable redeclarations that do
738 * not include a type. For example, "invariant gl_Position;". This flag
739 * is used to note these cases when no type is specified.
740 */
741 int invariant;
742 };
743
744
745 class ast_parameter_declarator : public ast_node {
746 public:
747 ast_parameter_declarator() :
748 type(NULL),
749 identifier(NULL),
750 array_specifier(NULL),
751 formal_parameter(false),
752 is_void(false)
753 {
754 /* empty */
755 }
756
757 virtual void print(void) const;
758
759 virtual ir_rvalue *hir(exec_list *instructions,
760 struct _mesa_glsl_parse_state *state);
761
762 ast_fully_specified_type *type;
763 const char *identifier;
764 ast_array_specifier *array_specifier;
765
766 static void parameters_to_hir(exec_list *ast_parameters,
767 bool formal, exec_list *ir_parameters,
768 struct _mesa_glsl_parse_state *state);
769
770 private:
771 /** Is this parameter declaration part of a formal parameter list? */
772 bool formal_parameter;
773
774 /**
775 * Is this parameter 'void' type?
776 *
777 * This field is set by \c ::hir.
778 */
779 bool is_void;
780 };
781
782
783 class ast_function : public ast_node {
784 public:
785 ast_function(void);
786
787 virtual void print(void) const;
788
789 virtual ir_rvalue *hir(exec_list *instructions,
790 struct _mesa_glsl_parse_state *state);
791
792 ast_fully_specified_type *return_type;
793 const char *identifier;
794
795 exec_list parameters;
796
797 private:
798 /**
799 * Is this prototype part of the function definition?
800 *
801 * Used by ast_function_definition::hir to process the parameters, etc.
802 * of the function.
803 *
804 * \sa ::hir
805 */
806 bool is_definition;
807
808 /**
809 * Function signature corresponding to this function prototype instance
810 *
811 * Used by ast_function_definition::hir to process the parameters, etc.
812 * of the function.
813 *
814 * \sa ::hir
815 */
816 class ir_function_signature *signature;
817
818 friend class ast_function_definition;
819 };
820
821
822 class ast_expression_statement : public ast_node {
823 public:
824 ast_expression_statement(ast_expression *);
825 virtual void print(void) const;
826
827 virtual ir_rvalue *hir(exec_list *instructions,
828 struct _mesa_glsl_parse_state *state);
829
830 ast_expression *expression;
831 };
832
833
834 class ast_case_label : public ast_node {
835 public:
836 ast_case_label(ast_expression *test_value);
837 virtual void print(void) const;
838
839 virtual ir_rvalue *hir(exec_list *instructions,
840 struct _mesa_glsl_parse_state *state);
841
842 /**
843 * An test value of NULL means 'default'.
844 */
845 ast_expression *test_value;
846 };
847
848
849 class ast_case_label_list : public ast_node {
850 public:
851 ast_case_label_list(void);
852 virtual void print(void) const;
853
854 virtual ir_rvalue *hir(exec_list *instructions,
855 struct _mesa_glsl_parse_state *state);
856
857 /**
858 * A list of case labels.
859 */
860 exec_list labels;
861 };
862
863
864 class ast_case_statement : public ast_node {
865 public:
866 ast_case_statement(ast_case_label_list *labels);
867 virtual void print(void) const;
868
869 virtual ir_rvalue *hir(exec_list *instructions,
870 struct _mesa_glsl_parse_state *state);
871
872 ast_case_label_list *labels;
873
874 /**
875 * A list of statements.
876 */
877 exec_list stmts;
878 };
879
880
881 class ast_case_statement_list : public ast_node {
882 public:
883 ast_case_statement_list(void);
884 virtual void print(void) const;
885
886 virtual ir_rvalue *hir(exec_list *instructions,
887 struct _mesa_glsl_parse_state *state);
888
889 /**
890 * A list of cases.
891 */
892 exec_list cases;
893 };
894
895
896 class ast_switch_body : public ast_node {
897 public:
898 ast_switch_body(ast_case_statement_list *stmts);
899 virtual void print(void) const;
900
901 virtual ir_rvalue *hir(exec_list *instructions,
902 struct _mesa_glsl_parse_state *state);
903
904 ast_case_statement_list *stmts;
905 };
906
907
908 class ast_selection_statement : public ast_node {
909 public:
910 ast_selection_statement(ast_expression *condition,
911 ast_node *then_statement,
912 ast_node *else_statement);
913 virtual void print(void) const;
914
915 virtual ir_rvalue *hir(exec_list *instructions,
916 struct _mesa_glsl_parse_state *state);
917
918 ast_expression *condition;
919 ast_node *then_statement;
920 ast_node *else_statement;
921 };
922
923
924 class ast_switch_statement : public ast_node {
925 public:
926 ast_switch_statement(ast_expression *test_expression,
927 ast_node *body);
928 virtual void print(void) const;
929
930 virtual ir_rvalue *hir(exec_list *instructions,
931 struct _mesa_glsl_parse_state *state);
932
933 ast_expression *test_expression;
934 ast_node *body;
935
936 protected:
937 void test_to_hir(exec_list *, struct _mesa_glsl_parse_state *);
938 };
939
940 class ast_iteration_statement : public ast_node {
941 public:
942 ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
943 ast_expression *rest_expression, ast_node *body);
944
945 virtual void print(void) const;
946
947 virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
948
949 enum ast_iteration_modes {
950 ast_for,
951 ast_while,
952 ast_do_while
953 } mode;
954
955
956 ast_node *init_statement;
957 ast_node *condition;
958 ast_expression *rest_expression;
959
960 ast_node *body;
961
962 /**
963 * Generate IR from the condition of a loop
964 *
965 * This is factored out of ::hir because some loops have the condition
966 * test at the top (for and while), and others have it at the end (do-while).
967 */
968 void condition_to_hir(exec_list *, struct _mesa_glsl_parse_state *);
969 };
970
971
972 class ast_jump_statement : public ast_node {
973 public:
974 ast_jump_statement(int mode, ast_expression *return_value);
975 virtual void print(void) const;
976
977 virtual ir_rvalue *hir(exec_list *instructions,
978 struct _mesa_glsl_parse_state *state);
979
980 enum ast_jump_modes {
981 ast_continue,
982 ast_break,
983 ast_return,
984 ast_discard
985 } mode;
986
987 ast_expression *opt_return_value;
988 };
989
990
991 class ast_function_definition : public ast_node {
992 public:
993 ast_function_definition() : prototype(NULL), body(NULL)
994 {
995 }
996
997 virtual void print(void) const;
998
999 virtual ir_rvalue *hir(exec_list *instructions,
1000 struct _mesa_glsl_parse_state *state);
1001
1002 ast_function *prototype;
1003 ast_compound_statement *body;
1004 };
1005
1006 class ast_interface_block : public ast_node {
1007 public:
1008 ast_interface_block(ast_type_qualifier layout,
1009 const char *instance_name,
1010 ast_array_specifier *array_specifier)
1011 : layout(layout), block_name(NULL), instance_name(instance_name),
1012 array_specifier(array_specifier)
1013 {
1014 }
1015
1016 virtual ir_rvalue *hir(exec_list *instructions,
1017 struct _mesa_glsl_parse_state *state);
1018
1019 ast_type_qualifier layout;
1020 const char *block_name;
1021
1022 /**
1023 * Declared name of the block instance, if specified.
1024 *
1025 * If the block does not have an instance name, this field will be
1026 * \c NULL.
1027 */
1028 const char *instance_name;
1029
1030 /** List of ast_declarator_list * */
1031 exec_list declarations;
1032
1033 /**
1034 * Declared array size of the block instance
1035 *
1036 * If the block is not declared as an array or if the block instance array
1037 * is unsized, this field will be \c NULL.
1038 */
1039 ast_array_specifier *array_specifier;
1040 };
1041
1042
1043 /**
1044 * AST node representing a declaration of the input layout for geometry
1045 * shaders.
1046 */
1047 class ast_gs_input_layout : public ast_node
1048 {
1049 public:
1050 ast_gs_input_layout(const struct YYLTYPE &locp, GLenum prim_type)
1051 : prim_type(prim_type)
1052 {
1053 set_location(locp);
1054 }
1055
1056 virtual ir_rvalue *hir(exec_list *instructions,
1057 struct _mesa_glsl_parse_state *state);
1058
1059 private:
1060 const GLenum prim_type;
1061 };
1062
1063
1064 /**
1065 * AST node representing a decalaration of the input layout for compute
1066 * shaders.
1067 */
1068 class ast_cs_input_layout : public ast_node
1069 {
1070 public:
1071 ast_cs_input_layout(const struct YYLTYPE &locp, const unsigned *local_size)
1072 {
1073 memcpy(this->local_size, local_size, sizeof(this->local_size));
1074 set_location(locp);
1075 }
1076
1077 virtual ir_rvalue *hir(exec_list *instructions,
1078 struct _mesa_glsl_parse_state *state);
1079
1080 private:
1081 unsigned local_size[3];
1082 };
1083
1084 /*@}*/
1085
1086 extern void
1087 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
1088
1089 extern ir_rvalue *
1090 _mesa_ast_field_selection_to_hir(const ast_expression *expr,
1091 exec_list *instructions,
1092 struct _mesa_glsl_parse_state *state);
1093
1094 extern ir_rvalue *
1095 _mesa_ast_array_index_to_hir(void *mem_ctx,
1096 struct _mesa_glsl_parse_state *state,
1097 ir_rvalue *array, ir_rvalue *idx,
1098 YYLTYPE &loc, YYLTYPE &idx_loc);
1099
1100 extern void
1101 _mesa_ast_set_aggregate_type(const glsl_type *type,
1102 ast_expression *expr);
1103
1104 void
1105 emit_function(_mesa_glsl_parse_state *state, ir_function *f);
1106
1107 extern void
1108 check_builtin_array_max_size(const char *name, unsigned size,
1109 YYLTYPE loc, struct _mesa_glsl_parse_state *state);
1110
1111 #endif /* AST_H */