1 /* YACC parser for Java expressions, for GDB.
2 Copyright (C) 1997, 1998, 1999, 2000, 2006, 2007, 2008
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 2 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, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* Parse a Java expression from text in a string,
23 and return the result as a struct expression pointer.
24 That structure contains arithmetic operations in reverse polish,
25 with constants represented by operations that are followed by special data.
26 See expression.h for the details of the format.
27 What is important here is that it can be built up sequentially
28 during the process of parsing; the lower levels of the tree always
29 come first in the result. Well, almost always; see ArrayAccess.
31 Note that malloc's and realloc's in this file are transformed to
32 xmalloc and xrealloc respectively by the same sed command in the
33 makefile that remaps any other malloc/realloc inserted by the parser
34 generator. Doing this with #defines and trying to control the interaction
35 with include files (<malloc.h> and <stdlib.h> for example) just became
36 too messy, particularly when such includes can be inserted at random
37 times by the parser generator. */
42 #include "gdb_string.h"
44 #include "expression.h"
46 #include "parser-defs.h"
49 #include "bfd.h" /* Required by objfiles.h. */
50 #include "symfile.h" /* Required by objfiles.h. */
51 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
54 #define parse_type builtin_type (parse_gdbarch)
56 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
57 as well as gratuitiously global symbol names, so we can have multiple
58 yacc generated parsers in gdb. Note that these are only the variables
59 produced by yacc. If other parser generators (bison, byacc, etc) produce
60 additional global names that conflict at link time, then those parser
61 generators need to be fixed instead of adding those names to this list. */
63 #define yymaxdepth java_maxdepth
64 #define yyparse java_parse
65 #define yylex java_lex
66 #define yyerror java_error
67 #define yylval java_lval
68 #define yychar java_char
69 #define yydebug java_debug
70 #define yypact java_pact
73 #define yydef java_def
74 #define yychk java_chk
75 #define yypgo java_pgo
76 #define yyact java_act
77 #define yyexca java_exca
78 #define yyerrflag java_errflag
79 #define yynerrs java_nerrs
83 #define yy_yys java_yys
84 #define yystate java_state
85 #define yytmp java_tmp
87 #define yy_yyv java_yyv
88 #define yyval java_val
89 #define yylloc java_lloc
90 #define yyreds java_reds /* With YYDEBUG defined */
91 #define yytoks java_toks /* With YYDEBUG defined */
92 #define yyname java_name /* With YYDEBUG defined */
93 #define yyrule java_rule /* With YYDEBUG defined */
94 #define yylhs java_yylhs
95 #define yylen java_yylen
96 #define yydefred java_yydefred
97 #define yydgoto java_yydgoto
98 #define yysindex java_yysindex
99 #define yyrindex java_yyrindex
100 #define yygindex java_yygindex
101 #define yytable java_yytable
102 #define yycheck java_yycheck
105 #define YYDEBUG 1 /* Default to yydebug support */
108 #define YYFPRINTF parser_fprintf
112 static int yylex (void);
114 void yyerror (char *);
116 static struct type *java_type_from_name (struct stoken);
117 static void push_expression_name (struct stoken);
118 static void push_fieldnames (struct stoken);
120 static struct expression *copy_exp (struct expression *, int);
121 static void insert_exp (int, struct expression *);
125 /* Although the yacc "value" of an expression is not used,
126 since the result is stored in the structure being created,
127 other node types do have values. */
144 struct symtoken ssym;
146 enum exp_opcode opcode;
147 struct internalvar *ivar;
152 /* YYSTYPE gets defined by %union */
153 static int parse_number (char *, int, int, YYSTYPE *);
156 %type <lval> rcurly Dims Dims_opt
157 %type <tval> ClassOrInterfaceType ClassType /* ReferenceType Type ArrayType */
158 %type <tval> IntegralType FloatingPointType NumericType PrimitiveType ArrayType PrimitiveOrArrayType
160 %token <typed_val_int> INTEGER_LITERAL
161 %token <typed_val_float> FLOATING_POINT_LITERAL
163 %token <sval> IDENTIFIER
164 %token <sval> STRING_LITERAL
165 %token <lval> BOOLEAN_LITERAL
166 %token <tsym> TYPENAME
167 %type <sval> Name SimpleName QualifiedName ForcedName
169 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
170 but which would parse as a valid number in the current input radix.
171 E.g. "c" when input_radix==16. Depending on the parse, it will be
172 turned into a name or into a number. */
174 %token <sval> NAME_OR_INT
178 /* Special type cases, put in to allow the parser to distinguish different
180 %token LONG SHORT BYTE INT CHAR BOOLEAN DOUBLE FLOAT
184 %token <opcode> ASSIGN_MODIFY
189 %right '=' ASSIGN_MODIFY
197 %left '<' '>' LEQ GEQ
201 %right INCREMENT DECREMENT
211 type_exp: PrimitiveOrArrayType
213 write_exp_elt_opcode(OP_TYPE);
214 write_exp_elt_type($1);
215 write_exp_elt_opcode(OP_TYPE);
219 PrimitiveOrArrayType:
227 write_exp_elt_opcode (OP_STRING);
228 write_exp_string ($1);
229 write_exp_elt_opcode (OP_STRING);
235 { write_exp_elt_opcode (OP_LONG);
236 write_exp_elt_type ($1.type);
237 write_exp_elt_longcst ((LONGEST)($1.val));
238 write_exp_elt_opcode (OP_LONG); }
241 parse_number ($1.ptr, $1.length, 0, &val);
242 write_exp_elt_opcode (OP_LONG);
243 write_exp_elt_type (val.typed_val_int.type);
244 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
245 write_exp_elt_opcode (OP_LONG);
247 | FLOATING_POINT_LITERAL
248 { write_exp_elt_opcode (OP_DOUBLE);
249 write_exp_elt_type ($1.type);
250 write_exp_elt_dblcst ($1.dval);
251 write_exp_elt_opcode (OP_DOUBLE); }
253 { write_exp_elt_opcode (OP_LONG);
254 write_exp_elt_type (java_boolean_type);
255 write_exp_elt_longcst ((LONGEST)$1);
256 write_exp_elt_opcode (OP_LONG); }
270 { $$ = java_boolean_type; }
280 { $$ = java_byte_type; }
282 { $$ = java_short_type; }
284 { $$ = java_int_type; }
286 { $$ = java_long_type; }
288 { $$ = java_char_type; }
293 { $$ = java_float_type; }
295 { $$ = java_double_type; }
305 ClassOrInterfaceType:
307 { $$ = java_type_from_name ($1); }
316 { $$ = java_array_type ($1, $2); }
318 { $$ = java_array_type (java_type_from_name ($1), $2); }
338 { $$.length = $1.length + $3.length + 1;
339 if ($1.ptr + $1.length + 1 == $3.ptr
340 && $1.ptr[$1.length] == '.')
341 $$.ptr = $1.ptr; /* Optimization. */
344 $$.ptr = (char *) malloc ($$.length + 1);
345 make_cleanup (free, $$.ptr);
346 sprintf ($$.ptr, "%.*s.%.*s",
347 $1.length, $1.ptr, $3.length, $3.ptr);
353 { write_exp_elt_opcode(OP_TYPE);
354 write_exp_elt_type($1);
355 write_exp_elt_opcode(OP_TYPE);}
359 /* Expressions, including the comma operator. */
361 | exp1 ',' Expression
362 { write_exp_elt_opcode (BINOP_COMMA); }
367 | ArrayCreationExpression
373 | ClassInstanceCreationExpression
377 | lcurly ArgumentList rcurly
378 { write_exp_elt_opcode (OP_ARRAY);
379 write_exp_elt_longcst ((LONGEST) 0);
380 write_exp_elt_longcst ((LONGEST) $3);
381 write_exp_elt_opcode (OP_ARRAY); }
386 { start_arglist (); }
391 { $$ = end_arglist () - 1; }
394 ClassInstanceCreationExpression:
395 NEW ClassType '(' ArgumentList_opt ')'
396 { internal_error (__FILE__, __LINE__,
397 _("FIXME - ClassInstanceCreationExpression")); }
403 | ArgumentList ',' Expression
413 ArrayCreationExpression:
414 NEW PrimitiveType DimExprs Dims_opt
415 { internal_error (__FILE__, __LINE__,
416 _("FIXME - ArrayCreationExpression")); }
417 | NEW ClassOrInterfaceType DimExprs Dims_opt
418 { internal_error (__FILE__, __LINE__,
419 _("FIXME - ArrayCreationExpression")); }
445 Primary '.' SimpleName
446 { push_fieldnames ($3); }
447 | VARIABLE '.' SimpleName
448 { push_fieldnames ($3); }
449 /*| SUPER '.' SimpleName { FIXME } */
454 { push_expression_name ($1); }
461 { write_exp_elt_opcode (OP_FUNCALL);
462 write_exp_elt_longcst ((LONGEST) end_arglist ());
463 write_exp_elt_opcode (OP_FUNCALL); }
464 | Primary '.' SimpleName '(' ArgumentList_opt ')'
465 { error (_("Form of method invocation not implemented")); }
466 | SUPER '.' SimpleName '(' ArgumentList_opt ')'
467 { error (_("Form of method invocation not implemented")); }
471 Name '[' Expression ']'
473 /* Emit code for the Name now, then exchange it in the
474 expout array with the Expression's code. We could
475 introduce a OP_SWAP code or a reversed version of
476 BINOP_SUBSCRIPT, but that makes the rest of GDB pay
477 for our parsing kludges. */
478 struct expression *name_expr;
480 push_expression_name ($1);
481 name_expr = copy_exp (expout, expout_ptr);
482 expout_ptr -= name_expr->nelts;
483 insert_exp (expout_ptr-length_of_subexp (expout, expout_ptr),
486 write_exp_elt_opcode (BINOP_SUBSCRIPT);
488 | VARIABLE '[' Expression ']'
489 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
490 | PrimaryNoNewArray '[' Expression ']'
491 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
497 { push_expression_name ($1); }
499 /* Already written by write_dollar_variable. */
500 | PostIncrementExpression
501 | PostDecrementExpression
504 PostIncrementExpression:
505 PostfixExpression INCREMENT
506 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
509 PostDecrementExpression:
510 PostfixExpression DECREMENT
511 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
515 PreIncrementExpression
516 | PreDecrementExpression
517 | '+' UnaryExpression
518 | '-' UnaryExpression
519 { write_exp_elt_opcode (UNOP_NEG); }
520 | '*' UnaryExpression
521 { write_exp_elt_opcode (UNOP_IND); } /*FIXME not in Java */
522 | UnaryExpressionNotPlusMinus
525 PreIncrementExpression:
526 INCREMENT UnaryExpression
527 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
530 PreDecrementExpression:
531 DECREMENT UnaryExpression
532 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
535 UnaryExpressionNotPlusMinus:
537 | '~' UnaryExpression
538 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
539 | '!' UnaryExpression
540 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
545 '(' PrimitiveType Dims_opt ')' UnaryExpression
546 { write_exp_elt_opcode (UNOP_CAST);
547 write_exp_elt_type (java_array_type ($2, $3));
548 write_exp_elt_opcode (UNOP_CAST); }
549 | '(' Expression ')' UnaryExpressionNotPlusMinus
551 int exp_size = expout_ptr;
552 int last_exp_size = length_of_subexp(expout, expout_ptr);
555 int base = expout_ptr - last_exp_size - 3;
556 if (base < 0 || expout->elts[base+2].opcode != OP_TYPE)
557 error (_("Invalid cast expression"));
558 type = expout->elts[base+1].type;
559 /* Remove the 'Expression' and slide the
560 UnaryExpressionNotPlusMinus down to replace it. */
561 for (i = 0; i < last_exp_size; i++)
562 expout->elts[base + i] = expout->elts[base + i + 3];
564 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
565 type = lookup_pointer_type (type);
566 write_exp_elt_opcode (UNOP_CAST);
567 write_exp_elt_type (type);
568 write_exp_elt_opcode (UNOP_CAST);
570 | '(' Name Dims ')' UnaryExpressionNotPlusMinus
571 { write_exp_elt_opcode (UNOP_CAST);
572 write_exp_elt_type (java_array_type (java_type_from_name ($2), $3));
573 write_exp_elt_opcode (UNOP_CAST); }
577 MultiplicativeExpression:
579 | MultiplicativeExpression '*' UnaryExpression
580 { write_exp_elt_opcode (BINOP_MUL); }
581 | MultiplicativeExpression '/' UnaryExpression
582 { write_exp_elt_opcode (BINOP_DIV); }
583 | MultiplicativeExpression '%' UnaryExpression
584 { write_exp_elt_opcode (BINOP_REM); }
588 MultiplicativeExpression
589 | AdditiveExpression '+' MultiplicativeExpression
590 { write_exp_elt_opcode (BINOP_ADD); }
591 | AdditiveExpression '-' MultiplicativeExpression
592 { write_exp_elt_opcode (BINOP_SUB); }
597 | ShiftExpression LSH AdditiveExpression
598 { write_exp_elt_opcode (BINOP_LSH); }
599 | ShiftExpression RSH AdditiveExpression
600 { write_exp_elt_opcode (BINOP_RSH); }
601 /* | ShiftExpression >>> AdditiveExpression { FIXME } */
604 RelationalExpression:
606 | RelationalExpression '<' ShiftExpression
607 { write_exp_elt_opcode (BINOP_LESS); }
608 | RelationalExpression '>' ShiftExpression
609 { write_exp_elt_opcode (BINOP_GTR); }
610 | RelationalExpression LEQ ShiftExpression
611 { write_exp_elt_opcode (BINOP_LEQ); }
612 | RelationalExpression GEQ ShiftExpression
613 { write_exp_elt_opcode (BINOP_GEQ); }
614 /* | RelationalExpresion INSTANCEOF ReferenceType { FIXME } */
619 | EqualityExpression EQUAL RelationalExpression
620 { write_exp_elt_opcode (BINOP_EQUAL); }
621 | EqualityExpression NOTEQUAL RelationalExpression
622 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
627 | AndExpression '&' EqualityExpression
628 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
631 ExclusiveOrExpression:
633 | ExclusiveOrExpression '^' AndExpression
634 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
636 InclusiveOrExpression:
637 ExclusiveOrExpression
638 | InclusiveOrExpression '|' ExclusiveOrExpression
639 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
642 ConditionalAndExpression:
643 InclusiveOrExpression
644 | ConditionalAndExpression ANDAND InclusiveOrExpression
645 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
648 ConditionalOrExpression:
649 ConditionalAndExpression
650 | ConditionalOrExpression OROR ConditionalAndExpression
651 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
654 ConditionalExpression:
655 ConditionalOrExpression
656 | ConditionalOrExpression '?' Expression ':' ConditionalExpression
657 { write_exp_elt_opcode (TERNOP_COND); }
660 AssignmentExpression:
661 ConditionalExpression
666 LeftHandSide '=' ConditionalExpression
667 { write_exp_elt_opcode (BINOP_ASSIGN); }
668 | LeftHandSide ASSIGN_MODIFY ConditionalExpression
669 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
670 write_exp_elt_opcode ($2);
671 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
676 { push_expression_name ($1); }
678 /* Already written by write_dollar_variable. */
689 /* Take care of parsing a number (anything that starts with a digit).
690 Set yylval and return the token type; update lexptr.
691 LEN is the number of characters in it. */
693 /*** Needs some error checking for the float case ***/
696 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
699 ULONGEST limit, limit_div_base;
702 int base = input_radix;
708 /* It's a float since it contains a point or an exponent. */
710 int num = 0; /* number of tokens scanned by scanf */
711 char saved_char = p[len];
713 p[len] = 0; /* null-terminate the token */
714 num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%c",
715 &putithere->typed_val_float.dval, &c);
716 p[len] = saved_char; /* restore the input stream */
717 if (num != 1) /* check scanf found ONLY a float ... */
719 /* See if it has `f' or `d' suffix (float or double). */
721 c = tolower (p[len - 1]);
723 if (c == 'f' || c == 'F')
724 putithere->typed_val_float.type = parse_type->builtin_float;
725 else if (isdigit (c) || c == '.' || c == 'd' || c == 'D')
726 putithere->typed_val_float.type = parse_type->builtin_double;
730 return FLOATING_POINT_LITERAL;
733 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
765 /* A paranoid calculation of (1<<64)-1. */
766 limit = (ULONGEST)0xffffffff;
767 limit = ((limit << 16) << 16) | limit;
768 if (c == 'l' || c == 'L')
770 type = java_long_type;
775 type = java_int_type;
777 limit_div_base = limit / (ULONGEST) base;
782 if (c >= '0' && c <= '9')
784 else if (c >= 'A' && c <= 'Z')
786 else if (c >= 'a' && c <= 'z')
789 return ERROR; /* Char not a digit */
792 if (n > limit_div_base
793 || (n *= base) > limit - c)
794 error (_("Numeric constant too large"));
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 builtin_type_uint64
800 rather than java_long_type. 0x80000000 will become -0x80000000 instead
801 of 0x80000000L, because we don't know the sign at this point.
803 if (type == java_int_type && n > (ULONGEST)0x80000000)
804 type = builtin_type_uint64;
806 putithere->typed_val_int.val = n;
807 putithere->typed_val_int.type = type;
809 return INTEGER_LITERAL;
816 enum exp_opcode opcode;
819 static const struct token tokentab3[] =
821 {">>=", ASSIGN_MODIFY, BINOP_RSH},
822 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
825 static const struct token tokentab2[] =
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}
847 /* Read one token, getting characters through lexptr. */
858 static char *tempbuf;
859 static int tempbufsize;
863 prev_lexptr = 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)
871 yylval.opcode = tokentab3[i].opcode;
872 return tokentab3[i].token;
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)
880 yylval.opcode = tokentab2[i].opcode;
881 return tokentab2[i].token;
884 switch (c = *tokstart)
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++
902 c = parse_escape (&lexptr);
904 error (_("Empty character constant"));
906 yylval.typed_val_int.val = c;
907 yylval.typed_val_int.type = java_char_type;
912 namelen = skip_quoted (tokstart) - tokstart;
915 lexptr = tokstart + namelen;
916 if (lexptr[-1] != '\'')
917 error (_("Unmatched single quote"));
922 error (_("Invalid character constant"));
924 return INTEGER_LITERAL;
932 if (paren_depth == 0)
939 if (comma_terminates && paren_depth == 0)
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. */
962 int got_dot = 0, got_e = 0, toktype;
964 int hex = input_radix > 10;
966 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
971 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
979 /* This test includes !hex because 'e' is a valid hex digit
980 and thus does not indicate a floating point number when
982 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
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 == '.')
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
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')))
1000 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1001 if (toktype == ERROR)
1003 char *err_copy = (char *) alloca (p - tokstart + 1);
1005 memcpy (err_copy, tokstart, p - tokstart);
1006 err_copy[p - tokstart] = 0;
1007 error (_("Invalid number \"%s\""), err_copy);
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 */
1047 tokptr = ++tokstart;
1051 /* Grow the static temp buffer if necessary, including allocating
1052 the first one on demand. */
1053 if (tempbufindex + 1 >= tempbufsize)
1055 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1061 /* Do nothing, loop will terminate. */
1065 c = parse_escape (&tokptr);
1070 tempbuf[tempbufindex++] = c;
1073 tempbuf[tempbufindex++] = *tokptr++;
1076 } while ((*tokptr != '"') && (*tokptr != '\0'));
1077 if (*tokptr++ != '"')
1079 error (_("Unterminated string in expression"));
1081 tempbuf[tempbufindex] = '\0'; /* See note above */
1082 yylval.sval.ptr = tempbuf;
1083 yylval.sval.length = tempbufindex;
1085 return (STRING_LITERAL);
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);
1093 /* It's a name. See how long it is. */
1095 for (c = tokstart[namelen];
1098 || (c >= '0' && c <= '9')
1099 || (c >= 'a' && c <= 'z')
1100 || (c >= 'A' && c <= 'Z')
1107 while (tokstart[++i] && tokstart[i] != '>');
1108 if (tokstart[i] == '>')
1111 c = tokstart[++namelen];
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')
1125 /* Catch specific keywords. Should be done with a data structure. */
1129 if (strncmp (tokstart, "boolean", 7) == 0)
1133 if (strncmp (tokstart, "double", 6) == 0)
1137 if (strncmp (tokstart, "short", 5) == 0)
1139 if (strncmp (tokstart, "false", 5) == 0)
1142 return BOOLEAN_LITERAL;
1144 if (strncmp (tokstart, "super", 5) == 0)
1146 if (strncmp (tokstart, "float", 5) == 0)
1150 if (strncmp (tokstart, "long", 4) == 0)
1152 if (strncmp (tokstart, "byte", 4) == 0)
1154 if (strncmp (tokstart, "char", 4) == 0)
1156 if (strncmp (tokstart, "true", 4) == 0)
1159 return BOOLEAN_LITERAL;
1163 if (strncmp (tokstart, "int", 3) == 0)
1165 if (strncmp (tokstart, "new", 3) == 0)
1172 yylval.sval.ptr = tokstart;
1173 yylval.sval.length = namelen;
1175 if (*tokstart == '$')
1177 write_dollar_variable (yylval.sval);
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)))
1187 YYSTYPE newlval; /* Its value is ignored. */
1188 int hextype = parse_number (tokstart, namelen, 0, &newlval);
1189 if (hextype == INTEGER_LITERAL)
1199 lexptr = prev_lexptr;
1202 error (_("%s: near `%s'"), msg, lexptr);
1204 error (_("error in expression, near `%s'"), lexptr);
1207 static struct type *
1208 java_type_from_name (struct stoken name)
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);
1217 /* If NAME is a valid variable name in this scope, push it and return 1.
1218 Otherwise, return 0. */
1221 push_variable (struct stoken name)
1223 char *tmp = copy_name (name);
1224 int is_a_field_of_this = 0;
1226 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN,
1227 &is_a_field_of_this);
1228 if (sym && SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1230 if (symbol_read_needs_frame (sym))
1232 if (innermost_block == 0 ||
1233 contained_in (block_found, innermost_block))
1234 innermost_block = block_found;
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);
1245 if (is_a_field_of_this)
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);
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. */
1267 push_fieldnames (struct stoken name)
1270 struct stoken token;
1271 token.ptr = name.ptr;
1274 if (i == name.length || name.ptr[i] == '.')
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;
1283 if (i >= name.length)
1288 /* Helper routine for push_expression_name.
1289 Handle a qualified name, where DOT_INDEX is the index of the first '.' */
1292 push_qualified_expression_name (struct stoken name, int dot_index)
1294 struct stoken token;
1298 token.ptr = name.ptr;
1299 token.length = dot_index;
1301 if (push_variable (token))
1303 token.ptr = name.ptr + dot_index + 1;
1304 token.length = name.length - dot_index - 1;
1305 push_fieldnames (token);
1309 token.ptr = name.ptr;
1312 token.length = dot_index;
1313 tmp = copy_name (token);
1314 typ = java_lookup_class (tmp);
1317 if (dot_index == name.length)
1319 write_exp_elt_opcode(OP_TYPE);
1320 write_exp_elt_type(typ);
1321 write_exp_elt_opcode(OP_TYPE);
1324 dot_index++; /* Skip '.' */
1325 name.ptr += dot_index;
1326 name.length -= dot_index;
1328 while (dot_index < name.length && name.ptr[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)
1339 name.ptr += dot_index;
1340 name.length -= dot_index;
1341 push_fieldnames (name);
1345 else if (dot_index >= name.length)
1347 dot_index++; /* Skip '.' */
1348 while (dot_index < name.length && name.ptr[dot_index] != '.')
1351 error (_("unknown type `%.*s'"), name.length, name.ptr);
1354 /* Handle Name in an expression (or LHS).
1355 Handle VAR, TYPE, TYPE.FIELD1....FIELDN and VAR.FIELD1....FIELDN. */
1358 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;