Upgrade GDB from 7.4.1 to 7.6.1 on the vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / jv-exp.y
CommitLineData
5796c8dc 1/* YACC parser for Java expressions, for GDB.
ef5ccd6c 2 Copyright (C) 1997-2013 Free Software Foundation, Inc.
5796c8dc
SS
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19/* Parse a Java expression from text in a string,
20 and return the result as a struct expression pointer.
21 That structure contains arithmetic operations in reverse polish,
22 with constants represented by operations that are followed by special data.
23 See expression.h for the details of the format.
24 What is important here is that it can be built up sequentially
25 during the process of parsing; the lower levels of the tree always
26 come first in the result. Well, almost always; see ArrayAccess.
27
28 Note that malloc's and realloc's in this file are transformed to
29 xmalloc and xrealloc respectively by the same sed command in the
30 makefile that remaps any other malloc/realloc inserted by the parser
31 generator. Doing this with #defines and trying to control the interaction
32 with include files (<malloc.h> and <stdlib.h> for example) just became
33 too messy, particularly when such includes can be inserted at random
34 times by the parser generator. */
35
36%{
37
38#include "defs.h"
39#include "gdb_string.h"
40#include <ctype.h>
41#include "expression.h"
42#include "value.h"
43#include "parser-defs.h"
44#include "language.h"
45#include "jv-lang.h"
46#include "bfd.h" /* Required by objfiles.h. */
47#include "symfile.h" /* Required by objfiles.h. */
48#include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
49#include "block.h"
50
51#define parse_type builtin_type (parse_gdbarch)
52#define parse_java_type builtin_java_type (parse_gdbarch)
53
54/* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
55 as well as gratuitiously global symbol names, so we can have multiple
56 yacc generated parsers in gdb. Note that these are only the variables
57 produced by yacc. If other parser generators (bison, byacc, etc) produce
58 additional global names that conflict at link time, then those parser
c50c785c 59 generators need to be fixed instead of adding those names to this list. */
5796c8dc
SS
60
61#define yymaxdepth java_maxdepth
62#define yyparse java_parse
63#define yylex java_lex
64#define yyerror java_error
65#define yylval java_lval
66#define yychar java_char
67#define yydebug java_debug
68#define yypact java_pact
69#define yyr1 java_r1
70#define yyr2 java_r2
71#define yydef java_def
72#define yychk java_chk
73#define yypgo java_pgo
74#define yyact java_act
75#define yyexca java_exca
76#define yyerrflag java_errflag
77#define yynerrs java_nerrs
78#define yyps java_ps
79#define yypv java_pv
80#define yys java_s
81#define yy_yys java_yys
82#define yystate java_state
83#define yytmp java_tmp
84#define yyv java_v
85#define yy_yyv java_yyv
86#define yyval java_val
87#define yylloc java_lloc
88#define yyreds java_reds /* With YYDEBUG defined */
89#define yytoks java_toks /* With YYDEBUG defined */
90#define yyname java_name /* With YYDEBUG defined */
91#define yyrule java_rule /* With YYDEBUG defined */
92#define yylhs java_yylhs
93#define yylen java_yylen
94#define yydefred java_yydefred
95#define yydgoto java_yydgoto
96#define yysindex java_yysindex
97#define yyrindex java_yyrindex
98#define yygindex java_yygindex
99#define yytable java_yytable
100#define yycheck java_yycheck
ef5ccd6c
JM
101#define yyss java_yyss
102#define yysslim java_yysslim
103#define yyssp java_yyssp
104#define yystacksize java_yystacksize
105#define yyvs java_yyvs
106#define yyvsp java_yyvsp
5796c8dc
SS
107
108#ifndef YYDEBUG
109#define YYDEBUG 1 /* Default to yydebug support */
110#endif
111
112#define YYFPRINTF parser_fprintf
113
114int yyparse (void);
115
116static int yylex (void);
117
118void yyerror (char *);
119
120static struct type *java_type_from_name (struct stoken);
121static void push_expression_name (struct stoken);
122static void push_fieldnames (struct stoken);
123
124static struct expression *copy_exp (struct expression *, int);
125static void insert_exp (int, struct expression *);
126
127%}
128
129/* Although the yacc "value" of an expression is not used,
130 since the result is stored in the structure being created,
131 other node types do have values. */
132
133%union
134 {
135 LONGEST lval;
136 struct {
137 LONGEST val;
138 struct type *type;
139 } typed_val_int;
140 struct {
141 DOUBLEST dval;
142 struct type *type;
143 } typed_val_float;
144 struct symbol *sym;
145 struct type *tval;
146 struct stoken sval;
147 struct ttype tsym;
148 struct symtoken ssym;
149 struct block *bval;
150 enum exp_opcode opcode;
151 struct internalvar *ivar;
152 int *ivec;
153 }
154
155%{
156/* YYSTYPE gets defined by %union */
157static int parse_number (char *, int, int, YYSTYPE *);
158%}
159
160%type <lval> rcurly Dims Dims_opt
161%type <tval> ClassOrInterfaceType ClassType /* ReferenceType Type ArrayType */
162%type <tval> IntegralType FloatingPointType NumericType PrimitiveType ArrayType PrimitiveOrArrayType
163
164%token <typed_val_int> INTEGER_LITERAL
165%token <typed_val_float> FLOATING_POINT_LITERAL
166
167%token <sval> IDENTIFIER
168%token <sval> STRING_LITERAL
169%token <lval> BOOLEAN_LITERAL
170%token <tsym> TYPENAME
171%type <sval> Name SimpleName QualifiedName ForcedName
172
173/* A NAME_OR_INT is a symbol which is not known in the symbol table,
174 but which would parse as a valid number in the current input radix.
175 E.g. "c" when input_radix==16. Depending on the parse, it will be
176 turned into a name or into a number. */
177
178%token <sval> NAME_OR_INT
179
180%token ERROR
181
182/* Special type cases, put in to allow the parser to distinguish different
183 legal basetypes. */
184%token LONG SHORT BYTE INT CHAR BOOLEAN DOUBLE FLOAT
185
186%token VARIABLE
187
188%token <opcode> ASSIGN_MODIFY
189
190%token SUPER NEW
191
192%left ','
193%right '=' ASSIGN_MODIFY
194%right '?'
195%left OROR
196%left ANDAND
197%left '|'
198%left '^'
199%left '&'
200%left EQUAL NOTEQUAL
201%left '<' '>' LEQ GEQ
202%left LSH RSH
203%left '+' '-'
204%left '*' '/' '%'
205%right INCREMENT DECREMENT
206%right '.' '[' '('
207
208\f
209%%
210
211start : exp1
212 | type_exp
213 ;
214
215type_exp: PrimitiveOrArrayType
216 {
217 write_exp_elt_opcode(OP_TYPE);
218 write_exp_elt_type($1);
219 write_exp_elt_opcode(OP_TYPE);
220 }
221 ;
222
223PrimitiveOrArrayType:
224 PrimitiveType
225 | ArrayType
226 ;
227
228StringLiteral:
229 STRING_LITERAL
230 {
231 write_exp_elt_opcode (OP_STRING);
232 write_exp_string ($1);
233 write_exp_elt_opcode (OP_STRING);
234 }
235;
236
237Literal:
238 INTEGER_LITERAL
239 { write_exp_elt_opcode (OP_LONG);
240 write_exp_elt_type ($1.type);
241 write_exp_elt_longcst ((LONGEST)($1.val));
242 write_exp_elt_opcode (OP_LONG); }
243| NAME_OR_INT
244 { YYSTYPE val;
245 parse_number ($1.ptr, $1.length, 0, &val);
246 write_exp_elt_opcode (OP_LONG);
247 write_exp_elt_type (val.typed_val_int.type);
248 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
249 write_exp_elt_opcode (OP_LONG);
250 }
251| FLOATING_POINT_LITERAL
252 { write_exp_elt_opcode (OP_DOUBLE);
253 write_exp_elt_type ($1.type);
254 write_exp_elt_dblcst ($1.dval);
255 write_exp_elt_opcode (OP_DOUBLE); }
256| BOOLEAN_LITERAL
257 { write_exp_elt_opcode (OP_LONG);
258 write_exp_elt_type (parse_java_type->builtin_boolean);
259 write_exp_elt_longcst ((LONGEST)$1);
260 write_exp_elt_opcode (OP_LONG); }
261| StringLiteral
262 ;
263
264/* UNUSED:
265Type:
266 PrimitiveType
267| ReferenceType
268;
269*/
270
271PrimitiveType:
272 NumericType
273| BOOLEAN
274 { $$ = parse_java_type->builtin_boolean; }
275;
276
277NumericType:
278 IntegralType
279| FloatingPointType
280;
281
282IntegralType:
283 BYTE
284 { $$ = parse_java_type->builtin_byte; }
285| SHORT
286 { $$ = parse_java_type->builtin_short; }
287| INT
288 { $$ = parse_java_type->builtin_int; }
289| LONG
290 { $$ = parse_java_type->builtin_long; }
291| CHAR
292 { $$ = parse_java_type->builtin_char; }
293;
294
295FloatingPointType:
296 FLOAT
297 { $$ = parse_java_type->builtin_float; }
298| DOUBLE
299 { $$ = parse_java_type->builtin_double; }
300;
301
302/* UNUSED:
303ReferenceType:
304 ClassOrInterfaceType
305| ArrayType
306;
307*/
308
309ClassOrInterfaceType:
310 Name
311 { $$ = java_type_from_name ($1); }
312;
313
314ClassType:
315 ClassOrInterfaceType
316;
317
318ArrayType:
319 PrimitiveType Dims
320 { $$ = java_array_type ($1, $2); }
321| Name Dims
322 { $$ = java_array_type (java_type_from_name ($1), $2); }
323;
324
325Name:
326 IDENTIFIER
327| QualifiedName
328;
329
330ForcedName:
331 SimpleName
332| QualifiedName
333;
334
335SimpleName:
336 IDENTIFIER
337| NAME_OR_INT
338;
339
340QualifiedName:
341 Name '.' SimpleName
342 { $$.length = $1.length + $3.length + 1;
343 if ($1.ptr + $1.length + 1 == $3.ptr
344 && $1.ptr[$1.length] == '.')
c50c785c 345 $$.ptr = $1.ptr; /* Optimization. */
5796c8dc
SS
346 else
347 {
348 $$.ptr = (char *) malloc ($$.length + 1);
349 make_cleanup (free, $$.ptr);
350 sprintf ($$.ptr, "%.*s.%.*s",
351 $1.length, $1.ptr, $3.length, $3.ptr);
352 } }
353;
354
355/*
356type_exp: type
357 { write_exp_elt_opcode(OP_TYPE);
358 write_exp_elt_type($1);
359 write_exp_elt_opcode(OP_TYPE);}
360 ;
361 */
362
363/* Expressions, including the comma operator. */
364exp1 : Expression
365 | exp1 ',' Expression
366 { write_exp_elt_opcode (BINOP_COMMA); }
367 ;
368
369Primary:
370 PrimaryNoNewArray
371| ArrayCreationExpression
372;
373
374PrimaryNoNewArray:
375 Literal
376| '(' Expression ')'
377| ClassInstanceCreationExpression
378| FieldAccess
379| MethodInvocation
380| ArrayAccess
381| lcurly ArgumentList rcurly
382 { write_exp_elt_opcode (OP_ARRAY);
383 write_exp_elt_longcst ((LONGEST) 0);
384 write_exp_elt_longcst ((LONGEST) $3);
385 write_exp_elt_opcode (OP_ARRAY); }
386;
387
388lcurly:
389 '{'
390 { start_arglist (); }
391;
392
393rcurly:
394 '}'
395 { $$ = end_arglist () - 1; }
396;
397
398ClassInstanceCreationExpression:
399 NEW ClassType '(' ArgumentList_opt ')'
400 { internal_error (__FILE__, __LINE__,
401 _("FIXME - ClassInstanceCreationExpression")); }
402;
403
404ArgumentList:
405 Expression
406 { arglist_len = 1; }
407| ArgumentList ',' Expression
408 { arglist_len++; }
409;
410
411ArgumentList_opt:
412 /* EMPTY */
413 { arglist_len = 0; }
414| ArgumentList
415;
416
417ArrayCreationExpression:
418 NEW PrimitiveType DimExprs Dims_opt
419 { internal_error (__FILE__, __LINE__,
420 _("FIXME - ArrayCreationExpression")); }
421| NEW ClassOrInterfaceType DimExprs Dims_opt
422 { internal_error (__FILE__, __LINE__,
423 _("FIXME - ArrayCreationExpression")); }
424;
425
426DimExprs:
427 DimExpr
428| DimExprs DimExpr
429;
430
431DimExpr:
432 '[' Expression ']'
433;
434
435Dims:
436 '[' ']'
437 { $$ = 1; }
438| Dims '[' ']'
439 { $$ = $1 + 1; }
440;
441
442Dims_opt:
443 Dims
444| /* EMPTY */
445 { $$ = 0; }
446;
447
448FieldAccess:
449 Primary '.' SimpleName
450 { push_fieldnames ($3); }
451| VARIABLE '.' SimpleName
452 { push_fieldnames ($3); }
453/*| SUPER '.' SimpleName { FIXME } */
454;
455
456FuncStart:
457 Name '('
458 { push_expression_name ($1); }
459;
460
461MethodInvocation:
462 FuncStart
463 { start_arglist(); }
464 ArgumentList_opt ')'
465 { write_exp_elt_opcode (OP_FUNCALL);
466 write_exp_elt_longcst ((LONGEST) end_arglist ());
467 write_exp_elt_opcode (OP_FUNCALL); }
468| Primary '.' SimpleName '(' ArgumentList_opt ')'
469 { error (_("Form of method invocation not implemented")); }
470| SUPER '.' SimpleName '(' ArgumentList_opt ')'
471 { error (_("Form of method invocation not implemented")); }
472;
473
474ArrayAccess:
475 Name '[' Expression ']'
476 {
477 /* Emit code for the Name now, then exchange it in the
478 expout array with the Expression's code. We could
479 introduce a OP_SWAP code or a reversed version of
480 BINOP_SUBSCRIPT, but that makes the rest of GDB pay
481 for our parsing kludges. */
482 struct expression *name_expr;
483
484 push_expression_name ($1);
485 name_expr = copy_exp (expout, expout_ptr);
486 expout_ptr -= name_expr->nelts;
487 insert_exp (expout_ptr-length_of_subexp (expout, expout_ptr),
488 name_expr);
489 free (name_expr);
490 write_exp_elt_opcode (BINOP_SUBSCRIPT);
491 }
492| VARIABLE '[' Expression ']'
493 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
494| PrimaryNoNewArray '[' Expression ']'
495 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
496;
497
498PostfixExpression:
499 Primary
500| Name
501 { push_expression_name ($1); }
502| VARIABLE
c50c785c 503 /* Already written by write_dollar_variable. */
5796c8dc
SS
504| PostIncrementExpression
505| PostDecrementExpression
506;
507
508PostIncrementExpression:
509 PostfixExpression INCREMENT
510 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
511;
512
513PostDecrementExpression:
514 PostfixExpression DECREMENT
515 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
516;
517
518UnaryExpression:
519 PreIncrementExpression
520| PreDecrementExpression
521| '+' UnaryExpression
522| '-' UnaryExpression
523 { write_exp_elt_opcode (UNOP_NEG); }
524| '*' UnaryExpression
525 { write_exp_elt_opcode (UNOP_IND); } /*FIXME not in Java */
526| UnaryExpressionNotPlusMinus
527;
528
529PreIncrementExpression:
530 INCREMENT UnaryExpression
531 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
532;
533
534PreDecrementExpression:
535 DECREMENT UnaryExpression
536 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
537;
538
539UnaryExpressionNotPlusMinus:
540 PostfixExpression
541| '~' UnaryExpression
542 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
543| '!' UnaryExpression
544 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
545| CastExpression
546 ;
547
548CastExpression:
549 '(' PrimitiveType Dims_opt ')' UnaryExpression
550 { write_exp_elt_opcode (UNOP_CAST);
551 write_exp_elt_type (java_array_type ($2, $3));
552 write_exp_elt_opcode (UNOP_CAST); }
553| '(' Expression ')' UnaryExpressionNotPlusMinus
554 {
5796c8dc
SS
555 int last_exp_size = length_of_subexp(expout, expout_ptr);
556 struct type *type;
557 int i;
558 int base = expout_ptr - last_exp_size - 3;
559 if (base < 0 || expout->elts[base+2].opcode != OP_TYPE)
560 error (_("Invalid cast expression"));
561 type = expout->elts[base+1].type;
562 /* Remove the 'Expression' and slide the
c50c785c 563 UnaryExpressionNotPlusMinus down to replace it. */
5796c8dc
SS
564 for (i = 0; i < last_exp_size; i++)
565 expout->elts[base + i] = expout->elts[base + i + 3];
566 expout_ptr -= 3;
567 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
568 type = lookup_pointer_type (type);
569 write_exp_elt_opcode (UNOP_CAST);
570 write_exp_elt_type (type);
571 write_exp_elt_opcode (UNOP_CAST);
572 }
573| '(' Name Dims ')' UnaryExpressionNotPlusMinus
574 { write_exp_elt_opcode (UNOP_CAST);
575 write_exp_elt_type (java_array_type (java_type_from_name ($2), $3));
576 write_exp_elt_opcode (UNOP_CAST); }
577;
578
579
580MultiplicativeExpression:
581 UnaryExpression
582| MultiplicativeExpression '*' UnaryExpression
583 { write_exp_elt_opcode (BINOP_MUL); }
584| MultiplicativeExpression '/' UnaryExpression
585 { write_exp_elt_opcode (BINOP_DIV); }
586| MultiplicativeExpression '%' UnaryExpression
587 { write_exp_elt_opcode (BINOP_REM); }
588;
589
590AdditiveExpression:
591 MultiplicativeExpression
592| AdditiveExpression '+' MultiplicativeExpression
593 { write_exp_elt_opcode (BINOP_ADD); }
594| AdditiveExpression '-' MultiplicativeExpression
595 { write_exp_elt_opcode (BINOP_SUB); }
596;
597
598ShiftExpression:
599 AdditiveExpression
600| ShiftExpression LSH AdditiveExpression
601 { write_exp_elt_opcode (BINOP_LSH); }
602| ShiftExpression RSH AdditiveExpression
603 { write_exp_elt_opcode (BINOP_RSH); }
604/* | ShiftExpression >>> AdditiveExpression { FIXME } */
605;
606
607RelationalExpression:
608 ShiftExpression
609| RelationalExpression '<' ShiftExpression
610 { write_exp_elt_opcode (BINOP_LESS); }
611| RelationalExpression '>' ShiftExpression
612 { write_exp_elt_opcode (BINOP_GTR); }
613| RelationalExpression LEQ ShiftExpression
614 { write_exp_elt_opcode (BINOP_LEQ); }
615| RelationalExpression GEQ ShiftExpression
616 { write_exp_elt_opcode (BINOP_GEQ); }
617/* | RelationalExpresion INSTANCEOF ReferenceType { FIXME } */
618;
619
620EqualityExpression:
621 RelationalExpression
622| EqualityExpression EQUAL RelationalExpression
623 { write_exp_elt_opcode (BINOP_EQUAL); }
624| EqualityExpression NOTEQUAL RelationalExpression
625 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
626;
627
628AndExpression:
629 EqualityExpression
630| AndExpression '&' EqualityExpression
631 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
632;
633
634ExclusiveOrExpression:
635 AndExpression
636| ExclusiveOrExpression '^' AndExpression
637 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
638;
639InclusiveOrExpression:
640 ExclusiveOrExpression
641| InclusiveOrExpression '|' ExclusiveOrExpression
642 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
643;
644
645ConditionalAndExpression:
646 InclusiveOrExpression
647| ConditionalAndExpression ANDAND InclusiveOrExpression
648 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
649;
650
651ConditionalOrExpression:
652 ConditionalAndExpression
653| ConditionalOrExpression OROR ConditionalAndExpression
654 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
655;
656
657ConditionalExpression:
658 ConditionalOrExpression
659| ConditionalOrExpression '?' Expression ':' ConditionalExpression
660 { write_exp_elt_opcode (TERNOP_COND); }
661;
662
663AssignmentExpression:
664 ConditionalExpression
665| Assignment
666;
667
668Assignment:
669 LeftHandSide '=' ConditionalExpression
670 { write_exp_elt_opcode (BINOP_ASSIGN); }
671| LeftHandSide ASSIGN_MODIFY ConditionalExpression
672 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
673 write_exp_elt_opcode ($2);
674 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
675;
676
677LeftHandSide:
678 ForcedName
679 { push_expression_name ($1); }
680| VARIABLE
c50c785c 681 /* Already written by write_dollar_variable. */
5796c8dc
SS
682| FieldAccess
683| ArrayAccess
684;
685
686
687Expression:
688 AssignmentExpression
689;
690
691%%
692/* Take care of parsing a number (anything that starts with a digit).
693 Set yylval and return the token type; update lexptr.
694 LEN is the number of characters in it. */
695
696/*** Needs some error checking for the float case ***/
697
698static int
699parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
700{
701 ULONGEST n = 0;
702 ULONGEST limit, limit_div_base;
703
704 int c;
705 int base = input_radix;
706
707 struct type *type;
708
709 if (parsed_float)
710 {
c50c785c
JM
711 const char *suffix;
712 int suffix_len;
713
714 if (! parse_float (p, len, &putithere->typed_val_float.dval, &suffix))
5796c8dc 715 return ERROR;
5796c8dc 716
c50c785c 717 suffix_len = p + len - suffix;
5796c8dc 718
c50c785c 719 if (suffix_len == 0)
5796c8dc 720 putithere->typed_val_float.type = parse_type->builtin_double;
c50c785c
JM
721 else if (suffix_len == 1)
722 {
723 /* See if it has `f' or `d' suffix (float or double). */
724 if (tolower (*suffix) == 'f')
725 putithere->typed_val_float.type =
726 parse_type->builtin_float;
727 else if (tolower (*suffix) == 'd')
728 putithere->typed_val_float.type =
729 parse_type->builtin_double;
730 else
731 return ERROR;
732 }
5796c8dc
SS
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];
c50c785c 771 /* A paranoid calculation of (1<<64)-1. */
5796c8dc
SS
772 limit = (ULONGEST)0xffffffff;
773 limit = ((limit << 16) << 16) | limit;
774 if (c == 'l' || c == 'L')
775 {
776 type = parse_java_type->builtin_long;
777 len--;
778 }
779 else
780 {
781 type = parse_java_type->builtin_int;
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)
800 error (_("Numeric constant too large"));
801 n += c;
802 }
803
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
806 parse_type->builtin_uint64 rather than parse_java_type->builtin_long.
807 0x80000000 will become -0x80000000 instead of 0x80000000L, because we
808 don't know the sign at this point. */
809 if (type == parse_java_type->builtin_int && n > (ULONGEST)0x80000000)
810 type = parse_type->builtin_uint64;
811
812 putithere->typed_val_int.val = n;
813 putithere->typed_val_int.type = type;
814
815 return INTEGER_LITERAL;
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 (void)
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
869 prev_lexptr = lexptr;
870
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 (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
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 (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
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++
c50c785c 904 for example). */
5796c8dc
SS
905 lexptr++;
906 c = *lexptr++;
907 if (c == '\\')
cf7f2e2d 908 c = parse_escape (parse_gdbarch, &lexptr);
5796c8dc
SS
909 else if (c == '\'')
910 error (_("Empty character constant"));
911
912 yylval.typed_val_int.val = c;
913 yylval.typed_val_int.type = parse_java_type->builtin_char;
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] != '\'')
923 error (_("Unmatched single quote"));
924 namelen -= 2;
925 tokstart++;
926 goto tryname;
927 }
928 error (_("Invalid character constant"));
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')
c50c785c 953 goto symbol; /* Nope, must be a symbol. */
5796c8dc
SS
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 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;
1013 error (_("Invalid number \"%s\""), err_copy);
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
c50c785c 1058 the first one on demand. */
5796c8dc
SS
1059 if (tempbufindex + 1 >= tempbufsize)
1060 {
1061 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1062 }
1063 switch (*tokptr)
1064 {
1065 case '\0':
1066 case '"':
c50c785c 1067 /* Do nothing, loop will terminate. */
5796c8dc
SS
1068 break;
1069 case '\\':
1070 tokptr++;
cf7f2e2d 1071 c = parse_escape (parse_gdbarch, &tokptr);
5796c8dc
SS
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 {
1085 error (_("Unterminated string in expression"));
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. ';'). */
1097 error (_("Invalid character '%c' in expression"), c);
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 (strncmp (tokstart, "boolean", 7) == 0)
1136 return BOOLEAN;
1137 break;
1138 case 6:
1139 if (strncmp (tokstart, "double", 6) == 0)
1140 return DOUBLE;
1141 break;
1142 case 5:
1143 if (strncmp (tokstart, "short", 5) == 0)
1144 return SHORT;
1145 if (strncmp (tokstart, "false", 5) == 0)
1146 {
1147 yylval.lval = 0;
1148 return BOOLEAN_LITERAL;
1149 }
1150 if (strncmp (tokstart, "super", 5) == 0)
1151 return SUPER;
1152 if (strncmp (tokstart, "float", 5) == 0)
1153 return FLOAT;
1154 break;
1155 case 4:
1156 if (strncmp (tokstart, "long", 4) == 0)
1157 return LONG;
1158 if (strncmp (tokstart, "byte", 4) == 0)
1159 return BYTE;
1160 if (strncmp (tokstart, "char", 4) == 0)
1161 return CHAR;
1162 if (strncmp (tokstart, "true", 4) == 0)
1163 {
1164 yylval.lval = 1;
1165 return BOOLEAN_LITERAL;
1166 }
1167 break;
1168 case 3:
1169 if (strncmp (tokstart, "int", 3) == 0)
1170 return INT;
1171 if (strncmp (tokstart, "new", 3) == 0)
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 (char *msg)
1203{
1204 if (prev_lexptr)
1205 lexptr = prev_lexptr;
1206
1207 if (msg)
1208 error (_("%s: near `%s'"), msg, lexptr);
1209 else
1210 error (_("error in expression, near `%s'"), lexptr);
1211}
1212
1213static struct type *
1214java_type_from_name (struct stoken name)
1215{
1216 char *tmp = copy_name (name);
1217 struct type *typ = java_lookup_class (tmp);
1218 if (typ == NULL || TYPE_CODE (typ) != TYPE_CODE_STRUCT)
1219 error (_("No class named `%s'"), tmp);
1220 return typ;
1221}
1222
1223/* If NAME is a valid variable name in this scope, push it and return 1.
c50c785c 1224 Otherwise, return 0. */
5796c8dc
SS
1225
1226static int
1227push_variable (struct stoken name)
1228{
1229 char *tmp = copy_name (name);
ef5ccd6c 1230 struct field_of_this_result is_a_field_of_this;
5796c8dc
SS
1231 struct symbol *sym;
1232 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN,
1233 &is_a_field_of_this);
1234 if (sym && SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1235 {
1236 if (symbol_read_needs_frame (sym))
1237 {
1238 if (innermost_block == 0 ||
1239 contained_in (block_found, innermost_block))
1240 innermost_block = block_found;
1241 }
1242
1243 write_exp_elt_opcode (OP_VAR_VALUE);
1244 /* We want to use the selected frame, not another more inner frame
1245 which happens to be in the same block. */
1246 write_exp_elt_block (NULL);
1247 write_exp_elt_sym (sym);
1248 write_exp_elt_opcode (OP_VAR_VALUE);
1249 return 1;
1250 }
ef5ccd6c 1251 if (is_a_field_of_this.type != NULL)
5796c8dc
SS
1252 {
1253 /* it hangs off of `this'. Must not inadvertently convert from a
1254 method call to data ref. */
1255 if (innermost_block == 0 ||
1256 contained_in (block_found, innermost_block))
1257 innermost_block = block_found;
1258 write_exp_elt_opcode (OP_THIS);
1259 write_exp_elt_opcode (OP_THIS);
1260 write_exp_elt_opcode (STRUCTOP_PTR);
1261 write_exp_string (name);
1262 write_exp_elt_opcode (STRUCTOP_PTR);
1263 return 1;
1264 }
1265 return 0;
1266}
1267
1268/* Assuming a reference expression has been pushed, emit the
1269 STRUCTOP_PTR ops to access the field named NAME. If NAME is a
c50c785c 1270 qualified name (has '.'), generate a field access for each part. */
5796c8dc
SS
1271
1272static void
1273push_fieldnames (struct stoken name)
1274{
1275 int i;
1276 struct stoken token;
1277 token.ptr = name.ptr;
1278 for (i = 0; ; i++)
1279 {
1280 if (i == name.length || name.ptr[i] == '.')
1281 {
c50c785c 1282 /* token.ptr is start of current field name. */
5796c8dc
SS
1283 token.length = &name.ptr[i] - token.ptr;
1284 write_exp_elt_opcode (STRUCTOP_PTR);
1285 write_exp_string (token);
1286 write_exp_elt_opcode (STRUCTOP_PTR);
1287 token.ptr += token.length + 1;
1288 }
1289 if (i >= name.length)
1290 break;
1291 }
1292}
1293
1294/* Helper routine for push_expression_name.
1295 Handle a qualified name, where DOT_INDEX is the index of the first '.' */
1296
1297static void
1298push_qualified_expression_name (struct stoken name, int dot_index)
1299{
1300 struct stoken token;
1301 char *tmp;
1302 struct type *typ;
1303
1304 token.ptr = name.ptr;
1305 token.length = dot_index;
1306
1307 if (push_variable (token))
1308 {
1309 token.ptr = name.ptr + dot_index + 1;
1310 token.length = name.length - dot_index - 1;
1311 push_fieldnames (token);
1312 return;
1313 }
1314
1315 token.ptr = name.ptr;
1316 for (;;)
1317 {
1318 token.length = dot_index;
1319 tmp = copy_name (token);
1320 typ = java_lookup_class (tmp);
1321 if (typ != NULL)
1322 {
1323 if (dot_index == name.length)
1324 {
1325 write_exp_elt_opcode(OP_TYPE);
1326 write_exp_elt_type(typ);
1327 write_exp_elt_opcode(OP_TYPE);
1328 return;
1329 }
1330 dot_index++; /* Skip '.' */
1331 name.ptr += dot_index;
1332 name.length -= dot_index;
1333 dot_index = 0;
1334 while (dot_index < name.length && name.ptr[dot_index] != '.')
1335 dot_index++;
1336 token.ptr = name.ptr;
1337 token.length = dot_index;
1338 write_exp_elt_opcode (OP_SCOPE);
1339 write_exp_elt_type (typ);
1340 write_exp_string (token);
1341 write_exp_elt_opcode (OP_SCOPE);
1342 if (dot_index < name.length)
1343 {
1344 dot_index++;
1345 name.ptr += dot_index;
1346 name.length -= dot_index;
1347 push_fieldnames (name);
1348 }
1349 return;
1350 }
1351 else if (dot_index >= name.length)
1352 break;
1353 dot_index++; /* Skip '.' */
1354 while (dot_index < name.length && name.ptr[dot_index] != '.')
1355 dot_index++;
1356 }
1357 error (_("unknown type `%.*s'"), name.length, name.ptr);
1358}
1359
1360/* Handle Name in an expression (or LHS).
c50c785c 1361 Handle VAR, TYPE, TYPE.FIELD1....FIELDN and VAR.FIELD1....FIELDN. */
5796c8dc
SS
1362
1363static void
1364push_expression_name (struct stoken name)
1365{
1366 char *tmp;
1367 struct type *typ;
5796c8dc
SS
1368 int i;
1369
1370 for (i = 0; i < name.length; i++)
1371 {
1372 if (name.ptr[i] == '.')
1373 {
c50c785c 1374 /* It's a Qualified Expression Name. */
5796c8dc
SS
1375 push_qualified_expression_name (name, i);
1376 return;
1377 }
1378 }
1379
c50c785c 1380 /* It's a Simple Expression Name. */
5796c8dc
SS
1381
1382 if (push_variable (name))
1383 return;
1384 tmp = copy_name (name);
1385 typ = java_lookup_class (tmp);
1386 if (typ != NULL)
1387 {
1388 write_exp_elt_opcode(OP_TYPE);
1389 write_exp_elt_type(typ);
1390 write_exp_elt_opcode(OP_TYPE);
1391 }
1392 else
1393 {
1394 struct minimal_symbol *msymbol;
1395
1396 msymbol = lookup_minimal_symbol (tmp, NULL, NULL);
1397 if (msymbol != NULL)
1398 write_exp_msymbol (msymbol);
1399 else if (!have_full_symbols () && !have_partial_symbols ())
1400 error (_("No symbol table is loaded. Use the \"file\" command"));
1401 else
ef5ccd6c 1402 error (_("No symbol \"%s\" in current context."), tmp);
5796c8dc
SS
1403 }
1404
1405}
1406
1407
1408/* The following two routines, copy_exp and insert_exp, aren't specific to
1409 Java, so they could go in parse.c, but their only purpose is to support
1410 the parsing kludges we use in this file, so maybe it's best to isolate
1411 them here. */
1412
1413/* Copy the expression whose last element is at index ENDPOS - 1 in EXPR
1414 into a freshly malloc'ed struct expression. Its language_defn is set
1415 to null. */
1416static struct expression *
1417copy_exp (struct expression *expr, int endpos)
1418{
1419 int len = length_of_subexp (expr, endpos);
1420 struct expression *new
1421 = (struct expression *) malloc (sizeof (*new) + EXP_ELEM_TO_BYTES (len));
1422 new->nelts = len;
1423 memcpy (new->elts, expr->elts + endpos - len, EXP_ELEM_TO_BYTES (len));
1424 new->language_defn = 0;
1425
1426 return new;
1427}
1428
1429/* Insert the expression NEW into the current expression (expout) at POS. */
1430static void
1431insert_exp (int pos, struct expression *new)
1432{
1433 int newlen = new->nelts;
1434
1435 /* Grow expout if necessary. In this function's only use at present,
1436 this should never be necessary. */
1437 if (expout_ptr + newlen > expout_size)
1438 {
1439 expout_size = max (expout_size * 2, expout_ptr + newlen + 10);
1440 expout = (struct expression *)
1441 realloc ((char *) expout, (sizeof (struct expression)
1442 + EXP_ELEM_TO_BYTES (expout_size)));
1443 }
1444
1445 {
1446 int i;
1447
1448 for (i = expout_ptr - 1; i >= pos; i--)
1449 expout->elts[i + newlen] = expout->elts[i];
1450 }
1451
1452 memcpy (expout->elts + pos, new->elts, EXP_ELEM_TO_BYTES (newlen));
1453 expout_ptr += newlen;
1454}