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
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"
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. */
138 struct symtoken ssym;
141 enum exp_opcode opcode;
142 struct internalvar *ivar;
149 /* YYSTYPE gets defined by %union */
150 static int parse_number (char *, int, int, YYSTYPE *);
153 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
155 %type <tval> type typebase qualified_type
156 %type <tvec> nonempty_typelist
157 /* %type <bval> block */
159 /* Fancy type parsing. */
160 %type <voidval> func_mod direct_abs_decl abs_decl
162 %type <lval> array_mod
164 %token <typed_val_int> INT
165 %token <typed_val_float> FLOAT
167 /* Both NAME and TYPENAME tokens represent symbols in the input,
168 and both convey their data as strings.
169 But a TYPENAME is a string that happens to be defined as a typedef
170 or builtin type name (such as int or char)
171 and a NAME is any other symbol.
172 Contexts where this distinction is not important can use the
173 nonterminal "name", which matches either NAME or TYPENAME. */
176 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
177 %token <tsym> TYPENAME
179 %type <ssym> name_not_typename
180 %type <tsym> typename
182 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
183 but which would parse as a valid number in the current input radix.
184 E.g. "c" when input_radix==16. Depending on the parse, it will be
185 turned into a name or into a number. */
187 %token <ssym> NAME_OR_INT
189 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
193 /* Special type cases, put in to allow the parser to distinguish different
195 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
197 %token <voidval> VARIABLE
199 %token <opcode> ASSIGN_MODIFY
208 %right '=' ASSIGN_MODIFY
216 %left '<' '>' LEQ GEQ
221 %right UNARY INCREMENT DECREMENT
222 %right ARROW '.' '[' '('
223 %token <ssym> BLOCKNAME
224 %token <bval> FILENAME
236 { write_exp_elt_opcode(OP_TYPE);
237 write_exp_elt_type($1);
238 write_exp_elt_opcode(OP_TYPE);}
241 /* Expressions, including the comma operator. */
244 { write_exp_elt_opcode (BINOP_COMMA); }
247 /* Expressions, not including the comma operator. */
248 exp : '*' exp %prec UNARY
249 { write_exp_elt_opcode (UNOP_IND); }
252 exp : '&' exp %prec UNARY
253 { write_exp_elt_opcode (UNOP_ADDR); }
256 exp : '-' exp %prec UNARY
257 { write_exp_elt_opcode (UNOP_NEG); }
260 exp : '+' exp %prec UNARY
261 { write_exp_elt_opcode (UNOP_PLUS); }
264 exp : '!' exp %prec UNARY
265 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
268 exp : '~' exp %prec UNARY
269 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
272 exp : INCREMENT exp %prec UNARY
273 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
276 exp : DECREMENT exp %prec UNARY
277 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
280 exp : exp INCREMENT %prec UNARY
281 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
284 exp : exp DECREMENT %prec UNARY
285 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
288 exp : SIZEOF exp %prec UNARY
289 { write_exp_elt_opcode (UNOP_SIZEOF); }
293 { write_exp_elt_opcode (STRUCTOP_PTR);
294 write_exp_string ($3);
295 write_exp_elt_opcode (STRUCTOP_PTR); }
298 exp : exp ARROW qualified_name
299 { /* exp->type::name becomes exp->*(&type::name) */
300 /* Note: this doesn't work if name is a
301 static member! FIXME */
302 write_exp_elt_opcode (UNOP_ADDR);
303 write_exp_elt_opcode (STRUCTOP_MPTR); }
306 exp : exp ARROW '*' exp
307 { write_exp_elt_opcode (STRUCTOP_MPTR); }
311 { write_exp_elt_opcode (STRUCTOP_STRUCT);
312 write_exp_string ($3);
313 write_exp_elt_opcode (STRUCTOP_STRUCT); }
316 exp : exp '.' qualified_name
317 { /* exp.type::name becomes exp.*(&type::name) */
318 /* Note: this doesn't work if name is a
319 static member! FIXME */
320 write_exp_elt_opcode (UNOP_ADDR);
321 write_exp_elt_opcode (STRUCTOP_MEMBER); }
324 exp : exp '.' '*' exp
325 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
328 exp : exp '[' exp1 ']'
329 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
333 /* This is to save the value of arglist_len
334 being accumulated by an outer function call. */
335 { start_arglist (); }
336 arglist ')' %prec ARROW
337 { write_exp_elt_opcode (OP_FUNCALL);
338 write_exp_elt_longcst ((LONGEST) end_arglist ());
339 write_exp_elt_opcode (OP_FUNCALL); }
343 { start_arglist (); }
353 arglist : arglist ',' exp %prec ABOVE_COMMA
358 { $$ = end_arglist () - 1; }
360 exp : lcurly arglist rcurly %prec ARROW
361 { write_exp_elt_opcode (OP_ARRAY);
362 write_exp_elt_longcst ((LONGEST) 0);
363 write_exp_elt_longcst ((LONGEST) $3);
364 write_exp_elt_opcode (OP_ARRAY); }
367 exp : lcurly type rcurly exp %prec UNARY
368 { write_exp_elt_opcode (UNOP_MEMVAL);
369 write_exp_elt_type ($2);
370 write_exp_elt_opcode (UNOP_MEMVAL); }
373 exp : '(' type ')' exp %prec UNARY
374 { write_exp_elt_opcode (UNOP_CAST);
375 write_exp_elt_type ($2);
376 write_exp_elt_opcode (UNOP_CAST); }
383 /* Binary operators in order of decreasing precedence. */
386 { write_exp_elt_opcode (BINOP_REPEAT); }
390 { write_exp_elt_opcode (BINOP_MUL); }
394 { write_exp_elt_opcode (BINOP_DIV); }
398 { write_exp_elt_opcode (BINOP_REM); }
402 { write_exp_elt_opcode (BINOP_ADD); }
406 { write_exp_elt_opcode (BINOP_SUB); }
410 { write_exp_elt_opcode (BINOP_LSH); }
414 { write_exp_elt_opcode (BINOP_RSH); }
418 { write_exp_elt_opcode (BINOP_EQUAL); }
421 exp : exp NOTEQUAL exp
422 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
426 { write_exp_elt_opcode (BINOP_LEQ); }
430 { write_exp_elt_opcode (BINOP_GEQ); }
434 { write_exp_elt_opcode (BINOP_LESS); }
438 { write_exp_elt_opcode (BINOP_GTR); }
442 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
446 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
450 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
454 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
458 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
461 exp : exp '?' exp ':' exp %prec '?'
462 { write_exp_elt_opcode (TERNOP_COND); }
466 { write_exp_elt_opcode (BINOP_ASSIGN); }
469 exp : exp ASSIGN_MODIFY exp
470 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
471 write_exp_elt_opcode ($2);
472 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
476 { write_exp_elt_opcode (OP_LONG);
477 write_exp_elt_type ($1.type);
478 write_exp_elt_longcst ((LONGEST)($1.val));
479 write_exp_elt_opcode (OP_LONG); }
484 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
485 write_exp_elt_opcode (OP_LONG);
486 write_exp_elt_type (val.typed_val_int.type);
487 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
488 write_exp_elt_opcode (OP_LONG);
494 { write_exp_elt_opcode (OP_DOUBLE);
495 write_exp_elt_type ($1.type);
496 write_exp_elt_dblcst ($1.dval);
497 write_exp_elt_opcode (OP_DOUBLE); }
504 /* Already written by write_dollar_variable. */
507 exp : SIZEOF '(' type ')' %prec UNARY
508 { write_exp_elt_opcode (OP_LONG);
509 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_int);
511 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
512 write_exp_elt_opcode (OP_LONG); }
516 { /* C strings are converted into array constants with
517 an explicit null byte added at the end. Thus
518 the array upper bound is the string length.
519 There is no such thing in C as a completely empty
521 char *sp = $1.ptr; int count = $1.length;
524 write_exp_elt_opcode (OP_LONG);
525 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_char);
526 write_exp_elt_longcst ((LONGEST)(*sp++));
527 write_exp_elt_opcode (OP_LONG);
529 write_exp_elt_opcode (OP_LONG);
530 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_char);
531 write_exp_elt_longcst ((LONGEST)'\0');
532 write_exp_elt_opcode (OP_LONG);
533 write_exp_elt_opcode (OP_ARRAY);
534 write_exp_elt_longcst ((LONGEST) 0);
535 write_exp_elt_longcst ((LONGEST) ($1.length));
536 write_exp_elt_opcode (OP_ARRAY); }
541 { write_exp_elt_opcode (OP_LONG);
542 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_bool);
543 write_exp_elt_longcst ((LONGEST) 1);
544 write_exp_elt_opcode (OP_LONG); }
548 { write_exp_elt_opcode (OP_LONG);
549 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_bool);
550 write_exp_elt_longcst ((LONGEST) 0);
551 write_exp_elt_opcode (OP_LONG); }
559 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
561 error ("No file or function \"%s\".",
562 copy_name ($1.stoken));
570 block : block COLONCOLON name
572 = lookup_symbol (copy_name ($3), $1,
573 VAR_DOMAIN, (int *) NULL,
574 (struct symtab **) NULL);
575 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
576 error ("No function \"%s\" in specified context.",
578 $$ = SYMBOL_BLOCK_VALUE (tem); }
581 variable: block COLONCOLON name
582 { struct symbol *sym;
583 sym = lookup_symbol (copy_name ($3), $1,
584 VAR_DOMAIN, (int *) NULL,
585 (struct symtab **) NULL);
587 error ("No symbol \"%s\" in specified context.",
590 write_exp_elt_opcode (OP_VAR_VALUE);
591 /* block_found is set by lookup_symbol. */
592 write_exp_elt_block (block_found);
593 write_exp_elt_sym (sym);
594 write_exp_elt_opcode (OP_VAR_VALUE); }
597 qualified_name: typebase COLONCOLON name
599 struct type *type = $1;
600 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
601 && TYPE_CODE (type) != TYPE_CODE_UNION
602 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
603 error ("`%s' is not defined as an aggregate type.",
606 write_exp_elt_opcode (OP_SCOPE);
607 write_exp_elt_type (type);
608 write_exp_string ($3);
609 write_exp_elt_opcode (OP_SCOPE);
611 | typebase COLONCOLON '~' name
613 struct type *type = $1;
614 struct stoken tmp_token;
615 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
616 && TYPE_CODE (type) != TYPE_CODE_UNION
617 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
618 error ("`%s' is not defined as an aggregate type.",
621 tmp_token.ptr = (char*) alloca ($4.length + 2);
622 tmp_token.length = $4.length + 1;
623 tmp_token.ptr[0] = '~';
624 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
625 tmp_token.ptr[tmp_token.length] = 0;
627 /* Check for valid destructor name. */
628 destructor_name_p (tmp_token.ptr, type);
629 write_exp_elt_opcode (OP_SCOPE);
630 write_exp_elt_type (type);
631 write_exp_string (tmp_token);
632 write_exp_elt_opcode (OP_SCOPE);
636 variable: qualified_name
639 char *name = copy_name ($2);
641 struct minimal_symbol *msymbol;
644 lookup_symbol (name, (const struct block *) NULL,
645 VAR_DOMAIN, (int *) NULL,
646 (struct symtab **) NULL);
649 write_exp_elt_opcode (OP_VAR_VALUE);
650 write_exp_elt_block (NULL);
651 write_exp_elt_sym (sym);
652 write_exp_elt_opcode (OP_VAR_VALUE);
656 msymbol = lookup_minimal_symbol (name, NULL, NULL);
659 write_exp_msymbol (msymbol,
660 lookup_function_type (builtin_type (current_gdbarch)->builtin_int),
661 builtin_type (current_gdbarch)->builtin_int);
664 if (!have_full_symbols () && !have_partial_symbols ())
665 error ("No symbol table is loaded. Use the \"file\" command.");
667 error ("No symbol \"%s\" in current context.", name);
671 variable: name_not_typename
672 { struct symbol *sym = $1.sym;
676 if (symbol_read_needs_frame (sym))
678 if (innermost_block == 0 ||
679 contained_in (block_found,
681 innermost_block = block_found;
684 write_exp_elt_opcode (OP_VAR_VALUE);
685 /* We want to use the selected frame, not
686 another more inner frame which happens to
687 be in the same block. */
688 write_exp_elt_block (NULL);
689 write_exp_elt_sym (sym);
690 write_exp_elt_opcode (OP_VAR_VALUE);
692 else if ($1.is_a_field_of_this)
694 /* C++: it hangs off of `this'. Must
695 not inadvertently convert from a method call
697 if (innermost_block == 0 ||
698 contained_in (block_found, innermost_block))
699 innermost_block = block_found;
700 write_exp_elt_opcode (OP_THIS);
701 write_exp_elt_opcode (OP_THIS);
702 write_exp_elt_opcode (STRUCTOP_PTR);
703 write_exp_string ($1.stoken);
704 write_exp_elt_opcode (STRUCTOP_PTR);
708 struct minimal_symbol *msymbol;
709 char *arg = copy_name ($1.stoken);
712 lookup_minimal_symbol (arg, NULL, NULL);
715 write_exp_msymbol (msymbol,
716 lookup_function_type (builtin_type (current_gdbarch)->builtin_int),
717 builtin_type (current_gdbarch)->builtin_int);
719 else if (!have_full_symbols () && !have_partial_symbols ())
720 error ("No symbol table is loaded. Use the \"file\" command.");
722 error ("No symbol \"%s\" in current context.",
723 copy_name ($1.stoken));
728 space_identifier : '@' NAME
729 { push_type_address_space (copy_name ($2.stoken));
730 push_type (tp_space_identifier);
734 const_or_volatile: const_or_volatile_noopt
738 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
741 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
742 | const_or_volatile_noopt
745 const_or_volatile_or_space_identifier:
746 const_or_volatile_or_space_identifier_noopt
751 { push_type (tp_pointer); $$ = 0; }
753 { push_type (tp_pointer); $$ = $2; }
755 { push_type (tp_reference); $$ = 0; }
757 { push_type (tp_reference); $$ = $2; }
761 direct_abs_decl: '(' abs_decl ')'
763 | direct_abs_decl array_mod
766 push_type (tp_array);
771 push_type (tp_array);
775 | direct_abs_decl func_mod
776 { push_type (tp_function); }
778 { push_type (tp_function); }
789 | '(' nonempty_typelist ')'
790 { free ($2); $$ = 0; }
793 /* We used to try to recognize pointer to member types here, but
794 that didn't work (shift/reduce conflicts meant that these rules never
795 got executed). The problem is that
796 int (foo::bar::baz::bizzle)
797 is a function type but
798 int (foo::bar::baz::bizzle::*)
799 is a pointer to member type. Stroustrup loses again! */
804 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
808 { $$ = builtin_type (current_gdbarch)->builtin_int; }
810 { $$ = builtin_type (current_gdbarch)->builtin_long; }
812 { $$ = builtin_type (current_gdbarch)->builtin_short; }
814 { $$ = builtin_type (current_gdbarch)->builtin_long; }
815 | LONG SIGNED_KEYWORD INT_KEYWORD
816 { $$ = builtin_type (current_gdbarch)->builtin_long; }
817 | LONG SIGNED_KEYWORD
818 { $$ = builtin_type (current_gdbarch)->builtin_long; }
819 | SIGNED_KEYWORD LONG INT_KEYWORD
820 { $$ = builtin_type (current_gdbarch)->builtin_long; }
821 | UNSIGNED LONG INT_KEYWORD
822 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
823 | LONG UNSIGNED INT_KEYWORD
824 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
826 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
828 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
829 | LONG LONG INT_KEYWORD
830 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
831 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
832 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
833 | LONG LONG SIGNED_KEYWORD
834 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
835 | SIGNED_KEYWORD LONG LONG
836 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
837 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
838 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
840 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
841 | UNSIGNED LONG LONG INT_KEYWORD
842 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
844 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
845 | LONG LONG UNSIGNED INT_KEYWORD
846 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
848 { $$ = builtin_type (current_gdbarch)->builtin_short; }
849 | SHORT SIGNED_KEYWORD INT_KEYWORD
850 { $$ = builtin_type (current_gdbarch)->builtin_short; }
851 | SHORT SIGNED_KEYWORD
852 { $$ = builtin_type (current_gdbarch)->builtin_short; }
853 | UNSIGNED SHORT INT_KEYWORD
854 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
856 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
857 | SHORT UNSIGNED INT_KEYWORD
858 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
860 { $$ = builtin_type (current_gdbarch)->builtin_double; }
861 | LONG DOUBLE_KEYWORD
862 { $$ = builtin_type (current_gdbarch)->builtin_long_double; }
864 { $$ = lookup_struct (copy_name ($2),
865 expression_context_block); }
867 { $$ = lookup_struct (copy_name ($2),
868 expression_context_block); }
870 { $$ = lookup_union (copy_name ($2),
871 expression_context_block); }
873 { $$ = lookup_enum (copy_name ($2),
874 expression_context_block); }
876 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
878 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_int; }
879 | SIGNED_KEYWORD typename
880 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
882 { $$ = builtin_type (current_gdbarch)->builtin_int; }
883 /* It appears that this rule for templates is never
884 reduced; template recognition happens by lookahead
885 in the token processing code in yylex. */
886 | TEMPLATE name '<' type '>'
887 { $$ = lookup_template_type(copy_name($2), $4,
888 expression_context_block);
890 | const_or_volatile_or_space_identifier_noopt typebase
891 { $$ = follow_types ($2); }
892 | typebase const_or_volatile_or_space_identifier_noopt
893 { $$ = follow_types ($1); }
897 /* FIXME: carlton/2003-09-25: This next bit leads to lots of
898 reduce-reduce conflicts, because the parser doesn't know whether or
899 not to use qualified_name or qualified_type: the rules are
900 identical. If the parser is parsing 'A::B::x', then, when it sees
901 the second '::', it knows that the expression to the left of it has
902 to be a type, so it uses qualified_type. But if it is parsing just
903 'A::B', then it doesn't have any way of knowing which rule to use,
904 so there's a reduce-reduce conflict; it picks qualified_name, since
905 that occurs earlier in this file than qualified_type.
907 There's no good way to fix this with the grammar as it stands; as
908 far as I can tell, some of the problems arise from ambiguities that
909 GDB introduces ('start' can be either an expression or a type), but
910 some of it is inherent to the nature of C++ (you want to treat the
911 input "(FOO)" fairly differently depending on whether FOO is an
912 expression or a type, and if FOO is a complex expression, this can
913 be hard to determine at the right time). Fortunately, it works
914 pretty well in most cases. For example, if you do 'ptype A::B',
915 where A::B is a nested type, then the parser will mistakenly
916 misidentify it as an expression; but evaluate_subexp will get
917 called with 'noside' set to EVAL_AVOID_SIDE_EFFECTS, and everything
918 will work out anyways. But there are situations where the parser
919 will get confused: the most common one that I've run into is when
924 where the parser doesn't realize that A::B has to be a type until
925 it hits the first right paren, at which point it's too late. (The
926 workaround is to type "print *(('A::B' *) x)" instead.) (And
927 another solution is to fix our symbol-handling code so that the
928 user never wants to type something like that in the first place,
929 because we get all the types right without the user's help!)
931 Perhaps we could fix this by making the lexer smarter. Some of
932 this functionality used to be in the lexer, but in a way that
933 worked even less well than the current solution: that attempt
934 involved having the parser sometimes handle '::' and having the
935 lexer sometimes handle it, and without a clear division of
936 responsibility, it quickly degenerated into a big mess. Probably
937 the eventual correct solution will give more of a role to the lexer
938 (ideally via code that is shared between the lexer and
939 decode_line_1), but I'm not holding my breath waiting for somebody
940 to get around to cleaning this up... */
942 qualified_type: typebase COLONCOLON name
944 struct type *type = $1;
945 struct type *new_type;
946 char *ncopy = alloca ($3.length + 1);
948 memcpy (ncopy, $3.ptr, $3.length);
949 ncopy[$3.length] = '\0';
951 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
952 && TYPE_CODE (type) != TYPE_CODE_UNION
953 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
954 error ("`%s' is not defined as an aggregate type.",
957 new_type = cp_lookup_nested_type (type, ncopy,
958 expression_context_block);
959 if (new_type == NULL)
960 error ("No type \"%s\" within class or namespace \"%s\".",
961 ncopy, TYPE_NAME (type));
970 $$.stoken.ptr = "int";
971 $$.stoken.length = 3;
972 $$.type = builtin_type (current_gdbarch)->builtin_int;
976 $$.stoken.ptr = "long";
977 $$.stoken.length = 4;
978 $$.type = builtin_type (current_gdbarch)->builtin_long;
982 $$.stoken.ptr = "short";
983 $$.stoken.length = 5;
984 $$.type = builtin_type (current_gdbarch)->builtin_short;
990 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
991 $<ivec>$[0] = 1; /* Number of types in vector */
994 | nonempty_typelist ',' type
995 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
996 $$ = (struct type **) realloc ((char *) $1, len);
997 $$[$<ivec>$[0]] = $3;
1002 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1003 { $$ = follow_types ($1); }
1006 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1007 | VOLATILE_KEYWORD CONST_KEYWORD
1010 const_or_volatile_noopt: const_and_volatile
1011 { push_type (tp_const);
1012 push_type (tp_volatile);
1015 { push_type (tp_const); }
1017 { push_type (tp_volatile); }
1020 name : NAME { $$ = $1.stoken; }
1021 | BLOCKNAME { $$ = $1.stoken; }
1022 | TYPENAME { $$ = $1.stoken; }
1023 | NAME_OR_INT { $$ = $1.stoken; }
1026 name_not_typename : NAME
1028 /* These would be useful if name_not_typename was useful, but it is just
1029 a fake for "variable", so these cause reduce/reduce conflicts because
1030 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1031 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1032 context where only a name could occur, this might be useful.
1039 /* Take care of parsing a number (anything that starts with a digit).
1040 Set yylval and return the token type; update lexptr.
1041 LEN is the number of characters in it. */
1043 /*** Needs some error checking for the float case ***/
1046 parse_number (p, len, parsed_float, putithere)
1052 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1053 here, and we do kind of silly things like cast to unsigned. */
1060 int base = input_radix;
1063 /* Number of "L" suffixes encountered. */
1066 /* We have found a "L" or "U" suffix. */
1067 int found_suffix = 0;
1070 struct type *signed_type;
1071 struct type *unsigned_type;
1075 /* It's a float since it contains a point or an exponent. */
1076 char *s = malloc (len);
1077 int num = 0; /* number of tokens scanned by scanf */
1078 char saved_char = p[len];
1080 p[len] = 0; /* null-terminate the token */
1081 num = sscanf (p, DOUBLEST_SCAN_FORMAT "%s",
1082 &putithere->typed_val_float.dval, s);
1083 p[len] = saved_char; /* restore the input stream */
1086 putithere->typed_val_float.type =
1087 builtin_type (current_gdbarch)->builtin_double;
1091 /* See if it has any float suffix: 'f' for float, 'l' for long
1093 if (!strcasecmp (s, "f"))
1094 putithere->typed_val_float.type =
1095 builtin_type (current_gdbarch)->builtin_float;
1096 else if (!strcasecmp (s, "l"))
1097 putithere->typed_val_float.type =
1098 builtin_type (current_gdbarch)->builtin_long_double;
1106 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1140 if (c >= 'A' && c <= 'Z')
1142 if (c != 'l' && c != 'u')
1144 if (c >= '0' && c <= '9')
1152 if (base > 10 && c >= 'a' && c <= 'f')
1156 n += i = c - 'a' + 10;
1169 return ERROR; /* Char not a digit */
1172 return ERROR; /* Invalid digit in this base */
1174 /* Portably test for overflow (only works for nonzero values, so make
1175 a second check for zero). FIXME: Can't we just make n and prevn
1176 unsigned and avoid this? */
1177 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1178 unsigned_p = 1; /* Try something unsigned */
1180 /* Portably test for unsigned overflow.
1181 FIXME: This check is wrong; for example it doesn't find overflow
1182 on 0x123456789 when LONGEST is 32 bits. */
1183 if (c != 'l' && c != 'u' && n != 0)
1185 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1186 error ("Numeric constant too large.");
1191 /* An integer constant is an int, a long, or a long long. An L
1192 suffix forces it to be long; an LL suffix forces it to be long
1193 long. If not forced to a larger size, it gets the first type of
1194 the above that it fits in. To figure out whether it fits, we
1195 shift it right and see whether anything remains. Note that we
1196 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1197 operation, because many compilers will warn about such a shift
1198 (which always produces a zero result). Sometimes TARGET_INT_BIT
1199 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
1200 the case where it is we just always shift the value more than
1201 once, with fewer bits each time. */
1203 un = (ULONGEST)n >> 2;
1205 && (un >> (TARGET_INT_BIT - 2)) == 0)
1207 high_bit = ((ULONGEST)1) << (TARGET_INT_BIT-1);
1209 /* A large decimal (not hex or octal) constant (between INT_MAX
1210 and UINT_MAX) is a long or unsigned long, according to ANSI,
1211 never an unsigned int, but this code treats it as unsigned
1212 int. This probably should be fixed. GCC gives a warning on
1215 unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_int;
1216 signed_type = builtin_type (current_gdbarch)->builtin_int;
1218 else if (long_p <= 1
1219 && (un >> (TARGET_LONG_BIT - 2)) == 0)
1221 high_bit = ((ULONGEST)1) << (TARGET_LONG_BIT-1);
1222 unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_long;
1223 signed_type = builtin_type (current_gdbarch)->builtin_long;
1228 if (sizeof (ULONGEST) * HOST_CHAR_BIT < TARGET_LONG_LONG_BIT)
1229 /* A long long does not fit in a LONGEST. */
1230 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1232 shift = (TARGET_LONG_LONG_BIT - 1);
1233 high_bit = (ULONGEST) 1 << shift;
1234 unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_long_long;
1235 signed_type = builtin_type (current_gdbarch)->builtin_long_long;
1238 putithere->typed_val_int.val = n;
1240 /* If the high bit of the worked out type is set then this number
1241 has to be unsigned. */
1243 if (unsigned_p || (n & high_bit))
1245 putithere->typed_val_int.type = unsigned_type;
1249 putithere->typed_val_int.type = signed_type;
1259 enum exp_opcode opcode;
1262 static const struct token tokentab3[] =
1264 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1265 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1268 static const struct token tokentab2[] =
1270 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1271 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1272 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1273 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1274 {"%=", ASSIGN_MODIFY, BINOP_REM},
1275 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1276 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1277 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1278 {"++", INCREMENT, BINOP_END},
1279 {"--", DECREMENT, BINOP_END},
1280 {"->", ARROW, BINOP_END},
1281 {"&&", ANDAND, BINOP_END},
1282 {"||", OROR, BINOP_END},
1283 {"::", COLONCOLON, BINOP_END},
1284 {"<<", LSH, BINOP_END},
1285 {">>", RSH, BINOP_END},
1286 {"==", EQUAL, BINOP_END},
1287 {"!=", NOTEQUAL, BINOP_END},
1288 {"<=", LEQ, BINOP_END},
1289 {">=", GEQ, BINOP_END}
1292 /* Read one token, getting characters through lexptr. */
1303 static char *tempbuf;
1304 static int tempbufsize;
1305 struct symbol * sym_class = NULL;
1306 char * token_string = NULL;
1307 int class_prefix = 0;
1312 /* Check if this is a macro invocation that we need to expand. */
1313 if (! scanning_macro_expansion ())
1315 char *expanded = macro_expand_next (&lexptr,
1316 expression_macro_lookup_func,
1317 expression_macro_lookup_baton);
1320 scan_macro_expansion (expanded);
1323 prev_lexptr = lexptr;
1327 /* See if it is a special token of length 3. */
1328 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1329 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
1332 yylval.opcode = tokentab3[i].opcode;
1333 return tokentab3[i].token;
1336 /* See if it is a special token of length 2. */
1337 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1338 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
1341 yylval.opcode = tokentab2[i].opcode;
1342 return tokentab2[i].token;
1345 switch (c = *tokstart)
1348 /* If we were just scanning the result of a macro expansion,
1349 then we need to resume scanning the original text.
1350 Otherwise, we were already scanning the original text, and
1351 we're really done. */
1352 if (scanning_macro_expansion ())
1354 finished_macro_expansion ();
1367 /* We either have a character constant ('0' or '\177' for example)
1368 or we have a quoted symbol reference ('foo(int,int)' in C++
1373 c = parse_escape (&lexptr);
1375 error ("Empty character constant.");
1376 else if (! host_char_to_target (c, &c))
1378 int toklen = lexptr - tokstart + 1;
1379 char *tok = alloca (toklen + 1);
1380 memcpy (tok, tokstart, toklen);
1382 error ("There is no character corresponding to %s in the target "
1383 "character set `%s'.", tok, target_charset ());
1386 yylval.typed_val_int.val = c;
1387 yylval.typed_val_int.type = builtin_type (current_gdbarch)->builtin_char;
1392 namelen = skip_quoted (tokstart) - tokstart;
1395 lexptr = tokstart + namelen;
1397 if (lexptr[-1] != '\'')
1398 error ("Unmatched single quote.");
1403 error ("Invalid character constant.");
1413 if (paren_depth == 0)
1420 if (comma_terminates
1422 && ! scanning_macro_expansion ())
1428 /* Might be a floating point number. */
1429 if (lexptr[1] < '0' || lexptr[1] > '9')
1430 goto symbol; /* Nope, must be a symbol. */
1431 /* FALL THRU into number case. */
1444 /* It's a number. */
1445 int got_dot = 0, got_e = 0, toktype;
1447 int hex = input_radix > 10;
1449 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1454 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1462 /* This test includes !hex because 'e' is a valid hex digit
1463 and thus does not indicate a floating point number when
1464 the radix is hex. */
1465 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1466 got_dot = got_e = 1;
1467 /* This test does not include !hex, because a '.' always indicates
1468 a decimal floating point number regardless of the radix. */
1469 else if (!got_dot && *p == '.')
1471 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1472 && (*p == '-' || *p == '+'))
1473 /* This is the sign of the exponent, not the end of the
1476 /* We will take any letters or digits. parse_number will
1477 complain if past the radix, or if L or U are not final. */
1478 else if ((*p < '0' || *p > '9')
1479 && ((*p < 'a' || *p > 'z')
1480 && (*p < 'A' || *p > 'Z')))
1483 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1484 if (toktype == ERROR)
1486 char *err_copy = (char *) alloca (p - tokstart + 1);
1488 memcpy (err_copy, tokstart, p - tokstart);
1489 err_copy[p - tokstart] = 0;
1490 error ("Invalid number \"%s\".", err_copy);
1522 /* Build the gdb internal form of the input string in tempbuf,
1523 translating any standard C escape forms seen. Note that the
1524 buffer is null byte terminated *only* for the convenience of
1525 debugging gdb itself and printing the buffer contents when
1526 the buffer contains no embedded nulls. Gdb does not depend
1527 upon the buffer being null byte terminated, it uses the length
1528 string instead. This allows gdb to handle C strings (as well
1529 as strings in other languages) with embedded null bytes */
1531 tokptr = ++tokstart;
1535 char *char_start_pos = tokptr;
1537 /* Grow the static temp buffer if necessary, including allocating
1538 the first one on demand. */
1539 if (tempbufindex + 1 >= tempbufsize)
1541 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1547 /* Do nothing, loop will terminate. */
1551 c = parse_escape (&tokptr);
1556 tempbuf[tempbufindex++] = c;
1560 if (! host_char_to_target (c, &c))
1562 int len = tokptr - char_start_pos;
1563 char *copy = alloca (len + 1);
1564 memcpy (copy, char_start_pos, len);
1567 error ("There is no character corresponding to `%s' "
1568 "in the target character set `%s'.",
1569 copy, target_charset ());
1571 tempbuf[tempbufindex++] = c;
1574 } while ((*tokptr != '"') && (*tokptr != '\0'));
1575 if (*tokptr++ != '"')
1577 error ("Unterminated string in expression.");
1579 tempbuf[tempbufindex] = '\0'; /* See note above */
1580 yylval.sval.ptr = tempbuf;
1581 yylval.sval.length = tempbufindex;
1586 if (!(c == '_' || c == '$'
1587 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1588 /* We must have come across a bad character (e.g. ';'). */
1589 error ("Invalid character '%c' in expression.", c);
1591 /* It's a name. See how long it is. */
1593 for (c = tokstart[namelen];
1594 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1595 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1597 /* Template parameter lists are part of the name.
1598 FIXME: This mishandles `print $a<4&&$a>3'. */
1602 /* Scan ahead to get rest of the template specification. Note
1603 that we look ahead only when the '<' adjoins non-whitespace
1604 characters; for comparison expressions, e.g. "a < b > c",
1605 there must be spaces before the '<', etc. */
1607 char * p = find_template_name_end (tokstart + namelen);
1609 namelen = p - tokstart;
1612 c = tokstart[++namelen];
1615 /* The token "if" terminates the expression and is NOT removed from
1616 the input stream. It doesn't count if it appears in the
1617 expansion of a macro. */
1619 && tokstart[0] == 'i'
1620 && tokstart[1] == 'f'
1621 && ! scanning_macro_expansion ())
1630 /* Catch specific keywords. Should be done with a data structure. */
1634 if (strncmp (tokstart, "unsigned", 8) == 0)
1636 if (current_language->la_language == language_cplus
1637 && strncmp (tokstart, "template", 8) == 0)
1639 if (strncmp (tokstart, "volatile", 8) == 0)
1640 return VOLATILE_KEYWORD;
1643 if (strncmp (tokstart, "struct", 6) == 0)
1645 if (strncmp (tokstart, "signed", 6) == 0)
1646 return SIGNED_KEYWORD;
1647 if (strncmp (tokstart, "sizeof", 6) == 0)
1649 if (strncmp (tokstart, "double", 6) == 0)
1650 return DOUBLE_KEYWORD;
1653 if (current_language->la_language == language_cplus)
1655 if (strncmp (tokstart, "false", 5) == 0)
1656 return FALSEKEYWORD;
1657 if (strncmp (tokstart, "class", 5) == 0)
1660 if (strncmp (tokstart, "union", 5) == 0)
1662 if (strncmp (tokstart, "short", 5) == 0)
1664 if (strncmp (tokstart, "const", 5) == 0)
1665 return CONST_KEYWORD;
1668 if (strncmp (tokstart, "enum", 4) == 0)
1670 if (strncmp (tokstart, "long", 4) == 0)
1672 if (current_language->la_language == language_cplus)
1674 if (strncmp (tokstart, "true", 4) == 0)
1679 if (strncmp (tokstart, "int", 3) == 0)
1686 yylval.sval.ptr = tokstart;
1687 yylval.sval.length = namelen;
1689 if (*tokstart == '$')
1691 write_dollar_variable (yylval.sval);
1695 /* Look ahead and see if we can consume more of the input
1696 string to get a reasonable class/namespace spec or a
1697 fully-qualified name. This is a kludge to get around the
1698 HP aCC compiler's generation of symbol names with embedded
1699 colons for namespace and nested classes. */
1701 /* NOTE: carlton/2003-09-24: I don't entirely understand the
1702 HP-specific code, either here or in linespec. Having said that,
1703 I suspect that we're actually moving towards their model: we want
1704 symbols whose names are fully qualified, which matches the
1705 description above. */
1708 /* Only do it if not inside single quotes */
1709 sym_class = parse_nested_classes_for_hpacc (yylval.sval.ptr, yylval.sval.length,
1710 &token_string, &class_prefix, &lexptr);
1713 /* Replace the current token with the bigger one we found */
1714 yylval.sval.ptr = token_string;
1715 yylval.sval.length = strlen (token_string);
1719 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1720 functions or symtabs. If this is not so, then ...
1721 Use token-type TYPENAME for symbols that happen to be defined
1722 currently as names of types; NAME for other symbols.
1723 The caller is not constrained to care about the distinction. */
1725 char *tmp = copy_name (yylval.sval);
1727 int is_a_field_of_this = 0;
1730 sym = lookup_symbol (tmp, expression_context_block,
1732 current_language->la_language == language_cplus
1733 ? &is_a_field_of_this : (int *) NULL,
1734 (struct symtab **) NULL);
1735 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1736 no psymtabs (coff, xcoff, or some future change to blow away the
1737 psymtabs once once symbols are read). */
1738 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1740 yylval.ssym.sym = sym;
1741 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1745 { /* See if it's a file name. */
1746 struct symtab *symtab;
1748 symtab = lookup_symtab (tmp);
1752 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1757 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1759 /* NOTE: carlton/2003-09-25: There used to be code here to
1760 handle nested types. It didn't work very well. See the
1761 comment before qualified_type for more info. */
1762 yylval.tsym.type = SYMBOL_TYPE (sym);
1766 = language_lookup_primitive_type_by_name (current_language,
1767 current_gdbarch, tmp);
1768 if (yylval.tsym.type != NULL)
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);