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