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 rcurly exp %prec UNARY
475 { write_exp_elt_opcode (UNOP_MEMVAL);
476 write_exp_elt_type ($2);
477 write_exp_elt_opcode (UNOP_MEMVAL); }
480 exp : '(' type ')' exp %prec UNARY
481 { write_exp_elt_opcode (UNOP_CAST);
482 write_exp_elt_type ($2);
483 write_exp_elt_opcode (UNOP_CAST); }
490 /* Binary operators in order of decreasing precedence. */
493 { write_exp_elt_opcode (BINOP_REPEAT); }
497 { write_exp_elt_opcode (BINOP_MUL); }
501 { write_exp_elt_opcode (BINOP_DIV); }
505 { write_exp_elt_opcode (BINOP_REM); }
509 { write_exp_elt_opcode (BINOP_ADD); }
513 { write_exp_elt_opcode (BINOP_SUB); }
517 { write_exp_elt_opcode (BINOP_LSH); }
521 { write_exp_elt_opcode (BINOP_RSH); }
525 { write_exp_elt_opcode (BINOP_EQUAL); }
528 exp : exp NOTEQUAL exp
529 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
533 { write_exp_elt_opcode (BINOP_LEQ); }
537 { write_exp_elt_opcode (BINOP_GEQ); }
541 { write_exp_elt_opcode (BINOP_LESS); }
545 { write_exp_elt_opcode (BINOP_GTR); }
549 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
553 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
557 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
561 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
565 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
568 exp : exp '?' exp ':' exp %prec '?'
569 { write_exp_elt_opcode (TERNOP_COND); }
573 { write_exp_elt_opcode (BINOP_ASSIGN); }
576 exp : exp ASSIGN_MODIFY exp
577 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
578 write_exp_elt_opcode ($2);
579 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
583 { write_exp_elt_opcode (OP_LONG);
584 write_exp_elt_type ($1.type);
585 write_exp_elt_longcst ((LONGEST)($1.val));
586 write_exp_elt_opcode (OP_LONG); }
591 struct stoken_vector vec;
594 write_exp_string_vector ($1.type, &vec);
600 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
601 write_exp_elt_opcode (OP_LONG);
602 write_exp_elt_type (val.typed_val_int.type);
603 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
604 write_exp_elt_opcode (OP_LONG);
610 { write_exp_elt_opcode (OP_DOUBLE);
611 write_exp_elt_type ($1.type);
612 write_exp_elt_dblcst ($1.dval);
613 write_exp_elt_opcode (OP_DOUBLE); }
617 { write_exp_elt_opcode (OP_DECFLOAT);
618 write_exp_elt_type ($1.type);
619 write_exp_elt_decfloatcst ($1.val);
620 write_exp_elt_opcode (OP_DECFLOAT); }
628 write_dollar_variable ($1);
632 exp : SIZEOF '(' type ')' %prec UNARY
633 { write_exp_elt_opcode (OP_LONG);
634 write_exp_elt_type (lookup_signed_typename
635 (parse_language, parse_gdbarch,
638 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
639 write_exp_elt_opcode (OP_LONG); }
642 exp : REINTERPRET_CAST '<' type '>' '(' exp ')' %prec UNARY
643 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST);
644 write_exp_elt_type ($3);
645 write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
648 exp : STATIC_CAST '<' type '>' '(' exp ')' %prec UNARY
649 { write_exp_elt_opcode (UNOP_CAST);
650 write_exp_elt_type ($3);
651 write_exp_elt_opcode (UNOP_CAST); }
654 exp : DYNAMIC_CAST '<' type '>' '(' exp ')' %prec UNARY
655 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST);
656 write_exp_elt_type ($3);
657 write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
660 exp : CONST_CAST '<' type '>' '(' exp ')' %prec UNARY
661 { /* We could do more error checking here, but
662 it doesn't seem worthwhile. */
663 write_exp_elt_opcode (UNOP_CAST);
664 write_exp_elt_type ($3);
665 write_exp_elt_opcode (UNOP_CAST); }
671 /* We copy the string here, and not in the
672 lexer, to guarantee that we do not leak a
673 string. Note that we follow the
674 NUL-termination convention of the
676 struct typed_stoken *vec = XNEW (struct typed_stoken);
681 vec->length = $1.length;
682 vec->ptr = malloc ($1.length + 1);
683 memcpy (vec->ptr, $1.ptr, $1.length + 1);
688 /* Note that we NUL-terminate here, but just
692 $$.tokens = realloc ($$.tokens,
693 $$.len * sizeof (struct typed_stoken));
695 p = malloc ($2.length + 1);
696 memcpy (p, $2.ptr, $2.length + 1);
698 $$.tokens[$$.len - 1].type = $2.type;
699 $$.tokens[$$.len - 1].length = $2.length;
700 $$.tokens[$$.len - 1].ptr = p;
707 enum c_string_type type = C_STRING;
709 for (i = 0; i < $1.len; ++i)
711 switch ($1.tokens[i].type)
719 && type != $1.tokens[i].type)
720 error (_("Undefined string concatenation."));
721 type = $1.tokens[i].type;
725 internal_error (__FILE__, __LINE__,
726 "unrecognized type in string concatenation");
730 write_exp_string_vector (type, &$1);
731 for (i = 0; i < $1.len; ++i)
732 free ($1.tokens[i].ptr);
739 { write_exp_elt_opcode (OP_LONG);
740 write_exp_elt_type (parse_type->builtin_bool);
741 write_exp_elt_longcst ((LONGEST) 1);
742 write_exp_elt_opcode (OP_LONG); }
746 { write_exp_elt_opcode (OP_LONG);
747 write_exp_elt_type (parse_type->builtin_bool);
748 write_exp_elt_longcst ((LONGEST) 0);
749 write_exp_elt_opcode (OP_LONG); }
757 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
759 error (_("No file or function \"%s\"."),
760 copy_name ($1.stoken));
768 block : block COLONCOLON name
770 = lookup_symbol (copy_name ($3), $1,
771 VAR_DOMAIN, (int *) NULL);
772 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
773 error (_("No function \"%s\" in specified context."),
775 $$ = SYMBOL_BLOCK_VALUE (tem); }
778 variable: name_not_typename ENTRY
779 { struct symbol *sym = $1.sym;
781 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
782 || !symbol_read_needs_frame (sym))
783 error (_("@entry can be used only for function "
784 "parameters, not for \"%s\""),
785 copy_name ($1.stoken));
787 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
788 write_exp_elt_sym (sym);
789 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
793 variable: block COLONCOLON name
794 { struct symbol *sym;
795 sym = lookup_symbol (copy_name ($3), $1,
796 VAR_DOMAIN, (int *) NULL);
798 error (_("No symbol \"%s\" in specified context."),
800 if (symbol_read_needs_frame (sym))
802 if (innermost_block == 0
803 || contained_in (block_found,
805 innermost_block = block_found;
808 write_exp_elt_opcode (OP_VAR_VALUE);
809 /* block_found is set by lookup_symbol. */
810 write_exp_elt_block (block_found);
811 write_exp_elt_sym (sym);
812 write_exp_elt_opcode (OP_VAR_VALUE); }
815 qualified_name: TYPENAME COLONCOLON name
817 struct type *type = $1.type;
818 CHECK_TYPEDEF (type);
819 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
820 && TYPE_CODE (type) != TYPE_CODE_UNION
821 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
822 error (_("`%s' is not defined as an aggregate type."),
825 write_exp_elt_opcode (OP_SCOPE);
826 write_exp_elt_type (type);
827 write_exp_string ($3);
828 write_exp_elt_opcode (OP_SCOPE);
830 | TYPENAME COLONCOLON '~' name
832 struct type *type = $1.type;
833 struct stoken tmp_token;
834 CHECK_TYPEDEF (type);
835 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
836 && TYPE_CODE (type) != TYPE_CODE_UNION
837 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
838 error (_("`%s' is not defined as an aggregate type."),
841 tmp_token.ptr = (char*) alloca ($4.length + 2);
842 tmp_token.length = $4.length + 1;
843 tmp_token.ptr[0] = '~';
844 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
845 tmp_token.ptr[tmp_token.length] = 0;
847 /* Check for valid destructor name. */
848 destructor_name_p (tmp_token.ptr, $1.type);
849 write_exp_elt_opcode (OP_SCOPE);
850 write_exp_elt_type (type);
851 write_exp_string (tmp_token);
852 write_exp_elt_opcode (OP_SCOPE);
854 | TYPENAME COLONCOLON name COLONCOLON name
856 char *copy = copy_name ($3);
857 error (_("No type \"%s\" within class "
858 "or namespace \"%s\"."),
859 copy, TYPE_NAME ($1.type));
863 variable: qualified_name
864 | COLONCOLON name_not_typename
866 char *name = copy_name ($2.stoken);
868 struct minimal_symbol *msymbol;
871 lookup_symbol (name, (const struct block *) NULL,
872 VAR_DOMAIN, (int *) NULL);
875 write_exp_elt_opcode (OP_VAR_VALUE);
876 write_exp_elt_block (NULL);
877 write_exp_elt_sym (sym);
878 write_exp_elt_opcode (OP_VAR_VALUE);
882 msymbol = lookup_minimal_symbol (name, NULL, NULL);
884 write_exp_msymbol (msymbol);
885 else if (!have_full_symbols () && !have_partial_symbols ())
886 error (_("No symbol table is loaded. Use the \"file\" command."));
888 error (_("No symbol \"%s\" in current context."), name);
892 variable: name_not_typename
893 { struct symbol *sym = $1.sym;
897 if (symbol_read_needs_frame (sym))
899 if (innermost_block == 0
900 || contained_in (block_found,
902 innermost_block = block_found;
905 write_exp_elt_opcode (OP_VAR_VALUE);
906 /* We want to use the selected frame, not
907 another more inner frame which happens to
908 be in the same block. */
909 write_exp_elt_block (NULL);
910 write_exp_elt_sym (sym);
911 write_exp_elt_opcode (OP_VAR_VALUE);
913 else if ($1.is_a_field_of_this)
915 /* C++: it hangs off of `this'. Must
916 not inadvertently convert from a method call
918 if (innermost_block == 0
919 || contained_in (block_found,
921 innermost_block = block_found;
922 write_exp_elt_opcode (OP_THIS);
923 write_exp_elt_opcode (OP_THIS);
924 write_exp_elt_opcode (STRUCTOP_PTR);
925 write_exp_string ($1.stoken);
926 write_exp_elt_opcode (STRUCTOP_PTR);
930 struct minimal_symbol *msymbol;
931 char *arg = copy_name ($1.stoken);
934 lookup_minimal_symbol (arg, NULL, NULL);
936 write_exp_msymbol (msymbol);
937 else if (!have_full_symbols () && !have_partial_symbols ())
938 error (_("No symbol table is loaded. Use the \"file\" command."));
940 error (_("No symbol \"%s\" in current context."),
941 copy_name ($1.stoken));
946 space_identifier : '@' NAME
947 { insert_type_address_space (copy_name ($2.stoken)); }
950 const_or_volatile: const_or_volatile_noopt
954 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
957 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
958 | const_or_volatile_noopt
961 const_or_volatile_or_space_identifier:
962 const_or_volatile_or_space_identifier_noopt
968 { insert_type (tp_pointer); }
969 const_or_volatile_or_space_identifier
971 { insert_type (tp_pointer); }
972 const_or_volatile_or_space_identifier
974 { insert_type (tp_reference); }
976 { insert_type (tp_reference); }
979 ptr_operator_ts: ptr_operator
981 $$ = get_type_stack ();
982 /* This cleanup is eventually run by
984 make_cleanup (type_stack_cleanup, $$);
988 abs_decl: ptr_operator_ts direct_abs_decl
989 { $$ = append_type_stack ($2, $1); }
994 direct_abs_decl: '(' abs_decl ')'
996 | direct_abs_decl array_mod
998 push_type_stack ($1);
1000 push_type (tp_array);
1001 $$ = get_type_stack ();
1006 push_type (tp_array);
1007 $$ = get_type_stack ();
1010 | direct_abs_decl func_mod
1012 push_type_stack ($1);
1014 $$ = get_type_stack ();
1019 $$ = get_type_stack ();
1031 | '(' parameter_typelist ')'
1035 /* We used to try to recognize pointer to member types here, but
1036 that didn't work (shift/reduce conflicts meant that these rules never
1037 got executed). The problem is that
1038 int (foo::bar::baz::bizzle)
1039 is a function type but
1040 int (foo::bar::baz::bizzle::*)
1041 is a pointer to member type. Stroustrup loses again! */
1046 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1050 { $$ = lookup_signed_typename (parse_language,
1054 { $$ = lookup_signed_typename (parse_language,
1058 { $$ = lookup_signed_typename (parse_language,
1062 { $$ = lookup_signed_typename (parse_language,
1065 | LONG SIGNED_KEYWORD INT_KEYWORD
1066 { $$ = lookup_signed_typename (parse_language,
1069 | LONG SIGNED_KEYWORD
1070 { $$ = lookup_signed_typename (parse_language,
1073 | SIGNED_KEYWORD LONG INT_KEYWORD
1074 { $$ = lookup_signed_typename (parse_language,
1077 | UNSIGNED LONG INT_KEYWORD
1078 { $$ = lookup_unsigned_typename (parse_language,
1081 | LONG UNSIGNED INT_KEYWORD
1082 { $$ = lookup_unsigned_typename (parse_language,
1086 { $$ = lookup_unsigned_typename (parse_language,
1090 { $$ = lookup_signed_typename (parse_language,
1093 | LONG LONG INT_KEYWORD
1094 { $$ = lookup_signed_typename (parse_language,
1097 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1098 { $$ = lookup_signed_typename (parse_language,
1101 | LONG LONG SIGNED_KEYWORD
1102 { $$ = lookup_signed_typename (parse_language,
1105 | SIGNED_KEYWORD LONG LONG
1106 { $$ = lookup_signed_typename (parse_language,
1109 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1110 { $$ = lookup_signed_typename (parse_language,
1113 | UNSIGNED LONG LONG
1114 { $$ = lookup_unsigned_typename (parse_language,
1117 | UNSIGNED LONG LONG INT_KEYWORD
1118 { $$ = lookup_unsigned_typename (parse_language,
1121 | LONG LONG UNSIGNED
1122 { $$ = lookup_unsigned_typename (parse_language,
1125 | LONG LONG UNSIGNED INT_KEYWORD
1126 { $$ = lookup_unsigned_typename (parse_language,
1130 { $$ = lookup_signed_typename (parse_language,
1133 | SHORT SIGNED_KEYWORD INT_KEYWORD
1134 { $$ = lookup_signed_typename (parse_language,
1137 | SHORT SIGNED_KEYWORD
1138 { $$ = lookup_signed_typename (parse_language,
1141 | UNSIGNED SHORT INT_KEYWORD
1142 { $$ = lookup_unsigned_typename (parse_language,
1146 { $$ = lookup_unsigned_typename (parse_language,
1149 | SHORT UNSIGNED INT_KEYWORD
1150 { $$ = lookup_unsigned_typename (parse_language,
1154 { $$ = lookup_typename (parse_language, parse_gdbarch,
1155 "double", (struct block *) NULL,
1157 | LONG DOUBLE_KEYWORD
1158 { $$ = lookup_typename (parse_language, parse_gdbarch,
1160 (struct block *) NULL, 0); }
1162 { $$ = lookup_struct (copy_name ($2),
1163 expression_context_block); }
1165 { $$ = lookup_struct (copy_name ($2),
1166 expression_context_block); }
1168 { $$ = lookup_union (copy_name ($2),
1169 expression_context_block); }
1171 { $$ = lookup_enum (copy_name ($2),
1172 expression_context_block); }
1174 { $$ = lookup_unsigned_typename (parse_language,
1176 TYPE_NAME($2.type)); }
1178 { $$ = lookup_unsigned_typename (parse_language,
1181 | SIGNED_KEYWORD typename
1182 { $$ = lookup_signed_typename (parse_language,
1184 TYPE_NAME($2.type)); }
1186 { $$ = lookup_signed_typename (parse_language,
1189 /* It appears that this rule for templates is never
1190 reduced; template recognition happens by lookahead
1191 in the token processing code in yylex. */
1192 | TEMPLATE name '<' type '>'
1193 { $$ = lookup_template_type(copy_name($2), $4,
1194 expression_context_block);
1196 | const_or_volatile_or_space_identifier_noopt typebase
1197 { $$ = follow_types ($2); }
1198 | typebase const_or_volatile_or_space_identifier_noopt
1199 { $$ = follow_types ($1); }
1205 $$.stoken.ptr = "int";
1206 $$.stoken.length = 3;
1207 $$.type = lookup_signed_typename (parse_language,
1213 $$.stoken.ptr = "long";
1214 $$.stoken.length = 4;
1215 $$.type = lookup_signed_typename (parse_language,
1221 $$.stoken.ptr = "short";
1222 $$.stoken.length = 5;
1223 $$.type = lookup_signed_typename (parse_language,
1231 { check_parameter_typelist ($1); }
1232 | nonempty_typelist ',' DOTDOTDOT
1234 VEC_safe_push (type_ptr, $1, NULL);
1235 check_parameter_typelist ($1);
1243 VEC (type_ptr) *typelist = NULL;
1244 VEC_safe_push (type_ptr, typelist, $1);
1247 | nonempty_typelist ',' type
1249 VEC_safe_push (type_ptr, $1, $3);
1257 push_type_stack ($2);
1258 $$ = follow_types ($1);
1262 conversion_type_id: typebase conversion_declarator
1263 { $$ = follow_types ($1); }
1266 conversion_declarator: /* Nothing. */
1267 | ptr_operator conversion_declarator
1270 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1271 | VOLATILE_KEYWORD CONST_KEYWORD
1274 const_or_volatile_noopt: const_and_volatile
1275 { insert_type (tp_const);
1276 insert_type (tp_volatile);
1279 { insert_type (tp_const); }
1281 { insert_type (tp_volatile); }
1284 operator: OPERATOR NEW
1285 { $$ = operator_stoken (" new"); }
1287 { $$ = operator_stoken (" delete"); }
1288 | OPERATOR NEW '[' ']'
1289 { $$ = operator_stoken (" new[]"); }
1290 | OPERATOR DELETE '[' ']'
1291 { $$ = operator_stoken (" delete[]"); }
1293 { $$ = operator_stoken ("+"); }
1295 { $$ = operator_stoken ("-"); }
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 (">"); }
1318 | OPERATOR ASSIGN_MODIFY
1319 { const char *op = "unknown";
1343 case BINOP_BITWISE_IOR:
1346 case BINOP_BITWISE_AND:
1349 case BINOP_BITWISE_XOR:
1356 $$ = operator_stoken (op);
1359 { $$ = operator_stoken ("<<"); }
1361 { $$ = operator_stoken (">>"); }
1363 { $$ = operator_stoken ("=="); }
1365 { $$ = operator_stoken ("!="); }
1367 { $$ = operator_stoken ("<="); }
1369 { $$ = operator_stoken (">="); }
1371 { $$ = operator_stoken ("&&"); }
1373 { $$ = operator_stoken ("||"); }
1374 | OPERATOR INCREMENT
1375 { $$ = operator_stoken ("++"); }
1376 | OPERATOR DECREMENT
1377 { $$ = operator_stoken ("--"); }
1379 { $$ = operator_stoken (","); }
1380 | OPERATOR ARROW_STAR
1381 { $$ = operator_stoken ("->*"); }
1383 { $$ = operator_stoken ("->"); }
1385 { $$ = operator_stoken ("()"); }
1387 { $$ = operator_stoken ("[]"); }
1388 | OPERATOR conversion_type_id
1391 struct ui_file *buf = mem_fileopen ();
1393 c_print_type ($2, NULL, buf, -1, 0);
1394 name = ui_file_xstrdup (buf, &length);
1395 ui_file_delete (buf);
1396 $$ = operator_stoken (name);
1403 name : NAME { $$ = $1.stoken; }
1404 | BLOCKNAME { $$ = $1.stoken; }
1405 | TYPENAME { $$ = $1.stoken; }
1406 | NAME_OR_INT { $$ = $1.stoken; }
1407 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1408 | operator { $$ = $1; }
1411 name_not_typename : NAME
1413 /* These would be useful if name_not_typename was useful, but it is just
1414 a fake for "variable", so these cause reduce/reduce conflicts because
1415 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1416 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1417 context where only a name could occur, this might be useful.
1423 $$.sym = lookup_symbol ($1.ptr,
1424 expression_context_block,
1426 &$$.is_a_field_of_this);
1433 /* Returns a stoken of the operator name given by OP (which does not
1434 include the string "operator"). */
1435 static struct stoken
1436 operator_stoken (const char *op)
1438 static const char *operator_string = "operator";
1439 struct stoken st = { NULL, 0 };
1440 st.length = strlen (operator_string) + strlen (op);
1441 st.ptr = malloc (st.length + 1);
1442 strcpy (st.ptr, operator_string);
1443 strcat (st.ptr, op);
1445 /* The toplevel (c_parse) will free the memory allocated here. */
1446 make_cleanup (free, st.ptr);
1450 /* Validate a parameter typelist. */
1453 check_parameter_typelist (VEC (type_ptr) *params)
1458 for (ix = 0; VEC_iterate (type_ptr, params, ix, type); ++ix)
1460 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1464 if (VEC_length (type_ptr, params) == 1)
1469 VEC_free (type_ptr, params);
1470 error (_("parameter types following 'void'"));
1474 VEC_free (type_ptr, params);
1475 error (_("'void' invalid as parameter type"));
1481 /* Take care of parsing a number (anything that starts with a digit).
1482 Set yylval and return the token type; update lexptr.
1483 LEN is the number of characters in it. */
1485 /*** Needs some error checking for the float case ***/
1488 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1490 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1491 here, and we do kind of silly things like cast to unsigned. */
1498 int base = input_radix;
1501 /* Number of "L" suffixes encountered. */
1504 /* We have found a "L" or "U" suffix. */
1505 int found_suffix = 0;
1508 struct type *signed_type;
1509 struct type *unsigned_type;
1513 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1514 point. Return DECFLOAT. */
1516 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1519 putithere->typed_val_decfloat.type
1520 = parse_type->builtin_decfloat;
1521 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1522 gdbarch_byte_order (parse_gdbarch), p);
1527 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1530 putithere->typed_val_decfloat.type
1531 = parse_type->builtin_decdouble;
1532 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1533 gdbarch_byte_order (parse_gdbarch), p);
1538 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1541 putithere->typed_val_decfloat.type
1542 = parse_type->builtin_declong;
1543 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1544 gdbarch_byte_order (parse_gdbarch), p);
1549 if (! parse_c_float (parse_gdbarch, p, len,
1550 &putithere->typed_val_float.dval,
1551 &putithere->typed_val_float.type))
1556 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1600 if (c >= 'A' && c <= 'Z')
1602 if (c != 'l' && c != 'u')
1604 if (c >= '0' && c <= '9')
1612 if (base > 10 && c >= 'a' && c <= 'f')
1616 n += i = c - 'a' + 10;
1629 return ERROR; /* Char not a digit */
1632 return ERROR; /* Invalid digit in this base */
1634 /* Portably test for overflow (only works for nonzero values, so make
1635 a second check for zero). FIXME: Can't we just make n and prevn
1636 unsigned and avoid this? */
1637 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1638 unsigned_p = 1; /* Try something unsigned */
1640 /* Portably test for unsigned overflow.
1641 FIXME: This check is wrong; for example it doesn't find overflow
1642 on 0x123456789 when LONGEST is 32 bits. */
1643 if (c != 'l' && c != 'u' && n != 0)
1645 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1646 error (_("Numeric constant too large."));
1651 /* An integer constant is an int, a long, or a long long. An L
1652 suffix forces it to be long; an LL suffix forces it to be long
1653 long. If not forced to a larger size, it gets the first type of
1654 the above that it fits in. To figure out whether it fits, we
1655 shift it right and see whether anything remains. Note that we
1656 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1657 operation, because many compilers will warn about such a shift
1658 (which always produces a zero result). Sometimes gdbarch_int_bit
1659 or gdbarch_long_bit will be that big, sometimes not. To deal with
1660 the case where it is we just always shift the value more than
1661 once, with fewer bits each time. */
1663 un = (ULONGEST)n >> 2;
1665 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1667 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1669 /* A large decimal (not hex or octal) constant (between INT_MAX
1670 and UINT_MAX) is a long or unsigned long, according to ANSI,
1671 never an unsigned int, but this code treats it as unsigned
1672 int. This probably should be fixed. GCC gives a warning on
1675 unsigned_type = parse_type->builtin_unsigned_int;
1676 signed_type = parse_type->builtin_int;
1678 else if (long_p <= 1
1679 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1681 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1682 unsigned_type = parse_type->builtin_unsigned_long;
1683 signed_type = parse_type->builtin_long;
1688 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1689 < gdbarch_long_long_bit (parse_gdbarch))
1690 /* A long long does not fit in a LONGEST. */
1691 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1693 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1694 high_bit = (ULONGEST) 1 << shift;
1695 unsigned_type = parse_type->builtin_unsigned_long_long;
1696 signed_type = parse_type->builtin_long_long;
1699 putithere->typed_val_int.val = n;
1701 /* If the high bit of the worked out type is set then this number
1702 has to be unsigned. */
1704 if (unsigned_p || (n & high_bit))
1706 putithere->typed_val_int.type = unsigned_type;
1710 putithere->typed_val_int.type = signed_type;
1716 /* Temporary obstack used for holding strings. */
1717 static struct obstack tempbuf;
1718 static int tempbuf_init;
1720 /* Parse a C escape sequence. The initial backslash of the sequence
1721 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1722 last character of the sequence. If OUTPUT is not NULL, the
1723 translated form of the escape sequence will be written there. If
1724 OUTPUT is NULL, no output is written and the call will only affect
1725 *PTR. If an escape sequence is expressed in target bytes, then the
1726 entire sequence will simply be copied to OUTPUT. Return 1 if any
1727 character was emitted, 0 otherwise. */
1730 c_parse_escape (char **ptr, struct obstack *output)
1732 char *tokptr = *ptr;
1735 /* Some escape sequences undergo character set conversion. Those we
1739 /* Hex escapes do not undergo character set conversion, so keep
1740 the escape sequence for later. */
1743 obstack_grow_str (output, "\\x");
1745 if (!isxdigit (*tokptr))
1746 error (_("\\x escape without a following hex digit"));
1747 while (isxdigit (*tokptr))
1750 obstack_1grow (output, *tokptr);
1755 /* Octal escapes do not undergo character set conversion, so
1756 keep the escape sequence for later. */
1768 obstack_grow_str (output, "\\");
1770 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1774 obstack_1grow (output, *tokptr);
1780 /* We handle UCNs later. We could handle them here, but that
1781 would mean a spurious error in the case where the UCN could
1782 be converted to the target charset but not the host
1788 int i, len = c == 'U' ? 8 : 4;
1791 obstack_1grow (output, '\\');
1792 obstack_1grow (output, *tokptr);
1795 if (!isxdigit (*tokptr))
1796 error (_("\\%c escape without a following hex digit"), c);
1797 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1800 obstack_1grow (output, *tokptr);
1806 /* We must pass backslash through so that it does not
1807 cause quoting during the second expansion. */
1810 obstack_grow_str (output, "\\\\");
1814 /* Escapes which undergo conversion. */
1817 obstack_1grow (output, '\a');
1822 obstack_1grow (output, '\b');
1827 obstack_1grow (output, '\f');
1832 obstack_1grow (output, '\n');
1837 obstack_1grow (output, '\r');
1842 obstack_1grow (output, '\t');
1847 obstack_1grow (output, '\v');
1851 /* GCC extension. */
1854 obstack_1grow (output, HOST_ESCAPE_CHAR);
1858 /* Backslash-newline expands to nothing at all. */
1864 /* A few escapes just expand to the character itself. */
1868 /* GCC extensions. */
1873 /* Unrecognized escapes turn into the character itself. */
1876 obstack_1grow (output, *tokptr);
1884 /* Parse a string or character literal from TOKPTR. The string or
1885 character may be wide or unicode. *OUTPTR is set to just after the
1886 end of the literal in the input string. The resulting token is
1887 stored in VALUE. This returns a token value, either STRING or
1888 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1889 number of host characters in the literal. */
1891 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1895 enum c_string_type type;
1897 /* Build the gdb internal form of the input string in tempbuf. Note
1898 that the buffer is null byte terminated *only* for the
1899 convenience of debugging gdb itself and printing the buffer
1900 contents when the buffer contains no embedded nulls. Gdb does
1901 not depend upon the buffer being null byte terminated, it uses
1902 the length string instead. This allows gdb to handle C strings
1903 (as well as strings in other languages) with embedded null
1909 obstack_free (&tempbuf, NULL);
1910 obstack_init (&tempbuf);
1912 /* Record the string type. */
1915 type = C_WIDE_STRING;
1918 else if (*tokptr == 'u')
1923 else if (*tokptr == 'U')
1931 /* Skip the quote. */
1945 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1947 else if (c == quote)
1951 obstack_1grow (&tempbuf, c);
1953 /* FIXME: this does the wrong thing with multi-byte host
1954 characters. We could use mbrlen here, but that would
1955 make "set host-charset" a bit less useful. */
1960 if (*tokptr != quote)
1963 error (_("Unterminated string in expression."));
1965 error (_("Unmatched single quote."));
1970 value->ptr = obstack_base (&tempbuf);
1971 value->length = obstack_object_size (&tempbuf);
1975 return quote == '"' ? STRING : CHAR;
1982 enum exp_opcode opcode;
1986 static const struct token tokentab3[] =
1988 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1989 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
1990 {"->*", ARROW_STAR, BINOP_END, 1},
1991 {"...", DOTDOTDOT, BINOP_END, 0}
1994 static const struct token tokentab2[] =
1996 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1997 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1998 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1999 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2000 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2001 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2002 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2003 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2004 {"++", INCREMENT, BINOP_END, 0},
2005 {"--", DECREMENT, BINOP_END, 0},
2006 {"->", ARROW, BINOP_END, 0},
2007 {"&&", ANDAND, BINOP_END, 0},
2008 {"||", OROR, BINOP_END, 0},
2009 /* "::" is *not* only C++: gdb overrides its meaning in several
2010 different ways, e.g., 'filename'::func, function::variable. */
2011 {"::", COLONCOLON, BINOP_END, 0},
2012 {"<<", LSH, BINOP_END, 0},
2013 {">>", RSH, BINOP_END, 0},
2014 {"==", EQUAL, BINOP_END, 0},
2015 {"!=", NOTEQUAL, BINOP_END, 0},
2016 {"<=", LEQ, BINOP_END, 0},
2017 {">=", GEQ, BINOP_END, 0},
2018 {".*", DOT_STAR, BINOP_END, 1}
2021 /* Identifier-like tokens. */
2022 static const struct token ident_tokens[] =
2024 {"unsigned", UNSIGNED, OP_NULL, 0},
2025 {"template", TEMPLATE, OP_NULL, 1},
2026 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2027 {"struct", STRUCT, OP_NULL, 0},
2028 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2029 {"sizeof", SIZEOF, OP_NULL, 0},
2030 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2031 {"false", FALSEKEYWORD, OP_NULL, 1},
2032 {"class", CLASS, OP_NULL, 1},
2033 {"union", UNION, OP_NULL, 0},
2034 {"short", SHORT, OP_NULL, 0},
2035 {"const", CONST_KEYWORD, OP_NULL, 0},
2036 {"enum", ENUM, OP_NULL, 0},
2037 {"long", LONG, OP_NULL, 0},
2038 {"true", TRUEKEYWORD, OP_NULL, 1},
2039 {"int", INT_KEYWORD, OP_NULL, 0},
2040 {"new", NEW, OP_NULL, 1},
2041 {"delete", DELETE, OP_NULL, 1},
2042 {"operator", OPERATOR, OP_NULL, 1},
2044 {"and", ANDAND, BINOP_END, 1},
2045 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
2046 {"bitand", '&', OP_NULL, 1},
2047 {"bitor", '|', OP_NULL, 1},
2048 {"compl", '~', OP_NULL, 1},
2049 {"not", '!', OP_NULL, 1},
2050 {"not_eq", NOTEQUAL, BINOP_END, 1},
2051 {"or", OROR, BINOP_END, 1},
2052 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
2053 {"xor", '^', OP_NULL, 1},
2054 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1},
2056 {"const_cast", CONST_CAST, OP_NULL, 1 },
2057 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, 1 },
2058 {"static_cast", STATIC_CAST, OP_NULL, 1 },
2059 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, 1 }
2062 /* When we find that lexptr (the global var defined in parse.c) is
2063 pointing at a macro invocation, we expand the invocation, and call
2064 scan_macro_expansion to save the old lexptr here and point lexptr
2065 into the expanded text. When we reach the end of that, we call
2066 end_macro_expansion to pop back to the value we saved here. The
2067 macro expansion code promises to return only fully-expanded text,
2068 so we don't need to "push" more than one level.
2070 This is disgusting, of course. It would be cleaner to do all macro
2071 expansion beforehand, and then hand that to lexptr. But we don't
2072 really know where the expression ends. Remember, in a command like
2074 (gdb) break *ADDRESS if CONDITION
2076 we evaluate ADDRESS in the scope of the current frame, but we
2077 evaluate CONDITION in the scope of the breakpoint's location. So
2078 it's simply wrong to try to macro-expand the whole thing at once. */
2079 static char *macro_original_text;
2081 /* We save all intermediate macro expansions on this obstack for the
2082 duration of a single parse. The expansion text may sometimes have
2083 to live past the end of the expansion, due to yacc lookahead.
2084 Rather than try to be clever about saving the data for a single
2085 token, we simply keep it all and delete it after parsing has
2087 static struct obstack expansion_obstack;
2090 scan_macro_expansion (char *expansion)
2094 /* We'd better not be trying to push the stack twice. */
2095 gdb_assert (! macro_original_text);
2097 /* Copy to the obstack, and then free the intermediate
2099 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2102 /* Save the old lexptr value, so we can return to it when we're done
2103 parsing the expanded text. */
2104 macro_original_text = lexptr;
2110 scanning_macro_expansion (void)
2112 return macro_original_text != 0;
2117 finished_macro_expansion (void)
2119 /* There'd better be something to pop back to. */
2120 gdb_assert (macro_original_text);
2122 /* Pop back to the original text. */
2123 lexptr = macro_original_text;
2124 macro_original_text = 0;
2129 scan_macro_cleanup (void *dummy)
2131 if (macro_original_text)
2132 finished_macro_expansion ();
2134 obstack_free (&expansion_obstack, NULL);
2137 /* Return true iff the token represents a C++ cast operator. */
2140 is_cast_operator (const char *token, int len)
2142 return (! strncmp (token, "dynamic_cast", len)
2143 || ! strncmp (token, "static_cast", len)
2144 || ! strncmp (token, "reinterpret_cast", len)
2145 || ! strncmp (token, "const_cast", len));
2148 /* The scope used for macro expansion. */
2149 static struct macro_scope *expression_macro_scope;
2151 /* This is set if a NAME token appeared at the very end of the input
2152 string, with no whitespace separating the name from the EOF. This
2153 is used only when parsing to do field name completion. */
2154 static int saw_name_at_eof;
2156 /* This is set if the previously-returned token was a structure
2157 operator -- either '.' or ARROW. This is used only when parsing to
2158 do field name completion. */
2159 static int last_was_structop;
2161 /* Read one token, getting characters through lexptr. */
2164 lex_one_token (void)
2170 int saw_structop = last_was_structop;
2173 last_was_structop = 0;
2177 /* Check if this is a macro invocation that we need to expand. */
2178 if (! scanning_macro_expansion ())
2180 char *expanded = macro_expand_next (&lexptr,
2181 standard_macro_lookup,
2182 expression_macro_scope);
2185 scan_macro_expansion (expanded);
2188 prev_lexptr = lexptr;
2191 /* See if it is a special token of length 3. */
2192 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2193 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2195 if (tokentab3[i].cxx_only
2196 && parse_language->la_language != language_cplus)
2200 yylval.opcode = tokentab3[i].opcode;
2201 return tokentab3[i].token;
2204 /* See if it is a special token of length 2. */
2205 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2206 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2208 if (tokentab2[i].cxx_only
2209 && parse_language->la_language != language_cplus)
2213 yylval.opcode = tokentab2[i].opcode;
2214 if (in_parse_field && tokentab2[i].token == ARROW)
2215 last_was_structop = 1;
2216 return tokentab2[i].token;
2219 switch (c = *tokstart)
2222 /* If we were just scanning the result of a macro expansion,
2223 then we need to resume scanning the original text.
2224 If we're parsing for field name completion, and the previous
2225 token allows such completion, return a COMPLETE token.
2226 Otherwise, we were already scanning the original text, and
2227 we're really done. */
2228 if (scanning_macro_expansion ())
2230 finished_macro_expansion ();
2233 else if (saw_name_at_eof)
2235 saw_name_at_eof = 0;
2238 else if (saw_structop)
2257 if (paren_depth == 0)
2264 if (comma_terminates
2266 && ! scanning_macro_expansion ())
2272 /* Might be a floating point number. */
2273 if (lexptr[1] < '0' || lexptr[1] > '9')
2276 last_was_structop = 1;
2277 goto symbol; /* Nope, must be a symbol. */
2279 /* FALL THRU into number case. */
2292 /* It's a number. */
2293 int got_dot = 0, got_e = 0, toktype;
2295 int hex = input_radix > 10;
2297 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2302 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2310 /* This test includes !hex because 'e' is a valid hex digit
2311 and thus does not indicate a floating point number when
2312 the radix is hex. */
2313 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2314 got_dot = got_e = 1;
2315 /* This test does not include !hex, because a '.' always indicates
2316 a decimal floating point number regardless of the radix. */
2317 else if (!got_dot && *p == '.')
2319 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2320 && (*p == '-' || *p == '+'))
2321 /* This is the sign of the exponent, not the end of the
2324 /* We will take any letters or digits. parse_number will
2325 complain if past the radix, or if L or U are not final. */
2326 else if ((*p < '0' || *p > '9')
2327 && ((*p < 'a' || *p > 'z')
2328 && (*p < 'A' || *p > 'Z')))
2331 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2332 if (toktype == ERROR)
2334 char *err_copy = (char *) alloca (p - tokstart + 1);
2336 memcpy (err_copy, tokstart, p - tokstart);
2337 err_copy[p - tokstart] = 0;
2338 error (_("Invalid number \"%s\"."), err_copy);
2346 char *p = &tokstart[1];
2347 size_t len = strlen ("entry");
2349 while (isspace (*p))
2351 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2383 if (tokstart[1] != '"' && tokstart[1] != '\'')
2390 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2395 error (_("Empty character constant."));
2396 else if (host_len > 2 && c == '\'')
2399 namelen = lexptr - tokstart - 1;
2402 else if (host_len > 1)
2403 error (_("Invalid character constant."));
2409 if (!(c == '_' || c == '$'
2410 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2411 /* We must have come across a bad character (e.g. ';'). */
2412 error (_("Invalid character '%c' in expression."), c);
2414 /* It's a name. See how long it is. */
2416 for (c = tokstart[namelen];
2417 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2418 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2420 /* Template parameter lists are part of the name.
2421 FIXME: This mishandles `print $a<4&&$a>3'. */
2425 if (! is_cast_operator (tokstart, namelen))
2427 /* Scan ahead to get rest of the template specification. Note
2428 that we look ahead only when the '<' adjoins non-whitespace
2429 characters; for comparison expressions, e.g. "a < b > c",
2430 there must be spaces before the '<', etc. */
2432 char * p = find_template_name_end (tokstart + namelen);
2434 namelen = p - tokstart;
2438 c = tokstart[++namelen];
2441 /* The token "if" terminates the expression and is NOT removed from
2442 the input stream. It doesn't count if it appears in the
2443 expansion of a macro. */
2445 && tokstart[0] == 'i'
2446 && tokstart[1] == 'f'
2447 && ! scanning_macro_expansion ())
2452 /* For the same reason (breakpoint conditions), "thread N"
2453 terminates the expression. "thread" could be an identifier, but
2454 an identifier is never followed by a number without intervening
2455 punctuation. "task" is similar. Handle abbreviations of these,
2456 similarly to breakpoint.c:find_condition_and_thread. */
2458 && (strncmp (tokstart, "thread", namelen) == 0
2459 || strncmp (tokstart, "task", namelen) == 0)
2460 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2461 && ! scanning_macro_expansion ())
2463 char *p = tokstart + namelen + 1;
2464 while (*p == ' ' || *p == '\t')
2466 if (*p >= '0' && *p <= '9')
2474 yylval.sval.ptr = tokstart;
2475 yylval.sval.length = namelen;
2477 /* Catch specific keywords. */
2478 copy = copy_name (yylval.sval);
2479 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2480 if (strcmp (copy, ident_tokens[i].operator) == 0)
2482 if (ident_tokens[i].cxx_only
2483 && parse_language->la_language != language_cplus)
2486 /* It is ok to always set this, even though we don't always
2487 strictly need to. */
2488 yylval.opcode = ident_tokens[i].opcode;
2489 return ident_tokens[i].token;
2492 if (*tokstart == '$')
2495 if (in_parse_field && *lexptr == '\0')
2496 saw_name_at_eof = 1;
2500 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2507 DEF_VEC_O (token_and_value);
2509 /* A FIFO of tokens that have been read but not yet returned to the
2511 static VEC (token_and_value) *token_fifo;
2513 /* Non-zero if the lexer should return tokens from the FIFO. */
2516 /* Temporary storage for c_lex; this holds symbol names as they are
2518 static struct obstack name_obstack;
2520 /* Classify a NAME token. The contents of the token are in `yylval'.
2521 Updates yylval and returns the new token type. BLOCK is the block
2522 in which lookups start; this can be NULL to mean the global
2525 classify_name (struct block *block)
2529 int is_a_field_of_this = 0;
2531 copy = copy_name (yylval.sval);
2533 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2534 parse_language->la_language == language_cplus
2535 ? &is_a_field_of_this : (int *) NULL);
2537 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2539 yylval.ssym.sym = sym;
2540 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2545 /* See if it's a file name. */
2546 struct symtab *symtab;
2548 symtab = lookup_symtab (copy);
2551 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2556 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2558 yylval.tsym.type = SYMBOL_TYPE (sym);
2563 = language_lookup_primitive_type_by_name (parse_language,
2564 parse_gdbarch, copy);
2565 if (yylval.tsym.type != NULL)
2568 /* Input names that aren't symbols but ARE valid hex numbers, when
2569 the input radix permits them, can be names or numbers depending
2570 on the parse. Note we support radixes > 16 here. */
2572 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2573 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2575 YYSTYPE newlval; /* Its value is ignored. */
2576 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2579 yylval.ssym.sym = sym;
2580 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2585 /* Any other kind of symbol */
2586 yylval.ssym.sym = sym;
2587 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2590 && parse_language->la_language == language_cplus
2591 && !is_a_field_of_this
2592 && !lookup_minimal_symbol (copy, NULL, NULL))
2593 return UNKNOWN_CPP_NAME;
2598 /* Like classify_name, but used by the inner loop of the lexer, when a
2599 name might have already been seen. FIRST_NAME is true if the token
2600 in `yylval' is the first component of a name, false otherwise. */
2603 classify_inner_name (struct block *block, int first_name)
2605 struct type *type, *new_type;
2609 return classify_name (block);
2611 type = check_typedef (yylval.tsym.type);
2612 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2613 && TYPE_CODE (type) != TYPE_CODE_UNION
2614 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2617 copy = copy_name (yylval.tsym.stoken);
2618 yylval.ssym.sym = cp_lookup_nested_symbol (yylval.tsym.type, copy, block);
2619 if (yylval.ssym.sym == NULL)
2622 switch (SYMBOL_CLASS (yylval.ssym.sym))
2629 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym);;
2633 yylval.ssym.is_a_field_of_this = 0;
2636 internal_error (__FILE__, __LINE__, _("not reached"));
2639 /* The outer level of a two-level lexer. This calls the inner lexer
2640 to return tokens. It then either returns these tokens, or
2641 aggregates them into a larger token. This lets us work around a
2642 problem in our parsing approach, where the parser could not
2643 distinguish between qualified names and qualified types at the
2646 This approach is still not ideal, because it mishandles template
2647 types. See the comment in lex_one_token for an example. However,
2648 this is still an improvement over the earlier approach, and will
2649 suffice until we move to better parsing technology. */
2653 token_and_value current;
2654 int first_was_coloncolon, last_was_coloncolon, first_iter;
2656 if (popping && !VEC_empty (token_and_value, token_fifo))
2658 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2659 VEC_ordered_remove (token_and_value, token_fifo, 0);
2665 current.token = lex_one_token ();
2666 if (current.token == NAME)
2667 current.token = classify_name (expression_context_block);
2668 if (parse_language->la_language != language_cplus
2669 || (current.token != TYPENAME && current.token != COLONCOLON))
2670 return current.token;
2672 first_was_coloncolon = current.token == COLONCOLON;
2673 last_was_coloncolon = first_was_coloncolon;
2674 obstack_free (&name_obstack, obstack_base (&name_obstack));
2675 if (!last_was_coloncolon)
2676 obstack_grow (&name_obstack, yylval.sval.ptr, yylval.sval.length);
2677 current.value = yylval;
2681 token_and_value next;
2683 next.token = lex_one_token ();
2684 next.value = yylval;
2686 if (next.token == NAME && last_was_coloncolon)
2690 classification = classify_inner_name (first_was_coloncolon
2692 : expression_context_block,
2694 /* We keep going until we either run out of names, or until
2695 we have a qualified name which is not a type. */
2696 if (classification != TYPENAME && classification != NAME)
2698 /* Push the final component and leave the loop. */
2699 VEC_safe_push (token_and_value, token_fifo, &next);
2703 /* Update the partial name we are constructing. */
2706 /* We don't want to put a leading "::" into the name. */
2707 obstack_grow_str (&name_obstack, "::");
2709 obstack_grow (&name_obstack, next.value.sval.ptr,
2710 next.value.sval.length);
2712 yylval.sval.ptr = obstack_base (&name_obstack);
2713 yylval.sval.length = obstack_object_size (&name_obstack);
2714 current.value = yylval;
2715 current.token = classification;
2717 last_was_coloncolon = 0;
2719 else if (next.token == COLONCOLON && !last_was_coloncolon)
2720 last_was_coloncolon = 1;
2723 /* We've reached the end of the name. */
2724 VEC_safe_push (token_and_value, token_fifo, &next);
2733 /* If we ended with a "::", insert it too. */
2734 if (last_was_coloncolon)
2737 memset (&cc, 0, sizeof (token_and_value));
2738 if (first_was_coloncolon && first_iter)
2743 cc.token = COLONCOLON;
2744 VEC_safe_insert (token_and_value, token_fifo, 0, &cc);
2747 yylval = current.value;
2748 yylval.sval.ptr = obstack_copy0 (&expansion_obstack,
2750 yylval.sval.length);
2751 return current.token;
2758 struct cleanup *back_to = make_cleanup (free_current_contents,
2759 &expression_macro_scope);
2761 /* Set up the scope for macro expansion. */
2762 expression_macro_scope = NULL;
2764 if (expression_context_block)
2765 expression_macro_scope
2766 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2768 expression_macro_scope = default_macro_scope ();
2769 if (! expression_macro_scope)
2770 expression_macro_scope = user_macro_scope ();
2772 /* Initialize macro expansion code. */
2773 obstack_init (&expansion_obstack);
2774 gdb_assert (! macro_original_text);
2775 make_cleanup (scan_macro_cleanup, 0);
2777 make_cleanup_restore_integer (&yydebug);
2778 yydebug = parser_debug;
2780 /* Initialize some state used by the lexer. */
2781 last_was_structop = 0;
2782 saw_name_at_eof = 0;
2784 VEC_free (token_and_value, token_fifo);
2786 obstack_init (&name_obstack);
2787 make_cleanup_obstack_free (&name_obstack);
2789 result = yyparse ();
2790 do_cleanups (back_to);
2799 lexptr = prev_lexptr;
2801 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);