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