3 * Copyright © 2009 Intel Corporation
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:
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
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.
30 #include "glsl_parser_extras.h"
32 struct _mesa_glsl_parse_state
;
37 * \defgroup AST Abstract syntax tree node definitions
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.
48 * Base class of all abstract syntax tree nodes
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
)
58 node
= rzalloc_size(ctx
, size
);
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
)
72 * Print an AST node in something approximating the original GLSL code
74 virtual void print(void) const;
77 * Convert the AST node to the high-level intermediate representation
79 virtual ir_rvalue
*hir(exec_list
*instructions
,
80 struct _mesa_glsl_parse_state
*state
);
83 * Retrieve the source location of an AST node
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.
88 * \sa _mesa_glsl_error, ast_node::set_location
90 struct YYLTYPE
get_location(void) const
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
;
104 * Set the source location of an AST node from a parser location
106 * \sa ast_node::get_location
108 void set_location(const struct YYLTYPE
&locp
)
110 this->location
.source
= locp
.source
;
111 this->location
.line
= locp
.first_line
;
112 this->location
.column
= locp
.first_column
;
116 * Source location of the AST node.
119 unsigned source
; /**< GLSL source number. */
120 unsigned line
; /**< Line number within the source string. */
121 unsigned column
; /**< Column in the line. */
128 * The only constructor is protected so that only derived class objects can
136 * Operators for AST expression nodes.
140 ast_plus
, /**< Unary + operator. */
197 * Representation of any sort of expression.
199 class ast_expression
: public ast_node
{
201 ast_expression(int oper
, ast_expression
*,
202 ast_expression
*, ast_expression
*);
204 ast_expression(const char *identifier
) :
207 subexpressions
[0] = NULL
;
208 subexpressions
[1] = NULL
;
209 subexpressions
[2] = NULL
;
210 primary_expression
.identifier
= identifier
;
211 this->non_lvalue_description
= NULL
;
214 static const char *operator_string(enum ast_operators op
);
216 virtual ir_rvalue
*hir(exec_list
*instructions
,
217 struct _mesa_glsl_parse_state
*state
);
219 virtual void print(void) const;
221 enum ast_operators oper
;
223 ast_expression
*subexpressions
[3];
226 const char *identifier
;
228 float float_constant
;
229 unsigned uint_constant
;
231 } primary_expression
;
235 * List of expressions for an \c ast_sequence or parameters for an
236 * \c ast_function_call
238 exec_list expressions
;
241 * For things that can't be l-values, this describes what it is.
243 * This text is used by the code that generates IR for assignments to
244 * detect and emit useful messages for assignments to some things that
245 * can't be l-values. For example, pre- or post-incerement expressions.
248 * This pointer may be \c NULL.
250 const char *non_lvalue_description
;
253 class ast_expression_bin
: public ast_expression
{
255 ast_expression_bin(int oper
, ast_expression
*, ast_expression
*);
257 virtual void print(void) const;
261 * Subclass of expressions for function calls
263 class ast_function_expression
: public ast_expression
{
265 ast_function_expression(ast_expression
*callee
)
266 : ast_expression(ast_function_call
, callee
,
273 ast_function_expression(class ast_type_specifier
*type
)
274 : ast_expression(ast_function_call
, (ast_expression
*) type
,
281 bool is_constructor() const
286 virtual ir_rvalue
*hir(exec_list
*instructions
,
287 struct _mesa_glsl_parse_state
*state
);
291 * Is this function call actually a constructor?
297 * C-style aggregate initialization class
299 * Represents C-style initializers of vectors, matrices, arrays, and
300 * structures. E.g., vec3 pos = {1.0, 0.0, -1.0} is equivalent to
301 * vec3 pos = vec3(1.0, 0.0, -1.0).
303 * Specified in GLSL 4.20 and GL_ARB_shading_language_420pack.
305 * \sa _mesa_ast_set_aggregate_type
307 class ast_aggregate_initializer
: public ast_expression
{
309 ast_aggregate_initializer()
310 : ast_expression(ast_aggregate
, NULL
, NULL
, NULL
),
311 constructor_type(NULL
)
316 ast_type_specifier
*constructor_type
;
317 virtual ir_rvalue
*hir(exec_list
*instructions
,
318 struct _mesa_glsl_parse_state
*state
);
322 * Number of possible operators for an ast_expression
324 * This is done as a define instead of as an additional value in the enum so
325 * that the compiler won't generate spurious messages like "warning:
326 * enumeration value ‘ast_num_operators’ not handled in switch"
328 #define AST_NUM_OPERATORS (ast_sequence + 1)
331 class ast_compound_statement
: public ast_node
{
333 ast_compound_statement(int new_scope
, ast_node
*statements
);
334 virtual void print(void) const;
336 virtual ir_rvalue
*hir(exec_list
*instructions
,
337 struct _mesa_glsl_parse_state
*state
);
340 exec_list statements
;
343 class ast_declaration
: public ast_node
{
345 ast_declaration(const char *identifier
, bool is_array
, ast_expression
*array_size
,
346 ast_expression
*initializer
);
347 virtual void print(void) const;
349 const char *identifier
;
352 ast_expression
*array_size
;
354 ast_expression
*initializer
;
359 ast_precision_none
= 0, /**< Absence of precision qualifier. */
361 ast_precision_medium
,
365 struct ast_type_qualifier
{
366 /* Callers of this ralloc-based new need not call delete. It's
367 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
368 static void* operator new(size_t size
, void *ctx
)
372 node
= rzalloc_size(ctx
, size
);
373 assert(node
!= NULL
);
378 /* If the user *does* call delete, that's OK, we will just
379 * ralloc_free in that case. */
380 static void operator delete(void *table
)
387 unsigned invariant
:1;
389 unsigned attribute
:1;
397 unsigned noperspective
:1;
399 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
401 unsigned origin_upper_left
:1;
402 unsigned pixel_center_integer
:1;
406 * Flag set if GL_ARB_explicit_attrib_location "location" layout
409 unsigned explicit_location
:1;
411 * Flag set if GL_ARB_explicit_attrib_location "index" layout
414 unsigned explicit_index
:1;
417 * Flag set if GL_ARB_shading_language_420pack "binding" layout
420 unsigned explicit_binding
:1;
422 /** \name Layout qualifiers for GL_AMD_conservative_depth */
424 unsigned depth_any
:1;
425 unsigned depth_greater
:1;
426 unsigned depth_less
:1;
427 unsigned depth_unchanged
:1;
430 /** \name Layout qualifiers for GL_ARB_uniform_buffer_object */
435 unsigned column_major
:1;
436 unsigned row_major
:1;
439 /** \name Layout qualifiers for GLSL 1.50 geometry shaders */
441 unsigned prim_type
:1;
442 unsigned max_vertices
:1;
445 /** \brief Set of flags, accessed by name. */
448 /** \brief Set of flags, accessed as a bitmask. */
452 /** Precision of the type (highp/medium/lowp). */
453 unsigned precision
:2;
456 * Location specified via GL_ARB_explicit_attrib_location layout
459 * This field is only valid if \c explicit_location is set.
463 * Index specified via GL_ARB_explicit_attrib_location layout
466 * This field is only valid if \c explicit_index is set.
470 /** Maximum output vertices in GLSL 1.50 geometry shaders. */
473 /** Input or output primitive type in GLSL 1.50 geometry shaders */
477 * Binding specified via GL_ARB_shading_language_420pack's "binding" keyword.
480 * This field is only valid if \c explicit_binding is set.
485 * Return true if and only if an interpolation qualifier is present.
487 bool has_interpolation() const;
490 * Return whether a layout qualifier is present.
492 bool has_layout() const;
495 * Return whether a storage qualifier is present.
497 bool has_storage() const;
500 * Return whether an auxiliary storage qualifier is present.
502 bool has_auxiliary_storage() const;
505 * \brief Return string representation of interpolation qualifier.
507 * If an interpolation qualifier is present, then return that qualifier's
508 * string representation. Otherwise, return null. For example, if the
509 * noperspective bit is set, then this returns "noperspective".
511 * If multiple interpolation qualifiers are somehow present, then the
512 * returned string is undefined but not null.
514 const char *interpolation_string() const;
516 bool merge_qualifier(YYLTYPE
*loc
,
517 _mesa_glsl_parse_state
*state
,
518 ast_type_qualifier q
);
521 class ast_declarator_list
;
523 class ast_struct_specifier
: public ast_node
{
526 * \brief Make a shallow copy of an ast_struct_specifier.
528 * Use only if the objects are allocated from the same context and will not
529 * be modified. Zeros the inherited ast_node's fields.
531 ast_struct_specifier(const ast_struct_specifier
& that
):
532 ast_node(), name(that
.name
), declarations(that
.declarations
),
533 is_declaration(that
.is_declaration
)
538 ast_struct_specifier(const char *identifier
,
539 ast_declarator_list
*declarator_list
);
540 virtual void print(void) const;
542 virtual ir_rvalue
*hir(exec_list
*instructions
,
543 struct _mesa_glsl_parse_state
*state
);
546 /* List of ast_declarator_list * */
547 exec_list declarations
;
553 class ast_type_specifier
: public ast_node
{
556 * \brief Make a shallow copy of an ast_type_specifier, specifying array
559 * Use only if the objects are allocated from the same context and will not
560 * be modified. Zeros the inherited ast_node's fields.
562 ast_type_specifier(const ast_type_specifier
*that
, bool is_array
,
563 ast_expression
*array_size
)
564 : ast_node(), type_name(that
->type_name
), structure(that
->structure
),
565 is_array(is_array
), array_size(array_size
),
566 default_precision(that
->default_precision
)
571 /** Construct a type specifier from a type name */
572 ast_type_specifier(const char *name
)
573 : type_name(name
), structure(NULL
),
574 is_array(false), array_size(NULL
),
575 default_precision(ast_precision_none
)
580 /** Construct a type specifier from a structure definition */
581 ast_type_specifier(ast_struct_specifier
*s
)
582 : type_name(s
->name
), structure(s
),
583 is_array(false), array_size(NULL
),
584 default_precision(ast_precision_none
)
589 const struct glsl_type
*glsl_type(const char **name
,
590 struct _mesa_glsl_parse_state
*state
)
593 virtual void print(void) const;
595 ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
597 const char *type_name
;
598 ast_struct_specifier
*structure
;
601 ast_expression
*array_size
;
603 /** For precision statements, this is the given precision; otherwise none. */
604 unsigned default_precision
:2;
608 class ast_fully_specified_type
: public ast_node
{
610 virtual void print(void) const;
611 bool has_qualifiers() const;
613 ast_type_qualifier qualifier
;
614 ast_type_specifier
*specifier
;
618 class ast_declarator_list
: public ast_node
{
620 ast_declarator_list(ast_fully_specified_type
*);
621 virtual void print(void) const;
623 virtual ir_rvalue
*hir(exec_list
*instructions
,
624 struct _mesa_glsl_parse_state
*state
);
626 ast_fully_specified_type
*type
;
627 /** List of 'ast_declaration *' */
628 exec_list declarations
;
631 * Special flag for vertex shader "invariant" declarations.
633 * Vertex shaders can contain "invariant" variable redeclarations that do
634 * not include a type. For example, "invariant gl_Position;". This flag
635 * is used to note these cases when no type is specified.
640 * Flag indicating that these declarators are in a uniform block,
641 * allowing UBO type qualifiers.
643 bool ubo_qualifiers_valid
;
647 class ast_parameter_declarator
: public ast_node
{
649 ast_parameter_declarator() :
654 formal_parameter(false),
660 virtual void print(void) const;
662 virtual ir_rvalue
*hir(exec_list
*instructions
,
663 struct _mesa_glsl_parse_state
*state
);
665 ast_fully_specified_type
*type
;
666 const char *identifier
;
668 ast_expression
*array_size
;
670 static void parameters_to_hir(exec_list
*ast_parameters
,
671 bool formal
, exec_list
*ir_parameters
,
672 struct _mesa_glsl_parse_state
*state
);
675 /** Is this parameter declaration part of a formal parameter list? */
676 bool formal_parameter
;
679 * Is this parameter 'void' type?
681 * This field is set by \c ::hir.
687 class ast_function
: public ast_node
{
691 virtual void print(void) const;
693 virtual ir_rvalue
*hir(exec_list
*instructions
,
694 struct _mesa_glsl_parse_state
*state
);
696 ast_fully_specified_type
*return_type
;
697 const char *identifier
;
699 exec_list parameters
;
703 * Is this prototype part of the function definition?
705 * Used by ast_function_definition::hir to process the parameters, etc.
713 * Function signature corresponding to this function prototype instance
715 * Used by ast_function_definition::hir to process the parameters, etc.
720 class ir_function_signature
*signature
;
722 friend class ast_function_definition
;
726 class ast_expression_statement
: public ast_node
{
728 ast_expression_statement(ast_expression
*);
729 virtual void print(void) const;
731 virtual ir_rvalue
*hir(exec_list
*instructions
,
732 struct _mesa_glsl_parse_state
*state
);
734 ast_expression
*expression
;
738 class ast_case_label
: public ast_node
{
740 ast_case_label(ast_expression
*test_value
);
741 virtual void print(void) const;
743 virtual ir_rvalue
*hir(exec_list
*instructions
,
744 struct _mesa_glsl_parse_state
*state
);
747 * An test value of NULL means 'default'.
749 ast_expression
*test_value
;
753 class ast_case_label_list
: public ast_node
{
755 ast_case_label_list(void);
756 virtual void print(void) const;
758 virtual ir_rvalue
*hir(exec_list
*instructions
,
759 struct _mesa_glsl_parse_state
*state
);
762 * A list of case labels.
768 class ast_case_statement
: public ast_node
{
770 ast_case_statement(ast_case_label_list
*labels
);
771 virtual void print(void) const;
773 virtual ir_rvalue
*hir(exec_list
*instructions
,
774 struct _mesa_glsl_parse_state
*state
);
776 ast_case_label_list
*labels
;
779 * A list of statements.
785 class ast_case_statement_list
: public ast_node
{
787 ast_case_statement_list(void);
788 virtual void print(void) const;
790 virtual ir_rvalue
*hir(exec_list
*instructions
,
791 struct _mesa_glsl_parse_state
*state
);
800 class ast_switch_body
: public ast_node
{
802 ast_switch_body(ast_case_statement_list
*stmts
);
803 virtual void print(void) const;
805 virtual ir_rvalue
*hir(exec_list
*instructions
,
806 struct _mesa_glsl_parse_state
*state
);
808 ast_case_statement_list
*stmts
;
812 class ast_selection_statement
: public ast_node
{
814 ast_selection_statement(ast_expression
*condition
,
815 ast_node
*then_statement
,
816 ast_node
*else_statement
);
817 virtual void print(void) const;
819 virtual ir_rvalue
*hir(exec_list
*instructions
,
820 struct _mesa_glsl_parse_state
*state
);
822 ast_expression
*condition
;
823 ast_node
*then_statement
;
824 ast_node
*else_statement
;
828 class ast_switch_statement
: public ast_node
{
830 ast_switch_statement(ast_expression
*test_expression
,
832 virtual void print(void) const;
834 virtual ir_rvalue
*hir(exec_list
*instructions
,
835 struct _mesa_glsl_parse_state
*state
);
837 ast_expression
*test_expression
;
841 void test_to_hir(exec_list
*, struct _mesa_glsl_parse_state
*);
844 class ast_iteration_statement
: public ast_node
{
846 ast_iteration_statement(int mode
, ast_node
*init
, ast_node
*condition
,
847 ast_expression
*rest_expression
, ast_node
*body
);
849 virtual void print(void) const;
851 virtual ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
853 enum ast_iteration_modes
{
860 ast_node
*init_statement
;
862 ast_expression
*rest_expression
;
868 * Generate IR from the condition of a loop
870 * This is factored out of ::hir because some loops have the condition
871 * test at the top (for and while), and others have it at the end (do-while).
873 void condition_to_hir(class ir_loop
*, struct _mesa_glsl_parse_state
*);
877 class ast_jump_statement
: public ast_node
{
879 ast_jump_statement(int mode
, ast_expression
*return_value
);
880 virtual void print(void) const;
882 virtual ir_rvalue
*hir(exec_list
*instructions
,
883 struct _mesa_glsl_parse_state
*state
);
885 enum ast_jump_modes
{
892 ast_expression
*opt_return_value
;
896 class ast_function_definition
: public ast_node
{
898 virtual void print(void) const;
900 virtual ir_rvalue
*hir(exec_list
*instructions
,
901 struct _mesa_glsl_parse_state
*state
);
903 ast_function
*prototype
;
904 ast_compound_statement
*body
;
907 class ast_interface_block
: public ast_node
{
909 ast_interface_block(ast_type_qualifier layout
,
910 const char *instance_name
,
912 ast_expression
*array_size
)
913 : layout(layout
), block_name(NULL
), instance_name(instance_name
),
914 is_array(is_array
), array_size(array_size
)
917 assert(array_size
== NULL
);
920 virtual ir_rvalue
*hir(exec_list
*instructions
,
921 struct _mesa_glsl_parse_state
*state
);
923 ast_type_qualifier layout
;
924 const char *block_name
;
927 * Declared name of the block instance, if specified.
929 * If the block does not have an instance name, this field will be
932 const char *instance_name
;
934 /** List of ast_declarator_list * */
935 exec_list declarations
;
938 * True if the block is declared as an array
941 * A block can only be an array if it also has an instance name. If this
942 * field is true, ::instance_name must also not be \c NULL.
947 * Declared array size of the block instance
949 * If the block is not declared as an array or if the block instance array
950 * is unsized, this field will be \c NULL.
952 ast_expression
*array_size
;
957 * AST node representing a declaration of the input layout for geometry
960 class ast_gs_input_layout
: public ast_node
963 ast_gs_input_layout(const struct YYLTYPE
&locp
, GLenum prim_type
)
964 : prim_type(prim_type
)
969 virtual ir_rvalue
*hir(exec_list
*instructions
,
970 struct _mesa_glsl_parse_state
*state
);
973 const GLenum prim_type
;
979 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
);
982 _mesa_ast_field_selection_to_hir(const ast_expression
*expr
,
983 exec_list
*instructions
,
984 struct _mesa_glsl_parse_state
*state
);
987 _mesa_ast_array_index_to_hir(void *mem_ctx
,
988 struct _mesa_glsl_parse_state
*state
,
989 ir_rvalue
*array
, ir_rvalue
*idx
,
990 YYLTYPE
&loc
, YYLTYPE
&idx_loc
);
993 _mesa_ast_set_aggregate_type(const ast_type_specifier
*type
,
994 ast_expression
*expr
,
995 _mesa_glsl_parse_state
*state
);
998 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
);
1001 check_builtin_array_max_size(const char *name
, unsigned size
,
1002 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
);