OSDN Git Service

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