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 *);
170 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
172 %type <tval> type typebase
173 %type <tvec> nonempty_typelist func_mod parameter_typelist
174 /* %type <bval> block */
176 /* Fancy type parsing. */
178 %type <lval> array_mod
179 %type <tval> conversion_type_id
181 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
183 %token <typed_val_int> INT
184 %token <typed_val_float> FLOAT
185 %token <typed_val_decfloat> DECFLOAT
187 /* Both NAME and TYPENAME tokens represent symbols in the input,
188 and both convey their data as strings.
189 But a TYPENAME is a string that happens to be defined as a typedef
190 or builtin type name (such as int or char)
191 and a NAME is any other symbol.
192 Contexts where this distinction is not important can use the
193 nonterminal "name", which matches either NAME or TYPENAME. */
195 %token <tsval> STRING
197 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
198 %token <ssym> UNKNOWN_CPP_NAME
199 %token <voidval> COMPLETE
200 %token <tsym> TYPENAME
202 %type <svec> string_exp
203 %type <ssym> name_not_typename
204 %type <tsym> typename
206 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
207 but which would parse as a valid number in the current input radix.
208 E.g. "c" when input_radix==16. Depending on the parse, it will be
209 turned into a name or into a number. */
211 %token <ssym> NAME_OR_INT
214 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
218 %type <sval> operator
219 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
222 /* Special type cases, put in to allow the parser to distinguish different
224 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
226 %token <sval> VARIABLE
228 %token <opcode> ASSIGN_MODIFY
237 %right '=' ASSIGN_MODIFY
245 %left '<' '>' LEQ GEQ
250 %right UNARY INCREMENT DECREMENT
251 %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
252 %token <ssym> BLOCKNAME
253 %token <bval> FILENAME
267 { write_exp_elt_opcode(OP_TYPE);
268 write_exp_elt_type($1);
269 write_exp_elt_opcode(OP_TYPE);}
272 /* Expressions, including the comma operator. */
275 { write_exp_elt_opcode (BINOP_COMMA); }
278 /* Expressions, not including the comma operator. */
279 exp : '*' exp %prec UNARY
280 { write_exp_elt_opcode (UNOP_IND); }
283 exp : '&' exp %prec UNARY
284 { write_exp_elt_opcode (UNOP_ADDR); }
287 exp : '-' exp %prec UNARY
288 { write_exp_elt_opcode (UNOP_NEG); }
291 exp : '+' exp %prec UNARY
292 { write_exp_elt_opcode (UNOP_PLUS); }
295 exp : '!' exp %prec UNARY
296 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
299 exp : '~' exp %prec UNARY
300 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
303 exp : INCREMENT exp %prec UNARY
304 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
307 exp : DECREMENT exp %prec UNARY
308 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
311 exp : exp INCREMENT %prec UNARY
312 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
315 exp : exp DECREMENT %prec UNARY
316 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
319 exp : SIZEOF exp %prec UNARY
320 { write_exp_elt_opcode (UNOP_SIZEOF); }
324 { write_exp_elt_opcode (STRUCTOP_PTR);
325 write_exp_string ($3);
326 write_exp_elt_opcode (STRUCTOP_PTR); }
329 exp : exp ARROW name COMPLETE
330 { mark_struct_expression ();
331 write_exp_elt_opcode (STRUCTOP_PTR);
332 write_exp_string ($3);
333 write_exp_elt_opcode (STRUCTOP_PTR); }
336 exp : exp ARROW COMPLETE
338 mark_struct_expression ();
339 write_exp_elt_opcode (STRUCTOP_PTR);
342 write_exp_string (s);
343 write_exp_elt_opcode (STRUCTOP_PTR); }
346 exp : exp ARROW qualified_name
347 { /* exp->type::name becomes exp->*(&type::name) */
348 /* Note: this doesn't work if name is a
349 static member! FIXME */
350 write_exp_elt_opcode (UNOP_ADDR);
351 write_exp_elt_opcode (STRUCTOP_MPTR); }
354 exp : exp ARROW_STAR exp
355 { write_exp_elt_opcode (STRUCTOP_MPTR); }
359 { write_exp_elt_opcode (STRUCTOP_STRUCT);
360 write_exp_string ($3);
361 write_exp_elt_opcode (STRUCTOP_STRUCT); }
364 exp : exp '.' name COMPLETE
365 { mark_struct_expression ();
366 write_exp_elt_opcode (STRUCTOP_STRUCT);
367 write_exp_string ($3);
368 write_exp_elt_opcode (STRUCTOP_STRUCT); }
371 exp : exp '.' COMPLETE
373 mark_struct_expression ();
374 write_exp_elt_opcode (STRUCTOP_STRUCT);
377 write_exp_string (s);
378 write_exp_elt_opcode (STRUCTOP_STRUCT); }
381 exp : exp '.' qualified_name
382 { /* exp.type::name becomes exp.*(&type::name) */
383 /* Note: this doesn't work if name is a
384 static member! FIXME */
385 write_exp_elt_opcode (UNOP_ADDR);
386 write_exp_elt_opcode (STRUCTOP_MEMBER); }
389 exp : exp DOT_STAR exp
390 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
393 exp : exp '[' exp1 ']'
394 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
398 /* This is to save the value of arglist_len
399 being accumulated by an outer function call. */
400 { start_arglist (); }
401 arglist ')' %prec ARROW
402 { write_exp_elt_opcode (OP_FUNCALL);
403 write_exp_elt_longcst ((LONGEST) end_arglist ());
404 write_exp_elt_opcode (OP_FUNCALL); }
407 exp : UNKNOWN_CPP_NAME '('
409 /* This could potentially be a an argument defined
410 lookup function (Koenig). */
411 write_exp_elt_opcode (OP_ADL_FUNC);
412 write_exp_elt_block (expression_context_block);
413 write_exp_elt_sym (NULL); /* Placeholder. */
414 write_exp_string ($1.stoken);
415 write_exp_elt_opcode (OP_ADL_FUNC);
417 /* This is to save the value of arglist_len
418 being accumulated by an outer function call. */
422 arglist ')' %prec ARROW
424 write_exp_elt_opcode (OP_FUNCALL);
425 write_exp_elt_longcst ((LONGEST) end_arglist ());
426 write_exp_elt_opcode (OP_FUNCALL);
431 { start_arglist (); }
441 arglist : arglist ',' exp %prec ABOVE_COMMA
445 exp : exp '(' parameter_typelist ')' const_or_volatile
447 VEC (type_ptr) *type_list = $3;
448 struct type *type_elt;
449 LONGEST len = VEC_length (type_ptr, type_list);
451 write_exp_elt_opcode (TYPE_INSTANCE);
452 write_exp_elt_longcst (len);
454 VEC_iterate (type_ptr, type_list, i, type_elt);
456 write_exp_elt_type (type_elt);
457 write_exp_elt_longcst(len);
458 write_exp_elt_opcode (TYPE_INSTANCE);
459 VEC_free (type_ptr, type_list);
464 { $$ = end_arglist () - 1; }
466 exp : lcurly arglist rcurly %prec ARROW
467 { write_exp_elt_opcode (OP_ARRAY);
468 write_exp_elt_longcst ((LONGEST) 0);
469 write_exp_elt_longcst ((LONGEST) $3);
470 write_exp_elt_opcode (OP_ARRAY); }
473 exp : lcurly type rcurly exp %prec UNARY
474 { write_exp_elt_opcode (UNOP_MEMVAL);
475 write_exp_elt_type ($2);
476 write_exp_elt_opcode (UNOP_MEMVAL); }
479 exp : '(' type ')' exp %prec UNARY
480 { write_exp_elt_opcode (UNOP_CAST);
481 write_exp_elt_type ($2);
482 write_exp_elt_opcode (UNOP_CAST); }
489 /* Binary operators in order of decreasing precedence. */
492 { write_exp_elt_opcode (BINOP_REPEAT); }
496 { write_exp_elt_opcode (BINOP_MUL); }
500 { write_exp_elt_opcode (BINOP_DIV); }
504 { write_exp_elt_opcode (BINOP_REM); }
508 { write_exp_elt_opcode (BINOP_ADD); }
512 { write_exp_elt_opcode (BINOP_SUB); }
516 { write_exp_elt_opcode (BINOP_LSH); }
520 { write_exp_elt_opcode (BINOP_RSH); }
524 { write_exp_elt_opcode (BINOP_EQUAL); }
527 exp : exp NOTEQUAL exp
528 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
532 { write_exp_elt_opcode (BINOP_LEQ); }
536 { write_exp_elt_opcode (BINOP_GEQ); }
540 { write_exp_elt_opcode (BINOP_LESS); }
544 { write_exp_elt_opcode (BINOP_GTR); }
548 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
552 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
556 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
560 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
564 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
567 exp : exp '?' exp ':' exp %prec '?'
568 { write_exp_elt_opcode (TERNOP_COND); }
572 { write_exp_elt_opcode (BINOP_ASSIGN); }
575 exp : exp ASSIGN_MODIFY exp
576 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
577 write_exp_elt_opcode ($2);
578 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
582 { write_exp_elt_opcode (OP_LONG);
583 write_exp_elt_type ($1.type);
584 write_exp_elt_longcst ((LONGEST)($1.val));
585 write_exp_elt_opcode (OP_LONG); }
590 struct stoken_vector vec;
593 write_exp_string_vector ($1.type, &vec);
599 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
600 write_exp_elt_opcode (OP_LONG);
601 write_exp_elt_type (val.typed_val_int.type);
602 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
603 write_exp_elt_opcode (OP_LONG);
609 { write_exp_elt_opcode (OP_DOUBLE);
610 write_exp_elt_type ($1.type);
611 write_exp_elt_dblcst ($1.dval);
612 write_exp_elt_opcode (OP_DOUBLE); }
616 { write_exp_elt_opcode (OP_DECFLOAT);
617 write_exp_elt_type ($1.type);
618 write_exp_elt_decfloatcst ($1.val);
619 write_exp_elt_opcode (OP_DECFLOAT); }
627 write_dollar_variable ($1);
631 exp : SIZEOF '(' type ')' %prec UNARY
632 { write_exp_elt_opcode (OP_LONG);
633 write_exp_elt_type (lookup_signed_typename
634 (parse_language, parse_gdbarch,
637 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
638 write_exp_elt_opcode (OP_LONG); }
641 exp : REINTERPRET_CAST '<' type '>' '(' exp ')' %prec UNARY
642 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST);
643 write_exp_elt_type ($3);
644 write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
647 exp : STATIC_CAST '<' type '>' '(' exp ')' %prec UNARY
648 { write_exp_elt_opcode (UNOP_CAST);
649 write_exp_elt_type ($3);
650 write_exp_elt_opcode (UNOP_CAST); }
653 exp : DYNAMIC_CAST '<' type '>' '(' exp ')' %prec UNARY
654 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST);
655 write_exp_elt_type ($3);
656 write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
659 exp : CONST_CAST '<' type '>' '(' exp ')' %prec UNARY
660 { /* We could do more error checking here, but
661 it doesn't seem worthwhile. */
662 write_exp_elt_opcode (UNOP_CAST);
663 write_exp_elt_type ($3);
664 write_exp_elt_opcode (UNOP_CAST); }
670 /* We copy the string here, and not in the
671 lexer, to guarantee that we do not leak a
672 string. Note that we follow the
673 NUL-termination convention of the
675 struct typed_stoken *vec = XNEW (struct typed_stoken);
680 vec->length = $1.length;
681 vec->ptr = malloc ($1.length + 1);
682 memcpy (vec->ptr, $1.ptr, $1.length + 1);
687 /* Note that we NUL-terminate here, but just
691 $$.tokens = realloc ($$.tokens,
692 $$.len * sizeof (struct typed_stoken));
694 p = malloc ($2.length + 1);
695 memcpy (p, $2.ptr, $2.length + 1);
697 $$.tokens[$$.len - 1].type = $2.type;
698 $$.tokens[$$.len - 1].length = $2.length;
699 $$.tokens[$$.len - 1].ptr = p;
706 enum c_string_type type = C_STRING;
708 for (i = 0; i < $1.len; ++i)
710 switch ($1.tokens[i].type)
718 && type != $1.tokens[i].type)
719 error (_("Undefined string concatenation."));
720 type = $1.tokens[i].type;
724 internal_error (__FILE__, __LINE__,
725 "unrecognized type in string concatenation");
729 write_exp_string_vector (type, &$1);
730 for (i = 0; i < $1.len; ++i)
731 free ($1.tokens[i].ptr);
738 { write_exp_elt_opcode (OP_LONG);
739 write_exp_elt_type (parse_type->builtin_bool);
740 write_exp_elt_longcst ((LONGEST) 1);
741 write_exp_elt_opcode (OP_LONG); }
745 { write_exp_elt_opcode (OP_LONG);
746 write_exp_elt_type (parse_type->builtin_bool);
747 write_exp_elt_longcst ((LONGEST) 0);
748 write_exp_elt_opcode (OP_LONG); }
756 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
758 error (_("No file or function \"%s\"."),
759 copy_name ($1.stoken));
767 block : block COLONCOLON name
769 = lookup_symbol (copy_name ($3), $1,
770 VAR_DOMAIN, (int *) NULL);
771 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
772 error (_("No function \"%s\" in specified context."),
774 $$ = SYMBOL_BLOCK_VALUE (tem); }
777 variable: name_not_typename ENTRY
778 { struct symbol *sym = $1.sym;
780 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
781 || !symbol_read_needs_frame (sym))
782 error (_("@entry can be used only for function "
783 "parameters, not for \"%s\""),
784 copy_name ($1.stoken));
786 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
787 write_exp_elt_sym (sym);
788 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
792 variable: block COLONCOLON name
793 { struct symbol *sym;
794 sym = lookup_symbol (copy_name ($3), $1,
795 VAR_DOMAIN, (int *) NULL);
797 error (_("No symbol \"%s\" in specified context."),
799 if (symbol_read_needs_frame (sym))
801 if (innermost_block == 0
802 || contained_in (block_found,
804 innermost_block = block_found;
807 write_exp_elt_opcode (OP_VAR_VALUE);
808 /* block_found is set by lookup_symbol. */
809 write_exp_elt_block (block_found);
810 write_exp_elt_sym (sym);
811 write_exp_elt_opcode (OP_VAR_VALUE); }
814 qualified_name: TYPENAME COLONCOLON name
816 struct type *type = $1.type;
817 CHECK_TYPEDEF (type);
818 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
819 && TYPE_CODE (type) != TYPE_CODE_UNION
820 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
821 error (_("`%s' is not defined as an aggregate type."),
824 write_exp_elt_opcode (OP_SCOPE);
825 write_exp_elt_type (type);
826 write_exp_string ($3);
827 write_exp_elt_opcode (OP_SCOPE);
829 | TYPENAME COLONCOLON '~' name
831 struct type *type = $1.type;
832 struct stoken tmp_token;
833 CHECK_TYPEDEF (type);
834 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
835 && TYPE_CODE (type) != TYPE_CODE_UNION
836 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
837 error (_("`%s' is not defined as an aggregate type."),
840 tmp_token.ptr = (char*) alloca ($4.length + 2);
841 tmp_token.length = $4.length + 1;
842 tmp_token.ptr[0] = '~';
843 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
844 tmp_token.ptr[tmp_token.length] = 0;
846 /* Check for valid destructor name. */
847 destructor_name_p (tmp_token.ptr, $1.type);
848 write_exp_elt_opcode (OP_SCOPE);
849 write_exp_elt_type (type);
850 write_exp_string (tmp_token);
851 write_exp_elt_opcode (OP_SCOPE);
853 | TYPENAME COLONCOLON name COLONCOLON name
855 char *copy = copy_name ($3);
856 error (_("No type \"%s\" within class "
857 "or namespace \"%s\"."),
858 copy, TYPE_NAME ($1.type));
862 variable: qualified_name
863 | COLONCOLON name_not_typename
865 char *name = copy_name ($2.stoken);
867 struct minimal_symbol *msymbol;
870 lookup_symbol (name, (const struct block *) NULL,
871 VAR_DOMAIN, (int *) NULL);
874 write_exp_elt_opcode (OP_VAR_VALUE);
875 write_exp_elt_block (NULL);
876 write_exp_elt_sym (sym);
877 write_exp_elt_opcode (OP_VAR_VALUE);
881 msymbol = lookup_minimal_symbol (name, NULL, NULL);
883 write_exp_msymbol (msymbol);
884 else if (!have_full_symbols () && !have_partial_symbols ())
885 error (_("No symbol table is loaded. Use the \"file\" command."));
887 error (_("No symbol \"%s\" in current context."), name);
891 variable: name_not_typename
892 { struct symbol *sym = $1.sym;
896 if (symbol_read_needs_frame (sym))
898 if (innermost_block == 0
899 || contained_in (block_found,
901 innermost_block = block_found;
904 write_exp_elt_opcode (OP_VAR_VALUE);
905 /* We want to use the selected frame, not
906 another more inner frame which happens to
907 be in the same block. */
908 write_exp_elt_block (NULL);
909 write_exp_elt_sym (sym);
910 write_exp_elt_opcode (OP_VAR_VALUE);
912 else if ($1.is_a_field_of_this)
914 /* C++: it hangs off of `this'. Must
915 not inadvertently convert from a method call
917 if (innermost_block == 0
918 || contained_in (block_found,
920 innermost_block = block_found;
921 write_exp_elt_opcode (OP_THIS);
922 write_exp_elt_opcode (OP_THIS);
923 write_exp_elt_opcode (STRUCTOP_PTR);
924 write_exp_string ($1.stoken);
925 write_exp_elt_opcode (STRUCTOP_PTR);
929 struct minimal_symbol *msymbol;
930 char *arg = copy_name ($1.stoken);
933 lookup_minimal_symbol (arg, NULL, NULL);
935 write_exp_msymbol (msymbol);
936 else if (!have_full_symbols () && !have_partial_symbols ())
937 error (_("No symbol table is loaded. Use the \"file\" command."));
939 error (_("No symbol \"%s\" in current context."),
940 copy_name ($1.stoken));
945 space_identifier : '@' NAME
946 { insert_type_address_space (copy_name ($2.stoken)); }
949 const_or_volatile: const_or_volatile_noopt
953 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
956 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
957 | const_or_volatile_noopt
960 const_or_volatile_or_space_identifier:
961 const_or_volatile_or_space_identifier_noopt
967 { insert_type (tp_pointer); }
968 const_or_volatile_or_space_identifier
970 { insert_type (tp_pointer); }
971 const_or_volatile_or_space_identifier
973 { insert_type (tp_reference); }
975 { insert_type (tp_reference); }
978 ptr_operator_ts: ptr_operator
980 $$ = get_type_stack ();
981 /* This cleanup is eventually run by
983 make_cleanup (type_stack_cleanup, $$);
987 abs_decl: ptr_operator_ts direct_abs_decl
988 { $$ = append_type_stack ($2, $1); }
993 direct_abs_decl: '(' abs_decl ')'
995 | direct_abs_decl array_mod
997 push_type_stack ($1);
999 push_type (tp_array);
1000 $$ = get_type_stack ();
1005 push_type (tp_array);
1006 $$ = get_type_stack ();
1009 | direct_abs_decl func_mod
1011 push_type_stack ($1);
1013 $$ = get_type_stack ();
1018 $$ = get_type_stack ();
1030 | '(' parameter_typelist ')'
1034 /* We used to try to recognize pointer to member types here, but
1035 that didn't work (shift/reduce conflicts meant that these rules never
1036 got executed). The problem is that
1037 int (foo::bar::baz::bizzle)
1038 is a function type but
1039 int (foo::bar::baz::bizzle::*)
1040 is a pointer to member type. Stroustrup loses again! */
1045 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1049 { $$ = lookup_signed_typename (parse_language,
1053 { $$ = lookup_signed_typename (parse_language,
1057 { $$ = lookup_signed_typename (parse_language,
1061 { $$ = lookup_signed_typename (parse_language,
1064 | LONG SIGNED_KEYWORD INT_KEYWORD
1065 { $$ = lookup_signed_typename (parse_language,
1068 | LONG SIGNED_KEYWORD
1069 { $$ = lookup_signed_typename (parse_language,
1072 | SIGNED_KEYWORD LONG INT_KEYWORD
1073 { $$ = lookup_signed_typename (parse_language,
1076 | UNSIGNED LONG INT_KEYWORD
1077 { $$ = lookup_unsigned_typename (parse_language,
1080 | LONG UNSIGNED INT_KEYWORD
1081 { $$ = lookup_unsigned_typename (parse_language,
1085 { $$ = lookup_unsigned_typename (parse_language,
1089 { $$ = lookup_signed_typename (parse_language,
1092 | LONG LONG INT_KEYWORD
1093 { $$ = lookup_signed_typename (parse_language,
1096 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1097 { $$ = lookup_signed_typename (parse_language,
1100 | LONG LONG SIGNED_KEYWORD
1101 { $$ = lookup_signed_typename (parse_language,
1104 | SIGNED_KEYWORD LONG LONG
1105 { $$ = lookup_signed_typename (parse_language,
1108 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1109 { $$ = lookup_signed_typename (parse_language,
1112 | UNSIGNED LONG LONG
1113 { $$ = lookup_unsigned_typename (parse_language,
1116 | UNSIGNED LONG LONG INT_KEYWORD
1117 { $$ = lookup_unsigned_typename (parse_language,
1120 | LONG LONG UNSIGNED
1121 { $$ = lookup_unsigned_typename (parse_language,
1124 | LONG LONG UNSIGNED INT_KEYWORD
1125 { $$ = lookup_unsigned_typename (parse_language,
1129 { $$ = lookup_signed_typename (parse_language,
1132 | SHORT SIGNED_KEYWORD INT_KEYWORD
1133 { $$ = lookup_signed_typename (parse_language,
1136 | SHORT SIGNED_KEYWORD
1137 { $$ = lookup_signed_typename (parse_language,
1140 | UNSIGNED SHORT INT_KEYWORD
1141 { $$ = lookup_unsigned_typename (parse_language,
1145 { $$ = lookup_unsigned_typename (parse_language,
1148 | SHORT UNSIGNED INT_KEYWORD
1149 { $$ = lookup_unsigned_typename (parse_language,
1153 { $$ = lookup_typename (parse_language, parse_gdbarch,
1154 "double", (struct block *) NULL,
1156 | LONG DOUBLE_KEYWORD
1157 { $$ = lookup_typename (parse_language, parse_gdbarch,
1159 (struct block *) NULL, 0); }
1161 { $$ = lookup_struct (copy_name ($2),
1162 expression_context_block); }
1164 { $$ = lookup_struct (copy_name ($2),
1165 expression_context_block); }
1167 { $$ = lookup_union (copy_name ($2),
1168 expression_context_block); }
1170 { $$ = lookup_enum (copy_name ($2),
1171 expression_context_block); }
1173 { $$ = lookup_unsigned_typename (parse_language,
1175 TYPE_NAME($2.type)); }
1177 { $$ = lookup_unsigned_typename (parse_language,
1180 | SIGNED_KEYWORD typename
1181 { $$ = lookup_signed_typename (parse_language,
1183 TYPE_NAME($2.type)); }
1185 { $$ = lookup_signed_typename (parse_language,
1188 /* It appears that this rule for templates is never
1189 reduced; template recognition happens by lookahead
1190 in the token processing code in yylex. */
1191 | TEMPLATE name '<' type '>'
1192 { $$ = lookup_template_type(copy_name($2), $4,
1193 expression_context_block);
1195 | const_or_volatile_or_space_identifier_noopt typebase
1196 { $$ = follow_types ($2); }
1197 | typebase const_or_volatile_or_space_identifier_noopt
1198 { $$ = follow_types ($1); }
1204 $$.stoken.ptr = "int";
1205 $$.stoken.length = 3;
1206 $$.type = lookup_signed_typename (parse_language,
1212 $$.stoken.ptr = "long";
1213 $$.stoken.length = 4;
1214 $$.type = lookup_signed_typename (parse_language,
1220 $$.stoken.ptr = "short";
1221 $$.stoken.length = 5;
1222 $$.type = lookup_signed_typename (parse_language,
1230 | nonempty_typelist ',' DOTDOTDOT
1232 VEC_safe_push (type_ptr, $1, NULL);
1240 VEC (type_ptr) *typelist = NULL;
1241 VEC_safe_push (type_ptr, typelist, $1);
1244 | nonempty_typelist ',' type
1246 VEC_safe_push (type_ptr, $1, $3);
1254 push_type_stack ($2);
1255 $$ = follow_types ($1);
1259 conversion_type_id: typebase conversion_declarator
1260 { $$ = follow_types ($1); }
1263 conversion_declarator: /* Nothing. */
1264 | ptr_operator conversion_declarator
1267 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1268 | VOLATILE_KEYWORD CONST_KEYWORD
1271 const_or_volatile_noopt: const_and_volatile
1272 { insert_type (tp_const);
1273 insert_type (tp_volatile);
1276 { insert_type (tp_const); }
1278 { insert_type (tp_volatile); }
1281 operator: OPERATOR NEW
1282 { $$ = operator_stoken (" new"); }
1284 { $$ = operator_stoken (" delete"); }
1285 | OPERATOR NEW '[' ']'
1286 { $$ = operator_stoken (" new[]"); }
1287 | OPERATOR DELETE '[' ']'
1288 { $$ = operator_stoken (" delete[]"); }
1290 { $$ = operator_stoken ("+"); }
1292 { $$ = operator_stoken ("-"); }
1294 { $$ = operator_stoken ("*"); }
1296 { $$ = operator_stoken ("/"); }
1298 { $$ = operator_stoken ("%"); }
1300 { $$ = operator_stoken ("^"); }
1302 { $$ = operator_stoken ("&"); }
1304 { $$ = operator_stoken ("|"); }
1306 { $$ = operator_stoken ("~"); }
1308 { $$ = operator_stoken ("!"); }
1310 { $$ = operator_stoken ("="); }
1312 { $$ = operator_stoken ("<"); }
1314 { $$ = operator_stoken (">"); }
1315 | OPERATOR ASSIGN_MODIFY
1316 { const char *op = "unknown";
1340 case BINOP_BITWISE_IOR:
1343 case BINOP_BITWISE_AND:
1346 case BINOP_BITWISE_XOR:
1353 $$ = operator_stoken (op);
1356 { $$ = operator_stoken ("<<"); }
1358 { $$ = operator_stoken (">>"); }
1360 { $$ = operator_stoken ("=="); }
1362 { $$ = operator_stoken ("!="); }
1364 { $$ = operator_stoken ("<="); }
1366 { $$ = operator_stoken (">="); }
1368 { $$ = operator_stoken ("&&"); }
1370 { $$ = operator_stoken ("||"); }
1371 | OPERATOR INCREMENT
1372 { $$ = operator_stoken ("++"); }
1373 | OPERATOR DECREMENT
1374 { $$ = operator_stoken ("--"); }
1376 { $$ = operator_stoken (","); }
1377 | OPERATOR ARROW_STAR
1378 { $$ = operator_stoken ("->*"); }
1380 { $$ = operator_stoken ("->"); }
1382 { $$ = operator_stoken ("()"); }
1384 { $$ = operator_stoken ("[]"); }
1385 | OPERATOR conversion_type_id
1388 struct ui_file *buf = mem_fileopen ();
1390 c_print_type ($2, NULL, buf, -1, 0);
1391 name = ui_file_xstrdup (buf, &length);
1392 ui_file_delete (buf);
1393 $$ = operator_stoken (name);
1400 name : NAME { $$ = $1.stoken; }
1401 | BLOCKNAME { $$ = $1.stoken; }
1402 | TYPENAME { $$ = $1.stoken; }
1403 | NAME_OR_INT { $$ = $1.stoken; }
1404 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1405 | operator { $$ = $1; }
1408 name_not_typename : NAME
1410 /* These would be useful if name_not_typename was useful, but it is just
1411 a fake for "variable", so these cause reduce/reduce conflicts because
1412 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1413 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1414 context where only a name could occur, this might be useful.
1420 $$.sym = lookup_symbol ($1.ptr,
1421 expression_context_block,
1423 &$$.is_a_field_of_this);
1430 /* Returns a stoken of the operator name given by OP (which does not
1431 include the string "operator"). */
1432 static struct stoken
1433 operator_stoken (const char *op)
1435 static const char *operator_string = "operator";
1436 struct stoken st = { NULL, 0 };
1437 st.length = strlen (operator_string) + strlen (op);
1438 st.ptr = malloc (st.length + 1);
1439 strcpy (st.ptr, operator_string);
1440 strcat (st.ptr, op);
1442 /* The toplevel (c_parse) will free the memory allocated here. */
1443 make_cleanup (free, st.ptr);
1447 /* Take care of parsing a number (anything that starts with a digit).
1448 Set yylval and return the token type; update lexptr.
1449 LEN is the number of characters in it. */
1451 /*** Needs some error checking for the float case ***/
1454 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1456 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1457 here, and we do kind of silly things like cast to unsigned. */
1464 int base = input_radix;
1467 /* Number of "L" suffixes encountered. */
1470 /* We have found a "L" or "U" suffix. */
1471 int found_suffix = 0;
1474 struct type *signed_type;
1475 struct type *unsigned_type;
1479 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1480 point. Return DECFLOAT. */
1482 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1485 putithere->typed_val_decfloat.type
1486 = parse_type->builtin_decfloat;
1487 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1488 gdbarch_byte_order (parse_gdbarch), p);
1493 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1496 putithere->typed_val_decfloat.type
1497 = parse_type->builtin_decdouble;
1498 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1499 gdbarch_byte_order (parse_gdbarch), p);
1504 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1507 putithere->typed_val_decfloat.type
1508 = parse_type->builtin_declong;
1509 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1510 gdbarch_byte_order (parse_gdbarch), p);
1515 if (! parse_c_float (parse_gdbarch, p, len,
1516 &putithere->typed_val_float.dval,
1517 &putithere->typed_val_float.type))
1522 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1566 if (c >= 'A' && c <= 'Z')
1568 if (c != 'l' && c != 'u')
1570 if (c >= '0' && c <= '9')
1578 if (base > 10 && c >= 'a' && c <= 'f')
1582 n += i = c - 'a' + 10;
1595 return ERROR; /* Char not a digit */
1598 return ERROR; /* Invalid digit in this base */
1600 /* Portably test for overflow (only works for nonzero values, so make
1601 a second check for zero). FIXME: Can't we just make n and prevn
1602 unsigned and avoid this? */
1603 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1604 unsigned_p = 1; /* Try something unsigned */
1606 /* Portably test for unsigned overflow.
1607 FIXME: This check is wrong; for example it doesn't find overflow
1608 on 0x123456789 when LONGEST is 32 bits. */
1609 if (c != 'l' && c != 'u' && n != 0)
1611 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1612 error (_("Numeric constant too large."));
1617 /* An integer constant is an int, a long, or a long long. An L
1618 suffix forces it to be long; an LL suffix forces it to be long
1619 long. If not forced to a larger size, it gets the first type of
1620 the above that it fits in. To figure out whether it fits, we
1621 shift it right and see whether anything remains. Note that we
1622 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1623 operation, because many compilers will warn about such a shift
1624 (which always produces a zero result). Sometimes gdbarch_int_bit
1625 or gdbarch_long_bit will be that big, sometimes not. To deal with
1626 the case where it is we just always shift the value more than
1627 once, with fewer bits each time. */
1629 un = (ULONGEST)n >> 2;
1631 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1633 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1635 /* A large decimal (not hex or octal) constant (between INT_MAX
1636 and UINT_MAX) is a long or unsigned long, according to ANSI,
1637 never an unsigned int, but this code treats it as unsigned
1638 int. This probably should be fixed. GCC gives a warning on
1641 unsigned_type = parse_type->builtin_unsigned_int;
1642 signed_type = parse_type->builtin_int;
1644 else if (long_p <= 1
1645 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1647 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1648 unsigned_type = parse_type->builtin_unsigned_long;
1649 signed_type = parse_type->builtin_long;
1654 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1655 < gdbarch_long_long_bit (parse_gdbarch))
1656 /* A long long does not fit in a LONGEST. */
1657 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1659 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1660 high_bit = (ULONGEST) 1 << shift;
1661 unsigned_type = parse_type->builtin_unsigned_long_long;
1662 signed_type = parse_type->builtin_long_long;
1665 putithere->typed_val_int.val = n;
1667 /* If the high bit of the worked out type is set then this number
1668 has to be unsigned. */
1670 if (unsigned_p || (n & high_bit))
1672 putithere->typed_val_int.type = unsigned_type;
1676 putithere->typed_val_int.type = signed_type;
1682 /* Temporary obstack used for holding strings. */
1683 static struct obstack tempbuf;
1684 static int tempbuf_init;
1686 /* Parse a C escape sequence. The initial backslash of the sequence
1687 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1688 last character of the sequence. If OUTPUT is not NULL, the
1689 translated form of the escape sequence will be written there. If
1690 OUTPUT is NULL, no output is written and the call will only affect
1691 *PTR. If an escape sequence is expressed in target bytes, then the
1692 entire sequence will simply be copied to OUTPUT. Return 1 if any
1693 character was emitted, 0 otherwise. */
1696 c_parse_escape (char **ptr, struct obstack *output)
1698 char *tokptr = *ptr;
1701 /* Some escape sequences undergo character set conversion. Those we
1705 /* Hex escapes do not undergo character set conversion, so keep
1706 the escape sequence for later. */
1709 obstack_grow_str (output, "\\x");
1711 if (!isxdigit (*tokptr))
1712 error (_("\\x escape without a following hex digit"));
1713 while (isxdigit (*tokptr))
1716 obstack_1grow (output, *tokptr);
1721 /* Octal escapes do not undergo character set conversion, so
1722 keep the escape sequence for later. */
1734 obstack_grow_str (output, "\\");
1736 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1740 obstack_1grow (output, *tokptr);
1746 /* We handle UCNs later. We could handle them here, but that
1747 would mean a spurious error in the case where the UCN could
1748 be converted to the target charset but not the host
1754 int i, len = c == 'U' ? 8 : 4;
1757 obstack_1grow (output, '\\');
1758 obstack_1grow (output, *tokptr);
1761 if (!isxdigit (*tokptr))
1762 error (_("\\%c escape without a following hex digit"), c);
1763 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1766 obstack_1grow (output, *tokptr);
1772 /* We must pass backslash through so that it does not
1773 cause quoting during the second expansion. */
1776 obstack_grow_str (output, "\\\\");
1780 /* Escapes which undergo conversion. */
1783 obstack_1grow (output, '\a');
1788 obstack_1grow (output, '\b');
1793 obstack_1grow (output, '\f');
1798 obstack_1grow (output, '\n');
1803 obstack_1grow (output, '\r');
1808 obstack_1grow (output, '\t');
1813 obstack_1grow (output, '\v');
1817 /* GCC extension. */
1820 obstack_1grow (output, HOST_ESCAPE_CHAR);
1824 /* Backslash-newline expands to nothing at all. */
1830 /* A few escapes just expand to the character itself. */
1834 /* GCC extensions. */
1839 /* Unrecognized escapes turn into the character itself. */
1842 obstack_1grow (output, *tokptr);
1850 /* Parse a string or character literal from TOKPTR. The string or
1851 character may be wide or unicode. *OUTPTR is set to just after the
1852 end of the literal in the input string. The resulting token is
1853 stored in VALUE. This returns a token value, either STRING or
1854 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1855 number of host characters in the literal. */
1857 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1861 enum c_string_type type;
1863 /* Build the gdb internal form of the input string in tempbuf. Note
1864 that the buffer is null byte terminated *only* for the
1865 convenience of debugging gdb itself and printing the buffer
1866 contents when the buffer contains no embedded nulls. Gdb does
1867 not depend upon the buffer being null byte terminated, it uses
1868 the length string instead. This allows gdb to handle C strings
1869 (as well as strings in other languages) with embedded null
1875 obstack_free (&tempbuf, NULL);
1876 obstack_init (&tempbuf);
1878 /* Record the string type. */
1881 type = C_WIDE_STRING;
1884 else if (*tokptr == 'u')
1889 else if (*tokptr == 'U')
1897 /* Skip the quote. */
1911 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1913 else if (c == quote)
1917 obstack_1grow (&tempbuf, c);
1919 /* FIXME: this does the wrong thing with multi-byte host
1920 characters. We could use mbrlen here, but that would
1921 make "set host-charset" a bit less useful. */
1926 if (*tokptr != quote)
1929 error (_("Unterminated string in expression."));
1931 error (_("Unmatched single quote."));
1936 value->ptr = obstack_base (&tempbuf);
1937 value->length = obstack_object_size (&tempbuf);
1941 return quote == '"' ? STRING : CHAR;
1948 enum exp_opcode opcode;
1952 static const struct token tokentab3[] =
1954 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1955 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
1956 {"->*", ARROW_STAR, BINOP_END, 1},
1957 {"...", DOTDOTDOT, BINOP_END, 0}
1960 static const struct token tokentab2[] =
1962 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1963 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1964 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1965 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
1966 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
1967 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
1968 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
1969 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
1970 {"++", INCREMENT, BINOP_END, 0},
1971 {"--", DECREMENT, BINOP_END, 0},
1972 {"->", ARROW, BINOP_END, 0},
1973 {"&&", ANDAND, BINOP_END, 0},
1974 {"||", OROR, BINOP_END, 0},
1975 /* "::" is *not* only C++: gdb overrides its meaning in several
1976 different ways, e.g., 'filename'::func, function::variable. */
1977 {"::", COLONCOLON, BINOP_END, 0},
1978 {"<<", LSH, BINOP_END, 0},
1979 {">>", RSH, BINOP_END, 0},
1980 {"==", EQUAL, BINOP_END, 0},
1981 {"!=", NOTEQUAL, BINOP_END, 0},
1982 {"<=", LEQ, BINOP_END, 0},
1983 {">=", GEQ, BINOP_END, 0},
1984 {".*", DOT_STAR, BINOP_END, 1}
1987 /* Identifier-like tokens. */
1988 static const struct token ident_tokens[] =
1990 {"unsigned", UNSIGNED, OP_NULL, 0},
1991 {"template", TEMPLATE, OP_NULL, 1},
1992 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
1993 {"struct", STRUCT, OP_NULL, 0},
1994 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
1995 {"sizeof", SIZEOF, OP_NULL, 0},
1996 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
1997 {"false", FALSEKEYWORD, OP_NULL, 1},
1998 {"class", CLASS, OP_NULL, 1},
1999 {"union", UNION, OP_NULL, 0},
2000 {"short", SHORT, OP_NULL, 0},
2001 {"const", CONST_KEYWORD, OP_NULL, 0},
2002 {"enum", ENUM, OP_NULL, 0},
2003 {"long", LONG, OP_NULL, 0},
2004 {"true", TRUEKEYWORD, OP_NULL, 1},
2005 {"int", INT_KEYWORD, OP_NULL, 0},
2006 {"new", NEW, OP_NULL, 1},
2007 {"delete", DELETE, OP_NULL, 1},
2008 {"operator", OPERATOR, OP_NULL, 1},
2010 {"and", ANDAND, BINOP_END, 1},
2011 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
2012 {"bitand", '&', OP_NULL, 1},
2013 {"bitor", '|', OP_NULL, 1},
2014 {"compl", '~', OP_NULL, 1},
2015 {"not", '!', OP_NULL, 1},
2016 {"not_eq", NOTEQUAL, BINOP_END, 1},
2017 {"or", OROR, BINOP_END, 1},
2018 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
2019 {"xor", '^', OP_NULL, 1},
2020 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1},
2022 {"const_cast", CONST_CAST, OP_NULL, 1 },
2023 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, 1 },
2024 {"static_cast", STATIC_CAST, OP_NULL, 1 },
2025 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, 1 }
2028 /* When we find that lexptr (the global var defined in parse.c) is
2029 pointing at a macro invocation, we expand the invocation, and call
2030 scan_macro_expansion to save the old lexptr here and point lexptr
2031 into the expanded text. When we reach the end of that, we call
2032 end_macro_expansion to pop back to the value we saved here. The
2033 macro expansion code promises to return only fully-expanded text,
2034 so we don't need to "push" more than one level.
2036 This is disgusting, of course. It would be cleaner to do all macro
2037 expansion beforehand, and then hand that to lexptr. But we don't
2038 really know where the expression ends. Remember, in a command like
2040 (gdb) break *ADDRESS if CONDITION
2042 we evaluate ADDRESS in the scope of the current frame, but we
2043 evaluate CONDITION in the scope of the breakpoint's location. So
2044 it's simply wrong to try to macro-expand the whole thing at once. */
2045 static char *macro_original_text;
2047 /* We save all intermediate macro expansions on this obstack for the
2048 duration of a single parse. The expansion text may sometimes have
2049 to live past the end of the expansion, due to yacc lookahead.
2050 Rather than try to be clever about saving the data for a single
2051 token, we simply keep it all and delete it after parsing has
2053 static struct obstack expansion_obstack;
2056 scan_macro_expansion (char *expansion)
2060 /* We'd better not be trying to push the stack twice. */
2061 gdb_assert (! macro_original_text);
2063 /* Copy to the obstack, and then free the intermediate
2065 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2068 /* Save the old lexptr value, so we can return to it when we're done
2069 parsing the expanded text. */
2070 macro_original_text = lexptr;
2076 scanning_macro_expansion (void)
2078 return macro_original_text != 0;
2083 finished_macro_expansion (void)
2085 /* There'd better be something to pop back to. */
2086 gdb_assert (macro_original_text);
2088 /* Pop back to the original text. */
2089 lexptr = macro_original_text;
2090 macro_original_text = 0;
2095 scan_macro_cleanup (void *dummy)
2097 if (macro_original_text)
2098 finished_macro_expansion ();
2100 obstack_free (&expansion_obstack, NULL);
2103 /* Return true iff the token represents a C++ cast operator. */
2106 is_cast_operator (const char *token, int len)
2108 return (! strncmp (token, "dynamic_cast", len)
2109 || ! strncmp (token, "static_cast", len)
2110 || ! strncmp (token, "reinterpret_cast", len)
2111 || ! strncmp (token, "const_cast", len));
2114 /* The scope used for macro expansion. */
2115 static struct macro_scope *expression_macro_scope;
2117 /* This is set if a NAME token appeared at the very end of the input
2118 string, with no whitespace separating the name from the EOF. This
2119 is used only when parsing to do field name completion. */
2120 static int saw_name_at_eof;
2122 /* This is set if the previously-returned token was a structure
2123 operator -- either '.' or ARROW. This is used only when parsing to
2124 do field name completion. */
2125 static int last_was_structop;
2127 /* Read one token, getting characters through lexptr. */
2130 lex_one_token (void)
2136 int saw_structop = last_was_structop;
2139 last_was_structop = 0;
2143 /* Check if this is a macro invocation that we need to expand. */
2144 if (! scanning_macro_expansion ())
2146 char *expanded = macro_expand_next (&lexptr,
2147 standard_macro_lookup,
2148 expression_macro_scope);
2151 scan_macro_expansion (expanded);
2154 prev_lexptr = lexptr;
2157 /* See if it is a special token of length 3. */
2158 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2159 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2161 if (tokentab3[i].cxx_only
2162 && parse_language->la_language != language_cplus)
2166 yylval.opcode = tokentab3[i].opcode;
2167 return tokentab3[i].token;
2170 /* See if it is a special token of length 2. */
2171 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2172 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2174 if (tokentab2[i].cxx_only
2175 && parse_language->la_language != language_cplus)
2179 yylval.opcode = tokentab2[i].opcode;
2180 if (in_parse_field && tokentab2[i].token == ARROW)
2181 last_was_structop = 1;
2182 return tokentab2[i].token;
2185 switch (c = *tokstart)
2188 /* If we were just scanning the result of a macro expansion,
2189 then we need to resume scanning the original text.
2190 If we're parsing for field name completion, and the previous
2191 token allows such completion, return a COMPLETE token.
2192 Otherwise, we were already scanning the original text, and
2193 we're really done. */
2194 if (scanning_macro_expansion ())
2196 finished_macro_expansion ();
2199 else if (saw_name_at_eof)
2201 saw_name_at_eof = 0;
2204 else if (saw_structop)
2223 if (paren_depth == 0)
2230 if (comma_terminates
2232 && ! scanning_macro_expansion ())
2238 /* Might be a floating point number. */
2239 if (lexptr[1] < '0' || lexptr[1] > '9')
2242 last_was_structop = 1;
2243 goto symbol; /* Nope, must be a symbol. */
2245 /* FALL THRU into number case. */
2258 /* It's a number. */
2259 int got_dot = 0, got_e = 0, toktype;
2261 int hex = input_radix > 10;
2263 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2268 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2276 /* This test includes !hex because 'e' is a valid hex digit
2277 and thus does not indicate a floating point number when
2278 the radix is hex. */
2279 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2280 got_dot = got_e = 1;
2281 /* This test does not include !hex, because a '.' always indicates
2282 a decimal floating point number regardless of the radix. */
2283 else if (!got_dot && *p == '.')
2285 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2286 && (*p == '-' || *p == '+'))
2287 /* This is the sign of the exponent, not the end of the
2290 /* We will take any letters or digits. parse_number will
2291 complain if past the radix, or if L or U are not final. */
2292 else if ((*p < '0' || *p > '9')
2293 && ((*p < 'a' || *p > 'z')
2294 && (*p < 'A' || *p > 'Z')))
2297 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2298 if (toktype == ERROR)
2300 char *err_copy = (char *) alloca (p - tokstart + 1);
2302 memcpy (err_copy, tokstart, p - tokstart);
2303 err_copy[p - tokstart] = 0;
2304 error (_("Invalid number \"%s\"."), err_copy);
2312 char *p = &tokstart[1];
2313 size_t len = strlen ("entry");
2315 while (isspace (*p))
2317 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2349 if (tokstart[1] != '"' && tokstart[1] != '\'')
2356 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2361 error (_("Empty character constant."));
2362 else if (host_len > 2 && c == '\'')
2365 namelen = lexptr - tokstart - 1;
2368 else if (host_len > 1)
2369 error (_("Invalid character constant."));
2375 if (!(c == '_' || c == '$'
2376 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2377 /* We must have come across a bad character (e.g. ';'). */
2378 error (_("Invalid character '%c' in expression."), c);
2380 /* It's a name. See how long it is. */
2382 for (c = tokstart[namelen];
2383 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2384 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2386 /* Template parameter lists are part of the name.
2387 FIXME: This mishandles `print $a<4&&$a>3'. */
2391 if (! is_cast_operator (tokstart, namelen))
2393 /* Scan ahead to get rest of the template specification. Note
2394 that we look ahead only when the '<' adjoins non-whitespace
2395 characters; for comparison expressions, e.g. "a < b > c",
2396 there must be spaces before the '<', etc. */
2398 char * p = find_template_name_end (tokstart + namelen);
2400 namelen = p - tokstart;
2404 c = tokstart[++namelen];
2407 /* The token "if" terminates the expression and is NOT removed from
2408 the input stream. It doesn't count if it appears in the
2409 expansion of a macro. */
2411 && tokstart[0] == 'i'
2412 && tokstart[1] == 'f'
2413 && ! scanning_macro_expansion ())
2418 /* For the same reason (breakpoint conditions), "thread N"
2419 terminates the expression. "thread" could be an identifier, but
2420 an identifier is never followed by a number without intervening
2421 punctuation. "task" is similar. Handle abbreviations of these,
2422 similarly to breakpoint.c:find_condition_and_thread. */
2424 && (strncmp (tokstart, "thread", namelen) == 0
2425 || strncmp (tokstart, "task", namelen) == 0)
2426 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2427 && ! scanning_macro_expansion ())
2429 char *p = tokstart + namelen + 1;
2430 while (*p == ' ' || *p == '\t')
2432 if (*p >= '0' && *p <= '9')
2440 yylval.sval.ptr = tokstart;
2441 yylval.sval.length = namelen;
2443 /* Catch specific keywords. */
2444 copy = copy_name (yylval.sval);
2445 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2446 if (strcmp (copy, ident_tokens[i].operator) == 0)
2448 if (ident_tokens[i].cxx_only
2449 && parse_language->la_language != language_cplus)
2452 /* It is ok to always set this, even though we don't always
2453 strictly need to. */
2454 yylval.opcode = ident_tokens[i].opcode;
2455 return ident_tokens[i].token;
2458 if (*tokstart == '$')
2461 if (in_parse_field && *lexptr == '\0')
2462 saw_name_at_eof = 1;
2466 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2473 DEF_VEC_O (token_and_value);
2475 /* A FIFO of tokens that have been read but not yet returned to the
2477 static VEC (token_and_value) *token_fifo;
2479 /* Non-zero if the lexer should return tokens from the FIFO. */
2482 /* Temporary storage for c_lex; this holds symbol names as they are
2484 static struct obstack name_obstack;
2486 /* Classify a NAME token. The contents of the token are in `yylval'.
2487 Updates yylval and returns the new token type. BLOCK is the block
2488 in which lookups start; this can be NULL to mean the global
2491 classify_name (struct block *block)
2495 int is_a_field_of_this = 0;
2497 copy = copy_name (yylval.sval);
2499 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2500 parse_language->la_language == language_cplus
2501 ? &is_a_field_of_this : (int *) NULL);
2503 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2505 yylval.ssym.sym = sym;
2506 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2511 /* See if it's a file name. */
2512 struct symtab *symtab;
2514 symtab = lookup_symtab (copy);
2517 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2522 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2524 yylval.tsym.type = SYMBOL_TYPE (sym);
2529 = language_lookup_primitive_type_by_name (parse_language,
2530 parse_gdbarch, copy);
2531 if (yylval.tsym.type != NULL)
2534 /* Input names that aren't symbols but ARE valid hex numbers, when
2535 the input radix permits them, can be names or numbers depending
2536 on the parse. Note we support radixes > 16 here. */
2538 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2539 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2541 YYSTYPE newlval; /* Its value is ignored. */
2542 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2545 yylval.ssym.sym = sym;
2546 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2551 /* Any other kind of symbol */
2552 yylval.ssym.sym = sym;
2553 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2556 && parse_language->la_language == language_cplus
2557 && !is_a_field_of_this
2558 && !lookup_minimal_symbol (copy, NULL, NULL))
2559 return UNKNOWN_CPP_NAME;
2564 /* Like classify_name, but used by the inner loop of the lexer, when a
2565 name might have already been seen. FIRST_NAME is true if the token
2566 in `yylval' is the first component of a name, false otherwise. */
2569 classify_inner_name (struct block *block, int first_name)
2571 struct type *type, *new_type;
2575 return classify_name (block);
2577 type = check_typedef (yylval.tsym.type);
2578 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2579 && TYPE_CODE (type) != TYPE_CODE_UNION
2580 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2583 copy = copy_name (yylval.tsym.stoken);
2584 yylval.ssym.sym = cp_lookup_nested_symbol (yylval.tsym.type, copy, block);
2585 if (yylval.ssym.sym == NULL)
2588 switch (SYMBOL_CLASS (yylval.ssym.sym))
2595 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym);;
2599 yylval.ssym.is_a_field_of_this = 0;
2602 internal_error (__FILE__, __LINE__, _("not reached"));
2605 /* The outer level of a two-level lexer. This calls the inner lexer
2606 to return tokens. It then either returns these tokens, or
2607 aggregates them into a larger token. This lets us work around a
2608 problem in our parsing approach, where the parser could not
2609 distinguish between qualified names and qualified types at the
2612 This approach is still not ideal, because it mishandles template
2613 types. See the comment in lex_one_token for an example. However,
2614 this is still an improvement over the earlier approach, and will
2615 suffice until we move to better parsing technology. */
2619 token_and_value current;
2620 int first_was_coloncolon, last_was_coloncolon, first_iter;
2622 if (popping && !VEC_empty (token_and_value, token_fifo))
2624 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2625 VEC_ordered_remove (token_and_value, token_fifo, 0);
2631 current.token = lex_one_token ();
2632 if (current.token == NAME)
2633 current.token = classify_name (expression_context_block);
2634 if (parse_language->la_language != language_cplus
2635 || (current.token != TYPENAME && current.token != COLONCOLON))
2636 return current.token;
2638 first_was_coloncolon = current.token == COLONCOLON;
2639 last_was_coloncolon = first_was_coloncolon;
2640 obstack_free (&name_obstack, obstack_base (&name_obstack));
2641 if (!last_was_coloncolon)
2642 obstack_grow (&name_obstack, yylval.sval.ptr, yylval.sval.length);
2643 current.value = yylval;
2647 token_and_value next;
2649 next.token = lex_one_token ();
2650 next.value = yylval;
2652 if (next.token == NAME && last_was_coloncolon)
2656 classification = classify_inner_name (first_was_coloncolon
2658 : expression_context_block,
2660 /* We keep going until we either run out of names, or until
2661 we have a qualified name which is not a type. */
2662 if (classification != TYPENAME && classification != NAME)
2664 /* Push the final component and leave the loop. */
2665 VEC_safe_push (token_and_value, token_fifo, &next);
2669 /* Update the partial name we are constructing. */
2672 /* We don't want to put a leading "::" into the name. */
2673 obstack_grow_str (&name_obstack, "::");
2675 obstack_grow (&name_obstack, next.value.sval.ptr,
2676 next.value.sval.length);
2678 yylval.sval.ptr = obstack_base (&name_obstack);
2679 yylval.sval.length = obstack_object_size (&name_obstack);
2680 current.value = yylval;
2681 current.token = classification;
2683 last_was_coloncolon = 0;
2685 else if (next.token == COLONCOLON && !last_was_coloncolon)
2686 last_was_coloncolon = 1;
2689 /* We've reached the end of the name. */
2690 VEC_safe_push (token_and_value, token_fifo, &next);
2699 /* If we ended with a "::", insert it too. */
2700 if (last_was_coloncolon)
2703 memset (&cc, 0, sizeof (token_and_value));
2704 if (first_was_coloncolon && first_iter)
2709 cc.token = COLONCOLON;
2710 VEC_safe_insert (token_and_value, token_fifo, 0, &cc);
2713 yylval = current.value;
2714 yylval.sval.ptr = obstack_copy0 (&expansion_obstack,
2716 yylval.sval.length);
2717 return current.token;
2724 struct cleanup *back_to = make_cleanup (free_current_contents,
2725 &expression_macro_scope);
2727 /* Set up the scope for macro expansion. */
2728 expression_macro_scope = NULL;
2730 if (expression_context_block)
2731 expression_macro_scope
2732 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2734 expression_macro_scope = default_macro_scope ();
2735 if (! expression_macro_scope)
2736 expression_macro_scope = user_macro_scope ();
2738 /* Initialize macro expansion code. */
2739 obstack_init (&expansion_obstack);
2740 gdb_assert (! macro_original_text);
2741 make_cleanup (scan_macro_cleanup, 0);
2743 make_cleanup_restore_integer (&yydebug);
2744 yydebug = parser_debug;
2746 /* Initialize some state used by the lexer. */
2747 last_was_structop = 0;
2748 saw_name_at_eof = 0;
2750 VEC_free (token_and_value, token_fifo);
2752 obstack_init (&name_obstack);
2753 make_cleanup_obstack_free (&name_obstack);
2755 result = yyparse ();
2756 do_cleanups (back_to);
2765 lexptr = prev_lexptr;
2767 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);