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