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