glsl: Add parsing for GLSL uniform blocks.
[profile/ivi/mesa.git] / src / 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    /* Callers of this ralloc-based new need not call delete. It's
53     * easier to just ralloc_free 'ctx' (or any of its ancestors). */
54    static void* operator new(size_t size, void *ctx)
55    {
56       void *node;
57
58       node = rzalloc_size(ctx, size);
59       assert(node != NULL);
60
61       return node;
62    }
63
64    /* If the user *does* call delete, that's OK, we will just
65     * ralloc_free in that case. */
66    static void operator delete(void *table)
67    {
68       ralloc_free(table);
69    }
70
71    /**
72     * Print an AST node in something approximating the original GLSL code
73     */
74    virtual void print(void) const;
75
76    /**
77     * Convert the AST node to the high-level intermediate representation
78     */
79    virtual ir_rvalue *hir(exec_list *instructions,
80                           struct _mesa_glsl_parse_state *state);
81
82    /**
83     * Retrieve the source location of an AST node
84     *
85     * This function is primarily used to get the source position of an AST node
86     * into a form that can be passed to \c _mesa_glsl_error.
87     *
88     * \sa _mesa_glsl_error, ast_node::set_location
89     */
90    struct YYLTYPE get_location(void) const
91    {
92       struct YYLTYPE locp;
93
94       locp.source = this->location.source;
95       locp.first_line = this->location.line;
96       locp.first_column = this->location.column;
97       locp.last_line = locp.first_line;
98       locp.last_column = locp.first_column;
99
100       return locp;
101    }
102
103    /**
104     * Set the source location of an AST node from a parser location
105     *
106     * \sa ast_node::get_location
107     */
108    void set_location(const struct YYLTYPE &locp)
109    {
110       this->location.source = locp.source;
111       this->location.line = locp.first_line;
112       this->location.column = locp.first_column;
113    }
114
115    /**
116     * Source location of the AST node.
117     */
118    struct {
119       unsigned source;    /**< GLSL source number. */
120       unsigned line;      /**< Line number within the source string. */
121       unsigned column;    /**< Column in the line. */
122    } location;
123
124    exec_node link;
125
126 protected:
127    /**
128     * The only constructor is protected so that only derived class objects can
129     * be created.
130     */
131    ast_node(void);
132 };
133
134
135 /**
136  * Operators for AST expression nodes.
137  */
138 enum ast_operators {
139    ast_assign,
140    ast_plus,        /**< Unary + operator. */
141    ast_neg,
142    ast_add,
143    ast_sub,
144    ast_mul,
145    ast_div,
146    ast_mod,
147    ast_lshift,
148    ast_rshift,
149    ast_less,
150    ast_greater,
151    ast_lequal,
152    ast_gequal,
153    ast_equal,
154    ast_nequal,
155    ast_bit_and,
156    ast_bit_xor,
157    ast_bit_or,
158    ast_bit_not,
159    ast_logic_and,
160    ast_logic_xor,
161    ast_logic_or,
162    ast_logic_not,
163
164    ast_mul_assign,
165    ast_div_assign,
166    ast_mod_assign,
167    ast_add_assign,
168    ast_sub_assign,
169    ast_ls_assign,
170    ast_rs_assign,
171    ast_and_assign,
172    ast_xor_assign,
173    ast_or_assign,
174
175    ast_conditional,
176
177    ast_pre_inc,
178    ast_pre_dec,
179    ast_post_inc,
180    ast_post_dec,
181    ast_field_selection,
182    ast_array_index,
183
184    ast_function_call,
185
186    ast_identifier,
187    ast_int_constant,
188    ast_uint_constant,
189    ast_float_constant,
190    ast_bool_constant,
191
192    ast_sequence
193 };
194
195 /**
196  * Representation of any sort of expression.
197  */
198 class ast_expression : public ast_node {
199 public:
200    ast_expression(int oper, ast_expression *,
201                   ast_expression *, ast_expression *);
202
203    ast_expression(const char *identifier) :
204       oper(ast_identifier)
205    {
206       subexpressions[0] = NULL;
207       subexpressions[1] = NULL;
208       subexpressions[2] = NULL;
209       primary_expression.identifier = identifier;
210       this->non_lvalue_description = NULL;
211    }
212
213    static const char *operator_string(enum ast_operators op);
214
215    virtual ir_rvalue *hir(exec_list *instructions,
216                           struct _mesa_glsl_parse_state *state);
217
218    virtual void print(void) const;
219
220    enum ast_operators oper;
221
222    ast_expression *subexpressions[3];
223
224    union {
225       const char *identifier;
226       int int_constant;
227       float float_constant;
228       unsigned uint_constant;
229       int bool_constant;
230    } primary_expression;
231
232
233    /**
234     * List of expressions for an \c ast_sequence or parameters for an
235     * \c ast_function_call
236     */
237    exec_list expressions;
238
239    /**
240     * For things that can't be l-values, this describes what it is.
241     *
242     * This text is used by the code that generates IR for assignments to
243     * detect and emit useful messages for assignments to some things that
244     * can't be l-values.  For example, pre- or post-incerement expressions.
245     *
246     * \note
247     * This pointer may be \c NULL.
248     */
249    const char *non_lvalue_description;
250 };
251
252 class ast_expression_bin : public ast_expression {
253 public:
254    ast_expression_bin(int oper, ast_expression *, ast_expression *);
255
256    virtual void print(void) const;
257 };
258
259 /**
260  * Subclass of expressions for function calls
261  */
262 class ast_function_expression : public ast_expression {
263 public:
264    ast_function_expression(ast_expression *callee)
265       : ast_expression(ast_function_call, callee,
266                        NULL, NULL),
267         cons(false)
268    {
269       /* empty */
270    }
271
272    ast_function_expression(class ast_type_specifier *type)
273       : ast_expression(ast_function_call, (ast_expression *) type,
274                        NULL, NULL),
275         cons(true)
276    {
277       /* empty */
278    }
279
280    bool is_constructor() const
281    {
282       return cons;
283    }
284
285    virtual ir_rvalue *hir(exec_list *instructions,
286                           struct _mesa_glsl_parse_state *state);
287
288 private:
289    /**
290     * Is this function call actually a constructor?
291     */
292    bool cons;
293 };
294
295
296 /**
297  * Number of possible operators for an ast_expression
298  *
299  * This is done as a define instead of as an additional value in the enum so
300  * that the compiler won't generate spurious messages like "warning:
301  * enumeration value ‘ast_num_operators’ not handled in switch"
302  */
303 #define AST_NUM_OPERATORS (ast_sequence + 1)
304
305
306 class ast_compound_statement : public ast_node {
307 public:
308    ast_compound_statement(int new_scope, ast_node *statements);
309    virtual void print(void) const;
310
311    virtual ir_rvalue *hir(exec_list *instructions,
312                           struct _mesa_glsl_parse_state *state);
313
314    int new_scope;
315    exec_list statements;
316 };
317
318 class ast_declaration : public ast_node {
319 public:
320    ast_declaration(const char *identifier, int is_array, ast_expression *array_size,
321                    ast_expression *initializer);
322    virtual void print(void) const;
323
324    const char *identifier;
325    
326    int is_array;
327    ast_expression *array_size;
328
329    ast_expression *initializer;
330 };
331
332
333 enum {
334    ast_precision_none = 0, /**< Absence of precision qualifier. */
335    ast_precision_high,
336    ast_precision_medium,
337    ast_precision_low
338 };
339
340 struct ast_type_qualifier {
341    union {
342       struct {
343          unsigned invariant:1;
344          unsigned constant:1;
345          unsigned attribute:1;
346          unsigned varying:1;
347          unsigned in:1;
348          unsigned out:1;
349          unsigned centroid:1;
350          unsigned uniform:1;
351          unsigned smooth:1;
352          unsigned flat:1;
353          unsigned noperspective:1;
354
355          /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
356          /*@{*/
357          unsigned origin_upper_left:1;
358          unsigned pixel_center_integer:1;
359          /*@}*/
360
361          /**
362           * Flag set if GL_ARB_explicit_attrib_location "location" layout
363           * qualifier is used.
364           */
365          unsigned explicit_location:1;
366          /**
367           * Flag set if GL_ARB_explicit_attrib_location "index" layout
368           * qualifier is used.
369           */
370          unsigned explicit_index:1;
371
372          /** \name Layout qualifiers for GL_AMD_conservative_depth */
373          /** \{ */
374          unsigned depth_any:1;
375          unsigned depth_greater:1;
376          unsigned depth_less:1;
377          unsigned depth_unchanged:1;
378          /** \} */
379
380          /** \name Layout qualifiers for GL_ARB_uniform_buffer_object */
381          /** \{ */
382          unsigned std140:1;
383          unsigned shared:1;
384          unsigned packed:1;
385          unsigned column_major:1;
386          unsigned row_major:1;
387          /** \} */
388       }
389       /** \brief Set of flags, accessed by name. */
390       q;
391
392       /** \brief Set of flags, accessed as a bitmask. */
393       unsigned i;
394    } flags;
395
396    /**
397     * Location specified via GL_ARB_explicit_attrib_location layout
398     *
399     * \note
400     * This field is only valid if \c explicit_location is set.
401     */
402    int location;
403    /**
404     * Index specified via GL_ARB_explicit_attrib_location layout
405     *
406     * \note
407     * This field is only valid if \c explicit_index is set.
408     */
409    int index;
410
411    /**
412     * Return true if and only if an interpolation qualifier is present.
413     */
414    bool has_interpolation() const;
415
416    /**
417     * \brief Return string representation of interpolation qualifier.
418     *
419     * If an interpolation qualifier is present, then return that qualifier's
420     * string representation. Otherwise, return null. For example, if the
421     * noperspective bit is set, then this returns "noperspective".
422     *
423     * If multiple interpolation qualifiers are somehow present, then the
424     * returned string is undefined but not null.
425     */
426    const char *interpolation_string() const;
427 };
428
429 class ast_declarator_list;
430
431 class ast_struct_specifier : public ast_node {
432 public:
433    ast_struct_specifier(const char *identifier,
434                         ast_declarator_list *declarator_list);
435    virtual void print(void) const;
436
437    virtual ir_rvalue *hir(exec_list *instructions,
438                           struct _mesa_glsl_parse_state *state);
439
440    const char *name;
441    /* List of ast_declarator_list * */
442    exec_list declarations;
443 };
444
445
446
447 class ast_type_specifier : public ast_node {
448 public:
449    /** Construct a type specifier from a type name */
450    ast_type_specifier(const char *name) 
451       : type_name(name), structure(NULL),
452         is_array(false), array_size(NULL), precision(ast_precision_none),
453         is_precision_statement(false)
454    {
455       /* empty */
456    }
457
458    /** Construct a type specifier from a structure definition */
459    ast_type_specifier(ast_struct_specifier *s)
460       : type_name(s->name), structure(s),
461         is_array(false), array_size(NULL), precision(ast_precision_none),
462         is_precision_statement(false)
463    {
464       /* empty */
465    }
466
467    const struct glsl_type *glsl_type(const char **name,
468                                      struct _mesa_glsl_parse_state *state)
469       const;
470
471    virtual void print(void) const;
472
473    ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
474
475    const char *type_name;
476    ast_struct_specifier *structure;
477
478    int is_array;
479    ast_expression *array_size;
480
481    unsigned precision:2;
482
483    bool is_precision_statement;
484 };
485
486
487 class ast_fully_specified_type : public ast_node {
488 public:
489    virtual void print(void) const;
490    bool has_qualifiers() const;
491
492    ast_type_qualifier qualifier;
493    ast_type_specifier *specifier;
494 };
495
496
497 class ast_declarator_list : public ast_node {
498 public:
499    ast_declarator_list(ast_fully_specified_type *);
500    virtual void print(void) const;
501
502    virtual ir_rvalue *hir(exec_list *instructions,
503                           struct _mesa_glsl_parse_state *state);
504
505    ast_fully_specified_type *type;
506    exec_list declarations;
507
508    /**
509     * Special flag for vertex shader "invariant" declarations.
510     *
511     * Vertex shaders can contain "invariant" variable redeclarations that do
512     * not include a type.  For example, "invariant gl_Position;".  This flag
513     * is used to note these cases when no type is specified.
514     */
515    int invariant;
516 };
517
518
519 class ast_parameter_declarator : public ast_node {
520 public:
521    ast_parameter_declarator()
522    {
523       this->identifier = NULL;
524       this->is_array = false;
525       this->array_size = 0;
526    }
527
528    virtual void print(void) const;
529
530    virtual ir_rvalue *hir(exec_list *instructions,
531                           struct _mesa_glsl_parse_state *state);
532
533    ast_fully_specified_type *type;
534    const char *identifier;
535    int is_array;
536    ast_expression *array_size;
537
538    static void parameters_to_hir(exec_list *ast_parameters,
539                                  bool formal, exec_list *ir_parameters,
540                                  struct _mesa_glsl_parse_state *state);
541
542 private:
543    /** Is this parameter declaration part of a formal parameter list? */
544    bool formal_parameter;
545
546    /**
547     * Is this parameter 'void' type?
548     *
549     * This field is set by \c ::hir.
550     */
551    bool is_void;
552 };
553
554
555 class ast_function : public ast_node {
556 public:
557    ast_function(void);
558
559    virtual void print(void) const;
560
561    virtual ir_rvalue *hir(exec_list *instructions,
562                           struct _mesa_glsl_parse_state *state);
563
564    ast_fully_specified_type *return_type;
565    const char *identifier;
566
567    exec_list parameters;
568
569 private:
570    /**
571     * Is this prototype part of the function definition?
572     *
573     * Used by ast_function_definition::hir to process the parameters, etc.
574     * of the function.
575     *
576     * \sa ::hir
577     */
578    bool is_definition;
579
580    /**
581     * Function signature corresponding to this function prototype instance
582     *
583     * Used by ast_function_definition::hir to process the parameters, etc.
584     * of the function.
585     *
586     * \sa ::hir
587     */
588    class ir_function_signature *signature;
589
590    friend class ast_function_definition;
591 };
592
593
594 class ast_expression_statement : public ast_node {
595 public:
596    ast_expression_statement(ast_expression *);
597    virtual void print(void) const;
598
599    virtual ir_rvalue *hir(exec_list *instructions,
600                           struct _mesa_glsl_parse_state *state);
601
602    ast_expression *expression;
603 };
604
605
606 class ast_case_label : public ast_node {
607 public:
608    ast_case_label(ast_expression *test_value);
609    virtual void print(void) const;
610
611    virtual ir_rvalue *hir(exec_list *instructions,
612                           struct _mesa_glsl_parse_state *state);
613
614    /**
615     * An test value of NULL means 'default'.
616     */
617    ast_expression *test_value;
618 };
619
620
621 class ast_case_label_list : public ast_node {
622 public:
623    ast_case_label_list(void);
624    virtual void print(void) const;
625
626    virtual ir_rvalue *hir(exec_list *instructions,
627                           struct _mesa_glsl_parse_state *state);
628
629    /**
630     * A list of case labels.
631     */
632    exec_list labels;
633 };
634
635
636 class ast_case_statement : public ast_node {
637 public:
638    ast_case_statement(ast_case_label_list *labels);
639    virtual void print(void) const;
640
641    virtual ir_rvalue *hir(exec_list *instructions,
642                           struct _mesa_glsl_parse_state *state);
643
644    ast_case_label_list *labels;
645
646    /**
647     * A list of statements.
648     */
649    exec_list stmts;
650 };
651
652
653 class ast_case_statement_list : public ast_node {
654 public:
655    ast_case_statement_list(void);
656    virtual void print(void) const;
657
658    virtual ir_rvalue *hir(exec_list *instructions,
659                           struct _mesa_glsl_parse_state *state);
660
661    /**
662     * A list of cases.
663     */
664    exec_list cases;
665 };
666
667
668 class ast_switch_body : public ast_node {
669 public:
670    ast_switch_body(ast_case_statement_list *stmts);
671    virtual void print(void) const;
672
673    virtual ir_rvalue *hir(exec_list *instructions,
674                           struct _mesa_glsl_parse_state *state);
675
676    ast_case_statement_list *stmts;
677 };
678
679
680 class ast_selection_statement : public ast_node {
681 public:
682    ast_selection_statement(ast_expression *condition,
683                            ast_node *then_statement,
684                            ast_node *else_statement);
685    virtual void print(void) const;
686
687    virtual ir_rvalue *hir(exec_list *instructions,
688                           struct _mesa_glsl_parse_state *state);
689
690    ast_expression *condition;
691    ast_node *then_statement;
692    ast_node *else_statement;
693 };
694
695
696 class ast_switch_statement : public ast_node {
697 public:
698    ast_switch_statement(ast_expression *test_expression,
699                         ast_node *body);
700    virtual void print(void) const;
701
702    virtual ir_rvalue *hir(exec_list *instructions,
703                           struct _mesa_glsl_parse_state *state);
704
705    ast_expression *test_expression;
706    ast_node *body;
707
708 protected:
709    void test_to_hir(exec_list *, struct _mesa_glsl_parse_state *);
710 };
711
712 class ast_iteration_statement : public ast_node {
713 public:
714    ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
715                            ast_expression *rest_expression, ast_node *body);
716
717    virtual void print(void) const;
718
719    virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
720
721    enum ast_iteration_modes {
722       ast_for,
723       ast_while,
724       ast_do_while
725    } mode;
726    
727
728    ast_node *init_statement;
729    ast_node *condition;
730    ast_expression *rest_expression;
731
732    ast_node *body;
733
734 private:
735    /**
736     * Generate IR from the condition of a loop
737     *
738     * This is factored out of ::hir because some loops have the condition
739     * test at the top (for and while), and others have it at the end (do-while).
740     */
741    void condition_to_hir(class ir_loop *, struct _mesa_glsl_parse_state *);
742 };
743
744
745 class ast_jump_statement : public ast_node {
746 public:
747    ast_jump_statement(int mode, ast_expression *return_value);
748    virtual void print(void) const;
749
750    virtual ir_rvalue *hir(exec_list *instructions,
751                           struct _mesa_glsl_parse_state *state);
752
753    enum ast_jump_modes {
754       ast_continue,
755       ast_break,
756       ast_return,
757       ast_discard
758    } mode;
759
760    ast_expression *opt_return_value;
761 };
762
763
764 class ast_function_definition : public ast_node {
765 public:
766    virtual void print(void) const;
767
768    virtual ir_rvalue *hir(exec_list *instructions,
769                           struct _mesa_glsl_parse_state *state);
770
771    ast_function *prototype;
772    ast_compound_statement *body;
773 };
774
775 class ast_uniform_block : public ast_node {
776 public:
777    ast_uniform_block(ast_type_qualifier layout,
778                      const char *block_name,
779                      ast_declarator_list *member_list)
780    : layout(layout), block_name(block_name)
781    {
782       declarations.push_degenerate_list_at_head(&member_list->link);
783    }
784
785    virtual ir_rvalue *hir(exec_list *instructions,
786                           struct _mesa_glsl_parse_state *state);
787
788    ast_type_qualifier layout;
789    const char *block_name;
790    /** List of ast_declarator_list * */
791    exec_list declarations;
792 };
793 /*@}*/
794
795 extern void
796 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
797
798 extern ir_rvalue *
799 _mesa_ast_field_selection_to_hir(const ast_expression *expr,
800                                  exec_list *instructions,
801                                  struct _mesa_glsl_parse_state *state);
802
803 void
804 emit_function(_mesa_glsl_parse_state *state, ir_function *f);
805
806 #endif /* AST_H */