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