1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2003, 2004, 2006, 2007 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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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 */
54 #include "cp-support.h"
57 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
58 as well as gratuitiously global symbol names, so we can have multiple
59 yacc generated parsers in gdb. Note that these are only the variables
60 produced by yacc. If other parser generators (bison, byacc, etc) produce
61 additional global names that conflict at link time, then those parser
62 generators need to be fixed instead of adding those names to this list. */
64 #define yymaxdepth c_maxdepth
65 #define yyparse c_parse
67 #define yyerror c_error
70 #define yydebug c_debug
79 #define yyerrflag c_errflag
80 #define yynerrs c_nerrs
85 #define yystate c_state
91 #define yyreds c_reds /* With YYDEBUG defined */
92 #define yytoks c_toks /* With YYDEBUG defined */
93 #define yyname c_name /* With YYDEBUG defined */
94 #define yyrule c_rule /* With YYDEBUG defined */
97 #define yydefred c_yydefred
98 #define yydgoto c_yydgoto
99 #define yysindex c_yysindex
100 #define yyrindex c_yyrindex
101 #define yygindex c_yygindex
102 #define yytable c_yytable
103 #define yycheck c_yycheck
106 #define YYDEBUG 1 /* Default to yydebug support */
109 #define YYFPRINTF parser_fprintf
113 static int yylex (void);
115 void yyerror (char *);
119 /* Although the yacc "value" of an expression is not used,
120 since the result is stored in the structure being created,
121 other node types do have values. */
137 } typed_val_decfloat;
142 struct symtoken ssym;
145 enum exp_opcode opcode;
146 struct internalvar *ivar;
153 /* YYSTYPE gets defined by %union */
154 static int parse_number (char *, int, int, YYSTYPE *);
157 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
159 %type <tval> type typebase qualified_type
160 %type <tvec> nonempty_typelist
161 /* %type <bval> block */
163 /* Fancy type parsing. */
164 %type <voidval> func_mod direct_abs_decl abs_decl
166 %type <lval> array_mod
168 %token <typed_val_int> INT
169 %token <typed_val_float> FLOAT
170 %token <typed_val_decfloat> DECFLOAT
172 /* Both NAME and TYPENAME tokens represent symbols in the input,
173 and both convey their data as strings.
174 But a TYPENAME is a string that happens to be defined as a typedef
175 or builtin type name (such as int or char)
176 and a NAME is any other symbol.
177 Contexts where this distinction is not important can use the
178 nonterminal "name", which matches either NAME or TYPENAME. */
181 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
182 %token <tsym> TYPENAME
184 %type <ssym> name_not_typename
185 %type <tsym> typename
187 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
188 but which would parse as a valid number in the current input radix.
189 E.g. "c" when input_radix==16. Depending on the parse, it will be
190 turned into a name or into a number. */
192 %token <ssym> NAME_OR_INT
194 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
198 /* Special type cases, put in to allow the parser to distinguish different
200 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
202 %token <voidval> VARIABLE
204 %token <opcode> ASSIGN_MODIFY
213 %right '=' ASSIGN_MODIFY
221 %left '<' '>' LEQ GEQ
226 %right UNARY INCREMENT DECREMENT
227 %right ARROW '.' '[' '('
228 %token <ssym> BLOCKNAME
229 %token <bval> FILENAME
241 { write_exp_elt_opcode(OP_TYPE);
242 write_exp_elt_type($1);
243 write_exp_elt_opcode(OP_TYPE);}
246 /* Expressions, including the comma operator. */
249 { write_exp_elt_opcode (BINOP_COMMA); }
252 /* Expressions, not including the comma operator. */
253 exp : '*' exp %prec UNARY
254 { write_exp_elt_opcode (UNOP_IND); }
257 exp : '&' exp %prec UNARY
258 { write_exp_elt_opcode (UNOP_ADDR); }
261 exp : '-' exp %prec UNARY
262 { write_exp_elt_opcode (UNOP_NEG); }
265 exp : '+' exp %prec UNARY
266 { write_exp_elt_opcode (UNOP_PLUS); }
269 exp : '!' exp %prec UNARY
270 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
273 exp : '~' exp %prec UNARY
274 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
277 exp : INCREMENT exp %prec UNARY
278 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
281 exp : DECREMENT exp %prec UNARY
282 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
285 exp : exp INCREMENT %prec UNARY
286 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
289 exp : exp DECREMENT %prec UNARY
290 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
293 exp : SIZEOF exp %prec UNARY
294 { write_exp_elt_opcode (UNOP_SIZEOF); }
298 { write_exp_elt_opcode (STRUCTOP_PTR);
299 write_exp_string ($3);
300 write_exp_elt_opcode (STRUCTOP_PTR); }
303 exp : exp ARROW qualified_name
304 { /* exp->type::name becomes exp->*(&type::name) */
305 /* Note: this doesn't work if name is a
306 static member! FIXME */
307 write_exp_elt_opcode (UNOP_ADDR);
308 write_exp_elt_opcode (STRUCTOP_MPTR); }
311 exp : exp ARROW '*' exp
312 { write_exp_elt_opcode (STRUCTOP_MPTR); }
316 { write_exp_elt_opcode (STRUCTOP_STRUCT);
317 write_exp_string ($3);
318 write_exp_elt_opcode (STRUCTOP_STRUCT); }
321 exp : exp '.' qualified_name
322 { /* exp.type::name becomes exp.*(&type::name) */
323 /* Note: this doesn't work if name is a
324 static member! FIXME */
325 write_exp_elt_opcode (UNOP_ADDR);
326 write_exp_elt_opcode (STRUCTOP_MEMBER); }
329 exp : exp '.' '*' exp
330 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
333 exp : exp '[' exp1 ']'
334 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
338 /* This is to save the value of arglist_len
339 being accumulated by an outer function call. */
340 { start_arglist (); }
341 arglist ')' %prec ARROW
342 { write_exp_elt_opcode (OP_FUNCALL);
343 write_exp_elt_longcst ((LONGEST) end_arglist ());
344 write_exp_elt_opcode (OP_FUNCALL); }
348 { start_arglist (); }
358 arglist : arglist ',' exp %prec ABOVE_COMMA
363 { $$ = end_arglist () - 1; }
365 exp : lcurly arglist rcurly %prec ARROW
366 { write_exp_elt_opcode (OP_ARRAY);
367 write_exp_elt_longcst ((LONGEST) 0);
368 write_exp_elt_longcst ((LONGEST) $3);
369 write_exp_elt_opcode (OP_ARRAY); }
372 exp : lcurly type rcurly exp %prec UNARY
373 { write_exp_elt_opcode (UNOP_MEMVAL);
374 write_exp_elt_type ($2);
375 write_exp_elt_opcode (UNOP_MEMVAL); }
378 exp : '(' type ')' exp %prec UNARY
379 { write_exp_elt_opcode (UNOP_CAST);
380 write_exp_elt_type ($2);
381 write_exp_elt_opcode (UNOP_CAST); }
388 /* Binary operators in order of decreasing precedence. */
391 { write_exp_elt_opcode (BINOP_REPEAT); }
395 { write_exp_elt_opcode (BINOP_MUL); }
399 { write_exp_elt_opcode (BINOP_DIV); }
403 { write_exp_elt_opcode (BINOP_REM); }
407 { write_exp_elt_opcode (BINOP_ADD); }
411 { write_exp_elt_opcode (BINOP_SUB); }
415 { write_exp_elt_opcode (BINOP_LSH); }
419 { write_exp_elt_opcode (BINOP_RSH); }
423 { write_exp_elt_opcode (BINOP_EQUAL); }
426 exp : exp NOTEQUAL exp
427 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
431 { write_exp_elt_opcode (BINOP_LEQ); }
435 { write_exp_elt_opcode (BINOP_GEQ); }
439 { write_exp_elt_opcode (BINOP_LESS); }
443 { write_exp_elt_opcode (BINOP_GTR); }
447 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
451 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
455 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
459 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
463 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
466 exp : exp '?' exp ':' exp %prec '?'
467 { write_exp_elt_opcode (TERNOP_COND); }
471 { write_exp_elt_opcode (BINOP_ASSIGN); }
474 exp : exp ASSIGN_MODIFY exp
475 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
476 write_exp_elt_opcode ($2);
477 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
481 { write_exp_elt_opcode (OP_LONG);
482 write_exp_elt_type ($1.type);
483 write_exp_elt_longcst ((LONGEST)($1.val));
484 write_exp_elt_opcode (OP_LONG); }
489 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
490 write_exp_elt_opcode (OP_LONG);
491 write_exp_elt_type (val.typed_val_int.type);
492 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
493 write_exp_elt_opcode (OP_LONG);
499 { write_exp_elt_opcode (OP_DOUBLE);
500 write_exp_elt_type ($1.type);
501 write_exp_elt_dblcst ($1.dval);
502 write_exp_elt_opcode (OP_DOUBLE); }
506 { write_exp_elt_opcode (OP_DECFLOAT);
507 write_exp_elt_type ($1.type);
508 write_exp_elt_decfloatcst ($1.val);
509 write_exp_elt_opcode (OP_DECFLOAT); }
516 /* Already written by write_dollar_variable. */
519 exp : SIZEOF '(' type ')' %prec UNARY
520 { write_exp_elt_opcode (OP_LONG);
521 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_int);
523 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
524 write_exp_elt_opcode (OP_LONG); }
528 { /* C strings are converted into array constants with
529 an explicit null byte added at the end. Thus
530 the array upper bound is the string length.
531 There is no such thing in C as a completely empty
533 char *sp = $1.ptr; int count = $1.length;
536 write_exp_elt_opcode (OP_LONG);
537 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_char);
538 write_exp_elt_longcst ((LONGEST)(*sp++));
539 write_exp_elt_opcode (OP_LONG);
541 write_exp_elt_opcode (OP_LONG);
542 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_char);
543 write_exp_elt_longcst ((LONGEST)'\0');
544 write_exp_elt_opcode (OP_LONG);
545 write_exp_elt_opcode (OP_ARRAY);
546 write_exp_elt_longcst ((LONGEST) 0);
547 write_exp_elt_longcst ((LONGEST) ($1.length));
548 write_exp_elt_opcode (OP_ARRAY); }
553 { write_exp_elt_opcode (OP_LONG);
554 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_bool);
555 write_exp_elt_longcst ((LONGEST) 1);
556 write_exp_elt_opcode (OP_LONG); }
560 { write_exp_elt_opcode (OP_LONG);
561 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_bool);
562 write_exp_elt_longcst ((LONGEST) 0);
563 write_exp_elt_opcode (OP_LONG); }
571 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
573 error ("No file or function \"%s\".",
574 copy_name ($1.stoken));
582 block : block COLONCOLON name
584 = lookup_symbol (copy_name ($3), $1,
585 VAR_DOMAIN, (int *) NULL,
586 (struct symtab **) NULL);
587 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
588 error ("No function \"%s\" in specified context.",
590 $$ = SYMBOL_BLOCK_VALUE (tem); }
593 variable: block COLONCOLON name
594 { struct symbol *sym;
595 sym = lookup_symbol (copy_name ($3), $1,
596 VAR_DOMAIN, (int *) NULL,
597 (struct symtab **) NULL);
599 error ("No symbol \"%s\" in specified context.",
602 write_exp_elt_opcode (OP_VAR_VALUE);
603 /* block_found is set by lookup_symbol. */
604 write_exp_elt_block (block_found);
605 write_exp_elt_sym (sym);
606 write_exp_elt_opcode (OP_VAR_VALUE); }
609 qualified_name: typebase COLONCOLON name
611 struct type *type = $1;
612 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
613 && TYPE_CODE (type) != TYPE_CODE_UNION
614 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
615 error ("`%s' is not defined as an aggregate type.",
618 write_exp_elt_opcode (OP_SCOPE);
619 write_exp_elt_type (type);
620 write_exp_string ($3);
621 write_exp_elt_opcode (OP_SCOPE);
623 | typebase COLONCOLON '~' name
625 struct type *type = $1;
626 struct stoken tmp_token;
627 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
628 && TYPE_CODE (type) != TYPE_CODE_UNION
629 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
630 error ("`%s' is not defined as an aggregate type.",
633 tmp_token.ptr = (char*) alloca ($4.length + 2);
634 tmp_token.length = $4.length + 1;
635 tmp_token.ptr[0] = '~';
636 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
637 tmp_token.ptr[tmp_token.length] = 0;
639 /* Check for valid destructor name. */
640 destructor_name_p (tmp_token.ptr, type);
641 write_exp_elt_opcode (OP_SCOPE);
642 write_exp_elt_type (type);
643 write_exp_string (tmp_token);
644 write_exp_elt_opcode (OP_SCOPE);
648 variable: qualified_name
651 char *name = copy_name ($2);
653 struct minimal_symbol *msymbol;
656 lookup_symbol (name, (const struct block *) NULL,
657 VAR_DOMAIN, (int *) NULL,
658 (struct symtab **) NULL);
661 write_exp_elt_opcode (OP_VAR_VALUE);
662 write_exp_elt_block (NULL);
663 write_exp_elt_sym (sym);
664 write_exp_elt_opcode (OP_VAR_VALUE);
668 msymbol = lookup_minimal_symbol (name, NULL, NULL);
671 write_exp_msymbol (msymbol,
672 lookup_function_type (builtin_type (current_gdbarch)->builtin_int),
673 builtin_type (current_gdbarch)->builtin_int);
676 if (!have_full_symbols () && !have_partial_symbols ())
677 error ("No symbol table is loaded. Use the \"file\" command.");
679 error ("No symbol \"%s\" in current context.", name);
683 variable: name_not_typename
684 { struct symbol *sym = $1.sym;
688 if (symbol_read_needs_frame (sym))
690 if (innermost_block == 0 ||
691 contained_in (block_found,
693 innermost_block = block_found;
696 write_exp_elt_opcode (OP_VAR_VALUE);
697 /* We want to use the selected frame, not
698 another more inner frame which happens to
699 be in the same block. */
700 write_exp_elt_block (NULL);
701 write_exp_elt_sym (sym);
702 write_exp_elt_opcode (OP_VAR_VALUE);
704 else if ($1.is_a_field_of_this)
706 /* C++: it hangs off of `this'. Must
707 not inadvertently convert from a method call
709 if (innermost_block == 0 ||
710 contained_in (block_found, innermost_block))
711 innermost_block = block_found;
712 write_exp_elt_opcode (OP_THIS);
713 write_exp_elt_opcode (OP_THIS);
714 write_exp_elt_opcode (STRUCTOP_PTR);
715 write_exp_string ($1.stoken);
716 write_exp_elt_opcode (STRUCTOP_PTR);
720 struct minimal_symbol *msymbol;
721 char *arg = copy_name ($1.stoken);
724 lookup_minimal_symbol (arg, NULL, NULL);
727 write_exp_msymbol (msymbol,
728 lookup_function_type (builtin_type (current_gdbarch)->builtin_int),
729 builtin_type (current_gdbarch)->builtin_int);
731 else if (!have_full_symbols () && !have_partial_symbols ())
732 error ("No symbol table is loaded. Use the \"file\" command.");
734 error ("No symbol \"%s\" in current context.",
735 copy_name ($1.stoken));
740 space_identifier : '@' NAME
741 { push_type_address_space (copy_name ($2.stoken));
742 push_type (tp_space_identifier);
746 const_or_volatile: const_or_volatile_noopt
750 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
753 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
754 | const_or_volatile_noopt
757 const_or_volatile_or_space_identifier:
758 const_or_volatile_or_space_identifier_noopt
763 { push_type (tp_pointer); $$ = 0; }
765 { push_type (tp_pointer); $$ = $2; }
767 { push_type (tp_reference); $$ = 0; }
769 { push_type (tp_reference); $$ = $2; }
773 direct_abs_decl: '(' abs_decl ')'
775 | direct_abs_decl array_mod
778 push_type (tp_array);
783 push_type (tp_array);
787 | direct_abs_decl func_mod
788 { push_type (tp_function); }
790 { push_type (tp_function); }
801 | '(' nonempty_typelist ')'
802 { free ($2); $$ = 0; }
805 /* We used to try to recognize pointer to member types here, but
806 that didn't work (shift/reduce conflicts meant that these rules never
807 got executed). The problem is that
808 int (foo::bar::baz::bizzle)
809 is a function type but
810 int (foo::bar::baz::bizzle::*)
811 is a pointer to member type. Stroustrup loses again! */
816 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
820 { $$ = builtin_type (current_gdbarch)->builtin_int; }
822 { $$ = builtin_type (current_gdbarch)->builtin_long; }
824 { $$ = builtin_type (current_gdbarch)->builtin_short; }
826 { $$ = builtin_type (current_gdbarch)->builtin_long; }
827 | LONG SIGNED_KEYWORD INT_KEYWORD
828 { $$ = builtin_type (current_gdbarch)->builtin_long; }
829 | LONG SIGNED_KEYWORD
830 { $$ = builtin_type (current_gdbarch)->builtin_long; }
831 | SIGNED_KEYWORD LONG INT_KEYWORD
832 { $$ = builtin_type (current_gdbarch)->builtin_long; }
833 | UNSIGNED LONG INT_KEYWORD
834 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
835 | LONG UNSIGNED INT_KEYWORD
836 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
838 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
840 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
841 | LONG LONG INT_KEYWORD
842 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
843 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
844 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
845 | LONG LONG SIGNED_KEYWORD
846 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
847 | SIGNED_KEYWORD LONG LONG
848 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
849 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
850 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
852 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
853 | UNSIGNED LONG LONG INT_KEYWORD
854 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
856 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
857 | LONG LONG UNSIGNED INT_KEYWORD
858 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
860 { $$ = builtin_type (current_gdbarch)->builtin_short; }
861 | SHORT SIGNED_KEYWORD INT_KEYWORD
862 { $$ = builtin_type (current_gdbarch)->builtin_short; }
863 | SHORT SIGNED_KEYWORD
864 { $$ = builtin_type (current_gdbarch)->builtin_short; }
865 | UNSIGNED SHORT INT_KEYWORD
866 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
868 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
869 | SHORT UNSIGNED INT_KEYWORD
870 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
872 { $$ = builtin_type (current_gdbarch)->builtin_double; }
873 | LONG DOUBLE_KEYWORD
874 { $$ = builtin_type (current_gdbarch)->builtin_long_double; }
876 { $$ = lookup_struct (copy_name ($2),
877 expression_context_block); }
879 { $$ = lookup_struct (copy_name ($2),
880 expression_context_block); }
882 { $$ = lookup_union (copy_name ($2),
883 expression_context_block); }
885 { $$ = lookup_enum (copy_name ($2),
886 expression_context_block); }
888 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
890 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_int; }
891 | SIGNED_KEYWORD typename
892 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
894 { $$ = builtin_type (current_gdbarch)->builtin_int; }
895 /* It appears that this rule for templates is never
896 reduced; template recognition happens by lookahead
897 in the token processing code in yylex. */
898 | TEMPLATE name '<' type '>'
899 { $$ = lookup_template_type(copy_name($2), $4,
900 expression_context_block);
902 | const_or_volatile_or_space_identifier_noopt typebase
903 { $$ = follow_types ($2); }
904 | typebase const_or_volatile_or_space_identifier_noopt
905 { $$ = follow_types ($1); }
909 /* FIXME: carlton/2003-09-25: This next bit leads to lots of
910 reduce-reduce conflicts, because the parser doesn't know whether or
911 not to use qualified_name or qualified_type: the rules are
912 identical. If the parser is parsing 'A::B::x', then, when it sees
913 the second '::', it knows that the expression to the left of it has
914 to be a type, so it uses qualified_type. But if it is parsing just
915 'A::B', then it doesn't have any way of knowing which rule to use,
916 so there's a reduce-reduce conflict; it picks qualified_name, since
917 that occurs earlier in this file than qualified_type.
919 There's no good way to fix this with the grammar as it stands; as
920 far as I can tell, some of the problems arise from ambiguities that
921 GDB introduces ('start' can be either an expression or a type), but
922 some of it is inherent to the nature of C++ (you want to treat the
923 input "(FOO)" fairly differently depending on whether FOO is an
924 expression or a type, and if FOO is a complex expression, this can
925 be hard to determine at the right time). Fortunately, it works
926 pretty well in most cases. For example, if you do 'ptype A::B',
927 where A::B is a nested type, then the parser will mistakenly
928 misidentify it as an expression; but evaluate_subexp will get
929 called with 'noside' set to EVAL_AVOID_SIDE_EFFECTS, and everything
930 will work out anyways. But there are situations where the parser
931 will get confused: the most common one that I've run into is when
936 where the parser doesn't realize that A::B has to be a type until
937 it hits the first right paren, at which point it's too late. (The
938 workaround is to type "print *(('A::B' *) x)" instead.) (And
939 another solution is to fix our symbol-handling code so that the
940 user never wants to type something like that in the first place,
941 because we get all the types right without the user's help!)
943 Perhaps we could fix this by making the lexer smarter. Some of
944 this functionality used to be in the lexer, but in a way that
945 worked even less well than the current solution: that attempt
946 involved having the parser sometimes handle '::' and having the
947 lexer sometimes handle it, and without a clear division of
948 responsibility, it quickly degenerated into a big mess. Probably
949 the eventual correct solution will give more of a role to the lexer
950 (ideally via code that is shared between the lexer and
951 decode_line_1), but I'm not holding my breath waiting for somebody
952 to get around to cleaning this up... */
954 qualified_type: typebase COLONCOLON name
956 struct type *type = $1;
957 struct type *new_type;
958 char *ncopy = alloca ($3.length + 1);
960 memcpy (ncopy, $3.ptr, $3.length);
961 ncopy[$3.length] = '\0';
963 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
964 && TYPE_CODE (type) != TYPE_CODE_UNION
965 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
966 error ("`%s' is not defined as an aggregate type.",
969 new_type = cp_lookup_nested_type (type, ncopy,
970 expression_context_block);
971 if (new_type == NULL)
972 error ("No type \"%s\" within class or namespace \"%s\".",
973 ncopy, TYPE_NAME (type));
982 $$.stoken.ptr = "int";
983 $$.stoken.length = 3;
984 $$.type = builtin_type (current_gdbarch)->builtin_int;
988 $$.stoken.ptr = "long";
989 $$.stoken.length = 4;
990 $$.type = builtin_type (current_gdbarch)->builtin_long;
994 $$.stoken.ptr = "short";
995 $$.stoken.length = 5;
996 $$.type = builtin_type (current_gdbarch)->builtin_short;
1002 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1003 $<ivec>$[0] = 1; /* Number of types in vector */
1006 | nonempty_typelist ',' type
1007 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1008 $$ = (struct type **) realloc ((char *) $1, len);
1009 $$[$<ivec>$[0]] = $3;
1014 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1015 { $$ = follow_types ($1); }
1018 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1019 | VOLATILE_KEYWORD CONST_KEYWORD
1022 const_or_volatile_noopt: const_and_volatile
1023 { push_type (tp_const);
1024 push_type (tp_volatile);
1027 { push_type (tp_const); }
1029 { push_type (tp_volatile); }
1032 name : NAME { $$ = $1.stoken; }
1033 | BLOCKNAME { $$ = $1.stoken; }
1034 | TYPENAME { $$ = $1.stoken; }
1035 | NAME_OR_INT { $$ = $1.stoken; }
1038 name_not_typename : NAME
1040 /* These would be useful if name_not_typename was useful, but it is just
1041 a fake for "variable", so these cause reduce/reduce conflicts because
1042 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1043 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1044 context where only a name could occur, this might be useful.
1051 /* Take care of parsing a number (anything that starts with a digit).
1052 Set yylval and return the token type; update lexptr.
1053 LEN is the number of characters in it. */
1055 /*** Needs some error checking for the float case ***/
1058 parse_number (p, len, parsed_float, putithere)
1064 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1065 here, and we do kind of silly things like cast to unsigned. */
1072 int base = input_radix;
1075 /* Number of "L" suffixes encountered. */
1078 /* We have found a "L" or "U" suffix. */
1079 int found_suffix = 0;
1082 struct type *signed_type;
1083 struct type *unsigned_type;
1087 /* It's a float since it contains a point or an exponent. */
1088 char *s = malloc (len);
1089 int num = 0; /* number of tokens scanned by scanf */
1090 char saved_char = p[len];
1092 p[len] = 0; /* null-terminate the token */
1094 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1095 point. Return DECFLOAT. */
1097 if (p[len - 2] == 'd' && p[len - 1] == 'f')
1100 putithere->typed_val_decfloat.type
1101 = builtin_type (current_gdbarch)->builtin_decfloat;
1102 decimal_from_string (putithere->typed_val_decfloat.val, 4, p);
1103 p[len] = saved_char;
1107 if (p[len - 2] == 'd' && p[len - 1] == 'd')
1110 putithere->typed_val_decfloat.type
1111 = builtin_type (current_gdbarch)->builtin_decdouble;
1112 decimal_from_string (putithere->typed_val_decfloat.val, 8, p);
1113 p[len] = saved_char;
1117 if (p[len - 2] == 'd' && p[len - 1] == 'l')
1120 putithere->typed_val_decfloat.type
1121 = builtin_type (current_gdbarch)->builtin_declong;
1122 decimal_from_string (putithere->typed_val_decfloat.val, 16, p);
1123 p[len] = saved_char;
1127 num = sscanf (p, DOUBLEST_SCAN_FORMAT "%s",
1128 &putithere->typed_val_float.dval, s);
1129 p[len] = saved_char; /* restore the input stream */
1132 putithere->typed_val_float.type =
1133 builtin_type (current_gdbarch)->builtin_double;
1137 /* See if it has any float suffix: 'f' for float, 'l' for long
1139 if (!strcasecmp (s, "f"))
1140 putithere->typed_val_float.type =
1141 builtin_type (current_gdbarch)->builtin_float;
1142 else if (!strcasecmp (s, "l"))
1143 putithere->typed_val_float.type =
1144 builtin_type (current_gdbarch)->builtin_long_double;
1156 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1190 if (c >= 'A' && c <= 'Z')
1192 if (c != 'l' && c != 'u')
1194 if (c >= '0' && c <= '9')
1202 if (base > 10 && c >= 'a' && c <= 'f')
1206 n += i = c - 'a' + 10;
1219 return ERROR; /* Char not a digit */
1222 return ERROR; /* Invalid digit in this base */
1224 /* Portably test for overflow (only works for nonzero values, so make
1225 a second check for zero). FIXME: Can't we just make n and prevn
1226 unsigned and avoid this? */
1227 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1228 unsigned_p = 1; /* Try something unsigned */
1230 /* Portably test for unsigned overflow.
1231 FIXME: This check is wrong; for example it doesn't find overflow
1232 on 0x123456789 when LONGEST is 32 bits. */
1233 if (c != 'l' && c != 'u' && n != 0)
1235 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1236 error ("Numeric constant too large.");
1241 /* An integer constant is an int, a long, or a long long. An L
1242 suffix forces it to be long; an LL suffix forces it to be long
1243 long. If not forced to a larger size, it gets the first type of
1244 the above that it fits in. To figure out whether it fits, we
1245 shift it right and see whether anything remains. Note that we
1246 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1247 operation, because many compilers will warn about such a shift
1248 (which always produces a zero result). Sometimes gdbarch_int_bit
1249 or gdbarch_long_bit will be that big, sometimes not. To deal with
1250 the case where it is we just always shift the value more than
1251 once, with fewer bits each time. */
1253 un = (ULONGEST)n >> 2;
1255 && (un >> (gdbarch_int_bit (current_gdbarch) - 2)) == 0)
1257 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (current_gdbarch) - 1);
1259 /* A large decimal (not hex or octal) constant (between INT_MAX
1260 and UINT_MAX) is a long or unsigned long, according to ANSI,
1261 never an unsigned int, but this code treats it as unsigned
1262 int. This probably should be fixed. GCC gives a warning on
1265 unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_int;
1266 signed_type = builtin_type (current_gdbarch)->builtin_int;
1268 else if (long_p <= 1
1269 && (un >> (gdbarch_long_bit (current_gdbarch) - 2)) == 0)
1271 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (current_gdbarch) - 1);
1272 unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_long;
1273 signed_type = builtin_type (current_gdbarch)->builtin_long;
1278 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1279 < gdbarch_long_long_bit (current_gdbarch))
1280 /* A long long does not fit in a LONGEST. */
1281 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1283 shift = (gdbarch_long_long_bit (current_gdbarch) - 1);
1284 high_bit = (ULONGEST) 1 << shift;
1285 unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_long_long;
1286 signed_type = builtin_type (current_gdbarch)->builtin_long_long;
1289 putithere->typed_val_int.val = n;
1291 /* If the high bit of the worked out type is set then this number
1292 has to be unsigned. */
1294 if (unsigned_p || (n & high_bit))
1296 putithere->typed_val_int.type = unsigned_type;
1300 putithere->typed_val_int.type = signed_type;
1310 enum exp_opcode opcode;
1313 static const struct token tokentab3[] =
1315 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1316 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1319 static const struct token tokentab2[] =
1321 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1322 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1323 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1324 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1325 {"%=", ASSIGN_MODIFY, BINOP_REM},
1326 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1327 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1328 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1329 {"++", INCREMENT, BINOP_END},
1330 {"--", DECREMENT, BINOP_END},
1331 {"->", ARROW, BINOP_END},
1332 {"&&", ANDAND, BINOP_END},
1333 {"||", OROR, BINOP_END},
1334 {"::", COLONCOLON, BINOP_END},
1335 {"<<", LSH, BINOP_END},
1336 {">>", RSH, BINOP_END},
1337 {"==", EQUAL, BINOP_END},
1338 {"!=", NOTEQUAL, BINOP_END},
1339 {"<=", LEQ, BINOP_END},
1340 {">=", GEQ, BINOP_END}
1343 /* Read one token, getting characters through lexptr. */
1354 static char *tempbuf;
1355 static int tempbufsize;
1356 char * token_string = NULL;
1357 int class_prefix = 0;
1361 /* Check if this is a macro invocation that we need to expand. */
1362 if (! scanning_macro_expansion ())
1364 char *expanded = macro_expand_next (&lexptr,
1365 expression_macro_lookup_func,
1366 expression_macro_lookup_baton);
1369 scan_macro_expansion (expanded);
1372 prev_lexptr = lexptr;
1375 /* See if it is a special token of length 3. */
1376 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1377 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
1380 yylval.opcode = tokentab3[i].opcode;
1381 return tokentab3[i].token;
1384 /* See if it is a special token of length 2. */
1385 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1386 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
1389 yylval.opcode = tokentab2[i].opcode;
1390 return tokentab2[i].token;
1393 switch (c = *tokstart)
1396 /* If we were just scanning the result of a macro expansion,
1397 then we need to resume scanning the original text.
1398 Otherwise, we were already scanning the original text, and
1399 we're really done. */
1400 if (scanning_macro_expansion ())
1402 finished_macro_expansion ();
1415 /* We either have a character constant ('0' or '\177' for example)
1416 or we have a quoted symbol reference ('foo(int,int)' in C++
1421 c = parse_escape (&lexptr);
1423 error ("Empty character constant.");
1424 else if (! host_char_to_target (c, &c))
1426 int toklen = lexptr - tokstart + 1;
1427 char *tok = alloca (toklen + 1);
1428 memcpy (tok, tokstart, toklen);
1430 error ("There is no character corresponding to %s in the target "
1431 "character set `%s'.", tok, target_charset ());
1434 yylval.typed_val_int.val = c;
1435 yylval.typed_val_int.type = builtin_type (current_gdbarch)->builtin_char;
1440 namelen = skip_quoted (tokstart) - tokstart;
1443 lexptr = tokstart + namelen;
1444 if (lexptr[-1] != '\'')
1445 error ("Unmatched single quote.");
1450 error ("Invalid character constant.");
1460 if (paren_depth == 0)
1467 if (comma_terminates
1469 && ! scanning_macro_expansion ())
1475 /* Might be a floating point number. */
1476 if (lexptr[1] < '0' || lexptr[1] > '9')
1477 goto symbol; /* Nope, must be a symbol. */
1478 /* FALL THRU into number case. */
1491 /* It's a number. */
1492 int got_dot = 0, got_e = 0, toktype;
1494 int hex = input_radix > 10;
1496 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1501 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1509 /* This test includes !hex because 'e' is a valid hex digit
1510 and thus does not indicate a floating point number when
1511 the radix is hex. */
1512 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1513 got_dot = got_e = 1;
1514 /* This test does not include !hex, because a '.' always indicates
1515 a decimal floating point number regardless of the radix. */
1516 else if (!got_dot && *p == '.')
1518 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1519 && (*p == '-' || *p == '+'))
1520 /* This is the sign of the exponent, not the end of the
1523 /* We will take any letters or digits. parse_number will
1524 complain if past the radix, or if L or U are not final. */
1525 else if ((*p < '0' || *p > '9')
1526 && ((*p < 'a' || *p > 'z')
1527 && (*p < 'A' || *p > 'Z')))
1530 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1531 if (toktype == ERROR)
1533 char *err_copy = (char *) alloca (p - tokstart + 1);
1535 memcpy (err_copy, tokstart, p - tokstart);
1536 err_copy[p - tokstart] = 0;
1537 error ("Invalid number \"%s\".", err_copy);
1569 /* Build the gdb internal form of the input string in tempbuf,
1570 translating any standard C escape forms seen. Note that the
1571 buffer is null byte terminated *only* for the convenience of
1572 debugging gdb itself and printing the buffer contents when
1573 the buffer contains no embedded nulls. Gdb does not depend
1574 upon the buffer being null byte terminated, it uses the length
1575 string instead. This allows gdb to handle C strings (as well
1576 as strings in other languages) with embedded null bytes */
1578 tokptr = ++tokstart;
1582 char *char_start_pos = tokptr;
1584 /* Grow the static temp buffer if necessary, including allocating
1585 the first one on demand. */
1586 if (tempbufindex + 1 >= tempbufsize)
1588 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1594 /* Do nothing, loop will terminate. */
1598 c = parse_escape (&tokptr);
1603 tempbuf[tempbufindex++] = c;
1607 if (! host_char_to_target (c, &c))
1609 int len = tokptr - char_start_pos;
1610 char *copy = alloca (len + 1);
1611 memcpy (copy, char_start_pos, len);
1614 error ("There is no character corresponding to `%s' "
1615 "in the target character set `%s'.",
1616 copy, target_charset ());
1618 tempbuf[tempbufindex++] = c;
1621 } while ((*tokptr != '"') && (*tokptr != '\0'));
1622 if (*tokptr++ != '"')
1624 error ("Unterminated string in expression.");
1626 tempbuf[tempbufindex] = '\0'; /* See note above */
1627 yylval.sval.ptr = tempbuf;
1628 yylval.sval.length = tempbufindex;
1633 if (!(c == '_' || c == '$'
1634 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1635 /* We must have come across a bad character (e.g. ';'). */
1636 error ("Invalid character '%c' in expression.", c);
1638 /* It's a name. See how long it is. */
1640 for (c = tokstart[namelen];
1641 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1642 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1644 /* Template parameter lists are part of the name.
1645 FIXME: This mishandles `print $a<4&&$a>3'. */
1649 /* Scan ahead to get rest of the template specification. Note
1650 that we look ahead only when the '<' adjoins non-whitespace
1651 characters; for comparison expressions, e.g. "a < b > c",
1652 there must be spaces before the '<', etc. */
1654 char * p = find_template_name_end (tokstart + namelen);
1656 namelen = p - tokstart;
1659 c = tokstart[++namelen];
1662 /* The token "if" terminates the expression and is NOT removed from
1663 the input stream. It doesn't count if it appears in the
1664 expansion of a macro. */
1666 && tokstart[0] == 'i'
1667 && tokstart[1] == 'f'
1668 && ! scanning_macro_expansion ())
1677 /* Catch specific keywords. Should be done with a data structure. */
1681 if (strncmp (tokstart, "unsigned", 8) == 0)
1683 if (current_language->la_language == language_cplus
1684 && strncmp (tokstart, "template", 8) == 0)
1686 if (strncmp (tokstart, "volatile", 8) == 0)
1687 return VOLATILE_KEYWORD;
1690 if (strncmp (tokstart, "struct", 6) == 0)
1692 if (strncmp (tokstart, "signed", 6) == 0)
1693 return SIGNED_KEYWORD;
1694 if (strncmp (tokstart, "sizeof", 6) == 0)
1696 if (strncmp (tokstart, "double", 6) == 0)
1697 return DOUBLE_KEYWORD;
1700 if (current_language->la_language == language_cplus)
1702 if (strncmp (tokstart, "false", 5) == 0)
1703 return FALSEKEYWORD;
1704 if (strncmp (tokstart, "class", 5) == 0)
1707 if (strncmp (tokstart, "union", 5) == 0)
1709 if (strncmp (tokstart, "short", 5) == 0)
1711 if (strncmp (tokstart, "const", 5) == 0)
1712 return CONST_KEYWORD;
1715 if (strncmp (tokstart, "enum", 4) == 0)
1717 if (strncmp (tokstart, "long", 4) == 0)
1719 if (current_language->la_language == language_cplus)
1721 if (strncmp (tokstart, "true", 4) == 0)
1726 if (strncmp (tokstart, "int", 3) == 0)
1733 yylval.sval.ptr = tokstart;
1734 yylval.sval.length = namelen;
1736 if (*tokstart == '$')
1738 write_dollar_variable (yylval.sval);
1742 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1743 functions or symtabs. If this is not so, then ...
1744 Use token-type TYPENAME for symbols that happen to be defined
1745 currently as names of types; NAME for other symbols.
1746 The caller is not constrained to care about the distinction. */
1748 char *tmp = copy_name (yylval.sval);
1750 int is_a_field_of_this = 0;
1753 sym = lookup_symbol (tmp, expression_context_block,
1755 current_language->la_language == language_cplus
1756 ? &is_a_field_of_this : (int *) NULL,
1757 (struct symtab **) NULL);
1758 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1759 no psymtabs (coff, xcoff, or some future change to blow away the
1760 psymtabs once once symbols are read). */
1761 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1763 yylval.ssym.sym = sym;
1764 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1768 { /* See if it's a file name. */
1769 struct symtab *symtab;
1771 symtab = lookup_symtab (tmp);
1775 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1780 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1782 /* NOTE: carlton/2003-09-25: There used to be code here to
1783 handle nested types. It didn't work very well. See the
1784 comment before qualified_type for more info. */
1785 yylval.tsym.type = SYMBOL_TYPE (sym);
1789 = language_lookup_primitive_type_by_name (current_language,
1790 current_gdbarch, tmp);
1791 if (yylval.tsym.type != NULL)
1794 /* Input names that aren't symbols but ARE valid hex numbers,
1795 when the input radix permits them, can be names or numbers
1796 depending on the parse. Note we support radixes > 16 here. */
1798 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1799 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1801 YYSTYPE newlval; /* Its value is ignored. */
1802 hextype = parse_number (tokstart, namelen, 0, &newlval);
1805 yylval.ssym.sym = sym;
1806 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1811 /* Any other kind of symbol */
1812 yylval.ssym.sym = sym;
1813 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1823 lexptr = prev_lexptr;
1825 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);