1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986-2019 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 theclass = lookup_objc_class (pstate->gdbarch (),
478 copy_name ($2.stoken));
480 error (_("%s is not an ObjC Class"),
481 copy_name ($2.stoken));
482 write_exp_elt_opcode (pstate, OP_LONG);
483 write_exp_elt_type (pstate,
484 parse_type (pstate)->builtin_int);
485 write_exp_elt_longcst (pstate, (LONGEST) theclass);
486 write_exp_elt_opcode (pstate, OP_LONG);
490 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
491 end_msglist (pstate);
492 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
496 exp : OBJC_LBRAC CLASSNAME
498 write_exp_elt_opcode (pstate, OP_LONG);
499 write_exp_elt_type (pstate,
500 parse_type (pstate)->builtin_int);
501 write_exp_elt_longcst (pstate, (LONGEST) $2.theclass);
502 write_exp_elt_opcode (pstate, OP_LONG);
506 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
507 end_msglist (pstate);
508 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
515 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
516 end_msglist (pstate);
517 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
522 { add_msglist(&$1, 0); }
530 msgarg : name ':' exp
531 { add_msglist(&$1, 1); }
532 | ':' exp /* Unnamed arg. */
533 { add_msglist(0, 1); }
534 | ',' exp /* Variable number of args. */
535 { add_msglist(0, 0); }
539 /* This is to save the value of arglist_len
540 being accumulated by an outer function call. */
541 { pstate->start_arglist (); }
542 arglist ')' %prec ARROW
543 { write_exp_elt_opcode (pstate, OP_FUNCALL);
544 write_exp_elt_longcst (pstate,
545 pstate->end_arglist ());
546 write_exp_elt_opcode (pstate, OP_FUNCALL); }
549 /* This is here to disambiguate with the production for
550 "func()::static_var" further below, which uses
551 function_method_void. */
552 exp : exp '(' ')' %prec ARROW
553 { pstate->start_arglist ();
554 write_exp_elt_opcode (pstate, OP_FUNCALL);
555 write_exp_elt_longcst (pstate,
556 pstate->end_arglist ());
557 write_exp_elt_opcode (pstate, OP_FUNCALL); }
561 exp : UNKNOWN_CPP_NAME '('
563 /* This could potentially be a an argument defined
564 lookup function (Koenig). */
565 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
567 (pstate, pstate->expression_context_block);
568 write_exp_elt_sym (pstate,
569 NULL); /* Placeholder. */
570 write_exp_string (pstate, $1.stoken);
571 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
573 /* This is to save the value of arglist_len
574 being accumulated by an outer function call. */
576 pstate->start_arglist ();
578 arglist ')' %prec ARROW
580 write_exp_elt_opcode (pstate, OP_FUNCALL);
581 write_exp_elt_longcst (pstate,
582 pstate->end_arglist ());
583 write_exp_elt_opcode (pstate, OP_FUNCALL);
588 { pstate->start_arglist (); }
595 { pstate->arglist_len = 1; }
598 arglist : arglist ',' exp %prec ABOVE_COMMA
599 { pstate->arglist_len++; }
602 function_method: exp '(' parameter_typelist ')' const_or_volatile
604 std::vector<struct type *> *type_list = $3;
605 LONGEST len = type_list->size ();
607 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
608 /* Save the const/volatile qualifiers as
609 recorded by the const_or_volatile
610 production's actions. */
611 write_exp_elt_longcst
614 .follow_type_instance_flags ()));
615 write_exp_elt_longcst (pstate, len);
616 for (type *type_elt : *type_list)
617 write_exp_elt_type (pstate, type_elt);
618 write_exp_elt_longcst(pstate, len);
619 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
623 function_method_void: exp '(' ')' const_or_volatile
624 { write_exp_elt_opcode (pstate, TYPE_INSTANCE);
626 write_exp_elt_longcst
628 cpstate->type_stack.follow_type_instance_flags ());
629 write_exp_elt_longcst (pstate, 0);
630 write_exp_elt_longcst (pstate, 0);
631 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
635 exp : function_method
638 /* Normally we must interpret "func()" as a function call, instead of
639 a type. The user needs to write func(void) to disambiguate.
640 However, in the "func()::static_var" case, there's no
642 function_method_void_or_typelist: function_method
643 | function_method_void
646 exp : function_method_void_or_typelist COLONCOLON name
648 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
649 write_exp_string (pstate, $3);
650 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
655 { $$ = pstate->end_arglist () - 1; }
657 exp : lcurly arglist rcurly %prec ARROW
658 { write_exp_elt_opcode (pstate, OP_ARRAY);
659 write_exp_elt_longcst (pstate, (LONGEST) 0);
660 write_exp_elt_longcst (pstate, (LONGEST) $3);
661 write_exp_elt_opcode (pstate, OP_ARRAY); }
664 exp : lcurly type_exp rcurly exp %prec UNARY
665 { write_exp_elt_opcode (pstate, UNOP_MEMVAL_TYPE); }
668 exp : '(' type_exp ')' exp %prec UNARY
669 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
676 /* Binary operators in order of decreasing precedence. */
679 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
683 { write_exp_elt_opcode (pstate, BINOP_MUL); }
687 { write_exp_elt_opcode (pstate, BINOP_DIV); }
691 { write_exp_elt_opcode (pstate, BINOP_REM); }
695 { write_exp_elt_opcode (pstate, BINOP_ADD); }
699 { write_exp_elt_opcode (pstate, BINOP_SUB); }
703 { write_exp_elt_opcode (pstate, BINOP_LSH); }
707 { write_exp_elt_opcode (pstate, BINOP_RSH); }
711 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
714 exp : exp NOTEQUAL exp
715 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
719 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
723 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
727 { write_exp_elt_opcode (pstate, BINOP_LESS); }
731 { write_exp_elt_opcode (pstate, BINOP_GTR); }
735 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
739 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
743 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
747 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
751 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
754 exp : exp '?' exp ':' exp %prec '?'
755 { write_exp_elt_opcode (pstate, TERNOP_COND); }
759 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
762 exp : exp ASSIGN_MODIFY exp
763 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
764 write_exp_elt_opcode (pstate, $2);
765 write_exp_elt_opcode (pstate,
766 BINOP_ASSIGN_MODIFY); }
770 { write_exp_elt_opcode (pstate, OP_LONG);
771 write_exp_elt_type (pstate, $1.type);
772 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
773 write_exp_elt_opcode (pstate, OP_LONG); }
778 struct stoken_vector vec;
781 write_exp_string_vector (pstate, $1.type, &vec);
787 parse_number (pstate, $1.stoken.ptr,
788 $1.stoken.length, 0, &val);
789 write_exp_elt_opcode (pstate, OP_LONG);
790 write_exp_elt_type (pstate, val.typed_val_int.type);
791 write_exp_elt_longcst (pstate,
792 (LONGEST) val.typed_val_int.val);
793 write_exp_elt_opcode (pstate, OP_LONG);
799 { write_exp_elt_opcode (pstate, OP_FLOAT);
800 write_exp_elt_type (pstate, $1.type);
801 write_exp_elt_floatcst (pstate, $1.val);
802 write_exp_elt_opcode (pstate, OP_FLOAT); }
808 exp : DOLLAR_VARIABLE
810 write_dollar_variable (pstate, $1);
814 exp : SELECTOR '(' name ')'
816 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR);
817 write_exp_string (pstate, $3);
818 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR); }
821 exp : SIZEOF '(' type ')' %prec UNARY
822 { struct type *type = $3;
823 write_exp_elt_opcode (pstate, OP_LONG);
824 write_exp_elt_type (pstate, lookup_signed_typename
825 (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));
970 block : block COLONCOLON name
972 = lookup_symbol (copy_name ($3), $1,
973 VAR_DOMAIN, NULL).symbol;
975 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
976 error (_("No function \"%s\" in specified context."),
978 $$ = SYMBOL_BLOCK_VALUE (tem); }
981 variable: name_not_typename ENTRY
982 { struct symbol *sym = $1.sym.symbol;
984 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
985 || !symbol_read_needs_frame (sym))
986 error (_("@entry can be used only for function "
987 "parameters, not for \"%s\""),
988 copy_name ($1.stoken));
990 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
991 write_exp_elt_sym (pstate, sym);
992 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
996 variable: block COLONCOLON name
997 { struct block_symbol sym
998 = lookup_symbol (copy_name ($3), $1,
1001 if (sym.symbol == 0)
1002 error (_("No symbol \"%s\" in specified context."),
1004 if (symbol_read_needs_frame (sym.symbol))
1006 innermost_block.update (sym);
1008 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1009 write_exp_elt_block (pstate, sym.block);
1010 write_exp_elt_sym (pstate, sym.symbol);
1011 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
1014 qualified_name: TYPENAME COLONCOLON name
1016 struct type *type = $1.type;
1017 type = check_typedef (type);
1018 if (!type_aggregate_p (type))
1019 error (_("`%s' is not defined as an aggregate type."),
1020 TYPE_SAFE_NAME (type));
1022 write_exp_elt_opcode (pstate, OP_SCOPE);
1023 write_exp_elt_type (pstate, type);
1024 write_exp_string (pstate, $3);
1025 write_exp_elt_opcode (pstate, OP_SCOPE);
1027 | TYPENAME COLONCOLON '~' name
1029 struct type *type = $1.type;
1030 struct stoken tmp_token;
1033 type = check_typedef (type);
1034 if (!type_aggregate_p (type))
1035 error (_("`%s' is not defined as an aggregate type."),
1036 TYPE_SAFE_NAME (type));
1037 buf = (char *) alloca ($4.length + 2);
1038 tmp_token.ptr = buf;
1039 tmp_token.length = $4.length + 1;
1041 memcpy (buf+1, $4.ptr, $4.length);
1042 buf[tmp_token.length] = 0;
1044 /* Check for valid destructor name. */
1045 destructor_name_p (tmp_token.ptr, $1.type);
1046 write_exp_elt_opcode (pstate, OP_SCOPE);
1047 write_exp_elt_type (pstate, type);
1048 write_exp_string (pstate, tmp_token);
1049 write_exp_elt_opcode (pstate, OP_SCOPE);
1051 | TYPENAME COLONCOLON name COLONCOLON name
1053 char *copy = copy_name ($3);
1054 error (_("No type \"%s\" within class "
1055 "or namespace \"%s\"."),
1056 copy, TYPE_SAFE_NAME ($1.type));
1060 variable: qualified_name
1061 | COLONCOLON name_not_typename
1063 char *name = copy_name ($2.stoken);
1065 struct bound_minimal_symbol msymbol;
1068 = lookup_symbol (name, (const struct block *) NULL,
1069 VAR_DOMAIN, NULL).symbol;
1072 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1073 write_exp_elt_block (pstate, NULL);
1074 write_exp_elt_sym (pstate, sym);
1075 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1079 msymbol = lookup_bound_minimal_symbol (name);
1080 if (msymbol.minsym != NULL)
1081 write_exp_msymbol (pstate, msymbol);
1082 else if (!have_full_symbols () && !have_partial_symbols ())
1083 error (_("No symbol table is loaded. Use the \"file\" command."));
1085 error (_("No symbol \"%s\" in current context."), name);
1089 variable: name_not_typename
1090 { struct block_symbol sym = $1.sym;
1094 if (symbol_read_needs_frame (sym.symbol))
1095 innermost_block.update (sym);
1097 /* If we found a function, see if it's
1098 an ifunc resolver that has the same
1099 address as the ifunc symbol itself.
1100 If so, prefer the ifunc symbol. */
1102 bound_minimal_symbol resolver
1103 = find_gnu_ifunc (sym.symbol);
1104 if (resolver.minsym != NULL)
1105 write_exp_msymbol (pstate, resolver);
1108 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1109 write_exp_elt_block (pstate, sym.block);
1110 write_exp_elt_sym (pstate, sym.symbol);
1111 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1114 else if ($1.is_a_field_of_this)
1116 /* C++: it hangs off of `this'. Must
1117 not inadvertently convert from a method call
1119 innermost_block.update (sym);
1120 write_exp_elt_opcode (pstate, OP_THIS);
1121 write_exp_elt_opcode (pstate, OP_THIS);
1122 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1123 write_exp_string (pstate, $1.stoken);
1124 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1128 char *arg = copy_name ($1.stoken);
1130 bound_minimal_symbol msymbol
1131 = lookup_bound_minimal_symbol (arg);
1132 if (msymbol.minsym == NULL)
1134 if (!have_full_symbols () && !have_partial_symbols ())
1135 error (_("No symbol table is loaded. Use the \"file\" command."));
1137 error (_("No symbol \"%s\" in current context."),
1138 copy_name ($1.stoken));
1141 /* This minsym might be an alias for
1142 another function. See if we can find
1143 the debug symbol for the target, and
1144 if so, use it instead, since it has
1145 return type / prototype info. This
1146 is important for example for "p
1147 *__errno_location()". */
1148 symbol *alias_target
1149 = ((msymbol.minsym->type != mst_text_gnu_ifunc
1150 && msymbol.minsym->type != mst_data_gnu_ifunc)
1151 ? find_function_alias_target (msymbol)
1153 if (alias_target != NULL)
1155 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1157 (pstate, SYMBOL_BLOCK_VALUE (alias_target));
1158 write_exp_elt_sym (pstate, alias_target);
1159 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1162 write_exp_msymbol (pstate, msymbol);
1167 space_identifier : '@' NAME
1169 cpstate->type_stack.insert (pstate, copy_name ($2.stoken));
1173 const_or_volatile: const_or_volatile_noopt
1177 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
1180 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
1181 | const_or_volatile_noopt
1184 const_or_volatile_or_space_identifier:
1185 const_or_volatile_or_space_identifier_noopt
1191 { cpstate->type_stack.insert (tp_pointer); }
1192 const_or_volatile_or_space_identifier
1194 { cpstate->type_stack.insert (tp_pointer); }
1195 const_or_volatile_or_space_identifier
1197 { cpstate->type_stack.insert (tp_reference); }
1199 { cpstate->type_stack.insert (tp_reference); }
1201 { cpstate->type_stack.insert (tp_rvalue_reference); }
1202 | ANDAND ptr_operator
1203 { cpstate->type_stack.insert (tp_rvalue_reference); }
1206 ptr_operator_ts: ptr_operator
1208 $$ = cpstate->type_stack.create ();
1209 cpstate->type_stacks.emplace_back ($$);
1213 abs_decl: ptr_operator_ts direct_abs_decl
1214 { $$ = $2->append ($1); }
1219 direct_abs_decl: '(' abs_decl ')'
1221 | direct_abs_decl array_mod
1223 cpstate->type_stack.push ($1);
1224 cpstate->type_stack.push ($2);
1225 cpstate->type_stack.push (tp_array);
1226 $$ = cpstate->type_stack.create ();
1227 cpstate->type_stacks.emplace_back ($$);
1231 cpstate->type_stack.push ($1);
1232 cpstate->type_stack.push (tp_array);
1233 $$ = cpstate->type_stack.create ();
1234 cpstate->type_stacks.emplace_back ($$);
1237 | direct_abs_decl func_mod
1239 cpstate->type_stack.push ($1);
1240 cpstate->type_stack.push ($2);
1241 $$ = cpstate->type_stack.create ();
1242 cpstate->type_stacks.emplace_back ($$);
1246 cpstate->type_stack.push ($1);
1247 $$ = cpstate->type_stack.create ();
1248 cpstate->type_stacks.emplace_back ($$);
1258 | OBJC_LBRAC INT ']'
1264 $$ = new std::vector<struct type *>;
1265 cpstate->type_lists.emplace_back ($$);
1267 | '(' parameter_typelist ')'
1271 /* We used to try to recognize pointer to member types here, but
1272 that didn't work (shift/reduce conflicts meant that these rules never
1273 got executed). The problem is that
1274 int (foo::bar::baz::bizzle)
1275 is a function type but
1276 int (foo::bar::baz::bizzle::*)
1277 is a pointer to member type. Stroustrup loses again! */
1282 /* Implements (approximately): (type-qualifier)* type-specifier.
1284 When type-specifier is only ever a single word, like 'float' then these
1285 arrive as pre-built TYPENAME tokens thanks to the classify_name
1286 function. However, when a type-specifier can contain multiple words,
1287 for example 'double' can appear as just 'double' or 'long double', and
1288 similarly 'long' can appear as just 'long' or in 'long double', then
1289 these type-specifiers are parsed into their own tokens in the function
1290 lex_one_token and the ident_tokens array. These separate tokens are all
1296 { $$ = lookup_signed_typename (pstate->language (),
1300 { $$ = lookup_signed_typename (pstate->language (),
1304 { $$ = lookup_signed_typename (pstate->language (),
1308 { $$ = lookup_signed_typename (pstate->language (),
1311 | LONG SIGNED_KEYWORD INT_KEYWORD
1312 { $$ = lookup_signed_typename (pstate->language (),
1315 | LONG SIGNED_KEYWORD
1316 { $$ = lookup_signed_typename (pstate->language (),
1319 | SIGNED_KEYWORD LONG INT_KEYWORD
1320 { $$ = lookup_signed_typename (pstate->language (),
1323 | UNSIGNED LONG INT_KEYWORD
1324 { $$ = lookup_unsigned_typename (pstate->language (),
1327 | LONG UNSIGNED INT_KEYWORD
1328 { $$ = lookup_unsigned_typename (pstate->language (),
1332 { $$ = lookup_unsigned_typename (pstate->language (),
1336 { $$ = lookup_signed_typename (pstate->language (),
1339 | LONG LONG INT_KEYWORD
1340 { $$ = lookup_signed_typename (pstate->language (),
1343 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1344 { $$ = lookup_signed_typename (pstate->language (),
1347 | LONG LONG SIGNED_KEYWORD
1348 { $$ = lookup_signed_typename (pstate->language (),
1351 | SIGNED_KEYWORD LONG LONG
1352 { $$ = lookup_signed_typename (pstate->language (),
1355 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1356 { $$ = lookup_signed_typename (pstate->language (),
1359 | UNSIGNED LONG LONG
1360 { $$ = lookup_unsigned_typename (pstate->language (),
1363 | UNSIGNED LONG LONG INT_KEYWORD
1364 { $$ = lookup_unsigned_typename (pstate->language (),
1367 | LONG LONG UNSIGNED
1368 { $$ = lookup_unsigned_typename (pstate->language (),
1371 | LONG LONG UNSIGNED INT_KEYWORD
1372 { $$ = lookup_unsigned_typename (pstate->language (),
1376 { $$ = lookup_signed_typename (pstate->language (),
1379 | SHORT SIGNED_KEYWORD INT_KEYWORD
1380 { $$ = lookup_signed_typename (pstate->language (),
1383 | SHORT SIGNED_KEYWORD
1384 { $$ = lookup_signed_typename (pstate->language (),
1387 | UNSIGNED SHORT INT_KEYWORD
1388 { $$ = lookup_unsigned_typename (pstate->language (),
1392 { $$ = lookup_unsigned_typename (pstate->language (),
1395 | SHORT UNSIGNED INT_KEYWORD
1396 { $$ = lookup_unsigned_typename (pstate->language (),
1400 { $$ = lookup_typename (pstate->language (),
1405 | LONG DOUBLE_KEYWORD
1406 { $$ = lookup_typename (pstate->language (),
1413 = lookup_struct (copy_name ($2),
1414 pstate->expression_context_block);
1418 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1422 | STRUCT name COMPLETE
1424 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1429 { $$ = lookup_struct
1430 (copy_name ($2), pstate->expression_context_block);
1434 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1438 | CLASS name COMPLETE
1440 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1446 = lookup_union (copy_name ($2),
1447 pstate->expression_context_block);
1451 pstate->mark_completion_tag (TYPE_CODE_UNION,
1455 | UNION name COMPLETE
1457 pstate->mark_completion_tag (TYPE_CODE_UNION,
1462 { $$ = lookup_enum (copy_name ($2),
1463 pstate->expression_context_block);
1467 pstate->mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1470 | ENUM name COMPLETE
1472 pstate->mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1476 | UNSIGNED type_name
1477 { $$ = lookup_unsigned_typename (pstate->language (),
1479 TYPE_NAME($2.type)); }
1481 { $$ = lookup_unsigned_typename (pstate->language (),
1484 | SIGNED_KEYWORD type_name
1485 { $$ = lookup_signed_typename (pstate->language (),
1487 TYPE_NAME($2.type)); }
1489 { $$ = lookup_signed_typename (pstate->language (),
1492 /* It appears that this rule for templates is never
1493 reduced; template recognition happens by lookahead
1494 in the token processing code in yylex. */
1495 | TEMPLATE name '<' type '>'
1496 { $$ = lookup_template_type
1498 pstate->expression_context_block);
1500 | const_or_volatile_or_space_identifier_noopt typebase
1501 { $$ = cpstate->type_stack.follow_types ($2); }
1502 | typebase const_or_volatile_or_space_identifier_noopt
1503 { $$ = cpstate->type_stack.follow_types ($1); }
1509 $$.stoken.ptr = "int";
1510 $$.stoken.length = 3;
1511 $$.type = lookup_signed_typename (pstate->language (),
1517 $$.stoken.ptr = "long";
1518 $$.stoken.length = 4;
1519 $$.type = lookup_signed_typename (pstate->language (),
1525 $$.stoken.ptr = "short";
1526 $$.stoken.length = 5;
1527 $$.type = lookup_signed_typename (pstate->language (),
1535 { check_parameter_typelist ($1); }
1536 | nonempty_typelist ',' DOTDOTDOT
1538 $1->push_back (NULL);
1539 check_parameter_typelist ($1);
1547 std::vector<struct type *> *typelist
1548 = new std::vector<struct type *>;
1549 cpstate->type_lists.emplace_back (typelist);
1551 typelist->push_back ($1);
1554 | nonempty_typelist ',' type
1564 cpstate->type_stack.push ($2);
1565 $$ = cpstate->type_stack.follow_types ($1);
1569 conversion_type_id: typebase conversion_declarator
1570 { $$ = cpstate->type_stack.follow_types ($1); }
1573 conversion_declarator: /* Nothing. */
1574 | ptr_operator conversion_declarator
1577 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1578 | VOLATILE_KEYWORD CONST_KEYWORD
1581 const_or_volatile_noopt: const_and_volatile
1582 { cpstate->type_stack.insert (tp_const);
1583 cpstate->type_stack.insert (tp_volatile);
1586 { cpstate->type_stack.insert (tp_const); }
1588 { cpstate->type_stack.insert (tp_volatile); }
1592 { $$ = operator_stoken (" new"); }
1594 { $$ = operator_stoken (" delete"); }
1595 | OPERATOR NEW '[' ']'
1596 { $$ = operator_stoken (" new[]"); }
1597 | OPERATOR DELETE '[' ']'
1598 { $$ = operator_stoken (" delete[]"); }
1599 | OPERATOR NEW OBJC_LBRAC ']'
1600 { $$ = operator_stoken (" new[]"); }
1601 | OPERATOR DELETE OBJC_LBRAC ']'
1602 { $$ = operator_stoken (" delete[]"); }
1604 { $$ = operator_stoken ("+"); }
1606 { $$ = operator_stoken ("-"); }
1608 { $$ = operator_stoken ("*"); }
1610 { $$ = operator_stoken ("/"); }
1612 { $$ = operator_stoken ("%"); }
1614 { $$ = operator_stoken ("^"); }
1616 { $$ = operator_stoken ("&"); }
1618 { $$ = operator_stoken ("|"); }
1620 { $$ = operator_stoken ("~"); }
1622 { $$ = operator_stoken ("!"); }
1624 { $$ = operator_stoken ("="); }
1626 { $$ = operator_stoken ("<"); }
1628 { $$ = operator_stoken (">"); }
1629 | OPERATOR ASSIGN_MODIFY
1630 { const char *op = " unknown";
1654 case BINOP_BITWISE_IOR:
1657 case BINOP_BITWISE_AND:
1660 case BINOP_BITWISE_XOR:
1667 $$ = operator_stoken (op);
1670 { $$ = operator_stoken ("<<"); }
1672 { $$ = operator_stoken (">>"); }
1674 { $$ = operator_stoken ("=="); }
1676 { $$ = operator_stoken ("!="); }
1678 { $$ = operator_stoken ("<="); }
1680 { $$ = operator_stoken (">="); }
1682 { $$ = operator_stoken ("&&"); }
1684 { $$ = operator_stoken ("||"); }
1685 | OPERATOR INCREMENT
1686 { $$ = operator_stoken ("++"); }
1687 | OPERATOR DECREMENT
1688 { $$ = operator_stoken ("--"); }
1690 { $$ = operator_stoken (","); }
1691 | OPERATOR ARROW_STAR
1692 { $$ = operator_stoken ("->*"); }
1694 { $$ = operator_stoken ("->"); }
1696 { $$ = operator_stoken ("()"); }
1698 { $$ = operator_stoken ("[]"); }
1699 | OPERATOR OBJC_LBRAC ']'
1700 { $$ = operator_stoken ("[]"); }
1701 | OPERATOR conversion_type_id
1704 c_print_type ($2, NULL, &buf, -1, 0,
1705 &type_print_raw_options);
1707 /* This also needs canonicalization. */
1709 = cp_canonicalize_string (buf.c_str ());
1711 canon = std::move (buf.string ());
1712 $$ = operator_stoken ((" " + canon).c_str ());
1716 /* This rule exists in order to allow some tokens that would not normally
1717 match the 'name' rule to appear as fields within a struct. The example
1718 that initially motivated this was the RISC-V target which models the
1719 floating point registers as a union with fields called 'float' and
1720 'double'. The 'float' string becomes a TYPENAME token and can appear
1721 anywhere a 'name' can, however 'double' is its own token,
1722 DOUBLE_KEYWORD, and doesn't match the 'name' rule.*/
1725 | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
1726 | INT_KEYWORD { $$ = typename_stoken ("int"); }
1727 | LONG { $$ = typename_stoken ("long"); }
1728 | SHORT { $$ = typename_stoken ("short"); }
1729 | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
1730 | UNSIGNED { $$ = typename_stoken ("unsigned"); }
1733 name : NAME { $$ = $1.stoken; }
1734 | BLOCKNAME { $$ = $1.stoken; }
1735 | TYPENAME { $$ = $1.stoken; }
1736 | NAME_OR_INT { $$ = $1.stoken; }
1737 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1741 name_not_typename : NAME
1743 /* These would be useful if name_not_typename was useful, but it is just
1744 a fake for "variable", so these cause reduce/reduce conflicts because
1745 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1746 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1747 context where only a name could occur, this might be useful.
1752 struct field_of_this_result is_a_field_of_this;
1756 = lookup_symbol ($1.ptr,
1757 pstate->expression_context_block,
1759 &is_a_field_of_this);
1760 $$.is_a_field_of_this
1761 = is_a_field_of_this.type != NULL;
1768 /* Like write_exp_string, but prepends a '~'. */
1771 write_destructor_name (struct parser_state *par_state, struct stoken token)
1773 char *copy = (char *) alloca (token.length + 1);
1776 memcpy (©[1], token.ptr, token.length);
1781 write_exp_string (par_state, token);
1784 /* Returns a stoken of the operator name given by OP (which does not
1785 include the string "operator"). */
1787 static struct stoken
1788 operator_stoken (const char *op)
1790 struct stoken st = { NULL, 0 };
1793 st.length = CP_OPERATOR_LEN + strlen (op);
1794 buf = (char *) malloc (st.length + 1);
1795 strcpy (buf, CP_OPERATOR_STR);
1799 /* The toplevel (c_parse) will free the memory allocated here. */
1800 cpstate->strings.emplace_back (buf);
1804 /* Returns a stoken of the type named TYPE. */
1806 static struct stoken
1807 typename_stoken (const char *type)
1809 struct stoken st = { type, 0 };
1810 st.length = strlen (type);
1814 /* Return true if the type is aggregate-like. */
1817 type_aggregate_p (struct type *type)
1819 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
1820 || TYPE_CODE (type) == TYPE_CODE_UNION
1821 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE
1822 || (TYPE_CODE (type) == TYPE_CODE_ENUM
1823 && TYPE_DECLARED_CLASS (type)));
1826 /* Validate a parameter typelist. */
1829 check_parameter_typelist (std::vector<struct type *> *params)
1834 for (ix = 0; ix < params->size (); ++ix)
1836 type = (*params)[ix];
1837 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1841 if (params->size () == 1)
1846 error (_("parameter types following 'void'"));
1849 error (_("'void' invalid as parameter type"));
1854 /* Take care of parsing a number (anything that starts with a digit).
1855 Set yylval and return the token type; update lexptr.
1856 LEN is the number of characters in it. */
1858 /*** Needs some error checking for the float case ***/
1861 parse_number (struct parser_state *par_state,
1862 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1870 int base = input_radix;
1873 /* Number of "L" suffixes encountered. */
1876 /* We have found a "L" or "U" suffix. */
1877 int found_suffix = 0;
1880 struct type *signed_type;
1881 struct type *unsigned_type;
1884 p = (char *) alloca (len);
1885 memcpy (p, buf, len);
1889 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
1890 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1892 putithere->typed_val_float.type
1893 = parse_type (par_state)->builtin_decfloat;
1896 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1898 putithere->typed_val_float.type
1899 = parse_type (par_state)->builtin_decdouble;
1902 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1904 putithere->typed_val_float.type
1905 = parse_type (par_state)->builtin_declong;
1908 /* Handle suffixes: 'f' for float, 'l' for long double. */
1909 else if (len >= 1 && TOLOWER (p[len - 1]) == 'f')
1911 putithere->typed_val_float.type
1912 = parse_type (par_state)->builtin_float;
1915 else if (len >= 1 && TOLOWER (p[len - 1]) == 'l')
1917 putithere->typed_val_float.type
1918 = parse_type (par_state)->builtin_long_double;
1921 /* Default type for floating-point literals is double. */
1924 putithere->typed_val_float.type
1925 = parse_type (par_state)->builtin_double;
1928 if (!parse_float (p, len,
1929 putithere->typed_val_float.type,
1930 putithere->typed_val_float.val))
1935 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1936 if (p[0] == '0' && len > 1)
1979 if (c >= 'A' && c <= 'Z')
1981 if (c != 'l' && c != 'u')
1983 if (c >= '0' && c <= '9')
1991 if (base > 10 && c >= 'a' && c <= 'f')
1995 n += i = c - 'a' + 10;
2008 return ERROR; /* Char not a digit */
2011 return ERROR; /* Invalid digit in this base */
2013 /* Portably test for overflow (only works for nonzero values, so make
2014 a second check for zero). FIXME: Can't we just make n and prevn
2015 unsigned and avoid this? */
2016 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
2017 unsigned_p = 1; /* Try something unsigned */
2019 /* Portably test for unsigned overflow.
2020 FIXME: This check is wrong; for example it doesn't find overflow
2021 on 0x123456789 when LONGEST is 32 bits. */
2022 if (c != 'l' && c != 'u' && n != 0)
2024 if (unsigned_p && prevn >= n)
2025 error (_("Numeric constant too large."));
2030 /* An integer constant is an int, a long, or a long long. An L
2031 suffix forces it to be long; an LL suffix forces it to be long
2032 long. If not forced to a larger size, it gets the first type of
2033 the above that it fits in. To figure out whether it fits, we
2034 shift it right and see whether anything remains. Note that we
2035 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
2036 operation, because many compilers will warn about such a shift
2037 (which always produces a zero result). Sometimes gdbarch_int_bit
2038 or gdbarch_long_bit will be that big, sometimes not. To deal with
2039 the case where it is we just always shift the value more than
2040 once, with fewer bits each time. */
2044 && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0)
2047 = ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
2049 /* A large decimal (not hex or octal) constant (between INT_MAX
2050 and UINT_MAX) is a long or unsigned long, according to ANSI,
2051 never an unsigned int, but this code treats it as unsigned
2052 int. This probably should be fixed. GCC gives a warning on
2055 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
2056 signed_type = parse_type (par_state)->builtin_int;
2058 else if (long_p <= 1
2059 && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0)
2062 = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1);
2063 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
2064 signed_type = parse_type (par_state)->builtin_long;
2069 if (sizeof (ULONGEST) * HOST_CHAR_BIT
2070 < gdbarch_long_long_bit (par_state->gdbarch ()))
2071 /* A long long does not fit in a LONGEST. */
2072 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
2074 shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1);
2075 high_bit = (ULONGEST) 1 << shift;
2076 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
2077 signed_type = parse_type (par_state)->builtin_long_long;
2080 putithere->typed_val_int.val = n;
2082 /* If the high bit of the worked out type is set then this number
2083 has to be unsigned. */
2085 if (unsigned_p || (n & high_bit))
2087 putithere->typed_val_int.type = unsigned_type;
2091 putithere->typed_val_int.type = signed_type;
2097 /* Temporary obstack used for holding strings. */
2098 static struct obstack tempbuf;
2099 static int tempbuf_init;
2101 /* Parse a C escape sequence. The initial backslash of the sequence
2102 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2103 last character of the sequence. If OUTPUT is not NULL, the
2104 translated form of the escape sequence will be written there. If
2105 OUTPUT is NULL, no output is written and the call will only affect
2106 *PTR. If an escape sequence is expressed in target bytes, then the
2107 entire sequence will simply be copied to OUTPUT. Return 1 if any
2108 character was emitted, 0 otherwise. */
2111 c_parse_escape (const char **ptr, struct obstack *output)
2113 const char *tokptr = *ptr;
2116 /* Some escape sequences undergo character set conversion. Those we
2120 /* Hex escapes do not undergo character set conversion, so keep
2121 the escape sequence for later. */
2124 obstack_grow_str (output, "\\x");
2126 if (!ISXDIGIT (*tokptr))
2127 error (_("\\x escape without a following hex digit"));
2128 while (ISXDIGIT (*tokptr))
2131 obstack_1grow (output, *tokptr);
2136 /* Octal escapes do not undergo character set conversion, so
2137 keep the escape sequence for later. */
2149 obstack_grow_str (output, "\\");
2151 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
2155 obstack_1grow (output, *tokptr);
2161 /* We handle UCNs later. We could handle them here, but that
2162 would mean a spurious error in the case where the UCN could
2163 be converted to the target charset but not the host
2169 int i, len = c == 'U' ? 8 : 4;
2172 obstack_1grow (output, '\\');
2173 obstack_1grow (output, *tokptr);
2176 if (!ISXDIGIT (*tokptr))
2177 error (_("\\%c escape without a following hex digit"), c);
2178 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
2181 obstack_1grow (output, *tokptr);
2187 /* We must pass backslash through so that it does not
2188 cause quoting during the second expansion. */
2191 obstack_grow_str (output, "\\\\");
2195 /* Escapes which undergo conversion. */
2198 obstack_1grow (output, '\a');
2203 obstack_1grow (output, '\b');
2208 obstack_1grow (output, '\f');
2213 obstack_1grow (output, '\n');
2218 obstack_1grow (output, '\r');
2223 obstack_1grow (output, '\t');
2228 obstack_1grow (output, '\v');
2232 /* GCC extension. */
2235 obstack_1grow (output, HOST_ESCAPE_CHAR);
2239 /* Backslash-newline expands to nothing at all. */
2245 /* A few escapes just expand to the character itself. */
2249 /* GCC extensions. */
2254 /* Unrecognized escapes turn into the character itself. */
2257 obstack_1grow (output, *tokptr);
2265 /* Parse a string or character literal from TOKPTR. The string or
2266 character may be wide or unicode. *OUTPTR is set to just after the
2267 end of the literal in the input string. The resulting token is
2268 stored in VALUE. This returns a token value, either STRING or
2269 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2270 number of host characters in the literal. */
2273 parse_string_or_char (const char *tokptr, const char **outptr,
2274 struct typed_stoken *value, int *host_chars)
2280 /* Build the gdb internal form of the input string in tempbuf. Note
2281 that the buffer is null byte terminated *only* for the
2282 convenience of debugging gdb itself and printing the buffer
2283 contents when the buffer contains no embedded nulls. Gdb does
2284 not depend upon the buffer being null byte terminated, it uses
2285 the length string instead. This allows gdb to handle C strings
2286 (as well as strings in other languages) with embedded null
2292 obstack_free (&tempbuf, NULL);
2293 obstack_init (&tempbuf);
2295 /* Record the string type. */
2298 type = C_WIDE_STRING;
2301 else if (*tokptr == 'u')
2306 else if (*tokptr == 'U')
2311 else if (*tokptr == '@')
2313 /* An Objective C string. */
2321 /* Skip the quote. */
2335 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2337 else if (c == quote)
2341 obstack_1grow (&tempbuf, c);
2343 /* FIXME: this does the wrong thing with multi-byte host
2344 characters. We could use mbrlen here, but that would
2345 make "set host-charset" a bit less useful. */
2350 if (*tokptr != quote)
2353 error (_("Unterminated string in expression."));
2355 error (_("Unmatched single quote."));
2360 value->ptr = (char *) obstack_base (&tempbuf);
2361 value->length = obstack_object_size (&tempbuf);
2365 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2368 /* This is used to associate some attributes with a token. */
2372 /* If this bit is set, the token is C++-only. */
2376 /* If this bit is set, the token is conditional: if there is a
2377 symbol of the same name, then the token is a symbol; otherwise,
2378 the token is a keyword. */
2382 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2388 enum exp_opcode opcode;
2392 static const struct token tokentab3[] =
2394 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2395 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2396 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2397 {"...", DOTDOTDOT, BINOP_END, 0}
2400 static const struct token tokentab2[] =
2402 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2403 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2404 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2405 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2406 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2407 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2408 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2409 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2410 {"++", INCREMENT, BINOP_END, 0},
2411 {"--", DECREMENT, BINOP_END, 0},
2412 {"->", ARROW, BINOP_END, 0},
2413 {"&&", ANDAND, BINOP_END, 0},
2414 {"||", OROR, BINOP_END, 0},
2415 /* "::" is *not* only C++: gdb overrides its meaning in several
2416 different ways, e.g., 'filename'::func, function::variable. */
2417 {"::", COLONCOLON, BINOP_END, 0},
2418 {"<<", LSH, BINOP_END, 0},
2419 {">>", RSH, BINOP_END, 0},
2420 {"==", EQUAL, BINOP_END, 0},
2421 {"!=", NOTEQUAL, BINOP_END, 0},
2422 {"<=", LEQ, BINOP_END, 0},
2423 {">=", GEQ, BINOP_END, 0},
2424 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2427 /* Identifier-like tokens. Only type-specifiers than can appear in
2428 multi-word type names (for example 'double' can appear in 'long
2429 double') need to be listed here. type-specifiers that are only ever
2430 single word (like 'float') are handled by the classify_name function. */
2431 static const struct token ident_tokens[] =
2433 {"unsigned", UNSIGNED, OP_NULL, 0},
2434 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2435 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2436 {"struct", STRUCT, OP_NULL, 0},
2437 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2438 {"sizeof", SIZEOF, OP_NULL, 0},
2439 {"_Alignof", ALIGNOF, OP_NULL, 0},
2440 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
2441 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2442 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2443 {"class", CLASS, OP_NULL, FLAG_CXX},
2444 {"union", UNION, OP_NULL, 0},
2445 {"short", SHORT, OP_NULL, 0},
2446 {"const", CONST_KEYWORD, OP_NULL, 0},
2447 {"enum", ENUM, OP_NULL, 0},
2448 {"long", LONG, OP_NULL, 0},
2449 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2450 {"int", INT_KEYWORD, OP_NULL, 0},
2451 {"new", NEW, OP_NULL, FLAG_CXX},
2452 {"delete", DELETE, OP_NULL, FLAG_CXX},
2453 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2455 {"and", ANDAND, BINOP_END, FLAG_CXX},
2456 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2457 {"bitand", '&', OP_NULL, FLAG_CXX},
2458 {"bitor", '|', OP_NULL, FLAG_CXX},
2459 {"compl", '~', OP_NULL, FLAG_CXX},
2460 {"not", '!', OP_NULL, FLAG_CXX},
2461 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2462 {"or", OROR, BINOP_END, FLAG_CXX},
2463 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2464 {"xor", '^', OP_NULL, FLAG_CXX},
2465 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2467 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2468 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2469 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2470 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2472 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2473 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2474 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2475 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2476 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2478 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2483 scan_macro_expansion (char *expansion)
2487 /* We'd better not be trying to push the stack twice. */
2488 gdb_assert (! cpstate->macro_original_text);
2490 /* Copy to the obstack, and then free the intermediate
2492 copy = (char *) obstack_copy0 (&cpstate->expansion_obstack, expansion,
2493 strlen (expansion));
2496 /* Save the old lexptr value, so we can return to it when we're done
2497 parsing the expanded text. */
2498 cpstate->macro_original_text = pstate->lexptr;
2499 pstate->lexptr = copy;
2503 scanning_macro_expansion (void)
2505 return cpstate->macro_original_text != 0;
2509 finished_macro_expansion (void)
2511 /* There'd better be something to pop back to. */
2512 gdb_assert (cpstate->macro_original_text);
2514 /* Pop back to the original text. */
2515 pstate->lexptr = cpstate->macro_original_text;
2516 cpstate->macro_original_text = 0;
2519 /* Return true iff the token represents a C++ cast operator. */
2522 is_cast_operator (const char *token, int len)
2524 return (! strncmp (token, "dynamic_cast", len)
2525 || ! strncmp (token, "static_cast", len)
2526 || ! strncmp (token, "reinterpret_cast", len)
2527 || ! strncmp (token, "const_cast", len));
2530 /* The scope used for macro expansion. */
2531 static struct macro_scope *expression_macro_scope;
2533 /* This is set if a NAME token appeared at the very end of the input
2534 string, with no whitespace separating the name from the EOF. This
2535 is used only when parsing to do field name completion. */
2536 static int saw_name_at_eof;
2538 /* This is set if the previously-returned token was a structure
2539 operator -- either '.' or ARROW. */
2540 static bool last_was_structop;
2542 /* Depth of parentheses. */
2543 static int paren_depth;
2545 /* Read one token, getting characters through lexptr. */
2548 lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
2553 const char *tokstart;
2554 bool saw_structop = last_was_structop;
2557 last_was_structop = false;
2558 *is_quoted_name = false;
2562 /* Check if this is a macro invocation that we need to expand. */
2563 if (! scanning_macro_expansion ())
2565 char *expanded = macro_expand_next (&pstate->lexptr,
2566 standard_macro_lookup,
2567 expression_macro_scope);
2570 scan_macro_expansion (expanded);
2573 pstate->prev_lexptr = pstate->lexptr;
2575 tokstart = pstate->lexptr;
2576 /* See if it is a special token of length 3. */
2577 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2578 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
2580 if ((tokentab3[i].flags & FLAG_CXX) != 0
2581 && par_state->language ()->la_language != language_cplus)
2584 pstate->lexptr += 3;
2585 yylval.opcode = tokentab3[i].opcode;
2586 return tokentab3[i].token;
2589 /* See if it is a special token of length 2. */
2590 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2591 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
2593 if ((tokentab2[i].flags & FLAG_CXX) != 0
2594 && par_state->language ()->la_language != language_cplus)
2597 pstate->lexptr += 2;
2598 yylval.opcode = tokentab2[i].opcode;
2599 if (tokentab2[i].token == ARROW)
2600 last_was_structop = 1;
2601 return tokentab2[i].token;
2604 switch (c = *tokstart)
2607 /* If we were just scanning the result of a macro expansion,
2608 then we need to resume scanning the original text.
2609 If we're parsing for field name completion, and the previous
2610 token allows such completion, return a COMPLETE token.
2611 Otherwise, we were already scanning the original text, and
2612 we're really done. */
2613 if (scanning_macro_expansion ())
2615 finished_macro_expansion ();
2618 else if (saw_name_at_eof)
2620 saw_name_at_eof = 0;
2623 else if (par_state->parse_completion && saw_structop)
2638 if (par_state->language ()->la_language == language_objc
2645 if (paren_depth == 0)
2652 if (pstate->comma_terminates
2654 && ! scanning_macro_expansion ())
2660 /* Might be a floating point number. */
2661 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
2663 last_was_structop = true;
2664 goto symbol; /* Nope, must be a symbol. */
2679 /* It's a number. */
2680 int got_dot = 0, got_e = 0, toktype;
2681 const char *p = tokstart;
2682 int hex = input_radix > 10;
2684 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2689 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2697 /* This test includes !hex because 'e' is a valid hex digit
2698 and thus does not indicate a floating point number when
2699 the radix is hex. */
2700 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2701 got_dot = got_e = 1;
2702 /* This test does not include !hex, because a '.' always indicates
2703 a decimal floating point number regardless of the radix. */
2704 else if (!got_dot && *p == '.')
2706 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2707 && (*p == '-' || *p == '+'))
2708 /* This is the sign of the exponent, not the end of the
2711 /* We will take any letters or digits. parse_number will
2712 complain if past the radix, or if L or U are not final. */
2713 else if ((*p < '0' || *p > '9')
2714 && ((*p < 'a' || *p > 'z')
2715 && (*p < 'A' || *p > 'Z')))
2718 toktype = parse_number (par_state, tokstart, p - tokstart,
2719 got_dot|got_e, &yylval);
2720 if (toktype == ERROR)
2722 char *err_copy = (char *) alloca (p - tokstart + 1);
2724 memcpy (err_copy, tokstart, p - tokstart);
2725 err_copy[p - tokstart] = 0;
2726 error (_("Invalid number \"%s\"."), err_copy);
2734 const char *p = &tokstart[1];
2736 if (par_state->language ()->la_language == language_objc)
2738 size_t len = strlen ("selector");
2740 if (strncmp (p, "selector", len) == 0
2741 && (p[len] == '\0' || ISSPACE (p[len])))
2743 pstate->lexptr = p + len;
2750 while (ISSPACE (*p))
2752 size_t len = strlen ("entry");
2753 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
2756 pstate->lexptr = &p[len];
2785 if (tokstart[1] != '"' && tokstart[1] != '\'')
2794 int result = parse_string_or_char (tokstart, &pstate->lexptr,
2795 &yylval.tsval, &host_len);
2799 error (_("Empty character constant."));
2800 else if (host_len > 2 && c == '\'')
2803 namelen = pstate->lexptr - tokstart - 1;
2804 *is_quoted_name = true;
2808 else if (host_len > 1)
2809 error (_("Invalid character constant."));
2815 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
2816 /* We must have come across a bad character (e.g. ';'). */
2817 error (_("Invalid character '%c' in expression."), c);
2819 /* It's a name. See how long it is. */
2821 for (c = tokstart[namelen];
2822 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
2824 /* Template parameter lists are part of the name.
2825 FIXME: This mishandles `print $a<4&&$a>3'. */
2829 if (! is_cast_operator (tokstart, namelen))
2831 /* Scan ahead to get rest of the template specification. Note
2832 that we look ahead only when the '<' adjoins non-whitespace
2833 characters; for comparison expressions, e.g. "a < b > c",
2834 there must be spaces before the '<', etc. */
2835 const char *p = find_template_name_end (tokstart + namelen);
2838 namelen = p - tokstart;
2842 c = tokstart[++namelen];
2845 /* The token "if" terminates the expression and is NOT removed from
2846 the input stream. It doesn't count if it appears in the
2847 expansion of a macro. */
2849 && tokstart[0] == 'i'
2850 && tokstart[1] == 'f'
2851 && ! scanning_macro_expansion ())
2856 /* For the same reason (breakpoint conditions), "thread N"
2857 terminates the expression. "thread" could be an identifier, but
2858 an identifier is never followed by a number without intervening
2859 punctuation. "task" is similar. Handle abbreviations of these,
2860 similarly to breakpoint.c:find_condition_and_thread. */
2862 && (strncmp (tokstart, "thread", namelen) == 0
2863 || strncmp (tokstart, "task", namelen) == 0)
2864 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2865 && ! scanning_macro_expansion ())
2867 const char *p = tokstart + namelen + 1;
2869 while (*p == ' ' || *p == '\t')
2871 if (*p >= '0' && *p <= '9')
2875 pstate->lexptr += namelen;
2879 yylval.sval.ptr = tokstart;
2880 yylval.sval.length = namelen;
2882 /* Catch specific keywords. */
2883 copy = copy_name (yylval.sval);
2884 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2885 if (strcmp (copy, ident_tokens[i].oper) == 0)
2887 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2888 && par_state->language ()->la_language != language_cplus)
2891 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2893 struct field_of_this_result is_a_field_of_this;
2895 if (lookup_symbol (copy,
2896 pstate->expression_context_block,
2898 (par_state->language ()->la_language
2899 == language_cplus ? &is_a_field_of_this
2903 /* The keyword is shadowed. */
2908 /* It is ok to always set this, even though we don't always
2909 strictly need to. */
2910 yylval.opcode = ident_tokens[i].opcode;
2911 return ident_tokens[i].token;
2914 if (*tokstart == '$')
2915 return DOLLAR_VARIABLE;
2917 if (pstate->parse_completion && *pstate->lexptr == '\0')
2918 saw_name_at_eof = 1;
2920 yylval.ssym.stoken = yylval.sval;
2921 yylval.ssym.sym.symbol = NULL;
2922 yylval.ssym.sym.block = NULL;
2923 yylval.ssym.is_a_field_of_this = 0;
2927 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2928 struct token_and_value
2934 /* A FIFO of tokens that have been read but not yet returned to the
2936 static std::vector<token_and_value> token_fifo;
2938 /* Non-zero if the lexer should return tokens from the FIFO. */
2941 /* Temporary storage for c_lex; this holds symbol names as they are
2943 auto_obstack name_obstack;
2945 /* Classify a NAME token. The contents of the token are in `yylval'.
2946 Updates yylval and returns the new token type. BLOCK is the block
2947 in which lookups start; this can be NULL to mean the global scope.
2948 IS_QUOTED_NAME is non-zero if the name token was originally quoted
2949 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
2950 a structure operator -- either '.' or ARROW */
2953 classify_name (struct parser_state *par_state, const struct block *block,
2954 bool is_quoted_name, bool is_after_structop)
2956 struct block_symbol bsym;
2958 struct field_of_this_result is_a_field_of_this;
2960 copy = copy_name (yylval.sval);
2962 /* Initialize this in case we *don't* use it in this call; that way
2963 we can refer to it unconditionally below. */
2964 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
2966 bsym = lookup_symbol (copy, block, VAR_DOMAIN,
2967 par_state->language ()->la_name_of_this
2968 ? &is_a_field_of_this : NULL);
2970 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK)
2972 yylval.ssym.sym = bsym;
2973 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2976 else if (!bsym.symbol)
2978 /* If we found a field of 'this', we might have erroneously
2979 found a constructor where we wanted a type name. Handle this
2980 case by noticing that we found a constructor and then look up
2981 the type tag instead. */
2982 if (is_a_field_of_this.type != NULL
2983 && is_a_field_of_this.fn_field != NULL
2984 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
2987 struct field_of_this_result inner_is_a_field_of_this;
2989 bsym = lookup_symbol (copy, block, STRUCT_DOMAIN,
2990 &inner_is_a_field_of_this);
2991 if (bsym.symbol != NULL)
2993 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
2998 /* If we found a field on the "this" object, or we are looking
2999 up a field on a struct, then we want to prefer it over a
3000 filename. However, if the name was quoted, then it is better
3001 to check for a filename or a block, since this is the only
3002 way the user has of requiring the extension to be used. */
3003 if ((is_a_field_of_this.type == NULL && !is_after_structop)
3006 /* See if it's a file name. */
3007 struct symtab *symtab;
3009 symtab = lookup_symtab (copy);
3012 yylval.bval = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab),
3019 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF)
3021 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3025 /* See if it's an ObjC classname. */
3026 if (par_state->language ()->la_language == language_objc && !bsym.symbol)
3028 CORE_ADDR Class = lookup_objc_class (par_state->gdbarch (), copy);
3033 yylval.theclass.theclass = Class;
3034 sym = lookup_struct_typedef (copy,
3035 par_state->expression_context_block, 1);
3037 yylval.theclass.type = SYMBOL_TYPE (sym);
3042 /* Input names that aren't symbols but ARE valid hex numbers, when
3043 the input radix permits them, can be names or numbers depending
3044 on the parse. Note we support radixes > 16 here. */
3046 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3047 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3049 YYSTYPE newlval; /* Its value is ignored. */
3050 int hextype = parse_number (par_state, copy, yylval.sval.length,
3055 yylval.ssym.sym = bsym;
3056 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3061 /* Any other kind of symbol */
3062 yylval.ssym.sym = bsym;
3063 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3065 if (bsym.symbol == NULL
3066 && par_state->language ()->la_language == language_cplus
3067 && is_a_field_of_this.type == NULL
3068 && lookup_minimal_symbol (copy, NULL, NULL).minsym == NULL)
3069 return UNKNOWN_CPP_NAME;
3074 /* Like classify_name, but used by the inner loop of the lexer, when a
3075 name might have already been seen. CONTEXT is the context type, or
3076 NULL if this is the first component of a name. */
3079 classify_inner_name (struct parser_state *par_state,
3080 const struct block *block, struct type *context)
3085 if (context == NULL)
3086 return classify_name (par_state, block, false, false);
3088 type = check_typedef (context);
3089 if (!type_aggregate_p (type))
3092 copy = copy_name (yylval.ssym.stoken);
3093 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3094 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy, block, VAR_DOMAIN);
3096 /* If no symbol was found, search for a matching base class named
3097 COPY. This will allow users to enter qualified names of class members
3098 relative to the `this' pointer. */
3099 if (yylval.ssym.sym.symbol == NULL)
3101 struct type *base_type = cp_find_type_baseclass_by_name (type, copy);
3103 if (base_type != NULL)
3105 yylval.tsym.type = base_type;
3112 switch (SYMBOL_CLASS (yylval.ssym.sym.symbol))
3116 /* cp_lookup_nested_symbol might have accidentally found a constructor
3117 named COPY when we really wanted a base class of the same name.
3118 Double-check this case by looking for a base class. */
3120 struct type *base_type = cp_find_type_baseclass_by_name (type, copy);
3122 if (base_type != NULL)
3124 yylval.tsym.type = base_type;
3131 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
3137 internal_error (__FILE__, __LINE__, _("not reached"));
3140 /* The outer level of a two-level lexer. This calls the inner lexer
3141 to return tokens. It then either returns these tokens, or
3142 aggregates them into a larger token. This lets us work around a
3143 problem in our parsing approach, where the parser could not
3144 distinguish between qualified names and qualified types at the
3147 This approach is still not ideal, because it mishandles template
3148 types. See the comment in lex_one_token for an example. However,
3149 this is still an improvement over the earlier approach, and will
3150 suffice until we move to better parsing technology. */
3155 token_and_value current;
3156 int first_was_coloncolon, last_was_coloncolon;
3157 struct type *context_type = NULL;
3158 int last_to_examine, next_to_examine, checkpoint;
3159 const struct block *search_block;
3160 bool is_quoted_name, last_lex_was_structop;
3162 if (popping && !token_fifo.empty ())
3166 last_lex_was_structop = last_was_structop;
3168 /* Read the first token and decide what to do. Most of the
3169 subsequent code is C++-only; but also depends on seeing a "::" or
3171 current.token = lex_one_token (pstate, &is_quoted_name);
3172 if (current.token == NAME)
3173 current.token = classify_name (pstate, pstate->expression_context_block,
3174 is_quoted_name, last_lex_was_structop);
3175 if (pstate->language ()->la_language != language_cplus
3176 || (current.token != TYPENAME && current.token != COLONCOLON
3177 && current.token != FILENAME))
3178 return current.token;
3180 /* Read any sequence of alternating "::" and name-like tokens into
3182 current.value = yylval;
3183 token_fifo.push_back (current);
3184 last_was_coloncolon = current.token == COLONCOLON;
3189 /* We ignore quoted names other than the very first one.
3190 Subsequent ones do not have any special meaning. */
3191 current.token = lex_one_token (pstate, &ignore);
3192 current.value = yylval;
3193 token_fifo.push_back (current);
3195 if ((last_was_coloncolon && current.token != NAME)
3196 || (!last_was_coloncolon && current.token != COLONCOLON))
3198 last_was_coloncolon = !last_was_coloncolon;
3202 /* We always read one extra token, so compute the number of tokens
3203 to examine accordingly. */
3204 last_to_examine = token_fifo.size () - 2;
3205 next_to_examine = 0;
3207 current = token_fifo[next_to_examine];
3210 name_obstack.clear ();
3212 if (current.token == FILENAME)
3213 search_block = current.value.bval;
3214 else if (current.token == COLONCOLON)
3215 search_block = NULL;
3218 gdb_assert (current.token == TYPENAME);
3219 search_block = pstate->expression_context_block;
3220 obstack_grow (&name_obstack, current.value.sval.ptr,
3221 current.value.sval.length);
3222 context_type = current.value.tsym.type;
3226 first_was_coloncolon = current.token == COLONCOLON;
3227 last_was_coloncolon = first_was_coloncolon;
3229 while (next_to_examine <= last_to_examine)
3231 token_and_value next;
3233 next = token_fifo[next_to_examine];
3236 if (next.token == NAME && last_was_coloncolon)
3240 yylval = next.value;
3241 classification = classify_inner_name (pstate, search_block,
3243 /* We keep going until we either run out of names, or until
3244 we have a qualified name which is not a type. */
3245 if (classification != TYPENAME && classification != NAME)
3248 /* Accept up to this token. */
3249 checkpoint = next_to_examine;
3251 /* Update the partial name we are constructing. */
3252 if (context_type != NULL)
3254 /* We don't want to put a leading "::" into the name. */
3255 obstack_grow_str (&name_obstack, "::");
3257 obstack_grow (&name_obstack, next.value.sval.ptr,
3258 next.value.sval.length);
3260 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
3261 yylval.sval.length = obstack_object_size (&name_obstack);
3262 current.value = yylval;
3263 current.token = classification;
3265 last_was_coloncolon = 0;
3267 if (classification == NAME)
3270 context_type = yylval.tsym.type;
3272 else if (next.token == COLONCOLON && !last_was_coloncolon)
3273 last_was_coloncolon = 1;
3276 /* We've reached the end of the name. */
3281 /* If we have a replacement token, install it as the first token in
3282 the FIFO, and delete the other constituent tokens. */
3285 current.value.sval.ptr
3286 = (const char *) obstack_copy0 (&cpstate->expansion_obstack,
3287 current.value.sval.ptr,
3288 current.value.sval.length);
3290 token_fifo[0] = current;
3292 token_fifo.erase (token_fifo.begin () + 1,
3293 token_fifo.begin () + checkpoint);
3297 current = token_fifo[0];
3298 token_fifo.erase (token_fifo.begin ());
3299 yylval = current.value;
3300 return current.token;
3304 c_parse (struct parser_state *par_state)
3306 /* Setting up the parser state. */
3307 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3308 gdb_assert (par_state != NULL);
3311 c_parse_state cstate;
3312 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3314 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
3316 if (par_state->expression_context_block)
3318 = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
3320 macro_scope = default_macro_scope ();
3322 macro_scope = user_macro_scope ();
3324 scoped_restore restore_macro_scope
3325 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
3327 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3330 /* Initialize some state used by the lexer. */
3331 last_was_structop = false;
3332 saw_name_at_eof = 0;
3335 token_fifo.clear ();
3337 name_obstack.clear ();
3344 /* This is called via the YYPRINT macro when parser debugging is
3345 enabled. It prints a token's value. */
3348 c_print_token (FILE *file, int type, YYSTYPE value)
3353 parser_fprintf (file, "typed_val_int<%s, %s>",
3354 TYPE_SAFE_NAME (value.typed_val_int.type),
3355 pulongest (value.typed_val_int.val));
3361 char *copy = (char *) alloca (value.tsval.length + 1);
3363 memcpy (copy, value.tsval.ptr, value.tsval.length);
3364 copy[value.tsval.length] = '\0';
3366 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3371 case DOLLAR_VARIABLE:
3372 parser_fprintf (file, "sval<%s>", copy_name (value.sval));
3376 parser_fprintf (file, "tsym<type=%s, name=%s>",
3377 TYPE_SAFE_NAME (value.tsym.type),
3378 copy_name (value.tsym.stoken));
3382 case UNKNOWN_CPP_NAME:
3385 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3386 copy_name (value.ssym.stoken),
3387 (value.ssym.sym.symbol == NULL
3388 ? "(null)" : SYMBOL_PRINT_NAME (value.ssym.sym.symbol)),
3389 value.ssym.is_a_field_of_this);
3393 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3401 yyerror (const char *msg)
3403 if (pstate->prev_lexptr)
3404 pstate->lexptr = pstate->prev_lexptr;
3406 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);