1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991, 1993, 1994, 1996, 1997
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 /* 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. */
41 #include "gdb_string.h"
43 #include "expression.h"
45 #include "parser-defs.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 */
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. */
59 #define yymaxdepth c_maxdepth
60 #define yyparse c_parse
62 #define yyerror c_error
65 #define yydebug c_debug
74 #define yyerrflag c_errflag
75 #define yynerrs c_nerrs
80 #define yystate c_state
86 #define yyreds c_reds /* With YYDEBUG defined */
87 #define yytoks c_toks /* With YYDEBUG defined */
90 #define yydefred c_yydefred
91 #define yydgoto c_yydgoto
92 #define yysindex c_yysindex
93 #define yyrindex c_yyrindex
94 #define yygindex c_yygindex
95 #define yytable c_yytable
96 #define yycheck c_yycheck
99 #define YYDEBUG 0 /* Default to no yydebug support */
103 yyparse PARAMS ((void));
106 yylex PARAMS ((void));
109 yyerror PARAMS ((char *));
113 /* Although the yacc "value" of an expression is not used,
114 since the result is stored in the structure being created,
115 other node types do have values. */
132 struct symtoken ssym;
135 enum exp_opcode opcode;
136 struct internalvar *ivar;
143 /* YYSTYPE gets defined by %union */
145 parse_number PARAMS ((char *, int, int, YYSTYPE *));
148 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
150 %type <tval> type typebase
151 %type <tvec> nonempty_typelist
152 /* %type <bval> block */
154 /* Fancy type parsing. */
155 %type <voidval> func_mod direct_abs_decl abs_decl
157 %type <lval> array_mod
159 %token <typed_val_int> INT
160 %token <typed_val_float> FLOAT
162 /* Both NAME and TYPENAME tokens represent symbols in the input,
163 and both convey their data as strings.
164 But a TYPENAME is a string that happens to be defined as a typedef
165 or builtin type name (such as int or char)
166 and a NAME is any other symbol.
167 Contexts where this distinction is not important can use the
168 nonterminal "name", which matches either NAME or TYPENAME. */
171 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
172 %token <tsym> TYPENAME
174 %type <ssym> name_not_typename
175 %type <tsym> typename
177 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
178 but which would parse as a valid number in the current input radix.
179 E.g. "c" when input_radix==16. Depending on the parse, it will be
180 turned into a name or into a number. */
182 %token <ssym> NAME_OR_INT
184 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
188 /* Special type cases, put in to allow the parser to distinguish different
190 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
192 %token <voidval> VARIABLE
194 %token <opcode> ASSIGN_MODIFY
201 %right '=' ASSIGN_MODIFY
209 %left '<' '>' LEQ GEQ
214 %right UNARY INCREMENT DECREMENT
215 %right ARROW '.' '[' '('
216 %token <ssym> BLOCKNAME
228 { write_exp_elt_opcode(OP_TYPE);
229 write_exp_elt_type($1);
230 write_exp_elt_opcode(OP_TYPE);}
233 /* Expressions, including the comma operator. */
236 { write_exp_elt_opcode (BINOP_COMMA); }
239 /* Expressions, not including the comma operator. */
240 exp : '*' exp %prec UNARY
241 { write_exp_elt_opcode (UNOP_IND); }
243 exp : '&' exp %prec UNARY
244 { write_exp_elt_opcode (UNOP_ADDR); }
246 exp : '-' exp %prec UNARY
247 { write_exp_elt_opcode (UNOP_NEG); }
250 exp : '!' exp %prec UNARY
251 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
254 exp : '~' exp %prec UNARY
255 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
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 INCREMENT %prec UNARY
267 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
270 exp : exp DECREMENT %prec UNARY
271 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
274 exp : SIZEOF exp %prec UNARY
275 { write_exp_elt_opcode (UNOP_SIZEOF); }
279 { write_exp_elt_opcode (STRUCTOP_PTR);
280 write_exp_string ($3);
281 write_exp_elt_opcode (STRUCTOP_PTR); }
284 exp : exp ARROW qualified_name
285 { /* exp->type::name becomes exp->*(&type::name) */
286 /* Note: this doesn't work if name is a
287 static member! FIXME */
288 write_exp_elt_opcode (UNOP_ADDR);
289 write_exp_elt_opcode (STRUCTOP_MPTR); }
291 exp : exp ARROW '*' exp
292 { write_exp_elt_opcode (STRUCTOP_MPTR); }
296 { write_exp_elt_opcode (STRUCTOP_STRUCT);
297 write_exp_string ($3);
298 write_exp_elt_opcode (STRUCTOP_STRUCT); }
302 Need to find a better way to do this...
304 { write_exp_elt_opcode (STRUCTOP_FIELD);
305 write_exp_string ($3);
306 write_exp_elt_opcode (STRUCTOP_FIELD);
310 exp : exp '.' qualified_name
311 { /* exp.type::name becomes exp.*(&type::name) */
312 /* Note: this doesn't work if name is a
313 static member! FIXME */
314 write_exp_elt_opcode (UNOP_ADDR);
315 write_exp_elt_opcode (STRUCTOP_MEMBER); }
318 exp : exp '.' '*' exp
319 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
322 exp : exp '[' exp1 ']'
323 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
327 /* This is to save the value of arglist_len
328 being accumulated by an outer function call. */
329 { start_arglist (); }
330 arglist ')' %prec ARROW
331 { write_exp_elt_opcode (OP_FUNCALL);
332 write_exp_elt_longcst ((LONGEST) end_arglist ());
333 write_exp_elt_opcode (OP_FUNCALL); }
337 { start_arglist (); }
347 arglist : arglist ',' exp %prec ABOVE_COMMA
352 { $$ = end_arglist () - 1; }
354 exp : lcurly arglist rcurly %prec ARROW
355 { write_exp_elt_opcode (OP_ARRAY);
356 write_exp_elt_longcst ((LONGEST) 0);
357 write_exp_elt_longcst ((LONGEST) $3);
358 write_exp_elt_opcode (OP_ARRAY); }
361 exp : lcurly type rcurly exp %prec UNARY
362 { write_exp_elt_opcode (UNOP_MEMVAL);
363 write_exp_elt_type ($2);
364 write_exp_elt_opcode (UNOP_MEMVAL); }
367 exp : '(' type ')' exp %prec UNARY
368 { write_exp_elt_opcode (UNOP_CAST);
369 write_exp_elt_type ($2);
370 write_exp_elt_opcode (UNOP_CAST); }
377 /* Binary operators in order of decreasing precedence. */
380 { write_exp_elt_opcode (BINOP_REPEAT); }
384 { write_exp_elt_opcode (BINOP_MUL); }
388 { write_exp_elt_opcode (BINOP_DIV); }
392 { write_exp_elt_opcode (BINOP_REM); }
396 { write_exp_elt_opcode (BINOP_ADD); }
400 { write_exp_elt_opcode (BINOP_SUB); }
404 { write_exp_elt_opcode (BINOP_LSH); }
408 { write_exp_elt_opcode (BINOP_RSH); }
412 { write_exp_elt_opcode (BINOP_EQUAL); }
415 exp : exp NOTEQUAL exp
416 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
420 { write_exp_elt_opcode (BINOP_LEQ); }
424 { write_exp_elt_opcode (BINOP_GEQ); }
428 { write_exp_elt_opcode (BINOP_LESS); }
432 { write_exp_elt_opcode (BINOP_GTR); }
436 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
440 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
444 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
448 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
452 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
455 exp : exp '?' exp ':' exp %prec '?'
456 { write_exp_elt_opcode (TERNOP_COND); }
460 { write_exp_elt_opcode (BINOP_ASSIGN); }
463 exp : exp ASSIGN_MODIFY exp
464 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
465 write_exp_elt_opcode ($2);
466 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
470 { write_exp_elt_opcode (OP_LONG);
471 write_exp_elt_type ($1.type);
472 write_exp_elt_longcst ((LONGEST)($1.val));
473 write_exp_elt_opcode (OP_LONG); }
478 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
479 write_exp_elt_opcode (OP_LONG);
480 write_exp_elt_type (val.typed_val_int.type);
481 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
482 write_exp_elt_opcode (OP_LONG);
488 { write_exp_elt_opcode (OP_DOUBLE);
489 write_exp_elt_type ($1.type);
490 write_exp_elt_dblcst ($1.dval);
491 write_exp_elt_opcode (OP_DOUBLE); }
498 /* Already written by write_dollar_variable. */
501 exp : SIZEOF '(' type ')' %prec UNARY
502 { write_exp_elt_opcode (OP_LONG);
503 write_exp_elt_type (builtin_type_int);
505 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
506 write_exp_elt_opcode (OP_LONG); }
510 { /* C strings are converted into array constants with
511 an explicit null byte added at the end. Thus
512 the array upper bound is the string length.
513 There is no such thing in C as a completely empty
515 char *sp = $1.ptr; int count = $1.length;
518 write_exp_elt_opcode (OP_LONG);
519 write_exp_elt_type (builtin_type_char);
520 write_exp_elt_longcst ((LONGEST)(*sp++));
521 write_exp_elt_opcode (OP_LONG);
523 write_exp_elt_opcode (OP_LONG);
524 write_exp_elt_type (builtin_type_char);
525 write_exp_elt_longcst ((LONGEST)'\0');
526 write_exp_elt_opcode (OP_LONG);
527 write_exp_elt_opcode (OP_ARRAY);
528 write_exp_elt_longcst ((LONGEST) 0);
529 write_exp_elt_longcst ((LONGEST) ($1.length));
530 write_exp_elt_opcode (OP_ARRAY); }
535 { write_exp_elt_opcode (OP_THIS);
536 write_exp_elt_opcode (OP_THIS); }
544 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
548 lookup_symtab (copy_name ($1.stoken));
550 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), STATIC_BLOCK);
552 error ("No file or function \"%s\".",
553 copy_name ($1.stoken));
558 block : block COLONCOLON name
560 = lookup_symbol (copy_name ($3), $1,
561 VAR_NAMESPACE, (int *) NULL,
562 (struct symtab **) NULL);
563 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
564 error ("No function \"%s\" in specified context.",
566 $$ = SYMBOL_BLOCK_VALUE (tem); }
569 variable: block COLONCOLON name
570 { struct symbol *sym;
571 sym = lookup_symbol (copy_name ($3), $1,
572 VAR_NAMESPACE, (int *) NULL,
573 (struct symtab **) NULL);
575 error ("No symbol \"%s\" in specified context.",
578 write_exp_elt_opcode (OP_VAR_VALUE);
579 /* block_found is set by lookup_symbol. */
580 write_exp_elt_block (block_found);
581 write_exp_elt_sym (sym);
582 write_exp_elt_opcode (OP_VAR_VALUE); }
585 qualified_name: typebase COLONCOLON name
587 struct type *type = $1;
588 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
589 && TYPE_CODE (type) != TYPE_CODE_UNION)
590 error ("`%s' is not defined as an aggregate type.",
593 write_exp_elt_opcode (OP_SCOPE);
594 write_exp_elt_type (type);
595 write_exp_string ($3);
596 write_exp_elt_opcode (OP_SCOPE);
598 | typebase COLONCOLON '~' name
600 struct type *type = $1;
601 struct stoken tmp_token;
602 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
603 && TYPE_CODE (type) != TYPE_CODE_UNION)
604 error ("`%s' is not defined as an aggregate type.",
607 tmp_token.ptr = (char*) alloca ($4.length + 2);
608 tmp_token.length = $4.length + 1;
609 tmp_token.ptr[0] = '~';
610 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
611 tmp_token.ptr[tmp_token.length] = 0;
613 /* Check for valid destructor name. */
614 destructor_name_p (tmp_token.ptr, type);
615 write_exp_elt_opcode (OP_SCOPE);
616 write_exp_elt_type (type);
617 write_exp_string (tmp_token);
618 write_exp_elt_opcode (OP_SCOPE);
622 variable: qualified_name
625 char *name = copy_name ($2);
627 struct minimal_symbol *msymbol;
630 lookup_symbol (name, (const struct block *) NULL,
631 VAR_NAMESPACE, (int *) NULL,
632 (struct symtab **) NULL);
635 write_exp_elt_opcode (OP_VAR_VALUE);
636 write_exp_elt_block (NULL);
637 write_exp_elt_sym (sym);
638 write_exp_elt_opcode (OP_VAR_VALUE);
642 msymbol = lookup_minimal_symbol (name, NULL, NULL);
645 write_exp_msymbol (msymbol,
646 lookup_function_type (builtin_type_int),
650 if (!have_full_symbols () && !have_partial_symbols ())
651 error ("No symbol table is loaded. Use the \"file\" command.");
653 error ("No symbol \"%s\" in current context.", name);
657 variable: name_not_typename
658 { struct symbol *sym = $1.sym;
662 if (symbol_read_needs_frame (sym))
664 if (innermost_block == 0 ||
665 contained_in (block_found,
667 innermost_block = block_found;
670 write_exp_elt_opcode (OP_VAR_VALUE);
671 /* We want to use the selected frame, not
672 another more inner frame which happens to
673 be in the same block. */
674 write_exp_elt_block (NULL);
675 write_exp_elt_sym (sym);
676 write_exp_elt_opcode (OP_VAR_VALUE);
678 else if ($1.is_a_field_of_this)
680 /* C++: it hangs off of `this'. Must
681 not inadvertently convert from a method call
683 if (innermost_block == 0 ||
684 contained_in (block_found, innermost_block))
685 innermost_block = block_found;
686 write_exp_elt_opcode (OP_THIS);
687 write_exp_elt_opcode (OP_THIS);
688 write_exp_elt_opcode (STRUCTOP_PTR);
689 write_exp_string ($1.stoken);
690 write_exp_elt_opcode (STRUCTOP_PTR);
694 struct minimal_symbol *msymbol;
695 register char *arg = copy_name ($1.stoken);
698 lookup_minimal_symbol (arg, NULL, NULL);
701 write_exp_msymbol (msymbol,
702 lookup_function_type (builtin_type_int),
705 else if (!have_full_symbols () && !have_partial_symbols ())
706 error ("No symbol table is loaded. Use the \"file\" command.");
708 error ("No symbol \"%s\" in current context.",
709 copy_name ($1.stoken));
716 /* "const" and "volatile" are curently ignored. A type qualifier
717 before the type is currently handled in the typebase rule.
718 The reason for recognizing these here (shift/reduce conflicts)
719 might be obsolete now that some pointer to member rules have
721 | typebase CONST_KEYWORD
722 | typebase VOLATILE_KEYWORD
724 { $$ = follow_types ($1); }
725 | typebase CONST_KEYWORD abs_decl
726 { $$ = follow_types ($1); }
727 | typebase VOLATILE_KEYWORD abs_decl
728 { $$ = follow_types ($1); }
732 { push_type (tp_pointer); $$ = 0; }
734 { push_type (tp_pointer); $$ = $2; }
736 { push_type (tp_reference); $$ = 0; }
738 { push_type (tp_reference); $$ = $2; }
742 direct_abs_decl: '(' abs_decl ')'
744 | direct_abs_decl array_mod
747 push_type (tp_array);
752 push_type (tp_array);
756 | direct_abs_decl func_mod
757 { push_type (tp_function); }
759 { push_type (tp_function); }
770 | '(' nonempty_typelist ')'
771 { free ((PTR)$2); $$ = 0; }
774 /* We used to try to recognize more pointer to member types here, but
775 that didn't work (shift/reduce conflicts meant that these rules never
776 got executed). The problem is that
777 int (foo::bar::baz::bizzle)
778 is a function type but
779 int (foo::bar::baz::bizzle::*)
780 is a pointer to member type. Stroustrup loses again! */
783 | typebase COLONCOLON '*'
784 { $$ = lookup_member_type (builtin_type_int, $1); }
787 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
791 { $$ = builtin_type_int; }
793 { $$ = builtin_type_long; }
795 { $$ = builtin_type_short; }
797 { $$ = builtin_type_long; }
798 | UNSIGNED LONG INT_KEYWORD
799 { $$ = builtin_type_unsigned_long; }
801 { $$ = builtin_type_long_long; }
802 | LONG LONG INT_KEYWORD
803 { $$ = builtin_type_long_long; }
805 { $$ = builtin_type_unsigned_long_long; }
806 | UNSIGNED LONG LONG INT_KEYWORD
807 { $$ = builtin_type_unsigned_long_long; }
809 { $$ = builtin_type_short; }
810 | UNSIGNED SHORT INT_KEYWORD
811 { $$ = builtin_type_unsigned_short; }
813 { $$ = builtin_type_double; }
814 | LONG DOUBLE_KEYWORD
815 { $$ = builtin_type_long_double; }
817 { $$ = lookup_struct (copy_name ($2),
818 expression_context_block); }
820 { $$ = lookup_struct (copy_name ($2),
821 expression_context_block); }
823 { $$ = lookup_union (copy_name ($2),
824 expression_context_block); }
826 { $$ = lookup_enum (copy_name ($2),
827 expression_context_block); }
829 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
831 { $$ = builtin_type_unsigned_int; }
832 | SIGNED_KEYWORD typename
833 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
835 { $$ = builtin_type_int; }
836 | TEMPLATE name '<' type '>'
837 { $$ = lookup_template_type(copy_name($2), $4,
838 expression_context_block);
840 /* "const" and "volatile" are curently ignored. A type qualifier
841 after the type is handled in the ptype rule. I think these could
843 | CONST_KEYWORD typebase { $$ = $2; }
844 | VOLATILE_KEYWORD typebase { $$ = $2; }
850 $$.stoken.ptr = "int";
851 $$.stoken.length = 3;
852 $$.type = builtin_type_int;
856 $$.stoken.ptr = "long";
857 $$.stoken.length = 4;
858 $$.type = builtin_type_long;
862 $$.stoken.ptr = "short";
863 $$.stoken.length = 5;
864 $$.type = builtin_type_short;
870 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
871 $<ivec>$[0] = 1; /* Number of types in vector */
874 | nonempty_typelist ',' type
875 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
876 $$ = (struct type **) realloc ((char *) $1, len);
877 $$[$<ivec>$[0]] = $3;
881 name : NAME { $$ = $1.stoken; }
882 | BLOCKNAME { $$ = $1.stoken; }
883 | TYPENAME { $$ = $1.stoken; }
884 | NAME_OR_INT { $$ = $1.stoken; }
887 name_not_typename : NAME
889 /* These would be useful if name_not_typename was useful, but it is just
890 a fake for "variable", so these cause reduce/reduce conflicts because
891 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
892 =exp) or just an exp. If name_not_typename was ever used in an lvalue
893 context where only a name could occur, this might be useful.
900 /* Take care of parsing a number (anything that starts with a digit).
901 Set yylval and return the token type; update lexptr.
902 LEN is the number of characters in it. */
904 /*** Needs some error checking for the float case ***/
907 parse_number (p, len, parsed_float, putithere)
913 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
914 here, and we do kind of silly things like cast to unsigned. */
915 register LONGEST n = 0;
916 register LONGEST prevn = 0;
921 register int base = input_radix;
924 /* Number of "L" suffixes encountered. */
927 /* We have found a "L" or "U" suffix. */
928 int found_suffix = 0;
931 struct type *signed_type;
932 struct type *unsigned_type;
938 /* It's a float since it contains a point or an exponent. */
940 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
941 sscanf (p, "%g", &putithere->typed_val_float.dval);
942 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
943 sscanf (p, "%lg", &putithere->typed_val_float.dval);
946 #ifdef PRINTF_HAS_LONG_DOUBLE
947 sscanf (p, "%Lg", &putithere->typed_val_float.dval);
949 /* Scan it into a double, then assign it to the long double.
950 This at least wins with values representable in the range
953 sscanf (p, "%lg", &temp);
954 putithere->typed_val_float.dval = temp;
958 /* See if it has `f' or `l' suffix (float or long double). */
960 c = tolower (p[len - 1]);
963 putithere->typed_val_float.type = builtin_type_float;
965 putithere->typed_val_float.type = builtin_type_long_double;
966 else if (isdigit (c) || c == '.')
967 putithere->typed_val_float.type = builtin_type_double;
974 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1008 if (c >= 'A' && c <= 'Z')
1010 if (c != 'l' && c != 'u')
1012 if (c >= '0' && c <= '9')
1020 if (base > 10 && c >= 'a' && c <= 'f')
1024 n += i = c - 'a' + 10;
1037 return ERROR; /* Char not a digit */
1040 return ERROR; /* Invalid digit in this base */
1042 /* Portably test for overflow (only works for nonzero values, so make
1043 a second check for zero). FIXME: Can't we just make n and prevn
1044 unsigned and avoid this? */
1045 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1046 unsigned_p = 1; /* Try something unsigned */
1048 /* Portably test for unsigned overflow.
1049 FIXME: This check is wrong; for example it doesn't find overflow
1050 on 0x123456789 when LONGEST is 32 bits. */
1051 if (c != 'l' && c != 'u' && n != 0)
1053 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1054 error ("Numeric constant too large.");
1059 /* An integer constant is an int, a long, or a long long. An L
1060 suffix forces it to be long; an LL suffix forces it to be long
1061 long. If not forced to a larger size, it gets the first type of
1062 the above that it fits in. To figure out whether it fits, we
1063 shift it right and see whether anything remains. Note that we
1064 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1065 operation, because many compilers will warn about such a shift
1066 (which always produces a zero result). Sometimes TARGET_INT_BIT
1067 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
1068 the case where it is we just always shift the value more than
1069 once, with fewer bits each time. */
1071 un = (ULONGEST)n >> 2;
1073 && (un >> (TARGET_INT_BIT - 2)) == 0)
1075 high_bit = ((ULONGEST)1) << (TARGET_INT_BIT-1);
1077 /* A large decimal (not hex or octal) constant (between INT_MAX
1078 and UINT_MAX) is a long or unsigned long, according to ANSI,
1079 never an unsigned int, but this code treats it as unsigned
1080 int. This probably should be fixed. GCC gives a warning on
1083 unsigned_type = builtin_type_unsigned_int;
1084 signed_type = builtin_type_int;
1086 else if (long_p <= 1
1087 && (un >> (TARGET_LONG_BIT - 2)) == 0)
1089 high_bit = ((ULONGEST)1) << (TARGET_LONG_BIT-1);
1090 unsigned_type = builtin_type_unsigned_long;
1091 signed_type = builtin_type_long;
1095 high_bit = (((ULONGEST)1)
1096 << (TARGET_LONG_LONG_BIT - 32 - 1)
1100 /* A long long does not fit in a LONGEST. */
1102 (ULONGEST)1 << (sizeof (LONGEST) * HOST_CHAR_BIT - 1);
1103 unsigned_type = builtin_type_unsigned_long_long;
1104 signed_type = builtin_type_long_long;
1107 putithere->typed_val_int.val = n;
1109 /* If the high bit of the worked out type is set then this number
1110 has to be unsigned. */
1112 if (unsigned_p || (n & high_bit))
1114 putithere->typed_val_int.type = unsigned_type;
1118 putithere->typed_val_int.type = signed_type;
1128 enum exp_opcode opcode;
1131 static const struct token tokentab3[] =
1133 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1134 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1137 static const struct token tokentab2[] =
1139 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1140 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1141 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1142 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1143 {"%=", ASSIGN_MODIFY, BINOP_REM},
1144 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1145 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1146 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1147 {"++", INCREMENT, BINOP_END},
1148 {"--", DECREMENT, BINOP_END},
1149 {"->", ARROW, BINOP_END},
1150 {"&&", ANDAND, BINOP_END},
1151 {"||", OROR, BINOP_END},
1152 {"::", COLONCOLON, BINOP_END},
1153 {"<<", LSH, BINOP_END},
1154 {">>", RSH, BINOP_END},
1155 {"==", EQUAL, BINOP_END},
1156 {"!=", NOTEQUAL, BINOP_END},
1157 {"<=", LEQ, BINOP_END},
1158 {">=", GEQ, BINOP_END}
1161 /* Read one token, getting characters through lexptr. */
1172 static char *tempbuf;
1173 static int tempbufsize;
1178 /* See if it is a special token of length 3. */
1179 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1180 if (STREQN (tokstart, tokentab3[i].operator, 3))
1183 yylval.opcode = tokentab3[i].opcode;
1184 return tokentab3[i].token;
1187 /* See if it is a special token of length 2. */
1188 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1189 if (STREQN (tokstart, tokentab2[i].operator, 2))
1192 yylval.opcode = tokentab2[i].opcode;
1193 return tokentab2[i].token;
1196 switch (c = *tokstart)
1208 /* We either have a character constant ('0' or '\177' for example)
1209 or we have a quoted symbol reference ('foo(int,int)' in C++
1214 c = parse_escape (&lexptr);
1216 error ("Empty character constant.");
1218 yylval.typed_val_int.val = c;
1219 yylval.typed_val_int.type = builtin_type_char;
1224 namelen = skip_quoted (tokstart) - tokstart;
1227 lexptr = tokstart + namelen;
1228 if (lexptr[-1] != '\'')
1229 error ("Unmatched single quote.");
1234 error ("Invalid character constant.");
1244 if (paren_depth == 0)
1251 if (comma_terminates && paren_depth == 0)
1257 /* Might be a floating point number. */
1258 if (lexptr[1] < '0' || lexptr[1] > '9')
1259 goto symbol; /* Nope, must be a symbol. */
1260 /* FALL THRU into number case. */
1273 /* It's a number. */
1274 int got_dot = 0, got_e = 0, toktype;
1275 register char *p = tokstart;
1276 int hex = input_radix > 10;
1278 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1283 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1291 /* This test includes !hex because 'e' is a valid hex digit
1292 and thus does not indicate a floating point number when
1293 the radix is hex. */
1294 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1295 got_dot = got_e = 1;
1296 /* This test does not include !hex, because a '.' always indicates
1297 a decimal floating point number regardless of the radix. */
1298 else if (!got_dot && *p == '.')
1300 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1301 && (*p == '-' || *p == '+'))
1302 /* This is the sign of the exponent, not the end of the
1305 /* We will take any letters or digits. parse_number will
1306 complain if past the radix, or if L or U are not final. */
1307 else if ((*p < '0' || *p > '9')
1308 && ((*p < 'a' || *p > 'z')
1309 && (*p < 'A' || *p > 'Z')))
1312 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1313 if (toktype == ERROR)
1315 char *err_copy = (char *) alloca (p - tokstart + 1);
1317 memcpy (err_copy, tokstart, p - tokstart);
1318 err_copy[p - tokstart] = 0;
1319 error ("Invalid number \"%s\".", err_copy);
1351 /* Build the gdb internal form of the input string in tempbuf,
1352 translating any standard C escape forms seen. Note that the
1353 buffer is null byte terminated *only* for the convenience of
1354 debugging gdb itself and printing the buffer contents when
1355 the buffer contains no embedded nulls. Gdb does not depend
1356 upon the buffer being null byte terminated, it uses the length
1357 string instead. This allows gdb to handle C strings (as well
1358 as strings in other languages) with embedded null bytes */
1360 tokptr = ++tokstart;
1364 /* Grow the static temp buffer if necessary, including allocating
1365 the first one on demand. */
1366 if (tempbufindex + 1 >= tempbufsize)
1368 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1374 /* Do nothing, loop will terminate. */
1378 c = parse_escape (&tokptr);
1383 tempbuf[tempbufindex++] = c;
1386 tempbuf[tempbufindex++] = *tokptr++;
1389 } while ((*tokptr != '"') && (*tokptr != '\0'));
1390 if (*tokptr++ != '"')
1392 error ("Unterminated string in expression.");
1394 tempbuf[tempbufindex] = '\0'; /* See note above */
1395 yylval.sval.ptr = tempbuf;
1396 yylval.sval.length = tempbufindex;
1401 if (!(c == '_' || c == '$'
1402 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1403 /* We must have come across a bad character (e.g. ';'). */
1404 error ("Invalid character '%c' in expression.", c);
1406 /* It's a name. See how long it is. */
1408 for (c = tokstart[namelen];
1409 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1410 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1412 /* Template parameter lists are part of the name.
1413 FIXME: This mishandles `print $a<4&&$a>3'. */
1418 int nesting_level = 1;
1419 while (tokstart[++i])
1421 if (tokstart[i] == '<')
1423 else if (tokstart[i] == '>')
1425 if (--nesting_level == 0)
1429 if (tokstart[i] == '>')
1434 c = tokstart[++namelen];
1437 /* The token "if" terminates the expression and is NOT
1438 removed from the input stream. */
1439 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1448 /* Catch specific keywords. Should be done with a data structure. */
1452 if (STREQN (tokstart, "unsigned", 8))
1454 if (current_language->la_language == language_cplus
1455 && STREQN (tokstart, "template", 8))
1457 if (STREQN (tokstart, "volatile", 8))
1458 return VOLATILE_KEYWORD;
1461 if (STREQN (tokstart, "struct", 6))
1463 if (STREQN (tokstart, "signed", 6))
1464 return SIGNED_KEYWORD;
1465 if (STREQN (tokstart, "sizeof", 6))
1467 if (STREQN (tokstart, "double", 6))
1468 return DOUBLE_KEYWORD;
1471 if (current_language->la_language == language_cplus
1472 && STREQN (tokstart, "class", 5))
1474 if (STREQN (tokstart, "union", 5))
1476 if (STREQN (tokstart, "short", 5))
1478 if (STREQN (tokstart, "const", 5))
1479 return CONST_KEYWORD;
1482 if (STREQN (tokstart, "enum", 4))
1484 if (STREQN (tokstart, "long", 4))
1486 if (current_language->la_language == language_cplus
1487 && STREQN (tokstart, "this", 4))
1489 static const char this_name[] =
1490 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1492 if (lookup_symbol (this_name, expression_context_block,
1493 VAR_NAMESPACE, (int *) NULL,
1494 (struct symtab **) NULL))
1499 if (STREQN (tokstart, "int", 3))
1506 yylval.sval.ptr = tokstart;
1507 yylval.sval.length = namelen;
1509 if (*tokstart == '$')
1511 write_dollar_variable (yylval.sval);
1515 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1516 functions or symtabs. If this is not so, then ...
1517 Use token-type TYPENAME for symbols that happen to be defined
1518 currently as names of types; NAME for other symbols.
1519 The caller is not constrained to care about the distinction. */
1521 char *tmp = copy_name (yylval.sval);
1523 int is_a_field_of_this = 0;
1526 sym = lookup_symbol (tmp, expression_context_block,
1528 current_language->la_language == language_cplus
1529 ? &is_a_field_of_this : (int *) NULL,
1530 (struct symtab **) NULL);
1531 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1532 no psymtabs (coff, xcoff, or some future change to blow away the
1533 psymtabs once once symbols are read). */
1534 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1535 lookup_symtab (tmp))
1537 yylval.ssym.sym = sym;
1538 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1541 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1544 /* Despite the following flaw, we need to keep this code enabled.
1545 Because we can get called from check_stub_method, if we don't
1546 handle nested types then it screws many operations in any
1547 program which uses nested types. */
1548 /* In "A::x", if x is a member function of A and there happens
1549 to be a type (nested or not, since the stabs don't make that
1550 distinction) named x, then this code incorrectly thinks we
1551 are dealing with nested types rather than a member function. */
1555 struct symbol *best_sym;
1557 /* Look ahead to detect nested types. This probably should be
1558 done in the grammar, but trying seemed to introduce a lot
1559 of shift/reduce and reduce/reduce conflicts. It's possible
1560 that it could be done, though. Or perhaps a non-grammar, but
1561 less ad hoc, approach would work well. */
1563 /* Since we do not currently have any way of distinguishing
1564 a nested type from a non-nested one (the stabs don't tell
1565 us whether a type is nested), we just ignore the
1572 /* Skip whitespace. */
1573 while (*p == ' ' || *p == '\t' || *p == '\n')
1575 if (*p == ':' && p[1] == ':')
1577 /* Skip the `::'. */
1579 /* Skip whitespace. */
1580 while (*p == ' ' || *p == '\t' || *p == '\n')
1583 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1584 || (*p >= 'a' && *p <= 'z')
1585 || (*p >= 'A' && *p <= 'Z'))
1589 struct symbol *cur_sym;
1590 /* As big as the whole rest of the expression, which is
1591 at least big enough. */
1592 char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1596 memcpy (tmp1, tmp, strlen (tmp));
1597 tmp1 += strlen (tmp);
1598 memcpy (tmp1, "::", 2);
1600 memcpy (tmp1, namestart, p - namestart);
1601 tmp1[p - namestart] = '\0';
1602 cur_sym = lookup_symbol (ncopy, expression_context_block,
1603 VAR_NAMESPACE, (int *) NULL,
1604 (struct symtab **) NULL);
1607 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1625 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1627 yylval.tsym.type = SYMBOL_TYPE (sym);
1631 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1634 /* Input names that aren't symbols but ARE valid hex numbers,
1635 when the input radix permits them, can be names or numbers
1636 depending on the parse. Note we support radixes > 16 here. */
1638 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1639 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1641 YYSTYPE newlval; /* Its value is ignored. */
1642 hextype = parse_number (tokstart, namelen, 0, &newlval);
1645 yylval.ssym.sym = sym;
1646 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1651 /* Any other kind of symbol */
1652 yylval.ssym.sym = sym;
1653 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1662 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);