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. */
190 %token <ssym> UNKNOWN_CPP_NAME
191 %token <voidval> COMPLETE
192 %token <tsym> TYPENAME
194 %type <svec> string_exp
195 %type <ssym> name_not_typename
196 %type <tsym> typename
198 /* It is UNKNOWN_CPP_NAME or ENTRY, depending on the context. */
199 %type <ssym> unknown_cpp_name
201 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
202 but which would parse as a valid number in the current input radix.
203 E.g. "c" when input_radix==16. Depending on the parse, it will be
204 turned into a name or into a number. */
206 %token <ssym> NAME_OR_INT
209 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
213 %type <sval> operator
214 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
216 /* Special type cases, put in to allow the parser to distinguish different
218 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
220 %token <sval> VARIABLE
222 %token <opcode> ASSIGN_MODIFY
231 %right '=' ASSIGN_MODIFY
239 %left '<' '>' LEQ GEQ
244 %right UNARY INCREMENT DECREMENT
245 %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
246 %token <ssym> BLOCKNAME
247 %token <bval> FILENAME
259 { write_exp_elt_opcode(OP_TYPE);
260 write_exp_elt_type($1);
261 write_exp_elt_opcode(OP_TYPE);}
264 /* Expressions, including the comma operator. */
267 { write_exp_elt_opcode (BINOP_COMMA); }
270 /* Expressions, not including the comma operator. */
271 exp : '*' exp %prec UNARY
272 { write_exp_elt_opcode (UNOP_IND); }
275 exp : '&' exp %prec UNARY
276 { write_exp_elt_opcode (UNOP_ADDR); }
279 exp : '-' exp %prec UNARY
280 { write_exp_elt_opcode (UNOP_NEG); }
283 exp : '+' exp %prec UNARY
284 { write_exp_elt_opcode (UNOP_PLUS); }
287 exp : '!' exp %prec UNARY
288 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
291 exp : '~' exp %prec UNARY
292 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
295 exp : INCREMENT exp %prec UNARY
296 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
299 exp : DECREMENT exp %prec UNARY
300 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
303 exp : exp INCREMENT %prec UNARY
304 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
307 exp : exp DECREMENT %prec UNARY
308 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
311 exp : SIZEOF exp %prec UNARY
312 { write_exp_elt_opcode (UNOP_SIZEOF); }
316 { write_exp_elt_opcode (STRUCTOP_PTR);
317 write_exp_string ($3);
318 write_exp_elt_opcode (STRUCTOP_PTR); }
321 exp : exp ARROW name COMPLETE
322 { mark_struct_expression ();
323 write_exp_elt_opcode (STRUCTOP_PTR);
324 write_exp_string ($3);
325 write_exp_elt_opcode (STRUCTOP_PTR); }
328 exp : exp ARROW COMPLETE
330 mark_struct_expression ();
331 write_exp_elt_opcode (STRUCTOP_PTR);
334 write_exp_string (s);
335 write_exp_elt_opcode (STRUCTOP_PTR); }
338 exp : exp ARROW qualified_name
339 { /* exp->type::name becomes exp->*(&type::name) */
340 /* Note: this doesn't work if name is a
341 static member! FIXME */
342 write_exp_elt_opcode (UNOP_ADDR);
343 write_exp_elt_opcode (STRUCTOP_MPTR); }
346 exp : exp ARROW_STAR exp
347 { write_exp_elt_opcode (STRUCTOP_MPTR); }
351 { write_exp_elt_opcode (STRUCTOP_STRUCT);
352 write_exp_string ($3);
353 write_exp_elt_opcode (STRUCTOP_STRUCT); }
356 exp : exp '.' name COMPLETE
357 { mark_struct_expression ();
358 write_exp_elt_opcode (STRUCTOP_STRUCT);
359 write_exp_string ($3);
360 write_exp_elt_opcode (STRUCTOP_STRUCT); }
363 exp : exp '.' COMPLETE
365 mark_struct_expression ();
366 write_exp_elt_opcode (STRUCTOP_STRUCT);
369 write_exp_string (s);
370 write_exp_elt_opcode (STRUCTOP_STRUCT); }
373 exp : exp '.' qualified_name
374 { /* exp.type::name becomes exp.*(&type::name) */
375 /* Note: this doesn't work if name is a
376 static member! FIXME */
377 write_exp_elt_opcode (UNOP_ADDR);
378 write_exp_elt_opcode (STRUCTOP_MEMBER); }
381 exp : exp DOT_STAR exp
382 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
385 exp : exp '[' exp1 ']'
386 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
390 /* This is to save the value of arglist_len
391 being accumulated by an outer function call. */
392 { start_arglist (); }
393 arglist ')' %prec ARROW
394 { write_exp_elt_opcode (OP_FUNCALL);
395 write_exp_elt_longcst ((LONGEST) end_arglist ());
396 write_exp_elt_opcode (OP_FUNCALL); }
399 exp : unknown_cpp_name '('
401 /* This could potentially be a an argument defined
402 lookup function (Koenig). */
403 write_exp_elt_opcode (OP_ADL_FUNC);
404 write_exp_elt_block (expression_context_block);
405 write_exp_elt_sym (NULL); /* Placeholder. */
406 write_exp_string ($1.stoken);
407 write_exp_elt_opcode (OP_ADL_FUNC);
409 /* This is to save the value of arglist_len
410 being accumulated by an outer function call. */
414 arglist ')' %prec ARROW
416 write_exp_elt_opcode (OP_FUNCALL);
417 write_exp_elt_longcst ((LONGEST) end_arglist ());
418 write_exp_elt_opcode (OP_FUNCALL);
422 unknown_cpp_name : UNKNOWN_CPP_NAME
427 { start_arglist (); }
437 arglist : arglist ',' exp %prec ABOVE_COMMA
441 exp : exp '(' nonempty_typelist ')' const_or_volatile
443 write_exp_elt_opcode (TYPE_INSTANCE);
444 write_exp_elt_longcst ((LONGEST) $<ivec>3[0]);
445 for (i = 0; i < $<ivec>3[0]; ++i)
446 write_exp_elt_type ($<tvec>3[i + 1]);
447 write_exp_elt_longcst((LONGEST) $<ivec>3[0]);
448 write_exp_elt_opcode (TYPE_INSTANCE);
454 { $$ = end_arglist () - 1; }
456 exp : lcurly arglist rcurly %prec ARROW
457 { write_exp_elt_opcode (OP_ARRAY);
458 write_exp_elt_longcst ((LONGEST) 0);
459 write_exp_elt_longcst ((LONGEST) $3);
460 write_exp_elt_opcode (OP_ARRAY); }
463 exp : lcurly type rcurly exp %prec UNARY
464 { write_exp_elt_opcode (UNOP_MEMVAL);
465 write_exp_elt_type ($2);
466 write_exp_elt_opcode (UNOP_MEMVAL); }
469 exp : '(' type ')' exp %prec UNARY
470 { write_exp_elt_opcode (UNOP_CAST);
471 write_exp_elt_type ($2);
472 write_exp_elt_opcode (UNOP_CAST); }
479 /* Binary operators in order of decreasing precedence. */
482 { write_exp_elt_opcode (BINOP_REPEAT); }
486 { write_exp_elt_opcode (BINOP_MUL); }
490 { write_exp_elt_opcode (BINOP_DIV); }
494 { write_exp_elt_opcode (BINOP_REM); }
498 { write_exp_elt_opcode (BINOP_ADD); }
502 { write_exp_elt_opcode (BINOP_SUB); }
506 { write_exp_elt_opcode (BINOP_LSH); }
510 { write_exp_elt_opcode (BINOP_RSH); }
514 { write_exp_elt_opcode (BINOP_EQUAL); }
517 exp : exp NOTEQUAL exp
518 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
522 { write_exp_elt_opcode (BINOP_LEQ); }
526 { write_exp_elt_opcode (BINOP_GEQ); }
530 { write_exp_elt_opcode (BINOP_LESS); }
534 { write_exp_elt_opcode (BINOP_GTR); }
538 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
542 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
546 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
550 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
554 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
557 exp : exp '?' exp ':' exp %prec '?'
558 { write_exp_elt_opcode (TERNOP_COND); }
562 { write_exp_elt_opcode (BINOP_ASSIGN); }
565 exp : exp ASSIGN_MODIFY exp
566 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
567 write_exp_elt_opcode ($2);
568 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
572 { write_exp_elt_opcode (OP_LONG);
573 write_exp_elt_type ($1.type);
574 write_exp_elt_longcst ((LONGEST)($1.val));
575 write_exp_elt_opcode (OP_LONG); }
580 struct stoken_vector vec;
583 write_exp_string_vector ($1.type, &vec);
589 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
590 write_exp_elt_opcode (OP_LONG);
591 write_exp_elt_type (val.typed_val_int.type);
592 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
593 write_exp_elt_opcode (OP_LONG);
599 { write_exp_elt_opcode (OP_DOUBLE);
600 write_exp_elt_type ($1.type);
601 write_exp_elt_dblcst ($1.dval);
602 write_exp_elt_opcode (OP_DOUBLE); }
606 { write_exp_elt_opcode (OP_DECFLOAT);
607 write_exp_elt_type ($1.type);
608 write_exp_elt_decfloatcst ($1.val);
609 write_exp_elt_opcode (OP_DECFLOAT); }
617 write_dollar_variable ($1);
621 exp : SIZEOF '(' type ')' %prec UNARY
622 { write_exp_elt_opcode (OP_LONG);
623 write_exp_elt_type (lookup_signed_typename
624 (parse_language, parse_gdbarch,
627 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
628 write_exp_elt_opcode (OP_LONG); }
631 exp : REINTERPRET_CAST '<' type '>' '(' exp ')' %prec UNARY
632 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST);
633 write_exp_elt_type ($3);
634 write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
637 exp : STATIC_CAST '<' type '>' '(' exp ')' %prec UNARY
638 { write_exp_elt_opcode (UNOP_CAST);
639 write_exp_elt_type ($3);
640 write_exp_elt_opcode (UNOP_CAST); }
643 exp : DYNAMIC_CAST '<' type '>' '(' exp ')' %prec UNARY
644 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST);
645 write_exp_elt_type ($3);
646 write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
649 exp : CONST_CAST '<' type '>' '(' exp ')' %prec UNARY
650 { /* We could do more error checking here, but
651 it doesn't seem worthwhile. */
652 write_exp_elt_opcode (UNOP_CAST);
653 write_exp_elt_type ($3);
654 write_exp_elt_opcode (UNOP_CAST); }
660 /* We copy the string here, and not in the
661 lexer, to guarantee that we do not leak a
662 string. Note that we follow the
663 NUL-termination convention of the
665 struct typed_stoken *vec = XNEW (struct typed_stoken);
670 vec->length = $1.length;
671 vec->ptr = malloc ($1.length + 1);
672 memcpy (vec->ptr, $1.ptr, $1.length + 1);
677 /* Note that we NUL-terminate here, but just
681 $$.tokens = realloc ($$.tokens,
682 $$.len * sizeof (struct typed_stoken));
684 p = malloc ($2.length + 1);
685 memcpy (p, $2.ptr, $2.length + 1);
687 $$.tokens[$$.len - 1].type = $2.type;
688 $$.tokens[$$.len - 1].length = $2.length;
689 $$.tokens[$$.len - 1].ptr = p;
696 enum c_string_type type = C_STRING;
698 for (i = 0; i < $1.len; ++i)
700 switch ($1.tokens[i].type)
708 && type != $1.tokens[i].type)
709 error (_("Undefined string concatenation."));
710 type = $1.tokens[i].type;
714 internal_error (__FILE__, __LINE__,
715 "unrecognized type in string concatenation");
719 write_exp_string_vector (type, &$1);
720 for (i = 0; i < $1.len; ++i)
721 free ($1.tokens[i].ptr);
728 { write_exp_elt_opcode (OP_LONG);
729 write_exp_elt_type (parse_type->builtin_bool);
730 write_exp_elt_longcst ((LONGEST) 1);
731 write_exp_elt_opcode (OP_LONG); }
735 { write_exp_elt_opcode (OP_LONG);
736 write_exp_elt_type (parse_type->builtin_bool);
737 write_exp_elt_longcst ((LONGEST) 0);
738 write_exp_elt_opcode (OP_LONG); }
746 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
748 error (_("No file or function \"%s\"."),
749 copy_name ($1.stoken));
757 block : block COLONCOLON name
759 = lookup_symbol (copy_name ($3), $1,
760 VAR_DOMAIN, (int *) NULL);
761 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
762 error (_("No function \"%s\" in specified context."),
764 $$ = SYMBOL_BLOCK_VALUE (tem); }
767 variable: name_not_typename '@' ENTRY
768 { struct symbol *sym = $1.sym;
770 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
771 || !symbol_read_needs_frame (sym))
772 error (_("@entry can be used only for function "
773 "parameters, not for \"%s\""),
774 copy_name ($1.stoken));
776 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
777 write_exp_elt_sym (sym);
778 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
782 variable: block COLONCOLON name
783 { struct symbol *sym;
784 sym = lookup_symbol (copy_name ($3), $1,
785 VAR_DOMAIN, (int *) NULL);
787 error (_("No symbol \"%s\" in specified context."),
790 write_exp_elt_opcode (OP_VAR_VALUE);
791 /* block_found is set by lookup_symbol. */
792 write_exp_elt_block (block_found);
793 write_exp_elt_sym (sym);
794 write_exp_elt_opcode (OP_VAR_VALUE); }
797 qualified_name: TYPENAME COLONCOLON name
799 struct type *type = $1.type;
800 CHECK_TYPEDEF (type);
801 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
802 && TYPE_CODE (type) != TYPE_CODE_UNION
803 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
804 error (_("`%s' is not defined as an aggregate type."),
807 write_exp_elt_opcode (OP_SCOPE);
808 write_exp_elt_type (type);
809 write_exp_string ($3);
810 write_exp_elt_opcode (OP_SCOPE);
812 | TYPENAME COLONCOLON '~' name
814 struct type *type = $1.type;
815 struct stoken tmp_token;
816 CHECK_TYPEDEF (type);
817 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
818 && TYPE_CODE (type) != TYPE_CODE_UNION
819 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
820 error (_("`%s' is not defined as an aggregate type."),
823 tmp_token.ptr = (char*) alloca ($4.length + 2);
824 tmp_token.length = $4.length + 1;
825 tmp_token.ptr[0] = '~';
826 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
827 tmp_token.ptr[tmp_token.length] = 0;
829 /* Check for valid destructor name. */
830 destructor_name_p (tmp_token.ptr, $1.type);
831 write_exp_elt_opcode (OP_SCOPE);
832 write_exp_elt_type (type);
833 write_exp_string (tmp_token);
834 write_exp_elt_opcode (OP_SCOPE);
836 | TYPENAME COLONCOLON name COLONCOLON name
838 char *copy = copy_name ($3);
839 error (_("No type \"%s\" within class "
840 "or namespace \"%s\"."),
841 copy, TYPE_NAME ($1.type));
845 variable: qualified_name
846 | COLONCOLON name_not_typename
848 char *name = copy_name ($2.stoken);
850 struct minimal_symbol *msymbol;
853 lookup_symbol (name, (const struct block *) NULL,
854 VAR_DOMAIN, (int *) NULL);
857 write_exp_elt_opcode (OP_VAR_VALUE);
858 write_exp_elt_block (NULL);
859 write_exp_elt_sym (sym);
860 write_exp_elt_opcode (OP_VAR_VALUE);
864 msymbol = lookup_minimal_symbol (name, NULL, NULL);
866 write_exp_msymbol (msymbol);
867 else if (!have_full_symbols () && !have_partial_symbols ())
868 error (_("No symbol table is loaded. Use the \"file\" command."));
870 error (_("No symbol \"%s\" in current context."), name);
874 variable: name_not_typename
875 { struct symbol *sym = $1.sym;
879 if (symbol_read_needs_frame (sym))
881 if (innermost_block == 0
882 || contained_in (block_found,
884 innermost_block = block_found;
887 write_exp_elt_opcode (OP_VAR_VALUE);
888 /* We want to use the selected frame, not
889 another more inner frame which happens to
890 be in the same block. */
891 write_exp_elt_block (NULL);
892 write_exp_elt_sym (sym);
893 write_exp_elt_opcode (OP_VAR_VALUE);
895 else if ($1.is_a_field_of_this)
897 /* C++: it hangs off of `this'. Must
898 not inadvertently convert from a method call
900 if (innermost_block == 0
901 || contained_in (block_found,
903 innermost_block = block_found;
904 write_exp_elt_opcode (OP_THIS);
905 write_exp_elt_opcode (OP_THIS);
906 write_exp_elt_opcode (STRUCTOP_PTR);
907 write_exp_string ($1.stoken);
908 write_exp_elt_opcode (STRUCTOP_PTR);
912 struct minimal_symbol *msymbol;
913 char *arg = copy_name ($1.stoken);
916 lookup_minimal_symbol (arg, NULL, NULL);
918 write_exp_msymbol (msymbol);
919 else if (!have_full_symbols () && !have_partial_symbols ())
920 error (_("No symbol table is loaded. Use the \"file\" command."));
922 error (_("No symbol \"%s\" in current context."),
923 copy_name ($1.stoken));
928 space_identifier : '@' NAME
929 { push_type_address_space (copy_name ($2.stoken));
930 push_type (tp_space_identifier);
934 const_or_volatile: const_or_volatile_noopt
938 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
941 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
942 | const_or_volatile_noopt
945 const_or_volatile_or_space_identifier:
946 const_or_volatile_or_space_identifier_noopt
951 { push_type (tp_pointer); $$ = 0; }
953 { push_type (tp_pointer); $$ = $2; }
955 { push_type (tp_reference); $$ = 0; }
957 { push_type (tp_reference); $$ = $2; }
961 direct_abs_decl: '(' abs_decl ')'
963 | direct_abs_decl array_mod
966 push_type (tp_array);
971 push_type (tp_array);
975 | direct_abs_decl func_mod
976 { push_type (tp_function); }
978 { push_type (tp_function); }
989 | '(' nonempty_typelist ')'
990 { free ($2); $$ = 0; }
993 /* We used to try to recognize pointer to member types here, but
994 that didn't work (shift/reduce conflicts meant that these rules never
995 got executed). The problem is that
996 int (foo::bar::baz::bizzle)
997 is a function type but
998 int (foo::bar::baz::bizzle::*)
999 is a pointer to member type. Stroustrup loses again! */
1004 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1008 { $$ = lookup_signed_typename (parse_language,
1012 { $$ = lookup_signed_typename (parse_language,
1016 { $$ = lookup_signed_typename (parse_language,
1020 { $$ = lookup_signed_typename (parse_language,
1023 | LONG SIGNED_KEYWORD INT_KEYWORD
1024 { $$ = lookup_signed_typename (parse_language,
1027 | LONG SIGNED_KEYWORD
1028 { $$ = lookup_signed_typename (parse_language,
1031 | SIGNED_KEYWORD LONG INT_KEYWORD
1032 { $$ = lookup_signed_typename (parse_language,
1035 | UNSIGNED LONG INT_KEYWORD
1036 { $$ = lookup_unsigned_typename (parse_language,
1039 | LONG UNSIGNED INT_KEYWORD
1040 { $$ = lookup_unsigned_typename (parse_language,
1044 { $$ = lookup_unsigned_typename (parse_language,
1048 { $$ = lookup_signed_typename (parse_language,
1051 | LONG LONG INT_KEYWORD
1052 { $$ = lookup_signed_typename (parse_language,
1055 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1056 { $$ = lookup_signed_typename (parse_language,
1059 | LONG LONG SIGNED_KEYWORD
1060 { $$ = lookup_signed_typename (parse_language,
1063 | SIGNED_KEYWORD LONG LONG
1064 { $$ = lookup_signed_typename (parse_language,
1067 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1068 { $$ = lookup_signed_typename (parse_language,
1071 | UNSIGNED LONG LONG
1072 { $$ = lookup_unsigned_typename (parse_language,
1075 | UNSIGNED LONG LONG INT_KEYWORD
1076 { $$ = lookup_unsigned_typename (parse_language,
1079 | LONG LONG UNSIGNED
1080 { $$ = lookup_unsigned_typename (parse_language,
1083 | LONG LONG UNSIGNED INT_KEYWORD
1084 { $$ = lookup_unsigned_typename (parse_language,
1088 { $$ = lookup_signed_typename (parse_language,
1091 | SHORT SIGNED_KEYWORD INT_KEYWORD
1092 { $$ = lookup_signed_typename (parse_language,
1095 | SHORT SIGNED_KEYWORD
1096 { $$ = lookup_signed_typename (parse_language,
1099 | UNSIGNED SHORT INT_KEYWORD
1100 { $$ = lookup_unsigned_typename (parse_language,
1104 { $$ = lookup_unsigned_typename (parse_language,
1107 | SHORT UNSIGNED INT_KEYWORD
1108 { $$ = lookup_unsigned_typename (parse_language,
1112 { $$ = lookup_typename (parse_language, parse_gdbarch,
1113 "double", (struct block *) NULL,
1115 | LONG DOUBLE_KEYWORD
1116 { $$ = lookup_typename (parse_language, parse_gdbarch,
1118 (struct block *) NULL, 0); }
1120 { $$ = lookup_struct (copy_name ($2),
1121 expression_context_block); }
1123 { $$ = lookup_struct (copy_name ($2),
1124 expression_context_block); }
1126 { $$ = lookup_union (copy_name ($2),
1127 expression_context_block); }
1129 { $$ = lookup_enum (copy_name ($2),
1130 expression_context_block); }
1132 { $$ = lookup_unsigned_typename (parse_language,
1134 TYPE_NAME($2.type)); }
1136 { $$ = lookup_unsigned_typename (parse_language,
1139 | SIGNED_KEYWORD typename
1140 { $$ = lookup_signed_typename (parse_language,
1142 TYPE_NAME($2.type)); }
1144 { $$ = lookup_signed_typename (parse_language,
1147 /* It appears that this rule for templates is never
1148 reduced; template recognition happens by lookahead
1149 in the token processing code in yylex. */
1150 | TEMPLATE name '<' type '>'
1151 { $$ = lookup_template_type(copy_name($2), $4,
1152 expression_context_block);
1154 | const_or_volatile_or_space_identifier_noopt typebase
1155 { $$ = follow_types ($2); }
1156 | typebase const_or_volatile_or_space_identifier_noopt
1157 { $$ = follow_types ($1); }
1163 $$.stoken.ptr = "int";
1164 $$.stoken.length = 3;
1165 $$.type = lookup_signed_typename (parse_language,
1171 $$.stoken.ptr = "long";
1172 $$.stoken.length = 4;
1173 $$.type = lookup_signed_typename (parse_language,
1179 $$.stoken.ptr = "short";
1180 $$.stoken.length = 5;
1181 $$.type = lookup_signed_typename (parse_language,
1189 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1190 $<ivec>$[0] = 1; /* Number of types in vector */
1193 | nonempty_typelist ',' type
1194 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1195 $$ = (struct type **) realloc ((char *) $1, len);
1196 $$[$<ivec>$[0]] = $3;
1201 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1202 { $$ = follow_types ($1); }
1205 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1206 | VOLATILE_KEYWORD CONST_KEYWORD
1209 const_or_volatile_noopt: const_and_volatile
1210 { push_type (tp_const);
1211 push_type (tp_volatile);
1214 { push_type (tp_const); }
1216 { push_type (tp_volatile); }
1219 operator: OPERATOR NEW
1220 { $$ = operator_stoken (" new"); }
1222 { $$ = operator_stoken (" delete"); }
1223 | OPERATOR NEW '[' ']'
1224 { $$ = operator_stoken (" new[]"); }
1225 | OPERATOR DELETE '[' ']'
1226 { $$ = operator_stoken (" delete[]"); }
1228 { $$ = operator_stoken ("+"); }
1230 { $$ = operator_stoken ("-"); }
1232 { $$ = operator_stoken ("*"); }
1234 { $$ = operator_stoken ("/"); }
1236 { $$ = operator_stoken ("%"); }
1238 { $$ = operator_stoken ("^"); }
1240 { $$ = operator_stoken ("&"); }
1242 { $$ = operator_stoken ("|"); }
1244 { $$ = operator_stoken ("~"); }
1246 { $$ = operator_stoken ("!"); }
1248 { $$ = operator_stoken ("="); }
1250 { $$ = operator_stoken ("<"); }
1252 { $$ = operator_stoken (">"); }
1253 | OPERATOR ASSIGN_MODIFY
1254 { const char *op = "unknown";
1278 case BINOP_BITWISE_IOR:
1281 case BINOP_BITWISE_AND:
1284 case BINOP_BITWISE_XOR:
1291 $$ = operator_stoken (op);
1294 { $$ = operator_stoken ("<<"); }
1296 { $$ = operator_stoken (">>"); }
1298 { $$ = operator_stoken ("=="); }
1300 { $$ = operator_stoken ("!="); }
1302 { $$ = operator_stoken ("<="); }
1304 { $$ = operator_stoken (">="); }
1306 { $$ = operator_stoken ("&&"); }
1308 { $$ = operator_stoken ("||"); }
1309 | OPERATOR INCREMENT
1310 { $$ = operator_stoken ("++"); }
1311 | OPERATOR DECREMENT
1312 { $$ = operator_stoken ("--"); }
1314 { $$ = operator_stoken (","); }
1315 | OPERATOR ARROW_STAR
1316 { $$ = operator_stoken ("->*"); }
1318 { $$ = operator_stoken ("->"); }
1320 { $$ = operator_stoken ("()"); }
1322 { $$ = operator_stoken ("[]"); }
1326 struct ui_file *buf = mem_fileopen ();
1328 c_print_type ($2, NULL, buf, -1, 0);
1329 name = ui_file_xstrdup (buf, &length);
1330 ui_file_delete (buf);
1331 $$ = operator_stoken (name);
1338 name : NAME { $$ = $1.stoken; }
1339 | BLOCKNAME { $$ = $1.stoken; }
1340 | TYPENAME { $$ = $1.stoken; }
1341 | NAME_OR_INT { $$ = $1.stoken; }
1342 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1343 | ENTRY { $$ = $1.stoken; }
1344 | operator { $$ = $1; }
1347 name_not_typename : NAME
1350 /* These would be useful if name_not_typename was useful, but it is just
1351 a fake for "variable", so these cause reduce/reduce conflicts because
1352 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1353 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1354 context where only a name could occur, this might be useful.
1360 $$.sym = lookup_symbol ($1.ptr,
1361 expression_context_block,
1363 &$$.is_a_field_of_this);
1370 /* Returns a stoken of the operator name given by OP (which does not
1371 include the string "operator"). */
1372 static struct stoken
1373 operator_stoken (const char *op)
1375 static const char *operator_string = "operator";
1376 struct stoken st = { NULL, 0 };
1377 st.length = strlen (operator_string) + strlen (op);
1378 st.ptr = malloc (st.length + 1);
1379 strcpy (st.ptr, operator_string);
1380 strcat (st.ptr, op);
1382 /* The toplevel (c_parse) will free the memory allocated here. */
1383 make_cleanup (free, st.ptr);
1387 /* Take care of parsing a number (anything that starts with a digit).
1388 Set yylval and return the token type; update lexptr.
1389 LEN is the number of characters in it. */
1391 /*** Needs some error checking for the float case ***/
1394 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1396 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1397 here, and we do kind of silly things like cast to unsigned. */
1404 int base = input_radix;
1407 /* Number of "L" suffixes encountered. */
1410 /* We have found a "L" or "U" suffix. */
1411 int found_suffix = 0;
1414 struct type *signed_type;
1415 struct type *unsigned_type;
1422 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1423 point. Return DECFLOAT. */
1425 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1428 putithere->typed_val_decfloat.type
1429 = parse_type->builtin_decfloat;
1430 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1431 gdbarch_byte_order (parse_gdbarch), p);
1436 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1439 putithere->typed_val_decfloat.type
1440 = parse_type->builtin_decdouble;
1441 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1442 gdbarch_byte_order (parse_gdbarch), p);
1447 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1450 putithere->typed_val_decfloat.type
1451 = parse_type->builtin_declong;
1452 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1453 gdbarch_byte_order (parse_gdbarch), p);
1458 if (! parse_c_float (parse_gdbarch, p, len,
1459 &putithere->typed_val_float.dval,
1460 &putithere->typed_val_float.type))
1465 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1509 if (c >= 'A' && c <= 'Z')
1511 if (c != 'l' && c != 'u')
1513 if (c >= '0' && c <= '9')
1521 if (base > 10 && c >= 'a' && c <= 'f')
1525 n += i = c - 'a' + 10;
1538 return ERROR; /* Char not a digit */
1541 return ERROR; /* Invalid digit in this base */
1543 /* Portably test for overflow (only works for nonzero values, so make
1544 a second check for zero). FIXME: Can't we just make n and prevn
1545 unsigned and avoid this? */
1546 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1547 unsigned_p = 1; /* Try something unsigned */
1549 /* Portably test for unsigned overflow.
1550 FIXME: This check is wrong; for example it doesn't find overflow
1551 on 0x123456789 when LONGEST is 32 bits. */
1552 if (c != 'l' && c != 'u' && n != 0)
1554 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1555 error (_("Numeric constant too large."));
1560 /* An integer constant is an int, a long, or a long long. An L
1561 suffix forces it to be long; an LL suffix forces it to be long
1562 long. If not forced to a larger size, it gets the first type of
1563 the above that it fits in. To figure out whether it fits, we
1564 shift it right and see whether anything remains. Note that we
1565 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1566 operation, because many compilers will warn about such a shift
1567 (which always produces a zero result). Sometimes gdbarch_int_bit
1568 or gdbarch_long_bit will be that big, sometimes not. To deal with
1569 the case where it is we just always shift the value more than
1570 once, with fewer bits each time. */
1572 un = (ULONGEST)n >> 2;
1574 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1576 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1578 /* A large decimal (not hex or octal) constant (between INT_MAX
1579 and UINT_MAX) is a long or unsigned long, according to ANSI,
1580 never an unsigned int, but this code treats it as unsigned
1581 int. This probably should be fixed. GCC gives a warning on
1584 unsigned_type = parse_type->builtin_unsigned_int;
1585 signed_type = parse_type->builtin_int;
1587 else if (long_p <= 1
1588 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1590 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1591 unsigned_type = parse_type->builtin_unsigned_long;
1592 signed_type = parse_type->builtin_long;
1597 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1598 < gdbarch_long_long_bit (parse_gdbarch))
1599 /* A long long does not fit in a LONGEST. */
1600 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1602 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1603 high_bit = (ULONGEST) 1 << shift;
1604 unsigned_type = parse_type->builtin_unsigned_long_long;
1605 signed_type = parse_type->builtin_long_long;
1608 putithere->typed_val_int.val = n;
1610 /* If the high bit of the worked out type is set then this number
1611 has to be unsigned. */
1613 if (unsigned_p || (n & high_bit))
1615 putithere->typed_val_int.type = unsigned_type;
1619 putithere->typed_val_int.type = signed_type;
1625 /* Temporary obstack used for holding strings. */
1626 static struct obstack tempbuf;
1627 static int tempbuf_init;
1629 /* Parse a C escape sequence. The initial backslash of the sequence
1630 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1631 last character of the sequence. If OUTPUT is not NULL, the
1632 translated form of the escape sequence will be written there. If
1633 OUTPUT is NULL, no output is written and the call will only affect
1634 *PTR. If an escape sequence is expressed in target bytes, then the
1635 entire sequence will simply be copied to OUTPUT. Return 1 if any
1636 character was emitted, 0 otherwise. */
1639 c_parse_escape (char **ptr, struct obstack *output)
1641 char *tokptr = *ptr;
1644 /* Some escape sequences undergo character set conversion. Those we
1648 /* Hex escapes do not undergo character set conversion, so keep
1649 the escape sequence for later. */
1652 obstack_grow_str (output, "\\x");
1654 if (!isxdigit (*tokptr))
1655 error (_("\\x escape without a following hex digit"));
1656 while (isxdigit (*tokptr))
1659 obstack_1grow (output, *tokptr);
1664 /* Octal escapes do not undergo character set conversion, so
1665 keep the escape sequence for later. */
1677 obstack_grow_str (output, "\\");
1679 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1683 obstack_1grow (output, *tokptr);
1689 /* We handle UCNs later. We could handle them here, but that
1690 would mean a spurious error in the case where the UCN could
1691 be converted to the target charset but not the host
1697 int i, len = c == 'U' ? 8 : 4;
1700 obstack_1grow (output, '\\');
1701 obstack_1grow (output, *tokptr);
1704 if (!isxdigit (*tokptr))
1705 error (_("\\%c escape without a following hex digit"), c);
1706 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1709 obstack_1grow (output, *tokptr);
1715 /* We must pass backslash through so that it does not
1716 cause quoting during the second expansion. */
1719 obstack_grow_str (output, "\\\\");
1723 /* Escapes which undergo conversion. */
1726 obstack_1grow (output, '\a');
1731 obstack_1grow (output, '\b');
1736 obstack_1grow (output, '\f');
1741 obstack_1grow (output, '\n');
1746 obstack_1grow (output, '\r');
1751 obstack_1grow (output, '\t');
1756 obstack_1grow (output, '\v');
1760 /* GCC extension. */
1763 obstack_1grow (output, HOST_ESCAPE_CHAR);
1767 /* Backslash-newline expands to nothing at all. */
1773 /* A few escapes just expand to the character itself. */
1777 /* GCC extensions. */
1782 /* Unrecognized escapes turn into the character itself. */
1785 obstack_1grow (output, *tokptr);
1793 /* Parse a string or character literal from TOKPTR. The string or
1794 character may be wide or unicode. *OUTPTR is set to just after the
1795 end of the literal in the input string. The resulting token is
1796 stored in VALUE. This returns a token value, either STRING or
1797 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1798 number of host characters in the literal. */
1800 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1804 enum c_string_type type;
1806 /* Build the gdb internal form of the input string in tempbuf. Note
1807 that the buffer is null byte terminated *only* for the
1808 convenience of debugging gdb itself and printing the buffer
1809 contents when the buffer contains no embedded nulls. Gdb does
1810 not depend upon the buffer being null byte terminated, it uses
1811 the length string instead. This allows gdb to handle C strings
1812 (as well as strings in other languages) with embedded null
1818 obstack_free (&tempbuf, NULL);
1819 obstack_init (&tempbuf);
1821 /* Record the string type. */
1824 type = C_WIDE_STRING;
1827 else if (*tokptr == 'u')
1832 else if (*tokptr == 'U')
1840 /* Skip the quote. */
1854 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1856 else if (c == quote)
1860 obstack_1grow (&tempbuf, c);
1862 /* FIXME: this does the wrong thing with multi-byte host
1863 characters. We could use mbrlen here, but that would
1864 make "set host-charset" a bit less useful. */
1869 if (*tokptr != quote)
1872 error (_("Unterminated string in expression."));
1874 error (_("Unmatched single quote."));
1879 value->ptr = obstack_base (&tempbuf);
1880 value->length = obstack_object_size (&tempbuf);
1884 return quote == '"' ? STRING : CHAR;
1891 enum exp_opcode opcode;
1895 static const struct token tokentab3[] =
1897 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1898 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
1899 {"->*", ARROW_STAR, BINOP_END, 1}
1902 static const struct token tokentab2[] =
1904 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1905 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1906 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1907 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
1908 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
1909 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
1910 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
1911 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
1912 {"++", INCREMENT, BINOP_END, 0},
1913 {"--", DECREMENT, BINOP_END, 0},
1914 {"->", ARROW, BINOP_END, 0},
1915 {"&&", ANDAND, BINOP_END, 0},
1916 {"||", OROR, BINOP_END, 0},
1917 /* "::" is *not* only C++: gdb overrides its meaning in several
1918 different ways, e.g., 'filename'::func, function::variable. */
1919 {"::", COLONCOLON, BINOP_END, 0},
1920 {"<<", LSH, BINOP_END, 0},
1921 {">>", RSH, BINOP_END, 0},
1922 {"==", EQUAL, BINOP_END, 0},
1923 {"!=", NOTEQUAL, BINOP_END, 0},
1924 {"<=", LEQ, BINOP_END, 0},
1925 {">=", GEQ, BINOP_END, 0},
1926 {".*", DOT_STAR, BINOP_END, 1}
1929 /* Identifier-like tokens. */
1930 static const struct token ident_tokens[] =
1932 {"unsigned", UNSIGNED, OP_NULL, 0},
1933 {"template", TEMPLATE, OP_NULL, 1},
1934 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
1935 {"struct", STRUCT, OP_NULL, 0},
1936 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
1937 {"sizeof", SIZEOF, OP_NULL, 0},
1938 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
1939 {"false", FALSEKEYWORD, OP_NULL, 1},
1940 {"class", CLASS, OP_NULL, 1},
1941 {"union", UNION, OP_NULL, 0},
1942 {"short", SHORT, OP_NULL, 0},
1943 {"const", CONST_KEYWORD, OP_NULL, 0},
1944 {"enum", ENUM, OP_NULL, 0},
1945 {"long", LONG, OP_NULL, 0},
1946 {"true", TRUEKEYWORD, OP_NULL, 1},
1947 {"int", INT_KEYWORD, OP_NULL, 0},
1948 {"new", NEW, OP_NULL, 1},
1949 {"delete", DELETE, OP_NULL, 1},
1950 {"operator", OPERATOR, OP_NULL, 1},
1952 {"and", ANDAND, BINOP_END, 1},
1953 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
1954 {"bitand", '&', OP_NULL, 1},
1955 {"bitor", '|', OP_NULL, 1},
1956 {"compl", '~', OP_NULL, 1},
1957 {"not", '!', OP_NULL, 1},
1958 {"not_eq", NOTEQUAL, BINOP_END, 1},
1959 {"or", OROR, BINOP_END, 1},
1960 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
1961 {"xor", '^', OP_NULL, 1},
1962 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1},
1964 {"const_cast", CONST_CAST, OP_NULL, 1 },
1965 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, 1 },
1966 {"static_cast", STATIC_CAST, OP_NULL, 1 },
1967 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, 1 }
1970 /* When we find that lexptr (the global var defined in parse.c) is
1971 pointing at a macro invocation, we expand the invocation, and call
1972 scan_macro_expansion to save the old lexptr here and point lexptr
1973 into the expanded text. When we reach the end of that, we call
1974 end_macro_expansion to pop back to the value we saved here. The
1975 macro expansion code promises to return only fully-expanded text,
1976 so we don't need to "push" more than one level.
1978 This is disgusting, of course. It would be cleaner to do all macro
1979 expansion beforehand, and then hand that to lexptr. But we don't
1980 really know where the expression ends. Remember, in a command like
1982 (gdb) break *ADDRESS if CONDITION
1984 we evaluate ADDRESS in the scope of the current frame, but we
1985 evaluate CONDITION in the scope of the breakpoint's location. So
1986 it's simply wrong to try to macro-expand the whole thing at once. */
1987 static char *macro_original_text;
1989 /* We save all intermediate macro expansions on this obstack for the
1990 duration of a single parse. The expansion text may sometimes have
1991 to live past the end of the expansion, due to yacc lookahead.
1992 Rather than try to be clever about saving the data for a single
1993 token, we simply keep it all and delete it after parsing has
1995 static struct obstack expansion_obstack;
1998 scan_macro_expansion (char *expansion)
2002 /* We'd better not be trying to push the stack twice. */
2003 gdb_assert (! macro_original_text);
2005 /* Copy to the obstack, and then free the intermediate
2007 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2010 /* Save the old lexptr value, so we can return to it when we're done
2011 parsing the expanded text. */
2012 macro_original_text = lexptr;
2018 scanning_macro_expansion (void)
2020 return macro_original_text != 0;
2025 finished_macro_expansion (void)
2027 /* There'd better be something to pop back to. */
2028 gdb_assert (macro_original_text);
2030 /* Pop back to the original text. */
2031 lexptr = macro_original_text;
2032 macro_original_text = 0;
2037 scan_macro_cleanup (void *dummy)
2039 if (macro_original_text)
2040 finished_macro_expansion ();
2042 obstack_free (&expansion_obstack, NULL);
2045 /* Return true iff the token represents a C++ cast operator. */
2048 is_cast_operator (const char *token, int len)
2050 return (! strncmp (token, "dynamic_cast", len)
2051 || ! strncmp (token, "static_cast", len)
2052 || ! strncmp (token, "reinterpret_cast", len)
2053 || ! strncmp (token, "const_cast", len));
2056 /* The scope used for macro expansion. */
2057 static struct macro_scope *expression_macro_scope;
2059 /* This is set if a NAME token appeared at the very end of the input
2060 string, with no whitespace separating the name from the EOF. This
2061 is used only when parsing to do field name completion. */
2062 static int saw_name_at_eof;
2064 /* This is set if the previously-returned token was a structure
2065 operator -- either '.' or ARROW. This is used only when parsing to
2066 do field name completion. */
2067 static int last_was_structop;
2069 /* Read one token, getting characters through lexptr. */
2072 lex_one_token (void)
2078 int saw_structop = last_was_structop;
2081 last_was_structop = 0;
2085 /* Check if this is a macro invocation that we need to expand. */
2086 if (! scanning_macro_expansion ())
2088 char *expanded = macro_expand_next (&lexptr,
2089 standard_macro_lookup,
2090 expression_macro_scope);
2093 scan_macro_expansion (expanded);
2096 prev_lexptr = lexptr;
2099 /* See if it is a special token of length 3. */
2100 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2101 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2103 if (tokentab3[i].cxx_only
2104 && parse_language->la_language != language_cplus)
2108 yylval.opcode = tokentab3[i].opcode;
2109 return tokentab3[i].token;
2112 /* See if it is a special token of length 2. */
2113 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2114 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2116 if (tokentab2[i].cxx_only
2117 && parse_language->la_language != language_cplus)
2121 yylval.opcode = tokentab2[i].opcode;
2122 if (in_parse_field && tokentab2[i].token == ARROW)
2123 last_was_structop = 1;
2124 return tokentab2[i].token;
2127 switch (c = *tokstart)
2130 /* If we were just scanning the result of a macro expansion,
2131 then we need to resume scanning the original text.
2132 If we're parsing for field name completion, and the previous
2133 token allows such completion, return a COMPLETE token.
2134 Otherwise, we were already scanning the original text, and
2135 we're really done. */
2136 if (scanning_macro_expansion ())
2138 finished_macro_expansion ();
2141 else if (saw_name_at_eof)
2143 saw_name_at_eof = 0;
2146 else if (saw_structop)
2165 if (paren_depth == 0)
2172 if (comma_terminates
2174 && ! scanning_macro_expansion ())
2180 /* Might be a floating point number. */
2181 if (lexptr[1] < '0' || lexptr[1] > '9')
2184 last_was_structop = 1;
2185 goto symbol; /* Nope, must be a symbol. */
2187 /* FALL THRU into number case. */
2200 /* It's a number. */
2201 int got_dot = 0, got_e = 0, toktype;
2203 int hex = input_radix > 10;
2205 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2210 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2218 /* This test includes !hex because 'e' is a valid hex digit
2219 and thus does not indicate a floating point number when
2220 the radix is hex. */
2221 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2222 got_dot = got_e = 1;
2223 /* This test does not include !hex, because a '.' always indicates
2224 a decimal floating point number regardless of the radix. */
2225 else if (!got_dot && *p == '.')
2227 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2228 && (*p == '-' || *p == '+'))
2229 /* This is the sign of the exponent, not the end of the
2232 /* We will take any letters or digits. parse_number will
2233 complain if past the radix, or if L or U are not final. */
2234 else if ((*p < '0' || *p > '9')
2235 && ((*p < 'a' || *p > 'z')
2236 && (*p < 'A' || *p > 'Z')))
2239 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2240 if (toktype == ERROR)
2242 char *err_copy = (char *) alloca (p - tokstart + 1);
2244 memcpy (err_copy, tokstart, p - tokstart);
2245 err_copy[p - tokstart] = 0;
2246 error (_("Invalid number \"%s\"."), err_copy);
2277 if (tokstart[1] != '"' && tokstart[1] != '\'')
2284 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2289 error (_("Empty character constant."));
2290 else if (host_len > 2 && c == '\'')
2293 namelen = lexptr - tokstart - 1;
2296 else if (host_len > 1)
2297 error (_("Invalid character constant."));
2303 if (!(c == '_' || c == '$'
2304 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2305 /* We must have come across a bad character (e.g. ';'). */
2306 error (_("Invalid character '%c' in expression."), c);
2308 /* It's a name. See how long it is. */
2310 for (c = tokstart[namelen];
2311 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2312 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2314 /* Template parameter lists are part of the name.
2315 FIXME: This mishandles `print $a<4&&$a>3'. */
2319 if (! is_cast_operator (tokstart, namelen))
2321 /* Scan ahead to get rest of the template specification. Note
2322 that we look ahead only when the '<' adjoins non-whitespace
2323 characters; for comparison expressions, e.g. "a < b > c",
2324 there must be spaces before the '<', etc. */
2326 char * p = find_template_name_end (tokstart + namelen);
2328 namelen = p - tokstart;
2332 c = tokstart[++namelen];
2335 /* The token "if" terminates the expression and is NOT removed from
2336 the input stream. It doesn't count if it appears in the
2337 expansion of a macro. */
2339 && tokstart[0] == 'i'
2340 && tokstart[1] == 'f'
2341 && ! scanning_macro_expansion ())
2346 /* For the same reason (breakpoint conditions), "thread N"
2347 terminates the expression. "thread" could be an identifier, but
2348 an identifier is never followed by a number without intervening
2349 punctuation. "task" is similar. Handle abbreviations of these,
2350 similarly to breakpoint.c:find_condition_and_thread. */
2352 && (strncmp (tokstart, "thread", namelen) == 0
2353 || strncmp (tokstart, "task", namelen) == 0)
2354 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2355 && ! scanning_macro_expansion ())
2357 char *p = tokstart + namelen + 1;
2358 while (*p == ' ' || *p == '\t')
2360 if (*p >= '0' && *p <= '9')
2368 yylval.sval.ptr = tokstart;
2369 yylval.sval.length = namelen;
2371 /* Catch specific keywords. */
2372 copy = copy_name (yylval.sval);
2373 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2374 if (strcmp (copy, ident_tokens[i].operator) == 0)
2376 if (ident_tokens[i].cxx_only
2377 && parse_language->la_language != language_cplus)
2380 /* It is ok to always set this, even though we don't always
2381 strictly need to. */
2382 yylval.opcode = ident_tokens[i].opcode;
2383 return ident_tokens[i].token;
2386 if (*tokstart == '$')
2389 if (in_parse_field && *lexptr == '\0')
2390 saw_name_at_eof = 1;
2394 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2401 DEF_VEC_O (token_and_value);
2403 /* A FIFO of tokens that have been read but not yet returned to the
2405 static VEC (token_and_value) *token_fifo;
2407 /* Non-zero if the lexer should return tokens from the FIFO. */
2410 /* Temporary storage for c_lex; this holds symbol names as they are
2412 static struct obstack name_obstack;
2414 /* Classify a NAME token. The contents of the token are in `yylval'.
2415 Updates yylval and returns the new token type. BLOCK is the block
2416 in which lookups start; this can be NULL to mean the global
2419 classify_name (struct block *block)
2423 int is_a_field_of_this = 0;
2425 copy = copy_name (yylval.sval);
2427 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2428 parse_language->la_language == language_cplus
2429 ? &is_a_field_of_this : (int *) NULL);
2431 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2433 yylval.ssym.sym = sym;
2434 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2439 /* See if it's a file name. */
2440 struct symtab *symtab;
2442 symtab = lookup_symtab (copy);
2445 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2450 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2452 yylval.tsym.type = SYMBOL_TYPE (sym);
2457 = language_lookup_primitive_type_by_name (parse_language,
2458 parse_gdbarch, copy);
2459 if (yylval.tsym.type != NULL)
2462 /* Input names that aren't symbols but ARE valid hex numbers, when
2463 the input radix permits them, can be names or numbers depending
2464 on the parse. Note we support radixes > 16 here. */
2466 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2467 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2469 YYSTYPE newlval; /* Its value is ignored. */
2470 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2473 yylval.ssym.sym = sym;
2474 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2479 /* Any other kind of symbol */
2480 yylval.ssym.sym = sym;
2481 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2484 && parse_language->la_language == language_cplus
2485 && !is_a_field_of_this
2486 && !lookup_minimal_symbol (copy, NULL, NULL))
2487 return UNKNOWN_CPP_NAME;
2492 /* Like classify_name, but used by the inner loop of the lexer, when a
2493 name might have already been seen. FIRST_NAME is true if the token
2494 in `yylval' is the first component of a name, false otherwise. If
2495 this function returns NAME, it might not have updated `yylval'.
2496 This is ok because the caller only cares about TYPENAME. */
2498 classify_inner_name (struct block *block, int first_name)
2500 struct type *type, *new_type;
2504 return classify_name (block);
2506 type = check_typedef (yylval.tsym.type);
2507 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2508 && TYPE_CODE (type) != TYPE_CODE_UNION
2509 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2510 /* We know the caller won't expect us to update yylval. */
2513 copy = copy_name (yylval.tsym.stoken);
2514 new_type = cp_lookup_nested_type (yylval.tsym.type, copy, block);
2516 if (new_type == NULL)
2517 /* We know the caller won't expect us to update yylval. */
2520 yylval.tsym.type = new_type;
2524 /* The outer level of a two-level lexer. This calls the inner lexer
2525 to return tokens. It then either returns these tokens, or
2526 aggregates them into a larger token. This lets us work around a
2527 problem in our parsing approach, where the parser could not
2528 distinguish between qualified names and qualified types at the
2531 This approach is still not ideal, because it mishandles template
2532 types. See the comment in lex_one_token for an example. However,
2533 this is still an improvement over the earlier approach, and will
2534 suffice until we move to better parsing technology. */
2538 token_and_value current;
2539 int first_was_coloncolon, last_was_coloncolon, first_iter;
2541 if (popping && !VEC_empty (token_and_value, token_fifo))
2543 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2544 VEC_ordered_remove (token_and_value, token_fifo, 0);
2550 current.token = lex_one_token ();
2551 if (current.token == NAME)
2552 current.token = classify_name (expression_context_block);
2553 if ((current.token == NAME || current.token == UNKNOWN_CPP_NAME)
2554 && yylval.sval.length == strlen ("entry")
2555 && strncmp (yylval.sval.ptr, "entry", strlen ("entry")) == 0)
2556 current.token = ENTRY;
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);