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