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;
307 class ast_struct_specifier
: public ast_node
{
309 ast_struct_specifier(char *identifier
, ast_node
*declarator_list
);
310 virtual void print(void) const;
312 virtual ir_rvalue
*hir(exec_list
*instructions
,
313 struct _mesa_glsl_parse_state
*state
);
316 exec_list declarations
;
354 ast_sampler2drectshadow
,
355 ast_samplercubeshadow
,
358 ast_sampler1darrayshadow
,
359 ast_sampler2darrayshadow
,
378 class ast_type_specifier
: public ast_node
{
380 ast_type_specifier(int specifier
);
382 /** Construct a type specifier from a type name */
383 ast_type_specifier(const char *name
)
384 : type_specifier(ast_type_name
), type_name(name
), structure(NULL
),
385 is_array(false), array_size(NULL
), precision(ast_precision_high
)
390 /** Construct a type specifier from a structure definition */
391 ast_type_specifier(ast_struct_specifier
*s
)
392 : type_specifier(ast_struct
), type_name(s
->name
), structure(s
),
393 is_array(false), array_size(NULL
), precision(ast_precision_high
)
398 const struct glsl_type
*glsl_type(const char **name
,
399 struct _mesa_glsl_parse_state
*state
)
402 virtual void print(void) const;
404 ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
406 enum ast_types type_specifier
;
408 const char *type_name
;
409 ast_struct_specifier
*structure
;
412 ast_expression
*array_size
;
414 unsigned precision
:2;
418 class ast_fully_specified_type
: public ast_node
{
420 virtual void print(void) const;
421 bool has_qualifiers() const;
423 ast_type_qualifier qualifier
;
424 ast_type_specifier
*specifier
;
428 class ast_declarator_list
: public ast_node
{
430 ast_declarator_list(ast_fully_specified_type
*);
431 virtual void print(void) const;
433 virtual ir_rvalue
*hir(exec_list
*instructions
,
434 struct _mesa_glsl_parse_state
*state
);
436 ast_fully_specified_type
*type
;
437 exec_list declarations
;
440 * Special flag for vertex shader "invariant" declarations.
442 * Vertex shaders can contain "invariant" variable redeclarations that do
443 * not include a type. For example, "invariant gl_Position;". This flag
444 * is used to note these cases when no type is specified.
450 class ast_parameter_declarator
: public ast_node
{
452 virtual void print(void) const;
454 virtual ir_rvalue
*hir(exec_list
*instructions
,
455 struct _mesa_glsl_parse_state
*state
);
457 ast_fully_specified_type
*type
;
460 ast_expression
*array_size
;
462 static void parameters_to_hir(exec_list
*ast_parameters
,
463 bool formal
, exec_list
*ir_parameters
,
464 struct _mesa_glsl_parse_state
*state
);
467 /** Is this parameter declaration part of a formal parameter list? */
468 bool formal_parameter
;
471 * Is this parameter 'void' type?
473 * This field is set by \c ::hir.
479 class ast_function
: public ast_node
{
483 virtual void print(void) const;
485 virtual ir_rvalue
*hir(exec_list
*instructions
,
486 struct _mesa_glsl_parse_state
*state
);
488 ast_fully_specified_type
*return_type
;
491 exec_list parameters
;
495 * Is this prototype part of the function definition?
497 * Used by ast_function_definition::hir to process the parameters, etc.
505 * Function signature corresponding to this function prototype instance
507 * Used by ast_function_definition::hir to process the parameters, etc.
512 class ir_function_signature
*signature
;
514 friend class ast_function_definition
;
518 class ast_declaration_statement
: public ast_node
{
520 ast_declaration_statement(void);
529 class ast_function
*function
;
530 ast_declarator_list
*declarator
;
531 ast_type_specifier
*type
;
537 class ast_expression_statement
: public ast_node
{
539 ast_expression_statement(ast_expression
*);
540 virtual void print(void) const;
542 virtual ir_rvalue
*hir(exec_list
*instructions
,
543 struct _mesa_glsl_parse_state
*state
);
545 ast_expression
*expression
;
549 class ast_case_label
: public ast_node
{
553 * An expression of NULL means 'default'.
555 ast_expression
*expression
;
558 class ast_selection_statement
: public ast_node
{
560 ast_selection_statement(ast_expression
*condition
,
561 ast_node
*then_statement
,
562 ast_node
*else_statement
);
563 virtual void print(void) const;
565 virtual ir_rvalue
*hir(exec_list
*instructions
,
566 struct _mesa_glsl_parse_state
*state
);
568 ast_expression
*condition
;
569 ast_node
*then_statement
;
570 ast_node
*else_statement
;
574 class ast_switch_statement
: public ast_node
{
576 ast_expression
*expression
;
577 exec_list statements
;
580 class ast_iteration_statement
: public ast_node
{
582 ast_iteration_statement(int mode
, ast_node
*init
, ast_node
*condition
,
583 ast_expression
*rest_expression
, ast_node
*body
);
585 virtual void print(void) const;
587 virtual ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
589 enum ast_iteration_modes
{
596 ast_node
*init_statement
;
598 ast_expression
*rest_expression
;
604 * Generate IR from the condition of a loop
606 * This is factored out of ::hir because some loops have the condition
607 * test at the top (for and while), and others have it at the end (do-while).
609 void condition_to_hir(class ir_loop
*, struct _mesa_glsl_parse_state
*);
613 class ast_jump_statement
: public ast_node
{
615 ast_jump_statement(int mode
, ast_expression
*return_value
);
616 virtual void print(void) const;
618 virtual ir_rvalue
*hir(exec_list
*instructions
,
619 struct _mesa_glsl_parse_state
*state
);
621 enum ast_jump_modes
{
628 ast_expression
*opt_return_value
;
632 class ast_function_definition
: public ast_node
{
634 virtual void print(void) const;
636 virtual ir_rvalue
*hir(exec_list
*instructions
,
637 struct _mesa_glsl_parse_state
*state
);
639 ast_function
*prototype
;
640 ast_compound_statement
*body
;
645 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
);
647 extern struct ir_rvalue
*
648 _mesa_ast_field_selection_to_hir(const struct ast_expression
*expr
,
649 exec_list
*instructions
,
650 struct _mesa_glsl_parse_state
*state
);