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