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 ir_instruction
;
33 struct _mesa_glsl_parse_state
;
39 /* Callers of this talloc-based new need not call delete. It's
40 * easier to just talloc_free 'ctx' (or any of its ancestors). */
41 static void* operator new(size_t size
, void *ctx
)
45 node
= talloc_size(ctx
, size
);
51 /* If the user *does* call delete, that's OK, we will just
52 * talloc_free in that case. */
53 static void operator delete(void *table
)
58 virtual void print(void) const;
59 virtual ir_rvalue
*hir(exec_list
*instructions
,
60 struct _mesa_glsl_parse_state
*state
);
63 * Retrieve the source location of an AST node
65 * This function is primarily used to get the source position of an AST node
66 * into a form that can be passed to \c _mesa_glsl_error.
68 * \sa _mesa_glsl_error, ast_node::set_location
70 struct YYLTYPE
get_location(void) const
74 locp
.source
= this->location
.source
;
75 locp
.first_line
= this->location
.line
;
76 locp
.first_column
= this->location
.column
;
77 locp
.last_line
= locp
.first_line
;
78 locp
.last_column
= locp
.first_column
;
84 * Set the source location of an AST node from a parser location
86 * \sa ast_node::get_location
88 void set_location(const struct YYLTYPE
&locp
)
90 this->location
.source
= locp
.source
;
91 this->location
.line
= locp
.first_line
;
92 this->location
.column
= locp
.first_column
;
110 ast_plus
, /**< Unary + operator. */
165 class ast_expression
: public ast_node
{
167 ast_expression(int oper
, ast_expression
*,
168 ast_expression
*, ast_expression
*);
170 ast_expression(const char *identifier
) :
173 subexpressions
[0] = NULL
;
174 subexpressions
[1] = NULL
;
175 subexpressions
[2] = NULL
;
176 primary_expression
.identifier
= (char *) identifier
;
179 static const char *operator_string(enum ast_operators op
);
181 virtual ir_rvalue
*hir(exec_list
*instructions
,
182 struct _mesa_glsl_parse_state
*state
);
184 virtual void print(void) const;
186 enum ast_operators oper
;
188 ast_expression
*subexpressions
[3];
193 float float_constant
;
194 unsigned uint_constant
;
196 } primary_expression
;
200 * List of expressions for an \c ast_sequence or parameters for an
201 * \c ast_function_call
203 exec_list expressions
;
206 class ast_expression_bin
: public ast_expression
{
208 ast_expression_bin(int oper
, ast_expression
*, ast_expression
*);
210 virtual void print(void) const;
214 * Subclass of expressions for function calls
216 class ast_function_expression
: public ast_expression
{
218 ast_function_expression(ast_expression
*callee
)
219 : ast_expression(ast_function_call
, callee
,
226 ast_function_expression(class ast_type_specifier
*type
)
227 : ast_expression(ast_function_call
, (ast_expression
*) type
,
234 bool is_constructor() const
239 virtual ir_rvalue
*hir(exec_list
*instructions
,
240 struct _mesa_glsl_parse_state
*state
);
244 * Is this function call actually a constructor?
251 * Number of possible operators for an ast_expression
253 * This is done as a define instead of as an additional value in the enum so
254 * that the compiler won't generate spurious messages like "warning:
255 * enumeration value ‘ast_num_operators’ not handled in switch"
257 #define AST_NUM_OPERATORS (ast_sequence + 1)
260 class ast_compound_statement
: public ast_node
{
262 ast_compound_statement(int new_scope
, ast_node
*statements
);
263 virtual void print(void) const;
265 virtual ir_rvalue
*hir(exec_list
*instructions
,
266 struct _mesa_glsl_parse_state
*state
);
269 exec_list statements
;
272 class ast_declaration
: public ast_node
{
274 ast_declaration(char *identifier
, int is_array
, ast_expression
*array_size
,
275 ast_expression
*initializer
);
276 virtual void print(void) const;
281 ast_expression
*array_size
;
283 ast_expression
*initializer
;
288 ast_precision_high
= 0, /**< Default precision. */
289 ast_precision_medium
,
293 struct ast_type_qualifier
{
294 unsigned invariant
:1;
296 unsigned attribute
:1;
304 unsigned noperspective
:1;
306 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
308 unsigned origin_upper_left
:1;
309 unsigned pixel_center_integer
:1;
313 class ast_struct_specifier
: public ast_node
{
315 ast_struct_specifier(char *identifier
, ast_node
*declarator_list
);
316 virtual void print(void) const;
318 virtual ir_rvalue
*hir(exec_list
*instructions
,
319 struct _mesa_glsl_parse_state
*state
);
322 exec_list declarations
;
360 ast_sampler2drectshadow
,
361 ast_samplercubeshadow
,
364 ast_sampler1darrayshadow
,
365 ast_sampler2darrayshadow
,
384 class ast_type_specifier
: public ast_node
{
386 ast_type_specifier(int specifier
);
388 /** Construct a type specifier from a type name */
389 ast_type_specifier(const char *name
)
390 : type_specifier(ast_type_name
), type_name(name
), structure(NULL
),
391 is_array(false), array_size(NULL
), precision(ast_precision_high
)
396 /** Construct a type specifier from a structure definition */
397 ast_type_specifier(ast_struct_specifier
*s
)
398 : type_specifier(ast_struct
), type_name(s
->name
), structure(s
),
399 is_array(false), array_size(NULL
), precision(ast_precision_high
)
404 const struct glsl_type
*glsl_type(const char **name
,
405 struct _mesa_glsl_parse_state
*state
)
408 virtual void print(void) const;
410 ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
412 enum ast_types type_specifier
;
414 const char *type_name
;
415 ast_struct_specifier
*structure
;
418 ast_expression
*array_size
;
420 unsigned precision
:2;
424 class ast_fully_specified_type
: public ast_node
{
426 virtual void print(void) const;
427 bool has_qualifiers() const;
429 ast_type_qualifier qualifier
;
430 ast_type_specifier
*specifier
;
434 class ast_declarator_list
: public ast_node
{
436 ast_declarator_list(ast_fully_specified_type
*);
437 virtual void print(void) const;
439 virtual ir_rvalue
*hir(exec_list
*instructions
,
440 struct _mesa_glsl_parse_state
*state
);
442 ast_fully_specified_type
*type
;
443 exec_list declarations
;
446 * Special flag for vertex shader "invariant" declarations.
448 * Vertex shaders can contain "invariant" variable redeclarations that do
449 * not include a type. For example, "invariant gl_Position;". This flag
450 * is used to note these cases when no type is specified.
456 class ast_parameter_declarator
: public ast_node
{
458 ast_parameter_declarator()
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
);
659 extern struct ir_rvalue
*
660 _mesa_ast_field_selection_to_hir(const struct ast_expression
*expr
,
661 exec_list
*instructions
,
662 struct _mesa_glsl_parse_state
*state
);