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 DECLARE_RALLOC_CXX_OPERATORS(ast_node
);
55 * Print an AST node in something approximating the original GLSL code
57 virtual void print(void) const;
60 * Convert the AST node to the high-level intermediate representation
62 virtual ir_rvalue
*hir(exec_list
*instructions
,
63 struct _mesa_glsl_parse_state
*state
);
66 * Retrieve the source location of an AST node
68 * This function is primarily used to get the source position of an AST node
69 * into a form that can be passed to \c _mesa_glsl_error.
71 * \sa _mesa_glsl_error, ast_node::set_location
73 struct YYLTYPE
get_location(void) const
77 locp
.source
= this->location
.source
;
78 locp
.first_line
= this->location
.line
;
79 locp
.first_column
= this->location
.column
;
80 locp
.last_line
= locp
.first_line
;
81 locp
.last_column
= locp
.first_column
;
87 * Set the source location of an AST node from a parser location
89 * \sa ast_node::get_location
91 void set_location(const struct YYLTYPE
&locp
)
93 this->location
.source
= locp
.source
;
94 this->location
.line
= locp
.first_line
;
95 this->location
.column
= locp
.first_column
;
99 * Source location of the AST node.
102 unsigned source
; /**< GLSL source number. */
103 unsigned line
; /**< Line number within the source string. */
104 unsigned column
; /**< Column in the line. */
111 * The only constructor is protected so that only derived class objects can
119 * Operators for AST expression nodes.
123 ast_plus
, /**< Unary + operator. */
180 * Representation of any sort of expression.
182 class ast_expression
: public ast_node
{
184 ast_expression(int oper
, ast_expression
*,
185 ast_expression
*, ast_expression
*);
187 ast_expression(const char *identifier
) :
190 subexpressions
[0] = NULL
;
191 subexpressions
[1] = NULL
;
192 subexpressions
[2] = NULL
;
193 primary_expression
.identifier
= identifier
;
194 this->non_lvalue_description
= NULL
;
197 static const char *operator_string(enum ast_operators op
);
199 virtual ir_rvalue
*hir(exec_list
*instructions
,
200 struct _mesa_glsl_parse_state
*state
);
202 virtual void print(void) const;
204 enum ast_operators oper
;
206 ast_expression
*subexpressions
[3];
209 const char *identifier
;
211 float float_constant
;
212 unsigned uint_constant
;
214 } primary_expression
;
218 * List of expressions for an \c ast_sequence or parameters for an
219 * \c ast_function_call
221 exec_list expressions
;
224 * For things that can't be l-values, this describes what it is.
226 * This text is used by the code that generates IR for assignments to
227 * detect and emit useful messages for assignments to some things that
228 * can't be l-values. For example, pre- or post-incerement expressions.
231 * This pointer may be \c NULL.
233 const char *non_lvalue_description
;
236 class ast_expression_bin
: public ast_expression
{
238 ast_expression_bin(int oper
, ast_expression
*, ast_expression
*);
240 virtual void print(void) const;
244 * Subclass of expressions for function calls
246 class ast_function_expression
: public ast_expression
{
248 ast_function_expression(ast_expression
*callee
)
249 : ast_expression(ast_function_call
, callee
,
256 ast_function_expression(class ast_type_specifier
*type
)
257 : ast_expression(ast_function_call
, (ast_expression
*) type
,
264 bool is_constructor() const
269 virtual ir_rvalue
*hir(exec_list
*instructions
,
270 struct _mesa_glsl_parse_state
*state
);
274 * Is this function call actually a constructor?
280 * C-style aggregate initialization class
282 * Represents C-style initializers of vectors, matrices, arrays, and
283 * structures. E.g., vec3 pos = {1.0, 0.0, -1.0} is equivalent to
284 * vec3 pos = vec3(1.0, 0.0, -1.0).
286 * Specified in GLSL 4.20 and GL_ARB_shading_language_420pack.
288 * \sa _mesa_ast_set_aggregate_type
290 class ast_aggregate_initializer
: public ast_expression
{
292 ast_aggregate_initializer()
293 : ast_expression(ast_aggregate
, NULL
, NULL
, NULL
),
294 constructor_type(NULL
)
299 ast_type_specifier
*constructor_type
;
300 virtual ir_rvalue
*hir(exec_list
*instructions
,
301 struct _mesa_glsl_parse_state
*state
);
305 * Number of possible operators for an ast_expression
307 * This is done as a define instead of as an additional value in the enum so
308 * that the compiler won't generate spurious messages like "warning:
309 * enumeration value ‘ast_num_operators’ not handled in switch"
311 #define AST_NUM_OPERATORS (ast_sequence + 1)
314 class ast_compound_statement
: public ast_node
{
316 ast_compound_statement(int new_scope
, ast_node
*statements
);
317 virtual void print(void) const;
319 virtual ir_rvalue
*hir(exec_list
*instructions
,
320 struct _mesa_glsl_parse_state
*state
);
323 exec_list statements
;
326 class ast_declaration
: public ast_node
{
328 ast_declaration(const char *identifier
, bool is_array
, ast_expression
*array_size
,
329 ast_expression
*initializer
);
330 virtual void print(void) const;
332 const char *identifier
;
335 ast_expression
*array_size
;
337 ast_expression
*initializer
;
342 ast_precision_none
= 0, /**< Absence of precision qualifier. */
344 ast_precision_medium
,
348 struct ast_type_qualifier
{
349 DECLARE_RALLOC_CXX_OPERATORS(ast_type_qualifier
);
353 unsigned invariant
:1;
355 unsigned attribute
:1;
363 unsigned noperspective
:1;
365 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
367 unsigned origin_upper_left
:1;
368 unsigned pixel_center_integer
:1;
372 * Flag set if GL_ARB_explicit_attrib_location "location" layout
375 unsigned explicit_location
:1;
377 * Flag set if GL_ARB_explicit_attrib_location "index" layout
380 unsigned explicit_index
:1;
383 * Flag set if GL_ARB_shading_language_420pack "binding" layout
386 unsigned explicit_binding
:1;
389 * Flag set if GL_ARB_shader_atomic counter "offset" layout
392 unsigned explicit_offset
:1;
394 /** \name Layout qualifiers for GL_AMD_conservative_depth */
396 unsigned depth_any
:1;
397 unsigned depth_greater
:1;
398 unsigned depth_less
:1;
399 unsigned depth_unchanged
:1;
402 /** \name Layout qualifiers for GL_ARB_uniform_buffer_object */
407 unsigned column_major
:1;
408 unsigned row_major
:1;
411 /** \name Layout qualifiers for GLSL 1.50 geometry shaders */
413 unsigned prim_type
:1;
414 unsigned max_vertices
:1;
417 /** \brief Set of flags, accessed by name. */
420 /** \brief Set of flags, accessed as a bitmask. */
424 /** Precision of the type (highp/medium/lowp). */
425 unsigned precision
:2;
428 * Location specified via GL_ARB_explicit_attrib_location layout
431 * This field is only valid if \c explicit_location is set.
435 * Index specified via GL_ARB_explicit_attrib_location layout
438 * This field is only valid if \c explicit_index is set.
442 /** Maximum output vertices in GLSL 1.50 geometry shaders. */
445 /** Input or output primitive type in GLSL 1.50 geometry shaders */
449 * Binding specified via GL_ARB_shading_language_420pack's "binding" keyword.
452 * This field is only valid if \c explicit_binding is set.
457 * Offset specified via GL_ARB_shader_atomic_counter's "offset"
461 * This field is only valid if \c explicit_offset is set.
466 * Return true if and only if an interpolation qualifier is present.
468 bool has_interpolation() const;
471 * Return whether a layout qualifier is present.
473 bool has_layout() const;
476 * Return whether a storage qualifier is present.
478 bool has_storage() const;
481 * Return whether an auxiliary storage qualifier is present.
483 bool has_auxiliary_storage() const;
486 * \brief Return string representation of interpolation qualifier.
488 * If an interpolation qualifier is present, then return that qualifier's
489 * string representation. Otherwise, return null. For example, if the
490 * noperspective bit is set, then this returns "noperspective".
492 * If multiple interpolation qualifiers are somehow present, then the
493 * returned string is undefined but not null.
495 const char *interpolation_string() const;
497 bool merge_qualifier(YYLTYPE
*loc
,
498 _mesa_glsl_parse_state
*state
,
499 ast_type_qualifier q
);
502 class ast_declarator_list
;
504 class ast_struct_specifier
: public ast_node
{
507 * \brief Make a shallow copy of an ast_struct_specifier.
509 * Use only if the objects are allocated from the same context and will not
510 * be modified. Zeros the inherited ast_node's fields.
512 ast_struct_specifier(const ast_struct_specifier
& that
):
513 ast_node(), name(that
.name
), declarations(that
.declarations
),
514 is_declaration(that
.is_declaration
)
519 ast_struct_specifier(const char *identifier
,
520 ast_declarator_list
*declarator_list
);
521 virtual void print(void) const;
523 virtual ir_rvalue
*hir(exec_list
*instructions
,
524 struct _mesa_glsl_parse_state
*state
);
527 /* List of ast_declarator_list * */
528 exec_list declarations
;
534 class ast_type_specifier
: public ast_node
{
537 * \brief Make a shallow copy of an ast_type_specifier, specifying array
540 * Use only if the objects are allocated from the same context and will not
541 * be modified. Zeros the inherited ast_node's fields.
543 ast_type_specifier(const ast_type_specifier
*that
, bool is_array
,
544 ast_expression
*array_size
)
545 : ast_node(), type_name(that
->type_name
), structure(that
->structure
),
546 is_array(is_array
), array_size(array_size
),
547 default_precision(that
->default_precision
)
552 /** Construct a type specifier from a type name */
553 ast_type_specifier(const char *name
)
554 : type_name(name
), structure(NULL
),
555 is_array(false), array_size(NULL
),
556 default_precision(ast_precision_none
)
561 /** Construct a type specifier from a structure definition */
562 ast_type_specifier(ast_struct_specifier
*s
)
563 : type_name(s
->name
), structure(s
),
564 is_array(false), array_size(NULL
),
565 default_precision(ast_precision_none
)
570 const struct glsl_type
*glsl_type(const char **name
,
571 struct _mesa_glsl_parse_state
*state
)
574 virtual void print(void) const;
576 ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
578 const char *type_name
;
579 ast_struct_specifier
*structure
;
582 ast_expression
*array_size
;
584 /** For precision statements, this is the given precision; otherwise none. */
585 unsigned default_precision
:2;
589 class ast_fully_specified_type
: public ast_node
{
591 virtual void print(void) const;
592 bool has_qualifiers() const;
594 ast_fully_specified_type() : qualifier(), specifier(NULL
)
598 const struct glsl_type
*glsl_type(const char **name
,
599 struct _mesa_glsl_parse_state
*state
)
602 ast_type_qualifier qualifier
;
603 ast_type_specifier
*specifier
;
607 class ast_declarator_list
: public ast_node
{
609 ast_declarator_list(ast_fully_specified_type
*);
610 virtual void print(void) const;
612 virtual ir_rvalue
*hir(exec_list
*instructions
,
613 struct _mesa_glsl_parse_state
*state
);
615 ast_fully_specified_type
*type
;
616 /** List of 'ast_declaration *' */
617 exec_list declarations
;
620 * Special flag for vertex shader "invariant" declarations.
622 * Vertex shaders can contain "invariant" variable redeclarations that do
623 * not include a type. For example, "invariant gl_Position;". This flag
624 * is used to note these cases when no type is specified.
630 class ast_parameter_declarator
: public ast_node
{
632 ast_parameter_declarator() :
637 formal_parameter(false),
643 virtual void print(void) const;
645 virtual ir_rvalue
*hir(exec_list
*instructions
,
646 struct _mesa_glsl_parse_state
*state
);
648 ast_fully_specified_type
*type
;
649 const char *identifier
;
651 ast_expression
*array_size
;
653 static void parameters_to_hir(exec_list
*ast_parameters
,
654 bool formal
, exec_list
*ir_parameters
,
655 struct _mesa_glsl_parse_state
*state
);
658 /** Is this parameter declaration part of a formal parameter list? */
659 bool formal_parameter
;
662 * Is this parameter 'void' type?
664 * This field is set by \c ::hir.
670 class ast_function
: public ast_node
{
674 virtual void print(void) const;
676 virtual ir_rvalue
*hir(exec_list
*instructions
,
677 struct _mesa_glsl_parse_state
*state
);
679 ast_fully_specified_type
*return_type
;
680 const char *identifier
;
682 exec_list parameters
;
686 * Is this prototype part of the function definition?
688 * Used by ast_function_definition::hir to process the parameters, etc.
696 * Function signature corresponding to this function prototype instance
698 * Used by ast_function_definition::hir to process the parameters, etc.
703 class ir_function_signature
*signature
;
705 friend class ast_function_definition
;
709 class ast_expression_statement
: public ast_node
{
711 ast_expression_statement(ast_expression
*);
712 virtual void print(void) const;
714 virtual ir_rvalue
*hir(exec_list
*instructions
,
715 struct _mesa_glsl_parse_state
*state
);
717 ast_expression
*expression
;
721 class ast_case_label
: public ast_node
{
723 ast_case_label(ast_expression
*test_value
);
724 virtual void print(void) const;
726 virtual ir_rvalue
*hir(exec_list
*instructions
,
727 struct _mesa_glsl_parse_state
*state
);
730 * An test value of NULL means 'default'.
732 ast_expression
*test_value
;
736 class ast_case_label_list
: public ast_node
{
738 ast_case_label_list(void);
739 virtual void print(void) const;
741 virtual ir_rvalue
*hir(exec_list
*instructions
,
742 struct _mesa_glsl_parse_state
*state
);
745 * A list of case labels.
751 class ast_case_statement
: public ast_node
{
753 ast_case_statement(ast_case_label_list
*labels
);
754 virtual void print(void) const;
756 virtual ir_rvalue
*hir(exec_list
*instructions
,
757 struct _mesa_glsl_parse_state
*state
);
759 ast_case_label_list
*labels
;
762 * A list of statements.
768 class ast_case_statement_list
: public ast_node
{
770 ast_case_statement_list(void);
771 virtual void print(void) const;
773 virtual ir_rvalue
*hir(exec_list
*instructions
,
774 struct _mesa_glsl_parse_state
*state
);
783 class ast_switch_body
: public ast_node
{
785 ast_switch_body(ast_case_statement_list
*stmts
);
786 virtual void print(void) const;
788 virtual ir_rvalue
*hir(exec_list
*instructions
,
789 struct _mesa_glsl_parse_state
*state
);
791 ast_case_statement_list
*stmts
;
795 class ast_selection_statement
: public ast_node
{
797 ast_selection_statement(ast_expression
*condition
,
798 ast_node
*then_statement
,
799 ast_node
*else_statement
);
800 virtual void print(void) const;
802 virtual ir_rvalue
*hir(exec_list
*instructions
,
803 struct _mesa_glsl_parse_state
*state
);
805 ast_expression
*condition
;
806 ast_node
*then_statement
;
807 ast_node
*else_statement
;
811 class ast_switch_statement
: public ast_node
{
813 ast_switch_statement(ast_expression
*test_expression
,
815 virtual void print(void) const;
817 virtual ir_rvalue
*hir(exec_list
*instructions
,
818 struct _mesa_glsl_parse_state
*state
);
820 ast_expression
*test_expression
;
824 void test_to_hir(exec_list
*, struct _mesa_glsl_parse_state
*);
827 class ast_iteration_statement
: public ast_node
{
829 ast_iteration_statement(int mode
, ast_node
*init
, ast_node
*condition
,
830 ast_expression
*rest_expression
, ast_node
*body
);
832 virtual void print(void) const;
834 virtual ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
836 enum ast_iteration_modes
{
843 ast_node
*init_statement
;
845 ast_expression
*rest_expression
;
851 * Generate IR from the condition of a loop
853 * This is factored out of ::hir because some loops have the condition
854 * test at the top (for and while), and others have it at the end (do-while).
856 void condition_to_hir(class ir_loop
*, struct _mesa_glsl_parse_state
*);
860 class ast_jump_statement
: public ast_node
{
862 ast_jump_statement(int mode
, ast_expression
*return_value
);
863 virtual void print(void) const;
865 virtual ir_rvalue
*hir(exec_list
*instructions
,
866 struct _mesa_glsl_parse_state
*state
);
868 enum ast_jump_modes
{
875 ast_expression
*opt_return_value
;
879 class ast_function_definition
: public ast_node
{
881 ast_function_definition() : prototype(NULL
), body(NULL
)
885 virtual void print(void) const;
887 virtual ir_rvalue
*hir(exec_list
*instructions
,
888 struct _mesa_glsl_parse_state
*state
);
890 ast_function
*prototype
;
891 ast_compound_statement
*body
;
894 class ast_interface_block
: public ast_node
{
896 ast_interface_block(ast_type_qualifier layout
,
897 const char *instance_name
,
899 ast_expression
*array_size
)
900 : layout(layout
), block_name(NULL
), instance_name(instance_name
),
901 is_array(is_array
), array_size(array_size
)
904 assert(array_size
== NULL
);
907 virtual ir_rvalue
*hir(exec_list
*instructions
,
908 struct _mesa_glsl_parse_state
*state
);
910 ast_type_qualifier layout
;
911 const char *block_name
;
914 * Declared name of the block instance, if specified.
916 * If the block does not have an instance name, this field will be
919 const char *instance_name
;
921 /** List of ast_declarator_list * */
922 exec_list declarations
;
925 * True if the block is declared as an array
928 * A block can only be an array if it also has an instance name. If this
929 * field is true, ::instance_name must also not be \c NULL.
934 * Declared array size of the block instance
936 * If the block is not declared as an array or if the block instance array
937 * is unsized, this field will be \c NULL.
939 ast_expression
*array_size
;
944 * AST node representing a declaration of the input layout for geometry
947 class ast_gs_input_layout
: public ast_node
950 ast_gs_input_layout(const struct YYLTYPE
&locp
, GLenum prim_type
)
951 : prim_type(prim_type
)
956 virtual ir_rvalue
*hir(exec_list
*instructions
,
957 struct _mesa_glsl_parse_state
*state
);
960 const GLenum prim_type
;
966 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
);
969 _mesa_ast_field_selection_to_hir(const ast_expression
*expr
,
970 exec_list
*instructions
,
971 struct _mesa_glsl_parse_state
*state
);
974 _mesa_ast_array_index_to_hir(void *mem_ctx
,
975 struct _mesa_glsl_parse_state
*state
,
976 ir_rvalue
*array
, ir_rvalue
*idx
,
977 YYLTYPE
&loc
, YYLTYPE
&idx_loc
);
980 _mesa_ast_set_aggregate_type(const ast_type_specifier
*type
,
981 ast_expression
*expr
,
982 _mesa_glsl_parse_state
*state
);
985 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
);
988 check_builtin_array_max_size(const char *name
, unsigned size
,
989 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
);