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