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"
58 #define parse_type(ps) builtin_type (ps->gdbarch ())
60 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
62 #define GDB_YY_REMAP_PREFIX c_
65 /* The state of the parser, used internally when we are parsing the
68 static struct parser_state *pstate = NULL;
70 /* Data that must be held for the duration of a parse. */
74 /* These are used to hold type lists and type stacks that are
75 allocated during the parse. */
76 std::vector<std::unique_ptr<std::vector<struct type *>>> type_lists;
77 std::vector<std::unique_ptr<struct type_stack>> type_stacks;
79 /* Storage for some strings allocated during the parse. */
80 std::vector<gdb::unique_xmalloc_ptr<char>> strings;
82 /* When we find that lexptr (the global var defined in parse.c) is
83 pointing at a macro invocation, we expand the invocation, and call
84 scan_macro_expansion to save the old lexptr here and point lexptr
85 into the expanded text. When we reach the end of that, we call
86 end_macro_expansion to pop back to the value we saved here. The
87 macro expansion code promises to return only fully-expanded text,
88 so we don't need to "push" more than one level.
90 This is disgusting, of course. It would be cleaner to do all macro
91 expansion beforehand, and then hand that to lexptr. But we don't
92 really know where the expression ends. Remember, in a command like
94 (gdb) break *ADDRESS if CONDITION
96 we evaluate ADDRESS in the scope of the current frame, but we
97 evaluate CONDITION in the scope of the breakpoint's location. So
98 it's simply wrong to try to macro-expand the whole thing at once. */
99 const char *macro_original_text = nullptr;
101 /* We save all intermediate macro expansions on this obstack for the
102 duration of a single parse. The expansion text may sometimes have
103 to live past the end of the expansion, due to yacc lookahead.
104 Rather than try to be clever about saving the data for a single
105 token, we simply keep it all and delete it after parsing has
107 auto_obstack expansion_obstack;
109 /* The type stack. */
110 struct type_stack type_stack;
113 /* This is set and cleared in c_parse. */
115 static struct c_parse_state *cpstate;
119 static int yylex (void);
121 static void yyerror (const char *);
123 static int type_aggregate_p (struct type *);
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. */
144 struct typed_stoken tsval;
146 struct symtoken ssym;
148 const struct block *bval;
149 enum exp_opcode opcode;
151 struct stoken_vector svec;
152 std::vector<struct type *> *tvec;
154 struct type_stack *type_stack;
156 struct objc_class_str theclass;
160 /* YYSTYPE gets defined by %union */
161 static int parse_number (struct parser_state *par_state,
162 const char *, int, int, YYSTYPE *);
163 static struct stoken operator_stoken (const char *);
164 static struct stoken typename_stoken (const char *);
165 static void check_parameter_typelist (std::vector<struct type *> *);
166 static void write_destructor_name (struct parser_state *par_state,
170 static void c_print_token (FILE *file, int type, YYSTYPE value);
171 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
175 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly function_method
177 %type <tval> type typebase
178 %type <tvec> nonempty_typelist func_mod parameter_typelist
179 /* %type <bval> block */
181 /* Fancy type parsing. */
183 %type <lval> array_mod
184 %type <tval> conversion_type_id
186 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
188 %token <typed_val_int> INT
189 %token <typed_val_float> FLOAT
191 /* Both NAME and TYPENAME tokens represent symbols in the input,
192 and both convey their data as strings.
193 But a TYPENAME is a string that happens to be defined as a typedef
194 or builtin type name (such as int or char)
195 and a NAME is any other symbol.
196 Contexts where this distinction is not important can use the
197 nonterminal "name", which matches either NAME or TYPENAME. */
199 %token <tsval> STRING
200 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
201 %token SELECTOR /* ObjC "@selector" pseudo-operator */
203 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
204 %token <ssym> UNKNOWN_CPP_NAME
205 %token <voidval> COMPLETE
206 %token <tsym> TYPENAME
207 %token <theclass> CLASSNAME /* ObjC Class name */
208 %type <sval> name field_name
209 %type <svec> string_exp
210 %type <ssym> name_not_typename
211 %type <tsym> type_name
213 /* This is like a '[' token, but is only generated when parsing
214 Objective C. This lets us reuse the same parser without
215 erroneously parsing ObjC-specific expressions in C. */
218 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
219 but which would parse as a valid number in the current input radix.
220 E.g. "c" when input_radix==16. Depending on the parse, it will be
221 turned into a name or into a number. */
223 %token <ssym> NAME_OR_INT
226 %token STRUCT CLASS UNION ENUM SIZEOF ALIGNOF UNSIGNED COLONCOLON
231 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
237 /* Special type cases, put in to allow the parser to distinguish different
239 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
241 %token <sval> DOLLAR_VARIABLE
243 %token <opcode> ASSIGN_MODIFY
252 %right '=' ASSIGN_MODIFY
260 %left '<' '>' LEQ GEQ
265 %right UNARY INCREMENT DECREMENT
266 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
267 %token <ssym> BLOCKNAME
268 %token <bval> FILENAME
282 { write_exp_elt_opcode(pstate, OP_TYPE);
283 write_exp_elt_type(pstate, $1);
284 write_exp_elt_opcode(pstate, OP_TYPE);}
287 write_exp_elt_opcode (pstate, OP_TYPEOF);
289 | TYPEOF '(' type ')'
291 write_exp_elt_opcode (pstate, OP_TYPE);
292 write_exp_elt_type (pstate, $3);
293 write_exp_elt_opcode (pstate, OP_TYPE);
295 | DECLTYPE '(' exp ')'
297 write_exp_elt_opcode (pstate, OP_DECLTYPE);
301 /* Expressions, including the comma operator. */
304 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
307 /* Expressions, not including the comma operator. */
308 exp : '*' exp %prec UNARY
309 { write_exp_elt_opcode (pstate, UNOP_IND); }
312 exp : '&' exp %prec UNARY
313 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
316 exp : '-' exp %prec UNARY
317 { write_exp_elt_opcode (pstate, UNOP_NEG); }
320 exp : '+' exp %prec UNARY
321 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
324 exp : '!' exp %prec UNARY
325 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
328 exp : '~' exp %prec UNARY
329 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
332 exp : INCREMENT exp %prec UNARY
333 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
336 exp : DECREMENT exp %prec UNARY
337 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
340 exp : exp INCREMENT %prec UNARY
341 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
344 exp : exp DECREMENT %prec UNARY
345 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
348 exp : TYPEID '(' exp ')' %prec UNARY
349 { write_exp_elt_opcode (pstate, OP_TYPEID); }
352 exp : TYPEID '(' type_exp ')' %prec UNARY
353 { write_exp_elt_opcode (pstate, OP_TYPEID); }
356 exp : SIZEOF exp %prec UNARY
357 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
360 exp : ALIGNOF '(' type_exp ')' %prec UNARY
361 { write_exp_elt_opcode (pstate, UNOP_ALIGNOF); }
364 exp : exp ARROW field_name
365 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
366 write_exp_string (pstate, $3);
367 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
370 exp : exp ARROW field_name COMPLETE
371 { pstate->mark_struct_expression ();
372 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
373 write_exp_string (pstate, $3);
374 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
377 exp : exp ARROW COMPLETE
379 pstate->mark_struct_expression ();
380 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
383 write_exp_string (pstate, s);
384 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
387 exp : exp ARROW '~' name
388 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
389 write_destructor_name (pstate, $4);
390 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
393 exp : exp ARROW '~' name COMPLETE
394 { pstate->mark_struct_expression ();
395 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
396 write_destructor_name (pstate, $4);
397 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
400 exp : exp ARROW qualified_name
401 { /* exp->type::name becomes exp->*(&type::name) */
402 /* Note: this doesn't work if name is a
403 static member! FIXME */
404 write_exp_elt_opcode (pstate, UNOP_ADDR);
405 write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
408 exp : exp ARROW_STAR exp
409 { write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
412 exp : exp '.' field_name
413 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
414 write_exp_string (pstate, $3);
415 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
418 exp : exp '.' field_name COMPLETE
419 { pstate->mark_struct_expression ();
420 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
421 write_exp_string (pstate, $3);
422 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
425 exp : exp '.' COMPLETE
427 pstate->mark_struct_expression ();
428 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
431 write_exp_string (pstate, s);
432 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
435 exp : exp '.' '~' name
436 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
437 write_destructor_name (pstate, $4);
438 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
441 exp : exp '.' '~' name COMPLETE
442 { pstate->mark_struct_expression ();
443 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
444 write_destructor_name (pstate, $4);
445 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
448 exp : exp '.' qualified_name
449 { /* exp.type::name becomes exp.*(&type::name) */
450 /* Note: this doesn't work if name is a
451 static member! FIXME */
452 write_exp_elt_opcode (pstate, UNOP_ADDR);
453 write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
456 exp : exp DOT_STAR exp
457 { write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
460 exp : exp '[' exp1 ']'
461 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
464 exp : exp OBJC_LBRAC exp1 ']'
465 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
469 * The rules below parse ObjC message calls of the form:
470 * '[' target selector {':' argument}* ']'
473 exp : OBJC_LBRAC TYPENAME
477 std::string copy = copy_name ($2.stoken);
478 theclass = lookup_objc_class (pstate->gdbarch (),
481 error (_("%s is not an ObjC Class"),
483 write_exp_elt_opcode (pstate, OP_LONG);
484 write_exp_elt_type (pstate,
485 parse_type (pstate)->builtin_int);
486 write_exp_elt_longcst (pstate, (LONGEST) theclass);
487 write_exp_elt_opcode (pstate, OP_LONG);
491 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
492 end_msglist (pstate);
493 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
497 exp : OBJC_LBRAC CLASSNAME
499 write_exp_elt_opcode (pstate, OP_LONG);
500 write_exp_elt_type (pstate,
501 parse_type (pstate)->builtin_int);
502 write_exp_elt_longcst (pstate, (LONGEST) $2.theclass);
503 write_exp_elt_opcode (pstate, OP_LONG);
507 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
508 end_msglist (pstate);
509 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
516 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
517 end_msglist (pstate);
518 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
523 { add_msglist(&$1, 0); }
531 msgarg : name ':' exp
532 { add_msglist(&$1, 1); }
533 | ':' exp /* Unnamed arg. */
534 { add_msglist(0, 1); }
535 | ',' exp /* Variable number of args. */
536 { add_msglist(0, 0); }
540 /* This is to save the value of arglist_len
541 being accumulated by an outer function call. */
542 { pstate->start_arglist (); }
543 arglist ')' %prec ARROW
544 { write_exp_elt_opcode (pstate, OP_FUNCALL);
545 write_exp_elt_longcst (pstate,
546 pstate->end_arglist ());
547 write_exp_elt_opcode (pstate, OP_FUNCALL); }
550 /* This is here to disambiguate with the production for
551 "func()::static_var" further below, which uses
552 function_method_void. */
553 exp : exp '(' ')' %prec ARROW
554 { pstate->start_arglist ();
555 write_exp_elt_opcode (pstate, OP_FUNCALL);
556 write_exp_elt_longcst (pstate,
557 pstate->end_arglist ());
558 write_exp_elt_opcode (pstate, OP_FUNCALL); }
562 exp : UNKNOWN_CPP_NAME '('
564 /* This could potentially be a an argument defined
565 lookup function (Koenig). */
566 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
568 (pstate, pstate->expression_context_block);
569 write_exp_elt_sym (pstate,
570 NULL); /* Placeholder. */
571 write_exp_string (pstate, $1.stoken);
572 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
574 /* This is to save the value of arglist_len
575 being accumulated by an outer function call. */
577 pstate->start_arglist ();
579 arglist ')' %prec ARROW
581 write_exp_elt_opcode (pstate, OP_FUNCALL);
582 write_exp_elt_longcst (pstate,
583 pstate->end_arglist ());
584 write_exp_elt_opcode (pstate, OP_FUNCALL);
589 { pstate->start_arglist (); }
596 { pstate->arglist_len = 1; }
599 arglist : arglist ',' exp %prec ABOVE_COMMA
600 { pstate->arglist_len++; }
603 function_method: exp '(' parameter_typelist ')' const_or_volatile
605 std::vector<struct type *> *type_list = $3;
606 LONGEST len = type_list->size ();
608 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
609 /* Save the const/volatile qualifiers as
610 recorded by the const_or_volatile
611 production's actions. */
612 write_exp_elt_longcst
615 .follow_type_instance_flags ()));
616 write_exp_elt_longcst (pstate, len);
617 for (type *type_elt : *type_list)
618 write_exp_elt_type (pstate, type_elt);
619 write_exp_elt_longcst(pstate, len);
620 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
624 function_method_void: exp '(' ')' const_or_volatile
625 { write_exp_elt_opcode (pstate, TYPE_INSTANCE);
627 write_exp_elt_longcst
629 cpstate->type_stack.follow_type_instance_flags ());
630 write_exp_elt_longcst (pstate, 0);
631 write_exp_elt_longcst (pstate, 0);
632 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
636 exp : function_method
639 /* Normally we must interpret "func()" as a function call, instead of
640 a type. The user needs to write func(void) to disambiguate.
641 However, in the "func()::static_var" case, there's no
643 function_method_void_or_typelist: function_method
644 | function_method_void
647 exp : function_method_void_or_typelist COLONCOLON name
649 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
650 write_exp_string (pstate, $3);
651 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
656 { $$ = pstate->end_arglist () - 1; }
658 exp : lcurly arglist rcurly %prec ARROW
659 { write_exp_elt_opcode (pstate, OP_ARRAY);
660 write_exp_elt_longcst (pstate, (LONGEST) 0);
661 write_exp_elt_longcst (pstate, (LONGEST) $3);
662 write_exp_elt_opcode (pstate, OP_ARRAY); }
665 exp : lcurly type_exp rcurly exp %prec UNARY
666 { write_exp_elt_opcode (pstate, UNOP_MEMVAL_TYPE); }
669 exp : '(' type_exp ')' exp %prec UNARY
670 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
677 /* Binary operators in order of decreasing precedence. */
680 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
684 { write_exp_elt_opcode (pstate, BINOP_MUL); }
688 { write_exp_elt_opcode (pstate, BINOP_DIV); }
692 { write_exp_elt_opcode (pstate, BINOP_REM); }
696 { write_exp_elt_opcode (pstate, BINOP_ADD); }
700 { write_exp_elt_opcode (pstate, BINOP_SUB); }
704 { write_exp_elt_opcode (pstate, BINOP_LSH); }
708 { write_exp_elt_opcode (pstate, BINOP_RSH); }
712 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
715 exp : exp NOTEQUAL exp
716 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
720 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
724 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
728 { write_exp_elt_opcode (pstate, BINOP_LESS); }
732 { write_exp_elt_opcode (pstate, BINOP_GTR); }
736 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
740 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
744 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
748 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
752 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
755 exp : exp '?' exp ':' exp %prec '?'
756 { write_exp_elt_opcode (pstate, TERNOP_COND); }
760 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
763 exp : exp ASSIGN_MODIFY exp
764 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
765 write_exp_elt_opcode (pstate, $2);
766 write_exp_elt_opcode (pstate,
767 BINOP_ASSIGN_MODIFY); }
771 { write_exp_elt_opcode (pstate, OP_LONG);
772 write_exp_elt_type (pstate, $1.type);
773 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
774 write_exp_elt_opcode (pstate, OP_LONG); }
779 struct stoken_vector vec;
782 write_exp_string_vector (pstate, $1.type, &vec);
788 parse_number (pstate, $1.stoken.ptr,
789 $1.stoken.length, 0, &val);
790 write_exp_elt_opcode (pstate, OP_LONG);
791 write_exp_elt_type (pstate, val.typed_val_int.type);
792 write_exp_elt_longcst (pstate,
793 (LONGEST) val.typed_val_int.val);
794 write_exp_elt_opcode (pstate, OP_LONG);
800 { write_exp_elt_opcode (pstate, OP_FLOAT);
801 write_exp_elt_type (pstate, $1.type);
802 write_exp_elt_floatcst (pstate, $1.val);
803 write_exp_elt_opcode (pstate, OP_FLOAT); }
809 exp : DOLLAR_VARIABLE
811 write_dollar_variable (pstate, $1);
815 exp : SELECTOR '(' name ')'
817 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR);
818 write_exp_string (pstate, $3);
819 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR); }
822 exp : SIZEOF '(' type ')' %prec UNARY
823 { struct type *type = $3;
824 write_exp_elt_opcode (pstate, OP_LONG);
825 write_exp_elt_type (pstate, lookup_signed_typename
826 (pstate->language (),
828 type = check_typedef (type);
830 /* $5.3.3/2 of the C++ Standard (n3290 draft)
831 says of sizeof: "When applied to a reference
832 or a reference type, the result is the size of
833 the referenced type." */
834 if (TYPE_IS_REFERENCE (type))
835 type = check_typedef (TYPE_TARGET_TYPE (type));
836 write_exp_elt_longcst (pstate,
837 (LONGEST) TYPE_LENGTH (type));
838 write_exp_elt_opcode (pstate, OP_LONG); }
841 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
842 { write_exp_elt_opcode (pstate,
843 UNOP_REINTERPRET_CAST); }
846 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
847 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
850 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
851 { write_exp_elt_opcode (pstate, UNOP_DYNAMIC_CAST); }
854 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
855 { /* We could do more error checking here, but
856 it doesn't seem worthwhile. */
857 write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
863 /* We copy the string here, and not in the
864 lexer, to guarantee that we do not leak a
865 string. Note that we follow the
866 NUL-termination convention of the
868 struct typed_stoken *vec = XNEW (struct typed_stoken);
873 vec->length = $1.length;
874 vec->ptr = (char *) malloc ($1.length + 1);
875 memcpy (vec->ptr, $1.ptr, $1.length + 1);
880 /* Note that we NUL-terminate here, but just
884 $$.tokens = XRESIZEVEC (struct typed_stoken,
887 p = (char *) malloc ($2.length + 1);
888 memcpy (p, $2.ptr, $2.length + 1);
890 $$.tokens[$$.len - 1].type = $2.type;
891 $$.tokens[$$.len - 1].length = $2.length;
892 $$.tokens[$$.len - 1].ptr = p;
899 c_string_type type = C_STRING;
901 for (i = 0; i < $1.len; ++i)
903 switch ($1.tokens[i].type)
911 && type != $1.tokens[i].type)
912 error (_("Undefined string concatenation."));
913 type = (enum c_string_type_values) $1.tokens[i].type;
917 internal_error (__FILE__, __LINE__,
918 "unrecognized type in string concatenation");
922 write_exp_string_vector (pstate, type, &$1);
923 for (i = 0; i < $1.len; ++i)
924 free ($1.tokens[i].ptr);
929 exp : NSSTRING /* ObjC NextStep NSString constant
930 * of the form '@' '"' string '"'.
932 { write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING);
933 write_exp_string (pstate, $1);
934 write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING); }
939 { write_exp_elt_opcode (pstate, OP_LONG);
940 write_exp_elt_type (pstate,
941 parse_type (pstate)->builtin_bool);
942 write_exp_elt_longcst (pstate, (LONGEST) 1);
943 write_exp_elt_opcode (pstate, OP_LONG); }
947 { write_exp_elt_opcode (pstate, OP_LONG);
948 write_exp_elt_type (pstate,
949 parse_type (pstate)->builtin_bool);
950 write_exp_elt_longcst (pstate, (LONGEST) 0);
951 write_exp_elt_opcode (pstate, OP_LONG); }
959 $$ = SYMBOL_BLOCK_VALUE ($1.sym.symbol);
961 error (_("No file or function \"%s\"."),
962 copy_name ($1.stoken).c_str ());
970 block : block COLONCOLON name
972 std::string copy = copy_name ($3);
974 = lookup_symbol (copy.c_str (), $1,
975 VAR_DOMAIN, NULL).symbol;
977 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
978 error (_("No function \"%s\" in specified context."),
980 $$ = SYMBOL_BLOCK_VALUE (tem); }
983 variable: name_not_typename ENTRY
984 { struct symbol *sym = $1.sym.symbol;
986 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
987 || !symbol_read_needs_frame (sym))
988 error (_("@entry can be used only for function "
989 "parameters, not for \"%s\""),
990 copy_name ($1.stoken).c_str ());
992 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
993 write_exp_elt_sym (pstate, sym);
994 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
998 variable: block COLONCOLON name
1000 std::string copy = copy_name ($3);
1001 struct block_symbol sym
1002 = lookup_symbol (copy.c_str (), $1,
1005 if (sym.symbol == 0)
1006 error (_("No symbol \"%s\" in specified context."),
1008 if (symbol_read_needs_frame (sym.symbol))
1009 pstate->block_tracker->update (sym);
1011 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1012 write_exp_elt_block (pstate, sym.block);
1013 write_exp_elt_sym (pstate, sym.symbol);
1014 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
1017 qualified_name: TYPENAME COLONCOLON name
1019 struct type *type = $1.type;
1020 type = check_typedef (type);
1021 if (!type_aggregate_p (type))
1022 error (_("`%s' is not defined as an aggregate type."),
1023 TYPE_SAFE_NAME (type));
1025 write_exp_elt_opcode (pstate, OP_SCOPE);
1026 write_exp_elt_type (pstate, type);
1027 write_exp_string (pstate, $3);
1028 write_exp_elt_opcode (pstate, OP_SCOPE);
1030 | TYPENAME COLONCOLON '~' name
1032 struct type *type = $1.type;
1033 struct stoken tmp_token;
1036 type = check_typedef (type);
1037 if (!type_aggregate_p (type))
1038 error (_("`%s' is not defined as an aggregate type."),
1039 TYPE_SAFE_NAME (type));
1040 buf = (char *) alloca ($4.length + 2);
1041 tmp_token.ptr = buf;
1042 tmp_token.length = $4.length + 1;
1044 memcpy (buf+1, $4.ptr, $4.length);
1045 buf[tmp_token.length] = 0;
1047 /* Check for valid destructor name. */
1048 destructor_name_p (tmp_token.ptr, $1.type);
1049 write_exp_elt_opcode (pstate, OP_SCOPE);
1050 write_exp_elt_type (pstate, type);
1051 write_exp_string (pstate, tmp_token);
1052 write_exp_elt_opcode (pstate, OP_SCOPE);
1054 | TYPENAME COLONCOLON name COLONCOLON name
1056 std::string copy = copy_name ($3);
1057 error (_("No type \"%s\" within class "
1058 "or namespace \"%s\"."),
1059 copy.c_str (), TYPE_SAFE_NAME ($1.type));
1063 variable: qualified_name
1064 | COLONCOLON name_not_typename
1066 std::string name = copy_name ($2.stoken);
1068 struct bound_minimal_symbol msymbol;
1071 = lookup_symbol (name.c_str (),
1072 (const struct block *) NULL,
1073 VAR_DOMAIN, NULL).symbol;
1076 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1077 write_exp_elt_block (pstate, NULL);
1078 write_exp_elt_sym (pstate, sym);
1079 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1083 msymbol = lookup_bound_minimal_symbol (name.c_str ());
1084 if (msymbol.minsym != NULL)
1085 write_exp_msymbol (pstate, msymbol);
1086 else if (!have_full_symbols () && !have_partial_symbols ())
1087 error (_("No symbol table is loaded. Use the \"file\" command."));
1089 error (_("No symbol \"%s\" in current context."),
1094 variable: name_not_typename
1095 { struct block_symbol sym = $1.sym;
1099 if (symbol_read_needs_frame (sym.symbol))
1100 pstate->block_tracker->update (sym);
1102 /* If we found a function, see if it's
1103 an ifunc resolver that has the same
1104 address as the ifunc symbol itself.
1105 If so, prefer the ifunc symbol. */
1107 bound_minimal_symbol resolver
1108 = find_gnu_ifunc (sym.symbol);
1109 if (resolver.minsym != NULL)
1110 write_exp_msymbol (pstate, resolver);
1113 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1114 write_exp_elt_block (pstate, sym.block);
1115 write_exp_elt_sym (pstate, sym.symbol);
1116 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1119 else if ($1.is_a_field_of_this)
1121 /* C++: it hangs off of `this'. Must
1122 not inadvertently convert from a method call
1124 pstate->block_tracker->update (sym);
1125 write_exp_elt_opcode (pstate, OP_THIS);
1126 write_exp_elt_opcode (pstate, OP_THIS);
1127 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1128 write_exp_string (pstate, $1.stoken);
1129 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1133 std::string arg = copy_name ($1.stoken);
1135 bound_minimal_symbol msymbol
1136 = lookup_bound_minimal_symbol (arg.c_str ());
1137 if (msymbol.minsym == NULL)
1139 if (!have_full_symbols () && !have_partial_symbols ())
1140 error (_("No symbol table is loaded. Use the \"file\" command."));
1142 error (_("No symbol \"%s\" in current context."),
1146 /* This minsym might be an alias for
1147 another function. See if we can find
1148 the debug symbol for the target, and
1149 if so, use it instead, since it has
1150 return type / prototype info. This
1151 is important for example for "p
1152 *__errno_location()". */
1153 symbol *alias_target
1154 = ((msymbol.minsym->type != mst_text_gnu_ifunc
1155 && msymbol.minsym->type != mst_data_gnu_ifunc)
1156 ? find_function_alias_target (msymbol)
1158 if (alias_target != NULL)
1160 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1162 (pstate, SYMBOL_BLOCK_VALUE (alias_target));
1163 write_exp_elt_sym (pstate, alias_target);
1164 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1167 write_exp_msymbol (pstate, msymbol);
1172 space_identifier : '@' NAME
1174 cpstate->type_stack.insert (pstate,
1175 copy_name ($2.stoken).c_str ());
1179 const_or_volatile: const_or_volatile_noopt
1183 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
1186 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
1187 | const_or_volatile_noopt
1190 const_or_volatile_or_space_identifier:
1191 const_or_volatile_or_space_identifier_noopt
1197 { cpstate->type_stack.insert (tp_pointer); }
1198 const_or_volatile_or_space_identifier
1200 { cpstate->type_stack.insert (tp_pointer); }
1201 const_or_volatile_or_space_identifier
1203 { cpstate->type_stack.insert (tp_reference); }
1205 { cpstate->type_stack.insert (tp_reference); }
1207 { cpstate->type_stack.insert (tp_rvalue_reference); }
1208 | ANDAND ptr_operator
1209 { cpstate->type_stack.insert (tp_rvalue_reference); }
1212 ptr_operator_ts: ptr_operator
1214 $$ = cpstate->type_stack.create ();
1215 cpstate->type_stacks.emplace_back ($$);
1219 abs_decl: ptr_operator_ts direct_abs_decl
1220 { $$ = $2->append ($1); }
1225 direct_abs_decl: '(' abs_decl ')'
1227 | direct_abs_decl array_mod
1229 cpstate->type_stack.push ($1);
1230 cpstate->type_stack.push ($2);
1231 cpstate->type_stack.push (tp_array);
1232 $$ = cpstate->type_stack.create ();
1233 cpstate->type_stacks.emplace_back ($$);
1237 cpstate->type_stack.push ($1);
1238 cpstate->type_stack.push (tp_array);
1239 $$ = cpstate->type_stack.create ();
1240 cpstate->type_stacks.emplace_back ($$);
1243 | direct_abs_decl func_mod
1245 cpstate->type_stack.push ($1);
1246 cpstate->type_stack.push ($2);
1247 $$ = cpstate->type_stack.create ();
1248 cpstate->type_stacks.emplace_back ($$);
1252 cpstate->type_stack.push ($1);
1253 $$ = cpstate->type_stack.create ();
1254 cpstate->type_stacks.emplace_back ($$);
1264 | OBJC_LBRAC INT ']'
1270 $$ = new std::vector<struct type *>;
1271 cpstate->type_lists.emplace_back ($$);
1273 | '(' parameter_typelist ')'
1277 /* We used to try to recognize pointer to member types here, but
1278 that didn't work (shift/reduce conflicts meant that these rules never
1279 got executed). The problem is that
1280 int (foo::bar::baz::bizzle)
1281 is a function type but
1282 int (foo::bar::baz::bizzle::*)
1283 is a pointer to member type. Stroustrup loses again! */
1288 /* Implements (approximately): (type-qualifier)* type-specifier.
1290 When type-specifier is only ever a single word, like 'float' then these
1291 arrive as pre-built TYPENAME tokens thanks to the classify_name
1292 function. However, when a type-specifier can contain multiple words,
1293 for example 'double' can appear as just 'double' or 'long double', and
1294 similarly 'long' can appear as just 'long' or in 'long double', then
1295 these type-specifiers are parsed into their own tokens in the function
1296 lex_one_token and the ident_tokens array. These separate tokens are all
1302 { $$ = lookup_signed_typename (pstate->language (),
1305 { $$ = lookup_signed_typename (pstate->language (),
1308 { $$ = lookup_signed_typename (pstate->language (),
1311 { $$ = lookup_signed_typename (pstate->language (),
1313 | LONG SIGNED_KEYWORD INT_KEYWORD
1314 { $$ = lookup_signed_typename (pstate->language (),
1316 | LONG SIGNED_KEYWORD
1317 { $$ = lookup_signed_typename (pstate->language (),
1319 | SIGNED_KEYWORD LONG INT_KEYWORD
1320 { $$ = lookup_signed_typename (pstate->language (),
1322 | UNSIGNED LONG INT_KEYWORD
1323 { $$ = lookup_unsigned_typename (pstate->language (),
1325 | LONG UNSIGNED INT_KEYWORD
1326 { $$ = lookup_unsigned_typename (pstate->language (),
1329 { $$ = lookup_unsigned_typename (pstate->language (),
1332 { $$ = lookup_signed_typename (pstate->language (),
1334 | LONG LONG INT_KEYWORD
1335 { $$ = lookup_signed_typename (pstate->language (),
1337 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1338 { $$ = lookup_signed_typename (pstate->language (),
1340 | LONG LONG SIGNED_KEYWORD
1341 { $$ = lookup_signed_typename (pstate->language (),
1343 | SIGNED_KEYWORD LONG LONG
1344 { $$ = lookup_signed_typename (pstate->language (),
1346 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1347 { $$ = lookup_signed_typename (pstate->language (),
1349 | UNSIGNED LONG LONG
1350 { $$ = lookup_unsigned_typename (pstate->language (),
1352 | UNSIGNED LONG LONG INT_KEYWORD
1353 { $$ = lookup_unsigned_typename (pstate->language (),
1355 | LONG LONG UNSIGNED
1356 { $$ = lookup_unsigned_typename (pstate->language (),
1358 | LONG LONG UNSIGNED INT_KEYWORD
1359 { $$ = lookup_unsigned_typename (pstate->language (),
1362 { $$ = lookup_signed_typename (pstate->language (),
1364 | SHORT SIGNED_KEYWORD INT_KEYWORD
1365 { $$ = lookup_signed_typename (pstate->language (),
1367 | SHORT SIGNED_KEYWORD
1368 { $$ = lookup_signed_typename (pstate->language (),
1370 | UNSIGNED SHORT INT_KEYWORD
1371 { $$ = lookup_unsigned_typename (pstate->language (),
1374 { $$ = lookup_unsigned_typename (pstate->language (),
1376 | SHORT UNSIGNED INT_KEYWORD
1377 { $$ = lookup_unsigned_typename (pstate->language (),
1380 { $$ = lookup_typename (pstate->language (),
1384 | LONG DOUBLE_KEYWORD
1385 { $$ = lookup_typename (pstate->language (),
1391 = lookup_struct (copy_name ($2).c_str (),
1392 pstate->expression_context_block);
1396 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1400 | STRUCT name COMPLETE
1402 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1407 { $$ = lookup_struct
1408 (copy_name ($2).c_str (),
1409 pstate->expression_context_block);
1413 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1417 | CLASS name COMPLETE
1419 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1425 = lookup_union (copy_name ($2).c_str (),
1426 pstate->expression_context_block);
1430 pstate->mark_completion_tag (TYPE_CODE_UNION,
1434 | UNION name COMPLETE
1436 pstate->mark_completion_tag (TYPE_CODE_UNION,
1441 { $$ = lookup_enum (copy_name ($2).c_str (),
1442 pstate->expression_context_block);
1446 pstate->mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1449 | ENUM name COMPLETE
1451 pstate->mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1455 | UNSIGNED type_name
1456 { $$ = lookup_unsigned_typename (pstate->language (),
1457 TYPE_NAME($2.type)); }
1459 { $$ = lookup_unsigned_typename (pstate->language (),
1461 | SIGNED_KEYWORD type_name
1462 { $$ = lookup_signed_typename (pstate->language (),
1463 TYPE_NAME($2.type)); }
1465 { $$ = lookup_signed_typename (pstate->language (),
1467 /* It appears that this rule for templates is never
1468 reduced; template recognition happens by lookahead
1469 in the token processing code in yylex. */
1470 | TEMPLATE name '<' type '>'
1471 { $$ = lookup_template_type
1472 (copy_name($2).c_str (), $4,
1473 pstate->expression_context_block);
1475 | const_or_volatile_or_space_identifier_noopt typebase
1476 { $$ = cpstate->type_stack.follow_types ($2); }
1477 | typebase const_or_volatile_or_space_identifier_noopt
1478 { $$ = cpstate->type_stack.follow_types ($1); }
1484 $$.stoken.ptr = "int";
1485 $$.stoken.length = 3;
1486 $$.type = lookup_signed_typename (pstate->language (),
1491 $$.stoken.ptr = "long";
1492 $$.stoken.length = 4;
1493 $$.type = lookup_signed_typename (pstate->language (),
1498 $$.stoken.ptr = "short";
1499 $$.stoken.length = 5;
1500 $$.type = lookup_signed_typename (pstate->language (),
1507 { check_parameter_typelist ($1); }
1508 | nonempty_typelist ',' DOTDOTDOT
1510 $1->push_back (NULL);
1511 check_parameter_typelist ($1);
1519 std::vector<struct type *> *typelist
1520 = new std::vector<struct type *>;
1521 cpstate->type_lists.emplace_back (typelist);
1523 typelist->push_back ($1);
1526 | nonempty_typelist ',' type
1536 cpstate->type_stack.push ($2);
1537 $$ = cpstate->type_stack.follow_types ($1);
1541 conversion_type_id: typebase conversion_declarator
1542 { $$ = cpstate->type_stack.follow_types ($1); }
1545 conversion_declarator: /* Nothing. */
1546 | ptr_operator conversion_declarator
1549 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1550 | VOLATILE_KEYWORD CONST_KEYWORD
1553 const_or_volatile_noopt: const_and_volatile
1554 { cpstate->type_stack.insert (tp_const);
1555 cpstate->type_stack.insert (tp_volatile);
1558 { cpstate->type_stack.insert (tp_const); }
1560 { cpstate->type_stack.insert (tp_volatile); }
1564 { $$ = operator_stoken (" new"); }
1566 { $$ = operator_stoken (" delete"); }
1567 | OPERATOR NEW '[' ']'
1568 { $$ = operator_stoken (" new[]"); }
1569 | OPERATOR DELETE '[' ']'
1570 { $$ = operator_stoken (" delete[]"); }
1571 | OPERATOR NEW OBJC_LBRAC ']'
1572 { $$ = operator_stoken (" new[]"); }
1573 | OPERATOR DELETE OBJC_LBRAC ']'
1574 { $$ = operator_stoken (" delete[]"); }
1576 { $$ = operator_stoken ("+"); }
1578 { $$ = operator_stoken ("-"); }
1580 { $$ = operator_stoken ("*"); }
1582 { $$ = operator_stoken ("/"); }
1584 { $$ = operator_stoken ("%"); }
1586 { $$ = operator_stoken ("^"); }
1588 { $$ = operator_stoken ("&"); }
1590 { $$ = operator_stoken ("|"); }
1592 { $$ = operator_stoken ("~"); }
1594 { $$ = operator_stoken ("!"); }
1596 { $$ = operator_stoken ("="); }
1598 { $$ = operator_stoken ("<"); }
1600 { $$ = operator_stoken (">"); }
1601 | OPERATOR ASSIGN_MODIFY
1602 { const char *op = " unknown";
1626 case BINOP_BITWISE_IOR:
1629 case BINOP_BITWISE_AND:
1632 case BINOP_BITWISE_XOR:
1639 $$ = operator_stoken (op);
1642 { $$ = operator_stoken ("<<"); }
1644 { $$ = operator_stoken (">>"); }
1646 { $$ = operator_stoken ("=="); }
1648 { $$ = operator_stoken ("!="); }
1650 { $$ = operator_stoken ("<="); }
1652 { $$ = operator_stoken (">="); }
1654 { $$ = operator_stoken ("&&"); }
1656 { $$ = operator_stoken ("||"); }
1657 | OPERATOR INCREMENT
1658 { $$ = operator_stoken ("++"); }
1659 | OPERATOR DECREMENT
1660 { $$ = operator_stoken ("--"); }
1662 { $$ = operator_stoken (","); }
1663 | OPERATOR ARROW_STAR
1664 { $$ = operator_stoken ("->*"); }
1666 { $$ = operator_stoken ("->"); }
1668 { $$ = operator_stoken ("()"); }
1670 { $$ = operator_stoken ("[]"); }
1671 | OPERATOR OBJC_LBRAC ']'
1672 { $$ = operator_stoken ("[]"); }
1673 | OPERATOR conversion_type_id
1676 c_print_type ($2, NULL, &buf, -1, 0,
1677 &type_print_raw_options);
1679 /* This also needs canonicalization. */
1681 = cp_canonicalize_string (buf.c_str ());
1683 canon = std::move (buf.string ());
1684 $$ = operator_stoken ((" " + canon).c_str ());
1688 /* This rule exists in order to allow some tokens that would not normally
1689 match the 'name' rule to appear as fields within a struct. The example
1690 that initially motivated this was the RISC-V target which models the
1691 floating point registers as a union with fields called 'float' and
1692 'double'. The 'float' string becomes a TYPENAME token and can appear
1693 anywhere a 'name' can, however 'double' is its own token,
1694 DOUBLE_KEYWORD, and doesn't match the 'name' rule.*/
1697 | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
1698 | INT_KEYWORD { $$ = typename_stoken ("int"); }
1699 | LONG { $$ = typename_stoken ("long"); }
1700 | SHORT { $$ = typename_stoken ("short"); }
1701 | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
1702 | UNSIGNED { $$ = typename_stoken ("unsigned"); }
1705 name : NAME { $$ = $1.stoken; }
1706 | BLOCKNAME { $$ = $1.stoken; }
1707 | TYPENAME { $$ = $1.stoken; }
1708 | NAME_OR_INT { $$ = $1.stoken; }
1709 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1713 name_not_typename : NAME
1715 /* These would be useful if name_not_typename was useful, but it is just
1716 a fake for "variable", so these cause reduce/reduce conflicts because
1717 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1718 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1719 context where only a name could occur, this might be useful.
1724 struct field_of_this_result is_a_field_of_this;
1728 = lookup_symbol ($1.ptr,
1729 pstate->expression_context_block,
1731 &is_a_field_of_this);
1732 $$.is_a_field_of_this
1733 = is_a_field_of_this.type != NULL;
1740 /* Like write_exp_string, but prepends a '~'. */
1743 write_destructor_name (struct parser_state *par_state, struct stoken token)
1745 char *copy = (char *) alloca (token.length + 1);
1748 memcpy (©[1], token.ptr, token.length);
1753 write_exp_string (par_state, token);
1756 /* Returns a stoken of the operator name given by OP (which does not
1757 include the string "operator"). */
1759 static struct stoken
1760 operator_stoken (const char *op)
1762 struct stoken st = { NULL, 0 };
1765 st.length = CP_OPERATOR_LEN + strlen (op);
1766 buf = (char *) malloc (st.length + 1);
1767 strcpy (buf, CP_OPERATOR_STR);
1771 /* The toplevel (c_parse) will free the memory allocated here. */
1772 cpstate->strings.emplace_back (buf);
1776 /* Returns a stoken of the type named TYPE. */
1778 static struct stoken
1779 typename_stoken (const char *type)
1781 struct stoken st = { type, 0 };
1782 st.length = strlen (type);
1786 /* Return true if the type is aggregate-like. */
1789 type_aggregate_p (struct type *type)
1791 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
1792 || TYPE_CODE (type) == TYPE_CODE_UNION
1793 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE
1794 || (TYPE_CODE (type) == TYPE_CODE_ENUM
1795 && TYPE_DECLARED_CLASS (type)));
1798 /* Validate a parameter typelist. */
1801 check_parameter_typelist (std::vector<struct type *> *params)
1806 for (ix = 0; ix < params->size (); ++ix)
1808 type = (*params)[ix];
1809 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1813 if (params->size () == 1)
1818 error (_("parameter types following 'void'"));
1821 error (_("'void' invalid as parameter type"));
1826 /* Take care of parsing a number (anything that starts with a digit).
1827 Set yylval and return the token type; update lexptr.
1828 LEN is the number of characters in it. */
1830 /*** Needs some error checking for the float case ***/
1833 parse_number (struct parser_state *par_state,
1834 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1842 int base = input_radix;
1845 /* Number of "L" suffixes encountered. */
1848 /* We have found a "L" or "U" suffix. */
1849 int found_suffix = 0;
1852 struct type *signed_type;
1853 struct type *unsigned_type;
1856 p = (char *) alloca (len);
1857 memcpy (p, buf, len);
1861 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
1862 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1864 putithere->typed_val_float.type
1865 = parse_type (par_state)->builtin_decfloat;
1868 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1870 putithere->typed_val_float.type
1871 = parse_type (par_state)->builtin_decdouble;
1874 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1876 putithere->typed_val_float.type
1877 = parse_type (par_state)->builtin_declong;
1880 /* Handle suffixes: 'f' for float, 'l' for long double. */
1881 else if (len >= 1 && TOLOWER (p[len - 1]) == 'f')
1883 putithere->typed_val_float.type
1884 = parse_type (par_state)->builtin_float;
1887 else if (len >= 1 && TOLOWER (p[len - 1]) == 'l')
1889 putithere->typed_val_float.type
1890 = parse_type (par_state)->builtin_long_double;
1893 /* Default type for floating-point literals is double. */
1896 putithere->typed_val_float.type
1897 = parse_type (par_state)->builtin_double;
1900 if (!parse_float (p, len,
1901 putithere->typed_val_float.type,
1902 putithere->typed_val_float.val))
1907 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1908 if (p[0] == '0' && len > 1)
1951 if (c >= 'A' && c <= 'Z')
1953 if (c != 'l' && c != 'u')
1955 if (c >= '0' && c <= '9')
1963 if (base > 10 && c >= 'a' && c <= 'f')
1967 n += i = c - 'a' + 10;
1980 return ERROR; /* Char not a digit */
1983 return ERROR; /* Invalid digit in this base */
1985 /* Portably test for overflow (only works for nonzero values, so make
1986 a second check for zero). FIXME: Can't we just make n and prevn
1987 unsigned and avoid this? */
1988 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1989 unsigned_p = 1; /* Try something unsigned */
1991 /* Portably test for unsigned overflow.
1992 FIXME: This check is wrong; for example it doesn't find overflow
1993 on 0x123456789 when LONGEST is 32 bits. */
1994 if (c != 'l' && c != 'u' && n != 0)
1996 if (unsigned_p && prevn >= n)
1997 error (_("Numeric constant too large."));
2002 /* An integer constant is an int, a long, or a long long. An L
2003 suffix forces it to be long; an LL suffix forces it to be long
2004 long. If not forced to a larger size, it gets the first type of
2005 the above that it fits in. To figure out whether it fits, we
2006 shift it right and see whether anything remains. Note that we
2007 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
2008 operation, because many compilers will warn about such a shift
2009 (which always produces a zero result). Sometimes gdbarch_int_bit
2010 or gdbarch_long_bit will be that big, sometimes not. To deal with
2011 the case where it is we just always shift the value more than
2012 once, with fewer bits each time. */
2016 && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0)
2019 = ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
2021 /* A large decimal (not hex or octal) constant (between INT_MAX
2022 and UINT_MAX) is a long or unsigned long, according to ANSI,
2023 never an unsigned int, but this code treats it as unsigned
2024 int. This probably should be fixed. GCC gives a warning on
2027 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
2028 signed_type = parse_type (par_state)->builtin_int;
2030 else if (long_p <= 1
2031 && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0)
2034 = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1);
2035 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
2036 signed_type = parse_type (par_state)->builtin_long;
2041 if (sizeof (ULONGEST) * HOST_CHAR_BIT
2042 < gdbarch_long_long_bit (par_state->gdbarch ()))
2043 /* A long long does not fit in a LONGEST. */
2044 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
2046 shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1);
2047 high_bit = (ULONGEST) 1 << shift;
2048 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
2049 signed_type = parse_type (par_state)->builtin_long_long;
2052 putithere->typed_val_int.val = n;
2054 /* If the high bit of the worked out type is set then this number
2055 has to be unsigned. */
2057 if (unsigned_p || (n & high_bit))
2059 putithere->typed_val_int.type = unsigned_type;
2063 putithere->typed_val_int.type = signed_type;
2069 /* Temporary obstack used for holding strings. */
2070 static struct obstack tempbuf;
2071 static int tempbuf_init;
2073 /* Parse a C escape sequence. The initial backslash of the sequence
2074 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2075 last character of the sequence. If OUTPUT is not NULL, the
2076 translated form of the escape sequence will be written there. If
2077 OUTPUT is NULL, no output is written and the call will only affect
2078 *PTR. If an escape sequence is expressed in target bytes, then the
2079 entire sequence will simply be copied to OUTPUT. Return 1 if any
2080 character was emitted, 0 otherwise. */
2083 c_parse_escape (const char **ptr, struct obstack *output)
2085 const char *tokptr = *ptr;
2088 /* Some escape sequences undergo character set conversion. Those we
2092 /* Hex escapes do not undergo character set conversion, so keep
2093 the escape sequence for later. */
2096 obstack_grow_str (output, "\\x");
2098 if (!ISXDIGIT (*tokptr))
2099 error (_("\\x escape without a following hex digit"));
2100 while (ISXDIGIT (*tokptr))
2103 obstack_1grow (output, *tokptr);
2108 /* Octal escapes do not undergo character set conversion, so
2109 keep the escape sequence for later. */
2121 obstack_grow_str (output, "\\");
2123 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
2127 obstack_1grow (output, *tokptr);
2133 /* We handle UCNs later. We could handle them here, but that
2134 would mean a spurious error in the case where the UCN could
2135 be converted to the target charset but not the host
2141 int i, len = c == 'U' ? 8 : 4;
2144 obstack_1grow (output, '\\');
2145 obstack_1grow (output, *tokptr);
2148 if (!ISXDIGIT (*tokptr))
2149 error (_("\\%c escape without a following hex digit"), c);
2150 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
2153 obstack_1grow (output, *tokptr);
2159 /* We must pass backslash through so that it does not
2160 cause quoting during the second expansion. */
2163 obstack_grow_str (output, "\\\\");
2167 /* Escapes which undergo conversion. */
2170 obstack_1grow (output, '\a');
2175 obstack_1grow (output, '\b');
2180 obstack_1grow (output, '\f');
2185 obstack_1grow (output, '\n');
2190 obstack_1grow (output, '\r');
2195 obstack_1grow (output, '\t');
2200 obstack_1grow (output, '\v');
2204 /* GCC extension. */
2207 obstack_1grow (output, HOST_ESCAPE_CHAR);
2211 /* Backslash-newline expands to nothing at all. */
2217 /* A few escapes just expand to the character itself. */
2221 /* GCC extensions. */
2226 /* Unrecognized escapes turn into the character itself. */
2229 obstack_1grow (output, *tokptr);
2237 /* Parse a string or character literal from TOKPTR. The string or
2238 character may be wide or unicode. *OUTPTR is set to just after the
2239 end of the literal in the input string. The resulting token is
2240 stored in VALUE. This returns a token value, either STRING or
2241 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2242 number of host characters in the literal. */
2245 parse_string_or_char (const char *tokptr, const char **outptr,
2246 struct typed_stoken *value, int *host_chars)
2252 /* Build the gdb internal form of the input string in tempbuf. Note
2253 that the buffer is null byte terminated *only* for the
2254 convenience of debugging gdb itself and printing the buffer
2255 contents when the buffer contains no embedded nulls. Gdb does
2256 not depend upon the buffer being null byte terminated, it uses
2257 the length string instead. This allows gdb to handle C strings
2258 (as well as strings in other languages) with embedded null
2264 obstack_free (&tempbuf, NULL);
2265 obstack_init (&tempbuf);
2267 /* Record the string type. */
2270 type = C_WIDE_STRING;
2273 else if (*tokptr == 'u')
2278 else if (*tokptr == 'U')
2283 else if (*tokptr == '@')
2285 /* An Objective C string. */
2293 /* Skip the quote. */
2307 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2309 else if (c == quote)
2313 obstack_1grow (&tempbuf, c);
2315 /* FIXME: this does the wrong thing with multi-byte host
2316 characters. We could use mbrlen here, but that would
2317 make "set host-charset" a bit less useful. */
2322 if (*tokptr != quote)
2325 error (_("Unterminated string in expression."));
2327 error (_("Unmatched single quote."));
2332 value->ptr = (char *) obstack_base (&tempbuf);
2333 value->length = obstack_object_size (&tempbuf);
2337 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2340 /* This is used to associate some attributes with a token. */
2344 /* If this bit is set, the token is C++-only. */
2348 /* If this bit is set, the token is conditional: if there is a
2349 symbol of the same name, then the token is a symbol; otherwise,
2350 the token is a keyword. */
2354 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2360 enum exp_opcode opcode;
2364 static const struct token tokentab3[] =
2366 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2367 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2368 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2369 {"...", DOTDOTDOT, BINOP_END, 0}
2372 static const struct token tokentab2[] =
2374 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2375 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2376 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2377 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2378 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2379 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2380 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2381 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2382 {"++", INCREMENT, BINOP_END, 0},
2383 {"--", DECREMENT, BINOP_END, 0},
2384 {"->", ARROW, BINOP_END, 0},
2385 {"&&", ANDAND, BINOP_END, 0},
2386 {"||", OROR, BINOP_END, 0},
2387 /* "::" is *not* only C++: gdb overrides its meaning in several
2388 different ways, e.g., 'filename'::func, function::variable. */
2389 {"::", COLONCOLON, BINOP_END, 0},
2390 {"<<", LSH, BINOP_END, 0},
2391 {">>", RSH, BINOP_END, 0},
2392 {"==", EQUAL, BINOP_END, 0},
2393 {"!=", NOTEQUAL, BINOP_END, 0},
2394 {"<=", LEQ, BINOP_END, 0},
2395 {">=", GEQ, BINOP_END, 0},
2396 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2399 /* Identifier-like tokens. Only type-specifiers than can appear in
2400 multi-word type names (for example 'double' can appear in 'long
2401 double') need to be listed here. type-specifiers that are only ever
2402 single word (like 'float') are handled by the classify_name function. */
2403 static const struct token ident_tokens[] =
2405 {"unsigned", UNSIGNED, OP_NULL, 0},
2406 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2407 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2408 {"struct", STRUCT, OP_NULL, 0},
2409 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2410 {"sizeof", SIZEOF, OP_NULL, 0},
2411 {"_Alignof", ALIGNOF, OP_NULL, 0},
2412 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
2413 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2414 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2415 {"class", CLASS, OP_NULL, FLAG_CXX},
2416 {"union", UNION, OP_NULL, 0},
2417 {"short", SHORT, OP_NULL, 0},
2418 {"const", CONST_KEYWORD, OP_NULL, 0},
2419 {"enum", ENUM, OP_NULL, 0},
2420 {"long", LONG, OP_NULL, 0},
2421 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2422 {"int", INT_KEYWORD, OP_NULL, 0},
2423 {"new", NEW, OP_NULL, FLAG_CXX},
2424 {"delete", DELETE, OP_NULL, FLAG_CXX},
2425 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2427 {"and", ANDAND, BINOP_END, FLAG_CXX},
2428 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2429 {"bitand", '&', OP_NULL, FLAG_CXX},
2430 {"bitor", '|', OP_NULL, FLAG_CXX},
2431 {"compl", '~', OP_NULL, FLAG_CXX},
2432 {"not", '!', OP_NULL, FLAG_CXX},
2433 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2434 {"or", OROR, BINOP_END, FLAG_CXX},
2435 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2436 {"xor", '^', OP_NULL, FLAG_CXX},
2437 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2439 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2440 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2441 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2442 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2444 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2445 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2446 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2447 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2448 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2450 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2455 scan_macro_expansion (char *expansion)
2459 /* We'd better not be trying to push the stack twice. */
2460 gdb_assert (! cpstate->macro_original_text);
2462 /* Copy to the obstack, and then free the intermediate
2464 copy = obstack_strdup (&cpstate->expansion_obstack, expansion);
2467 /* Save the old lexptr value, so we can return to it when we're done
2468 parsing the expanded text. */
2469 cpstate->macro_original_text = pstate->lexptr;
2470 pstate->lexptr = copy;
2474 scanning_macro_expansion (void)
2476 return cpstate->macro_original_text != 0;
2480 finished_macro_expansion (void)
2482 /* There'd better be something to pop back to. */
2483 gdb_assert (cpstate->macro_original_text);
2485 /* Pop back to the original text. */
2486 pstate->lexptr = cpstate->macro_original_text;
2487 cpstate->macro_original_text = 0;
2490 /* Return true iff the token represents a C++ cast operator. */
2493 is_cast_operator (const char *token, int len)
2495 return (! strncmp (token, "dynamic_cast", len)
2496 || ! strncmp (token, "static_cast", len)
2497 || ! strncmp (token, "reinterpret_cast", len)
2498 || ! strncmp (token, "const_cast", len));
2501 /* The scope used for macro expansion. */
2502 static struct macro_scope *expression_macro_scope;
2504 /* This is set if a NAME token appeared at the very end of the input
2505 string, with no whitespace separating the name from the EOF. This
2506 is used only when parsing to do field name completion. */
2507 static int saw_name_at_eof;
2509 /* This is set if the previously-returned token was a structure
2510 operator -- either '.' or ARROW. */
2511 static bool last_was_structop;
2513 /* Depth of parentheses. */
2514 static int paren_depth;
2516 /* Read one token, getting characters through lexptr. */
2519 lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
2524 const char *tokstart;
2525 bool saw_structop = last_was_structop;
2527 last_was_structop = false;
2528 *is_quoted_name = false;
2532 /* Check if this is a macro invocation that we need to expand. */
2533 if (! scanning_macro_expansion ())
2535 char *expanded = macro_expand_next (&pstate->lexptr,
2536 standard_macro_lookup,
2537 expression_macro_scope);
2540 scan_macro_expansion (expanded);
2543 pstate->prev_lexptr = pstate->lexptr;
2545 tokstart = pstate->lexptr;
2546 /* See if it is a special token of length 3. */
2547 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2548 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
2550 if ((tokentab3[i].flags & FLAG_CXX) != 0
2551 && par_state->language ()->la_language != language_cplus)
2554 pstate->lexptr += 3;
2555 yylval.opcode = tokentab3[i].opcode;
2556 return tokentab3[i].token;
2559 /* See if it is a special token of length 2. */
2560 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2561 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
2563 if ((tokentab2[i].flags & FLAG_CXX) != 0
2564 && par_state->language ()->la_language != language_cplus)
2567 pstate->lexptr += 2;
2568 yylval.opcode = tokentab2[i].opcode;
2569 if (tokentab2[i].token == ARROW)
2570 last_was_structop = 1;
2571 return tokentab2[i].token;
2574 switch (c = *tokstart)
2577 /* If we were just scanning the result of a macro expansion,
2578 then we need to resume scanning the original text.
2579 If we're parsing for field name completion, and the previous
2580 token allows such completion, return a COMPLETE token.
2581 Otherwise, we were already scanning the original text, and
2582 we're really done. */
2583 if (scanning_macro_expansion ())
2585 finished_macro_expansion ();
2588 else if (saw_name_at_eof)
2590 saw_name_at_eof = 0;
2593 else if (par_state->parse_completion && saw_structop)
2608 if (par_state->language ()->la_language == language_objc
2615 if (paren_depth == 0)
2622 if (pstate->comma_terminates
2624 && ! scanning_macro_expansion ())
2630 /* Might be a floating point number. */
2631 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
2633 last_was_structop = true;
2634 goto symbol; /* Nope, must be a symbol. */
2649 /* It's a number. */
2650 int got_dot = 0, got_e = 0, toktype;
2651 const char *p = tokstart;
2652 int hex = input_radix > 10;
2654 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2659 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2667 /* This test includes !hex because 'e' is a valid hex digit
2668 and thus does not indicate a floating point number when
2669 the radix is hex. */
2670 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2671 got_dot = got_e = 1;
2672 /* This test does not include !hex, because a '.' always indicates
2673 a decimal floating point number regardless of the radix. */
2674 else if (!got_dot && *p == '.')
2676 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2677 && (*p == '-' || *p == '+'))
2678 /* This is the sign of the exponent, not the end of the
2681 /* We will take any letters or digits. parse_number will
2682 complain if past the radix, or if L or U are not final. */
2683 else if ((*p < '0' || *p > '9')
2684 && ((*p < 'a' || *p > 'z')
2685 && (*p < 'A' || *p > 'Z')))
2688 toktype = parse_number (par_state, tokstart, p - tokstart,
2689 got_dot|got_e, &yylval);
2690 if (toktype == ERROR)
2692 char *err_copy = (char *) alloca (p - tokstart + 1);
2694 memcpy (err_copy, tokstart, p - tokstart);
2695 err_copy[p - tokstart] = 0;
2696 error (_("Invalid number \"%s\"."), err_copy);
2704 const char *p = &tokstart[1];
2706 if (par_state->language ()->la_language == language_objc)
2708 size_t len = strlen ("selector");
2710 if (strncmp (p, "selector", len) == 0
2711 && (p[len] == '\0' || ISSPACE (p[len])))
2713 pstate->lexptr = p + len;
2720 while (ISSPACE (*p))
2722 size_t len = strlen ("entry");
2723 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
2726 pstate->lexptr = &p[len];
2755 if (tokstart[1] != '"' && tokstart[1] != '\'')
2764 int result = parse_string_or_char (tokstart, &pstate->lexptr,
2765 &yylval.tsval, &host_len);
2769 error (_("Empty character constant."));
2770 else if (host_len > 2 && c == '\'')
2773 namelen = pstate->lexptr - tokstart - 1;
2774 *is_quoted_name = true;
2778 else if (host_len > 1)
2779 error (_("Invalid character constant."));
2785 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
2786 /* We must have come across a bad character (e.g. ';'). */
2787 error (_("Invalid character '%c' in expression."), c);
2789 /* It's a name. See how long it is. */
2791 for (c = tokstart[namelen];
2792 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
2794 /* Template parameter lists are part of the name.
2795 FIXME: This mishandles `print $a<4&&$a>3'. */
2799 if (! is_cast_operator (tokstart, namelen))
2801 /* Scan ahead to get rest of the template specification. Note
2802 that we look ahead only when the '<' adjoins non-whitespace
2803 characters; for comparison expressions, e.g. "a < b > c",
2804 there must be spaces before the '<', etc. */
2805 const char *p = find_template_name_end (tokstart + namelen);
2808 namelen = p - tokstart;
2812 c = tokstart[++namelen];
2815 /* The token "if" terminates the expression and is NOT removed from
2816 the input stream. It doesn't count if it appears in the
2817 expansion of a macro. */
2819 && tokstart[0] == 'i'
2820 && tokstart[1] == 'f'
2821 && ! scanning_macro_expansion ())
2826 /* For the same reason (breakpoint conditions), "thread N"
2827 terminates the expression. "thread" could be an identifier, but
2828 an identifier is never followed by a number without intervening
2829 punctuation. "task" is similar. Handle abbreviations of these,
2830 similarly to breakpoint.c:find_condition_and_thread. */
2832 && (strncmp (tokstart, "thread", namelen) == 0
2833 || strncmp (tokstart, "task", namelen) == 0)
2834 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2835 && ! scanning_macro_expansion ())
2837 const char *p = tokstart + namelen + 1;
2839 while (*p == ' ' || *p == '\t')
2841 if (*p >= '0' && *p <= '9')
2845 pstate->lexptr += namelen;
2849 yylval.sval.ptr = tokstart;
2850 yylval.sval.length = namelen;
2852 /* Catch specific keywords. */
2853 std::string copy = copy_name (yylval.sval);
2854 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2855 if (copy == ident_tokens[i].oper)
2857 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2858 && par_state->language ()->la_language != language_cplus)
2861 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2863 struct field_of_this_result is_a_field_of_this;
2865 if (lookup_symbol (copy.c_str (),
2866 pstate->expression_context_block,
2868 (par_state->language ()->la_language
2869 == language_cplus ? &is_a_field_of_this
2873 /* The keyword is shadowed. */
2878 /* It is ok to always set this, even though we don't always
2879 strictly need to. */
2880 yylval.opcode = ident_tokens[i].opcode;
2881 return ident_tokens[i].token;
2884 if (*tokstart == '$')
2885 return DOLLAR_VARIABLE;
2887 if (pstate->parse_completion && *pstate->lexptr == '\0')
2888 saw_name_at_eof = 1;
2890 yylval.ssym.stoken = yylval.sval;
2891 yylval.ssym.sym.symbol = NULL;
2892 yylval.ssym.sym.block = NULL;
2893 yylval.ssym.is_a_field_of_this = 0;
2897 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2898 struct token_and_value
2904 /* A FIFO of tokens that have been read but not yet returned to the
2906 static std::vector<token_and_value> token_fifo;
2908 /* Non-zero if the lexer should return tokens from the FIFO. */
2911 /* Temporary storage for c_lex; this holds symbol names as they are
2913 auto_obstack name_obstack;
2915 /* Classify a NAME token. The contents of the token are in `yylval'.
2916 Updates yylval and returns the new token type. BLOCK is the block
2917 in which lookups start; this can be NULL to mean the global scope.
2918 IS_QUOTED_NAME is non-zero if the name token was originally quoted
2919 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
2920 a structure operator -- either '.' or ARROW */
2923 classify_name (struct parser_state *par_state, const struct block *block,
2924 bool is_quoted_name, bool is_after_structop)
2926 struct block_symbol bsym;
2927 struct field_of_this_result is_a_field_of_this;
2929 std::string copy = copy_name (yylval.sval);
2931 /* Initialize this in case we *don't* use it in this call; that way
2932 we can refer to it unconditionally below. */
2933 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
2935 bsym = lookup_symbol (copy.c_str (), block, VAR_DOMAIN,
2936 par_state->language ()->la_name_of_this
2937 ? &is_a_field_of_this : NULL);
2939 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK)
2941 yylval.ssym.sym = bsym;
2942 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2945 else if (!bsym.symbol)
2947 /* If we found a field of 'this', we might have erroneously
2948 found a constructor where we wanted a type name. Handle this
2949 case by noticing that we found a constructor and then look up
2950 the type tag instead. */
2951 if (is_a_field_of_this.type != NULL
2952 && is_a_field_of_this.fn_field != NULL
2953 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
2956 struct field_of_this_result inner_is_a_field_of_this;
2958 bsym = lookup_symbol (copy.c_str (), block, STRUCT_DOMAIN,
2959 &inner_is_a_field_of_this);
2960 if (bsym.symbol != NULL)
2962 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
2967 /* If we found a field on the "this" object, or we are looking
2968 up a field on a struct, then we want to prefer it over a
2969 filename. However, if the name was quoted, then it is better
2970 to check for a filename or a block, since this is the only
2971 way the user has of requiring the extension to be used. */
2972 if ((is_a_field_of_this.type == NULL && !is_after_structop)
2975 /* See if it's a file name. */
2976 struct symtab *symtab;
2978 symtab = lookup_symtab (copy.c_str ());
2981 yylval.bval = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab),
2988 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF)
2990 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
2994 /* See if it's an ObjC classname. */
2995 if (par_state->language ()->la_language == language_objc && !bsym.symbol)
2997 CORE_ADDR Class = lookup_objc_class (par_state->gdbarch (),
3003 yylval.theclass.theclass = Class;
3004 sym = lookup_struct_typedef (copy.c_str (),
3005 par_state->expression_context_block, 1);
3007 yylval.theclass.type = SYMBOL_TYPE (sym);
3012 /* Input names that aren't symbols but ARE valid hex numbers, when
3013 the input radix permits them, can be names or numbers depending
3014 on the parse. Note we support radixes > 16 here. */
3016 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3017 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3019 YYSTYPE newlval; /* Its value is ignored. */
3020 int hextype = parse_number (par_state, copy.c_str (), yylval.sval.length,
3025 yylval.ssym.sym = bsym;
3026 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3031 /* Any other kind of symbol */
3032 yylval.ssym.sym = bsym;
3033 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3035 if (bsym.symbol == NULL
3036 && par_state->language ()->la_language == language_cplus
3037 && is_a_field_of_this.type == NULL
3038 && lookup_minimal_symbol (copy.c_str (), NULL, NULL).minsym == NULL)
3039 return UNKNOWN_CPP_NAME;
3044 /* Like classify_name, but used by the inner loop of the lexer, when a
3045 name might have already been seen. CONTEXT is the context type, or
3046 NULL if this is the first component of a name. */
3049 classify_inner_name (struct parser_state *par_state,
3050 const struct block *block, struct type *context)
3054 if (context == NULL)
3055 return classify_name (par_state, block, false, false);
3057 type = check_typedef (context);
3058 if (!type_aggregate_p (type))
3061 std::string copy = copy_name (yylval.ssym.stoken);
3062 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3063 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy.c_str (), block,
3066 /* If no symbol was found, search for a matching base class named
3067 COPY. This will allow users to enter qualified names of class members
3068 relative to the `this' pointer. */
3069 if (yylval.ssym.sym.symbol == NULL)
3071 struct type *base_type = cp_find_type_baseclass_by_name (type,
3074 if (base_type != NULL)
3076 yylval.tsym.type = base_type;
3083 switch (SYMBOL_CLASS (yylval.ssym.sym.symbol))
3087 /* cp_lookup_nested_symbol might have accidentally found a constructor
3088 named COPY when we really wanted a base class of the same name.
3089 Double-check this case by looking for a base class. */
3091 struct type *base_type
3092 = cp_find_type_baseclass_by_name (type, copy.c_str ());
3094 if (base_type != NULL)
3096 yylval.tsym.type = base_type;
3103 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
3109 internal_error (__FILE__, __LINE__, _("not reached"));
3112 /* The outer level of a two-level lexer. This calls the inner lexer
3113 to return tokens. It then either returns these tokens, or
3114 aggregates them into a larger token. This lets us work around a
3115 problem in our parsing approach, where the parser could not
3116 distinguish between qualified names and qualified types at the
3119 This approach is still not ideal, because it mishandles template
3120 types. See the comment in lex_one_token for an example. However,
3121 this is still an improvement over the earlier approach, and will
3122 suffice until we move to better parsing technology. */
3127 token_and_value current;
3128 int first_was_coloncolon, last_was_coloncolon;
3129 struct type *context_type = NULL;
3130 int last_to_examine, next_to_examine, checkpoint;
3131 const struct block *search_block;
3132 bool is_quoted_name, last_lex_was_structop;
3134 if (popping && !token_fifo.empty ())
3138 last_lex_was_structop = last_was_structop;
3140 /* Read the first token and decide what to do. Most of the
3141 subsequent code is C++-only; but also depends on seeing a "::" or
3143 current.token = lex_one_token (pstate, &is_quoted_name);
3144 if (current.token == NAME)
3145 current.token = classify_name (pstate, pstate->expression_context_block,
3146 is_quoted_name, last_lex_was_structop);
3147 if (pstate->language ()->la_language != language_cplus
3148 || (current.token != TYPENAME && current.token != COLONCOLON
3149 && current.token != FILENAME))
3150 return current.token;
3152 /* Read any sequence of alternating "::" and name-like tokens into
3154 current.value = yylval;
3155 token_fifo.push_back (current);
3156 last_was_coloncolon = current.token == COLONCOLON;
3161 /* We ignore quoted names other than the very first one.
3162 Subsequent ones do not have any special meaning. */
3163 current.token = lex_one_token (pstate, &ignore);
3164 current.value = yylval;
3165 token_fifo.push_back (current);
3167 if ((last_was_coloncolon && current.token != NAME)
3168 || (!last_was_coloncolon && current.token != COLONCOLON))
3170 last_was_coloncolon = !last_was_coloncolon;
3174 /* We always read one extra token, so compute the number of tokens
3175 to examine accordingly. */
3176 last_to_examine = token_fifo.size () - 2;
3177 next_to_examine = 0;
3179 current = token_fifo[next_to_examine];
3182 name_obstack.clear ();
3184 if (current.token == FILENAME)
3185 search_block = current.value.bval;
3186 else if (current.token == COLONCOLON)
3187 search_block = NULL;
3190 gdb_assert (current.token == TYPENAME);
3191 search_block = pstate->expression_context_block;
3192 obstack_grow (&name_obstack, current.value.sval.ptr,
3193 current.value.sval.length);
3194 context_type = current.value.tsym.type;
3198 first_was_coloncolon = current.token == COLONCOLON;
3199 last_was_coloncolon = first_was_coloncolon;
3201 while (next_to_examine <= last_to_examine)
3203 token_and_value next;
3205 next = token_fifo[next_to_examine];
3208 if (next.token == NAME && last_was_coloncolon)
3212 yylval = next.value;
3213 classification = classify_inner_name (pstate, search_block,
3215 /* We keep going until we either run out of names, or until
3216 we have a qualified name which is not a type. */
3217 if (classification != TYPENAME && classification != NAME)
3220 /* Accept up to this token. */
3221 checkpoint = next_to_examine;
3223 /* Update the partial name we are constructing. */
3224 if (context_type != NULL)
3226 /* We don't want to put a leading "::" into the name. */
3227 obstack_grow_str (&name_obstack, "::");
3229 obstack_grow (&name_obstack, next.value.sval.ptr,
3230 next.value.sval.length);
3232 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
3233 yylval.sval.length = obstack_object_size (&name_obstack);
3234 current.value = yylval;
3235 current.token = classification;
3237 last_was_coloncolon = 0;
3239 if (classification == NAME)
3242 context_type = yylval.tsym.type;
3244 else if (next.token == COLONCOLON && !last_was_coloncolon)
3245 last_was_coloncolon = 1;
3248 /* We've reached the end of the name. */
3253 /* If we have a replacement token, install it as the first token in
3254 the FIFO, and delete the other constituent tokens. */
3257 current.value.sval.ptr
3258 = obstack_strndup (&cpstate->expansion_obstack,
3259 current.value.sval.ptr,
3260 current.value.sval.length);
3262 token_fifo[0] = current;
3264 token_fifo.erase (token_fifo.begin () + 1,
3265 token_fifo.begin () + checkpoint);
3269 current = token_fifo[0];
3270 token_fifo.erase (token_fifo.begin ());
3271 yylval = current.value;
3272 return current.token;
3276 c_parse (struct parser_state *par_state)
3278 /* Setting up the parser state. */
3279 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3280 gdb_assert (par_state != NULL);
3283 c_parse_state cstate;
3284 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3286 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
3288 if (par_state->expression_context_block)
3290 = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
3292 macro_scope = default_macro_scope ();
3294 macro_scope = user_macro_scope ();
3296 scoped_restore restore_macro_scope
3297 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
3299 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3302 /* Initialize some state used by the lexer. */
3303 last_was_structop = false;
3304 saw_name_at_eof = 0;
3307 token_fifo.clear ();
3309 name_obstack.clear ();
3316 /* This is called via the YYPRINT macro when parser debugging is
3317 enabled. It prints a token's value. */
3320 c_print_token (FILE *file, int type, YYSTYPE value)
3325 parser_fprintf (file, "typed_val_int<%s, %s>",
3326 TYPE_SAFE_NAME (value.typed_val_int.type),
3327 pulongest (value.typed_val_int.val));
3333 char *copy = (char *) alloca (value.tsval.length + 1);
3335 memcpy (copy, value.tsval.ptr, value.tsval.length);
3336 copy[value.tsval.length] = '\0';
3338 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3343 case DOLLAR_VARIABLE:
3344 parser_fprintf (file, "sval<%s>", copy_name (value.sval).c_str ());
3348 parser_fprintf (file, "tsym<type=%s, name=%s>",
3349 TYPE_SAFE_NAME (value.tsym.type),
3350 copy_name (value.tsym.stoken).c_str ());
3354 case UNKNOWN_CPP_NAME:
3357 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3358 copy_name (value.ssym.stoken).c_str (),
3359 (value.ssym.sym.symbol == NULL
3360 ? "(null)" : value.ssym.sym.symbol->print_name ()),
3361 value.ssym.is_a_field_of_this);
3365 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3373 yyerror (const char *msg)
3375 if (pstate->prev_lexptr)
3376 pstate->lexptr = pstate->prev_lexptr;
3378 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);