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?
279 class ast_array_specifier
: public ast_node
{
281 /** Unsized array specifier ([]) */
282 explicit ast_array_specifier(const struct YYLTYPE
&locp
)
283 : dimension_count(1), is_unsized_array(true)
288 /** Sized array specifier ([dim]) */
289 ast_array_specifier(const struct YYLTYPE
&locp
, ast_expression
*dim
)
290 : dimension_count(1), is_unsized_array(false)
293 array_dimensions
.push_tail(&dim
->link
);
296 void add_dimension(ast_expression
*dim
)
298 array_dimensions
.push_tail(&dim
->link
);
302 virtual void print(void) const;
304 /* Count including sized and unsized dimensions */
305 unsigned dimension_count
;
307 /* If true, this means that the array has an unsized outermost dimension. */
308 bool is_unsized_array
;
310 /* This list contains objects of type ast_node containing the
311 * sized dimensions only, in outermost-to-innermost order.
313 exec_list array_dimensions
;
317 * C-style aggregate initialization class
319 * Represents C-style initializers of vectors, matrices, arrays, and
320 * structures. E.g., vec3 pos = {1.0, 0.0, -1.0} is equivalent to
321 * vec3 pos = vec3(1.0, 0.0, -1.0).
323 * Specified in GLSL 4.20 and GL_ARB_shading_language_420pack.
325 * \sa _mesa_ast_set_aggregate_type
327 class ast_aggregate_initializer
: public ast_expression
{
329 ast_aggregate_initializer()
330 : ast_expression(ast_aggregate
, NULL
, NULL
, NULL
),
331 constructor_type(NULL
)
337 * glsl_type of the aggregate, which is inferred from the LHS of whatever
338 * the aggregate is being used to initialize. This can't be inferred at
339 * parse time (since the parser deals with ast_type_specifiers, not
340 * glsl_types), so the parser leaves it NULL. However, the ast-to-hir
341 * conversion code makes sure to fill it in with the appropriate type
342 * before hir() is called.
344 const glsl_type
*constructor_type
;
346 virtual ir_rvalue
*hir(exec_list
*instructions
,
347 struct _mesa_glsl_parse_state
*state
);
351 * Number of possible operators for an ast_expression
353 * This is done as a define instead of as an additional value in the enum so
354 * that the compiler won't generate spurious messages like "warning:
355 * enumeration value ‘ast_num_operators’ not handled in switch"
357 #define AST_NUM_OPERATORS (ast_sequence + 1)
360 class ast_compound_statement
: public ast_node
{
362 ast_compound_statement(int new_scope
, ast_node
*statements
);
363 virtual void print(void) const;
365 virtual ir_rvalue
*hir(exec_list
*instructions
,
366 struct _mesa_glsl_parse_state
*state
);
369 exec_list statements
;
372 class ast_declaration
: public ast_node
{
374 ast_declaration(const char *identifier
,
375 ast_array_specifier
*array_specifier
,
376 ast_expression
*initializer
);
377 virtual void print(void) const;
379 const char *identifier
;
381 ast_array_specifier
*array_specifier
;
383 ast_expression
*initializer
;
388 ast_precision_none
= 0, /**< Absence of precision qualifier. */
390 ast_precision_medium
,
394 struct ast_type_qualifier
{
395 DECLARE_RALLOC_CXX_OPERATORS(ast_type_qualifier
);
399 unsigned invariant
:1;
401 unsigned attribute
:1;
410 unsigned noperspective
:1;
412 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
414 unsigned origin_upper_left
:1;
415 unsigned pixel_center_integer
:1;
419 * Flag set if GL_ARB_explicit_attrib_location "location" layout
422 unsigned explicit_location
:1;
424 * Flag set if GL_ARB_explicit_attrib_location "index" layout
427 unsigned explicit_index
:1;
430 * Flag set if GL_ARB_shading_language_420pack "binding" layout
433 unsigned explicit_binding
:1;
436 * Flag set if GL_ARB_shader_atomic counter "offset" layout
439 unsigned explicit_offset
:1;
441 /** \name Layout qualifiers for GL_AMD_conservative_depth */
443 unsigned depth_any
:1;
444 unsigned depth_greater
:1;
445 unsigned depth_less
:1;
446 unsigned depth_unchanged
:1;
449 /** \name Layout qualifiers for GL_ARB_uniform_buffer_object */
454 unsigned column_major
:1;
455 unsigned row_major
:1;
458 /** \name Layout qualifiers for GLSL 1.50 geometry shaders */
460 unsigned prim_type
:1;
461 unsigned max_vertices
:1;
465 * local_size_{x,y,z} flags for compute shaders. Bit 0 represents
466 * local_size_x, and so on.
468 unsigned local_size
:3;
470 /** \name Layout and memory qualifiers for ARB_shader_image_load_store. */
472 unsigned early_fragment_tests
:1;
473 unsigned explicit_image_format
:1;
475 unsigned _volatile
:1;
476 unsigned restrict_flag
:1;
477 unsigned read_only
:1; /**< "readonly" qualifier. */
478 unsigned write_only
:1; /**< "writeonly" qualifier. */
481 /** \brief Set of flags, accessed by name. */
484 /** \brief Set of flags, accessed as a bitmask. */
488 /** Precision of the type (highp/medium/lowp). */
489 unsigned precision
:2;
492 * Location specified via GL_ARB_explicit_attrib_location layout
495 * This field is only valid if \c explicit_location is set.
499 * Index specified via GL_ARB_explicit_attrib_location layout
502 * This field is only valid if \c explicit_index is set.
506 /** Maximum output vertices in GLSL 1.50 geometry shaders. */
509 /** Input or output primitive type in GLSL 1.50 geometry shaders */
513 * Binding specified via GL_ARB_shading_language_420pack's "binding" keyword.
516 * This field is only valid if \c explicit_binding is set.
521 * Offset specified via GL_ARB_shader_atomic_counter's "offset"
525 * This field is only valid if \c explicit_offset is set.
530 * Local size specified via GL_ARB_compute_shader's "local_size_{x,y,z}"
531 * layout qualifier. Element i of this array is only valid if
532 * flags.q.local_size & (1 << i) is set.
537 * Image format specified with an ARB_shader_image_load_store
541 * This field is only valid if \c explicit_image_format is set.
546 * Base type of the data read from or written to this image. Only
547 * the following enumerants are allowed: GLSL_TYPE_UINT,
548 * GLSL_TYPE_INT, GLSL_TYPE_FLOAT.
551 * This field is only valid if \c explicit_image_format is set.
553 glsl_base_type image_base_type
;
556 * Return true if and only if an interpolation qualifier is present.
558 bool has_interpolation() const;
561 * Return whether a layout qualifier is present.
563 bool has_layout() const;
566 * Return whether a storage qualifier is present.
568 bool has_storage() const;
571 * Return whether an auxiliary storage qualifier is present.
573 bool has_auxiliary_storage() const;
576 * \brief Return string representation of interpolation qualifier.
578 * If an interpolation qualifier is present, then return that qualifier's
579 * string representation. Otherwise, return null. For example, if the
580 * noperspective bit is set, then this returns "noperspective".
582 * If multiple interpolation qualifiers are somehow present, then the
583 * returned string is undefined but not null.
585 const char *interpolation_string() const;
587 bool merge_qualifier(YYLTYPE
*loc
,
588 _mesa_glsl_parse_state
*state
,
589 ast_type_qualifier q
);
592 class ast_declarator_list
;
594 class ast_struct_specifier
: public ast_node
{
597 * \brief Make a shallow copy of an ast_struct_specifier.
599 * Use only if the objects are allocated from the same context and will not
600 * be modified. Zeros the inherited ast_node's fields.
602 ast_struct_specifier(const ast_struct_specifier
& that
):
603 ast_node(), name(that
.name
), declarations(that
.declarations
),
604 is_declaration(that
.is_declaration
)
609 ast_struct_specifier(const char *identifier
,
610 ast_declarator_list
*declarator_list
);
611 virtual void print(void) const;
613 virtual ir_rvalue
*hir(exec_list
*instructions
,
614 struct _mesa_glsl_parse_state
*state
);
617 /* List of ast_declarator_list * */
618 exec_list declarations
;
624 class ast_type_specifier
: public ast_node
{
627 * \brief Make a shallow copy of an ast_type_specifier, specifying array
630 * Use only if the objects are allocated from the same context and will not
631 * be modified. Zeros the inherited ast_node's fields.
633 ast_type_specifier(const ast_type_specifier
*that
,
634 ast_array_specifier
*array_specifier
)
635 : ast_node(), type_name(that
->type_name
), structure(that
->structure
),
636 array_specifier(array_specifier
),
637 default_precision(that
->default_precision
)
642 /** Construct a type specifier from a type name */
643 ast_type_specifier(const char *name
)
644 : type_name(name
), structure(NULL
), array_specifier(NULL
),
645 default_precision(ast_precision_none
)
650 /** Construct a type specifier from a structure definition */
651 ast_type_specifier(ast_struct_specifier
*s
)
652 : type_name(s
->name
), structure(s
), array_specifier(NULL
),
653 default_precision(ast_precision_none
)
658 const struct glsl_type
*glsl_type(const char **name
,
659 struct _mesa_glsl_parse_state
*state
)
662 virtual void print(void) const;
664 ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
666 const char *type_name
;
667 ast_struct_specifier
*structure
;
669 ast_array_specifier
*array_specifier
;
671 /** For precision statements, this is the given precision; otherwise none. */
672 unsigned default_precision
:2;
676 class ast_fully_specified_type
: public ast_node
{
678 virtual void print(void) const;
679 bool has_qualifiers() const;
681 ast_fully_specified_type() : qualifier(), specifier(NULL
)
685 const struct glsl_type
*glsl_type(const char **name
,
686 struct _mesa_glsl_parse_state
*state
)
689 ast_type_qualifier qualifier
;
690 ast_type_specifier
*specifier
;
694 class ast_declarator_list
: public ast_node
{
696 ast_declarator_list(ast_fully_specified_type
*);
697 virtual void print(void) const;
699 virtual ir_rvalue
*hir(exec_list
*instructions
,
700 struct _mesa_glsl_parse_state
*state
);
702 ast_fully_specified_type
*type
;
703 /** List of 'ast_declaration *' */
704 exec_list declarations
;
707 * Special flag for vertex shader "invariant" declarations.
709 * Vertex shaders can contain "invariant" variable redeclarations that do
710 * not include a type. For example, "invariant gl_Position;". This flag
711 * is used to note these cases when no type is specified.
717 class ast_parameter_declarator
: public ast_node
{
719 ast_parameter_declarator() :
722 array_specifier(NULL
),
723 formal_parameter(false),
729 virtual void print(void) const;
731 virtual ir_rvalue
*hir(exec_list
*instructions
,
732 struct _mesa_glsl_parse_state
*state
);
734 ast_fully_specified_type
*type
;
735 const char *identifier
;
736 ast_array_specifier
*array_specifier
;
738 static void parameters_to_hir(exec_list
*ast_parameters
,
739 bool formal
, exec_list
*ir_parameters
,
740 struct _mesa_glsl_parse_state
*state
);
743 /** Is this parameter declaration part of a formal parameter list? */
744 bool formal_parameter
;
747 * Is this parameter 'void' type?
749 * This field is set by \c ::hir.
755 class ast_function
: public ast_node
{
759 virtual void print(void) const;
761 virtual ir_rvalue
*hir(exec_list
*instructions
,
762 struct _mesa_glsl_parse_state
*state
);
764 ast_fully_specified_type
*return_type
;
765 const char *identifier
;
767 exec_list parameters
;
771 * Is this prototype part of the function definition?
773 * Used by ast_function_definition::hir to process the parameters, etc.
781 * Function signature corresponding to this function prototype instance
783 * Used by ast_function_definition::hir to process the parameters, etc.
788 class ir_function_signature
*signature
;
790 friend class ast_function_definition
;
794 class ast_expression_statement
: public ast_node
{
796 ast_expression_statement(ast_expression
*);
797 virtual void print(void) const;
799 virtual ir_rvalue
*hir(exec_list
*instructions
,
800 struct _mesa_glsl_parse_state
*state
);
802 ast_expression
*expression
;
806 class ast_case_label
: public ast_node
{
808 ast_case_label(ast_expression
*test_value
);
809 virtual void print(void) const;
811 virtual ir_rvalue
*hir(exec_list
*instructions
,
812 struct _mesa_glsl_parse_state
*state
);
815 * An test value of NULL means 'default'.
817 ast_expression
*test_value
;
821 class ast_case_label_list
: public ast_node
{
823 ast_case_label_list(void);
824 virtual void print(void) const;
826 virtual ir_rvalue
*hir(exec_list
*instructions
,
827 struct _mesa_glsl_parse_state
*state
);
830 * A list of case labels.
836 class ast_case_statement
: public ast_node
{
838 ast_case_statement(ast_case_label_list
*labels
);
839 virtual void print(void) const;
841 virtual ir_rvalue
*hir(exec_list
*instructions
,
842 struct _mesa_glsl_parse_state
*state
);
844 ast_case_label_list
*labels
;
847 * A list of statements.
853 class ast_case_statement_list
: public ast_node
{
855 ast_case_statement_list(void);
856 virtual void print(void) const;
858 virtual ir_rvalue
*hir(exec_list
*instructions
,
859 struct _mesa_glsl_parse_state
*state
);
868 class ast_switch_body
: public ast_node
{
870 ast_switch_body(ast_case_statement_list
*stmts
);
871 virtual void print(void) const;
873 virtual ir_rvalue
*hir(exec_list
*instructions
,
874 struct _mesa_glsl_parse_state
*state
);
876 ast_case_statement_list
*stmts
;
880 class ast_selection_statement
: public ast_node
{
882 ast_selection_statement(ast_expression
*condition
,
883 ast_node
*then_statement
,
884 ast_node
*else_statement
);
885 virtual void print(void) const;
887 virtual ir_rvalue
*hir(exec_list
*instructions
,
888 struct _mesa_glsl_parse_state
*state
);
890 ast_expression
*condition
;
891 ast_node
*then_statement
;
892 ast_node
*else_statement
;
896 class ast_switch_statement
: public ast_node
{
898 ast_switch_statement(ast_expression
*test_expression
,
900 virtual void print(void) const;
902 virtual ir_rvalue
*hir(exec_list
*instructions
,
903 struct _mesa_glsl_parse_state
*state
);
905 ast_expression
*test_expression
;
909 void test_to_hir(exec_list
*, struct _mesa_glsl_parse_state
*);
912 class ast_iteration_statement
: public ast_node
{
914 ast_iteration_statement(int mode
, ast_node
*init
, ast_node
*condition
,
915 ast_expression
*rest_expression
, ast_node
*body
);
917 virtual void print(void) const;
919 virtual ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
921 enum ast_iteration_modes
{
928 ast_node
*init_statement
;
930 ast_expression
*rest_expression
;
935 * Generate IR from the condition of a loop
937 * This is factored out of ::hir because some loops have the condition
938 * test at the top (for and while), and others have it at the end (do-while).
940 void condition_to_hir(exec_list
*, struct _mesa_glsl_parse_state
*);
944 class ast_jump_statement
: public ast_node
{
946 ast_jump_statement(int mode
, ast_expression
*return_value
);
947 virtual void print(void) const;
949 virtual ir_rvalue
*hir(exec_list
*instructions
,
950 struct _mesa_glsl_parse_state
*state
);
952 enum ast_jump_modes
{
959 ast_expression
*opt_return_value
;
963 class ast_function_definition
: public ast_node
{
965 ast_function_definition() : prototype(NULL
), body(NULL
)
969 virtual void print(void) const;
971 virtual ir_rvalue
*hir(exec_list
*instructions
,
972 struct _mesa_glsl_parse_state
*state
);
974 ast_function
*prototype
;
975 ast_compound_statement
*body
;
978 class ast_interface_block
: public ast_node
{
980 ast_interface_block(ast_type_qualifier layout
,
981 const char *instance_name
,
982 ast_array_specifier
*array_specifier
)
983 : layout(layout
), block_name(NULL
), instance_name(instance_name
),
984 array_specifier(array_specifier
)
988 virtual ir_rvalue
*hir(exec_list
*instructions
,
989 struct _mesa_glsl_parse_state
*state
);
991 ast_type_qualifier layout
;
992 const char *block_name
;
995 * Declared name of the block instance, if specified.
997 * If the block does not have an instance name, this field will be
1000 const char *instance_name
;
1002 /** List of ast_declarator_list * */
1003 exec_list declarations
;
1006 * Declared array size of the block instance
1008 * If the block is not declared as an array or if the block instance array
1009 * is unsized, this field will be \c NULL.
1011 ast_array_specifier
*array_specifier
;
1016 * AST node representing a declaration of the input layout for geometry
1019 class ast_gs_input_layout
: public ast_node
1022 ast_gs_input_layout(const struct YYLTYPE
&locp
, GLenum prim_type
)
1023 : prim_type(prim_type
)
1028 virtual ir_rvalue
*hir(exec_list
*instructions
,
1029 struct _mesa_glsl_parse_state
*state
);
1032 const GLenum prim_type
;
1037 * AST node representing a decalaration of the input layout for compute
1040 class ast_cs_input_layout
: public ast_node
1043 ast_cs_input_layout(const struct YYLTYPE
&locp
, const unsigned *local_size
)
1045 memcpy(this->local_size
, local_size
, sizeof(this->local_size
));
1049 virtual ir_rvalue
*hir(exec_list
*instructions
,
1050 struct _mesa_glsl_parse_state
*state
);
1053 unsigned local_size
[3];
1059 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
);
1062 _mesa_ast_field_selection_to_hir(const ast_expression
*expr
,
1063 exec_list
*instructions
,
1064 struct _mesa_glsl_parse_state
*state
);
1067 _mesa_ast_array_index_to_hir(void *mem_ctx
,
1068 struct _mesa_glsl_parse_state
*state
,
1069 ir_rvalue
*array
, ir_rvalue
*idx
,
1070 YYLTYPE
&loc
, YYLTYPE
&idx_loc
);
1073 _mesa_ast_set_aggregate_type(const glsl_type
*type
,
1074 ast_expression
*expr
);
1077 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
);
1080 check_builtin_array_max_size(const char *name
, unsigned size
,
1081 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
);