1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991, 1993, 1994
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
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.
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. */
42 #include "expression.h"
44 #include "parser-defs.h"
47 #include "bfd.h" /* Required by objfiles.h. */
48 #include "symfile.h" /* Required by objfiles.h. */
49 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
51 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
52 as well as gratuitiously global symbol names, so we can have multiple
53 yacc generated parsers in gdb. Note that these are only the variables
54 produced by yacc. If other parser generators (bison, byacc, etc) produce
55 additional global names that conflict at link time, then those parser
56 generators need to be fixed instead of adding those names to this list. */
58 #define yymaxdepth c_maxdepth
59 #define yyparse c_parse
61 #define yyerror c_error
64 #define yydebug c_debug
73 #define yyerrflag c_errflag
74 #define yynerrs c_nerrs
79 #define yystate c_state
85 #define yyreds c_reds /* With YYDEBUG defined */
86 #define yytoks c_toks /* With YYDEBUG defined */
89 #define YYDEBUG 0 /* Default to no yydebug support */
93 yyparse PARAMS ((void));
96 yylex PARAMS ((void));
99 yyerror PARAMS ((char *));
103 /* Although the yacc "value" of an expression is not used,
104 since the result is stored in the structure being created,
105 other node types do have values. */
119 struct symtoken ssym;
122 enum exp_opcode opcode;
123 struct internalvar *ivar;
130 /* YYSTYPE gets defined by %union */
132 parse_number PARAMS ((char *, int, int, YYSTYPE *));
135 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
137 %type <tval> type typebase
138 %type <tvec> nonempty_typelist
139 /* %type <bval> block */
141 /* Fancy type parsing. */
142 %type <voidval> func_mod direct_abs_decl abs_decl
144 %type <lval> array_mod
146 %token <typed_val> INT
149 /* Both NAME and TYPENAME tokens represent symbols in the input,
150 and both convey their data as strings.
151 But a TYPENAME is a string that happens to be defined as a typedef
152 or builtin type name (such as int or char)
153 and a NAME is any other symbol.
154 Contexts where this distinction is not important can use the
155 nonterminal "name", which matches either NAME or TYPENAME. */
158 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
159 %token <tsym> TYPENAME
161 %type <ssym> name_not_typename
162 %type <tsym> typename
164 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
165 but which would parse as a valid number in the current input radix.
166 E.g. "c" when input_radix==16. Depending on the parse, it will be
167 turned into a name or into a number. */
169 %token <ssym> NAME_OR_INT
171 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
175 /* Special type cases, put in to allow the parser to distinguish different
177 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD
178 %token <lval> LAST REGNAME
180 %token <ivar> VARIABLE
182 %token <opcode> ASSIGN_MODIFY
189 %right '=' ASSIGN_MODIFY
197 %left '<' '>' LEQ GEQ
202 %right UNARY INCREMENT DECREMENT
203 %right ARROW '.' '[' '('
204 %token <ssym> BLOCKNAME
216 { write_exp_elt_opcode(OP_TYPE);
217 write_exp_elt_type($1);
218 write_exp_elt_opcode(OP_TYPE);}
221 /* Expressions, including the comma operator. */
224 { write_exp_elt_opcode (BINOP_COMMA); }
227 /* Expressions, not including the comma operator. */
228 exp : '*' exp %prec UNARY
229 { write_exp_elt_opcode (UNOP_IND); }
231 exp : '&' exp %prec UNARY
232 { write_exp_elt_opcode (UNOP_ADDR); }
234 exp : '-' exp %prec UNARY
235 { write_exp_elt_opcode (UNOP_NEG); }
238 exp : '!' exp %prec UNARY
239 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
242 exp : '~' exp %prec UNARY
243 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
246 exp : INCREMENT exp %prec UNARY
247 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
250 exp : DECREMENT exp %prec UNARY
251 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
254 exp : exp INCREMENT %prec UNARY
255 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
258 exp : exp DECREMENT %prec UNARY
259 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
262 exp : SIZEOF exp %prec UNARY
263 { write_exp_elt_opcode (UNOP_SIZEOF); }
267 { write_exp_elt_opcode (STRUCTOP_PTR);
268 write_exp_string ($3);
269 write_exp_elt_opcode (STRUCTOP_PTR); }
272 exp : exp ARROW qualified_name
273 { /* exp->type::name becomes exp->*(&type::name) */
274 /* Note: this doesn't work if name is a
275 static member! FIXME */
276 write_exp_elt_opcode (UNOP_ADDR);
277 write_exp_elt_opcode (STRUCTOP_MPTR); }
279 exp : exp ARROW '*' exp
280 { write_exp_elt_opcode (STRUCTOP_MPTR); }
284 { write_exp_elt_opcode (STRUCTOP_STRUCT);
285 write_exp_string ($3);
286 write_exp_elt_opcode (STRUCTOP_STRUCT); }
289 exp : exp '.' qualified_name
290 { /* exp.type::name becomes exp.*(&type::name) */
291 /* Note: this doesn't work if name is a
292 static member! FIXME */
293 write_exp_elt_opcode (UNOP_ADDR);
294 write_exp_elt_opcode (STRUCTOP_MEMBER); }
297 exp : exp '.' '*' exp
298 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
301 exp : exp '[' exp1 ']'
302 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
306 /* This is to save the value of arglist_len
307 being accumulated by an outer function call. */
308 { start_arglist (); }
309 arglist ')' %prec ARROW
310 { write_exp_elt_opcode (OP_FUNCALL);
311 write_exp_elt_longcst ((LONGEST) end_arglist ());
312 write_exp_elt_opcode (OP_FUNCALL); }
316 { start_arglist (); }
326 arglist : arglist ',' exp %prec ABOVE_COMMA
331 { $$ = end_arglist () - 1; }
333 exp : lcurly arglist rcurly %prec ARROW
334 { write_exp_elt_opcode (OP_ARRAY);
335 write_exp_elt_longcst ((LONGEST) 0);
336 write_exp_elt_longcst ((LONGEST) $3);
337 write_exp_elt_opcode (OP_ARRAY); }
340 exp : lcurly type rcurly exp %prec UNARY
341 { write_exp_elt_opcode (UNOP_MEMVAL);
342 write_exp_elt_type ($2);
343 write_exp_elt_opcode (UNOP_MEMVAL); }
346 exp : '(' type ')' exp %prec UNARY
347 { write_exp_elt_opcode (UNOP_CAST);
348 write_exp_elt_type ($2);
349 write_exp_elt_opcode (UNOP_CAST); }
356 /* Binary operators in order of decreasing precedence. */
359 { write_exp_elt_opcode (BINOP_REPEAT); }
363 { write_exp_elt_opcode (BINOP_MUL); }
367 { write_exp_elt_opcode (BINOP_DIV); }
371 { write_exp_elt_opcode (BINOP_REM); }
375 { write_exp_elt_opcode (BINOP_ADD); }
379 { write_exp_elt_opcode (BINOP_SUB); }
383 { write_exp_elt_opcode (BINOP_LSH); }
387 { write_exp_elt_opcode (BINOP_RSH); }
391 { write_exp_elt_opcode (BINOP_EQUAL); }
394 exp : exp NOTEQUAL exp
395 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
399 { write_exp_elt_opcode (BINOP_LEQ); }
403 { write_exp_elt_opcode (BINOP_GEQ); }
407 { write_exp_elt_opcode (BINOP_LESS); }
411 { write_exp_elt_opcode (BINOP_GTR); }
415 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
419 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
423 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
427 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
431 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
434 exp : exp '?' exp ':' exp %prec '?'
435 { write_exp_elt_opcode (TERNOP_COND); }
439 { write_exp_elt_opcode (BINOP_ASSIGN); }
442 exp : exp ASSIGN_MODIFY exp
443 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
444 write_exp_elt_opcode ($2);
445 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
449 { write_exp_elt_opcode (OP_LONG);
450 write_exp_elt_type ($1.type);
451 write_exp_elt_longcst ((LONGEST)($1.val));
452 write_exp_elt_opcode (OP_LONG); }
457 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
458 write_exp_elt_opcode (OP_LONG);
459 write_exp_elt_type (val.typed_val.type);
460 write_exp_elt_longcst ((LONGEST)val.typed_val.val);
461 write_exp_elt_opcode (OP_LONG);
467 { write_exp_elt_opcode (OP_DOUBLE);
468 write_exp_elt_type (builtin_type_double);
469 write_exp_elt_dblcst ($1);
470 write_exp_elt_opcode (OP_DOUBLE); }
477 { write_exp_elt_opcode (OP_LAST);
478 write_exp_elt_longcst ((LONGEST) $1);
479 write_exp_elt_opcode (OP_LAST); }
483 { write_exp_elt_opcode (OP_REGISTER);
484 write_exp_elt_longcst ((LONGEST) $1);
485 write_exp_elt_opcode (OP_REGISTER); }
489 { write_exp_elt_opcode (OP_INTERNALVAR);
490 write_exp_elt_intern ($1);
491 write_exp_elt_opcode (OP_INTERNALVAR); }
494 exp : SIZEOF '(' type ')' %prec UNARY
495 { write_exp_elt_opcode (OP_LONG);
496 write_exp_elt_type (builtin_type_int);
497 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
498 write_exp_elt_opcode (OP_LONG); }
502 { /* C strings are converted into array constants with
503 an explicit null byte added at the end. Thus
504 the array upper bound is the string length.
505 There is no such thing in C as a completely empty
507 char *sp = $1.ptr; int count = $1.length;
510 write_exp_elt_opcode (OP_LONG);
511 write_exp_elt_type (builtin_type_char);
512 write_exp_elt_longcst ((LONGEST)(*sp++));
513 write_exp_elt_opcode (OP_LONG);
515 write_exp_elt_opcode (OP_LONG);
516 write_exp_elt_type (builtin_type_char);
517 write_exp_elt_longcst ((LONGEST)'\0');
518 write_exp_elt_opcode (OP_LONG);
519 write_exp_elt_opcode (OP_ARRAY);
520 write_exp_elt_longcst ((LONGEST) 0);
521 write_exp_elt_longcst ((LONGEST) ($1.length));
522 write_exp_elt_opcode (OP_ARRAY); }
527 { write_exp_elt_opcode (OP_THIS);
528 write_exp_elt_opcode (OP_THIS); }
536 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
540 lookup_symtab (copy_name ($1.stoken));
542 $$ = BLOCKVECTOR_BLOCK
543 (BLOCKVECTOR (tem), STATIC_BLOCK);
545 error ("No file or function \"%s\".",
546 copy_name ($1.stoken));
551 block : block COLONCOLON name
553 = lookup_symbol (copy_name ($3), $1,
554 VAR_NAMESPACE, (int *) NULL,
555 (struct symtab **) NULL);
556 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
557 error ("No function \"%s\" in specified context.",
559 $$ = SYMBOL_BLOCK_VALUE (tem); }
562 variable: block COLONCOLON name
563 { struct symbol *sym;
564 sym = lookup_symbol (copy_name ($3), $1,
565 VAR_NAMESPACE, (int *) NULL,
566 (struct symtab **) NULL);
568 error ("No symbol \"%s\" in specified context.",
571 write_exp_elt_opcode (OP_VAR_VALUE);
572 /* block_found is set by lookup_symbol. */
573 write_exp_elt_block (block_found);
574 write_exp_elt_sym (sym);
575 write_exp_elt_opcode (OP_VAR_VALUE); }
578 qualified_name: typebase COLONCOLON name
580 struct type *type = $1;
581 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
582 && TYPE_CODE (type) != TYPE_CODE_UNION)
583 error ("`%s' is not defined as an aggregate type.",
586 write_exp_elt_opcode (OP_SCOPE);
587 write_exp_elt_type (type);
588 write_exp_string ($3);
589 write_exp_elt_opcode (OP_SCOPE);
591 | typebase COLONCOLON '~' name
593 struct type *type = $1;
594 struct stoken tmp_token;
595 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
596 && TYPE_CODE (type) != TYPE_CODE_UNION)
597 error ("`%s' is not defined as an aggregate type.",
600 if (!STREQ (type_name_no_tag (type), $4.ptr))
601 error ("invalid destructor `%s::~%s'",
602 type_name_no_tag (type), $4.ptr);
604 tmp_token.ptr = (char*) alloca ($4.length + 2);
605 tmp_token.length = $4.length + 1;
606 tmp_token.ptr[0] = '~';
607 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
608 tmp_token.ptr[tmp_token.length] = 0;
609 write_exp_elt_opcode (OP_SCOPE);
610 write_exp_elt_type (type);
611 write_exp_string (tmp_token);
612 write_exp_elt_opcode (OP_SCOPE);
616 variable: qualified_name
619 char *name = copy_name ($2);
621 struct minimal_symbol *msymbol;
624 lookup_symbol (name, (const struct block *) NULL,
625 VAR_NAMESPACE, (int *) NULL,
626 (struct symtab **) NULL);
629 write_exp_elt_opcode (OP_VAR_VALUE);
630 write_exp_elt_block (NULL);
631 write_exp_elt_sym (sym);
632 write_exp_elt_opcode (OP_VAR_VALUE);
636 msymbol = lookup_minimal_symbol (name,
637 (struct objfile *) NULL);
640 write_exp_msymbol (msymbol,
641 lookup_function_type (builtin_type_int),
645 if (!have_full_symbols () && !have_partial_symbols ())
646 error ("No symbol table is loaded. Use the \"file\" command.");
648 error ("No symbol \"%s\" in current context.", name);
652 variable: name_not_typename
653 { struct symbol *sym = $1.sym;
657 if (symbol_read_needs_frame (sym))
659 if (innermost_block == 0 ||
660 contained_in (block_found,
662 innermost_block = block_found;
665 write_exp_elt_opcode (OP_VAR_VALUE);
666 /* We want to use the selected frame, not
667 another more inner frame which happens to
668 be in the same block. */
669 write_exp_elt_block (NULL);
670 write_exp_elt_sym (sym);
671 write_exp_elt_opcode (OP_VAR_VALUE);
673 else if ($1.is_a_field_of_this)
675 /* C++: it hangs off of `this'. Must
676 not inadvertently convert from a method call
678 if (innermost_block == 0 ||
679 contained_in (block_found, innermost_block))
680 innermost_block = block_found;
681 write_exp_elt_opcode (OP_THIS);
682 write_exp_elt_opcode (OP_THIS);
683 write_exp_elt_opcode (STRUCTOP_PTR);
684 write_exp_string ($1.stoken);
685 write_exp_elt_opcode (STRUCTOP_PTR);
689 struct minimal_symbol *msymbol;
690 register char *arg = copy_name ($1.stoken);
692 msymbol = lookup_minimal_symbol (arg,
693 (struct objfile *) NULL);
696 write_exp_msymbol (msymbol,
697 lookup_function_type (builtin_type_int),
700 else if (!have_full_symbols () && !have_partial_symbols ())
701 error ("No symbol table is loaded. Use the \"file\" command.");
703 error ("No symbol \"%s\" in current context.",
704 copy_name ($1.stoken));
711 /* "const" and "volatile" are curently ignored. A type qualifier
712 before the type is currently handled in the typebase rule.
713 The reason for recognizing these here (shift/reduce conflicts)
714 might be obsolete now that some pointer to member rules have
716 | typebase CONST_KEYWORD
717 | typebase VOLATILE_KEYWORD
719 { $$ = follow_types ($1); }
720 | typebase CONST_KEYWORD abs_decl
721 { $$ = follow_types ($1); }
722 | typebase VOLATILE_KEYWORD abs_decl
723 { $$ = follow_types ($1); }
727 { push_type (tp_pointer); $$ = 0; }
729 { push_type (tp_pointer); $$ = $2; }
731 { push_type (tp_reference); $$ = 0; }
733 { push_type (tp_reference); $$ = $2; }
737 direct_abs_decl: '(' abs_decl ')'
739 | direct_abs_decl array_mod
742 push_type (tp_array);
747 push_type (tp_array);
751 | direct_abs_decl func_mod
752 { push_type (tp_function); }
754 { push_type (tp_function); }
765 | '(' nonempty_typelist ')'
766 { free ((PTR)$2); $$ = 0; }
769 /* We used to try to recognize more pointer to member types here, but
770 that didn't work (shift/reduce conflicts meant that these rules never
771 got executed). The problem is that
772 int (foo::bar::baz::bizzle)
773 is a function type but
774 int (foo::bar::baz::bizzle::*)
775 is a pointer to member type. Stroustrup loses again! */
778 | typebase COLONCOLON '*'
779 { $$ = lookup_member_type (builtin_type_int, $1); }
782 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
786 { $$ = builtin_type_int; }
788 { $$ = builtin_type_long; }
790 { $$ = builtin_type_short; }
792 { $$ = builtin_type_long; }
793 | UNSIGNED LONG INT_KEYWORD
794 { $$ = builtin_type_unsigned_long; }
796 { $$ = builtin_type_long_long; }
797 | LONG LONG INT_KEYWORD
798 { $$ = builtin_type_long_long; }
800 { $$ = builtin_type_unsigned_long_long; }
801 | UNSIGNED LONG LONG INT_KEYWORD
802 { $$ = builtin_type_unsigned_long_long; }
804 { $$ = builtin_type_short; }
805 | UNSIGNED SHORT INT_KEYWORD
806 { $$ = builtin_type_unsigned_short; }
808 { $$ = lookup_struct (copy_name ($2),
809 expression_context_block); }
811 { $$ = lookup_struct (copy_name ($2),
812 expression_context_block); }
814 { $$ = lookup_union (copy_name ($2),
815 expression_context_block); }
817 { $$ = lookup_enum (copy_name ($2),
818 expression_context_block); }
820 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
822 { $$ = builtin_type_unsigned_int; }
823 | SIGNED_KEYWORD typename
824 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
826 { $$ = builtin_type_int; }
827 | TEMPLATE name '<' type '>'
828 { $$ = lookup_template_type(copy_name($2), $4,
829 expression_context_block);
831 /* "const" and "volatile" are curently ignored. A type qualifier
832 after the type is handled in the ptype rule. I think these could
834 | CONST_KEYWORD typebase { $$ = $2; }
835 | VOLATILE_KEYWORD typebase { $$ = $2; }
841 $$.stoken.ptr = "int";
842 $$.stoken.length = 3;
843 $$.type = builtin_type_int;
847 $$.stoken.ptr = "long";
848 $$.stoken.length = 4;
849 $$.type = builtin_type_long;
853 $$.stoken.ptr = "short";
854 $$.stoken.length = 5;
855 $$.type = builtin_type_short;
861 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
862 $<ivec>$[0] = 1; /* Number of types in vector */
865 | nonempty_typelist ',' type
866 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
867 $$ = (struct type **) realloc ((char *) $1, len);
868 $$[$<ivec>$[0]] = $3;
872 name : NAME { $$ = $1.stoken; }
873 | BLOCKNAME { $$ = $1.stoken; }
874 | TYPENAME { $$ = $1.stoken; }
875 | NAME_OR_INT { $$ = $1.stoken; }
878 name_not_typename : NAME
880 /* These would be useful if name_not_typename was useful, but it is just
881 a fake for "variable", so these cause reduce/reduce conflicts because
882 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
883 =exp) or just an exp. If name_not_typename was ever used in an lvalue
884 context where only a name could occur, this might be useful.
891 /* Take care of parsing a number (anything that starts with a digit).
892 Set yylval and return the token type; update lexptr.
893 LEN is the number of characters in it. */
895 /*** Needs some error checking for the float case ***/
898 parse_number (p, len, parsed_float, putithere)
904 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
905 here, and we do kind of silly things like cast to unsigned. */
906 register LONGEST n = 0;
907 register LONGEST prevn = 0;
912 register int base = input_radix;
915 /* Number of "L" suffixes encountered. */
918 /* We have found a "L" or "U" suffix. */
919 int found_suffix = 0;
921 unsigned LONGEST high_bit;
922 struct type *signed_type;
923 struct type *unsigned_type;
927 /* It's a float since it contains a point or an exponent. */
928 putithere->dval = atof (p);
932 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
966 if (c >= 'A' && c <= 'Z')
968 if (c != 'l' && c != 'u')
970 if (c >= '0' && c <= '9')
978 if (base > 10 && c >= 'a' && c <= 'f')
982 n += i = c - 'a' + 10;
995 return ERROR; /* Char not a digit */
998 return ERROR; /* Invalid digit in this base */
1000 /* Portably test for overflow (only works for nonzero values, so make
1001 a second check for zero). FIXME: Can't we just make n and prevn
1002 unsigned and avoid this? */
1003 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1004 unsigned_p = 1; /* Try something unsigned */
1006 /* Portably test for unsigned overflow.
1007 FIXME: This check is wrong; for example it doesn't find overflow
1008 on 0x123456789 when LONGEST is 32 bits. */
1009 if (c != 'l' && c != 'u' && n != 0)
1011 if ((unsigned_p && (unsigned LONGEST) prevn >= (unsigned LONGEST) n))
1012 error ("Numeric constant too large.");
1017 /* An integer constant is an int, a long, or a long long. An L
1018 suffix forces it to be long; an LL suffix forces it to be long
1019 long. If not forced to a larger size, it gets the first type of
1020 the above that it fits in. To figure out whether it fits, we
1021 shift it right and see whether anything remains. Note that we
1022 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1023 operation, because many compilers will warn about such a shift
1024 (which always produces a zero result). Sometimes TARGET_INT_BIT
1025 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
1026 the case where it is we just always shift the value more than
1027 once, with fewer bits each time. */
1029 un = (unsigned LONGEST)n >> 2;
1031 && (un >> (TARGET_INT_BIT - 2)) == 0)
1033 high_bit = ((unsigned LONGEST)1) << (TARGET_INT_BIT-1);
1035 /* A large decimal (not hex or octal) constant (between INT_MAX
1036 and UINT_MAX) is a long or unsigned long, according to ANSI,
1037 never an unsigned int, but this code treats it as unsigned
1038 int. This probably should be fixed. GCC gives a warning on
1041 unsigned_type = builtin_type_unsigned_int;
1042 signed_type = builtin_type_int;
1044 else if (long_p <= 1
1045 && (un >> (TARGET_LONG_BIT - 2)) == 0)
1047 high_bit = ((unsigned LONGEST)1) << (TARGET_LONG_BIT-1);
1048 unsigned_type = builtin_type_unsigned_long;
1049 signed_type = builtin_type_long;
1053 high_bit = (((unsigned LONGEST)1)
1054 << (TARGET_LONG_LONG_BIT - 32 - 1)
1058 /* A long long does not fit in a LONGEST. */
1060 (unsigned LONGEST)1 << (sizeof (LONGEST) * HOST_CHAR_BIT - 1);
1061 unsigned_type = builtin_type_unsigned_long_long;
1062 signed_type = builtin_type_long_long;
1065 putithere->typed_val.val = n;
1067 /* If the high bit of the worked out type is set then this number
1068 has to be unsigned. */
1070 if (unsigned_p || (n & high_bit))
1072 putithere->typed_val.type = unsigned_type;
1076 putithere->typed_val.type = signed_type;
1086 enum exp_opcode opcode;
1089 static const struct token tokentab3[] =
1091 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1092 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1095 static const struct token tokentab2[] =
1097 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1098 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1099 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1100 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1101 {"%=", ASSIGN_MODIFY, BINOP_REM},
1102 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1103 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1104 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1105 {"++", INCREMENT, BINOP_END},
1106 {"--", DECREMENT, BINOP_END},
1107 {"->", ARROW, BINOP_END},
1108 {"&&", ANDAND, BINOP_END},
1109 {"||", OROR, BINOP_END},
1110 {"::", COLONCOLON, BINOP_END},
1111 {"<<", LSH, BINOP_END},
1112 {">>", RSH, BINOP_END},
1113 {"==", EQUAL, BINOP_END},
1114 {"!=", NOTEQUAL, BINOP_END},
1115 {"<=", LEQ, BINOP_END},
1116 {">=", GEQ, BINOP_END}
1119 /* Read one token, getting characters through lexptr. */
1130 static char *tempbuf;
1131 static int tempbufsize;
1136 /* See if it is a special token of length 3. */
1137 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1138 if (STREQN (tokstart, tokentab3[i].operator, 3))
1141 yylval.opcode = tokentab3[i].opcode;
1142 return tokentab3[i].token;
1145 /* See if it is a special token of length 2. */
1146 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1147 if (STREQN (tokstart, tokentab2[i].operator, 2))
1150 yylval.opcode = tokentab2[i].opcode;
1151 return tokentab2[i].token;
1154 switch (c = *tokstart)
1166 /* We either have a character constant ('0' or '\177' for example)
1167 or we have a quoted symbol reference ('foo(int,int)' in C++
1172 c = parse_escape (&lexptr);
1174 yylval.typed_val.val = c;
1175 yylval.typed_val.type = builtin_type_char;
1180 namelen = skip_quoted (tokstart) - tokstart;
1183 lexptr = tokstart + namelen;
1184 if (lexptr[-1] != '\'')
1185 error ("Unmatched single quote.");
1190 error ("Invalid character constant.");
1200 if (paren_depth == 0)
1207 if (comma_terminates && paren_depth == 0)
1213 /* Might be a floating point number. */
1214 if (lexptr[1] < '0' || lexptr[1] > '9')
1215 goto symbol; /* Nope, must be a symbol. */
1216 /* FALL THRU into number case. */
1229 /* It's a number. */
1230 int got_dot = 0, got_e = 0, toktype;
1231 register char *p = tokstart;
1232 int hex = input_radix > 10;
1234 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1239 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1247 /* This test includes !hex because 'e' is a valid hex digit
1248 and thus does not indicate a floating point number when
1249 the radix is hex. */
1250 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1251 got_dot = got_e = 1;
1252 /* This test does not include !hex, because a '.' always indicates
1253 a decimal floating point number regardless of the radix. */
1254 else if (!got_dot && *p == '.')
1256 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1257 && (*p == '-' || *p == '+'))
1258 /* This is the sign of the exponent, not the end of the
1261 /* We will take any letters or digits. parse_number will
1262 complain if past the radix, or if L or U are not final. */
1263 else if ((*p < '0' || *p > '9')
1264 && ((*p < 'a' || *p > 'z')
1265 && (*p < 'A' || *p > 'Z')))
1268 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1269 if (toktype == ERROR)
1271 char *err_copy = (char *) alloca (p - tokstart + 1);
1273 memcpy (err_copy, tokstart, p - tokstart);
1274 err_copy[p - tokstart] = 0;
1275 error ("Invalid number \"%s\".", err_copy);
1307 /* Build the gdb internal form of the input string in tempbuf,
1308 translating any standard C escape forms seen. Note that the
1309 buffer is null byte terminated *only* for the convenience of
1310 debugging gdb itself and printing the buffer contents when
1311 the buffer contains no embedded nulls. Gdb does not depend
1312 upon the buffer being null byte terminated, it uses the length
1313 string instead. This allows gdb to handle C strings (as well
1314 as strings in other languages) with embedded null bytes */
1316 tokptr = ++tokstart;
1320 /* Grow the static temp buffer if necessary, including allocating
1321 the first one on demand. */
1322 if (tempbufindex + 1 >= tempbufsize)
1324 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1330 /* Do nothing, loop will terminate. */
1334 c = parse_escape (&tokptr);
1339 tempbuf[tempbufindex++] = c;
1342 tempbuf[tempbufindex++] = *tokptr++;
1345 } while ((*tokptr != '"') && (*tokptr != '\0'));
1346 if (*tokptr++ != '"')
1348 error ("Unterminated string in expression.");
1350 tempbuf[tempbufindex] = '\0'; /* See note above */
1351 yylval.sval.ptr = tempbuf;
1352 yylval.sval.length = tempbufindex;
1357 if (!(c == '_' || c == '$'
1358 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1359 /* We must have come across a bad character (e.g. ';'). */
1360 error ("Invalid character '%c' in expression.", c);
1362 /* It's a name. See how long it is. */
1364 for (c = tokstart[namelen];
1365 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1366 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1367 c = tokstart[++namelen])
1370 /* The token "if" terminates the expression and is NOT
1371 removed from the input stream. */
1372 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1379 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1380 and $$digits (equivalent to $<-digits> if you could type that).
1381 Make token type LAST, and put the number (the digits) in yylval. */
1384 if (*tokstart == '$')
1386 register int negate = 0;
1388 /* Double dollar means negate the number and add -1 as well.
1389 Thus $$ alone means -1. */
1390 if (namelen >= 2 && tokstart[1] == '$')
1397 /* Just dollars (one or two) */
1398 yylval.lval = - negate;
1401 /* Is the rest of the token digits? */
1402 for (; c < namelen; c++)
1403 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1407 yylval.lval = atoi (tokstart + 1 + negate);
1409 yylval.lval = - yylval.lval;
1414 /* Handle tokens that refer to machine registers:
1415 $ followed by a register name. */
1417 if (*tokstart == '$') {
1418 for (c = 0; c < NUM_REGS; c++)
1419 if (namelen - 1 == strlen (reg_names[c])
1420 && STREQN (tokstart + 1, reg_names[c], namelen - 1))
1425 for (c = 0; c < num_std_regs; c++)
1426 if (namelen - 1 == strlen (std_regs[c].name)
1427 && STREQN (tokstart + 1, std_regs[c].name, namelen - 1))
1429 yylval.lval = std_regs[c].regnum;
1433 /* Catch specific keywords. Should be done with a data structure. */
1437 if (STREQN (tokstart, "unsigned", 8))
1439 if (current_language->la_language == language_cplus
1440 && STREQN (tokstart, "template", 8))
1442 if (STREQN (tokstart, "volatile", 8))
1443 return VOLATILE_KEYWORD;
1446 if (STREQN (tokstart, "struct", 6))
1448 if (STREQN (tokstart, "signed", 6))
1449 return SIGNED_KEYWORD;
1450 if (STREQN (tokstart, "sizeof", 6))
1454 if (current_language->la_language == language_cplus
1455 && STREQN (tokstart, "class", 5))
1457 if (STREQN (tokstart, "union", 5))
1459 if (STREQN (tokstart, "short", 5))
1461 if (STREQN (tokstart, "const", 5))
1462 return CONST_KEYWORD;
1465 if (STREQN (tokstart, "enum", 4))
1467 if (STREQN (tokstart, "long", 4))
1469 if (current_language->la_language == language_cplus
1470 && STREQN (tokstart, "this", 4))
1472 static const char this_name[] =
1473 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1475 if (lookup_symbol (this_name, expression_context_block,
1476 VAR_NAMESPACE, (int *) NULL,
1477 (struct symtab **) NULL))
1482 if (STREQN (tokstart, "int", 3))
1489 yylval.sval.ptr = tokstart;
1490 yylval.sval.length = namelen;
1492 /* Any other names starting in $ are debugger internal variables. */
1494 if (*tokstart == '$')
1496 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1500 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1501 functions or symtabs. If this is not so, then ...
1502 Use token-type TYPENAME for symbols that happen to be defined
1503 currently as names of types; NAME for other symbols.
1504 The caller is not constrained to care about the distinction. */
1506 char *tmp = copy_name (yylval.sval);
1508 int is_a_field_of_this = 0;
1511 sym = lookup_symbol (tmp, expression_context_block,
1513 current_language->la_language == language_cplus
1514 ? &is_a_field_of_this : (int *) NULL,
1515 (struct symtab **) NULL);
1516 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1517 no psymtabs (coff, xcoff, or some future change to blow away the
1518 psymtabs once once symbols are read). */
1519 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1520 lookup_symtab (tmp))
1522 yylval.ssym.sym = sym;
1523 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1526 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1529 /* Despite the following flaw, we need to keep this code enabled.
1530 Because we can get called from check_stub_method, if we don't
1531 handle nested types then it screws many operations in any
1532 program which uses nested types. */
1533 /* In "A::x", if x is a member function of A and there happens
1534 to be a type (nested or not, since the stabs don't make that
1535 distinction) named x, then this code incorrectly thinks we
1536 are dealing with nested types rather than a member function. */
1540 struct symbol *best_sym;
1542 /* Look ahead to detect nested types. This probably should be
1543 done in the grammar, but trying seemed to introduce a lot
1544 of shift/reduce and reduce/reduce conflicts. It's possible
1545 that it could be done, though. Or perhaps a non-grammar, but
1546 less ad hoc, approach would work well. */
1548 /* Since we do not currently have any way of distinguishing
1549 a nested type from a non-nested one (the stabs don't tell
1550 us whether a type is nested), we just ignore the
1557 /* Skip whitespace. */
1558 while (*p == ' ' || *p == '\t' || *p == '\n')
1560 if (*p == ':' && p[1] == ':')
1562 /* Skip the `::'. */
1564 /* Skip whitespace. */
1565 while (*p == ' ' || *p == '\t' || *p == '\n')
1568 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1569 || (*p >= 'a' && *p <= 'z')
1570 || (*p >= 'A' && *p <= 'Z'))
1574 struct symbol *cur_sym;
1575 /* As big as the whole rest of the expression, which is
1576 at least big enough. */
1577 char *tmp = alloca (strlen (namestart)+1);
1579 memcpy (tmp, namestart, p - namestart);
1580 tmp[p - namestart] = '\0';
1581 cur_sym = lookup_symbol (tmp, expression_context_block,
1582 VAR_NAMESPACE, (int *) NULL,
1583 (struct symtab **) NULL);
1586 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1604 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1606 yylval.tsym.type = SYMBOL_TYPE (sym);
1610 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1613 /* Input names that aren't symbols but ARE valid hex numbers,
1614 when the input radix permits them, can be names or numbers
1615 depending on the parse. Note we support radixes > 16 here. */
1617 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1618 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1620 YYSTYPE newlval; /* Its value is ignored. */
1621 hextype = parse_number (tokstart, namelen, 0, &newlval);
1624 yylval.ssym.sym = sym;
1625 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1630 /* Any other kind of symbol */
1631 yylval.ssym.sym = sym;
1632 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1641 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);