]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/jv-exp.y
* breakpoint.c:
[thirdparty/binutils-gdb.git] / gdb / jv-exp.y
1 /* YACC parser for Java expressions, for GDB.
2 Copyright (C) 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
21
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.
30
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. */
38
39 %{
40
41 #include "defs.h"
42 #include "gdb_string.h"
43 #include <ctype.h>
44 #include "expression.h"
45 #include "value.h"
46 #include "parser-defs.h"
47 #include "language.h"
48 #include "jv-lang.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 */
52 #include "block.h"
53
54 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
55 as well as gratuitiously global symbol names, so we can have multiple
56 yacc generated parsers in gdb. Note that these are only the variables
57 produced by yacc. If other parser generators (bison, byacc, etc) produce
58 additional global names that conflict at link time, then those parser
59 generators need to be fixed instead of adding those names to this list. */
60
61 #define yymaxdepth java_maxdepth
62 #define yyparse java_parse
63 #define yylex java_lex
64 #define yyerror java_error
65 #define yylval java_lval
66 #define yychar java_char
67 #define yydebug java_debug
68 #define yypact java_pact
69 #define yyr1 java_r1
70 #define yyr2 java_r2
71 #define yydef java_def
72 #define yychk java_chk
73 #define yypgo java_pgo
74 #define yyact java_act
75 #define yyexca java_exca
76 #define yyerrflag java_errflag
77 #define yynerrs java_nerrs
78 #define yyps java_ps
79 #define yypv java_pv
80 #define yys java_s
81 #define yy_yys java_yys
82 #define yystate java_state
83 #define yytmp java_tmp
84 #define yyv java_v
85 #define yy_yyv java_yyv
86 #define yyval java_val
87 #define yylloc java_lloc
88 #define yyreds java_reds /* With YYDEBUG defined */
89 #define yytoks java_toks /* With YYDEBUG defined */
90 #define yyname java_name /* With YYDEBUG defined */
91 #define yyrule java_rule /* With YYDEBUG defined */
92 #define yylhs java_yylhs
93 #define yylen java_yylen
94 #define yydefred java_yydefred
95 #define yydgoto java_yydgoto
96 #define yysindex java_yysindex
97 #define yyrindex java_yyrindex
98 #define yygindex java_yygindex
99 #define yytable java_yytable
100 #define yycheck java_yycheck
101
102 #ifndef YYDEBUG
103 #define YYDEBUG 1 /* Default to yydebug support */
104 #endif
105
106 #define YYFPRINTF parser_fprintf
107
108 int yyparse (void);
109
110 static int yylex (void);
111
112 void yyerror (char *);
113
114 static struct type *java_type_from_name (struct stoken);
115 static void push_expression_name (struct stoken);
116 static void push_fieldnames (struct stoken);
117
118 static struct expression *copy_exp (struct expression *, int);
119 static void insert_exp (int, struct expression *);
120
121 %}
122
123 /* Although the yacc "value" of an expression is not used,
124 since the result is stored in the structure being created,
125 other node types do have values. */
126
127 %union
128 {
129 LONGEST lval;
130 struct {
131 LONGEST val;
132 struct type *type;
133 } typed_val_int;
134 struct {
135 DOUBLEST dval;
136 struct type *type;
137 } typed_val_float;
138 struct symbol *sym;
139 struct type *tval;
140 struct stoken sval;
141 struct ttype tsym;
142 struct symtoken ssym;
143 struct block *bval;
144 enum exp_opcode opcode;
145 struct internalvar *ivar;
146 int *ivec;
147 }
148
149 %{
150 /* YYSTYPE gets defined by %union */
151 static int parse_number (char *, int, int, YYSTYPE *);
152 %}
153
154 %type <lval> rcurly Dims Dims_opt
155 %type <tval> ClassOrInterfaceType ClassType /* ReferenceType Type ArrayType */
156 %type <tval> IntegralType FloatingPointType NumericType PrimitiveType ArrayType PrimitiveOrArrayType
157
158 %token <typed_val_int> INTEGER_LITERAL
159 %token <typed_val_float> FLOATING_POINT_LITERAL
160
161 %token <sval> IDENTIFIER
162 %token <sval> STRING_LITERAL
163 %token <lval> BOOLEAN_LITERAL
164 %token <tsym> TYPENAME
165 %type <sval> Name SimpleName QualifiedName ForcedName
166
167 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
168 but which would parse as a valid number in the current input radix.
169 E.g. "c" when input_radix==16. Depending on the parse, it will be
170 turned into a name or into a number. */
171
172 %token <sval> NAME_OR_INT
173
174 %token ERROR
175
176 /* Special type cases, put in to allow the parser to distinguish different
177 legal basetypes. */
178 %token LONG SHORT BYTE INT CHAR BOOLEAN DOUBLE FLOAT
179
180 %token VARIABLE
181
182 %token <opcode> ASSIGN_MODIFY
183
184 %token SUPER NEW
185
186 %left ','
187 %right '=' ASSIGN_MODIFY
188 %right '?'
189 %left OROR
190 %left ANDAND
191 %left '|'
192 %left '^'
193 %left '&'
194 %left EQUAL NOTEQUAL
195 %left '<' '>' LEQ GEQ
196 %left LSH RSH
197 %left '+' '-'
198 %left '*' '/' '%'
199 %right INCREMENT DECREMENT
200 %right '.' '[' '('
201
202 \f
203 %%
204
205 start : exp1
206 | type_exp
207 ;
208
209 type_exp: PrimitiveOrArrayType
210 {
211 write_exp_elt_opcode(OP_TYPE);
212 write_exp_elt_type($1);
213 write_exp_elt_opcode(OP_TYPE);
214 }
215 ;
216
217 PrimitiveOrArrayType:
218 PrimitiveType
219 | ArrayType
220 ;
221
222 StringLiteral:
223 STRING_LITERAL
224 {
225 write_exp_elt_opcode (OP_STRING);
226 write_exp_string ($1);
227 write_exp_elt_opcode (OP_STRING);
228 }
229 ;
230
231 Literal:
232 INTEGER_LITERAL
233 { write_exp_elt_opcode (OP_LONG);
234 write_exp_elt_type ($1.type);
235 write_exp_elt_longcst ((LONGEST)($1.val));
236 write_exp_elt_opcode (OP_LONG); }
237 | NAME_OR_INT
238 { YYSTYPE val;
239 parse_number ($1.ptr, $1.length, 0, &val);
240 write_exp_elt_opcode (OP_LONG);
241 write_exp_elt_type (val.typed_val_int.type);
242 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
243 write_exp_elt_opcode (OP_LONG);
244 }
245 | FLOATING_POINT_LITERAL
246 { write_exp_elt_opcode (OP_DOUBLE);
247 write_exp_elt_type ($1.type);
248 write_exp_elt_dblcst ($1.dval);
249 write_exp_elt_opcode (OP_DOUBLE); }
250 | BOOLEAN_LITERAL
251 { write_exp_elt_opcode (OP_LONG);
252 write_exp_elt_type (java_boolean_type);
253 write_exp_elt_longcst ((LONGEST)$1);
254 write_exp_elt_opcode (OP_LONG); }
255 | StringLiteral
256 ;
257
258 /* UNUSED:
259 Type:
260 PrimitiveType
261 | ReferenceType
262 ;
263 */
264
265 PrimitiveType:
266 NumericType
267 | BOOLEAN
268 { $$ = java_boolean_type; }
269 ;
270
271 NumericType:
272 IntegralType
273 | FloatingPointType
274 ;
275
276 IntegralType:
277 BYTE
278 { $$ = java_byte_type; }
279 | SHORT
280 { $$ = java_short_type; }
281 | INT
282 { $$ = java_int_type; }
283 | LONG
284 { $$ = java_long_type; }
285 | CHAR
286 { $$ = java_char_type; }
287 ;
288
289 FloatingPointType:
290 FLOAT
291 { $$ = java_float_type; }
292 | DOUBLE
293 { $$ = java_double_type; }
294 ;
295
296 /* UNUSED:
297 ReferenceType:
298 ClassOrInterfaceType
299 | ArrayType
300 ;
301 */
302
303 ClassOrInterfaceType:
304 Name
305 { $$ = java_type_from_name ($1); }
306 ;
307
308 ClassType:
309 ClassOrInterfaceType
310 ;
311
312 ArrayType:
313 PrimitiveType Dims
314 { $$ = java_array_type ($1, $2); }
315 | Name Dims
316 { $$ = java_array_type (java_type_from_name ($1), $2); }
317 ;
318
319 Name:
320 IDENTIFIER
321 | QualifiedName
322 ;
323
324 ForcedName:
325 SimpleName
326 | QualifiedName
327 ;
328
329 SimpleName:
330 IDENTIFIER
331 | NAME_OR_INT
332 ;
333
334 QualifiedName:
335 Name '.' SimpleName
336 { $$.length = $1.length + $3.length + 1;
337 if ($1.ptr + $1.length + 1 == $3.ptr
338 && $1.ptr[$1.length] == '.')
339 $$.ptr = $1.ptr; /* Optimization. */
340 else
341 {
342 $$.ptr = (char *) malloc ($$.length + 1);
343 make_cleanup (free, $$.ptr);
344 sprintf ($$.ptr, "%.*s.%.*s",
345 $1.length, $1.ptr, $3.length, $3.ptr);
346 } }
347 ;
348
349 /*
350 type_exp: type
351 { write_exp_elt_opcode(OP_TYPE);
352 write_exp_elt_type($1);
353 write_exp_elt_opcode(OP_TYPE);}
354 ;
355 */
356
357 /* Expressions, including the comma operator. */
358 exp1 : Expression
359 | exp1 ',' Expression
360 { write_exp_elt_opcode (BINOP_COMMA); }
361 ;
362
363 Primary:
364 PrimaryNoNewArray
365 | ArrayCreationExpression
366 ;
367
368 PrimaryNoNewArray:
369 Literal
370 | '(' Expression ')'
371 | ClassInstanceCreationExpression
372 | FieldAccess
373 | MethodInvocation
374 | ArrayAccess
375 | lcurly ArgumentList rcurly
376 { write_exp_elt_opcode (OP_ARRAY);
377 write_exp_elt_longcst ((LONGEST) 0);
378 write_exp_elt_longcst ((LONGEST) $3);
379 write_exp_elt_opcode (OP_ARRAY); }
380 ;
381
382 lcurly:
383 '{'
384 { start_arglist (); }
385 ;
386
387 rcurly:
388 '}'
389 { $$ = end_arglist () - 1; }
390 ;
391
392 ClassInstanceCreationExpression:
393 NEW ClassType '(' ArgumentList_opt ')'
394 { internal_error (__FILE__, __LINE__,
395 _("FIXME - ClassInstanceCreationExpression")); }
396 ;
397
398 ArgumentList:
399 Expression
400 { arglist_len = 1; }
401 | ArgumentList ',' Expression
402 { arglist_len++; }
403 ;
404
405 ArgumentList_opt:
406 /* EMPTY */
407 { arglist_len = 0; }
408 | ArgumentList
409 ;
410
411 ArrayCreationExpression:
412 NEW PrimitiveType DimExprs Dims_opt
413 { internal_error (__FILE__, __LINE__,
414 _("FIXME - ArrayCreationExpression")); }
415 | NEW ClassOrInterfaceType DimExprs Dims_opt
416 { internal_error (__FILE__, __LINE__,
417 _("FIXME - ArrayCreationExpression")); }
418 ;
419
420 DimExprs:
421 DimExpr
422 | DimExprs DimExpr
423 ;
424
425 DimExpr:
426 '[' Expression ']'
427 ;
428
429 Dims:
430 '[' ']'
431 { $$ = 1; }
432 | Dims '[' ']'
433 { $$ = $1 + 1; }
434 ;
435
436 Dims_opt:
437 Dims
438 | /* EMPTY */
439 { $$ = 0; }
440 ;
441
442 FieldAccess:
443 Primary '.' SimpleName
444 { push_fieldnames ($3); }
445 | VARIABLE '.' SimpleName
446 { push_fieldnames ($3); }
447 /*| SUPER '.' SimpleName { FIXME } */
448 ;
449
450 FuncStart:
451 Name '('
452 { push_expression_name ($1); }
453 ;
454
455 MethodInvocation:
456 FuncStart
457 { start_arglist(); }
458 ArgumentList_opt ')'
459 { write_exp_elt_opcode (OP_FUNCALL);
460 write_exp_elt_longcst ((LONGEST) end_arglist ());
461 write_exp_elt_opcode (OP_FUNCALL); }
462 | Primary '.' SimpleName '(' ArgumentList_opt ')'
463 { error (_("Form of method invocation not implemented")); }
464 | SUPER '.' SimpleName '(' ArgumentList_opt ')'
465 { error (_("Form of method invocation not implemented")); }
466 ;
467
468 ArrayAccess:
469 Name '[' Expression ']'
470 {
471 /* Emit code for the Name now, then exchange it in the
472 expout array with the Expression's code. We could
473 introduce a OP_SWAP code or a reversed version of
474 BINOP_SUBSCRIPT, but that makes the rest of GDB pay
475 for our parsing kludges. */
476 struct expression *name_expr;
477
478 push_expression_name ($1);
479 name_expr = copy_exp (expout, expout_ptr);
480 expout_ptr -= name_expr->nelts;
481 insert_exp (expout_ptr-length_of_subexp (expout, expout_ptr),
482 name_expr);
483 free (name_expr);
484 write_exp_elt_opcode (BINOP_SUBSCRIPT);
485 }
486 | VARIABLE '[' Expression ']'
487 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
488 | PrimaryNoNewArray '[' Expression ']'
489 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
490 ;
491
492 PostfixExpression:
493 Primary
494 | Name
495 { push_expression_name ($1); }
496 | VARIABLE
497 /* Already written by write_dollar_variable. */
498 | PostIncrementExpression
499 | PostDecrementExpression
500 ;
501
502 PostIncrementExpression:
503 PostfixExpression INCREMENT
504 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
505 ;
506
507 PostDecrementExpression:
508 PostfixExpression DECREMENT
509 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
510 ;
511
512 UnaryExpression:
513 PreIncrementExpression
514 | PreDecrementExpression
515 | '+' UnaryExpression
516 | '-' UnaryExpression
517 { write_exp_elt_opcode (UNOP_NEG); }
518 | '*' UnaryExpression
519 { write_exp_elt_opcode (UNOP_IND); } /*FIXME not in Java */
520 | UnaryExpressionNotPlusMinus
521 ;
522
523 PreIncrementExpression:
524 INCREMENT UnaryExpression
525 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
526 ;
527
528 PreDecrementExpression:
529 DECREMENT UnaryExpression
530 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
531 ;
532
533 UnaryExpressionNotPlusMinus:
534 PostfixExpression
535 | '~' UnaryExpression
536 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
537 | '!' UnaryExpression
538 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
539 | CastExpression
540 ;
541
542 CastExpression:
543 '(' PrimitiveType Dims_opt ')' UnaryExpression
544 { write_exp_elt_opcode (UNOP_CAST);
545 write_exp_elt_type (java_array_type ($2, $3));
546 write_exp_elt_opcode (UNOP_CAST); }
547 | '(' Expression ')' UnaryExpressionNotPlusMinus
548 {
549 int exp_size = expout_ptr;
550 int last_exp_size = length_of_subexp(expout, expout_ptr);
551 struct type *type;
552 int i;
553 int base = expout_ptr - last_exp_size - 3;
554 if (base < 0 || expout->elts[base+2].opcode != OP_TYPE)
555 error (_("Invalid cast expression"));
556 type = expout->elts[base+1].type;
557 /* Remove the 'Expression' and slide the
558 UnaryExpressionNotPlusMinus down to replace it. */
559 for (i = 0; i < last_exp_size; i++)
560 expout->elts[base + i] = expout->elts[base + i + 3];
561 expout_ptr -= 3;
562 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
563 type = lookup_pointer_type (type);
564 write_exp_elt_opcode (UNOP_CAST);
565 write_exp_elt_type (type);
566 write_exp_elt_opcode (UNOP_CAST);
567 }
568 | '(' Name Dims ')' UnaryExpressionNotPlusMinus
569 { write_exp_elt_opcode (UNOP_CAST);
570 write_exp_elt_type (java_array_type (java_type_from_name ($2), $3));
571 write_exp_elt_opcode (UNOP_CAST); }
572 ;
573
574
575 MultiplicativeExpression:
576 UnaryExpression
577 | MultiplicativeExpression '*' UnaryExpression
578 { write_exp_elt_opcode (BINOP_MUL); }
579 | MultiplicativeExpression '/' UnaryExpression
580 { write_exp_elt_opcode (BINOP_DIV); }
581 | MultiplicativeExpression '%' UnaryExpression
582 { write_exp_elt_opcode (BINOP_REM); }
583 ;
584
585 AdditiveExpression:
586 MultiplicativeExpression
587 | AdditiveExpression '+' MultiplicativeExpression
588 { write_exp_elt_opcode (BINOP_ADD); }
589 | AdditiveExpression '-' MultiplicativeExpression
590 { write_exp_elt_opcode (BINOP_SUB); }
591 ;
592
593 ShiftExpression:
594 AdditiveExpression
595 | ShiftExpression LSH AdditiveExpression
596 { write_exp_elt_opcode (BINOP_LSH); }
597 | ShiftExpression RSH AdditiveExpression
598 { write_exp_elt_opcode (BINOP_RSH); }
599 /* | ShiftExpression >>> AdditiveExpression { FIXME } */
600 ;
601
602 RelationalExpression:
603 ShiftExpression
604 | RelationalExpression '<' ShiftExpression
605 { write_exp_elt_opcode (BINOP_LESS); }
606 | RelationalExpression '>' ShiftExpression
607 { write_exp_elt_opcode (BINOP_GTR); }
608 | RelationalExpression LEQ ShiftExpression
609 { write_exp_elt_opcode (BINOP_LEQ); }
610 | RelationalExpression GEQ ShiftExpression
611 { write_exp_elt_opcode (BINOP_GEQ); }
612 /* | RelationalExpresion INSTANCEOF ReferenceType { FIXME } */
613 ;
614
615 EqualityExpression:
616 RelationalExpression
617 | EqualityExpression EQUAL RelationalExpression
618 { write_exp_elt_opcode (BINOP_EQUAL); }
619 | EqualityExpression NOTEQUAL RelationalExpression
620 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
621 ;
622
623 AndExpression:
624 EqualityExpression
625 | AndExpression '&' EqualityExpression
626 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
627 ;
628
629 ExclusiveOrExpression:
630 AndExpression
631 | ExclusiveOrExpression '^' AndExpression
632 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
633 ;
634 InclusiveOrExpression:
635 ExclusiveOrExpression
636 | InclusiveOrExpression '|' ExclusiveOrExpression
637 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
638 ;
639
640 ConditionalAndExpression:
641 InclusiveOrExpression
642 | ConditionalAndExpression ANDAND InclusiveOrExpression
643 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
644 ;
645
646 ConditionalOrExpression:
647 ConditionalAndExpression
648 | ConditionalOrExpression OROR ConditionalAndExpression
649 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
650 ;
651
652 ConditionalExpression:
653 ConditionalOrExpression
654 | ConditionalOrExpression '?' Expression ':' ConditionalExpression
655 { write_exp_elt_opcode (TERNOP_COND); }
656 ;
657
658 AssignmentExpression:
659 ConditionalExpression
660 | Assignment
661 ;
662
663 Assignment:
664 LeftHandSide '=' ConditionalExpression
665 { write_exp_elt_opcode (BINOP_ASSIGN); }
666 | LeftHandSide ASSIGN_MODIFY ConditionalExpression
667 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
668 write_exp_elt_opcode ($2);
669 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
670 ;
671
672 LeftHandSide:
673 ForcedName
674 { push_expression_name ($1); }
675 | VARIABLE
676 /* Already written by write_dollar_variable. */
677 | FieldAccess
678 | ArrayAccess
679 ;
680
681
682 Expression:
683 AssignmentExpression
684 ;
685
686 %%
687 /* Take care of parsing a number (anything that starts with a digit).
688 Set yylval and return the token type; update lexptr.
689 LEN is the number of characters in it. */
690
691 /*** Needs some error checking for the float case ***/
692
693 static int
694 parse_number (p, len, parsed_float, putithere)
695 char *p;
696 int len;
697 int parsed_float;
698 YYSTYPE *putithere;
699 {
700 ULONGEST n = 0;
701 ULONGEST limit, limit_div_base;
702
703 int c;
704 int base = input_radix;
705
706 struct type *type;
707
708 if (parsed_float)
709 {
710 /* It's a float since it contains a point or an exponent. */
711 char c;
712 int num = 0; /* number of tokens scanned by scanf */
713 char saved_char = p[len];
714
715 p[len] = 0; /* null-terminate the token */
716 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
717 num = sscanf (p, "%g%c", (float *) &putithere->typed_val_float.dval, &c);
718 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
719 num = sscanf (p, "%lg%c", (double *) &putithere->typed_val_float.dval, &c);
720 else
721 {
722 #ifdef SCANF_HAS_LONG_DOUBLE
723 num = sscanf (p, "%Lg%c", &putithere->typed_val_float.dval, &c);
724 #else
725 /* Scan it into a double, then assign it to the long double.
726 This at least wins with values representable in the range
727 of doubles. */
728 double temp;
729 num = sscanf (p, "%lg%c", &temp, &c);
730 putithere->typed_val_float.dval = temp;
731 #endif
732 }
733 p[len] = saved_char; /* restore the input stream */
734 if (num != 1) /* check scanf found ONLY a float ... */
735 return ERROR;
736 /* See if it has `f' or `d' suffix (float or double). */
737
738 c = tolower (p[len - 1]);
739
740 if (c == 'f' || c == 'F')
741 putithere->typed_val_float.type = builtin_type_float;
742 else if (isdigit (c) || c == '.' || c == 'd' || c == 'D')
743 putithere->typed_val_float.type = builtin_type_double;
744 else
745 return ERROR;
746
747 return FLOATING_POINT_LITERAL;
748 }
749
750 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
751 if (p[0] == '0')
752 switch (p[1])
753 {
754 case 'x':
755 case 'X':
756 if (len >= 3)
757 {
758 p += 2;
759 base = 16;
760 len -= 2;
761 }
762 break;
763
764 case 't':
765 case 'T':
766 case 'd':
767 case 'D':
768 if (len >= 3)
769 {
770 p += 2;
771 base = 10;
772 len -= 2;
773 }
774 break;
775
776 default:
777 base = 8;
778 break;
779 }
780
781 c = p[len-1];
782 /* A paranoid calculation of (1<<64)-1. */
783 limit = (ULONGEST)0xffffffff;
784 limit = ((limit << 16) << 16) | limit;
785 if (c == 'l' || c == 'L')
786 {
787 type = java_long_type;
788 len--;
789 }
790 else
791 {
792 type = java_int_type;
793 }
794 limit_div_base = limit / (ULONGEST) base;
795
796 while (--len >= 0)
797 {
798 c = *p++;
799 if (c >= '0' && c <= '9')
800 c -= '0';
801 else if (c >= 'A' && c <= 'Z')
802 c -= 'A' - 10;
803 else if (c >= 'a' && c <= 'z')
804 c -= 'a' - 10;
805 else
806 return ERROR; /* Char not a digit */
807 if (c >= base)
808 return ERROR;
809 if (n > limit_div_base
810 || (n *= base) > limit - c)
811 error (_("Numeric constant too large"));
812 n += c;
813 }
814
815 /* If the type is bigger than a 32-bit signed integer can be, implicitly
816 promote to long. Java does not do this, so mark it as builtin_type_uint64
817 rather than java_long_type. 0x80000000 will become -0x80000000 instead
818 of 0x80000000L, because we don't know the sign at this point.
819 */
820 if (type == java_int_type && n > (ULONGEST)0x80000000)
821 type = builtin_type_uint64;
822
823 putithere->typed_val_int.val = n;
824 putithere->typed_val_int.type = type;
825
826 return INTEGER_LITERAL;
827 }
828
829 struct token
830 {
831 char *operator;
832 int token;
833 enum exp_opcode opcode;
834 };
835
836 static const struct token tokentab3[] =
837 {
838 {">>=", ASSIGN_MODIFY, BINOP_RSH},
839 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
840 };
841
842 static const struct token tokentab2[] =
843 {
844 {"+=", ASSIGN_MODIFY, BINOP_ADD},
845 {"-=", ASSIGN_MODIFY, BINOP_SUB},
846 {"*=", ASSIGN_MODIFY, BINOP_MUL},
847 {"/=", ASSIGN_MODIFY, BINOP_DIV},
848 {"%=", ASSIGN_MODIFY, BINOP_REM},
849 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
850 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
851 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
852 {"++", INCREMENT, BINOP_END},
853 {"--", DECREMENT, BINOP_END},
854 {"&&", ANDAND, BINOP_END},
855 {"||", OROR, BINOP_END},
856 {"<<", LSH, BINOP_END},
857 {">>", RSH, BINOP_END},
858 {"==", EQUAL, BINOP_END},
859 {"!=", NOTEQUAL, BINOP_END},
860 {"<=", LEQ, BINOP_END},
861 {">=", GEQ, BINOP_END}
862 };
863
864 /* Read one token, getting characters through lexptr. */
865
866 static int
867 yylex ()
868 {
869 int c;
870 int namelen;
871 unsigned int i;
872 char *tokstart;
873 char *tokptr;
874 int tempbufindex;
875 static char *tempbuf;
876 static int tempbufsize;
877
878 retry:
879
880 prev_lexptr = lexptr;
881
882 tokstart = lexptr;
883 /* See if it is a special token of length 3. */
884 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
885 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
886 {
887 lexptr += 3;
888 yylval.opcode = tokentab3[i].opcode;
889 return tokentab3[i].token;
890 }
891
892 /* See if it is a special token of length 2. */
893 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
894 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
895 {
896 lexptr += 2;
897 yylval.opcode = tokentab2[i].opcode;
898 return tokentab2[i].token;
899 }
900
901 switch (c = *tokstart)
902 {
903 case 0:
904 return 0;
905
906 case ' ':
907 case '\t':
908 case '\n':
909 lexptr++;
910 goto retry;
911
912 case '\'':
913 /* We either have a character constant ('0' or '\177' for example)
914 or we have a quoted symbol reference ('foo(int,int)' in C++
915 for example). */
916 lexptr++;
917 c = *lexptr++;
918 if (c == '\\')
919 c = parse_escape (&lexptr);
920 else if (c == '\'')
921 error (_("Empty character constant"));
922
923 yylval.typed_val_int.val = c;
924 yylval.typed_val_int.type = java_char_type;
925
926 c = *lexptr++;
927 if (c != '\'')
928 {
929 namelen = skip_quoted (tokstart) - tokstart;
930 if (namelen > 2)
931 {
932 lexptr = tokstart + namelen;
933 if (lexptr[-1] != '\'')
934 error (_("Unmatched single quote"));
935 namelen -= 2;
936 tokstart++;
937 goto tryname;
938 }
939 error (_("Invalid character constant"));
940 }
941 return INTEGER_LITERAL;
942
943 case '(':
944 paren_depth++;
945 lexptr++;
946 return c;
947
948 case ')':
949 if (paren_depth == 0)
950 return 0;
951 paren_depth--;
952 lexptr++;
953 return c;
954
955 case ',':
956 if (comma_terminates && paren_depth == 0)
957 return 0;
958 lexptr++;
959 return c;
960
961 case '.':
962 /* Might be a floating point number. */
963 if (lexptr[1] < '0' || lexptr[1] > '9')
964 goto symbol; /* Nope, must be a symbol. */
965 /* FALL THRU into number case. */
966
967 case '0':
968 case '1':
969 case '2':
970 case '3':
971 case '4':
972 case '5':
973 case '6':
974 case '7':
975 case '8':
976 case '9':
977 {
978 /* It's a number. */
979 int got_dot = 0, got_e = 0, toktype;
980 char *p = tokstart;
981 int hex = input_radix > 10;
982
983 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
984 {
985 p += 2;
986 hex = 1;
987 }
988 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
989 {
990 p += 2;
991 hex = 0;
992 }
993
994 for (;; ++p)
995 {
996 /* This test includes !hex because 'e' is a valid hex digit
997 and thus does not indicate a floating point number when
998 the radix is hex. */
999 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1000 got_dot = got_e = 1;
1001 /* This test does not include !hex, because a '.' always indicates
1002 a decimal floating point number regardless of the radix. */
1003 else if (!got_dot && *p == '.')
1004 got_dot = 1;
1005 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1006 && (*p == '-' || *p == '+'))
1007 /* This is the sign of the exponent, not the end of the
1008 number. */
1009 continue;
1010 /* We will take any letters or digits. parse_number will
1011 complain if past the radix, or if L or U are not final. */
1012 else if ((*p < '0' || *p > '9')
1013 && ((*p < 'a' || *p > 'z')
1014 && (*p < 'A' || *p > 'Z')))
1015 break;
1016 }
1017 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1018 if (toktype == ERROR)
1019 {
1020 char *err_copy = (char *) alloca (p - tokstart + 1);
1021
1022 memcpy (err_copy, tokstart, p - tokstart);
1023 err_copy[p - tokstart] = 0;
1024 error (_("Invalid number \"%s\""), err_copy);
1025 }
1026 lexptr = p;
1027 return toktype;
1028 }
1029
1030 case '+':
1031 case '-':
1032 case '*':
1033 case '/':
1034 case '%':
1035 case '|':
1036 case '&':
1037 case '^':
1038 case '~':
1039 case '!':
1040 case '<':
1041 case '>':
1042 case '[':
1043 case ']':
1044 case '?':
1045 case ':':
1046 case '=':
1047 case '{':
1048 case '}':
1049 symbol:
1050 lexptr++;
1051 return c;
1052
1053 case '"':
1054
1055 /* Build the gdb internal form of the input string in tempbuf,
1056 translating any standard C escape forms seen. Note that the
1057 buffer is null byte terminated *only* for the convenience of
1058 debugging gdb itself and printing the buffer contents when
1059 the buffer contains no embedded nulls. Gdb does not depend
1060 upon the buffer being null byte terminated, it uses the length
1061 string instead. This allows gdb to handle C strings (as well
1062 as strings in other languages) with embedded null bytes */
1063
1064 tokptr = ++tokstart;
1065 tempbufindex = 0;
1066
1067 do {
1068 /* Grow the static temp buffer if necessary, including allocating
1069 the first one on demand. */
1070 if (tempbufindex + 1 >= tempbufsize)
1071 {
1072 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1073 }
1074 switch (*tokptr)
1075 {
1076 case '\0':
1077 case '"':
1078 /* Do nothing, loop will terminate. */
1079 break;
1080 case '\\':
1081 tokptr++;
1082 c = parse_escape (&tokptr);
1083 if (c == -1)
1084 {
1085 continue;
1086 }
1087 tempbuf[tempbufindex++] = c;
1088 break;
1089 default:
1090 tempbuf[tempbufindex++] = *tokptr++;
1091 break;
1092 }
1093 } while ((*tokptr != '"') && (*tokptr != '\0'));
1094 if (*tokptr++ != '"')
1095 {
1096 error (_("Unterminated string in expression"));
1097 }
1098 tempbuf[tempbufindex] = '\0'; /* See note above */
1099 yylval.sval.ptr = tempbuf;
1100 yylval.sval.length = tempbufindex;
1101 lexptr = tokptr;
1102 return (STRING_LITERAL);
1103 }
1104
1105 if (!(c == '_' || c == '$'
1106 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1107 /* We must have come across a bad character (e.g. ';'). */
1108 error (_("Invalid character '%c' in expression"), c);
1109
1110 /* It's a name. See how long it is. */
1111 namelen = 0;
1112 for (c = tokstart[namelen];
1113 (c == '_'
1114 || c == '$'
1115 || (c >= '0' && c <= '9')
1116 || (c >= 'a' && c <= 'z')
1117 || (c >= 'A' && c <= 'Z')
1118 || c == '<');
1119 )
1120 {
1121 if (c == '<')
1122 {
1123 int i = namelen;
1124 while (tokstart[++i] && tokstart[i] != '>');
1125 if (tokstart[i] == '>')
1126 namelen = i;
1127 }
1128 c = tokstart[++namelen];
1129 }
1130
1131 /* The token "if" terminates the expression and is NOT
1132 removed from the input stream. */
1133 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1134 {
1135 return 0;
1136 }
1137
1138 lexptr += namelen;
1139
1140 tryname:
1141
1142 /* Catch specific keywords. Should be done with a data structure. */
1143 switch (namelen)
1144 {
1145 case 7:
1146 if (DEPRECATED_STREQN (tokstart, "boolean", 7))
1147 return BOOLEAN;
1148 break;
1149 case 6:
1150 if (DEPRECATED_STREQN (tokstart, "double", 6))
1151 return DOUBLE;
1152 break;
1153 case 5:
1154 if (DEPRECATED_STREQN (tokstart, "short", 5))
1155 return SHORT;
1156 if (DEPRECATED_STREQN (tokstart, "false", 5))
1157 {
1158 yylval.lval = 0;
1159 return BOOLEAN_LITERAL;
1160 }
1161 if (DEPRECATED_STREQN (tokstart, "super", 5))
1162 return SUPER;
1163 if (DEPRECATED_STREQN (tokstart, "float", 5))
1164 return FLOAT;
1165 break;
1166 case 4:
1167 if (DEPRECATED_STREQN (tokstart, "long", 4))
1168 return LONG;
1169 if (DEPRECATED_STREQN (tokstart, "byte", 4))
1170 return BYTE;
1171 if (DEPRECATED_STREQN (tokstart, "char", 4))
1172 return CHAR;
1173 if (DEPRECATED_STREQN (tokstart, "true", 4))
1174 {
1175 yylval.lval = 1;
1176 return BOOLEAN_LITERAL;
1177 }
1178 break;
1179 case 3:
1180 if (strncmp (tokstart, "int", 3) == 0)
1181 return INT;
1182 if (strncmp (tokstart, "new", 3) == 0)
1183 return NEW;
1184 break;
1185 default:
1186 break;
1187 }
1188
1189 yylval.sval.ptr = tokstart;
1190 yylval.sval.length = namelen;
1191
1192 if (*tokstart == '$')
1193 {
1194 write_dollar_variable (yylval.sval);
1195 return VARIABLE;
1196 }
1197
1198 /* Input names that aren't symbols but ARE valid hex numbers,
1199 when the input radix permits them, can be names or numbers
1200 depending on the parse. Note we support radixes > 16 here. */
1201 if (((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1202 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1203 {
1204 YYSTYPE newlval; /* Its value is ignored. */
1205 int hextype = parse_number (tokstart, namelen, 0, &newlval);
1206 if (hextype == INTEGER_LITERAL)
1207 return NAME_OR_INT;
1208 }
1209 return IDENTIFIER;
1210 }
1211
1212 void
1213 yyerror (msg)
1214 char *msg;
1215 {
1216 if (prev_lexptr)
1217 lexptr = prev_lexptr;
1218
1219 if (msg)
1220 error (_("%s: near `%s'"), msg, lexptr);
1221 else
1222 error (_("error in expression, near `%s'"), lexptr);
1223 }
1224
1225 static struct type *
1226 java_type_from_name (name)
1227 struct stoken name;
1228
1229 {
1230 char *tmp = copy_name (name);
1231 struct type *typ = java_lookup_class (tmp);
1232 if (typ == NULL || TYPE_CODE (typ) != TYPE_CODE_STRUCT)
1233 error (_("No class named `%s'"), tmp);
1234 return typ;
1235 }
1236
1237 /* If NAME is a valid variable name in this scope, push it and return 1.
1238 Otherwise, return 0. */
1239
1240 static int
1241 push_variable (struct stoken name)
1242 {
1243 char *tmp = copy_name (name);
1244 int is_a_field_of_this = 0;
1245 struct symbol *sym;
1246 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN,
1247 &is_a_field_of_this, (struct symtab **) NULL);
1248 if (sym && SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1249 {
1250 if (symbol_read_needs_frame (sym))
1251 {
1252 if (innermost_block == 0 ||
1253 contained_in (block_found, innermost_block))
1254 innermost_block = block_found;
1255 }
1256
1257 write_exp_elt_opcode (OP_VAR_VALUE);
1258 /* We want to use the selected frame, not another more inner frame
1259 which happens to be in the same block. */
1260 write_exp_elt_block (NULL);
1261 write_exp_elt_sym (sym);
1262 write_exp_elt_opcode (OP_VAR_VALUE);
1263 return 1;
1264 }
1265 if (is_a_field_of_this)
1266 {
1267 /* it hangs off of `this'. Must not inadvertently convert from a
1268 method call to data ref. */
1269 if (innermost_block == 0 ||
1270 contained_in (block_found, innermost_block))
1271 innermost_block = block_found;
1272 write_exp_elt_opcode (OP_THIS);
1273 write_exp_elt_opcode (OP_THIS);
1274 write_exp_elt_opcode (STRUCTOP_PTR);
1275 write_exp_string (name);
1276 write_exp_elt_opcode (STRUCTOP_PTR);
1277 return 1;
1278 }
1279 return 0;
1280 }
1281
1282 /* Assuming a reference expression has been pushed, emit the
1283 STRUCTOP_STRUCT ops to access the field named NAME. If NAME is a
1284 qualified name (has '.'), generate a field access for each part. */
1285
1286 static void
1287 push_fieldnames (name)
1288 struct stoken name;
1289 {
1290 int i;
1291 struct stoken token;
1292 token.ptr = name.ptr;
1293 for (i = 0; ; i++)
1294 {
1295 if (i == name.length || name.ptr[i] == '.')
1296 {
1297 /* token.ptr is start of current field name. */
1298 token.length = &name.ptr[i] - token.ptr;
1299 write_exp_elt_opcode (STRUCTOP_STRUCT);
1300 write_exp_string (token);
1301 write_exp_elt_opcode (STRUCTOP_STRUCT);
1302 token.ptr += token.length + 1;
1303 }
1304 if (i >= name.length)
1305 break;
1306 }
1307 }
1308
1309 /* Helper routine for push_expression_name.
1310 Handle a qualified name, where DOT_INDEX is the index of the first '.' */
1311
1312 static void
1313 push_qualified_expression_name (struct stoken name, int dot_index)
1314 {
1315 struct stoken token;
1316 char *tmp;
1317 struct type *typ;
1318
1319 token.ptr = name.ptr;
1320 token.length = dot_index;
1321
1322 if (push_variable (token))
1323 {
1324 token.ptr = name.ptr + dot_index + 1;
1325 token.length = name.length - dot_index - 1;
1326 push_fieldnames (token);
1327 return;
1328 }
1329
1330 token.ptr = name.ptr;
1331 for (;;)
1332 {
1333 token.length = dot_index;
1334 tmp = copy_name (token);
1335 typ = java_lookup_class (tmp);
1336 if (typ != NULL)
1337 {
1338 if (dot_index == name.length)
1339 {
1340 write_exp_elt_opcode(OP_TYPE);
1341 write_exp_elt_type(typ);
1342 write_exp_elt_opcode(OP_TYPE);
1343 return;
1344 }
1345 dot_index++; /* Skip '.' */
1346 name.ptr += dot_index;
1347 name.length -= dot_index;
1348 dot_index = 0;
1349 while (dot_index < name.length && name.ptr[dot_index] != '.')
1350 dot_index++;
1351 token.ptr = name.ptr;
1352 token.length = dot_index;
1353 write_exp_elt_opcode (OP_SCOPE);
1354 write_exp_elt_type (typ);
1355 write_exp_string (token);
1356 write_exp_elt_opcode (OP_SCOPE);
1357 if (dot_index < name.length)
1358 {
1359 dot_index++;
1360 name.ptr += dot_index;
1361 name.length -= dot_index;
1362 push_fieldnames (name);
1363 }
1364 return;
1365 }
1366 else if (dot_index >= name.length)
1367 break;
1368 dot_index++; /* Skip '.' */
1369 while (dot_index < name.length && name.ptr[dot_index] != '.')
1370 dot_index++;
1371 }
1372 error (_("unknown type `%.*s'"), name.length, name.ptr);
1373 }
1374
1375 /* Handle Name in an expression (or LHS).
1376 Handle VAR, TYPE, TYPE.FIELD1....FIELDN and VAR.FIELD1....FIELDN. */
1377
1378 static void
1379 push_expression_name (name)
1380 struct stoken name;
1381 {
1382 char *tmp;
1383 struct type *typ;
1384 char *ptr;
1385 int i;
1386
1387 for (i = 0; i < name.length; i++)
1388 {
1389 if (name.ptr[i] == '.')
1390 {
1391 /* It's a Qualified Expression Name. */
1392 push_qualified_expression_name (name, i);
1393 return;
1394 }
1395 }
1396
1397 /* It's a Simple Expression Name. */
1398
1399 if (push_variable (name))
1400 return;
1401 tmp = copy_name (name);
1402 typ = java_lookup_class (tmp);
1403 if (typ != NULL)
1404 {
1405 write_exp_elt_opcode(OP_TYPE);
1406 write_exp_elt_type(typ);
1407 write_exp_elt_opcode(OP_TYPE);
1408 }
1409 else
1410 {
1411 struct minimal_symbol *msymbol;
1412
1413 msymbol = lookup_minimal_symbol (tmp, NULL, NULL);
1414 if (msymbol != NULL)
1415 {
1416 write_exp_msymbol (msymbol,
1417 lookup_function_type (builtin_type_int),
1418 builtin_type_int);
1419 }
1420 else if (!have_full_symbols () && !have_partial_symbols ())
1421 error (_("No symbol table is loaded. Use the \"file\" command"));
1422 else
1423 error (_("No symbol \"%s\" in current context"), tmp);
1424 }
1425
1426 }
1427
1428
1429 /* The following two routines, copy_exp and insert_exp, aren't specific to
1430 Java, so they could go in parse.c, but their only purpose is to support
1431 the parsing kludges we use in this file, so maybe it's best to isolate
1432 them here. */
1433
1434 /* Copy the expression whose last element is at index ENDPOS - 1 in EXPR
1435 into a freshly malloc'ed struct expression. Its language_defn is set
1436 to null. */
1437 static struct expression *
1438 copy_exp (expr, endpos)
1439 struct expression *expr;
1440 int endpos;
1441 {
1442 int len = length_of_subexp (expr, endpos);
1443 struct expression *new
1444 = (struct expression *) malloc (sizeof (*new) + EXP_ELEM_TO_BYTES (len));
1445 new->nelts = len;
1446 memcpy (new->elts, expr->elts + endpos - len, EXP_ELEM_TO_BYTES (len));
1447 new->language_defn = 0;
1448
1449 return new;
1450 }
1451
1452 /* Insert the expression NEW into the current expression (expout) at POS. */
1453 static void
1454 insert_exp (pos, new)
1455 int pos;
1456 struct expression *new;
1457 {
1458 int newlen = new->nelts;
1459
1460 /* Grow expout if necessary. In this function's only use at present,
1461 this should never be necessary. */
1462 if (expout_ptr + newlen > expout_size)
1463 {
1464 expout_size = max (expout_size * 2, expout_ptr + newlen + 10);
1465 expout = (struct expression *)
1466 realloc ((char *) expout, (sizeof (struct expression)
1467 + EXP_ELEM_TO_BYTES (expout_size)));
1468 }
1469
1470 {
1471 int i;
1472
1473 for (i = expout_ptr - 1; i >= pos; i--)
1474 expout->elts[i + newlen] = expout->elts[i];
1475 }
1476
1477 memcpy (expout->elts + pos, new->elts, EXP_ELEM_TO_BYTES (newlen));
1478 expout_ptr += newlen;
1479 }