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