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