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 1 /* Default to yydebug support */
106 #define YYFPRINTF parser_fprintf
110 static int yylex (void);
112 void yyerror (char *);
116 /* Although the yacc "value" of an expression is not used,
117 since the result is stored in the structure being created,
118 other node types do have values. */
135 struct symtoken ssym;
138 enum exp_opcode opcode;
139 struct internalvar *ivar;
146 /* YYSTYPE gets defined by %union */
147 static int parse_number (char *, int, int, YYSTYPE *);
150 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
152 %type <tval> type typebase
153 %type <tvec> nonempty_typelist
154 /* %type <bval> block */
156 /* Fancy type parsing. */
157 %type <voidval> func_mod direct_abs_decl abs_decl
159 %type <lval> array_mod
161 %token <typed_val_int> INT
162 %token <typed_val_float> FLOAT
164 /* Both NAME and TYPENAME tokens represent symbols in the input,
165 and both convey their data as strings.
166 But a TYPENAME is a string that happens to be defined as a typedef
167 or builtin type name (such as int or char)
168 and a NAME is any other symbol.
169 Contexts where this distinction is not important can use the
170 nonterminal "name", which matches either NAME or TYPENAME. */
173 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
174 %token <tsym> TYPENAME
176 %type <ssym> name_not_typename
177 %type <tsym> typename
179 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
180 but which would parse as a valid number in the current input radix.
181 E.g. "c" when input_radix==16. Depending on the parse, it will be
182 turned into a name or into a number. */
184 %token <ssym> NAME_OR_INT
186 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
190 /* Special type cases, put in to allow the parser to distinguish different
192 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
194 %token <voidval> VARIABLE
196 %token <opcode> ASSIGN_MODIFY
206 %right '=' ASSIGN_MODIFY
214 %left '<' '>' LEQ GEQ
219 %right UNARY INCREMENT DECREMENT
220 %right ARROW '.' '[' '('
221 %token <ssym> BLOCKNAME
222 %token <bval> FILENAME
234 { write_exp_elt_opcode(OP_TYPE);
235 write_exp_elt_type($1);
236 write_exp_elt_opcode(OP_TYPE);}
239 /* Expressions, including the comma operator. */
242 { write_exp_elt_opcode (BINOP_COMMA); }
245 /* Expressions, not including the comma operator. */
246 exp : '*' exp %prec UNARY
247 { write_exp_elt_opcode (UNOP_IND); }
249 exp : '&' exp %prec UNARY
250 { write_exp_elt_opcode (UNOP_ADDR); }
252 exp : '-' exp %prec UNARY
253 { write_exp_elt_opcode (UNOP_NEG); }
256 exp : '!' exp %prec UNARY
257 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
260 exp : '~' exp %prec UNARY
261 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
264 exp : INCREMENT exp %prec UNARY
265 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
268 exp : DECREMENT exp %prec UNARY
269 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
272 exp : exp INCREMENT %prec UNARY
273 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
276 exp : exp DECREMENT %prec UNARY
277 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
280 exp : SIZEOF exp %prec UNARY
281 { write_exp_elt_opcode (UNOP_SIZEOF); }
285 { write_exp_elt_opcode (STRUCTOP_PTR);
286 write_exp_string ($3);
287 write_exp_elt_opcode (STRUCTOP_PTR); }
290 exp : exp ARROW qualified_name
291 { /* exp->type::name becomes exp->*(&type::name) */
292 /* Note: this doesn't work if name is a
293 static member! FIXME */
294 write_exp_elt_opcode (UNOP_ADDR);
295 write_exp_elt_opcode (STRUCTOP_MPTR); }
298 exp : exp ARROW '*' exp
299 { write_exp_elt_opcode (STRUCTOP_MPTR); }
303 { write_exp_elt_opcode (STRUCTOP_STRUCT);
304 write_exp_string ($3);
305 write_exp_elt_opcode (STRUCTOP_STRUCT); }
308 exp : exp '.' qualified_name
309 { /* exp.type::name becomes exp.*(&type::name) */
310 /* Note: this doesn't work if name is a
311 static member! FIXME */
312 write_exp_elt_opcode (UNOP_ADDR);
313 write_exp_elt_opcode (STRUCTOP_MEMBER); }
316 exp : exp '.' '*' exp
317 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
320 exp : exp '[' exp1 ']'
321 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
325 /* This is to save the value of arglist_len
326 being accumulated by an outer function call. */
327 { start_arglist (); }
328 arglist ')' %prec ARROW
329 { write_exp_elt_opcode (OP_FUNCALL);
330 write_exp_elt_longcst ((LONGEST) end_arglist ());
331 write_exp_elt_opcode (OP_FUNCALL); }
335 { start_arglist (); }
345 arglist : arglist ',' exp %prec ABOVE_COMMA
350 { $$ = end_arglist () - 1; }
352 exp : lcurly arglist rcurly %prec ARROW
353 { write_exp_elt_opcode (OP_ARRAY);
354 write_exp_elt_longcst ((LONGEST) 0);
355 write_exp_elt_longcst ((LONGEST) $3);
356 write_exp_elt_opcode (OP_ARRAY); }
359 exp : lcurly type rcurly exp %prec UNARY
360 { write_exp_elt_opcode (UNOP_MEMVAL);
361 write_exp_elt_type ($2);
362 write_exp_elt_opcode (UNOP_MEMVAL); }
365 exp : '(' type ')' exp %prec UNARY
366 { write_exp_elt_opcode (UNOP_CAST);
367 write_exp_elt_type ($2);
368 write_exp_elt_opcode (UNOP_CAST); }
375 /* Binary operators in order of decreasing precedence. */
378 { write_exp_elt_opcode (BINOP_REPEAT); }
382 { write_exp_elt_opcode (BINOP_MUL); }
386 { write_exp_elt_opcode (BINOP_DIV); }
390 { write_exp_elt_opcode (BINOP_REM); }
394 { write_exp_elt_opcode (BINOP_ADD); }
398 { write_exp_elt_opcode (BINOP_SUB); }
402 { write_exp_elt_opcode (BINOP_LSH); }
406 { write_exp_elt_opcode (BINOP_RSH); }
410 { write_exp_elt_opcode (BINOP_EQUAL); }
413 exp : exp NOTEQUAL exp
414 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
418 { write_exp_elt_opcode (BINOP_LEQ); }
422 { write_exp_elt_opcode (BINOP_GEQ); }
426 { write_exp_elt_opcode (BINOP_LESS); }
430 { write_exp_elt_opcode (BINOP_GTR); }
434 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
438 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
442 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
446 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
450 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
453 exp : exp '?' exp ':' exp %prec '?'
454 { write_exp_elt_opcode (TERNOP_COND); }
458 { write_exp_elt_opcode (BINOP_ASSIGN); }
461 exp : exp ASSIGN_MODIFY exp
462 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
463 write_exp_elt_opcode ($2);
464 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
468 { write_exp_elt_opcode (OP_LONG);
469 write_exp_elt_type ($1.type);
470 write_exp_elt_longcst ((LONGEST)($1.val));
471 write_exp_elt_opcode (OP_LONG); }
476 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
477 write_exp_elt_opcode (OP_LONG);
478 write_exp_elt_type (val.typed_val_int.type);
479 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
480 write_exp_elt_opcode (OP_LONG);
486 { write_exp_elt_opcode (OP_DOUBLE);
487 write_exp_elt_type ($1.type);
488 write_exp_elt_dblcst ($1.dval);
489 write_exp_elt_opcode (OP_DOUBLE); }
496 /* Already written by write_dollar_variable. */
499 exp : SIZEOF '(' type ')' %prec UNARY
500 { write_exp_elt_opcode (OP_LONG);
501 write_exp_elt_type (builtin_type_int);
503 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
504 write_exp_elt_opcode (OP_LONG); }
508 { /* C strings are converted into array constants with
509 an explicit null byte added at the end. Thus
510 the array upper bound is the string length.
511 There is no such thing in C as a completely empty
513 char *sp = $1.ptr; int count = $1.length;
516 write_exp_elt_opcode (OP_LONG);
517 write_exp_elt_type (builtin_type_char);
518 write_exp_elt_longcst ((LONGEST)(*sp++));
519 write_exp_elt_opcode (OP_LONG);
521 write_exp_elt_opcode (OP_LONG);
522 write_exp_elt_type (builtin_type_char);
523 write_exp_elt_longcst ((LONGEST)'\0');
524 write_exp_elt_opcode (OP_LONG);
525 write_exp_elt_opcode (OP_ARRAY);
526 write_exp_elt_longcst ((LONGEST) 0);
527 write_exp_elt_longcst ((LONGEST) ($1.length));
528 write_exp_elt_opcode (OP_ARRAY); }
533 { write_exp_elt_opcode (OP_THIS);
534 write_exp_elt_opcode (OP_THIS); }
538 { write_exp_elt_opcode (OP_LONG);
539 write_exp_elt_type (builtin_type_bool);
540 write_exp_elt_longcst ((LONGEST) 1);
541 write_exp_elt_opcode (OP_LONG); }
545 { write_exp_elt_opcode (OP_LONG);
546 write_exp_elt_type (builtin_type_bool);
547 write_exp_elt_longcst ((LONGEST) 0);
548 write_exp_elt_opcode (OP_LONG); }
556 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
558 error ("No file or function \"%s\".",
559 copy_name ($1.stoken));
567 block : block COLONCOLON name
569 = lookup_symbol (copy_name ($3), $1,
570 VAR_NAMESPACE, (int *) NULL,
571 (struct symtab **) NULL);
572 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
573 error ("No function \"%s\" in specified context.",
575 $$ = SYMBOL_BLOCK_VALUE (tem); }
578 variable: block COLONCOLON name
579 { struct symbol *sym;
580 sym = lookup_symbol (copy_name ($3), $1,
581 VAR_NAMESPACE, (int *) NULL,
582 (struct symtab **) NULL);
584 error ("No symbol \"%s\" in specified context.",
587 write_exp_elt_opcode (OP_VAR_VALUE);
588 /* block_found is set by lookup_symbol. */
589 write_exp_elt_block (block_found);
590 write_exp_elt_sym (sym);
591 write_exp_elt_opcode (OP_VAR_VALUE); }
594 qualified_name: typebase COLONCOLON name
596 struct type *type = $1;
597 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
598 && TYPE_CODE (type) != TYPE_CODE_UNION)
599 error ("`%s' is not defined as an aggregate type.",
602 write_exp_elt_opcode (OP_SCOPE);
603 write_exp_elt_type (type);
604 write_exp_string ($3);
605 write_exp_elt_opcode (OP_SCOPE);
607 | typebase COLONCOLON '~' name
609 struct type *type = $1;
610 struct stoken tmp_token;
611 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
612 && TYPE_CODE (type) != TYPE_CODE_UNION)
613 error ("`%s' is not defined as an aggregate type.",
616 tmp_token.ptr = (char*) alloca ($4.length + 2);
617 tmp_token.length = $4.length + 1;
618 tmp_token.ptr[0] = '~';
619 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
620 tmp_token.ptr[tmp_token.length] = 0;
622 /* Check for valid destructor name. */
623 destructor_name_p (tmp_token.ptr, type);
624 write_exp_elt_opcode (OP_SCOPE);
625 write_exp_elt_type (type);
626 write_exp_string (tmp_token);
627 write_exp_elt_opcode (OP_SCOPE);
631 variable: qualified_name
634 char *name = copy_name ($2);
636 struct minimal_symbol *msymbol;
639 lookup_symbol (name, (const struct block *) NULL,
640 VAR_NAMESPACE, (int *) NULL,
641 (struct symtab **) NULL);
644 write_exp_elt_opcode (OP_VAR_VALUE);
645 write_exp_elt_block (NULL);
646 write_exp_elt_sym (sym);
647 write_exp_elt_opcode (OP_VAR_VALUE);
651 msymbol = lookup_minimal_symbol (name, NULL, NULL);
654 write_exp_msymbol (msymbol,
655 lookup_function_type (builtin_type_int),
659 if (!have_full_symbols () && !have_partial_symbols ())
660 error ("No symbol table is loaded. Use the \"file\" command.");
662 error ("No symbol \"%s\" in current context.", name);
666 variable: name_not_typename
667 { struct symbol *sym = $1.sym;
671 if (symbol_read_needs_frame (sym))
673 if (innermost_block == 0 ||
674 contained_in (block_found,
676 innermost_block = block_found;
679 write_exp_elt_opcode (OP_VAR_VALUE);
680 /* We want to use the selected frame, not
681 another more inner frame which happens to
682 be in the same block. */
683 write_exp_elt_block (NULL);
684 write_exp_elt_sym (sym);
685 write_exp_elt_opcode (OP_VAR_VALUE);
687 else if ($1.is_a_field_of_this)
689 /* C++: it hangs off of `this'. Must
690 not inadvertently convert from a method call
692 if (innermost_block == 0 ||
693 contained_in (block_found, innermost_block))
694 innermost_block = block_found;
695 write_exp_elt_opcode (OP_THIS);
696 write_exp_elt_opcode (OP_THIS);
697 write_exp_elt_opcode (STRUCTOP_PTR);
698 write_exp_string ($1.stoken);
699 write_exp_elt_opcode (STRUCTOP_PTR);
703 struct minimal_symbol *msymbol;
704 register char *arg = copy_name ($1.stoken);
707 lookup_minimal_symbol (arg, NULL, NULL);
710 write_exp_msymbol (msymbol,
711 lookup_function_type (builtin_type_int),
714 else if (!have_full_symbols () && !have_partial_symbols ())
715 error ("No symbol table is loaded. Use the \"file\" command.");
717 error ("No symbol \"%s\" in current context.",
718 copy_name ($1.stoken));
723 space_identifier : '@' NAME
724 { push_type_address_space (copy_name ($2.stoken));
725 push_type (tp_space_identifier);
729 const_or_volatile: const_or_volatile_noopt
733 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
736 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
737 | const_or_volatile_noopt
740 const_or_volatile_or_space_identifier:
741 const_or_volatile_or_space_identifier_noopt
746 { push_type (tp_pointer); $$ = 0; }
748 { push_type (tp_pointer); $$ = $2; }
750 { push_type (tp_reference); $$ = 0; }
752 { push_type (tp_reference); $$ = $2; }
756 direct_abs_decl: '(' abs_decl ')'
758 | direct_abs_decl array_mod
761 push_type (tp_array);
766 push_type (tp_array);
770 | direct_abs_decl func_mod
771 { push_type (tp_function); }
773 { push_type (tp_function); }
784 | '(' nonempty_typelist ')'
785 { free ((PTR)$2); $$ = 0; }
788 /* We used to try to recognize more pointer to member types here, but
789 that didn't work (shift/reduce conflicts meant that these rules never
790 got executed). The problem is that
791 int (foo::bar::baz::bizzle)
792 is a function type but
793 int (foo::bar::baz::bizzle::*)
794 is a pointer to member type. Stroustrup loses again! */
797 | typebase COLONCOLON '*'
798 { $$ = lookup_member_type (builtin_type_int, $1); }
801 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
805 { $$ = builtin_type_int; }
807 { $$ = builtin_type_long; }
809 { $$ = builtin_type_short; }
811 { $$ = builtin_type_long; }
812 | LONG SIGNED_KEYWORD INT_KEYWORD
813 { $$ = builtin_type_long; }
814 | LONG SIGNED_KEYWORD
815 { $$ = builtin_type_long; }
816 | SIGNED_KEYWORD LONG INT_KEYWORD
817 { $$ = builtin_type_long; }
818 | UNSIGNED LONG INT_KEYWORD
819 { $$ = builtin_type_unsigned_long; }
820 | LONG UNSIGNED INT_KEYWORD
821 { $$ = builtin_type_unsigned_long; }
823 { $$ = builtin_type_unsigned_long; }
825 { $$ = builtin_type_long_long; }
826 | LONG LONG INT_KEYWORD
827 { $$ = builtin_type_long_long; }
828 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
829 { $$ = builtin_type_long_long; }
830 | LONG LONG SIGNED_KEYWORD
831 { $$ = builtin_type_long_long; }
832 | SIGNED_KEYWORD LONG LONG
833 { $$ = builtin_type_long_long; }
835 { $$ = builtin_type_unsigned_long_long; }
836 | UNSIGNED LONG LONG INT_KEYWORD
837 { $$ = builtin_type_unsigned_long_long; }
839 { $$ = builtin_type_unsigned_long_long; }
840 | LONG LONG UNSIGNED INT_KEYWORD
841 { $$ = builtin_type_unsigned_long_long; }
842 | SIGNED_KEYWORD LONG LONG
843 { $$ = lookup_signed_typename ("long long"); }
844 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
845 { $$ = lookup_signed_typename ("long long"); }
847 { $$ = builtin_type_short; }
848 | SHORT SIGNED_KEYWORD INT_KEYWORD
849 { $$ = builtin_type_short; }
850 | SHORT SIGNED_KEYWORD
851 { $$ = builtin_type_short; }
852 | UNSIGNED SHORT INT_KEYWORD
853 { $$ = builtin_type_unsigned_short; }
855 { $$ = builtin_type_unsigned_short; }
856 | SHORT UNSIGNED INT_KEYWORD
857 { $$ = builtin_type_unsigned_short; }
859 { $$ = builtin_type_double; }
860 | LONG DOUBLE_KEYWORD
861 { $$ = builtin_type_long_double; }
863 { $$ = lookup_struct (copy_name ($2),
864 expression_context_block); }
866 { $$ = lookup_struct (copy_name ($2),
867 expression_context_block); }
869 { $$ = lookup_union (copy_name ($2),
870 expression_context_block); }
872 { $$ = lookup_enum (copy_name ($2),
873 expression_context_block); }
875 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
877 { $$ = builtin_type_unsigned_int; }
878 | SIGNED_KEYWORD typename
879 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
881 { $$ = builtin_type_int; }
882 /* It appears that this rule for templates is never
883 reduced; template recognition happens by lookahead
884 in the token processing code in yylex. */
885 | TEMPLATE name '<' type '>'
886 { $$ = lookup_template_type(copy_name($2), $4,
887 expression_context_block);
889 | const_or_volatile_or_space_identifier_noopt typebase
890 { $$ = follow_types ($2); }
891 | typebase const_or_volatile_or_space_identifier_noopt
892 { $$ = follow_types ($1); }
898 $$.stoken.ptr = "int";
899 $$.stoken.length = 3;
900 $$.type = builtin_type_int;
904 $$.stoken.ptr = "long";
905 $$.stoken.length = 4;
906 $$.type = builtin_type_long;
910 $$.stoken.ptr = "short";
911 $$.stoken.length = 5;
912 $$.type = builtin_type_short;
918 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
919 $<ivec>$[0] = 1; /* Number of types in vector */
922 | nonempty_typelist ',' type
923 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
924 $$ = (struct type **) realloc ((char *) $1, len);
925 $$[$<ivec>$[0]] = $3;
930 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
931 { $$ = follow_types ($1); }
934 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
935 | VOLATILE_KEYWORD CONST_KEYWORD
938 const_or_volatile_noopt: const_and_volatile
939 { push_type (tp_const);
940 push_type (tp_volatile);
943 { push_type (tp_const); }
945 { push_type (tp_volatile); }
948 name : NAME { $$ = $1.stoken; }
949 | BLOCKNAME { $$ = $1.stoken; }
950 | TYPENAME { $$ = $1.stoken; }
951 | NAME_OR_INT { $$ = $1.stoken; }
954 name_not_typename : NAME
956 /* These would be useful if name_not_typename was useful, but it is just
957 a fake for "variable", so these cause reduce/reduce conflicts because
958 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
959 =exp) or just an exp. If name_not_typename was ever used in an lvalue
960 context where only a name could occur, this might be useful.
967 /* Take care of parsing a number (anything that starts with a digit).
968 Set yylval and return the token type; update lexptr.
969 LEN is the number of characters in it. */
971 /*** Needs some error checking for the float case ***/
974 parse_number (p, len, parsed_float, putithere)
980 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
981 here, and we do kind of silly things like cast to unsigned. */
982 register LONGEST n = 0;
983 register LONGEST prevn = 0;
988 register int base = input_radix;
991 /* Number of "L" suffixes encountered. */
994 /* We have found a "L" or "U" suffix. */
995 int found_suffix = 0;
998 struct type *signed_type;
999 struct type *unsigned_type;
1003 /* It's a float since it contains a point or an exponent. */
1005 int num = 0; /* number of tokens scanned by scanf */
1006 char saved_char = p[len];
1008 p[len] = 0; /* null-terminate the token */
1009 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
1010 num = sscanf (p, "%g%c", (float *) &putithere->typed_val_float.dval,&c);
1011 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
1012 num = sscanf (p, "%lg%c", (double *) &putithere->typed_val_float.dval,&c);
1015 #ifdef SCANF_HAS_LONG_DOUBLE
1016 num = sscanf (p, "%Lg%c", &putithere->typed_val_float.dval,&c);
1018 /* Scan it into a double, then assign it to the long double.
1019 This at least wins with values representable in the range
1022 num = sscanf (p, "%lg%c", &temp,&c);
1023 putithere->typed_val_float.dval = temp;
1026 p[len] = saved_char; /* restore the input stream */
1027 if (num != 1) /* check scanf found ONLY a float ... */
1029 /* See if it has `f' or `l' suffix (float or long double). */
1031 c = tolower (p[len - 1]);
1034 putithere->typed_val_float.type = builtin_type_float;
1036 putithere->typed_val_float.type = builtin_type_long_double;
1037 else if (isdigit (c) || c == '.')
1038 putithere->typed_val_float.type = builtin_type_double;
1045 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1079 if (c >= 'A' && c <= 'Z')
1081 if (c != 'l' && c != 'u')
1083 if (c >= '0' && c <= '9')
1091 if (base > 10 && c >= 'a' && c <= 'f')
1095 n += i = c - 'a' + 10;
1108 return ERROR; /* Char not a digit */
1111 return ERROR; /* Invalid digit in this base */
1113 /* Portably test for overflow (only works for nonzero values, so make
1114 a second check for zero). FIXME: Can't we just make n and prevn
1115 unsigned and avoid this? */
1116 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1117 unsigned_p = 1; /* Try something unsigned */
1119 /* Portably test for unsigned overflow.
1120 FIXME: This check is wrong; for example it doesn't find overflow
1121 on 0x123456789 when LONGEST is 32 bits. */
1122 if (c != 'l' && c != 'u' && n != 0)
1124 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1125 error ("Numeric constant too large.");
1130 /* An integer constant is an int, a long, or a long long. An L
1131 suffix forces it to be long; an LL suffix forces it to be long
1132 long. If not forced to a larger size, it gets the first type of
1133 the above that it fits in. To figure out whether it fits, we
1134 shift it right and see whether anything remains. Note that we
1135 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1136 operation, because many compilers will warn about such a shift
1137 (which always produces a zero result). Sometimes TARGET_INT_BIT
1138 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
1139 the case where it is we just always shift the value more than
1140 once, with fewer bits each time. */
1142 un = (ULONGEST)n >> 2;
1144 && (un >> (TARGET_INT_BIT - 2)) == 0)
1146 high_bit = ((ULONGEST)1) << (TARGET_INT_BIT-1);
1148 /* A large decimal (not hex or octal) constant (between INT_MAX
1149 and UINT_MAX) is a long or unsigned long, according to ANSI,
1150 never an unsigned int, but this code treats it as unsigned
1151 int. This probably should be fixed. GCC gives a warning on
1154 unsigned_type = builtin_type_unsigned_int;
1155 signed_type = builtin_type_int;
1157 else if (long_p <= 1
1158 && (un >> (TARGET_LONG_BIT - 2)) == 0)
1160 high_bit = ((ULONGEST)1) << (TARGET_LONG_BIT-1);
1161 unsigned_type = builtin_type_unsigned_long;
1162 signed_type = builtin_type_long;
1167 if (sizeof (ULONGEST) * HOST_CHAR_BIT < TARGET_LONG_LONG_BIT)
1168 /* A long long does not fit in a LONGEST. */
1169 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1171 shift = (TARGET_LONG_LONG_BIT - 1);
1172 high_bit = (ULONGEST) 1 << shift;
1173 unsigned_type = builtin_type_unsigned_long_long;
1174 signed_type = builtin_type_long_long;
1177 putithere->typed_val_int.val = n;
1179 /* If the high bit of the worked out type is set then this number
1180 has to be unsigned. */
1182 if (unsigned_p || (n & high_bit))
1184 putithere->typed_val_int.type = unsigned_type;
1188 putithere->typed_val_int.type = signed_type;
1198 enum exp_opcode opcode;
1201 static const struct token tokentab3[] =
1203 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1204 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1207 static const struct token tokentab2[] =
1209 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1210 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1211 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1212 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1213 {"%=", ASSIGN_MODIFY, BINOP_REM},
1214 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1215 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1216 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1217 {"++", INCREMENT, BINOP_END},
1218 {"--", DECREMENT, BINOP_END},
1219 {"->", ARROW, BINOP_END},
1220 {"&&", ANDAND, BINOP_END},
1221 {"||", OROR, BINOP_END},
1222 {"::", COLONCOLON, BINOP_END},
1223 {"<<", LSH, BINOP_END},
1224 {">>", RSH, BINOP_END},
1225 {"==", EQUAL, BINOP_END},
1226 {"!=", NOTEQUAL, BINOP_END},
1227 {"<=", LEQ, BINOP_END},
1228 {">=", GEQ, BINOP_END}
1231 /* Read one token, getting characters through lexptr. */
1242 static char *tempbuf;
1243 static int tempbufsize;
1244 struct symbol * sym_class = NULL;
1245 char * token_string = NULL;
1246 int class_prefix = 0;
1251 /* Check if this is a macro invocation that we need to expand. */
1252 if (! scanning_macro_expansion ())
1254 char *expanded = macro_expand_next (&lexptr,
1255 expression_macro_lookup_func,
1256 expression_macro_lookup_baton);
1259 scan_macro_expansion (expanded);
1262 prev_lexptr = lexptr;
1266 /* See if it is a special token of length 3. */
1267 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1268 if (STREQN (tokstart, tokentab3[i].operator, 3))
1271 yylval.opcode = tokentab3[i].opcode;
1272 return tokentab3[i].token;
1275 /* See if it is a special token of length 2. */
1276 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1277 if (STREQN (tokstart, tokentab2[i].operator, 2))
1280 yylval.opcode = tokentab2[i].opcode;
1281 return tokentab2[i].token;
1284 switch (c = *tokstart)
1287 /* If we were just scanning the result of a macro expansion,
1288 then we need to resume scanning the original text.
1289 Otherwise, we were already scanning the original text, and
1290 we're really done. */
1291 if (scanning_macro_expansion ())
1293 finished_macro_expansion ();
1306 /* We either have a character constant ('0' or '\177' for example)
1307 or we have a quoted symbol reference ('foo(int,int)' in C++
1312 c = parse_escape (&lexptr);
1314 error ("Empty character constant.");
1316 yylval.typed_val_int.val = c;
1317 yylval.typed_val_int.type = builtin_type_char;
1322 namelen = skip_quoted (tokstart) - tokstart;
1325 lexptr = tokstart + namelen;
1327 if (lexptr[-1] != '\'')
1328 error ("Unmatched single quote.");
1333 error ("Invalid character constant.");
1343 if (paren_depth == 0)
1350 if (comma_terminates
1352 && ! scanning_macro_expansion ())
1358 /* Might be a floating point number. */
1359 if (lexptr[1] < '0' || lexptr[1] > '9')
1360 goto symbol; /* Nope, must be a symbol. */
1361 /* FALL THRU into number case. */
1374 /* It's a number. */
1375 int got_dot = 0, got_e = 0, toktype;
1376 register char *p = tokstart;
1377 int hex = input_radix > 10;
1379 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1384 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1392 /* This test includes !hex because 'e' is a valid hex digit
1393 and thus does not indicate a floating point number when
1394 the radix is hex. */
1395 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1396 got_dot = got_e = 1;
1397 /* This test does not include !hex, because a '.' always indicates
1398 a decimal floating point number regardless of the radix. */
1399 else if (!got_dot && *p == '.')
1401 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1402 && (*p == '-' || *p == '+'))
1403 /* This is the sign of the exponent, not the end of the
1406 /* We will take any letters or digits. parse_number will
1407 complain if past the radix, or if L or U are not final. */
1408 else if ((*p < '0' || *p > '9')
1409 && ((*p < 'a' || *p > 'z')
1410 && (*p < 'A' || *p > 'Z')))
1413 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1414 if (toktype == ERROR)
1416 char *err_copy = (char *) alloca (p - tokstart + 1);
1418 memcpy (err_copy, tokstart, p - tokstart);
1419 err_copy[p - tokstart] = 0;
1420 error ("Invalid number \"%s\".", err_copy);
1452 /* Build the gdb internal form of the input string in tempbuf,
1453 translating any standard C escape forms seen. Note that the
1454 buffer is null byte terminated *only* for the convenience of
1455 debugging gdb itself and printing the buffer contents when
1456 the buffer contains no embedded nulls. Gdb does not depend
1457 upon the buffer being null byte terminated, it uses the length
1458 string instead. This allows gdb to handle C strings (as well
1459 as strings in other languages) with embedded null bytes */
1461 tokptr = ++tokstart;
1465 /* Grow the static temp buffer if necessary, including allocating
1466 the first one on demand. */
1467 if (tempbufindex + 1 >= tempbufsize)
1469 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1475 /* Do nothing, loop will terminate. */
1479 c = parse_escape (&tokptr);
1484 tempbuf[tempbufindex++] = c;
1487 tempbuf[tempbufindex++] = *tokptr++;
1490 } while ((*tokptr != '"') && (*tokptr != '\0'));
1491 if (*tokptr++ != '"')
1493 error ("Unterminated string in expression.");
1495 tempbuf[tempbufindex] = '\0'; /* See note above */
1496 yylval.sval.ptr = tempbuf;
1497 yylval.sval.length = tempbufindex;
1502 if (!(c == '_' || c == '$'
1503 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1504 /* We must have come across a bad character (e.g. ';'). */
1505 error ("Invalid character '%c' in expression.", c);
1507 /* It's a name. See how long it is. */
1509 for (c = tokstart[namelen];
1510 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1511 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1513 /* Template parameter lists are part of the name.
1514 FIXME: This mishandles `print $a<4&&$a>3'. */
1518 /* Scan ahead to get rest of the template specification. Note
1519 that we look ahead only when the '<' adjoins non-whitespace
1520 characters; for comparison expressions, e.g. "a < b > c",
1521 there must be spaces before the '<', etc. */
1523 char * p = find_template_name_end (tokstart + namelen);
1525 namelen = p - tokstart;
1528 c = tokstart[++namelen];
1531 /* The token "if" terminates the expression and is NOT removed from
1532 the input stream. It doesn't count if it appears in the
1533 expansion of a macro. */
1535 && tokstart[0] == 'i'
1536 && tokstart[1] == 'f'
1537 && ! scanning_macro_expansion ())
1546 /* Catch specific keywords. Should be done with a data structure. */
1550 if (STREQN (tokstart, "unsigned", 8))
1552 if (current_language->la_language == language_cplus
1553 && STREQN (tokstart, "template", 8))
1555 if (STREQN (tokstart, "volatile", 8))
1556 return VOLATILE_KEYWORD;
1559 if (STREQN (tokstart, "struct", 6))
1561 if (STREQN (tokstart, "signed", 6))
1562 return SIGNED_KEYWORD;
1563 if (STREQN (tokstart, "sizeof", 6))
1565 if (STREQN (tokstart, "double", 6))
1566 return DOUBLE_KEYWORD;
1569 if (current_language->la_language == language_cplus)
1571 if (STREQN (tokstart, "false", 5))
1572 return FALSEKEYWORD;
1573 if (STREQN (tokstart, "class", 5))
1576 if (STREQN (tokstart, "union", 5))
1578 if (STREQN (tokstart, "short", 5))
1580 if (STREQN (tokstart, "const", 5))
1581 return CONST_KEYWORD;
1584 if (STREQN (tokstart, "enum", 4))
1586 if (STREQN (tokstart, "long", 4))
1588 if (current_language->la_language == language_cplus)
1590 if (STREQN (tokstart, "true", 4))
1593 if (STREQN (tokstart, "this", 4))
1595 static const char this_name[] =
1596 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1598 if (lookup_symbol (this_name, expression_context_block,
1599 VAR_NAMESPACE, (int *) NULL,
1600 (struct symtab **) NULL))
1606 if (STREQN (tokstart, "int", 3))
1613 yylval.sval.ptr = tokstart;
1614 yylval.sval.length = namelen;
1616 if (*tokstart == '$')
1618 write_dollar_variable (yylval.sval);
1622 /* Look ahead and see if we can consume more of the input
1623 string to get a reasonable class/namespace spec or a
1624 fully-qualified name. This is a kludge to get around the
1625 HP aCC compiler's generation of symbol names with embedded
1626 colons for namespace and nested classes. */
1629 /* Only do it if not inside single quotes */
1630 sym_class = parse_nested_classes_for_hpacc (yylval.sval.ptr, yylval.sval.length,
1631 &token_string, &class_prefix, &lexptr);
1634 /* Replace the current token with the bigger one we found */
1635 yylval.sval.ptr = token_string;
1636 yylval.sval.length = strlen (token_string);
1640 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1641 functions or symtabs. If this is not so, then ...
1642 Use token-type TYPENAME for symbols that happen to be defined
1643 currently as names of types; NAME for other symbols.
1644 The caller is not constrained to care about the distinction. */
1646 char *tmp = copy_name (yylval.sval);
1648 int is_a_field_of_this = 0;
1651 sym = lookup_symbol (tmp, expression_context_block,
1653 current_language->la_language == language_cplus
1654 ? &is_a_field_of_this : (int *) NULL,
1655 (struct symtab **) NULL);
1656 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1657 no psymtabs (coff, xcoff, or some future change to blow away the
1658 psymtabs once once symbols are read). */
1659 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1661 yylval.ssym.sym = sym;
1662 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1666 { /* See if it's a file name. */
1667 struct symtab *symtab;
1669 symtab = lookup_symtab (tmp);
1673 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1678 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1681 /* Despite the following flaw, we need to keep this code enabled.
1682 Because we can get called from check_stub_method, if we don't
1683 handle nested types then it screws many operations in any
1684 program which uses nested types. */
1685 /* In "A::x", if x is a member function of A and there happens
1686 to be a type (nested or not, since the stabs don't make that
1687 distinction) named x, then this code incorrectly thinks we
1688 are dealing with nested types rather than a member function. */
1692 struct symbol *best_sym;
1694 /* Look ahead to detect nested types. This probably should be
1695 done in the grammar, but trying seemed to introduce a lot
1696 of shift/reduce and reduce/reduce conflicts. It's possible
1697 that it could be done, though. Or perhaps a non-grammar, but
1698 less ad hoc, approach would work well. */
1700 /* Since we do not currently have any way of distinguishing
1701 a nested type from a non-nested one (the stabs don't tell
1702 us whether a type is nested), we just ignore the
1709 /* Skip whitespace. */
1710 while (*p == ' ' || *p == '\t' || *p == '\n')
1712 if (*p == ':' && p[1] == ':')
1714 /* Skip the `::'. */
1716 /* Skip whitespace. */
1717 while (*p == ' ' || *p == '\t' || *p == '\n')
1720 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1721 || (*p >= 'a' && *p <= 'z')
1722 || (*p >= 'A' && *p <= 'Z'))
1726 struct symbol *cur_sym;
1727 /* As big as the whole rest of the expression, which is
1728 at least big enough. */
1729 char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1733 memcpy (tmp1, tmp, strlen (tmp));
1734 tmp1 += strlen (tmp);
1735 memcpy (tmp1, "::", 2);
1737 memcpy (tmp1, namestart, p - namestart);
1738 tmp1[p - namestart] = '\0';
1739 cur_sym = lookup_symbol (ncopy, expression_context_block,
1740 VAR_NAMESPACE, (int *) NULL,
1741 (struct symtab **) NULL);
1744 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1762 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1764 yylval.tsym.type = SYMBOL_TYPE (sym);
1768 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1771 /* Input names that aren't symbols but ARE valid hex numbers,
1772 when the input radix permits them, can be names or numbers
1773 depending on the parse. Note we support radixes > 16 here. */
1775 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1776 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1778 YYSTYPE newlval; /* Its value is ignored. */
1779 hextype = parse_number (tokstart, namelen, 0, &newlval);
1782 yylval.ssym.sym = sym;
1783 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1788 /* Any other kind of symbol */
1789 yylval.ssym.sym = sym;
1790 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1800 lexptr = prev_lexptr;
1802 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);