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