* gas/cgen feature
[binutils-gdb.git] / gdb / p-exp.y
blobedf34ca7a583f6d3cb9cffc8880acf284cfd054a
1 /* YACC parser for Pascal expressions, for GDB.
2 Copyright (C) 2000
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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. */
21 /* This file is derived from c-exp.y */
23 /* Parse a Pascal expression from text in a string,
24 and return the result as a struct expression pointer.
25 That structure contains arithmetic operations in reverse polish,
26 with constants represented by operations that are followed by special data.
27 See expression.h for the details of the format.
28 What is important here is that it can be built up sequentially
29 during the process of parsing; the lower levels of the tree always
30 come first in the result.
32 Note that malloc's and realloc's in this file are transformed to
33 xmalloc and xrealloc respectively by the same sed command in the
34 makefile that remaps any other malloc/realloc inserted by the parser
35 generator. Doing this with #defines and trying to control the interaction
36 with include files (<malloc.h> and <stdlib.h> for example) just became
37 too messy, particularly when such includes can be inserted at random
38 times by the parser generator. */
40 /* FIXME: there are still 21 shift/reduce conflicts
41 Other known bugs or limitations:
42 - pascal string operations are not supported at all.
43 - there are some problems with boolean types.
44 - Pascal type hexadecimal constants are not supported
45 because they conflict with the internal variables format.
46 Probably also lots of other problems, less well defined PM */
49 #include "defs.h"
50 #include "gdb_string.h"
51 #include <ctype.h>
52 #include "expression.h"
53 #include "value.h"
54 #include "parser-defs.h"
55 #include "language.h"
56 #include "p-lang.h"
57 #include "bfd.h" /* Required by objfiles.h. */
58 #include "symfile.h" /* Required by objfiles.h. */
59 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
61 /* MSVC uses strnicmp instead of strncasecmp */
62 #ifdef _MSC_VER
63 #define strncasecmp strnicmp
64 #endif
66 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
67 as well as gratuitiously global symbol names, so we can have multiple
68 yacc generated parsers in gdb. Note that these are only the variables
69 produced by yacc. If other parser generators (bison, byacc, etc) produce
70 additional global names that conflict at link time, then those parser
71 generators need to be fixed instead of adding those names to this list. */
73 #define yymaxdepth pascal_maxdepth
74 #define yyparse pascal_parse
75 #define yylex pascal_lex
76 #define yyerror pascal_error
77 #define yylval pascal_lval
78 #define yychar pascal_char
79 #define yydebug pascal_debug
80 #define yypact pascal_pact
81 #define yyr1 pascal_r1
82 #define yyr2 pascal_r2
83 #define yydef pascal_def
84 #define yychk pascal_chk
85 #define yypgo pascal_pgo
86 #define yyact pascal_act
87 #define yyexca pascal_exca
88 #define yyerrflag pascal_errflag
89 #define yynerrs pascal_nerrs
90 #define yyps pascal_ps
91 #define yypv pascal_pv
92 #define yys pascal_s
93 #define yy_yys pascal_yys
94 #define yystate pascal_state
95 #define yytmp pascal_tmp
96 #define yyv pascal_v
97 #define yy_yyv pascal_yyv
98 #define yyval pascal_val
99 #define yylloc pascal_lloc
100 #define yyreds pascal_reds /* With YYDEBUG defined */
101 #define yytoks pascal_toks /* With YYDEBUG defined */
102 #define yylhs pascal_yylhs
103 #define yylen pascal_yylen
104 #define yydefred pascal_yydefred
105 #define yydgoto pascal_yydgoto
106 #define yysindex pascal_yysindex
107 #define yyrindex pascal_yyrindex
108 #define yygindex pascal_yygindex
109 #define yytable pascal_yytable
110 #define yycheck pascal_yycheck
112 #ifndef YYDEBUG
113 #define YYDEBUG 0 /* Default to no yydebug support */
114 #endif
116 int yyparse (void);
118 static int yylex (void);
120 void
121 yyerror (char *);
123 static char * uptok (char *, int);
126 /* Although the yacc "value" of an expression is not used,
127 since the result is stored in the structure being created,
128 other node types do have values. */
130 %union
132 LONGEST lval;
133 struct {
134 LONGEST val;
135 struct type *type;
136 } typed_val_int;
137 struct {
138 DOUBLEST dval;
139 struct type *type;
140 } typed_val_float;
141 struct symbol *sym;
142 struct type *tval;
143 struct stoken sval;
144 struct ttype tsym;
145 struct symtoken ssym;
146 int voidval;
147 struct block *bval;
148 enum exp_opcode opcode;
149 struct internalvar *ivar;
151 struct type **tvec;
152 int *ivec;
156 /* YYSTYPE gets defined by %union */
157 static int
158 parse_number (char *, int, int, YYSTYPE *);
161 %type <voidval> exp exp1 type_exp start variable qualified_name
162 %type <tval> type typebase
163 /* %type <bval> block */
165 /* Fancy type parsing. */
166 %type <tval> ptype
168 %token <typed_val_int> INT
169 %token <typed_val_float> FLOAT
171 /* Both NAME and TYPENAME tokens represent symbols in the input,
172 and both convey their data as strings.
173 But a TYPENAME is a string that happens to be defined as a typedef
174 or builtin type name (such as int or char)
175 and a NAME is any other symbol.
176 Contexts where this distinction is not important can use the
177 nonterminal "name", which matches either NAME or TYPENAME. */
179 %token <sval> STRING
180 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
181 %token <tsym> TYPENAME
182 %type <sval> name
183 %type <ssym> name_not_typename
185 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
186 but which would parse as a valid number in the current input radix.
187 E.g. "c" when input_radix==16. Depending on the parse, it will be
188 turned into a name or into a number. */
190 %token <ssym> NAME_OR_INT
192 %token STRUCT CLASS SIZEOF COLONCOLON
193 %token ERROR
195 /* Special type cases, put in to allow the parser to distinguish different
196 legal basetypes. */
198 %token <voidval> VARIABLE
201 /* Object pascal */
202 %token THIS
203 %token <lval> TRUE FALSE
205 %left ','
206 %left ABOVE_COMMA
207 %right ASSIGN
208 %left NOT
209 %left OR
210 %left XOR
211 %left ANDAND
212 %left '=' NOTEQUAL
213 %left '<' '>' LEQ GEQ
214 %left LSH RSH DIV MOD
215 %left '@'
216 %left '+' '-'
217 %left '*' '/'
218 %right UNARY INCREMENT DECREMENT
219 %right ARROW '.' '[' '('
220 %token <ssym> BLOCKNAME
221 %type <bval> block
222 %left COLONCOLON
227 start : exp1
228 | type_exp
231 type_exp: type
232 { write_exp_elt_opcode(OP_TYPE);
233 write_exp_elt_type($1);
234 write_exp_elt_opcode(OP_TYPE);}
237 /* Expressions, including the comma operator. */
238 exp1 : exp
239 | exp1 ',' exp
240 { write_exp_elt_opcode (BINOP_COMMA); }
243 /* Expressions, not including the comma operator. */
244 exp : exp '^' %prec UNARY
245 { write_exp_elt_opcode (UNOP_IND); }
247 exp : '@' exp %prec UNARY
248 { write_exp_elt_opcode (UNOP_ADDR); }
250 exp : '-' exp %prec UNARY
251 { write_exp_elt_opcode (UNOP_NEG); }
254 exp : NOT exp %prec UNARY
255 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
258 exp : INCREMENT '(' exp ')' %prec UNARY
259 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
262 exp : DECREMENT '(' exp ')' %prec UNARY
263 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
266 exp : exp '.' name
267 { write_exp_elt_opcode (STRUCTOP_STRUCT);
268 write_exp_string ($3);
269 write_exp_elt_opcode (STRUCTOP_STRUCT); }
272 exp : exp '[' exp1 ']'
273 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
276 exp : exp '('
277 /* This is to save the value of arglist_len
278 being accumulated by an outer function call. */
279 { start_arglist (); }
280 arglist ')' %prec ARROW
281 { write_exp_elt_opcode (OP_FUNCALL);
282 write_exp_elt_longcst ((LONGEST) end_arglist ());
283 write_exp_elt_opcode (OP_FUNCALL); }
286 arglist :
287 | exp
288 { arglist_len = 1; }
289 | arglist ',' exp %prec ABOVE_COMMA
290 { arglist_len++; }
293 exp : type '(' exp ')' %prec UNARY
294 { write_exp_elt_opcode (UNOP_CAST);
295 write_exp_elt_type ($1);
296 write_exp_elt_opcode (UNOP_CAST); }
299 exp : '(' exp1 ')'
303 /* Binary operators in order of decreasing precedence. */
305 exp : exp '*' exp
306 { write_exp_elt_opcode (BINOP_MUL); }
309 exp : exp '/' exp
310 { write_exp_elt_opcode (BINOP_DIV); }
313 exp : exp DIV exp
314 { write_exp_elt_opcode (BINOP_INTDIV); }
317 exp : exp MOD exp
318 { write_exp_elt_opcode (BINOP_REM); }
321 exp : exp '+' exp
322 { write_exp_elt_opcode (BINOP_ADD); }
325 exp : exp '-' exp
326 { write_exp_elt_opcode (BINOP_SUB); }
329 exp : exp LSH exp
330 { write_exp_elt_opcode (BINOP_LSH); }
333 exp : exp RSH exp
334 { write_exp_elt_opcode (BINOP_RSH); }
337 exp : exp '=' exp
338 { write_exp_elt_opcode (BINOP_EQUAL); }
341 exp : exp NOTEQUAL exp
342 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
345 exp : exp LEQ exp
346 { write_exp_elt_opcode (BINOP_LEQ); }
349 exp : exp GEQ exp
350 { write_exp_elt_opcode (BINOP_GEQ); }
353 exp : exp '<' exp
354 { write_exp_elt_opcode (BINOP_LESS); }
357 exp : exp '>' exp
358 { write_exp_elt_opcode (BINOP_GTR); }
361 exp : exp ANDAND exp
362 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
365 exp : exp XOR exp
366 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
369 exp : exp OR exp
370 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
373 exp : exp ASSIGN exp
374 { write_exp_elt_opcode (BINOP_ASSIGN); }
377 exp : TRUE
378 { write_exp_elt_opcode (OP_BOOL);
379 write_exp_elt_longcst ((LONGEST) $1);
380 write_exp_elt_opcode (OP_BOOL); }
383 exp : FALSE
384 { write_exp_elt_opcode (OP_BOOL);
385 write_exp_elt_longcst ((LONGEST) $1);
386 write_exp_elt_opcode (OP_BOOL); }
389 exp : INT
390 { write_exp_elt_opcode (OP_LONG);
391 write_exp_elt_type ($1.type);
392 write_exp_elt_longcst ((LONGEST)($1.val));
393 write_exp_elt_opcode (OP_LONG); }
396 exp : NAME_OR_INT
397 { YYSTYPE val;
398 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
399 write_exp_elt_opcode (OP_LONG);
400 write_exp_elt_type (val.typed_val_int.type);
401 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
402 write_exp_elt_opcode (OP_LONG);
407 exp : FLOAT
408 { write_exp_elt_opcode (OP_DOUBLE);
409 write_exp_elt_type ($1.type);
410 write_exp_elt_dblcst ($1.dval);
411 write_exp_elt_opcode (OP_DOUBLE); }
414 exp : variable
417 exp : VARIABLE
418 /* Already written by write_dollar_variable. */
421 exp : SIZEOF '(' type ')' %prec UNARY
422 { write_exp_elt_opcode (OP_LONG);
423 write_exp_elt_type (builtin_type_int);
424 CHECK_TYPEDEF ($3);
425 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
426 write_exp_elt_opcode (OP_LONG); }
429 exp : STRING
430 { /* C strings are converted into array constants with
431 an explicit null byte added at the end. Thus
432 the array upper bound is the string length.
433 There is no such thing in C as a completely empty
434 string. */
435 char *sp = $1.ptr; int count = $1.length;
436 while (count-- > 0)
438 write_exp_elt_opcode (OP_LONG);
439 write_exp_elt_type (builtin_type_char);
440 write_exp_elt_longcst ((LONGEST)(*sp++));
441 write_exp_elt_opcode (OP_LONG);
443 write_exp_elt_opcode (OP_LONG);
444 write_exp_elt_type (builtin_type_char);
445 write_exp_elt_longcst ((LONGEST)'\0');
446 write_exp_elt_opcode (OP_LONG);
447 write_exp_elt_opcode (OP_ARRAY);
448 write_exp_elt_longcst ((LONGEST) 0);
449 write_exp_elt_longcst ((LONGEST) ($1.length));
450 write_exp_elt_opcode (OP_ARRAY); }
453 /* Object pascal */
454 exp : THIS
455 { write_exp_elt_opcode (OP_THIS);
456 write_exp_elt_opcode (OP_THIS); }
459 /* end of object pascal. */
461 block : BLOCKNAME
463 if ($1.sym != 0)
464 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
465 else
467 struct symtab *tem =
468 lookup_symtab (copy_name ($1.stoken));
469 if (tem)
470 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), STATIC_BLOCK);
471 else
472 error ("No file or function \"%s\".",
473 copy_name ($1.stoken));
478 block : block COLONCOLON name
479 { struct symbol *tem
480 = lookup_symbol (copy_name ($3), $1,
481 VAR_NAMESPACE, (int *) NULL,
482 (struct symtab **) NULL);
483 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
484 error ("No function \"%s\" in specified context.",
485 copy_name ($3));
486 $$ = SYMBOL_BLOCK_VALUE (tem); }
489 variable: block COLONCOLON name
490 { struct symbol *sym;
491 sym = lookup_symbol (copy_name ($3), $1,
492 VAR_NAMESPACE, (int *) NULL,
493 (struct symtab **) NULL);
494 if (sym == 0)
495 error ("No symbol \"%s\" in specified context.",
496 copy_name ($3));
498 write_exp_elt_opcode (OP_VAR_VALUE);
499 /* block_found is set by lookup_symbol. */
500 write_exp_elt_block (block_found);
501 write_exp_elt_sym (sym);
502 write_exp_elt_opcode (OP_VAR_VALUE); }
505 qualified_name: typebase COLONCOLON name
507 struct type *type = $1;
508 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
509 && TYPE_CODE (type) != TYPE_CODE_UNION)
510 error ("`%s' is not defined as an aggregate type.",
511 TYPE_NAME (type));
513 write_exp_elt_opcode (OP_SCOPE);
514 write_exp_elt_type (type);
515 write_exp_string ($3);
516 write_exp_elt_opcode (OP_SCOPE);
520 variable: qualified_name
521 | COLONCOLON name
523 char *name = copy_name ($2);
524 struct symbol *sym;
525 struct minimal_symbol *msymbol;
527 sym =
528 lookup_symbol (name, (const struct block *) NULL,
529 VAR_NAMESPACE, (int *) NULL,
530 (struct symtab **) NULL);
531 if (sym)
533 write_exp_elt_opcode (OP_VAR_VALUE);
534 write_exp_elt_block (NULL);
535 write_exp_elt_sym (sym);
536 write_exp_elt_opcode (OP_VAR_VALUE);
537 break;
540 msymbol = lookup_minimal_symbol (name, NULL, NULL);
541 if (msymbol != NULL)
543 write_exp_msymbol (msymbol,
544 lookup_function_type (builtin_type_int),
545 builtin_type_int);
547 else
548 if (!have_full_symbols () && !have_partial_symbols ())
549 error ("No symbol table is loaded. Use the \"file\" command.");
550 else
551 error ("No symbol \"%s\" in current context.", name);
555 variable: name_not_typename
556 { struct symbol *sym = $1.sym;
558 if (sym)
560 if (symbol_read_needs_frame (sym))
562 if (innermost_block == 0 ||
563 contained_in (block_found,
564 innermost_block))
565 innermost_block = block_found;
568 write_exp_elt_opcode (OP_VAR_VALUE);
569 /* We want to use the selected frame, not
570 another more inner frame which happens to
571 be in the same block. */
572 write_exp_elt_block (NULL);
573 write_exp_elt_sym (sym);
574 write_exp_elt_opcode (OP_VAR_VALUE);
576 else if ($1.is_a_field_of_this)
578 /* Object pascal: it hangs off of `this'. Must
579 not inadvertently convert from a method call
580 to data ref. */
581 if (innermost_block == 0 ||
582 contained_in (block_found, innermost_block))
583 innermost_block = block_found;
584 write_exp_elt_opcode (OP_THIS);
585 write_exp_elt_opcode (OP_THIS);
586 write_exp_elt_opcode (STRUCTOP_PTR);
587 write_exp_string ($1.stoken);
588 write_exp_elt_opcode (STRUCTOP_PTR);
590 else
592 struct minimal_symbol *msymbol;
593 register char *arg = copy_name ($1.stoken);
595 msymbol =
596 lookup_minimal_symbol (arg, NULL, NULL);
597 if (msymbol != NULL)
599 write_exp_msymbol (msymbol,
600 lookup_function_type (builtin_type_int),
601 builtin_type_int);
603 else if (!have_full_symbols () && !have_partial_symbols ())
604 error ("No symbol table is loaded. Use the \"file\" command.");
605 else
606 error ("No symbol \"%s\" in current context.",
607 copy_name ($1.stoken));
613 ptype : typebase
616 /* We used to try to recognize more pointer to member types here, but
617 that didn't work (shift/reduce conflicts meant that these rules never
618 got executed). The problem is that
619 int (foo::bar::baz::bizzle)
620 is a function type but
621 int (foo::bar::baz::bizzle::*)
622 is a pointer to member type. Stroustrup loses again! */
624 type : ptype
625 | typebase COLONCOLON '*'
626 { $$ = lookup_member_type (builtin_type_int, $1); }
629 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
630 : TYPENAME
631 { $$ = $1.type; }
632 | STRUCT name
633 { $$ = lookup_struct (copy_name ($2),
634 expression_context_block); }
635 | CLASS name
636 { $$ = lookup_struct (copy_name ($2),
637 expression_context_block); }
638 /* "const" and "volatile" are curently ignored. A type qualifier
639 after the type is handled in the ptype rule. I think these could
640 be too. */
643 name : NAME { $$ = $1.stoken; }
644 | BLOCKNAME { $$ = $1.stoken; }
645 | TYPENAME { $$ = $1.stoken; }
646 | NAME_OR_INT { $$ = $1.stoken; }
649 name_not_typename : NAME
650 | BLOCKNAME
651 /* These would be useful if name_not_typename was useful, but it is just
652 a fake for "variable", so these cause reduce/reduce conflicts because
653 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
654 =exp) or just an exp. If name_not_typename was ever used in an lvalue
655 context where only a name could occur, this might be useful.
656 | NAME_OR_INT
662 /* Take care of parsing a number (anything that starts with a digit).
663 Set yylval and return the token type; update lexptr.
664 LEN is the number of characters in it. */
666 /*** Needs some error checking for the float case ***/
668 static int
669 parse_number (p, len, parsed_float, putithere)
670 register char *p;
671 register int len;
672 int parsed_float;
673 YYSTYPE *putithere;
675 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
676 here, and we do kind of silly things like cast to unsigned. */
677 register LONGEST n = 0;
678 register LONGEST prevn = 0;
679 ULONGEST un;
681 register int i = 0;
682 register int c;
683 register int base = input_radix;
684 int unsigned_p = 0;
686 /* Number of "L" suffixes encountered. */
687 int long_p = 0;
689 /* We have found a "L" or "U" suffix. */
690 int found_suffix = 0;
692 ULONGEST high_bit;
693 struct type *signed_type;
694 struct type *unsigned_type;
696 if (parsed_float)
698 /* It's a float since it contains a point or an exponent. */
699 char c;
700 int num = 0; /* number of tokens scanned by scanf */
701 char saved_char = p[len];
703 p[len] = 0; /* null-terminate the token */
704 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
705 num = sscanf (p, "%g%c", (float *) &putithere->typed_val_float.dval,&c);
706 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
707 num = sscanf (p, "%lg%c", (double *) &putithere->typed_val_float.dval,&c);
708 else
710 #ifdef SCANF_HAS_LONG_DOUBLE
711 num = sscanf (p, "%Lg%c", &putithere->typed_val_float.dval,&c);
712 #else
713 /* Scan it into a double, then assign it to the long double.
714 This at least wins with values representable in the range
715 of doubles. */
716 double temp;
717 num = sscanf (p, "%lg%c", &temp,&c);
718 putithere->typed_val_float.dval = temp;
719 #endif
721 p[len] = saved_char; /* restore the input stream */
722 if (num != 1) /* check scanf found ONLY a float ... */
723 return ERROR;
724 /* See if it has `f' or `l' suffix (float or long double). */
726 c = tolower (p[len - 1]);
728 if (c == 'f')
729 putithere->typed_val_float.type = builtin_type_float;
730 else if (c == 'l')
731 putithere->typed_val_float.type = builtin_type_long_double;
732 else if (isdigit (c) || c == '.')
733 putithere->typed_val_float.type = builtin_type_double;
734 else
735 return ERROR;
737 return FLOAT;
740 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
741 if (p[0] == '0')
742 switch (p[1])
744 case 'x':
745 case 'X':
746 if (len >= 3)
748 p += 2;
749 base = 16;
750 len -= 2;
752 break;
754 case 't':
755 case 'T':
756 case 'd':
757 case 'D':
758 if (len >= 3)
760 p += 2;
761 base = 10;
762 len -= 2;
764 break;
766 default:
767 base = 8;
768 break;
771 while (len-- > 0)
773 c = *p++;
774 if (c >= 'A' && c <= 'Z')
775 c += 'a' - 'A';
776 if (c != 'l' && c != 'u')
777 n *= base;
778 if (c >= '0' && c <= '9')
780 if (found_suffix)
781 return ERROR;
782 n += i = c - '0';
784 else
786 if (base > 10 && c >= 'a' && c <= 'f')
788 if (found_suffix)
789 return ERROR;
790 n += i = c - 'a' + 10;
792 else if (c == 'l')
794 ++long_p;
795 found_suffix = 1;
797 else if (c == 'u')
799 unsigned_p = 1;
800 found_suffix = 1;
802 else
803 return ERROR; /* Char not a digit */
805 if (i >= base)
806 return ERROR; /* Invalid digit in this base */
808 /* Portably test for overflow (only works for nonzero values, so make
809 a second check for zero). FIXME: Can't we just make n and prevn
810 unsigned and avoid this? */
811 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
812 unsigned_p = 1; /* Try something unsigned */
814 /* Portably test for unsigned overflow.
815 FIXME: This check is wrong; for example it doesn't find overflow
816 on 0x123456789 when LONGEST is 32 bits. */
817 if (c != 'l' && c != 'u' && n != 0)
819 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
820 error ("Numeric constant too large.");
822 prevn = n;
825 /* An integer constant is an int, a long, or a long long. An L
826 suffix forces it to be long; an LL suffix forces it to be long
827 long. If not forced to a larger size, it gets the first type of
828 the above that it fits in. To figure out whether it fits, we
829 shift it right and see whether anything remains. Note that we
830 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
831 operation, because many compilers will warn about such a shift
832 (which always produces a zero result). Sometimes TARGET_INT_BIT
833 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
834 the case where it is we just always shift the value more than
835 once, with fewer bits each time. */
837 un = (ULONGEST)n >> 2;
838 if (long_p == 0
839 && (un >> (TARGET_INT_BIT - 2)) == 0)
841 high_bit = ((ULONGEST)1) << (TARGET_INT_BIT-1);
843 /* A large decimal (not hex or octal) constant (between INT_MAX
844 and UINT_MAX) is a long or unsigned long, according to ANSI,
845 never an unsigned int, but this code treats it as unsigned
846 int. This probably should be fixed. GCC gives a warning on
847 such constants. */
849 unsigned_type = builtin_type_unsigned_int;
850 signed_type = builtin_type_int;
852 else if (long_p <= 1
853 && (un >> (TARGET_LONG_BIT - 2)) == 0)
855 high_bit = ((ULONGEST)1) << (TARGET_LONG_BIT-1);
856 unsigned_type = builtin_type_unsigned_long;
857 signed_type = builtin_type_long;
859 else
861 high_bit = (((ULONGEST)1)
862 << (TARGET_LONG_LONG_BIT - 32 - 1)
863 << 16
864 << 16);
865 if (high_bit == 0)
866 /* A long long does not fit in a LONGEST. */
867 high_bit =
868 (ULONGEST)1 << (sizeof (LONGEST) * HOST_CHAR_BIT - 1);
869 unsigned_type = builtin_type_unsigned_long_long;
870 signed_type = builtin_type_long_long;
873 putithere->typed_val_int.val = n;
875 /* If the high bit of the worked out type is set then this number
876 has to be unsigned. */
878 if (unsigned_p || (n & high_bit))
880 putithere->typed_val_int.type = unsigned_type;
882 else
884 putithere->typed_val_int.type = signed_type;
887 return INT;
890 struct token
892 char *operator;
893 int token;
894 enum exp_opcode opcode;
897 static const struct token tokentab3[] =
899 {"shr", RSH, BINOP_END},
900 {"shl", LSH, BINOP_END},
901 {"and", ANDAND, BINOP_END},
902 {"div", DIV, BINOP_END},
903 {"not", NOT, BINOP_END},
904 {"mod", MOD, BINOP_END},
905 {"inc", INCREMENT, BINOP_END},
906 {"dec", DECREMENT, BINOP_END},
907 {"xor", XOR, BINOP_END}
910 static const struct token tokentab2[] =
912 {"or", OR, BINOP_END},
913 {"<>", NOTEQUAL, BINOP_END},
914 {"<=", LEQ, BINOP_END},
915 {">=", GEQ, BINOP_END},
916 {":=", ASSIGN, BINOP_END}
919 /* Allocate uppercased var */
920 /* make an uppercased copy of tokstart */
921 static char * uptok (tokstart, namelen)
922 char *tokstart;
923 int namelen;
925 int i;
926 char *uptokstart = (char *)malloc(namelen+1);
927 for (i = 0;i <= namelen;i++)
929 if ((tokstart[i]>='a' && tokstart[i]<='z'))
930 uptokstart[i] = tokstart[i]-('a'-'A');
931 else
932 uptokstart[i] = tokstart[i];
934 uptokstart[namelen]='\0';
935 return uptokstart;
937 /* Read one token, getting characters through lexptr. */
940 static int
941 yylex ()
943 int c;
944 int namelen;
945 unsigned int i;
946 char *tokstart;
947 char *uptokstart;
948 char *tokptr;
949 char *p;
950 int explen, tempbufindex;
951 static char *tempbuf;
952 static int tempbufsize;
954 retry:
956 tokstart = lexptr;
957 explen = strlen (lexptr);
958 /* See if it is a special token of length 3. */
959 if (explen > 2)
960 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
961 if (strncasecmp (tokstart, tokentab3[i].operator, 3) == 0
962 && (!isalpha (tokentab3[i].operator[0]) || explen == 3
963 || (!isalpha (tokstart[3]) && !isdigit (tokstart[3]) && tokstart[3] != '_')))
965 lexptr += 3;
966 yylval.opcode = tokentab3[i].opcode;
967 return tokentab3[i].token;
970 /* See if it is a special token of length 2. */
971 if (explen > 1)
972 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
973 if (strncasecmp (tokstart, tokentab2[i].operator, 2) == 0
974 && (!isalpha (tokentab2[i].operator[0]) || explen == 2
975 || (!isalpha (tokstart[2]) && !isdigit (tokstart[2]) && tokstart[2] != '_')))
977 lexptr += 2;
978 yylval.opcode = tokentab2[i].opcode;
979 return tokentab2[i].token;
982 switch (c = *tokstart)
984 case 0:
985 return 0;
987 case ' ':
988 case '\t':
989 case '\n':
990 lexptr++;
991 goto retry;
993 case '\'':
994 /* We either have a character constant ('0' or '\177' for example)
995 or we have a quoted symbol reference ('foo(int,int)' in object pascal
996 for example). */
997 lexptr++;
998 c = *lexptr++;
999 if (c == '\\')
1000 c = parse_escape (&lexptr);
1001 else if (c == '\'')
1002 error ("Empty character constant.");
1004 yylval.typed_val_int.val = c;
1005 yylval.typed_val_int.type = builtin_type_char;
1007 c = *lexptr++;
1008 if (c != '\'')
1010 namelen = skip_quoted (tokstart) - tokstart;
1011 if (namelen > 2)
1013 lexptr = tokstart + namelen;
1014 if (lexptr[-1] != '\'')
1015 error ("Unmatched single quote.");
1016 namelen -= 2;
1017 tokstart++;
1018 uptokstart = uptok(tokstart,namelen);
1019 goto tryname;
1021 error ("Invalid character constant.");
1023 return INT;
1025 case '(':
1026 paren_depth++;
1027 lexptr++;
1028 return c;
1030 case ')':
1031 if (paren_depth == 0)
1032 return 0;
1033 paren_depth--;
1034 lexptr++;
1035 return c;
1037 case ',':
1038 if (comma_terminates && paren_depth == 0)
1039 return 0;
1040 lexptr++;
1041 return c;
1043 case '.':
1044 /* Might be a floating point number. */
1045 if (lexptr[1] < '0' || lexptr[1] > '9')
1046 goto symbol; /* Nope, must be a symbol. */
1047 /* FALL THRU into number case. */
1049 case '0':
1050 case '1':
1051 case '2':
1052 case '3':
1053 case '4':
1054 case '5':
1055 case '6':
1056 case '7':
1057 case '8':
1058 case '9':
1060 /* It's a number. */
1061 int got_dot = 0, got_e = 0, toktype;
1062 register char *p = tokstart;
1063 int hex = input_radix > 10;
1065 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1067 p += 2;
1068 hex = 1;
1070 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1072 p += 2;
1073 hex = 0;
1076 for (;; ++p)
1078 /* This test includes !hex because 'e' is a valid hex digit
1079 and thus does not indicate a floating point number when
1080 the radix is hex. */
1081 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1082 got_dot = got_e = 1;
1083 /* This test does not include !hex, because a '.' always indicates
1084 a decimal floating point number regardless of the radix. */
1085 else if (!got_dot && *p == '.')
1086 got_dot = 1;
1087 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1088 && (*p == '-' || *p == '+'))
1089 /* This is the sign of the exponent, not the end of the
1090 number. */
1091 continue;
1092 /* We will take any letters or digits. parse_number will
1093 complain if past the radix, or if L or U are not final. */
1094 else if ((*p < '0' || *p > '9')
1095 && ((*p < 'a' || *p > 'z')
1096 && (*p < 'A' || *p > 'Z')))
1097 break;
1099 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1100 if (toktype == ERROR)
1102 char *err_copy = (char *) alloca (p - tokstart + 1);
1104 memcpy (err_copy, tokstart, p - tokstart);
1105 err_copy[p - tokstart] = 0;
1106 error ("Invalid number \"%s\".", err_copy);
1108 lexptr = p;
1109 return toktype;
1112 case '+':
1113 case '-':
1114 case '*':
1115 case '/':
1116 case '|':
1117 case '&':
1118 case '^':
1119 case '~':
1120 case '!':
1121 case '@':
1122 case '<':
1123 case '>':
1124 case '[':
1125 case ']':
1126 case '?':
1127 case ':':
1128 case '=':
1129 case '{':
1130 case '}':
1131 symbol:
1132 lexptr++;
1133 return c;
1135 case '"':
1137 /* Build the gdb internal form of the input string in tempbuf,
1138 translating any standard C escape forms seen. Note that the
1139 buffer is null byte terminated *only* for the convenience of
1140 debugging gdb itself and printing the buffer contents when
1141 the buffer contains no embedded nulls. Gdb does not depend
1142 upon the buffer being null byte terminated, it uses the length
1143 string instead. This allows gdb to handle C strings (as well
1144 as strings in other languages) with embedded null bytes */
1146 tokptr = ++tokstart;
1147 tempbufindex = 0;
1149 do {
1150 /* Grow the static temp buffer if necessary, including allocating
1151 the first one on demand. */
1152 if (tempbufindex + 1 >= tempbufsize)
1154 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1156 switch (*tokptr)
1158 case '\0':
1159 case '"':
1160 /* Do nothing, loop will terminate. */
1161 break;
1162 case '\\':
1163 tokptr++;
1164 c = parse_escape (&tokptr);
1165 if (c == -1)
1167 continue;
1169 tempbuf[tempbufindex++] = c;
1170 break;
1171 default:
1172 tempbuf[tempbufindex++] = *tokptr++;
1173 break;
1175 } while ((*tokptr != '"') && (*tokptr != '\0'));
1176 if (*tokptr++ != '"')
1178 error ("Unterminated string in expression.");
1180 tempbuf[tempbufindex] = '\0'; /* See note above */
1181 yylval.sval.ptr = tempbuf;
1182 yylval.sval.length = tempbufindex;
1183 lexptr = tokptr;
1184 return (STRING);
1187 if (!(c == '_' || c == '$'
1188 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1189 /* We must have come across a bad character (e.g. ';'). */
1190 error ("Invalid character '%c' in expression.", c);
1192 /* It's a name. See how long it is. */
1193 namelen = 0;
1194 for (c = tokstart[namelen];
1195 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1196 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1198 /* Template parameter lists are part of the name.
1199 FIXME: This mishandles `print $a<4&&$a>3'. */
1200 if (c == '<')
1202 int i = namelen;
1203 int nesting_level = 1;
1204 while (tokstart[++i])
1206 if (tokstart[i] == '<')
1207 nesting_level++;
1208 else if (tokstart[i] == '>')
1210 if (--nesting_level == 0)
1211 break;
1214 if (tokstart[i] == '>')
1215 namelen = i;
1216 else
1217 break;
1220 /* do NOT uppercase internals because of registers !!! */
1221 c = tokstart[++namelen];
1224 uptokstart = uptok(tokstart,namelen);
1226 /* The token "if" terminates the expression and is NOT
1227 removed from the input stream. */
1228 if (namelen == 2 && uptokstart[0] == 'I' && uptokstart[1] == 'F')
1230 return 0;
1233 lexptr += namelen;
1235 tryname:
1237 /* Catch specific keywords. Should be done with a data structure. */
1238 switch (namelen)
1240 case 6:
1241 if (STREQ (uptokstart, "OBJECT"))
1242 return CLASS;
1243 if (STREQ (uptokstart, "RECORD"))
1244 return STRUCT;
1245 if (STREQ (uptokstart, "SIZEOF"))
1246 return SIZEOF;
1247 break;
1248 case 5:
1249 if (STREQ (uptokstart, "CLASS"))
1250 return CLASS;
1251 if (STREQ (uptokstart, "FALSE"))
1253 yylval.lval = 0;
1254 return FALSE;
1256 break;
1257 case 4:
1258 if (STREQ (uptokstart, "TRUE"))
1260 yylval.lval = 1;
1261 return TRUE;
1263 if (STREQ (uptokstart, "SELF"))
1265 /* here we search for 'this' like
1266 inserted in FPC stabs debug info */
1267 static const char this_name[] =
1268 { /* CPLUS_MARKER,*/ 't', 'h', 'i', 's', '\0' };
1270 if (lookup_symbol (this_name, expression_context_block,
1271 VAR_NAMESPACE, (int *) NULL,
1272 (struct symtab **) NULL))
1273 return THIS;
1275 break;
1276 default:
1277 break;
1280 yylval.sval.ptr = tokstart;
1281 yylval.sval.length = namelen;
1283 if (*tokstart == '$')
1285 /* $ is the normal prefix for pascal hexadecimal values
1286 but this conflicts with the GDB use for debugger variables
1287 so in expression to enter hexadecimal values
1288 we still need to use C syntax with 0xff */
1289 write_dollar_variable (yylval.sval);
1290 return VARIABLE;
1293 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1294 functions or symtabs. If this is not so, then ...
1295 Use token-type TYPENAME for symbols that happen to be defined
1296 currently as names of types; NAME for other symbols.
1297 The caller is not constrained to care about the distinction. */
1299 char *tmp = copy_name (yylval.sval);
1300 struct symbol *sym;
1301 int is_a_field_of_this = 0;
1302 int hextype;
1304 sym = lookup_symbol (tmp, expression_context_block,
1305 VAR_NAMESPACE,
1306 &is_a_field_of_this,
1307 (struct symtab **) NULL);
1308 /* second chance uppercased ! */
1309 if (!sym)
1311 for (i = 0;i <= namelen;i++)
1313 if ((tmp[i]>='a' && tmp[i]<='z'))
1314 tmp[i] -= ('a'-'A');
1315 /* I am not sure that copy_name gives excatly the same result ! */
1316 if ((tokstart[i]>='a' && tokstart[i]<='z'))
1317 tokstart[i] -= ('a'-'A');
1319 sym = lookup_symbol (tmp, expression_context_block,
1320 VAR_NAMESPACE,
1321 &is_a_field_of_this,
1322 (struct symtab **) NULL);
1324 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1325 no psymtabs (coff, xcoff, or some future change to blow away the
1326 psymtabs once once symbols are read). */
1327 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1328 lookup_symtab (tmp))
1330 yylval.ssym.sym = sym;
1331 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1332 return BLOCKNAME;
1334 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1336 #if 1
1337 /* Despite the following flaw, we need to keep this code enabled.
1338 Because we can get called from check_stub_method, if we don't
1339 handle nested types then it screws many operations in any
1340 program which uses nested types. */
1341 /* In "A::x", if x is a member function of A and there happens
1342 to be a type (nested or not, since the stabs don't make that
1343 distinction) named x, then this code incorrectly thinks we
1344 are dealing with nested types rather than a member function. */
1346 char *p;
1347 char *namestart;
1348 struct symbol *best_sym;
1350 /* Look ahead to detect nested types. This probably should be
1351 done in the grammar, but trying seemed to introduce a lot
1352 of shift/reduce and reduce/reduce conflicts. It's possible
1353 that it could be done, though. Or perhaps a non-grammar, but
1354 less ad hoc, approach would work well. */
1356 /* Since we do not currently have any way of distinguishing
1357 a nested type from a non-nested one (the stabs don't tell
1358 us whether a type is nested), we just ignore the
1359 containing type. */
1361 p = lexptr;
1362 best_sym = sym;
1363 while (1)
1365 /* Skip whitespace. */
1366 while (*p == ' ' || *p == '\t' || *p == '\n')
1367 ++p;
1368 if (*p == ':' && p[1] == ':')
1370 /* Skip the `::'. */
1371 p += 2;
1372 /* Skip whitespace. */
1373 while (*p == ' ' || *p == '\t' || *p == '\n')
1374 ++p;
1375 namestart = p;
1376 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1377 || (*p >= 'a' && *p <= 'z')
1378 || (*p >= 'A' && *p <= 'Z'))
1379 ++p;
1380 if (p != namestart)
1382 struct symbol *cur_sym;
1383 /* As big as the whole rest of the expression, which is
1384 at least big enough. */
1385 char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1386 char *tmp1;
1388 tmp1 = ncopy;
1389 memcpy (tmp1, tmp, strlen (tmp));
1390 tmp1 += strlen (tmp);
1391 memcpy (tmp1, "::", 2);
1392 tmp1 += 2;
1393 memcpy (tmp1, namestart, p - namestart);
1394 tmp1[p - namestart] = '\0';
1395 cur_sym = lookup_symbol (ncopy, expression_context_block,
1396 VAR_NAMESPACE, (int *) NULL,
1397 (struct symtab **) NULL);
1398 if (cur_sym)
1400 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1402 best_sym = cur_sym;
1403 lexptr = p;
1405 else
1406 break;
1408 else
1409 break;
1411 else
1412 break;
1414 else
1415 break;
1418 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1419 #else /* not 0 */
1420 yylval.tsym.type = SYMBOL_TYPE (sym);
1421 #endif /* not 0 */
1422 return TYPENAME;
1424 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1425 return TYPENAME;
1427 /* Input names that aren't symbols but ARE valid hex numbers,
1428 when the input radix permits them, can be names or numbers
1429 depending on the parse. Note we support radixes > 16 here. */
1430 if (!sym &&
1431 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1432 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1434 YYSTYPE newlval; /* Its value is ignored. */
1435 hextype = parse_number (tokstart, namelen, 0, &newlval);
1436 if (hextype == INT)
1438 yylval.ssym.sym = sym;
1439 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1440 return NAME_OR_INT;
1444 free(uptokstart);
1445 /* Any other kind of symbol */
1446 yylval.ssym.sym = sym;
1447 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1448 return NAME;
1452 void
1453 yyerror (msg)
1454 char *msg;
1456 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);