ast.h revision 3c6fea3048a0d9add2fec621d30c32f3519d8868
1/* -*- c++ -*- */ 2/* 3 * Copyright © 2009 Intel Corporation 4 * 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: 11 * 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 14 * Software. 15 * 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. 23 */ 24 25#pragma once 26#ifndef AST_H 27#define AST_H 28 29#include "main/simple_list.h" 30#include "list.h" 31#include "glsl_parser_extras.h" 32 33struct ir_instruction; 34struct _mesa_glsl_parse_state; 35 36struct YYLTYPE; 37 38class ast_node : public simple_node { 39public: 40 virtual ~ast_node(); 41 virtual void print(void) const; 42 virtual ir_rvalue *hir(exec_list *instructions, 43 struct _mesa_glsl_parse_state *state); 44 45 /** 46 * Retrieve the source location of an AST node 47 * 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. 50 * 51 * \sa _mesa_glsl_error, ast_node::set_location 52 */ 53 struct YYLTYPE get_location(void) const 54 { 55 struct YYLTYPE locp; 56 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; 62 63 return locp; 64 } 65 66 /** 67 * Set the source location of an AST node from a parser location 68 * 69 * \sa ast_node::get_location 70 */ 71 void set_location(const struct YYLTYPE *locp) 72 { 73 this->location.source = locp->source; 74 this->location.line = locp->first_line; 75 this->location.column = locp->first_column; 76 } 77 78 79 int type; 80 81 struct { 82 unsigned source; 83 unsigned line; 84 unsigned column; 85 } location; 86 87protected: 88 ast_node(void); 89}; 90 91 92enum ast_operators { 93 ast_assign, 94 ast_plus, /**< Unary + operator. */ 95 ast_neg, 96 ast_add, 97 ast_sub, 98 ast_mul, 99 ast_div, 100 ast_mod, 101 ast_lshift, 102 ast_rshift, 103 ast_less, 104 ast_greater, 105 ast_lequal, 106 ast_gequal, 107 ast_equal, 108 ast_nequal, 109 ast_bit_and, 110 ast_bit_xor, 111 ast_bit_or, 112 ast_bit_not, 113 ast_logic_and, 114 ast_logic_xor, 115 ast_logic_or, 116 ast_logic_not, 117 118 ast_mul_assign, 119 ast_div_assign, 120 ast_mod_assign, 121 ast_add_assign, 122 ast_sub_assign, 123 ast_ls_assign, 124 ast_rs_assign, 125 ast_and_assign, 126 ast_xor_assign, 127 ast_or_assign, 128 129 ast_conditional, 130 131 ast_pre_inc, 132 ast_pre_dec, 133 ast_post_inc, 134 ast_post_dec, 135 ast_field_selection, 136 ast_array_index, 137 138 ast_function_call, 139 140 ast_identifier, 141 ast_int_constant, 142 ast_uint_constant, 143 ast_float_constant, 144 ast_bool_constant, 145 146 ast_sequence 147}; 148 149class ast_expression : public ast_node { 150public: 151 ast_expression(int oper, ast_expression *, 152 ast_expression *, ast_expression *); 153 154 ast_expression(const char *identifier) : 155 oper(ast_identifier) 156 { 157 subexpressions[0] = NULL; 158 subexpressions[1] = NULL; 159 subexpressions[2] = NULL; 160 primary_expression.identifier = (char *) identifier; 161 } 162 163 static const char *operator_string(enum ast_operators op); 164 165 virtual ir_rvalue *hir(exec_list *instructions, 166 struct _mesa_glsl_parse_state *state); 167 168 virtual void print(void) const; 169 170 enum ast_operators oper; 171 172 ast_expression *subexpressions[3]; 173 174 union { 175 char *identifier; 176 int int_constant; 177 float float_constant; 178 unsigned uint_constant; 179 int bool_constant; 180 } primary_expression; 181 182 183 /** 184 * List of expressions for an \c ast_sequence. 185 */ 186 struct simple_node expressions; 187}; 188 189class ast_expression_bin : public ast_expression { 190public: 191 ast_expression_bin(int oper, ast_expression *, ast_expression *); 192 193 virtual void print(void) const; 194}; 195 196/** 197 * Subclass of expressions for function calls 198 */ 199class ast_function_expression : public ast_expression { 200public: 201 ast_function_expression(ast_expression *callee) 202 : ast_expression(ast_function_call, callee, 203 NULL, NULL), 204 cons(false) 205 { 206 /* empty */ 207 } 208 209 ast_function_expression(class ast_type_specifier *type) 210 : ast_expression(ast_function_call, (ast_expression *) type, 211 NULL, NULL), 212 cons(true) 213 { 214 /* empty */ 215 } 216 217 bool is_constructor() const 218 { 219 return cons; 220 } 221 222 virtual ir_rvalue *hir(exec_list *instructions, 223 struct _mesa_glsl_parse_state *state); 224 225private: 226 /** 227 * Is this function call actually a constructor? 228 */ 229 bool cons; 230}; 231 232 233/** 234 * Number of possible operators for an ast_expression 235 * 236 * This is done as a define instead of as an additional value in the enum so 237 * that the compiler won't generate spurious messages like "warning: 238 * enumeration value ‘ast_num_operators’ not handled in switch" 239 */ 240#define AST_NUM_OPERATORS (ast_sequence + 1) 241 242 243class ast_compound_statement : public ast_node { 244public: 245 ast_compound_statement(int new_scope, ast_node *statements); 246 virtual void print(void) const; 247 248 virtual ir_rvalue *hir(exec_list *instructions, 249 struct _mesa_glsl_parse_state *state); 250 251 int new_scope; 252 struct simple_node statements; 253}; 254 255class ast_declaration : public ast_node { 256public: 257 ast_declaration(char *identifier, int is_array, ast_expression *array_size, 258 ast_expression *initializer); 259 virtual void print(void) const; 260 261 char *identifier; 262 263 int is_array; 264 ast_expression *array_size; 265 266 ast_expression *initializer; 267}; 268 269 270enum { 271 ast_precision_high = 0, /**< Default precision. */ 272 ast_precision_medium, 273 ast_precision_low 274}; 275 276struct ast_type_qualifier { 277 unsigned invariant:1; 278 unsigned constant:1; 279 unsigned attribute:1; 280 unsigned varying:1; 281 unsigned in:1; 282 unsigned out:1; 283 unsigned centroid:1; 284 unsigned uniform:1; 285 unsigned smooth:1; 286 unsigned flat:1; 287 unsigned noperspective:1; 288}; 289 290class ast_struct_specifier : public ast_node { 291public: 292 ast_struct_specifier(char *identifier, ast_node *declarator_list); 293 virtual void print(void) const; 294 295 char *name; 296 struct simple_node declarations; 297}; 298 299 300enum ast_types { 301 ast_void, 302 ast_float, 303 ast_int, 304 ast_uint, 305 ast_bool, 306 ast_vec2, 307 ast_vec3, 308 ast_vec4, 309 ast_bvec2, 310 ast_bvec3, 311 ast_bvec4, 312 ast_ivec2, 313 ast_ivec3, 314 ast_ivec4, 315 ast_uvec2, 316 ast_uvec3, 317 ast_uvec4, 318 ast_mat2, 319 ast_mat2x3, 320 ast_mat2x4, 321 ast_mat3x2, 322 ast_mat3, 323 ast_mat3x4, 324 ast_mat4x2, 325 ast_mat4x3, 326 ast_mat4, 327 ast_sampler1d, 328 ast_sampler2d, 329 ast_sampler3d, 330 ast_samplercube, 331 ast_sampler1dshadow, 332 ast_sampler2dshadow, 333 ast_samplercubeshadow, 334 ast_sampler1darray, 335 ast_sampler2darray, 336 ast_sampler1darrayshadow, 337 ast_sampler2darrayshadow, 338 ast_isampler1d, 339 ast_isampler2d, 340 ast_isampler3d, 341 ast_isamplercube, 342 ast_isampler1darray, 343 ast_isampler2darray, 344 ast_usampler1d, 345 ast_usampler2d, 346 ast_usampler3d, 347 ast_usamplercube, 348 ast_usampler1darray, 349 ast_usampler2darray, 350 351 ast_struct, 352 ast_type_name 353}; 354 355 356class ast_type_specifier : public ast_node { 357public: 358 ast_type_specifier(int specifier); 359 360 /** Construct a type specifier from a type name */ 361 ast_type_specifier(const char *name) 362 : type_specifier(ast_type_name), type_name(name), structure(NULL), 363 is_array(false), array_size(NULL), precision(ast_precision_high) 364 { 365 /* empty */ 366 } 367 368 /** Construct a type specifier from a structure definition */ 369 ast_type_specifier(ast_struct_specifier *s) 370 : type_specifier(ast_struct), type_name(s->name), structure(s), 371 is_array(false), array_size(NULL), precision(ast_precision_high) 372 { 373 /* empty */ 374 } 375 376 virtual void print(void) const; 377 378 enum ast_types type_specifier; 379 380 const char *type_name; 381 ast_struct_specifier *structure; 382 383 int is_array; 384 ast_expression *array_size; 385 386 unsigned precision:2; 387}; 388 389 390class ast_fully_specified_type : public ast_node { 391public: 392 virtual void print(void) const; 393 394 ast_type_qualifier qualifier; 395 ast_type_specifier *specifier; 396}; 397 398 399class ast_declarator_list : public ast_node { 400public: 401 ast_declarator_list(ast_fully_specified_type *); 402 virtual void print(void) const; 403 404 virtual ir_rvalue *hir(exec_list *instructions, 405 struct _mesa_glsl_parse_state *state); 406 407 ast_fully_specified_type *type; 408 struct simple_node declarations; 409 410 /** 411 * Special flag for vertex shader "invariant" declarations. 412 * 413 * Vertex shaders can contain "invariant" variable redeclarations that do 414 * not include a type. For example, "invariant gl_Position;". This flag 415 * is used to note these cases when no type is specified. 416 */ 417 int invariant; 418}; 419 420 421class ast_parameter_declarator : public ast_node { 422public: 423 virtual void print(void) const; 424 425 virtual ir_rvalue *hir(exec_list *instructions, 426 struct _mesa_glsl_parse_state *state); 427 428 ast_fully_specified_type *type; 429 char *identifier; 430 int is_array; 431 ast_expression *array_size; 432}; 433 434 435class ast_function : public ast_node { 436public: 437 ast_function(void); 438 439 virtual void print(void) const; 440 441 ast_fully_specified_type *return_type; 442 char *identifier; 443 444 struct simple_node parameters; 445}; 446 447 448class ast_declaration_statement : public ast_node { 449public: 450 ast_declaration_statement(void); 451 452 enum { 453 ast_function, 454 ast_declaration, 455 ast_precision 456 } mode; 457 458 union { 459 class ast_function *function; 460 ast_declarator_list *declarator; 461 ast_type_specifier *type; 462 ast_node *node; 463 } declaration; 464}; 465 466 467class ast_expression_statement : public ast_node { 468public: 469 ast_expression_statement(ast_expression *); 470 virtual void print(void) const; 471 472 virtual ir_rvalue *hir(exec_list *instructions, 473 struct _mesa_glsl_parse_state *state); 474 475 ast_expression *expression; 476}; 477 478 479class ast_case_label : public ast_node { 480public: 481 482 /** 483 * An expression of NULL means 'default'. 484 */ 485 ast_expression *expression; 486}; 487 488class ast_selection_statement : public ast_node { 489public: 490 ast_selection_statement(ast_expression *condition, 491 ast_node *then_statement, 492 ast_node *else_statement); 493 virtual void print(void) const; 494 495 virtual ir_rvalue *hir(exec_list *instructions, 496 struct _mesa_glsl_parse_state *state); 497 498 ast_expression *condition; 499 ast_node *then_statement; 500 ast_node *else_statement; 501}; 502 503 504class ast_switch_statement : public ast_node { 505public: 506 ast_expression *expression; 507 struct simple_node statements; 508}; 509 510class ast_iteration_statement : public ast_node { 511public: 512 ast_iteration_statement(int mode, ast_node *init, ast_node *condition, 513 ast_expression *rest_expression, ast_node *body); 514 515 virtual void print(void) const; 516 517 enum ast_iteration_modes { 518 ast_for, 519 ast_while, 520 ast_do_while 521 } mode; 522 523 524 ast_node *init_statement; 525 ast_node *condition; 526 ast_expression *rest_expression; 527 528 ast_node *body; 529}; 530 531 532class ast_jump_statement : public ast_node { 533public: 534 ast_jump_statement(int mode, ast_expression *return_value); 535 virtual void print(void) const; 536 537 virtual ir_rvalue *hir(exec_list *instructions, 538 struct _mesa_glsl_parse_state *state); 539 540 enum ast_jump_modes { 541 ast_continue, 542 ast_break, 543 ast_return, 544 ast_discard 545 } mode; 546 547 ast_expression *opt_return_value; 548}; 549 550 551class ast_function_definition : public ast_node { 552public: 553 virtual void print(void) const; 554 555 virtual ir_rvalue *hir(exec_list *instructions, 556 struct _mesa_glsl_parse_state *state); 557 558 ast_function *prototype; 559 ast_compound_statement *body; 560}; 561 562 563extern void 564_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state); 565 566extern struct ir_rvalue * 567_mesa_ast_field_selection_to_hir(const struct ast_expression *expr, 568 exec_list *instructions, 569 struct _mesa_glsl_parse_state *state); 570 571#endif /* AST_H */ 572