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