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