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;
416 /** \name Layout qualifiers for GL_AMD_conservative_depth */
418 unsigned depth_any
:1;
419 unsigned depth_greater
:1;
420 unsigned depth_less
:1;
421 unsigned depth_unchanged
:1;
424 /** \name Layout qualifiers for GL_ARB_uniform_buffer_object */
429 unsigned column_major
:1;
430 unsigned row_major
:1;
433 /** \brief Set of flags, accessed by name. */
436 /** \brief Set of flags, accessed as a bitmask. */
441 * Location specified via GL_ARB_explicit_attrib_location layout
444 * This field is only valid if \c explicit_location is set.
448 * Index specified via GL_ARB_explicit_attrib_location layout
451 * This field is only valid if \c explicit_index is set.
456 * Return true if and only if an interpolation qualifier is present.
458 bool has_interpolation() const;
461 * Return whether a layout qualifier is present.
463 bool has_layout() const;
466 * Return whether a storage qualifier is present.
468 bool has_storage() const;
471 * Return whether an auxiliary storage qualifier is present.
473 bool has_auxiliary_storage() const;
476 * \brief Return string representation of interpolation qualifier.
478 * If an interpolation qualifier is present, then return that qualifier's
479 * string representation. Otherwise, return null. For example, if the
480 * noperspective bit is set, then this returns "noperspective".
482 * If multiple interpolation qualifiers are somehow present, then the
483 * returned string is undefined but not null.
485 const char *interpolation_string() const;
487 bool merge_qualifier(YYLTYPE
*loc
,
488 _mesa_glsl_parse_state
*state
,
489 ast_type_qualifier q
);
492 class ast_declarator_list
;
494 class ast_struct_specifier
: public ast_node
{
497 * \brief Make a shallow copy of an ast_struct_specifier.
499 * Use only if the objects are allocated from the same context and will not
500 * be modified. Zeros the inherited ast_node's fields.
502 ast_struct_specifier(const ast_struct_specifier
& that
):
503 ast_node(), name(that
.name
), declarations(that
.declarations
),
504 is_declaration(that
.is_declaration
)
509 ast_struct_specifier(const char *identifier
,
510 ast_declarator_list
*declarator_list
);
511 virtual void print(void) const;
513 virtual ir_rvalue
*hir(exec_list
*instructions
,
514 struct _mesa_glsl_parse_state
*state
);
517 /* List of ast_declarator_list * */
518 exec_list declarations
;
524 class ast_type_specifier
: public ast_node
{
527 * \brief Make a shallow copy of an ast_type_specifier, specifying array
530 * Use only if the objects are allocated from the same context and will not
531 * be modified. Zeros the inherited ast_node's fields.
533 ast_type_specifier(const ast_type_specifier
*that
, bool is_array
,
534 ast_expression
*array_size
)
535 : ast_node(), type_name(that
->type_name
), structure(that
->structure
),
536 is_array(is_array
), array_size(array_size
), precision(that
->precision
),
537 default_precision(that
->default_precision
)
542 /** Construct a type specifier from a type name */
543 ast_type_specifier(const char *name
)
544 : type_name(name
), structure(NULL
),
545 is_array(false), array_size(NULL
), precision(ast_precision_none
),
546 default_precision(ast_precision_none
)
551 /** Construct a type specifier from a structure definition */
552 ast_type_specifier(ast_struct_specifier
*s
)
553 : type_name(s
->name
), structure(s
),
554 is_array(false), array_size(NULL
), precision(ast_precision_none
),
555 default_precision(ast_precision_none
)
560 const struct glsl_type
*glsl_type(const char **name
,
561 struct _mesa_glsl_parse_state
*state
)
564 virtual void print(void) const;
566 ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
568 const char *type_name
;
569 ast_struct_specifier
*structure
;
572 ast_expression
*array_size
;
574 unsigned precision
:2;
576 /** For precision statements, this is the given precision; otherwise none. */
577 unsigned default_precision
:2;
581 class ast_fully_specified_type
: public ast_node
{
583 virtual void print(void) const;
584 bool has_qualifiers() const;
586 ast_type_qualifier qualifier
;
587 ast_type_specifier
*specifier
;
591 class ast_declarator_list
: public ast_node
{
593 ast_declarator_list(ast_fully_specified_type
*);
594 virtual void print(void) const;
596 virtual ir_rvalue
*hir(exec_list
*instructions
,
597 struct _mesa_glsl_parse_state
*state
);
599 ast_fully_specified_type
*type
;
600 /** List of 'ast_declaration *' */
601 exec_list declarations
;
604 * Special flag for vertex shader "invariant" declarations.
606 * Vertex shaders can contain "invariant" variable redeclarations that do
607 * not include a type. For example, "invariant gl_Position;". This flag
608 * is used to note these cases when no type is specified.
613 * Flag indicating that these declarators are in a uniform block,
614 * allowing UBO type qualifiers.
616 bool ubo_qualifiers_valid
;
620 class ast_parameter_declarator
: public ast_node
{
622 ast_parameter_declarator() :
627 formal_parameter(false),
633 virtual void print(void) const;
635 virtual ir_rvalue
*hir(exec_list
*instructions
,
636 struct _mesa_glsl_parse_state
*state
);
638 ast_fully_specified_type
*type
;
639 const char *identifier
;
641 ast_expression
*array_size
;
643 static void parameters_to_hir(exec_list
*ast_parameters
,
644 bool formal
, exec_list
*ir_parameters
,
645 struct _mesa_glsl_parse_state
*state
);
648 /** Is this parameter declaration part of a formal parameter list? */
649 bool formal_parameter
;
652 * Is this parameter 'void' type?
654 * This field is set by \c ::hir.
660 class ast_function
: public ast_node
{
664 virtual void print(void) const;
666 virtual ir_rvalue
*hir(exec_list
*instructions
,
667 struct _mesa_glsl_parse_state
*state
);
669 ast_fully_specified_type
*return_type
;
670 const char *identifier
;
672 exec_list parameters
;
676 * Is this prototype part of the function definition?
678 * Used by ast_function_definition::hir to process the parameters, etc.
686 * Function signature corresponding to this function prototype instance
688 * Used by ast_function_definition::hir to process the parameters, etc.
693 class ir_function_signature
*signature
;
695 friend class ast_function_definition
;
699 class ast_expression_statement
: public ast_node
{
701 ast_expression_statement(ast_expression
*);
702 virtual void print(void) const;
704 virtual ir_rvalue
*hir(exec_list
*instructions
,
705 struct _mesa_glsl_parse_state
*state
);
707 ast_expression
*expression
;
711 class ast_case_label
: public ast_node
{
713 ast_case_label(ast_expression
*test_value
);
714 virtual void print(void) const;
716 virtual ir_rvalue
*hir(exec_list
*instructions
,
717 struct _mesa_glsl_parse_state
*state
);
720 * An test value of NULL means 'default'.
722 ast_expression
*test_value
;
726 class ast_case_label_list
: public ast_node
{
728 ast_case_label_list(void);
729 virtual void print(void) const;
731 virtual ir_rvalue
*hir(exec_list
*instructions
,
732 struct _mesa_glsl_parse_state
*state
);
735 * A list of case labels.
741 class ast_case_statement
: public ast_node
{
743 ast_case_statement(ast_case_label_list
*labels
);
744 virtual void print(void) const;
746 virtual ir_rvalue
*hir(exec_list
*instructions
,
747 struct _mesa_glsl_parse_state
*state
);
749 ast_case_label_list
*labels
;
752 * A list of statements.
758 class ast_case_statement_list
: public ast_node
{
760 ast_case_statement_list(void);
761 virtual void print(void) const;
763 virtual ir_rvalue
*hir(exec_list
*instructions
,
764 struct _mesa_glsl_parse_state
*state
);
773 class ast_switch_body
: public ast_node
{
775 ast_switch_body(ast_case_statement_list
*stmts
);
776 virtual void print(void) const;
778 virtual ir_rvalue
*hir(exec_list
*instructions
,
779 struct _mesa_glsl_parse_state
*state
);
781 ast_case_statement_list
*stmts
;
785 class ast_selection_statement
: public ast_node
{
787 ast_selection_statement(ast_expression
*condition
,
788 ast_node
*then_statement
,
789 ast_node
*else_statement
);
790 virtual void print(void) const;
792 virtual ir_rvalue
*hir(exec_list
*instructions
,
793 struct _mesa_glsl_parse_state
*state
);
795 ast_expression
*condition
;
796 ast_node
*then_statement
;
797 ast_node
*else_statement
;
801 class ast_switch_statement
: public ast_node
{
803 ast_switch_statement(ast_expression
*test_expression
,
805 virtual void print(void) const;
807 virtual ir_rvalue
*hir(exec_list
*instructions
,
808 struct _mesa_glsl_parse_state
*state
);
810 ast_expression
*test_expression
;
814 void test_to_hir(exec_list
*, struct _mesa_glsl_parse_state
*);
817 class ast_iteration_statement
: public ast_node
{
819 ast_iteration_statement(int mode
, ast_node
*init
, ast_node
*condition
,
820 ast_expression
*rest_expression
, ast_node
*body
);
822 virtual void print(void) const;
824 virtual ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
826 enum ast_iteration_modes
{
833 ast_node
*init_statement
;
835 ast_expression
*rest_expression
;
841 * Generate IR from the condition of a loop
843 * This is factored out of ::hir because some loops have the condition
844 * test at the top (for and while), and others have it at the end (do-while).
846 void condition_to_hir(class ir_loop
*, struct _mesa_glsl_parse_state
*);
850 class ast_jump_statement
: public ast_node
{
852 ast_jump_statement(int mode
, ast_expression
*return_value
);
853 virtual void print(void) const;
855 virtual ir_rvalue
*hir(exec_list
*instructions
,
856 struct _mesa_glsl_parse_state
*state
);
858 enum ast_jump_modes
{
865 ast_expression
*opt_return_value
;
869 class ast_function_definition
: public ast_node
{
871 virtual void print(void) const;
873 virtual ir_rvalue
*hir(exec_list
*instructions
,
874 struct _mesa_glsl_parse_state
*state
);
876 ast_function
*prototype
;
877 ast_compound_statement
*body
;
880 class ast_interface_block
: public ast_node
{
882 ast_interface_block(ast_type_qualifier layout
,
883 const char *instance_name
,
884 ast_expression
*array_size
)
885 : layout(layout
), block_name(NULL
), instance_name(instance_name
),
886 array_size(array_size
)
891 virtual ir_rvalue
*hir(exec_list
*instructions
,
892 struct _mesa_glsl_parse_state
*state
);
894 ast_type_qualifier layout
;
895 const char *block_name
;
898 * Declared name of the block instance, if specified.
900 * If the block does not have an instance name, this field will be
903 const char *instance_name
;
905 /** List of ast_declarator_list * */
906 exec_list declarations
;
909 * Declared array size of the block instance
911 * If the block is not declared as an array, this field will be \c NULL.
914 * A block can only be an array if it also has an instance name. If this
915 * field is not \c NULL, ::instance_name must also not be \c NULL.
917 ast_expression
*array_size
;
922 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
);
925 _mesa_ast_field_selection_to_hir(const ast_expression
*expr
,
926 exec_list
*instructions
,
927 struct _mesa_glsl_parse_state
*state
);
930 _mesa_ast_array_index_to_hir(void *mem_ctx
,
931 struct _mesa_glsl_parse_state
*state
,
932 ir_rvalue
*array
, ir_rvalue
*idx
,
933 YYLTYPE
&loc
, YYLTYPE
&idx_loc
);
936 _mesa_ast_set_aggregate_type(const ast_type_specifier
*type
,
937 ast_expression
*expr
,
938 _mesa_glsl_parse_state
*state
);
941 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
);
944 check_builtin_array_max_size(const char *name
, unsigned size
,
945 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
);