1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986-2020 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Parse a C expression from text in a string,
20 and return the result as a struct expression pointer.
21 That structure contains arithmetic operations in reverse polish,
22 with constants represented by operations that are followed by special data.
23 See expression.h for the details of the format.
24 What is important here is that it can be built up sequentially
25 during the process of parsing; the lower levels of the tree always
26 come first in the result.
28 Note that malloc's and realloc's in this file are transformed to
29 xmalloc and xrealloc respectively by the same sed command in the
30 makefile that remaps any other malloc/realloc inserted by the parser
31 generator. Doing this with #defines and trying to control the interaction
32 with include files (<malloc.h> and <stdlib.h> for example) just became
33 too messy, particularly when such includes can be inserted at random
34 times by the parser generator. */
40 #include "expression.h"
42 #include "parser-defs.h"
45 #include "c-support.h"
46 #include "bfd.h" /* Required by objfiles.h. */
47 #include "symfile.h" /* Required by objfiles.h. */
48 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
51 #include "cp-support.h"
52 #include "macroscope.h"
53 #include "objc-lang.h"
54 #include "typeprint.h"
56 #include "type-stack.h"
57 #include "target-float.h"
59 #define parse_type(ps) builtin_type (ps->gdbarch ())
61 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
63 #define GDB_YY_REMAP_PREFIX c_
66 /* The state of the parser, used internally when we are parsing the
69 static struct parser_state *pstate = NULL;
71 /* Data that must be held for the duration of a parse. */
75 /* These are used to hold type lists and type stacks that are
76 allocated during the parse. */
77 std::vector<std::unique_ptr<std::vector<struct type *>>> type_lists;
78 std::vector<std::unique_ptr<struct type_stack>> type_stacks;
80 /* Storage for some strings allocated during the parse. */
81 std::vector<gdb::unique_xmalloc_ptr<char>> strings;
83 /* When we find that lexptr (the global var defined in parse.c) is
84 pointing at a macro invocation, we expand the invocation, and call
85 scan_macro_expansion to save the old lexptr here and point lexptr
86 into the expanded text. When we reach the end of that, we call
87 end_macro_expansion to pop back to the value we saved here. The
88 macro expansion code promises to return only fully-expanded text,
89 so we don't need to "push" more than one level.
91 This is disgusting, of course. It would be cleaner to do all macro
92 expansion beforehand, and then hand that to lexptr. But we don't
93 really know where the expression ends. Remember, in a command like
95 (gdb) break *ADDRESS if CONDITION
97 we evaluate ADDRESS in the scope of the current frame, but we
98 evaluate CONDITION in the scope of the breakpoint's location. So
99 it's simply wrong to try to macro-expand the whole thing at once. */
100 const char *macro_original_text = nullptr;
102 /* We save all intermediate macro expansions on this obstack for the
103 duration of a single parse. The expansion text may sometimes have
104 to live past the end of the expansion, due to yacc lookahead.
105 Rather than try to be clever about saving the data for a single
106 token, we simply keep it all and delete it after parsing has
108 auto_obstack expansion_obstack;
110 /* The type stack. */
111 struct type_stack type_stack;
114 /* This is set and cleared in c_parse. */
116 static struct c_parse_state *cpstate;
120 static int yylex (void);
122 static void yyerror (const char *);
124 static int type_aggregate_p (struct type *);
128 /* Although the yacc "value" of an expression is not used,
129 since the result is stored in the structure being created,
130 other node types do have values. */
145 struct typed_stoken tsval;
147 struct symtoken ssym;
149 const struct block *bval;
150 enum exp_opcode opcode;
152 struct stoken_vector svec;
153 std::vector<struct type *> *tvec;
155 struct type_stack *type_stack;
157 struct objc_class_str theclass;
161 /* YYSTYPE gets defined by %union */
162 static int parse_number (struct parser_state *par_state,
163 const char *, int, int, YYSTYPE *);
164 static struct stoken operator_stoken (const char *);
165 static struct stoken typename_stoken (const char *);
166 static void check_parameter_typelist (std::vector<struct type *> *);
167 static void write_destructor_name (struct parser_state *par_state,
171 static void c_print_token (FILE *file, int type, YYSTYPE value);
172 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
176 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly function_method
178 %type <tval> type typebase
179 %type <tvec> nonempty_typelist func_mod parameter_typelist
180 /* %type <bval> block */
182 /* Fancy type parsing. */
184 %type <lval> array_mod
185 %type <tval> conversion_type_id
187 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
189 %token <typed_val_int> INT COMPLEX_INT
190 %token <typed_val_float> FLOAT COMPLEX_FLOAT
192 /* Both NAME and TYPENAME tokens represent symbols in the input,
193 and both convey their data as strings.
194 But a TYPENAME is a string that happens to be defined as a typedef
195 or builtin type name (such as int or char)
196 and a NAME is any other symbol.
197 Contexts where this distinction is not important can use the
198 nonterminal "name", which matches either NAME or TYPENAME. */
200 %token <tsval> STRING
201 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
202 %token SELECTOR /* ObjC "@selector" pseudo-operator */
204 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
205 %token <ssym> UNKNOWN_CPP_NAME
206 %token <voidval> COMPLETE
207 %token <tsym> TYPENAME
208 %token <theclass> CLASSNAME /* ObjC Class name */
209 %type <sval> name field_name
210 %type <svec> string_exp
211 %type <ssym> name_not_typename
212 %type <tsym> type_name
214 /* This is like a '[' token, but is only generated when parsing
215 Objective C. This lets us reuse the same parser without
216 erroneously parsing ObjC-specific expressions in C. */
219 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
220 but which would parse as a valid number in the current input radix.
221 E.g. "c" when input_radix==16. Depending on the parse, it will be
222 turned into a name or into a number. */
224 %token <ssym> NAME_OR_INT
227 %token STRUCT CLASS UNION ENUM SIZEOF ALIGNOF UNSIGNED COLONCOLON
232 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
238 /* Special type cases, put in to allow the parser to distinguish different
240 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
241 %token RESTRICT ATOMIC
243 %token <sval> DOLLAR_VARIABLE
245 %token <opcode> ASSIGN_MODIFY
254 %right '=' ASSIGN_MODIFY
262 %left '<' '>' LEQ GEQ
267 %right UNARY INCREMENT DECREMENT
268 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
269 %token <ssym> BLOCKNAME
270 %token <bval> FILENAME
284 { write_exp_elt_opcode(pstate, OP_TYPE);
285 write_exp_elt_type(pstate, $1);
286 write_exp_elt_opcode(pstate, OP_TYPE);}
289 write_exp_elt_opcode (pstate, OP_TYPEOF);
291 | TYPEOF '(' type ')'
293 write_exp_elt_opcode (pstate, OP_TYPE);
294 write_exp_elt_type (pstate, $3);
295 write_exp_elt_opcode (pstate, OP_TYPE);
297 | DECLTYPE '(' exp ')'
299 write_exp_elt_opcode (pstate, OP_DECLTYPE);
303 /* Expressions, including the comma operator. */
306 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
309 /* Expressions, not including the comma operator. */
310 exp : '*' exp %prec UNARY
311 { write_exp_elt_opcode (pstate, UNOP_IND); }
314 exp : '&' exp %prec UNARY
315 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
318 exp : '-' exp %prec UNARY
319 { write_exp_elt_opcode (pstate, UNOP_NEG); }
322 exp : '+' exp %prec UNARY
323 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
326 exp : '!' exp %prec UNARY
327 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
330 exp : '~' exp %prec UNARY
331 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
334 exp : INCREMENT exp %prec UNARY
335 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
338 exp : DECREMENT exp %prec UNARY
339 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
342 exp : exp INCREMENT %prec UNARY
343 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
346 exp : exp DECREMENT %prec UNARY
347 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
350 exp : TYPEID '(' exp ')' %prec UNARY
351 { write_exp_elt_opcode (pstate, OP_TYPEID); }
354 exp : TYPEID '(' type_exp ')' %prec UNARY
355 { write_exp_elt_opcode (pstate, OP_TYPEID); }
358 exp : SIZEOF exp %prec UNARY
359 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
362 exp : ALIGNOF '(' type_exp ')' %prec UNARY
363 { write_exp_elt_opcode (pstate, UNOP_ALIGNOF); }
366 exp : exp ARROW field_name
367 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
368 write_exp_string (pstate, $3);
369 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
372 exp : exp ARROW field_name COMPLETE
373 { pstate->mark_struct_expression ();
374 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
375 write_exp_string (pstate, $3);
376 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
379 exp : exp ARROW COMPLETE
381 pstate->mark_struct_expression ();
382 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
385 write_exp_string (pstate, s);
386 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
389 exp : exp ARROW '~' name
390 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
391 write_destructor_name (pstate, $4);
392 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
395 exp : exp ARROW '~' name COMPLETE
396 { pstate->mark_struct_expression ();
397 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
398 write_destructor_name (pstate, $4);
399 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
402 exp : exp ARROW qualified_name
403 { /* exp->type::name becomes exp->*(&type::name) */
404 /* Note: this doesn't work if name is a
405 static member! FIXME */
406 write_exp_elt_opcode (pstate, UNOP_ADDR);
407 write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
410 exp : exp ARROW_STAR exp
411 { write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
414 exp : exp '.' field_name
415 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
416 write_exp_string (pstate, $3);
417 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
420 exp : exp '.' field_name COMPLETE
421 { pstate->mark_struct_expression ();
422 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
423 write_exp_string (pstate, $3);
424 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
427 exp : exp '.' COMPLETE
429 pstate->mark_struct_expression ();
430 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
433 write_exp_string (pstate, s);
434 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
437 exp : exp '.' '~' name
438 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
439 write_destructor_name (pstate, $4);
440 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
443 exp : exp '.' '~' name COMPLETE
444 { pstate->mark_struct_expression ();
445 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
446 write_destructor_name (pstate, $4);
447 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
450 exp : exp '.' qualified_name
451 { /* exp.type::name becomes exp.*(&type::name) */
452 /* Note: this doesn't work if name is a
453 static member! FIXME */
454 write_exp_elt_opcode (pstate, UNOP_ADDR);
455 write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
458 exp : exp DOT_STAR exp
459 { write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
462 exp : exp '[' exp1 ']'
463 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
466 exp : exp OBJC_LBRAC exp1 ']'
467 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
471 * The rules below parse ObjC message calls of the form:
472 * '[' target selector {':' argument}* ']'
475 exp : OBJC_LBRAC TYPENAME
479 std::string copy = copy_name ($2.stoken);
480 theclass = lookup_objc_class (pstate->gdbarch (),
483 error (_("%s is not an ObjC Class"),
485 write_exp_elt_opcode (pstate, OP_LONG);
486 write_exp_elt_type (pstate,
487 parse_type (pstate)->builtin_int);
488 write_exp_elt_longcst (pstate, (LONGEST) theclass);
489 write_exp_elt_opcode (pstate, OP_LONG);
493 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
494 end_msglist (pstate);
495 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
499 exp : OBJC_LBRAC CLASSNAME
501 write_exp_elt_opcode (pstate, OP_LONG);
502 write_exp_elt_type (pstate,
503 parse_type (pstate)->builtin_int);
504 write_exp_elt_longcst (pstate, (LONGEST) $2.theclass);
505 write_exp_elt_opcode (pstate, OP_LONG);
509 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
510 end_msglist (pstate);
511 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
518 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
519 end_msglist (pstate);
520 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
525 { add_msglist(&$1, 0); }
533 msgarg : name ':' exp
534 { add_msglist(&$1, 1); }
535 | ':' exp /* Unnamed arg. */
536 { add_msglist(0, 1); }
537 | ',' exp /* Variable number of args. */
538 { add_msglist(0, 0); }
542 /* This is to save the value of arglist_len
543 being accumulated by an outer function call. */
544 { pstate->start_arglist (); }
545 arglist ')' %prec ARROW
546 { write_exp_elt_opcode (pstate, OP_FUNCALL);
547 write_exp_elt_longcst (pstate,
548 pstate->end_arglist ());
549 write_exp_elt_opcode (pstate, OP_FUNCALL); }
552 /* This is here to disambiguate with the production for
553 "func()::static_var" further below, which uses
554 function_method_void. */
555 exp : exp '(' ')' %prec ARROW
556 { pstate->start_arglist ();
557 write_exp_elt_opcode (pstate, OP_FUNCALL);
558 write_exp_elt_longcst (pstate,
559 pstate->end_arglist ());
560 write_exp_elt_opcode (pstate, OP_FUNCALL); }
564 exp : UNKNOWN_CPP_NAME '('
566 /* This could potentially be a an argument defined
567 lookup function (Koenig). */
568 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
570 (pstate, pstate->expression_context_block);
571 write_exp_elt_sym (pstate,
572 NULL); /* Placeholder. */
573 write_exp_string (pstate, $1.stoken);
574 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
576 /* This is to save the value of arglist_len
577 being accumulated by an outer function call. */
579 pstate->start_arglist ();
581 arglist ')' %prec ARROW
583 write_exp_elt_opcode (pstate, OP_FUNCALL);
584 write_exp_elt_longcst (pstate,
585 pstate->end_arglist ());
586 write_exp_elt_opcode (pstate, OP_FUNCALL);
591 { pstate->start_arglist (); }
598 { pstate->arglist_len = 1; }
601 arglist : arglist ',' exp %prec ABOVE_COMMA
602 { pstate->arglist_len++; }
605 function_method: exp '(' parameter_typelist ')' const_or_volatile
607 std::vector<struct type *> *type_list = $3;
608 LONGEST len = type_list->size ();
610 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
611 /* Save the const/volatile qualifiers as
612 recorded by the const_or_volatile
613 production's actions. */
614 write_exp_elt_longcst
617 .follow_type_instance_flags ()));
618 write_exp_elt_longcst (pstate, len);
619 for (type *type_elt : *type_list)
620 write_exp_elt_type (pstate, type_elt);
621 write_exp_elt_longcst(pstate, len);
622 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
626 function_method_void: exp '(' ')' const_or_volatile
627 { write_exp_elt_opcode (pstate, TYPE_INSTANCE);
629 write_exp_elt_longcst
631 cpstate->type_stack.follow_type_instance_flags ());
632 write_exp_elt_longcst (pstate, 0);
633 write_exp_elt_longcst (pstate, 0);
634 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
638 exp : function_method
641 /* Normally we must interpret "func()" as a function call, instead of
642 a type. The user needs to write func(void) to disambiguate.
643 However, in the "func()::static_var" case, there's no
645 function_method_void_or_typelist: function_method
646 | function_method_void
649 exp : function_method_void_or_typelist COLONCOLON name
651 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
652 write_exp_string (pstate, $3);
653 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
658 { $$ = pstate->end_arglist () - 1; }
660 exp : lcurly arglist rcurly %prec ARROW
661 { write_exp_elt_opcode (pstate, OP_ARRAY);
662 write_exp_elt_longcst (pstate, (LONGEST) 0);
663 write_exp_elt_longcst (pstate, (LONGEST) $3);
664 write_exp_elt_opcode (pstate, OP_ARRAY); }
667 exp : lcurly type_exp rcurly exp %prec UNARY
668 { write_exp_elt_opcode (pstate, UNOP_MEMVAL_TYPE); }
671 exp : '(' type_exp ')' exp %prec UNARY
672 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
679 /* Binary operators in order of decreasing precedence. */
682 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
686 { write_exp_elt_opcode (pstate, BINOP_MUL); }
690 { write_exp_elt_opcode (pstate, BINOP_DIV); }
694 { write_exp_elt_opcode (pstate, BINOP_REM); }
698 { write_exp_elt_opcode (pstate, BINOP_ADD); }
702 { write_exp_elt_opcode (pstate, BINOP_SUB); }
706 { write_exp_elt_opcode (pstate, BINOP_LSH); }
710 { write_exp_elt_opcode (pstate, BINOP_RSH); }
714 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
717 exp : exp NOTEQUAL exp
718 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
722 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
726 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
730 { write_exp_elt_opcode (pstate, BINOP_LESS); }
734 { write_exp_elt_opcode (pstate, BINOP_GTR); }
738 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
742 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
746 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
750 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
754 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
757 exp : exp '?' exp ':' exp %prec '?'
758 { write_exp_elt_opcode (pstate, TERNOP_COND); }
762 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
765 exp : exp ASSIGN_MODIFY exp
766 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
767 write_exp_elt_opcode (pstate, $2);
768 write_exp_elt_opcode (pstate,
769 BINOP_ASSIGN_MODIFY); }
773 { write_exp_elt_opcode (pstate, OP_LONG);
774 write_exp_elt_type (pstate, $1.type);
775 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
776 write_exp_elt_opcode (pstate, OP_LONG); }
781 write_exp_elt_opcode (pstate, OP_LONG);
782 write_exp_elt_type (pstate, TYPE_TARGET_TYPE ($1.type));
783 write_exp_elt_longcst (pstate, 0);
784 write_exp_elt_opcode (pstate, OP_LONG);
785 write_exp_elt_opcode (pstate, OP_LONG);
786 write_exp_elt_type (pstate, TYPE_TARGET_TYPE ($1.type));
787 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
788 write_exp_elt_opcode (pstate, OP_LONG);
789 write_exp_elt_opcode (pstate, OP_COMPLEX);
790 write_exp_elt_type (pstate, $1.type);
791 write_exp_elt_opcode (pstate, OP_COMPLEX);
797 struct stoken_vector vec;
800 write_exp_string_vector (pstate, $1.type, &vec);
806 parse_number (pstate, $1.stoken.ptr,
807 $1.stoken.length, 0, &val);
808 write_exp_elt_opcode (pstate, OP_LONG);
809 write_exp_elt_type (pstate, val.typed_val_int.type);
810 write_exp_elt_longcst (pstate,
811 (LONGEST) val.typed_val_int.val);
812 write_exp_elt_opcode (pstate, OP_LONG);
818 { write_exp_elt_opcode (pstate, OP_FLOAT);
819 write_exp_elt_type (pstate, $1.type);
820 write_exp_elt_floatcst (pstate, $1.val);
821 write_exp_elt_opcode (pstate, OP_FLOAT); }
826 struct type *underlying
827 = TYPE_TARGET_TYPE ($1.type);
829 write_exp_elt_opcode (pstate, OP_FLOAT);
830 write_exp_elt_type (pstate, underlying);
832 target_float_from_host_double (val, underlying, 0);
833 write_exp_elt_floatcst (pstate, val);
834 write_exp_elt_opcode (pstate, OP_FLOAT);
835 write_exp_elt_opcode (pstate, OP_FLOAT);
836 write_exp_elt_type (pstate, underlying);
837 write_exp_elt_floatcst (pstate, $1.val);
838 write_exp_elt_opcode (pstate, OP_FLOAT);
839 write_exp_elt_opcode (pstate, OP_COMPLEX);
840 write_exp_elt_type (pstate, $1.type);
841 write_exp_elt_opcode (pstate, OP_COMPLEX);
848 exp : DOLLAR_VARIABLE
850 write_dollar_variable (pstate, $1);
854 exp : SELECTOR '(' name ')'
856 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR);
857 write_exp_string (pstate, $3);
858 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR); }
861 exp : SIZEOF '(' type ')' %prec UNARY
862 { struct type *type = $3;
863 write_exp_elt_opcode (pstate, OP_LONG);
864 write_exp_elt_type (pstate, lookup_signed_typename
865 (pstate->language (),
867 type = check_typedef (type);
869 /* $5.3.3/2 of the C++ Standard (n3290 draft)
870 says of sizeof: "When applied to a reference
871 or a reference type, the result is the size of
872 the referenced type." */
873 if (TYPE_IS_REFERENCE (type))
874 type = check_typedef (TYPE_TARGET_TYPE (type));
875 write_exp_elt_longcst (pstate,
876 (LONGEST) TYPE_LENGTH (type));
877 write_exp_elt_opcode (pstate, OP_LONG); }
880 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
881 { write_exp_elt_opcode (pstate,
882 UNOP_REINTERPRET_CAST); }
885 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
886 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
889 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
890 { write_exp_elt_opcode (pstate, UNOP_DYNAMIC_CAST); }
893 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
894 { /* We could do more error checking here, but
895 it doesn't seem worthwhile. */
896 write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
902 /* We copy the string here, and not in the
903 lexer, to guarantee that we do not leak a
904 string. Note that we follow the
905 NUL-termination convention of the
907 struct typed_stoken *vec = XNEW (struct typed_stoken);
912 vec->length = $1.length;
913 vec->ptr = (char *) malloc ($1.length + 1);
914 memcpy (vec->ptr, $1.ptr, $1.length + 1);
919 /* Note that we NUL-terminate here, but just
923 $$.tokens = XRESIZEVEC (struct typed_stoken,
926 p = (char *) malloc ($2.length + 1);
927 memcpy (p, $2.ptr, $2.length + 1);
929 $$.tokens[$$.len - 1].type = $2.type;
930 $$.tokens[$$.len - 1].length = $2.length;
931 $$.tokens[$$.len - 1].ptr = p;
938 c_string_type type = C_STRING;
940 for (i = 0; i < $1.len; ++i)
942 switch ($1.tokens[i].type)
950 && type != $1.tokens[i].type)
951 error (_("Undefined string concatenation."));
952 type = (enum c_string_type_values) $1.tokens[i].type;
956 internal_error (__FILE__, __LINE__,
957 "unrecognized type in string concatenation");
961 write_exp_string_vector (pstate, type, &$1);
962 for (i = 0; i < $1.len; ++i)
963 free ($1.tokens[i].ptr);
968 exp : NSSTRING /* ObjC NextStep NSString constant
969 * of the form '@' '"' string '"'.
971 { write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING);
972 write_exp_string (pstate, $1);
973 write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING); }
978 { write_exp_elt_opcode (pstate, OP_LONG);
979 write_exp_elt_type (pstate,
980 parse_type (pstate)->builtin_bool);
981 write_exp_elt_longcst (pstate, (LONGEST) 1);
982 write_exp_elt_opcode (pstate, OP_LONG); }
986 { write_exp_elt_opcode (pstate, OP_LONG);
987 write_exp_elt_type (pstate,
988 parse_type (pstate)->builtin_bool);
989 write_exp_elt_longcst (pstate, (LONGEST) 0);
990 write_exp_elt_opcode (pstate, OP_LONG); }
998 $$ = SYMBOL_BLOCK_VALUE ($1.sym.symbol);
1000 error (_("No file or function \"%s\"."),
1001 copy_name ($1.stoken).c_str ());
1009 block : block COLONCOLON name
1011 std::string copy = copy_name ($3);
1013 = lookup_symbol (copy.c_str (), $1,
1014 VAR_DOMAIN, NULL).symbol;
1016 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
1017 error (_("No function \"%s\" in specified context."),
1019 $$ = SYMBOL_BLOCK_VALUE (tem); }
1022 variable: name_not_typename ENTRY
1023 { struct symbol *sym = $1.sym.symbol;
1025 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
1026 || !symbol_read_needs_frame (sym))
1027 error (_("@entry can be used only for function "
1028 "parameters, not for \"%s\""),
1029 copy_name ($1.stoken).c_str ());
1031 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
1032 write_exp_elt_sym (pstate, sym);
1033 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
1037 variable: block COLONCOLON name
1039 std::string copy = copy_name ($3);
1040 struct block_symbol sym
1041 = lookup_symbol (copy.c_str (), $1,
1044 if (sym.symbol == 0)
1045 error (_("No symbol \"%s\" in specified context."),
1047 if (symbol_read_needs_frame (sym.symbol))
1048 pstate->block_tracker->update (sym);
1050 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1051 write_exp_elt_block (pstate, sym.block);
1052 write_exp_elt_sym (pstate, sym.symbol);
1053 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
1056 qualified_name: TYPENAME COLONCOLON name
1058 struct type *type = $1.type;
1059 type = check_typedef (type);
1060 if (!type_aggregate_p (type))
1061 error (_("`%s' is not defined as an aggregate type."),
1062 TYPE_SAFE_NAME (type));
1064 write_exp_elt_opcode (pstate, OP_SCOPE);
1065 write_exp_elt_type (pstate, type);
1066 write_exp_string (pstate, $3);
1067 write_exp_elt_opcode (pstate, OP_SCOPE);
1069 | TYPENAME COLONCOLON '~' name
1071 struct type *type = $1.type;
1072 struct stoken tmp_token;
1075 type = check_typedef (type);
1076 if (!type_aggregate_p (type))
1077 error (_("`%s' is not defined as an aggregate type."),
1078 TYPE_SAFE_NAME (type));
1079 buf = (char *) alloca ($4.length + 2);
1080 tmp_token.ptr = buf;
1081 tmp_token.length = $4.length + 1;
1083 memcpy (buf+1, $4.ptr, $4.length);
1084 buf[tmp_token.length] = 0;
1086 /* Check for valid destructor name. */
1087 destructor_name_p (tmp_token.ptr, $1.type);
1088 write_exp_elt_opcode (pstate, OP_SCOPE);
1089 write_exp_elt_type (pstate, type);
1090 write_exp_string (pstate, tmp_token);
1091 write_exp_elt_opcode (pstate, OP_SCOPE);
1093 | TYPENAME COLONCOLON name COLONCOLON name
1095 std::string copy = copy_name ($3);
1096 error (_("No type \"%s\" within class "
1097 "or namespace \"%s\"."),
1098 copy.c_str (), TYPE_SAFE_NAME ($1.type));
1102 variable: qualified_name
1103 | COLONCOLON name_not_typename
1105 std::string name = copy_name ($2.stoken);
1107 struct bound_minimal_symbol msymbol;
1110 = lookup_symbol (name.c_str (),
1111 (const struct block *) NULL,
1112 VAR_DOMAIN, NULL).symbol;
1115 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1116 write_exp_elt_block (pstate, NULL);
1117 write_exp_elt_sym (pstate, sym);
1118 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1122 msymbol = lookup_bound_minimal_symbol (name.c_str ());
1123 if (msymbol.minsym != NULL)
1124 write_exp_msymbol (pstate, msymbol);
1125 else if (!have_full_symbols () && !have_partial_symbols ())
1126 error (_("No symbol table is loaded. Use the \"file\" command."));
1128 error (_("No symbol \"%s\" in current context."),
1133 variable: name_not_typename
1134 { struct block_symbol sym = $1.sym;
1138 if (symbol_read_needs_frame (sym.symbol))
1139 pstate->block_tracker->update (sym);
1141 /* If we found a function, see if it's
1142 an ifunc resolver that has the same
1143 address as the ifunc symbol itself.
1144 If so, prefer the ifunc symbol. */
1146 bound_minimal_symbol resolver
1147 = find_gnu_ifunc (sym.symbol);
1148 if (resolver.minsym != NULL)
1149 write_exp_msymbol (pstate, resolver);
1152 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1153 write_exp_elt_block (pstate, sym.block);
1154 write_exp_elt_sym (pstate, sym.symbol);
1155 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1158 else if ($1.is_a_field_of_this)
1160 /* C++: it hangs off of `this'. Must
1161 not inadvertently convert from a method call
1163 pstate->block_tracker->update (sym);
1164 write_exp_elt_opcode (pstate, OP_THIS);
1165 write_exp_elt_opcode (pstate, OP_THIS);
1166 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1167 write_exp_string (pstate, $1.stoken);
1168 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1172 std::string arg = copy_name ($1.stoken);
1174 bound_minimal_symbol msymbol
1175 = lookup_bound_minimal_symbol (arg.c_str ());
1176 if (msymbol.minsym == NULL)
1178 if (!have_full_symbols () && !have_partial_symbols ())
1179 error (_("No symbol table is loaded. Use the \"file\" command."));
1181 error (_("No symbol \"%s\" in current context."),
1185 /* This minsym might be an alias for
1186 another function. See if we can find
1187 the debug symbol for the target, and
1188 if so, use it instead, since it has
1189 return type / prototype info. This
1190 is important for example for "p
1191 *__errno_location()". */
1192 symbol *alias_target
1193 = ((msymbol.minsym->type != mst_text_gnu_ifunc
1194 && msymbol.minsym->type != mst_data_gnu_ifunc)
1195 ? find_function_alias_target (msymbol)
1197 if (alias_target != NULL)
1199 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1201 (pstate, SYMBOL_BLOCK_VALUE (alias_target));
1202 write_exp_elt_sym (pstate, alias_target);
1203 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1206 write_exp_msymbol (pstate, msymbol);
1211 const_or_volatile: const_or_volatile_noopt
1217 { cpstate->type_stack.insert (tp_const); }
1219 { cpstate->type_stack.insert (tp_volatile); }
1221 { cpstate->type_stack.insert (tp_atomic); }
1223 { cpstate->type_stack.insert (tp_restrict); }
1226 cpstate->type_stack.insert (pstate,
1227 copy_name ($2.stoken).c_str ());
1231 qualifier_seq_noopt:
1233 | qualifier_seq single_qualifier
1243 { cpstate->type_stack.insert (tp_pointer); }
1246 { cpstate->type_stack.insert (tp_pointer); }
1249 { cpstate->type_stack.insert (tp_reference); }
1251 { cpstate->type_stack.insert (tp_reference); }
1253 { cpstate->type_stack.insert (tp_rvalue_reference); }
1254 | ANDAND ptr_operator
1255 { cpstate->type_stack.insert (tp_rvalue_reference); }
1258 ptr_operator_ts: ptr_operator
1260 $$ = cpstate->type_stack.create ();
1261 cpstate->type_stacks.emplace_back ($$);
1265 abs_decl: ptr_operator_ts direct_abs_decl
1266 { $$ = $2->append ($1); }
1271 direct_abs_decl: '(' abs_decl ')'
1273 | direct_abs_decl array_mod
1275 cpstate->type_stack.push ($1);
1276 cpstate->type_stack.push ($2);
1277 cpstate->type_stack.push (tp_array);
1278 $$ = cpstate->type_stack.create ();
1279 cpstate->type_stacks.emplace_back ($$);
1283 cpstate->type_stack.push ($1);
1284 cpstate->type_stack.push (tp_array);
1285 $$ = cpstate->type_stack.create ();
1286 cpstate->type_stacks.emplace_back ($$);
1289 | direct_abs_decl func_mod
1291 cpstate->type_stack.push ($1);
1292 cpstate->type_stack.push ($2);
1293 $$ = cpstate->type_stack.create ();
1294 cpstate->type_stacks.emplace_back ($$);
1298 cpstate->type_stack.push ($1);
1299 $$ = cpstate->type_stack.create ();
1300 cpstate->type_stacks.emplace_back ($$);
1310 | OBJC_LBRAC INT ']'
1316 $$ = new std::vector<struct type *>;
1317 cpstate->type_lists.emplace_back ($$);
1319 | '(' parameter_typelist ')'
1323 /* We used to try to recognize pointer to member types here, but
1324 that didn't work (shift/reduce conflicts meant that these rules never
1325 got executed). The problem is that
1326 int (foo::bar::baz::bizzle)
1327 is a function type but
1328 int (foo::bar::baz::bizzle::*)
1329 is a pointer to member type. Stroustrup loses again! */
1334 /* Implements (approximately): (type-qualifier)* type-specifier.
1336 When type-specifier is only ever a single word, like 'float' then these
1337 arrive as pre-built TYPENAME tokens thanks to the classify_name
1338 function. However, when a type-specifier can contain multiple words,
1339 for example 'double' can appear as just 'double' or 'long double', and
1340 similarly 'long' can appear as just 'long' or in 'long double', then
1341 these type-specifiers are parsed into their own tokens in the function
1342 lex_one_token and the ident_tokens array. These separate tokens are all
1348 { $$ = lookup_signed_typename (pstate->language (),
1351 { $$ = lookup_signed_typename (pstate->language (),
1354 { $$ = lookup_signed_typename (pstate->language (),
1357 { $$ = lookup_signed_typename (pstate->language (),
1359 | LONG SIGNED_KEYWORD INT_KEYWORD
1360 { $$ = lookup_signed_typename (pstate->language (),
1362 | LONG SIGNED_KEYWORD
1363 { $$ = lookup_signed_typename (pstate->language (),
1365 | SIGNED_KEYWORD LONG INT_KEYWORD
1366 { $$ = lookup_signed_typename (pstate->language (),
1368 | UNSIGNED LONG INT_KEYWORD
1369 { $$ = lookup_unsigned_typename (pstate->language (),
1371 | LONG UNSIGNED INT_KEYWORD
1372 { $$ = lookup_unsigned_typename (pstate->language (),
1375 { $$ = lookup_unsigned_typename (pstate->language (),
1378 { $$ = lookup_signed_typename (pstate->language (),
1380 | LONG LONG INT_KEYWORD
1381 { $$ = lookup_signed_typename (pstate->language (),
1383 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1384 { $$ = lookup_signed_typename (pstate->language (),
1386 | LONG LONG SIGNED_KEYWORD
1387 { $$ = lookup_signed_typename (pstate->language (),
1389 | SIGNED_KEYWORD LONG LONG
1390 { $$ = lookup_signed_typename (pstate->language (),
1392 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1393 { $$ = lookup_signed_typename (pstate->language (),
1395 | UNSIGNED LONG LONG
1396 { $$ = lookup_unsigned_typename (pstate->language (),
1398 | UNSIGNED LONG LONG INT_KEYWORD
1399 { $$ = lookup_unsigned_typename (pstate->language (),
1401 | LONG LONG UNSIGNED
1402 { $$ = lookup_unsigned_typename (pstate->language (),
1404 | LONG LONG UNSIGNED INT_KEYWORD
1405 { $$ = lookup_unsigned_typename (pstate->language (),
1408 { $$ = lookup_signed_typename (pstate->language (),
1410 | SHORT SIGNED_KEYWORD INT_KEYWORD
1411 { $$ = lookup_signed_typename (pstate->language (),
1413 | SHORT SIGNED_KEYWORD
1414 { $$ = lookup_signed_typename (pstate->language (),
1416 | UNSIGNED SHORT INT_KEYWORD
1417 { $$ = lookup_unsigned_typename (pstate->language (),
1420 { $$ = lookup_unsigned_typename (pstate->language (),
1422 | SHORT UNSIGNED INT_KEYWORD
1423 { $$ = lookup_unsigned_typename (pstate->language (),
1426 { $$ = lookup_typename (pstate->language (),
1430 | LONG DOUBLE_KEYWORD
1431 { $$ = lookup_typename (pstate->language (),
1437 = lookup_struct (copy_name ($2).c_str (),
1438 pstate->expression_context_block);
1442 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1446 | STRUCT name COMPLETE
1448 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1453 { $$ = lookup_struct
1454 (copy_name ($2).c_str (),
1455 pstate->expression_context_block);
1459 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1463 | CLASS name COMPLETE
1465 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1471 = lookup_union (copy_name ($2).c_str (),
1472 pstate->expression_context_block);
1476 pstate->mark_completion_tag (TYPE_CODE_UNION,
1480 | UNION name COMPLETE
1482 pstate->mark_completion_tag (TYPE_CODE_UNION,
1487 { $$ = lookup_enum (copy_name ($2).c_str (),
1488 pstate->expression_context_block);
1492 pstate->mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1495 | ENUM name COMPLETE
1497 pstate->mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1501 | UNSIGNED type_name
1502 { $$ = lookup_unsigned_typename (pstate->language (),
1503 TYPE_NAME($2.type)); }
1505 { $$ = lookup_unsigned_typename (pstate->language (),
1507 | SIGNED_KEYWORD type_name
1508 { $$ = lookup_signed_typename (pstate->language (),
1509 TYPE_NAME($2.type)); }
1511 { $$ = lookup_signed_typename (pstate->language (),
1513 /* It appears that this rule for templates is never
1514 reduced; template recognition happens by lookahead
1515 in the token processing code in yylex. */
1516 | TEMPLATE name '<' type '>'
1517 { $$ = lookup_template_type
1518 (copy_name($2).c_str (), $4,
1519 pstate->expression_context_block);
1521 | qualifier_seq_noopt typebase
1522 { $$ = cpstate->type_stack.follow_types ($2); }
1523 | typebase qualifier_seq_noopt
1524 { $$ = cpstate->type_stack.follow_types ($1); }
1530 $$.stoken.ptr = "int";
1531 $$.stoken.length = 3;
1532 $$.type = lookup_signed_typename (pstate->language (),
1537 $$.stoken.ptr = "long";
1538 $$.stoken.length = 4;
1539 $$.type = lookup_signed_typename (pstate->language (),
1544 $$.stoken.ptr = "short";
1545 $$.stoken.length = 5;
1546 $$.type = lookup_signed_typename (pstate->language (),
1553 { check_parameter_typelist ($1); }
1554 | nonempty_typelist ',' DOTDOTDOT
1556 $1->push_back (NULL);
1557 check_parameter_typelist ($1);
1565 std::vector<struct type *> *typelist
1566 = new std::vector<struct type *>;
1567 cpstate->type_lists.emplace_back (typelist);
1569 typelist->push_back ($1);
1572 | nonempty_typelist ',' type
1582 cpstate->type_stack.push ($2);
1583 $$ = cpstate->type_stack.follow_types ($1);
1587 conversion_type_id: typebase conversion_declarator
1588 { $$ = cpstate->type_stack.follow_types ($1); }
1591 conversion_declarator: /* Nothing. */
1592 | ptr_operator conversion_declarator
1595 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1596 | VOLATILE_KEYWORD CONST_KEYWORD
1599 const_or_volatile_noopt: const_and_volatile
1600 { cpstate->type_stack.insert (tp_const);
1601 cpstate->type_stack.insert (tp_volatile);
1604 { cpstate->type_stack.insert (tp_const); }
1606 { cpstate->type_stack.insert (tp_volatile); }
1610 { $$ = operator_stoken (" new"); }
1612 { $$ = operator_stoken (" delete"); }
1613 | OPERATOR NEW '[' ']'
1614 { $$ = operator_stoken (" new[]"); }
1615 | OPERATOR DELETE '[' ']'
1616 { $$ = operator_stoken (" delete[]"); }
1617 | OPERATOR NEW OBJC_LBRAC ']'
1618 { $$ = operator_stoken (" new[]"); }
1619 | OPERATOR DELETE OBJC_LBRAC ']'
1620 { $$ = operator_stoken (" delete[]"); }
1622 { $$ = operator_stoken ("+"); }
1624 { $$ = operator_stoken ("-"); }
1626 { $$ = operator_stoken ("*"); }
1628 { $$ = operator_stoken ("/"); }
1630 { $$ = operator_stoken ("%"); }
1632 { $$ = operator_stoken ("^"); }
1634 { $$ = operator_stoken ("&"); }
1636 { $$ = operator_stoken ("|"); }
1638 { $$ = operator_stoken ("~"); }
1640 { $$ = operator_stoken ("!"); }
1642 { $$ = operator_stoken ("="); }
1644 { $$ = operator_stoken ("<"); }
1646 { $$ = operator_stoken (">"); }
1647 | OPERATOR ASSIGN_MODIFY
1648 { const char *op = " unknown";
1672 case BINOP_BITWISE_IOR:
1675 case BINOP_BITWISE_AND:
1678 case BINOP_BITWISE_XOR:
1685 $$ = operator_stoken (op);
1688 { $$ = operator_stoken ("<<"); }
1690 { $$ = operator_stoken (">>"); }
1692 { $$ = operator_stoken ("=="); }
1694 { $$ = operator_stoken ("!="); }
1696 { $$ = operator_stoken ("<="); }
1698 { $$ = operator_stoken (">="); }
1700 { $$ = operator_stoken ("&&"); }
1702 { $$ = operator_stoken ("||"); }
1703 | OPERATOR INCREMENT
1704 { $$ = operator_stoken ("++"); }
1705 | OPERATOR DECREMENT
1706 { $$ = operator_stoken ("--"); }
1708 { $$ = operator_stoken (","); }
1709 | OPERATOR ARROW_STAR
1710 { $$ = operator_stoken ("->*"); }
1712 { $$ = operator_stoken ("->"); }
1714 { $$ = operator_stoken ("()"); }
1716 { $$ = operator_stoken ("[]"); }
1717 | OPERATOR OBJC_LBRAC ']'
1718 { $$ = operator_stoken ("[]"); }
1719 | OPERATOR conversion_type_id
1722 c_print_type ($2, NULL, &buf, -1, 0,
1723 &type_print_raw_options);
1725 /* This also needs canonicalization. */
1727 = cp_canonicalize_string (buf.c_str ());
1729 canon = std::move (buf.string ());
1730 $$ = operator_stoken ((" " + canon).c_str ());
1734 /* This rule exists in order to allow some tokens that would not normally
1735 match the 'name' rule to appear as fields within a struct. The example
1736 that initially motivated this was the RISC-V target which models the
1737 floating point registers as a union with fields called 'float' and
1738 'double'. The 'float' string becomes a TYPENAME token and can appear
1739 anywhere a 'name' can, however 'double' is its own token,
1740 DOUBLE_KEYWORD, and doesn't match the 'name' rule.*/
1743 | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
1744 | INT_KEYWORD { $$ = typename_stoken ("int"); }
1745 | LONG { $$ = typename_stoken ("long"); }
1746 | SHORT { $$ = typename_stoken ("short"); }
1747 | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
1748 | UNSIGNED { $$ = typename_stoken ("unsigned"); }
1751 name : NAME { $$ = $1.stoken; }
1752 | BLOCKNAME { $$ = $1.stoken; }
1753 | TYPENAME { $$ = $1.stoken; }
1754 | NAME_OR_INT { $$ = $1.stoken; }
1755 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1759 name_not_typename : NAME
1761 /* These would be useful if name_not_typename was useful, but it is just
1762 a fake for "variable", so these cause reduce/reduce conflicts because
1763 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1764 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1765 context where only a name could occur, this might be useful.
1770 struct field_of_this_result is_a_field_of_this;
1774 = lookup_symbol ($1.ptr,
1775 pstate->expression_context_block,
1777 &is_a_field_of_this);
1778 $$.is_a_field_of_this
1779 = is_a_field_of_this.type != NULL;
1786 /* Like write_exp_string, but prepends a '~'. */
1789 write_destructor_name (struct parser_state *par_state, struct stoken token)
1791 char *copy = (char *) alloca (token.length + 1);
1794 memcpy (©[1], token.ptr, token.length);
1799 write_exp_string (par_state, token);
1802 /* Returns a stoken of the operator name given by OP (which does not
1803 include the string "operator"). */
1805 static struct stoken
1806 operator_stoken (const char *op)
1808 struct stoken st = { NULL, 0 };
1811 st.length = CP_OPERATOR_LEN + strlen (op);
1812 buf = (char *) malloc (st.length + 1);
1813 strcpy (buf, CP_OPERATOR_STR);
1817 /* The toplevel (c_parse) will free the memory allocated here. */
1818 cpstate->strings.emplace_back (buf);
1822 /* Returns a stoken of the type named TYPE. */
1824 static struct stoken
1825 typename_stoken (const char *type)
1827 struct stoken st = { type, 0 };
1828 st.length = strlen (type);
1832 /* Return true if the type is aggregate-like. */
1835 type_aggregate_p (struct type *type)
1837 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
1838 || TYPE_CODE (type) == TYPE_CODE_UNION
1839 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE
1840 || (TYPE_CODE (type) == TYPE_CODE_ENUM
1841 && TYPE_DECLARED_CLASS (type)));
1844 /* Validate a parameter typelist. */
1847 check_parameter_typelist (std::vector<struct type *> *params)
1852 for (ix = 0; ix < params->size (); ++ix)
1854 type = (*params)[ix];
1855 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1859 if (params->size () == 1)
1864 error (_("parameter types following 'void'"));
1867 error (_("'void' invalid as parameter type"));
1872 /* Take care of parsing a number (anything that starts with a digit).
1873 Set yylval and return the token type; update lexptr.
1874 LEN is the number of characters in it. */
1876 /*** Needs some error checking for the float case ***/
1879 parse_number (struct parser_state *par_state,
1880 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1888 int base = input_radix;
1891 /* Number of "L" suffixes encountered. */
1894 /* Imaginary number. */
1895 bool imaginary_p = false;
1897 /* We have found a "L" or "U" (or "i") suffix. */
1898 int found_suffix = 0;
1901 struct type *signed_type;
1902 struct type *unsigned_type;
1905 p = (char *) alloca (len);
1906 memcpy (p, buf, len);
1910 if (len >= 1 && p[len - 1] == 'i')
1916 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
1917 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1919 putithere->typed_val_float.type
1920 = parse_type (par_state)->builtin_decfloat;
1923 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1925 putithere->typed_val_float.type
1926 = parse_type (par_state)->builtin_decdouble;
1929 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1931 putithere->typed_val_float.type
1932 = parse_type (par_state)->builtin_declong;
1935 /* Handle suffixes: 'f' for float, 'l' for long double. */
1936 else if (len >= 1 && TOLOWER (p[len - 1]) == 'f')
1938 putithere->typed_val_float.type
1939 = parse_type (par_state)->builtin_float;
1942 else if (len >= 1 && TOLOWER (p[len - 1]) == 'l')
1944 putithere->typed_val_float.type
1945 = parse_type (par_state)->builtin_long_double;
1948 /* Default type for floating-point literals is double. */
1951 putithere->typed_val_float.type
1952 = parse_type (par_state)->builtin_double;
1955 if (!parse_float (p, len,
1956 putithere->typed_val_float.type,
1957 putithere->typed_val_float.val))
1961 putithere->typed_val_float.type
1962 = init_complex_type (nullptr, putithere->typed_val_float.type);
1964 return imaginary_p ? COMPLEX_FLOAT : FLOAT;
1967 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1968 if (p[0] == '0' && len > 1)
2011 if (c >= 'A' && c <= 'Z')
2013 if (c != 'l' && c != 'u' && c != 'i')
2015 if (c >= '0' && c <= '9')
2023 if (base > 10 && c >= 'a' && c <= 'f')
2027 n += i = c - 'a' + 10;
2045 return ERROR; /* Char not a digit */
2048 return ERROR; /* Invalid digit in this base */
2050 /* Portably test for overflow (only works for nonzero values, so make
2051 a second check for zero). FIXME: Can't we just make n and prevn
2052 unsigned and avoid this? */
2053 if (c != 'l' && c != 'u' && c != 'i' && (prevn >= n) && n != 0)
2054 unsigned_p = 1; /* Try something unsigned */
2056 /* Portably test for unsigned overflow.
2057 FIXME: This check is wrong; for example it doesn't find overflow
2058 on 0x123456789 when LONGEST is 32 bits. */
2059 if (c != 'l' && c != 'u' && c != 'i' && n != 0)
2061 if (unsigned_p && prevn >= n)
2062 error (_("Numeric constant too large."));
2067 /* An integer constant is an int, a long, or a long long. An L
2068 suffix forces it to be long; an LL suffix forces it to be long
2069 long. If not forced to a larger size, it gets the first type of
2070 the above that it fits in. To figure out whether it fits, we
2071 shift it right and see whether anything remains. Note that we
2072 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
2073 operation, because many compilers will warn about such a shift
2074 (which always produces a zero result). Sometimes gdbarch_int_bit
2075 or gdbarch_long_bit will be that big, sometimes not. To deal with
2076 the case where it is we just always shift the value more than
2077 once, with fewer bits each time. */
2081 && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0)
2084 = ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
2086 /* A large decimal (not hex or octal) constant (between INT_MAX
2087 and UINT_MAX) is a long or unsigned long, according to ANSI,
2088 never an unsigned int, but this code treats it as unsigned
2089 int. This probably should be fixed. GCC gives a warning on
2092 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
2093 signed_type = parse_type (par_state)->builtin_int;
2095 else if (long_p <= 1
2096 && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0)
2099 = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1);
2100 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
2101 signed_type = parse_type (par_state)->builtin_long;
2106 if (sizeof (ULONGEST) * HOST_CHAR_BIT
2107 < gdbarch_long_long_bit (par_state->gdbarch ()))
2108 /* A long long does not fit in a LONGEST. */
2109 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
2111 shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1);
2112 high_bit = (ULONGEST) 1 << shift;
2113 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
2114 signed_type = parse_type (par_state)->builtin_long_long;
2117 putithere->typed_val_int.val = n;
2119 /* If the high bit of the worked out type is set then this number
2120 has to be unsigned. */
2122 if (unsigned_p || (n & high_bit))
2124 putithere->typed_val_int.type = unsigned_type;
2128 putithere->typed_val_int.type = signed_type;
2132 putithere->typed_val_int.type
2133 = init_complex_type (nullptr, putithere->typed_val_int.type);
2135 return imaginary_p ? COMPLEX_INT : INT;
2138 /* Temporary obstack used for holding strings. */
2139 static struct obstack tempbuf;
2140 static int tempbuf_init;
2142 /* Parse a C escape sequence. The initial backslash of the sequence
2143 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2144 last character of the sequence. If OUTPUT is not NULL, the
2145 translated form of the escape sequence will be written there. If
2146 OUTPUT is NULL, no output is written and the call will only affect
2147 *PTR. If an escape sequence is expressed in target bytes, then the
2148 entire sequence will simply be copied to OUTPUT. Return 1 if any
2149 character was emitted, 0 otherwise. */
2152 c_parse_escape (const char **ptr, struct obstack *output)
2154 const char *tokptr = *ptr;
2157 /* Some escape sequences undergo character set conversion. Those we
2161 /* Hex escapes do not undergo character set conversion, so keep
2162 the escape sequence for later. */
2165 obstack_grow_str (output, "\\x");
2167 if (!ISXDIGIT (*tokptr))
2168 error (_("\\x escape without a following hex digit"));
2169 while (ISXDIGIT (*tokptr))
2172 obstack_1grow (output, *tokptr);
2177 /* Octal escapes do not undergo character set conversion, so
2178 keep the escape sequence for later. */
2190 obstack_grow_str (output, "\\");
2192 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
2196 obstack_1grow (output, *tokptr);
2202 /* We handle UCNs later. We could handle them here, but that
2203 would mean a spurious error in the case where the UCN could
2204 be converted to the target charset but not the host
2210 int i, len = c == 'U' ? 8 : 4;
2213 obstack_1grow (output, '\\');
2214 obstack_1grow (output, *tokptr);
2217 if (!ISXDIGIT (*tokptr))
2218 error (_("\\%c escape without a following hex digit"), c);
2219 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
2222 obstack_1grow (output, *tokptr);
2228 /* We must pass backslash through so that it does not
2229 cause quoting during the second expansion. */
2232 obstack_grow_str (output, "\\\\");
2236 /* Escapes which undergo conversion. */
2239 obstack_1grow (output, '\a');
2244 obstack_1grow (output, '\b');
2249 obstack_1grow (output, '\f');
2254 obstack_1grow (output, '\n');
2259 obstack_1grow (output, '\r');
2264 obstack_1grow (output, '\t');
2269 obstack_1grow (output, '\v');
2273 /* GCC extension. */
2276 obstack_1grow (output, HOST_ESCAPE_CHAR);
2280 /* Backslash-newline expands to nothing at all. */
2286 /* A few escapes just expand to the character itself. */
2290 /* GCC extensions. */
2295 /* Unrecognized escapes turn into the character itself. */
2298 obstack_1grow (output, *tokptr);
2306 /* Parse a string or character literal from TOKPTR. The string or
2307 character may be wide or unicode. *OUTPTR is set to just after the
2308 end of the literal in the input string. The resulting token is
2309 stored in VALUE. This returns a token value, either STRING or
2310 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2311 number of host characters in the literal. */
2314 parse_string_or_char (const char *tokptr, const char **outptr,
2315 struct typed_stoken *value, int *host_chars)
2321 /* Build the gdb internal form of the input string in tempbuf. Note
2322 that the buffer is null byte terminated *only* for the
2323 convenience of debugging gdb itself and printing the buffer
2324 contents when the buffer contains no embedded nulls. Gdb does
2325 not depend upon the buffer being null byte terminated, it uses
2326 the length string instead. This allows gdb to handle C strings
2327 (as well as strings in other languages) with embedded null
2333 obstack_free (&tempbuf, NULL);
2334 obstack_init (&tempbuf);
2336 /* Record the string type. */
2339 type = C_WIDE_STRING;
2342 else if (*tokptr == 'u')
2347 else if (*tokptr == 'U')
2352 else if (*tokptr == '@')
2354 /* An Objective C string. */
2362 /* Skip the quote. */
2376 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2378 else if (c == quote)
2382 obstack_1grow (&tempbuf, c);
2384 /* FIXME: this does the wrong thing with multi-byte host
2385 characters. We could use mbrlen here, but that would
2386 make "set host-charset" a bit less useful. */
2391 if (*tokptr != quote)
2394 error (_("Unterminated string in expression."));
2396 error (_("Unmatched single quote."));
2401 value->ptr = (char *) obstack_base (&tempbuf);
2402 value->length = obstack_object_size (&tempbuf);
2406 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2409 /* This is used to associate some attributes with a token. */
2413 /* If this bit is set, the token is C++-only. */
2417 /* If this bit is set, the token is C-only. */
2421 /* If this bit is set, the token is conditional: if there is a
2422 symbol of the same name, then the token is a symbol; otherwise,
2423 the token is a keyword. */
2427 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2433 enum exp_opcode opcode;
2437 static const struct token tokentab3[] =
2439 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2440 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2441 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2442 {"...", DOTDOTDOT, BINOP_END, 0}
2445 static const struct token tokentab2[] =
2447 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2448 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2449 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2450 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2451 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2452 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2453 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2454 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2455 {"++", INCREMENT, BINOP_END, 0},
2456 {"--", DECREMENT, BINOP_END, 0},
2457 {"->", ARROW, BINOP_END, 0},
2458 {"&&", ANDAND, BINOP_END, 0},
2459 {"||", OROR, BINOP_END, 0},
2460 /* "::" is *not* only C++: gdb overrides its meaning in several
2461 different ways, e.g., 'filename'::func, function::variable. */
2462 {"::", COLONCOLON, BINOP_END, 0},
2463 {"<<", LSH, BINOP_END, 0},
2464 {">>", RSH, BINOP_END, 0},
2465 {"==", EQUAL, BINOP_END, 0},
2466 {"!=", NOTEQUAL, BINOP_END, 0},
2467 {"<=", LEQ, BINOP_END, 0},
2468 {">=", GEQ, BINOP_END, 0},
2469 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2472 /* Identifier-like tokens. Only type-specifiers than can appear in
2473 multi-word type names (for example 'double' can appear in 'long
2474 double') need to be listed here. type-specifiers that are only ever
2475 single word (like 'float') are handled by the classify_name function. */
2476 static const struct token ident_tokens[] =
2478 {"unsigned", UNSIGNED, OP_NULL, 0},
2479 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2480 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2481 {"struct", STRUCT, OP_NULL, 0},
2482 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2483 {"sizeof", SIZEOF, OP_NULL, 0},
2484 {"_Alignof", ALIGNOF, OP_NULL, 0},
2485 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
2486 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2487 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2488 {"class", CLASS, OP_NULL, FLAG_CXX},
2489 {"union", UNION, OP_NULL, 0},
2490 {"short", SHORT, OP_NULL, 0},
2491 {"const", CONST_KEYWORD, OP_NULL, 0},
2492 {"restrict", RESTRICT, OP_NULL, FLAG_C | FLAG_SHADOW},
2493 {"__restrict__", RESTRICT, OP_NULL, 0},
2494 {"__restrict", RESTRICT, OP_NULL, 0},
2495 {"_Atomic", ATOMIC, OP_NULL, 0},
2496 {"enum", ENUM, OP_NULL, 0},
2497 {"long", LONG, OP_NULL, 0},
2498 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2499 {"int", INT_KEYWORD, OP_NULL, 0},
2500 {"new", NEW, OP_NULL, FLAG_CXX},
2501 {"delete", DELETE, OP_NULL, FLAG_CXX},
2502 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2504 {"and", ANDAND, BINOP_END, FLAG_CXX},
2505 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2506 {"bitand", '&', OP_NULL, FLAG_CXX},
2507 {"bitor", '|', OP_NULL, FLAG_CXX},
2508 {"compl", '~', OP_NULL, FLAG_CXX},
2509 {"not", '!', OP_NULL, FLAG_CXX},
2510 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2511 {"or", OROR, BINOP_END, FLAG_CXX},
2512 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2513 {"xor", '^', OP_NULL, FLAG_CXX},
2514 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2516 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2517 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2518 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2519 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2521 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2522 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2523 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2524 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2525 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2527 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2532 scan_macro_expansion (char *expansion)
2536 /* We'd better not be trying to push the stack twice. */
2537 gdb_assert (! cpstate->macro_original_text);
2539 /* Copy to the obstack, and then free the intermediate
2541 copy = obstack_strdup (&cpstate->expansion_obstack, expansion);
2544 /* Save the old lexptr value, so we can return to it when we're done
2545 parsing the expanded text. */
2546 cpstate->macro_original_text = pstate->lexptr;
2547 pstate->lexptr = copy;
2551 scanning_macro_expansion (void)
2553 return cpstate->macro_original_text != 0;
2557 finished_macro_expansion (void)
2559 /* There'd better be something to pop back to. */
2560 gdb_assert (cpstate->macro_original_text);
2562 /* Pop back to the original text. */
2563 pstate->lexptr = cpstate->macro_original_text;
2564 cpstate->macro_original_text = 0;
2567 /* Return true iff the token represents a C++ cast operator. */
2570 is_cast_operator (const char *token, int len)
2572 return (! strncmp (token, "dynamic_cast", len)
2573 || ! strncmp (token, "static_cast", len)
2574 || ! strncmp (token, "reinterpret_cast", len)
2575 || ! strncmp (token, "const_cast", len));
2578 /* The scope used for macro expansion. */
2579 static struct macro_scope *expression_macro_scope;
2581 /* This is set if a NAME token appeared at the very end of the input
2582 string, with no whitespace separating the name from the EOF. This
2583 is used only when parsing to do field name completion. */
2584 static int saw_name_at_eof;
2586 /* This is set if the previously-returned token was a structure
2587 operator -- either '.' or ARROW. */
2588 static bool last_was_structop;
2590 /* Depth of parentheses. */
2591 static int paren_depth;
2593 /* Read one token, getting characters through lexptr. */
2596 lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
2601 const char *tokstart;
2602 bool saw_structop = last_was_structop;
2604 last_was_structop = false;
2605 *is_quoted_name = false;
2609 /* Check if this is a macro invocation that we need to expand. */
2610 if (! scanning_macro_expansion ())
2612 char *expanded = macro_expand_next (&pstate->lexptr,
2613 standard_macro_lookup,
2614 expression_macro_scope);
2617 scan_macro_expansion (expanded);
2620 pstate->prev_lexptr = pstate->lexptr;
2622 tokstart = pstate->lexptr;
2623 /* See if it is a special token of length 3. */
2624 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2625 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
2627 if ((tokentab3[i].flags & FLAG_CXX) != 0
2628 && par_state->language ()->la_language != language_cplus)
2630 gdb_assert ((tokentab3[i].flags & FLAG_C) == 0);
2632 pstate->lexptr += 3;
2633 yylval.opcode = tokentab3[i].opcode;
2634 return tokentab3[i].token;
2637 /* See if it is a special token of length 2. */
2638 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2639 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
2641 if ((tokentab2[i].flags & FLAG_CXX) != 0
2642 && par_state->language ()->la_language != language_cplus)
2644 gdb_assert ((tokentab2[i].flags & FLAG_C) == 0);
2646 pstate->lexptr += 2;
2647 yylval.opcode = tokentab2[i].opcode;
2648 if (tokentab2[i].token == ARROW)
2649 last_was_structop = 1;
2650 return tokentab2[i].token;
2653 switch (c = *tokstart)
2656 /* If we were just scanning the result of a macro expansion,
2657 then we need to resume scanning the original text.
2658 If we're parsing for field name completion, and the previous
2659 token allows such completion, return a COMPLETE token.
2660 Otherwise, we were already scanning the original text, and
2661 we're really done. */
2662 if (scanning_macro_expansion ())
2664 finished_macro_expansion ();
2667 else if (saw_name_at_eof)
2669 saw_name_at_eof = 0;
2672 else if (par_state->parse_completion && saw_structop)
2687 if (par_state->language ()->la_language == language_objc
2694 if (paren_depth == 0)
2701 if (pstate->comma_terminates
2703 && ! scanning_macro_expansion ())
2709 /* Might be a floating point number. */
2710 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
2712 last_was_structop = true;
2713 goto symbol; /* Nope, must be a symbol. */
2728 /* It's a number. */
2729 int got_dot = 0, got_e = 0, toktype;
2730 const char *p = tokstart;
2731 int hex = input_radix > 10;
2733 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2738 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2746 /* This test includes !hex because 'e' is a valid hex digit
2747 and thus does not indicate a floating point number when
2748 the radix is hex. */
2749 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2750 got_dot = got_e = 1;
2751 /* This test does not include !hex, because a '.' always indicates
2752 a decimal floating point number regardless of the radix. */
2753 else if (!got_dot && *p == '.')
2755 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2756 && (*p == '-' || *p == '+'))
2757 /* This is the sign of the exponent, not the end of the
2760 /* We will take any letters or digits. parse_number will
2761 complain if past the radix, or if L or U are not final. */
2762 else if ((*p < '0' || *p > '9')
2763 && ((*p < 'a' || *p > 'z')
2764 && (*p < 'A' || *p > 'Z')))
2767 toktype = parse_number (par_state, tokstart, p - tokstart,
2768 got_dot|got_e, &yylval);
2769 if (toktype == ERROR)
2771 char *err_copy = (char *) alloca (p - tokstart + 1);
2773 memcpy (err_copy, tokstart, p - tokstart);
2774 err_copy[p - tokstart] = 0;
2775 error (_("Invalid number \"%s\"."), err_copy);
2783 const char *p = &tokstart[1];
2785 if (par_state->language ()->la_language == language_objc)
2787 size_t len = strlen ("selector");
2789 if (strncmp (p, "selector", len) == 0
2790 && (p[len] == '\0' || ISSPACE (p[len])))
2792 pstate->lexptr = p + len;
2799 while (ISSPACE (*p))
2801 size_t len = strlen ("entry");
2802 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
2805 pstate->lexptr = &p[len];
2834 if (tokstart[1] != '"' && tokstart[1] != '\'')
2843 int result = parse_string_or_char (tokstart, &pstate->lexptr,
2844 &yylval.tsval, &host_len);
2848 error (_("Empty character constant."));
2849 else if (host_len > 2 && c == '\'')
2852 namelen = pstate->lexptr - tokstart - 1;
2853 *is_quoted_name = true;
2857 else if (host_len > 1)
2858 error (_("Invalid character constant."));
2864 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
2865 /* We must have come across a bad character (e.g. ';'). */
2866 error (_("Invalid character '%c' in expression."), c);
2868 /* It's a name. See how long it is. */
2870 for (c = tokstart[namelen];
2871 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
2873 /* Template parameter lists are part of the name.
2874 FIXME: This mishandles `print $a<4&&$a>3'. */
2878 if (! is_cast_operator (tokstart, namelen))
2880 /* Scan ahead to get rest of the template specification. Note
2881 that we look ahead only when the '<' adjoins non-whitespace
2882 characters; for comparison expressions, e.g. "a < b > c",
2883 there must be spaces before the '<', etc. */
2884 const char *p = find_template_name_end (tokstart + namelen);
2887 namelen = p - tokstart;
2891 c = tokstart[++namelen];
2894 /* The token "if" terminates the expression and is NOT removed from
2895 the input stream. It doesn't count if it appears in the
2896 expansion of a macro. */
2898 && tokstart[0] == 'i'
2899 && tokstart[1] == 'f'
2900 && ! scanning_macro_expansion ())
2905 /* For the same reason (breakpoint conditions), "thread N"
2906 terminates the expression. "thread" could be an identifier, but
2907 an identifier is never followed by a number without intervening
2908 punctuation. "task" is similar. Handle abbreviations of these,
2909 similarly to breakpoint.c:find_condition_and_thread. */
2911 && (strncmp (tokstart, "thread", namelen) == 0
2912 || strncmp (tokstart, "task", namelen) == 0)
2913 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2914 && ! scanning_macro_expansion ())
2916 const char *p = tokstart + namelen + 1;
2918 while (*p == ' ' || *p == '\t')
2920 if (*p >= '0' && *p <= '9')
2924 pstate->lexptr += namelen;
2928 yylval.sval.ptr = tokstart;
2929 yylval.sval.length = namelen;
2931 /* Catch specific keywords. */
2932 std::string copy = copy_name (yylval.sval);
2933 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2934 if (copy == ident_tokens[i].oper)
2936 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2937 && par_state->language ()->la_language != language_cplus)
2939 if ((ident_tokens[i].flags & FLAG_C) != 0
2940 && par_state->language ()->la_language != language_c
2941 && par_state->language ()->la_language != language_objc)
2944 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2946 struct field_of_this_result is_a_field_of_this;
2948 if (lookup_symbol (copy.c_str (),
2949 pstate->expression_context_block,
2951 (par_state->language ()->la_language
2952 == language_cplus ? &is_a_field_of_this
2956 /* The keyword is shadowed. */
2961 /* It is ok to always set this, even though we don't always
2962 strictly need to. */
2963 yylval.opcode = ident_tokens[i].opcode;
2964 return ident_tokens[i].token;
2967 if (*tokstart == '$')
2968 return DOLLAR_VARIABLE;
2970 if (pstate->parse_completion && *pstate->lexptr == '\0')
2971 saw_name_at_eof = 1;
2973 yylval.ssym.stoken = yylval.sval;
2974 yylval.ssym.sym.symbol = NULL;
2975 yylval.ssym.sym.block = NULL;
2976 yylval.ssym.is_a_field_of_this = 0;
2980 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2981 struct token_and_value
2987 /* A FIFO of tokens that have been read but not yet returned to the
2989 static std::vector<token_and_value> token_fifo;
2991 /* Non-zero if the lexer should return tokens from the FIFO. */
2994 /* Temporary storage for c_lex; this holds symbol names as they are
2996 auto_obstack name_obstack;
2998 /* Classify a NAME token. The contents of the token are in `yylval'.
2999 Updates yylval and returns the new token type. BLOCK is the block
3000 in which lookups start; this can be NULL to mean the global scope.
3001 IS_QUOTED_NAME is non-zero if the name token was originally quoted
3002 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
3003 a structure operator -- either '.' or ARROW */
3006 classify_name (struct parser_state *par_state, const struct block *block,
3007 bool is_quoted_name, bool is_after_structop)
3009 struct block_symbol bsym;
3010 struct field_of_this_result is_a_field_of_this;
3012 std::string copy = copy_name (yylval.sval);
3014 /* Initialize this in case we *don't* use it in this call; that way
3015 we can refer to it unconditionally below. */
3016 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
3018 bsym = lookup_symbol (copy.c_str (), block, VAR_DOMAIN,
3019 par_state->language ()->la_name_of_this
3020 ? &is_a_field_of_this : NULL);
3022 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK)
3024 yylval.ssym.sym = bsym;
3025 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3028 else if (!bsym.symbol)
3030 /* If we found a field of 'this', we might have erroneously
3031 found a constructor where we wanted a type name. Handle this
3032 case by noticing that we found a constructor and then look up
3033 the type tag instead. */
3034 if (is_a_field_of_this.type != NULL
3035 && is_a_field_of_this.fn_field != NULL
3036 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
3039 struct field_of_this_result inner_is_a_field_of_this;
3041 bsym = lookup_symbol (copy.c_str (), block, STRUCT_DOMAIN,
3042 &inner_is_a_field_of_this);
3043 if (bsym.symbol != NULL)
3045 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3050 /* If we found a field on the "this" object, or we are looking
3051 up a field on a struct, then we want to prefer it over a
3052 filename. However, if the name was quoted, then it is better
3053 to check for a filename or a block, since this is the only
3054 way the user has of requiring the extension to be used. */
3055 if ((is_a_field_of_this.type == NULL && !is_after_structop)
3058 /* See if it's a file name. */
3059 struct symtab *symtab;
3061 symtab = lookup_symtab (copy.c_str ());
3064 yylval.bval = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab),
3071 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF)
3073 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3077 /* See if it's an ObjC classname. */
3078 if (par_state->language ()->la_language == language_objc && !bsym.symbol)
3080 CORE_ADDR Class = lookup_objc_class (par_state->gdbarch (),
3086 yylval.theclass.theclass = Class;
3087 sym = lookup_struct_typedef (copy.c_str (),
3088 par_state->expression_context_block, 1);
3090 yylval.theclass.type = SYMBOL_TYPE (sym);
3095 /* Input names that aren't symbols but ARE valid hex numbers, when
3096 the input radix permits them, can be names or numbers depending
3097 on the parse. Note we support radixes > 16 here. */
3099 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3100 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3102 YYSTYPE newlval; /* Its value is ignored. */
3103 int hextype = parse_number (par_state, copy.c_str (), yylval.sval.length,
3108 yylval.ssym.sym = bsym;
3109 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3114 /* Any other kind of symbol */
3115 yylval.ssym.sym = bsym;
3116 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3118 if (bsym.symbol == NULL
3119 && par_state->language ()->la_language == language_cplus
3120 && is_a_field_of_this.type == NULL
3121 && lookup_minimal_symbol (copy.c_str (), NULL, NULL).minsym == NULL)
3122 return UNKNOWN_CPP_NAME;
3127 /* Like classify_name, but used by the inner loop of the lexer, when a
3128 name might have already been seen. CONTEXT is the context type, or
3129 NULL if this is the first component of a name. */
3132 classify_inner_name (struct parser_state *par_state,
3133 const struct block *block, struct type *context)
3137 if (context == NULL)
3138 return classify_name (par_state, block, false, false);
3140 type = check_typedef (context);
3141 if (!type_aggregate_p (type))
3144 std::string copy = copy_name (yylval.ssym.stoken);
3145 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3146 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy.c_str (), block,
3149 /* If no symbol was found, search for a matching base class named
3150 COPY. This will allow users to enter qualified names of class members
3151 relative to the `this' pointer. */
3152 if (yylval.ssym.sym.symbol == NULL)
3154 struct type *base_type = cp_find_type_baseclass_by_name (type,
3157 if (base_type != NULL)
3159 yylval.tsym.type = base_type;
3166 switch (SYMBOL_CLASS (yylval.ssym.sym.symbol))
3170 /* cp_lookup_nested_symbol might have accidentally found a constructor
3171 named COPY when we really wanted a base class of the same name.
3172 Double-check this case by looking for a base class. */
3174 struct type *base_type
3175 = cp_find_type_baseclass_by_name (type, copy.c_str ());
3177 if (base_type != NULL)
3179 yylval.tsym.type = base_type;
3186 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
3192 internal_error (__FILE__, __LINE__, _("not reached"));
3195 /* The outer level of a two-level lexer. This calls the inner lexer
3196 to return tokens. It then either returns these tokens, or
3197 aggregates them into a larger token. This lets us work around a
3198 problem in our parsing approach, where the parser could not
3199 distinguish between qualified names and qualified types at the
3202 This approach is still not ideal, because it mishandles template
3203 types. See the comment in lex_one_token for an example. However,
3204 this is still an improvement over the earlier approach, and will
3205 suffice until we move to better parsing technology. */
3210 token_and_value current;
3211 int first_was_coloncolon, last_was_coloncolon;
3212 struct type *context_type = NULL;
3213 int last_to_examine, next_to_examine, checkpoint;
3214 const struct block *search_block;
3215 bool is_quoted_name, last_lex_was_structop;
3217 if (popping && !token_fifo.empty ())
3221 last_lex_was_structop = last_was_structop;
3223 /* Read the first token and decide what to do. Most of the
3224 subsequent code is C++-only; but also depends on seeing a "::" or
3226 current.token = lex_one_token (pstate, &is_quoted_name);
3227 if (current.token == NAME)
3228 current.token = classify_name (pstate, pstate->expression_context_block,
3229 is_quoted_name, last_lex_was_structop);
3230 if (pstate->language ()->la_language != language_cplus
3231 || (current.token != TYPENAME && current.token != COLONCOLON
3232 && current.token != FILENAME))
3233 return current.token;
3235 /* Read any sequence of alternating "::" and name-like tokens into
3237 current.value = yylval;
3238 token_fifo.push_back (current);
3239 last_was_coloncolon = current.token == COLONCOLON;
3244 /* We ignore quoted names other than the very first one.
3245 Subsequent ones do not have any special meaning. */
3246 current.token = lex_one_token (pstate, &ignore);
3247 current.value = yylval;
3248 token_fifo.push_back (current);
3250 if ((last_was_coloncolon && current.token != NAME)
3251 || (!last_was_coloncolon && current.token != COLONCOLON))
3253 last_was_coloncolon = !last_was_coloncolon;
3257 /* We always read one extra token, so compute the number of tokens
3258 to examine accordingly. */
3259 last_to_examine = token_fifo.size () - 2;
3260 next_to_examine = 0;
3262 current = token_fifo[next_to_examine];
3265 name_obstack.clear ();
3267 if (current.token == FILENAME)
3268 search_block = current.value.bval;
3269 else if (current.token == COLONCOLON)
3270 search_block = NULL;
3273 gdb_assert (current.token == TYPENAME);
3274 search_block = pstate->expression_context_block;
3275 obstack_grow (&name_obstack, current.value.sval.ptr,
3276 current.value.sval.length);
3277 context_type = current.value.tsym.type;
3281 first_was_coloncolon = current.token == COLONCOLON;
3282 last_was_coloncolon = first_was_coloncolon;
3284 while (next_to_examine <= last_to_examine)
3286 token_and_value next;
3288 next = token_fifo[next_to_examine];
3291 if (next.token == NAME && last_was_coloncolon)
3295 yylval = next.value;
3296 classification = classify_inner_name (pstate, search_block,
3298 /* We keep going until we either run out of names, or until
3299 we have a qualified name which is not a type. */
3300 if (classification != TYPENAME && classification != NAME)
3303 /* Accept up to this token. */
3304 checkpoint = next_to_examine;
3306 /* Update the partial name we are constructing. */
3307 if (context_type != NULL)
3309 /* We don't want to put a leading "::" into the name. */
3310 obstack_grow_str (&name_obstack, "::");
3312 obstack_grow (&name_obstack, next.value.sval.ptr,
3313 next.value.sval.length);
3315 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
3316 yylval.sval.length = obstack_object_size (&name_obstack);
3317 current.value = yylval;
3318 current.token = classification;
3320 last_was_coloncolon = 0;
3322 if (classification == NAME)
3325 context_type = yylval.tsym.type;
3327 else if (next.token == COLONCOLON && !last_was_coloncolon)
3328 last_was_coloncolon = 1;
3331 /* We've reached the end of the name. */
3336 /* If we have a replacement token, install it as the first token in
3337 the FIFO, and delete the other constituent tokens. */
3340 current.value.sval.ptr
3341 = obstack_strndup (&cpstate->expansion_obstack,
3342 current.value.sval.ptr,
3343 current.value.sval.length);
3345 token_fifo[0] = current;
3347 token_fifo.erase (token_fifo.begin () + 1,
3348 token_fifo.begin () + checkpoint);
3352 current = token_fifo[0];
3353 token_fifo.erase (token_fifo.begin ());
3354 yylval = current.value;
3355 return current.token;
3359 c_parse (struct parser_state *par_state)
3361 /* Setting up the parser state. */
3362 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3363 gdb_assert (par_state != NULL);
3366 c_parse_state cstate;
3367 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3369 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
3371 if (par_state->expression_context_block)
3373 = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
3375 macro_scope = default_macro_scope ();
3377 macro_scope = user_macro_scope ();
3379 scoped_restore restore_macro_scope
3380 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
3382 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3385 /* Initialize some state used by the lexer. */
3386 last_was_structop = false;
3387 saw_name_at_eof = 0;
3390 token_fifo.clear ();
3392 name_obstack.clear ();
3399 /* This is called via the YYPRINT macro when parser debugging is
3400 enabled. It prints a token's value. */
3403 c_print_token (FILE *file, int type, YYSTYPE value)
3408 parser_fprintf (file, "typed_val_int<%s, %s>",
3409 TYPE_SAFE_NAME (value.typed_val_int.type),
3410 pulongest (value.typed_val_int.val));
3416 char *copy = (char *) alloca (value.tsval.length + 1);
3418 memcpy (copy, value.tsval.ptr, value.tsval.length);
3419 copy[value.tsval.length] = '\0';
3421 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3426 case DOLLAR_VARIABLE:
3427 parser_fprintf (file, "sval<%s>", copy_name (value.sval).c_str ());
3431 parser_fprintf (file, "tsym<type=%s, name=%s>",
3432 TYPE_SAFE_NAME (value.tsym.type),
3433 copy_name (value.tsym.stoken).c_str ());
3437 case UNKNOWN_CPP_NAME:
3440 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3441 copy_name (value.ssym.stoken).c_str (),
3442 (value.ssym.sym.symbol == NULL
3443 ? "(null)" : value.ssym.sym.symbol->print_name ()),
3444 value.ssym.is_a_field_of_this);
3448 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3456 yyerror (const char *msg)
3458 if (pstate->prev_lexptr)
3459 pstate->lexptr = pstate->prev_lexptr;
3461 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);