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;
40 virtual void print(void) const;
41 virtual ir_rvalue *hir(exec_list *instructions,
42 struct _mesa_glsl_parse_state *state);
45 * Retrieve the source location of an AST node
47 * This function is primarily used to get the source position of an AST node
48 * into a form that can be passed to \c _mesa_glsl_error.
50 * \sa _mesa_glsl_error, ast_node::set_location
52 struct YYLTYPE get_location(void) const
56 locp.source = this->location.source;
57 locp.first_line = this->location.line;
58 locp.first_column = this->location.column;
59 locp.last_line = locp.first_line;
60 locp.last_column = locp.first_column;
66 * Set the source location of an AST node from a parser location
68 * \sa ast_node::get_location
70 void set_location(const struct YYLTYPE &locp)
72 this->location.source = locp.source;
73 this->location.line = locp.first_line;
74 this->location.column = locp.first_column;
92 ast_plus, /**< Unary + operator. */
147 class ast_expression : public ast_node {
149 ast_expression(int oper, ast_expression *,
150 ast_expression *, ast_expression *);
152 ast_expression(const char *identifier) :
155 subexpressions[0] = NULL;
156 subexpressions[1] = NULL;
157 subexpressions[2] = NULL;
158 primary_expression.identifier = (char *) identifier;
161 static const char *operator_string(enum ast_operators op);
163 virtual ir_rvalue *hir(exec_list *instructions,
164 struct _mesa_glsl_parse_state *state);
166 virtual void print(void) const;
168 enum ast_operators oper;
170 ast_expression *subexpressions[3];
175 float float_constant;
176 unsigned uint_constant;
178 } primary_expression;
182 * List of expressions for an \c ast_sequence or parameters for an
183 * \c ast_function_call
185 exec_list expressions;
188 class ast_expression_bin : public ast_expression {
190 ast_expression_bin(int oper, ast_expression *, ast_expression *);
192 virtual void print(void) const;
196 * Subclass of expressions for function calls
198 class ast_function_expression : public ast_expression {
200 ast_function_expression(ast_expression *callee)
201 : ast_expression(ast_function_call, callee,
208 ast_function_expression(class ast_type_specifier *type)
209 : ast_expression(ast_function_call, (ast_expression *) type,
216 bool is_constructor() const
221 virtual ir_rvalue *hir(exec_list *instructions,
222 struct _mesa_glsl_parse_state *state);
226 * Is this function call actually a constructor?
233 * Number of possible operators for an ast_expression
235 * This is done as a define instead of as an additional value in the enum so
236 * that the compiler won't generate spurious messages like "warning:
237 * enumeration value ‘ast_num_operators’ not handled in switch"
239 #define AST_NUM_OPERATORS (ast_sequence + 1)
242 class ast_compound_statement : public ast_node {
244 ast_compound_statement(int new_scope, ast_node *statements);
245 virtual void print(void) const;
247 virtual ir_rvalue *hir(exec_list *instructions,
248 struct _mesa_glsl_parse_state *state);
251 exec_list statements;
254 class ast_declaration : public ast_node {
256 ast_declaration(char *identifier, int is_array, ast_expression *array_size,
257 ast_expression *initializer);
258 virtual void print(void) const;
263 ast_expression *array_size;
265 ast_expression *initializer;
270 ast_precision_high = 0, /**< Default precision. */
271 ast_precision_medium,
275 struct ast_type_qualifier {
276 unsigned invariant:1;
278 unsigned attribute:1;
286 unsigned noperspective:1;
289 class ast_struct_specifier : public ast_node {
291 ast_struct_specifier(char *identifier, ast_node *declarator_list);
292 virtual void print(void) const;
294 virtual ir_rvalue *hir(exec_list *instructions,
295 struct _mesa_glsl_parse_state *state);
298 exec_list declarations;
336 ast_sampler2drectshadow,
337 ast_samplercubeshadow,
340 ast_sampler1darrayshadow,
341 ast_sampler2darrayshadow,
360 class ast_type_specifier : public ast_node {
362 ast_type_specifier(int specifier);
364 /** Construct a type specifier from a type name */
365 ast_type_specifier(const char *name)
366 : type_specifier(ast_type_name), type_name(name), structure(NULL),
367 is_array(false), array_size(NULL), precision(ast_precision_high)
372 /** Construct a type specifier from a structure definition */
373 ast_type_specifier(ast_struct_specifier *s)
374 : type_specifier(ast_struct), type_name(s->name), structure(s),
375 is_array(false), array_size(NULL), precision(ast_precision_high)
380 const struct glsl_type *glsl_type(const char **name,
381 struct _mesa_glsl_parse_state *state)
384 virtual void print(void) const;
386 ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
388 enum ast_types type_specifier;
390 const char *type_name;
391 ast_struct_specifier *structure;
394 ast_expression *array_size;
396 unsigned precision:2;
400 class ast_fully_specified_type : public ast_node {
402 virtual void print(void) const;
404 ast_type_qualifier qualifier;
405 ast_type_specifier *specifier;
409 class ast_declarator_list : public ast_node {
411 ast_declarator_list(ast_fully_specified_type *);
412 virtual void print(void) const;
414 virtual ir_rvalue *hir(exec_list *instructions,
415 struct _mesa_glsl_parse_state *state);
417 ast_fully_specified_type *type;
418 exec_list declarations;
421 * Special flag for vertex shader "invariant" declarations.
423 * Vertex shaders can contain "invariant" variable redeclarations that do
424 * not include a type. For example, "invariant gl_Position;". This flag
425 * is used to note these cases when no type is specified.
431 class ast_parameter_declarator : public ast_node {
433 virtual void print(void) const;
435 virtual ir_rvalue *hir(exec_list *instructions,
436 struct _mesa_glsl_parse_state *state);
438 ast_fully_specified_type *type;
441 ast_expression *array_size;
443 static void parameters_to_hir(exec_list *ast_parameters,
444 bool formal, exec_list *ir_parameters,
445 struct _mesa_glsl_parse_state *state);
448 /** Is this parameter declaration part of a formal parameter list? */
449 bool formal_parameter;
452 * Is this parameter 'void' type?
454 * This field is set by \c ::hir.
460 class ast_function : public ast_node {
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 *return_type;
472 exec_list parameters;
476 * Is this prototype part of the function definition?
478 * Used by ast_function_definition::hir to process the parameters, etc.
486 * Function signature corresponding to this function prototype instance
488 * Used by ast_function_definition::hir to process the parameters, etc.
493 class ir_function_signature *signature;
495 friend class ast_function_definition;
499 class ast_declaration_statement : public ast_node {
501 ast_declaration_statement(void);
510 class ast_function *function;
511 ast_declarator_list *declarator;
512 ast_type_specifier *type;
518 class ast_expression_statement : public ast_node {
520 ast_expression_statement(ast_expression *);
521 virtual void print(void) const;
523 virtual ir_rvalue *hir(exec_list *instructions,
524 struct _mesa_glsl_parse_state *state);
526 ast_expression *expression;
530 class ast_case_label : public ast_node {
534 * An expression of NULL means 'default'.
536 ast_expression *expression;
539 class ast_selection_statement : public ast_node {
541 ast_selection_statement(ast_expression *condition,
542 ast_node *then_statement,
543 ast_node *else_statement);
544 virtual void print(void) const;
546 virtual ir_rvalue *hir(exec_list *instructions,
547 struct _mesa_glsl_parse_state *state);
549 ast_expression *condition;
550 ast_node *then_statement;
551 ast_node *else_statement;
555 class ast_switch_statement : public ast_node {
557 ast_expression *expression;
558 exec_list statements;
561 class ast_iteration_statement : public ast_node {
563 ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
564 ast_expression *rest_expression, ast_node *body);
566 virtual void print(void) const;
568 virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
570 enum ast_iteration_modes {
577 ast_node *init_statement;
579 ast_expression *rest_expression;
585 * Generate IR from the condition of a loop
587 * This is factored out of ::hir because some loops have the condition
588 * test at the top (for and while), and others have it at the end (do-while).
590 void condition_to_hir(class ir_loop *, struct _mesa_glsl_parse_state *);
594 class ast_jump_statement : public ast_node {
596 ast_jump_statement(int mode, ast_expression *return_value);
597 virtual void print(void) const;
599 virtual ir_rvalue *hir(exec_list *instructions,
600 struct _mesa_glsl_parse_state *state);
602 enum ast_jump_modes {
609 ast_expression *opt_return_value;
613 class ast_function_definition : public ast_node {
615 virtual void print(void) const;
617 virtual ir_rvalue *hir(exec_list *instructions,
618 struct _mesa_glsl_parse_state *state);
620 ast_function *prototype;
621 ast_compound_statement *body;
626 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
628 extern struct ir_rvalue *
629 _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
630 exec_list *instructions,
631 struct _mesa_glsl_parse_state *state);