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 talloc-based new need not call delete. It's
53 * easier to just talloc_free 'ctx' (or any of its ancestors). */
54 static void* operator new(size_t size
, void *ctx
)
58 node
= talloc_zero_size(ctx
, size
);
64 /* If the user *does* call delete, that's OK, we will just
65 * talloc_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
= (char *) identifier
;
212 static const char *operator_string(enum ast_operators op
);
214 virtual ir_rvalue
*hir(exec_list
*instructions
,
215 struct _mesa_glsl_parse_state
*state
);
217 virtual void print(void) const;
219 enum ast_operators oper
;
221 ast_expression
*subexpressions
[3];
226 float float_constant
;
227 unsigned uint_constant
;
229 } primary_expression
;
233 * List of expressions for an \c ast_sequence or parameters for an
234 * \c ast_function_call
236 exec_list expressions
;
239 class ast_expression_bin
: public ast_expression
{
241 ast_expression_bin(int oper
, ast_expression
*, ast_expression
*);
243 virtual void print(void) const;
247 * Subclass of expressions for function calls
249 class ast_function_expression
: public ast_expression
{
251 ast_function_expression(ast_expression
*callee
)
252 : ast_expression(ast_function_call
, callee
,
259 ast_function_expression(class ast_type_specifier
*type
)
260 : ast_expression(ast_function_call
, (ast_expression
*) type
,
267 bool is_constructor() const
272 virtual ir_rvalue
*hir(exec_list
*instructions
,
273 struct _mesa_glsl_parse_state
*state
);
277 * Is this function call actually a constructor?
284 * Number of possible operators for an ast_expression
286 * This is done as a define instead of as an additional value in the enum so
287 * that the compiler won't generate spurious messages like "warning:
288 * enumeration value ‘ast_num_operators’ not handled in switch"
290 #define AST_NUM_OPERATORS (ast_sequence + 1)
293 class ast_compound_statement
: public ast_node
{
295 ast_compound_statement(int new_scope
, ast_node
*statements
);
296 virtual void print(void) const;
298 virtual ir_rvalue
*hir(exec_list
*instructions
,
299 struct _mesa_glsl_parse_state
*state
);
302 exec_list statements
;
305 class ast_declaration
: public ast_node
{
307 ast_declaration(char *identifier
, int is_array
, ast_expression
*array_size
,
308 ast_expression
*initializer
);
309 virtual void print(void) const;
314 ast_expression
*array_size
;
316 ast_expression
*initializer
;
321 ast_precision_none
= 0, /**< Absence of precision qualifier. */
323 ast_precision_medium
,
327 struct ast_type_qualifier
{
330 unsigned invariant
:1;
332 unsigned attribute
:1;
340 unsigned noperspective
:1;
342 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
344 unsigned origin_upper_left
:1;
345 unsigned pixel_center_integer
:1;
349 * Flag set if GL_ARB_explicit_attrib_location "location" layout
352 unsigned explicit_location
:1;
354 /** \brief Set of flags, accessed by name. */
357 /** \brief Set of flags, accessed as a bitmask. */
362 * Location specified via GL_ARB_explicit_attrib_location layout
365 * This field is only valid if \c explicit_location is set.
370 * Return true if and only if an interpolation qualifier is present.
372 bool has_interpolation() const;
375 * \brief Return string representation of interpolation qualifier.
377 * If an interpolation qualifier is present, then return that qualifier's
378 * string representation. Otherwise, return null. For example, if the
379 * noperspective bit is set, then this returns "noperspective".
381 * If multiple interpolation qualifiers are somehow present, then the
382 * returned string is undefined but not null.
384 const char *interpolation_string() const;
387 class ast_struct_specifier
: public ast_node
{
389 ast_struct_specifier(char *identifier
, ast_node
*declarator_list
);
390 virtual void print(void) const;
392 virtual ir_rvalue
*hir(exec_list
*instructions
,
393 struct _mesa_glsl_parse_state
*state
);
396 exec_list declarations
;
434 ast_sampler2drectshadow
,
435 ast_samplercubeshadow
,
438 ast_sampler1darrayshadow
,
439 ast_sampler2darrayshadow
,
458 class ast_type_specifier
: public ast_node
{
460 ast_type_specifier(int specifier
);
462 /** Construct a type specifier from a type name */
463 ast_type_specifier(const char *name
)
464 : type_specifier(ast_type_name
), type_name(name
), structure(NULL
),
465 is_array(false), array_size(NULL
), precision(ast_precision_none
),
466 is_precision_statement(false)
471 /** Construct a type specifier from a structure definition */
472 ast_type_specifier(ast_struct_specifier
*s
)
473 : type_specifier(ast_struct
), type_name(s
->name
), structure(s
),
474 is_array(false), array_size(NULL
), precision(ast_precision_none
),
475 is_precision_statement(false)
480 const struct glsl_type
*glsl_type(const char **name
,
481 struct _mesa_glsl_parse_state
*state
)
484 virtual void print(void) const;
486 ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
488 enum ast_types type_specifier
;
490 const char *type_name
;
491 ast_struct_specifier
*structure
;
494 ast_expression
*array_size
;
496 unsigned precision
:2;
498 bool is_precision_statement
;
502 class ast_fully_specified_type
: public ast_node
{
504 virtual void print(void) const;
505 bool has_qualifiers() const;
507 ast_type_qualifier qualifier
;
508 ast_type_specifier
*specifier
;
512 class ast_declarator_list
: public ast_node
{
514 ast_declarator_list(ast_fully_specified_type
*);
515 virtual void print(void) const;
517 virtual ir_rvalue
*hir(exec_list
*instructions
,
518 struct _mesa_glsl_parse_state
*state
);
520 ast_fully_specified_type
*type
;
521 exec_list declarations
;
524 * Special flag for vertex shader "invariant" declarations.
526 * Vertex shaders can contain "invariant" variable redeclarations that do
527 * not include a type. For example, "invariant gl_Position;". This flag
528 * is used to note these cases when no type is specified.
534 class ast_parameter_declarator
: public ast_node
{
536 ast_parameter_declarator()
538 this->identifier
= NULL
;
539 this->is_array
= false;
540 this->array_size
= 0;
543 virtual void print(void) const;
545 virtual ir_rvalue
*hir(exec_list
*instructions
,
546 struct _mesa_glsl_parse_state
*state
);
548 ast_fully_specified_type
*type
;
551 ast_expression
*array_size
;
553 static void parameters_to_hir(exec_list
*ast_parameters
,
554 bool formal
, exec_list
*ir_parameters
,
555 struct _mesa_glsl_parse_state
*state
);
558 /** Is this parameter declaration part of a formal parameter list? */
559 bool formal_parameter
;
562 * Is this parameter 'void' type?
564 * This field is set by \c ::hir.
570 class ast_function
: public ast_node
{
574 virtual void print(void) const;
576 virtual ir_rvalue
*hir(exec_list
*instructions
,
577 struct _mesa_glsl_parse_state
*state
);
579 ast_fully_specified_type
*return_type
;
582 exec_list parameters
;
586 * Is this prototype part of the function definition?
588 * Used by ast_function_definition::hir to process the parameters, etc.
596 * Function signature corresponding to this function prototype instance
598 * Used by ast_function_definition::hir to process the parameters, etc.
603 class ir_function_signature
*signature
;
605 friend class ast_function_definition
;
609 class ast_expression_statement
: public ast_node
{
611 ast_expression_statement(ast_expression
*);
612 virtual void print(void) const;
614 virtual ir_rvalue
*hir(exec_list
*instructions
,
615 struct _mesa_glsl_parse_state
*state
);
617 ast_expression
*expression
;
621 class ast_case_label
: public ast_node
{
625 * An expression of NULL means 'default'.
627 ast_expression
*expression
;
630 class ast_selection_statement
: public ast_node
{
632 ast_selection_statement(ast_expression
*condition
,
633 ast_node
*then_statement
,
634 ast_node
*else_statement
);
635 virtual void print(void) const;
637 virtual ir_rvalue
*hir(exec_list
*instructions
,
638 struct _mesa_glsl_parse_state
*state
);
640 ast_expression
*condition
;
641 ast_node
*then_statement
;
642 ast_node
*else_statement
;
646 class ast_switch_statement
: public ast_node
{
648 ast_expression
*expression
;
649 exec_list statements
;
652 class ast_iteration_statement
: public ast_node
{
654 ast_iteration_statement(int mode
, ast_node
*init
, ast_node
*condition
,
655 ast_expression
*rest_expression
, ast_node
*body
);
657 virtual void print(void) const;
659 virtual ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
661 enum ast_iteration_modes
{
668 ast_node
*init_statement
;
670 ast_expression
*rest_expression
;
676 * Generate IR from the condition of a loop
678 * This is factored out of ::hir because some loops have the condition
679 * test at the top (for and while), and others have it at the end (do-while).
681 void condition_to_hir(class ir_loop
*, struct _mesa_glsl_parse_state
*);
685 class ast_jump_statement
: public ast_node
{
687 ast_jump_statement(int mode
, ast_expression
*return_value
);
688 virtual void print(void) const;
690 virtual ir_rvalue
*hir(exec_list
*instructions
,
691 struct _mesa_glsl_parse_state
*state
);
693 enum ast_jump_modes
{
700 ast_expression
*opt_return_value
;
704 class ast_function_definition
: public ast_node
{
706 virtual void print(void) const;
708 virtual ir_rvalue
*hir(exec_list
*instructions
,
709 struct _mesa_glsl_parse_state
*state
);
711 ast_function
*prototype
;
712 ast_compound_statement
*body
;
717 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
);
720 _mesa_ast_field_selection_to_hir(const ast_expression
*expr
,
721 exec_list
*instructions
,
722 struct _mesa_glsl_parse_state
*state
);
725 emit_function(_mesa_glsl_parse_state
*state
, exec_list
*instructions
,