AMD64 - Fix format conversions and other warnings.
[dragonfly.git] / contrib / gdb-6 / gdb / jv-exp.y
blobfcbb84f2c7c847f1b85316c94e78678b6f49f941
1 /* YACC parser for Java expressions, for GDB.
2 Copyright (C) 1997, 1998, 1999, 2000, 2006, 2007
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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. */
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.
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. */
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"
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
59 generators need to be fixed instead of adding those names to this list. */
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
102 #ifndef YYDEBUG
103 #define YYDEBUG 1 /* Default to yydebug support */
104 #endif
106 #define YYFPRINTF parser_fprintf
108 int yyparse (void);
110 static int yylex (void);
112 void yyerror (char *);
114 static struct type *java_type_from_name (struct stoken);
115 static void push_expression_name (struct stoken);
116 static void push_fieldnames (struct stoken);
118 static struct expression *copy_exp (struct expression *, int);
119 static void insert_exp (int, struct expression *);
123 /* Although the yacc "value" of an expression is not used,
124 since the result is stored in the structure being created,
125 other node types do have values. */
127 %union
129 LONGEST lval;
130 struct {
131 LONGEST val;
132 struct type *type;
133 } typed_val_int;
134 struct {
135 DOUBLEST dval;
136 struct type *type;
137 } typed_val_float;
138 struct symbol *sym;
139 struct type *tval;
140 struct stoken sval;
141 struct ttype tsym;
142 struct symtoken ssym;
143 struct block *bval;
144 enum exp_opcode opcode;
145 struct internalvar *ivar;
146 int *ivec;
150 /* YYSTYPE gets defined by %union */
151 static int parse_number (char *, int, int, YYSTYPE *);
154 %type <lval> rcurly Dims Dims_opt
155 %type <tval> ClassOrInterfaceType ClassType /* ReferenceType Type ArrayType */
156 %type <tval> IntegralType FloatingPointType NumericType PrimitiveType ArrayType PrimitiveOrArrayType
158 %token <typed_val_int> INTEGER_LITERAL
159 %token <typed_val_float> FLOATING_POINT_LITERAL
161 %token <sval> IDENTIFIER
162 %token <sval> STRING_LITERAL
163 %token <lval> BOOLEAN_LITERAL
164 %token <tsym> TYPENAME
165 %type <sval> Name SimpleName QualifiedName ForcedName
167 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
168 but which would parse as a valid number in the current input radix.
169 E.g. "c" when input_radix==16. Depending on the parse, it will be
170 turned into a name or into a number. */
172 %token <sval> NAME_OR_INT
174 %token ERROR
176 /* Special type cases, put in to allow the parser to distinguish different
177 legal basetypes. */
178 %token LONG SHORT BYTE INT CHAR BOOLEAN DOUBLE FLOAT
180 %token VARIABLE
182 %token <opcode> ASSIGN_MODIFY
184 %token SUPER NEW
186 %left ','
187 %right '=' ASSIGN_MODIFY
188 %right '?'
189 %left OROR
190 %left ANDAND
191 %left '|'
192 %left '^'
193 %left '&'
194 %left EQUAL NOTEQUAL
195 %left '<' '>' LEQ GEQ
196 %left LSH RSH
197 %left '+' '-'
198 %left '*' '/' '%'
199 %right INCREMENT DECREMENT
200 %right '.' '[' '('
205 start : exp1
206 | type_exp
209 type_exp: PrimitiveOrArrayType
211 write_exp_elt_opcode(OP_TYPE);
212 write_exp_elt_type($1);
213 write_exp_elt_opcode(OP_TYPE);
217 PrimitiveOrArrayType:
218 PrimitiveType
219 | ArrayType
222 StringLiteral:
223 STRING_LITERAL
225 write_exp_elt_opcode (OP_STRING);
226 write_exp_string ($1);
227 write_exp_elt_opcode (OP_STRING);
231 Literal:
232 INTEGER_LITERAL
233 { write_exp_elt_opcode (OP_LONG);
234 write_exp_elt_type ($1.type);
235 write_exp_elt_longcst ((LONGEST)($1.val));
236 write_exp_elt_opcode (OP_LONG); }
237 | NAME_OR_INT
238 { YYSTYPE val;
239 parse_number ($1.ptr, $1.length, 0, &val);
240 write_exp_elt_opcode (OP_LONG);
241 write_exp_elt_type (val.typed_val_int.type);
242 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
243 write_exp_elt_opcode (OP_LONG);
245 | FLOATING_POINT_LITERAL
246 { write_exp_elt_opcode (OP_DOUBLE);
247 write_exp_elt_type ($1.type);
248 write_exp_elt_dblcst ($1.dval);
249 write_exp_elt_opcode (OP_DOUBLE); }
250 | BOOLEAN_LITERAL
251 { write_exp_elt_opcode (OP_LONG);
252 write_exp_elt_type (java_boolean_type);
253 write_exp_elt_longcst ((LONGEST)$1);
254 write_exp_elt_opcode (OP_LONG); }
255 | StringLiteral
258 /* UNUSED:
259 Type:
260 PrimitiveType
261 | ReferenceType
265 PrimitiveType:
266 NumericType
267 | BOOLEAN
268 { $$ = java_boolean_type; }
271 NumericType:
272 IntegralType
273 | FloatingPointType
276 IntegralType:
277 BYTE
278 { $$ = java_byte_type; }
279 | SHORT
280 { $$ = java_short_type; }
281 | INT
282 { $$ = java_int_type; }
283 | LONG
284 { $$ = java_long_type; }
285 | CHAR
286 { $$ = java_char_type; }
289 FloatingPointType:
290 FLOAT
291 { $$ = java_float_type; }
292 | DOUBLE
293 { $$ = java_double_type; }
296 /* UNUSED:
297 ReferenceType:
298 ClassOrInterfaceType
299 | ArrayType
303 ClassOrInterfaceType:
304 Name
305 { $$ = java_type_from_name ($1); }
308 ClassType:
309 ClassOrInterfaceType
312 ArrayType:
313 PrimitiveType Dims
314 { $$ = java_array_type ($1, $2); }
315 | Name Dims
316 { $$ = java_array_type (java_type_from_name ($1), $2); }
319 Name:
320 IDENTIFIER
321 | QualifiedName
324 ForcedName:
325 SimpleName
326 | QualifiedName
329 SimpleName:
330 IDENTIFIER
331 | NAME_OR_INT
334 QualifiedName:
335 Name '.' SimpleName
336 { $$.length = $1.length + $3.length + 1;
337 if ($1.ptr + $1.length + 1 == $3.ptr
338 && $1.ptr[$1.length] == '.')
339 $$.ptr = $1.ptr; /* Optimization. */
340 else
342 $$.ptr = (char *) malloc ($$.length + 1);
343 make_cleanup (free, $$.ptr);
344 sprintf ($$.ptr, "%.*s.%.*s",
345 $1.length, $1.ptr, $3.length, $3.ptr);
350 type_exp: type
351 { write_exp_elt_opcode(OP_TYPE);
352 write_exp_elt_type($1);
353 write_exp_elt_opcode(OP_TYPE);}
357 /* Expressions, including the comma operator. */
358 exp1 : Expression
359 | exp1 ',' Expression
360 { write_exp_elt_opcode (BINOP_COMMA); }
363 Primary:
364 PrimaryNoNewArray
365 | ArrayCreationExpression
368 PrimaryNoNewArray:
369 Literal
370 | '(' Expression ')'
371 | ClassInstanceCreationExpression
372 | FieldAccess
373 | MethodInvocation
374 | ArrayAccess
375 | lcurly ArgumentList rcurly
376 { write_exp_elt_opcode (OP_ARRAY);
377 write_exp_elt_longcst ((LONGEST) 0);
378 write_exp_elt_longcst ((LONGEST) $3);
379 write_exp_elt_opcode (OP_ARRAY); }
382 lcurly:
384 { start_arglist (); }
387 rcurly:
389 { $$ = end_arglist () - 1; }
392 ClassInstanceCreationExpression:
393 NEW ClassType '(' ArgumentList_opt ')'
394 { internal_error (__FILE__, __LINE__,
395 _("FIXME - ClassInstanceCreationExpression")); }
398 ArgumentList:
399 Expression
400 { arglist_len = 1; }
401 | ArgumentList ',' Expression
402 { arglist_len++; }
405 ArgumentList_opt:
406 /* EMPTY */
407 { arglist_len = 0; }
408 | ArgumentList
411 ArrayCreationExpression:
412 NEW PrimitiveType DimExprs Dims_opt
413 { internal_error (__FILE__, __LINE__,
414 _("FIXME - ArrayCreationExpression")); }
415 | NEW ClassOrInterfaceType DimExprs Dims_opt
416 { internal_error (__FILE__, __LINE__,
417 _("FIXME - ArrayCreationExpression")); }
420 DimExprs:
421 DimExpr
422 | DimExprs DimExpr
425 DimExpr:
426 '[' Expression ']'
429 Dims:
430 '[' ']'
431 { $$ = 1; }
432 | Dims '[' ']'
433 { $$ = $1 + 1; }
436 Dims_opt:
437 Dims
438 | /* EMPTY */
439 { $$ = 0; }
442 FieldAccess:
443 Primary '.' SimpleName
444 { push_fieldnames ($3); }
445 | VARIABLE '.' SimpleName
446 { push_fieldnames ($3); }
447 /*| SUPER '.' SimpleName { FIXME } */
450 FuncStart:
451 Name '('
452 { push_expression_name ($1); }
455 MethodInvocation:
456 FuncStart
457 { start_arglist(); }
458 ArgumentList_opt ')'
459 { write_exp_elt_opcode (OP_FUNCALL);
460 write_exp_elt_longcst ((LONGEST) end_arglist ());
461 write_exp_elt_opcode (OP_FUNCALL); }
462 | Primary '.' SimpleName '(' ArgumentList_opt ')'
463 { error (_("Form of method invocation not implemented")); }
464 | SUPER '.' SimpleName '(' ArgumentList_opt ')'
465 { error (_("Form of method invocation not implemented")); }
468 ArrayAccess:
469 Name '[' Expression ']'
471 /* Emit code for the Name now, then exchange it in the
472 expout array with the Expression's code. We could
473 introduce a OP_SWAP code or a reversed version of
474 BINOP_SUBSCRIPT, but that makes the rest of GDB pay
475 for our parsing kludges. */
476 struct expression *name_expr;
478 push_expression_name ($1);
479 name_expr = copy_exp (expout, expout_ptr);
480 expout_ptr -= name_expr->nelts;
481 insert_exp (expout_ptr-length_of_subexp (expout, expout_ptr),
482 name_expr);
483 free (name_expr);
484 write_exp_elt_opcode (BINOP_SUBSCRIPT);
486 | VARIABLE '[' Expression ']'
487 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
488 | PrimaryNoNewArray '[' Expression ']'
489 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
492 PostfixExpression:
493 Primary
494 | Name
495 { push_expression_name ($1); }
496 | VARIABLE
497 /* Already written by write_dollar_variable. */
498 | PostIncrementExpression
499 | PostDecrementExpression
502 PostIncrementExpression:
503 PostfixExpression INCREMENT
504 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
507 PostDecrementExpression:
508 PostfixExpression DECREMENT
509 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
512 UnaryExpression:
513 PreIncrementExpression
514 | PreDecrementExpression
515 | '+' UnaryExpression
516 | '-' UnaryExpression
517 { write_exp_elt_opcode (UNOP_NEG); }
518 | '*' UnaryExpression
519 { write_exp_elt_opcode (UNOP_IND); } /*FIXME not in Java */
520 | UnaryExpressionNotPlusMinus
523 PreIncrementExpression:
524 INCREMENT UnaryExpression
525 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
528 PreDecrementExpression:
529 DECREMENT UnaryExpression
530 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
533 UnaryExpressionNotPlusMinus:
534 PostfixExpression
535 | '~' UnaryExpression
536 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
537 | '!' UnaryExpression
538 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
539 | CastExpression
542 CastExpression:
543 '(' PrimitiveType Dims_opt ')' UnaryExpression
544 { write_exp_elt_opcode (UNOP_CAST);
545 write_exp_elt_type (java_array_type ($2, $3));
546 write_exp_elt_opcode (UNOP_CAST); }
547 | '(' Expression ')' UnaryExpressionNotPlusMinus
549 int exp_size = expout_ptr;
550 int last_exp_size = length_of_subexp(expout, expout_ptr);
551 struct type *type;
552 int i;
553 int base = expout_ptr - last_exp_size - 3;
554 if (base < 0 || expout->elts[base+2].opcode != OP_TYPE)
555 error (_("Invalid cast expression"));
556 type = expout->elts[base+1].type;
557 /* Remove the 'Expression' and slide the
558 UnaryExpressionNotPlusMinus down to replace it. */
559 for (i = 0; i < last_exp_size; i++)
560 expout->elts[base + i] = expout->elts[base + i + 3];
561 expout_ptr -= 3;
562 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
563 type = lookup_pointer_type (type);
564 write_exp_elt_opcode (UNOP_CAST);
565 write_exp_elt_type (type);
566 write_exp_elt_opcode (UNOP_CAST);
568 | '(' Name Dims ')' UnaryExpressionNotPlusMinus
569 { write_exp_elt_opcode (UNOP_CAST);
570 write_exp_elt_type (java_array_type (java_type_from_name ($2), $3));
571 write_exp_elt_opcode (UNOP_CAST); }
575 MultiplicativeExpression:
576 UnaryExpression
577 | MultiplicativeExpression '*' UnaryExpression
578 { write_exp_elt_opcode (BINOP_MUL); }
579 | MultiplicativeExpression '/' UnaryExpression
580 { write_exp_elt_opcode (BINOP_DIV); }
581 | MultiplicativeExpression '%' UnaryExpression
582 { write_exp_elt_opcode (BINOP_REM); }
585 AdditiveExpression:
586 MultiplicativeExpression
587 | AdditiveExpression '+' MultiplicativeExpression
588 { write_exp_elt_opcode (BINOP_ADD); }
589 | AdditiveExpression '-' MultiplicativeExpression
590 { write_exp_elt_opcode (BINOP_SUB); }
593 ShiftExpression:
594 AdditiveExpression
595 | ShiftExpression LSH AdditiveExpression
596 { write_exp_elt_opcode (BINOP_LSH); }
597 | ShiftExpression RSH AdditiveExpression
598 { write_exp_elt_opcode (BINOP_RSH); }
599 /* | ShiftExpression >>> AdditiveExpression { FIXME } */
602 RelationalExpression:
603 ShiftExpression
604 | RelationalExpression '<' ShiftExpression
605 { write_exp_elt_opcode (BINOP_LESS); }
606 | RelationalExpression '>' ShiftExpression
607 { write_exp_elt_opcode (BINOP_GTR); }
608 | RelationalExpression LEQ ShiftExpression
609 { write_exp_elt_opcode (BINOP_LEQ); }
610 | RelationalExpression GEQ ShiftExpression
611 { write_exp_elt_opcode (BINOP_GEQ); }
612 /* | RelationalExpresion INSTANCEOF ReferenceType { FIXME } */
615 EqualityExpression:
616 RelationalExpression
617 | EqualityExpression EQUAL RelationalExpression
618 { write_exp_elt_opcode (BINOP_EQUAL); }
619 | EqualityExpression NOTEQUAL RelationalExpression
620 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
623 AndExpression:
624 EqualityExpression
625 | AndExpression '&' EqualityExpression
626 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
629 ExclusiveOrExpression:
630 AndExpression
631 | ExclusiveOrExpression '^' AndExpression
632 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
634 InclusiveOrExpression:
635 ExclusiveOrExpression
636 | InclusiveOrExpression '|' ExclusiveOrExpression
637 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
640 ConditionalAndExpression:
641 InclusiveOrExpression
642 | ConditionalAndExpression ANDAND InclusiveOrExpression
643 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
646 ConditionalOrExpression:
647 ConditionalAndExpression
648 | ConditionalOrExpression OROR ConditionalAndExpression
649 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
652 ConditionalExpression:
653 ConditionalOrExpression
654 | ConditionalOrExpression '?' Expression ':' ConditionalExpression
655 { write_exp_elt_opcode (TERNOP_COND); }
658 AssignmentExpression:
659 ConditionalExpression
660 | Assignment
663 Assignment:
664 LeftHandSide '=' ConditionalExpression
665 { write_exp_elt_opcode (BINOP_ASSIGN); }
666 | LeftHandSide ASSIGN_MODIFY ConditionalExpression
667 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
668 write_exp_elt_opcode ($2);
669 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
672 LeftHandSide:
673 ForcedName
674 { push_expression_name ($1); }
675 | VARIABLE
676 /* Already written by write_dollar_variable. */
677 | FieldAccess
678 | ArrayAccess
682 Expression:
683 AssignmentExpression
687 /* Take care of parsing a number (anything that starts with a digit).
688 Set yylval and return the token type; update lexptr.
689 LEN is the number of characters in it. */
691 /*** Needs some error checking for the float case ***/
693 static int
694 parse_number (p, len, parsed_float, putithere)
695 char *p;
696 int len;
697 int parsed_float;
698 YYSTYPE *putithere;
700 ULONGEST n = 0;
701 ULONGEST limit, limit_div_base;
703 int c;
704 int base = input_radix;
706 struct type *type;
708 if (parsed_float)
710 /* It's a float since it contains a point or an exponent. */
711 char c;
712 int num = 0; /* number of tokens scanned by scanf */
713 char saved_char = p[len];
715 p[len] = 0; /* null-terminate the token */
716 num = sscanf (p, DOUBLEST_SCAN_FORMAT "%c",
717 &putithere->typed_val_float.dval, &c);
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). */
723 c = tolower (p[len - 1]);
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;
732 return FLOATING_POINT_LITERAL;
735 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
736 if (p[0] == '0')
737 switch (p[1])
739 case 'x':
740 case 'X':
741 if (len >= 3)
743 p += 2;
744 base = 16;
745 len -= 2;
747 break;
749 case 't':
750 case 'T':
751 case 'd':
752 case 'D':
753 if (len >= 3)
755 p += 2;
756 base = 10;
757 len -= 2;
759 break;
761 default:
762 base = 8;
763 break;
766 c = p[len-1];
767 /* A paranoid calculation of (1<<64)-1. */
768 limit = (ULONGEST)0xffffffff;
769 limit = ((limit << 16) << 16) | limit;
770 if (c == 'l' || c == 'L')
772 type = java_long_type;
773 len--;
775 else
777 type = java_int_type;
779 limit_div_base = limit / (ULONGEST) base;
781 while (--len >= 0)
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;
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.
805 if (type == java_int_type && n > (ULONGEST)0x80000000)
806 type = builtin_type_uint64;
808 putithere->typed_val_int.val = n;
809 putithere->typed_val_int.type = type;
811 return INTEGER_LITERAL;
814 struct token
816 char *operator;
817 int token;
818 enum exp_opcode opcode;
821 static const struct token tokentab3[] =
823 {">>=", ASSIGN_MODIFY, BINOP_RSH},
824 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
827 static const struct token tokentab2[] =
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}
849 /* Read one token, getting characters through lexptr. */
851 static int
852 yylex ()
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;
863 retry:
865 prev_lexptr = lexptr;
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 (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
872 lexptr += 3;
873 yylval.opcode = tokentab3[i].opcode;
874 return tokentab3[i].token;
877 /* See if it is a special token of length 2. */
878 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
879 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
881 lexptr += 2;
882 yylval.opcode = tokentab2[i].opcode;
883 return tokentab2[i].token;
886 switch (c = *tokstart)
888 case 0:
889 return 0;
891 case ' ':
892 case '\t':
893 case '\n':
894 lexptr++;
895 goto retry;
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"));
908 yylval.typed_val_int.val = c;
909 yylval.typed_val_int.type = java_char_type;
911 c = *lexptr++;
912 if (c != '\'')
914 namelen = skip_quoted (tokstart) - tokstart;
915 if (namelen > 2)
917 lexptr = tokstart + namelen;
918 if (lexptr[-1] != '\'')
919 error (_("Unmatched single quote"));
920 namelen -= 2;
921 tokstart++;
922 goto tryname;
924 error (_("Invalid character constant"));
926 return INTEGER_LITERAL;
928 case '(':
929 paren_depth++;
930 lexptr++;
931 return c;
933 case ')':
934 if (paren_depth == 0)
935 return 0;
936 paren_depth--;
937 lexptr++;
938 return c;
940 case ',':
941 if (comma_terminates && paren_depth == 0)
942 return 0;
943 lexptr++;
944 return c;
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. */
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':
963 /* It's a number. */
964 int got_dot = 0, got_e = 0, toktype;
965 char *p = tokstart;
966 int hex = input_radix > 10;
968 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
970 p += 2;
971 hex = 1;
973 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
975 p += 2;
976 hex = 0;
979 for (;; ++p)
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;
1002 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1003 if (toktype == ERROR)
1005 char *err_copy = (char *) alloca (p - tokstart + 1);
1007 memcpy (err_copy, tokstart, p - tokstart);
1008 err_copy[p - tokstart] = 0;
1009 error (_("Invalid number \"%s\""), err_copy);
1011 lexptr = p;
1012 return toktype;
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;
1038 case '"':
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 */
1049 tokptr = ++tokstart;
1050 tempbufindex = 0;
1052 do {
1053 /* Grow the static temp buffer if necessary, including allocating
1054 the first one on demand. */
1055 if (tempbufindex + 1 >= tempbufsize)
1057 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1059 switch (*tokptr)
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)
1070 continue;
1072 tempbuf[tempbufindex++] = c;
1073 break;
1074 default:
1075 tempbuf[tempbufindex++] = *tokptr++;
1076 break;
1078 } while ((*tokptr != '"') && (*tokptr != '\0'));
1079 if (*tokptr++ != '"')
1081 error (_("Unterminated string in expression"));
1083 tempbuf[tempbufindex] = '\0'; /* See note above */
1084 yylval.sval.ptr = tempbuf;
1085 yylval.sval.length = tempbufindex;
1086 lexptr = tokptr;
1087 return (STRING_LITERAL);
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);
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 == '<');
1106 if (c == '<')
1108 int i = namelen;
1109 while (tokstart[++i] && tokstart[i] != '>');
1110 if (tokstart[i] == '>')
1111 namelen = i;
1113 c = tokstart[++namelen];
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')
1120 return 0;
1123 lexptr += namelen;
1125 tryname:
1127 /* Catch specific keywords. Should be done with a data structure. */
1128 switch (namelen)
1130 case 7:
1131 if (DEPRECATED_STREQN (tokstart, "boolean", 7))
1132 return BOOLEAN;
1133 break;
1134 case 6:
1135 if (DEPRECATED_STREQN (tokstart, "double", 6))
1136 return DOUBLE;
1137 break;
1138 case 5:
1139 if (DEPRECATED_STREQN (tokstart, "short", 5))
1140 return SHORT;
1141 if (DEPRECATED_STREQN (tokstart, "false", 5))
1143 yylval.lval = 0;
1144 return BOOLEAN_LITERAL;
1146 if (DEPRECATED_STREQN (tokstart, "super", 5))
1147 return SUPER;
1148 if (DEPRECATED_STREQN (tokstart, "float", 5))
1149 return FLOAT;
1150 break;
1151 case 4:
1152 if (DEPRECATED_STREQN (tokstart, "long", 4))
1153 return LONG;
1154 if (DEPRECATED_STREQN (tokstart, "byte", 4))
1155 return BYTE;
1156 if (DEPRECATED_STREQN (tokstart, "char", 4))
1157 return CHAR;
1158 if (DEPRECATED_STREQN (tokstart, "true", 4))
1160 yylval.lval = 1;
1161 return BOOLEAN_LITERAL;
1163 break;
1164 case 3:
1165 if (strncmp (tokstart, "int", 3) == 0)
1166 return INT;
1167 if (strncmp (tokstart, "new", 3) == 0)
1168 return NEW;
1169 break;
1170 default:
1171 break;
1174 yylval.sval.ptr = tokstart;
1175 yylval.sval.length = namelen;
1177 if (*tokstart == '$')
1179 write_dollar_variable (yylval.sval);
1180 return VARIABLE;
1183 /* Input names that aren't symbols but ARE valid hex numbers,
1184 when the input radix permits them, can be names or numbers
1185 depending on the parse. Note we support radixes > 16 here. */
1186 if (((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1187 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1189 YYSTYPE newlval; /* Its value is ignored. */
1190 int hextype = parse_number (tokstart, namelen, 0, &newlval);
1191 if (hextype == INTEGER_LITERAL)
1192 return NAME_OR_INT;
1194 return IDENTIFIER;
1197 void
1198 yyerror (msg)
1199 char *msg;
1201 if (prev_lexptr)
1202 lexptr = prev_lexptr;
1204 if (msg)
1205 error (_("%s: near `%s'"), msg, lexptr);
1206 else
1207 error (_("error in expression, near `%s'"), lexptr);
1210 static struct type *
1211 java_type_from_name (name)
1212 struct stoken name;
1215 char *tmp = copy_name (name);
1216 struct type *typ = java_lookup_class (tmp);
1217 if (typ == NULL || TYPE_CODE (typ) != TYPE_CODE_STRUCT)
1218 error (_("No class named `%s'"), tmp);
1219 return typ;
1222 /* If NAME is a valid variable name in this scope, push it and return 1.
1223 Otherwise, return 0. */
1225 static int
1226 push_variable (struct stoken name)
1228 char *tmp = copy_name (name);
1229 int is_a_field_of_this = 0;
1230 struct symbol *sym;
1231 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN,
1232 &is_a_field_of_this, (struct symtab **) NULL);
1233 if (sym && SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1235 if (symbol_read_needs_frame (sym))
1237 if (innermost_block == 0 ||
1238 contained_in (block_found, innermost_block))
1239 innermost_block = block_found;
1242 write_exp_elt_opcode (OP_VAR_VALUE);
1243 /* We want to use the selected frame, not another more inner frame
1244 which happens to be in the same block. */
1245 write_exp_elt_block (NULL);
1246 write_exp_elt_sym (sym);
1247 write_exp_elt_opcode (OP_VAR_VALUE);
1248 return 1;
1250 if (is_a_field_of_this)
1252 /* it hangs off of `this'. Must not inadvertently convert from a
1253 method call to data ref. */
1254 if (innermost_block == 0 ||
1255 contained_in (block_found, innermost_block))
1256 innermost_block = block_found;
1257 write_exp_elt_opcode (OP_THIS);
1258 write_exp_elt_opcode (OP_THIS);
1259 write_exp_elt_opcode (STRUCTOP_PTR);
1260 write_exp_string (name);
1261 write_exp_elt_opcode (STRUCTOP_PTR);
1262 return 1;
1264 return 0;
1267 /* Assuming a reference expression has been pushed, emit the
1268 STRUCTOP_PTR ops to access the field named NAME. If NAME is a
1269 qualified name (has '.'), generate a field access for each part. */
1271 static void
1272 push_fieldnames (name)
1273 struct stoken name;
1275 int i;
1276 struct stoken token;
1277 token.ptr = name.ptr;
1278 for (i = 0; ; i++)
1280 if (i == name.length || name.ptr[i] == '.')
1282 /* token.ptr is start of current field name. */
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;
1289 if (i >= name.length)
1290 break;
1294 /* Helper routine for push_expression_name.
1295 Handle a qualified name, where DOT_INDEX is the index of the first '.' */
1297 static void
1298 push_qualified_expression_name (struct stoken name, int dot_index)
1300 struct stoken token;
1301 char *tmp;
1302 struct type *typ;
1304 token.ptr = name.ptr;
1305 token.length = dot_index;
1307 if (push_variable (token))
1309 token.ptr = name.ptr + dot_index + 1;
1310 token.length = name.length - dot_index - 1;
1311 push_fieldnames (token);
1312 return;
1315 token.ptr = name.ptr;
1316 for (;;)
1318 token.length = dot_index;
1319 tmp = copy_name (token);
1320 typ = java_lookup_class (tmp);
1321 if (typ != NULL)
1323 if (dot_index == name.length)
1325 write_exp_elt_opcode(OP_TYPE);
1326 write_exp_elt_type(typ);
1327 write_exp_elt_opcode(OP_TYPE);
1328 return;
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)
1344 dot_index++;
1345 name.ptr += dot_index;
1346 name.length -= dot_index;
1347 push_fieldnames (name);
1349 return;
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++;
1357 error (_("unknown type `%.*s'"), name.length, name.ptr);
1360 /* Handle Name in an expression (or LHS).
1361 Handle VAR, TYPE, TYPE.FIELD1....FIELDN and VAR.FIELD1....FIELDN. */
1363 static void
1364 push_expression_name (name)
1365 struct stoken name;
1367 char *tmp;
1368 struct type *typ;
1369 char *ptr;
1370 int i;
1372 for (i = 0; i < name.length; i++)
1374 if (name.ptr[i] == '.')
1376 /* It's a Qualified Expression Name. */
1377 push_qualified_expression_name (name, i);
1378 return;
1382 /* It's a Simple Expression Name. */
1384 if (push_variable (name))
1385 return;
1386 tmp = copy_name (name);
1387 typ = java_lookup_class (tmp);
1388 if (typ != NULL)
1390 write_exp_elt_opcode(OP_TYPE);
1391 write_exp_elt_type(typ);
1392 write_exp_elt_opcode(OP_TYPE);
1394 else
1396 struct minimal_symbol *msymbol;
1398 msymbol = lookup_minimal_symbol (tmp, NULL, NULL);
1399 if (msymbol != NULL)
1401 write_exp_msymbol (msymbol,
1402 lookup_function_type (builtin_type_int),
1403 builtin_type_int);
1405 else if (!have_full_symbols () && !have_partial_symbols ())
1406 error (_("No symbol table is loaded. Use the \"file\" command"));
1407 else
1408 error (_("No symbol \"%s\" in current context"), tmp);
1414 /* The following two routines, copy_exp and insert_exp, aren't specific to
1415 Java, so they could go in parse.c, but their only purpose is to support
1416 the parsing kludges we use in this file, so maybe it's best to isolate
1417 them here. */
1419 /* Copy the expression whose last element is at index ENDPOS - 1 in EXPR
1420 into a freshly malloc'ed struct expression. Its language_defn is set
1421 to null. */
1422 static struct expression *
1423 copy_exp (expr, endpos)
1424 struct expression *expr;
1425 int endpos;
1427 int len = length_of_subexp (expr, endpos);
1428 struct expression *new
1429 = (struct expression *) malloc (sizeof (*new) + EXP_ELEM_TO_BYTES (len));
1430 new->nelts = len;
1431 memcpy (new->elts, expr->elts + endpos - len, EXP_ELEM_TO_BYTES (len));
1432 new->language_defn = 0;
1434 return new;
1437 /* Insert the expression NEW into the current expression (expout) at POS. */
1438 static void
1439 insert_exp (pos, new)
1440 int pos;
1441 struct expression *new;
1443 int newlen = new->nelts;
1445 /* Grow expout if necessary. In this function's only use at present,
1446 this should never be necessary. */
1447 if (expout_ptr + newlen > expout_size)
1449 expout_size = max (expout_size * 2, expout_ptr + newlen + 10);
1450 expout = (struct expression *)
1451 realloc ((char *) expout, (sizeof (struct expression)
1452 + EXP_ELEM_TO_BYTES (expout_size)));
1456 int i;
1458 for (i = expout_ptr - 1; i >= pos; i--)
1459 expout->elts[i + newlen] = expout->elts[i];
1462 memcpy (expout->elts + pos, new->elts, EXP_ELEM_TO_BYTES (newlen));
1463 expout_ptr += newlen;