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