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.
183 struct simple_node expressions
;
186 class ast_expression_bin
: public ast_expression
{
188 ast_expression_bin(int oper
, ast_expression
*, ast_expression
*);
190 virtual void print(void) const;
194 * Subclass of expressions for function calls
196 class ast_function_expression
: public ast_expression
{
198 ast_function_expression(ast_expression
*callee
)
199 : ast_expression(ast_function_call
, callee
,
206 ast_function_expression(class ast_type_specifier
*type
)
207 : ast_expression(ast_function_call
, (ast_expression
*) type
,
214 bool is_constructor() const
219 virtual ir_rvalue
*hir(exec_list
*instructions
,
220 struct _mesa_glsl_parse_state
*state
);
224 * Is this function call actually a constructor?
231 * Number of possible operators for an ast_expression
233 * This is done as a define instead of as an additional value in the enum so
234 * that the compiler won't generate spurious messages like "warning:
235 * enumeration value ‘ast_num_operators’ not handled in switch"
237 #define AST_NUM_OPERATORS (ast_sequence + 1)
240 class ast_compound_statement
: public ast_node
{
242 ast_compound_statement(int new_scope
, ast_node
*statements
);
243 virtual void print(void) const;
245 virtual ir_rvalue
*hir(exec_list
*instructions
,
246 struct _mesa_glsl_parse_state
*state
);
249 struct simple_node statements
;
252 class ast_declaration
: public ast_node
{
254 ast_declaration(char *identifier
, int is_array
, ast_expression
*array_size
,
255 ast_expression
*initializer
);
256 virtual void print(void) const;
261 ast_expression
*array_size
;
263 ast_expression
*initializer
;
268 ast_precision_high
= 0, /**< Default precision. */
269 ast_precision_medium
,
273 struct ast_type_qualifier
{
274 unsigned invariant
:1;
276 unsigned attribute
:1;
284 unsigned noperspective
:1;
287 class ast_struct_specifier
: public ast_node
{
289 ast_struct_specifier(char *identifier
, ast_node
*declarator_list
);
290 virtual void print(void) const;
293 struct simple_node declarations
;
331 ast_sampler2drectshadow
,
332 ast_samplercubeshadow
,
335 ast_sampler1darrayshadow
,
336 ast_sampler2darrayshadow
,
355 class ast_type_specifier
: public ast_node
{
357 ast_type_specifier(int specifier
);
359 /** Construct a type specifier from a type name */
360 ast_type_specifier(const char *name
)
361 : type_specifier(ast_type_name
), type_name(name
), structure(NULL
),
362 is_array(false), array_size(NULL
), precision(ast_precision_high
)
367 /** Construct a type specifier from a structure definition */
368 ast_type_specifier(ast_struct_specifier
*s
)
369 : type_specifier(ast_struct
), type_name(s
->name
), structure(s
),
370 is_array(false), array_size(NULL
), precision(ast_precision_high
)
375 const struct glsl_type
*glsl_type(const char **name
,
376 struct _mesa_glsl_parse_state
*state
)
379 virtual void print(void) const;
381 enum ast_types type_specifier
;
383 const char *type_name
;
384 ast_struct_specifier
*structure
;
387 ast_expression
*array_size
;
389 unsigned precision
:2;
393 class ast_fully_specified_type
: public ast_node
{
395 virtual void print(void) const;
397 ast_type_qualifier qualifier
;
398 ast_type_specifier
*specifier
;
402 class ast_declarator_list
: public ast_node
{
404 ast_declarator_list(ast_fully_specified_type
*);
405 virtual void print(void) const;
407 virtual ir_rvalue
*hir(exec_list
*instructions
,
408 struct _mesa_glsl_parse_state
*state
);
410 ast_fully_specified_type
*type
;
411 struct simple_node declarations
;
414 * Special flag for vertex shader "invariant" declarations.
416 * Vertex shaders can contain "invariant" variable redeclarations that do
417 * not include a type. For example, "invariant gl_Position;". This flag
418 * is used to note these cases when no type is specified.
424 class ast_parameter_declarator
: public ast_node
{
426 virtual void print(void) const;
428 virtual ir_rvalue
*hir(exec_list
*instructions
,
429 struct _mesa_glsl_parse_state
*state
);
431 ast_fully_specified_type
*type
;
434 ast_expression
*array_size
;
436 static void parameters_to_hir(simple_node
*ast_parameters
,
437 bool formal
, exec_list
*ir_parameters
,
438 struct _mesa_glsl_parse_state
*state
);
441 /** Is this parameter declaration part of a formal parameter list? */
442 bool formal_parameter
;
445 * Is this parameter 'void' type?
447 * This field is set by \c ::hir.
453 class ast_function
: public ast_node
{
457 virtual void print(void) const;
459 virtual ir_rvalue
*hir(exec_list
*instructions
,
460 struct _mesa_glsl_parse_state
*state
);
462 ast_fully_specified_type
*return_type
;
465 struct simple_node parameters
;
469 * Is this prototype part of the function definition?
471 * Used by ast_function_definition::hir to process the parameters, etc.
479 * Function signature corresponding to this function prototype instance
481 * Used by ast_function_definition::hir to process the parameters, etc.
486 class ir_function_signature
*signature
;
488 friend class ast_function_definition
;
492 class ast_declaration_statement
: public ast_node
{
494 ast_declaration_statement(void);
503 class ast_function
*function
;
504 ast_declarator_list
*declarator
;
505 ast_type_specifier
*type
;
511 class ast_expression_statement
: public ast_node
{
513 ast_expression_statement(ast_expression
*);
514 virtual void print(void) const;
516 virtual ir_rvalue
*hir(exec_list
*instructions
,
517 struct _mesa_glsl_parse_state
*state
);
519 ast_expression
*expression
;
523 class ast_case_label
: public ast_node
{
527 * An expression of NULL means 'default'.
529 ast_expression
*expression
;
532 class ast_selection_statement
: public ast_node
{
534 ast_selection_statement(ast_expression
*condition
,
535 ast_node
*then_statement
,
536 ast_node
*else_statement
);
537 virtual void print(void) const;
539 virtual ir_rvalue
*hir(exec_list
*instructions
,
540 struct _mesa_glsl_parse_state
*state
);
542 ast_expression
*condition
;
543 ast_node
*then_statement
;
544 ast_node
*else_statement
;
548 class ast_switch_statement
: public ast_node
{
550 ast_expression
*expression
;
551 struct simple_node statements
;
554 class ast_iteration_statement
: public ast_node
{
556 ast_iteration_statement(int mode
, ast_node
*init
, ast_node
*condition
,
557 ast_expression
*rest_expression
, ast_node
*body
);
559 virtual void print(void) const;
561 virtual ir_rvalue
*hir(exec_list
*, struct _mesa_glsl_parse_state
*);
563 enum ast_iteration_modes
{
570 ast_node
*init_statement
;
572 ast_expression
*rest_expression
;
578 * Generate IR from the condition of a loop
580 * This is factored out of ::hir because some loops have the condition
581 * test at the top (for and while), and others have it at the end (do-while).
583 void condition_to_hir(class ir_loop
*, struct _mesa_glsl_parse_state
*);
587 class ast_jump_statement
: public ast_node
{
589 ast_jump_statement(int mode
, ast_expression
*return_value
);
590 virtual void print(void) const;
592 virtual ir_rvalue
*hir(exec_list
*instructions
,
593 struct _mesa_glsl_parse_state
*state
);
595 enum ast_jump_modes
{
602 ast_expression
*opt_return_value
;
606 class ast_function_definition
: public ast_node
{
608 virtual void print(void) const;
610 virtual ir_rvalue
*hir(exec_list
*instructions
,
611 struct _mesa_glsl_parse_state
*state
);
613 ast_function
*prototype
;
614 ast_compound_statement
*body
;
619 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
);
621 extern struct ir_rvalue
*
622 _mesa_ast_field_selection_to_hir(const struct ast_expression
*expr
,
623 exec_list
*instructions
,
624 struct _mesa_glsl_parse_state
*state
);