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