1 /* YACC parser for Java expressions, for GDB. 2 Copyright (C) 1997-2000, 2006-2012 Free Software Foundation, Inc. 3 4 This file is part of GDB. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18 19 /* Parse a Java expression from text in a string, 20 and return the result as a struct expression pointer. 21 That structure contains arithmetic operations in reverse polish, 22 with constants represented by operations that are followed by special data. 23 See expression.h for the details of the format. 24 What is important here is that it can be built up sequentially 25 during the process of parsing; the lower levels of the tree always 26 come first in the result. Well, almost always; see ArrayAccess. 27 28 Note that malloc's and realloc's in this file are transformed to 29 xmalloc and xrealloc respectively by the same sed command in the 30 makefile that remaps any other malloc/realloc inserted by the parser 31 generator. Doing this with #defines and trying to control the interaction 32 with include files (<malloc.h> and <stdlib.h> for example) just became 33 too messy, particularly when such includes can be inserted at random 34 times by the parser generator. */ 35 36 %{ 37 38 #include "defs.h" 39 #include "gdb_string.h" 40 #include <ctype.h> 41 #include "expression.h" 42 #include "value.h" 43 #include "parser-defs.h" 44 #include "language.h" 45 #include "jv-lang.h" 46 #include "bfd.h" /* Required by objfiles.h. */ 47 #include "symfile.h" /* Required by objfiles.h. */ 48 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */ 49 #include "block.h" 50 51 #define parse_type builtin_type (parse_gdbarch) 52 #define parse_java_type builtin_java_type (parse_gdbarch) 53 54 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc), 55 as well as gratuitiously global symbol names, so we can have multiple 56 yacc generated parsers in gdb. Note that these are only the variables 57 produced by yacc. If other parser generators (bison, byacc, etc) produce 58 additional global names that conflict at link time, then those parser 59 generators need to be fixed instead of adding those names to this list. */ 60 61 #define yymaxdepth java_maxdepth 62 #define yyparse java_parse 63 #define yylex java_lex 64 #define yyerror java_error 65 #define yylval java_lval 66 #define yychar java_char 67 #define yydebug java_debug 68 #define yypact java_pact 69 #define yyr1 java_r1 70 #define yyr2 java_r2 71 #define yydef java_def 72 #define yychk java_chk 73 #define yypgo java_pgo 74 #define yyact java_act 75 #define yyexca java_exca 76 #define yyerrflag java_errflag 77 #define yynerrs java_nerrs 78 #define yyps java_ps 79 #define yypv java_pv 80 #define yys java_s 81 #define yy_yys java_yys 82 #define yystate java_state 83 #define yytmp java_tmp 84 #define yyv java_v 85 #define yy_yyv java_yyv 86 #define yyval java_val 87 #define yylloc java_lloc 88 #define yyreds java_reds /* With YYDEBUG defined */ 89 #define yytoks java_toks /* With YYDEBUG defined */ 90 #define yyname java_name /* With YYDEBUG defined */ 91 #define yyrule java_rule /* With YYDEBUG defined */ 92 #define yylhs java_yylhs 93 #define yylen java_yylen 94 #define yydefred java_yydefred 95 #define yydgoto java_yydgoto 96 #define yysindex java_yysindex 97 #define yyrindex java_yyrindex 98 #define yygindex java_yygindex 99 #define yytable java_yytable 100 #define yycheck java_yycheck 101 102 #ifndef YYDEBUG 103 #define YYDEBUG 1 /* Default to yydebug support */ 104 #endif 105 106 #define YYFPRINTF parser_fprintf 107 108 int yyparse (void); 109 110 static int yylex (void); 111 112 void yyerror (char *); 113 114 static struct type *java_type_from_name (struct stoken); 115 static void push_expression_name (struct stoken); 116 static void push_fieldnames (struct stoken); 117 118 static struct expression *copy_exp (struct expression *, int); 119 static void insert_exp (int, struct expression *); 120 121 %} 122 123 /* Although the yacc "value" of an expression is not used, 124 since the result is stored in the structure being created, 125 other node types do have values. */ 126 127 %union 128 { 129 LONGEST lval; 130 struct { 131 LONGEST val; 132 struct type *type; 133 } typed_val_int; 134 struct { 135 DOUBLEST dval; 136 struct type *type; 137 } typed_val_float; 138 struct symbol *sym; 139 struct type *tval; 140 struct stoken sval; 141 struct ttype tsym; 142 struct symtoken ssym; 143 struct block *bval; 144 enum exp_opcode opcode; 145 struct internalvar *ivar; 146 int *ivec; 147 } 148 149 %{ 150 /* YYSTYPE gets defined by %union */ 151 static int parse_number (char *, int, int, YYSTYPE *); 152 %} 153 154 %type <lval> rcurly Dims Dims_opt 155 %type <tval> ClassOrInterfaceType ClassType /* ReferenceType Type ArrayType */ 156 %type <tval> IntegralType FloatingPointType NumericType PrimitiveType ArrayType PrimitiveOrArrayType 157 158 %token <typed_val_int> INTEGER_LITERAL 159 %token <typed_val_float> FLOATING_POINT_LITERAL 160 161 %token <sval> IDENTIFIER 162 %token <sval> STRING_LITERAL 163 %token <lval> BOOLEAN_LITERAL 164 %token <tsym> TYPENAME 165 %type <sval> Name SimpleName QualifiedName ForcedName 166 167 /* A NAME_OR_INT is a symbol which is not known in the symbol table, 168 but which would parse as a valid number in the current input radix. 169 E.g. "c" when input_radix==16. Depending on the parse, it will be 170 turned into a name or into a number. */ 171 172 %token <sval> NAME_OR_INT 173 174 %token ERROR 175 176 /* Special type cases, put in to allow the parser to distinguish different 177 legal basetypes. */ 178 %token LONG SHORT BYTE INT CHAR BOOLEAN DOUBLE FLOAT 179 180 %token VARIABLE 181 182 %token <opcode> ASSIGN_MODIFY 183 184 %token SUPER NEW 185 186 %left ',' 187 %right '=' ASSIGN_MODIFY 188 %right '?' 189 %left OROR 190 %left ANDAND 191 %left '|' 192 %left '^' 193 %left '&' 194 %left EQUAL NOTEQUAL 195 %left '<' '>' LEQ GEQ 196 %left LSH RSH 197 %left '+' '-' 198 %left '*' '/' '%' 199 %right INCREMENT DECREMENT 200 %right '.' '[' '(' 201 202 203 %% 204 205 start : exp1 206 | type_exp 207 ; 208 209 type_exp: PrimitiveOrArrayType 210 { 211 write_exp_elt_opcode(OP_TYPE); 212 write_exp_elt_type($1); 213 write_exp_elt_opcode(OP_TYPE); 214 } 215 ; 216 217 PrimitiveOrArrayType: 218 PrimitiveType 219 | ArrayType 220 ; 221 222 StringLiteral: 223 STRING_LITERAL 224 { 225 write_exp_elt_opcode (OP_STRING); 226 write_exp_string ($1); 227 write_exp_elt_opcode (OP_STRING); 228 } 229 ; 230 231 Literal: 232 INTEGER_LITERAL 233 { write_exp_elt_opcode (OP_LONG); 234 write_exp_elt_type ($1.type); 235 write_exp_elt_longcst ((LONGEST)($1.val)); 236 write_exp_elt_opcode (OP_LONG); } 237 | NAME_OR_INT 238 { YYSTYPE val; 239 parse_number ($1.ptr, $1.length, 0, &val); 240 write_exp_elt_opcode (OP_LONG); 241 write_exp_elt_type (val.typed_val_int.type); 242 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val); 243 write_exp_elt_opcode (OP_LONG); 244 } 245 | FLOATING_POINT_LITERAL 246 { write_exp_elt_opcode (OP_DOUBLE); 247 write_exp_elt_type ($1.type); 248 write_exp_elt_dblcst ($1.dval); 249 write_exp_elt_opcode (OP_DOUBLE); } 250 | BOOLEAN_LITERAL 251 { write_exp_elt_opcode (OP_LONG); 252 write_exp_elt_type (parse_java_type->builtin_boolean); 253 write_exp_elt_longcst ((LONGEST)$1); 254 write_exp_elt_opcode (OP_LONG); } 255 | StringLiteral 256 ; 257 258 /* UNUSED: 259 Type: 260 PrimitiveType 261 | ReferenceType 262 ; 263 */ 264 265 PrimitiveType: 266 NumericType 267 | BOOLEAN 268 { $$ = parse_java_type->builtin_boolean; } 269 ; 270 271 NumericType: 272 IntegralType 273 | FloatingPointType 274 ; 275 276 IntegralType: 277 BYTE 278 { $$ = parse_java_type->builtin_byte; } 279 | SHORT 280 { $$ = parse_java_type->builtin_short; } 281 | INT 282 { $$ = parse_java_type->builtin_int; } 283 | LONG 284 { $$ = parse_java_type->builtin_long; } 285 | CHAR 286 { $$ = parse_java_type->builtin_char; } 287 ; 288 289 FloatingPointType: 290 FLOAT 291 { $$ = parse_java_type->builtin_float; } 292 | DOUBLE 293 { $$ = parse_java_type->builtin_double; } 294 ; 295 296 /* UNUSED: 297 ReferenceType: 298 ClassOrInterfaceType 299 | ArrayType 300 ; 301 */ 302 303 ClassOrInterfaceType: 304 Name 305 { $$ = java_type_from_name ($1); } 306 ; 307 308 ClassType: 309 ClassOrInterfaceType 310 ; 311 312 ArrayType: 313 PrimitiveType Dims 314 { $$ = java_array_type ($1, $2); } 315 | Name Dims 316 { $$ = java_array_type (java_type_from_name ($1), $2); } 317 ; 318 319 Name: 320 IDENTIFIER 321 | QualifiedName 322 ; 323 324 ForcedName: 325 SimpleName 326 | QualifiedName 327 ; 328 329 SimpleName: 330 IDENTIFIER 331 | NAME_OR_INT 332 ; 333 334 QualifiedName: 335 Name '.' SimpleName 336 { $$.length = $1.length + $3.length + 1; 337 if ($1.ptr + $1.length + 1 == $3.ptr 338 && $1.ptr[$1.length] == '.') 339 $$.ptr = $1.ptr; /* Optimization. */ 340 else 341 { 342 $$.ptr = (char *) malloc ($$.length + 1); 343 make_cleanup (free, $$.ptr); 344 sprintf ($$.ptr, "%.*s.%.*s", 345 $1.length, $1.ptr, $3.length, $3.ptr); 346 } } 347 ; 348 349 /* 350 type_exp: type 351 { write_exp_elt_opcode(OP_TYPE); 352 write_exp_elt_type($1); 353 write_exp_elt_opcode(OP_TYPE);} 354 ; 355 */ 356 357 /* Expressions, including the comma operator. */ 358 exp1 : Expression 359 | exp1 ',' Expression 360 { write_exp_elt_opcode (BINOP_COMMA); } 361 ; 362 363 Primary: 364 PrimaryNoNewArray 365 | ArrayCreationExpression 366 ; 367 368 PrimaryNoNewArray: 369 Literal 370 | '(' Expression ')' 371 | ClassInstanceCreationExpression 372 | FieldAccess 373 | MethodInvocation 374 | ArrayAccess 375 | lcurly ArgumentList rcurly 376 { write_exp_elt_opcode (OP_ARRAY); 377 write_exp_elt_longcst ((LONGEST) 0); 378 write_exp_elt_longcst ((LONGEST) $3); 379 write_exp_elt_opcode (OP_ARRAY); } 380 ; 381 382 lcurly: 383 '{' 384 { start_arglist (); } 385 ; 386 387 rcurly: 388 '}' 389 { $$ = end_arglist () - 1; } 390 ; 391 392 ClassInstanceCreationExpression: 393 NEW ClassType '(' ArgumentList_opt ')' 394 { internal_error (__FILE__, __LINE__, 395 _("FIXME - ClassInstanceCreationExpression")); } 396 ; 397 398 ArgumentList: 399 Expression 400 { arglist_len = 1; } 401 | ArgumentList ',' Expression 402 { arglist_len++; } 403 ; 404 405 ArgumentList_opt: 406 /* EMPTY */ 407 { arglist_len = 0; } 408 | ArgumentList 409 ; 410 411 ArrayCreationExpression: 412 NEW PrimitiveType DimExprs Dims_opt 413 { internal_error (__FILE__, __LINE__, 414 _("FIXME - ArrayCreationExpression")); } 415 | NEW ClassOrInterfaceType DimExprs Dims_opt 416 { internal_error (__FILE__, __LINE__, 417 _("FIXME - ArrayCreationExpression")); } 418 ; 419 420 DimExprs: 421 DimExpr 422 | DimExprs DimExpr 423 ; 424 425 DimExpr: 426 '[' Expression ']' 427 ; 428 429 Dims: 430 '[' ']' 431 { $$ = 1; } 432 | Dims '[' ']' 433 { $$ = $1 + 1; } 434 ; 435 436 Dims_opt: 437 Dims 438 | /* EMPTY */ 439 { $$ = 0; } 440 ; 441 442 FieldAccess: 443 Primary '.' SimpleName 444 { push_fieldnames ($3); } 445 | VARIABLE '.' SimpleName 446 { push_fieldnames ($3); } 447 /*| SUPER '.' SimpleName { FIXME } */ 448 ; 449 450 FuncStart: 451 Name '(' 452 { push_expression_name ($1); } 453 ; 454 455 MethodInvocation: 456 FuncStart 457 { start_arglist(); } 458 ArgumentList_opt ')' 459 { write_exp_elt_opcode (OP_FUNCALL); 460 write_exp_elt_longcst ((LONGEST) end_arglist ()); 461 write_exp_elt_opcode (OP_FUNCALL); } 462 | Primary '.' SimpleName '(' ArgumentList_opt ')' 463 { error (_("Form of method invocation not implemented")); } 464 | SUPER '.' SimpleName '(' ArgumentList_opt ')' 465 { error (_("Form of method invocation not implemented")); } 466 ; 467 468 ArrayAccess: 469 Name '[' Expression ']' 470 { 471 /* Emit code for the Name now, then exchange it in the 472 expout array with the Expression's code. We could 473 introduce a OP_SWAP code or a reversed version of 474 BINOP_SUBSCRIPT, but that makes the rest of GDB pay 475 for our parsing kludges. */ 476 struct expression *name_expr; 477 478 push_expression_name ($1); 479 name_expr = copy_exp (expout, expout_ptr); 480 expout_ptr -= name_expr->nelts; 481 insert_exp (expout_ptr-length_of_subexp (expout, expout_ptr), 482 name_expr); 483 free (name_expr); 484 write_exp_elt_opcode (BINOP_SUBSCRIPT); 485 } 486 | VARIABLE '[' Expression ']' 487 { write_exp_elt_opcode (BINOP_SUBSCRIPT); } 488 | PrimaryNoNewArray '[' Expression ']' 489 { write_exp_elt_opcode (BINOP_SUBSCRIPT); } 490 ; 491 492 PostfixExpression: 493 Primary 494 | Name 495 { push_expression_name ($1); } 496 | VARIABLE 497 /* Already written by write_dollar_variable. */ 498 | PostIncrementExpression 499 | PostDecrementExpression 500 ; 501 502 PostIncrementExpression: 503 PostfixExpression INCREMENT 504 { write_exp_elt_opcode (UNOP_POSTINCREMENT); } 505 ; 506 507 PostDecrementExpression: 508 PostfixExpression DECREMENT 509 { write_exp_elt_opcode (UNOP_POSTDECREMENT); } 510 ; 511 512 UnaryExpression: 513 PreIncrementExpression 514 | PreDecrementExpression 515 | '+' UnaryExpression 516 | '-' UnaryExpression 517 { write_exp_elt_opcode (UNOP_NEG); } 518 | '*' UnaryExpression 519 { write_exp_elt_opcode (UNOP_IND); } /*FIXME not in Java */ 520 | UnaryExpressionNotPlusMinus 521 ; 522 523 PreIncrementExpression: 524 INCREMENT UnaryExpression 525 { write_exp_elt_opcode (UNOP_PREINCREMENT); } 526 ; 527 528 PreDecrementExpression: 529 DECREMENT UnaryExpression 530 { write_exp_elt_opcode (UNOP_PREDECREMENT); } 531 ; 532 533 UnaryExpressionNotPlusMinus: 534 PostfixExpression 535 | '~' UnaryExpression 536 { write_exp_elt_opcode (UNOP_COMPLEMENT); } 537 | '!' UnaryExpression 538 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); } 539 | CastExpression 540 ; 541 542 CastExpression: 543 '(' PrimitiveType Dims_opt ')' UnaryExpression 544 { write_exp_elt_opcode (UNOP_CAST); 545 write_exp_elt_type (java_array_type ($2, $3)); 546 write_exp_elt_opcode (UNOP_CAST); } 547 | '(' Expression ')' UnaryExpressionNotPlusMinus 548 { 549 int last_exp_size = length_of_subexp(expout, expout_ptr); 550 struct type *type; 551 int i; 552 int base = expout_ptr - last_exp_size - 3; 553 if (base < 0 || expout->elts[base+2].opcode != OP_TYPE) 554 error (_("Invalid cast expression")); 555 type = expout->elts[base+1].type; 556 /* Remove the 'Expression' and slide the 557 UnaryExpressionNotPlusMinus down to replace it. */ 558 for (i = 0; i < last_exp_size; i++) 559 expout->elts[base + i] = expout->elts[base + i + 3]; 560 expout_ptr -= 3; 561 if (TYPE_CODE (type) == TYPE_CODE_STRUCT) 562 type = lookup_pointer_type (type); 563 write_exp_elt_opcode (UNOP_CAST); 564 write_exp_elt_type (type); 565 write_exp_elt_opcode (UNOP_CAST); 566 } 567 | '(' Name Dims ')' UnaryExpressionNotPlusMinus 568 { write_exp_elt_opcode (UNOP_CAST); 569 write_exp_elt_type (java_array_type (java_type_from_name ($2), $3)); 570 write_exp_elt_opcode (UNOP_CAST); } 571 ; 572 573 574 MultiplicativeExpression: 575 UnaryExpression 576 | MultiplicativeExpression '*' UnaryExpression 577 { write_exp_elt_opcode (BINOP_MUL); } 578 | MultiplicativeExpression '/' UnaryExpression 579 { write_exp_elt_opcode (BINOP_DIV); } 580 | MultiplicativeExpression '%' UnaryExpression 581 { write_exp_elt_opcode (BINOP_REM); } 582 ; 583 584 AdditiveExpression: 585 MultiplicativeExpression 586 | AdditiveExpression '+' MultiplicativeExpression 587 { write_exp_elt_opcode (BINOP_ADD); } 588 | AdditiveExpression '-' MultiplicativeExpression 589 { write_exp_elt_opcode (BINOP_SUB); } 590 ; 591 592 ShiftExpression: 593 AdditiveExpression 594 | ShiftExpression LSH AdditiveExpression 595 { write_exp_elt_opcode (BINOP_LSH); } 596 | ShiftExpression RSH AdditiveExpression 597 { write_exp_elt_opcode (BINOP_RSH); } 598 /* | ShiftExpression >>> AdditiveExpression { FIXME } */ 599 ; 600 601 RelationalExpression: 602 ShiftExpression 603 | RelationalExpression '<' ShiftExpression 604 { write_exp_elt_opcode (BINOP_LESS); } 605 | RelationalExpression '>' ShiftExpression 606 { write_exp_elt_opcode (BINOP_GTR); } 607 | RelationalExpression LEQ ShiftExpression 608 { write_exp_elt_opcode (BINOP_LEQ); } 609 | RelationalExpression GEQ ShiftExpression 610 { write_exp_elt_opcode (BINOP_GEQ); } 611 /* | RelationalExpresion INSTANCEOF ReferenceType { FIXME } */ 612 ; 613 614 EqualityExpression: 615 RelationalExpression 616 | EqualityExpression EQUAL RelationalExpression 617 { write_exp_elt_opcode (BINOP_EQUAL); } 618 | EqualityExpression NOTEQUAL RelationalExpression 619 { write_exp_elt_opcode (BINOP_NOTEQUAL); } 620 ; 621 622 AndExpression: 623 EqualityExpression 624 | AndExpression '&' EqualityExpression 625 { write_exp_elt_opcode (BINOP_BITWISE_AND); } 626 ; 627 628 ExclusiveOrExpression: 629 AndExpression 630 | ExclusiveOrExpression '^' AndExpression 631 { write_exp_elt_opcode (BINOP_BITWISE_XOR); } 632 ; 633 InclusiveOrExpression: 634 ExclusiveOrExpression 635 | InclusiveOrExpression '|' ExclusiveOrExpression 636 { write_exp_elt_opcode (BINOP_BITWISE_IOR); } 637 ; 638 639 ConditionalAndExpression: 640 InclusiveOrExpression 641 | ConditionalAndExpression ANDAND InclusiveOrExpression 642 { write_exp_elt_opcode (BINOP_LOGICAL_AND); } 643 ; 644 645 ConditionalOrExpression: 646 ConditionalAndExpression 647 | ConditionalOrExpression OROR ConditionalAndExpression 648 { write_exp_elt_opcode (BINOP_LOGICAL_OR); } 649 ; 650 651 ConditionalExpression: 652 ConditionalOrExpression 653 | ConditionalOrExpression '?' Expression ':' ConditionalExpression 654 { write_exp_elt_opcode (TERNOP_COND); } 655 ; 656 657 AssignmentExpression: 658 ConditionalExpression 659 | Assignment 660 ; 661 662 Assignment: 663 LeftHandSide '=' ConditionalExpression 664 { write_exp_elt_opcode (BINOP_ASSIGN); } 665 | LeftHandSide ASSIGN_MODIFY ConditionalExpression 666 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); 667 write_exp_elt_opcode ($2); 668 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); } 669 ; 670 671 LeftHandSide: 672 ForcedName 673 { push_expression_name ($1); } 674 | VARIABLE 675 /* Already written by write_dollar_variable. */ 676 | FieldAccess 677 | ArrayAccess 678 ; 679 680 681 Expression: 682 AssignmentExpression 683 ; 684 685 %% 686 /* Take care of parsing a number (anything that starts with a digit). 687 Set yylval and return the token type; update lexptr. 688 LEN is the number of characters in it. */ 689 690 /*** Needs some error checking for the float case ***/ 691 692 static int 693 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere) 694 { 695 ULONGEST n = 0; 696 ULONGEST limit, limit_div_base; 697 698 int c; 699 int base = input_radix; 700 701 struct type *type; 702 703 if (parsed_float) 704 { 705 const char *suffix; 706 int suffix_len; 707 708 if (! parse_float (p, len, &putithere->typed_val_float.dval, &suffix)) 709 return ERROR; 710 711 suffix_len = p + len - suffix; 712 713 if (suffix_len == 0) 714 putithere->typed_val_float.type = parse_type->builtin_double; 715 else if (suffix_len == 1) 716 { 717 /* See if it has `f' or `d' suffix (float or double). */ 718 if (tolower (*suffix) == 'f') 719 putithere->typed_val_float.type = 720 parse_type->builtin_float; 721 else if (tolower (*suffix) == 'd') 722 putithere->typed_val_float.type = 723 parse_type->builtin_double; 724 else 725 return ERROR; 726 } 727 else 728 return ERROR; 729 730 return FLOATING_POINT_LITERAL; 731 } 732 733 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */ 734 if (p[0] == '0') 735 switch (p[1]) 736 { 737 case 'x': 738 case 'X': 739 if (len >= 3) 740 { 741 p += 2; 742 base = 16; 743 len -= 2; 744 } 745 break; 746 747 case 't': 748 case 'T': 749 case 'd': 750 case 'D': 751 if (len >= 3) 752 { 753 p += 2; 754 base = 10; 755 len -= 2; 756 } 757 break; 758 759 default: 760 base = 8; 761 break; 762 } 763 764 c = p[len-1]; 765 /* A paranoid calculation of (1<<64)-1. */ 766 limit = (ULONGEST)0xffffffff; 767 limit = ((limit << 16) << 16) | limit; 768 if (c == 'l' || c == 'L') 769 { 770 type = parse_java_type->builtin_long; 771 len--; 772 } 773 else 774 { 775 type = parse_java_type->builtin_int; 776 } 777 limit_div_base = limit / (ULONGEST) base; 778 779 while (--len >= 0) 780 { 781 c = *p++; 782 if (c >= '0' && c <= '9') 783 c -= '0'; 784 else if (c >= 'A' && c <= 'Z') 785 c -= 'A' - 10; 786 else if (c >= 'a' && c <= 'z') 787 c -= 'a' - 10; 788 else 789 return ERROR; /* Char not a digit */ 790 if (c >= base) 791 return ERROR; 792 if (n > limit_div_base 793 || (n *= base) > limit - c) 794 error (_("Numeric constant too large")); 795 n += c; 796 } 797 798 /* If the type is bigger than a 32-bit signed integer can be, implicitly 799 promote to long. Java does not do this, so mark it as 800 parse_type->builtin_uint64 rather than parse_java_type->builtin_long. 801 0x80000000 will become -0x80000000 instead of 0x80000000L, because we 802 don't know the sign at this point. */ 803 if (type == parse_java_type->builtin_int && n > (ULONGEST)0x80000000) 804 type = parse_type->builtin_uint64; 805 806 putithere->typed_val_int.val = n; 807 putithere->typed_val_int.type = type; 808 809 return INTEGER_LITERAL; 810 } 811 812 struct token 813 { 814 char *operator; 815 int token; 816 enum exp_opcode opcode; 817 }; 818 819 static const struct token tokentab3[] = 820 { 821 {">>=", ASSIGN_MODIFY, BINOP_RSH}, 822 {"<<=", ASSIGN_MODIFY, BINOP_LSH} 823 }; 824 825 static const struct token tokentab2[] = 826 { 827 {"+=", ASSIGN_MODIFY, BINOP_ADD}, 828 {"-=", ASSIGN_MODIFY, BINOP_SUB}, 829 {"*=", ASSIGN_MODIFY, BINOP_MUL}, 830 {"/=", ASSIGN_MODIFY, BINOP_DIV}, 831 {"%=", ASSIGN_MODIFY, BINOP_REM}, 832 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR}, 833 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND}, 834 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR}, 835 {"++", INCREMENT, BINOP_END}, 836 {"--", DECREMENT, BINOP_END}, 837 {"&&", ANDAND, BINOP_END}, 838 {"||", OROR, BINOP_END}, 839 {"<<", LSH, BINOP_END}, 840 {">>", RSH, BINOP_END}, 841 {"==", EQUAL, BINOP_END}, 842 {"!=", NOTEQUAL, BINOP_END}, 843 {"<=", LEQ, BINOP_END}, 844 {">=", GEQ, BINOP_END} 845 }; 846 847 /* Read one token, getting characters through lexptr. */ 848 849 static int 850 yylex (void) 851 { 852 int c; 853 int namelen; 854 unsigned int i; 855 char *tokstart; 856 char *tokptr; 857 int tempbufindex; 858 static char *tempbuf; 859 static int tempbufsize; 860 861 retry: 862 863 prev_lexptr = lexptr; 864 865 tokstart = lexptr; 866 /* See if it is a special token of length 3. */ 867 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++) 868 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0) 869 { 870 lexptr += 3; 871 yylval.opcode = tokentab3[i].opcode; 872 return tokentab3[i].token; 873 } 874 875 /* See if it is a special token of length 2. */ 876 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++) 877 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0) 878 { 879 lexptr += 2; 880 yylval.opcode = tokentab2[i].opcode; 881 return tokentab2[i].token; 882 } 883 884 switch (c = *tokstart) 885 { 886 case 0: 887 return 0; 888 889 case ' ': 890 case '\t': 891 case '\n': 892 lexptr++; 893 goto retry; 894 895 case '\'': 896 /* We either have a character constant ('0' or '\177' for example) 897 or we have a quoted symbol reference ('foo(int,int)' in C++ 898 for example). */ 899 lexptr++; 900 c = *lexptr++; 901 if (c == '\\') 902 c = parse_escape (parse_gdbarch, &lexptr); 903 else if (c == '\'') 904 error (_("Empty character constant")); 905 906 yylval.typed_val_int.val = c; 907 yylval.typed_val_int.type = parse_java_type->builtin_char; 908 909 c = *lexptr++; 910 if (c != '\'') 911 { 912 namelen = skip_quoted (tokstart) - tokstart; 913 if (namelen > 2) 914 { 915 lexptr = tokstart + namelen; 916 if (lexptr[-1] != '\'') 917 error (_("Unmatched single quote")); 918 namelen -= 2; 919 tokstart++; 920 goto tryname; 921 } 922 error (_("Invalid character constant")); 923 } 924 return INTEGER_LITERAL; 925 926 case '(': 927 paren_depth++; 928 lexptr++; 929 return c; 930 931 case ')': 932 if (paren_depth == 0) 933 return 0; 934 paren_depth--; 935 lexptr++; 936 return c; 937 938 case ',': 939 if (comma_terminates && paren_depth == 0) 940 return 0; 941 lexptr++; 942 return c; 943 944 case '.': 945 /* Might be a floating point number. */ 946 if (lexptr[1] < '0' || lexptr[1] > '9') 947 goto symbol; /* Nope, must be a symbol. */ 948 /* FALL THRU into number case. */ 949 950 case '0': 951 case '1': 952 case '2': 953 case '3': 954 case '4': 955 case '5': 956 case '6': 957 case '7': 958 case '8': 959 case '9': 960 { 961 /* It's a number. */ 962 int got_dot = 0, got_e = 0, toktype; 963 char *p = tokstart; 964 int hex = input_radix > 10; 965 966 if (c == '0' && (p[1] == 'x' || p[1] == 'X')) 967 { 968 p += 2; 969 hex = 1; 970 } 971 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D')) 972 { 973 p += 2; 974 hex = 0; 975 } 976 977 for (;; ++p) 978 { 979 /* This test includes !hex because 'e' is a valid hex digit 980 and thus does not indicate a floating point number when 981 the radix is hex. */ 982 if (!hex && !got_e && (*p == 'e' || *p == 'E')) 983 got_dot = got_e = 1; 984 /* This test does not include !hex, because a '.' always indicates 985 a decimal floating point number regardless of the radix. */ 986 else if (!got_dot && *p == '.') 987 got_dot = 1; 988 else if (got_e && (p[-1] == 'e' || p[-1] == 'E') 989 && (*p == '-' || *p == '+')) 990 /* This is the sign of the exponent, not the end of the 991 number. */ 992 continue; 993 /* We will take any letters or digits. parse_number will 994 complain if past the radix, or if L or U are not final. */ 995 else if ((*p < '0' || *p > '9') 996 && ((*p < 'a' || *p > 'z') 997 && (*p < 'A' || *p > 'Z'))) 998 break; 999 } 1000 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval); 1001 if (toktype == ERROR) 1002 { 1003 char *err_copy = (char *) alloca (p - tokstart + 1); 1004 1005 memcpy (err_copy, tokstart, p - tokstart); 1006 err_copy[p - tokstart] = 0; 1007 error (_("Invalid number \"%s\""), err_copy); 1008 } 1009 lexptr = p; 1010 return toktype; 1011 } 1012 1013 case '+': 1014 case '-': 1015 case '*': 1016 case '/': 1017 case '%': 1018 case '|': 1019 case '&': 1020 case '^': 1021 case '~': 1022 case '!': 1023 case '<': 1024 case '>': 1025 case '[': 1026 case ']': 1027 case '?': 1028 case ':': 1029 case '=': 1030 case '{': 1031 case '}': 1032 symbol: 1033 lexptr++; 1034 return c; 1035 1036 case '"': 1037 1038 /* Build the gdb internal form of the input string in tempbuf, 1039 translating any standard C escape forms seen. Note that the 1040 buffer is null byte terminated *only* for the convenience of 1041 debugging gdb itself and printing the buffer contents when 1042 the buffer contains no embedded nulls. Gdb does not depend 1043 upon the buffer being null byte terminated, it uses the length 1044 string instead. This allows gdb to handle C strings (as well 1045 as strings in other languages) with embedded null bytes */ 1046 1047 tokptr = ++tokstart; 1048 tempbufindex = 0; 1049 1050 do { 1051 /* Grow the static temp buffer if necessary, including allocating 1052 the first one on demand. */ 1053 if (tempbufindex + 1 >= tempbufsize) 1054 { 1055 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64); 1056 } 1057 switch (*tokptr) 1058 { 1059 case '\0': 1060 case '"': 1061 /* Do nothing, loop will terminate. */ 1062 break; 1063 case '\\': 1064 tokptr++; 1065 c = parse_escape (parse_gdbarch, &tokptr); 1066 if (c == -1) 1067 { 1068 continue; 1069 } 1070 tempbuf[tempbufindex++] = c; 1071 break; 1072 default: 1073 tempbuf[tempbufindex++] = *tokptr++; 1074 break; 1075 } 1076 } while ((*tokptr != '"') && (*tokptr != '\0')); 1077 if (*tokptr++ != '"') 1078 { 1079 error (_("Unterminated string in expression")); 1080 } 1081 tempbuf[tempbufindex] = '\0'; /* See note above */ 1082 yylval.sval.ptr = tempbuf; 1083 yylval.sval.length = tempbufindex; 1084 lexptr = tokptr; 1085 return (STRING_LITERAL); 1086 } 1087 1088 if (!(c == '_' || c == '$' 1089 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) 1090 /* We must have come across a bad character (e.g. ';'). */ 1091 error (_("Invalid character '%c' in expression"), c); 1092 1093 /* It's a name. See how long it is. */ 1094 namelen = 0; 1095 for (c = tokstart[namelen]; 1096 (c == '_' 1097 || c == '$' 1098 || (c >= '0' && c <= '9') 1099 || (c >= 'a' && c <= 'z') 1100 || (c >= 'A' && c <= 'Z') 1101 || c == '<'); 1102 ) 1103 { 1104 if (c == '<') 1105 { 1106 int i = namelen; 1107 while (tokstart[++i] && tokstart[i] != '>'); 1108 if (tokstart[i] == '>') 1109 namelen = i; 1110 } 1111 c = tokstart[++namelen]; 1112 } 1113 1114 /* The token "if" terminates the expression and is NOT 1115 removed from the input stream. */ 1116 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f') 1117 { 1118 return 0; 1119 } 1120 1121 lexptr += namelen; 1122 1123 tryname: 1124 1125 /* Catch specific keywords. Should be done with a data structure. */ 1126 switch (namelen) 1127 { 1128 case 7: 1129 if (strncmp (tokstart, "boolean", 7) == 0) 1130 return BOOLEAN; 1131 break; 1132 case 6: 1133 if (strncmp (tokstart, "double", 6) == 0) 1134 return DOUBLE; 1135 break; 1136 case 5: 1137 if (strncmp (tokstart, "short", 5) == 0) 1138 return SHORT; 1139 if (strncmp (tokstart, "false", 5) == 0) 1140 { 1141 yylval.lval = 0; 1142 return BOOLEAN_LITERAL; 1143 } 1144 if (strncmp (tokstart, "super", 5) == 0) 1145 return SUPER; 1146 if (strncmp (tokstart, "float", 5) == 0) 1147 return FLOAT; 1148 break; 1149 case 4: 1150 if (strncmp (tokstart, "long", 4) == 0) 1151 return LONG; 1152 if (strncmp (tokstart, "byte", 4) == 0) 1153 return BYTE; 1154 if (strncmp (tokstart, "char", 4) == 0) 1155 return CHAR; 1156 if (strncmp (tokstart, "true", 4) == 0) 1157 { 1158 yylval.lval = 1; 1159 return BOOLEAN_LITERAL; 1160 } 1161 break; 1162 case 3: 1163 if (strncmp (tokstart, "int", 3) == 0) 1164 return INT; 1165 if (strncmp (tokstart, "new", 3) == 0) 1166 return NEW; 1167 break; 1168 default: 1169 break; 1170 } 1171 1172 yylval.sval.ptr = tokstart; 1173 yylval.sval.length = namelen; 1174 1175 if (*tokstart == '$') 1176 { 1177 write_dollar_variable (yylval.sval); 1178 return VARIABLE; 1179 } 1180 1181 /* Input names that aren't symbols but ARE valid hex numbers, 1182 when the input radix permits them, can be names or numbers 1183 depending on the parse. Note we support radixes > 16 here. */ 1184 if (((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) || 1185 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10))) 1186 { 1187 YYSTYPE newlval; /* Its value is ignored. */ 1188 int hextype = parse_number (tokstart, namelen, 0, &newlval); 1189 if (hextype == INTEGER_LITERAL) 1190 return NAME_OR_INT; 1191 } 1192 return IDENTIFIER; 1193 } 1194 1195 void 1196 yyerror (char *msg) 1197 { 1198 if (prev_lexptr) 1199 lexptr = prev_lexptr; 1200 1201 if (msg) 1202 error (_("%s: near `%s'"), msg, lexptr); 1203 else 1204 error (_("error in expression, near `%s'"), lexptr); 1205 } 1206 1207 static struct type * 1208 java_type_from_name (struct stoken name) 1209 { 1210 char *tmp = copy_name (name); 1211 struct type *typ = java_lookup_class (tmp); 1212 if (typ == NULL || TYPE_CODE (typ) != TYPE_CODE_STRUCT) 1213 error (_("No class named `%s'"), tmp); 1214 return typ; 1215 } 1216 1217 /* If NAME is a valid variable name in this scope, push it and return 1. 1218 Otherwise, return 0. */ 1219 1220 static int 1221 push_variable (struct stoken name) 1222 { 1223 char *tmp = copy_name (name); 1224 int is_a_field_of_this = 0; 1225 struct symbol *sym; 1226 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN, 1227 &is_a_field_of_this); 1228 if (sym && SYMBOL_CLASS (sym) != LOC_TYPEDEF) 1229 { 1230 if (symbol_read_needs_frame (sym)) 1231 { 1232 if (innermost_block == 0 || 1233 contained_in (block_found, innermost_block)) 1234 innermost_block = block_found; 1235 } 1236 1237 write_exp_elt_opcode (OP_VAR_VALUE); 1238 /* We want to use the selected frame, not another more inner frame 1239 which happens to be in the same block. */ 1240 write_exp_elt_block (NULL); 1241 write_exp_elt_sym (sym); 1242 write_exp_elt_opcode (OP_VAR_VALUE); 1243 return 1; 1244 } 1245 if (is_a_field_of_this) 1246 { 1247 /* it hangs off of `this'. Must not inadvertently convert from a 1248 method call to data ref. */ 1249 if (innermost_block == 0 || 1250 contained_in (block_found, innermost_block)) 1251 innermost_block = block_found; 1252 write_exp_elt_opcode (OP_THIS); 1253 write_exp_elt_opcode (OP_THIS); 1254 write_exp_elt_opcode (STRUCTOP_PTR); 1255 write_exp_string (name); 1256 write_exp_elt_opcode (STRUCTOP_PTR); 1257 return 1; 1258 } 1259 return 0; 1260 } 1261 1262 /* Assuming a reference expression has been pushed, emit the 1263 STRUCTOP_PTR ops to access the field named NAME. If NAME is a 1264 qualified name (has '.'), generate a field access for each part. */ 1265 1266 static void 1267 push_fieldnames (struct stoken name) 1268 { 1269 int i; 1270 struct stoken token; 1271 token.ptr = name.ptr; 1272 for (i = 0; ; i++) 1273 { 1274 if (i == name.length || name.ptr[i] == '.') 1275 { 1276 /* token.ptr is start of current field name. */ 1277 token.length = &name.ptr[i] - token.ptr; 1278 write_exp_elt_opcode (STRUCTOP_PTR); 1279 write_exp_string (token); 1280 write_exp_elt_opcode (STRUCTOP_PTR); 1281 token.ptr += token.length + 1; 1282 } 1283 if (i >= name.length) 1284 break; 1285 } 1286 } 1287 1288 /* Helper routine for push_expression_name. 1289 Handle a qualified name, where DOT_INDEX is the index of the first '.' */ 1290 1291 static void 1292 push_qualified_expression_name (struct stoken name, int dot_index) 1293 { 1294 struct stoken token; 1295 char *tmp; 1296 struct type *typ; 1297 1298 token.ptr = name.ptr; 1299 token.length = dot_index; 1300 1301 if (push_variable (token)) 1302 { 1303 token.ptr = name.ptr + dot_index + 1; 1304 token.length = name.length - dot_index - 1; 1305 push_fieldnames (token); 1306 return; 1307 } 1308 1309 token.ptr = name.ptr; 1310 for (;;) 1311 { 1312 token.length = dot_index; 1313 tmp = copy_name (token); 1314 typ = java_lookup_class (tmp); 1315 if (typ != NULL) 1316 { 1317 if (dot_index == name.length) 1318 { 1319 write_exp_elt_opcode(OP_TYPE); 1320 write_exp_elt_type(typ); 1321 write_exp_elt_opcode(OP_TYPE); 1322 return; 1323 } 1324 dot_index++; /* Skip '.' */ 1325 name.ptr += dot_index; 1326 name.length -= dot_index; 1327 dot_index = 0; 1328 while (dot_index < name.length && name.ptr[dot_index] != '.') 1329 dot_index++; 1330 token.ptr = name.ptr; 1331 token.length = dot_index; 1332 write_exp_elt_opcode (OP_SCOPE); 1333 write_exp_elt_type (typ); 1334 write_exp_string (token); 1335 write_exp_elt_opcode (OP_SCOPE); 1336 if (dot_index < name.length) 1337 { 1338 dot_index++; 1339 name.ptr += dot_index; 1340 name.length -= dot_index; 1341 push_fieldnames (name); 1342 } 1343 return; 1344 } 1345 else if (dot_index >= name.length) 1346 break; 1347 dot_index++; /* Skip '.' */ 1348 while (dot_index < name.length && name.ptr[dot_index] != '.') 1349 dot_index++; 1350 } 1351 error (_("unknown type `%.*s'"), name.length, name.ptr); 1352 } 1353 1354 /* Handle Name in an expression (or LHS). 1355 Handle VAR, TYPE, TYPE.FIELD1....FIELDN and VAR.FIELD1....FIELDN. */ 1356 1357 static void 1358 push_expression_name (struct stoken name) 1359 { 1360 char *tmp; 1361 struct type *typ; 1362 int i; 1363 1364 for (i = 0; i < name.length; i++) 1365 { 1366 if (name.ptr[i] == '.') 1367 { 1368 /* It's a Qualified Expression Name. */ 1369 push_qualified_expression_name (name, i); 1370 return; 1371 } 1372 } 1373 1374 /* It's a Simple Expression Name. */ 1375 1376 if (push_variable (name)) 1377 return; 1378 tmp = copy_name (name); 1379 typ = java_lookup_class (tmp); 1380 if (typ != NULL) 1381 { 1382 write_exp_elt_opcode(OP_TYPE); 1383 write_exp_elt_type(typ); 1384 write_exp_elt_opcode(OP_TYPE); 1385 } 1386 else 1387 { 1388 struct minimal_symbol *msymbol; 1389 1390 msymbol = lookup_minimal_symbol (tmp, NULL, NULL); 1391 if (msymbol != NULL) 1392 write_exp_msymbol (msymbol); 1393 else if (!have_full_symbols () && !have_partial_symbols ()) 1394 error (_("No symbol table is loaded. Use the \"file\" command")); 1395 else 1396 error (_("No symbol \"%s\" in current context"), tmp); 1397 } 1398 1399 } 1400 1401 1402 /* The following two routines, copy_exp and insert_exp, aren't specific to 1403 Java, so they could go in parse.c, but their only purpose is to support 1404 the parsing kludges we use in this file, so maybe it's best to isolate 1405 them here. */ 1406 1407 /* Copy the expression whose last element is at index ENDPOS - 1 in EXPR 1408 into a freshly malloc'ed struct expression. Its language_defn is set 1409 to null. */ 1410 static struct expression * 1411 copy_exp (struct expression *expr, int endpos) 1412 { 1413 int len = length_of_subexp (expr, endpos); 1414 struct expression *new 1415 = (struct expression *) malloc (sizeof (*new) + EXP_ELEM_TO_BYTES (len)); 1416 new->nelts = len; 1417 memcpy (new->elts, expr->elts + endpos - len, EXP_ELEM_TO_BYTES (len)); 1418 new->language_defn = 0; 1419 1420 return new; 1421 } 1422 1423 /* Insert the expression NEW into the current expression (expout) at POS. */ 1424 static void 1425 insert_exp (int pos, struct expression *new) 1426 { 1427 int newlen = new->nelts; 1428 1429 /* Grow expout if necessary. In this function's only use at present, 1430 this should never be necessary. */ 1431 if (expout_ptr + newlen > expout_size) 1432 { 1433 expout_size = max (expout_size * 2, expout_ptr + newlen + 10); 1434 expout = (struct expression *) 1435 realloc ((char *) expout, (sizeof (struct expression) 1436 + EXP_ELEM_TO_BYTES (expout_size))); 1437 } 1438 1439 { 1440 int i; 1441 1442 for (i = expout_ptr - 1; i >= pos; i--) 1443 expout->elts[i + newlen] = expout->elts[i]; 1444 } 1445 1446 memcpy (expout->elts + pos, new->elts, EXP_ELEM_TO_BYTES (newlen)); 1447 expout_ptr += newlen; 1448 } 1449