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, 2009
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 3 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, see <http://www.gnu.org/licenses/>. */
21 /* Parse a C expression from text in a string,
22 and return the result as a struct expression pointer.
23 That structure contains arithmetic operations in reverse polish,
24 with constants represented by operations that are followed by special data.
25 See expression.h for the details of the format.
26 What is important here is that it can be built up sequentially
27 during the process of parsing; the lower levels of the tree always
28 come first in the result.
30 Note that malloc's and realloc's in this file are transformed to
31 xmalloc and xrealloc respectively by the same sed command in the
32 makefile that remaps any other malloc/realloc inserted by the parser
33 generator. Doing this with #defines and trying to control the interaction
34 with include files (<malloc.h> and <stdlib.h> for example) just became
35 too messy, particularly when such includes can be inserted at random
36 times by the parser generator. */
41 #include "gdb_string.h"
43 #include "expression.h"
45 #include "parser-defs.h"
48 #include "bfd.h" /* Required by objfiles.h. */
49 #include "symfile.h" /* Required by objfiles.h. */
50 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
53 #include "cp-support.h"
55 #include "gdb_assert.h"
56 #include "macroscope.h"
58 #define parse_type builtin_type (parse_gdbarch)
60 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
61 as well as gratuitiously global symbol names, so we can have multiple
62 yacc generated parsers in gdb. Note that these are only the variables
63 produced by yacc. If other parser generators (bison, byacc, etc) produce
64 additional global names that conflict at link time, then those parser
65 generators need to be fixed instead of adding those names to this list. */
67 #define yymaxdepth c_maxdepth
68 #define yyparse c_parse_internal
70 #define yyerror c_error
73 #define yydebug c_debug
82 #define yyerrflag c_errflag
83 #define yynerrs c_nerrs
88 #define yystate c_state
94 #define yyreds c_reds /* With YYDEBUG defined */
95 #define yytoks c_toks /* With YYDEBUG defined */
96 #define yyname c_name /* With YYDEBUG defined */
97 #define yyrule c_rule /* With YYDEBUG defined */
100 #define yydefred c_yydefred
101 #define yydgoto c_yydgoto
102 #define yysindex c_yysindex
103 #define yyrindex c_yyrindex
104 #define yygindex c_yygindex
105 #define yytable c_yytable
106 #define yycheck c_yycheck
109 #define YYDEBUG 1 /* Default to yydebug support */
112 #define YYFPRINTF parser_fprintf
116 static int yylex (void);
118 void yyerror (char *);
122 /* Although the yacc "value" of an expression is not used,
123 since the result is stored in the structure being created,
124 other node types do have values. */
140 } typed_val_decfloat;
144 struct typed_stoken tsval;
146 struct symtoken ssym;
149 enum exp_opcode opcode;
150 struct internalvar *ivar;
152 struct stoken_vector svec;
158 /* YYSTYPE gets defined by %union */
159 static int parse_number (char *, int, int, YYSTYPE *);
162 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
164 %type <tval> type typebase qualified_type
165 %type <tvec> nonempty_typelist
166 /* %type <bval> block */
168 /* Fancy type parsing. */
169 %type <voidval> func_mod direct_abs_decl abs_decl
171 %type <lval> array_mod
173 %token <typed_val_int> INT
174 %token <typed_val_float> FLOAT
175 %token <typed_val_decfloat> DECFLOAT
177 /* Both NAME and TYPENAME tokens represent symbols in the input,
178 and both convey their data as strings.
179 But a TYPENAME is a string that happens to be defined as a typedef
180 or builtin type name (such as int or char)
181 and a NAME is any other symbol.
182 Contexts where this distinction is not important can use the
183 nonterminal "name", which matches either NAME or TYPENAME. */
185 %token <tsval> STRING
187 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
188 %token <voidval> COMPLETE
189 %token <tsym> TYPENAME
191 %type <svec> string_exp
192 %type <ssym> name_not_typename
193 %type <tsym> typename
195 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
196 but which would parse as a valid number in the current input radix.
197 E.g. "c" when input_radix==16. Depending on the parse, it will be
198 turned into a name or into a number. */
200 %token <ssym> NAME_OR_INT
202 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
206 /* Special type cases, put in to allow the parser to distinguish different
208 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
210 %token <voidval> VARIABLE
212 %token <opcode> ASSIGN_MODIFY
221 %right '=' ASSIGN_MODIFY
229 %left '<' '>' LEQ GEQ
234 %right UNARY INCREMENT DECREMENT
235 %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
236 %token <ssym> BLOCKNAME
237 %token <bval> FILENAME
249 { write_exp_elt_opcode(OP_TYPE);
250 write_exp_elt_type($1);
251 write_exp_elt_opcode(OP_TYPE);}
254 /* Expressions, including the comma operator. */
257 { write_exp_elt_opcode (BINOP_COMMA); }
260 /* Expressions, not including the comma operator. */
261 exp : '*' exp %prec UNARY
262 { write_exp_elt_opcode (UNOP_IND); }
265 exp : '&' exp %prec UNARY
266 { write_exp_elt_opcode (UNOP_ADDR); }
269 exp : '-' exp %prec UNARY
270 { write_exp_elt_opcode (UNOP_NEG); }
273 exp : '+' exp %prec UNARY
274 { write_exp_elt_opcode (UNOP_PLUS); }
277 exp : '!' exp %prec UNARY
278 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
281 exp : '~' exp %prec UNARY
282 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
285 exp : INCREMENT exp %prec UNARY
286 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
289 exp : DECREMENT exp %prec UNARY
290 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
293 exp : exp INCREMENT %prec UNARY
294 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
297 exp : exp DECREMENT %prec UNARY
298 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
301 exp : SIZEOF exp %prec UNARY
302 { write_exp_elt_opcode (UNOP_SIZEOF); }
306 { write_exp_elt_opcode (STRUCTOP_PTR);
307 write_exp_string ($3);
308 write_exp_elt_opcode (STRUCTOP_PTR); }
311 exp : exp ARROW name COMPLETE
312 { mark_struct_expression ();
313 write_exp_elt_opcode (STRUCTOP_PTR);
314 write_exp_string ($3);
315 write_exp_elt_opcode (STRUCTOP_PTR); }
318 exp : exp ARROW COMPLETE
320 mark_struct_expression ();
321 write_exp_elt_opcode (STRUCTOP_PTR);
324 write_exp_string (s);
325 write_exp_elt_opcode (STRUCTOP_PTR); }
328 exp : exp ARROW qualified_name
329 { /* exp->type::name becomes exp->*(&type::name) */
330 /* Note: this doesn't work if name is a
331 static member! FIXME */
332 write_exp_elt_opcode (UNOP_ADDR);
333 write_exp_elt_opcode (STRUCTOP_MPTR); }
336 exp : exp ARROW_STAR exp
337 { write_exp_elt_opcode (STRUCTOP_MPTR); }
341 { write_exp_elt_opcode (STRUCTOP_STRUCT);
342 write_exp_string ($3);
343 write_exp_elt_opcode (STRUCTOP_STRUCT); }
346 exp : exp '.' name COMPLETE
347 { mark_struct_expression ();
348 write_exp_elt_opcode (STRUCTOP_STRUCT);
349 write_exp_string ($3);
350 write_exp_elt_opcode (STRUCTOP_STRUCT); }
353 exp : exp '.' COMPLETE
355 mark_struct_expression ();
356 write_exp_elt_opcode (STRUCTOP_STRUCT);
359 write_exp_string (s);
360 write_exp_elt_opcode (STRUCTOP_STRUCT); }
363 exp : exp '.' qualified_name
364 { /* exp.type::name becomes exp.*(&type::name) */
365 /* Note: this doesn't work if name is a
366 static member! FIXME */
367 write_exp_elt_opcode (UNOP_ADDR);
368 write_exp_elt_opcode (STRUCTOP_MEMBER); }
371 exp : exp DOT_STAR exp
372 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
375 exp : exp '[' exp1 ']'
376 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
380 /* This is to save the value of arglist_len
381 being accumulated by an outer function call. */
382 { start_arglist (); }
383 arglist ')' %prec ARROW
384 { write_exp_elt_opcode (OP_FUNCALL);
385 write_exp_elt_longcst ((LONGEST) end_arglist ());
386 write_exp_elt_opcode (OP_FUNCALL); }
390 { start_arglist (); }
400 arglist : arglist ',' exp %prec ABOVE_COMMA
405 { $$ = end_arglist () - 1; }
407 exp : lcurly arglist rcurly %prec ARROW
408 { write_exp_elt_opcode (OP_ARRAY);
409 write_exp_elt_longcst ((LONGEST) 0);
410 write_exp_elt_longcst ((LONGEST) $3);
411 write_exp_elt_opcode (OP_ARRAY); }
414 exp : lcurly type rcurly exp %prec UNARY
415 { write_exp_elt_opcode (UNOP_MEMVAL);
416 write_exp_elt_type ($2);
417 write_exp_elt_opcode (UNOP_MEMVAL); }
420 exp : '(' type ')' exp %prec UNARY
421 { write_exp_elt_opcode (UNOP_CAST);
422 write_exp_elt_type ($2);
423 write_exp_elt_opcode (UNOP_CAST); }
430 /* Binary operators in order of decreasing precedence. */
433 { write_exp_elt_opcode (BINOP_REPEAT); }
437 { write_exp_elt_opcode (BINOP_MUL); }
441 { write_exp_elt_opcode (BINOP_DIV); }
445 { write_exp_elt_opcode (BINOP_REM); }
449 { write_exp_elt_opcode (BINOP_ADD); }
453 { write_exp_elt_opcode (BINOP_SUB); }
457 { write_exp_elt_opcode (BINOP_LSH); }
461 { write_exp_elt_opcode (BINOP_RSH); }
465 { write_exp_elt_opcode (BINOP_EQUAL); }
468 exp : exp NOTEQUAL exp
469 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
473 { write_exp_elt_opcode (BINOP_LEQ); }
477 { write_exp_elt_opcode (BINOP_GEQ); }
481 { write_exp_elt_opcode (BINOP_LESS); }
485 { write_exp_elt_opcode (BINOP_GTR); }
489 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
493 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
497 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
501 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
505 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
508 exp : exp '?' exp ':' exp %prec '?'
509 { write_exp_elt_opcode (TERNOP_COND); }
513 { write_exp_elt_opcode (BINOP_ASSIGN); }
516 exp : exp ASSIGN_MODIFY exp
517 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
518 write_exp_elt_opcode ($2);
519 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
523 { write_exp_elt_opcode (OP_LONG);
524 write_exp_elt_type ($1.type);
525 write_exp_elt_longcst ((LONGEST)($1.val));
526 write_exp_elt_opcode (OP_LONG); }
531 struct stoken_vector vec;
534 write_exp_string_vector ($1.type, &vec);
540 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
541 write_exp_elt_opcode (OP_LONG);
542 write_exp_elt_type (val.typed_val_int.type);
543 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
544 write_exp_elt_opcode (OP_LONG);
550 { write_exp_elt_opcode (OP_DOUBLE);
551 write_exp_elt_type ($1.type);
552 write_exp_elt_dblcst ($1.dval);
553 write_exp_elt_opcode (OP_DOUBLE); }
557 { write_exp_elt_opcode (OP_DECFLOAT);
558 write_exp_elt_type ($1.type);
559 write_exp_elt_decfloatcst ($1.val);
560 write_exp_elt_opcode (OP_DECFLOAT); }
567 /* Already written by write_dollar_variable. */
570 exp : SIZEOF '(' type ')' %prec UNARY
571 { write_exp_elt_opcode (OP_LONG);
572 write_exp_elt_type (parse_type->builtin_int);
574 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
575 write_exp_elt_opcode (OP_LONG); }
581 /* We copy the string here, and not in the
582 lexer, to guarantee that we do not leak a
583 string. Note that we follow the
584 NUL-termination convention of the
586 struct typed_stoken *vec = XNEW (struct typed_stoken);
591 vec->length = $1.length;
592 vec->ptr = malloc ($1.length + 1);
593 memcpy (vec->ptr, $1.ptr, $1.length + 1);
598 /* Note that we NUL-terminate here, but just
602 $$.tokens = realloc ($$.tokens,
603 $$.len * sizeof (struct typed_stoken));
605 p = malloc ($2.length + 1);
606 memcpy (p, $2.ptr, $2.length + 1);
608 $$.tokens[$$.len - 1].type = $2.type;
609 $$.tokens[$$.len - 1].length = $2.length;
610 $$.tokens[$$.len - 1].ptr = p;
617 enum c_string_type type = C_STRING;
619 for (i = 0; i < $1.len; ++i)
621 switch ($1.tokens[i].type)
629 && type != $1.tokens[i].type)
630 error ("Undefined string concatenation.");
631 type = $1.tokens[i].type;
635 internal_error (__FILE__, __LINE__,
636 "unrecognized type in string concatenation");
640 write_exp_string_vector (type, &$1);
641 for (i = 0; i < $1.len; ++i)
642 free ($1.tokens[i].ptr);
649 { write_exp_elt_opcode (OP_LONG);
650 write_exp_elt_type (parse_type->builtin_bool);
651 write_exp_elt_longcst ((LONGEST) 1);
652 write_exp_elt_opcode (OP_LONG); }
656 { write_exp_elt_opcode (OP_LONG);
657 write_exp_elt_type (parse_type->builtin_bool);
658 write_exp_elt_longcst ((LONGEST) 0);
659 write_exp_elt_opcode (OP_LONG); }
667 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
669 error ("No file or function \"%s\".",
670 copy_name ($1.stoken));
678 block : block COLONCOLON name
680 = lookup_symbol (copy_name ($3), $1,
681 VAR_DOMAIN, (int *) NULL);
682 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
683 error ("No function \"%s\" in specified context.",
685 $$ = SYMBOL_BLOCK_VALUE (tem); }
688 variable: block COLONCOLON name
689 { struct symbol *sym;
690 sym = lookup_symbol (copy_name ($3), $1,
691 VAR_DOMAIN, (int *) NULL);
693 error ("No symbol \"%s\" in specified context.",
696 write_exp_elt_opcode (OP_VAR_VALUE);
697 /* block_found is set by lookup_symbol. */
698 write_exp_elt_block (block_found);
699 write_exp_elt_sym (sym);
700 write_exp_elt_opcode (OP_VAR_VALUE); }
703 qualified_name: typebase COLONCOLON name
705 struct type *type = $1;
706 CHECK_TYPEDEF (type);
707 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
708 && TYPE_CODE (type) != TYPE_CODE_UNION
709 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
710 error ("`%s' is not defined as an aggregate type.",
713 write_exp_elt_opcode (OP_SCOPE);
714 write_exp_elt_type (type);
715 write_exp_string ($3);
716 write_exp_elt_opcode (OP_SCOPE);
718 | typebase COLONCOLON '~' name
720 struct type *type = $1;
721 struct stoken tmp_token;
722 CHECK_TYPEDEF (type);
723 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
724 && TYPE_CODE (type) != TYPE_CODE_UNION
725 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
726 error ("`%s' is not defined as an aggregate type.",
729 tmp_token.ptr = (char*) alloca ($4.length + 2);
730 tmp_token.length = $4.length + 1;
731 tmp_token.ptr[0] = '~';
732 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
733 tmp_token.ptr[tmp_token.length] = 0;
735 /* Check for valid destructor name. */
736 destructor_name_p (tmp_token.ptr, type);
737 write_exp_elt_opcode (OP_SCOPE);
738 write_exp_elt_type (type);
739 write_exp_string (tmp_token);
740 write_exp_elt_opcode (OP_SCOPE);
744 variable: qualified_name
747 char *name = copy_name ($2);
749 struct minimal_symbol *msymbol;
752 lookup_symbol (name, (const struct block *) NULL,
753 VAR_DOMAIN, (int *) NULL);
756 write_exp_elt_opcode (OP_VAR_VALUE);
757 write_exp_elt_block (NULL);
758 write_exp_elt_sym (sym);
759 write_exp_elt_opcode (OP_VAR_VALUE);
763 msymbol = lookup_minimal_symbol (name, NULL, NULL);
765 write_exp_msymbol (msymbol);
766 else if (!have_full_symbols () && !have_partial_symbols ())
767 error ("No symbol table is loaded. Use the \"file\" command.");
769 error ("No symbol \"%s\" in current context.", name);
773 variable: name_not_typename
774 { struct symbol *sym = $1.sym;
778 if (symbol_read_needs_frame (sym))
780 if (innermost_block == 0
781 || contained_in (block_found,
783 innermost_block = block_found;
786 write_exp_elt_opcode (OP_VAR_VALUE);
787 /* We want to use the selected frame, not
788 another more inner frame which happens to
789 be in the same block. */
790 write_exp_elt_block (NULL);
791 write_exp_elt_sym (sym);
792 write_exp_elt_opcode (OP_VAR_VALUE);
794 else if ($1.is_a_field_of_this)
796 /* C++: it hangs off of `this'. Must
797 not inadvertently convert from a method call
799 if (innermost_block == 0
800 || contained_in (block_found,
802 innermost_block = block_found;
803 write_exp_elt_opcode (OP_THIS);
804 write_exp_elt_opcode (OP_THIS);
805 write_exp_elt_opcode (STRUCTOP_PTR);
806 write_exp_string ($1.stoken);
807 write_exp_elt_opcode (STRUCTOP_PTR);
811 struct minimal_symbol *msymbol;
812 char *arg = copy_name ($1.stoken);
815 lookup_minimal_symbol (arg, NULL, NULL);
817 write_exp_msymbol (msymbol);
818 else if (!have_full_symbols () && !have_partial_symbols ())
819 error ("No symbol table is loaded. Use the \"file\" command.");
821 error ("No symbol \"%s\" in current context.",
822 copy_name ($1.stoken));
827 space_identifier : '@' NAME
828 { push_type_address_space (copy_name ($2.stoken));
829 push_type (tp_space_identifier);
833 const_or_volatile: const_or_volatile_noopt
837 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
840 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
841 | const_or_volatile_noopt
844 const_or_volatile_or_space_identifier:
845 const_or_volatile_or_space_identifier_noopt
850 { push_type (tp_pointer); $$ = 0; }
852 { push_type (tp_pointer); $$ = $2; }
854 { push_type (tp_reference); $$ = 0; }
856 { push_type (tp_reference); $$ = $2; }
860 direct_abs_decl: '(' abs_decl ')'
862 | direct_abs_decl array_mod
865 push_type (tp_array);
870 push_type (tp_array);
874 | direct_abs_decl func_mod
875 { push_type (tp_function); }
877 { push_type (tp_function); }
888 | '(' nonempty_typelist ')'
889 { free ($2); $$ = 0; }
892 /* We used to try to recognize pointer to member types here, but
893 that didn't work (shift/reduce conflicts meant that these rules never
894 got executed). The problem is that
895 int (foo::bar::baz::bizzle)
896 is a function type but
897 int (foo::bar::baz::bizzle::*)
898 is a pointer to member type. Stroustrup loses again! */
903 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
907 { $$ = parse_type->builtin_int; }
909 { $$ = parse_type->builtin_long; }
911 { $$ = parse_type->builtin_short; }
913 { $$ = parse_type->builtin_long; }
914 | LONG SIGNED_KEYWORD INT_KEYWORD
915 { $$ = parse_type->builtin_long; }
916 | LONG SIGNED_KEYWORD
917 { $$ = parse_type->builtin_long; }
918 | SIGNED_KEYWORD LONG INT_KEYWORD
919 { $$ = parse_type->builtin_long; }
920 | UNSIGNED LONG INT_KEYWORD
921 { $$ = parse_type->builtin_unsigned_long; }
922 | LONG UNSIGNED INT_KEYWORD
923 { $$ = parse_type->builtin_unsigned_long; }
925 { $$ = parse_type->builtin_unsigned_long; }
927 { $$ = parse_type->builtin_long_long; }
928 | LONG LONG INT_KEYWORD
929 { $$ = parse_type->builtin_long_long; }
930 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
931 { $$ = parse_type->builtin_long_long; }
932 | LONG LONG SIGNED_KEYWORD
933 { $$ = parse_type->builtin_long_long; }
934 | SIGNED_KEYWORD LONG LONG
935 { $$ = parse_type->builtin_long_long; }
936 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
937 { $$ = parse_type->builtin_long_long; }
939 { $$ = parse_type->builtin_unsigned_long_long; }
940 | UNSIGNED LONG LONG INT_KEYWORD
941 { $$ = parse_type->builtin_unsigned_long_long; }
943 { $$ = parse_type->builtin_unsigned_long_long; }
944 | LONG LONG UNSIGNED INT_KEYWORD
945 { $$ = parse_type->builtin_unsigned_long_long; }
947 { $$ = parse_type->builtin_short; }
948 | SHORT SIGNED_KEYWORD INT_KEYWORD
949 { $$ = parse_type->builtin_short; }
950 | SHORT SIGNED_KEYWORD
951 { $$ = parse_type->builtin_short; }
952 | UNSIGNED SHORT INT_KEYWORD
953 { $$ = parse_type->builtin_unsigned_short; }
955 { $$ = parse_type->builtin_unsigned_short; }
956 | SHORT UNSIGNED INT_KEYWORD
957 { $$ = parse_type->builtin_unsigned_short; }
959 { $$ = parse_type->builtin_double; }
960 | LONG DOUBLE_KEYWORD
961 { $$ = parse_type->builtin_long_double; }
963 { $$ = lookup_struct (copy_name ($2),
964 expression_context_block); }
966 { $$ = lookup_struct (copy_name ($2),
967 expression_context_block); }
969 { $$ = lookup_union (copy_name ($2),
970 expression_context_block); }
972 { $$ = lookup_enum (copy_name ($2),
973 expression_context_block); }
975 { $$ = lookup_unsigned_typename (parse_language,
977 TYPE_NAME($2.type)); }
979 { $$ = parse_type->builtin_unsigned_int; }
980 | SIGNED_KEYWORD typename
981 { $$ = lookup_signed_typename (parse_language,
983 TYPE_NAME($2.type)); }
985 { $$ = parse_type->builtin_int; }
986 /* It appears that this rule for templates is never
987 reduced; template recognition happens by lookahead
988 in the token processing code in yylex. */
989 | TEMPLATE name '<' type '>'
990 { $$ = lookup_template_type(copy_name($2), $4,
991 expression_context_block);
993 | const_or_volatile_or_space_identifier_noopt typebase
994 { $$ = follow_types ($2); }
995 | typebase const_or_volatile_or_space_identifier_noopt
996 { $$ = follow_types ($1); }
1000 /* FIXME: carlton/2003-09-25: This next bit leads to lots of
1001 reduce-reduce conflicts, because the parser doesn't know whether or
1002 not to use qualified_name or qualified_type: the rules are
1003 identical. If the parser is parsing 'A::B::x', then, when it sees
1004 the second '::', it knows that the expression to the left of it has
1005 to be a type, so it uses qualified_type. But if it is parsing just
1006 'A::B', then it doesn't have any way of knowing which rule to use,
1007 so there's a reduce-reduce conflict; it picks qualified_name, since
1008 that occurs earlier in this file than qualified_type.
1010 There's no good way to fix this with the grammar as it stands; as
1011 far as I can tell, some of the problems arise from ambiguities that
1012 GDB introduces ('start' can be either an expression or a type), but
1013 some of it is inherent to the nature of C++ (you want to treat the
1014 input "(FOO)" fairly differently depending on whether FOO is an
1015 expression or a type, and if FOO is a complex expression, this can
1016 be hard to determine at the right time). Fortunately, it works
1017 pretty well in most cases. For example, if you do 'ptype A::B',
1018 where A::B is a nested type, then the parser will mistakenly
1019 misidentify it as an expression; but evaluate_subexp will get
1020 called with 'noside' set to EVAL_AVOID_SIDE_EFFECTS, and everything
1021 will work out anyways. But there are situations where the parser
1022 will get confused: the most common one that I've run into is when
1025 print *((A::B *) x)"
1027 where the parser doesn't realize that A::B has to be a type until
1028 it hits the first right paren, at which point it's too late. (The
1029 workaround is to type "print *(('A::B' *) x)" instead.) (And
1030 another solution is to fix our symbol-handling code so that the
1031 user never wants to type something like that in the first place,
1032 because we get all the types right without the user's help!)
1034 Perhaps we could fix this by making the lexer smarter. Some of
1035 this functionality used to be in the lexer, but in a way that
1036 worked even less well than the current solution: that attempt
1037 involved having the parser sometimes handle '::' and having the
1038 lexer sometimes handle it, and without a clear division of
1039 responsibility, it quickly degenerated into a big mess. Probably
1040 the eventual correct solution will give more of a role to the lexer
1041 (ideally via code that is shared between the lexer and
1042 decode_line_1), but I'm not holding my breath waiting for somebody
1043 to get around to cleaning this up... */
1045 qualified_type: typebase COLONCOLON name
1047 struct type *type = $1;
1048 struct type *new_type;
1049 char *ncopy = alloca ($3.length + 1);
1051 memcpy (ncopy, $3.ptr, $3.length);
1052 ncopy[$3.length] = '\0';
1054 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1055 && TYPE_CODE (type) != TYPE_CODE_UNION
1056 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
1057 error ("`%s' is not defined as an aggregate type.",
1060 new_type = cp_lookup_nested_type (type, ncopy,
1061 expression_context_block);
1062 if (new_type == NULL)
1063 error ("No type \"%s\" within class or namespace \"%s\".",
1064 ncopy, TYPE_NAME (type));
1073 $$.stoken.ptr = "int";
1074 $$.stoken.length = 3;
1075 $$.type = parse_type->builtin_int;
1079 $$.stoken.ptr = "long";
1080 $$.stoken.length = 4;
1081 $$.type = parse_type->builtin_long;
1085 $$.stoken.ptr = "short";
1086 $$.stoken.length = 5;
1087 $$.type = parse_type->builtin_short;
1093 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1094 $<ivec>$[0] = 1; /* Number of types in vector */
1097 | nonempty_typelist ',' type
1098 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1099 $$ = (struct type **) realloc ((char *) $1, len);
1100 $$[$<ivec>$[0]] = $3;
1105 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1106 { $$ = follow_types ($1); }
1109 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1110 | VOLATILE_KEYWORD CONST_KEYWORD
1113 const_or_volatile_noopt: const_and_volatile
1114 { push_type (tp_const);
1115 push_type (tp_volatile);
1118 { push_type (tp_const); }
1120 { push_type (tp_volatile); }
1123 name : NAME { $$ = $1.stoken; }
1124 | BLOCKNAME { $$ = $1.stoken; }
1125 | TYPENAME { $$ = $1.stoken; }
1126 | NAME_OR_INT { $$ = $1.stoken; }
1129 name_not_typename : NAME
1131 /* These would be useful if name_not_typename was useful, but it is just
1132 a fake for "variable", so these cause reduce/reduce conflicts because
1133 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1134 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1135 context where only a name could occur, this might be useful.
1142 /* Take care of parsing a number (anything that starts with a digit).
1143 Set yylval and return the token type; update lexptr.
1144 LEN is the number of characters in it. */
1146 /*** Needs some error checking for the float case ***/
1149 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1151 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1152 here, and we do kind of silly things like cast to unsigned. */
1159 int base = input_radix;
1162 /* Number of "L" suffixes encountered. */
1165 /* We have found a "L" or "U" suffix. */
1166 int found_suffix = 0;
1169 struct type *signed_type;
1170 struct type *unsigned_type;
1174 /* It's a float since it contains a point or an exponent. */
1176 int num; /* number of tokens scanned by scanf */
1179 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1180 point. Return DECFLOAT. */
1182 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1185 putithere->typed_val_decfloat.type
1186 = parse_type->builtin_decfloat;
1187 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1188 gdbarch_byte_order (parse_gdbarch), p);
1193 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1196 putithere->typed_val_decfloat.type
1197 = parse_type->builtin_decdouble;
1198 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1199 gdbarch_byte_order (parse_gdbarch), p);
1204 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1207 putithere->typed_val_decfloat.type
1208 = parse_type->builtin_declong;
1209 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1210 gdbarch_byte_order (parse_gdbarch), p);
1216 saved_char = p[len];
1217 p[len] = 0; /* null-terminate the token */
1218 num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%s",
1219 &putithere->typed_val_float.dval, s);
1220 p[len] = saved_char; /* restore the input stream */
1223 putithere->typed_val_float.type =
1224 parse_type->builtin_double;
1228 /* See if it has any float suffix: 'f' for float, 'l' for long
1230 if (!strcasecmp (s, "f"))
1231 putithere->typed_val_float.type =
1232 parse_type->builtin_float;
1233 else if (!strcasecmp (s, "l"))
1234 putithere->typed_val_float.type =
1235 parse_type->builtin_long_double;
1247 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1281 if (c >= 'A' && c <= 'Z')
1283 if (c != 'l' && c != 'u')
1285 if (c >= '0' && c <= '9')
1293 if (base > 10 && c >= 'a' && c <= 'f')
1297 n += i = c - 'a' + 10;
1310 return ERROR; /* Char not a digit */
1313 return ERROR; /* Invalid digit in this base */
1315 /* Portably test for overflow (only works for nonzero values, so make
1316 a second check for zero). FIXME: Can't we just make n and prevn
1317 unsigned and avoid this? */
1318 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1319 unsigned_p = 1; /* Try something unsigned */
1321 /* Portably test for unsigned overflow.
1322 FIXME: This check is wrong; for example it doesn't find overflow
1323 on 0x123456789 when LONGEST is 32 bits. */
1324 if (c != 'l' && c != 'u' && n != 0)
1326 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1327 error ("Numeric constant too large.");
1332 /* An integer constant is an int, a long, or a long long. An L
1333 suffix forces it to be long; an LL suffix forces it to be long
1334 long. If not forced to a larger size, it gets the first type of
1335 the above that it fits in. To figure out whether it fits, we
1336 shift it right and see whether anything remains. Note that we
1337 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1338 operation, because many compilers will warn about such a shift
1339 (which always produces a zero result). Sometimes gdbarch_int_bit
1340 or gdbarch_long_bit will be that big, sometimes not. To deal with
1341 the case where it is we just always shift the value more than
1342 once, with fewer bits each time. */
1344 un = (ULONGEST)n >> 2;
1346 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1348 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1350 /* A large decimal (not hex or octal) constant (between INT_MAX
1351 and UINT_MAX) is a long or unsigned long, according to ANSI,
1352 never an unsigned int, but this code treats it as unsigned
1353 int. This probably should be fixed. GCC gives a warning on
1356 unsigned_type = parse_type->builtin_unsigned_int;
1357 signed_type = parse_type->builtin_int;
1359 else if (long_p <= 1
1360 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1362 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1363 unsigned_type = parse_type->builtin_unsigned_long;
1364 signed_type = parse_type->builtin_long;
1369 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1370 < gdbarch_long_long_bit (parse_gdbarch))
1371 /* A long long does not fit in a LONGEST. */
1372 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1374 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1375 high_bit = (ULONGEST) 1 << shift;
1376 unsigned_type = parse_type->builtin_unsigned_long_long;
1377 signed_type = parse_type->builtin_long_long;
1380 putithere->typed_val_int.val = n;
1382 /* If the high bit of the worked out type is set then this number
1383 has to be unsigned. */
1385 if (unsigned_p || (n & high_bit))
1387 putithere->typed_val_int.type = unsigned_type;
1391 putithere->typed_val_int.type = signed_type;
1397 /* Temporary obstack used for holding strings. */
1398 static struct obstack tempbuf;
1399 static int tempbuf_init;
1401 /* Parse a C escape sequence. The initial backslash of the sequence
1402 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1403 last character of the sequence. If OUTPUT is not NULL, the
1404 translated form of the escape sequence will be written there. If
1405 OUTPUT is NULL, no output is written and the call will only affect
1406 *PTR. If an escape sequence is expressed in target bytes, then the
1407 entire sequence will simply be copied to OUTPUT. Return 1 if any
1408 character was emitted, 0 otherwise. */
1411 c_parse_escape (char **ptr, struct obstack *output)
1413 char *tokptr = *ptr;
1416 /* Some escape sequences undergo character set conversion. Those we
1420 /* Hex escapes do not undergo character set conversion, so keep
1421 the escape sequence for later. */
1424 obstack_grow_str (output, "\\x");
1426 if (!isxdigit (*tokptr))
1427 error (_("\\x escape without a following hex digit"));
1428 while (isxdigit (*tokptr))
1431 obstack_1grow (output, *tokptr);
1436 /* Octal escapes do not undergo character set conversion, so
1437 keep the escape sequence for later. */
1449 obstack_grow_str (output, "\\");
1451 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1455 obstack_1grow (output, *tokptr);
1461 /* We handle UCNs later. We could handle them here, but that
1462 would mean a spurious error in the case where the UCN could
1463 be converted to the target charset but not the host
1469 int i, len = c == 'U' ? 8 : 4;
1472 obstack_1grow (output, '\\');
1473 obstack_1grow (output, *tokptr);
1476 if (!isxdigit (*tokptr))
1477 error (_("\\%c escape without a following hex digit"), c);
1478 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1481 obstack_1grow (output, *tokptr);
1487 /* We must pass backslash through so that it does not
1488 cause quoting during the second expansion. */
1491 obstack_grow_str (output, "\\\\");
1495 /* Escapes which undergo conversion. */
1498 obstack_1grow (output, '\a');
1503 obstack_1grow (output, '\b');
1508 obstack_1grow (output, '\f');
1513 obstack_1grow (output, '\n');
1518 obstack_1grow (output, '\r');
1523 obstack_1grow (output, '\t');
1528 obstack_1grow (output, '\v');
1532 /* GCC extension. */
1535 obstack_1grow (output, HOST_ESCAPE_CHAR);
1539 /* Backslash-newline expands to nothing at all. */
1545 /* A few escapes just expand to the character itself. */
1549 /* GCC extensions. */
1554 /* Unrecognized escapes turn into the character itself. */
1557 obstack_1grow (output, *tokptr);
1565 /* Parse a string or character literal from TOKPTR. The string or
1566 character may be wide or unicode. *OUTPTR is set to just after the
1567 end of the literal in the input string. The resulting token is
1568 stored in VALUE. This returns a token value, either STRING or
1569 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1570 number of host characters in the literal. */
1572 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1576 enum c_string_type type;
1578 /* Build the gdb internal form of the input string in tempbuf. Note
1579 that the buffer is null byte terminated *only* for the
1580 convenience of debugging gdb itself and printing the buffer
1581 contents when the buffer contains no embedded nulls. Gdb does
1582 not depend upon the buffer being null byte terminated, it uses
1583 the length string instead. This allows gdb to handle C strings
1584 (as well as strings in other languages) with embedded null
1590 obstack_free (&tempbuf, NULL);
1591 obstack_init (&tempbuf);
1593 /* Record the string type. */
1596 type = C_WIDE_STRING;
1599 else if (*tokptr == 'u')
1604 else if (*tokptr == 'U')
1612 /* Skip the quote. */
1626 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1628 else if (c == quote)
1632 obstack_1grow (&tempbuf, c);
1634 /* FIXME: this does the wrong thing with multi-byte host
1635 characters. We could use mbrlen here, but that would
1636 make "set host-charset" a bit less useful. */
1641 if (*tokptr != quote)
1644 error ("Unterminated string in expression.");
1646 error ("Unmatched single quote.");
1651 value->ptr = obstack_base (&tempbuf);
1652 value->length = obstack_object_size (&tempbuf);
1656 return quote == '"' ? STRING : CHAR;
1663 enum exp_opcode opcode;
1667 static const struct token tokentab3[] =
1669 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1670 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
1671 {"->*", ARROW_STAR, BINOP_END, 1}
1674 static const struct token tokentab2[] =
1676 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1677 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1678 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1679 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
1680 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
1681 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
1682 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
1683 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
1684 {"++", INCREMENT, BINOP_END, 0},
1685 {"--", DECREMENT, BINOP_END, 0},
1686 {"->", ARROW, BINOP_END, 0},
1687 {"&&", ANDAND, BINOP_END, 0},
1688 {"||", OROR, BINOP_END, 0},
1689 /* "::" is *not* only C++: gdb overrides its meaning in several
1690 different ways, e.g., 'filename'::func, function::variable. */
1691 {"::", COLONCOLON, BINOP_END, 0},
1692 {"<<", LSH, BINOP_END, 0},
1693 {">>", RSH, BINOP_END, 0},
1694 {"==", EQUAL, BINOP_END, 0},
1695 {"!=", NOTEQUAL, BINOP_END, 0},
1696 {"<=", LEQ, BINOP_END, 0},
1697 {">=", GEQ, BINOP_END, 0},
1698 {".*", DOT_STAR, BINOP_END, 1}
1701 /* Identifier-like tokens. */
1702 static const struct token ident_tokens[] =
1704 {"unsigned", UNSIGNED, OP_NULL, 0},
1705 {"template", TEMPLATE, OP_NULL, 1},
1706 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
1707 {"struct", STRUCT, OP_NULL, 0},
1708 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
1709 {"sizeof", SIZEOF, OP_NULL, 0},
1710 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
1711 {"false", FALSEKEYWORD, OP_NULL, 1},
1712 {"class", CLASS, OP_NULL, 1},
1713 {"union", UNION, OP_NULL, 0},
1714 {"short", SHORT, OP_NULL, 0},
1715 {"const", CONST_KEYWORD, OP_NULL, 0},
1716 {"enum", ENUM, OP_NULL, 0},
1717 {"long", LONG, OP_NULL, 0},
1718 {"true", TRUEKEYWORD, OP_NULL, 1},
1719 {"int", INT_KEYWORD, OP_NULL, 0},
1721 {"and", ANDAND, BINOP_END, 1},
1722 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
1723 {"bitand", '&', OP_NULL, 1},
1724 {"bitor", '|', OP_NULL, 1},
1725 {"compl", '~', OP_NULL, 1},
1726 {"not", '!', OP_NULL, 1},
1727 {"not_eq", NOTEQUAL, BINOP_END, 1},
1728 {"or", OROR, BINOP_END, 1},
1729 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
1730 {"xor", '^', OP_NULL, 1},
1731 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1}
1734 /* When we find that lexptr (the global var defined in parse.c) is
1735 pointing at a macro invocation, we expand the invocation, and call
1736 scan_macro_expansion to save the old lexptr here and point lexptr
1737 into the expanded text. When we reach the end of that, we call
1738 end_macro_expansion to pop back to the value we saved here. The
1739 macro expansion code promises to return only fully-expanded text,
1740 so we don't need to "push" more than one level.
1742 This is disgusting, of course. It would be cleaner to do all macro
1743 expansion beforehand, and then hand that to lexptr. But we don't
1744 really know where the expression ends. Remember, in a command like
1746 (gdb) break *ADDRESS if CONDITION
1748 we evaluate ADDRESS in the scope of the current frame, but we
1749 evaluate CONDITION in the scope of the breakpoint's location. So
1750 it's simply wrong to try to macro-expand the whole thing at once. */
1751 static char *macro_original_text;
1753 /* We save all intermediate macro expansions on this obstack for the
1754 duration of a single parse. The expansion text may sometimes have
1755 to live past the end of the expansion, due to yacc lookahead.
1756 Rather than try to be clever about saving the data for a single
1757 token, we simply keep it all and delete it after parsing has
1759 static struct obstack expansion_obstack;
1762 scan_macro_expansion (char *expansion)
1766 /* We'd better not be trying to push the stack twice. */
1767 gdb_assert (! macro_original_text);
1769 /* Copy to the obstack, and then free the intermediate
1771 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
1774 /* Save the old lexptr value, so we can return to it when we're done
1775 parsing the expanded text. */
1776 macro_original_text = lexptr;
1782 scanning_macro_expansion (void)
1784 return macro_original_text != 0;
1789 finished_macro_expansion (void)
1791 /* There'd better be something to pop back to. */
1792 gdb_assert (macro_original_text);
1794 /* Pop back to the original text. */
1795 lexptr = macro_original_text;
1796 macro_original_text = 0;
1801 scan_macro_cleanup (void *dummy)
1803 if (macro_original_text)
1804 finished_macro_expansion ();
1806 obstack_free (&expansion_obstack, NULL);
1810 /* The scope used for macro expansion. */
1811 static struct macro_scope *expression_macro_scope;
1813 /* This is set if a NAME token appeared at the very end of the input
1814 string, with no whitespace separating the name from the EOF. This
1815 is used only when parsing to do field name completion. */
1816 static int saw_name_at_eof;
1818 /* This is set if the previously-returned token was a structure
1819 operator -- either '.' or ARROW. This is used only when parsing to
1820 do field name completion. */
1821 static int last_was_structop;
1823 /* Read one token, getting characters through lexptr. */
1832 int saw_structop = last_was_structop;
1835 last_was_structop = 0;
1839 /* Check if this is a macro invocation that we need to expand. */
1840 if (! scanning_macro_expansion ())
1842 char *expanded = macro_expand_next (&lexptr,
1843 standard_macro_lookup,
1844 expression_macro_scope);
1847 scan_macro_expansion (expanded);
1850 prev_lexptr = lexptr;
1853 /* See if it is a special token of length 3. */
1854 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1855 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
1857 if (tokentab3[i].cxx_only
1858 && parse_language->la_language != language_cplus)
1862 yylval.opcode = tokentab3[i].opcode;
1863 return tokentab3[i].token;
1866 /* See if it is a special token of length 2. */
1867 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1868 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
1870 if (tokentab2[i].cxx_only
1871 && parse_language->la_language != language_cplus)
1875 yylval.opcode = tokentab2[i].opcode;
1876 if (in_parse_field && tokentab2[i].token == ARROW)
1877 last_was_structop = 1;
1878 return tokentab2[i].token;
1881 switch (c = *tokstart)
1884 /* If we were just scanning the result of a macro expansion,
1885 then we need to resume scanning the original text.
1886 If we're parsing for field name completion, and the previous
1887 token allows such completion, return a COMPLETE token.
1888 Otherwise, we were already scanning the original text, and
1889 we're really done. */
1890 if (scanning_macro_expansion ())
1892 finished_macro_expansion ();
1895 else if (saw_name_at_eof)
1897 saw_name_at_eof = 0;
1900 else if (saw_structop)
1919 if (paren_depth == 0)
1926 if (comma_terminates
1928 && ! scanning_macro_expansion ())
1934 /* Might be a floating point number. */
1935 if (lexptr[1] < '0' || lexptr[1] > '9')
1938 last_was_structop = 1;
1939 goto symbol; /* Nope, must be a symbol. */
1941 /* FALL THRU into number case. */
1954 /* It's a number. */
1955 int got_dot = 0, got_e = 0, toktype;
1957 int hex = input_radix > 10;
1959 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1964 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1972 /* This test includes !hex because 'e' is a valid hex digit
1973 and thus does not indicate a floating point number when
1974 the radix is hex. */
1975 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1976 got_dot = got_e = 1;
1977 /* This test does not include !hex, because a '.' always indicates
1978 a decimal floating point number regardless of the radix. */
1979 else if (!got_dot && *p == '.')
1981 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1982 && (*p == '-' || *p == '+'))
1983 /* This is the sign of the exponent, not the end of the
1986 /* We will take any letters or digits. parse_number will
1987 complain if past the radix, or if L or U are not final. */
1988 else if ((*p < '0' || *p > '9')
1989 && ((*p < 'a' || *p > 'z')
1990 && (*p < 'A' || *p > 'Z')))
1993 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1994 if (toktype == ERROR)
1996 char *err_copy = (char *) alloca (p - tokstart + 1);
1998 memcpy (err_copy, tokstart, p - tokstart);
1999 err_copy[p - tokstart] = 0;
2000 error ("Invalid number \"%s\".", err_copy);
2031 if (tokstart[1] != '"' && tokstart[1] != '\'')
2038 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2043 error ("Empty character constant.");
2044 else if (host_len > 2 && c == '\'')
2047 namelen = lexptr - tokstart - 1;
2050 else if (host_len > 1)
2051 error ("Invalid character constant.");
2057 if (!(c == '_' || c == '$'
2058 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2059 /* We must have come across a bad character (e.g. ';'). */
2060 error ("Invalid character '%c' in expression.", c);
2062 /* It's a name. See how long it is. */
2064 for (c = tokstart[namelen];
2065 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2066 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2068 /* Template parameter lists are part of the name.
2069 FIXME: This mishandles `print $a<4&&$a>3'. */
2073 /* Scan ahead to get rest of the template specification. Note
2074 that we look ahead only when the '<' adjoins non-whitespace
2075 characters; for comparison expressions, e.g. "a < b > c",
2076 there must be spaces before the '<', etc. */
2078 char * p = find_template_name_end (tokstart + namelen);
2080 namelen = p - tokstart;
2083 c = tokstart[++namelen];
2086 /* The token "if" terminates the expression and is NOT removed from
2087 the input stream. It doesn't count if it appears in the
2088 expansion of a macro. */
2090 && tokstart[0] == 'i'
2091 && tokstart[1] == 'f'
2092 && ! scanning_macro_expansion ())
2101 yylval.sval.ptr = tokstart;
2102 yylval.sval.length = namelen;
2104 /* Catch specific keywords. */
2105 copy = copy_name (yylval.sval);
2106 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2107 if (strcmp (copy, ident_tokens[i].operator) == 0)
2109 if (ident_tokens[i].cxx_only
2110 && parse_language->la_language != language_cplus)
2113 /* It is ok to always set this, even though we don't always
2114 strictly need to. */
2115 yylval.opcode = ident_tokens[i].opcode;
2116 return ident_tokens[i].token;
2119 if (*tokstart == '$')
2121 write_dollar_variable (yylval.sval);
2125 /* Use token-type BLOCKNAME for symbols that happen to be defined as
2126 functions or symtabs. If this is not so, then ...
2127 Use token-type TYPENAME for symbols that happen to be defined
2128 currently as names of types; NAME for other symbols.
2129 The caller is not constrained to care about the distinction. */
2132 int is_a_field_of_this = 0;
2135 sym = lookup_symbol (copy, expression_context_block,
2137 parse_language->la_language == language_cplus
2138 ? &is_a_field_of_this : (int *) NULL);
2139 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
2140 no psymtabs (coff, xcoff, or some future change to blow away the
2141 psymtabs once once symbols are read). */
2142 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2144 yylval.ssym.sym = sym;
2145 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2149 { /* See if it's a file name. */
2150 struct symtab *symtab;
2152 symtab = lookup_symtab (copy);
2156 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2161 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2163 /* NOTE: carlton/2003-09-25: There used to be code here to
2164 handle nested types. It didn't work very well. See the
2165 comment before qualified_type for more info. */
2166 yylval.tsym.type = SYMBOL_TYPE (sym);
2170 = language_lookup_primitive_type_by_name (parse_language,
2171 parse_gdbarch, copy);
2172 if (yylval.tsym.type != NULL)
2175 /* Input names that aren't symbols but ARE valid hex numbers,
2176 when the input radix permits them, can be names or numbers
2177 depending on the parse. Note we support radixes > 16 here. */
2179 && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
2180 || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
2182 YYSTYPE newlval; /* Its value is ignored. */
2183 hextype = parse_number (tokstart, namelen, 0, &newlval);
2186 yylval.ssym.sym = sym;
2187 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2192 /* Any other kind of symbol */
2193 yylval.ssym.sym = sym;
2194 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2195 if (in_parse_field && *lexptr == '\0')
2196 saw_name_at_eof = 1;
2205 struct cleanup *back_to = make_cleanup (free_current_contents,
2206 &expression_macro_scope);
2208 /* Set up the scope for macro expansion. */
2209 expression_macro_scope = NULL;
2211 if (expression_context_block)
2212 expression_macro_scope
2213 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2215 expression_macro_scope = default_macro_scope ();
2216 if (! expression_macro_scope)
2217 expression_macro_scope = user_macro_scope ();
2219 /* Initialize macro expansion code. */
2220 obstack_init (&expansion_obstack);
2221 gdb_assert (! macro_original_text);
2222 make_cleanup (scan_macro_cleanup, 0);
2224 /* Initialize some state used by the lexer. */
2225 last_was_structop = 0;
2226 saw_name_at_eof = 0;
2228 result = yyparse ();
2229 do_cleanups (back_to);
2238 lexptr = prev_lexptr;
2240 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);