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