1 /* YACC parser for Java expressions, for GDB.
2 Copyright (C) 1997, 1998, 1999, 2000, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* Parse a Java expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result. Well, almost always; see ArrayAccess.
29 Note that malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator. */
40 #include "gdb_string.h"
42 #include "expression.h"
44 #include "parser-defs.h"
47 #include "bfd.h" /* Required by objfiles.h. */
48 #include "symfile.h" /* Required by objfiles.h. */
49 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
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
104 #define YYDEBUG 1 /* Default to yydebug support */
107 #define YYFPRINTF parser_fprintf
111 static int yylex (void);
113 void yyerror (char *);
115 static struct type *java_type_from_name (struct stoken);
116 static void push_expression_name (struct stoken);
117 static void push_fieldnames (struct stoken);
119 static struct expression *copy_exp (struct expression *, int);
120 static void insert_exp (int, struct expression *);
124 /* Although the yacc "value" of an expression is not used,
125 since the result is stored in the structure being created,
126 other node types do have values. */
143 struct symtoken ssym;
145 enum exp_opcode opcode;
146 struct internalvar *ivar;
151 /* YYSTYPE gets defined by %union */
152 static int parse_number (char *, int, int, YYSTYPE *);
155 %type <lval> rcurly Dims Dims_opt
156 %type <tval> ClassOrInterfaceType ClassType /* ReferenceType Type ArrayType */
157 %type <tval> IntegralType FloatingPointType NumericType PrimitiveType ArrayType PrimitiveOrArrayType
159 %token <typed_val_int> INTEGER_LITERAL
160 %token <typed_val_float> FLOATING_POINT_LITERAL
162 %token <sval> IDENTIFIER
163 %token <sval> STRING_LITERAL
164 %token <lval> BOOLEAN_LITERAL
165 %token <tsym> TYPENAME
166 %type <sval> Name SimpleName QualifiedName ForcedName
168 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
169 but which would parse as a valid number in the current input radix.
170 E.g. "c" when input_radix==16. Depending on the parse, it will be
171 turned into a name or into a number. */
173 %token <sval> NAME_OR_INT
177 /* Special type cases, put in to allow the parser to distinguish different
179 %token LONG SHORT BYTE INT CHAR BOOLEAN DOUBLE FLOAT
183 %token <opcode> ASSIGN_MODIFY
188 %right '=' ASSIGN_MODIFY
196 %left '<' '>' LEQ GEQ
200 %right INCREMENT DECREMENT
210 type_exp: PrimitiveOrArrayType
212 write_exp_elt_opcode(OP_TYPE);
213 write_exp_elt_type($1);
214 write_exp_elt_opcode(OP_TYPE);
218 PrimitiveOrArrayType:
226 write_exp_elt_opcode (OP_STRING);
227 write_exp_string ($1);
228 write_exp_elt_opcode (OP_STRING);
234 { write_exp_elt_opcode (OP_LONG);
235 write_exp_elt_type ($1.type);
236 write_exp_elt_longcst ((LONGEST)($1.val));
237 write_exp_elt_opcode (OP_LONG); }
240 parse_number ($1.ptr, $1.length, 0, &val);
241 write_exp_elt_opcode (OP_LONG);
242 write_exp_elt_type (val.typed_val_int.type);
243 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
244 write_exp_elt_opcode (OP_LONG);
246 | FLOATING_POINT_LITERAL
247 { write_exp_elt_opcode (OP_DOUBLE);
248 write_exp_elt_type ($1.type);
249 write_exp_elt_dblcst ($1.dval);
250 write_exp_elt_opcode (OP_DOUBLE); }
252 { write_exp_elt_opcode (OP_LONG);
253 write_exp_elt_type (parse_java_type->builtin_boolean);
254 write_exp_elt_longcst ((LONGEST)$1);
255 write_exp_elt_opcode (OP_LONG); }
269 { $$ = parse_java_type->builtin_boolean; }
279 { $$ = parse_java_type->builtin_byte; }
281 { $$ = parse_java_type->builtin_short; }
283 { $$ = parse_java_type->builtin_int; }
285 { $$ = parse_java_type->builtin_long; }
287 { $$ = parse_java_type->builtin_char; }
292 { $$ = parse_java_type->builtin_float; }
294 { $$ = parse_java_type->builtin_double; }
304 ClassOrInterfaceType:
306 { $$ = java_type_from_name ($1); }
315 { $$ = java_array_type ($1, $2); }
317 { $$ = java_array_type (java_type_from_name ($1), $2); }
337 { $$.length = $1.length + $3.length + 1;
338 if ($1.ptr + $1.length + 1 == $3.ptr
339 && $1.ptr[$1.length] == '.')
340 $$.ptr = $1.ptr; /* Optimization. */
343 $$.ptr = (char *) malloc ($$.length + 1);
344 make_cleanup (free, $$.ptr);
345 sprintf ($$.ptr, "%.*s.%.*s",
346 $1.length, $1.ptr, $3.length, $3.ptr);
352 { write_exp_elt_opcode(OP_TYPE);
353 write_exp_elt_type($1);
354 write_exp_elt_opcode(OP_TYPE);}
358 /* Expressions, including the comma operator. */
360 | exp1 ',' Expression
361 { write_exp_elt_opcode (BINOP_COMMA); }
366 | ArrayCreationExpression
372 | ClassInstanceCreationExpression
376 | lcurly ArgumentList rcurly
377 { write_exp_elt_opcode (OP_ARRAY);
378 write_exp_elt_longcst ((LONGEST) 0);
379 write_exp_elt_longcst ((LONGEST) $3);
380 write_exp_elt_opcode (OP_ARRAY); }
385 { start_arglist (); }
390 { $$ = end_arglist () - 1; }
393 ClassInstanceCreationExpression:
394 NEW ClassType '(' ArgumentList_opt ')'
395 { internal_error (__FILE__, __LINE__,
396 _("FIXME - ClassInstanceCreationExpression")); }
402 | ArgumentList ',' Expression
412 ArrayCreationExpression:
413 NEW PrimitiveType DimExprs Dims_opt
414 { internal_error (__FILE__, __LINE__,
415 _("FIXME - ArrayCreationExpression")); }
416 | NEW ClassOrInterfaceType DimExprs Dims_opt
417 { internal_error (__FILE__, __LINE__,
418 _("FIXME - ArrayCreationExpression")); }
444 Primary '.' SimpleName
445 { push_fieldnames ($3); }
446 | VARIABLE '.' SimpleName
447 { push_fieldnames ($3); }
448 /*| SUPER '.' SimpleName { FIXME } */
453 { push_expression_name ($1); }
460 { write_exp_elt_opcode (OP_FUNCALL);
461 write_exp_elt_longcst ((LONGEST) end_arglist ());
462 write_exp_elt_opcode (OP_FUNCALL); }
463 | Primary '.' SimpleName '(' ArgumentList_opt ')'
464 { error (_("Form of method invocation not implemented")); }
465 | SUPER '.' SimpleName '(' ArgumentList_opt ')'
466 { error (_("Form of method invocation not implemented")); }
470 Name '[' Expression ']'
472 /* Emit code for the Name now, then exchange it in the
473 expout array with the Expression's code. We could
474 introduce a OP_SWAP code or a reversed version of
475 BINOP_SUBSCRIPT, but that makes the rest of GDB pay
476 for our parsing kludges. */
477 struct expression *name_expr;
479 push_expression_name ($1);
480 name_expr = copy_exp (expout, expout_ptr);
481 expout_ptr -= name_expr->nelts;
482 insert_exp (expout_ptr-length_of_subexp (expout, expout_ptr),
485 write_exp_elt_opcode (BINOP_SUBSCRIPT);
487 | VARIABLE '[' Expression ']'
488 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
489 | PrimaryNoNewArray '[' Expression ']'
490 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
496 { push_expression_name ($1); }
498 /* Already written by write_dollar_variable. */
499 | PostIncrementExpression
500 | PostDecrementExpression
503 PostIncrementExpression:
504 PostfixExpression INCREMENT
505 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
508 PostDecrementExpression:
509 PostfixExpression DECREMENT
510 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
514 PreIncrementExpression
515 | PreDecrementExpression
516 | '+' UnaryExpression
517 | '-' UnaryExpression
518 { write_exp_elt_opcode (UNOP_NEG); }
519 | '*' UnaryExpression
520 { write_exp_elt_opcode (UNOP_IND); } /*FIXME not in Java */
521 | UnaryExpressionNotPlusMinus
524 PreIncrementExpression:
525 INCREMENT UnaryExpression
526 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
529 PreDecrementExpression:
530 DECREMENT UnaryExpression
531 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
534 UnaryExpressionNotPlusMinus:
536 | '~' UnaryExpression
537 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
538 | '!' UnaryExpression
539 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
544 '(' PrimitiveType Dims_opt ')' UnaryExpression
545 { write_exp_elt_opcode (UNOP_CAST);
546 write_exp_elt_type (java_array_type ($2, $3));
547 write_exp_elt_opcode (UNOP_CAST); }
548 | '(' Expression ')' UnaryExpressionNotPlusMinus
550 int last_exp_size = length_of_subexp(expout, expout_ptr);
553 int base = expout_ptr - last_exp_size - 3;
554 if (base < 0 || expout->elts[base+2].opcode != OP_TYPE)
555 error (_("Invalid cast expression"));
556 type = expout->elts[base+1].type;
557 /* Remove the 'Expression' and slide the
558 UnaryExpressionNotPlusMinus down to replace it. */
559 for (i = 0; i < last_exp_size; i++)
560 expout->elts[base + i] = expout->elts[base + i + 3];
562 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
563 type = lookup_pointer_type (type);
564 write_exp_elt_opcode (UNOP_CAST);
565 write_exp_elt_type (type);
566 write_exp_elt_opcode (UNOP_CAST);
568 | '(' Name Dims ')' UnaryExpressionNotPlusMinus
569 { write_exp_elt_opcode (UNOP_CAST);
570 write_exp_elt_type (java_array_type (java_type_from_name ($2), $3));
571 write_exp_elt_opcode (UNOP_CAST); }
575 MultiplicativeExpression:
577 | MultiplicativeExpression '*' UnaryExpression
578 { write_exp_elt_opcode (BINOP_MUL); }
579 | MultiplicativeExpression '/' UnaryExpression
580 { write_exp_elt_opcode (BINOP_DIV); }
581 | MultiplicativeExpression '%' UnaryExpression
582 { write_exp_elt_opcode (BINOP_REM); }
586 MultiplicativeExpression
587 | AdditiveExpression '+' MultiplicativeExpression
588 { write_exp_elt_opcode (BINOP_ADD); }
589 | AdditiveExpression '-' MultiplicativeExpression
590 { write_exp_elt_opcode (BINOP_SUB); }
595 | ShiftExpression LSH AdditiveExpression
596 { write_exp_elt_opcode (BINOP_LSH); }
597 | ShiftExpression RSH AdditiveExpression
598 { write_exp_elt_opcode (BINOP_RSH); }
599 /* | ShiftExpression >>> AdditiveExpression { FIXME } */
602 RelationalExpression:
604 | RelationalExpression '<' ShiftExpression
605 { write_exp_elt_opcode (BINOP_LESS); }
606 | RelationalExpression '>' ShiftExpression
607 { write_exp_elt_opcode (BINOP_GTR); }
608 | RelationalExpression LEQ ShiftExpression
609 { write_exp_elt_opcode (BINOP_LEQ); }
610 | RelationalExpression GEQ ShiftExpression
611 { write_exp_elt_opcode (BINOP_GEQ); }
612 /* | RelationalExpresion INSTANCEOF ReferenceType { FIXME } */
617 | EqualityExpression EQUAL RelationalExpression
618 { write_exp_elt_opcode (BINOP_EQUAL); }
619 | EqualityExpression NOTEQUAL RelationalExpression
620 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
625 | AndExpression '&' EqualityExpression
626 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
629 ExclusiveOrExpression:
631 | ExclusiveOrExpression '^' AndExpression
632 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
634 InclusiveOrExpression:
635 ExclusiveOrExpression
636 | InclusiveOrExpression '|' ExclusiveOrExpression
637 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
640 ConditionalAndExpression:
641 InclusiveOrExpression
642 | ConditionalAndExpression ANDAND InclusiveOrExpression
643 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
646 ConditionalOrExpression:
647 ConditionalAndExpression
648 | ConditionalOrExpression OROR ConditionalAndExpression
649 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
652 ConditionalExpression:
653 ConditionalOrExpression
654 | ConditionalOrExpression '?' Expression ':' ConditionalExpression
655 { write_exp_elt_opcode (TERNOP_COND); }
658 AssignmentExpression:
659 ConditionalExpression
664 LeftHandSide '=' ConditionalExpression
665 { write_exp_elt_opcode (BINOP_ASSIGN); }
666 | LeftHandSide ASSIGN_MODIFY ConditionalExpression
667 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
668 write_exp_elt_opcode ($2);
669 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
674 { push_expression_name ($1); }
676 /* Already written by write_dollar_variable. */
687 /* Take care of parsing a number (anything that starts with a digit).
688 Set yylval and return the token type; update lexptr.
689 LEN is the number of characters in it. */
691 /*** Needs some error checking for the float case ***/
694 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
697 ULONGEST limit, limit_div_base;
700 int base = input_radix;
709 if (! parse_float (p, len, &putithere->typed_val_float.dval, &suffix))
712 suffix_len = p + len - suffix;
715 putithere->typed_val_float.type = parse_type->builtin_double;
716 else if (suffix_len == 1)
718 /* See if it has `f' or `d' suffix (float or double). */
719 if (tolower (*suffix) == 'f')
720 putithere->typed_val_float.type =
721 parse_type->builtin_float;
722 else if (tolower (*suffix) == 'd')
723 putithere->typed_val_float.type =
724 parse_type->builtin_double;
731 return FLOATING_POINT_LITERAL;
734 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
766 /* A paranoid calculation of (1<<64)-1. */
767 limit = (ULONGEST)0xffffffff;
768 limit = ((limit << 16) << 16) | limit;
769 if (c == 'l' || c == 'L')
771 type = parse_java_type->builtin_long;
776 type = parse_java_type->builtin_int;
778 limit_div_base = limit / (ULONGEST) base;
783 if (c >= '0' && c <= '9')
785 else if (c >= 'A' && c <= 'Z')
787 else if (c >= 'a' && c <= 'z')
790 return ERROR; /* Char not a digit */
793 if (n > limit_div_base
794 || (n *= base) > limit - c)
795 error (_("Numeric constant too large"));
799 /* If the type is bigger than a 32-bit signed integer can be, implicitly
800 promote to long. Java does not do this, so mark it as
801 parse_type->builtin_uint64 rather than parse_java_type->builtin_long.
802 0x80000000 will become -0x80000000 instead of 0x80000000L, because we
803 don't know the sign at this point. */
804 if (type == parse_java_type->builtin_int && n > (ULONGEST)0x80000000)
805 type = parse_type->builtin_uint64;
807 putithere->typed_val_int.val = n;
808 putithere->typed_val_int.type = type;
810 return INTEGER_LITERAL;
817 enum exp_opcode opcode;
820 static const struct token tokentab3[] =
822 {">>=", ASSIGN_MODIFY, BINOP_RSH},
823 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
826 static const struct token tokentab2[] =
828 {"+=", ASSIGN_MODIFY, BINOP_ADD},
829 {"-=", ASSIGN_MODIFY, BINOP_SUB},
830 {"*=", ASSIGN_MODIFY, BINOP_MUL},
831 {"/=", ASSIGN_MODIFY, BINOP_DIV},
832 {"%=", ASSIGN_MODIFY, BINOP_REM},
833 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
834 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
835 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
836 {"++", INCREMENT, BINOP_END},
837 {"--", DECREMENT, BINOP_END},
838 {"&&", ANDAND, BINOP_END},
839 {"||", OROR, BINOP_END},
840 {"<<", LSH, BINOP_END},
841 {">>", RSH, BINOP_END},
842 {"==", EQUAL, BINOP_END},
843 {"!=", NOTEQUAL, BINOP_END},
844 {"<=", LEQ, BINOP_END},
845 {">=", GEQ, BINOP_END}
848 /* Read one token, getting characters through lexptr. */
859 static char *tempbuf;
860 static int tempbufsize;
864 prev_lexptr = lexptr;
867 /* See if it is a special token of length 3. */
868 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
869 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
872 yylval.opcode = tokentab3[i].opcode;
873 return tokentab3[i].token;
876 /* See if it is a special token of length 2. */
877 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
878 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
881 yylval.opcode = tokentab2[i].opcode;
882 return tokentab2[i].token;
885 switch (c = *tokstart)
897 /* We either have a character constant ('0' or '\177' for example)
898 or we have a quoted symbol reference ('foo(int,int)' in C++
903 c = parse_escape (parse_gdbarch, &lexptr);
905 error (_("Empty character constant"));
907 yylval.typed_val_int.val = c;
908 yylval.typed_val_int.type = parse_java_type->builtin_char;
913 namelen = skip_quoted (tokstart) - tokstart;
916 lexptr = tokstart + namelen;
917 if (lexptr[-1] != '\'')
918 error (_("Unmatched single quote"));
923 error (_("Invalid character constant"));
925 return INTEGER_LITERAL;
933 if (paren_depth == 0)
940 if (comma_terminates && paren_depth == 0)
946 /* Might be a floating point number. */
947 if (lexptr[1] < '0' || lexptr[1] > '9')
948 goto symbol; /* Nope, must be a symbol. */
949 /* FALL THRU into number case. */
963 int got_dot = 0, got_e = 0, toktype;
965 int hex = input_radix > 10;
967 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
972 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
980 /* This test includes !hex because 'e' is a valid hex digit
981 and thus does not indicate a floating point number when
983 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
985 /* This test does not include !hex, because a '.' always indicates
986 a decimal floating point number regardless of the radix. */
987 else if (!got_dot && *p == '.')
989 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
990 && (*p == '-' || *p == '+'))
991 /* This is the sign of the exponent, not the end of the
994 /* We will take any letters or digits. parse_number will
995 complain if past the radix, or if L or U are not final. */
996 else if ((*p < '0' || *p > '9')
997 && ((*p < 'a' || *p > 'z')
998 && (*p < 'A' || *p > 'Z')))
1001 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1002 if (toktype == ERROR)
1004 char *err_copy = (char *) alloca (p - tokstart + 1);
1006 memcpy (err_copy, tokstart, p - tokstart);
1007 err_copy[p - tokstart] = 0;
1008 error (_("Invalid number \"%s\""), err_copy);
1039 /* Build the gdb internal form of the input string in tempbuf,
1040 translating any standard C escape forms seen. Note that the
1041 buffer is null byte terminated *only* for the convenience of
1042 debugging gdb itself and printing the buffer contents when
1043 the buffer contains no embedded nulls. Gdb does not depend
1044 upon the buffer being null byte terminated, it uses the length
1045 string instead. This allows gdb to handle C strings (as well
1046 as strings in other languages) with embedded null bytes */
1048 tokptr = ++tokstart;
1052 /* Grow the static temp buffer if necessary, including allocating
1053 the first one on demand. */
1054 if (tempbufindex + 1 >= tempbufsize)
1056 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1062 /* Do nothing, loop will terminate. */
1066 c = parse_escape (parse_gdbarch, &tokptr);
1071 tempbuf[tempbufindex++] = c;
1074 tempbuf[tempbufindex++] = *tokptr++;
1077 } while ((*tokptr != '"') && (*tokptr != '\0'));
1078 if (*tokptr++ != '"')
1080 error (_("Unterminated string in expression"));
1082 tempbuf[tempbufindex] = '\0'; /* See note above */
1083 yylval.sval.ptr = tempbuf;
1084 yylval.sval.length = tempbufindex;
1086 return (STRING_LITERAL);
1089 if (!(c == '_' || c == '$'
1090 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1091 /* We must have come across a bad character (e.g. ';'). */
1092 error (_("Invalid character '%c' in expression"), c);
1094 /* It's a name. See how long it is. */
1096 for (c = tokstart[namelen];
1099 || (c >= '0' && c <= '9')
1100 || (c >= 'a' && c <= 'z')
1101 || (c >= 'A' && c <= 'Z')
1108 while (tokstart[++i] && tokstart[i] != '>');
1109 if (tokstart[i] == '>')
1112 c = tokstart[++namelen];
1115 /* The token "if" terminates the expression and is NOT
1116 removed from the input stream. */
1117 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1126 /* Catch specific keywords. Should be done with a data structure. */
1130 if (strncmp (tokstart, "boolean", 7) == 0)
1134 if (strncmp (tokstart, "double", 6) == 0)
1138 if (strncmp (tokstart, "short", 5) == 0)
1140 if (strncmp (tokstart, "false", 5) == 0)
1143 return BOOLEAN_LITERAL;
1145 if (strncmp (tokstart, "super", 5) == 0)
1147 if (strncmp (tokstart, "float", 5) == 0)
1151 if (strncmp (tokstart, "long", 4) == 0)
1153 if (strncmp (tokstart, "byte", 4) == 0)
1155 if (strncmp (tokstart, "char", 4) == 0)
1157 if (strncmp (tokstart, "true", 4) == 0)
1160 return BOOLEAN_LITERAL;
1164 if (strncmp (tokstart, "int", 3) == 0)
1166 if (strncmp (tokstart, "new", 3) == 0)
1173 yylval.sval.ptr = tokstart;
1174 yylval.sval.length = namelen;
1176 if (*tokstart == '$')
1178 write_dollar_variable (yylval.sval);
1182 /* Input names that aren't symbols but ARE valid hex numbers,
1183 when the input radix permits them, can be names or numbers
1184 depending on the parse. Note we support radixes > 16 here. */
1185 if (((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1186 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1188 YYSTYPE newlval; /* Its value is ignored. */
1189 int hextype = parse_number (tokstart, namelen, 0, &newlval);
1190 if (hextype == INTEGER_LITERAL)
1200 lexptr = prev_lexptr;
1203 error (_("%s: near `%s'"), msg, lexptr);
1205 error (_("error in expression, near `%s'"), lexptr);
1208 static struct type *
1209 java_type_from_name (struct stoken name)
1211 char *tmp = copy_name (name);
1212 struct type *typ = java_lookup_class (tmp);
1213 if (typ == NULL || TYPE_CODE (typ) != TYPE_CODE_STRUCT)
1214 error (_("No class named `%s'"), tmp);
1218 /* If NAME is a valid variable name in this scope, push it and return 1.
1219 Otherwise, return 0. */
1222 push_variable (struct stoken name)
1224 char *tmp = copy_name (name);
1225 int is_a_field_of_this = 0;
1227 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN,
1228 &is_a_field_of_this);
1229 if (sym && SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1231 if (symbol_read_needs_frame (sym))
1233 if (innermost_block == 0 ||
1234 contained_in (block_found, innermost_block))
1235 innermost_block = block_found;
1238 write_exp_elt_opcode (OP_VAR_VALUE);
1239 /* We want to use the selected frame, not another more inner frame
1240 which happens to be in the same block. */
1241 write_exp_elt_block (NULL);
1242 write_exp_elt_sym (sym);
1243 write_exp_elt_opcode (OP_VAR_VALUE);
1246 if (is_a_field_of_this)
1248 /* it hangs off of `this'. Must not inadvertently convert from a
1249 method call to data ref. */
1250 if (innermost_block == 0 ||
1251 contained_in (block_found, innermost_block))
1252 innermost_block = block_found;
1253 write_exp_elt_opcode (OP_THIS);
1254 write_exp_elt_opcode (OP_THIS);
1255 write_exp_elt_opcode (STRUCTOP_PTR);
1256 write_exp_string (name);
1257 write_exp_elt_opcode (STRUCTOP_PTR);
1263 /* Assuming a reference expression has been pushed, emit the
1264 STRUCTOP_PTR ops to access the field named NAME. If NAME is a
1265 qualified name (has '.'), generate a field access for each part. */
1268 push_fieldnames (struct stoken name)
1271 struct stoken token;
1272 token.ptr = name.ptr;
1275 if (i == name.length || name.ptr[i] == '.')
1277 /* token.ptr is start of current field name. */
1278 token.length = &name.ptr[i] - token.ptr;
1279 write_exp_elt_opcode (STRUCTOP_PTR);
1280 write_exp_string (token);
1281 write_exp_elt_opcode (STRUCTOP_PTR);
1282 token.ptr += token.length + 1;
1284 if (i >= name.length)
1289 /* Helper routine for push_expression_name.
1290 Handle a qualified name, where DOT_INDEX is the index of the first '.' */
1293 push_qualified_expression_name (struct stoken name, int dot_index)
1295 struct stoken token;
1299 token.ptr = name.ptr;
1300 token.length = dot_index;
1302 if (push_variable (token))
1304 token.ptr = name.ptr + dot_index + 1;
1305 token.length = name.length - dot_index - 1;
1306 push_fieldnames (token);
1310 token.ptr = name.ptr;
1313 token.length = dot_index;
1314 tmp = copy_name (token);
1315 typ = java_lookup_class (tmp);
1318 if (dot_index == name.length)
1320 write_exp_elt_opcode(OP_TYPE);
1321 write_exp_elt_type(typ);
1322 write_exp_elt_opcode(OP_TYPE);
1325 dot_index++; /* Skip '.' */
1326 name.ptr += dot_index;
1327 name.length -= dot_index;
1329 while (dot_index < name.length && name.ptr[dot_index] != '.')
1331 token.ptr = name.ptr;
1332 token.length = dot_index;
1333 write_exp_elt_opcode (OP_SCOPE);
1334 write_exp_elt_type (typ);
1335 write_exp_string (token);
1336 write_exp_elt_opcode (OP_SCOPE);
1337 if (dot_index < name.length)
1340 name.ptr += dot_index;
1341 name.length -= dot_index;
1342 push_fieldnames (name);
1346 else if (dot_index >= name.length)
1348 dot_index++; /* Skip '.' */
1349 while (dot_index < name.length && name.ptr[dot_index] != '.')
1352 error (_("unknown type `%.*s'"), name.length, name.ptr);
1355 /* Handle Name in an expression (or LHS).
1356 Handle VAR, TYPE, TYPE.FIELD1....FIELDN and VAR.FIELD1....FIELDN. */
1359 push_expression_name (struct stoken name)
1365 for (i = 0; i < name.length; i++)
1367 if (name.ptr[i] == '.')
1369 /* It's a Qualified Expression Name. */
1370 push_qualified_expression_name (name, i);
1375 /* It's a Simple Expression Name. */
1377 if (push_variable (name))
1379 tmp = copy_name (name);
1380 typ = java_lookup_class (tmp);
1383 write_exp_elt_opcode(OP_TYPE);
1384 write_exp_elt_type(typ);
1385 write_exp_elt_opcode(OP_TYPE);
1389 struct minimal_symbol *msymbol;
1391 msymbol = lookup_minimal_symbol (tmp, NULL, NULL);
1392 if (msymbol != NULL)
1393 write_exp_msymbol (msymbol);
1394 else if (!have_full_symbols () && !have_partial_symbols ())
1395 error (_("No symbol table is loaded. Use the \"file\" command"));
1397 error (_("No symbol \"%s\" in current context"), tmp);
1403 /* The following two routines, copy_exp and insert_exp, aren't specific to
1404 Java, so they could go in parse.c, but their only purpose is to support
1405 the parsing kludges we use in this file, so maybe it's best to isolate
1408 /* Copy the expression whose last element is at index ENDPOS - 1 in EXPR
1409 into a freshly malloc'ed struct expression. Its language_defn is set
1411 static struct expression *
1412 copy_exp (struct expression *expr, int endpos)
1414 int len = length_of_subexp (expr, endpos);
1415 struct expression *new
1416 = (struct expression *) malloc (sizeof (*new) + EXP_ELEM_TO_BYTES (len));
1418 memcpy (new->elts, expr->elts + endpos - len, EXP_ELEM_TO_BYTES (len));
1419 new->language_defn = 0;
1424 /* Insert the expression NEW into the current expression (expout) at POS. */
1426 insert_exp (int pos, struct expression *new)
1428 int newlen = new->nelts;
1430 /* Grow expout if necessary. In this function's only use at present,
1431 this should never be necessary. */
1432 if (expout_ptr + newlen > expout_size)
1434 expout_size = max (expout_size * 2, expout_ptr + newlen + 10);
1435 expout = (struct expression *)
1436 realloc ((char *) expout, (sizeof (struct expression)
1437 + EXP_ELEM_TO_BYTES (expout_size)));
1443 for (i = expout_ptr - 1; i >= pos; i--)
1444 expout->elts[i + newlen] = expout->elts[i];
1447 memcpy (expout->elts + pos, new->elts, EXP_ELEM_TO_BYTES (newlen));
1448 expout_ptr += newlen;