1 /* YACC parser for Java expressions, for GDB.
2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
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.
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. */
41 #include "expression.h"
43 #include "parser-defs.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 */
50 #include "completer.h"
52 #define parse_type builtin_type (parse_gdbarch)
53 #define parse_java_type builtin_java_type (parse_gdbarch)
55 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
56 as well as gratuitiously global symbol names, so we can have multiple
57 yacc generated parsers in gdb. Note that these are only the variables
58 produced by yacc. If other parser generators (bison, byacc, etc) produce
59 additional global names that conflict at link time, then those parser
60 generators need to be fixed instead of adding those names to this list. */
62 #define yymaxdepth java_maxdepth
63 #define yyparse java_parse
64 #define yylex java_lex
65 #define yyerror java_error
66 #define yylval java_lval
67 #define yychar java_char
68 #define yydebug java_debug
69 #define yypact java_pact
72 #define yydef java_def
73 #define yychk java_chk
74 #define yypgo java_pgo
75 #define yyact java_act
76 #define yyexca java_exca
77 #define yyerrflag java_errflag
78 #define yynerrs java_nerrs
82 #define yy_yys java_yys
83 #define yystate java_state
84 #define yytmp java_tmp
86 #define yy_yyv java_yyv
87 #define yyval java_val
88 #define yylloc java_lloc
89 #define yyreds java_reds /* With YYDEBUG defined */
90 #define yytoks java_toks /* With YYDEBUG defined */
91 #define yyname java_name /* With YYDEBUG defined */
92 #define yyrule java_rule /* With YYDEBUG defined */
93 #define yylhs java_yylhs
94 #define yylen java_yylen
95 #define yydefred java_yydefred
96 #define yydgoto java_yydgoto
97 #define yysindex java_yysindex
98 #define yyrindex java_yyrindex
99 #define yygindex java_yygindex
100 #define yytable java_yytable
101 #define yycheck java_yycheck
102 #define yyss java_yyss
103 #define yysslim java_yysslim
104 #define yyssp java_yyssp
105 #define yystacksize java_yystacksize
106 #define yyvs java_yyvs
107 #define yyvsp java_yyvsp
110 #define YYDEBUG 1 /* Default to yydebug support */
113 #define YYFPRINTF parser_fprintf
117 static int yylex (void);
119 void yyerror (char *);
121 static struct type *java_type_from_name (struct stoken);
122 static void push_expression_name (struct stoken);
123 static void push_fieldnames (struct stoken);
125 static struct expression *copy_exp (struct expression *, int);
126 static void insert_exp (int, struct expression *);
130 /* Although the yacc "value" of an expression is not used,
131 since the result is stored in the structure being created,
132 other node types do have values. */
149 struct symtoken ssym;
151 enum exp_opcode opcode;
152 struct internalvar *ivar;
157 /* YYSTYPE gets defined by %union */
158 static int parse_number (const char *, int, int, YYSTYPE *);
161 %type <lval> rcurly Dims Dims_opt
162 %type <tval> ClassOrInterfaceType ClassType /* ReferenceType Type ArrayType */
163 %type <tval> IntegralType FloatingPointType NumericType PrimitiveType ArrayType PrimitiveOrArrayType
165 %token <typed_val_int> INTEGER_LITERAL
166 %token <typed_val_float> FLOATING_POINT_LITERAL
168 %token <sval> IDENTIFIER
169 %token <sval> STRING_LITERAL
170 %token <lval> BOOLEAN_LITERAL
171 %token <tsym> TYPENAME
172 %type <sval> Name SimpleName QualifiedName ForcedName
174 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
175 but which would parse as a valid number in the current input radix.
176 E.g. "c" when input_radix==16. Depending on the parse, it will be
177 turned into a name or into a number. */
179 %token <sval> NAME_OR_INT
183 /* Special type cases, put in to allow the parser to distinguish different
185 %token LONG SHORT BYTE INT CHAR BOOLEAN DOUBLE FLOAT
189 %token <opcode> ASSIGN_MODIFY
194 %right '=' ASSIGN_MODIFY
202 %left '<' '>' LEQ GEQ
206 %right INCREMENT DECREMENT
216 type_exp: PrimitiveOrArrayType
218 write_exp_elt_opcode(OP_TYPE);
219 write_exp_elt_type($1);
220 write_exp_elt_opcode(OP_TYPE);
224 PrimitiveOrArrayType:
232 write_exp_elt_opcode (OP_STRING);
233 write_exp_string ($1);
234 write_exp_elt_opcode (OP_STRING);
240 { write_exp_elt_opcode (OP_LONG);
241 write_exp_elt_type ($1.type);
242 write_exp_elt_longcst ((LONGEST)($1.val));
243 write_exp_elt_opcode (OP_LONG); }
246 parse_number ($1.ptr, $1.length, 0, &val);
247 write_exp_elt_opcode (OP_LONG);
248 write_exp_elt_type (val.typed_val_int.type);
249 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
250 write_exp_elt_opcode (OP_LONG);
252 | FLOATING_POINT_LITERAL
253 { write_exp_elt_opcode (OP_DOUBLE);
254 write_exp_elt_type ($1.type);
255 write_exp_elt_dblcst ($1.dval);
256 write_exp_elt_opcode (OP_DOUBLE); }
258 { write_exp_elt_opcode (OP_LONG);
259 write_exp_elt_type (parse_java_type->builtin_boolean);
260 write_exp_elt_longcst ((LONGEST)$1);
261 write_exp_elt_opcode (OP_LONG); }
275 { $$ = parse_java_type->builtin_boolean; }
285 { $$ = parse_java_type->builtin_byte; }
287 { $$ = parse_java_type->builtin_short; }
289 { $$ = parse_java_type->builtin_int; }
291 { $$ = parse_java_type->builtin_long; }
293 { $$ = parse_java_type->builtin_char; }
298 { $$ = parse_java_type->builtin_float; }
300 { $$ = parse_java_type->builtin_double; }
310 ClassOrInterfaceType:
312 { $$ = java_type_from_name ($1); }
321 { $$ = java_array_type ($1, $2); }
323 { $$ = java_array_type (java_type_from_name ($1), $2); }
343 { $$.length = $1.length + $3.length + 1;
344 if ($1.ptr + $1.length + 1 == $3.ptr
345 && $1.ptr[$1.length] == '.')
346 $$.ptr = $1.ptr; /* Optimization. */
351 buf = malloc ($$.length + 1);
352 make_cleanup (free, buf);
353 sprintf (buf, "%.*s.%.*s",
354 $1.length, $1.ptr, $3.length, $3.ptr);
361 { write_exp_elt_opcode(OP_TYPE);
362 write_exp_elt_type($1);
363 write_exp_elt_opcode(OP_TYPE);}
367 /* Expressions, including the comma operator. */
369 | exp1 ',' Expression
370 { write_exp_elt_opcode (BINOP_COMMA); }
375 | ArrayCreationExpression
381 | ClassInstanceCreationExpression
385 | lcurly ArgumentList rcurly
386 { write_exp_elt_opcode (OP_ARRAY);
387 write_exp_elt_longcst ((LONGEST) 0);
388 write_exp_elt_longcst ((LONGEST) $3);
389 write_exp_elt_opcode (OP_ARRAY); }
394 { start_arglist (); }
399 { $$ = end_arglist () - 1; }
402 ClassInstanceCreationExpression:
403 NEW ClassType '(' ArgumentList_opt ')'
404 { internal_error (__FILE__, __LINE__,
405 _("FIXME - ClassInstanceCreationExpression")); }
411 | ArgumentList ',' Expression
421 ArrayCreationExpression:
422 NEW PrimitiveType DimExprs Dims_opt
423 { internal_error (__FILE__, __LINE__,
424 _("FIXME - ArrayCreationExpression")); }
425 | NEW ClassOrInterfaceType DimExprs Dims_opt
426 { internal_error (__FILE__, __LINE__,
427 _("FIXME - ArrayCreationExpression")); }
453 Primary '.' SimpleName
454 { push_fieldnames ($3); }
455 | VARIABLE '.' SimpleName
456 { push_fieldnames ($3); }
457 /*| SUPER '.' SimpleName { FIXME } */
462 { push_expression_name ($1); }
469 { write_exp_elt_opcode (OP_FUNCALL);
470 write_exp_elt_longcst ((LONGEST) end_arglist ());
471 write_exp_elt_opcode (OP_FUNCALL); }
472 | Primary '.' SimpleName '(' ArgumentList_opt ')'
473 { error (_("Form of method invocation not implemented")); }
474 | SUPER '.' SimpleName '(' ArgumentList_opt ')'
475 { error (_("Form of method invocation not implemented")); }
479 Name '[' Expression ']'
481 /* Emit code for the Name now, then exchange it in the
482 expout array with the Expression's code. We could
483 introduce a OP_SWAP code or a reversed version of
484 BINOP_SUBSCRIPT, but that makes the rest of GDB pay
485 for our parsing kludges. */
486 struct expression *name_expr;
488 push_expression_name ($1);
489 name_expr = copy_exp (expout, expout_ptr);
490 expout_ptr -= name_expr->nelts;
491 insert_exp (expout_ptr-length_of_subexp (expout, expout_ptr),
494 write_exp_elt_opcode (BINOP_SUBSCRIPT);
496 | VARIABLE '[' Expression ']'
497 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
498 | PrimaryNoNewArray '[' Expression ']'
499 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
505 { push_expression_name ($1); }
507 /* Already written by write_dollar_variable. */
508 | PostIncrementExpression
509 | PostDecrementExpression
512 PostIncrementExpression:
513 PostfixExpression INCREMENT
514 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
517 PostDecrementExpression:
518 PostfixExpression DECREMENT
519 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
523 PreIncrementExpression
524 | PreDecrementExpression
525 | '+' UnaryExpression
526 | '-' UnaryExpression
527 { write_exp_elt_opcode (UNOP_NEG); }
528 | '*' UnaryExpression
529 { write_exp_elt_opcode (UNOP_IND); } /*FIXME not in Java */
530 | UnaryExpressionNotPlusMinus
533 PreIncrementExpression:
534 INCREMENT UnaryExpression
535 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
538 PreDecrementExpression:
539 DECREMENT UnaryExpression
540 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
543 UnaryExpressionNotPlusMinus:
545 | '~' UnaryExpression
546 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
547 | '!' UnaryExpression
548 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
553 '(' PrimitiveType Dims_opt ')' UnaryExpression
554 { write_exp_elt_opcode (UNOP_CAST);
555 write_exp_elt_type (java_array_type ($2, $3));
556 write_exp_elt_opcode (UNOP_CAST); }
557 | '(' Expression ')' UnaryExpressionNotPlusMinus
559 int last_exp_size = length_of_subexp(expout, expout_ptr);
562 int base = expout_ptr - last_exp_size - 3;
563 if (base < 0 || expout->elts[base+2].opcode != OP_TYPE)
564 error (_("Invalid cast expression"));
565 type = expout->elts[base+1].type;
566 /* Remove the 'Expression' and slide the
567 UnaryExpressionNotPlusMinus down to replace it. */
568 for (i = 0; i < last_exp_size; i++)
569 expout->elts[base + i] = expout->elts[base + i + 3];
571 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
572 type = lookup_pointer_type (type);
573 write_exp_elt_opcode (UNOP_CAST);
574 write_exp_elt_type (type);
575 write_exp_elt_opcode (UNOP_CAST);
577 | '(' Name Dims ')' UnaryExpressionNotPlusMinus
578 { write_exp_elt_opcode (UNOP_CAST);
579 write_exp_elt_type (java_array_type (java_type_from_name ($2), $3));
580 write_exp_elt_opcode (UNOP_CAST); }
584 MultiplicativeExpression:
586 | MultiplicativeExpression '*' UnaryExpression
587 { write_exp_elt_opcode (BINOP_MUL); }
588 | MultiplicativeExpression '/' UnaryExpression
589 { write_exp_elt_opcode (BINOP_DIV); }
590 | MultiplicativeExpression '%' UnaryExpression
591 { write_exp_elt_opcode (BINOP_REM); }
595 MultiplicativeExpression
596 | AdditiveExpression '+' MultiplicativeExpression
597 { write_exp_elt_opcode (BINOP_ADD); }
598 | AdditiveExpression '-' MultiplicativeExpression
599 { write_exp_elt_opcode (BINOP_SUB); }
604 | ShiftExpression LSH AdditiveExpression
605 { write_exp_elt_opcode (BINOP_LSH); }
606 | ShiftExpression RSH AdditiveExpression
607 { write_exp_elt_opcode (BINOP_RSH); }
608 /* | ShiftExpression >>> AdditiveExpression { FIXME } */
611 RelationalExpression:
613 | RelationalExpression '<' ShiftExpression
614 { write_exp_elt_opcode (BINOP_LESS); }
615 | RelationalExpression '>' ShiftExpression
616 { write_exp_elt_opcode (BINOP_GTR); }
617 | RelationalExpression LEQ ShiftExpression
618 { write_exp_elt_opcode (BINOP_LEQ); }
619 | RelationalExpression GEQ ShiftExpression
620 { write_exp_elt_opcode (BINOP_GEQ); }
621 /* | RelationalExpresion INSTANCEOF ReferenceType { FIXME } */
626 | EqualityExpression EQUAL RelationalExpression
627 { write_exp_elt_opcode (BINOP_EQUAL); }
628 | EqualityExpression NOTEQUAL RelationalExpression
629 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
634 | AndExpression '&' EqualityExpression
635 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
638 ExclusiveOrExpression:
640 | ExclusiveOrExpression '^' AndExpression
641 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
643 InclusiveOrExpression:
644 ExclusiveOrExpression
645 | InclusiveOrExpression '|' ExclusiveOrExpression
646 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
649 ConditionalAndExpression:
650 InclusiveOrExpression
651 | ConditionalAndExpression ANDAND InclusiveOrExpression
652 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
655 ConditionalOrExpression:
656 ConditionalAndExpression
657 | ConditionalOrExpression OROR ConditionalAndExpression
658 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
661 ConditionalExpression:
662 ConditionalOrExpression
663 | ConditionalOrExpression '?' Expression ':' ConditionalExpression
664 { write_exp_elt_opcode (TERNOP_COND); }
667 AssignmentExpression:
668 ConditionalExpression
673 LeftHandSide '=' ConditionalExpression
674 { write_exp_elt_opcode (BINOP_ASSIGN); }
675 | LeftHandSide ASSIGN_MODIFY ConditionalExpression
676 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
677 write_exp_elt_opcode ($2);
678 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
683 { push_expression_name ($1); }
685 /* Already written by write_dollar_variable. */
696 /* Take care of parsing a number (anything that starts with a digit).
697 Set yylval and return the token type; update lexptr.
698 LEN is the number of characters in it. */
700 /*** Needs some error checking for the float case ***/
703 parse_number (const char *p, int len, int parsed_float, YYSTYPE *putithere)
706 ULONGEST limit, limit_div_base;
709 int base = input_radix;
718 if (! parse_float (p, len, &putithere->typed_val_float.dval, &suffix))
721 suffix_len = p + len - suffix;
724 putithere->typed_val_float.type = parse_type->builtin_double;
725 else if (suffix_len == 1)
727 /* See if it has `f' or `d' suffix (float or double). */
728 if (tolower (*suffix) == 'f')
729 putithere->typed_val_float.type =
730 parse_type->builtin_float;
731 else if (tolower (*suffix) == 'd')
732 putithere->typed_val_float.type =
733 parse_type->builtin_double;
740 return FLOATING_POINT_LITERAL;
743 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
775 /* A paranoid calculation of (1<<64)-1. */
776 limit = (ULONGEST)0xffffffff;
777 limit = ((limit << 16) << 16) | limit;
778 if (c == 'l' || c == 'L')
780 type = parse_java_type->builtin_long;
785 type = parse_java_type->builtin_int;
787 limit_div_base = limit / (ULONGEST) base;
792 if (c >= '0' && c <= '9')
794 else if (c >= 'A' && c <= 'Z')
796 else if (c >= 'a' && c <= 'z')
799 return ERROR; /* Char not a digit */
802 if (n > limit_div_base
803 || (n *= base) > limit - c)
804 error (_("Numeric constant too large"));
808 /* If the type is bigger than a 32-bit signed integer can be, implicitly
809 promote to long. Java does not do this, so mark it as
810 parse_type->builtin_uint64 rather than parse_java_type->builtin_long.
811 0x80000000 will become -0x80000000 instead of 0x80000000L, because we
812 don't know the sign at this point. */
813 if (type == parse_java_type->builtin_int && n > (ULONGEST)0x80000000)
814 type = parse_type->builtin_uint64;
816 putithere->typed_val_int.val = n;
817 putithere->typed_val_int.type = type;
819 return INTEGER_LITERAL;
826 enum exp_opcode opcode;
829 static const struct token tokentab3[] =
831 {">>=", ASSIGN_MODIFY, BINOP_RSH},
832 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
835 static const struct token tokentab2[] =
837 {"+=", ASSIGN_MODIFY, BINOP_ADD},
838 {"-=", ASSIGN_MODIFY, BINOP_SUB},
839 {"*=", ASSIGN_MODIFY, BINOP_MUL},
840 {"/=", ASSIGN_MODIFY, BINOP_DIV},
841 {"%=", ASSIGN_MODIFY, BINOP_REM},
842 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
843 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
844 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
845 {"++", INCREMENT, BINOP_END},
846 {"--", DECREMENT, BINOP_END},
847 {"&&", ANDAND, BINOP_END},
848 {"||", OROR, BINOP_END},
849 {"<<", LSH, BINOP_END},
850 {">>", RSH, BINOP_END},
851 {"==", EQUAL, BINOP_END},
852 {"!=", NOTEQUAL, BINOP_END},
853 {"<=", LEQ, BINOP_END},
854 {">=", GEQ, BINOP_END}
857 /* Read one token, getting characters through lexptr. */
865 const char *tokstart;
868 static char *tempbuf;
869 static int tempbufsize;
873 prev_lexptr = lexptr;
876 /* See if it is a special token of length 3. */
877 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
878 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
881 yylval.opcode = tokentab3[i].opcode;
882 return tokentab3[i].token;
885 /* See if it is a special token of length 2. */
886 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
887 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
890 yylval.opcode = tokentab2[i].opcode;
891 return tokentab2[i].token;
894 switch (c = *tokstart)
906 /* We either have a character constant ('0' or '\177' for example)
907 or we have a quoted symbol reference ('foo(int,int)' in C++
912 c = parse_escape (parse_gdbarch, &lexptr);
914 error (_("Empty character constant"));
916 yylval.typed_val_int.val = c;
917 yylval.typed_val_int.type = parse_java_type->builtin_char;
922 namelen = skip_quoted (tokstart) - tokstart;
925 lexptr = tokstart + namelen;
926 if (lexptr[-1] != '\'')
927 error (_("Unmatched single quote"));
932 error (_("Invalid character constant"));
934 return INTEGER_LITERAL;
942 if (paren_depth == 0)
949 if (comma_terminates && paren_depth == 0)
955 /* Might be a floating point number. */
956 if (lexptr[1] < '0' || lexptr[1] > '9')
957 goto symbol; /* Nope, must be a symbol. */
958 /* FALL THRU into number case. */
972 int got_dot = 0, got_e = 0, toktype;
973 const char *p = tokstart;
974 int hex = input_radix > 10;
976 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
981 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
989 /* This test includes !hex because 'e' is a valid hex digit
990 and thus does not indicate a floating point number when
992 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
994 /* This test does not include !hex, because a '.' always indicates
995 a decimal floating point number regardless of the radix. */
996 else if (!got_dot && *p == '.')
998 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
999 && (*p == '-' || *p == '+'))
1000 /* This is the sign of the exponent, not the end of the
1003 /* We will take any letters or digits. parse_number will
1004 complain if past the radix, or if L or U are not final. */
1005 else if ((*p < '0' || *p > '9')
1006 && ((*p < 'a' || *p > 'z')
1007 && (*p < 'A' || *p > 'Z')))
1010 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1011 if (toktype == ERROR)
1013 char *err_copy = (char *) alloca (p - tokstart + 1);
1015 memcpy (err_copy, tokstart, p - tokstart);
1016 err_copy[p - tokstart] = 0;
1017 error (_("Invalid number \"%s\""), err_copy);
1048 /* Build the gdb internal form of the input string in tempbuf,
1049 translating any standard C escape forms seen. Note that the
1050 buffer is null byte terminated *only* for the convenience of
1051 debugging gdb itself and printing the buffer contents when
1052 the buffer contains no embedded nulls. Gdb does not depend
1053 upon the buffer being null byte terminated, it uses the length
1054 string instead. This allows gdb to handle C strings (as well
1055 as strings in other languages) with embedded null bytes */
1057 tokptr = ++tokstart;
1061 /* Grow the static temp buffer if necessary, including allocating
1062 the first one on demand. */
1063 if (tempbufindex + 1 >= tempbufsize)
1065 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1071 /* Do nothing, loop will terminate. */
1075 c = parse_escape (parse_gdbarch, &tokptr);
1080 tempbuf[tempbufindex++] = c;
1083 tempbuf[tempbufindex++] = *tokptr++;
1086 } while ((*tokptr != '"') && (*tokptr != '\0'));
1087 if (*tokptr++ != '"')
1089 error (_("Unterminated string in expression"));
1091 tempbuf[tempbufindex] = '\0'; /* See note above */
1092 yylval.sval.ptr = tempbuf;
1093 yylval.sval.length = tempbufindex;
1095 return (STRING_LITERAL);
1098 if (!(c == '_' || c == '$'
1099 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1100 /* We must have come across a bad character (e.g. ';'). */
1101 error (_("Invalid character '%c' in expression"), c);
1103 /* It's a name. See how long it is. */
1105 for (c = tokstart[namelen];
1108 || (c >= '0' && c <= '9')
1109 || (c >= 'a' && c <= 'z')
1110 || (c >= 'A' && c <= 'Z')
1117 while (tokstart[++i] && tokstart[i] != '>');
1118 if (tokstart[i] == '>')
1121 c = tokstart[++namelen];
1124 /* The token "if" terminates the expression and is NOT
1125 removed from the input stream. */
1126 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1135 /* Catch specific keywords. Should be done with a data structure. */
1139 if (strncmp (tokstart, "boolean", 7) == 0)
1143 if (strncmp (tokstart, "double", 6) == 0)
1147 if (strncmp (tokstart, "short", 5) == 0)
1149 if (strncmp (tokstart, "false", 5) == 0)
1152 return BOOLEAN_LITERAL;
1154 if (strncmp (tokstart, "super", 5) == 0)
1156 if (strncmp (tokstart, "float", 5) == 0)
1160 if (strncmp (tokstart, "long", 4) == 0)
1162 if (strncmp (tokstart, "byte", 4) == 0)
1164 if (strncmp (tokstart, "char", 4) == 0)
1166 if (strncmp (tokstart, "true", 4) == 0)
1169 return BOOLEAN_LITERAL;
1173 if (strncmp (tokstart, "int", 3) == 0)
1175 if (strncmp (tokstart, "new", 3) == 0)
1182 yylval.sval.ptr = tokstart;
1183 yylval.sval.length = namelen;
1185 if (*tokstart == '$')
1187 write_dollar_variable (yylval.sval);
1191 /* Input names that aren't symbols but ARE valid hex numbers,
1192 when the input radix permits them, can be names or numbers
1193 depending on the parse. Note we support radixes > 16 here. */
1194 if (((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1195 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1197 YYSTYPE newlval; /* Its value is ignored. */
1198 int hextype = parse_number (tokstart, namelen, 0, &newlval);
1199 if (hextype == INTEGER_LITERAL)
1209 lexptr = prev_lexptr;
1212 error (_("%s: near `%s'"), msg, lexptr);
1214 error (_("error in expression, near `%s'"), lexptr);
1217 static struct type *
1218 java_type_from_name (struct stoken name)
1220 char *tmp = copy_name (name);
1221 struct type *typ = java_lookup_class (tmp);
1222 if (typ == NULL || TYPE_CODE (typ) != TYPE_CODE_STRUCT)
1223 error (_("No class named `%s'"), tmp);
1227 /* If NAME is a valid variable name in this scope, push it and return 1.
1228 Otherwise, return 0. */
1231 push_variable (struct stoken name)
1233 char *tmp = copy_name (name);
1234 struct field_of_this_result is_a_field_of_this;
1236 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN,
1237 &is_a_field_of_this);
1238 if (sym && SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1240 if (symbol_read_needs_frame (sym))
1242 if (innermost_block == 0 ||
1243 contained_in (block_found, innermost_block))
1244 innermost_block = block_found;
1247 write_exp_elt_opcode (OP_VAR_VALUE);
1248 /* We want to use the selected frame, not another more inner frame
1249 which happens to be in the same block. */
1250 write_exp_elt_block (NULL);
1251 write_exp_elt_sym (sym);
1252 write_exp_elt_opcode (OP_VAR_VALUE);
1255 if (is_a_field_of_this.type != NULL)
1257 /* it hangs off of `this'. Must not inadvertently convert from a
1258 method call to data ref. */
1259 if (innermost_block == 0 ||
1260 contained_in (block_found, innermost_block))
1261 innermost_block = block_found;
1262 write_exp_elt_opcode (OP_THIS);
1263 write_exp_elt_opcode (OP_THIS);
1264 write_exp_elt_opcode (STRUCTOP_PTR);
1265 write_exp_string (name);
1266 write_exp_elt_opcode (STRUCTOP_PTR);
1272 /* Assuming a reference expression has been pushed, emit the
1273 STRUCTOP_PTR ops to access the field named NAME. If NAME is a
1274 qualified name (has '.'), generate a field access for each part. */
1277 push_fieldnames (struct stoken name)
1280 struct stoken token;
1281 token.ptr = name.ptr;
1284 if (i == name.length || name.ptr[i] == '.')
1286 /* token.ptr is start of current field name. */
1287 token.length = &name.ptr[i] - token.ptr;
1288 write_exp_elt_opcode (STRUCTOP_PTR);
1289 write_exp_string (token);
1290 write_exp_elt_opcode (STRUCTOP_PTR);
1291 token.ptr += token.length + 1;
1293 if (i >= name.length)
1298 /* Helper routine for push_expression_name.
1299 Handle a qualified name, where DOT_INDEX is the index of the first '.' */
1302 push_qualified_expression_name (struct stoken name, int dot_index)
1304 struct stoken token;
1308 token.ptr = name.ptr;
1309 token.length = dot_index;
1311 if (push_variable (token))
1313 token.ptr = name.ptr + dot_index + 1;
1314 token.length = name.length - dot_index - 1;
1315 push_fieldnames (token);
1319 token.ptr = name.ptr;
1322 token.length = dot_index;
1323 tmp = copy_name (token);
1324 typ = java_lookup_class (tmp);
1327 if (dot_index == name.length)
1329 write_exp_elt_opcode(OP_TYPE);
1330 write_exp_elt_type(typ);
1331 write_exp_elt_opcode(OP_TYPE);
1334 dot_index++; /* Skip '.' */
1335 name.ptr += dot_index;
1336 name.length -= dot_index;
1338 while (dot_index < name.length && name.ptr[dot_index] != '.')
1340 token.ptr = name.ptr;
1341 token.length = dot_index;
1342 write_exp_elt_opcode (OP_SCOPE);
1343 write_exp_elt_type (typ);
1344 write_exp_string (token);
1345 write_exp_elt_opcode (OP_SCOPE);
1346 if (dot_index < name.length)
1349 name.ptr += dot_index;
1350 name.length -= dot_index;
1351 push_fieldnames (name);
1355 else if (dot_index >= name.length)
1357 dot_index++; /* Skip '.' */
1358 while (dot_index < name.length && name.ptr[dot_index] != '.')
1361 error (_("unknown type `%.*s'"), name.length, name.ptr);
1364 /* Handle Name in an expression (or LHS).
1365 Handle VAR, TYPE, TYPE.FIELD1....FIELDN and VAR.FIELD1....FIELDN. */
1368 push_expression_name (struct stoken name)
1374 for (i = 0; i < name.length; i++)
1376 if (name.ptr[i] == '.')
1378 /* It's a Qualified Expression Name. */
1379 push_qualified_expression_name (name, i);
1384 /* It's a Simple Expression Name. */
1386 if (push_variable (name))
1388 tmp = copy_name (name);
1389 typ = java_lookup_class (tmp);
1392 write_exp_elt_opcode(OP_TYPE);
1393 write_exp_elt_type(typ);
1394 write_exp_elt_opcode(OP_TYPE);
1398 struct bound_minimal_symbol msymbol;
1400 msymbol = lookup_bound_minimal_symbol (tmp);
1401 if (msymbol.minsym != NULL)
1402 write_exp_msymbol (msymbol);
1403 else if (!have_full_symbols () && !have_partial_symbols ())
1404 error (_("No symbol table is loaded. Use the \"file\" command"));
1406 error (_("No symbol \"%s\" in current context."), tmp);
1412 /* The following two routines, copy_exp and insert_exp, aren't specific to
1413 Java, so they could go in parse.c, but their only purpose is to support
1414 the parsing kludges we use in this file, so maybe it's best to isolate
1417 /* Copy the expression whose last element is at index ENDPOS - 1 in EXPR
1418 into a freshly malloc'ed struct expression. Its language_defn is set
1420 static struct expression *
1421 copy_exp (struct expression *expr, int endpos)
1423 int len = length_of_subexp (expr, endpos);
1424 struct expression *new
1425 = (struct expression *) malloc (sizeof (*new) + EXP_ELEM_TO_BYTES (len));
1427 memcpy (new->elts, expr->elts + endpos - len, EXP_ELEM_TO_BYTES (len));
1428 new->language_defn = 0;
1433 /* Insert the expression NEW into the current expression (expout) at POS. */
1435 insert_exp (int pos, struct expression *new)
1437 int newlen = new->nelts;
1439 /* Grow expout if necessary. In this function's only use at present,
1440 this should never be necessary. */
1441 if (expout_ptr + newlen > expout_size)
1443 expout_size = max (expout_size * 2, expout_ptr + newlen + 10);
1444 expout = (struct expression *)
1445 realloc ((char *) expout, (sizeof (struct expression)
1446 + EXP_ELEM_TO_BYTES (expout_size)));
1452 for (i = expout_ptr - 1; i >= pos; i--)
1453 expout->elts[i + newlen] = expout->elts[i];
1456 memcpy (expout->elts + pos, new->elts, EXP_ELEM_TO_BYTES (newlen));
1457 expout_ptr += newlen;