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, 2010, 2011
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 *);
160 static struct stoken operator_stoken (const char *);
163 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
165 %type <tval> type typebase
166 %type <tvec> nonempty_typelist
167 /* %type <bval> block */
169 /* Fancy type parsing. */
170 %type <voidval> func_mod direct_abs_decl abs_decl
172 %type <lval> array_mod
174 %token <typed_val_int> INT
175 %token <typed_val_float> FLOAT
176 %token <typed_val_decfloat> DECFLOAT
178 /* Both NAME and TYPENAME tokens represent symbols in the input,
179 and both convey their data as strings.
180 But a TYPENAME is a string that happens to be defined as a typedef
181 or builtin type name (such as int or char)
182 and a NAME is any other symbol.
183 Contexts where this distinction is not important can use the
184 nonterminal "name", which matches either NAME or TYPENAME. */
186 %token <tsval> STRING
188 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
189 %token <ssym> UNKNOWN_CPP_NAME
190 %token <voidval> COMPLETE
191 %token <tsym> TYPENAME
193 %type <svec> string_exp
194 %type <ssym> name_not_typename
195 %type <tsym> typename
197 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
198 but which would parse as a valid number in the current input radix.
199 E.g. "c" when input_radix==16. Depending on the parse, it will be
200 turned into a name or into a number. */
202 %token <ssym> NAME_OR_INT
205 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
209 %type <sval> operator
210 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
213 /* Special type cases, put in to allow the parser to distinguish different
215 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
217 %token <sval> VARIABLE
219 %token <opcode> ASSIGN_MODIFY
228 %right '=' ASSIGN_MODIFY
236 %left '<' '>' LEQ GEQ
241 %right UNARY INCREMENT DECREMENT
242 %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
243 %token <ssym> BLOCKNAME
244 %token <bval> FILENAME
256 { write_exp_elt_opcode(OP_TYPE);
257 write_exp_elt_type($1);
258 write_exp_elt_opcode(OP_TYPE);}
261 /* Expressions, including the comma operator. */
264 { write_exp_elt_opcode (BINOP_COMMA); }
267 /* Expressions, not including the comma operator. */
268 exp : '*' exp %prec UNARY
269 { write_exp_elt_opcode (UNOP_IND); }
272 exp : '&' exp %prec UNARY
273 { write_exp_elt_opcode (UNOP_ADDR); }
276 exp : '-' exp %prec UNARY
277 { write_exp_elt_opcode (UNOP_NEG); }
280 exp : '+' exp %prec UNARY
281 { write_exp_elt_opcode (UNOP_PLUS); }
284 exp : '!' exp %prec UNARY
285 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
288 exp : '~' exp %prec UNARY
289 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
292 exp : INCREMENT exp %prec UNARY
293 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
296 exp : DECREMENT exp %prec UNARY
297 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
300 exp : exp INCREMENT %prec UNARY
301 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
304 exp : exp DECREMENT %prec UNARY
305 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
308 exp : SIZEOF exp %prec UNARY
309 { write_exp_elt_opcode (UNOP_SIZEOF); }
313 { write_exp_elt_opcode (STRUCTOP_PTR);
314 write_exp_string ($3);
315 write_exp_elt_opcode (STRUCTOP_PTR); }
318 exp : exp ARROW name COMPLETE
319 { mark_struct_expression ();
320 write_exp_elt_opcode (STRUCTOP_PTR);
321 write_exp_string ($3);
322 write_exp_elt_opcode (STRUCTOP_PTR); }
325 exp : exp ARROW COMPLETE
327 mark_struct_expression ();
328 write_exp_elt_opcode (STRUCTOP_PTR);
331 write_exp_string (s);
332 write_exp_elt_opcode (STRUCTOP_PTR); }
335 exp : exp ARROW qualified_name
336 { /* exp->type::name becomes exp->*(&type::name) */
337 /* Note: this doesn't work if name is a
338 static member! FIXME */
339 write_exp_elt_opcode (UNOP_ADDR);
340 write_exp_elt_opcode (STRUCTOP_MPTR); }
343 exp : exp ARROW_STAR exp
344 { write_exp_elt_opcode (STRUCTOP_MPTR); }
348 { write_exp_elt_opcode (STRUCTOP_STRUCT);
349 write_exp_string ($3);
350 write_exp_elt_opcode (STRUCTOP_STRUCT); }
353 exp : exp '.' name COMPLETE
354 { mark_struct_expression ();
355 write_exp_elt_opcode (STRUCTOP_STRUCT);
356 write_exp_string ($3);
357 write_exp_elt_opcode (STRUCTOP_STRUCT); }
360 exp : exp '.' COMPLETE
362 mark_struct_expression ();
363 write_exp_elt_opcode (STRUCTOP_STRUCT);
366 write_exp_string (s);
367 write_exp_elt_opcode (STRUCTOP_STRUCT); }
370 exp : exp '.' qualified_name
371 { /* exp.type::name becomes exp.*(&type::name) */
372 /* Note: this doesn't work if name is a
373 static member! FIXME */
374 write_exp_elt_opcode (UNOP_ADDR);
375 write_exp_elt_opcode (STRUCTOP_MEMBER); }
378 exp : exp DOT_STAR exp
379 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
382 exp : exp '[' exp1 ']'
383 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
387 /* This is to save the value of arglist_len
388 being accumulated by an outer function call. */
389 { start_arglist (); }
390 arglist ')' %prec ARROW
391 { write_exp_elt_opcode (OP_FUNCALL);
392 write_exp_elt_longcst ((LONGEST) end_arglist ());
393 write_exp_elt_opcode (OP_FUNCALL); }
396 exp : UNKNOWN_CPP_NAME '('
398 /* This could potentially be a an argument defined
399 lookup function (Koenig). */
400 write_exp_elt_opcode (OP_ADL_FUNC);
401 write_exp_elt_block (expression_context_block);
402 write_exp_elt_sym (NULL); /* Placeholder. */
403 write_exp_string ($1.stoken);
404 write_exp_elt_opcode (OP_ADL_FUNC);
406 /* This is to save the value of arglist_len
407 being accumulated by an outer function call. */
411 arglist ')' %prec ARROW
413 write_exp_elt_opcode (OP_FUNCALL);
414 write_exp_elt_longcst ((LONGEST) end_arglist ());
415 write_exp_elt_opcode (OP_FUNCALL);
420 { start_arglist (); }
430 arglist : arglist ',' exp %prec ABOVE_COMMA
434 exp : exp '(' nonempty_typelist ')' const_or_volatile
436 write_exp_elt_opcode (TYPE_INSTANCE);
437 write_exp_elt_longcst ((LONGEST) $<ivec>3[0]);
438 for (i = 0; i < $<ivec>3[0]; ++i)
439 write_exp_elt_type ($<tvec>3[i + 1]);
440 write_exp_elt_longcst((LONGEST) $<ivec>3[0]);
441 write_exp_elt_opcode (TYPE_INSTANCE);
447 { $$ = end_arglist () - 1; }
449 exp : lcurly arglist rcurly %prec ARROW
450 { write_exp_elt_opcode (OP_ARRAY);
451 write_exp_elt_longcst ((LONGEST) 0);
452 write_exp_elt_longcst ((LONGEST) $3);
453 write_exp_elt_opcode (OP_ARRAY); }
456 exp : lcurly type rcurly exp %prec UNARY
457 { write_exp_elt_opcode (UNOP_MEMVAL);
458 write_exp_elt_type ($2);
459 write_exp_elt_opcode (UNOP_MEMVAL); }
462 exp : '(' type ')' exp %prec UNARY
463 { write_exp_elt_opcode (UNOP_CAST);
464 write_exp_elt_type ($2);
465 write_exp_elt_opcode (UNOP_CAST); }
472 /* Binary operators in order of decreasing precedence. */
475 { write_exp_elt_opcode (BINOP_REPEAT); }
479 { write_exp_elt_opcode (BINOP_MUL); }
483 { write_exp_elt_opcode (BINOP_DIV); }
487 { write_exp_elt_opcode (BINOP_REM); }
491 { write_exp_elt_opcode (BINOP_ADD); }
495 { write_exp_elt_opcode (BINOP_SUB); }
499 { write_exp_elt_opcode (BINOP_LSH); }
503 { write_exp_elt_opcode (BINOP_RSH); }
507 { write_exp_elt_opcode (BINOP_EQUAL); }
510 exp : exp NOTEQUAL exp
511 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
515 { write_exp_elt_opcode (BINOP_LEQ); }
519 { write_exp_elt_opcode (BINOP_GEQ); }
523 { write_exp_elt_opcode (BINOP_LESS); }
527 { write_exp_elt_opcode (BINOP_GTR); }
531 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
535 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
539 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
543 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
547 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
550 exp : exp '?' exp ':' exp %prec '?'
551 { write_exp_elt_opcode (TERNOP_COND); }
555 { write_exp_elt_opcode (BINOP_ASSIGN); }
558 exp : exp ASSIGN_MODIFY exp
559 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
560 write_exp_elt_opcode ($2);
561 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
565 { write_exp_elt_opcode (OP_LONG);
566 write_exp_elt_type ($1.type);
567 write_exp_elt_longcst ((LONGEST)($1.val));
568 write_exp_elt_opcode (OP_LONG); }
573 struct stoken_vector vec;
576 write_exp_string_vector ($1.type, &vec);
582 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
583 write_exp_elt_opcode (OP_LONG);
584 write_exp_elt_type (val.typed_val_int.type);
585 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
586 write_exp_elt_opcode (OP_LONG);
592 { write_exp_elt_opcode (OP_DOUBLE);
593 write_exp_elt_type ($1.type);
594 write_exp_elt_dblcst ($1.dval);
595 write_exp_elt_opcode (OP_DOUBLE); }
599 { write_exp_elt_opcode (OP_DECFLOAT);
600 write_exp_elt_type ($1.type);
601 write_exp_elt_decfloatcst ($1.val);
602 write_exp_elt_opcode (OP_DECFLOAT); }
610 write_dollar_variable ($1);
614 exp : SIZEOF '(' type ')' %prec UNARY
615 { write_exp_elt_opcode (OP_LONG);
616 write_exp_elt_type (lookup_signed_typename
617 (parse_language, parse_gdbarch,
620 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
621 write_exp_elt_opcode (OP_LONG); }
624 exp : REINTERPRET_CAST '<' type '>' '(' exp ')' %prec UNARY
625 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST);
626 write_exp_elt_type ($3);
627 write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
630 exp : STATIC_CAST '<' type '>' '(' exp ')' %prec UNARY
631 { write_exp_elt_opcode (UNOP_CAST);
632 write_exp_elt_type ($3);
633 write_exp_elt_opcode (UNOP_CAST); }
636 exp : DYNAMIC_CAST '<' type '>' '(' exp ')' %prec UNARY
637 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST);
638 write_exp_elt_type ($3);
639 write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
642 exp : CONST_CAST '<' type '>' '(' exp ')' %prec UNARY
643 { /* We could do more error checking here, but
644 it doesn't seem worthwhile. */
645 write_exp_elt_opcode (UNOP_CAST);
646 write_exp_elt_type ($3);
647 write_exp_elt_opcode (UNOP_CAST); }
653 /* We copy the string here, and not in the
654 lexer, to guarantee that we do not leak a
655 string. Note that we follow the
656 NUL-termination convention of the
658 struct typed_stoken *vec = XNEW (struct typed_stoken);
663 vec->length = $1.length;
664 vec->ptr = malloc ($1.length + 1);
665 memcpy (vec->ptr, $1.ptr, $1.length + 1);
670 /* Note that we NUL-terminate here, but just
674 $$.tokens = realloc ($$.tokens,
675 $$.len * sizeof (struct typed_stoken));
677 p = malloc ($2.length + 1);
678 memcpy (p, $2.ptr, $2.length + 1);
680 $$.tokens[$$.len - 1].type = $2.type;
681 $$.tokens[$$.len - 1].length = $2.length;
682 $$.tokens[$$.len - 1].ptr = p;
689 enum c_string_type type = C_STRING;
691 for (i = 0; i < $1.len; ++i)
693 switch ($1.tokens[i].type)
701 && type != $1.tokens[i].type)
702 error (_("Undefined string concatenation."));
703 type = $1.tokens[i].type;
707 internal_error (__FILE__, __LINE__,
708 "unrecognized type in string concatenation");
712 write_exp_string_vector (type, &$1);
713 for (i = 0; i < $1.len; ++i)
714 free ($1.tokens[i].ptr);
721 { write_exp_elt_opcode (OP_LONG);
722 write_exp_elt_type (parse_type->builtin_bool);
723 write_exp_elt_longcst ((LONGEST) 1);
724 write_exp_elt_opcode (OP_LONG); }
728 { write_exp_elt_opcode (OP_LONG);
729 write_exp_elt_type (parse_type->builtin_bool);
730 write_exp_elt_longcst ((LONGEST) 0);
731 write_exp_elt_opcode (OP_LONG); }
739 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
741 error (_("No file or function \"%s\"."),
742 copy_name ($1.stoken));
750 block : block COLONCOLON name
752 = lookup_symbol (copy_name ($3), $1,
753 VAR_DOMAIN, (int *) NULL);
754 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
755 error (_("No function \"%s\" in specified context."),
757 $$ = SYMBOL_BLOCK_VALUE (tem); }
760 variable: name_not_typename ENTRY
761 { struct symbol *sym = $1.sym;
763 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
764 || !symbol_read_needs_frame (sym))
765 error (_("@entry can be used only for function "
766 "parameters, not for \"%s\""),
767 copy_name ($1.stoken));
769 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
770 write_exp_elt_sym (sym);
771 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
775 variable: block COLONCOLON name
776 { struct symbol *sym;
777 sym = lookup_symbol (copy_name ($3), $1,
778 VAR_DOMAIN, (int *) NULL);
780 error (_("No symbol \"%s\" in specified context."),
783 write_exp_elt_opcode (OP_VAR_VALUE);
784 /* block_found is set by lookup_symbol. */
785 write_exp_elt_block (block_found);
786 write_exp_elt_sym (sym);
787 write_exp_elt_opcode (OP_VAR_VALUE); }
790 qualified_name: TYPENAME COLONCOLON name
792 struct type *type = $1.type;
793 CHECK_TYPEDEF (type);
794 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
795 && TYPE_CODE (type) != TYPE_CODE_UNION
796 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
797 error (_("`%s' is not defined as an aggregate type."),
800 write_exp_elt_opcode (OP_SCOPE);
801 write_exp_elt_type (type);
802 write_exp_string ($3);
803 write_exp_elt_opcode (OP_SCOPE);
805 | TYPENAME COLONCOLON '~' name
807 struct type *type = $1.type;
808 struct stoken tmp_token;
809 CHECK_TYPEDEF (type);
810 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
811 && TYPE_CODE (type) != TYPE_CODE_UNION
812 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
813 error (_("`%s' is not defined as an aggregate type."),
816 tmp_token.ptr = (char*) alloca ($4.length + 2);
817 tmp_token.length = $4.length + 1;
818 tmp_token.ptr[0] = '~';
819 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
820 tmp_token.ptr[tmp_token.length] = 0;
822 /* Check for valid destructor name. */
823 destructor_name_p (tmp_token.ptr, $1.type);
824 write_exp_elt_opcode (OP_SCOPE);
825 write_exp_elt_type (type);
826 write_exp_string (tmp_token);
827 write_exp_elt_opcode (OP_SCOPE);
829 | TYPENAME COLONCOLON name COLONCOLON name
831 char *copy = copy_name ($3);
832 error (_("No type \"%s\" within class "
833 "or namespace \"%s\"."),
834 copy, TYPE_NAME ($1.type));
838 variable: qualified_name
839 | COLONCOLON name_not_typename
841 char *name = copy_name ($2.stoken);
843 struct minimal_symbol *msymbol;
846 lookup_symbol (name, (const struct block *) NULL,
847 VAR_DOMAIN, (int *) NULL);
850 write_exp_elt_opcode (OP_VAR_VALUE);
851 write_exp_elt_block (NULL);
852 write_exp_elt_sym (sym);
853 write_exp_elt_opcode (OP_VAR_VALUE);
857 msymbol = lookup_minimal_symbol (name, NULL, NULL);
859 write_exp_msymbol (msymbol);
860 else if (!have_full_symbols () && !have_partial_symbols ())
861 error (_("No symbol table is loaded. Use the \"file\" command."));
863 error (_("No symbol \"%s\" in current context."), name);
867 variable: name_not_typename
868 { struct symbol *sym = $1.sym;
872 if (symbol_read_needs_frame (sym))
874 if (innermost_block == 0
875 || contained_in (block_found,
877 innermost_block = block_found;
880 write_exp_elt_opcode (OP_VAR_VALUE);
881 /* We want to use the selected frame, not
882 another more inner frame which happens to
883 be in the same block. */
884 write_exp_elt_block (NULL);
885 write_exp_elt_sym (sym);
886 write_exp_elt_opcode (OP_VAR_VALUE);
888 else if ($1.is_a_field_of_this)
890 /* C++: it hangs off of `this'. Must
891 not inadvertently convert from a method call
893 if (innermost_block == 0
894 || contained_in (block_found,
896 innermost_block = block_found;
897 write_exp_elt_opcode (OP_THIS);
898 write_exp_elt_opcode (OP_THIS);
899 write_exp_elt_opcode (STRUCTOP_PTR);
900 write_exp_string ($1.stoken);
901 write_exp_elt_opcode (STRUCTOP_PTR);
905 struct minimal_symbol *msymbol;
906 char *arg = copy_name ($1.stoken);
909 lookup_minimal_symbol (arg, NULL, NULL);
911 write_exp_msymbol (msymbol);
912 else if (!have_full_symbols () && !have_partial_symbols ())
913 error (_("No symbol table is loaded. Use the \"file\" command."));
915 error (_("No symbol \"%s\" in current context."),
916 copy_name ($1.stoken));
921 space_identifier : '@' NAME
922 { push_type_address_space (copy_name ($2.stoken));
923 push_type (tp_space_identifier);
927 const_or_volatile: const_or_volatile_noopt
931 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
934 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
935 | const_or_volatile_noopt
938 const_or_volatile_or_space_identifier:
939 const_or_volatile_or_space_identifier_noopt
944 { push_type (tp_pointer); $$ = 0; }
946 { push_type (tp_pointer); $$ = $2; }
948 { push_type (tp_reference); $$ = 0; }
950 { push_type (tp_reference); $$ = $2; }
954 direct_abs_decl: '(' abs_decl ')'
956 | direct_abs_decl array_mod
959 push_type (tp_array);
964 push_type (tp_array);
968 | direct_abs_decl func_mod
969 { push_type (tp_function); }
971 { push_type (tp_function); }
982 | '(' nonempty_typelist ')'
983 { free ($2); $$ = 0; }
986 /* We used to try to recognize pointer to member types here, but
987 that didn't work (shift/reduce conflicts meant that these rules never
988 got executed). The problem is that
989 int (foo::bar::baz::bizzle)
990 is a function type but
991 int (foo::bar::baz::bizzle::*)
992 is a pointer to member type. Stroustrup loses again! */
997 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1001 { $$ = lookup_signed_typename (parse_language,
1005 { $$ = lookup_signed_typename (parse_language,
1009 { $$ = lookup_signed_typename (parse_language,
1013 { $$ = lookup_signed_typename (parse_language,
1016 | LONG SIGNED_KEYWORD INT_KEYWORD
1017 { $$ = lookup_signed_typename (parse_language,
1020 | LONG SIGNED_KEYWORD
1021 { $$ = lookup_signed_typename (parse_language,
1024 | SIGNED_KEYWORD LONG INT_KEYWORD
1025 { $$ = lookup_signed_typename (parse_language,
1028 | UNSIGNED LONG INT_KEYWORD
1029 { $$ = lookup_unsigned_typename (parse_language,
1032 | LONG UNSIGNED INT_KEYWORD
1033 { $$ = lookup_unsigned_typename (parse_language,
1037 { $$ = lookup_unsigned_typename (parse_language,
1041 { $$ = lookup_signed_typename (parse_language,
1044 | LONG LONG INT_KEYWORD
1045 { $$ = lookup_signed_typename (parse_language,
1048 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1049 { $$ = lookup_signed_typename (parse_language,
1052 | LONG LONG SIGNED_KEYWORD
1053 { $$ = lookup_signed_typename (parse_language,
1056 | SIGNED_KEYWORD LONG LONG
1057 { $$ = lookup_signed_typename (parse_language,
1060 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1061 { $$ = lookup_signed_typename (parse_language,
1064 | UNSIGNED LONG LONG
1065 { $$ = lookup_unsigned_typename (parse_language,
1068 | UNSIGNED LONG LONG INT_KEYWORD
1069 { $$ = lookup_unsigned_typename (parse_language,
1072 | LONG LONG UNSIGNED
1073 { $$ = lookup_unsigned_typename (parse_language,
1076 | LONG LONG UNSIGNED INT_KEYWORD
1077 { $$ = lookup_unsigned_typename (parse_language,
1081 { $$ = lookup_signed_typename (parse_language,
1084 | SHORT SIGNED_KEYWORD INT_KEYWORD
1085 { $$ = lookup_signed_typename (parse_language,
1088 | SHORT SIGNED_KEYWORD
1089 { $$ = lookup_signed_typename (parse_language,
1092 | UNSIGNED SHORT INT_KEYWORD
1093 { $$ = lookup_unsigned_typename (parse_language,
1097 { $$ = lookup_unsigned_typename (parse_language,
1100 | SHORT UNSIGNED INT_KEYWORD
1101 { $$ = lookup_unsigned_typename (parse_language,
1105 { $$ = lookup_typename (parse_language, parse_gdbarch,
1106 "double", (struct block *) NULL,
1108 | LONG DOUBLE_KEYWORD
1109 { $$ = lookup_typename (parse_language, parse_gdbarch,
1111 (struct block *) NULL, 0); }
1113 { $$ = lookup_struct (copy_name ($2),
1114 expression_context_block); }
1116 { $$ = lookup_struct (copy_name ($2),
1117 expression_context_block); }
1119 { $$ = lookup_union (copy_name ($2),
1120 expression_context_block); }
1122 { $$ = lookup_enum (copy_name ($2),
1123 expression_context_block); }
1125 { $$ = lookup_unsigned_typename (parse_language,
1127 TYPE_NAME($2.type)); }
1129 { $$ = lookup_unsigned_typename (parse_language,
1132 | SIGNED_KEYWORD typename
1133 { $$ = lookup_signed_typename (parse_language,
1135 TYPE_NAME($2.type)); }
1137 { $$ = lookup_signed_typename (parse_language,
1140 /* It appears that this rule for templates is never
1141 reduced; template recognition happens by lookahead
1142 in the token processing code in yylex. */
1143 | TEMPLATE name '<' type '>'
1144 { $$ = lookup_template_type(copy_name($2), $4,
1145 expression_context_block);
1147 | const_or_volatile_or_space_identifier_noopt typebase
1148 { $$ = follow_types ($2); }
1149 | typebase const_or_volatile_or_space_identifier_noopt
1150 { $$ = follow_types ($1); }
1156 $$.stoken.ptr = "int";
1157 $$.stoken.length = 3;
1158 $$.type = lookup_signed_typename (parse_language,
1164 $$.stoken.ptr = "long";
1165 $$.stoken.length = 4;
1166 $$.type = lookup_signed_typename (parse_language,
1172 $$.stoken.ptr = "short";
1173 $$.stoken.length = 5;
1174 $$.type = lookup_signed_typename (parse_language,
1182 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1183 $<ivec>$[0] = 1; /* Number of types in vector */
1186 | nonempty_typelist ',' type
1187 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1188 $$ = (struct type **) realloc ((char *) $1, len);
1189 $$[$<ivec>$[0]] = $3;
1194 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1195 { $$ = follow_types ($1); }
1198 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1199 | VOLATILE_KEYWORD CONST_KEYWORD
1202 const_or_volatile_noopt: const_and_volatile
1203 { push_type (tp_const);
1204 push_type (tp_volatile);
1207 { push_type (tp_const); }
1209 { push_type (tp_volatile); }
1212 operator: OPERATOR NEW
1213 { $$ = operator_stoken (" new"); }
1215 { $$ = operator_stoken (" delete"); }
1216 | OPERATOR NEW '[' ']'
1217 { $$ = operator_stoken (" new[]"); }
1218 | OPERATOR DELETE '[' ']'
1219 { $$ = operator_stoken (" delete[]"); }
1221 { $$ = operator_stoken ("+"); }
1223 { $$ = operator_stoken ("-"); }
1225 { $$ = operator_stoken ("*"); }
1227 { $$ = operator_stoken ("/"); }
1229 { $$ = operator_stoken ("%"); }
1231 { $$ = operator_stoken ("^"); }
1233 { $$ = operator_stoken ("&"); }
1235 { $$ = operator_stoken ("|"); }
1237 { $$ = operator_stoken ("~"); }
1239 { $$ = operator_stoken ("!"); }
1241 { $$ = operator_stoken ("="); }
1243 { $$ = operator_stoken ("<"); }
1245 { $$ = operator_stoken (">"); }
1246 | OPERATOR ASSIGN_MODIFY
1247 { const char *op = "unknown";
1271 case BINOP_BITWISE_IOR:
1274 case BINOP_BITWISE_AND:
1277 case BINOP_BITWISE_XOR:
1284 $$ = operator_stoken (op);
1287 { $$ = operator_stoken ("<<"); }
1289 { $$ = operator_stoken (">>"); }
1291 { $$ = operator_stoken ("=="); }
1293 { $$ = operator_stoken ("!="); }
1295 { $$ = operator_stoken ("<="); }
1297 { $$ = operator_stoken (">="); }
1299 { $$ = operator_stoken ("&&"); }
1301 { $$ = operator_stoken ("||"); }
1302 | OPERATOR INCREMENT
1303 { $$ = operator_stoken ("++"); }
1304 | OPERATOR DECREMENT
1305 { $$ = operator_stoken ("--"); }
1307 { $$ = operator_stoken (","); }
1308 | OPERATOR ARROW_STAR
1309 { $$ = operator_stoken ("->*"); }
1311 { $$ = operator_stoken ("->"); }
1313 { $$ = operator_stoken ("()"); }
1315 { $$ = operator_stoken ("[]"); }
1319 struct ui_file *buf = mem_fileopen ();
1321 c_print_type ($2, NULL, buf, -1, 0);
1322 name = ui_file_xstrdup (buf, &length);
1323 ui_file_delete (buf);
1324 $$ = operator_stoken (name);
1331 name : NAME { $$ = $1.stoken; }
1332 | BLOCKNAME { $$ = $1.stoken; }
1333 | TYPENAME { $$ = $1.stoken; }
1334 | NAME_OR_INT { $$ = $1.stoken; }
1335 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1336 | operator { $$ = $1; }
1339 name_not_typename : NAME
1341 /* These would be useful if name_not_typename was useful, but it is just
1342 a fake for "variable", so these cause reduce/reduce conflicts because
1343 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1344 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1345 context where only a name could occur, this might be useful.
1351 $$.sym = lookup_symbol ($1.ptr,
1352 expression_context_block,
1354 &$$.is_a_field_of_this);
1361 /* Returns a stoken of the operator name given by OP (which does not
1362 include the string "operator"). */
1363 static struct stoken
1364 operator_stoken (const char *op)
1366 static const char *operator_string = "operator";
1367 struct stoken st = { NULL, 0 };
1368 st.length = strlen (operator_string) + strlen (op);
1369 st.ptr = malloc (st.length + 1);
1370 strcpy (st.ptr, operator_string);
1371 strcat (st.ptr, op);
1373 /* The toplevel (c_parse) will free the memory allocated here. */
1374 make_cleanup (free, st.ptr);
1378 /* Take care of parsing a number (anything that starts with a digit).
1379 Set yylval and return the token type; update lexptr.
1380 LEN is the number of characters in it. */
1382 /*** Needs some error checking for the float case ***/
1385 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1387 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1388 here, and we do kind of silly things like cast to unsigned. */
1395 int base = input_radix;
1398 /* Number of "L" suffixes encountered. */
1401 /* We have found a "L" or "U" suffix. */
1402 int found_suffix = 0;
1405 struct type *signed_type;
1406 struct type *unsigned_type;
1413 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1414 point. Return DECFLOAT. */
1416 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1419 putithere->typed_val_decfloat.type
1420 = parse_type->builtin_decfloat;
1421 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1422 gdbarch_byte_order (parse_gdbarch), p);
1427 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1430 putithere->typed_val_decfloat.type
1431 = parse_type->builtin_decdouble;
1432 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1433 gdbarch_byte_order (parse_gdbarch), p);
1438 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1441 putithere->typed_val_decfloat.type
1442 = parse_type->builtin_declong;
1443 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1444 gdbarch_byte_order (parse_gdbarch), p);
1449 if (! parse_c_float (parse_gdbarch, p, len,
1450 &putithere->typed_val_float.dval,
1451 &putithere->typed_val_float.type))
1456 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1500 if (c >= 'A' && c <= 'Z')
1502 if (c != 'l' && c != 'u')
1504 if (c >= '0' && c <= '9')
1512 if (base > 10 && c >= 'a' && c <= 'f')
1516 n += i = c - 'a' + 10;
1529 return ERROR; /* Char not a digit */
1532 return ERROR; /* Invalid digit in this base */
1534 /* Portably test for overflow (only works for nonzero values, so make
1535 a second check for zero). FIXME: Can't we just make n and prevn
1536 unsigned and avoid this? */
1537 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1538 unsigned_p = 1; /* Try something unsigned */
1540 /* Portably test for unsigned overflow.
1541 FIXME: This check is wrong; for example it doesn't find overflow
1542 on 0x123456789 when LONGEST is 32 bits. */
1543 if (c != 'l' && c != 'u' && n != 0)
1545 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1546 error (_("Numeric constant too large."));
1551 /* An integer constant is an int, a long, or a long long. An L
1552 suffix forces it to be long; an LL suffix forces it to be long
1553 long. If not forced to a larger size, it gets the first type of
1554 the above that it fits in. To figure out whether it fits, we
1555 shift it right and see whether anything remains. Note that we
1556 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1557 operation, because many compilers will warn about such a shift
1558 (which always produces a zero result). Sometimes gdbarch_int_bit
1559 or gdbarch_long_bit will be that big, sometimes not. To deal with
1560 the case where it is we just always shift the value more than
1561 once, with fewer bits each time. */
1563 un = (ULONGEST)n >> 2;
1565 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1567 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1569 /* A large decimal (not hex or octal) constant (between INT_MAX
1570 and UINT_MAX) is a long or unsigned long, according to ANSI,
1571 never an unsigned int, but this code treats it as unsigned
1572 int. This probably should be fixed. GCC gives a warning on
1575 unsigned_type = parse_type->builtin_unsigned_int;
1576 signed_type = parse_type->builtin_int;
1578 else if (long_p <= 1
1579 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1581 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1582 unsigned_type = parse_type->builtin_unsigned_long;
1583 signed_type = parse_type->builtin_long;
1588 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1589 < gdbarch_long_long_bit (parse_gdbarch))
1590 /* A long long does not fit in a LONGEST. */
1591 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1593 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1594 high_bit = (ULONGEST) 1 << shift;
1595 unsigned_type = parse_type->builtin_unsigned_long_long;
1596 signed_type = parse_type->builtin_long_long;
1599 putithere->typed_val_int.val = n;
1601 /* If the high bit of the worked out type is set then this number
1602 has to be unsigned. */
1604 if (unsigned_p || (n & high_bit))
1606 putithere->typed_val_int.type = unsigned_type;
1610 putithere->typed_val_int.type = signed_type;
1616 /* Temporary obstack used for holding strings. */
1617 static struct obstack tempbuf;
1618 static int tempbuf_init;
1620 /* Parse a C escape sequence. The initial backslash of the sequence
1621 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1622 last character of the sequence. If OUTPUT is not NULL, the
1623 translated form of the escape sequence will be written there. If
1624 OUTPUT is NULL, no output is written and the call will only affect
1625 *PTR. If an escape sequence is expressed in target bytes, then the
1626 entire sequence will simply be copied to OUTPUT. Return 1 if any
1627 character was emitted, 0 otherwise. */
1630 c_parse_escape (char **ptr, struct obstack *output)
1632 char *tokptr = *ptr;
1635 /* Some escape sequences undergo character set conversion. Those we
1639 /* Hex escapes do not undergo character set conversion, so keep
1640 the escape sequence for later. */
1643 obstack_grow_str (output, "\\x");
1645 if (!isxdigit (*tokptr))
1646 error (_("\\x escape without a following hex digit"));
1647 while (isxdigit (*tokptr))
1650 obstack_1grow (output, *tokptr);
1655 /* Octal escapes do not undergo character set conversion, so
1656 keep the escape sequence for later. */
1668 obstack_grow_str (output, "\\");
1670 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1674 obstack_1grow (output, *tokptr);
1680 /* We handle UCNs later. We could handle them here, but that
1681 would mean a spurious error in the case where the UCN could
1682 be converted to the target charset but not the host
1688 int i, len = c == 'U' ? 8 : 4;
1691 obstack_1grow (output, '\\');
1692 obstack_1grow (output, *tokptr);
1695 if (!isxdigit (*tokptr))
1696 error (_("\\%c escape without a following hex digit"), c);
1697 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1700 obstack_1grow (output, *tokptr);
1706 /* We must pass backslash through so that it does not
1707 cause quoting during the second expansion. */
1710 obstack_grow_str (output, "\\\\");
1714 /* Escapes which undergo conversion. */
1717 obstack_1grow (output, '\a');
1722 obstack_1grow (output, '\b');
1727 obstack_1grow (output, '\f');
1732 obstack_1grow (output, '\n');
1737 obstack_1grow (output, '\r');
1742 obstack_1grow (output, '\t');
1747 obstack_1grow (output, '\v');
1751 /* GCC extension. */
1754 obstack_1grow (output, HOST_ESCAPE_CHAR);
1758 /* Backslash-newline expands to nothing at all. */
1764 /* A few escapes just expand to the character itself. */
1768 /* GCC extensions. */
1773 /* Unrecognized escapes turn into the character itself. */
1776 obstack_1grow (output, *tokptr);
1784 /* Parse a string or character literal from TOKPTR. The string or
1785 character may be wide or unicode. *OUTPTR is set to just after the
1786 end of the literal in the input string. The resulting token is
1787 stored in VALUE. This returns a token value, either STRING or
1788 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1789 number of host characters in the literal. */
1791 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1795 enum c_string_type type;
1797 /* Build the gdb internal form of the input string in tempbuf. Note
1798 that the buffer is null byte terminated *only* for the
1799 convenience of debugging gdb itself and printing the buffer
1800 contents when the buffer contains no embedded nulls. Gdb does
1801 not depend upon the buffer being null byte terminated, it uses
1802 the length string instead. This allows gdb to handle C strings
1803 (as well as strings in other languages) with embedded null
1809 obstack_free (&tempbuf, NULL);
1810 obstack_init (&tempbuf);
1812 /* Record the string type. */
1815 type = C_WIDE_STRING;
1818 else if (*tokptr == 'u')
1823 else if (*tokptr == 'U')
1831 /* Skip the quote. */
1845 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1847 else if (c == quote)
1851 obstack_1grow (&tempbuf, c);
1853 /* FIXME: this does the wrong thing with multi-byte host
1854 characters. We could use mbrlen here, but that would
1855 make "set host-charset" a bit less useful. */
1860 if (*tokptr != quote)
1863 error (_("Unterminated string in expression."));
1865 error (_("Unmatched single quote."));
1870 value->ptr = obstack_base (&tempbuf);
1871 value->length = obstack_object_size (&tempbuf);
1875 return quote == '"' ? STRING : CHAR;
1882 enum exp_opcode opcode;
1886 static const struct token tokentab3[] =
1888 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1889 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
1890 {"->*", ARROW_STAR, BINOP_END, 1}
1893 static const struct token tokentab2[] =
1895 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1896 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1897 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1898 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
1899 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
1900 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
1901 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
1902 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
1903 {"++", INCREMENT, BINOP_END, 0},
1904 {"--", DECREMENT, BINOP_END, 0},
1905 {"->", ARROW, BINOP_END, 0},
1906 {"&&", ANDAND, BINOP_END, 0},
1907 {"||", OROR, BINOP_END, 0},
1908 /* "::" is *not* only C++: gdb overrides its meaning in several
1909 different ways, e.g., 'filename'::func, function::variable. */
1910 {"::", COLONCOLON, BINOP_END, 0},
1911 {"<<", LSH, BINOP_END, 0},
1912 {">>", RSH, BINOP_END, 0},
1913 {"==", EQUAL, BINOP_END, 0},
1914 {"!=", NOTEQUAL, BINOP_END, 0},
1915 {"<=", LEQ, BINOP_END, 0},
1916 {">=", GEQ, BINOP_END, 0},
1917 {".*", DOT_STAR, BINOP_END, 1}
1920 /* Identifier-like tokens. */
1921 static const struct token ident_tokens[] =
1923 {"unsigned", UNSIGNED, OP_NULL, 0},
1924 {"template", TEMPLATE, OP_NULL, 1},
1925 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
1926 {"struct", STRUCT, OP_NULL, 0},
1927 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
1928 {"sizeof", SIZEOF, OP_NULL, 0},
1929 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
1930 {"false", FALSEKEYWORD, OP_NULL, 1},
1931 {"class", CLASS, OP_NULL, 1},
1932 {"union", UNION, OP_NULL, 0},
1933 {"short", SHORT, OP_NULL, 0},
1934 {"const", CONST_KEYWORD, OP_NULL, 0},
1935 {"enum", ENUM, OP_NULL, 0},
1936 {"long", LONG, OP_NULL, 0},
1937 {"true", TRUEKEYWORD, OP_NULL, 1},
1938 {"int", INT_KEYWORD, OP_NULL, 0},
1939 {"new", NEW, OP_NULL, 1},
1940 {"delete", DELETE, OP_NULL, 1},
1941 {"operator", OPERATOR, OP_NULL, 1},
1943 {"and", ANDAND, BINOP_END, 1},
1944 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
1945 {"bitand", '&', OP_NULL, 1},
1946 {"bitor", '|', OP_NULL, 1},
1947 {"compl", '~', OP_NULL, 1},
1948 {"not", '!', OP_NULL, 1},
1949 {"not_eq", NOTEQUAL, BINOP_END, 1},
1950 {"or", OROR, BINOP_END, 1},
1951 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
1952 {"xor", '^', OP_NULL, 1},
1953 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1},
1955 {"const_cast", CONST_CAST, OP_NULL, 1 },
1956 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, 1 },
1957 {"static_cast", STATIC_CAST, OP_NULL, 1 },
1958 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, 1 }
1961 /* When we find that lexptr (the global var defined in parse.c) is
1962 pointing at a macro invocation, we expand the invocation, and call
1963 scan_macro_expansion to save the old lexptr here and point lexptr
1964 into the expanded text. When we reach the end of that, we call
1965 end_macro_expansion to pop back to the value we saved here. The
1966 macro expansion code promises to return only fully-expanded text,
1967 so we don't need to "push" more than one level.
1969 This is disgusting, of course. It would be cleaner to do all macro
1970 expansion beforehand, and then hand that to lexptr. But we don't
1971 really know where the expression ends. Remember, in a command like
1973 (gdb) break *ADDRESS if CONDITION
1975 we evaluate ADDRESS in the scope of the current frame, but we
1976 evaluate CONDITION in the scope of the breakpoint's location. So
1977 it's simply wrong to try to macro-expand the whole thing at once. */
1978 static char *macro_original_text;
1980 /* We save all intermediate macro expansions on this obstack for the
1981 duration of a single parse. The expansion text may sometimes have
1982 to live past the end of the expansion, due to yacc lookahead.
1983 Rather than try to be clever about saving the data for a single
1984 token, we simply keep it all and delete it after parsing has
1986 static struct obstack expansion_obstack;
1989 scan_macro_expansion (char *expansion)
1993 /* We'd better not be trying to push the stack twice. */
1994 gdb_assert (! macro_original_text);
1996 /* Copy to the obstack, and then free the intermediate
1998 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2001 /* Save the old lexptr value, so we can return to it when we're done
2002 parsing the expanded text. */
2003 macro_original_text = lexptr;
2009 scanning_macro_expansion (void)
2011 return macro_original_text != 0;
2016 finished_macro_expansion (void)
2018 /* There'd better be something to pop back to. */
2019 gdb_assert (macro_original_text);
2021 /* Pop back to the original text. */
2022 lexptr = macro_original_text;
2023 macro_original_text = 0;
2028 scan_macro_cleanup (void *dummy)
2030 if (macro_original_text)
2031 finished_macro_expansion ();
2033 obstack_free (&expansion_obstack, NULL);
2036 /* Return true iff the token represents a C++ cast operator. */
2039 is_cast_operator (const char *token, int len)
2041 return (! strncmp (token, "dynamic_cast", len)
2042 || ! strncmp (token, "static_cast", len)
2043 || ! strncmp (token, "reinterpret_cast", len)
2044 || ! strncmp (token, "const_cast", len));
2047 /* The scope used for macro expansion. */
2048 static struct macro_scope *expression_macro_scope;
2050 /* This is set if a NAME token appeared at the very end of the input
2051 string, with no whitespace separating the name from the EOF. This
2052 is used only when parsing to do field name completion. */
2053 static int saw_name_at_eof;
2055 /* This is set if the previously-returned token was a structure
2056 operator -- either '.' or ARROW. This is used only when parsing to
2057 do field name completion. */
2058 static int last_was_structop;
2060 /* Read one token, getting characters through lexptr. */
2063 lex_one_token (void)
2069 int saw_structop = last_was_structop;
2072 last_was_structop = 0;
2076 /* Check if this is a macro invocation that we need to expand. */
2077 if (! scanning_macro_expansion ())
2079 char *expanded = macro_expand_next (&lexptr,
2080 standard_macro_lookup,
2081 expression_macro_scope);
2084 scan_macro_expansion (expanded);
2087 prev_lexptr = lexptr;
2090 /* See if it is a special token of length 3. */
2091 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2092 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2094 if (tokentab3[i].cxx_only
2095 && parse_language->la_language != language_cplus)
2099 yylval.opcode = tokentab3[i].opcode;
2100 return tokentab3[i].token;
2103 /* See if it is a special token of length 2. */
2104 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2105 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2107 if (tokentab2[i].cxx_only
2108 && parse_language->la_language != language_cplus)
2112 yylval.opcode = tokentab2[i].opcode;
2113 if (in_parse_field && tokentab2[i].token == ARROW)
2114 last_was_structop = 1;
2115 return tokentab2[i].token;
2118 switch (c = *tokstart)
2121 /* If we were just scanning the result of a macro expansion,
2122 then we need to resume scanning the original text.
2123 If we're parsing for field name completion, and the previous
2124 token allows such completion, return a COMPLETE token.
2125 Otherwise, we were already scanning the original text, and
2126 we're really done. */
2127 if (scanning_macro_expansion ())
2129 finished_macro_expansion ();
2132 else if (saw_name_at_eof)
2134 saw_name_at_eof = 0;
2137 else if (saw_structop)
2156 if (paren_depth == 0)
2163 if (comma_terminates
2165 && ! scanning_macro_expansion ())
2171 /* Might be a floating point number. */
2172 if (lexptr[1] < '0' || lexptr[1] > '9')
2175 last_was_structop = 1;
2176 goto symbol; /* Nope, must be a symbol. */
2178 /* FALL THRU into number case. */
2191 /* It's a number. */
2192 int got_dot = 0, got_e = 0, toktype;
2194 int hex = input_radix > 10;
2196 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2201 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2209 /* This test includes !hex because 'e' is a valid hex digit
2210 and thus does not indicate a floating point number when
2211 the radix is hex. */
2212 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2213 got_dot = got_e = 1;
2214 /* This test does not include !hex, because a '.' always indicates
2215 a decimal floating point number regardless of the radix. */
2216 else if (!got_dot && *p == '.')
2218 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2219 && (*p == '-' || *p == '+'))
2220 /* This is the sign of the exponent, not the end of the
2223 /* We will take any letters or digits. parse_number will
2224 complain if past the radix, or if L or U are not final. */
2225 else if ((*p < '0' || *p > '9')
2226 && ((*p < 'a' || *p > 'z')
2227 && (*p < 'A' || *p > 'Z')))
2230 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2231 if (toktype == ERROR)
2233 char *err_copy = (char *) alloca (p - tokstart + 1);
2235 memcpy (err_copy, tokstart, p - tokstart);
2236 err_copy[p - tokstart] = 0;
2237 error (_("Invalid number \"%s\"."), err_copy);
2245 char *p = &tokstart[1];
2246 size_t len = strlen ("entry");
2248 while (isspace (*p))
2250 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2282 if (tokstart[1] != '"' && tokstart[1] != '\'')
2289 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2294 error (_("Empty character constant."));
2295 else if (host_len > 2 && c == '\'')
2298 namelen = lexptr - tokstart - 1;
2301 else if (host_len > 1)
2302 error (_("Invalid character constant."));
2308 if (!(c == '_' || c == '$'
2309 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2310 /* We must have come across a bad character (e.g. ';'). */
2311 error (_("Invalid character '%c' in expression."), c);
2313 /* It's a name. See how long it is. */
2315 for (c = tokstart[namelen];
2316 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2317 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2319 /* Template parameter lists are part of the name.
2320 FIXME: This mishandles `print $a<4&&$a>3'. */
2324 if (! is_cast_operator (tokstart, namelen))
2326 /* Scan ahead to get rest of the template specification. Note
2327 that we look ahead only when the '<' adjoins non-whitespace
2328 characters; for comparison expressions, e.g. "a < b > c",
2329 there must be spaces before the '<', etc. */
2331 char * p = find_template_name_end (tokstart + namelen);
2333 namelen = p - tokstart;
2337 c = tokstart[++namelen];
2340 /* The token "if" terminates the expression and is NOT removed from
2341 the input stream. It doesn't count if it appears in the
2342 expansion of a macro. */
2344 && tokstart[0] == 'i'
2345 && tokstart[1] == 'f'
2346 && ! scanning_macro_expansion ())
2351 /* For the same reason (breakpoint conditions), "thread N"
2352 terminates the expression. "thread" could be an identifier, but
2353 an identifier is never followed by a number without intervening
2354 punctuation. "task" is similar. Handle abbreviations of these,
2355 similarly to breakpoint.c:find_condition_and_thread. */
2357 && (strncmp (tokstart, "thread", namelen) == 0
2358 || strncmp (tokstart, "task", namelen) == 0)
2359 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2360 && ! scanning_macro_expansion ())
2362 char *p = tokstart + namelen + 1;
2363 while (*p == ' ' || *p == '\t')
2365 if (*p >= '0' && *p <= '9')
2373 yylval.sval.ptr = tokstart;
2374 yylval.sval.length = namelen;
2376 /* Catch specific keywords. */
2377 copy = copy_name (yylval.sval);
2378 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2379 if (strcmp (copy, ident_tokens[i].operator) == 0)
2381 if (ident_tokens[i].cxx_only
2382 && parse_language->la_language != language_cplus)
2385 /* It is ok to always set this, even though we don't always
2386 strictly need to. */
2387 yylval.opcode = ident_tokens[i].opcode;
2388 return ident_tokens[i].token;
2391 if (*tokstart == '$')
2394 if (in_parse_field && *lexptr == '\0')
2395 saw_name_at_eof = 1;
2399 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2406 DEF_VEC_O (token_and_value);
2408 /* A FIFO of tokens that have been read but not yet returned to the
2410 static VEC (token_and_value) *token_fifo;
2412 /* Non-zero if the lexer should return tokens from the FIFO. */
2415 /* Temporary storage for c_lex; this holds symbol names as they are
2417 static struct obstack name_obstack;
2419 /* Classify a NAME token. The contents of the token are in `yylval'.
2420 Updates yylval and returns the new token type. BLOCK is the block
2421 in which lookups start; this can be NULL to mean the global
2424 classify_name (struct block *block)
2428 int is_a_field_of_this = 0;
2430 copy = copy_name (yylval.sval);
2432 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2433 parse_language->la_language == language_cplus
2434 ? &is_a_field_of_this : (int *) NULL);
2436 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2438 yylval.ssym.sym = sym;
2439 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2444 /* See if it's a file name. */
2445 struct symtab *symtab;
2447 symtab = lookup_symtab (copy);
2450 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2455 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2457 yylval.tsym.type = SYMBOL_TYPE (sym);
2462 = language_lookup_primitive_type_by_name (parse_language,
2463 parse_gdbarch, copy);
2464 if (yylval.tsym.type != NULL)
2467 /* Input names that aren't symbols but ARE valid hex numbers, when
2468 the input radix permits them, can be names or numbers depending
2469 on the parse. Note we support radixes > 16 here. */
2471 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2472 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2474 YYSTYPE newlval; /* Its value is ignored. */
2475 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2478 yylval.ssym.sym = sym;
2479 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2484 /* Any other kind of symbol */
2485 yylval.ssym.sym = sym;
2486 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2489 && parse_language->la_language == language_cplus
2490 && !is_a_field_of_this
2491 && !lookup_minimal_symbol (copy, NULL, NULL))
2492 return UNKNOWN_CPP_NAME;
2497 /* Like classify_name, but used by the inner loop of the lexer, when a
2498 name might have already been seen. FIRST_NAME is true if the token
2499 in `yylval' is the first component of a name, false otherwise. If
2500 this function returns NAME, it might not have updated `yylval'.
2501 This is ok because the caller only cares about TYPENAME. */
2503 classify_inner_name (struct block *block, int first_name)
2505 struct type *type, *new_type;
2509 return classify_name (block);
2511 type = check_typedef (yylval.tsym.type);
2512 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2513 && TYPE_CODE (type) != TYPE_CODE_UNION
2514 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2515 /* We know the caller won't expect us to update yylval. */
2518 copy = copy_name (yylval.tsym.stoken);
2519 new_type = cp_lookup_nested_type (yylval.tsym.type, copy, block);
2521 if (new_type == NULL)
2522 /* We know the caller won't expect us to update yylval. */
2525 yylval.tsym.type = new_type;
2529 /* The outer level of a two-level lexer. This calls the inner lexer
2530 to return tokens. It then either returns these tokens, or
2531 aggregates them into a larger token. This lets us work around a
2532 problem in our parsing approach, where the parser could not
2533 distinguish between qualified names and qualified types at the
2536 This approach is still not ideal, because it mishandles template
2537 types. See the comment in lex_one_token for an example. However,
2538 this is still an improvement over the earlier approach, and will
2539 suffice until we move to better parsing technology. */
2543 token_and_value current;
2544 int first_was_coloncolon, last_was_coloncolon, first_iter;
2546 if (popping && !VEC_empty (token_and_value, token_fifo))
2548 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2549 VEC_ordered_remove (token_and_value, token_fifo, 0);
2555 current.token = lex_one_token ();
2556 if (current.token == NAME)
2557 current.token = classify_name (expression_context_block);
2558 if (parse_language->la_language != language_cplus
2559 || (current.token != TYPENAME && current.token != COLONCOLON))
2560 return current.token;
2562 first_was_coloncolon = current.token == COLONCOLON;
2563 last_was_coloncolon = first_was_coloncolon;
2564 obstack_free (&name_obstack, obstack_base (&name_obstack));
2565 if (!last_was_coloncolon)
2566 obstack_grow (&name_obstack, yylval.sval.ptr, yylval.sval.length);
2567 current.value = yylval;
2571 token_and_value next;
2573 next.token = lex_one_token ();
2574 next.value = yylval;
2576 if (next.token == NAME && last_was_coloncolon)
2580 classification = classify_inner_name (first_was_coloncolon
2582 : expression_context_block,
2584 /* We keep going until we either run out of names, or until
2585 we have a qualified name which is not a type. */
2586 if (classification != TYPENAME)
2588 /* Push the final component and leave the loop. */
2589 VEC_safe_push (token_and_value, token_fifo, &next);
2593 /* Update the partial name we are constructing. */
2596 /* We don't want to put a leading "::" into the name. */
2597 obstack_grow_str (&name_obstack, "::");
2599 obstack_grow (&name_obstack, next.value.sval.ptr,
2600 next.value.sval.length);
2602 yylval.sval.ptr = obstack_base (&name_obstack);
2603 yylval.sval.length = obstack_object_size (&name_obstack);
2604 current.value = yylval;
2605 current.token = classification;
2607 last_was_coloncolon = 0;
2609 else if (next.token == COLONCOLON && !last_was_coloncolon)
2610 last_was_coloncolon = 1;
2613 /* We've reached the end of the name. */
2614 VEC_safe_push (token_and_value, token_fifo, &next);
2623 /* If we ended with a "::", insert it too. */
2624 if (last_was_coloncolon)
2627 memset (&cc, 0, sizeof (token_and_value));
2628 if (first_was_coloncolon && first_iter)
2633 cc.token = COLONCOLON;
2634 VEC_safe_insert (token_and_value, token_fifo, 0, &cc);
2637 yylval = current.value;
2638 yylval.sval.ptr = obstack_copy0 (&expansion_obstack,
2640 yylval.sval.length);
2641 return current.token;
2648 struct cleanup *back_to = make_cleanup (free_current_contents,
2649 &expression_macro_scope);
2651 /* Set up the scope for macro expansion. */
2652 expression_macro_scope = NULL;
2654 if (expression_context_block)
2655 expression_macro_scope
2656 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2658 expression_macro_scope = default_macro_scope ();
2659 if (! expression_macro_scope)
2660 expression_macro_scope = user_macro_scope ();
2662 /* Initialize macro expansion code. */
2663 obstack_init (&expansion_obstack);
2664 gdb_assert (! macro_original_text);
2665 make_cleanup (scan_macro_cleanup, 0);
2667 make_cleanup_restore_integer (&yydebug);
2668 yydebug = parser_debug;
2670 /* Initialize some state used by the lexer. */
2671 last_was_structop = 0;
2672 saw_name_at_eof = 0;
2674 VEC_free (token_and_value, token_fifo);
2676 obstack_init (&name_obstack);
2677 make_cleanup_obstack_free (&name_obstack);
2679 result = yyparse ();
2680 do_cleanups (back_to);
2689 lexptr = prev_lexptr;
2691 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);