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 0 /* Default to no yydebug support */
104 static int yylex (void);
106 void yyerror (char *);
108 static struct type *java_type_from_name (struct stoken);
109 static void push_expression_name (struct stoken);
110 static void push_fieldnames (struct stoken);
112 static struct expression *copy_exp (struct expression *, int);
113 static void insert_exp (int, struct expression *);
117 /* Although the yacc "value" of an expression is not used,
118 since the result is stored in the structure being created,
119 other node types do have values. */
136 struct symtoken ssym;
138 enum exp_opcode opcode;
139 struct internalvar *ivar;
144 /* YYSTYPE gets defined by %union */
145 static int parse_number (char *, int, int, YYSTYPE *);
148 %type <lval> rcurly Dims Dims_opt
149 %type <tval> ClassOrInterfaceType ClassType /* ReferenceType Type ArrayType */
150 %type <tval> IntegralType FloatingPointType NumericType PrimitiveType ArrayType PrimitiveOrArrayType
152 %token <typed_val_int> INTEGER_LITERAL
153 %token <typed_val_float> FLOATING_POINT_LITERAL
155 %token <sval> IDENTIFIER
156 %token <sval> STRING_LITERAL
157 %token <lval> BOOLEAN_LITERAL
158 %token <tsym> TYPENAME
159 %type <sval> Name SimpleName QualifiedName ForcedName
161 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
162 but which would parse as a valid number in the current input radix.
163 E.g. "c" when input_radix==16. Depending on the parse, it will be
164 turned into a name or into a number. */
166 %token <sval> NAME_OR_INT
170 /* Special type cases, put in to allow the parser to distinguish different
172 %token LONG SHORT BYTE INT CHAR BOOLEAN DOUBLE FLOAT
176 %token <opcode> ASSIGN_MODIFY
178 %token THIS SUPER NEW
181 %right '=' ASSIGN_MODIFY
189 %left '<' '>' LEQ GEQ
193 %right INCREMENT DECREMENT
203 type_exp: PrimitiveOrArrayType
205 write_exp_elt_opcode(OP_TYPE);
206 write_exp_elt_type($1);
207 write_exp_elt_opcode(OP_TYPE);
211 PrimitiveOrArrayType:
219 write_exp_elt_opcode (OP_STRING);
220 write_exp_string ($1);
221 write_exp_elt_opcode (OP_STRING);
227 { write_exp_elt_opcode (OP_LONG);
228 write_exp_elt_type ($1.type);
229 write_exp_elt_longcst ((LONGEST)($1.val));
230 write_exp_elt_opcode (OP_LONG); }
233 parse_number ($1.ptr, $1.length, 0, &val);
234 write_exp_elt_opcode (OP_LONG);
235 write_exp_elt_type (val.typed_val_int.type);
236 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
237 write_exp_elt_opcode (OP_LONG);
239 | FLOATING_POINT_LITERAL
240 { write_exp_elt_opcode (OP_DOUBLE);
241 write_exp_elt_type ($1.type);
242 write_exp_elt_dblcst ($1.dval);
243 write_exp_elt_opcode (OP_DOUBLE); }
245 { write_exp_elt_opcode (OP_LONG);
246 write_exp_elt_type (java_boolean_type);
247 write_exp_elt_longcst ((LONGEST)$1);
248 write_exp_elt_opcode (OP_LONG); }
262 { $$ = java_boolean_type; }
272 { $$ = java_byte_type; }
274 { $$ = java_short_type; }
276 { $$ = java_int_type; }
278 { $$ = java_long_type; }
280 { $$ = java_char_type; }
285 { $$ = java_float_type; }
287 { $$ = java_double_type; }
297 ClassOrInterfaceType:
299 { $$ = java_type_from_name ($1); }
308 { $$ = java_array_type ($1, $2); }
310 { $$ = java_array_type (java_type_from_name ($1), $2); }
330 { $$.length = $1.length + $3.length + 1;
331 if ($1.ptr + $1.length + 1 == $3.ptr
332 && $1.ptr[$1.length] == '.')
333 $$.ptr = $1.ptr; /* Optimization. */
336 $$.ptr = (char *) malloc ($$.length + 1);
337 make_cleanup (free, $$.ptr);
338 sprintf ($$.ptr, "%.*s.%.*s",
339 $1.length, $1.ptr, $3.length, $3.ptr);
345 { write_exp_elt_opcode(OP_TYPE);
346 write_exp_elt_type($1);
347 write_exp_elt_opcode(OP_TYPE);}
351 /* Expressions, including the comma operator. */
353 | exp1 ',' Expression
354 { write_exp_elt_opcode (BINOP_COMMA); }
359 | ArrayCreationExpression
365 { write_exp_elt_opcode (OP_THIS);
366 write_exp_elt_opcode (OP_THIS); }
368 | ClassInstanceCreationExpression
372 | lcurly ArgumentList rcurly
373 { write_exp_elt_opcode (OP_ARRAY);
374 write_exp_elt_longcst ((LONGEST) 0);
375 write_exp_elt_longcst ((LONGEST) $3);
376 write_exp_elt_opcode (OP_ARRAY); }
381 { start_arglist (); }
386 { $$ = end_arglist () - 1; }
389 ClassInstanceCreationExpression:
390 NEW ClassType '(' ArgumentList_opt ')'
391 { error ("FIXME - ClassInstanceCreationExpression"); }
397 | ArgumentList ',' Expression
407 ArrayCreationExpression:
408 NEW PrimitiveType DimExprs Dims_opt
409 { error ("FIXME - ArrayCreatiionExpression"); }
410 | NEW ClassOrInterfaceType DimExprs Dims_opt
411 { error ("FIXME - ArrayCreatiionExpression"); }
437 Primary '.' SimpleName
438 { push_fieldnames ($3); }
439 | VARIABLE '.' SimpleName
440 { push_fieldnames ($3); }
441 /*| SUPER '.' SimpleName { FIXME } */
445 Name '(' ArgumentList_opt ')'
446 { error ("method invocation not implemented"); }
447 | Primary '.' SimpleName '(' ArgumentList_opt ')'
448 { error ("method invocation not implemented"); }
449 | SUPER '.' SimpleName '(' ArgumentList_opt ')'
450 { error ("method invocation not implemented"); }
454 Name '[' Expression ']'
456 /* Emit code for the Name now, then exchange it in the
457 expout array with the Expression's code. We could
458 introduce a OP_SWAP code or a reversed version of
459 BINOP_SUBSCRIPT, but that makes the rest of GDB pay
460 for our parsing kludges. */
461 struct expression *name_expr;
463 push_expression_name ($1);
464 name_expr = copy_exp (expout, expout_ptr);
465 expout_ptr -= name_expr->nelts;
466 insert_exp (expout_ptr-length_of_subexp (expout, expout_ptr),
469 write_exp_elt_opcode (BINOP_SUBSCRIPT);
471 | VARIABLE '[' Expression ']'
472 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
473 | PrimaryNoNewArray '[' Expression ']'
474 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
480 { push_expression_name ($1); }
482 /* Already written by write_dollar_variable. */
483 | PostIncrementExpression
484 | PostDecrementExpression
487 PostIncrementExpression:
488 PostfixExpression INCREMENT
489 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
492 PostDecrementExpression:
493 PostfixExpression DECREMENT
494 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
498 PreIncrementExpression
499 | PreDecrementExpression
500 | '+' UnaryExpression
501 | '-' UnaryExpression
502 { write_exp_elt_opcode (UNOP_NEG); }
503 | '*' UnaryExpression
504 { write_exp_elt_opcode (UNOP_IND); } /*FIXME not in Java */
505 | UnaryExpressionNotPlusMinus
508 PreIncrementExpression:
509 INCREMENT UnaryExpression
510 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
513 PreDecrementExpression:
514 DECREMENT UnaryExpression
515 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
518 UnaryExpressionNotPlusMinus:
520 | '~' UnaryExpression
521 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
522 | '!' UnaryExpression
523 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
528 '(' PrimitiveType Dims_opt ')' UnaryExpression
529 { write_exp_elt_opcode (UNOP_CAST);
530 write_exp_elt_type (java_array_type ($2, $3));
531 write_exp_elt_opcode (UNOP_CAST); }
532 | '(' Expression ')' UnaryExpressionNotPlusMinus
534 int exp_size = expout_ptr;
535 int last_exp_size = length_of_subexp(expout, expout_ptr);
538 int base = expout_ptr - last_exp_size - 3;
539 if (base < 0 || expout->elts[base+2].opcode != OP_TYPE)
540 error ("invalid cast expression");
541 type = expout->elts[base+1].type;
542 /* Remove the 'Expression' and slide the
543 UnaryExpressionNotPlusMinus down to replace it. */
544 for (i = 0; i < last_exp_size; i++)
545 expout->elts[base + i] = expout->elts[base + i + 3];
547 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
548 type = lookup_pointer_type (type);
549 write_exp_elt_opcode (UNOP_CAST);
550 write_exp_elt_type (type);
551 write_exp_elt_opcode (UNOP_CAST);
553 | '(' Name Dims ')' UnaryExpressionNotPlusMinus
554 { write_exp_elt_opcode (UNOP_CAST);
555 write_exp_elt_type (java_array_type (java_type_from_name ($2), $3));
556 write_exp_elt_opcode (UNOP_CAST); }
560 MultiplicativeExpression:
562 | MultiplicativeExpression '*' UnaryExpression
563 { write_exp_elt_opcode (BINOP_MUL); }
564 | MultiplicativeExpression '/' UnaryExpression
565 { write_exp_elt_opcode (BINOP_DIV); }
566 | MultiplicativeExpression '%' UnaryExpression
567 { write_exp_elt_opcode (BINOP_REM); }
571 MultiplicativeExpression
572 | AdditiveExpression '+' MultiplicativeExpression
573 { write_exp_elt_opcode (BINOP_ADD); }
574 | AdditiveExpression '-' MultiplicativeExpression
575 { write_exp_elt_opcode (BINOP_SUB); }
580 | ShiftExpression LSH AdditiveExpression
581 { write_exp_elt_opcode (BINOP_LSH); }
582 | ShiftExpression RSH AdditiveExpression
583 { write_exp_elt_opcode (BINOP_RSH); }
584 /* | ShiftExpression >>> AdditiveExpression { FIXME } */
587 RelationalExpression:
589 | RelationalExpression '<' ShiftExpression
590 { write_exp_elt_opcode (BINOP_LESS); }
591 | RelationalExpression '>' ShiftExpression
592 { write_exp_elt_opcode (BINOP_GTR); }
593 | RelationalExpression LEQ ShiftExpression
594 { write_exp_elt_opcode (BINOP_LEQ); }
595 | RelationalExpression GEQ ShiftExpression
596 { write_exp_elt_opcode (BINOP_GEQ); }
597 /* | RelationalExpresion INSTANCEOF ReferenceType { FIXME } */
602 | EqualityExpression EQUAL RelationalExpression
603 { write_exp_elt_opcode (BINOP_EQUAL); }
604 | EqualityExpression NOTEQUAL RelationalExpression
605 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
610 | AndExpression '&' EqualityExpression
611 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
614 ExclusiveOrExpression:
616 | ExclusiveOrExpression '^' AndExpression
617 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
619 InclusiveOrExpression:
620 ExclusiveOrExpression
621 | InclusiveOrExpression '|' ExclusiveOrExpression
622 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
625 ConditionalAndExpression:
626 InclusiveOrExpression
627 | ConditionalAndExpression ANDAND InclusiveOrExpression
628 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
631 ConditionalOrExpression:
632 ConditionalAndExpression
633 | ConditionalOrExpression OROR ConditionalAndExpression
634 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
637 ConditionalExpression:
638 ConditionalOrExpression
639 | ConditionalOrExpression '?' Expression ':' ConditionalExpression
640 { write_exp_elt_opcode (TERNOP_COND); }
643 AssignmentExpression:
644 ConditionalExpression
649 LeftHandSide '=' ConditionalExpression
650 { write_exp_elt_opcode (BINOP_ASSIGN); }
651 | LeftHandSide ASSIGN_MODIFY ConditionalExpression
652 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
653 write_exp_elt_opcode ($2);
654 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
659 { push_expression_name ($1); }
661 /* Already written by write_dollar_variable. */
672 /* Take care of parsing a number (anything that starts with a digit).
673 Set yylval and return the token type; update lexptr.
674 LEN is the number of characters in it. */
676 /*** Needs some error checking for the float case ***/
679 parse_number (p, len, parsed_float, putithere)
685 register ULONGEST n = 0;
686 ULONGEST limit, limit_div_base;
689 register int base = input_radix;
695 /* It's a float since it contains a point or an exponent. */
697 int num = 0; /* number of tokens scanned by scanf */
698 char saved_char = p[len];
700 p[len] = 0; /* null-terminate the token */
701 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
702 num = sscanf (p, "%g%c", (float *) &putithere->typed_val_float.dval, &c);
703 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
704 num = sscanf (p, "%lg%c", (double *) &putithere->typed_val_float.dval, &c);
707 #ifdef SCANF_HAS_LONG_DOUBLE
708 num = sscanf (p, "%Lg%c", &putithere->typed_val_float.dval, &c);
710 /* Scan it into a double, then assign it to the long double.
711 This at least wins with values representable in the range
714 num = sscanf (p, "%lg%c", &temp, &c);
715 putithere->typed_val_float.dval = temp;
718 p[len] = saved_char; /* restore the input stream */
719 if (num != 1) /* check scanf found ONLY a float ... */
721 /* See if it has `f' or `d' suffix (float or double). */
723 c = tolower (p[len - 1]);
725 if (c == 'f' || c == 'F')
726 putithere->typed_val_float.type = builtin_type_float;
727 else if (isdigit (c) || c == '.' || c == 'd' || c == 'D')
728 putithere->typed_val_float.type = builtin_type_double;
732 return FLOATING_POINT_LITERAL;
735 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
767 /* A paranoid calculation of (1<<64)-1. */
768 limit = (ULONGEST)0xffffffff;
769 limit = ((limit << 16) << 16) | limit;
770 if (c == 'l' || c == 'L')
772 type = java_long_type;
777 type = java_int_type;
779 limit_div_base = limit / (ULONGEST) base;
784 if (c >= '0' && c <= '9')
786 else if (c >= 'A' && c <= 'Z')
788 else if (c >= 'a' && c <= 'z')
791 return ERROR; /* Char not a digit */
794 if (n > limit_div_base
795 || (n *= base) > limit - c)
796 error ("Numeric constant too large.");
800 /* If the type is bigger than a 32-bit signed integer can be, implicitly
801 promote to long. Java does not do this, so mark it as builtin_type_uint64
802 rather than java_long_type. 0x80000000 will become -0x80000000 instead
803 of 0x80000000L, because we don't know the sign at this point.
805 if (type == java_int_type && n > (ULONGEST)0x80000000)
806 type = builtin_type_uint64;
808 putithere->typed_val_int.val = n;
809 putithere->typed_val_int.type = type;
811 return INTEGER_LITERAL;
818 enum exp_opcode opcode;
821 static const struct token tokentab3[] =
823 {">>=", ASSIGN_MODIFY, BINOP_RSH},
824 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
827 static const struct token tokentab2[] =
829 {"+=", ASSIGN_MODIFY, BINOP_ADD},
830 {"-=", ASSIGN_MODIFY, BINOP_SUB},
831 {"*=", ASSIGN_MODIFY, BINOP_MUL},
832 {"/=", ASSIGN_MODIFY, BINOP_DIV},
833 {"%=", ASSIGN_MODIFY, BINOP_REM},
834 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
835 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
836 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
837 {"++", INCREMENT, BINOP_END},
838 {"--", DECREMENT, BINOP_END},
839 {"&&", ANDAND, BINOP_END},
840 {"||", OROR, BINOP_END},
841 {"<<", LSH, BINOP_END},
842 {">>", RSH, BINOP_END},
843 {"==", EQUAL, BINOP_END},
844 {"!=", NOTEQUAL, BINOP_END},
845 {"<=", LEQ, BINOP_END},
846 {">=", GEQ, BINOP_END}
849 /* Read one token, getting characters through lexptr. */
860 static char *tempbuf;
861 static int tempbufsize;
866 /* See if it is a special token of length 3. */
867 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
868 if (STREQN (tokstart, tokentab3[i].operator, 3))
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 (STREQN (tokstart, tokentab2[i].operator, 2))
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;
963 register char *p = tokstart;
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 (STREQN (tokstart, "boolean", 7))
1133 if (STREQN (tokstart, "double", 6))
1137 if (STREQN (tokstart, "short", 5))
1139 if (STREQN (tokstart, "false", 5))
1142 return BOOLEAN_LITERAL;
1144 if (STREQN (tokstart, "super", 5))
1146 if (STREQN (tokstart, "float", 5))
1150 if (STREQN (tokstart, "long", 4))
1152 if (STREQN (tokstart, "byte", 4))
1154 if (STREQN (tokstart, "char", 4))
1156 if (STREQN (tokstart, "true", 4))
1159 return BOOLEAN_LITERAL;
1161 if (current_language->la_language == language_cplus
1162 && STREQN (tokstart, "this", 4))
1164 static const char this_name[] =
1165 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1167 if (lookup_symbol (this_name, expression_context_block,
1168 VAR_NAMESPACE, (int *) NULL,
1169 (struct symtab **) NULL))
1174 if (STREQN (tokstart, "int", 3))
1176 if (STREQN (tokstart, "new", 3))
1183 yylval.sval.ptr = tokstart;
1184 yylval.sval.length = namelen;
1186 if (*tokstart == '$')
1188 write_dollar_variable (yylval.sval);
1192 /* Input names that aren't symbols but ARE valid hex numbers,
1193 when the input radix permits them, can be names or numbers
1194 depending on the parse. Note we support radixes > 16 here. */
1195 if (((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1196 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1198 YYSTYPE newlval; /* Its value is ignored. */
1199 int hextype = parse_number (tokstart, namelen, 0, &newlval);
1200 if (hextype == INTEGER_LITERAL)
1210 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
1213 static struct type *
1214 java_type_from_name (name)
1218 char *tmp = copy_name (name);
1219 struct type *typ = java_lookup_class (tmp);
1220 if (typ == NULL || TYPE_CODE (typ) != TYPE_CODE_STRUCT)
1221 error ("No class named %s.", tmp);
1225 /* If NAME is a valid variable name in this scope, push it and return 1.
1226 Otherwise, return 0. */
1229 push_variable (name)
1233 char *tmp = copy_name (name);
1234 int is_a_field_of_this = 0;
1236 sym = lookup_symbol (tmp, expression_context_block, VAR_NAMESPACE,
1237 &is_a_field_of_this, (struct symtab **) NULL);
1238 if (sym && SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1240 if (symbol_read_needs_frame (sym))
1242 if (innermost_block == 0 ||
1243 contained_in (block_found, innermost_block))
1244 innermost_block = block_found;
1247 write_exp_elt_opcode (OP_VAR_VALUE);
1248 /* We want to use the selected frame, not another more inner frame
1249 which happens to be in the same block. */
1250 write_exp_elt_block (NULL);
1251 write_exp_elt_sym (sym);
1252 write_exp_elt_opcode (OP_VAR_VALUE);
1255 if (is_a_field_of_this)
1257 /* it hangs off of `this'. Must not inadvertently convert from a
1258 method call to data ref. */
1259 if (innermost_block == 0 ||
1260 contained_in (block_found, innermost_block))
1261 innermost_block = block_found;
1262 write_exp_elt_opcode (OP_THIS);
1263 write_exp_elt_opcode (OP_THIS);
1264 write_exp_elt_opcode (STRUCTOP_PTR);
1265 write_exp_string (name);
1266 write_exp_elt_opcode (STRUCTOP_PTR);
1272 /* Assuming a reference expression has been pushed, emit the
1273 STRUCTOP_STRUCT ops to access the field named NAME. If NAME is a
1274 qualified name (has '.'), generate a field access for each part. */
1277 push_fieldnames (name)
1281 struct stoken token;
1282 token.ptr = name.ptr;
1285 if (i == name.length || name.ptr[i] == '.')
1287 /* token.ptr is start of current field name. */
1288 token.length = &name.ptr[i] - token.ptr;
1289 write_exp_elt_opcode (STRUCTOP_STRUCT);
1290 write_exp_string (token);
1291 write_exp_elt_opcode (STRUCTOP_STRUCT);
1292 token.ptr += token.length + 1;
1294 if (i >= name.length)
1299 /* Helper routine for push_expression_name.
1300 Handle a qualified name, where DOT_INDEX is the index of the first '.' */
1303 push_qualified_expression_name (name, dot_index)
1307 struct stoken token;
1311 token.ptr = name.ptr;
1312 token.length = dot_index;
1314 if (push_variable (token))
1316 token.ptr = name.ptr + dot_index + 1;
1317 token.length = name.length - dot_index - 1;
1318 push_fieldnames (token);
1322 token.ptr = name.ptr;
1325 token.length = dot_index;
1326 tmp = copy_name (token);
1327 typ = java_lookup_class (tmp);
1330 if (dot_index == name.length)
1332 write_exp_elt_opcode(OP_TYPE);
1333 write_exp_elt_type(typ);
1334 write_exp_elt_opcode(OP_TYPE);
1337 dot_index++; /* Skip '.' */
1338 name.ptr += dot_index;
1339 name.length -= dot_index;
1341 while (dot_index < name.length && name.ptr[dot_index] != '.')
1343 token.ptr = name.ptr;
1344 token.length = dot_index;
1345 write_exp_elt_opcode (OP_SCOPE);
1346 write_exp_elt_type (typ);
1347 write_exp_string (token);
1348 write_exp_elt_opcode (OP_SCOPE);
1349 if (dot_index < name.length)
1352 name.ptr += dot_index;
1353 name.length -= dot_index;
1354 push_fieldnames (name);
1358 else if (dot_index >= name.length)
1360 dot_index++; /* Skip '.' */
1361 while (dot_index < name.length && name.ptr[dot_index] != '.')
1364 error ("unknown type `%.*s'", name.length, name.ptr);
1367 /* Handle Name in an expression (or LHS).
1368 Handle VAR, TYPE, TYPE.FIELD1....FIELDN and VAR.FIELD1....FIELDN. */
1371 push_expression_name (name)
1379 for (i = 0; i < name.length; i++)
1381 if (name.ptr[i] == '.')
1383 /* It's a Qualified Expression Name. */
1384 push_qualified_expression_name (name, i);
1389 /* It's a Simple Expression Name. */
1391 if (push_variable (name))
1393 tmp = copy_name (name);
1394 typ = java_lookup_class (tmp);
1397 write_exp_elt_opcode(OP_TYPE);
1398 write_exp_elt_type(typ);
1399 write_exp_elt_opcode(OP_TYPE);
1403 struct minimal_symbol *msymbol;
1405 msymbol = lookup_minimal_symbol (tmp, NULL, NULL);
1406 if (msymbol != NULL)
1408 write_exp_msymbol (msymbol,
1409 lookup_function_type (builtin_type_int),
1412 else if (!have_full_symbols () && !have_partial_symbols ())
1413 error ("No symbol table is loaded. Use the \"file\" command.");
1415 error ("No symbol \"%s\" in current context.", tmp);
1421 /* The following two routines, copy_exp and insert_exp, aren't specific to
1422 Java, so they could go in parse.c, but their only purpose is to support
1423 the parsing kludges we use in this file, so maybe it's best to isolate
1426 /* Copy the expression whose last element is at index ENDPOS - 1 in EXPR
1427 into a freshly malloc'ed struct expression. Its language_defn is set
1429 static struct expression *
1430 copy_exp (expr, endpos)
1431 struct expression *expr;
1434 int len = length_of_subexp (expr, endpos);
1435 struct expression *new
1436 = (struct expression *) malloc (sizeof (*new) + EXP_ELEM_TO_BYTES (len));
1438 memcpy (new->elts, expr->elts + endpos - len, EXP_ELEM_TO_BYTES (len));
1439 new->language_defn = 0;
1444 /* Insert the expression NEW into the current expression (expout) at POS. */
1446 insert_exp (pos, new)
1448 struct expression *new;
1450 int newlen = new->nelts;
1452 /* Grow expout if necessary. In this function's only use at present,
1453 this should never be necessary. */
1454 if (expout_ptr + newlen > expout_size)
1456 expout_size = max (expout_size * 2, expout_ptr + newlen + 10);
1457 expout = (struct expression *)
1458 realloc ((char *) expout, (sizeof (struct expression)
1459 + EXP_ELEM_TO_BYTES (expout_size)));
1465 for (i = expout_ptr - 1; i >= pos; i--)
1466 expout->elts[i + newlen] = expout->elts[i];
1469 memcpy (expout->elts + pos, new->elts, EXP_ELEM_TO_BYTES (newlen));
1470 expout_ptr += newlen;