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