1 /* YACC parser for Java expressions, for GDB.
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 */
103 yyparse PARAMS ((void));
106 yylex PARAMS ((void));
109 yyerror PARAMS ((char *));
111 static struct type * java_type_from_name PARAMS ((struct stoken));
112 static void push_expression_name PARAMS ((struct stoken));
113 static void push_fieldnames PARAMS ((struct stoken));
115 static struct expression *copy_exp PARAMS ((struct expression *, int));
116 static void insert_exp PARAMS ((int, struct expression *));
120 /* Although the yacc "value" of an expression is not used,
121 since the result is stored in the structure being created,
122 other node types do have values. */
139 struct symtoken ssym;
141 enum exp_opcode opcode;
142 struct internalvar *ivar;
147 /* YYSTYPE gets defined by %union */
149 parse_number PARAMS ((char *, int, int, YYSTYPE *));
152 %type <lval> rcurly Dims Dims_opt
153 %type <tval> ClassOrInterfaceType ClassType /* ReferenceType Type ArrayType */
154 %type <tval> IntegralType FloatingPointType NumericType PrimitiveType
156 %token <typed_val_int> INTEGER_LITERAL
157 %token <typed_val_float> FLOATING_POINT_LITERAL
159 %token <sval> IDENTIFIER
160 %token <sval> STRING_LITERAL
161 %token <lval> BOOLEAN_LITERAL
162 %token <tsym> TYPENAME
163 %type <sval> Name SimpleName QualifiedName ForcedName
165 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
166 but which would parse as a valid number in the current input radix.
167 E.g. "c" when input_radix==16. Depending on the parse, it will be
168 turned into a name or into a number. */
170 %token <sval> NAME_OR_INT
174 /* Special type cases, put in to allow the parser to distinguish different
176 %token LONG SHORT BYTE INT CHAR BOOLEAN DOUBLE FLOAT
180 %token <opcode> ASSIGN_MODIFY
182 %token THIS SUPER NEW
185 %right '=' ASSIGN_MODIFY
193 %left '<' '>' LEQ GEQ
197 %right INCREMENT DECREMENT
204 /* | type_exp FIXME */
210 write_exp_elt_opcode (OP_STRING);
211 write_exp_string ($1);
212 write_exp_elt_opcode (OP_STRING);
218 { write_exp_elt_opcode (OP_LONG);
219 write_exp_elt_type ($1.type);
220 write_exp_elt_longcst ((LONGEST)($1.val));
221 write_exp_elt_opcode (OP_LONG); }
224 parse_number ($1.ptr, $1.length, 0, &val);
225 write_exp_elt_opcode (OP_LONG);
226 write_exp_elt_type (val.typed_val_int.type);
227 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
228 write_exp_elt_opcode (OP_LONG);
230 | FLOATING_POINT_LITERAL
231 { write_exp_elt_opcode (OP_DOUBLE);
232 write_exp_elt_type ($1.type);
233 write_exp_elt_dblcst ($1.dval);
234 write_exp_elt_opcode (OP_DOUBLE); }
236 { write_exp_elt_opcode (OP_LONG);
237 write_exp_elt_type (java_boolean_type);
238 write_exp_elt_longcst ((LONGEST)$1);
239 write_exp_elt_opcode (OP_LONG); }
253 { $$ = java_boolean_type; }
263 { $$ = java_byte_type; }
265 { $$ = java_short_type; }
267 { $$ = java_int_type; }
269 { $$ = java_long_type; }
271 { $$ = java_char_type; }
276 { $$ = java_float_type; }
278 { $$ = java_double_type; }
288 ClassOrInterfaceType:
290 { $$ = java_type_from_name ($1); }
300 { $$ = java_array_type ($1, $2); }
302 { $$ = java_array_type (java_type_from_name ($1), $2); }
323 { $$.length = $1.length + $3.length + 1;
324 if ($1.ptr + $1.length + 1 == $3.ptr
325 && $1.ptr[$1.length] == '.')
326 $$.ptr = $1.ptr; /* Optimization. */
329 $$.ptr = (char *) malloc ($$.length + 1);
330 make_cleanup (free, $$.ptr);
331 sprintf ($$.ptr, "%.*s.%.*s",
332 $1.length, $1.ptr, $3.length, $3.ptr);
338 { write_exp_elt_opcode(OP_TYPE);
339 write_exp_elt_type($1);
340 write_exp_elt_opcode(OP_TYPE);}
344 /* Expressions, including the comma operator. */
346 | exp1 ',' Expression
347 { write_exp_elt_opcode (BINOP_COMMA); }
352 | ArrayCreationExpression
358 { write_exp_elt_opcode (OP_THIS);
359 write_exp_elt_opcode (OP_THIS); }
361 | ClassInstanceCreationExpression
365 | lcurly ArgumentList rcurly
366 { write_exp_elt_opcode (OP_ARRAY);
367 write_exp_elt_longcst ((LONGEST) 0);
368 write_exp_elt_longcst ((LONGEST) $3);
369 write_exp_elt_opcode (OP_ARRAY); }
374 { start_arglist (); }
379 { $$ = end_arglist () - 1; }
382 ClassInstanceCreationExpression:
383 NEW ClassType '(' ArgumentList_opt ')'
384 { error ("FIXME - ClassInstanceCreationExpression"); }
390 | ArgumentList ',' Expression
400 ArrayCreationExpression:
401 NEW PrimitiveType DimExprs Dims_opt
402 { error ("FIXME - ArrayCreatiionExpression"); }
403 | NEW ClassOrInterfaceType DimExprs Dims_opt
404 { error ("FIXME - ArrayCreatiionExpression"); }
430 Primary '.' SimpleName
431 { push_fieldnames ($3); }
432 | VARIABLE '.' SimpleName
433 { push_fieldnames ($3); }
434 /*| SUPER '.' SimpleName { FIXME } */
438 Name '(' ArgumentList_opt ')'
439 { error ("method invocation not implemented"); }
440 | Primary '.' SimpleName '(' ArgumentList_opt ')'
441 { error ("method invocation not implemented"); }
442 | SUPER '.' SimpleName '(' ArgumentList_opt ')'
443 { error ("method invocation not implemented"); }
447 Name '[' Expression ']'
449 /* Emit code for the Name now, then exchange it in the
450 expout array with the Expression's code. We could
451 introduce a OP_SWAP code or a reversed version of
452 BINOP_SUBSCRIPT, but that makes the rest of GDB pay
453 for our parsing kludges. */
454 struct expression *name_expr;
456 push_expression_name ($1);
457 name_expr = copy_exp (expout, expout_ptr);
458 expout_ptr -= name_expr->nelts;
459 insert_exp (expout_ptr-length_of_subexp (expout, expout_ptr),
462 write_exp_elt_opcode (BINOP_SUBSCRIPT);
464 | VARIABLE '[' Expression ']'
465 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
466 | PrimaryNoNewArray '[' Expression ']'
467 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
473 { push_expression_name ($1); }
475 /* Already written by write_dollar_variable. */
476 | PostIncrementExpression
477 | PostDecrementExpression
480 PostIncrementExpression:
481 PostfixExpression INCREMENT
482 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
485 PostDecrementExpression:
486 PostfixExpression DECREMENT
487 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
491 PreIncrementExpression
492 | PreDecrementExpression
493 | '+' UnaryExpression
494 | '-' UnaryExpression
495 { write_exp_elt_opcode (UNOP_NEG); }
496 | '*' UnaryExpression
497 { write_exp_elt_opcode (UNOP_IND); } /*FIXME not in Java */
498 | UnaryExpressionNotPlusMinus
501 PreIncrementExpression:
502 INCREMENT UnaryExpression
503 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
506 PreDecrementExpression:
507 DECREMENT UnaryExpression
508 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
511 UnaryExpressionNotPlusMinus:
513 | '~' UnaryExpression
514 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
515 | '!' UnaryExpression
516 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
521 '(' PrimitiveType Dims_opt ')' UnaryExpression
522 { write_exp_elt_opcode (UNOP_CAST);
523 write_exp_elt_type (java_array_type ($2, $3));
524 write_exp_elt_opcode (UNOP_CAST); }
525 | '(' Expression ')' UnaryExpressionNotPlusMinus
527 int exp_size = expout_ptr;
528 int last_exp_size = length_of_subexp(expout, expout_ptr);
531 int base = expout_ptr - last_exp_size - 3;
532 if (base < 0 || expout->elts[base+2].opcode != OP_TYPE)
533 error ("invalid cast expression");
534 type = expout->elts[base+1].type;
535 /* Remove the 'Expression' and slide the
536 UnaryExpressionNotPlusMinus down to replace it. */
537 for (i = 0; i < last_exp_size; i++)
538 expout->elts[base + i] = expout->elts[base + i + 3];
540 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
541 type = lookup_pointer_type (type);
542 write_exp_elt_opcode (UNOP_CAST);
543 write_exp_elt_type (type);
544 write_exp_elt_opcode (UNOP_CAST);
546 | '(' Name Dims ')' UnaryExpressionNotPlusMinus
547 { write_exp_elt_opcode (UNOP_CAST);
548 write_exp_elt_type (java_array_type (java_type_from_name ($2), $3));
549 write_exp_elt_opcode (UNOP_CAST); }
553 MultiplicativeExpression:
555 | MultiplicativeExpression '*' UnaryExpression
556 { write_exp_elt_opcode (BINOP_MUL); }
557 | MultiplicativeExpression '/' UnaryExpression
558 { write_exp_elt_opcode (BINOP_DIV); }
559 | MultiplicativeExpression '%' UnaryExpression
560 { write_exp_elt_opcode (BINOP_REM); }
564 MultiplicativeExpression
565 | AdditiveExpression '+' MultiplicativeExpression
566 { write_exp_elt_opcode (BINOP_ADD); }
567 | AdditiveExpression '-' MultiplicativeExpression
568 { write_exp_elt_opcode (BINOP_SUB); }
573 | ShiftExpression LSH AdditiveExpression
574 { write_exp_elt_opcode (BINOP_LSH); }
575 | ShiftExpression RSH AdditiveExpression
576 { write_exp_elt_opcode (BINOP_RSH); }
577 /* | ShiftExpression >>> AdditiveExpression { FIXME } */
580 RelationalExpression:
582 | RelationalExpression '<' ShiftExpression
583 { write_exp_elt_opcode (BINOP_LESS); }
584 | RelationalExpression '>' ShiftExpression
585 { write_exp_elt_opcode (BINOP_GTR); }
586 | RelationalExpression LEQ ShiftExpression
587 { write_exp_elt_opcode (BINOP_LEQ); }
588 | RelationalExpression GEQ ShiftExpression
589 { write_exp_elt_opcode (BINOP_GEQ); }
590 /* | RelationalExpresion INSTANCEOF ReferenceType { FIXME } */
595 | EqualityExpression EQUAL RelationalExpression
596 { write_exp_elt_opcode (BINOP_EQUAL); }
597 | EqualityExpression NOTEQUAL RelationalExpression
598 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
603 | AndExpression '&' EqualityExpression
604 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
607 ExclusiveOrExpression:
609 | ExclusiveOrExpression '^' AndExpression
610 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
612 InclusiveOrExpression:
613 ExclusiveOrExpression
614 | InclusiveOrExpression '|' ExclusiveOrExpression
615 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
618 ConditionalAndExpression:
619 InclusiveOrExpression
620 | ConditionalAndExpression ANDAND InclusiveOrExpression
621 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
624 ConditionalOrExpression:
625 ConditionalAndExpression
626 | ConditionalOrExpression OROR ConditionalAndExpression
627 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
630 ConditionalExpression:
631 ConditionalOrExpression
632 | ConditionalOrExpression '?' Expression ':' ConditionalExpression
633 { write_exp_elt_opcode (TERNOP_COND); }
636 AssignmentExpression:
637 ConditionalExpression
642 LeftHandSide '=' ConditionalExpression
643 { write_exp_elt_opcode (BINOP_ASSIGN); }
644 | LeftHandSide ASSIGN_MODIFY ConditionalExpression
645 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
646 write_exp_elt_opcode ($2);
647 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
652 { push_expression_name ($1); }
654 /* Already written by write_dollar_variable. */
665 /* Take care of parsing a number (anything that starts with a digit).
666 Set yylval and return the token type; update lexptr.
667 LEN is the number of characters in it. */
669 /*** Needs some error checking for the float case ***/
672 parse_number (p, len, parsed_float, putithere)
678 register ULONGEST n = 0;
679 ULONGEST limit, limit_div_base;
682 register int base = input_radix;
688 /* It's a float since it contains a point or an exponent. */
690 int num = 0; /* number of tokens scanned by scanf */
691 char saved_char = p[len];
693 p[len] = 0; /* null-terminate the token */
694 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
695 num = sscanf (p, "%g%c", (float *) &putithere->typed_val_float.dval, &c);
696 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
697 num = sscanf (p, "%lg%c", (double *) &putithere->typed_val_float.dval, &c);
700 #ifdef PRINTF_HAS_LONG_DOUBLE
701 num = sscanf (p, "%Lg%c", &putithere->typed_val_float.dval, &c);
703 /* Scan it into a double, then assign it to the long double.
704 This at least wins with values representable in the range
707 num = sscanf (p, "%lg%c", &temp, &c);
708 putithere->typed_val_float.dval = temp;
711 p[len] = saved_char; /* restore the input stream */
712 if (num != 1) /* check scanf found ONLY a float ... */
714 /* See if it has `f' or `d' suffix (float or double). */
716 c = tolower (p[len - 1]);
718 if (c == 'f' || c == 'F')
719 putithere->typed_val_float.type = builtin_type_float;
720 else if (isdigit (c) || c == '.' || c == 'd' || c == 'D')
721 putithere->typed_val_float.type = builtin_type_double;
725 return FLOATING_POINT_LITERAL;
728 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
760 limit = (ULONGEST)0xffffffff;
761 if (c == 'l' || c == 'L')
763 type = java_long_type;
765 /* A paranoid calculation of (1<<64)-1. */
766 limit = ((limit << 16) << 16) | limit;
770 type = java_int_type;
772 limit_div_base = limit / (ULONGEST) base;
777 if (c >= '0' && c <= '9')
779 else if (c >= 'A' && c <= 'Z')
781 else if (c >= 'a' && c <= 'z')
784 return ERROR; /* Char not a digit */
787 if (n > limit_div_base
788 || (n *= base) > limit - c)
789 error ("Numeric constant too large.");
793 putithere->typed_val_int.val = n;
794 putithere->typed_val_int.type = type;
795 return INTEGER_LITERAL;
802 enum exp_opcode opcode;
805 static const struct token tokentab3[] =
807 {">>=", ASSIGN_MODIFY, BINOP_RSH},
808 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
811 static const struct token tokentab2[] =
813 {"+=", ASSIGN_MODIFY, BINOP_ADD},
814 {"-=", ASSIGN_MODIFY, BINOP_SUB},
815 {"*=", ASSIGN_MODIFY, BINOP_MUL},
816 {"/=", ASSIGN_MODIFY, BINOP_DIV},
817 {"%=", ASSIGN_MODIFY, BINOP_REM},
818 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
819 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
820 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
821 {"++", INCREMENT, BINOP_END},
822 {"--", DECREMENT, BINOP_END},
823 {"&&", ANDAND, BINOP_END},
824 {"||", OROR, BINOP_END},
825 {"<<", LSH, BINOP_END},
826 {">>", RSH, BINOP_END},
827 {"==", EQUAL, BINOP_END},
828 {"!=", NOTEQUAL, BINOP_END},
829 {"<=", LEQ, BINOP_END},
830 {">=", GEQ, BINOP_END}
833 /* Read one token, getting characters through lexptr. */
844 static char *tempbuf;
845 static int tempbufsize;
850 /* See if it is a special token of length 3. */
851 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
852 if (STREQN (tokstart, tokentab3[i].operator, 3))
855 yylval.opcode = tokentab3[i].opcode;
856 return tokentab3[i].token;
859 /* See if it is a special token of length 2. */
860 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
861 if (STREQN (tokstart, tokentab2[i].operator, 2))
864 yylval.opcode = tokentab2[i].opcode;
865 return tokentab2[i].token;
868 switch (c = *tokstart)
880 /* We either have a character constant ('0' or '\177' for example)
881 or we have a quoted symbol reference ('foo(int,int)' in C++
886 c = parse_escape (&lexptr);
888 error ("Empty character constant.");
890 yylval.typed_val_int.val = c;
891 yylval.typed_val_int.type = builtin_type_char;
896 namelen = skip_quoted (tokstart) - tokstart;
899 lexptr = tokstart + namelen;
900 if (lexptr[-1] != '\'')
901 error ("Unmatched single quote.");
906 error ("Invalid character constant.");
908 return INTEGER_LITERAL;
916 if (paren_depth == 0)
923 if (comma_terminates && paren_depth == 0)
929 /* Might be a floating point number. */
930 if (lexptr[1] < '0' || lexptr[1] > '9')
931 goto symbol; /* Nope, must be a symbol. */
932 /* FALL THRU into number case. */
946 int got_dot = 0, got_e = 0, toktype;
947 register char *p = tokstart;
948 int hex = input_radix > 10;
950 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
955 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
963 /* This test includes !hex because 'e' is a valid hex digit
964 and thus does not indicate a floating point number when
966 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
968 /* This test does not include !hex, because a '.' always indicates
969 a decimal floating point number regardless of the radix. */
970 else if (!got_dot && *p == '.')
972 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
973 && (*p == '-' || *p == '+'))
974 /* This is the sign of the exponent, not the end of the
977 /* We will take any letters or digits. parse_number will
978 complain if past the radix, or if L or U are not final. */
979 else if ((*p < '0' || *p > '9')
980 && ((*p < 'a' || *p > 'z')
981 && (*p < 'A' || *p > 'Z')))
984 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
985 if (toktype == ERROR)
987 char *err_copy = (char *) alloca (p - tokstart + 1);
989 memcpy (err_copy, tokstart, p - tokstart);
990 err_copy[p - tokstart] = 0;
991 error ("Invalid number \"%s\".", err_copy);
1022 /* Build the gdb internal form of the input string in tempbuf,
1023 translating any standard C escape forms seen. Note that the
1024 buffer is null byte terminated *only* for the convenience of
1025 debugging gdb itself and printing the buffer contents when
1026 the buffer contains no embedded nulls. Gdb does not depend
1027 upon the buffer being null byte terminated, it uses the length
1028 string instead. This allows gdb to handle C strings (as well
1029 as strings in other languages) with embedded null bytes */
1031 tokptr = ++tokstart;
1035 /* Grow the static temp buffer if necessary, including allocating
1036 the first one on demand. */
1037 if (tempbufindex + 1 >= tempbufsize)
1039 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1045 /* Do nothing, loop will terminate. */
1049 c = parse_escape (&tokptr);
1054 tempbuf[tempbufindex++] = c;
1057 tempbuf[tempbufindex++] = *tokptr++;
1060 } while ((*tokptr != '"') && (*tokptr != '\0'));
1061 if (*tokptr++ != '"')
1063 error ("Unterminated string in expression.");
1065 tempbuf[tempbufindex] = '\0'; /* See note above */
1066 yylval.sval.ptr = tempbuf;
1067 yylval.sval.length = tempbufindex;
1069 return (STRING_LITERAL);
1072 if (!(c == '_' || c == '$'
1073 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1074 /* We must have come across a bad character (e.g. ';'). */
1075 error ("Invalid character '%c' in expression.", c);
1077 /* It's a name. See how long it is. */
1079 for (c = tokstart[namelen];
1080 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1081 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1086 while (tokstart[++i] && tokstart[i] != '>');
1087 if (tokstart[i] == '>')
1090 c = tokstart[++namelen];
1093 /* The token "if" terminates the expression and is NOT
1094 removed from the input stream. */
1095 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1104 /* Catch specific keywords. Should be done with a data structure. */
1108 if (STREQN (tokstart, "boolean", 7))
1112 if (STREQN (tokstart, "double", 6))
1116 if (STREQN (tokstart, "short", 5))
1118 if (STREQN (tokstart, "false", 5))
1121 return BOOLEAN_LITERAL;
1123 if (STREQN (tokstart, "super", 5))
1125 if (STREQN (tokstart, "float", 5))
1129 if (STREQN (tokstart, "long", 4))
1131 if (STREQN (tokstart, "byte", 4))
1133 if (STREQN (tokstart, "char", 4))
1135 if (STREQN (tokstart, "true", 4))
1138 return BOOLEAN_LITERAL;
1140 if (current_language->la_language == language_cplus
1141 && STREQN (tokstart, "this", 4))
1143 static const char this_name[] =
1144 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1146 if (lookup_symbol (this_name, expression_context_block,
1147 VAR_NAMESPACE, (int *) NULL,
1148 (struct symtab **) NULL))
1153 if (STREQN (tokstart, "int", 3))
1155 if (STREQN (tokstart, "new", 3))
1162 yylval.sval.ptr = tokstart;
1163 yylval.sval.length = namelen;
1165 if (*tokstart == '$')
1167 write_dollar_variable (yylval.sval);
1171 /* Input names that aren't symbols but ARE valid hex numbers,
1172 when the input radix permits them, can be names or numbers
1173 depending on the parse. Note we support radixes > 16 here. */
1174 if (((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1175 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1177 YYSTYPE newlval; /* Its value is ignored. */
1178 int hextype = parse_number (tokstart, namelen, 0, &newlval);
1179 if (hextype == INTEGER_LITERAL)
1189 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
1192 static struct type *
1193 java_type_from_name (name)
1197 char *tmp = copy_name (name);
1198 struct type *typ = java_lookup_class (tmp);
1199 if (typ == NULL || TYPE_CODE (typ) != TYPE_CODE_STRUCT)
1200 error ("No class named %s.", tmp);
1204 /* If NAME is a valid variable name in this scope, push it and return 1.
1205 Otherwise, return 0. */
1208 push_variable (name)
1212 char *tmp = copy_name (name);
1213 int is_a_field_of_this = 0;
1215 sym = lookup_symbol (tmp, expression_context_block, VAR_NAMESPACE,
1216 &is_a_field_of_this, (struct symtab **) NULL);
1217 if (sym && SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1219 if (symbol_read_needs_frame (sym))
1221 if (innermost_block == 0 ||
1222 contained_in (block_found, innermost_block))
1223 innermost_block = block_found;
1226 write_exp_elt_opcode (OP_VAR_VALUE);
1227 /* We want to use the selected frame, not another more inner frame
1228 which happens to be in the same block. */
1229 write_exp_elt_block (NULL);
1230 write_exp_elt_sym (sym);
1231 write_exp_elt_opcode (OP_VAR_VALUE);
1234 if (is_a_field_of_this)
1236 /* it hangs off of `this'. Must not inadvertently convert from a
1237 method call to data ref. */
1238 if (innermost_block == 0 ||
1239 contained_in (block_found, innermost_block))
1240 innermost_block = block_found;
1241 write_exp_elt_opcode (OP_THIS);
1242 write_exp_elt_opcode (OP_THIS);
1243 write_exp_elt_opcode (STRUCTOP_PTR);
1244 write_exp_string (name);
1245 write_exp_elt_opcode (STRUCTOP_PTR);
1251 /* Assuming a reference expression has been pushed, emit the
1252 STRUCTOP_STRUCT ops to access the field named NAME. If NAME is a
1253 qualified name (has '.'), generate a field access for each part. */
1256 push_fieldnames (name)
1260 struct stoken token;
1261 token.ptr = name.ptr;
1264 if (i == name.length || name.ptr[i] == '.')
1266 /* token.ptr is start of current field name. */
1267 token.length = &name.ptr[i] - token.ptr;
1268 write_exp_elt_opcode (STRUCTOP_STRUCT);
1269 write_exp_string (token);
1270 write_exp_elt_opcode (STRUCTOP_STRUCT);
1271 token.ptr += token.length + 1;
1273 if (i >= name.length)
1278 /* Helper routine for push_expression_name.
1279 Handle a qualified name, where DOT_INDEX is the index of the first '.' */
1282 push_qualified_expression_name (name, dot_index)
1286 struct stoken token;
1290 token.ptr = name.ptr;
1291 token.length = dot_index;
1293 if (push_variable (token))
1295 token.ptr = name.ptr + dot_index + 1;
1296 token.length = name.length - dot_index - 1;
1297 push_fieldnames (token);
1301 token.ptr = name.ptr;
1304 token.length = dot_index;
1305 tmp = copy_name (token);
1306 typ = java_lookup_class (tmp);
1309 if (dot_index == name.length)
1311 write_exp_elt_opcode(OP_TYPE);
1312 write_exp_elt_type(typ);
1313 write_exp_elt_opcode(OP_TYPE);
1316 dot_index++; /* Skip '.' */
1317 name.ptr += dot_index;
1318 name.length -= dot_index;
1320 while (dot_index < name.length && name.ptr[dot_index] != '.')
1322 token.ptr = name.ptr;
1323 token.length = dot_index;
1324 write_exp_elt_opcode (OP_SCOPE);
1325 write_exp_elt_type (typ);
1326 write_exp_string (token);
1327 write_exp_elt_opcode (OP_SCOPE);
1328 if (dot_index < name.length)
1331 name.ptr += dot_index;
1332 name.length -= dot_index;
1333 push_fieldnames (name);
1337 else if (dot_index >= name.length)
1339 dot_index++; /* Skip '.' */
1340 while (dot_index < name.length && name.ptr[dot_index] != '.')
1343 error ("unknown type `%.*s'", name.length, name.ptr);
1346 /* Handle Name in an expression (or LHS).
1347 Handle VAR, TYPE, TYPE.FIELD1....FIELDN and VAR.FIELD1....FIELDN. */
1350 push_expression_name (name)
1358 for (i = 0; i < name.length; i++)
1360 if (name.ptr[i] == '.')
1362 /* It's a Qualified Expression Name. */
1363 push_qualified_expression_name (name, i);
1368 /* It's a Simple Expression Name. */
1370 if (push_variable (name))
1372 tmp = copy_name (name);
1373 typ = java_lookup_class (tmp);
1376 write_exp_elt_opcode(OP_TYPE);
1377 write_exp_elt_type(typ);
1378 write_exp_elt_opcode(OP_TYPE);
1382 struct minimal_symbol *msymbol;
1384 msymbol = lookup_minimal_symbol (tmp, NULL, NULL);
1385 if (msymbol != NULL)
1387 write_exp_msymbol (msymbol,
1388 lookup_function_type (builtin_type_int),
1391 else if (!have_full_symbols () && !have_partial_symbols ())
1392 error ("No symbol table is loaded. Use the \"file\" command.");
1394 error ("No symbol \"%s\" in current context.", tmp);
1400 /* The following two routines, copy_exp and insert_exp, aren't specific to
1401 Java, so they could go in parse.c, but their only purpose is to support
1402 the parsing kludges we use in this file, so maybe it's best to isolate
1405 /* Copy the expression whose last element is at index ENDPOS - 1 in EXPR
1406 into a freshly malloc'ed struct expression. Its language_defn is set
1408 static struct expression *
1409 copy_exp (struct expression *expr, int endpos)
1411 int len = length_of_subexp (expr, endpos);
1412 struct expression *new
1413 = (struct expression *) malloc (sizeof (*new) + EXP_ELEM_TO_BYTES (len));
1415 memcpy (new->elts, expr->elts + endpos - len, EXP_ELEM_TO_BYTES (len));
1416 new->language_defn = 0;
1421 /* Insert the expression NEW into the current expression (expout) at POS. */
1423 insert_exp (int pos,
1424 struct expression *new)
1426 int newlen = new->nelts;
1428 /* Grow expout if necessary. In this function's only use at present,
1429 this should never be necessary. */
1430 if (expout_ptr + newlen > expout_size)
1432 expout_size = max (expout_size * 2, expout_ptr + newlen + 10);
1433 expout = (struct expression *)
1434 realloc ((char *) expout, (sizeof (struct expression)
1435 + EXP_ELEM_TO_BYTES (expout_size)));
1441 for (i = expout_ptr - 1; i >= pos; i--)
1442 expout->elts[i + newlen] = expout->elts[i];
1445 memcpy (expout->elts + pos, new->elts, EXP_ELEM_TO_BYTES (newlen));
1446 expout_ptr += newlen;