1%{ 2/* 3 * Copyright © 2008, 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#include <stdio.h> 25#include <stdlib.h> 26#include <string.h> 27#include <assert.h> 28 29#include "ast.h" 30#include "glsl_parser_extras.h" 31#include "glsl_types.h" 32 33#define YYLEX_PARAM state->scanner 34 35%} 36 37%pure-parser 38%error-verbose 39 40%locations 41%initial-action { 42 @$.first_line = 1; 43 @$.first_column = 1; 44 @$.last_line = 1; 45 @$.last_column = 1; 46 @$.source = 0; 47} 48 49%lex-param {void *scanner} 50%parse-param {struct _mesa_glsl_parse_state *state} 51 52%union { 53 int n; 54 float real; 55 char *identifier; 56 57 struct ast_type_qualifier type_qualifier; 58 59 ast_node *node; 60 ast_type_specifier *type_specifier; 61 ast_fully_specified_type *fully_specified_type; 62 ast_function *function; 63 ast_parameter_declarator *parameter_declarator; 64 ast_function_definition *function_definition; 65 ast_compound_statement *compound_statement; 66 ast_expression *expression; 67 ast_declarator_list *declarator_list; 68 ast_struct_specifier *struct_specifier; 69 ast_declaration *declaration; 70 71 struct { 72 ast_node *cond; 73 ast_expression *rest; 74 } for_rest_statement; 75 76 struct { 77 ast_node *then_statement; 78 ast_node *else_statement; 79 } selection_rest_statement; 80} 81 82%token ATTRIBUTE CONST_TOK BOOL_TOK FLOAT_TOK INT_TOK UINT_TOK 83%token BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT 84%token BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 UVEC2 UVEC3 UVEC4 VEC2 VEC3 VEC4 85%token CENTROID IN_TOK OUT_TOK INOUT_TOK UNIFORM VARYING 86%token NOPERSPECTIVE FLAT SMOOTH 87%token MAT2X2 MAT2X3 MAT2X4 88%token MAT3X2 MAT3X3 MAT3X4 89%token MAT4X2 MAT4X3 MAT4X4 90%token SAMPLER1D SAMPLER2D SAMPLER3D SAMPLERCUBE SAMPLER1DSHADOW SAMPLER2DSHADOW 91%token SAMPLERCUBESHADOW SAMPLER1DARRAY SAMPLER2DARRAY SAMPLER1DARRAYSHADOW 92%token SAMPLER2DARRAYSHADOW ISAMPLER1D ISAMPLER2D ISAMPLER3D ISAMPLERCUBE 93%token ISAMPLER1DARRAY ISAMPLER2DARRAY USAMPLER1D USAMPLER2D USAMPLER3D 94%token USAMPLERCUBE USAMPLER1DARRAY USAMPLER2DARRAY 95%token STRUCT VOID_TOK WHILE 96%token <identifier> IDENTIFIER 97%token <real> FLOATCONSTANT 98%token <n> INTCONSTANT UINTCONSTANT BOOLCONSTANT 99%token <identifier> FIELD_SELECTION 100%token LEFT_OP RIGHT_OP 101%token INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP 102%token AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN 103%token MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN 104%token SUB_ASSIGN 105%token INVARIANT 106%token LOWP MEDIUMP HIGHP SUPERP PRECISION 107 108%token VERSION EXTENSION LINE COLON EOL INTERFACE OUTPUT 109%token PRAGMA_DEBUG_ON PRAGMA_DEBUG_OFF 110%token PRAGMA_OPTIMIZE_ON PRAGMA_OPTIMIZE_OFF 111%token LAYOUT_TOK 112 113 /* Reserved words that are not actually used in the grammar. 114 */ 115%token ASM CLASS UNION ENUM TYPEDEF TEMPLATE THIS PACKED_TOK GOTO 116%token INLINE_TOK NOINLINE VOLATILE PUBLIC_TOK STATIC EXTERN EXTERNAL 117%token LONG_TOK SHORT_TOK DOUBLE_TOK HALF FIXED_TOK UNSIGNED INPUT_TOK OUPTUT 118%token HVEC2 HVEC3 HVEC4 DVEC2 DVEC3 DVEC4 FVEC2 FVEC3 FVEC4 119%token SAMPLER2DRECT SAMPLER3DRECT SAMPLER2DRECTSHADOW 120%token SIZEOF CAST NAMESPACE USING 121 122%token ERROR_TOK 123 124%token COMMON PARTITION ACTIVE SAMPLERBUFFER FILTER 125%token IMAGE1D IMAGE2D IMAGE3D IMAGECUBE IMAGE1DARRAY IMAGE2DARRAY 126%token IIMAGE1D IIMAGE2D IIMAGE3D IIMAGECUBE IIMAGE1DARRAY IIMAGE2DARRAY 127%token UIMAGE1D UIMAGE2D UIMAGE3D UIMAGECUBE UIMAGE1DARRAY UIMAGE2DARRAY 128%token IMAGE1DSHADOW IMAGE2DSHADOW IMAGEBUFFER IIMAGEBUFFER UIMAGEBUFFER 129%token IMAGE1DARRAYSHADOW IMAGE2DARRAYSHADOW 130%token ROW_MAJOR 131 132%type <identifier> variable_identifier 133%type <node> statement 134%type <node> statement_list 135%type <node> simple_statement 136%type <n> precision_qualifier 137%type <type_qualifier> type_qualifier 138%type <type_qualifier> storage_qualifier 139%type <type_qualifier> interpolation_qualifier 140%type <type_qualifier> layout_qualifier 141%type <type_qualifier> layout_qualifier_id_list layout_qualifier_id 142%type <type_specifier> type_specifier 143%type <type_specifier> type_specifier_no_prec 144%type <type_specifier> type_specifier_nonarray 145%type <n> basic_type_specifier_nonarray 146%type <fully_specified_type> fully_specified_type 147%type <function> function_prototype 148%type <function> function_header 149%type <function> function_header_with_parameters 150%type <function> function_declarator 151%type <parameter_declarator> parameter_declarator 152%type <parameter_declarator> parameter_declaration 153%type <type_qualifier> parameter_qualifier 154%type <type_qualifier> parameter_type_qualifier 155%type <type_specifier> parameter_type_specifier 156%type <function_definition> function_definition 157%type <compound_statement> compound_statement_no_new_scope 158%type <compound_statement> compound_statement 159%type <node> statement_no_new_scope 160%type <node> expression_statement 161%type <expression> expression 162%type <expression> primary_expression 163%type <expression> assignment_expression 164%type <expression> conditional_expression 165%type <expression> logical_or_expression 166%type <expression> logical_xor_expression 167%type <expression> logical_and_expression 168%type <expression> inclusive_or_expression 169%type <expression> exclusive_or_expression 170%type <expression> and_expression 171%type <expression> equality_expression 172%type <expression> relational_expression 173%type <expression> shift_expression 174%type <expression> additive_expression 175%type <expression> multiplicative_expression 176%type <expression> unary_expression 177%type <expression> constant_expression 178%type <expression> integer_expression 179%type <expression> postfix_expression 180%type <expression> function_call_header_with_parameters 181%type <expression> function_call_header_no_parameters 182%type <expression> function_call_header 183%type <expression> function_call_generic 184%type <expression> function_call_or_method 185%type <expression> function_call 186%type <n> assignment_operator 187%type <n> unary_operator 188%type <expression> function_identifier 189%type <node> external_declaration 190%type <declarator_list> init_declarator_list 191%type <declarator_list> single_declaration 192%type <expression> initializer 193%type <node> declaration 194%type <node> declaration_statement 195%type <node> jump_statement 196%type <struct_specifier> struct_specifier 197%type <node> struct_declaration_list 198%type <declarator_list> struct_declaration 199%type <declaration> struct_declarator 200%type <declaration> struct_declarator_list 201%type <node> selection_statement 202%type <selection_rest_statement> selection_rest_statement 203%type <node> iteration_statement 204%type <node> condition 205%type <node> conditionopt 206%type <node> for_init_statement 207%type <for_rest_statement> for_rest_statement 208%% 209 210translation_unit: 211 version_statement extension_statement_list 212 { 213 _mesa_glsl_initialize_types(state); 214 } 215 external_declaration_list 216 ; 217 218version_statement: 219 /* blank - no #version specified: defaults are already set */ 220 | VERSION INTCONSTANT EOL 221 { 222 switch ($2) { 223 case 100: 224 state->es_shader = true; 225 case 110: 226 case 120: 227 case 130: 228 /* FINISHME: Check against implementation support versions. */ 229 state->language_version = $2; 230 break; 231 default: 232 _mesa_glsl_error(& @2, state, "Shading language version" 233 "%u is not supported\n", $2); 234 break; 235 } 236 } 237 ; 238 239pragma_statement: 240 PRAGMA_DEBUG_ON EOL 241 | PRAGMA_DEBUG_OFF EOL 242 | PRAGMA_OPTIMIZE_ON EOL 243 | PRAGMA_OPTIMIZE_OFF EOL 244 ; 245 246extension_statement_list: 247 248 | extension_statement_list extension_statement 249 ; 250 251extension_statement: 252 EXTENSION IDENTIFIER COLON IDENTIFIER EOL 253 { 254 if (!_mesa_glsl_process_extension($2, & @2, $4, & @4, state)) { 255 YYERROR; 256 } 257 } 258 ; 259 260external_declaration_list: 261 external_declaration 262 { 263 /* FINISHME: The NULL test is only required because 'precision' 264 * FINISHME: statements are not yet supported. 265 */ 266 if ($1 != NULL) 267 state->translation_unit.push_tail(& $1->link); 268 } 269 | external_declaration_list external_declaration 270 { 271 /* FINISHME: The NULL test is only required because 'precision' 272 * FINISHME: statements are not yet supported. 273 */ 274 if ($2 != NULL) 275 state->translation_unit.push_tail(& $2->link); 276 } 277 ; 278 279variable_identifier: 280 IDENTIFIER 281 ; 282 283primary_expression: 284 variable_identifier 285 { 286 void *ctx = state; 287 $$ = new(ctx) ast_expression(ast_identifier, NULL, NULL, NULL); 288 $$->set_location(yylloc); 289 $$->primary_expression.identifier = $1; 290 } 291 | INTCONSTANT 292 { 293 void *ctx = state; 294 $$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL); 295 $$->set_location(yylloc); 296 $$->primary_expression.int_constant = $1; 297 } 298 | UINTCONSTANT 299 { 300 void *ctx = state; 301 $$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL); 302 $$->set_location(yylloc); 303 $$->primary_expression.uint_constant = $1; 304 } 305 | FLOATCONSTANT 306 { 307 void *ctx = state; 308 $$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL); 309 $$->set_location(yylloc); 310 $$->primary_expression.float_constant = $1; 311 } 312 | BOOLCONSTANT 313 { 314 void *ctx = state; 315 $$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL); 316 $$->set_location(yylloc); 317 $$->primary_expression.bool_constant = $1; 318 } 319 | '(' expression ')' 320 { 321 $$ = $2; 322 } 323 ; 324 325postfix_expression: 326 primary_expression 327 | postfix_expression '[' integer_expression ']' 328 { 329 void *ctx = state; 330 $$ = new(ctx) ast_expression(ast_array_index, $1, $3, NULL); 331 $$->set_location(yylloc); 332 } 333 | function_call 334 { 335 $$ = $1; 336 } 337 | postfix_expression '.' IDENTIFIER 338 { 339 void *ctx = state; 340 $$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL); 341 $$->set_location(yylloc); 342 $$->primary_expression.identifier = $3; 343 } 344 | postfix_expression INC_OP 345 { 346 void *ctx = state; 347 $$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL); 348 $$->set_location(yylloc); 349 } 350 | postfix_expression DEC_OP 351 { 352 void *ctx = state; 353 $$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL); 354 $$->set_location(yylloc); 355 } 356 ; 357 358integer_expression: 359 expression 360 ; 361 362function_call: 363 function_call_or_method 364 ; 365 366function_call_or_method: 367 function_call_generic 368 | postfix_expression '.' function_call_generic 369 { 370 void *ctx = state; 371 $$ = new(ctx) ast_expression(ast_field_selection, $1, $3, NULL); 372 $$->set_location(yylloc); 373 } 374 ; 375 376function_call_generic: 377 function_call_header_with_parameters ')' 378 | function_call_header_no_parameters ')' 379 ; 380 381function_call_header_no_parameters: 382 function_call_header VOID_TOK 383 | function_call_header 384 ; 385 386function_call_header_with_parameters: 387 function_call_header assignment_expression 388 { 389 $$ = $1; 390 $$->set_location(yylloc); 391 $$->expressions.push_tail(& $2->link); 392 } 393 | function_call_header_with_parameters ',' assignment_expression 394 { 395 $$ = $1; 396 $$->set_location(yylloc); 397 $$->expressions.push_tail(& $3->link); 398 } 399 ; 400 401 // Grammar Note: Constructors look like functions, but lexical 402 // analysis recognized most of them as keywords. They are now 403 // recognized through "type_specifier". 404function_call_header: 405 function_identifier '(' 406 ; 407 408function_identifier: 409 type_specifier 410 { 411 void *ctx = state; 412 $$ = new(ctx) ast_function_expression($1); 413 $$->set_location(yylloc); 414 } 415 | IDENTIFIER 416 { 417 void *ctx = state; 418 ast_expression *callee = new(ctx) ast_expression($1); 419 $$ = new(ctx) ast_function_expression(callee); 420 $$->set_location(yylloc); 421 } 422 | FIELD_SELECTION 423 { 424 void *ctx = state; 425 ast_expression *callee = new(ctx) ast_expression($1); 426 $$ = new(ctx) ast_function_expression(callee); 427 $$->set_location(yylloc); 428 } 429 ; 430 431 // Grammar Note: No traditional style type casts. 432unary_expression: 433 postfix_expression 434 | INC_OP unary_expression 435 { 436 void *ctx = state; 437 $$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL); 438 $$->set_location(yylloc); 439 } 440 | DEC_OP unary_expression 441 { 442 void *ctx = state; 443 $$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL); 444 $$->set_location(yylloc); 445 } 446 | unary_operator unary_expression 447 { 448 void *ctx = state; 449 $$ = new(ctx) ast_expression($1, $2, NULL, NULL); 450 $$->set_location(yylloc); 451 } 452 ; 453 454 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported. 455unary_operator: 456 '+' { $$ = ast_plus; } 457 | '-' { $$ = ast_neg; } 458 | '!' { $$ = ast_logic_not; } 459 | '~' { $$ = ast_bit_not; } 460 ; 461 462multiplicative_expression: 463 unary_expression 464 | multiplicative_expression '*' unary_expression 465 { 466 void *ctx = state; 467 $$ = new(ctx) ast_expression_bin(ast_mul, $1, $3); 468 $$->set_location(yylloc); 469 } 470 | multiplicative_expression '/' unary_expression 471 { 472 void *ctx = state; 473 $$ = new(ctx) ast_expression_bin(ast_div, $1, $3); 474 $$->set_location(yylloc); 475 } 476 | multiplicative_expression '%' unary_expression 477 { 478 void *ctx = state; 479 $$ = new(ctx) ast_expression_bin(ast_mod, $1, $3); 480 $$->set_location(yylloc); 481 } 482 ; 483 484additive_expression: 485 multiplicative_expression 486 | additive_expression '+' multiplicative_expression 487 { 488 void *ctx = state; 489 $$ = new(ctx) ast_expression_bin(ast_add, $1, $3); 490 $$->set_location(yylloc); 491 } 492 | additive_expression '-' multiplicative_expression 493 { 494 void *ctx = state; 495 $$ = new(ctx) ast_expression_bin(ast_sub, $1, $3); 496 $$->set_location(yylloc); 497 } 498 ; 499 500shift_expression: 501 additive_expression 502 | shift_expression LEFT_OP additive_expression 503 { 504 void *ctx = state; 505 $$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3); 506 $$->set_location(yylloc); 507 } 508 | shift_expression RIGHT_OP additive_expression 509 { 510 void *ctx = state; 511 $$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3); 512 $$->set_location(yylloc); 513 } 514 ; 515 516relational_expression: 517 shift_expression 518 | relational_expression '<' shift_expression 519 { 520 void *ctx = state; 521 $$ = new(ctx) ast_expression_bin(ast_less, $1, $3); 522 $$->set_location(yylloc); 523 } 524 | relational_expression '>' shift_expression 525 { 526 void *ctx = state; 527 $$ = new(ctx) ast_expression_bin(ast_greater, $1, $3); 528 $$->set_location(yylloc); 529 } 530 | relational_expression LE_OP shift_expression 531 { 532 void *ctx = state; 533 $$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3); 534 $$->set_location(yylloc); 535 } 536 | relational_expression GE_OP shift_expression 537 { 538 void *ctx = state; 539 $$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3); 540 $$->set_location(yylloc); 541 } 542 ; 543 544equality_expression: 545 relational_expression 546 | equality_expression EQ_OP relational_expression 547 { 548 void *ctx = state; 549 $$ = new(ctx) ast_expression_bin(ast_equal, $1, $3); 550 $$->set_location(yylloc); 551 } 552 | equality_expression NE_OP relational_expression 553 { 554 void *ctx = state; 555 $$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3); 556 $$->set_location(yylloc); 557 } 558 ; 559 560and_expression: 561 equality_expression 562 | and_expression '&' equality_expression 563 { 564 void *ctx = state; 565 $$ = new(ctx) ast_expression_bin(ast_bit_and, $1, $3); 566 $$->set_location(yylloc); 567 } 568 ; 569 570exclusive_or_expression: 571 and_expression 572 | exclusive_or_expression '^' and_expression 573 { 574 void *ctx = state; 575 $$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3); 576 $$->set_location(yylloc); 577 } 578 ; 579 580inclusive_or_expression: 581 exclusive_or_expression 582 | inclusive_or_expression '|' exclusive_or_expression 583 { 584 void *ctx = state; 585 $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3); 586 $$->set_location(yylloc); 587 } 588 ; 589 590logical_and_expression: 591 inclusive_or_expression 592 | logical_and_expression AND_OP inclusive_or_expression 593 { 594 void *ctx = state; 595 $$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3); 596 $$->set_location(yylloc); 597 } 598 ; 599 600logical_xor_expression: 601 logical_and_expression 602 | logical_xor_expression XOR_OP logical_and_expression 603 { 604 void *ctx = state; 605 $$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3); 606 $$->set_location(yylloc); 607 } 608 ; 609 610logical_or_expression: 611 logical_xor_expression 612 | logical_or_expression OR_OP logical_xor_expression 613 { 614 void *ctx = state; 615 $$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3); 616 $$->set_location(yylloc); 617 } 618 ; 619 620conditional_expression: 621 logical_or_expression 622 | logical_or_expression '?' expression ':' assignment_expression 623 { 624 void *ctx = state; 625 $$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5); 626 $$->set_location(yylloc); 627 } 628 ; 629 630assignment_expression: 631 conditional_expression 632 | unary_expression assignment_operator assignment_expression 633 { 634 void *ctx = state; 635 $$ = new(ctx) ast_expression($2, $1, $3, NULL); 636 $$->set_location(yylloc); 637 } 638 ; 639 640assignment_operator: 641 '=' { $$ = ast_assign; } 642 | MUL_ASSIGN { $$ = ast_mul_assign; } 643 | DIV_ASSIGN { $$ = ast_div_assign; } 644 | MOD_ASSIGN { $$ = ast_mod_assign; } 645 | ADD_ASSIGN { $$ = ast_add_assign; } 646 | SUB_ASSIGN { $$ = ast_sub_assign; } 647 | LEFT_ASSIGN { $$ = ast_ls_assign; } 648 | RIGHT_ASSIGN { $$ = ast_rs_assign; } 649 | AND_ASSIGN { $$ = ast_and_assign; } 650 | XOR_ASSIGN { $$ = ast_xor_assign; } 651 | OR_ASSIGN { $$ = ast_or_assign; } 652 ; 653 654expression: 655 assignment_expression 656 { 657 $$ = $1; 658 } 659 | expression ',' assignment_expression 660 { 661 void *ctx = state; 662 if ($1->oper != ast_sequence) { 663 $$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL); 664 $$->set_location(yylloc); 665 $$->expressions.push_tail(& $1->link); 666 } else { 667 $$ = $1; 668 } 669 670 $$->expressions.push_tail(& $3->link); 671 } 672 ; 673 674constant_expression: 675 conditional_expression 676 ; 677 678declaration: 679 function_prototype ';' 680 { 681 $$ = $1; 682 } 683 | init_declarator_list ';' 684 { 685 $$ = $1; 686 } 687 | PRECISION precision_qualifier type_specifier_no_prec ';' 688 { 689 if (($3->type_specifier != ast_float) 690 && ($3->type_specifier != ast_int)) { 691 _mesa_glsl_error(& @3, state, "global precision qualifier can " 692 "only be applied to `int' or `float'\n"); 693 YYERROR; 694 } 695 696 $$ = NULL; /* FINISHME */ 697 } 698 ; 699 700function_prototype: 701 function_declarator ')' 702 ; 703 704function_declarator: 705 function_header 706 | function_header_with_parameters 707 ; 708 709function_header_with_parameters: 710 function_header parameter_declaration 711 { 712 $$ = $1; 713 $$->parameters.push_tail(& $2->link); 714 } 715 | function_header_with_parameters ',' parameter_declaration 716 { 717 $$ = $1; 718 $$->parameters.push_tail(& $3->link); 719 } 720 ; 721 722function_header: 723 fully_specified_type IDENTIFIER '(' 724 { 725 void *ctx = state; 726 $$ = new(ctx) ast_function(); 727 $$->set_location(yylloc); 728 $$->return_type = $1; 729 $$->identifier = $2; 730 } 731 ; 732 733parameter_declarator: 734 type_specifier IDENTIFIER 735 { 736 void *ctx = state; 737 $$ = new(ctx) ast_parameter_declarator(); 738 $$->set_location(yylloc); 739 $$->type = new(ctx) ast_fully_specified_type(); 740 $$->type->set_location(yylloc); 741 $$->type->specifier = $1; 742 $$->identifier = $2; 743 } 744 | type_specifier IDENTIFIER '[' constant_expression ']' 745 { 746 void *ctx = state; 747 $$ = new(ctx) ast_parameter_declarator(); 748 $$->set_location(yylloc); 749 $$->type = new(ctx) ast_fully_specified_type(); 750 $$->type->set_location(yylloc); 751 $$->type->specifier = $1; 752 $$->identifier = $2; 753 $$->is_array = true; 754 $$->array_size = $4; 755 } 756 ; 757 758parameter_declaration: 759 parameter_type_qualifier parameter_qualifier parameter_declarator 760 { 761 $1.flags.i |= $2.flags.i; 762 763 $$ = $3; 764 $$->type->qualifier = $1; 765 } 766 | parameter_qualifier parameter_declarator 767 { 768 $$ = $2; 769 $$->type->qualifier = $1; 770 } 771 | parameter_type_qualifier parameter_qualifier parameter_type_specifier 772 { 773 void *ctx = state; 774 $1.flags.i |= $2.flags.i; 775 776 $$ = new(ctx) ast_parameter_declarator(); 777 $$->set_location(yylloc); 778 $$->type = new(ctx) ast_fully_specified_type(); 779 $$->type->qualifier = $1; 780 $$->type->specifier = $3; 781 } 782 | parameter_qualifier parameter_type_specifier 783 { 784 void *ctx = state; 785 $$ = new(ctx) ast_parameter_declarator(); 786 $$->set_location(yylloc); 787 $$->type = new(ctx) ast_fully_specified_type(); 788 $$->type->qualifier = $1; 789 $$->type->specifier = $2; 790 } 791 ; 792 793parameter_qualifier: 794 /* empty */ 795 { 796 memset(& $$, 0, sizeof($$)); 797 } 798 | IN_TOK 799 { 800 memset(& $$, 0, sizeof($$)); 801 $$.flags.q.in = 1; 802 } 803 | OUT_TOK 804 { 805 memset(& $$, 0, sizeof($$)); 806 $$.flags.q.out = 1; 807 } 808 | INOUT_TOK 809 { 810 memset(& $$, 0, sizeof($$)); 811 $$.flags.q.in = 1; 812 $$.flags.q.out = 1; 813 } 814 ; 815 816parameter_type_specifier: 817 type_specifier 818 ; 819 820init_declarator_list: 821 single_declaration 822 | init_declarator_list ',' IDENTIFIER 823 { 824 void *ctx = state; 825 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, NULL); 826 decl->set_location(yylloc); 827 828 $$ = $1; 829 $$->declarations.push_tail(&decl->link); 830 } 831 | init_declarator_list ',' IDENTIFIER '[' ']' 832 { 833 void *ctx = state; 834 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, NULL); 835 decl->set_location(yylloc); 836 837 $$ = $1; 838 $$->declarations.push_tail(&decl->link); 839 } 840 | init_declarator_list ',' IDENTIFIER '[' constant_expression ']' 841 { 842 void *ctx = state; 843 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, NULL); 844 decl->set_location(yylloc); 845 846 $$ = $1; 847 $$->declarations.push_tail(&decl->link); 848 } 849 | init_declarator_list ',' IDENTIFIER '[' ']' '=' initializer 850 { 851 void *ctx = state; 852 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, $7); 853 decl->set_location(yylloc); 854 855 $$ = $1; 856 $$->declarations.push_tail(&decl->link); 857 } 858 | init_declarator_list ',' IDENTIFIER '[' constant_expression ']' '=' initializer 859 { 860 void *ctx = state; 861 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, $8); 862 decl->set_location(yylloc); 863 864 $$ = $1; 865 $$->declarations.push_tail(&decl->link); 866 } 867 | init_declarator_list ',' IDENTIFIER '=' initializer 868 { 869 void *ctx = state; 870 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, $5); 871 decl->set_location(yylloc); 872 873 $$ = $1; 874 $$->declarations.push_tail(&decl->link); 875 } 876 ; 877 878 // Grammar Note: No 'enum', or 'typedef'. 879single_declaration: 880 fully_specified_type 881 { 882 void *ctx = state; 883 if ($1->specifier->type_specifier != ast_struct) { 884 _mesa_glsl_error(& @1, state, "empty declaration list\n"); 885 YYERROR; 886 } else { 887 $$ = new(ctx) ast_declarator_list($1); 888 $$->set_location(yylloc); 889 } 890 } 891 | fully_specified_type IDENTIFIER 892 { 893 void *ctx = state; 894 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL); 895 896 $$ = new(ctx) ast_declarator_list($1); 897 $$->set_location(yylloc); 898 $$->declarations.push_tail(&decl->link); 899 } 900 | fully_specified_type IDENTIFIER '[' ']' 901 { 902 void *ctx = state; 903 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, NULL); 904 905 $$ = new(ctx) ast_declarator_list($1); 906 $$->set_location(yylloc); 907 $$->declarations.push_tail(&decl->link); 908 } 909 | fully_specified_type IDENTIFIER '[' constant_expression ']' 910 { 911 void *ctx = state; 912 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, NULL); 913 914 $$ = new(ctx) ast_declarator_list($1); 915 $$->set_location(yylloc); 916 $$->declarations.push_tail(&decl->link); 917 } 918 | fully_specified_type IDENTIFIER '[' ']' '=' initializer 919 { 920 void *ctx = state; 921 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, $6); 922 923 $$ = new(ctx) ast_declarator_list($1); 924 $$->set_location(yylloc); 925 $$->declarations.push_tail(&decl->link); 926 } 927 | fully_specified_type IDENTIFIER '[' constant_expression ']' '=' initializer 928 { 929 void *ctx = state; 930 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, $7); 931 932 $$ = new(ctx) ast_declarator_list($1); 933 $$->set_location(yylloc); 934 $$->declarations.push_tail(&decl->link); 935 } 936 | fully_specified_type IDENTIFIER '=' initializer 937 { 938 void *ctx = state; 939 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4); 940 941 $$ = new(ctx) ast_declarator_list($1); 942 $$->set_location(yylloc); 943 $$->declarations.push_tail(&decl->link); 944 } 945 | INVARIANT IDENTIFIER // Vertex only. 946 { 947 void *ctx = state; 948 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL); 949 950 $$ = new(ctx) ast_declarator_list(NULL); 951 $$->set_location(yylloc); 952 $$->invariant = true; 953 954 $$->declarations.push_tail(&decl->link); 955 } 956 ; 957 958fully_specified_type: 959 type_specifier 960 { 961 void *ctx = state; 962 $$ = new(ctx) ast_fully_specified_type(); 963 $$->set_location(yylloc); 964 $$->specifier = $1; 965 } 966 | type_qualifier type_specifier 967 { 968 void *ctx = state; 969 $$ = new(ctx) ast_fully_specified_type(); 970 $$->set_location(yylloc); 971 $$->qualifier = $1; 972 $$->specifier = $2; 973 } 974 ; 975 976layout_qualifier: 977 LAYOUT_TOK '(' layout_qualifier_id_list ')' 978 { 979 $$ = $3; 980 } 981 ; 982 983layout_qualifier_id_list: 984 layout_qualifier_id 985 | layout_qualifier_id_list ',' layout_qualifier_id 986 { 987 if (($1.flags.i & $3.flags.i) != 0) { 988 _mesa_glsl_error(& @3, state, 989 "duplicate layout qualifiers used\n"); 990 YYERROR; 991 } 992 993 $$.flags.i = $1.flags.i | $3.flags.i; 994 995 if ($1.flags.q.explicit_location) 996 $$.location = $1.location; 997 998 if ($3.flags.q.explicit_location) 999 $$.location = $3.location; 1000 } 1001 ; 1002 1003layout_qualifier_id: 1004 IDENTIFIER 1005 { 1006 bool got_one = false; 1007 1008 memset(& $$, 0, sizeof($$)); 1009 1010 if (state->ARB_fragment_coord_conventions_enable) { 1011 if (strcmp($1, "origin_upper_left") == 0) { 1012 got_one = true; 1013 $$.flags.q.origin_upper_left = 1; 1014 } else if (strcmp($1, "pixel_center_integer") == 0) { 1015 got_one = true; 1016 $$.flags.q.pixel_center_integer = 1; 1017 } 1018 } 1019 1020 /* If the identifier didn't match any known layout identifiers, 1021 * emit an error. 1022 */ 1023 if (!got_one) { 1024 _mesa_glsl_error(& @1, state, "unrecognized layout identifier " 1025 "`%s'\n", $1); 1026 YYERROR; 1027 } else if (state->ARB_fragment_coord_conventions_warn) { 1028 _mesa_glsl_warning(& @1, state, 1029 "GL_ARB_fragment_coord_conventions layout " 1030 "identifier `%s' used\n", $1); 1031 } 1032 } 1033 | IDENTIFIER '=' INTCONSTANT 1034 { 1035 bool got_one = false; 1036 1037 memset(& $$, 0, sizeof($$)); 1038 1039 if (state->ARB_explicit_attrib_location_enable) { 1040 /* FINISHME: Handle 'index' once GL_ARB_blend_func_exteneded and 1041 * FINISHME: GLSL 1.30 (or later) are supported. 1042 */ 1043 if (strcmp("location", $1) == 0) { 1044 got_one = true; 1045 1046 $$.flags.q.explicit_location = 1; 1047 1048 if ($3 >= 0) { 1049 $$.location = $3; 1050 } else { 1051 _mesa_glsl_error(& @3, state, 1052 "invalid location %d specified\n", $3); 1053 YYERROR; 1054 } 1055 } 1056 } 1057 1058 /* If the identifier didn't match any known layout identifiers, 1059 * emit an error. 1060 */ 1061 if (!got_one) { 1062 _mesa_glsl_error(& @1, state, "unrecognized layout identifier " 1063 "`%s'\n", $1); 1064 YYERROR; 1065 } else if (state->ARB_explicit_attrib_location_warn) { 1066 _mesa_glsl_warning(& @1, state, 1067 "GL_ARB_explicit_attrib_location layout " 1068 "identifier `%s' used\n", $1); 1069 } 1070 } 1071 ; 1072 1073interpolation_qualifier: 1074 SMOOTH 1075 { 1076 memset(& $$, 0, sizeof($$)); 1077 $$.flags.q.smooth = 1; 1078 } 1079 | FLAT 1080 { 1081 memset(& $$, 0, sizeof($$)); 1082 $$.flags.q.flat = 1; 1083 } 1084 | NOPERSPECTIVE 1085 { 1086 memset(& $$, 0, sizeof($$)); 1087 $$.flags.q.noperspective = 1; 1088 } 1089 ; 1090 1091parameter_type_qualifier: 1092 CONST_TOK 1093 { 1094 memset(& $$, 0, sizeof($$)); 1095 $$.flags.q.constant = 1; 1096 } 1097 ; 1098 1099type_qualifier: 1100 storage_qualifier 1101 | layout_qualifier 1102 | layout_qualifier storage_qualifier 1103 { 1104 $$ = $1; 1105 $$.flags.i |= $2.flags.i; 1106 } 1107 | interpolation_qualifier 1108 | interpolation_qualifier storage_qualifier 1109 { 1110 $$ = $1; 1111 $$.flags.i |= $2.flags.i; 1112 } 1113 | INVARIANT storage_qualifier 1114 { 1115 $$ = $2; 1116 $$.flags.q.invariant = 1; 1117 } 1118 | INVARIANT interpolation_qualifier storage_qualifier 1119 { 1120 $$ = $2; 1121 $$.flags.i |= $3.flags.i; 1122 $$.flags.q.invariant = 1; 1123 } 1124 | INVARIANT 1125 { 1126 memset(& $$, 0, sizeof($$)); 1127 $$.flags.q.invariant = 1; 1128 } 1129 ; 1130 1131storage_qualifier: 1132 CONST_TOK 1133 { 1134 memset(& $$, 0, sizeof($$)); 1135 $$.flags.q.constant = 1; 1136 } 1137 | ATTRIBUTE 1138 { 1139 memset(& $$, 0, sizeof($$)); 1140 $$.flags.q.attribute = 1; 1141 } 1142 | VARYING 1143 { 1144 memset(& $$, 0, sizeof($$)); 1145 $$.flags.q.varying = 1; 1146 } 1147 | CENTROID VARYING 1148 { 1149 memset(& $$, 0, sizeof($$)); 1150 $$.flags.q.centroid = 1; 1151 $$.flags.q.varying = 1; 1152 } 1153 | IN_TOK 1154 { 1155 memset(& $$, 0, sizeof($$)); 1156 $$.flags.q.in = 1; 1157 } 1158 | OUT_TOK 1159 { 1160 memset(& $$, 0, sizeof($$)); 1161 $$.flags.q.out = 1; 1162 } 1163 | CENTROID IN_TOK 1164 { 1165 memset(& $$, 0, sizeof($$)); 1166 $$.flags.q.centroid = 1; $$.flags.q.in = 1; 1167 } 1168 | CENTROID OUT_TOK 1169 { 1170 memset(& $$, 0, sizeof($$)); 1171 $$.flags.q.centroid = 1; $$.flags.q.out = 1; 1172 } 1173 | UNIFORM 1174 { 1175 memset(& $$, 0, sizeof($$)); 1176 $$.flags.q.uniform = 1; 1177 } 1178 ; 1179 1180type_specifier: 1181 type_specifier_no_prec 1182 | precision_qualifier type_specifier_no_prec 1183 { 1184 $$ = $2; 1185 $$->precision = $1; 1186 } 1187 ; 1188 1189type_specifier_no_prec: 1190 type_specifier_nonarray 1191 | type_specifier_nonarray '[' ']' 1192 { 1193 $$ = $1; 1194 $$->is_array = true; 1195 $$->array_size = NULL; 1196 } 1197 | type_specifier_nonarray '[' constant_expression ']' 1198 { 1199 $$ = $1; 1200 $$->is_array = true; 1201 $$->array_size = $3; 1202 } 1203 ; 1204 1205type_specifier_nonarray: 1206 basic_type_specifier_nonarray 1207 { 1208 void *ctx = state; 1209 $$ = new(ctx) ast_type_specifier($1); 1210 $$->set_location(yylloc); 1211 } 1212 | struct_specifier 1213 { 1214 void *ctx = state; 1215 $$ = new(ctx) ast_type_specifier($1); 1216 $$->set_location(yylloc); 1217 } 1218 | IDENTIFIER 1219 { 1220 void *ctx = state; 1221 $$ = new(ctx) ast_type_specifier($1); 1222 $$->set_location(yylloc); 1223 } 1224 ; 1225 1226basic_type_specifier_nonarray: 1227 VOID_TOK { $$ = ast_void; } 1228 | FLOAT_TOK { $$ = ast_float; } 1229 | INT_TOK { $$ = ast_int; } 1230 | UINT_TOK { $$ = ast_uint; } 1231 | BOOL_TOK { $$ = ast_bool; } 1232 | VEC2 { $$ = ast_vec2; } 1233 | VEC3 { $$ = ast_vec3; } 1234 | VEC4 { $$ = ast_vec4; } 1235 | BVEC2 { $$ = ast_bvec2; } 1236 | BVEC3 { $$ = ast_bvec3; } 1237 | BVEC4 { $$ = ast_bvec4; } 1238 | IVEC2 { $$ = ast_ivec2; } 1239 | IVEC3 { $$ = ast_ivec3; } 1240 | IVEC4 { $$ = ast_ivec4; } 1241 | UVEC2 { $$ = ast_uvec2; } 1242 | UVEC3 { $$ = ast_uvec3; } 1243 | UVEC4 { $$ = ast_uvec4; } 1244 | MAT2X2 { $$ = ast_mat2; } 1245 | MAT2X3 { $$ = ast_mat2x3; } 1246 | MAT2X4 { $$ = ast_mat2x4; } 1247 | MAT3X2 { $$ = ast_mat3x2; } 1248 | MAT3X3 { $$ = ast_mat3; } 1249 | MAT3X4 { $$ = ast_mat3x4; } 1250 | MAT4X2 { $$ = ast_mat4x2; } 1251 | MAT4X3 { $$ = ast_mat4x3; } 1252 | MAT4X4 { $$ = ast_mat4; } 1253 | SAMPLER1D { $$ = ast_sampler1d; } 1254 | SAMPLER2D { $$ = ast_sampler2d; } 1255 | SAMPLER2DRECT { $$ = ast_sampler2drect; } 1256 | SAMPLER3D { $$ = ast_sampler3d; } 1257 | SAMPLERCUBE { $$ = ast_samplercube; } 1258 | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; } 1259 | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; } 1260 | SAMPLER2DRECTSHADOW { $$ = ast_sampler2drectshadow; } 1261 | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; } 1262 | SAMPLER1DARRAY { $$ = ast_sampler1darray; } 1263 | SAMPLER2DARRAY { $$ = ast_sampler2darray; } 1264 | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; } 1265 | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; } 1266 | ISAMPLER1D { $$ = ast_isampler1d; } 1267 | ISAMPLER2D { $$ = ast_isampler2d; } 1268 | ISAMPLER3D { $$ = ast_isampler3d; } 1269 | ISAMPLERCUBE { $$ = ast_isamplercube; } 1270 | ISAMPLER1DARRAY { $$ = ast_isampler1darray; } 1271 | ISAMPLER2DARRAY { $$ = ast_isampler2darray; } 1272 | USAMPLER1D { $$ = ast_usampler1d; } 1273 | USAMPLER2D { $$ = ast_usampler2d; } 1274 | USAMPLER3D { $$ = ast_usampler3d; } 1275 | USAMPLERCUBE { $$ = ast_usamplercube; } 1276 | USAMPLER1DARRAY { $$ = ast_usampler1darray; } 1277 | USAMPLER2DARRAY { $$ = ast_usampler2darray; } 1278 ; 1279 1280precision_qualifier: 1281 HIGHP { 1282 if (!state->es_shader && state->language_version < 130) 1283 _mesa_glsl_error(& @1, state, 1284 "precision qualifier forbidden " 1285 "in GLSL %d.%d (1.30 or later " 1286 "required)\n", 1287 state->language_version / 100, 1288 state->language_version % 100); 1289 1290 $$ = ast_precision_high; 1291 } 1292 | MEDIUMP { 1293 if (!state->es_shader && state->language_version < 130) 1294 _mesa_glsl_error(& @1, state, 1295 "precision qualifier forbidden " 1296 "in GLSL %d.%d (1.30 or later " 1297 "required)\n", 1298 state->language_version / 100, 1299 state->language_version % 100); 1300 1301 $$ = ast_precision_medium; 1302 } 1303 | LOWP { 1304 if (!state->es_shader && state->language_version < 130) 1305 _mesa_glsl_error(& @1, state, 1306 "precision qualifier forbidden " 1307 "in GLSL %d.%d (1.30 or later " 1308 "required)\n", 1309 state->language_version / 100, 1310 state->language_version % 100); 1311 1312 $$ = ast_precision_low; 1313 } 1314 ; 1315 1316struct_specifier: 1317 STRUCT IDENTIFIER '{' struct_declaration_list '}' 1318 { 1319 void *ctx = state; 1320 $$ = new(ctx) ast_struct_specifier($2, $4); 1321 $$->set_location(yylloc); 1322 } 1323 | STRUCT '{' struct_declaration_list '}' 1324 { 1325 void *ctx = state; 1326 $$ = new(ctx) ast_struct_specifier(NULL, $3); 1327 $$->set_location(yylloc); 1328 } 1329 ; 1330 1331struct_declaration_list: 1332 struct_declaration 1333 { 1334 $$ = (ast_node *) $1; 1335 $1->link.self_link(); 1336 } 1337 | struct_declaration_list struct_declaration 1338 { 1339 $$ = (ast_node *) $1; 1340 $$->link.insert_before(& $2->link); 1341 } 1342 ; 1343 1344struct_declaration: 1345 type_specifier struct_declarator_list ';' 1346 { 1347 void *ctx = state; 1348 ast_fully_specified_type *type = new(ctx) ast_fully_specified_type(); 1349 type->set_location(yylloc); 1350 1351 type->specifier = $1; 1352 $$ = new(ctx) ast_declarator_list(type); 1353 $$->set_location(yylloc); 1354 1355 $$->declarations.push_degenerate_list_at_head(& $2->link); 1356 } 1357 ; 1358 1359struct_declarator_list: 1360 struct_declarator 1361 { 1362 $$ = $1; 1363 $1->link.self_link(); 1364 } 1365 | struct_declarator_list ',' struct_declarator 1366 { 1367 $$ = $1; 1368 $$->link.insert_before(& $3->link); 1369 } 1370 ; 1371 1372struct_declarator: 1373 IDENTIFIER 1374 { 1375 void *ctx = state; 1376 $$ = new(ctx) ast_declaration($1, false, NULL, NULL); 1377 $$->set_location(yylloc); 1378 } 1379 | IDENTIFIER '[' constant_expression ']' 1380 { 1381 void *ctx = state; 1382 $$ = new(ctx) ast_declaration($1, true, $3, NULL); 1383 $$->set_location(yylloc); 1384 } 1385 ; 1386 1387initializer: 1388 assignment_expression 1389 ; 1390 1391declaration_statement: 1392 declaration 1393 ; 1394 1395 // Grammar Note: labeled statements for SWITCH only; 'goto' is not 1396 // supported. 1397statement: 1398 compound_statement { $$ = (ast_node *) $1; } 1399 | simple_statement 1400 ; 1401 1402simple_statement: 1403 declaration_statement 1404 | expression_statement 1405 | selection_statement 1406 | switch_statement { $$ = NULL; } 1407 | case_label { $$ = NULL; } 1408 | iteration_statement 1409 | jump_statement 1410 ; 1411 1412compound_statement: 1413 '{' '}' 1414 { 1415 void *ctx = state; 1416 $$ = new(ctx) ast_compound_statement(true, NULL); 1417 $$->set_location(yylloc); 1418 } 1419 | '{' statement_list '}' 1420 { 1421 void *ctx = state; 1422 $$ = new(ctx) ast_compound_statement(true, $2); 1423 $$->set_location(yylloc); 1424 } 1425 ; 1426 1427statement_no_new_scope: 1428 compound_statement_no_new_scope { $$ = (ast_node *) $1; } 1429 | simple_statement 1430 ; 1431 1432compound_statement_no_new_scope: 1433 '{' '}' 1434 { 1435 void *ctx = state; 1436 $$ = new(ctx) ast_compound_statement(false, NULL); 1437 $$->set_location(yylloc); 1438 } 1439 | '{' statement_list '}' 1440 { 1441 void *ctx = state; 1442 $$ = new(ctx) ast_compound_statement(false, $2); 1443 $$->set_location(yylloc); 1444 } 1445 ; 1446 1447statement_list: 1448 statement 1449 { 1450 if ($1 == NULL) { 1451 _mesa_glsl_error(& @1, state, "<nil> statement\n"); 1452 assert($1 != NULL); 1453 } 1454 1455 $$ = $1; 1456 $$->link.self_link(); 1457 } 1458 | statement_list statement 1459 { 1460 if ($2 == NULL) { 1461 _mesa_glsl_error(& @2, state, "<nil> statement\n"); 1462 assert($2 != NULL); 1463 } 1464 $$ = $1; 1465 $$->link.insert_before(& $2->link); 1466 } 1467 ; 1468 1469expression_statement: 1470 ';' 1471 { 1472 void *ctx = state; 1473 $$ = new(ctx) ast_expression_statement(NULL); 1474 $$->set_location(yylloc); 1475 } 1476 | expression ';' 1477 { 1478 void *ctx = state; 1479 $$ = new(ctx) ast_expression_statement($1); 1480 $$->set_location(yylloc); 1481 } 1482 ; 1483 1484selection_statement: 1485 IF '(' expression ')' selection_rest_statement 1486 { 1487 $$ = new(state) ast_selection_statement($3, $5.then_statement, 1488 $5.else_statement); 1489 $$->set_location(yylloc); 1490 } 1491 ; 1492 1493selection_rest_statement: 1494 statement ELSE statement 1495 { 1496 $$.then_statement = $1; 1497 $$.else_statement = $3; 1498 } 1499 | statement 1500 { 1501 $$.then_statement = $1; 1502 $$.else_statement = NULL; 1503 } 1504 ; 1505 1506condition: 1507 expression 1508 { 1509 $$ = (ast_node *) $1; 1510 } 1511 | fully_specified_type IDENTIFIER '=' initializer 1512 { 1513 void *ctx = state; 1514 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4); 1515 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1); 1516 decl->set_location(yylloc); 1517 declarator->set_location(yylloc); 1518 1519 declarator->declarations.push_tail(&decl->link); 1520 $$ = declarator; 1521 } 1522 ; 1523 1524switch_statement: 1525 SWITCH '(' expression ')' compound_statement 1526 ; 1527 1528case_label: 1529 CASE expression ':' 1530 | DEFAULT ':' 1531 ; 1532 1533iteration_statement: 1534 WHILE '(' condition ')' statement_no_new_scope 1535 { 1536 void *ctx = state; 1537 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while, 1538 NULL, $3, NULL, $5); 1539 $$->set_location(yylloc); 1540 } 1541 | DO statement WHILE '(' expression ')' ';' 1542 { 1543 void *ctx = state; 1544 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while, 1545 NULL, $5, NULL, $2); 1546 $$->set_location(yylloc); 1547 } 1548 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope 1549 { 1550 void *ctx = state; 1551 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for, 1552 $3, $4.cond, $4.rest, $6); 1553 $$->set_location(yylloc); 1554 } 1555 ; 1556 1557for_init_statement: 1558 expression_statement 1559 | declaration_statement 1560 ; 1561 1562conditionopt: 1563 condition 1564 | /* empty */ 1565 { 1566 $$ = NULL; 1567 } 1568 ; 1569 1570for_rest_statement: 1571 conditionopt ';' 1572 { 1573 $$.cond = $1; 1574 $$.rest = NULL; 1575 } 1576 | conditionopt ';' expression 1577 { 1578 $$.cond = $1; 1579 $$.rest = $3; 1580 } 1581 ; 1582 1583 // Grammar Note: No 'goto'. Gotos are not supported. 1584jump_statement: 1585 CONTINUE ';' 1586 { 1587 void *ctx = state; 1588 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL); 1589 $$->set_location(yylloc); 1590 } 1591 | BREAK ';' 1592 { 1593 void *ctx = state; 1594 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL); 1595 $$->set_location(yylloc); 1596 } 1597 | RETURN ';' 1598 { 1599 void *ctx = state; 1600 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL); 1601 $$->set_location(yylloc); 1602 } 1603 | RETURN expression ';' 1604 { 1605 void *ctx = state; 1606 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2); 1607 $$->set_location(yylloc); 1608 } 1609 | DISCARD ';' // Fragment shader only. 1610 { 1611 void *ctx = state; 1612 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL); 1613 $$->set_location(yylloc); 1614 } 1615 ; 1616 1617external_declaration: 1618 function_definition { $$ = $1; } 1619 | declaration { $$ = $1; } 1620 | pragma_statement { $$ = NULL; } 1621 ; 1622 1623function_definition: 1624 function_prototype compound_statement_no_new_scope 1625 { 1626 void *ctx = state; 1627 $$ = new(ctx) ast_function_definition(); 1628 $$->set_location(yylloc); 1629 $$->prototype = $1; 1630 $$->body = $2; 1631 } 1632 ; 1633