1 /* YACC parser for Java expressions, for GDB.
2 Copyright 1997, 1998, 1999, 2000
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Parse a Java expression from text in a string,
22 and return the result as a struct expression pointer.
23 That structure contains arithmetic operations in reverse polish,
24 with constants represented by operations that are followed by special data.
25 See expression.h for the details of the format.
26 What is important here is that it can be built up sequentially
27 during the process of parsing; the lower levels of the tree always
28 come first in the result. Well, almost always; see ArrayAccess.
30 Note that malloc's and realloc's in this file are transformed to
31 xmalloc and xrealloc respectively by the same sed command in the
32 makefile that remaps any other malloc/realloc inserted by the parser
33 generator. Doing this with #defines and trying to control the interaction
34 with include files (<malloc.h> and <stdlib.h> for example) just became
35 too messy, particularly when such includes can be inserted at random
36 times by the parser generator. */
41 #include "gdb_string.h"
43 #include "expression.h"
45 #include "parser-defs.h"
48 #include "bfd.h" /* Required by objfiles.h. */
49 #include "symfile.h" /* Required by objfiles.h. */
50 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
52 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
53 as well as gratuitiously global symbol names, so we can have multiple
54 yacc generated parsers in gdb. Note that these are only the variables
55 produced by yacc. If other parser generators (bison, byacc, etc) produce
56 additional global names that conflict at link time, then those parser
57 generators need to be fixed instead of adding those names to this list. */
59 #define yymaxdepth java_maxdepth
60 #define yyparse java_parse
61 #define yylex java_lex
62 #define yyerror java_error
63 #define yylval java_lval
64 #define yychar java_char
65 #define yydebug java_debug
66 #define yypact java_pact
69 #define yydef java_def
70 #define yychk java_chk
71 #define yypgo java_pgo
72 #define yyact java_act
73 #define yyexca java_exca
74 #define yyerrflag java_errflag
75 #define yynerrs java_nerrs
79 #define yy_yys java_yys
80 #define yystate java_state
81 #define yytmp java_tmp
83 #define yy_yyv java_yyv
84 #define yyval java_val
85 #define yylloc java_lloc
86 #define yyreds java_reds /* With YYDEBUG defined */
87 #define yytoks java_toks /* With YYDEBUG defined */
88 #define yylhs java_yylhs
89 #define yylen java_yylen
90 #define yydefred java_yydefred
91 #define yydgoto java_yydgoto
92 #define yysindex java_yysindex
93 #define yyrindex java_yyrindex
94 #define yygindex java_yygindex
95 #define yytable java_yytable
96 #define yycheck java_yycheck
99 #define YYDEBUG 1 /* Default to yydebug support */
102 #define YYFPRINTF parser_fprintf
106 static int yylex (void);
108 void yyerror (char *);
110 static struct type *java_type_from_name (struct stoken);
111 static void push_expression_name (struct stoken);
112 static void push_fieldnames (struct stoken);
114 static struct expression *copy_exp (struct expression *, int);
115 static void insert_exp (int, struct expression *);
119 /* Although the yacc "value" of an expression is not used,
120 since the result is stored in the structure being created,
121 other node types do have values. */
138 struct symtoken ssym;
140 enum exp_opcode opcode;
141 struct internalvar *ivar;
146 /* YYSTYPE gets defined by %union */
147 static int parse_number (char *, int, int, YYSTYPE *);
150 %type <lval> rcurly Dims Dims_opt
151 %type <tval> ClassOrInterfaceType ClassType /* ReferenceType Type ArrayType */
152 %type <tval> IntegralType FloatingPointType NumericType PrimitiveType ArrayType PrimitiveOrArrayType
154 %token <typed_val_int> INTEGER_LITERAL
155 %token <typed_val_float> FLOATING_POINT_LITERAL
157 %token <sval> IDENTIFIER
158 %token <sval> STRING_LITERAL
159 %token <lval> BOOLEAN_LITERAL
160 %token <tsym> TYPENAME
161 %type <sval> Name SimpleName QualifiedName ForcedName
163 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
164 but which would parse as a valid number in the current input radix.
165 E.g. "c" when input_radix==16. Depending on the parse, it will be
166 turned into a name or into a number. */
168 %token <sval> NAME_OR_INT
172 /* Special type cases, put in to allow the parser to distinguish different
174 %token LONG SHORT BYTE INT CHAR BOOLEAN DOUBLE FLOAT
178 %token <opcode> ASSIGN_MODIFY
180 %token THIS SUPER NEW
183 %right '=' ASSIGN_MODIFY
191 %left '<' '>' LEQ GEQ
195 %right INCREMENT DECREMENT
205 type_exp: PrimitiveOrArrayType
207 write_exp_elt_opcode(OP_TYPE);
208 write_exp_elt_type($1);
209 write_exp_elt_opcode(OP_TYPE);
213 PrimitiveOrArrayType:
221 write_exp_elt_opcode (OP_STRING);
222 write_exp_string ($1);
223 write_exp_elt_opcode (OP_STRING);
229 { write_exp_elt_opcode (OP_LONG);
230 write_exp_elt_type ($1.type);
231 write_exp_elt_longcst ((LONGEST)($1.val));
232 write_exp_elt_opcode (OP_LONG); }
235 parse_number ($1.ptr, $1.length, 0, &val);
236 write_exp_elt_opcode (OP_LONG);
237 write_exp_elt_type (val.typed_val_int.type);
238 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
239 write_exp_elt_opcode (OP_LONG);
241 | FLOATING_POINT_LITERAL
242 { write_exp_elt_opcode (OP_DOUBLE);
243 write_exp_elt_type ($1.type);
244 write_exp_elt_dblcst ($1.dval);
245 write_exp_elt_opcode (OP_DOUBLE); }
247 { write_exp_elt_opcode (OP_LONG);
248 write_exp_elt_type (java_boolean_type);
249 write_exp_elt_longcst ((LONGEST)$1);
250 write_exp_elt_opcode (OP_LONG); }
264 { $$ = java_boolean_type; }
274 { $$ = java_byte_type; }
276 { $$ = java_short_type; }
278 { $$ = java_int_type; }
280 { $$ = java_long_type; }
282 { $$ = java_char_type; }
287 { $$ = java_float_type; }
289 { $$ = java_double_type; }
299 ClassOrInterfaceType:
301 { $$ = java_type_from_name ($1); }
310 { $$ = java_array_type ($1, $2); }
312 { $$ = java_array_type (java_type_from_name ($1), $2); }
332 { $$.length = $1.length + $3.length + 1;
333 if ($1.ptr + $1.length + 1 == $3.ptr
334 && $1.ptr[$1.length] == '.')
335 $$.ptr = $1.ptr; /* Optimization. */
338 $$.ptr = (char *) malloc ($$.length + 1);
339 make_cleanup (free, $$.ptr);
340 sprintf ($$.ptr, "%.*s.%.*s",
341 $1.length, $1.ptr, $3.length, $3.ptr);
347 { write_exp_elt_opcode(OP_TYPE);
348 write_exp_elt_type($1);
349 write_exp_elt_opcode(OP_TYPE);}
353 /* Expressions, including the comma operator. */
355 | exp1 ',' Expression
356 { write_exp_elt_opcode (BINOP_COMMA); }
361 | ArrayCreationExpression
367 { write_exp_elt_opcode (OP_THIS);
368 write_exp_elt_opcode (OP_THIS); }
370 | ClassInstanceCreationExpression
374 | lcurly ArgumentList rcurly
375 { write_exp_elt_opcode (OP_ARRAY);
376 write_exp_elt_longcst ((LONGEST) 0);
377 write_exp_elt_longcst ((LONGEST) $3);
378 write_exp_elt_opcode (OP_ARRAY); }
383 { start_arglist (); }
388 { $$ = end_arglist () - 1; }
391 ClassInstanceCreationExpression:
392 NEW ClassType '(' ArgumentList_opt ')'
393 { internal_error (__FILE__, __LINE__,
394 _("FIXME - ClassInstanceCreationExpression")); }
400 | ArgumentList ',' Expression
410 ArrayCreationExpression:
411 NEW PrimitiveType DimExprs Dims_opt
412 { internal_error (__FILE__, __LINE__,
413 _("FIXME - ArrayCreationExpression")); }
414 | NEW ClassOrInterfaceType DimExprs Dims_opt
415 { internal_error (__FILE__, __LINE__,
416 _("FIXME - ArrayCreationExpression")); }
442 Primary '.' SimpleName
443 { push_fieldnames ($3); }
444 | VARIABLE '.' SimpleName
445 { push_fieldnames ($3); }
446 /*| SUPER '.' SimpleName { FIXME } */
450 Name '(' ArgumentList_opt ')'
451 { error (_("Method invocation not implemented")); }
452 | Primary '.' SimpleName '(' ArgumentList_opt ')'
453 { error (_("Method invocation not implemented")); }
454 | SUPER '.' SimpleName '(' ArgumentList_opt ')'
455 { error (_("Method invocation not implemented")); }
459 Name '[' Expression ']'
461 /* Emit code for the Name now, then exchange it in the
462 expout array with the Expression's code. We could
463 introduce a OP_SWAP code or a reversed version of
464 BINOP_SUBSCRIPT, but that makes the rest of GDB pay
465 for our parsing kludges. */
466 struct expression *name_expr;
468 push_expression_name ($1);
469 name_expr = copy_exp (expout, expout_ptr);
470 expout_ptr -= name_expr->nelts;
471 insert_exp (expout_ptr-length_of_subexp (expout, expout_ptr),
474 write_exp_elt_opcode (BINOP_SUBSCRIPT);
476 | VARIABLE '[' Expression ']'
477 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
478 | PrimaryNoNewArray '[' Expression ']'
479 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
485 { push_expression_name ($1); }
487 /* Already written by write_dollar_variable. */
488 | PostIncrementExpression
489 | PostDecrementExpression
492 PostIncrementExpression:
493 PostfixExpression INCREMENT
494 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
497 PostDecrementExpression:
498 PostfixExpression DECREMENT
499 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
503 PreIncrementExpression
504 | PreDecrementExpression
505 | '+' UnaryExpression
506 | '-' UnaryExpression
507 { write_exp_elt_opcode (UNOP_NEG); }
508 | '*' UnaryExpression
509 { write_exp_elt_opcode (UNOP_IND); } /*FIXME not in Java */
510 | UnaryExpressionNotPlusMinus
513 PreIncrementExpression:
514 INCREMENT UnaryExpression
515 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
518 PreDecrementExpression:
519 DECREMENT UnaryExpression
520 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
523 UnaryExpressionNotPlusMinus:
525 | '~' UnaryExpression
526 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
527 | '!' UnaryExpression
528 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
533 '(' PrimitiveType Dims_opt ')' UnaryExpression
534 { write_exp_elt_opcode (UNOP_CAST);
535 write_exp_elt_type (java_array_type ($2, $3));
536 write_exp_elt_opcode (UNOP_CAST); }
537 | '(' Expression ')' UnaryExpressionNotPlusMinus
539 int exp_size = expout_ptr;
540 int last_exp_size = length_of_subexp(expout, expout_ptr);
543 int base = expout_ptr - last_exp_size - 3;
544 if (base < 0 || expout->elts[base+2].opcode != OP_TYPE)
545 error (_("Invalid cast expression"));
546 type = expout->elts[base+1].type;
547 /* Remove the 'Expression' and slide the
548 UnaryExpressionNotPlusMinus down to replace it. */
549 for (i = 0; i < last_exp_size; i++)
550 expout->elts[base + i] = expout->elts[base + i + 3];
552 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
553 type = lookup_pointer_type (type);
554 write_exp_elt_opcode (UNOP_CAST);
555 write_exp_elt_type (type);
556 write_exp_elt_opcode (UNOP_CAST);
558 | '(' Name Dims ')' UnaryExpressionNotPlusMinus
559 { write_exp_elt_opcode (UNOP_CAST);
560 write_exp_elt_type (java_array_type (java_type_from_name ($2), $3));
561 write_exp_elt_opcode (UNOP_CAST); }
565 MultiplicativeExpression:
567 | MultiplicativeExpression '*' UnaryExpression
568 { write_exp_elt_opcode (BINOP_MUL); }
569 | MultiplicativeExpression '/' UnaryExpression
570 { write_exp_elt_opcode (BINOP_DIV); }
571 | MultiplicativeExpression '%' UnaryExpression
572 { write_exp_elt_opcode (BINOP_REM); }
576 MultiplicativeExpression
577 | AdditiveExpression '+' MultiplicativeExpression
578 { write_exp_elt_opcode (BINOP_ADD); }
579 | AdditiveExpression '-' MultiplicativeExpression
580 { write_exp_elt_opcode (BINOP_SUB); }
585 | ShiftExpression LSH AdditiveExpression
586 { write_exp_elt_opcode (BINOP_LSH); }
587 | ShiftExpression RSH AdditiveExpression
588 { write_exp_elt_opcode (BINOP_RSH); }
589 /* | ShiftExpression >>> AdditiveExpression { FIXME } */
592 RelationalExpression:
594 | RelationalExpression '<' ShiftExpression
595 { write_exp_elt_opcode (BINOP_LESS); }
596 | RelationalExpression '>' ShiftExpression
597 { write_exp_elt_opcode (BINOP_GTR); }
598 | RelationalExpression LEQ ShiftExpression
599 { write_exp_elt_opcode (BINOP_LEQ); }
600 | RelationalExpression GEQ ShiftExpression
601 { write_exp_elt_opcode (BINOP_GEQ); }
602 /* | RelationalExpresion INSTANCEOF ReferenceType { FIXME } */
607 | EqualityExpression EQUAL RelationalExpression
608 { write_exp_elt_opcode (BINOP_EQUAL); }
609 | EqualityExpression NOTEQUAL RelationalExpression
610 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
615 | AndExpression '&' EqualityExpression
616 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
619 ExclusiveOrExpression:
621 | ExclusiveOrExpression '^' AndExpression
622 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
624 InclusiveOrExpression:
625 ExclusiveOrExpression
626 | InclusiveOrExpression '|' ExclusiveOrExpression
627 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
630 ConditionalAndExpression:
631 InclusiveOrExpression
632 | ConditionalAndExpression ANDAND InclusiveOrExpression
633 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
636 ConditionalOrExpression:
637 ConditionalAndExpression
638 | ConditionalOrExpression OROR ConditionalAndExpression
639 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
642 ConditionalExpression:
643 ConditionalOrExpression
644 | ConditionalOrExpression '?' Expression ':' ConditionalExpression
645 { write_exp_elt_opcode (TERNOP_COND); }
648 AssignmentExpression:
649 ConditionalExpression
654 LeftHandSide '=' ConditionalExpression
655 { write_exp_elt_opcode (BINOP_ASSIGN); }
656 | LeftHandSide ASSIGN_MODIFY ConditionalExpression
657 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
658 write_exp_elt_opcode ($2);
659 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
664 { push_expression_name ($1); }
666 /* Already written by write_dollar_variable. */
677 /* Take care of parsing a number (anything that starts with a digit).
678 Set yylval and return the token type; update lexptr.
679 LEN is the number of characters in it. */
681 /*** Needs some error checking for the float case ***/
684 parse_number (p, len, parsed_float, putithere)
690 register ULONGEST n = 0;
691 ULONGEST limit, limit_div_base;
694 register int base = input_radix;
700 /* It's a float since it contains a point or an exponent. */
702 int num = 0; /* number of tokens scanned by scanf */
703 char saved_char = p[len];
705 p[len] = 0; /* null-terminate the token */
706 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
707 num = sscanf (p, "%g%c", (float *) &putithere->typed_val_float.dval, &c);
708 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
709 num = sscanf (p, "%lg%c", (double *) &putithere->typed_val_float.dval, &c);
712 #ifdef SCANF_HAS_LONG_DOUBLE
713 num = sscanf (p, "%Lg%c", &putithere->typed_val_float.dval, &c);
715 /* Scan it into a double, then assign it to the long double.
716 This at least wins with values representable in the range
719 num = sscanf (p, "%lg%c", &temp, &c);
720 putithere->typed_val_float.dval = temp;
723 p[len] = saved_char; /* restore the input stream */
724 if (num != 1) /* check scanf found ONLY a float ... */
726 /* See if it has `f' or `d' suffix (float or double). */
728 c = tolower (p[len - 1]);
730 if (c == 'f' || c == 'F')
731 putithere->typed_val_float.type = builtin_type_float;
732 else if (isdigit (c) || c == '.' || c == 'd' || c == 'D')
733 putithere->typed_val_float.type = builtin_type_double;
737 return FLOATING_POINT_LITERAL;
740 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
772 /* A paranoid calculation of (1<<64)-1. */
773 limit = (ULONGEST)0xffffffff;
774 limit = ((limit << 16) << 16) | limit;
775 if (c == 'l' || c == 'L')
777 type = java_long_type;
782 type = java_int_type;
784 limit_div_base = limit / (ULONGEST) base;
789 if (c >= '0' && c <= '9')
791 else if (c >= 'A' && c <= 'Z')
793 else if (c >= 'a' && c <= 'z')
796 return ERROR; /* Char not a digit */
799 if (n > limit_div_base
800 || (n *= base) > limit - c)
801 error (_("Numeric constant too large"));
805 /* If the type is bigger than a 32-bit signed integer can be, implicitly
806 promote to long. Java does not do this, so mark it as builtin_type_uint64
807 rather than java_long_type. 0x80000000 will become -0x80000000 instead
808 of 0x80000000L, because we don't know the sign at this point.
810 if (type == java_int_type && n > (ULONGEST)0x80000000)
811 type = builtin_type_uint64;
813 putithere->typed_val_int.val = n;
814 putithere->typed_val_int.type = type;
816 return INTEGER_LITERAL;
823 enum exp_opcode opcode;
826 static const struct token tokentab3[] =
828 {">>=", ASSIGN_MODIFY, BINOP_RSH},
829 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
832 static const struct token tokentab2[] =
834 {"+=", ASSIGN_MODIFY, BINOP_ADD},
835 {"-=", ASSIGN_MODIFY, BINOP_SUB},
836 {"*=", ASSIGN_MODIFY, BINOP_MUL},
837 {"/=", ASSIGN_MODIFY, BINOP_DIV},
838 {"%=", ASSIGN_MODIFY, BINOP_REM},
839 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
840 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
841 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
842 {"++", INCREMENT, BINOP_END},
843 {"--", DECREMENT, BINOP_END},
844 {"&&", ANDAND, BINOP_END},
845 {"||", OROR, BINOP_END},
846 {"<<", LSH, BINOP_END},
847 {">>", RSH, BINOP_END},
848 {"==", EQUAL, BINOP_END},
849 {"!=", NOTEQUAL, BINOP_END},
850 {"<=", LEQ, BINOP_END},
851 {">=", GEQ, BINOP_END}
854 /* Read one token, getting characters through lexptr. */
865 static char *tempbuf;
866 static int tempbufsize;
870 prev_lexptr = lexptr;
873 /* See if it is a special token of length 3. */
874 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
875 if (STREQN (tokstart, tokentab3[i].operator, 3))
878 yylval.opcode = tokentab3[i].opcode;
879 return tokentab3[i].token;
882 /* See if it is a special token of length 2. */
883 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
884 if (STREQN (tokstart, tokentab2[i].operator, 2))
887 yylval.opcode = tokentab2[i].opcode;
888 return tokentab2[i].token;
891 switch (c = *tokstart)
903 /* We either have a character constant ('0' or '\177' for example)
904 or we have a quoted symbol reference ('foo(int,int)' in C++
909 c = parse_escape (&lexptr);
911 error (_("Empty character constant"));
913 yylval.typed_val_int.val = c;
914 yylval.typed_val_int.type = java_char_type;
919 namelen = skip_quoted (tokstart) - tokstart;
922 lexptr = tokstart + namelen;
923 if (lexptr[-1] != '\'')
924 error (_("Unmatched single quote"));
929 error (_("Invalid character constant"));
931 return INTEGER_LITERAL;
939 if (paren_depth == 0)
946 if (comma_terminates && paren_depth == 0)
952 /* Might be a floating point number. */
953 if (lexptr[1] < '0' || lexptr[1] > '9')
954 goto symbol; /* Nope, must be a symbol. */
955 /* FALL THRU into number case. */
969 int got_dot = 0, got_e = 0, toktype;
970 register char *p = tokstart;
971 int hex = input_radix > 10;
973 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
978 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
986 /* This test includes !hex because 'e' is a valid hex digit
987 and thus does not indicate a floating point number when
989 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
991 /* This test does not include !hex, because a '.' always indicates
992 a decimal floating point number regardless of the radix. */
993 else if (!got_dot && *p == '.')
995 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
996 && (*p == '-' || *p == '+'))
997 /* This is the sign of the exponent, not the end of the
1000 /* We will take any letters or digits. parse_number will
1001 complain if past the radix, or if L or U are not final. */
1002 else if ((*p < '0' || *p > '9')
1003 && ((*p < 'a' || *p > 'z')
1004 && (*p < 'A' || *p > 'Z')))
1007 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1008 if (toktype == ERROR)
1010 char *err_copy = (char *) alloca (p - tokstart + 1);
1012 memcpy (err_copy, tokstart, p - tokstart);
1013 err_copy[p - tokstart] = 0;
1014 error (_("Invalid number \"%s\""), err_copy);
1045 /* Build the gdb internal form of the input string in tempbuf,
1046 translating any standard C escape forms seen. Note that the
1047 buffer is null byte terminated *only* for the convenience of
1048 debugging gdb itself and printing the buffer contents when
1049 the buffer contains no embedded nulls. Gdb does not depend
1050 upon the buffer being null byte terminated, it uses the length
1051 string instead. This allows gdb to handle C strings (as well
1052 as strings in other languages) with embedded null bytes */
1054 tokptr = ++tokstart;
1058 /* Grow the static temp buffer if necessary, including allocating
1059 the first one on demand. */
1060 if (tempbufindex + 1 >= tempbufsize)
1062 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1068 /* Do nothing, loop will terminate. */
1072 c = parse_escape (&tokptr);
1077 tempbuf[tempbufindex++] = c;
1080 tempbuf[tempbufindex++] = *tokptr++;
1083 } while ((*tokptr != '"') && (*tokptr != '\0'));
1084 if (*tokptr++ != '"')
1086 error (_("Unterminated string in expression"));
1088 tempbuf[tempbufindex] = '\0'; /* See note above */
1089 yylval.sval.ptr = tempbuf;
1090 yylval.sval.length = tempbufindex;
1092 return (STRING_LITERAL);
1095 if (!(c == '_' || c == '$'
1096 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1097 /* We must have come across a bad character (e.g. ';'). */
1098 error (_("Invalid character '%c' in expression"), c);
1100 /* It's a name. See how long it is. */
1102 for (c = tokstart[namelen];
1105 || (c >= '0' && c <= '9')
1106 || (c >= 'a' && c <= 'z')
1107 || (c >= 'A' && c <= 'Z')
1114 while (tokstart[++i] && tokstart[i] != '>');
1115 if (tokstart[i] == '>')
1118 c = tokstart[++namelen];
1121 /* The token "if" terminates the expression and is NOT
1122 removed from the input stream. */
1123 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1132 /* Catch specific keywords. Should be done with a data structure. */
1136 if (STREQN (tokstart, "boolean", 7))
1140 if (STREQN (tokstart, "double", 6))
1144 if (STREQN (tokstart, "short", 5))
1146 if (STREQN (tokstart, "false", 5))
1149 return BOOLEAN_LITERAL;
1151 if (STREQN (tokstart, "super", 5))
1153 if (STREQN (tokstart, "float", 5))
1157 if (STREQN (tokstart, "long", 4))
1159 if (STREQN (tokstart, "byte", 4))
1161 if (STREQN (tokstart, "char", 4))
1163 if (STREQN (tokstart, "true", 4))
1166 return BOOLEAN_LITERAL;
1168 if (current_language->la_language == language_cplus
1169 && STREQN (tokstart, "this", 4))
1171 static const char this_name[] =
1172 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1174 if (lookup_symbol (this_name, expression_context_block,
1175 VAR_NAMESPACE, (int *) NULL,
1176 (struct symtab **) NULL))
1181 if (STREQN (tokstart, "int", 3))
1183 if (STREQN (tokstart, "new", 3))
1190 yylval.sval.ptr = tokstart;
1191 yylval.sval.length = namelen;
1193 if (*tokstart == '$')
1195 write_dollar_variable (yylval.sval);
1199 /* Input names that aren't symbols but ARE valid hex numbers,
1200 when the input radix permits them, can be names or numbers
1201 depending on the parse. Note we support radixes > 16 here. */
1202 if (((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1203 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1205 YYSTYPE newlval; /* Its value is ignored. */
1206 int hextype = parse_number (tokstart, namelen, 0, &newlval);
1207 if (hextype == INTEGER_LITERAL)
1218 lexptr = prev_lexptr;
1221 error (_("%s: near `%s'"), msg, lexptr);
1223 error (_("error in expression, near `%s'"), lexptr);
1226 static struct type *
1227 java_type_from_name (name)
1231 char *tmp = copy_name (name);
1232 struct type *typ = java_lookup_class (tmp);
1233 if (typ == NULL || TYPE_CODE (typ) != TYPE_CODE_STRUCT)
1234 error (_("No class named `%s'"), tmp);
1238 /* If NAME is a valid variable name in this scope, push it and return 1.
1239 Otherwise, return 0. */
1242 push_variable (name)
1246 char *tmp = copy_name (name);
1247 int is_a_field_of_this = 0;
1249 sym = lookup_symbol (tmp, expression_context_block, VAR_NAMESPACE,
1250 &is_a_field_of_this, (struct symtab **) NULL);
1251 if (sym && SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1253 if (symbol_read_needs_frame (sym))
1255 if (innermost_block == 0 ||
1256 contained_in (block_found, innermost_block))
1257 innermost_block = block_found;
1260 write_exp_elt_opcode (OP_VAR_VALUE);
1261 /* We want to use the selected frame, not another more inner frame
1262 which happens to be in the same block. */
1263 write_exp_elt_block (NULL);
1264 write_exp_elt_sym (sym);
1265 write_exp_elt_opcode (OP_VAR_VALUE);
1268 if (is_a_field_of_this)
1270 /* it hangs off of `this'. Must not inadvertently convert from a
1271 method call to data ref. */
1272 if (innermost_block == 0 ||
1273 contained_in (block_found, innermost_block))
1274 innermost_block = block_found;
1275 write_exp_elt_opcode (OP_THIS);
1276 write_exp_elt_opcode (OP_THIS);
1277 write_exp_elt_opcode (STRUCTOP_PTR);
1278 write_exp_string (name);
1279 write_exp_elt_opcode (STRUCTOP_PTR);
1285 /* Assuming a reference expression has been pushed, emit the
1286 STRUCTOP_STRUCT ops to access the field named NAME. If NAME is a
1287 qualified name (has '.'), generate a field access for each part. */
1290 push_fieldnames (name)
1294 struct stoken token;
1295 token.ptr = name.ptr;
1298 if (i == name.length || name.ptr[i] == '.')
1300 /* token.ptr is start of current field name. */
1301 token.length = &name.ptr[i] - token.ptr;
1302 write_exp_elt_opcode (STRUCTOP_STRUCT);
1303 write_exp_string (token);
1304 write_exp_elt_opcode (STRUCTOP_STRUCT);
1305 token.ptr += token.length + 1;
1307 if (i >= name.length)
1312 /* Helper routine for push_expression_name.
1313 Handle a qualified name, where DOT_INDEX is the index of the first '.' */
1316 push_qualified_expression_name (name, dot_index)
1320 struct stoken token;
1324 token.ptr = name.ptr;
1325 token.length = dot_index;
1327 if (push_variable (token))
1329 token.ptr = name.ptr + dot_index + 1;
1330 token.length = name.length - dot_index - 1;
1331 push_fieldnames (token);
1335 token.ptr = name.ptr;
1338 token.length = dot_index;
1339 tmp = copy_name (token);
1340 typ = java_lookup_class (tmp);
1343 if (dot_index == name.length)
1345 write_exp_elt_opcode(OP_TYPE);
1346 write_exp_elt_type(typ);
1347 write_exp_elt_opcode(OP_TYPE);
1350 dot_index++; /* Skip '.' */
1351 name.ptr += dot_index;
1352 name.length -= dot_index;
1354 while (dot_index < name.length && name.ptr[dot_index] != '.')
1356 token.ptr = name.ptr;
1357 token.length = dot_index;
1358 write_exp_elt_opcode (OP_SCOPE);
1359 write_exp_elt_type (typ);
1360 write_exp_string (token);
1361 write_exp_elt_opcode (OP_SCOPE);
1362 if (dot_index < name.length)
1365 name.ptr += dot_index;
1366 name.length -= dot_index;
1367 push_fieldnames (name);
1371 else if (dot_index >= name.length)
1373 dot_index++; /* Skip '.' */
1374 while (dot_index < name.length && name.ptr[dot_index] != '.')
1377 error (_("unknown type `%.*s'"), name.length, name.ptr);
1380 /* Handle Name in an expression (or LHS).
1381 Handle VAR, TYPE, TYPE.FIELD1....FIELDN and VAR.FIELD1....FIELDN. */
1384 push_expression_name (name)
1392 for (i = 0; i < name.length; i++)
1394 if (name.ptr[i] == '.')
1396 /* It's a Qualified Expression Name. */
1397 push_qualified_expression_name (name, i);
1402 /* It's a Simple Expression Name. */
1404 if (push_variable (name))
1406 tmp = copy_name (name);
1407 typ = java_lookup_class (tmp);
1410 write_exp_elt_opcode(OP_TYPE);
1411 write_exp_elt_type(typ);
1412 write_exp_elt_opcode(OP_TYPE);
1416 struct minimal_symbol *msymbol;
1418 msymbol = lookup_minimal_symbol (tmp, NULL, NULL);
1419 if (msymbol != NULL)
1421 write_exp_msymbol (msymbol,
1422 lookup_function_type (builtin_type_int),
1425 else if (!have_full_symbols () && !have_partial_symbols ())
1426 error (_("No symbol table is loaded. Use the \"file\" command"));
1428 error (_("No symbol \"%s\" in current context"), tmp);
1434 /* The following two routines, copy_exp and insert_exp, aren't specific to
1435 Java, so they could go in parse.c, but their only purpose is to support
1436 the parsing kludges we use in this file, so maybe it's best to isolate
1439 /* Copy the expression whose last element is at index ENDPOS - 1 in EXPR
1440 into a freshly malloc'ed struct expression. Its language_defn is set
1442 static struct expression *
1443 copy_exp (expr, endpos)
1444 struct expression *expr;
1447 int len = length_of_subexp (expr, endpos);
1448 struct expression *new
1449 = (struct expression *) malloc (sizeof (*new) + EXP_ELEM_TO_BYTES (len));
1451 memcpy (new->elts, expr->elts + endpos - len, EXP_ELEM_TO_BYTES (len));
1452 new->language_defn = 0;
1457 /* Insert the expression NEW into the current expression (expout) at POS. */
1459 insert_exp (pos, new)
1461 struct expression *new;
1463 int newlen = new->nelts;
1465 /* Grow expout if necessary. In this function's only use at present,
1466 this should never be necessary. */
1467 if (expout_ptr + newlen > expout_size)
1469 expout_size = max (expout_size * 2, expout_ptr + newlen + 10);
1470 expout = (struct expression *)
1471 realloc ((char *) expout, (sizeof (struct expression)
1472 + EXP_ELEM_TO_BYTES (expout_size)));
1478 for (i = expout_ptr - 1; i >= pos; i--)
1479 expout->elts[i + newlen] = expout->elts[i];
1482 memcpy (expout->elts + pos, new->elts, EXP_ELEM_TO_BYTES (newlen));
1483 expout_ptr += newlen;