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