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
;
38 /* Callers of this talloc-based new need not call delete. It's
39 * easier to just talloc_free 'ctx' (or any of its ancestors). */
40 static void* operator new(size_t size
, void *ctx
)
44 node
= talloc_zero_size(ctx
, size
);
50 /* If the user *does* call delete, that's OK, we will just
51 * talloc_free in that case. */
52 static void operator delete(void *table
)
57 virtual void print(void) const;
58 virtual ir_rvalue
*hir(exec_list
*instructions
,
59 struct _mesa_glsl_parse_state
*state
);
62 * Retrieve the source location of an AST node
64 * This function is primarily used to get the source position of an AST node
65 * into a form that can be passed to \c _mesa_glsl_error.
67 * \sa _mesa_glsl_error, ast_node::set_location
69 struct YYLTYPE
get_location(void) const
73 locp
.source
= this->location
.source
;
74 locp
.first_line
= this->location
.line
;
75 locp
.first_column
= this->location
.column
;
76 locp
.last_line
= locp
.first_line
;
77 locp
.last_column
= locp
.first_column
;
83 * Set the source location of an AST node from a parser location
85 * \sa ast_node::get_location
87 void set_location(const struct YYLTYPE
&locp
)
89 this->location
.source
= locp
.source
;
90 this->location
.line
= locp
.first_line
;
91 this->location
.column
= locp
.first_column
;
109 ast_plus
, /**< Unary + operator. */
164 class ast_expression
: public ast_node
{
166 ast_expression(int oper
, ast_expression
*,
167 ast_expression
*, ast_expression
*);
169 ast_expression(const char *identifier
) :
172 subexpressions
[0] = NULL
;
173 subexpressions
[1] = NULL
;
174 subexpressions
[2] = NULL
;
175 primary_expression
.identifier
= (char *) identifier
;
178 static const char *operator_string(enum ast_operators op
);
180 virtual ir_rvalue
*hir(exec_list
*instructions
,
181 struct _mesa_glsl_parse_state
*state
);
183 virtual void print(void) const;
185 enum ast_operators oper
;
187 ast_expression
*subexpressions
[3];
192 float float_constant
;
193 unsigned uint_constant
;
195 } primary_expression
;
199 * List of expressions for an \c ast_sequence or parameters for an
200 * \c ast_function_call
202 exec_list expressions
;
205 class ast_expression_bin
: public ast_expression
{
207 ast_expression_bin(int oper
, ast_expression
*, ast_expression
*);
209 virtual void print(void) const;
213 * Subclass of expressions for function calls
215 class ast_function_expression
: public ast_expression
{
217 ast_function_expression(ast_expression
*callee
)
218 : ast_expression(ast_function_call
, callee
,
225 ast_function_expression(class ast_type_specifier
*type
)
226 : ast_expression(ast_function_call
, (ast_expression
*) type
,
233 bool is_constructor() const
238 virtual ir_rvalue
*hir(exec_list
*instructions
,
239 struct _mesa_glsl_parse_state
*state
);
243 * Is this function call actually a constructor?
250 * Number of possible operators for an ast_expression
252 * This is done as a define instead of as an additional value in the enum so
253 * that the compiler won't generate spurious messages like "warning:
254 * enumeration value ‘ast_num_operators’ not handled in switch"
256 #define AST_NUM_OPERATORS (ast_sequence + 1)
259 class ast_compound_statement
: public ast_node
{
261 ast_compound_statement(int new_scope
, ast_node
*statements
);
262 virtual void print(void) const;
264 virtual ir_rvalue
*hir(exec_list
*instructions
,
265 struct _mesa_glsl_parse_state
*state
);
268 exec_list statements
;
271 class ast_declaration
: public ast_node
{
273 ast_declaration(char *identifier
, int is_array
, ast_expression
*array_size
,
274 ast_expression
*initializer
);
275 virtual void print(void) const;
280 ast_expression
*array_size
;
282 ast_expression
*initializer
;
287 ast_precision_high
= 0, /**< Default precision. */
288 ast_precision_medium
,
292 struct ast_type_qualifier
{
293 unsigned invariant
:1;
295 unsigned attribute
:1;
303 unsigned noperspective
:1;
305 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
307 unsigned origin_upper_left
:1;
308 unsigned pixel_center_integer
:1;
312 class ast_struct_specifier
: public ast_node
{
314 ast_struct_specifier(char *identifier
, ast_node
*declarator_list
);
315 virtual void print(void) const;
317 virtual ir_rvalue
*hir(exec_list
*instructions
,
318 struct _mesa_glsl_parse_state
*state
);
321 exec_list declarations
;
359 ast_sampler2drectshadow
,
360 ast_samplercubeshadow
,
363 ast_sampler1darrayshadow
,
364 ast_sampler2darrayshadow
,
383 class ast_type_specifier
: public ast_node
{
385 ast_type_specifier(int specifier
);
387 /** Construct a type specifier from a type name */
388 ast_type_specifier(const char *name
)
389 : type_specifier(ast_type_name
), type_name(name
), structure(NULL
),
390 is_array(false), array_size(NULL
), precision(ast_precision_high
)
395 /** Construct a type specifier from a structure definition */
396 ast_type_specifier(ast_struct_specifier
*s
)
397 : type_specifier(ast_struct
), type_name(s
->name
), structure(s
),
398 is_array(false), array_size(NULL
), precision(ast_precision_high
)
403 const struct glsl_type
*glsl_type(const char **name
,
404 struct _mesa_glsl_parse_state
*state
)
407 virtual void print(void) const;
409 ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
411 enum ast_types type_specifier
;
413 const char *type_name
;
414 ast_struct_specifier
*structure
;
417 ast_expression
*array_size
;
419 unsigned precision
:2;
423 class ast_fully_specified_type
: public ast_node
{
425 virtual void print(void) const;
426 bool has_qualifiers() const;
428 ast_type_qualifier qualifier
;
429 ast_type_specifier
*specifier
;
433 class ast_declarator_list
: public ast_node
{
435 ast_declarator_list(ast_fully_specified_type
*);
436 virtual void print(void) const;
438 virtual ir_rvalue
*hir(exec_list
*instructions
,
439 struct _mesa_glsl_parse_state
*state
);
441 ast_fully_specified_type
*type
;
442 exec_list declarations
;
445 * Special flag for vertex shader "invariant" declarations.
447 * Vertex shaders can contain "invariant" variable redeclarations that do
448 * not include a type. For example, "invariant gl_Position;". This flag
449 * is used to note these cases when no type is specified.
455 class ast_parameter_declarator
: public ast_node
{
457 ast_parameter_declarator()
459 this->identifier
= NULL
;
460 this->is_array
= false;
461 this->array_size
= 0;
464 virtual void print(void) const;
466 virtual ir_rvalue
*hir(exec_list
*instructions
,
467 struct _mesa_glsl_parse_state
*state
);
469 ast_fully_specified_type
*type
;
472 ast_expression
*array_size
;
474 static void parameters_to_hir(exec_list
*ast_parameters
,
475 bool formal
, exec_list
*ir_parameters
,
476 struct _mesa_glsl_parse_state
*state
);
479 /** Is this parameter declaration part of a formal parameter list? */
480 bool formal_parameter
;
483 * Is this parameter 'void' type?
485 * This field is set by \c ::hir.
491 class ast_function
: public ast_node
{
495 virtual void print(void) const;
497 virtual ir_rvalue
*hir(exec_list
*instructions
,
498 struct _mesa_glsl_parse_state
*state
);
500 ast_fully_specified_type
*return_type
;
503 exec_list parameters
;
507 * Is this prototype part of the function definition?
509 * Used by ast_function_definition::hir to process the parameters, etc.
517 * Function signature corresponding to this function prototype instance
519 * Used by ast_function_definition::hir to process the parameters, etc.
524 class ir_function_signature
*signature
;
526 friend class ast_function_definition
;
530 class ast_declaration_statement
: public ast_node
{
532 ast_declaration_statement(void);
541 class ast_function
*function
;
542 ast_declarator_list
*declarator
;
543 ast_type_specifier
*type
;
549 class ast_expression_statement
: public ast_node
{
551 ast_expression_statement(ast_expression
*);
552 virtual void print(void) const;
554 virtual ir_rvalue
*hir(exec_list
*instructions
,
555 struct _mesa_glsl_parse_state
*state
);
557 ast_expression
*expression
;
561 class ast_case_label
: public ast_node
{
565 * An expression of NULL means 'default'.
567 ast_expression
*expression
;
570 class ast_selection_statement
: public ast_node
{
572 ast_selection_statement(ast_expression
*condition
,
573 ast_node
*then_statement
,
574 ast_node
*else_statement
);
575 virtual void print(void) const;
577 virtual ir_rvalue
*hir(exec_list
*instructions
,
578 struct _mesa_glsl_parse_state
*state
);
580 ast_expression
*condition
;
581 ast_node
*then_statement
;
582 ast_node
*else_statement
;
586 class ast_switch_statement
: public ast_node
{
588 ast_expression
*expression
;
589 exec_list statements
;
592 class ast_iteration_statement
: public ast_node
{
594 ast_iteration_statement(int mode
, ast_node
*init
, ast_node
*condition
,
595 ast_expression
*rest_expression
, ast_node
*body
);
597 virtual void print(void) const;
599 virtual ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
601 enum ast_iteration_modes
{
608 ast_node
*init_statement
;
610 ast_expression
*rest_expression
;
616 * Generate IR from the condition of a loop
618 * This is factored out of ::hir because some loops have the condition
619 * test at the top (for and while), and others have it at the end (do-while).
621 void condition_to_hir(class ir_loop
*, struct _mesa_glsl_parse_state
*);
625 class ast_jump_statement
: public ast_node
{
627 ast_jump_statement(int mode
, ast_expression
*return_value
);
628 virtual void print(void) const;
630 virtual ir_rvalue
*hir(exec_list
*instructions
,
631 struct _mesa_glsl_parse_state
*state
);
633 enum ast_jump_modes
{
640 ast_expression
*opt_return_value
;
644 class ast_function_definition
: public ast_node
{
646 virtual void print(void) const;
648 virtual ir_rvalue
*hir(exec_list
*instructions
,
649 struct _mesa_glsl_parse_state
*state
);
651 ast_function
*prototype
;
652 ast_compound_statement
*body
;
657 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
);
660 _mesa_ast_field_selection_to_hir(const ast_expression
*expr
,
661 exec_list
*instructions
,
662 struct _mesa_glsl_parse_state
*state
);