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