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, 2008
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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* Parse a C expression from text in a string,
24 and return the result as a struct expression pointer.
25 That structure contains arithmetic operations in reverse polish,
26 with constants represented by operations that are followed by special data.
27 See expression.h for the details of the format.
28 What is important here is that it can be built up sequentially
29 during the process of parsing; the lower levels of the tree always
30 come first in the result.
32 Note that malloc's and realloc's in this file are transformed to
33 xmalloc and xrealloc respectively by the same sed command in the
34 makefile that remaps any other malloc/realloc inserted by the parser
35 generator. Doing this with #defines and trying to control the interaction
36 with include files (<malloc.h> and <stdlib.h> for example) just became
37 too messy, particularly when such includes can be inserted at random
38 times by the parser generator. */
43 #include "gdb_string.h"
45 #include "expression.h"
47 #include "parser-defs.h"
50 #include "bfd.h" /* Required by objfiles.h. */
51 #include "symfile.h" /* Required by objfiles.h. */
52 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
55 #include "cp-support.h"
58 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
59 as well as gratuitiously global symbol names, so we can have multiple
60 yacc generated parsers in gdb. Note that these are only the variables
61 produced by yacc. If other parser generators (bison, byacc, etc) produce
62 additional global names that conflict at link time, then those parser
63 generators need to be fixed instead of adding those names to this list. */
65 #define yymaxdepth c_maxdepth
66 #define yyparse c_parse
68 #define yyerror c_error
71 #define yydebug c_debug
80 #define yyerrflag c_errflag
81 #define yynerrs c_nerrs
86 #define yystate c_state
92 #define yyreds c_reds /* With YYDEBUG defined */
93 #define yytoks c_toks /* With YYDEBUG defined */
94 #define yyname c_name /* With YYDEBUG defined */
95 #define yyrule c_rule /* With YYDEBUG defined */
98 #define yydefred c_yydefred
99 #define yydgoto c_yydgoto
100 #define yysindex c_yysindex
101 #define yyrindex c_yyrindex
102 #define yygindex c_yygindex
103 #define yytable c_yytable
104 #define yycheck c_yycheck
107 #define YYDEBUG 1 /* Default to yydebug support */
110 #define YYFPRINTF parser_fprintf
114 static int yylex (void);
116 void yyerror (char *);
120 /* Although the yacc "value" of an expression is not used,
121 since the result is stored in the structure being created,
122 other node types do have values. */
138 } typed_val_decfloat;
143 struct symtoken ssym;
146 enum exp_opcode opcode;
147 struct internalvar *ivar;
154 /* YYSTYPE gets defined by %union */
155 static int parse_number (char *, int, int, YYSTYPE *);
158 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
160 %type <tval> type typebase qualified_type
161 %type <tvec> nonempty_typelist
162 /* %type <bval> block */
164 /* Fancy type parsing. */
165 %type <voidval> func_mod direct_abs_decl abs_decl
167 %type <lval> array_mod
169 %token <typed_val_int> INT
170 %token <typed_val_float> FLOAT
171 %token <typed_val_decfloat> DECFLOAT
173 /* Both NAME and TYPENAME tokens represent symbols in the input,
174 and both convey their data as strings.
175 But a TYPENAME is a string that happens to be defined as a typedef
176 or builtin type name (such as int or char)
177 and a NAME is any other symbol.
178 Contexts where this distinction is not important can use the
179 nonterminal "name", which matches either NAME or TYPENAME. */
182 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
183 %token <tsym> TYPENAME
185 %type <ssym> name_not_typename
186 %type <tsym> typename
188 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
189 but which would parse as a valid number in the current input radix.
190 E.g. "c" when input_radix==16. Depending on the parse, it will be
191 turned into a name or into a number. */
193 %token <ssym> NAME_OR_INT
195 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
199 /* Special type cases, put in to allow the parser to distinguish different
201 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
203 %token <voidval> VARIABLE
205 %token <opcode> ASSIGN_MODIFY
214 %right '=' ASSIGN_MODIFY
222 %left '<' '>' LEQ GEQ
227 %right UNARY INCREMENT DECREMENT
228 %right ARROW '.' '[' '('
229 %token <ssym> BLOCKNAME
230 %token <bval> FILENAME
242 { write_exp_elt_opcode(OP_TYPE);
243 write_exp_elt_type($1);
244 write_exp_elt_opcode(OP_TYPE);}
247 /* Expressions, including the comma operator. */
250 { write_exp_elt_opcode (BINOP_COMMA); }
253 /* Expressions, not including the comma operator. */
254 exp : '*' exp %prec UNARY
255 { write_exp_elt_opcode (UNOP_IND); }
258 exp : '&' exp %prec UNARY
259 { write_exp_elt_opcode (UNOP_ADDR); }
262 exp : '-' exp %prec UNARY
263 { write_exp_elt_opcode (UNOP_NEG); }
266 exp : '+' exp %prec UNARY
267 { write_exp_elt_opcode (UNOP_PLUS); }
270 exp : '!' exp %prec UNARY
271 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
274 exp : '~' exp %prec UNARY
275 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
278 exp : INCREMENT exp %prec UNARY
279 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
282 exp : DECREMENT exp %prec UNARY
283 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
286 exp : exp INCREMENT %prec UNARY
287 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
290 exp : exp DECREMENT %prec UNARY
291 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
294 exp : SIZEOF exp %prec UNARY
295 { write_exp_elt_opcode (UNOP_SIZEOF); }
299 { write_exp_elt_opcode (STRUCTOP_PTR);
300 write_exp_string ($3);
301 write_exp_elt_opcode (STRUCTOP_PTR); }
304 exp : exp ARROW qualified_name
305 { /* exp->type::name becomes exp->*(&type::name) */
306 /* Note: this doesn't work if name is a
307 static member! FIXME */
308 write_exp_elt_opcode (UNOP_ADDR);
309 write_exp_elt_opcode (STRUCTOP_MPTR); }
312 exp : exp ARROW '*' exp
313 { write_exp_elt_opcode (STRUCTOP_MPTR); }
317 { write_exp_elt_opcode (STRUCTOP_STRUCT);
318 write_exp_string ($3);
319 write_exp_elt_opcode (STRUCTOP_STRUCT); }
322 exp : exp '.' qualified_name
323 { /* exp.type::name becomes exp.*(&type::name) */
324 /* Note: this doesn't work if name is a
325 static member! FIXME */
326 write_exp_elt_opcode (UNOP_ADDR);
327 write_exp_elt_opcode (STRUCTOP_MEMBER); }
330 exp : exp '.' '*' exp
331 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
334 exp : exp '[' exp1 ']'
335 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
339 /* This is to save the value of arglist_len
340 being accumulated by an outer function call. */
341 { start_arglist (); }
342 arglist ')' %prec ARROW
343 { write_exp_elt_opcode (OP_FUNCALL);
344 write_exp_elt_longcst ((LONGEST) end_arglist ());
345 write_exp_elt_opcode (OP_FUNCALL); }
349 { start_arglist (); }
359 arglist : arglist ',' exp %prec ABOVE_COMMA
364 { $$ = end_arglist () - 1; }
366 exp : lcurly arglist rcurly %prec ARROW
367 { write_exp_elt_opcode (OP_ARRAY);
368 write_exp_elt_longcst ((LONGEST) 0);
369 write_exp_elt_longcst ((LONGEST) $3);
370 write_exp_elt_opcode (OP_ARRAY); }
373 exp : lcurly type rcurly exp %prec UNARY
374 { write_exp_elt_opcode (UNOP_MEMVAL);
375 write_exp_elt_type ($2);
376 write_exp_elt_opcode (UNOP_MEMVAL); }
379 exp : '(' type ')' exp %prec UNARY
380 { write_exp_elt_opcode (UNOP_CAST);
381 write_exp_elt_type ($2);
382 write_exp_elt_opcode (UNOP_CAST); }
389 /* Binary operators in order of decreasing precedence. */
392 { write_exp_elt_opcode (BINOP_REPEAT); }
396 { write_exp_elt_opcode (BINOP_MUL); }
400 { write_exp_elt_opcode (BINOP_DIV); }
404 { write_exp_elt_opcode (BINOP_REM); }
408 { write_exp_elt_opcode (BINOP_ADD); }
412 { write_exp_elt_opcode (BINOP_SUB); }
416 { write_exp_elt_opcode (BINOP_LSH); }
420 { write_exp_elt_opcode (BINOP_RSH); }
424 { write_exp_elt_opcode (BINOP_EQUAL); }
427 exp : exp NOTEQUAL exp
428 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
432 { write_exp_elt_opcode (BINOP_LEQ); }
436 { write_exp_elt_opcode (BINOP_GEQ); }
440 { write_exp_elt_opcode (BINOP_LESS); }
444 { write_exp_elt_opcode (BINOP_GTR); }
448 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
452 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
456 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
460 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
464 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
467 exp : exp '?' exp ':' exp %prec '?'
468 { write_exp_elt_opcode (TERNOP_COND); }
472 { write_exp_elt_opcode (BINOP_ASSIGN); }
475 exp : exp ASSIGN_MODIFY exp
476 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
477 write_exp_elt_opcode ($2);
478 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
482 { write_exp_elt_opcode (OP_LONG);
483 write_exp_elt_type ($1.type);
484 write_exp_elt_longcst ((LONGEST)($1.val));
485 write_exp_elt_opcode (OP_LONG); }
490 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
491 write_exp_elt_opcode (OP_LONG);
492 write_exp_elt_type (val.typed_val_int.type);
493 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
494 write_exp_elt_opcode (OP_LONG);
500 { write_exp_elt_opcode (OP_DOUBLE);
501 write_exp_elt_type ($1.type);
502 write_exp_elt_dblcst ($1.dval);
503 write_exp_elt_opcode (OP_DOUBLE); }
507 { write_exp_elt_opcode (OP_DECFLOAT);
508 write_exp_elt_type ($1.type);
509 write_exp_elt_decfloatcst ($1.val);
510 write_exp_elt_opcode (OP_DECFLOAT); }
517 /* Already written by write_dollar_variable. */
520 exp : SIZEOF '(' type ')' %prec UNARY
521 { write_exp_elt_opcode (OP_LONG);
522 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_int);
524 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
525 write_exp_elt_opcode (OP_LONG); }
529 { /* C strings are converted into array constants with
530 an explicit null byte added at the end. Thus
531 the array upper bound is the string length.
532 There is no such thing in C as a completely empty
534 char *sp = $1.ptr; int count = $1.length;
537 write_exp_elt_opcode (OP_LONG);
538 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_char);
539 write_exp_elt_longcst ((LONGEST)(*sp++));
540 write_exp_elt_opcode (OP_LONG);
542 write_exp_elt_opcode (OP_LONG);
543 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_char);
544 write_exp_elt_longcst ((LONGEST)'\0');
545 write_exp_elt_opcode (OP_LONG);
546 write_exp_elt_opcode (OP_ARRAY);
547 write_exp_elt_longcst ((LONGEST) 0);
548 write_exp_elt_longcst ((LONGEST) ($1.length));
549 write_exp_elt_opcode (OP_ARRAY); }
554 { write_exp_elt_opcode (OP_LONG);
555 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_bool);
556 write_exp_elt_longcst ((LONGEST) 1);
557 write_exp_elt_opcode (OP_LONG); }
561 { write_exp_elt_opcode (OP_LONG);
562 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_bool);
563 write_exp_elt_longcst ((LONGEST) 0);
564 write_exp_elt_opcode (OP_LONG); }
572 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
574 error ("No file or function \"%s\".",
575 copy_name ($1.stoken));
583 block : block COLONCOLON name
585 = lookup_symbol (copy_name ($3), $1,
586 VAR_DOMAIN, (int *) 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);
598 error ("No symbol \"%s\" in specified context.",
601 write_exp_elt_opcode (OP_VAR_VALUE);
602 /* block_found is set by lookup_symbol. */
603 write_exp_elt_block (block_found);
604 write_exp_elt_sym (sym);
605 write_exp_elt_opcode (OP_VAR_VALUE); }
608 qualified_name: typebase COLONCOLON name
610 struct type *type = $1;
611 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
612 && TYPE_CODE (type) != TYPE_CODE_UNION
613 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
614 error ("`%s' is not defined as an aggregate type.",
617 write_exp_elt_opcode (OP_SCOPE);
618 write_exp_elt_type (type);
619 write_exp_string ($3);
620 write_exp_elt_opcode (OP_SCOPE);
622 | typebase COLONCOLON '~' name
624 struct type *type = $1;
625 struct stoken tmp_token;
626 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
627 && TYPE_CODE (type) != TYPE_CODE_UNION
628 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
629 error ("`%s' is not defined as an aggregate type.",
632 tmp_token.ptr = (char*) alloca ($4.length + 2);
633 tmp_token.length = $4.length + 1;
634 tmp_token.ptr[0] = '~';
635 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
636 tmp_token.ptr[tmp_token.length] = 0;
638 /* Check for valid destructor name. */
639 destructor_name_p (tmp_token.ptr, type);
640 write_exp_elt_opcode (OP_SCOPE);
641 write_exp_elt_type (type);
642 write_exp_string (tmp_token);
643 write_exp_elt_opcode (OP_SCOPE);
647 variable: qualified_name
650 char *name = copy_name ($2);
652 struct minimal_symbol *msymbol;
655 lookup_symbol (name, (const struct block *) NULL,
656 VAR_DOMAIN, (int *) NULL);
659 write_exp_elt_opcode (OP_VAR_VALUE);
660 write_exp_elt_block (NULL);
661 write_exp_elt_sym (sym);
662 write_exp_elt_opcode (OP_VAR_VALUE);
666 msymbol = lookup_minimal_symbol (name, NULL, NULL);
669 write_exp_msymbol (msymbol,
670 lookup_function_type (builtin_type (current_gdbarch)->builtin_int),
671 builtin_type (current_gdbarch)->builtin_int);
674 if (!have_full_symbols () && !have_partial_symbols ())
675 error ("No symbol table is loaded. Use the \"file\" command.");
677 error ("No symbol \"%s\" in current context.", name);
681 variable: name_not_typename
682 { struct symbol *sym = $1.sym;
686 if (symbol_read_needs_frame (sym))
688 if (innermost_block == 0 ||
689 contained_in (block_found,
691 innermost_block = block_found;
694 write_exp_elt_opcode (OP_VAR_VALUE);
695 /* We want to use the selected frame, not
696 another more inner frame which happens to
697 be in the same block. */
698 write_exp_elt_block (NULL);
699 write_exp_elt_sym (sym);
700 write_exp_elt_opcode (OP_VAR_VALUE);
702 else if ($1.is_a_field_of_this)
704 /* C++: it hangs off of `this'. Must
705 not inadvertently convert from a method call
707 if (innermost_block == 0 ||
708 contained_in (block_found, innermost_block))
709 innermost_block = block_found;
710 write_exp_elt_opcode (OP_THIS);
711 write_exp_elt_opcode (OP_THIS);
712 write_exp_elt_opcode (STRUCTOP_PTR);
713 write_exp_string ($1.stoken);
714 write_exp_elt_opcode (STRUCTOP_PTR);
718 struct minimal_symbol *msymbol;
719 char *arg = copy_name ($1.stoken);
722 lookup_minimal_symbol (arg, NULL, NULL);
725 write_exp_msymbol (msymbol,
726 lookup_function_type (builtin_type (current_gdbarch)->builtin_int),
727 builtin_type (current_gdbarch)->builtin_int);
729 else if (!have_full_symbols () && !have_partial_symbols ())
730 error ("No symbol table is loaded. Use the \"file\" command.");
732 error ("No symbol \"%s\" in current context.",
733 copy_name ($1.stoken));
738 space_identifier : '@' NAME
739 { push_type_address_space (copy_name ($2.stoken));
740 push_type (tp_space_identifier);
744 const_or_volatile: const_or_volatile_noopt
748 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
751 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
752 | const_or_volatile_noopt
755 const_or_volatile_or_space_identifier:
756 const_or_volatile_or_space_identifier_noopt
761 { push_type (tp_pointer); $$ = 0; }
763 { push_type (tp_pointer); $$ = $2; }
765 { push_type (tp_reference); $$ = 0; }
767 { push_type (tp_reference); $$ = $2; }
771 direct_abs_decl: '(' abs_decl ')'
773 | direct_abs_decl array_mod
776 push_type (tp_array);
781 push_type (tp_array);
785 | direct_abs_decl func_mod
786 { push_type (tp_function); }
788 { push_type (tp_function); }
799 | '(' nonempty_typelist ')'
800 { free ($2); $$ = 0; }
803 /* We used to try to recognize pointer to member types here, but
804 that didn't work (shift/reduce conflicts meant that these rules never
805 got executed). The problem is that
806 int (foo::bar::baz::bizzle)
807 is a function type but
808 int (foo::bar::baz::bizzle::*)
809 is a pointer to member type. Stroustrup loses again! */
814 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
818 { $$ = builtin_type (current_gdbarch)->builtin_int; }
820 { $$ = builtin_type (current_gdbarch)->builtin_long; }
822 { $$ = builtin_type (current_gdbarch)->builtin_short; }
824 { $$ = builtin_type (current_gdbarch)->builtin_long; }
825 | LONG SIGNED_KEYWORD INT_KEYWORD
826 { $$ = builtin_type (current_gdbarch)->builtin_long; }
827 | LONG SIGNED_KEYWORD
828 { $$ = builtin_type (current_gdbarch)->builtin_long; }
829 | SIGNED_KEYWORD LONG INT_KEYWORD
830 { $$ = builtin_type (current_gdbarch)->builtin_long; }
831 | UNSIGNED LONG INT_KEYWORD
832 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
833 | LONG UNSIGNED INT_KEYWORD
834 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
836 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
838 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
839 | LONG LONG INT_KEYWORD
840 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
841 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
842 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
843 | LONG LONG SIGNED_KEYWORD
844 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
845 | SIGNED_KEYWORD LONG LONG
846 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
847 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
848 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
850 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
851 | UNSIGNED LONG LONG INT_KEYWORD
852 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
854 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
855 | LONG LONG UNSIGNED INT_KEYWORD
856 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
858 { $$ = builtin_type (current_gdbarch)->builtin_short; }
859 | SHORT SIGNED_KEYWORD INT_KEYWORD
860 { $$ = builtin_type (current_gdbarch)->builtin_short; }
861 | SHORT SIGNED_KEYWORD
862 { $$ = builtin_type (current_gdbarch)->builtin_short; }
863 | UNSIGNED SHORT INT_KEYWORD
864 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
866 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
867 | SHORT UNSIGNED INT_KEYWORD
868 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
870 { $$ = builtin_type (current_gdbarch)->builtin_double; }
871 | LONG DOUBLE_KEYWORD
872 { $$ = builtin_type (current_gdbarch)->builtin_long_double; }
874 { $$ = lookup_struct (copy_name ($2),
875 expression_context_block); }
877 { $$ = lookup_struct (copy_name ($2),
878 expression_context_block); }
880 { $$ = lookup_union (copy_name ($2),
881 expression_context_block); }
883 { $$ = lookup_enum (copy_name ($2),
884 expression_context_block); }
886 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
888 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_int; }
889 | SIGNED_KEYWORD typename
890 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
892 { $$ = builtin_type (current_gdbarch)->builtin_int; }
893 /* It appears that this rule for templates is never
894 reduced; template recognition happens by lookahead
895 in the token processing code in yylex. */
896 | TEMPLATE name '<' type '>'
897 { $$ = lookup_template_type(copy_name($2), $4,
898 expression_context_block);
900 | const_or_volatile_or_space_identifier_noopt typebase
901 { $$ = follow_types ($2); }
902 | typebase const_or_volatile_or_space_identifier_noopt
903 { $$ = follow_types ($1); }
907 /* FIXME: carlton/2003-09-25: This next bit leads to lots of
908 reduce-reduce conflicts, because the parser doesn't know whether or
909 not to use qualified_name or qualified_type: the rules are
910 identical. If the parser is parsing 'A::B::x', then, when it sees
911 the second '::', it knows that the expression to the left of it has
912 to be a type, so it uses qualified_type. But if it is parsing just
913 'A::B', then it doesn't have any way of knowing which rule to use,
914 so there's a reduce-reduce conflict; it picks qualified_name, since
915 that occurs earlier in this file than qualified_type.
917 There's no good way to fix this with the grammar as it stands; as
918 far as I can tell, some of the problems arise from ambiguities that
919 GDB introduces ('start' can be either an expression or a type), but
920 some of it is inherent to the nature of C++ (you want to treat the
921 input "(FOO)" fairly differently depending on whether FOO is an
922 expression or a type, and if FOO is a complex expression, this can
923 be hard to determine at the right time). Fortunately, it works
924 pretty well in most cases. For example, if you do 'ptype A::B',
925 where A::B is a nested type, then the parser will mistakenly
926 misidentify it as an expression; but evaluate_subexp will get
927 called with 'noside' set to EVAL_AVOID_SIDE_EFFECTS, and everything
928 will work out anyways. But there are situations where the parser
929 will get confused: the most common one that I've run into is when
934 where the parser doesn't realize that A::B has to be a type until
935 it hits the first right paren, at which point it's too late. (The
936 workaround is to type "print *(('A::B' *) x)" instead.) (And
937 another solution is to fix our symbol-handling code so that the
938 user never wants to type something like that in the first place,
939 because we get all the types right without the user's help!)
941 Perhaps we could fix this by making the lexer smarter. Some of
942 this functionality used to be in the lexer, but in a way that
943 worked even less well than the current solution: that attempt
944 involved having the parser sometimes handle '::' and having the
945 lexer sometimes handle it, and without a clear division of
946 responsibility, it quickly degenerated into a big mess. Probably
947 the eventual correct solution will give more of a role to the lexer
948 (ideally via code that is shared between the lexer and
949 decode_line_1), but I'm not holding my breath waiting for somebody
950 to get around to cleaning this up... */
952 qualified_type: typebase COLONCOLON name
954 struct type *type = $1;
955 struct type *new_type;
956 char *ncopy = alloca ($3.length + 1);
958 memcpy (ncopy, $3.ptr, $3.length);
959 ncopy[$3.length] = '\0';
961 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
962 && TYPE_CODE (type) != TYPE_CODE_UNION
963 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
964 error ("`%s' is not defined as an aggregate type.",
967 new_type = cp_lookup_nested_type (type, ncopy,
968 expression_context_block);
969 if (new_type == NULL)
970 error ("No type \"%s\" within class or namespace \"%s\".",
971 ncopy, TYPE_NAME (type));
980 $$.stoken.ptr = "int";
981 $$.stoken.length = 3;
982 $$.type = builtin_type (current_gdbarch)->builtin_int;
986 $$.stoken.ptr = "long";
987 $$.stoken.length = 4;
988 $$.type = builtin_type (current_gdbarch)->builtin_long;
992 $$.stoken.ptr = "short";
993 $$.stoken.length = 5;
994 $$.type = builtin_type (current_gdbarch)->builtin_short;
1000 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1001 $<ivec>$[0] = 1; /* Number of types in vector */
1004 | nonempty_typelist ',' type
1005 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1006 $$ = (struct type **) realloc ((char *) $1, len);
1007 $$[$<ivec>$[0]] = $3;
1012 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1013 { $$ = follow_types ($1); }
1016 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1017 | VOLATILE_KEYWORD CONST_KEYWORD
1020 const_or_volatile_noopt: const_and_volatile
1021 { push_type (tp_const);
1022 push_type (tp_volatile);
1025 { push_type (tp_const); }
1027 { push_type (tp_volatile); }
1030 name : NAME { $$ = $1.stoken; }
1031 | BLOCKNAME { $$ = $1.stoken; }
1032 | TYPENAME { $$ = $1.stoken; }
1033 | NAME_OR_INT { $$ = $1.stoken; }
1036 name_not_typename : NAME
1038 /* These would be useful if name_not_typename was useful, but it is just
1039 a fake for "variable", so these cause reduce/reduce conflicts because
1040 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1041 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1042 context where only a name could occur, this might be useful.
1049 /* Take care of parsing a number (anything that starts with a digit).
1050 Set yylval and return the token type; update lexptr.
1051 LEN is the number of characters in it. */
1053 /*** Needs some error checking for the float case ***/
1056 parse_number (p, len, parsed_float, putithere)
1062 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1063 here, and we do kind of silly things like cast to unsigned. */
1070 int base = input_radix;
1073 /* Number of "L" suffixes encountered. */
1076 /* We have found a "L" or "U" suffix. */
1077 int found_suffix = 0;
1080 struct type *signed_type;
1081 struct type *unsigned_type;
1085 /* It's a float since it contains a point or an exponent. */
1086 char *s = malloc (len);
1087 int num = 0; /* number of tokens scanned by scanf */
1088 char saved_char = p[len];
1090 p[len] = 0; /* null-terminate the token */
1092 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1093 point. Return DECFLOAT. */
1095 if (p[len - 2] == 'd' && p[len - 1] == 'f')
1098 putithere->typed_val_decfloat.type
1099 = builtin_type (current_gdbarch)->builtin_decfloat;
1100 decimal_from_string (putithere->typed_val_decfloat.val, 4, p);
1101 p[len] = saved_char;
1105 if (p[len - 2] == 'd' && p[len - 1] == 'd')
1108 putithere->typed_val_decfloat.type
1109 = builtin_type (current_gdbarch)->builtin_decdouble;
1110 decimal_from_string (putithere->typed_val_decfloat.val, 8, p);
1111 p[len] = saved_char;
1115 if (p[len - 2] == 'd' && p[len - 1] == 'l')
1118 putithere->typed_val_decfloat.type
1119 = builtin_type (current_gdbarch)->builtin_declong;
1120 decimal_from_string (putithere->typed_val_decfloat.val, 16, p);
1121 p[len] = saved_char;
1125 num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%s",
1126 &putithere->typed_val_float.dval, s);
1127 p[len] = saved_char; /* restore the input stream */
1130 putithere->typed_val_float.type =
1131 builtin_type (current_gdbarch)->builtin_double;
1135 /* See if it has any float suffix: 'f' for float, 'l' for long
1137 if (!strcasecmp (s, "f"))
1138 putithere->typed_val_float.type =
1139 builtin_type (current_gdbarch)->builtin_float;
1140 else if (!strcasecmp (s, "l"))
1141 putithere->typed_val_float.type =
1142 builtin_type (current_gdbarch)->builtin_long_double;
1154 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1188 if (c >= 'A' && c <= 'Z')
1190 if (c != 'l' && c != 'u')
1192 if (c >= '0' && c <= '9')
1200 if (base > 10 && c >= 'a' && c <= 'f')
1204 n += i = c - 'a' + 10;
1217 return ERROR; /* Char not a digit */
1220 return ERROR; /* Invalid digit in this base */
1222 /* Portably test for overflow (only works for nonzero values, so make
1223 a second check for zero). FIXME: Can't we just make n and prevn
1224 unsigned and avoid this? */
1225 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1226 unsigned_p = 1; /* Try something unsigned */
1228 /* Portably test for unsigned overflow.
1229 FIXME: This check is wrong; for example it doesn't find overflow
1230 on 0x123456789 when LONGEST is 32 bits. */
1231 if (c != 'l' && c != 'u' && n != 0)
1233 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1234 error ("Numeric constant too large.");
1239 /* An integer constant is an int, a long, or a long long. An L
1240 suffix forces it to be long; an LL suffix forces it to be long
1241 long. If not forced to a larger size, it gets the first type of
1242 the above that it fits in. To figure out whether it fits, we
1243 shift it right and see whether anything remains. Note that we
1244 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1245 operation, because many compilers will warn about such a shift
1246 (which always produces a zero result). Sometimes gdbarch_int_bit
1247 or gdbarch_long_bit will be that big, sometimes not. To deal with
1248 the case where it is we just always shift the value more than
1249 once, with fewer bits each time. */
1251 un = (ULONGEST)n >> 2;
1253 && (un >> (gdbarch_int_bit (current_gdbarch) - 2)) == 0)
1255 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (current_gdbarch) - 1);
1257 /* A large decimal (not hex or octal) constant (between INT_MAX
1258 and UINT_MAX) is a long or unsigned long, according to ANSI,
1259 never an unsigned int, but this code treats it as unsigned
1260 int. This probably should be fixed. GCC gives a warning on
1263 unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_int;
1264 signed_type = builtin_type (current_gdbarch)->builtin_int;
1266 else if (long_p <= 1
1267 && (un >> (gdbarch_long_bit (current_gdbarch) - 2)) == 0)
1269 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (current_gdbarch) - 1);
1270 unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_long;
1271 signed_type = builtin_type (current_gdbarch)->builtin_long;
1276 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1277 < gdbarch_long_long_bit (current_gdbarch))
1278 /* A long long does not fit in a LONGEST. */
1279 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1281 shift = (gdbarch_long_long_bit (current_gdbarch) - 1);
1282 high_bit = (ULONGEST) 1 << shift;
1283 unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_long_long;
1284 signed_type = builtin_type (current_gdbarch)->builtin_long_long;
1287 putithere->typed_val_int.val = n;
1289 /* If the high bit of the worked out type is set then this number
1290 has to be unsigned. */
1292 if (unsigned_p || (n & high_bit))
1294 putithere->typed_val_int.type = unsigned_type;
1298 putithere->typed_val_int.type = signed_type;
1308 enum exp_opcode opcode;
1311 static const struct token tokentab3[] =
1313 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1314 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1317 static const struct token tokentab2[] =
1319 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1320 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1321 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1322 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1323 {"%=", ASSIGN_MODIFY, BINOP_REM},
1324 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1325 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1326 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1327 {"++", INCREMENT, BINOP_END},
1328 {"--", DECREMENT, BINOP_END},
1329 {"->", ARROW, BINOP_END},
1330 {"&&", ANDAND, BINOP_END},
1331 {"||", OROR, BINOP_END},
1332 {"::", COLONCOLON, BINOP_END},
1333 {"<<", LSH, BINOP_END},
1334 {">>", RSH, BINOP_END},
1335 {"==", EQUAL, BINOP_END},
1336 {"!=", NOTEQUAL, BINOP_END},
1337 {"<=", LEQ, BINOP_END},
1338 {">=", GEQ, BINOP_END}
1341 /* Read one token, getting characters through lexptr. */
1352 static char *tempbuf;
1353 static int tempbufsize;
1354 char * token_string = NULL;
1355 int class_prefix = 0;
1359 /* Check if this is a macro invocation that we need to expand. */
1360 if (! scanning_macro_expansion ())
1362 char *expanded = macro_expand_next (&lexptr,
1363 expression_macro_lookup_func,
1364 expression_macro_lookup_baton);
1367 scan_macro_expansion (expanded);
1370 prev_lexptr = lexptr;
1373 /* See if it is a special token of length 3. */
1374 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1375 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
1378 yylval.opcode = tokentab3[i].opcode;
1379 return tokentab3[i].token;
1382 /* See if it is a special token of length 2. */
1383 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1384 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
1387 yylval.opcode = tokentab2[i].opcode;
1388 return tokentab2[i].token;
1391 switch (c = *tokstart)
1394 /* If we were just scanning the result of a macro expansion,
1395 then we need to resume scanning the original text.
1396 Otherwise, we were already scanning the original text, and
1397 we're really done. */
1398 if (scanning_macro_expansion ())
1400 finished_macro_expansion ();
1413 /* We either have a character constant ('0' or '\177' for example)
1414 or we have a quoted symbol reference ('foo(int,int)' in C++
1419 c = parse_escape (&lexptr);
1421 error ("Empty character constant.");
1422 else if (! host_char_to_target (c, &c))
1424 int toklen = lexptr - tokstart + 1;
1425 char *tok = alloca (toklen + 1);
1426 memcpy (tok, tokstart, toklen);
1428 error ("There is no character corresponding to %s in the target "
1429 "character set `%s'.", tok, target_charset ());
1432 yylval.typed_val_int.val = c;
1433 yylval.typed_val_int.type = builtin_type (current_gdbarch)->builtin_char;
1438 namelen = skip_quoted (tokstart) - tokstart;
1441 lexptr = tokstart + namelen;
1442 if (lexptr[-1] != '\'')
1443 error ("Unmatched single quote.");
1448 error ("Invalid character constant.");
1458 if (paren_depth == 0)
1465 if (comma_terminates
1467 && ! scanning_macro_expansion ())
1473 /* Might be a floating point number. */
1474 if (lexptr[1] < '0' || lexptr[1] > '9')
1475 goto symbol; /* Nope, must be a symbol. */
1476 /* FALL THRU into number case. */
1489 /* It's a number. */
1490 int got_dot = 0, got_e = 0, toktype;
1492 int hex = input_radix > 10;
1494 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1499 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1507 /* This test includes !hex because 'e' is a valid hex digit
1508 and thus does not indicate a floating point number when
1509 the radix is hex. */
1510 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1511 got_dot = got_e = 1;
1512 /* This test does not include !hex, because a '.' always indicates
1513 a decimal floating point number regardless of the radix. */
1514 else if (!got_dot && *p == '.')
1516 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1517 && (*p == '-' || *p == '+'))
1518 /* This is the sign of the exponent, not the end of the
1521 /* We will take any letters or digits. parse_number will
1522 complain if past the radix, or if L or U are not final. */
1523 else if ((*p < '0' || *p > '9')
1524 && ((*p < 'a' || *p > 'z')
1525 && (*p < 'A' || *p > 'Z')))
1528 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1529 if (toktype == ERROR)
1531 char *err_copy = (char *) alloca (p - tokstart + 1);
1533 memcpy (err_copy, tokstart, p - tokstart);
1534 err_copy[p - tokstart] = 0;
1535 error ("Invalid number \"%s\".", err_copy);
1567 /* Build the gdb internal form of the input string in tempbuf,
1568 translating any standard C escape forms seen. Note that the
1569 buffer is null byte terminated *only* for the convenience of
1570 debugging gdb itself and printing the buffer contents when
1571 the buffer contains no embedded nulls. Gdb does not depend
1572 upon the buffer being null byte terminated, it uses the length
1573 string instead. This allows gdb to handle C strings (as well
1574 as strings in other languages) with embedded null bytes */
1576 tokptr = ++tokstart;
1580 char *char_start_pos = tokptr;
1582 /* Grow the static temp buffer if necessary, including allocating
1583 the first one on demand. */
1584 if (tempbufindex + 1 >= tempbufsize)
1586 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1592 /* Do nothing, loop will terminate. */
1596 c = parse_escape (&tokptr);
1601 tempbuf[tempbufindex++] = c;
1605 if (! host_char_to_target (c, &c))
1607 int len = tokptr - char_start_pos;
1608 char *copy = alloca (len + 1);
1609 memcpy (copy, char_start_pos, len);
1612 error ("There is no character corresponding to `%s' "
1613 "in the target character set `%s'.",
1614 copy, target_charset ());
1616 tempbuf[tempbufindex++] = c;
1619 } while ((*tokptr != '"') && (*tokptr != '\0'));
1620 if (*tokptr++ != '"')
1622 error ("Unterminated string in expression.");
1624 tempbuf[tempbufindex] = '\0'; /* See note above */
1625 yylval.sval.ptr = tempbuf;
1626 yylval.sval.length = tempbufindex;
1631 if (!(c == '_' || c == '$'
1632 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1633 /* We must have come across a bad character (e.g. ';'). */
1634 error ("Invalid character '%c' in expression.", c);
1636 /* It's a name. See how long it is. */
1638 for (c = tokstart[namelen];
1639 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1640 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1642 /* Template parameter lists are part of the name.
1643 FIXME: This mishandles `print $a<4&&$a>3'. */
1647 /* Scan ahead to get rest of the template specification. Note
1648 that we look ahead only when the '<' adjoins non-whitespace
1649 characters; for comparison expressions, e.g. "a < b > c",
1650 there must be spaces before the '<', etc. */
1652 char * p = find_template_name_end (tokstart + namelen);
1654 namelen = p - tokstart;
1657 c = tokstart[++namelen];
1660 /* The token "if" terminates the expression and is NOT removed from
1661 the input stream. It doesn't count if it appears in the
1662 expansion of a macro. */
1664 && tokstart[0] == 'i'
1665 && tokstart[1] == 'f'
1666 && ! scanning_macro_expansion ())
1675 /* Catch specific keywords. Should be done with a data structure. */
1679 if (strncmp (tokstart, "unsigned", 8) == 0)
1681 if (current_language->la_language == language_cplus
1682 && strncmp (tokstart, "template", 8) == 0)
1684 if (strncmp (tokstart, "volatile", 8) == 0)
1685 return VOLATILE_KEYWORD;
1688 if (strncmp (tokstart, "struct", 6) == 0)
1690 if (strncmp (tokstart, "signed", 6) == 0)
1691 return SIGNED_KEYWORD;
1692 if (strncmp (tokstart, "sizeof", 6) == 0)
1694 if (strncmp (tokstart, "double", 6) == 0)
1695 return DOUBLE_KEYWORD;
1698 if (current_language->la_language == language_cplus)
1700 if (strncmp (tokstart, "false", 5) == 0)
1701 return FALSEKEYWORD;
1702 if (strncmp (tokstart, "class", 5) == 0)
1705 if (strncmp (tokstart, "union", 5) == 0)
1707 if (strncmp (tokstart, "short", 5) == 0)
1709 if (strncmp (tokstart, "const", 5) == 0)
1710 return CONST_KEYWORD;
1713 if (strncmp (tokstart, "enum", 4) == 0)
1715 if (strncmp (tokstart, "long", 4) == 0)
1717 if (current_language->la_language == language_cplus)
1719 if (strncmp (tokstart, "true", 4) == 0)
1724 if (strncmp (tokstart, "int", 3) == 0)
1731 yylval.sval.ptr = tokstart;
1732 yylval.sval.length = namelen;
1734 if (*tokstart == '$')
1736 write_dollar_variable (yylval.sval);
1740 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1741 functions or symtabs. If this is not so, then ...
1742 Use token-type TYPENAME for symbols that happen to be defined
1743 currently as names of types; NAME for other symbols.
1744 The caller is not constrained to care about the distinction. */
1746 char *tmp = copy_name (yylval.sval);
1748 int is_a_field_of_this = 0;
1751 sym = lookup_symbol (tmp, expression_context_block,
1753 current_language->la_language == language_cplus
1754 ? &is_a_field_of_this : (int *) NULL);
1755 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1756 no psymtabs (coff, xcoff, or some future change to blow away the
1757 psymtabs once once symbols are read). */
1758 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1760 yylval.ssym.sym = sym;
1761 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1765 { /* See if it's a file name. */
1766 struct symtab *symtab;
1768 symtab = lookup_symtab (tmp);
1772 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1777 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1779 /* NOTE: carlton/2003-09-25: There used to be code here to
1780 handle nested types. It didn't work very well. See the
1781 comment before qualified_type for more info. */
1782 yylval.tsym.type = SYMBOL_TYPE (sym);
1786 = language_lookup_primitive_type_by_name (current_language,
1787 current_gdbarch, tmp);
1788 if (yylval.tsym.type != NULL)
1791 /* Input names that aren't symbols but ARE valid hex numbers,
1792 when the input radix permits them, can be names or numbers
1793 depending on the parse. Note we support radixes > 16 here. */
1795 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1796 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1798 YYSTYPE newlval; /* Its value is ignored. */
1799 hextype = parse_number (tokstart, namelen, 0, &newlval);
1802 yylval.ssym.sym = sym;
1803 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1808 /* Any other kind of symbol */
1809 yylval.ssym.sym = sym;
1810 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1820 lexptr = prev_lexptr;
1822 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);