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
.first_line
;
79 locp
.first_column
= this->location
.first_column
;
80 locp
.last_line
= this->location
.last_line
;
81 locp
.last_column
= this->location
.last_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
.first_line
= locp
.first_line
;
95 this->location
.first_column
= locp
.first_column
;
96 this->location
.last_line
= locp
.last_line
;
97 this->location
.last_column
= locp
.last_column
;
101 * Set the source location range of an AST node using two location nodes
103 * \sa ast_node::set_location
105 void set_location_range(const struct YYLTYPE
&begin
, const struct YYLTYPE
&end
)
107 this->location
.source
= begin
.source
;
108 this->location
.first_line
= begin
.first_line
;
109 this->location
.last_line
= end
.last_line
;
110 this->location
.first_column
= begin
.first_column
;
111 this->location
.last_column
= end
.last_column
;
115 * Source location of the AST node.
118 unsigned source
; /**< GLSL source number. */
119 unsigned first_line
; /**< First line number within the source string. */
120 unsigned first_column
; /**< First column in the first line. */
121 unsigned last_line
; /**< Last line number within the source string. */
122 unsigned last_column
; /**< Last column in the last line. */
129 * The only constructor is protected so that only derived class objects can
137 * Operators for AST expression nodes.
141 ast_plus
, /**< Unary + operator. */
199 * Representation of any sort of expression.
201 class ast_expression
: public ast_node
{
203 ast_expression(int oper
, ast_expression
*,
204 ast_expression
*, ast_expression
*);
206 ast_expression(const char *identifier
) :
209 subexpressions
[0] = NULL
;
210 subexpressions
[1] = NULL
;
211 subexpressions
[2] = NULL
;
212 primary_expression
.identifier
= identifier
;
213 this->non_lvalue_description
= NULL
;
216 static const char *operator_string(enum ast_operators op
);
218 virtual ir_rvalue
*hir(exec_list
*instructions
,
219 struct _mesa_glsl_parse_state
*state
);
221 virtual void hir_no_rvalue(exec_list
*instructions
,
222 struct _mesa_glsl_parse_state
*state
);
224 ir_rvalue
*do_hir(exec_list
*instructions
,
225 struct _mesa_glsl_parse_state
*state
,
228 virtual void print(void) const;
230 enum ast_operators oper
;
232 ast_expression
*subexpressions
[3];
235 const char *identifier
;
237 float float_constant
;
238 unsigned uint_constant
;
240 double double_constant
;
241 } primary_expression
;
245 * List of expressions for an \c ast_sequence or parameters for an
246 * \c ast_function_call
248 exec_list expressions
;
251 * For things that can't be l-values, this describes what it is.
253 * This text is used by the code that generates IR for assignments to
254 * detect and emit useful messages for assignments to some things that
255 * can't be l-values. For example, pre- or post-incerement expressions.
258 * This pointer may be \c NULL.
260 const char *non_lvalue_description
;
263 class ast_expression_bin
: public ast_expression
{
265 ast_expression_bin(int oper
, ast_expression
*, ast_expression
*);
267 virtual void print(void) const;
271 * Subclass of expressions for function calls
273 class ast_function_expression
: public ast_expression
{
275 ast_function_expression(ast_expression
*callee
)
276 : ast_expression(ast_function_call
, callee
,
283 ast_function_expression(class ast_type_specifier
*type
)
284 : ast_expression(ast_function_call
, (ast_expression
*) type
,
291 bool is_constructor() const
296 virtual ir_rvalue
*hir(exec_list
*instructions
,
297 struct _mesa_glsl_parse_state
*state
);
299 virtual void hir_no_rvalue(exec_list
*instructions
,
300 struct _mesa_glsl_parse_state
*state
);
304 * Is this function call actually a constructor?
309 class ast_array_specifier
: public ast_node
{
311 /** Unsized array specifier ([]) */
312 explicit ast_array_specifier(const struct YYLTYPE
&locp
)
313 : is_unsized_array(true)
318 /** Sized array specifier ([dim]) */
319 ast_array_specifier(const struct YYLTYPE
&locp
, ast_expression
*dim
)
320 : is_unsized_array(false)
323 array_dimensions
.push_tail(&dim
->link
);
326 void add_dimension(ast_expression
*dim
)
328 array_dimensions
.push_tail(&dim
->link
);
331 virtual void print(void) const;
333 /* If true, this means that the array has an unsized outermost dimension. */
334 bool is_unsized_array
;
336 /* This list contains objects of type ast_node containing the
337 * sized dimensions only, in outermost-to-innermost order.
339 exec_list array_dimensions
;
343 * C-style aggregate initialization class
345 * Represents C-style initializers of vectors, matrices, arrays, and
346 * structures. E.g., vec3 pos = {1.0, 0.0, -1.0} is equivalent to
347 * vec3 pos = vec3(1.0, 0.0, -1.0).
349 * Specified in GLSL 4.20 and GL_ARB_shading_language_420pack.
351 * \sa _mesa_ast_set_aggregate_type
353 class ast_aggregate_initializer
: public ast_expression
{
355 ast_aggregate_initializer()
356 : ast_expression(ast_aggregate
, NULL
, NULL
, NULL
),
357 constructor_type(NULL
)
363 * glsl_type of the aggregate, which is inferred from the LHS of whatever
364 * the aggregate is being used to initialize. This can't be inferred at
365 * parse time (since the parser deals with ast_type_specifiers, not
366 * glsl_types), so the parser leaves it NULL. However, the ast-to-hir
367 * conversion code makes sure to fill it in with the appropriate type
368 * before hir() is called.
370 const glsl_type
*constructor_type
;
372 virtual ir_rvalue
*hir(exec_list
*instructions
,
373 struct _mesa_glsl_parse_state
*state
);
375 virtual void hir_no_rvalue(exec_list
*instructions
,
376 struct _mesa_glsl_parse_state
*state
);
380 * Number of possible operators for an ast_expression
382 * This is done as a define instead of as an additional value in the enum so
383 * that the compiler won't generate spurious messages like "warning:
384 * enumeration value ‘ast_num_operators’ not handled in switch"
386 #define AST_NUM_OPERATORS (ast_sequence + 1)
389 class ast_compound_statement
: public ast_node
{
391 ast_compound_statement(int new_scope
, ast_node
*statements
);
392 virtual void print(void) const;
394 virtual ir_rvalue
*hir(exec_list
*instructions
,
395 struct _mesa_glsl_parse_state
*state
);
398 exec_list statements
;
401 class ast_declaration
: public ast_node
{
403 ast_declaration(const char *identifier
,
404 ast_array_specifier
*array_specifier
,
405 ast_expression
*initializer
);
406 virtual void print(void) const;
408 const char *identifier
;
410 ast_array_specifier
*array_specifier
;
412 ast_expression
*initializer
;
417 ast_precision_none
= 0, /**< Absence of precision qualifier. */
419 ast_precision_medium
,
423 struct ast_type_qualifier
{
424 DECLARE_RALLOC_CXX_OPERATORS(ast_type_qualifier
);
428 unsigned invariant
:1;
431 unsigned attribute
:1;
441 unsigned noperspective
:1;
443 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
445 unsigned origin_upper_left
:1;
446 unsigned pixel_center_integer
:1;
450 * Flag set if GL_ARB_explicit_attrib_location "location" layout
453 unsigned explicit_location
:1;
455 * Flag set if GL_ARB_explicit_attrib_location "index" layout
458 unsigned explicit_index
:1;
461 * Flag set if GL_ARB_shading_language_420pack "binding" layout
464 unsigned explicit_binding
:1;
467 * Flag set if GL_ARB_shader_atomic counter "offset" layout
470 unsigned explicit_offset
:1;
472 /** \name Layout qualifiers for GL_AMD_conservative_depth */
474 unsigned depth_any
:1;
475 unsigned depth_greater
:1;
476 unsigned depth_less
:1;
477 unsigned depth_unchanged
:1;
480 /** \name Layout qualifiers for GL_ARB_uniform_buffer_object */
485 unsigned column_major
:1;
486 unsigned row_major
:1;
489 /** \name Layout qualifiers for GLSL 1.50 geometry shaders */
491 unsigned prim_type
:1;
492 unsigned max_vertices
:1;
496 * local_size_{x,y,z} flags for compute shaders. Bit 0 represents
497 * local_size_x, and so on.
499 unsigned local_size
:3;
501 /** \name Layout and memory qualifiers for ARB_shader_image_load_store. */
503 unsigned early_fragment_tests
:1;
504 unsigned explicit_image_format
:1;
506 unsigned _volatile
:1;
507 unsigned restrict_flag
:1;
508 unsigned read_only
:1; /**< "readonly" qualifier. */
509 unsigned write_only
:1; /**< "writeonly" qualifier. */
512 /** \name Layout qualifiers for GL_ARB_gpu_shader5 */
514 unsigned invocations
:1;
515 unsigned stream
:1; /**< Has stream value assigned */
516 unsigned explicit_stream
:1; /**< stream value assigned explicitly by shader code */
519 /** \brief Set of flags, accessed by name. */
522 /** \brief Set of flags, accessed as a bitmask. */
526 /** Precision of the type (highp/medium/lowp). */
527 unsigned precision
:2;
529 /** Geometry shader invocations for GL_ARB_gpu_shader5. */
533 * Location specified via GL_ARB_explicit_attrib_location layout
536 * This field is only valid if \c explicit_location is set.
540 * Index specified via GL_ARB_explicit_attrib_location layout
543 * This field is only valid if \c explicit_index is set.
547 /** Maximum output vertices in GLSL 1.50 geometry shaders. */
550 /** Stream in GLSL 1.50 geometry shaders. */
553 /** Input or output primitive type in GLSL 1.50 geometry shaders */
557 * Binding specified via GL_ARB_shading_language_420pack's "binding" keyword.
560 * This field is only valid if \c explicit_binding is set.
565 * Offset specified via GL_ARB_shader_atomic_counter's "offset"
569 * This field is only valid if \c explicit_offset is set.
574 * Local size specified via GL_ARB_compute_shader's "local_size_{x,y,z}"
575 * layout qualifier. Element i of this array is only valid if
576 * flags.q.local_size & (1 << i) is set.
581 * Image format specified with an ARB_shader_image_load_store
585 * This field is only valid if \c explicit_image_format is set.
590 * Base type of the data read from or written to this image. Only
591 * the following enumerants are allowed: GLSL_TYPE_UINT,
592 * GLSL_TYPE_INT, GLSL_TYPE_FLOAT.
595 * This field is only valid if \c explicit_image_format is set.
597 glsl_base_type image_base_type
;
600 * Return true if and only if an interpolation qualifier is present.
602 bool has_interpolation() const;
605 * Return whether a layout qualifier is present.
607 bool has_layout() const;
610 * Return whether a storage qualifier is present.
612 bool has_storage() const;
615 * Return whether an auxiliary storage qualifier is present.
617 bool has_auxiliary_storage() const;
620 * \brief Return string representation of interpolation qualifier.
622 * If an interpolation qualifier is present, then return that qualifier's
623 * string representation. Otherwise, return null. For example, if the
624 * noperspective bit is set, then this returns "noperspective".
626 * If multiple interpolation qualifiers are somehow present, then the
627 * returned string is undefined but not null.
629 const char *interpolation_string() const;
631 bool merge_qualifier(YYLTYPE
*loc
,
632 _mesa_glsl_parse_state
*state
,
633 ast_type_qualifier q
);
635 bool merge_in_qualifier(YYLTYPE
*loc
,
636 _mesa_glsl_parse_state
*state
,
637 ast_type_qualifier q
,
642 class ast_declarator_list
;
644 class ast_struct_specifier
: public ast_node
{
646 ast_struct_specifier(const char *identifier
,
647 ast_declarator_list
*declarator_list
);
648 virtual void print(void) const;
650 virtual ir_rvalue
*hir(exec_list
*instructions
,
651 struct _mesa_glsl_parse_state
*state
);
654 /* List of ast_declarator_list * */
655 exec_list declarations
;
661 class ast_type_specifier
: public ast_node
{
663 /** Construct a type specifier from a type name */
664 ast_type_specifier(const char *name
)
665 : type_name(name
), structure(NULL
), array_specifier(NULL
),
666 default_precision(ast_precision_none
)
671 /** Construct a type specifier from a structure definition */
672 ast_type_specifier(ast_struct_specifier
*s
)
673 : type_name(s
->name
), structure(s
), array_specifier(NULL
),
674 default_precision(ast_precision_none
)
679 const struct glsl_type
*glsl_type(const char **name
,
680 struct _mesa_glsl_parse_state
*state
)
683 virtual void print(void) const;
685 ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
687 const char *type_name
;
688 ast_struct_specifier
*structure
;
690 ast_array_specifier
*array_specifier
;
692 /** For precision statements, this is the given precision; otherwise none. */
693 unsigned default_precision
:2;
697 class ast_fully_specified_type
: public ast_node
{
699 virtual void print(void) const;
700 bool has_qualifiers() const;
702 ast_fully_specified_type() : qualifier(), specifier(NULL
)
706 const struct glsl_type
*glsl_type(const char **name
,
707 struct _mesa_glsl_parse_state
*state
)
710 ast_type_qualifier qualifier
;
711 ast_type_specifier
*specifier
;
715 class ast_declarator_list
: public ast_node
{
717 ast_declarator_list(ast_fully_specified_type
*);
718 virtual void print(void) const;
720 virtual ir_rvalue
*hir(exec_list
*instructions
,
721 struct _mesa_glsl_parse_state
*state
);
723 ast_fully_specified_type
*type
;
724 /** List of 'ast_declaration *' */
725 exec_list declarations
;
728 * Flags for redeclarations. In these cases, no type is specified, to
729 * `type` is allowed to be NULL. In all other cases, this would be an error.
731 int invariant
; /** < `invariant` redeclaration */
732 int precise
; /** < `precise` redeclaration */
736 class ast_parameter_declarator
: public ast_node
{
738 ast_parameter_declarator() :
741 array_specifier(NULL
),
742 formal_parameter(false),
748 virtual void print(void) const;
750 virtual ir_rvalue
*hir(exec_list
*instructions
,
751 struct _mesa_glsl_parse_state
*state
);
753 ast_fully_specified_type
*type
;
754 const char *identifier
;
755 ast_array_specifier
*array_specifier
;
757 static void parameters_to_hir(exec_list
*ast_parameters
,
758 bool formal
, exec_list
*ir_parameters
,
759 struct _mesa_glsl_parse_state
*state
);
762 /** Is this parameter declaration part of a formal parameter list? */
763 bool formal_parameter
;
766 * Is this parameter 'void' type?
768 * This field is set by \c ::hir.
774 class ast_function
: public ast_node
{
778 virtual void print(void) const;
780 virtual ir_rvalue
*hir(exec_list
*instructions
,
781 struct _mesa_glsl_parse_state
*state
);
783 ast_fully_specified_type
*return_type
;
784 const char *identifier
;
786 exec_list parameters
;
790 * Is this prototype part of the function definition?
792 * Used by ast_function_definition::hir to process the parameters, etc.
800 * Function signature corresponding to this function prototype instance
802 * Used by ast_function_definition::hir to process the parameters, etc.
807 class ir_function_signature
*signature
;
809 friend class ast_function_definition
;
813 class ast_expression_statement
: public ast_node
{
815 ast_expression_statement(ast_expression
*);
816 virtual void print(void) const;
818 virtual ir_rvalue
*hir(exec_list
*instructions
,
819 struct _mesa_glsl_parse_state
*state
);
821 ast_expression
*expression
;
825 class ast_case_label
: public ast_node
{
827 ast_case_label(ast_expression
*test_value
);
828 virtual void print(void) const;
830 virtual ir_rvalue
*hir(exec_list
*instructions
,
831 struct _mesa_glsl_parse_state
*state
);
834 * An test value of NULL means 'default'.
836 ast_expression
*test_value
;
840 class ast_case_label_list
: public ast_node
{
842 ast_case_label_list(void);
843 virtual void print(void) const;
845 virtual ir_rvalue
*hir(exec_list
*instructions
,
846 struct _mesa_glsl_parse_state
*state
);
849 * A list of case labels.
855 class ast_case_statement
: public ast_node
{
857 ast_case_statement(ast_case_label_list
*labels
);
858 virtual void print(void) const;
860 virtual ir_rvalue
*hir(exec_list
*instructions
,
861 struct _mesa_glsl_parse_state
*state
);
863 ast_case_label_list
*labels
;
866 * A list of statements.
872 class ast_case_statement_list
: public ast_node
{
874 ast_case_statement_list(void);
875 virtual void print(void) const;
877 virtual ir_rvalue
*hir(exec_list
*instructions
,
878 struct _mesa_glsl_parse_state
*state
);
887 class ast_switch_body
: public ast_node
{
889 ast_switch_body(ast_case_statement_list
*stmts
);
890 virtual void print(void) const;
892 virtual ir_rvalue
*hir(exec_list
*instructions
,
893 struct _mesa_glsl_parse_state
*state
);
895 ast_case_statement_list
*stmts
;
899 class ast_selection_statement
: public ast_node
{
901 ast_selection_statement(ast_expression
*condition
,
902 ast_node
*then_statement
,
903 ast_node
*else_statement
);
904 virtual void print(void) const;
906 virtual ir_rvalue
*hir(exec_list
*instructions
,
907 struct _mesa_glsl_parse_state
*state
);
909 ast_expression
*condition
;
910 ast_node
*then_statement
;
911 ast_node
*else_statement
;
915 class ast_switch_statement
: public ast_node
{
917 ast_switch_statement(ast_expression
*test_expression
,
919 virtual void print(void) const;
921 virtual ir_rvalue
*hir(exec_list
*instructions
,
922 struct _mesa_glsl_parse_state
*state
);
924 ast_expression
*test_expression
;
928 void test_to_hir(exec_list
*, struct _mesa_glsl_parse_state
*);
931 class ast_iteration_statement
: public ast_node
{
933 ast_iteration_statement(int mode
, ast_node
*init
, ast_node
*condition
,
934 ast_expression
*rest_expression
, ast_node
*body
);
936 virtual void print(void) const;
938 virtual ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
940 enum ast_iteration_modes
{
947 ast_node
*init_statement
;
949 ast_expression
*rest_expression
;
954 * Generate IR from the condition of a loop
956 * This is factored out of ::hir because some loops have the condition
957 * test at the top (for and while), and others have it at the end (do-while).
959 void condition_to_hir(exec_list
*, struct _mesa_glsl_parse_state
*);
963 class ast_jump_statement
: public ast_node
{
965 ast_jump_statement(int mode
, ast_expression
*return_value
);
966 virtual void print(void) const;
968 virtual ir_rvalue
*hir(exec_list
*instructions
,
969 struct _mesa_glsl_parse_state
*state
);
971 enum ast_jump_modes
{
978 ast_expression
*opt_return_value
;
982 class ast_function_definition
: public ast_node
{
984 ast_function_definition() : prototype(NULL
), body(NULL
)
988 virtual void print(void) const;
990 virtual ir_rvalue
*hir(exec_list
*instructions
,
991 struct _mesa_glsl_parse_state
*state
);
993 ast_function
*prototype
;
994 ast_compound_statement
*body
;
997 class ast_interface_block
: public ast_node
{
999 ast_interface_block(ast_type_qualifier layout
,
1000 const char *instance_name
,
1001 ast_array_specifier
*array_specifier
)
1002 : layout(layout
), block_name(NULL
), instance_name(instance_name
),
1003 array_specifier(array_specifier
)
1007 virtual ir_rvalue
*hir(exec_list
*instructions
,
1008 struct _mesa_glsl_parse_state
*state
);
1010 ast_type_qualifier layout
;
1011 const char *block_name
;
1014 * Declared name of the block instance, if specified.
1016 * If the block does not have an instance name, this field will be
1019 const char *instance_name
;
1021 /** List of ast_declarator_list * */
1022 exec_list declarations
;
1025 * Declared array size of the block instance
1027 * If the block is not declared as an array or if the block instance array
1028 * is unsized, this field will be \c NULL.
1030 ast_array_specifier
*array_specifier
;
1035 * AST node representing a declaration of the input layout for geometry
1038 class ast_gs_input_layout
: public ast_node
1041 ast_gs_input_layout(const struct YYLTYPE
&locp
, GLenum prim_type
)
1042 : prim_type(prim_type
)
1047 virtual ir_rvalue
*hir(exec_list
*instructions
,
1048 struct _mesa_glsl_parse_state
*state
);
1051 const GLenum prim_type
;
1056 * AST node representing a decalaration of the input layout for compute
1059 class ast_cs_input_layout
: public ast_node
1062 ast_cs_input_layout(const struct YYLTYPE
&locp
, const unsigned *local_size
)
1064 memcpy(this->local_size
, local_size
, sizeof(this->local_size
));
1068 virtual ir_rvalue
*hir(exec_list
*instructions
,
1069 struct _mesa_glsl_parse_state
*state
);
1072 unsigned local_size
[3];
1078 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
);
1081 _mesa_ast_field_selection_to_hir(const ast_expression
*expr
,
1082 exec_list
*instructions
,
1083 struct _mesa_glsl_parse_state
*state
);
1086 _mesa_ast_array_index_to_hir(void *mem_ctx
,
1087 struct _mesa_glsl_parse_state
*state
,
1088 ir_rvalue
*array
, ir_rvalue
*idx
,
1089 YYLTYPE
&loc
, YYLTYPE
&idx_loc
);
1092 _mesa_ast_set_aggregate_type(const glsl_type
*type
,
1093 ast_expression
*expr
);
1096 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
);
1099 check_builtin_array_max_size(const char *name
, unsigned size
,
1100 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
);