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