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