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