1 /* YACC parser for Java expressions, for GDB.
2 Copyright (C) 1997, 1998, 1999, 2000, 2006, 2007, 2008, 2009
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 exp_size = expout_ptr;
551 int last_exp_size = length_of_subexp(expout, expout_ptr);
554 int base = expout_ptr - last_exp_size - 3;
555 if (base < 0 || expout->elts[base+2].opcode != OP_TYPE)
556 error (_("Invalid cast expression"));
557 type = expout->elts[base+1].type;
558 /* Remove the 'Expression' and slide the
559 UnaryExpressionNotPlusMinus down to replace it. */
560 for (i = 0; i < last_exp_size; i++)
561 expout->elts[base + i] = expout->elts[base + i + 3];
563 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
564 type = lookup_pointer_type (type);
565 write_exp_elt_opcode (UNOP_CAST);
566 write_exp_elt_type (type);
567 write_exp_elt_opcode (UNOP_CAST);
569 | '(' Name Dims ')' UnaryExpressionNotPlusMinus
570 { write_exp_elt_opcode (UNOP_CAST);
571 write_exp_elt_type (java_array_type (java_type_from_name ($2), $3));
572 write_exp_elt_opcode (UNOP_CAST); }
576 MultiplicativeExpression:
578 | MultiplicativeExpression '*' UnaryExpression
579 { write_exp_elt_opcode (BINOP_MUL); }
580 | MultiplicativeExpression '/' UnaryExpression
581 { write_exp_elt_opcode (BINOP_DIV); }
582 | MultiplicativeExpression '%' UnaryExpression
583 { write_exp_elt_opcode (BINOP_REM); }
587 MultiplicativeExpression
588 | AdditiveExpression '+' MultiplicativeExpression
589 { write_exp_elt_opcode (BINOP_ADD); }
590 | AdditiveExpression '-' MultiplicativeExpression
591 { write_exp_elt_opcode (BINOP_SUB); }
596 | ShiftExpression LSH AdditiveExpression
597 { write_exp_elt_opcode (BINOP_LSH); }
598 | ShiftExpression RSH AdditiveExpression
599 { write_exp_elt_opcode (BINOP_RSH); }
600 /* | ShiftExpression >>> AdditiveExpression { FIXME } */
603 RelationalExpression:
605 | RelationalExpression '<' ShiftExpression
606 { write_exp_elt_opcode (BINOP_LESS); }
607 | RelationalExpression '>' ShiftExpression
608 { write_exp_elt_opcode (BINOP_GTR); }
609 | RelationalExpression LEQ ShiftExpression
610 { write_exp_elt_opcode (BINOP_LEQ); }
611 | RelationalExpression GEQ ShiftExpression
612 { write_exp_elt_opcode (BINOP_GEQ); }
613 /* | RelationalExpresion INSTANCEOF ReferenceType { FIXME } */
618 | EqualityExpression EQUAL RelationalExpression
619 { write_exp_elt_opcode (BINOP_EQUAL); }
620 | EqualityExpression NOTEQUAL RelationalExpression
621 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
626 | AndExpression '&' EqualityExpression
627 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
630 ExclusiveOrExpression:
632 | ExclusiveOrExpression '^' AndExpression
633 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
635 InclusiveOrExpression:
636 ExclusiveOrExpression
637 | InclusiveOrExpression '|' ExclusiveOrExpression
638 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
641 ConditionalAndExpression:
642 InclusiveOrExpression
643 | ConditionalAndExpression ANDAND InclusiveOrExpression
644 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
647 ConditionalOrExpression:
648 ConditionalAndExpression
649 | ConditionalOrExpression OROR ConditionalAndExpression
650 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
653 ConditionalExpression:
654 ConditionalOrExpression
655 | ConditionalOrExpression '?' Expression ':' ConditionalExpression
656 { write_exp_elt_opcode (TERNOP_COND); }
659 AssignmentExpression:
660 ConditionalExpression
665 LeftHandSide '=' ConditionalExpression
666 { write_exp_elt_opcode (BINOP_ASSIGN); }
667 | LeftHandSide ASSIGN_MODIFY ConditionalExpression
668 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
669 write_exp_elt_opcode ($2);
670 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
675 { push_expression_name ($1); }
677 /* Already written by write_dollar_variable. */
688 /* Take care of parsing a number (anything that starts with a digit).
689 Set yylval and return the token type; update lexptr.
690 LEN is the number of characters in it. */
692 /*** Needs some error checking for the float case ***/
695 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
698 ULONGEST limit, limit_div_base;
701 int base = input_radix;
707 /* It's a float since it contains a point or an exponent. */
709 int num = 0; /* number of tokens scanned by scanf */
710 char saved_char = p[len];
712 p[len] = 0; /* null-terminate the token */
713 num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%c",
714 &putithere->typed_val_float.dval, &c);
715 p[len] = saved_char; /* restore the input stream */
716 if (num != 1) /* check scanf found ONLY a float ... */
718 /* See if it has `f' or `d' suffix (float or double). */
720 c = tolower (p[len - 1]);
722 if (c == 'f' || c == 'F')
723 putithere->typed_val_float.type = parse_type->builtin_float;
724 else if (isdigit (c) || c == '.' || c == 'd' || c == 'D')
725 putithere->typed_val_float.type = parse_type->builtin_double;
729 return FLOATING_POINT_LITERAL;
732 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
764 /* A paranoid calculation of (1<<64)-1. */
765 limit = (ULONGEST)0xffffffff;
766 limit = ((limit << 16) << 16) | limit;
767 if (c == 'l' || c == 'L')
769 type = parse_java_type->builtin_long;
774 type = parse_java_type->builtin_int;
776 limit_div_base = limit / (ULONGEST) base;
781 if (c >= '0' && c <= '9')
783 else if (c >= 'A' && c <= 'Z')
785 else if (c >= 'a' && c <= 'z')
788 return ERROR; /* Char not a digit */
791 if (n > limit_div_base
792 || (n *= base) > limit - c)
793 error (_("Numeric constant too large"));
797 /* If the type is bigger than a 32-bit signed integer can be, implicitly
798 promote to long. Java does not do this, so mark it as
799 parse_type->builtin_uint64 rather than parse_java_type->builtin_long.
800 0x80000000 will become -0x80000000 instead of 0x80000000L, because we
801 don't know the sign at this point. */
802 if (type == parse_java_type->builtin_int && n > (ULONGEST)0x80000000)
803 type = parse_type->builtin_uint64;
805 putithere->typed_val_int.val = n;
806 putithere->typed_val_int.type = type;
808 return INTEGER_LITERAL;
815 enum exp_opcode opcode;
818 static const struct token tokentab3[] =
820 {">>=", ASSIGN_MODIFY, BINOP_RSH},
821 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
824 static const struct token tokentab2[] =
826 {"+=", ASSIGN_MODIFY, BINOP_ADD},
827 {"-=", ASSIGN_MODIFY, BINOP_SUB},
828 {"*=", ASSIGN_MODIFY, BINOP_MUL},
829 {"/=", ASSIGN_MODIFY, BINOP_DIV},
830 {"%=", ASSIGN_MODIFY, BINOP_REM},
831 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
832 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
833 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
834 {"++", INCREMENT, BINOP_END},
835 {"--", DECREMENT, BINOP_END},
836 {"&&", ANDAND, BINOP_END},
837 {"||", OROR, BINOP_END},
838 {"<<", LSH, BINOP_END},
839 {">>", RSH, BINOP_END},
840 {"==", EQUAL, BINOP_END},
841 {"!=", NOTEQUAL, BINOP_END},
842 {"<=", LEQ, BINOP_END},
843 {">=", GEQ, BINOP_END}
846 /* Read one token, getting characters through lexptr. */
857 static char *tempbuf;
858 static int tempbufsize;
862 prev_lexptr = lexptr;
865 /* See if it is a special token of length 3. */
866 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
867 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
870 yylval.opcode = tokentab3[i].opcode;
871 return tokentab3[i].token;
874 /* See if it is a special token of length 2. */
875 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
876 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
879 yylval.opcode = tokentab2[i].opcode;
880 return tokentab2[i].token;
883 switch (c = *tokstart)
895 /* We either have a character constant ('0' or '\177' for example)
896 or we have a quoted symbol reference ('foo(int,int)' in C++
901 c = parse_escape (&lexptr);
903 error (_("Empty character constant"));
905 yylval.typed_val_int.val = c;
906 yylval.typed_val_int.type = parse_java_type->builtin_char;
911 namelen = skip_quoted (tokstart) - tokstart;
914 lexptr = tokstart + namelen;
915 if (lexptr[-1] != '\'')
916 error (_("Unmatched single quote"));
921 error (_("Invalid character constant"));
923 return INTEGER_LITERAL;
931 if (paren_depth == 0)
938 if (comma_terminates && paren_depth == 0)
944 /* Might be a floating point number. */
945 if (lexptr[1] < '0' || lexptr[1] > '9')
946 goto symbol; /* Nope, must be a symbol. */
947 /* FALL THRU into number case. */
961 int got_dot = 0, got_e = 0, toktype;
963 int hex = input_radix > 10;
965 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
970 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
978 /* This test includes !hex because 'e' is a valid hex digit
979 and thus does not indicate a floating point number when
981 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
983 /* This test does not include !hex, because a '.' always indicates
984 a decimal floating point number regardless of the radix. */
985 else if (!got_dot && *p == '.')
987 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
988 && (*p == '-' || *p == '+'))
989 /* This is the sign of the exponent, not the end of the
992 /* We will take any letters or digits. parse_number will
993 complain if past the radix, or if L or U are not final. */
994 else if ((*p < '0' || *p > '9')
995 && ((*p < 'a' || *p > 'z')
996 && (*p < 'A' || *p > 'Z')))
999 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1000 if (toktype == ERROR)
1002 char *err_copy = (char *) alloca (p - tokstart + 1);
1004 memcpy (err_copy, tokstart, p - tokstart);
1005 err_copy[p - tokstart] = 0;
1006 error (_("Invalid number \"%s\""), err_copy);
1037 /* Build the gdb internal form of the input string in tempbuf,
1038 translating any standard C escape forms seen. Note that the
1039 buffer is null byte terminated *only* for the convenience of
1040 debugging gdb itself and printing the buffer contents when
1041 the buffer contains no embedded nulls. Gdb does not depend
1042 upon the buffer being null byte terminated, it uses the length
1043 string instead. This allows gdb to handle C strings (as well
1044 as strings in other languages) with embedded null bytes */
1046 tokptr = ++tokstart;
1050 /* Grow the static temp buffer if necessary, including allocating
1051 the first one on demand. */
1052 if (tempbufindex + 1 >= tempbufsize)
1054 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1060 /* Do nothing, loop will terminate. */
1064 c = parse_escape (&tokptr);
1069 tempbuf[tempbufindex++] = c;
1072 tempbuf[tempbufindex++] = *tokptr++;
1075 } while ((*tokptr != '"') && (*tokptr != '\0'));
1076 if (*tokptr++ != '"')
1078 error (_("Unterminated string in expression"));
1080 tempbuf[tempbufindex] = '\0'; /* See note above */
1081 yylval.sval.ptr = tempbuf;
1082 yylval.sval.length = tempbufindex;
1084 return (STRING_LITERAL);
1087 if (!(c == '_' || c == '$'
1088 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1089 /* We must have come across a bad character (e.g. ';'). */
1090 error (_("Invalid character '%c' in expression"), c);
1092 /* It's a name. See how long it is. */
1094 for (c = tokstart[namelen];
1097 || (c >= '0' && c <= '9')
1098 || (c >= 'a' && c <= 'z')
1099 || (c >= 'A' && c <= 'Z')
1106 while (tokstart[++i] && tokstart[i] != '>');
1107 if (tokstart[i] == '>')
1110 c = tokstart[++namelen];
1113 /* The token "if" terminates the expression and is NOT
1114 removed from the input stream. */
1115 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1124 /* Catch specific keywords. Should be done with a data structure. */
1128 if (strncmp (tokstart, "boolean", 7) == 0)
1132 if (strncmp (tokstart, "double", 6) == 0)
1136 if (strncmp (tokstart, "short", 5) == 0)
1138 if (strncmp (tokstart, "false", 5) == 0)
1141 return BOOLEAN_LITERAL;
1143 if (strncmp (tokstart, "super", 5) == 0)
1145 if (strncmp (tokstart, "float", 5) == 0)
1149 if (strncmp (tokstart, "long", 4) == 0)
1151 if (strncmp (tokstart, "byte", 4) == 0)
1153 if (strncmp (tokstart, "char", 4) == 0)
1155 if (strncmp (tokstart, "true", 4) == 0)
1158 return BOOLEAN_LITERAL;
1162 if (strncmp (tokstart, "int", 3) == 0)
1164 if (strncmp (tokstart, "new", 3) == 0)
1171 yylval.sval.ptr = tokstart;
1172 yylval.sval.length = namelen;
1174 if (*tokstart == '$')
1176 write_dollar_variable (yylval.sval);
1180 /* Input names that aren't symbols but ARE valid hex numbers,
1181 when the input radix permits them, can be names or numbers
1182 depending on the parse. Note we support radixes > 16 here. */
1183 if (((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1184 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1186 YYSTYPE newlval; /* Its value is ignored. */
1187 int hextype = parse_number (tokstart, namelen, 0, &newlval);
1188 if (hextype == INTEGER_LITERAL)
1198 lexptr = prev_lexptr;
1201 error (_("%s: near `%s'"), msg, lexptr);
1203 error (_("error in expression, near `%s'"), lexptr);
1206 static struct type *
1207 java_type_from_name (struct stoken name)
1209 char *tmp = copy_name (name);
1210 struct type *typ = java_lookup_class (tmp);
1211 if (typ == NULL || TYPE_CODE (typ) != TYPE_CODE_STRUCT)
1212 error (_("No class named `%s'"), tmp);
1216 /* If NAME is a valid variable name in this scope, push it and return 1.
1217 Otherwise, return 0. */
1220 push_variable (struct stoken name)
1222 char *tmp = copy_name (name);
1223 int is_a_field_of_this = 0;
1225 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN,
1226 &is_a_field_of_this);
1227 if (sym && SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1229 if (symbol_read_needs_frame (sym))
1231 if (innermost_block == 0 ||
1232 contained_in (block_found, innermost_block))
1233 innermost_block = block_found;
1236 write_exp_elt_opcode (OP_VAR_VALUE);
1237 /* We want to use the selected frame, not another more inner frame
1238 which happens to be in the same block. */
1239 write_exp_elt_block (NULL);
1240 write_exp_elt_sym (sym);
1241 write_exp_elt_opcode (OP_VAR_VALUE);
1244 if (is_a_field_of_this)
1246 /* it hangs off of `this'. Must not inadvertently convert from a
1247 method call to data ref. */
1248 if (innermost_block == 0 ||
1249 contained_in (block_found, innermost_block))
1250 innermost_block = block_found;
1251 write_exp_elt_opcode (OP_THIS);
1252 write_exp_elt_opcode (OP_THIS);
1253 write_exp_elt_opcode (STRUCTOP_PTR);
1254 write_exp_string (name);
1255 write_exp_elt_opcode (STRUCTOP_PTR);
1261 /* Assuming a reference expression has been pushed, emit the
1262 STRUCTOP_PTR ops to access the field named NAME. If NAME is a
1263 qualified name (has '.'), generate a field access for each part. */
1266 push_fieldnames (struct stoken name)
1269 struct stoken token;
1270 token.ptr = name.ptr;
1273 if (i == name.length || name.ptr[i] == '.')
1275 /* token.ptr is start of current field name. */
1276 token.length = &name.ptr[i] - token.ptr;
1277 write_exp_elt_opcode (STRUCTOP_PTR);
1278 write_exp_string (token);
1279 write_exp_elt_opcode (STRUCTOP_PTR);
1280 token.ptr += token.length + 1;
1282 if (i >= name.length)
1287 /* Helper routine for push_expression_name.
1288 Handle a qualified name, where DOT_INDEX is the index of the first '.' */
1291 push_qualified_expression_name (struct stoken name, int dot_index)
1293 struct stoken token;
1297 token.ptr = name.ptr;
1298 token.length = dot_index;
1300 if (push_variable (token))
1302 token.ptr = name.ptr + dot_index + 1;
1303 token.length = name.length - dot_index - 1;
1304 push_fieldnames (token);
1308 token.ptr = name.ptr;
1311 token.length = dot_index;
1312 tmp = copy_name (token);
1313 typ = java_lookup_class (tmp);
1316 if (dot_index == name.length)
1318 write_exp_elt_opcode(OP_TYPE);
1319 write_exp_elt_type(typ);
1320 write_exp_elt_opcode(OP_TYPE);
1323 dot_index++; /* Skip '.' */
1324 name.ptr += dot_index;
1325 name.length -= dot_index;
1327 while (dot_index < name.length && name.ptr[dot_index] != '.')
1329 token.ptr = name.ptr;
1330 token.length = dot_index;
1331 write_exp_elt_opcode (OP_SCOPE);
1332 write_exp_elt_type (typ);
1333 write_exp_string (token);
1334 write_exp_elt_opcode (OP_SCOPE);
1335 if (dot_index < name.length)
1338 name.ptr += dot_index;
1339 name.length -= dot_index;
1340 push_fieldnames (name);
1344 else if (dot_index >= name.length)
1346 dot_index++; /* Skip '.' */
1347 while (dot_index < name.length && name.ptr[dot_index] != '.')
1350 error (_("unknown type `%.*s'"), name.length, name.ptr);
1353 /* Handle Name in an expression (or LHS).
1354 Handle VAR, TYPE, TYPE.FIELD1....FIELDN and VAR.FIELD1....FIELDN. */
1357 push_expression_name (struct stoken name)
1364 for (i = 0; i < name.length; i++)
1366 if (name.ptr[i] == '.')
1368 /* It's a Qualified Expression Name. */
1369 push_qualified_expression_name (name, i);
1374 /* It's a Simple Expression Name. */
1376 if (push_variable (name))
1378 tmp = copy_name (name);
1379 typ = java_lookup_class (tmp);
1382 write_exp_elt_opcode(OP_TYPE);
1383 write_exp_elt_type(typ);
1384 write_exp_elt_opcode(OP_TYPE);
1388 struct minimal_symbol *msymbol;
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"));
1396 error (_("No symbol \"%s\" in current context"), tmp);
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
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
1410 static struct expression *
1411 copy_exp (struct expression *expr, int endpos)
1413 int len = length_of_subexp (expr, endpos);
1414 struct expression *new
1415 = (struct expression *) malloc (sizeof (*new) + EXP_ELEM_TO_BYTES (len));
1417 memcpy (new->elts, expr->elts + endpos - len, EXP_ELEM_TO_BYTES (len));
1418 new->language_defn = 0;
1423 /* Insert the expression NEW into the current expression (expout) at POS. */
1425 insert_exp (int pos, struct expression *new)
1427 int newlen = new->nelts;
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)
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)));
1442 for (i = expout_ptr - 1; i >= pos; i--)
1443 expout->elts[i + newlen] = expout->elts[i];
1446 memcpy (expout->elts + pos, new->elts, EXP_ELEM_TO_BYTES (newlen));
1447 expout_ptr += newlen;