1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989-2000, 2003-2004, 2006-2012 Free Software
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* Parse a C expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result.
29 Note that malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator. */
40 #include "gdb_string.h"
42 #include "expression.h"
44 #include "parser-defs.h"
47 #include "bfd.h" /* Required by objfiles.h. */
48 #include "symfile.h" /* Required by objfiles.h. */
49 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
52 #include "cp-support.h"
54 #include "gdb_assert.h"
55 #include "macroscope.h"
57 #define parse_type builtin_type (parse_gdbarch)
59 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
60 as well as gratuitiously global symbol names, so we can have multiple
61 yacc generated parsers in gdb. Note that these are only the variables
62 produced by yacc. If other parser generators (bison, byacc, etc) produce
63 additional global names that conflict at link time, then those parser
64 generators need to be fixed instead of adding those names to this list. */
66 #define yymaxdepth c_maxdepth
67 #define yyparse c_parse_internal
69 #define yyerror c_error
72 #define yydebug c_debug
81 #define yyerrflag c_errflag
82 #define yynerrs c_nerrs
87 #define yystate c_state
93 #define yyreds c_reds /* With YYDEBUG defined */
94 #define yytoks c_toks /* With YYDEBUG defined */
95 #define yyname c_name /* With YYDEBUG defined */
96 #define yyrule c_rule /* With YYDEBUG defined */
99 #define yydefred c_yydefred
100 #define yydgoto c_yydgoto
101 #define yysindex c_yysindex
102 #define yyrindex c_yyrindex
103 #define yygindex c_yygindex
104 #define yytable c_yytable
105 #define yycheck c_yycheck
107 #define yysslim c_yysslim
108 #define yyssp c_yyssp
109 #define yystacksize c_yystacksize
111 #define yyvsp c_yyvsp
114 #define YYDEBUG 1 /* Default to yydebug support */
117 #define YYFPRINTF parser_fprintf
121 static int yylex (void);
123 void yyerror (char *);
127 /* Although the yacc "value" of an expression is not used,
128 since the result is stored in the structure being created,
129 other node types do have values. */
145 } typed_val_decfloat;
149 struct typed_stoken tsval;
151 struct symtoken ssym;
154 enum exp_opcode opcode;
155 struct internalvar *ivar;
157 struct stoken_vector svec;
158 VEC (type_ptr) *tvec;
161 struct type_stack *type_stack;
165 /* YYSTYPE gets defined by %union */
166 static int parse_number (char *, int, int, YYSTYPE *);
167 static struct stoken operator_stoken (const char *);
168 static void check_parameter_typelist (VEC (type_ptr) *);
171 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
173 %type <tval> type typebase
174 %type <tvec> nonempty_typelist func_mod parameter_typelist
175 /* %type <bval> block */
177 /* Fancy type parsing. */
179 %type <lval> array_mod
180 %type <tval> conversion_type_id
182 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
184 %token <typed_val_int> INT
185 %token <typed_val_float> FLOAT
186 %token <typed_val_decfloat> DECFLOAT
188 /* Both NAME and TYPENAME tokens represent symbols in the input,
189 and both convey their data as strings.
190 But a TYPENAME is a string that happens to be defined as a typedef
191 or builtin type name (such as int or char)
192 and a NAME is any other symbol.
193 Contexts where this distinction is not important can use the
194 nonterminal "name", which matches either NAME or TYPENAME. */
196 %token <tsval> STRING
198 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
199 %token <ssym> UNKNOWN_CPP_NAME
200 %token <voidval> COMPLETE
201 %token <tsym> TYPENAME
203 %type <svec> string_exp
204 %type <ssym> name_not_typename
205 %type <tsym> typename
207 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
208 but which would parse as a valid number in the current input radix.
209 E.g. "c" when input_radix==16. Depending on the parse, it will be
210 turned into a name or into a number. */
212 %token <ssym> NAME_OR_INT
215 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
219 %type <sval> operator
220 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
225 /* Special type cases, put in to allow the parser to distinguish different
227 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
229 %token <sval> VARIABLE
231 %token <opcode> ASSIGN_MODIFY
240 %right '=' ASSIGN_MODIFY
248 %left '<' '>' LEQ GEQ
253 %right UNARY INCREMENT DECREMENT
254 %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
255 %token <ssym> BLOCKNAME
256 %token <bval> FILENAME
270 { write_exp_elt_opcode(OP_TYPE);
271 write_exp_elt_type($1);
272 write_exp_elt_opcode(OP_TYPE);}
275 write_exp_elt_opcode (OP_TYPEOF);
277 | TYPEOF '(' type ')'
279 write_exp_elt_opcode (OP_TYPE);
280 write_exp_elt_type ($3);
281 write_exp_elt_opcode (OP_TYPE);
283 | DECLTYPE '(' exp ')'
285 write_exp_elt_opcode (OP_DECLTYPE);
289 /* Expressions, including the comma operator. */
292 { write_exp_elt_opcode (BINOP_COMMA); }
295 /* Expressions, not including the comma operator. */
296 exp : '*' exp %prec UNARY
297 { write_exp_elt_opcode (UNOP_IND); }
300 exp : '&' exp %prec UNARY
301 { write_exp_elt_opcode (UNOP_ADDR); }
304 exp : '-' exp %prec UNARY
305 { write_exp_elt_opcode (UNOP_NEG); }
308 exp : '+' exp %prec UNARY
309 { write_exp_elt_opcode (UNOP_PLUS); }
312 exp : '!' exp %prec UNARY
313 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
316 exp : '~' exp %prec UNARY
317 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
320 exp : INCREMENT exp %prec UNARY
321 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
324 exp : DECREMENT exp %prec UNARY
325 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
328 exp : exp INCREMENT %prec UNARY
329 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
332 exp : exp DECREMENT %prec UNARY
333 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
336 exp : SIZEOF exp %prec UNARY
337 { write_exp_elt_opcode (UNOP_SIZEOF); }
341 { write_exp_elt_opcode (STRUCTOP_PTR);
342 write_exp_string ($3);
343 write_exp_elt_opcode (STRUCTOP_PTR); }
346 exp : exp ARROW name COMPLETE
347 { mark_struct_expression ();
348 write_exp_elt_opcode (STRUCTOP_PTR);
349 write_exp_string ($3);
350 write_exp_elt_opcode (STRUCTOP_PTR); }
353 exp : exp ARROW COMPLETE
355 mark_struct_expression ();
356 write_exp_elt_opcode (STRUCTOP_PTR);
359 write_exp_string (s);
360 write_exp_elt_opcode (STRUCTOP_PTR); }
363 exp : exp ARROW qualified_name
364 { /* exp->type::name becomes exp->*(&type::name) */
365 /* Note: this doesn't work if name is a
366 static member! FIXME */
367 write_exp_elt_opcode (UNOP_ADDR);
368 write_exp_elt_opcode (STRUCTOP_MPTR); }
371 exp : exp ARROW_STAR exp
372 { write_exp_elt_opcode (STRUCTOP_MPTR); }
376 { write_exp_elt_opcode (STRUCTOP_STRUCT);
377 write_exp_string ($3);
378 write_exp_elt_opcode (STRUCTOP_STRUCT); }
381 exp : exp '.' name COMPLETE
382 { mark_struct_expression ();
383 write_exp_elt_opcode (STRUCTOP_STRUCT);
384 write_exp_string ($3);
385 write_exp_elt_opcode (STRUCTOP_STRUCT); }
388 exp : exp '.' COMPLETE
390 mark_struct_expression ();
391 write_exp_elt_opcode (STRUCTOP_STRUCT);
394 write_exp_string (s);
395 write_exp_elt_opcode (STRUCTOP_STRUCT); }
398 exp : exp '.' qualified_name
399 { /* exp.type::name becomes exp.*(&type::name) */
400 /* Note: this doesn't work if name is a
401 static member! FIXME */
402 write_exp_elt_opcode (UNOP_ADDR);
403 write_exp_elt_opcode (STRUCTOP_MEMBER); }
406 exp : exp DOT_STAR exp
407 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
410 exp : exp '[' exp1 ']'
411 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
415 /* This is to save the value of arglist_len
416 being accumulated by an outer function call. */
417 { start_arglist (); }
418 arglist ')' %prec ARROW
419 { write_exp_elt_opcode (OP_FUNCALL);
420 write_exp_elt_longcst ((LONGEST) end_arglist ());
421 write_exp_elt_opcode (OP_FUNCALL); }
424 exp : UNKNOWN_CPP_NAME '('
426 /* This could potentially be a an argument defined
427 lookup function (Koenig). */
428 write_exp_elt_opcode (OP_ADL_FUNC);
429 write_exp_elt_block (expression_context_block);
430 write_exp_elt_sym (NULL); /* Placeholder. */
431 write_exp_string ($1.stoken);
432 write_exp_elt_opcode (OP_ADL_FUNC);
434 /* This is to save the value of arglist_len
435 being accumulated by an outer function call. */
439 arglist ')' %prec ARROW
441 write_exp_elt_opcode (OP_FUNCALL);
442 write_exp_elt_longcst ((LONGEST) end_arglist ());
443 write_exp_elt_opcode (OP_FUNCALL);
448 { start_arglist (); }
458 arglist : arglist ',' exp %prec ABOVE_COMMA
462 exp : exp '(' parameter_typelist ')' const_or_volatile
464 VEC (type_ptr) *type_list = $3;
465 struct type *type_elt;
466 LONGEST len = VEC_length (type_ptr, type_list);
468 write_exp_elt_opcode (TYPE_INSTANCE);
469 write_exp_elt_longcst (len);
471 VEC_iterate (type_ptr, type_list, i, type_elt);
473 write_exp_elt_type (type_elt);
474 write_exp_elt_longcst(len);
475 write_exp_elt_opcode (TYPE_INSTANCE);
476 VEC_free (type_ptr, type_list);
481 { $$ = end_arglist () - 1; }
483 exp : lcurly arglist rcurly %prec ARROW
484 { write_exp_elt_opcode (OP_ARRAY);
485 write_exp_elt_longcst ((LONGEST) 0);
486 write_exp_elt_longcst ((LONGEST) $3);
487 write_exp_elt_opcode (OP_ARRAY); }
490 exp : lcurly type_exp rcurly exp %prec UNARY
491 { write_exp_elt_opcode (UNOP_MEMVAL_TYPE); }
494 exp : '(' type_exp ')' exp %prec UNARY
495 { write_exp_elt_opcode (UNOP_CAST_TYPE); }
502 /* Binary operators in order of decreasing precedence. */
505 { write_exp_elt_opcode (BINOP_REPEAT); }
509 { write_exp_elt_opcode (BINOP_MUL); }
513 { write_exp_elt_opcode (BINOP_DIV); }
517 { write_exp_elt_opcode (BINOP_REM); }
521 { write_exp_elt_opcode (BINOP_ADD); }
525 { write_exp_elt_opcode (BINOP_SUB); }
529 { write_exp_elt_opcode (BINOP_LSH); }
533 { write_exp_elt_opcode (BINOP_RSH); }
537 { write_exp_elt_opcode (BINOP_EQUAL); }
540 exp : exp NOTEQUAL exp
541 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
545 { write_exp_elt_opcode (BINOP_LEQ); }
549 { write_exp_elt_opcode (BINOP_GEQ); }
553 { write_exp_elt_opcode (BINOP_LESS); }
557 { write_exp_elt_opcode (BINOP_GTR); }
561 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
565 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
569 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
573 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
577 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
580 exp : exp '?' exp ':' exp %prec '?'
581 { write_exp_elt_opcode (TERNOP_COND); }
585 { write_exp_elt_opcode (BINOP_ASSIGN); }
588 exp : exp ASSIGN_MODIFY exp
589 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
590 write_exp_elt_opcode ($2);
591 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
595 { write_exp_elt_opcode (OP_LONG);
596 write_exp_elt_type ($1.type);
597 write_exp_elt_longcst ((LONGEST)($1.val));
598 write_exp_elt_opcode (OP_LONG); }
603 struct stoken_vector vec;
606 write_exp_string_vector ($1.type, &vec);
612 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
613 write_exp_elt_opcode (OP_LONG);
614 write_exp_elt_type (val.typed_val_int.type);
615 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
616 write_exp_elt_opcode (OP_LONG);
622 { write_exp_elt_opcode (OP_DOUBLE);
623 write_exp_elt_type ($1.type);
624 write_exp_elt_dblcst ($1.dval);
625 write_exp_elt_opcode (OP_DOUBLE); }
629 { write_exp_elt_opcode (OP_DECFLOAT);
630 write_exp_elt_type ($1.type);
631 write_exp_elt_decfloatcst ($1.val);
632 write_exp_elt_opcode (OP_DECFLOAT); }
640 write_dollar_variable ($1);
644 exp : SIZEOF '(' type ')' %prec UNARY
645 { write_exp_elt_opcode (OP_LONG);
646 write_exp_elt_type (lookup_signed_typename
647 (parse_language, parse_gdbarch,
650 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
651 write_exp_elt_opcode (OP_LONG); }
654 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
655 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
658 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
659 { write_exp_elt_opcode (UNOP_CAST_TYPE); }
662 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
663 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
666 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
667 { /* We could do more error checking here, but
668 it doesn't seem worthwhile. */
669 write_exp_elt_opcode (UNOP_CAST_TYPE); }
675 /* We copy the string here, and not in the
676 lexer, to guarantee that we do not leak a
677 string. Note that we follow the
678 NUL-termination convention of the
680 struct typed_stoken *vec = XNEW (struct typed_stoken);
685 vec->length = $1.length;
686 vec->ptr = malloc ($1.length + 1);
687 memcpy (vec->ptr, $1.ptr, $1.length + 1);
692 /* Note that we NUL-terminate here, but just
696 $$.tokens = realloc ($$.tokens,
697 $$.len * sizeof (struct typed_stoken));
699 p = malloc ($2.length + 1);
700 memcpy (p, $2.ptr, $2.length + 1);
702 $$.tokens[$$.len - 1].type = $2.type;
703 $$.tokens[$$.len - 1].length = $2.length;
704 $$.tokens[$$.len - 1].ptr = p;
711 enum c_string_type type = C_STRING;
713 for (i = 0; i < $1.len; ++i)
715 switch ($1.tokens[i].type)
723 && type != $1.tokens[i].type)
724 error (_("Undefined string concatenation."));
725 type = $1.tokens[i].type;
729 internal_error (__FILE__, __LINE__,
730 "unrecognized type in string concatenation");
734 write_exp_string_vector (type, &$1);
735 for (i = 0; i < $1.len; ++i)
736 free ($1.tokens[i].ptr);
743 { write_exp_elt_opcode (OP_LONG);
744 write_exp_elt_type (parse_type->builtin_bool);
745 write_exp_elt_longcst ((LONGEST) 1);
746 write_exp_elt_opcode (OP_LONG); }
750 { write_exp_elt_opcode (OP_LONG);
751 write_exp_elt_type (parse_type->builtin_bool);
752 write_exp_elt_longcst ((LONGEST) 0);
753 write_exp_elt_opcode (OP_LONG); }
761 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
763 error (_("No file or function \"%s\"."),
764 copy_name ($1.stoken));
772 block : block COLONCOLON name
774 = lookup_symbol (copy_name ($3), $1,
775 VAR_DOMAIN, (int *) NULL);
776 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
777 error (_("No function \"%s\" in specified context."),
779 $$ = SYMBOL_BLOCK_VALUE (tem); }
782 variable: name_not_typename ENTRY
783 { struct symbol *sym = $1.sym;
785 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
786 || !symbol_read_needs_frame (sym))
787 error (_("@entry can be used only for function "
788 "parameters, not for \"%s\""),
789 copy_name ($1.stoken));
791 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
792 write_exp_elt_sym (sym);
793 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
797 variable: block COLONCOLON name
798 { struct symbol *sym;
799 sym = lookup_symbol (copy_name ($3), $1,
800 VAR_DOMAIN, (int *) NULL);
802 error (_("No symbol \"%s\" in specified context."),
804 if (symbol_read_needs_frame (sym))
806 if (innermost_block == 0
807 || contained_in (block_found,
809 innermost_block = block_found;
812 write_exp_elt_opcode (OP_VAR_VALUE);
813 /* block_found is set by lookup_symbol. */
814 write_exp_elt_block (block_found);
815 write_exp_elt_sym (sym);
816 write_exp_elt_opcode (OP_VAR_VALUE); }
819 qualified_name: TYPENAME COLONCOLON name
821 struct type *type = $1.type;
822 CHECK_TYPEDEF (type);
823 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
824 && TYPE_CODE (type) != TYPE_CODE_UNION
825 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
826 error (_("`%s' is not defined as an aggregate type."),
829 write_exp_elt_opcode (OP_SCOPE);
830 write_exp_elt_type (type);
831 write_exp_string ($3);
832 write_exp_elt_opcode (OP_SCOPE);
834 | TYPENAME COLONCOLON '~' name
836 struct type *type = $1.type;
837 struct stoken tmp_token;
838 CHECK_TYPEDEF (type);
839 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
840 && TYPE_CODE (type) != TYPE_CODE_UNION
841 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
842 error (_("`%s' is not defined as an aggregate type."),
845 tmp_token.ptr = (char*) alloca ($4.length + 2);
846 tmp_token.length = $4.length + 1;
847 tmp_token.ptr[0] = '~';
848 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
849 tmp_token.ptr[tmp_token.length] = 0;
851 /* Check for valid destructor name. */
852 destructor_name_p (tmp_token.ptr, $1.type);
853 write_exp_elt_opcode (OP_SCOPE);
854 write_exp_elt_type (type);
855 write_exp_string (tmp_token);
856 write_exp_elt_opcode (OP_SCOPE);
858 | TYPENAME COLONCOLON name COLONCOLON name
860 char *copy = copy_name ($3);
861 error (_("No type \"%s\" within class "
862 "or namespace \"%s\"."),
863 copy, TYPE_NAME ($1.type));
867 variable: qualified_name
868 | COLONCOLON name_not_typename
870 char *name = copy_name ($2.stoken);
872 struct minimal_symbol *msymbol;
875 lookup_symbol (name, (const struct block *) NULL,
876 VAR_DOMAIN, (int *) NULL);
879 write_exp_elt_opcode (OP_VAR_VALUE);
880 write_exp_elt_block (NULL);
881 write_exp_elt_sym (sym);
882 write_exp_elt_opcode (OP_VAR_VALUE);
886 msymbol = lookup_minimal_symbol (name, NULL, NULL);
888 write_exp_msymbol (msymbol);
889 else if (!have_full_symbols () && !have_partial_symbols ())
890 error (_("No symbol table is loaded. Use the \"file\" command."));
892 error (_("No symbol \"%s\" in current context."), name);
896 variable: name_not_typename
897 { struct symbol *sym = $1.sym;
901 if (symbol_read_needs_frame (sym))
903 if (innermost_block == 0
904 || contained_in (block_found,
906 innermost_block = block_found;
909 write_exp_elt_opcode (OP_VAR_VALUE);
910 /* We want to use the selected frame, not
911 another more inner frame which happens to
912 be in the same block. */
913 write_exp_elt_block (NULL);
914 write_exp_elt_sym (sym);
915 write_exp_elt_opcode (OP_VAR_VALUE);
917 else if ($1.is_a_field_of_this)
919 /* C++: it hangs off of `this'. Must
920 not inadvertently convert from a method call
922 if (innermost_block == 0
923 || contained_in (block_found,
925 innermost_block = block_found;
926 write_exp_elt_opcode (OP_THIS);
927 write_exp_elt_opcode (OP_THIS);
928 write_exp_elt_opcode (STRUCTOP_PTR);
929 write_exp_string ($1.stoken);
930 write_exp_elt_opcode (STRUCTOP_PTR);
934 struct minimal_symbol *msymbol;
935 char *arg = copy_name ($1.stoken);
938 lookup_minimal_symbol (arg, NULL, NULL);
940 write_exp_msymbol (msymbol);
941 else if (!have_full_symbols () && !have_partial_symbols ())
942 error (_("No symbol table is loaded. Use the \"file\" command."));
944 error (_("No symbol \"%s\" in current context."),
945 copy_name ($1.stoken));
950 space_identifier : '@' NAME
951 { insert_type_address_space (copy_name ($2.stoken)); }
954 const_or_volatile: const_or_volatile_noopt
958 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
961 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
962 | const_or_volatile_noopt
965 const_or_volatile_or_space_identifier:
966 const_or_volatile_or_space_identifier_noopt
972 { insert_type (tp_pointer); }
973 const_or_volatile_or_space_identifier
975 { insert_type (tp_pointer); }
976 const_or_volatile_or_space_identifier
978 { insert_type (tp_reference); }
980 { insert_type (tp_reference); }
983 ptr_operator_ts: ptr_operator
985 $$ = get_type_stack ();
986 /* This cleanup is eventually run by
988 make_cleanup (type_stack_cleanup, $$);
992 abs_decl: ptr_operator_ts direct_abs_decl
993 { $$ = append_type_stack ($2, $1); }
998 direct_abs_decl: '(' abs_decl ')'
1000 | direct_abs_decl array_mod
1002 push_type_stack ($1);
1004 push_type (tp_array);
1005 $$ = get_type_stack ();
1010 push_type (tp_array);
1011 $$ = get_type_stack ();
1014 | direct_abs_decl func_mod
1016 push_type_stack ($1);
1018 $$ = get_type_stack ();
1023 $$ = get_type_stack ();
1035 | '(' parameter_typelist ')'
1039 /* We used to try to recognize pointer to member types here, but
1040 that didn't work (shift/reduce conflicts meant that these rules never
1041 got executed). The problem is that
1042 int (foo::bar::baz::bizzle)
1043 is a function type but
1044 int (foo::bar::baz::bizzle::*)
1045 is a pointer to member type. Stroustrup loses again! */
1050 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1054 { $$ = lookup_signed_typename (parse_language,
1058 { $$ = lookup_signed_typename (parse_language,
1062 { $$ = lookup_signed_typename (parse_language,
1066 { $$ = lookup_signed_typename (parse_language,
1069 | LONG SIGNED_KEYWORD INT_KEYWORD
1070 { $$ = lookup_signed_typename (parse_language,
1073 | LONG SIGNED_KEYWORD
1074 { $$ = lookup_signed_typename (parse_language,
1077 | SIGNED_KEYWORD LONG INT_KEYWORD
1078 { $$ = lookup_signed_typename (parse_language,
1081 | UNSIGNED LONG INT_KEYWORD
1082 { $$ = lookup_unsigned_typename (parse_language,
1085 | LONG UNSIGNED INT_KEYWORD
1086 { $$ = lookup_unsigned_typename (parse_language,
1090 { $$ = lookup_unsigned_typename (parse_language,
1094 { $$ = lookup_signed_typename (parse_language,
1097 | LONG LONG INT_KEYWORD
1098 { $$ = lookup_signed_typename (parse_language,
1101 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1102 { $$ = lookup_signed_typename (parse_language,
1105 | LONG LONG SIGNED_KEYWORD
1106 { $$ = lookup_signed_typename (parse_language,
1109 | SIGNED_KEYWORD LONG LONG
1110 { $$ = lookup_signed_typename (parse_language,
1113 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1114 { $$ = lookup_signed_typename (parse_language,
1117 | UNSIGNED LONG LONG
1118 { $$ = lookup_unsigned_typename (parse_language,
1121 | UNSIGNED LONG LONG INT_KEYWORD
1122 { $$ = lookup_unsigned_typename (parse_language,
1125 | LONG LONG UNSIGNED
1126 { $$ = lookup_unsigned_typename (parse_language,
1129 | LONG LONG UNSIGNED INT_KEYWORD
1130 { $$ = lookup_unsigned_typename (parse_language,
1134 { $$ = lookup_signed_typename (parse_language,
1137 | SHORT SIGNED_KEYWORD INT_KEYWORD
1138 { $$ = lookup_signed_typename (parse_language,
1141 | SHORT SIGNED_KEYWORD
1142 { $$ = lookup_signed_typename (parse_language,
1145 | UNSIGNED SHORT INT_KEYWORD
1146 { $$ = lookup_unsigned_typename (parse_language,
1150 { $$ = lookup_unsigned_typename (parse_language,
1153 | SHORT UNSIGNED INT_KEYWORD
1154 { $$ = lookup_unsigned_typename (parse_language,
1158 { $$ = lookup_typename (parse_language, parse_gdbarch,
1159 "double", (struct block *) NULL,
1161 | LONG DOUBLE_KEYWORD
1162 { $$ = lookup_typename (parse_language, parse_gdbarch,
1164 (struct block *) NULL, 0); }
1166 { $$ = lookup_struct (copy_name ($2),
1167 expression_context_block); }
1169 { $$ = lookup_struct (copy_name ($2),
1170 expression_context_block); }
1172 { $$ = lookup_union (copy_name ($2),
1173 expression_context_block); }
1175 { $$ = lookup_enum (copy_name ($2),
1176 expression_context_block); }
1178 { $$ = lookup_unsigned_typename (parse_language,
1180 TYPE_NAME($2.type)); }
1182 { $$ = lookup_unsigned_typename (parse_language,
1185 | SIGNED_KEYWORD typename
1186 { $$ = lookup_signed_typename (parse_language,
1188 TYPE_NAME($2.type)); }
1190 { $$ = lookup_signed_typename (parse_language,
1193 /* It appears that this rule for templates is never
1194 reduced; template recognition happens by lookahead
1195 in the token processing code in yylex. */
1196 | TEMPLATE name '<' type '>'
1197 { $$ = lookup_template_type(copy_name($2), $4,
1198 expression_context_block);
1200 | const_or_volatile_or_space_identifier_noopt typebase
1201 { $$ = follow_types ($2); }
1202 | typebase const_or_volatile_or_space_identifier_noopt
1203 { $$ = follow_types ($1); }
1209 $$.stoken.ptr = "int";
1210 $$.stoken.length = 3;
1211 $$.type = lookup_signed_typename (parse_language,
1217 $$.stoken.ptr = "long";
1218 $$.stoken.length = 4;
1219 $$.type = lookup_signed_typename (parse_language,
1225 $$.stoken.ptr = "short";
1226 $$.stoken.length = 5;
1227 $$.type = lookup_signed_typename (parse_language,
1235 { check_parameter_typelist ($1); }
1236 | nonempty_typelist ',' DOTDOTDOT
1238 VEC_safe_push (type_ptr, $1, NULL);
1239 check_parameter_typelist ($1);
1247 VEC (type_ptr) *typelist = NULL;
1248 VEC_safe_push (type_ptr, typelist, $1);
1251 | nonempty_typelist ',' type
1253 VEC_safe_push (type_ptr, $1, $3);
1261 push_type_stack ($2);
1262 $$ = follow_types ($1);
1266 conversion_type_id: typebase conversion_declarator
1267 { $$ = follow_types ($1); }
1270 conversion_declarator: /* Nothing. */
1271 | ptr_operator conversion_declarator
1274 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1275 | VOLATILE_KEYWORD CONST_KEYWORD
1278 const_or_volatile_noopt: const_and_volatile
1279 { insert_type (tp_const);
1280 insert_type (tp_volatile);
1283 { insert_type (tp_const); }
1285 { insert_type (tp_volatile); }
1288 operator: OPERATOR NEW
1289 { $$ = operator_stoken (" new"); }
1291 { $$ = operator_stoken (" delete"); }
1292 | OPERATOR NEW '[' ']'
1293 { $$ = operator_stoken (" new[]"); }
1294 | OPERATOR DELETE '[' ']'
1295 { $$ = operator_stoken (" delete[]"); }
1297 { $$ = operator_stoken ("+"); }
1299 { $$ = operator_stoken ("-"); }
1301 { $$ = operator_stoken ("*"); }
1303 { $$ = operator_stoken ("/"); }
1305 { $$ = operator_stoken ("%"); }
1307 { $$ = operator_stoken ("^"); }
1309 { $$ = operator_stoken ("&"); }
1311 { $$ = operator_stoken ("|"); }
1313 { $$ = operator_stoken ("~"); }
1315 { $$ = operator_stoken ("!"); }
1317 { $$ = operator_stoken ("="); }
1319 { $$ = operator_stoken ("<"); }
1321 { $$ = operator_stoken (">"); }
1322 | OPERATOR ASSIGN_MODIFY
1323 { const char *op = "unknown";
1347 case BINOP_BITWISE_IOR:
1350 case BINOP_BITWISE_AND:
1353 case BINOP_BITWISE_XOR:
1360 $$ = operator_stoken (op);
1363 { $$ = operator_stoken ("<<"); }
1365 { $$ = operator_stoken (">>"); }
1367 { $$ = operator_stoken ("=="); }
1369 { $$ = operator_stoken ("!="); }
1371 { $$ = operator_stoken ("<="); }
1373 { $$ = operator_stoken (">="); }
1375 { $$ = operator_stoken ("&&"); }
1377 { $$ = operator_stoken ("||"); }
1378 | OPERATOR INCREMENT
1379 { $$ = operator_stoken ("++"); }
1380 | OPERATOR DECREMENT
1381 { $$ = operator_stoken ("--"); }
1383 { $$ = operator_stoken (","); }
1384 | OPERATOR ARROW_STAR
1385 { $$ = operator_stoken ("->*"); }
1387 { $$ = operator_stoken ("->"); }
1389 { $$ = operator_stoken ("()"); }
1391 { $$ = operator_stoken ("[]"); }
1392 | OPERATOR conversion_type_id
1395 struct ui_file *buf = mem_fileopen ();
1397 c_print_type ($2, NULL, buf, -1, 0);
1398 name = ui_file_xstrdup (buf, &length);
1399 ui_file_delete (buf);
1400 $$ = operator_stoken (name);
1407 name : NAME { $$ = $1.stoken; }
1408 | BLOCKNAME { $$ = $1.stoken; }
1409 | TYPENAME { $$ = $1.stoken; }
1410 | NAME_OR_INT { $$ = $1.stoken; }
1411 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1412 | operator { $$ = $1; }
1415 name_not_typename : NAME
1417 /* These would be useful if name_not_typename was useful, but it is just
1418 a fake for "variable", so these cause reduce/reduce conflicts because
1419 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1420 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1421 context where only a name could occur, this might be useful.
1427 $$.sym = lookup_symbol ($1.ptr,
1428 expression_context_block,
1430 &$$.is_a_field_of_this);
1437 /* Returns a stoken of the operator name given by OP (which does not
1438 include the string "operator"). */
1439 static struct stoken
1440 operator_stoken (const char *op)
1442 static const char *operator_string = "operator";
1443 struct stoken st = { NULL, 0 };
1444 st.length = strlen (operator_string) + strlen (op);
1445 st.ptr = malloc (st.length + 1);
1446 strcpy (st.ptr, operator_string);
1447 strcat (st.ptr, op);
1449 /* The toplevel (c_parse) will free the memory allocated here. */
1450 make_cleanup (free, st.ptr);
1454 /* Validate a parameter typelist. */
1457 check_parameter_typelist (VEC (type_ptr) *params)
1462 for (ix = 0; VEC_iterate (type_ptr, params, ix, type); ++ix)
1464 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1468 if (VEC_length (type_ptr, params) == 1)
1473 VEC_free (type_ptr, params);
1474 error (_("parameter types following 'void'"));
1478 VEC_free (type_ptr, params);
1479 error (_("'void' invalid as parameter type"));
1485 /* Take care of parsing a number (anything that starts with a digit).
1486 Set yylval and return the token type; update lexptr.
1487 LEN is the number of characters in it. */
1489 /*** Needs some error checking for the float case ***/
1492 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1494 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1495 here, and we do kind of silly things like cast to unsigned. */
1502 int base = input_radix;
1505 /* Number of "L" suffixes encountered. */
1508 /* We have found a "L" or "U" suffix. */
1509 int found_suffix = 0;
1512 struct type *signed_type;
1513 struct type *unsigned_type;
1517 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1518 point. Return DECFLOAT. */
1520 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1523 putithere->typed_val_decfloat.type
1524 = parse_type->builtin_decfloat;
1525 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1526 gdbarch_byte_order (parse_gdbarch), p);
1531 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1534 putithere->typed_val_decfloat.type
1535 = parse_type->builtin_decdouble;
1536 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1537 gdbarch_byte_order (parse_gdbarch), p);
1542 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1545 putithere->typed_val_decfloat.type
1546 = parse_type->builtin_declong;
1547 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1548 gdbarch_byte_order (parse_gdbarch), p);
1553 if (! parse_c_float (parse_gdbarch, p, len,
1554 &putithere->typed_val_float.dval,
1555 &putithere->typed_val_float.type))
1560 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1604 if (c >= 'A' && c <= 'Z')
1606 if (c != 'l' && c != 'u')
1608 if (c >= '0' && c <= '9')
1616 if (base > 10 && c >= 'a' && c <= 'f')
1620 n += i = c - 'a' + 10;
1633 return ERROR; /* Char not a digit */
1636 return ERROR; /* Invalid digit in this base */
1638 /* Portably test for overflow (only works for nonzero values, so make
1639 a second check for zero). FIXME: Can't we just make n and prevn
1640 unsigned and avoid this? */
1641 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1642 unsigned_p = 1; /* Try something unsigned */
1644 /* Portably test for unsigned overflow.
1645 FIXME: This check is wrong; for example it doesn't find overflow
1646 on 0x123456789 when LONGEST is 32 bits. */
1647 if (c != 'l' && c != 'u' && n != 0)
1649 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1650 error (_("Numeric constant too large."));
1655 /* An integer constant is an int, a long, or a long long. An L
1656 suffix forces it to be long; an LL suffix forces it to be long
1657 long. If not forced to a larger size, it gets the first type of
1658 the above that it fits in. To figure out whether it fits, we
1659 shift it right and see whether anything remains. Note that we
1660 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1661 operation, because many compilers will warn about such a shift
1662 (which always produces a zero result). Sometimes gdbarch_int_bit
1663 or gdbarch_long_bit will be that big, sometimes not. To deal with
1664 the case where it is we just always shift the value more than
1665 once, with fewer bits each time. */
1667 un = (ULONGEST)n >> 2;
1669 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1671 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1673 /* A large decimal (not hex or octal) constant (between INT_MAX
1674 and UINT_MAX) is a long or unsigned long, according to ANSI,
1675 never an unsigned int, but this code treats it as unsigned
1676 int. This probably should be fixed. GCC gives a warning on
1679 unsigned_type = parse_type->builtin_unsigned_int;
1680 signed_type = parse_type->builtin_int;
1682 else if (long_p <= 1
1683 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1685 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1686 unsigned_type = parse_type->builtin_unsigned_long;
1687 signed_type = parse_type->builtin_long;
1692 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1693 < gdbarch_long_long_bit (parse_gdbarch))
1694 /* A long long does not fit in a LONGEST. */
1695 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1697 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1698 high_bit = (ULONGEST) 1 << shift;
1699 unsigned_type = parse_type->builtin_unsigned_long_long;
1700 signed_type = parse_type->builtin_long_long;
1703 putithere->typed_val_int.val = n;
1705 /* If the high bit of the worked out type is set then this number
1706 has to be unsigned. */
1708 if (unsigned_p || (n & high_bit))
1710 putithere->typed_val_int.type = unsigned_type;
1714 putithere->typed_val_int.type = signed_type;
1720 /* Temporary obstack used for holding strings. */
1721 static struct obstack tempbuf;
1722 static int tempbuf_init;
1724 /* Parse a C escape sequence. The initial backslash of the sequence
1725 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1726 last character of the sequence. If OUTPUT is not NULL, the
1727 translated form of the escape sequence will be written there. If
1728 OUTPUT is NULL, no output is written and the call will only affect
1729 *PTR. If an escape sequence is expressed in target bytes, then the
1730 entire sequence will simply be copied to OUTPUT. Return 1 if any
1731 character was emitted, 0 otherwise. */
1734 c_parse_escape (char **ptr, struct obstack *output)
1736 char *tokptr = *ptr;
1739 /* Some escape sequences undergo character set conversion. Those we
1743 /* Hex escapes do not undergo character set conversion, so keep
1744 the escape sequence for later. */
1747 obstack_grow_str (output, "\\x");
1749 if (!isxdigit (*tokptr))
1750 error (_("\\x escape without a following hex digit"));
1751 while (isxdigit (*tokptr))
1754 obstack_1grow (output, *tokptr);
1759 /* Octal escapes do not undergo character set conversion, so
1760 keep the escape sequence for later. */
1772 obstack_grow_str (output, "\\");
1774 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1778 obstack_1grow (output, *tokptr);
1784 /* We handle UCNs later. We could handle them here, but that
1785 would mean a spurious error in the case where the UCN could
1786 be converted to the target charset but not the host
1792 int i, len = c == 'U' ? 8 : 4;
1795 obstack_1grow (output, '\\');
1796 obstack_1grow (output, *tokptr);
1799 if (!isxdigit (*tokptr))
1800 error (_("\\%c escape without a following hex digit"), c);
1801 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1804 obstack_1grow (output, *tokptr);
1810 /* We must pass backslash through so that it does not
1811 cause quoting during the second expansion. */
1814 obstack_grow_str (output, "\\\\");
1818 /* Escapes which undergo conversion. */
1821 obstack_1grow (output, '\a');
1826 obstack_1grow (output, '\b');
1831 obstack_1grow (output, '\f');
1836 obstack_1grow (output, '\n');
1841 obstack_1grow (output, '\r');
1846 obstack_1grow (output, '\t');
1851 obstack_1grow (output, '\v');
1855 /* GCC extension. */
1858 obstack_1grow (output, HOST_ESCAPE_CHAR);
1862 /* Backslash-newline expands to nothing at all. */
1868 /* A few escapes just expand to the character itself. */
1872 /* GCC extensions. */
1877 /* Unrecognized escapes turn into the character itself. */
1880 obstack_1grow (output, *tokptr);
1888 /* Parse a string or character literal from TOKPTR. The string or
1889 character may be wide or unicode. *OUTPTR is set to just after the
1890 end of the literal in the input string. The resulting token is
1891 stored in VALUE. This returns a token value, either STRING or
1892 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1893 number of host characters in the literal. */
1895 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1899 enum c_string_type type;
1901 /* Build the gdb internal form of the input string in tempbuf. Note
1902 that the buffer is null byte terminated *only* for the
1903 convenience of debugging gdb itself and printing the buffer
1904 contents when the buffer contains no embedded nulls. Gdb does
1905 not depend upon the buffer being null byte terminated, it uses
1906 the length string instead. This allows gdb to handle C strings
1907 (as well as strings in other languages) with embedded null
1913 obstack_free (&tempbuf, NULL);
1914 obstack_init (&tempbuf);
1916 /* Record the string type. */
1919 type = C_WIDE_STRING;
1922 else if (*tokptr == 'u')
1927 else if (*tokptr == 'U')
1935 /* Skip the quote. */
1949 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1951 else if (c == quote)
1955 obstack_1grow (&tempbuf, c);
1957 /* FIXME: this does the wrong thing with multi-byte host
1958 characters. We could use mbrlen here, but that would
1959 make "set host-charset" a bit less useful. */
1964 if (*tokptr != quote)
1967 error (_("Unterminated string in expression."));
1969 error (_("Unmatched single quote."));
1974 value->ptr = obstack_base (&tempbuf);
1975 value->length = obstack_object_size (&tempbuf);
1979 return quote == '"' ? STRING : CHAR;
1982 /* This is used to associate some attributes with a token. */
1986 /* If this bit is set, the token is C++-only. */
1990 /* If this bit is set, the token is conditional: if there is a
1991 symbol of the same name, then the token is a symbol; otherwise,
1992 the token is a keyword. */
2001 enum exp_opcode opcode;
2002 enum token_flags flags;
2005 static const struct token tokentab3[] =
2007 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2008 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2009 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2010 {"...", DOTDOTDOT, BINOP_END, 0}
2013 static const struct token tokentab2[] =
2015 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2016 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2017 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2018 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2019 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2020 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2021 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2022 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2023 {"++", INCREMENT, BINOP_END, 0},
2024 {"--", DECREMENT, BINOP_END, 0},
2025 {"->", ARROW, BINOP_END, 0},
2026 {"&&", ANDAND, BINOP_END, 0},
2027 {"||", OROR, BINOP_END, 0},
2028 /* "::" is *not* only C++: gdb overrides its meaning in several
2029 different ways, e.g., 'filename'::func, function::variable. */
2030 {"::", COLONCOLON, BINOP_END, 0},
2031 {"<<", LSH, BINOP_END, 0},
2032 {">>", RSH, BINOP_END, 0},
2033 {"==", EQUAL, BINOP_END, 0},
2034 {"!=", NOTEQUAL, BINOP_END, 0},
2035 {"<=", LEQ, BINOP_END, 0},
2036 {">=", GEQ, BINOP_END, 0},
2037 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2040 /* Identifier-like tokens. */
2041 static const struct token ident_tokens[] =
2043 {"unsigned", UNSIGNED, OP_NULL, 0},
2044 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2045 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2046 {"struct", STRUCT, OP_NULL, 0},
2047 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2048 {"sizeof", SIZEOF, OP_NULL, 0},
2049 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2050 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2051 {"class", CLASS, OP_NULL, FLAG_CXX},
2052 {"union", UNION, OP_NULL, 0},
2053 {"short", SHORT, OP_NULL, 0},
2054 {"const", CONST_KEYWORD, OP_NULL, 0},
2055 {"enum", ENUM, OP_NULL, 0},
2056 {"long", LONG, OP_NULL, 0},
2057 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2058 {"int", INT_KEYWORD, OP_NULL, 0},
2059 {"new", NEW, OP_NULL, FLAG_CXX},
2060 {"delete", DELETE, OP_NULL, FLAG_CXX},
2061 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2063 {"and", ANDAND, BINOP_END, FLAG_CXX},
2064 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2065 {"bitand", '&', OP_NULL, FLAG_CXX},
2066 {"bitor", '|', OP_NULL, FLAG_CXX},
2067 {"compl", '~', OP_NULL, FLAG_CXX},
2068 {"not", '!', OP_NULL, FLAG_CXX},
2069 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2070 {"or", OROR, BINOP_END, FLAG_CXX},
2071 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2072 {"xor", '^', OP_NULL, FLAG_CXX},
2073 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2075 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2076 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2077 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2078 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2080 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2081 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2082 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2083 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2084 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW }
2087 /* When we find that lexptr (the global var defined in parse.c) is
2088 pointing at a macro invocation, we expand the invocation, and call
2089 scan_macro_expansion to save the old lexptr here and point lexptr
2090 into the expanded text. When we reach the end of that, we call
2091 end_macro_expansion to pop back to the value we saved here. The
2092 macro expansion code promises to return only fully-expanded text,
2093 so we don't need to "push" more than one level.
2095 This is disgusting, of course. It would be cleaner to do all macro
2096 expansion beforehand, and then hand that to lexptr. But we don't
2097 really know where the expression ends. Remember, in a command like
2099 (gdb) break *ADDRESS if CONDITION
2101 we evaluate ADDRESS in the scope of the current frame, but we
2102 evaluate CONDITION in the scope of the breakpoint's location. So
2103 it's simply wrong to try to macro-expand the whole thing at once. */
2104 static char *macro_original_text;
2106 /* We save all intermediate macro expansions on this obstack for the
2107 duration of a single parse. The expansion text may sometimes have
2108 to live past the end of the expansion, due to yacc lookahead.
2109 Rather than try to be clever about saving the data for a single
2110 token, we simply keep it all and delete it after parsing has
2112 static struct obstack expansion_obstack;
2115 scan_macro_expansion (char *expansion)
2119 /* We'd better not be trying to push the stack twice. */
2120 gdb_assert (! macro_original_text);
2122 /* Copy to the obstack, and then free the intermediate
2124 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2127 /* Save the old lexptr value, so we can return to it when we're done
2128 parsing the expanded text. */
2129 macro_original_text = lexptr;
2135 scanning_macro_expansion (void)
2137 return macro_original_text != 0;
2142 finished_macro_expansion (void)
2144 /* There'd better be something to pop back to. */
2145 gdb_assert (macro_original_text);
2147 /* Pop back to the original text. */
2148 lexptr = macro_original_text;
2149 macro_original_text = 0;
2154 scan_macro_cleanup (void *dummy)
2156 if (macro_original_text)
2157 finished_macro_expansion ();
2159 obstack_free (&expansion_obstack, NULL);
2162 /* Return true iff the token represents a C++ cast operator. */
2165 is_cast_operator (const char *token, int len)
2167 return (! strncmp (token, "dynamic_cast", len)
2168 || ! strncmp (token, "static_cast", len)
2169 || ! strncmp (token, "reinterpret_cast", len)
2170 || ! strncmp (token, "const_cast", len));
2173 /* The scope used for macro expansion. */
2174 static struct macro_scope *expression_macro_scope;
2176 /* This is set if a NAME token appeared at the very end of the input
2177 string, with no whitespace separating the name from the EOF. This
2178 is used only when parsing to do field name completion. */
2179 static int saw_name_at_eof;
2181 /* This is set if the previously-returned token was a structure
2182 operator -- either '.' or ARROW. This is used only when parsing to
2183 do field name completion. */
2184 static int last_was_structop;
2186 /* Read one token, getting characters through lexptr. */
2189 lex_one_token (void)
2195 int saw_structop = last_was_structop;
2198 last_was_structop = 0;
2202 /* Check if this is a macro invocation that we need to expand. */
2203 if (! scanning_macro_expansion ())
2205 char *expanded = macro_expand_next (&lexptr,
2206 standard_macro_lookup,
2207 expression_macro_scope);
2210 scan_macro_expansion (expanded);
2213 prev_lexptr = lexptr;
2216 /* See if it is a special token of length 3. */
2217 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2218 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2220 if ((tokentab3[i].flags & FLAG_CXX) != 0
2221 && parse_language->la_language != language_cplus)
2225 yylval.opcode = tokentab3[i].opcode;
2226 return tokentab3[i].token;
2229 /* See if it is a special token of length 2. */
2230 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2231 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2233 if ((tokentab2[i].flags & FLAG_CXX) != 0
2234 && parse_language->la_language != language_cplus)
2238 yylval.opcode = tokentab2[i].opcode;
2239 if (in_parse_field && tokentab2[i].token == ARROW)
2240 last_was_structop = 1;
2241 return tokentab2[i].token;
2244 switch (c = *tokstart)
2247 /* If we were just scanning the result of a macro expansion,
2248 then we need to resume scanning the original text.
2249 If we're parsing for field name completion, and the previous
2250 token allows such completion, return a COMPLETE token.
2251 Otherwise, we were already scanning the original text, and
2252 we're really done. */
2253 if (scanning_macro_expansion ())
2255 finished_macro_expansion ();
2258 else if (saw_name_at_eof)
2260 saw_name_at_eof = 0;
2263 else if (saw_structop)
2282 if (paren_depth == 0)
2289 if (comma_terminates
2291 && ! scanning_macro_expansion ())
2297 /* Might be a floating point number. */
2298 if (lexptr[1] < '0' || lexptr[1] > '9')
2301 last_was_structop = 1;
2302 goto symbol; /* Nope, must be a symbol. */
2304 /* FALL THRU into number case. */
2317 /* It's a number. */
2318 int got_dot = 0, got_e = 0, toktype;
2320 int hex = input_radix > 10;
2322 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2327 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2335 /* This test includes !hex because 'e' is a valid hex digit
2336 and thus does not indicate a floating point number when
2337 the radix is hex. */
2338 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2339 got_dot = got_e = 1;
2340 /* This test does not include !hex, because a '.' always indicates
2341 a decimal floating point number regardless of the radix. */
2342 else if (!got_dot && *p == '.')
2344 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2345 && (*p == '-' || *p == '+'))
2346 /* This is the sign of the exponent, not the end of the
2349 /* We will take any letters or digits. parse_number will
2350 complain if past the radix, or if L or U are not final. */
2351 else if ((*p < '0' || *p > '9')
2352 && ((*p < 'a' || *p > 'z')
2353 && (*p < 'A' || *p > 'Z')))
2356 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2357 if (toktype == ERROR)
2359 char *err_copy = (char *) alloca (p - tokstart + 1);
2361 memcpy (err_copy, tokstart, p - tokstart);
2362 err_copy[p - tokstart] = 0;
2363 error (_("Invalid number \"%s\"."), err_copy);
2371 char *p = &tokstart[1];
2372 size_t len = strlen ("entry");
2374 while (isspace (*p))
2376 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2408 if (tokstart[1] != '"' && tokstart[1] != '\'')
2415 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2420 error (_("Empty character constant."));
2421 else if (host_len > 2 && c == '\'')
2424 namelen = lexptr - tokstart - 1;
2427 else if (host_len > 1)
2428 error (_("Invalid character constant."));
2434 if (!(c == '_' || c == '$'
2435 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2436 /* We must have come across a bad character (e.g. ';'). */
2437 error (_("Invalid character '%c' in expression."), c);
2439 /* It's a name. See how long it is. */
2441 for (c = tokstart[namelen];
2442 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2443 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2445 /* Template parameter lists are part of the name.
2446 FIXME: This mishandles `print $a<4&&$a>3'. */
2450 if (! is_cast_operator (tokstart, namelen))
2452 /* Scan ahead to get rest of the template specification. Note
2453 that we look ahead only when the '<' adjoins non-whitespace
2454 characters; for comparison expressions, e.g. "a < b > c",
2455 there must be spaces before the '<', etc. */
2457 char * p = find_template_name_end (tokstart + namelen);
2459 namelen = p - tokstart;
2463 c = tokstart[++namelen];
2466 /* The token "if" terminates the expression and is NOT removed from
2467 the input stream. It doesn't count if it appears in the
2468 expansion of a macro. */
2470 && tokstart[0] == 'i'
2471 && tokstart[1] == 'f'
2472 && ! scanning_macro_expansion ())
2477 /* For the same reason (breakpoint conditions), "thread N"
2478 terminates the expression. "thread" could be an identifier, but
2479 an identifier is never followed by a number without intervening
2480 punctuation. "task" is similar. Handle abbreviations of these,
2481 similarly to breakpoint.c:find_condition_and_thread. */
2483 && (strncmp (tokstart, "thread", namelen) == 0
2484 || strncmp (tokstart, "task", namelen) == 0)
2485 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2486 && ! scanning_macro_expansion ())
2488 char *p = tokstart + namelen + 1;
2489 while (*p == ' ' || *p == '\t')
2491 if (*p >= '0' && *p <= '9')
2499 yylval.sval.ptr = tokstart;
2500 yylval.sval.length = namelen;
2502 /* Catch specific keywords. */
2503 copy = copy_name (yylval.sval);
2504 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2505 if (strcmp (copy, ident_tokens[i].operator) == 0)
2507 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2508 && parse_language->la_language != language_cplus)
2511 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2513 int is_a_field_of_this = 0;
2515 if (lookup_symbol (copy, expression_context_block,
2517 (parse_language->la_language == language_cplus
2518 ? &is_a_field_of_this
2522 /* The keyword is shadowed. */
2527 /* It is ok to always set this, even though we don't always
2528 strictly need to. */
2529 yylval.opcode = ident_tokens[i].opcode;
2530 return ident_tokens[i].token;
2533 if (*tokstart == '$')
2536 if (in_parse_field && *lexptr == '\0')
2537 saw_name_at_eof = 1;
2541 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2548 DEF_VEC_O (token_and_value);
2550 /* A FIFO of tokens that have been read but not yet returned to the
2552 static VEC (token_and_value) *token_fifo;
2554 /* Non-zero if the lexer should return tokens from the FIFO. */
2557 /* Temporary storage for c_lex; this holds symbol names as they are
2559 static struct obstack name_obstack;
2561 /* Classify a NAME token. The contents of the token are in `yylval'.
2562 Updates yylval and returns the new token type. BLOCK is the block
2563 in which lookups start; this can be NULL to mean the global
2566 classify_name (struct block *block)
2570 int is_a_field_of_this = 0;
2572 copy = copy_name (yylval.sval);
2574 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2575 parse_language->la_language == language_cplus
2576 ? &is_a_field_of_this : (int *) NULL);
2578 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2580 yylval.ssym.sym = sym;
2581 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2586 /* See if it's a file name. */
2587 struct symtab *symtab;
2589 symtab = lookup_symtab (copy);
2592 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2597 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2599 yylval.tsym.type = SYMBOL_TYPE (sym);
2604 = language_lookup_primitive_type_by_name (parse_language,
2605 parse_gdbarch, copy);
2606 if (yylval.tsym.type != NULL)
2609 /* Input names that aren't symbols but ARE valid hex numbers, when
2610 the input radix permits them, can be names or numbers depending
2611 on the parse. Note we support radixes > 16 here. */
2613 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2614 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2616 YYSTYPE newlval; /* Its value is ignored. */
2617 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2620 yylval.ssym.sym = sym;
2621 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2626 /* Any other kind of symbol */
2627 yylval.ssym.sym = sym;
2628 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2631 && parse_language->la_language == language_cplus
2632 && !is_a_field_of_this
2633 && !lookup_minimal_symbol (copy, NULL, NULL))
2634 return UNKNOWN_CPP_NAME;
2639 /* Like classify_name, but used by the inner loop of the lexer, when a
2640 name might have already been seen. FIRST_NAME is true if the token
2641 in `yylval' is the first component of a name, false otherwise. */
2644 classify_inner_name (struct block *block, int first_name)
2646 struct type *type, *new_type;
2650 return classify_name (block);
2652 type = check_typedef (yylval.tsym.type);
2653 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2654 && TYPE_CODE (type) != TYPE_CODE_UNION
2655 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2658 copy = copy_name (yylval.tsym.stoken);
2659 yylval.ssym.sym = cp_lookup_nested_symbol (yylval.tsym.type, copy, block);
2660 if (yylval.ssym.sym == NULL)
2663 switch (SYMBOL_CLASS (yylval.ssym.sym))
2670 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym);;
2674 yylval.ssym.is_a_field_of_this = 0;
2677 internal_error (__FILE__, __LINE__, _("not reached"));
2680 /* The outer level of a two-level lexer. This calls the inner lexer
2681 to return tokens. It then either returns these tokens, or
2682 aggregates them into a larger token. This lets us work around a
2683 problem in our parsing approach, where the parser could not
2684 distinguish between qualified names and qualified types at the
2687 This approach is still not ideal, because it mishandles template
2688 types. See the comment in lex_one_token for an example. However,
2689 this is still an improvement over the earlier approach, and will
2690 suffice until we move to better parsing technology. */
2694 token_and_value current;
2695 int first_was_coloncolon, last_was_coloncolon, first_iter;
2697 if (popping && !VEC_empty (token_and_value, token_fifo))
2699 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2700 VEC_ordered_remove (token_and_value, token_fifo, 0);
2706 current.token = lex_one_token ();
2707 if (current.token == NAME)
2708 current.token = classify_name (expression_context_block);
2709 if (parse_language->la_language != language_cplus
2710 || (current.token != TYPENAME && current.token != COLONCOLON))
2711 return current.token;
2713 first_was_coloncolon = current.token == COLONCOLON;
2714 last_was_coloncolon = first_was_coloncolon;
2715 obstack_free (&name_obstack, obstack_base (&name_obstack));
2716 if (!last_was_coloncolon)
2717 obstack_grow (&name_obstack, yylval.sval.ptr, yylval.sval.length);
2718 current.value = yylval;
2722 token_and_value next;
2724 next.token = lex_one_token ();
2725 next.value = yylval;
2727 if (next.token == NAME && last_was_coloncolon)
2731 classification = classify_inner_name (first_was_coloncolon
2733 : expression_context_block,
2735 /* We keep going until we either run out of names, or until
2736 we have a qualified name which is not a type. */
2737 if (classification != TYPENAME && classification != NAME)
2739 /* Push the final component and leave the loop. */
2740 VEC_safe_push (token_and_value, token_fifo, &next);
2744 /* Update the partial name we are constructing. */
2747 /* We don't want to put a leading "::" into the name. */
2748 obstack_grow_str (&name_obstack, "::");
2750 obstack_grow (&name_obstack, next.value.sval.ptr,
2751 next.value.sval.length);
2753 yylval.sval.ptr = obstack_base (&name_obstack);
2754 yylval.sval.length = obstack_object_size (&name_obstack);
2755 current.value = yylval;
2756 current.token = classification;
2758 last_was_coloncolon = 0;
2760 else if (next.token == COLONCOLON && !last_was_coloncolon)
2761 last_was_coloncolon = 1;
2764 /* We've reached the end of the name. */
2765 VEC_safe_push (token_and_value, token_fifo, &next);
2774 /* If we ended with a "::", insert it too. */
2775 if (last_was_coloncolon)
2778 memset (&cc, 0, sizeof (token_and_value));
2779 if (first_was_coloncolon && first_iter)
2784 cc.token = COLONCOLON;
2785 VEC_safe_insert (token_and_value, token_fifo, 0, &cc);
2788 yylval = current.value;
2789 yylval.sval.ptr = obstack_copy0 (&expansion_obstack,
2791 yylval.sval.length);
2792 return current.token;
2799 struct cleanup *back_to = make_cleanup (free_current_contents,
2800 &expression_macro_scope);
2802 /* Set up the scope for macro expansion. */
2803 expression_macro_scope = NULL;
2805 if (expression_context_block)
2806 expression_macro_scope
2807 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2809 expression_macro_scope = default_macro_scope ();
2810 if (! expression_macro_scope)
2811 expression_macro_scope = user_macro_scope ();
2813 /* Initialize macro expansion code. */
2814 obstack_init (&expansion_obstack);
2815 gdb_assert (! macro_original_text);
2816 make_cleanup (scan_macro_cleanup, 0);
2818 make_cleanup_restore_integer (&yydebug);
2819 yydebug = parser_debug;
2821 /* Initialize some state used by the lexer. */
2822 last_was_structop = 0;
2823 saw_name_at_eof = 0;
2825 VEC_free (token_and_value, token_fifo);
2827 obstack_init (&name_obstack);
2828 make_cleanup_obstack_free (&name_obstack);
2830 result = yyparse ();
2831 do_cleanups (back_to);
2840 lexptr = prev_lexptr;
2842 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);