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