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
223 /* Special type cases, put in to allow the parser to distinguish different
225 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
227 %token <sval> VARIABLE
229 %token <opcode> ASSIGN_MODIFY
238 %right '=' ASSIGN_MODIFY
246 %left '<' '>' LEQ GEQ
251 %right UNARY INCREMENT DECREMENT
252 %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
253 %token <ssym> BLOCKNAME
254 %token <bval> FILENAME
268 { write_exp_elt_opcode(OP_TYPE);
269 write_exp_elt_type($1);
270 write_exp_elt_opcode(OP_TYPE);}
273 /* Expressions, including the comma operator. */
276 { write_exp_elt_opcode (BINOP_COMMA); }
279 /* Expressions, not including the comma operator. */
280 exp : '*' exp %prec UNARY
281 { write_exp_elt_opcode (UNOP_IND); }
284 exp : '&' exp %prec UNARY
285 { write_exp_elt_opcode (UNOP_ADDR); }
288 exp : '-' exp %prec UNARY
289 { write_exp_elt_opcode (UNOP_NEG); }
292 exp : '+' exp %prec UNARY
293 { write_exp_elt_opcode (UNOP_PLUS); }
296 exp : '!' exp %prec UNARY
297 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
300 exp : '~' exp %prec UNARY
301 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
304 exp : INCREMENT exp %prec UNARY
305 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
308 exp : DECREMENT exp %prec UNARY
309 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
312 exp : exp INCREMENT %prec UNARY
313 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
316 exp : exp DECREMENT %prec UNARY
317 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
320 exp : SIZEOF exp %prec UNARY
321 { write_exp_elt_opcode (UNOP_SIZEOF); }
325 { write_exp_elt_opcode (STRUCTOP_PTR);
326 write_exp_string ($3);
327 write_exp_elt_opcode (STRUCTOP_PTR); }
330 exp : exp ARROW name COMPLETE
331 { mark_struct_expression ();
332 write_exp_elt_opcode (STRUCTOP_PTR);
333 write_exp_string ($3);
334 write_exp_elt_opcode (STRUCTOP_PTR); }
337 exp : exp ARROW COMPLETE
339 mark_struct_expression ();
340 write_exp_elt_opcode (STRUCTOP_PTR);
343 write_exp_string (s);
344 write_exp_elt_opcode (STRUCTOP_PTR); }
347 exp : exp ARROW qualified_name
348 { /* exp->type::name becomes exp->*(&type::name) */
349 /* Note: this doesn't work if name is a
350 static member! FIXME */
351 write_exp_elt_opcode (UNOP_ADDR);
352 write_exp_elt_opcode (STRUCTOP_MPTR); }
355 exp : exp ARROW_STAR exp
356 { write_exp_elt_opcode (STRUCTOP_MPTR); }
360 { write_exp_elt_opcode (STRUCTOP_STRUCT);
361 write_exp_string ($3);
362 write_exp_elt_opcode (STRUCTOP_STRUCT); }
365 exp : exp '.' name COMPLETE
366 { mark_struct_expression ();
367 write_exp_elt_opcode (STRUCTOP_STRUCT);
368 write_exp_string ($3);
369 write_exp_elt_opcode (STRUCTOP_STRUCT); }
372 exp : exp '.' COMPLETE
374 mark_struct_expression ();
375 write_exp_elt_opcode (STRUCTOP_STRUCT);
378 write_exp_string (s);
379 write_exp_elt_opcode (STRUCTOP_STRUCT); }
382 exp : exp '.' qualified_name
383 { /* exp.type::name becomes exp.*(&type::name) */
384 /* Note: this doesn't work if name is a
385 static member! FIXME */
386 write_exp_elt_opcode (UNOP_ADDR);
387 write_exp_elt_opcode (STRUCTOP_MEMBER); }
390 exp : exp DOT_STAR exp
391 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
394 exp : exp '[' exp1 ']'
395 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
399 /* This is to save the value of arglist_len
400 being accumulated by an outer function call. */
401 { start_arglist (); }
402 arglist ')' %prec ARROW
403 { write_exp_elt_opcode (OP_FUNCALL);
404 write_exp_elt_longcst ((LONGEST) end_arglist ());
405 write_exp_elt_opcode (OP_FUNCALL); }
408 exp : UNKNOWN_CPP_NAME '('
410 /* This could potentially be a an argument defined
411 lookup function (Koenig). */
412 write_exp_elt_opcode (OP_ADL_FUNC);
413 write_exp_elt_block (expression_context_block);
414 write_exp_elt_sym (NULL); /* Placeholder. */
415 write_exp_string ($1.stoken);
416 write_exp_elt_opcode (OP_ADL_FUNC);
418 /* This is to save the value of arglist_len
419 being accumulated by an outer function call. */
423 arglist ')' %prec ARROW
425 write_exp_elt_opcode (OP_FUNCALL);
426 write_exp_elt_longcst ((LONGEST) end_arglist ());
427 write_exp_elt_opcode (OP_FUNCALL);
432 { start_arglist (); }
442 arglist : arglist ',' exp %prec ABOVE_COMMA
446 exp : exp '(' parameter_typelist ')' const_or_volatile
448 VEC (type_ptr) *type_list = $3;
449 struct type *type_elt;
450 LONGEST len = VEC_length (type_ptr, type_list);
452 write_exp_elt_opcode (TYPE_INSTANCE);
453 write_exp_elt_longcst (len);
455 VEC_iterate (type_ptr, type_list, i, type_elt);
457 write_exp_elt_type (type_elt);
458 write_exp_elt_longcst(len);
459 write_exp_elt_opcode (TYPE_INSTANCE);
460 VEC_free (type_ptr, type_list);
465 { $$ = end_arglist () - 1; }
467 exp : lcurly arglist rcurly %prec ARROW
468 { write_exp_elt_opcode (OP_ARRAY);
469 write_exp_elt_longcst ((LONGEST) 0);
470 write_exp_elt_longcst ((LONGEST) $3);
471 write_exp_elt_opcode (OP_ARRAY); }
474 exp : lcurly type_exp rcurly exp %prec UNARY
475 { write_exp_elt_opcode (UNOP_MEMVAL_TYPE); }
478 exp : '(' type_exp ')' exp %prec UNARY
479 { write_exp_elt_opcode (UNOP_CAST_TYPE); }
486 /* Binary operators in order of decreasing precedence. */
489 { write_exp_elt_opcode (BINOP_REPEAT); }
493 { write_exp_elt_opcode (BINOP_MUL); }
497 { write_exp_elt_opcode (BINOP_DIV); }
501 { write_exp_elt_opcode (BINOP_REM); }
505 { write_exp_elt_opcode (BINOP_ADD); }
509 { write_exp_elt_opcode (BINOP_SUB); }
513 { write_exp_elt_opcode (BINOP_LSH); }
517 { write_exp_elt_opcode (BINOP_RSH); }
521 { write_exp_elt_opcode (BINOP_EQUAL); }
524 exp : exp NOTEQUAL exp
525 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
529 { write_exp_elt_opcode (BINOP_LEQ); }
533 { write_exp_elt_opcode (BINOP_GEQ); }
537 { write_exp_elt_opcode (BINOP_LESS); }
541 { write_exp_elt_opcode (BINOP_GTR); }
545 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
549 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
553 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
557 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
561 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
564 exp : exp '?' exp ':' exp %prec '?'
565 { write_exp_elt_opcode (TERNOP_COND); }
569 { write_exp_elt_opcode (BINOP_ASSIGN); }
572 exp : exp ASSIGN_MODIFY exp
573 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
574 write_exp_elt_opcode ($2);
575 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
579 { write_exp_elt_opcode (OP_LONG);
580 write_exp_elt_type ($1.type);
581 write_exp_elt_longcst ((LONGEST)($1.val));
582 write_exp_elt_opcode (OP_LONG); }
587 struct stoken_vector vec;
590 write_exp_string_vector ($1.type, &vec);
596 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
597 write_exp_elt_opcode (OP_LONG);
598 write_exp_elt_type (val.typed_val_int.type);
599 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
600 write_exp_elt_opcode (OP_LONG);
606 { write_exp_elt_opcode (OP_DOUBLE);
607 write_exp_elt_type ($1.type);
608 write_exp_elt_dblcst ($1.dval);
609 write_exp_elt_opcode (OP_DOUBLE); }
613 { write_exp_elt_opcode (OP_DECFLOAT);
614 write_exp_elt_type ($1.type);
615 write_exp_elt_decfloatcst ($1.val);
616 write_exp_elt_opcode (OP_DECFLOAT); }
624 write_dollar_variable ($1);
628 exp : SIZEOF '(' type ')' %prec UNARY
629 { write_exp_elt_opcode (OP_LONG);
630 write_exp_elt_type (lookup_signed_typename
631 (parse_language, parse_gdbarch,
634 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
635 write_exp_elt_opcode (OP_LONG); }
638 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
639 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
642 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
643 { write_exp_elt_opcode (UNOP_CAST_TYPE); }
646 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
647 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
650 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
651 { /* We could do more error checking here, but
652 it doesn't seem worthwhile. */
653 write_exp_elt_opcode (UNOP_CAST_TYPE); }
659 /* We copy the string here, and not in the
660 lexer, to guarantee that we do not leak a
661 string. Note that we follow the
662 NUL-termination convention of the
664 struct typed_stoken *vec = XNEW (struct typed_stoken);
669 vec->length = $1.length;
670 vec->ptr = malloc ($1.length + 1);
671 memcpy (vec->ptr, $1.ptr, $1.length + 1);
676 /* Note that we NUL-terminate here, but just
680 $$.tokens = realloc ($$.tokens,
681 $$.len * sizeof (struct typed_stoken));
683 p = malloc ($2.length + 1);
684 memcpy (p, $2.ptr, $2.length + 1);
686 $$.tokens[$$.len - 1].type = $2.type;
687 $$.tokens[$$.len - 1].length = $2.length;
688 $$.tokens[$$.len - 1].ptr = p;
695 enum c_string_type type = C_STRING;
697 for (i = 0; i < $1.len; ++i)
699 switch ($1.tokens[i].type)
707 && type != $1.tokens[i].type)
708 error (_("Undefined string concatenation."));
709 type = $1.tokens[i].type;
713 internal_error (__FILE__, __LINE__,
714 "unrecognized type in string concatenation");
718 write_exp_string_vector (type, &$1);
719 for (i = 0; i < $1.len; ++i)
720 free ($1.tokens[i].ptr);
727 { write_exp_elt_opcode (OP_LONG);
728 write_exp_elt_type (parse_type->builtin_bool);
729 write_exp_elt_longcst ((LONGEST) 1);
730 write_exp_elt_opcode (OP_LONG); }
734 { write_exp_elt_opcode (OP_LONG);
735 write_exp_elt_type (parse_type->builtin_bool);
736 write_exp_elt_longcst ((LONGEST) 0);
737 write_exp_elt_opcode (OP_LONG); }
745 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
747 error (_("No file or function \"%s\"."),
748 copy_name ($1.stoken));
756 block : block COLONCOLON name
758 = lookup_symbol (copy_name ($3), $1,
759 VAR_DOMAIN, (int *) NULL);
760 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
761 error (_("No function \"%s\" in specified context."),
763 $$ = SYMBOL_BLOCK_VALUE (tem); }
766 variable: name_not_typename ENTRY
767 { struct symbol *sym = $1.sym;
769 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
770 || !symbol_read_needs_frame (sym))
771 error (_("@entry can be used only for function "
772 "parameters, not for \"%s\""),
773 copy_name ($1.stoken));
775 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
776 write_exp_elt_sym (sym);
777 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
781 variable: block COLONCOLON name
782 { struct symbol *sym;
783 sym = lookup_symbol (copy_name ($3), $1,
784 VAR_DOMAIN, (int *) NULL);
786 error (_("No symbol \"%s\" in specified context."),
788 if (symbol_read_needs_frame (sym))
790 if (innermost_block == 0
791 || contained_in (block_found,
793 innermost_block = block_found;
796 write_exp_elt_opcode (OP_VAR_VALUE);
797 /* block_found is set by lookup_symbol. */
798 write_exp_elt_block (block_found);
799 write_exp_elt_sym (sym);
800 write_exp_elt_opcode (OP_VAR_VALUE); }
803 qualified_name: TYPENAME COLONCOLON name
805 struct type *type = $1.type;
806 CHECK_TYPEDEF (type);
807 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
808 && TYPE_CODE (type) != TYPE_CODE_UNION
809 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
810 error (_("`%s' is not defined as an aggregate type."),
813 write_exp_elt_opcode (OP_SCOPE);
814 write_exp_elt_type (type);
815 write_exp_string ($3);
816 write_exp_elt_opcode (OP_SCOPE);
818 | TYPENAME COLONCOLON '~' name
820 struct type *type = $1.type;
821 struct stoken tmp_token;
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 tmp_token.ptr = (char*) alloca ($4.length + 2);
830 tmp_token.length = $4.length + 1;
831 tmp_token.ptr[0] = '~';
832 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
833 tmp_token.ptr[tmp_token.length] = 0;
835 /* Check for valid destructor name. */
836 destructor_name_p (tmp_token.ptr, $1.type);
837 write_exp_elt_opcode (OP_SCOPE);
838 write_exp_elt_type (type);
839 write_exp_string (tmp_token);
840 write_exp_elt_opcode (OP_SCOPE);
842 | TYPENAME COLONCOLON name COLONCOLON name
844 char *copy = copy_name ($3);
845 error (_("No type \"%s\" within class "
846 "or namespace \"%s\"."),
847 copy, TYPE_NAME ($1.type));
851 variable: qualified_name
852 | COLONCOLON name_not_typename
854 char *name = copy_name ($2.stoken);
856 struct minimal_symbol *msymbol;
859 lookup_symbol (name, (const struct block *) NULL,
860 VAR_DOMAIN, (int *) NULL);
863 write_exp_elt_opcode (OP_VAR_VALUE);
864 write_exp_elt_block (NULL);
865 write_exp_elt_sym (sym);
866 write_exp_elt_opcode (OP_VAR_VALUE);
870 msymbol = lookup_minimal_symbol (name, NULL, NULL);
872 write_exp_msymbol (msymbol);
873 else if (!have_full_symbols () && !have_partial_symbols ())
874 error (_("No symbol table is loaded. Use the \"file\" command."));
876 error (_("No symbol \"%s\" in current context."), name);
880 variable: name_not_typename
881 { struct symbol *sym = $1.sym;
885 if (symbol_read_needs_frame (sym))
887 if (innermost_block == 0
888 || contained_in (block_found,
890 innermost_block = block_found;
893 write_exp_elt_opcode (OP_VAR_VALUE);
894 /* We want to use the selected frame, not
895 another more inner frame which happens to
896 be in the same block. */
897 write_exp_elt_block (NULL);
898 write_exp_elt_sym (sym);
899 write_exp_elt_opcode (OP_VAR_VALUE);
901 else if ($1.is_a_field_of_this)
903 /* C++: it hangs off of `this'. Must
904 not inadvertently convert from a method call
906 if (innermost_block == 0
907 || contained_in (block_found,
909 innermost_block = block_found;
910 write_exp_elt_opcode (OP_THIS);
911 write_exp_elt_opcode (OP_THIS);
912 write_exp_elt_opcode (STRUCTOP_PTR);
913 write_exp_string ($1.stoken);
914 write_exp_elt_opcode (STRUCTOP_PTR);
918 struct minimal_symbol *msymbol;
919 char *arg = copy_name ($1.stoken);
922 lookup_minimal_symbol (arg, NULL, NULL);
924 write_exp_msymbol (msymbol);
925 else if (!have_full_symbols () && !have_partial_symbols ())
926 error (_("No symbol table is loaded. Use the \"file\" command."));
928 error (_("No symbol \"%s\" in current context."),
929 copy_name ($1.stoken));
934 space_identifier : '@' NAME
935 { insert_type_address_space (copy_name ($2.stoken)); }
938 const_or_volatile: const_or_volatile_noopt
942 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
945 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
946 | const_or_volatile_noopt
949 const_or_volatile_or_space_identifier:
950 const_or_volatile_or_space_identifier_noopt
956 { insert_type (tp_pointer); }
957 const_or_volatile_or_space_identifier
959 { insert_type (tp_pointer); }
960 const_or_volatile_or_space_identifier
962 { insert_type (tp_reference); }
964 { insert_type (tp_reference); }
967 ptr_operator_ts: ptr_operator
969 $$ = get_type_stack ();
970 /* This cleanup is eventually run by
972 make_cleanup (type_stack_cleanup, $$);
976 abs_decl: ptr_operator_ts direct_abs_decl
977 { $$ = append_type_stack ($2, $1); }
982 direct_abs_decl: '(' abs_decl ')'
984 | direct_abs_decl array_mod
986 push_type_stack ($1);
988 push_type (tp_array);
989 $$ = get_type_stack ();
994 push_type (tp_array);
995 $$ = get_type_stack ();
998 | direct_abs_decl func_mod
1000 push_type_stack ($1);
1002 $$ = get_type_stack ();
1007 $$ = get_type_stack ();
1019 | '(' parameter_typelist ')'
1023 /* We used to try to recognize pointer to member types here, but
1024 that didn't work (shift/reduce conflicts meant that these rules never
1025 got executed). The problem is that
1026 int (foo::bar::baz::bizzle)
1027 is a function type but
1028 int (foo::bar::baz::bizzle::*)
1029 is a pointer to member type. Stroustrup loses again! */
1034 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1038 { $$ = lookup_signed_typename (parse_language,
1042 { $$ = lookup_signed_typename (parse_language,
1046 { $$ = lookup_signed_typename (parse_language,
1050 { $$ = lookup_signed_typename (parse_language,
1053 | LONG SIGNED_KEYWORD INT_KEYWORD
1054 { $$ = lookup_signed_typename (parse_language,
1057 | LONG SIGNED_KEYWORD
1058 { $$ = lookup_signed_typename (parse_language,
1061 | SIGNED_KEYWORD LONG INT_KEYWORD
1062 { $$ = lookup_signed_typename (parse_language,
1065 | UNSIGNED LONG INT_KEYWORD
1066 { $$ = lookup_unsigned_typename (parse_language,
1069 | LONG UNSIGNED INT_KEYWORD
1070 { $$ = lookup_unsigned_typename (parse_language,
1074 { $$ = lookup_unsigned_typename (parse_language,
1078 { $$ = lookup_signed_typename (parse_language,
1081 | LONG LONG INT_KEYWORD
1082 { $$ = lookup_signed_typename (parse_language,
1085 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1086 { $$ = lookup_signed_typename (parse_language,
1089 | LONG LONG SIGNED_KEYWORD
1090 { $$ = lookup_signed_typename (parse_language,
1093 | SIGNED_KEYWORD LONG LONG
1094 { $$ = lookup_signed_typename (parse_language,
1097 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1098 { $$ = lookup_signed_typename (parse_language,
1101 | UNSIGNED LONG LONG
1102 { $$ = lookup_unsigned_typename (parse_language,
1105 | UNSIGNED LONG LONG INT_KEYWORD
1106 { $$ = lookup_unsigned_typename (parse_language,
1109 | LONG LONG UNSIGNED
1110 { $$ = lookup_unsigned_typename (parse_language,
1113 | LONG LONG UNSIGNED INT_KEYWORD
1114 { $$ = lookup_unsigned_typename (parse_language,
1118 { $$ = lookup_signed_typename (parse_language,
1121 | SHORT SIGNED_KEYWORD INT_KEYWORD
1122 { $$ = lookup_signed_typename (parse_language,
1125 | SHORT SIGNED_KEYWORD
1126 { $$ = lookup_signed_typename (parse_language,
1129 | UNSIGNED SHORT INT_KEYWORD
1130 { $$ = lookup_unsigned_typename (parse_language,
1134 { $$ = lookup_unsigned_typename (parse_language,
1137 | SHORT UNSIGNED INT_KEYWORD
1138 { $$ = lookup_unsigned_typename (parse_language,
1142 { $$ = lookup_typename (parse_language, parse_gdbarch,
1143 "double", (struct block *) NULL,
1145 | LONG DOUBLE_KEYWORD
1146 { $$ = lookup_typename (parse_language, parse_gdbarch,
1148 (struct block *) NULL, 0); }
1150 { $$ = lookup_struct (copy_name ($2),
1151 expression_context_block); }
1153 { $$ = lookup_struct (copy_name ($2),
1154 expression_context_block); }
1156 { $$ = lookup_union (copy_name ($2),
1157 expression_context_block); }
1159 { $$ = lookup_enum (copy_name ($2),
1160 expression_context_block); }
1162 { $$ = lookup_unsigned_typename (parse_language,
1164 TYPE_NAME($2.type)); }
1166 { $$ = lookup_unsigned_typename (parse_language,
1169 | SIGNED_KEYWORD typename
1170 { $$ = lookup_signed_typename (parse_language,
1172 TYPE_NAME($2.type)); }
1174 { $$ = lookup_signed_typename (parse_language,
1177 /* It appears that this rule for templates is never
1178 reduced; template recognition happens by lookahead
1179 in the token processing code in yylex. */
1180 | TEMPLATE name '<' type '>'
1181 { $$ = lookup_template_type(copy_name($2), $4,
1182 expression_context_block);
1184 | const_or_volatile_or_space_identifier_noopt typebase
1185 { $$ = follow_types ($2); }
1186 | typebase const_or_volatile_or_space_identifier_noopt
1187 { $$ = follow_types ($1); }
1193 $$.stoken.ptr = "int";
1194 $$.stoken.length = 3;
1195 $$.type = lookup_signed_typename (parse_language,
1201 $$.stoken.ptr = "long";
1202 $$.stoken.length = 4;
1203 $$.type = lookup_signed_typename (parse_language,
1209 $$.stoken.ptr = "short";
1210 $$.stoken.length = 5;
1211 $$.type = lookup_signed_typename (parse_language,
1219 { check_parameter_typelist ($1); }
1220 | nonempty_typelist ',' DOTDOTDOT
1222 VEC_safe_push (type_ptr, $1, NULL);
1223 check_parameter_typelist ($1);
1231 VEC (type_ptr) *typelist = NULL;
1232 VEC_safe_push (type_ptr, typelist, $1);
1235 | nonempty_typelist ',' type
1237 VEC_safe_push (type_ptr, $1, $3);
1245 push_type_stack ($2);
1246 $$ = follow_types ($1);
1250 conversion_type_id: typebase conversion_declarator
1251 { $$ = follow_types ($1); }
1254 conversion_declarator: /* Nothing. */
1255 | ptr_operator conversion_declarator
1258 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1259 | VOLATILE_KEYWORD CONST_KEYWORD
1262 const_or_volatile_noopt: const_and_volatile
1263 { insert_type (tp_const);
1264 insert_type (tp_volatile);
1267 { insert_type (tp_const); }
1269 { insert_type (tp_volatile); }
1272 operator: OPERATOR NEW
1273 { $$ = operator_stoken (" new"); }
1275 { $$ = operator_stoken (" delete"); }
1276 | OPERATOR NEW '[' ']'
1277 { $$ = operator_stoken (" new[]"); }
1278 | OPERATOR DELETE '[' ']'
1279 { $$ = operator_stoken (" delete[]"); }
1281 { $$ = operator_stoken ("+"); }
1283 { $$ = operator_stoken ("-"); }
1285 { $$ = operator_stoken ("*"); }
1287 { $$ = operator_stoken ("/"); }
1289 { $$ = operator_stoken ("%"); }
1291 { $$ = operator_stoken ("^"); }
1293 { $$ = operator_stoken ("&"); }
1295 { $$ = operator_stoken ("|"); }
1297 { $$ = operator_stoken ("~"); }
1299 { $$ = operator_stoken ("!"); }
1301 { $$ = operator_stoken ("="); }
1303 { $$ = operator_stoken ("<"); }
1305 { $$ = operator_stoken (">"); }
1306 | OPERATOR ASSIGN_MODIFY
1307 { const char *op = "unknown";
1331 case BINOP_BITWISE_IOR:
1334 case BINOP_BITWISE_AND:
1337 case BINOP_BITWISE_XOR:
1344 $$ = operator_stoken (op);
1347 { $$ = operator_stoken ("<<"); }
1349 { $$ = operator_stoken (">>"); }
1351 { $$ = operator_stoken ("=="); }
1353 { $$ = operator_stoken ("!="); }
1355 { $$ = operator_stoken ("<="); }
1357 { $$ = operator_stoken (">="); }
1359 { $$ = operator_stoken ("&&"); }
1361 { $$ = operator_stoken ("||"); }
1362 | OPERATOR INCREMENT
1363 { $$ = operator_stoken ("++"); }
1364 | OPERATOR DECREMENT
1365 { $$ = operator_stoken ("--"); }
1367 { $$ = operator_stoken (","); }
1368 | OPERATOR ARROW_STAR
1369 { $$ = operator_stoken ("->*"); }
1371 { $$ = operator_stoken ("->"); }
1373 { $$ = operator_stoken ("()"); }
1375 { $$ = operator_stoken ("[]"); }
1376 | OPERATOR conversion_type_id
1379 struct ui_file *buf = mem_fileopen ();
1381 c_print_type ($2, NULL, buf, -1, 0);
1382 name = ui_file_xstrdup (buf, &length);
1383 ui_file_delete (buf);
1384 $$ = operator_stoken (name);
1391 name : NAME { $$ = $1.stoken; }
1392 | BLOCKNAME { $$ = $1.stoken; }
1393 | TYPENAME { $$ = $1.stoken; }
1394 | NAME_OR_INT { $$ = $1.stoken; }
1395 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1396 | operator { $$ = $1; }
1399 name_not_typename : NAME
1401 /* These would be useful if name_not_typename was useful, but it is just
1402 a fake for "variable", so these cause reduce/reduce conflicts because
1403 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1404 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1405 context where only a name could occur, this might be useful.
1411 $$.sym = lookup_symbol ($1.ptr,
1412 expression_context_block,
1414 &$$.is_a_field_of_this);
1421 /* Returns a stoken of the operator name given by OP (which does not
1422 include the string "operator"). */
1423 static struct stoken
1424 operator_stoken (const char *op)
1426 static const char *operator_string = "operator";
1427 struct stoken st = { NULL, 0 };
1428 st.length = strlen (operator_string) + strlen (op);
1429 st.ptr = malloc (st.length + 1);
1430 strcpy (st.ptr, operator_string);
1431 strcat (st.ptr, op);
1433 /* The toplevel (c_parse) will free the memory allocated here. */
1434 make_cleanup (free, st.ptr);
1438 /* Validate a parameter typelist. */
1441 check_parameter_typelist (VEC (type_ptr) *params)
1446 for (ix = 0; VEC_iterate (type_ptr, params, ix, type); ++ix)
1448 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1452 if (VEC_length (type_ptr, params) == 1)
1457 VEC_free (type_ptr, params);
1458 error (_("parameter types following 'void'"));
1462 VEC_free (type_ptr, params);
1463 error (_("'void' invalid as parameter type"));
1469 /* Take care of parsing a number (anything that starts with a digit).
1470 Set yylval and return the token type; update lexptr.
1471 LEN is the number of characters in it. */
1473 /*** Needs some error checking for the float case ***/
1476 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1478 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1479 here, and we do kind of silly things like cast to unsigned. */
1486 int base = input_radix;
1489 /* Number of "L" suffixes encountered. */
1492 /* We have found a "L" or "U" suffix. */
1493 int found_suffix = 0;
1496 struct type *signed_type;
1497 struct type *unsigned_type;
1501 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1502 point. Return DECFLOAT. */
1504 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1507 putithere->typed_val_decfloat.type
1508 = parse_type->builtin_decfloat;
1509 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1510 gdbarch_byte_order (parse_gdbarch), p);
1515 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1518 putithere->typed_val_decfloat.type
1519 = parse_type->builtin_decdouble;
1520 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1521 gdbarch_byte_order (parse_gdbarch), p);
1526 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1529 putithere->typed_val_decfloat.type
1530 = parse_type->builtin_declong;
1531 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1532 gdbarch_byte_order (parse_gdbarch), p);
1537 if (! parse_c_float (parse_gdbarch, p, len,
1538 &putithere->typed_val_float.dval,
1539 &putithere->typed_val_float.type))
1544 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1588 if (c >= 'A' && c <= 'Z')
1590 if (c != 'l' && c != 'u')
1592 if (c >= '0' && c <= '9')
1600 if (base > 10 && c >= 'a' && c <= 'f')
1604 n += i = c - 'a' + 10;
1617 return ERROR; /* Char not a digit */
1620 return ERROR; /* Invalid digit in this base */
1622 /* Portably test for overflow (only works for nonzero values, so make
1623 a second check for zero). FIXME: Can't we just make n and prevn
1624 unsigned and avoid this? */
1625 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1626 unsigned_p = 1; /* Try something unsigned */
1628 /* Portably test for unsigned overflow.
1629 FIXME: This check is wrong; for example it doesn't find overflow
1630 on 0x123456789 when LONGEST is 32 bits. */
1631 if (c != 'l' && c != 'u' && n != 0)
1633 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1634 error (_("Numeric constant too large."));
1639 /* An integer constant is an int, a long, or a long long. An L
1640 suffix forces it to be long; an LL suffix forces it to be long
1641 long. If not forced to a larger size, it gets the first type of
1642 the above that it fits in. To figure out whether it fits, we
1643 shift it right and see whether anything remains. Note that we
1644 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1645 operation, because many compilers will warn about such a shift
1646 (which always produces a zero result). Sometimes gdbarch_int_bit
1647 or gdbarch_long_bit will be that big, sometimes not. To deal with
1648 the case where it is we just always shift the value more than
1649 once, with fewer bits each time. */
1651 un = (ULONGEST)n >> 2;
1653 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1655 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1657 /* A large decimal (not hex or octal) constant (between INT_MAX
1658 and UINT_MAX) is a long or unsigned long, according to ANSI,
1659 never an unsigned int, but this code treats it as unsigned
1660 int. This probably should be fixed. GCC gives a warning on
1663 unsigned_type = parse_type->builtin_unsigned_int;
1664 signed_type = parse_type->builtin_int;
1666 else if (long_p <= 1
1667 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1669 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1670 unsigned_type = parse_type->builtin_unsigned_long;
1671 signed_type = parse_type->builtin_long;
1676 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1677 < gdbarch_long_long_bit (parse_gdbarch))
1678 /* A long long does not fit in a LONGEST. */
1679 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1681 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1682 high_bit = (ULONGEST) 1 << shift;
1683 unsigned_type = parse_type->builtin_unsigned_long_long;
1684 signed_type = parse_type->builtin_long_long;
1687 putithere->typed_val_int.val = n;
1689 /* If the high bit of the worked out type is set then this number
1690 has to be unsigned. */
1692 if (unsigned_p || (n & high_bit))
1694 putithere->typed_val_int.type = unsigned_type;
1698 putithere->typed_val_int.type = signed_type;
1704 /* Temporary obstack used for holding strings. */
1705 static struct obstack tempbuf;
1706 static int tempbuf_init;
1708 /* Parse a C escape sequence. The initial backslash of the sequence
1709 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1710 last character of the sequence. If OUTPUT is not NULL, the
1711 translated form of the escape sequence will be written there. If
1712 OUTPUT is NULL, no output is written and the call will only affect
1713 *PTR. If an escape sequence is expressed in target bytes, then the
1714 entire sequence will simply be copied to OUTPUT. Return 1 if any
1715 character was emitted, 0 otherwise. */
1718 c_parse_escape (char **ptr, struct obstack *output)
1720 char *tokptr = *ptr;
1723 /* Some escape sequences undergo character set conversion. Those we
1727 /* Hex escapes do not undergo character set conversion, so keep
1728 the escape sequence for later. */
1731 obstack_grow_str (output, "\\x");
1733 if (!isxdigit (*tokptr))
1734 error (_("\\x escape without a following hex digit"));
1735 while (isxdigit (*tokptr))
1738 obstack_1grow (output, *tokptr);
1743 /* Octal escapes do not undergo character set conversion, so
1744 keep the escape sequence for later. */
1756 obstack_grow_str (output, "\\");
1758 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1762 obstack_1grow (output, *tokptr);
1768 /* We handle UCNs later. We could handle them here, but that
1769 would mean a spurious error in the case where the UCN could
1770 be converted to the target charset but not the host
1776 int i, len = c == 'U' ? 8 : 4;
1779 obstack_1grow (output, '\\');
1780 obstack_1grow (output, *tokptr);
1783 if (!isxdigit (*tokptr))
1784 error (_("\\%c escape without a following hex digit"), c);
1785 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1788 obstack_1grow (output, *tokptr);
1794 /* We must pass backslash through so that it does not
1795 cause quoting during the second expansion. */
1798 obstack_grow_str (output, "\\\\");
1802 /* Escapes which undergo conversion. */
1805 obstack_1grow (output, '\a');
1810 obstack_1grow (output, '\b');
1815 obstack_1grow (output, '\f');
1820 obstack_1grow (output, '\n');
1825 obstack_1grow (output, '\r');
1830 obstack_1grow (output, '\t');
1835 obstack_1grow (output, '\v');
1839 /* GCC extension. */
1842 obstack_1grow (output, HOST_ESCAPE_CHAR);
1846 /* Backslash-newline expands to nothing at all. */
1852 /* A few escapes just expand to the character itself. */
1856 /* GCC extensions. */
1861 /* Unrecognized escapes turn into the character itself. */
1864 obstack_1grow (output, *tokptr);
1872 /* Parse a string or character literal from TOKPTR. The string or
1873 character may be wide or unicode. *OUTPTR is set to just after the
1874 end of the literal in the input string. The resulting token is
1875 stored in VALUE. This returns a token value, either STRING or
1876 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1877 number of host characters in the literal. */
1879 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1883 enum c_string_type type;
1885 /* Build the gdb internal form of the input string in tempbuf. Note
1886 that the buffer is null byte terminated *only* for the
1887 convenience of debugging gdb itself and printing the buffer
1888 contents when the buffer contains no embedded nulls. Gdb does
1889 not depend upon the buffer being null byte terminated, it uses
1890 the length string instead. This allows gdb to handle C strings
1891 (as well as strings in other languages) with embedded null
1897 obstack_free (&tempbuf, NULL);
1898 obstack_init (&tempbuf);
1900 /* Record the string type. */
1903 type = C_WIDE_STRING;
1906 else if (*tokptr == 'u')
1911 else if (*tokptr == 'U')
1919 /* Skip the quote. */
1933 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1935 else if (c == quote)
1939 obstack_1grow (&tempbuf, c);
1941 /* FIXME: this does the wrong thing with multi-byte host
1942 characters. We could use mbrlen here, but that would
1943 make "set host-charset" a bit less useful. */
1948 if (*tokptr != quote)
1951 error (_("Unterminated string in expression."));
1953 error (_("Unmatched single quote."));
1958 value->ptr = obstack_base (&tempbuf);
1959 value->length = obstack_object_size (&tempbuf);
1963 return quote == '"' ? STRING : CHAR;
1966 /* This is used to associate some attributes with a token. */
1970 /* If this bit is set, the token is C++-only. */
1974 /* If this bit is set, the token is conditional: if there is a
1975 symbol of the same name, then the token is a symbol; otherwise,
1976 the token is a keyword. */
1985 enum exp_opcode opcode;
1986 enum token_flags flags;
1989 static const struct token tokentab3[] =
1991 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1992 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
1993 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
1994 {"...", DOTDOTDOT, BINOP_END, 0}
1997 static const struct token tokentab2[] =
1999 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2000 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2001 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2002 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2003 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2004 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2005 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2006 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2007 {"++", INCREMENT, BINOP_END, 0},
2008 {"--", DECREMENT, BINOP_END, 0},
2009 {"->", ARROW, BINOP_END, 0},
2010 {"&&", ANDAND, BINOP_END, 0},
2011 {"||", OROR, BINOP_END, 0},
2012 /* "::" is *not* only C++: gdb overrides its meaning in several
2013 different ways, e.g., 'filename'::func, function::variable. */
2014 {"::", COLONCOLON, BINOP_END, 0},
2015 {"<<", LSH, BINOP_END, 0},
2016 {">>", RSH, BINOP_END, 0},
2017 {"==", EQUAL, BINOP_END, 0},
2018 {"!=", NOTEQUAL, BINOP_END, 0},
2019 {"<=", LEQ, BINOP_END, 0},
2020 {">=", GEQ, BINOP_END, 0},
2021 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2024 /* Identifier-like tokens. */
2025 static const struct token ident_tokens[] =
2027 {"unsigned", UNSIGNED, OP_NULL, 0},
2028 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2029 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2030 {"struct", STRUCT, OP_NULL, 0},
2031 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2032 {"sizeof", SIZEOF, OP_NULL, 0},
2033 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2034 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2035 {"class", CLASS, OP_NULL, FLAG_CXX},
2036 {"union", UNION, OP_NULL, 0},
2037 {"short", SHORT, OP_NULL, 0},
2038 {"const", CONST_KEYWORD, OP_NULL, 0},
2039 {"enum", ENUM, OP_NULL, 0},
2040 {"long", LONG, OP_NULL, 0},
2041 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2042 {"int", INT_KEYWORD, OP_NULL, 0},
2043 {"new", NEW, OP_NULL, FLAG_CXX},
2044 {"delete", DELETE, OP_NULL, FLAG_CXX},
2045 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2047 {"and", ANDAND, BINOP_END, FLAG_CXX},
2048 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2049 {"bitand", '&', OP_NULL, FLAG_CXX},
2050 {"bitor", '|', OP_NULL, FLAG_CXX},
2051 {"compl", '~', OP_NULL, FLAG_CXX},
2052 {"not", '!', OP_NULL, FLAG_CXX},
2053 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2054 {"or", OROR, BINOP_END, FLAG_CXX},
2055 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2056 {"xor", '^', OP_NULL, FLAG_CXX},
2057 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2059 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2060 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2061 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2062 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX }
2065 /* When we find that lexptr (the global var defined in parse.c) is
2066 pointing at a macro invocation, we expand the invocation, and call
2067 scan_macro_expansion to save the old lexptr here and point lexptr
2068 into the expanded text. When we reach the end of that, we call
2069 end_macro_expansion to pop back to the value we saved here. The
2070 macro expansion code promises to return only fully-expanded text,
2071 so we don't need to "push" more than one level.
2073 This is disgusting, of course. It would be cleaner to do all macro
2074 expansion beforehand, and then hand that to lexptr. But we don't
2075 really know where the expression ends. Remember, in a command like
2077 (gdb) break *ADDRESS if CONDITION
2079 we evaluate ADDRESS in the scope of the current frame, but we
2080 evaluate CONDITION in the scope of the breakpoint's location. So
2081 it's simply wrong to try to macro-expand the whole thing at once. */
2082 static char *macro_original_text;
2084 /* We save all intermediate macro expansions on this obstack for the
2085 duration of a single parse. The expansion text may sometimes have
2086 to live past the end of the expansion, due to yacc lookahead.
2087 Rather than try to be clever about saving the data for a single
2088 token, we simply keep it all and delete it after parsing has
2090 static struct obstack expansion_obstack;
2093 scan_macro_expansion (char *expansion)
2097 /* We'd better not be trying to push the stack twice. */
2098 gdb_assert (! macro_original_text);
2100 /* Copy to the obstack, and then free the intermediate
2102 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2105 /* Save the old lexptr value, so we can return to it when we're done
2106 parsing the expanded text. */
2107 macro_original_text = lexptr;
2113 scanning_macro_expansion (void)
2115 return macro_original_text != 0;
2120 finished_macro_expansion (void)
2122 /* There'd better be something to pop back to. */
2123 gdb_assert (macro_original_text);
2125 /* Pop back to the original text. */
2126 lexptr = macro_original_text;
2127 macro_original_text = 0;
2132 scan_macro_cleanup (void *dummy)
2134 if (macro_original_text)
2135 finished_macro_expansion ();
2137 obstack_free (&expansion_obstack, NULL);
2140 /* Return true iff the token represents a C++ cast operator. */
2143 is_cast_operator (const char *token, int len)
2145 return (! strncmp (token, "dynamic_cast", len)
2146 || ! strncmp (token, "static_cast", len)
2147 || ! strncmp (token, "reinterpret_cast", len)
2148 || ! strncmp (token, "const_cast", len));
2151 /* The scope used for macro expansion. */
2152 static struct macro_scope *expression_macro_scope;
2154 /* This is set if a NAME token appeared at the very end of the input
2155 string, with no whitespace separating the name from the EOF. This
2156 is used only when parsing to do field name completion. */
2157 static int saw_name_at_eof;
2159 /* This is set if the previously-returned token was a structure
2160 operator -- either '.' or ARROW. This is used only when parsing to
2161 do field name completion. */
2162 static int last_was_structop;
2164 /* Read one token, getting characters through lexptr. */
2167 lex_one_token (void)
2173 int saw_structop = last_was_structop;
2176 last_was_structop = 0;
2180 /* Check if this is a macro invocation that we need to expand. */
2181 if (! scanning_macro_expansion ())
2183 char *expanded = macro_expand_next (&lexptr,
2184 standard_macro_lookup,
2185 expression_macro_scope);
2188 scan_macro_expansion (expanded);
2191 prev_lexptr = lexptr;
2194 /* See if it is a special token of length 3. */
2195 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2196 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2198 if ((tokentab3[i].flags & FLAG_CXX) != 0
2199 && parse_language->la_language != language_cplus)
2203 yylval.opcode = tokentab3[i].opcode;
2204 return tokentab3[i].token;
2207 /* See if it is a special token of length 2. */
2208 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2209 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2211 if ((tokentab2[i].flags & FLAG_CXX) != 0
2212 && parse_language->la_language != language_cplus)
2216 yylval.opcode = tokentab2[i].opcode;
2217 if (in_parse_field && tokentab2[i].token == ARROW)
2218 last_was_structop = 1;
2219 return tokentab2[i].token;
2222 switch (c = *tokstart)
2225 /* If we were just scanning the result of a macro expansion,
2226 then we need to resume scanning the original text.
2227 If we're parsing for field name completion, and the previous
2228 token allows such completion, return a COMPLETE token.
2229 Otherwise, we were already scanning the original text, and
2230 we're really done. */
2231 if (scanning_macro_expansion ())
2233 finished_macro_expansion ();
2236 else if (saw_name_at_eof)
2238 saw_name_at_eof = 0;
2241 else if (saw_structop)
2260 if (paren_depth == 0)
2267 if (comma_terminates
2269 && ! scanning_macro_expansion ())
2275 /* Might be a floating point number. */
2276 if (lexptr[1] < '0' || lexptr[1] > '9')
2279 last_was_structop = 1;
2280 goto symbol; /* Nope, must be a symbol. */
2282 /* FALL THRU into number case. */
2295 /* It's a number. */
2296 int got_dot = 0, got_e = 0, toktype;
2298 int hex = input_radix > 10;
2300 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2305 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2313 /* This test includes !hex because 'e' is a valid hex digit
2314 and thus does not indicate a floating point number when
2315 the radix is hex. */
2316 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2317 got_dot = got_e = 1;
2318 /* This test does not include !hex, because a '.' always indicates
2319 a decimal floating point number regardless of the radix. */
2320 else if (!got_dot && *p == '.')
2322 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2323 && (*p == '-' || *p == '+'))
2324 /* This is the sign of the exponent, not the end of the
2327 /* We will take any letters or digits. parse_number will
2328 complain if past the radix, or if L or U are not final. */
2329 else if ((*p < '0' || *p > '9')
2330 && ((*p < 'a' || *p > 'z')
2331 && (*p < 'A' || *p > 'Z')))
2334 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2335 if (toktype == ERROR)
2337 char *err_copy = (char *) alloca (p - tokstart + 1);
2339 memcpy (err_copy, tokstart, p - tokstart);
2340 err_copy[p - tokstart] = 0;
2341 error (_("Invalid number \"%s\"."), err_copy);
2349 char *p = &tokstart[1];
2350 size_t len = strlen ("entry");
2352 while (isspace (*p))
2354 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2386 if (tokstart[1] != '"' && tokstart[1] != '\'')
2393 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2398 error (_("Empty character constant."));
2399 else if (host_len > 2 && c == '\'')
2402 namelen = lexptr - tokstart - 1;
2405 else if (host_len > 1)
2406 error (_("Invalid character constant."));
2412 if (!(c == '_' || c == '$'
2413 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2414 /* We must have come across a bad character (e.g. ';'). */
2415 error (_("Invalid character '%c' in expression."), c);
2417 /* It's a name. See how long it is. */
2419 for (c = tokstart[namelen];
2420 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2421 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2423 /* Template parameter lists are part of the name.
2424 FIXME: This mishandles `print $a<4&&$a>3'. */
2428 if (! is_cast_operator (tokstart, namelen))
2430 /* Scan ahead to get rest of the template specification. Note
2431 that we look ahead only when the '<' adjoins non-whitespace
2432 characters; for comparison expressions, e.g. "a < b > c",
2433 there must be spaces before the '<', etc. */
2435 char * p = find_template_name_end (tokstart + namelen);
2437 namelen = p - tokstart;
2441 c = tokstart[++namelen];
2444 /* The token "if" terminates the expression and is NOT removed from
2445 the input stream. It doesn't count if it appears in the
2446 expansion of a macro. */
2448 && tokstart[0] == 'i'
2449 && tokstart[1] == 'f'
2450 && ! scanning_macro_expansion ())
2455 /* For the same reason (breakpoint conditions), "thread N"
2456 terminates the expression. "thread" could be an identifier, but
2457 an identifier is never followed by a number without intervening
2458 punctuation. "task" is similar. Handle abbreviations of these,
2459 similarly to breakpoint.c:find_condition_and_thread. */
2461 && (strncmp (tokstart, "thread", namelen) == 0
2462 || strncmp (tokstart, "task", namelen) == 0)
2463 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2464 && ! scanning_macro_expansion ())
2466 char *p = tokstart + namelen + 1;
2467 while (*p == ' ' || *p == '\t')
2469 if (*p >= '0' && *p <= '9')
2477 yylval.sval.ptr = tokstart;
2478 yylval.sval.length = namelen;
2480 /* Catch specific keywords. */
2481 copy = copy_name (yylval.sval);
2482 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2483 if (strcmp (copy, ident_tokens[i].operator) == 0)
2485 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2486 && parse_language->la_language != language_cplus)
2489 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2491 int is_a_field_of_this = 0;
2493 if (lookup_symbol (copy, expression_context_block,
2495 (parse_language->la_language == language_cplus
2496 ? &is_a_field_of_this
2500 /* The keyword is shadowed. */
2505 /* It is ok to always set this, even though we don't always
2506 strictly need to. */
2507 yylval.opcode = ident_tokens[i].opcode;
2508 return ident_tokens[i].token;
2511 if (*tokstart == '$')
2514 if (in_parse_field && *lexptr == '\0')
2515 saw_name_at_eof = 1;
2519 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2526 DEF_VEC_O (token_and_value);
2528 /* A FIFO of tokens that have been read but not yet returned to the
2530 static VEC (token_and_value) *token_fifo;
2532 /* Non-zero if the lexer should return tokens from the FIFO. */
2535 /* Temporary storage for c_lex; this holds symbol names as they are
2537 static struct obstack name_obstack;
2539 /* Classify a NAME token. The contents of the token are in `yylval'.
2540 Updates yylval and returns the new token type. BLOCK is the block
2541 in which lookups start; this can be NULL to mean the global
2544 classify_name (struct block *block)
2548 int is_a_field_of_this = 0;
2550 copy = copy_name (yylval.sval);
2552 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2553 parse_language->la_language == language_cplus
2554 ? &is_a_field_of_this : (int *) NULL);
2556 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2558 yylval.ssym.sym = sym;
2559 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2564 /* See if it's a file name. */
2565 struct symtab *symtab;
2567 symtab = lookup_symtab (copy);
2570 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2575 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2577 yylval.tsym.type = SYMBOL_TYPE (sym);
2582 = language_lookup_primitive_type_by_name (parse_language,
2583 parse_gdbarch, copy);
2584 if (yylval.tsym.type != NULL)
2587 /* Input names that aren't symbols but ARE valid hex numbers, when
2588 the input radix permits them, can be names or numbers depending
2589 on the parse. Note we support radixes > 16 here. */
2591 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2592 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2594 YYSTYPE newlval; /* Its value is ignored. */
2595 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2598 yylval.ssym.sym = sym;
2599 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2604 /* Any other kind of symbol */
2605 yylval.ssym.sym = sym;
2606 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2609 && parse_language->la_language == language_cplus
2610 && !is_a_field_of_this
2611 && !lookup_minimal_symbol (copy, NULL, NULL))
2612 return UNKNOWN_CPP_NAME;
2617 /* Like classify_name, but used by the inner loop of the lexer, when a
2618 name might have already been seen. FIRST_NAME is true if the token
2619 in `yylval' is the first component of a name, false otherwise. */
2622 classify_inner_name (struct block *block, int first_name)
2624 struct type *type, *new_type;
2628 return classify_name (block);
2630 type = check_typedef (yylval.tsym.type);
2631 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2632 && TYPE_CODE (type) != TYPE_CODE_UNION
2633 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2636 copy = copy_name (yylval.tsym.stoken);
2637 yylval.ssym.sym = cp_lookup_nested_symbol (yylval.tsym.type, copy, block);
2638 if (yylval.ssym.sym == NULL)
2641 switch (SYMBOL_CLASS (yylval.ssym.sym))
2648 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym);;
2652 yylval.ssym.is_a_field_of_this = 0;
2655 internal_error (__FILE__, __LINE__, _("not reached"));
2658 /* The outer level of a two-level lexer. This calls the inner lexer
2659 to return tokens. It then either returns these tokens, or
2660 aggregates them into a larger token. This lets us work around a
2661 problem in our parsing approach, where the parser could not
2662 distinguish between qualified names and qualified types at the
2665 This approach is still not ideal, because it mishandles template
2666 types. See the comment in lex_one_token for an example. However,
2667 this is still an improvement over the earlier approach, and will
2668 suffice until we move to better parsing technology. */
2672 token_and_value current;
2673 int first_was_coloncolon, last_was_coloncolon, first_iter;
2675 if (popping && !VEC_empty (token_and_value, token_fifo))
2677 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2678 VEC_ordered_remove (token_and_value, token_fifo, 0);
2684 current.token = lex_one_token ();
2685 if (current.token == NAME)
2686 current.token = classify_name (expression_context_block);
2687 if (parse_language->la_language != language_cplus
2688 || (current.token != TYPENAME && current.token != COLONCOLON))
2689 return current.token;
2691 first_was_coloncolon = current.token == COLONCOLON;
2692 last_was_coloncolon = first_was_coloncolon;
2693 obstack_free (&name_obstack, obstack_base (&name_obstack));
2694 if (!last_was_coloncolon)
2695 obstack_grow (&name_obstack, yylval.sval.ptr, yylval.sval.length);
2696 current.value = yylval;
2700 token_and_value next;
2702 next.token = lex_one_token ();
2703 next.value = yylval;
2705 if (next.token == NAME && last_was_coloncolon)
2709 classification = classify_inner_name (first_was_coloncolon
2711 : expression_context_block,
2713 /* We keep going until we either run out of names, or until
2714 we have a qualified name which is not a type. */
2715 if (classification != TYPENAME && classification != NAME)
2717 /* Push the final component and leave the loop. */
2718 VEC_safe_push (token_and_value, token_fifo, &next);
2722 /* Update the partial name we are constructing. */
2725 /* We don't want to put a leading "::" into the name. */
2726 obstack_grow_str (&name_obstack, "::");
2728 obstack_grow (&name_obstack, next.value.sval.ptr,
2729 next.value.sval.length);
2731 yylval.sval.ptr = obstack_base (&name_obstack);
2732 yylval.sval.length = obstack_object_size (&name_obstack);
2733 current.value = yylval;
2734 current.token = classification;
2736 last_was_coloncolon = 0;
2738 else if (next.token == COLONCOLON && !last_was_coloncolon)
2739 last_was_coloncolon = 1;
2742 /* We've reached the end of the name. */
2743 VEC_safe_push (token_and_value, token_fifo, &next);
2752 /* If we ended with a "::", insert it too. */
2753 if (last_was_coloncolon)
2756 memset (&cc, 0, sizeof (token_and_value));
2757 if (first_was_coloncolon && first_iter)
2762 cc.token = COLONCOLON;
2763 VEC_safe_insert (token_and_value, token_fifo, 0, &cc);
2766 yylval = current.value;
2767 yylval.sval.ptr = obstack_copy0 (&expansion_obstack,
2769 yylval.sval.length);
2770 return current.token;
2777 struct cleanup *back_to = make_cleanup (free_current_contents,
2778 &expression_macro_scope);
2780 /* Set up the scope for macro expansion. */
2781 expression_macro_scope = NULL;
2783 if (expression_context_block)
2784 expression_macro_scope
2785 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2787 expression_macro_scope = default_macro_scope ();
2788 if (! expression_macro_scope)
2789 expression_macro_scope = user_macro_scope ();
2791 /* Initialize macro expansion code. */
2792 obstack_init (&expansion_obstack);
2793 gdb_assert (! macro_original_text);
2794 make_cleanup (scan_macro_cleanup, 0);
2796 make_cleanup_restore_integer (&yydebug);
2797 yydebug = parser_debug;
2799 /* Initialize some state used by the lexer. */
2800 last_was_structop = 0;
2801 saw_name_at_eof = 0;
2803 VEC_free (token_and_value, token_fifo);
2805 obstack_init (&name_obstack);
2806 make_cleanup_obstack_free (&name_obstack);
2808 result = yyparse ();
2809 do_cleanups (back_to);
2818 lexptr = prev_lexptr;
2820 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);