455cb8113cda9c5bba86ad7e3ca99ea1bf3621cd
[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 /** \brief Set of flags, accessed by name. */
627 q;
628
629 /** \brief Set of flags, accessed as a bitmask. */
630 uint64_t i;
631 } flags;
632
633 /** Precision of the type (highp/medium/lowp). */
634 unsigned precision:2;
635
636 /** Type of layout qualifiers for GL_AMD_conservative_depth. */
637 unsigned depth_type:3;
638
639 /**
640 * Alignment specified via GL_ARB_enhanced_layouts "align" layout qualifier
641 */
642 ast_expression *align;
643
644 /** Geometry shader invocations for GL_ARB_gpu_shader5. */
645 ast_layout_expression *invocations;
646
647 /**
648 * Location specified via GL_ARB_explicit_attrib_location layout
649 *
650 * \note
651 * This field is only valid if \c explicit_location is set.
652 */
653 ast_expression *location;
654 /**
655 * Index specified via GL_ARB_explicit_attrib_location layout
656 *
657 * \note
658 * This field is only valid if \c explicit_index is set.
659 */
660 ast_expression *index;
661
662 /**
663 * Component specified via GL_ARB_enhaced_layouts
664 *
665 * \note
666 * This field is only valid if \c explicit_component is set.
667 */
668 ast_expression *component;
669
670 /** Maximum output vertices in GLSL 1.50 geometry shaders. */
671 ast_layout_expression *max_vertices;
672
673 /** Stream in GLSL 1.50 geometry shaders. */
674 ast_expression *stream;
675
676 /** xfb_buffer specified via the GL_ARB_enhanced_layouts keyword. */
677 ast_expression *xfb_buffer;
678
679 /** xfb_stride specified via the GL_ARB_enhanced_layouts keyword. */
680 ast_expression *xfb_stride;
681
682 /** global xfb_stride values for each buffer */
683 ast_layout_expression *out_xfb_stride[MAX_FEEDBACK_BUFFERS];
684
685 /**
686 * Input or output primitive type in GLSL 1.50 geometry shaders
687 * and tessellation shaders.
688 */
689 GLenum prim_type;
690
691 /**
692 * Binding specified via GL_ARB_shading_language_420pack's "binding" keyword.
693 *
694 * \note
695 * This field is only valid if \c explicit_binding is set.
696 */
697 ast_expression *binding;
698
699 /**
700 * Offset specified via GL_ARB_shader_atomic_counter's or
701 * GL_ARB_enhanced_layouts "offset" keyword, or by GL_ARB_enhanced_layouts
702 * "xfb_offset" keyword.
703 *
704 * \note
705 * This field is only valid if \c explicit_offset is set.
706 */
707 ast_expression *offset;
708
709 /**
710 * Local size specified via GL_ARB_compute_shader's "local_size_{x,y,z}"
711 * layout qualifier. Element i of this array is only valid if
712 * flags.q.local_size & (1 << i) is set.
713 */
714 ast_layout_expression *local_size[3];
715
716 /** Tessellation evaluation shader: vertex spacing (equal, fractional even/odd) */
717 enum gl_tess_spacing vertex_spacing;
718
719 /** Tessellation evaluation shader: vertex ordering (CW or CCW) */
720 GLenum ordering;
721
722 /** Tessellation evaluation shader: point mode */
723 bool point_mode;
724
725 /** Tessellation control shader: number of output vertices */
726 ast_layout_expression *vertices;
727
728 /**
729 * Image format specified with an ARB_shader_image_load_store
730 * layout qualifier.
731 *
732 * \note
733 * This field is only valid if \c explicit_image_format is set.
734 */
735 GLenum image_format;
736
737 /**
738 * Base type of the data read from or written to this image. Only
739 * the following enumerants are allowed: GLSL_TYPE_UINT,
740 * GLSL_TYPE_INT, GLSL_TYPE_FLOAT.
741 *
742 * \note
743 * This field is only valid if \c explicit_image_format is set.
744 */
745 glsl_base_type image_base_type;
746
747 /**
748 * Return true if and only if an interpolation qualifier is present.
749 */
750 bool has_interpolation() const;
751
752 /**
753 * Return whether a layout qualifier is present.
754 */
755 bool has_layout() const;
756
757 /**
758 * Return whether a storage qualifier is present.
759 */
760 bool has_storage() const;
761
762 /**
763 * Return whether an auxiliary storage qualifier is present.
764 */
765 bool has_auxiliary_storage() const;
766
767 /**
768 * Return true if and only if a memory qualifier is present.
769 */
770 bool has_memory() const;
771
772 /**
773 * Return true if the qualifier is a subroutine declaration.
774 */
775 bool is_subroutine_decl() const;
776
777 bool merge_qualifier(YYLTYPE *loc,
778 _mesa_glsl_parse_state *state,
779 const ast_type_qualifier &q,
780 bool is_single_layout_merge,
781 bool is_multiple_layouts_merge = false);
782
783 /**
784 * Validate current qualifier against the global out one.
785 */
786 bool validate_out_qualifier(YYLTYPE *loc,
787 _mesa_glsl_parse_state *state);
788
789 /**
790 * Merge current qualifier into the global out one.
791 */
792 bool merge_into_out_qualifier(YYLTYPE *loc,
793 _mesa_glsl_parse_state *state,
794 ast_node* &node);
795
796 /**
797 * Validate current qualifier against the global in one.
798 */
799 bool validate_in_qualifier(YYLTYPE *loc,
800 _mesa_glsl_parse_state *state);
801
802 /**
803 * Merge current qualifier into the global in one.
804 */
805 bool merge_into_in_qualifier(YYLTYPE *loc,
806 _mesa_glsl_parse_state *state,
807 ast_node* &node);
808
809 /**
810 * Push pending layout qualifiers to the global values.
811 */
812 bool push_to_global(YYLTYPE *loc,
813 _mesa_glsl_parse_state *state);
814
815 bool validate_flags(YYLTYPE *loc,
816 _mesa_glsl_parse_state *state,
817 const ast_type_qualifier &allowed_flags,
818 const char *message, const char *name);
819
820 ast_subroutine_list *subroutine_list;
821 };
822
823 class ast_declarator_list;
824
825 class ast_struct_specifier : public ast_node {
826 public:
827 ast_struct_specifier(void *lin_ctx, const char *identifier,
828 ast_declarator_list *declarator_list);
829 virtual void print(void) const;
830
831 virtual ir_rvalue *hir(exec_list *instructions,
832 struct _mesa_glsl_parse_state *state);
833
834 const char *name;
835 ast_type_qualifier *layout;
836 /* List of ast_declarator_list * */
837 exec_list declarations;
838 bool is_declaration;
839 };
840
841
842
843 class ast_type_specifier : public ast_node {
844 public:
845 /** Construct a type specifier from a type name */
846 ast_type_specifier(const char *name)
847 : type_name(name), structure(NULL), array_specifier(NULL),
848 default_precision(ast_precision_none)
849 {
850 /* empty */
851 }
852
853 /** Construct a type specifier from a structure definition */
854 ast_type_specifier(ast_struct_specifier *s)
855 : type_name(s->name), structure(s), array_specifier(NULL),
856 default_precision(ast_precision_none)
857 {
858 /* empty */
859 }
860
861 const struct glsl_type *glsl_type(const char **name,
862 struct _mesa_glsl_parse_state *state)
863 const;
864
865 virtual void print(void) const;
866
867 ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
868
869 const char *type_name;
870 ast_struct_specifier *structure;
871
872 ast_array_specifier *array_specifier;
873
874 /** For precision statements, this is the given precision; otherwise none. */
875 unsigned default_precision:2;
876 };
877
878
879 class ast_fully_specified_type : public ast_node {
880 public:
881 virtual void print(void) const;
882 bool has_qualifiers(_mesa_glsl_parse_state *state) const;
883
884 ast_fully_specified_type() : qualifier(), specifier(NULL)
885 {
886 }
887
888 const struct glsl_type *glsl_type(const char **name,
889 struct _mesa_glsl_parse_state *state)
890 const;
891
892 ast_type_qualifier qualifier;
893 ast_type_specifier *specifier;
894 };
895
896
897 class ast_declarator_list : public ast_node {
898 public:
899 ast_declarator_list(ast_fully_specified_type *);
900 virtual void print(void) const;
901
902 virtual ir_rvalue *hir(exec_list *instructions,
903 struct _mesa_glsl_parse_state *state);
904
905 ast_fully_specified_type *type;
906 /** List of 'ast_declaration *' */
907 exec_list declarations;
908
909 /**
910 * Flags for redeclarations. In these cases, no type is specified, to
911 * `type` is allowed to be NULL. In all other cases, this would be an error.
912 */
913 int invariant; /** < `invariant` redeclaration */
914 int precise; /** < `precise` redeclaration */
915 };
916
917
918 class ast_parameter_declarator : public ast_node {
919 public:
920 ast_parameter_declarator() :
921 type(NULL),
922 identifier(NULL),
923 array_specifier(NULL),
924 formal_parameter(false),
925 is_void(false)
926 {
927 /* empty */
928 }
929
930 virtual void print(void) const;
931
932 virtual ir_rvalue *hir(exec_list *instructions,
933 struct _mesa_glsl_parse_state *state);
934
935 ast_fully_specified_type *type;
936 const char *identifier;
937 ast_array_specifier *array_specifier;
938
939 static void parameters_to_hir(exec_list *ast_parameters,
940 bool formal, exec_list *ir_parameters,
941 struct _mesa_glsl_parse_state *state);
942
943 private:
944 /** Is this parameter declaration part of a formal parameter list? */
945 bool formal_parameter;
946
947 /**
948 * Is this parameter 'void' type?
949 *
950 * This field is set by \c ::hir.
951 */
952 bool is_void;
953 };
954
955
956 class ast_function : public ast_node {
957 public:
958 ast_function(void);
959
960 virtual void print(void) const;
961
962 virtual ir_rvalue *hir(exec_list *instructions,
963 struct _mesa_glsl_parse_state *state);
964
965 ast_fully_specified_type *return_type;
966 const char *identifier;
967
968 exec_list parameters;
969
970 private:
971 /**
972 * Is this prototype part of the function definition?
973 *
974 * Used by ast_function_definition::hir to process the parameters, etc.
975 * of the function.
976 *
977 * \sa ::hir
978 */
979 bool is_definition;
980
981 /**
982 * Function signature corresponding to this function prototype instance
983 *
984 * Used by ast_function_definition::hir to process the parameters, etc.
985 * of the function.
986 *
987 * \sa ::hir
988 */
989 class ir_function_signature *signature;
990
991 friend class ast_function_definition;
992 };
993
994
995 class ast_expression_statement : public ast_node {
996 public:
997 ast_expression_statement(ast_expression *);
998 virtual void print(void) const;
999
1000 virtual ir_rvalue *hir(exec_list *instructions,
1001 struct _mesa_glsl_parse_state *state);
1002
1003 ast_expression *expression;
1004 };
1005
1006
1007 class ast_case_label : public ast_node {
1008 public:
1009 ast_case_label(ast_expression *test_value);
1010 virtual void print(void) const;
1011
1012 virtual ir_rvalue *hir(exec_list *instructions,
1013 struct _mesa_glsl_parse_state *state);
1014
1015 /**
1016 * An test value of NULL means 'default'.
1017 */
1018 ast_expression *test_value;
1019 };
1020
1021
1022 class ast_case_label_list : public ast_node {
1023 public:
1024 ast_case_label_list(void);
1025 virtual void print(void) const;
1026
1027 virtual ir_rvalue *hir(exec_list *instructions,
1028 struct _mesa_glsl_parse_state *state);
1029
1030 /**
1031 * A list of case labels.
1032 */
1033 exec_list labels;
1034 };
1035
1036
1037 class ast_case_statement : public ast_node {
1038 public:
1039 ast_case_statement(ast_case_label_list *labels);
1040 virtual void print(void) const;
1041
1042 virtual ir_rvalue *hir(exec_list *instructions,
1043 struct _mesa_glsl_parse_state *state);
1044
1045 ast_case_label_list *labels;
1046
1047 /**
1048 * A list of statements.
1049 */
1050 exec_list stmts;
1051 };
1052
1053
1054 class ast_case_statement_list : public ast_node {
1055 public:
1056 ast_case_statement_list(void);
1057 virtual void print(void) const;
1058
1059 virtual ir_rvalue *hir(exec_list *instructions,
1060 struct _mesa_glsl_parse_state *state);
1061
1062 /**
1063 * A list of cases.
1064 */
1065 exec_list cases;
1066 };
1067
1068
1069 class ast_switch_body : public ast_node {
1070 public:
1071 ast_switch_body(ast_case_statement_list *stmts);
1072 virtual void print(void) const;
1073
1074 virtual ir_rvalue *hir(exec_list *instructions,
1075 struct _mesa_glsl_parse_state *state);
1076
1077 ast_case_statement_list *stmts;
1078 };
1079
1080
1081 class ast_selection_statement : public ast_node {
1082 public:
1083 ast_selection_statement(ast_expression *condition,
1084 ast_node *then_statement,
1085 ast_node *else_statement);
1086 virtual void print(void) const;
1087
1088 virtual ir_rvalue *hir(exec_list *instructions,
1089 struct _mesa_glsl_parse_state *state);
1090
1091 ast_expression *condition;
1092 ast_node *then_statement;
1093 ast_node *else_statement;
1094 };
1095
1096
1097 class ast_switch_statement : public ast_node {
1098 public:
1099 ast_switch_statement(ast_expression *test_expression,
1100 ast_node *body);
1101 virtual void print(void) const;
1102
1103 virtual ir_rvalue *hir(exec_list *instructions,
1104 struct _mesa_glsl_parse_state *state);
1105
1106 ast_expression *test_expression;
1107 ast_node *body;
1108
1109 protected:
1110 void test_to_hir(exec_list *, struct _mesa_glsl_parse_state *);
1111 };
1112
1113 class ast_iteration_statement : public ast_node {
1114 public:
1115 ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
1116 ast_expression *rest_expression, ast_node *body);
1117
1118 virtual void print(void) const;
1119
1120 virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
1121
1122 enum ast_iteration_modes {
1123 ast_for,
1124 ast_while,
1125 ast_do_while
1126 } mode;
1127
1128
1129 ast_node *init_statement;
1130 ast_node *condition;
1131 ast_expression *rest_expression;
1132
1133 ast_node *body;
1134
1135 /**
1136 * Generate IR from the condition of a loop
1137 *
1138 * This is factored out of ::hir because some loops have the condition
1139 * test at the top (for and while), and others have it at the end (do-while).
1140 */
1141 void condition_to_hir(exec_list *, struct _mesa_glsl_parse_state *);
1142 };
1143
1144
1145 class ast_jump_statement : public ast_node {
1146 public:
1147 ast_jump_statement(int mode, ast_expression *return_value);
1148 virtual void print(void) const;
1149
1150 virtual ir_rvalue *hir(exec_list *instructions,
1151 struct _mesa_glsl_parse_state *state);
1152
1153 enum ast_jump_modes {
1154 ast_continue,
1155 ast_break,
1156 ast_return,
1157 ast_discard
1158 } mode;
1159
1160 ast_expression *opt_return_value;
1161 };
1162
1163
1164 class ast_function_definition : public ast_node {
1165 public:
1166 ast_function_definition() : prototype(NULL), body(NULL)
1167 {
1168 }
1169
1170 virtual void print(void) const;
1171
1172 virtual ir_rvalue *hir(exec_list *instructions,
1173 struct _mesa_glsl_parse_state *state);
1174
1175 ast_function *prototype;
1176 ast_compound_statement *body;
1177 };
1178
1179 class ast_interface_block : public ast_node {
1180 public:
1181 ast_interface_block(const char *instance_name,
1182 ast_array_specifier *array_specifier)
1183 : block_name(NULL), instance_name(instance_name),
1184 array_specifier(array_specifier)
1185 {
1186 }
1187
1188 virtual ir_rvalue *hir(exec_list *instructions,
1189 struct _mesa_glsl_parse_state *state);
1190
1191 ast_type_qualifier default_layout;
1192 ast_type_qualifier layout;
1193 const char *block_name;
1194
1195 /**
1196 * Declared name of the block instance, if specified.
1197 *
1198 * If the block does not have an instance name, this field will be
1199 * \c NULL.
1200 */
1201 const char *instance_name;
1202
1203 /** List of ast_declarator_list * */
1204 exec_list declarations;
1205
1206 /**
1207 * Declared array size of the block instance
1208 *
1209 * If the block is not declared as an array or if the block instance array
1210 * is unsized, this field will be \c NULL.
1211 */
1212 ast_array_specifier *array_specifier;
1213 };
1214
1215
1216 /**
1217 * AST node representing a declaration of the output layout for tessellation
1218 * control shaders.
1219 */
1220 class ast_tcs_output_layout : public ast_node
1221 {
1222 public:
1223 ast_tcs_output_layout(const struct YYLTYPE &locp)
1224 {
1225 set_location(locp);
1226 }
1227
1228 virtual ir_rvalue *hir(exec_list *instructions,
1229 struct _mesa_glsl_parse_state *state);
1230 };
1231
1232
1233 /**
1234 * AST node representing a declaration of the input layout for geometry
1235 * shaders.
1236 */
1237 class ast_gs_input_layout : public ast_node
1238 {
1239 public:
1240 ast_gs_input_layout(const struct YYLTYPE &locp, GLenum prim_type)
1241 : prim_type(prim_type)
1242 {
1243 set_location(locp);
1244 }
1245
1246 virtual ir_rvalue *hir(exec_list *instructions,
1247 struct _mesa_glsl_parse_state *state);
1248
1249 private:
1250 const GLenum prim_type;
1251 };
1252
1253
1254 /**
1255 * AST node representing a decalaration of the input layout for compute
1256 * shaders.
1257 */
1258 class ast_cs_input_layout : public ast_node
1259 {
1260 public:
1261 ast_cs_input_layout(const struct YYLTYPE &locp,
1262 ast_layout_expression *const *local_size)
1263 {
1264 for (int i = 0; i < 3; i++) {
1265 this->local_size[i] = local_size[i];
1266 }
1267 set_location(locp);
1268 }
1269
1270 virtual ir_rvalue *hir(exec_list *instructions,
1271 struct _mesa_glsl_parse_state *state);
1272
1273 private:
1274 ast_layout_expression *local_size[3];
1275 };
1276
1277 /*@}*/
1278
1279 extern void
1280 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
1281
1282 extern ir_rvalue *
1283 _mesa_ast_field_selection_to_hir(const ast_expression *expr,
1284 exec_list *instructions,
1285 struct _mesa_glsl_parse_state *state);
1286
1287 extern ir_rvalue *
1288 _mesa_ast_array_index_to_hir(void *mem_ctx,
1289 struct _mesa_glsl_parse_state *state,
1290 ir_rvalue *array, ir_rvalue *idx,
1291 YYLTYPE &loc, YYLTYPE &idx_loc);
1292
1293 extern void
1294 _mesa_ast_set_aggregate_type(const glsl_type *type,
1295 ast_expression *expr);
1296
1297 void
1298 emit_function(_mesa_glsl_parse_state *state, ir_function *f);
1299
1300 extern void
1301 check_builtin_array_max_size(const char *name, unsigned size,
1302 YYLTYPE loc, struct _mesa_glsl_parse_state *state);
1303
1304 extern void _mesa_ast_process_interface_block(YYLTYPE *locp,
1305 _mesa_glsl_parse_state *state,
1306 ast_interface_block *const block,
1307 const struct ast_type_qualifier &q);
1308
1309 extern bool
1310 process_qualifier_constant(struct _mesa_glsl_parse_state *state,
1311 YYLTYPE *loc,
1312 const char *qual_indentifier,
1313 ast_expression *const_expression,
1314 unsigned *value);
1315 #endif /* AST_H */