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