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.
29 #include "main/simple_list.h"
31 #include "glsl_parser_extras.h"
33 struct ir_instruction
;
34 struct _mesa_glsl_parse_state
;
38 class ast_node
: public simple_node
{
41 virtual void print(void) const;
42 virtual ir_rvalue
*hir(exec_list
*instructions
,
43 struct _mesa_glsl_parse_state
*state
);
46 * Retrieve the source location of an AST node
48 * This function is primarily used to get the source position of an AST node
49 * into a form that can be passed to \c _mesa_glsl_error.
51 * \sa _mesa_glsl_error, ast_node::set_location
53 struct YYLTYPE
get_location(void) const
57 locp
.source
= this->location
.source
;
58 locp
.first_line
= this->location
.line
;
59 locp
.first_column
= this->location
.column
;
60 locp
.last_line
= locp
.first_line
;
61 locp
.last_column
= locp
.first_column
;
67 * Set the source location of an AST node from a parser location
69 * \sa ast_node::get_location
71 void set_location(const struct YYLTYPE
&locp
)
73 this->location
.source
= locp
.source
;
74 this->location
.line
= locp
.first_line
;
75 this->location
.column
= locp
.first_column
;
91 ast_plus
, /**< Unary + operator. */
146 class ast_expression
: public ast_node
{
148 ast_expression(int oper
, ast_expression
*,
149 ast_expression
*, ast_expression
*);
151 ast_expression(const char *identifier
) :
154 subexpressions
[0] = NULL
;
155 subexpressions
[1] = NULL
;
156 subexpressions
[2] = NULL
;
157 primary_expression
.identifier
= (char *) identifier
;
160 static const char *operator_string(enum ast_operators op
);
162 virtual ir_rvalue
*hir(exec_list
*instructions
,
163 struct _mesa_glsl_parse_state
*state
);
165 virtual void print(void) const;
167 enum ast_operators oper
;
169 ast_expression
*subexpressions
[3];
174 float float_constant
;
175 unsigned uint_constant
;
177 } primary_expression
;
181 * List of expressions for an \c ast_sequence or parameters for an
182 * \c ast_function_call
184 struct simple_node expressions
;
187 class ast_expression_bin
: public ast_expression
{
189 ast_expression_bin(int oper
, ast_expression
*, ast_expression
*);
191 virtual void print(void) const;
195 * Subclass of expressions for function calls
197 class ast_function_expression
: public ast_expression
{
199 ast_function_expression(ast_expression
*callee
)
200 : ast_expression(ast_function_call
, callee
,
207 ast_function_expression(class ast_type_specifier
*type
)
208 : ast_expression(ast_function_call
, (ast_expression
*) type
,
215 bool is_constructor() const
220 virtual ir_rvalue
*hir(exec_list
*instructions
,
221 struct _mesa_glsl_parse_state
*state
);
225 * Is this function call actually a constructor?
232 * Number of possible operators for an ast_expression
234 * This is done as a define instead of as an additional value in the enum so
235 * that the compiler won't generate spurious messages like "warning:
236 * enumeration value ‘ast_num_operators’ not handled in switch"
238 #define AST_NUM_OPERATORS (ast_sequence + 1)
241 class ast_compound_statement
: public ast_node
{
243 ast_compound_statement(int new_scope
, ast_node
*statements
);
244 virtual void print(void) const;
246 virtual ir_rvalue
*hir(exec_list
*instructions
,
247 struct _mesa_glsl_parse_state
*state
);
250 struct simple_node statements
;
253 class ast_declaration
: public ast_node
{
255 ast_declaration(char *identifier
, int is_array
, ast_expression
*array_size
,
256 ast_expression
*initializer
);
257 virtual void print(void) const;
262 ast_expression
*array_size
;
264 ast_expression
*initializer
;
269 ast_precision_high
= 0, /**< Default precision. */
270 ast_precision_medium
,
274 struct ast_type_qualifier
{
275 unsigned invariant
:1;
277 unsigned attribute
:1;
285 unsigned noperspective
:1;
288 class ast_struct_specifier
: public ast_node
{
290 ast_struct_specifier(char *identifier
, ast_node
*declarator_list
);
291 virtual void print(void) const;
293 virtual ir_rvalue
*hir(exec_list
*instructions
,
294 struct _mesa_glsl_parse_state
*state
);
297 struct simple_node declarations
;
335 ast_sampler2drectshadow
,
336 ast_samplercubeshadow
,
339 ast_sampler1darrayshadow
,
340 ast_sampler2darrayshadow
,
359 class ast_type_specifier
: public ast_node
{
361 ast_type_specifier(int specifier
);
363 /** Construct a type specifier from a type name */
364 ast_type_specifier(const char *name
)
365 : type_specifier(ast_type_name
), type_name(name
), structure(NULL
),
366 is_array(false), array_size(NULL
), precision(ast_precision_high
)
371 /** Construct a type specifier from a structure definition */
372 ast_type_specifier(ast_struct_specifier
*s
)
373 : type_specifier(ast_struct
), type_name(s
->name
), structure(s
),
374 is_array(false), array_size(NULL
), precision(ast_precision_high
)
379 const struct glsl_type
*glsl_type(const char **name
,
380 struct _mesa_glsl_parse_state
*state
)
383 virtual void print(void) const;
385 ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
387 enum ast_types type_specifier
;
389 const char *type_name
;
390 ast_struct_specifier
*structure
;
393 ast_expression
*array_size
;
395 unsigned precision
:2;
399 class ast_fully_specified_type
: public ast_node
{
401 virtual void print(void) const;
403 ast_type_qualifier qualifier
;
404 ast_type_specifier
*specifier
;
408 class ast_declarator_list
: public ast_node
{
410 ast_declarator_list(ast_fully_specified_type
*);
411 virtual void print(void) const;
413 virtual ir_rvalue
*hir(exec_list
*instructions
,
414 struct _mesa_glsl_parse_state
*state
);
416 ast_fully_specified_type
*type
;
417 struct simple_node declarations
;
420 * Special flag for vertex shader "invariant" declarations.
422 * Vertex shaders can contain "invariant" variable redeclarations that do
423 * not include a type. For example, "invariant gl_Position;". This flag
424 * is used to note these cases when no type is specified.
430 class ast_parameter_declarator
: public ast_node
{
432 virtual void print(void) const;
434 virtual ir_rvalue
*hir(exec_list
*instructions
,
435 struct _mesa_glsl_parse_state
*state
);
437 ast_fully_specified_type
*type
;
440 ast_expression
*array_size
;
442 static void parameters_to_hir(simple_node
*ast_parameters
,
443 bool formal
, exec_list
*ir_parameters
,
444 struct _mesa_glsl_parse_state
*state
);
447 /** Is this parameter declaration part of a formal parameter list? */
448 bool formal_parameter
;
451 * Is this parameter 'void' type?
453 * This field is set by \c ::hir.
459 class ast_function
: public ast_node
{
463 virtual void print(void) const;
465 virtual ir_rvalue
*hir(exec_list
*instructions
,
466 struct _mesa_glsl_parse_state
*state
);
468 ast_fully_specified_type
*return_type
;
471 struct simple_node parameters
;
475 * Is this prototype part of the function definition?
477 * Used by ast_function_definition::hir to process the parameters, etc.
485 * Function signature corresponding to this function prototype instance
487 * Used by ast_function_definition::hir to process the parameters, etc.
492 class ir_function_signature
*signature
;
494 friend class ast_function_definition
;
498 class ast_declaration_statement
: public ast_node
{
500 ast_declaration_statement(void);
509 class ast_function
*function
;
510 ast_declarator_list
*declarator
;
511 ast_type_specifier
*type
;
517 class ast_expression_statement
: public ast_node
{
519 ast_expression_statement(ast_expression
*);
520 virtual void print(void) const;
522 virtual ir_rvalue
*hir(exec_list
*instructions
,
523 struct _mesa_glsl_parse_state
*state
);
525 ast_expression
*expression
;
529 class ast_case_label
: public ast_node
{
533 * An expression of NULL means 'default'.
535 ast_expression
*expression
;
538 class ast_selection_statement
: public ast_node
{
540 ast_selection_statement(ast_expression
*condition
,
541 ast_node
*then_statement
,
542 ast_node
*else_statement
);
543 virtual void print(void) const;
545 virtual ir_rvalue
*hir(exec_list
*instructions
,
546 struct _mesa_glsl_parse_state
*state
);
548 ast_expression
*condition
;
549 ast_node
*then_statement
;
550 ast_node
*else_statement
;
554 class ast_switch_statement
: public ast_node
{
556 ast_expression
*expression
;
557 struct simple_node statements
;
560 class ast_iteration_statement
: public ast_node
{
562 ast_iteration_statement(int mode
, ast_node
*init
, ast_node
*condition
,
563 ast_expression
*rest_expression
, ast_node
*body
);
565 virtual void print(void) const;
567 virtual ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
569 enum ast_iteration_modes
{
576 ast_node
*init_statement
;
578 ast_expression
*rest_expression
;
584 * Generate IR from the condition of a loop
586 * This is factored out of ::hir because some loops have the condition
587 * test at the top (for and while), and others have it at the end (do-while).
589 void condition_to_hir(class ir_loop
*, struct _mesa_glsl_parse_state
*);
593 class ast_jump_statement
: public ast_node
{
595 ast_jump_statement(int mode
, ast_expression
*return_value
);
596 virtual void print(void) const;
598 virtual ir_rvalue
*hir(exec_list
*instructions
,
599 struct _mesa_glsl_parse_state
*state
);
601 enum ast_jump_modes
{
608 ast_expression
*opt_return_value
;
612 class ast_function_definition
: public ast_node
{
614 virtual void print(void) const;
616 virtual ir_rvalue
*hir(exec_list
*instructions
,
617 struct _mesa_glsl_parse_state
*state
);
619 ast_function
*prototype
;
620 ast_compound_statement
*body
;
625 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
);
627 extern struct ir_rvalue
*
628 _mesa_ast_field_selection_to_hir(const struct ast_expression
*expr
,
629 exec_list
*instructions
,
630 struct _mesa_glsl_parse_state
*state
);