1 /* YACC parser for C expressions, for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Parse a C expression from text in a string,
23 and return the result as a struct expression pointer.
24 That structure contains arithmetic operations in reverse polish,
25 with constants represented by operations that are followed by special data.
26 See expression.h for the details of the format.
27 What is important here is that it can be built up sequentially
28 during the process of parsing; the lower levels of the tree always
29 come first in the result.
31 Note that malloc's and realloc's in this file are transformed to
32 xmalloc and xrealloc respectively by the same sed command in the
33 makefile that remaps any other malloc/realloc inserted by the parser
34 generator. Doing this with #defines and trying to control the interaction
35 with include files (<malloc.h> and <stdlib.h> for example) just became
36 too messy, particularly when such includes can be inserted at random
37 times by the parser generator. */
42 #include "gdb_string.h"
44 #include "expression.h"
46 #include "parser-defs.h"
49 #include "bfd.h" /* Required by objfiles.h. */
50 #include "symfile.h" /* Required by objfiles.h. */
51 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
54 #include "cp-support.h"
56 /* Flag indicating we're dealing with HP-compiled objects */
57 extern int hp_som_som_object_present;
59 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
60 as well as gratuitiously global symbol names, so we can have multiple
61 yacc generated parsers in gdb. Note that these are only the variables
62 produced by yacc. If other parser generators (bison, byacc, etc) produce
63 additional global names that conflict at link time, then those parser
64 generators need to be fixed instead of adding those names to this list. */
66 #define yymaxdepth c_maxdepth
67 #define yyparse c_parse
69 #define yyerror c_error
72 #define yydebug c_debug
81 #define yyerrflag c_errflag
82 #define yynerrs c_nerrs
87 #define yystate c_state
93 #define yyreds c_reds /* With YYDEBUG defined */
94 #define yytoks c_toks /* With YYDEBUG defined */
95 #define yyname c_name /* With YYDEBUG defined */
96 #define yyrule c_rule /* With YYDEBUG defined */
99 #define yydefred c_yydefred
100 #define yydgoto c_yydgoto
101 #define yysindex c_yysindex
102 #define yyrindex c_yyrindex
103 #define yygindex c_yygindex
104 #define yytable c_yytable
105 #define yycheck c_yycheck
108 #define YYDEBUG 1 /* Default to yydebug support */
111 #define YYFPRINTF parser_fprintf
115 static int yylex (void);
117 void yyerror (char *);
121 /* Although the yacc "value" of an expression is not used,
122 since the result is stored in the structure being created,
123 other node types do have values. */
140 struct symtoken ssym;
143 enum exp_opcode opcode;
144 struct internalvar *ivar;
151 /* YYSTYPE gets defined by %union */
152 static int parse_number (char *, int, int, YYSTYPE *);
155 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
157 %type <tval> type typebase qualified_type
158 %type <tvec> nonempty_typelist
159 /* %type <bval> block */
161 /* Fancy type parsing. */
162 %type <voidval> func_mod direct_abs_decl abs_decl
164 %type <lval> array_mod
166 %token <typed_val_int> INT
167 %token <typed_val_float> FLOAT
169 /* Both NAME and TYPENAME tokens represent symbols in the input,
170 and both convey their data as strings.
171 But a TYPENAME is a string that happens to be defined as a typedef
172 or builtin type name (such as int or char)
173 and a NAME is any other symbol.
174 Contexts where this distinction is not important can use the
175 nonterminal "name", which matches either NAME or TYPENAME. */
178 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
179 %token <tsym> TYPENAME
181 %type <ssym> name_not_typename
182 %type <tsym> typename
184 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
185 but which would parse as a valid number in the current input radix.
186 E.g. "c" when input_radix==16. Depending on the parse, it will be
187 turned into a name or into a number. */
189 %token <ssym> NAME_OR_INT
191 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
195 /* Special type cases, put in to allow the parser to distinguish different
197 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
199 %token <voidval> VARIABLE
201 %token <opcode> ASSIGN_MODIFY
210 %right '=' ASSIGN_MODIFY
218 %left '<' '>' LEQ GEQ
223 %right UNARY INCREMENT DECREMENT
224 %right ARROW '.' '[' '('
225 %token <ssym> BLOCKNAME
226 %token <bval> FILENAME
238 { write_exp_elt_opcode(OP_TYPE);
239 write_exp_elt_type($1);
240 write_exp_elt_opcode(OP_TYPE);}
243 /* Expressions, including the comma operator. */
246 { write_exp_elt_opcode (BINOP_COMMA); }
249 /* Expressions, not including the comma operator. */
250 exp : '*' exp %prec UNARY
251 { write_exp_elt_opcode (UNOP_IND); }
254 exp : '&' exp %prec UNARY
255 { write_exp_elt_opcode (UNOP_ADDR); }
258 exp : '-' exp %prec UNARY
259 { write_exp_elt_opcode (UNOP_NEG); }
262 exp : '!' exp %prec UNARY
263 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
266 exp : '~' exp %prec UNARY
267 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
270 exp : INCREMENT exp %prec UNARY
271 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
274 exp : DECREMENT exp %prec UNARY
275 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
278 exp : exp INCREMENT %prec UNARY
279 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
282 exp : exp DECREMENT %prec UNARY
283 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
286 exp : SIZEOF exp %prec UNARY
287 { write_exp_elt_opcode (UNOP_SIZEOF); }
291 { write_exp_elt_opcode (STRUCTOP_PTR);
292 write_exp_string ($3);
293 write_exp_elt_opcode (STRUCTOP_PTR); }
296 exp : exp ARROW qualified_name
297 { /* exp->type::name becomes exp->*(&type::name) */
298 /* Note: this doesn't work if name is a
299 static member! FIXME */
300 write_exp_elt_opcode (UNOP_ADDR);
301 write_exp_elt_opcode (STRUCTOP_MPTR); }
304 exp : exp ARROW '*' exp
305 { write_exp_elt_opcode (STRUCTOP_MPTR); }
309 { write_exp_elt_opcode (STRUCTOP_STRUCT);
310 write_exp_string ($3);
311 write_exp_elt_opcode (STRUCTOP_STRUCT); }
314 exp : exp '.' qualified_name
315 { /* exp.type::name becomes exp.*(&type::name) */
316 /* Note: this doesn't work if name is a
317 static member! FIXME */
318 write_exp_elt_opcode (UNOP_ADDR);
319 write_exp_elt_opcode (STRUCTOP_MEMBER); }
322 exp : exp '.' '*' exp
323 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
326 exp : exp '[' exp1 ']'
327 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
331 /* This is to save the value of arglist_len
332 being accumulated by an outer function call. */
333 { start_arglist (); }
334 arglist ')' %prec ARROW
335 { write_exp_elt_opcode (OP_FUNCALL);
336 write_exp_elt_longcst ((LONGEST) end_arglist ());
337 write_exp_elt_opcode (OP_FUNCALL); }
341 { start_arglist (); }
351 arglist : arglist ',' exp %prec ABOVE_COMMA
356 { $$ = end_arglist () - 1; }
358 exp : lcurly arglist rcurly %prec ARROW
359 { write_exp_elt_opcode (OP_ARRAY);
360 write_exp_elt_longcst ((LONGEST) 0);
361 write_exp_elt_longcst ((LONGEST) $3);
362 write_exp_elt_opcode (OP_ARRAY); }
365 exp : lcurly type rcurly exp %prec UNARY
366 { write_exp_elt_opcode (UNOP_MEMVAL);
367 write_exp_elt_type ($2);
368 write_exp_elt_opcode (UNOP_MEMVAL); }
371 exp : '(' type ')' exp %prec UNARY
372 { write_exp_elt_opcode (UNOP_CAST);
373 write_exp_elt_type ($2);
374 write_exp_elt_opcode (UNOP_CAST); }
381 /* Binary operators in order of decreasing precedence. */
384 { write_exp_elt_opcode (BINOP_REPEAT); }
388 { write_exp_elt_opcode (BINOP_MUL); }
392 { write_exp_elt_opcode (BINOP_DIV); }
396 { write_exp_elt_opcode (BINOP_REM); }
400 { write_exp_elt_opcode (BINOP_ADD); }
404 { write_exp_elt_opcode (BINOP_SUB); }
408 { write_exp_elt_opcode (BINOP_LSH); }
412 { write_exp_elt_opcode (BINOP_RSH); }
416 { write_exp_elt_opcode (BINOP_EQUAL); }
419 exp : exp NOTEQUAL exp
420 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
424 { write_exp_elt_opcode (BINOP_LEQ); }
428 { write_exp_elt_opcode (BINOP_GEQ); }
432 { write_exp_elt_opcode (BINOP_LESS); }
436 { write_exp_elt_opcode (BINOP_GTR); }
440 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
444 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
448 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
452 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
456 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
459 exp : exp '?' exp ':' exp %prec '?'
460 { write_exp_elt_opcode (TERNOP_COND); }
464 { write_exp_elt_opcode (BINOP_ASSIGN); }
467 exp : exp ASSIGN_MODIFY exp
468 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
469 write_exp_elt_opcode ($2);
470 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
474 { write_exp_elt_opcode (OP_LONG);
475 write_exp_elt_type ($1.type);
476 write_exp_elt_longcst ((LONGEST)($1.val));
477 write_exp_elt_opcode (OP_LONG); }
482 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
483 write_exp_elt_opcode (OP_LONG);
484 write_exp_elt_type (val.typed_val_int.type);
485 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
486 write_exp_elt_opcode (OP_LONG);
492 { write_exp_elt_opcode (OP_DOUBLE);
493 write_exp_elt_type ($1.type);
494 write_exp_elt_dblcst ($1.dval);
495 write_exp_elt_opcode (OP_DOUBLE); }
502 /* Already written by write_dollar_variable. */
505 exp : SIZEOF '(' type ')' %prec UNARY
506 { write_exp_elt_opcode (OP_LONG);
507 write_exp_elt_type (builtin_type_int);
509 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
510 write_exp_elt_opcode (OP_LONG); }
514 { /* C strings are converted into array constants with
515 an explicit null byte added at the end. Thus
516 the array upper bound is the string length.
517 There is no such thing in C as a completely empty
519 char *sp = $1.ptr; int count = $1.length;
522 write_exp_elt_opcode (OP_LONG);
523 write_exp_elt_type (builtin_type_char);
524 write_exp_elt_longcst ((LONGEST)(*sp++));
525 write_exp_elt_opcode (OP_LONG);
527 write_exp_elt_opcode (OP_LONG);
528 write_exp_elt_type (builtin_type_char);
529 write_exp_elt_longcst ((LONGEST)'\0');
530 write_exp_elt_opcode (OP_LONG);
531 write_exp_elt_opcode (OP_ARRAY);
532 write_exp_elt_longcst ((LONGEST) 0);
533 write_exp_elt_longcst ((LONGEST) ($1.length));
534 write_exp_elt_opcode (OP_ARRAY); }
539 { write_exp_elt_opcode (OP_LONG);
540 write_exp_elt_type (builtin_type_bool);
541 write_exp_elt_longcst ((LONGEST) 1);
542 write_exp_elt_opcode (OP_LONG); }
546 { write_exp_elt_opcode (OP_LONG);
547 write_exp_elt_type (builtin_type_bool);
548 write_exp_elt_longcst ((LONGEST) 0);
549 write_exp_elt_opcode (OP_LONG); }
557 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
559 error ("No file or function \"%s\".",
560 copy_name ($1.stoken));
568 block : block COLONCOLON name
570 = lookup_symbol (copy_name ($3), $1,
571 VAR_DOMAIN, (int *) NULL,
572 (struct symtab **) NULL);
573 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
574 error ("No function \"%s\" in specified context.",
576 $$ = SYMBOL_BLOCK_VALUE (tem); }
579 variable: block COLONCOLON name
580 { struct symbol *sym;
581 sym = lookup_symbol (copy_name ($3), $1,
582 VAR_DOMAIN, (int *) NULL,
583 (struct symtab **) NULL);
585 error ("No symbol \"%s\" in specified context.",
588 write_exp_elt_opcode (OP_VAR_VALUE);
589 /* block_found is set by lookup_symbol. */
590 write_exp_elt_block (block_found);
591 write_exp_elt_sym (sym);
592 write_exp_elt_opcode (OP_VAR_VALUE); }
595 qualified_name: typebase COLONCOLON name
597 struct type *type = $1;
598 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
599 && TYPE_CODE (type) != TYPE_CODE_UNION
600 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
601 error ("`%s' is not defined as an aggregate type.",
604 write_exp_elt_opcode (OP_SCOPE);
605 write_exp_elt_type (type);
606 write_exp_string ($3);
607 write_exp_elt_opcode (OP_SCOPE);
609 | typebase COLONCOLON '~' name
611 struct type *type = $1;
612 struct stoken tmp_token;
613 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
614 && TYPE_CODE (type) != TYPE_CODE_UNION
615 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
616 error ("`%s' is not defined as an aggregate type.",
619 tmp_token.ptr = (char*) alloca ($4.length + 2);
620 tmp_token.length = $4.length + 1;
621 tmp_token.ptr[0] = '~';
622 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
623 tmp_token.ptr[tmp_token.length] = 0;
625 /* Check for valid destructor name. */
626 destructor_name_p (tmp_token.ptr, type);
627 write_exp_elt_opcode (OP_SCOPE);
628 write_exp_elt_type (type);
629 write_exp_string (tmp_token);
630 write_exp_elt_opcode (OP_SCOPE);
634 variable: qualified_name
637 char *name = copy_name ($2);
639 struct minimal_symbol *msymbol;
642 lookup_symbol (name, (const struct block *) NULL,
643 VAR_DOMAIN, (int *) NULL,
644 (struct symtab **) NULL);
647 write_exp_elt_opcode (OP_VAR_VALUE);
648 write_exp_elt_block (NULL);
649 write_exp_elt_sym (sym);
650 write_exp_elt_opcode (OP_VAR_VALUE);
654 msymbol = lookup_minimal_symbol (name, NULL, NULL);
657 write_exp_msymbol (msymbol,
658 lookup_function_type (builtin_type_int),
662 if (!have_full_symbols () && !have_partial_symbols ())
663 error ("No symbol table is loaded. Use the \"file\" command.");
665 error ("No symbol \"%s\" in current context.", name);
669 variable: name_not_typename
670 { struct symbol *sym = $1.sym;
674 if (symbol_read_needs_frame (sym))
676 if (innermost_block == 0 ||
677 contained_in (block_found,
679 innermost_block = block_found;
682 write_exp_elt_opcode (OP_VAR_VALUE);
683 /* We want to use the selected frame, not
684 another more inner frame which happens to
685 be in the same block. */
686 write_exp_elt_block (NULL);
687 write_exp_elt_sym (sym);
688 write_exp_elt_opcode (OP_VAR_VALUE);
690 else if ($1.is_a_field_of_this)
692 /* C++: it hangs off of `this'. Must
693 not inadvertently convert from a method call
695 if (innermost_block == 0 ||
696 contained_in (block_found, innermost_block))
697 innermost_block = block_found;
698 write_exp_elt_opcode (OP_THIS);
699 write_exp_elt_opcode (OP_THIS);
700 write_exp_elt_opcode (STRUCTOP_PTR);
701 write_exp_string ($1.stoken);
702 write_exp_elt_opcode (STRUCTOP_PTR);
706 struct minimal_symbol *msymbol;
707 char *arg = copy_name ($1.stoken);
710 lookup_minimal_symbol (arg, NULL, NULL);
713 write_exp_msymbol (msymbol,
714 lookup_function_type (builtin_type_int),
717 else if (!have_full_symbols () && !have_partial_symbols ())
718 error ("No symbol table is loaded. Use the \"file\" command.");
720 error ("No symbol \"%s\" in current context.",
721 copy_name ($1.stoken));
726 space_identifier : '@' NAME
727 { push_type_address_space (copy_name ($2.stoken));
728 push_type (tp_space_identifier);
732 const_or_volatile: const_or_volatile_noopt
736 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
739 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
740 | const_or_volatile_noopt
743 const_or_volatile_or_space_identifier:
744 const_or_volatile_or_space_identifier_noopt
749 { push_type (tp_pointer); $$ = 0; }
751 { push_type (tp_pointer); $$ = $2; }
753 { push_type (tp_reference); $$ = 0; }
755 { push_type (tp_reference); $$ = $2; }
759 direct_abs_decl: '(' abs_decl ')'
761 | direct_abs_decl array_mod
764 push_type (tp_array);
769 push_type (tp_array);
773 | direct_abs_decl func_mod
774 { push_type (tp_function); }
776 { push_type (tp_function); }
787 | '(' nonempty_typelist ')'
788 { free ($2); $$ = 0; }
791 /* We used to try to recognize more pointer to member types here, but
792 that didn't work (shift/reduce conflicts meant that these rules never
793 got executed). The problem is that
794 int (foo::bar::baz::bizzle)
795 is a function type but
796 int (foo::bar::baz::bizzle::*)
797 is a pointer to member type. Stroustrup loses again! */
800 | typebase COLONCOLON '*'
801 { $$ = lookup_member_type (builtin_type_int, $1); }
804 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
808 { $$ = builtin_type_int; }
810 { $$ = builtin_type_long; }
812 { $$ = builtin_type_short; }
814 { $$ = builtin_type_long; }
815 | LONG SIGNED_KEYWORD INT_KEYWORD
816 { $$ = builtin_type_long; }
817 | LONG SIGNED_KEYWORD
818 { $$ = builtin_type_long; }
819 | SIGNED_KEYWORD LONG INT_KEYWORD
820 { $$ = builtin_type_long; }
821 | UNSIGNED LONG INT_KEYWORD
822 { $$ = builtin_type_unsigned_long; }
823 | LONG UNSIGNED INT_KEYWORD
824 { $$ = builtin_type_unsigned_long; }
826 { $$ = builtin_type_unsigned_long; }
828 { $$ = builtin_type_long_long; }
829 | LONG LONG INT_KEYWORD
830 { $$ = builtin_type_long_long; }
831 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
832 { $$ = builtin_type_long_long; }
833 | LONG LONG SIGNED_KEYWORD
834 { $$ = builtin_type_long_long; }
835 | SIGNED_KEYWORD LONG LONG
836 { $$ = builtin_type_long_long; }
837 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
838 { $$ = builtin_type_long_long; }
840 { $$ = builtin_type_unsigned_long_long; }
841 | UNSIGNED LONG LONG INT_KEYWORD
842 { $$ = builtin_type_unsigned_long_long; }
844 { $$ = builtin_type_unsigned_long_long; }
845 | LONG LONG UNSIGNED INT_KEYWORD
846 { $$ = builtin_type_unsigned_long_long; }
848 { $$ = builtin_type_short; }
849 | SHORT SIGNED_KEYWORD INT_KEYWORD
850 { $$ = builtin_type_short; }
851 | SHORT SIGNED_KEYWORD
852 { $$ = builtin_type_short; }
853 | UNSIGNED SHORT INT_KEYWORD
854 { $$ = builtin_type_unsigned_short; }
856 { $$ = builtin_type_unsigned_short; }
857 | SHORT UNSIGNED INT_KEYWORD
858 { $$ = builtin_type_unsigned_short; }
860 { $$ = builtin_type_double; }
861 | LONG DOUBLE_KEYWORD
862 { $$ = builtin_type_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_unsigned_int; }
879 | SIGNED_KEYWORD typename
880 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
882 { $$ = builtin_type_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 /* FIXME: carlton/2003-09-25: Currently, the only qualified type
943 symbols that we generate are nested namespaces. Next on my TODO
944 list is to generate all nested type names properly (or at least as
945 well as possible, assuming that we're using DWARF-2). */
947 qualified_type: typebase COLONCOLON name
949 struct type *type = $1;
950 struct type *new_type;
951 char *ncopy = alloca ($3.length + 1);
953 memcpy (ncopy, $3.ptr, $3.length);
954 ncopy[$3.length] = '\0';
956 if (TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
957 error ("`%s' is not defined as a namespace.",
960 new_type = cp_lookup_nested_type (type, ncopy,
961 expression_context_block);
962 if (new_type == NULL)
963 error ("No type \"%s\" in namespace \"%s\".",
964 ncopy, TYPE_NAME (type));
973 $$.stoken.ptr = "int";
974 $$.stoken.length = 3;
975 $$.type = builtin_type_int;
979 $$.stoken.ptr = "long";
980 $$.stoken.length = 4;
981 $$.type = builtin_type_long;
985 $$.stoken.ptr = "short";
986 $$.stoken.length = 5;
987 $$.type = builtin_type_short;
993 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
994 $<ivec>$[0] = 1; /* Number of types in vector */
997 | nonempty_typelist ',' type
998 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
999 $$ = (struct type **) realloc ((char *) $1, len);
1000 $$[$<ivec>$[0]] = $3;
1005 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1006 { $$ = follow_types ($1); }
1009 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1010 | VOLATILE_KEYWORD CONST_KEYWORD
1013 const_or_volatile_noopt: const_and_volatile
1014 { push_type (tp_const);
1015 push_type (tp_volatile);
1018 { push_type (tp_const); }
1020 { push_type (tp_volatile); }
1023 name : NAME { $$ = $1.stoken; }
1024 | BLOCKNAME { $$ = $1.stoken; }
1025 | TYPENAME { $$ = $1.stoken; }
1026 | NAME_OR_INT { $$ = $1.stoken; }
1029 name_not_typename : NAME
1031 /* These would be useful if name_not_typename was useful, but it is just
1032 a fake for "variable", so these cause reduce/reduce conflicts because
1033 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1034 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1035 context where only a name could occur, this might be useful.
1042 /* Take care of parsing a number (anything that starts with a digit).
1043 Set yylval and return the token type; update lexptr.
1044 LEN is the number of characters in it. */
1046 /*** Needs some error checking for the float case ***/
1049 parse_number (p, len, parsed_float, putithere)
1055 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1056 here, and we do kind of silly things like cast to unsigned. */
1063 int base = input_radix;
1066 /* Number of "L" suffixes encountered. */
1069 /* We have found a "L" or "U" suffix. */
1070 int found_suffix = 0;
1073 struct type *signed_type;
1074 struct type *unsigned_type;
1078 /* It's a float since it contains a point or an exponent. */
1080 int num = 0; /* number of tokens scanned by scanf */
1081 char saved_char = p[len];
1083 p[len] = 0; /* null-terminate the token */
1084 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
1085 num = sscanf (p, "%g%c", (float *) &putithere->typed_val_float.dval,&c);
1086 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
1087 num = sscanf (p, "%lg%c", (double *) &putithere->typed_val_float.dval,&c);
1090 #ifdef SCANF_HAS_LONG_DOUBLE
1091 num = sscanf (p, "%Lg%c", &putithere->typed_val_float.dval,&c);
1093 /* Scan it into a double, then assign it to the long double.
1094 This at least wins with values representable in the range
1097 num = sscanf (p, "%lg%c", &temp,&c);
1098 putithere->typed_val_float.dval = temp;
1101 p[len] = saved_char; /* restore the input stream */
1102 if (num != 1) /* check scanf found ONLY a float ... */
1104 /* See if it has `f' or `l' suffix (float or long double). */
1106 c = tolower (p[len - 1]);
1109 putithere->typed_val_float.type = builtin_type_float;
1111 putithere->typed_val_float.type = builtin_type_long_double;
1112 else if (isdigit (c) || c == '.')
1113 putithere->typed_val_float.type = builtin_type_double;
1120 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1154 if (c >= 'A' && c <= 'Z')
1156 if (c != 'l' && c != 'u')
1158 if (c >= '0' && c <= '9')
1166 if (base > 10 && c >= 'a' && c <= 'f')
1170 n += i = c - 'a' + 10;
1183 return ERROR; /* Char not a digit */
1186 return ERROR; /* Invalid digit in this base */
1188 /* Portably test for overflow (only works for nonzero values, so make
1189 a second check for zero). FIXME: Can't we just make n and prevn
1190 unsigned and avoid this? */
1191 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1192 unsigned_p = 1; /* Try something unsigned */
1194 /* Portably test for unsigned overflow.
1195 FIXME: This check is wrong; for example it doesn't find overflow
1196 on 0x123456789 when LONGEST is 32 bits. */
1197 if (c != 'l' && c != 'u' && n != 0)
1199 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1200 error ("Numeric constant too large.");
1205 /* An integer constant is an int, a long, or a long long. An L
1206 suffix forces it to be long; an LL suffix forces it to be long
1207 long. If not forced to a larger size, it gets the first type of
1208 the above that it fits in. To figure out whether it fits, we
1209 shift it right and see whether anything remains. Note that we
1210 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1211 operation, because many compilers will warn about such a shift
1212 (which always produces a zero result). Sometimes TARGET_INT_BIT
1213 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
1214 the case where it is we just always shift the value more than
1215 once, with fewer bits each time. */
1217 un = (ULONGEST)n >> 2;
1219 && (un >> (TARGET_INT_BIT - 2)) == 0)
1221 high_bit = ((ULONGEST)1) << (TARGET_INT_BIT-1);
1223 /* A large decimal (not hex or octal) constant (between INT_MAX
1224 and UINT_MAX) is a long or unsigned long, according to ANSI,
1225 never an unsigned int, but this code treats it as unsigned
1226 int. This probably should be fixed. GCC gives a warning on
1229 unsigned_type = builtin_type_unsigned_int;
1230 signed_type = builtin_type_int;
1232 else if (long_p <= 1
1233 && (un >> (TARGET_LONG_BIT - 2)) == 0)
1235 high_bit = ((ULONGEST)1) << (TARGET_LONG_BIT-1);
1236 unsigned_type = builtin_type_unsigned_long;
1237 signed_type = builtin_type_long;
1242 if (sizeof (ULONGEST) * HOST_CHAR_BIT < TARGET_LONG_LONG_BIT)
1243 /* A long long does not fit in a LONGEST. */
1244 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1246 shift = (TARGET_LONG_LONG_BIT - 1);
1247 high_bit = (ULONGEST) 1 << shift;
1248 unsigned_type = builtin_type_unsigned_long_long;
1249 signed_type = builtin_type_long_long;
1252 putithere->typed_val_int.val = n;
1254 /* If the high bit of the worked out type is set then this number
1255 has to be unsigned. */
1257 if (unsigned_p || (n & high_bit))
1259 putithere->typed_val_int.type = unsigned_type;
1263 putithere->typed_val_int.type = signed_type;
1273 enum exp_opcode opcode;
1276 static const struct token tokentab3[] =
1278 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1279 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1282 static const struct token tokentab2[] =
1284 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1285 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1286 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1287 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1288 {"%=", ASSIGN_MODIFY, BINOP_REM},
1289 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1290 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1291 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1292 {"++", INCREMENT, BINOP_END},
1293 {"--", DECREMENT, BINOP_END},
1294 {"->", ARROW, BINOP_END},
1295 {"&&", ANDAND, BINOP_END},
1296 {"||", OROR, BINOP_END},
1297 {"::", COLONCOLON, BINOP_END},
1298 {"<<", LSH, BINOP_END},
1299 {">>", RSH, BINOP_END},
1300 {"==", EQUAL, BINOP_END},
1301 {"!=", NOTEQUAL, BINOP_END},
1302 {"<=", LEQ, BINOP_END},
1303 {">=", GEQ, BINOP_END}
1306 /* Read one token, getting characters through lexptr. */
1317 static char *tempbuf;
1318 static int tempbufsize;
1319 struct symbol * sym_class = NULL;
1320 char * token_string = NULL;
1321 int class_prefix = 0;
1326 /* Check if this is a macro invocation that we need to expand. */
1327 if (! scanning_macro_expansion ())
1329 char *expanded = macro_expand_next (&lexptr,
1330 expression_macro_lookup_func,
1331 expression_macro_lookup_baton);
1334 scan_macro_expansion (expanded);
1337 prev_lexptr = lexptr;
1341 /* See if it is a special token of length 3. */
1342 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1343 if (STREQN (tokstart, tokentab3[i].operator, 3))
1346 yylval.opcode = tokentab3[i].opcode;
1347 return tokentab3[i].token;
1350 /* See if it is a special token of length 2. */
1351 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1352 if (STREQN (tokstart, tokentab2[i].operator, 2))
1355 yylval.opcode = tokentab2[i].opcode;
1356 return tokentab2[i].token;
1359 switch (c = *tokstart)
1362 /* If we were just scanning the result of a macro expansion,
1363 then we need to resume scanning the original text.
1364 Otherwise, we were already scanning the original text, and
1365 we're really done. */
1366 if (scanning_macro_expansion ())
1368 finished_macro_expansion ();
1381 /* We either have a character constant ('0' or '\177' for example)
1382 or we have a quoted symbol reference ('foo(int,int)' in C++
1387 c = parse_escape (&lexptr);
1389 error ("Empty character constant.");
1390 else if (! host_char_to_target (c, &c))
1392 int toklen = lexptr - tokstart + 1;
1393 char *tok = alloca (toklen + 1);
1394 memcpy (tok, tokstart, toklen);
1396 error ("There is no character corresponding to %s in the target "
1397 "character set `%s'.", tok, target_charset ());
1400 yylval.typed_val_int.val = c;
1401 yylval.typed_val_int.type = builtin_type_char;
1406 namelen = skip_quoted (tokstart) - tokstart;
1409 lexptr = tokstart + namelen;
1411 if (lexptr[-1] != '\'')
1412 error ("Unmatched single quote.");
1417 error ("Invalid character constant.");
1427 if (paren_depth == 0)
1434 if (comma_terminates
1436 && ! scanning_macro_expansion ())
1442 /* Might be a floating point number. */
1443 if (lexptr[1] < '0' || lexptr[1] > '9')
1444 goto symbol; /* Nope, must be a symbol. */
1445 /* FALL THRU into number case. */
1458 /* It's a number. */
1459 int got_dot = 0, got_e = 0, toktype;
1461 int hex = input_radix > 10;
1463 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1468 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1476 /* This test includes !hex because 'e' is a valid hex digit
1477 and thus does not indicate a floating point number when
1478 the radix is hex. */
1479 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1480 got_dot = got_e = 1;
1481 /* This test does not include !hex, because a '.' always indicates
1482 a decimal floating point number regardless of the radix. */
1483 else if (!got_dot && *p == '.')
1485 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1486 && (*p == '-' || *p == '+'))
1487 /* This is the sign of the exponent, not the end of the
1490 /* We will take any letters or digits. parse_number will
1491 complain if past the radix, or if L or U are not final. */
1492 else if ((*p < '0' || *p > '9')
1493 && ((*p < 'a' || *p > 'z')
1494 && (*p < 'A' || *p > 'Z')))
1497 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1498 if (toktype == ERROR)
1500 char *err_copy = (char *) alloca (p - tokstart + 1);
1502 memcpy (err_copy, tokstart, p - tokstart);
1503 err_copy[p - tokstart] = 0;
1504 error ("Invalid number \"%s\".", err_copy);
1536 /* Build the gdb internal form of the input string in tempbuf,
1537 translating any standard C escape forms seen. Note that the
1538 buffer is null byte terminated *only* for the convenience of
1539 debugging gdb itself and printing the buffer contents when
1540 the buffer contains no embedded nulls. Gdb does not depend
1541 upon the buffer being null byte terminated, it uses the length
1542 string instead. This allows gdb to handle C strings (as well
1543 as strings in other languages) with embedded null bytes */
1545 tokptr = ++tokstart;
1549 char *char_start_pos = tokptr;
1551 /* Grow the static temp buffer if necessary, including allocating
1552 the first one on demand. */
1553 if (tempbufindex + 1 >= tempbufsize)
1555 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1561 /* Do nothing, loop will terminate. */
1565 c = parse_escape (&tokptr);
1570 tempbuf[tempbufindex++] = c;
1574 if (! host_char_to_target (c, &c))
1576 int len = tokptr - char_start_pos;
1577 char *copy = alloca (len + 1);
1578 memcpy (copy, char_start_pos, len);
1581 error ("There is no character corresponding to `%s' "
1582 "in the target character set `%s'.",
1583 copy, target_charset ());
1585 tempbuf[tempbufindex++] = c;
1588 } while ((*tokptr != '"') && (*tokptr != '\0'));
1589 if (*tokptr++ != '"')
1591 error ("Unterminated string in expression.");
1593 tempbuf[tempbufindex] = '\0'; /* See note above */
1594 yylval.sval.ptr = tempbuf;
1595 yylval.sval.length = tempbufindex;
1600 if (!(c == '_' || c == '$'
1601 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1602 /* We must have come across a bad character (e.g. ';'). */
1603 error ("Invalid character '%c' in expression.", c);
1605 /* It's a name. See how long it is. */
1607 for (c = tokstart[namelen];
1608 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1609 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1611 /* Template parameter lists are part of the name.
1612 FIXME: This mishandles `print $a<4&&$a>3'. */
1616 /* Scan ahead to get rest of the template specification. Note
1617 that we look ahead only when the '<' adjoins non-whitespace
1618 characters; for comparison expressions, e.g. "a < b > c",
1619 there must be spaces before the '<', etc. */
1621 char * p = find_template_name_end (tokstart + namelen);
1623 namelen = p - tokstart;
1626 c = tokstart[++namelen];
1629 /* The token "if" terminates the expression and is NOT removed from
1630 the input stream. It doesn't count if it appears in the
1631 expansion of a macro. */
1633 && tokstart[0] == 'i'
1634 && tokstart[1] == 'f'
1635 && ! scanning_macro_expansion ())
1644 /* Catch specific keywords. Should be done with a data structure. */
1648 if (STREQN (tokstart, "unsigned", 8))
1650 if (current_language->la_language == language_cplus
1651 && STREQN (tokstart, "template", 8))
1653 if (STREQN (tokstart, "volatile", 8))
1654 return VOLATILE_KEYWORD;
1657 if (STREQN (tokstart, "struct", 6))
1659 if (STREQN (tokstart, "signed", 6))
1660 return SIGNED_KEYWORD;
1661 if (STREQN (tokstart, "sizeof", 6))
1663 if (STREQN (tokstart, "double", 6))
1664 return DOUBLE_KEYWORD;
1667 if (current_language->la_language == language_cplus)
1669 if (STREQN (tokstart, "false", 5))
1670 return FALSEKEYWORD;
1671 if (STREQN (tokstart, "class", 5))
1674 if (STREQN (tokstart, "union", 5))
1676 if (STREQN (tokstart, "short", 5))
1678 if (STREQN (tokstart, "const", 5))
1679 return CONST_KEYWORD;
1682 if (STREQN (tokstart, "enum", 4))
1684 if (STREQN (tokstart, "long", 4))
1686 if (current_language->la_language == language_cplus)
1688 if (STREQN (tokstart, "true", 4))
1693 if (STREQN (tokstart, "int", 3))
1700 yylval.sval.ptr = tokstart;
1701 yylval.sval.length = namelen;
1703 if (*tokstart == '$')
1705 write_dollar_variable (yylval.sval);
1709 /* Look ahead and see if we can consume more of the input
1710 string to get a reasonable class/namespace spec or a
1711 fully-qualified name. This is a kludge to get around the
1712 HP aCC compiler's generation of symbol names with embedded
1713 colons for namespace and nested classes. */
1715 /* NOTE: carlton/2003-09-24: I don't entirely understand the
1716 HP-specific code, either here or in linespec. Having said that,
1717 I suspect that we're actually moving towards their model: we want
1718 symbols whose names are fully qualified, which matches the
1719 description above. */
1722 /* Only do it if not inside single quotes */
1723 sym_class = parse_nested_classes_for_hpacc (yylval.sval.ptr, yylval.sval.length,
1724 &token_string, &class_prefix, &lexptr);
1727 /* Replace the current token with the bigger one we found */
1728 yylval.sval.ptr = token_string;
1729 yylval.sval.length = strlen (token_string);
1733 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1734 functions or symtabs. If this is not so, then ...
1735 Use token-type TYPENAME for symbols that happen to be defined
1736 currently as names of types; NAME for other symbols.
1737 The caller is not constrained to care about the distinction. */
1739 char *tmp = copy_name (yylval.sval);
1741 int is_a_field_of_this = 0;
1744 sym = lookup_symbol (tmp, expression_context_block,
1746 current_language->la_language == language_cplus
1747 ? &is_a_field_of_this : (int *) NULL,
1748 (struct symtab **) NULL);
1749 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1750 no psymtabs (coff, xcoff, or some future change to blow away the
1751 psymtabs once once symbols are read). */
1752 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1754 yylval.ssym.sym = sym;
1755 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1759 { /* See if it's a file name. */
1760 struct symtab *symtab;
1762 symtab = lookup_symtab (tmp);
1766 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1771 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1773 /* NOTE: carlton/2003-09-25: There used to be code here to
1774 handle nested types. It didn't work very well. See the
1775 comment before qualified_type for more info. */
1776 yylval.tsym.type = SYMBOL_TYPE (sym);
1779 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1782 /* Input names that aren't symbols but ARE valid hex numbers,
1783 when the input radix permits them, can be names or numbers
1784 depending on the parse. Note we support radixes > 16 here. */
1786 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1787 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1789 YYSTYPE newlval; /* Its value is ignored. */
1790 hextype = parse_number (tokstart, namelen, 0, &newlval);
1793 yylval.ssym.sym = sym;
1794 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1799 /* Any other kind of symbol */
1800 yylval.ssym.sym = sym;
1801 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1811 lexptr = prev_lexptr;
1813 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);