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