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. */
196 * Representation of any sort of expression.
198 class ast_expression
: public ast_node
{
200 ast_expression(int oper
, ast_expression
*,
201 ast_expression
*, ast_expression
*);
203 ast_expression(const char *identifier
) :
206 subexpressions
[0] = NULL
;
207 subexpressions
[1] = NULL
;
208 subexpressions
[2] = NULL
;
209 primary_expression
.identifier
= identifier
;
210 this->non_lvalue_description
= NULL
;
213 static const char *operator_string(enum ast_operators op
);
215 virtual ir_rvalue
*hir(exec_list
*instructions
,
216 struct _mesa_glsl_parse_state
*state
);
218 virtual void print(void) const;
220 enum ast_operators oper
;
222 ast_expression
*subexpressions
[3];
225 const char *identifier
;
227 float float_constant
;
228 unsigned uint_constant
;
230 } primary_expression
;
234 * List of expressions for an \c ast_sequence or parameters for an
235 * \c ast_function_call
237 exec_list expressions
;
240 * For things that can't be l-values, this describes what it is.
242 * This text is used by the code that generates IR for assignments to
243 * detect and emit useful messages for assignments to some things that
244 * can't be l-values. For example, pre- or post-incerement expressions.
247 * This pointer may be \c NULL.
249 const char *non_lvalue_description
;
252 class ast_expression_bin
: public ast_expression
{
254 ast_expression_bin(int oper
, ast_expression
*, ast_expression
*);
256 virtual void print(void) const;
260 * Subclass of expressions for function calls
262 class ast_function_expression
: public ast_expression
{
264 ast_function_expression(ast_expression
*callee
)
265 : ast_expression(ast_function_call
, callee
,
272 ast_function_expression(class ast_type_specifier
*type
)
273 : ast_expression(ast_function_call
, (ast_expression
*) type
,
280 bool is_constructor() const
285 virtual ir_rvalue
*hir(exec_list
*instructions
,
286 struct _mesa_glsl_parse_state
*state
);
290 * Is this function call actually a constructor?
297 * Number of possible operators for an ast_expression
299 * This is done as a define instead of as an additional value in the enum so
300 * that the compiler won't generate spurious messages like "warning:
301 * enumeration value ‘ast_num_operators’ not handled in switch"
303 #define AST_NUM_OPERATORS (ast_sequence + 1)
306 class ast_compound_statement
: public ast_node
{
308 ast_compound_statement(int new_scope
, ast_node
*statements
);
309 virtual void print(void) const;
311 virtual ir_rvalue
*hir(exec_list
*instructions
,
312 struct _mesa_glsl_parse_state
*state
);
315 exec_list statements
;
318 class ast_declaration
: public ast_node
{
320 ast_declaration(const char *identifier
, bool is_array
, ast_expression
*array_size
,
321 ast_expression
*initializer
);
322 virtual void print(void) const;
324 const char *identifier
;
327 ast_expression
*array_size
;
329 ast_expression
*initializer
;
334 ast_precision_none
= 0, /**< Absence of precision qualifier. */
336 ast_precision_medium
,
340 struct ast_type_qualifier
{
341 /* Callers of this ralloc-based new need not call delete. It's
342 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
343 static void* operator new(size_t size
, void *ctx
)
347 node
= rzalloc_size(ctx
, size
);
348 assert(node
!= NULL
);
353 /* If the user *does* call delete, that's OK, we will just
354 * ralloc_free in that case. */
355 static void operator delete(void *table
)
362 unsigned invariant
:1;
364 unsigned attribute
:1;
372 unsigned noperspective
:1;
374 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
376 unsigned origin_upper_left
:1;
377 unsigned pixel_center_integer
:1;
381 * Flag set if GL_ARB_explicit_attrib_location "location" layout
384 unsigned explicit_location
:1;
386 * Flag set if GL_ARB_explicit_attrib_location "index" layout
389 unsigned explicit_index
:1;
391 /** \name Layout qualifiers for GL_AMD_conservative_depth */
393 unsigned depth_any
:1;
394 unsigned depth_greater
:1;
395 unsigned depth_less
:1;
396 unsigned depth_unchanged
:1;
399 /** \name Layout qualifiers for GL_ARB_uniform_buffer_object */
404 unsigned column_major
:1;
405 unsigned row_major
:1;
408 /** \brief Set of flags, accessed by name. */
411 /** \brief Set of flags, accessed as a bitmask. */
416 * Location specified via GL_ARB_explicit_attrib_location layout
419 * This field is only valid if \c explicit_location is set.
423 * Index specified via GL_ARB_explicit_attrib_location layout
426 * This field is only valid if \c explicit_index is set.
431 * Return true if and only if an interpolation qualifier is present.
433 bool has_interpolation() const;
436 * \brief Return string representation of interpolation qualifier.
438 * If an interpolation qualifier is present, then return that qualifier's
439 * string representation. Otherwise, return null. For example, if the
440 * noperspective bit is set, then this returns "noperspective".
442 * If multiple interpolation qualifiers are somehow present, then the
443 * returned string is undefined but not null.
445 const char *interpolation_string() const;
447 bool merge_qualifier(YYLTYPE
*loc
,
448 _mesa_glsl_parse_state
*state
,
449 ast_type_qualifier q
);
452 class ast_declarator_list
;
454 class ast_struct_specifier
: public ast_node
{
456 ast_struct_specifier(const char *identifier
,
457 ast_declarator_list
*declarator_list
);
458 virtual void print(void) const;
460 virtual ir_rvalue
*hir(exec_list
*instructions
,
461 struct _mesa_glsl_parse_state
*state
);
464 /* List of ast_declarator_list * */
465 exec_list declarations
;
470 class ast_type_specifier
: public ast_node
{
472 /** Construct a type specifier from a type name */
473 ast_type_specifier(const char *name
)
474 : type_name(name
), structure(NULL
),
475 is_array(false), array_size(NULL
), precision(ast_precision_none
),
476 is_precision_statement(false)
481 /** Construct a type specifier from a structure definition */
482 ast_type_specifier(ast_struct_specifier
*s
)
483 : type_name(s
->name
), structure(s
),
484 is_array(false), array_size(NULL
), precision(ast_precision_none
),
485 is_precision_statement(false)
490 const struct glsl_type
*glsl_type(const char **name
,
491 struct _mesa_glsl_parse_state
*state
)
494 virtual void print(void) const;
496 ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
498 const char *type_name
;
499 ast_struct_specifier
*structure
;
502 ast_expression
*array_size
;
504 unsigned precision
:2;
506 bool is_precision_statement
;
510 class ast_fully_specified_type
: public ast_node
{
512 virtual void print(void) const;
513 bool has_qualifiers() const;
515 ast_type_qualifier qualifier
;
516 ast_type_specifier
*specifier
;
520 class ast_declarator_list
: public ast_node
{
522 ast_declarator_list(ast_fully_specified_type
*);
523 virtual void print(void) const;
525 virtual ir_rvalue
*hir(exec_list
*instructions
,
526 struct _mesa_glsl_parse_state
*state
);
528 ast_fully_specified_type
*type
;
529 /** List of 'ast_declaration *' */
530 exec_list declarations
;
533 * Special flag for vertex shader "invariant" declarations.
535 * Vertex shaders can contain "invariant" variable redeclarations that do
536 * not include a type. For example, "invariant gl_Position;". This flag
537 * is used to note these cases when no type is specified.
542 * Flag indicating that these declarators are in a uniform block,
543 * allowing UBO type qualifiers.
545 bool ubo_qualifiers_valid
;
549 class ast_parameter_declarator
: public ast_node
{
551 ast_parameter_declarator() :
556 formal_parameter(false),
562 virtual void print(void) const;
564 virtual ir_rvalue
*hir(exec_list
*instructions
,
565 struct _mesa_glsl_parse_state
*state
);
567 ast_fully_specified_type
*type
;
568 const char *identifier
;
570 ast_expression
*array_size
;
572 static void parameters_to_hir(exec_list
*ast_parameters
,
573 bool formal
, exec_list
*ir_parameters
,
574 struct _mesa_glsl_parse_state
*state
);
577 /** Is this parameter declaration part of a formal parameter list? */
578 bool formal_parameter
;
581 * Is this parameter 'void' type?
583 * This field is set by \c ::hir.
589 class ast_function
: public ast_node
{
593 virtual void print(void) const;
595 virtual ir_rvalue
*hir(exec_list
*instructions
,
596 struct _mesa_glsl_parse_state
*state
);
598 ast_fully_specified_type
*return_type
;
599 const char *identifier
;
601 exec_list parameters
;
605 * Is this prototype part of the function definition?
607 * Used by ast_function_definition::hir to process the parameters, etc.
615 * Function signature corresponding to this function prototype instance
617 * Used by ast_function_definition::hir to process the parameters, etc.
622 class ir_function_signature
*signature
;
624 friend class ast_function_definition
;
628 class ast_expression_statement
: public ast_node
{
630 ast_expression_statement(ast_expression
*);
631 virtual void print(void) const;
633 virtual ir_rvalue
*hir(exec_list
*instructions
,
634 struct _mesa_glsl_parse_state
*state
);
636 ast_expression
*expression
;
640 class ast_case_label
: public ast_node
{
642 ast_case_label(ast_expression
*test_value
);
643 virtual void print(void) const;
645 virtual ir_rvalue
*hir(exec_list
*instructions
,
646 struct _mesa_glsl_parse_state
*state
);
649 * An test value of NULL means 'default'.
651 ast_expression
*test_value
;
655 class ast_case_label_list
: public ast_node
{
657 ast_case_label_list(void);
658 virtual void print(void) const;
660 virtual ir_rvalue
*hir(exec_list
*instructions
,
661 struct _mesa_glsl_parse_state
*state
);
664 * A list of case labels.
670 class ast_case_statement
: public ast_node
{
672 ast_case_statement(ast_case_label_list
*labels
);
673 virtual void print(void) const;
675 virtual ir_rvalue
*hir(exec_list
*instructions
,
676 struct _mesa_glsl_parse_state
*state
);
678 ast_case_label_list
*labels
;
681 * A list of statements.
687 class ast_case_statement_list
: public ast_node
{
689 ast_case_statement_list(void);
690 virtual void print(void) const;
692 virtual ir_rvalue
*hir(exec_list
*instructions
,
693 struct _mesa_glsl_parse_state
*state
);
702 class ast_switch_body
: public ast_node
{
704 ast_switch_body(ast_case_statement_list
*stmts
);
705 virtual void print(void) const;
707 virtual ir_rvalue
*hir(exec_list
*instructions
,
708 struct _mesa_glsl_parse_state
*state
);
710 ast_case_statement_list
*stmts
;
714 class ast_selection_statement
: public ast_node
{
716 ast_selection_statement(ast_expression
*condition
,
717 ast_node
*then_statement
,
718 ast_node
*else_statement
);
719 virtual void print(void) const;
721 virtual ir_rvalue
*hir(exec_list
*instructions
,
722 struct _mesa_glsl_parse_state
*state
);
724 ast_expression
*condition
;
725 ast_node
*then_statement
;
726 ast_node
*else_statement
;
730 class ast_switch_statement
: public ast_node
{
732 ast_switch_statement(ast_expression
*test_expression
,
734 virtual void print(void) const;
736 virtual ir_rvalue
*hir(exec_list
*instructions
,
737 struct _mesa_glsl_parse_state
*state
);
739 ast_expression
*test_expression
;
743 void test_to_hir(exec_list
*, struct _mesa_glsl_parse_state
*);
746 class ast_iteration_statement
: public ast_node
{
748 ast_iteration_statement(int mode
, ast_node
*init
, ast_node
*condition
,
749 ast_expression
*rest_expression
, ast_node
*body
);
751 virtual void print(void) const;
753 virtual ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
755 enum ast_iteration_modes
{
762 ast_node
*init_statement
;
764 ast_expression
*rest_expression
;
770 * Generate IR from the condition of a loop
772 * This is factored out of ::hir because some loops have the condition
773 * test at the top (for and while), and others have it at the end (do-while).
775 void condition_to_hir(class ir_loop
*, struct _mesa_glsl_parse_state
*);
779 class ast_jump_statement
: public ast_node
{
781 ast_jump_statement(int mode
, ast_expression
*return_value
);
782 virtual void print(void) const;
784 virtual ir_rvalue
*hir(exec_list
*instructions
,
785 struct _mesa_glsl_parse_state
*state
);
787 enum ast_jump_modes
{
794 ast_expression
*opt_return_value
;
798 class ast_function_definition
: public ast_node
{
800 virtual void print(void) const;
802 virtual ir_rvalue
*hir(exec_list
*instructions
,
803 struct _mesa_glsl_parse_state
*state
);
805 ast_function
*prototype
;
806 ast_compound_statement
*body
;
809 class ast_interface_block
: public ast_node
{
811 ast_interface_block(ast_type_qualifier layout
,
812 const char *instance_name
,
813 ast_expression
*array_size
)
814 : layout(layout
), block_name(NULL
), instance_name(instance_name
),
815 array_size(array_size
)
820 virtual ir_rvalue
*hir(exec_list
*instructions
,
821 struct _mesa_glsl_parse_state
*state
);
823 ast_type_qualifier layout
;
824 const char *block_name
;
827 * Declared name of the block instance, if specified.
829 * If the block does not have an instance name, this field will be
832 const char *instance_name
;
834 /** List of ast_declarator_list * */
835 exec_list declarations
;
838 * Declared array size of the block instance
840 * If the block is not declared as an array, this field will be \c NULL.
843 * A block can only be an array if it also has an instance name. If this
844 * field is not \c NULL, ::instance_name must also not be \c NULL.
846 ast_expression
*array_size
;
851 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
);
854 _mesa_ast_field_selection_to_hir(const ast_expression
*expr
,
855 exec_list
*instructions
,
856 struct _mesa_glsl_parse_state
*state
);
859 _mesa_ast_array_index_to_hir(void *mem_ctx
,
860 struct _mesa_glsl_parse_state
*state
,
861 ir_rvalue
*array
, ir_rvalue
*idx
,
862 YYLTYPE
&loc
, YYLTYPE
&idx_loc
);
865 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
);
868 check_builtin_array_max_size(const char *name
, unsigned size
,
869 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
);