1 /* YACC parser for C expressions, for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Parse a C expression from text in a string,
23 and return the result as a struct expression pointer.
24 That structure contains arithmetic operations in reverse polish,
25 with constants represented by operations that are followed by special data.
26 See expression.h for the details of the format.
27 What is important here is that it can be built up sequentially
28 during the process of parsing; the lower levels of the tree always
29 come first in the result.
31 Note that malloc's and realloc's in this file are transformed to
32 xmalloc and xrealloc respectively by the same sed command in the
33 makefile that remaps any other malloc/realloc inserted by the parser
34 generator. Doing this with #defines and trying to control the interaction
35 with include files (<malloc.h> and <stdlib.h> for example) just became
36 too messy, particularly when such includes can be inserted at random
37 times by the parser generator. */
42 #include "gdb_string.h"
44 #include "expression.h"
46 #include "parser-defs.h"
49 #include "bfd.h" /* Required by objfiles.h. */
50 #include "symfile.h" /* Required by objfiles.h. */
51 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
53 /* Flag indicating we're dealing with HP-compiled objects */
54 extern int hp_som_som_object_present;
56 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
57 as well as gratuitiously global symbol names, so we can have multiple
58 yacc generated parsers in gdb. Note that these are only the variables
59 produced by yacc. If other parser generators (bison, byacc, etc) produce
60 additional global names that conflict at link time, then those parser
61 generators need to be fixed instead of adding those names to this list. */
63 #define yymaxdepth c_maxdepth
64 #define yyparse c_parse
66 #define yyerror c_error
69 #define yydebug c_debug
78 #define yyerrflag c_errflag
79 #define yynerrs c_nerrs
84 #define yystate c_state
90 #define yyreds c_reds /* With YYDEBUG defined */
91 #define yytoks c_toks /* With YYDEBUG defined */
94 #define yydefred c_yydefred
95 #define yydgoto c_yydgoto
96 #define yysindex c_yysindex
97 #define yyrindex c_yyrindex
98 #define yygindex c_yygindex
99 #define yytable c_yytable
100 #define yycheck c_yycheck
103 #define YYDEBUG 0 /* Default to no yydebug support */
108 static int yylex (void);
110 void yyerror (char *);
114 /* Although the yacc "value" of an expression is not used,
115 since the result is stored in the structure being created,
116 other node types do have values. */
133 struct symtoken ssym;
136 enum exp_opcode opcode;
137 struct internalvar *ivar;
144 /* YYSTYPE gets defined by %union */
145 static int parse_number (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
204 %right '=' ASSIGN_MODIFY
212 %left '<' '>' LEQ GEQ
217 %right UNARY INCREMENT DECREMENT
218 %right ARROW '.' '[' '('
219 %token <ssym> BLOCKNAME
220 %token <bval> FILENAME
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. */
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 : '!' exp %prec UNARY
255 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
258 exp : '~' exp %prec UNARY
259 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
262 exp : INCREMENT exp %prec UNARY
263 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
266 exp : DECREMENT exp %prec UNARY
267 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
270 exp : exp INCREMENT %prec UNARY
271 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
274 exp : exp DECREMENT %prec UNARY
275 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
278 exp : SIZEOF exp %prec UNARY
279 { write_exp_elt_opcode (UNOP_SIZEOF); }
283 { write_exp_elt_opcode (STRUCTOP_PTR);
284 write_exp_string ($3);
285 write_exp_elt_opcode (STRUCTOP_PTR); }
288 exp : exp ARROW qualified_name
289 { /* exp->type::name becomes exp->*(&type::name) */
290 /* Note: this doesn't work if name is a
291 static member! FIXME */
292 write_exp_elt_opcode (UNOP_ADDR);
293 write_exp_elt_opcode (STRUCTOP_MPTR); }
296 exp : exp ARROW '*' exp
297 { write_exp_elt_opcode (STRUCTOP_MPTR); }
301 { write_exp_elt_opcode (STRUCTOP_STRUCT);
302 write_exp_string ($3);
303 write_exp_elt_opcode (STRUCTOP_STRUCT); }
306 exp : exp '.' qualified_name
307 { /* exp.type::name becomes exp.*(&type::name) */
308 /* Note: this doesn't work if name is a
309 static member! FIXME */
310 write_exp_elt_opcode (UNOP_ADDR);
311 write_exp_elt_opcode (STRUCTOP_MEMBER); }
314 exp : exp '.' '*' exp
315 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
318 exp : exp '[' exp1 ']'
319 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
323 /* This is to save the value of arglist_len
324 being accumulated by an outer function call. */
325 { start_arglist (); }
326 arglist ')' %prec ARROW
327 { write_exp_elt_opcode (OP_FUNCALL);
328 write_exp_elt_longcst ((LONGEST) end_arglist ());
329 write_exp_elt_opcode (OP_FUNCALL); }
333 { start_arglist (); }
343 arglist : arglist ',' exp %prec ABOVE_COMMA
348 { $$ = end_arglist () - 1; }
350 exp : lcurly arglist rcurly %prec ARROW
351 { write_exp_elt_opcode (OP_ARRAY);
352 write_exp_elt_longcst ((LONGEST) 0);
353 write_exp_elt_longcst ((LONGEST) $3);
354 write_exp_elt_opcode (OP_ARRAY); }
357 exp : lcurly type rcurly exp %prec UNARY
358 { write_exp_elt_opcode (UNOP_MEMVAL);
359 write_exp_elt_type ($2);
360 write_exp_elt_opcode (UNOP_MEMVAL); }
363 exp : '(' type ')' exp %prec UNARY
364 { write_exp_elt_opcode (UNOP_CAST);
365 write_exp_elt_type ($2);
366 write_exp_elt_opcode (UNOP_CAST); }
373 /* Binary operators in order of decreasing precedence. */
376 { write_exp_elt_opcode (BINOP_REPEAT); }
380 { write_exp_elt_opcode (BINOP_MUL); }
384 { write_exp_elt_opcode (BINOP_DIV); }
388 { write_exp_elt_opcode (BINOP_REM); }
392 { write_exp_elt_opcode (BINOP_ADD); }
396 { write_exp_elt_opcode (BINOP_SUB); }
400 { write_exp_elt_opcode (BINOP_LSH); }
404 { write_exp_elt_opcode (BINOP_RSH); }
408 { write_exp_elt_opcode (BINOP_EQUAL); }
411 exp : exp NOTEQUAL exp
412 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
416 { write_exp_elt_opcode (BINOP_LEQ); }
420 { write_exp_elt_opcode (BINOP_GEQ); }
424 { write_exp_elt_opcode (BINOP_LESS); }
428 { write_exp_elt_opcode (BINOP_GTR); }
432 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
436 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
440 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
444 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
448 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
451 exp : exp '?' exp ':' exp %prec '?'
452 { write_exp_elt_opcode (TERNOP_COND); }
456 { write_exp_elt_opcode (BINOP_ASSIGN); }
459 exp : exp ASSIGN_MODIFY exp
460 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
461 write_exp_elt_opcode ($2);
462 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
466 { write_exp_elt_opcode (OP_LONG);
467 write_exp_elt_type ($1.type);
468 write_exp_elt_longcst ((LONGEST)($1.val));
469 write_exp_elt_opcode (OP_LONG); }
474 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
475 write_exp_elt_opcode (OP_LONG);
476 write_exp_elt_type (val.typed_val_int.type);
477 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
478 write_exp_elt_opcode (OP_LONG);
484 { write_exp_elt_opcode (OP_DOUBLE);
485 write_exp_elt_type ($1.type);
486 write_exp_elt_dblcst ($1.dval);
487 write_exp_elt_opcode (OP_DOUBLE); }
494 /* Already written by write_dollar_variable. */
497 exp : SIZEOF '(' type ')' %prec UNARY
498 { write_exp_elt_opcode (OP_LONG);
499 write_exp_elt_type (builtin_type_int);
501 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
502 write_exp_elt_opcode (OP_LONG); }
506 { /* C strings are converted into array constants with
507 an explicit null byte added at the end. Thus
508 the array upper bound is the string length.
509 There is no such thing in C as a completely empty
511 char *sp = $1.ptr; int count = $1.length;
514 write_exp_elt_opcode (OP_LONG);
515 write_exp_elt_type (builtin_type_char);
516 write_exp_elt_longcst ((LONGEST)(*sp++));
517 write_exp_elt_opcode (OP_LONG);
519 write_exp_elt_opcode (OP_LONG);
520 write_exp_elt_type (builtin_type_char);
521 write_exp_elt_longcst ((LONGEST)'\0');
522 write_exp_elt_opcode (OP_LONG);
523 write_exp_elt_opcode (OP_ARRAY);
524 write_exp_elt_longcst ((LONGEST) 0);
525 write_exp_elt_longcst ((LONGEST) ($1.length));
526 write_exp_elt_opcode (OP_ARRAY); }
531 { write_exp_elt_opcode (OP_THIS);
532 write_exp_elt_opcode (OP_THIS); }
536 { write_exp_elt_opcode (OP_LONG);
537 write_exp_elt_type (builtin_type_bool);
538 write_exp_elt_longcst ((LONGEST) 1);
539 write_exp_elt_opcode (OP_LONG); }
543 { write_exp_elt_opcode (OP_LONG);
544 write_exp_elt_type (builtin_type_bool);
545 write_exp_elt_longcst ((LONGEST) 0);
546 write_exp_elt_opcode (OP_LONG); }
554 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
556 error ("No file or function \"%s\".",
557 copy_name ($1.stoken));
565 block : block COLONCOLON name
567 = lookup_symbol (copy_name ($3), $1,
568 VAR_NAMESPACE, (int *) NULL,
569 (struct symtab **) NULL);
570 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
571 error ("No function \"%s\" in specified context.",
573 $$ = SYMBOL_BLOCK_VALUE (tem); }
576 variable: block COLONCOLON name
577 { struct symbol *sym;
578 sym = lookup_symbol (copy_name ($3), $1,
579 VAR_NAMESPACE, (int *) NULL,
580 (struct symtab **) NULL);
582 error ("No symbol \"%s\" in specified context.",
585 write_exp_elt_opcode (OP_VAR_VALUE);
586 /* block_found is set by lookup_symbol. */
587 write_exp_elt_block (block_found);
588 write_exp_elt_sym (sym);
589 write_exp_elt_opcode (OP_VAR_VALUE); }
592 qualified_name: typebase COLONCOLON name
594 struct type *type = $1;
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 write_exp_elt_opcode (OP_SCOPE);
601 write_exp_elt_type (type);
602 write_exp_string ($3);
603 write_exp_elt_opcode (OP_SCOPE);
605 | typebase COLONCOLON '~' name
607 struct type *type = $1;
608 struct stoken tmp_token;
609 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
610 && TYPE_CODE (type) != TYPE_CODE_UNION)
611 error ("`%s' is not defined as an aggregate type.",
614 tmp_token.ptr = (char*) alloca ($4.length + 2);
615 tmp_token.length = $4.length + 1;
616 tmp_token.ptr[0] = '~';
617 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
618 tmp_token.ptr[tmp_token.length] = 0;
620 /* Check for valid destructor name. */
621 destructor_name_p (tmp_token.ptr, type);
622 write_exp_elt_opcode (OP_SCOPE);
623 write_exp_elt_type (type);
624 write_exp_string (tmp_token);
625 write_exp_elt_opcode (OP_SCOPE);
629 variable: qualified_name
632 char *name = copy_name ($2);
634 struct minimal_symbol *msymbol;
637 lookup_symbol (name, (const struct block *) NULL,
638 VAR_NAMESPACE, (int *) NULL,
639 (struct symtab **) NULL);
642 write_exp_elt_opcode (OP_VAR_VALUE);
643 write_exp_elt_block (NULL);
644 write_exp_elt_sym (sym);
645 write_exp_elt_opcode (OP_VAR_VALUE);
649 msymbol = lookup_minimal_symbol (name, NULL, NULL);
652 write_exp_msymbol (msymbol,
653 lookup_function_type (builtin_type_int),
657 if (!have_full_symbols () && !have_partial_symbols ())
658 error ("No symbol table is loaded. Use the \"file\" command.");
660 error ("No symbol \"%s\" in current context.", name);
664 variable: name_not_typename
665 { struct symbol *sym = $1.sym;
669 if (symbol_read_needs_frame (sym))
671 if (innermost_block == 0 ||
672 contained_in (block_found,
674 innermost_block = block_found;
677 write_exp_elt_opcode (OP_VAR_VALUE);
678 /* We want to use the selected frame, not
679 another more inner frame which happens to
680 be in the same block. */
681 write_exp_elt_block (NULL);
682 write_exp_elt_sym (sym);
683 write_exp_elt_opcode (OP_VAR_VALUE);
685 else if ($1.is_a_field_of_this)
687 /* C++: it hangs off of `this'. Must
688 not inadvertently convert from a method call
690 if (innermost_block == 0 ||
691 contained_in (block_found, innermost_block))
692 innermost_block = block_found;
693 write_exp_elt_opcode (OP_THIS);
694 write_exp_elt_opcode (OP_THIS);
695 write_exp_elt_opcode (STRUCTOP_PTR);
696 write_exp_string ($1.stoken);
697 write_exp_elt_opcode (STRUCTOP_PTR);
701 struct minimal_symbol *msymbol;
702 register char *arg = copy_name ($1.stoken);
705 lookup_minimal_symbol (arg, NULL, NULL);
708 write_exp_msymbol (msymbol,
709 lookup_function_type (builtin_type_int),
712 else if (!have_full_symbols () && !have_partial_symbols ())
713 error ("No symbol table is loaded. Use the \"file\" command.");
715 error ("No symbol \"%s\" in current context.",
716 copy_name ($1.stoken));
721 space_identifier : '@' NAME
722 { push_type_address_space (copy_name ($2.stoken));
723 push_type (tp_space_identifier);
727 const_or_volatile: const_or_volatile_noopt
731 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
734 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
735 | const_or_volatile_noopt
738 const_or_volatile_or_space_identifier:
739 const_or_volatile_or_space_identifier_noopt
744 { push_type (tp_pointer); $$ = 0; }
746 { push_type (tp_pointer); $$ = $2; }
748 { push_type (tp_reference); $$ = 0; }
750 { push_type (tp_reference); $$ = $2; }
754 direct_abs_decl: '(' abs_decl ')'
756 | direct_abs_decl array_mod
759 push_type (tp_array);
764 push_type (tp_array);
768 | direct_abs_decl func_mod
769 { push_type (tp_function); }
771 { push_type (tp_function); }
782 | '(' nonempty_typelist ')'
783 { free ((PTR)$2); $$ = 0; }
786 /* We used to try to recognize more pointer to member types here, but
787 that didn't work (shift/reduce conflicts meant that these rules never
788 got executed). The problem is that
789 int (foo::bar::baz::bizzle)
790 is a function type but
791 int (foo::bar::baz::bizzle::*)
792 is a pointer to member type. Stroustrup loses again! */
795 | typebase COLONCOLON '*'
796 { $$ = lookup_member_type (builtin_type_int, $1); }
799 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
803 { $$ = builtin_type_int; }
805 { $$ = builtin_type_long; }
807 { $$ = builtin_type_short; }
809 { $$ = builtin_type_long; }
810 | UNSIGNED LONG INT_KEYWORD
811 { $$ = builtin_type_unsigned_long; }
813 { $$ = builtin_type_long_long; }
814 | LONG LONG INT_KEYWORD
815 { $$ = builtin_type_long_long; }
817 { $$ = builtin_type_unsigned_long_long; }
818 | UNSIGNED LONG LONG INT_KEYWORD
819 { $$ = builtin_type_unsigned_long_long; }
820 | SIGNED_KEYWORD LONG LONG
821 { $$ = lookup_signed_typename ("long long"); }
822 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
823 { $$ = lookup_signed_typename ("long long"); }
825 { $$ = builtin_type_short; }
826 | UNSIGNED SHORT INT_KEYWORD
827 { $$ = builtin_type_unsigned_short; }
829 { $$ = builtin_type_double; }
830 | LONG DOUBLE_KEYWORD
831 { $$ = builtin_type_long_double; }
833 { $$ = lookup_struct (copy_name ($2),
834 expression_context_block); }
836 { $$ = lookup_struct (copy_name ($2),
837 expression_context_block); }
839 { $$ = lookup_union (copy_name ($2),
840 expression_context_block); }
842 { $$ = lookup_enum (copy_name ($2),
843 expression_context_block); }
845 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
847 { $$ = builtin_type_unsigned_int; }
848 | SIGNED_KEYWORD typename
849 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
851 { $$ = builtin_type_int; }
852 /* It appears that this rule for templates is never
853 reduced; template recognition happens by lookahead
854 in the token processing code in yylex. */
855 | TEMPLATE name '<' type '>'
856 { $$ = lookup_template_type(copy_name($2), $4,
857 expression_context_block);
859 | const_or_volatile_or_space_identifier_noopt typebase
860 { $$ = follow_types ($2); }
861 | typebase const_or_volatile_or_space_identifier_noopt
862 { $$ = follow_types ($1); }
868 $$.stoken.ptr = "int";
869 $$.stoken.length = 3;
870 $$.type = builtin_type_int;
874 $$.stoken.ptr = "long";
875 $$.stoken.length = 4;
876 $$.type = builtin_type_long;
880 $$.stoken.ptr = "short";
881 $$.stoken.length = 5;
882 $$.type = builtin_type_short;
888 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
889 $<ivec>$[0] = 1; /* Number of types in vector */
892 | nonempty_typelist ',' type
893 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
894 $$ = (struct type **) realloc ((char *) $1, len);
895 $$[$<ivec>$[0]] = $3;
900 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
901 { $$ = follow_types ($1); }
904 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
905 | VOLATILE_KEYWORD CONST_KEYWORD
908 const_or_volatile_noopt: const_and_volatile
909 { push_type (tp_const);
910 push_type (tp_volatile);
913 { push_type (tp_const); }
915 { push_type (tp_volatile); }
918 name : NAME { $$ = $1.stoken; }
919 | BLOCKNAME { $$ = $1.stoken; }
920 | TYPENAME { $$ = $1.stoken; }
921 | NAME_OR_INT { $$ = $1.stoken; }
924 name_not_typename : NAME
926 /* These would be useful if name_not_typename was useful, but it is just
927 a fake for "variable", so these cause reduce/reduce conflicts because
928 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
929 =exp) or just an exp. If name_not_typename was ever used in an lvalue
930 context where only a name could occur, this might be useful.
937 /* Take care of parsing a number (anything that starts with a digit).
938 Set yylval and return the token type; update lexptr.
939 LEN is the number of characters in it. */
941 /*** Needs some error checking for the float case ***/
944 parse_number (p, len, parsed_float, putithere)
950 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
951 here, and we do kind of silly things like cast to unsigned. */
952 register LONGEST n = 0;
953 register LONGEST prevn = 0;
958 register int base = input_radix;
961 /* Number of "L" suffixes encountered. */
964 /* We have found a "L" or "U" suffix. */
965 int found_suffix = 0;
968 struct type *signed_type;
969 struct type *unsigned_type;
973 /* It's a float since it contains a point or an exponent. */
975 int num = 0; /* number of tokens scanned by scanf */
976 char saved_char = p[len];
978 p[len] = 0; /* null-terminate the token */
979 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
980 num = sscanf (p, "%g%c", (float *) &putithere->typed_val_float.dval,&c);
981 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
982 num = sscanf (p, "%lg%c", (double *) &putithere->typed_val_float.dval,&c);
985 #ifdef SCANF_HAS_LONG_DOUBLE
986 num = sscanf (p, "%Lg%c", &putithere->typed_val_float.dval,&c);
988 /* Scan it into a double, then assign it to the long double.
989 This at least wins with values representable in the range
992 num = sscanf (p, "%lg%c", &temp,&c);
993 putithere->typed_val_float.dval = temp;
996 p[len] = saved_char; /* restore the input stream */
997 if (num != 1) /* check scanf found ONLY a float ... */
999 /* See if it has `f' or `l' suffix (float or long double). */
1001 c = tolower (p[len - 1]);
1004 putithere->typed_val_float.type = builtin_type_float;
1006 putithere->typed_val_float.type = builtin_type_long_double;
1007 else if (isdigit (c) || c == '.')
1008 putithere->typed_val_float.type = builtin_type_double;
1015 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1049 if (c >= 'A' && c <= 'Z')
1051 if (c != 'l' && c != 'u')
1053 if (c >= '0' && c <= '9')
1061 if (base > 10 && c >= 'a' && c <= 'f')
1065 n += i = c - 'a' + 10;
1078 return ERROR; /* Char not a digit */
1081 return ERROR; /* Invalid digit in this base */
1083 /* Portably test for overflow (only works for nonzero values, so make
1084 a second check for zero). FIXME: Can't we just make n and prevn
1085 unsigned and avoid this? */
1086 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1087 unsigned_p = 1; /* Try something unsigned */
1089 /* Portably test for unsigned overflow.
1090 FIXME: This check is wrong; for example it doesn't find overflow
1091 on 0x123456789 when LONGEST is 32 bits. */
1092 if (c != 'l' && c != 'u' && n != 0)
1094 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1095 error ("Numeric constant too large.");
1100 /* An integer constant is an int, a long, or a long long. An L
1101 suffix forces it to be long; an LL suffix forces it to be long
1102 long. If not forced to a larger size, it gets the first type of
1103 the above that it fits in. To figure out whether it fits, we
1104 shift it right and see whether anything remains. Note that we
1105 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1106 operation, because many compilers will warn about such a shift
1107 (which always produces a zero result). Sometimes TARGET_INT_BIT
1108 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
1109 the case where it is we just always shift the value more than
1110 once, with fewer bits each time. */
1112 un = (ULONGEST)n >> 2;
1114 && (un >> (TARGET_INT_BIT - 2)) == 0)
1116 high_bit = ((ULONGEST)1) << (TARGET_INT_BIT-1);
1118 /* A large decimal (not hex or octal) constant (between INT_MAX
1119 and UINT_MAX) is a long or unsigned long, according to ANSI,
1120 never an unsigned int, but this code treats it as unsigned
1121 int. This probably should be fixed. GCC gives a warning on
1124 unsigned_type = builtin_type_unsigned_int;
1125 signed_type = builtin_type_int;
1127 else if (long_p <= 1
1128 && (un >> (TARGET_LONG_BIT - 2)) == 0)
1130 high_bit = ((ULONGEST)1) << (TARGET_LONG_BIT-1);
1131 unsigned_type = builtin_type_unsigned_long;
1132 signed_type = builtin_type_long;
1137 if (sizeof (ULONGEST) * HOST_CHAR_BIT < TARGET_LONG_LONG_BIT)
1138 /* A long long does not fit in a LONGEST. */
1139 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1141 shift = (TARGET_LONG_LONG_BIT - 1);
1142 high_bit = (ULONGEST) 1 << shift;
1143 unsigned_type = builtin_type_unsigned_long_long;
1144 signed_type = builtin_type_long_long;
1147 putithere->typed_val_int.val = n;
1149 /* If the high bit of the worked out type is set then this number
1150 has to be unsigned. */
1152 if (unsigned_p || (n & high_bit))
1154 putithere->typed_val_int.type = unsigned_type;
1158 putithere->typed_val_int.type = signed_type;
1168 enum exp_opcode opcode;
1171 static const struct token tokentab3[] =
1173 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1174 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1177 static const struct token tokentab2[] =
1179 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1180 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1181 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1182 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1183 {"%=", ASSIGN_MODIFY, BINOP_REM},
1184 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1185 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1186 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1187 {"++", INCREMENT, BINOP_END},
1188 {"--", DECREMENT, BINOP_END},
1189 {"->", ARROW, BINOP_END},
1190 {"&&", ANDAND, BINOP_END},
1191 {"||", OROR, BINOP_END},
1192 {"::", COLONCOLON, BINOP_END},
1193 {"<<", LSH, BINOP_END},
1194 {">>", RSH, BINOP_END},
1195 {"==", EQUAL, BINOP_END},
1196 {"!=", NOTEQUAL, BINOP_END},
1197 {"<=", LEQ, BINOP_END},
1198 {">=", GEQ, BINOP_END}
1201 /* Read one token, getting characters through lexptr. */
1212 static char *tempbuf;
1213 static int tempbufsize;
1214 struct symbol * sym_class = NULL;
1215 char * token_string = NULL;
1216 int class_prefix = 0;
1224 /* See if it is a special token of length 3. */
1225 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1226 if (STREQN (tokstart, tokentab3[i].operator, 3))
1229 yylval.opcode = tokentab3[i].opcode;
1230 return tokentab3[i].token;
1233 /* See if it is a special token of length 2. */
1234 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1235 if (STREQN (tokstart, tokentab2[i].operator, 2))
1238 yylval.opcode = tokentab2[i].opcode;
1239 return tokentab2[i].token;
1242 switch (c = *tokstart)
1254 /* We either have a character constant ('0' or '\177' for example)
1255 or we have a quoted symbol reference ('foo(int,int)' in C++
1260 c = parse_escape (&lexptr);
1262 error ("Empty character constant.");
1264 yylval.typed_val_int.val = c;
1265 yylval.typed_val_int.type = builtin_type_char;
1270 namelen = skip_quoted (tokstart) - tokstart;
1273 lexptr = tokstart + namelen;
1275 if (lexptr[-1] != '\'')
1276 error ("Unmatched single quote.");
1281 error ("Invalid character constant.");
1291 if (paren_depth == 0)
1298 if (comma_terminates && paren_depth == 0)
1304 /* Might be a floating point number. */
1305 if (lexptr[1] < '0' || lexptr[1] > '9')
1306 goto symbol; /* Nope, must be a symbol. */
1307 /* FALL THRU into number case. */
1320 /* It's a number. */
1321 int got_dot = 0, got_e = 0, toktype;
1322 register char *p = tokstart;
1323 int hex = input_radix > 10;
1325 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1330 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1338 /* This test includes !hex because 'e' is a valid hex digit
1339 and thus does not indicate a floating point number when
1340 the radix is hex. */
1341 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1342 got_dot = got_e = 1;
1343 /* This test does not include !hex, because a '.' always indicates
1344 a decimal floating point number regardless of the radix. */
1345 else if (!got_dot && *p == '.')
1347 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1348 && (*p == '-' || *p == '+'))
1349 /* This is the sign of the exponent, not the end of the
1352 /* We will take any letters or digits. parse_number will
1353 complain if past the radix, or if L or U are not final. */
1354 else if ((*p < '0' || *p > '9')
1355 && ((*p < 'a' || *p > 'z')
1356 && (*p < 'A' || *p > 'Z')))
1359 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1360 if (toktype == ERROR)
1362 char *err_copy = (char *) alloca (p - tokstart + 1);
1364 memcpy (err_copy, tokstart, p - tokstart);
1365 err_copy[p - tokstart] = 0;
1366 error ("Invalid number \"%s\".", err_copy);
1398 /* Build the gdb internal form of the input string in tempbuf,
1399 translating any standard C escape forms seen. Note that the
1400 buffer is null byte terminated *only* for the convenience of
1401 debugging gdb itself and printing the buffer contents when
1402 the buffer contains no embedded nulls. Gdb does not depend
1403 upon the buffer being null byte terminated, it uses the length
1404 string instead. This allows gdb to handle C strings (as well
1405 as strings in other languages) with embedded null bytes */
1407 tokptr = ++tokstart;
1411 /* Grow the static temp buffer if necessary, including allocating
1412 the first one on demand. */
1413 if (tempbufindex + 1 >= tempbufsize)
1415 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1421 /* Do nothing, loop will terminate. */
1425 c = parse_escape (&tokptr);
1430 tempbuf[tempbufindex++] = c;
1433 tempbuf[tempbufindex++] = *tokptr++;
1436 } while ((*tokptr != '"') && (*tokptr != '\0'));
1437 if (*tokptr++ != '"')
1439 error ("Unterminated string in expression.");
1441 tempbuf[tempbufindex] = '\0'; /* See note above */
1442 yylval.sval.ptr = tempbuf;
1443 yylval.sval.length = tempbufindex;
1448 if (!(c == '_' || c == '$'
1449 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1450 /* We must have come across a bad character (e.g. ';'). */
1451 error ("Invalid character '%c' in expression.", c);
1453 /* It's a name. See how long it is. */
1455 for (c = tokstart[namelen];
1456 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1457 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1459 /* Template parameter lists are part of the name.
1460 FIXME: This mishandles `print $a<4&&$a>3'. */
1464 /* Scan ahead to get rest of the template specification. Note
1465 that we look ahead only when the '<' adjoins non-whitespace
1466 characters; for comparison expressions, e.g. "a < b > c",
1467 there must be spaces before the '<', etc. */
1469 char * p = find_template_name_end (tokstart + namelen);
1471 namelen = p - tokstart;
1474 c = tokstart[++namelen];
1477 /* The token "if" terminates the expression and is NOT
1478 removed from the input stream. */
1479 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1488 /* Catch specific keywords. Should be done with a data structure. */
1492 if (STREQN (tokstart, "unsigned", 8))
1494 if (current_language->la_language == language_cplus
1495 && STREQN (tokstart, "template", 8))
1497 if (STREQN (tokstart, "volatile", 8))
1498 return VOLATILE_KEYWORD;
1501 if (STREQN (tokstart, "struct", 6))
1503 if (STREQN (tokstart, "signed", 6))
1504 return SIGNED_KEYWORD;
1505 if (STREQN (tokstart, "sizeof", 6))
1507 if (STREQN (tokstart, "double", 6))
1508 return DOUBLE_KEYWORD;
1511 if (current_language->la_language == language_cplus)
1513 if (STREQN (tokstart, "false", 5))
1514 return FALSEKEYWORD;
1515 if (STREQN (tokstart, "class", 5))
1518 if (STREQN (tokstart, "union", 5))
1520 if (STREQN (tokstart, "short", 5))
1522 if (STREQN (tokstart, "const", 5))
1523 return CONST_KEYWORD;
1526 if (STREQN (tokstart, "enum", 4))
1528 if (STREQN (tokstart, "long", 4))
1530 if (current_language->la_language == language_cplus)
1532 if (STREQN (tokstart, "true", 4))
1535 if (STREQN (tokstart, "this", 4))
1537 static const char this_name[] =
1538 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1540 if (lookup_symbol (this_name, expression_context_block,
1541 VAR_NAMESPACE, (int *) NULL,
1542 (struct symtab **) NULL))
1548 if (STREQN (tokstart, "int", 3))
1555 yylval.sval.ptr = tokstart;
1556 yylval.sval.length = namelen;
1558 if (*tokstart == '$')
1560 write_dollar_variable (yylval.sval);
1564 /* Look ahead and see if we can consume more of the input
1565 string to get a reasonable class/namespace spec or a
1566 fully-qualified name. This is a kludge to get around the
1567 HP aCC compiler's generation of symbol names with embedded
1568 colons for namespace and nested classes. */
1571 /* Only do it if not inside single quotes */
1572 sym_class = parse_nested_classes_for_hpacc (yylval.sval.ptr, yylval.sval.length,
1573 &token_string, &class_prefix, &lexptr);
1576 /* Replace the current token with the bigger one we found */
1577 yylval.sval.ptr = token_string;
1578 yylval.sval.length = strlen (token_string);
1582 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1583 functions or symtabs. If this is not so, then ...
1584 Use token-type TYPENAME for symbols that happen to be defined
1585 currently as names of types; NAME for other symbols.
1586 The caller is not constrained to care about the distinction. */
1588 char *tmp = copy_name (yylval.sval);
1590 int is_a_field_of_this = 0;
1593 sym = lookup_symbol (tmp, expression_context_block,
1595 current_language->la_language == language_cplus
1596 ? &is_a_field_of_this : (int *) NULL,
1597 (struct symtab **) NULL);
1598 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1599 no psymtabs (coff, xcoff, or some future change to blow away the
1600 psymtabs once once symbols are read). */
1601 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1603 yylval.ssym.sym = sym;
1604 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1608 { /* See if it's a file name. */
1609 struct symtab *symtab;
1611 symtab = lookup_symtab (tmp);
1615 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1620 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1623 /* Despite the following flaw, we need to keep this code enabled.
1624 Because we can get called from check_stub_method, if we don't
1625 handle nested types then it screws many operations in any
1626 program which uses nested types. */
1627 /* In "A::x", if x is a member function of A and there happens
1628 to be a type (nested or not, since the stabs don't make that
1629 distinction) named x, then this code incorrectly thinks we
1630 are dealing with nested types rather than a member function. */
1634 struct symbol *best_sym;
1636 /* Look ahead to detect nested types. This probably should be
1637 done in the grammar, but trying seemed to introduce a lot
1638 of shift/reduce and reduce/reduce conflicts. It's possible
1639 that it could be done, though. Or perhaps a non-grammar, but
1640 less ad hoc, approach would work well. */
1642 /* Since we do not currently have any way of distinguishing
1643 a nested type from a non-nested one (the stabs don't tell
1644 us whether a type is nested), we just ignore the
1651 /* Skip whitespace. */
1652 while (*p == ' ' || *p == '\t' || *p == '\n')
1654 if (*p == ':' && p[1] == ':')
1656 /* Skip the `::'. */
1658 /* Skip whitespace. */
1659 while (*p == ' ' || *p == '\t' || *p == '\n')
1662 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1663 || (*p >= 'a' && *p <= 'z')
1664 || (*p >= 'A' && *p <= 'Z'))
1668 struct symbol *cur_sym;
1669 /* As big as the whole rest of the expression, which is
1670 at least big enough. */
1671 char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1675 memcpy (tmp1, tmp, strlen (tmp));
1676 tmp1 += strlen (tmp);
1677 memcpy (tmp1, "::", 2);
1679 memcpy (tmp1, namestart, p - namestart);
1680 tmp1[p - namestart] = '\0';
1681 cur_sym = lookup_symbol (ncopy, expression_context_block,
1682 VAR_NAMESPACE, (int *) NULL,
1683 (struct symtab **) NULL);
1686 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1704 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1706 yylval.tsym.type = SYMBOL_TYPE (sym);
1710 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1713 /* Input names that aren't symbols but ARE valid hex numbers,
1714 when the input radix permits them, can be names or numbers
1715 depending on the parse. Note we support radixes > 16 here. */
1717 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1718 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1720 YYSTYPE newlval; /* Its value is ignored. */
1721 hextype = parse_number (tokstart, namelen, 0, &newlval);
1724 yylval.ssym.sym = sym;
1725 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1730 /* Any other kind of symbol */
1731 yylval.ssym.sym = sym;
1732 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1741 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);