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
240 %token RESTRICT ATOMIC
242 %token <sval> DOLLAR_VARIABLE
244 %token <opcode> ASSIGN_MODIFY
253 %right '=' ASSIGN_MODIFY
261 %left '<' '>' LEQ GEQ
266 %right UNARY INCREMENT DECREMENT
267 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
268 %token <ssym> BLOCKNAME
269 %token <bval> FILENAME
283 { write_exp_elt_opcode(pstate, OP_TYPE);
284 write_exp_elt_type(pstate, $1);
285 write_exp_elt_opcode(pstate, OP_TYPE);}
288 write_exp_elt_opcode (pstate, OP_TYPEOF);
290 | TYPEOF '(' type ')'
292 write_exp_elt_opcode (pstate, OP_TYPE);
293 write_exp_elt_type (pstate, $3);
294 write_exp_elt_opcode (pstate, OP_TYPE);
296 | DECLTYPE '(' exp ')'
298 write_exp_elt_opcode (pstate, OP_DECLTYPE);
302 /* Expressions, including the comma operator. */
305 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
308 /* Expressions, not including the comma operator. */
309 exp : '*' exp %prec UNARY
310 { write_exp_elt_opcode (pstate, UNOP_IND); }
313 exp : '&' exp %prec UNARY
314 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
317 exp : '-' exp %prec UNARY
318 { write_exp_elt_opcode (pstate, UNOP_NEG); }
321 exp : '+' exp %prec UNARY
322 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
325 exp : '!' exp %prec UNARY
326 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
329 exp : '~' exp %prec UNARY
330 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
333 exp : INCREMENT exp %prec UNARY
334 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
337 exp : DECREMENT exp %prec UNARY
338 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
341 exp : exp INCREMENT %prec UNARY
342 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
345 exp : exp DECREMENT %prec UNARY
346 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
349 exp : TYPEID '(' exp ')' %prec UNARY
350 { write_exp_elt_opcode (pstate, OP_TYPEID); }
353 exp : TYPEID '(' type_exp ')' %prec UNARY
354 { write_exp_elt_opcode (pstate, OP_TYPEID); }
357 exp : SIZEOF exp %prec UNARY
358 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
361 exp : ALIGNOF '(' type_exp ')' %prec UNARY
362 { write_exp_elt_opcode (pstate, UNOP_ALIGNOF); }
365 exp : exp ARROW field_name
366 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
367 write_exp_string (pstate, $3);
368 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
371 exp : exp ARROW field_name COMPLETE
372 { pstate->mark_struct_expression ();
373 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
374 write_exp_string (pstate, $3);
375 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
378 exp : exp ARROW COMPLETE
380 pstate->mark_struct_expression ();
381 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
384 write_exp_string (pstate, s);
385 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
388 exp : exp ARROW '~' name
389 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
390 write_destructor_name (pstate, $4);
391 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
394 exp : exp ARROW '~' name COMPLETE
395 { pstate->mark_struct_expression ();
396 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
397 write_destructor_name (pstate, $4);
398 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
401 exp : exp ARROW qualified_name
402 { /* exp->type::name becomes exp->*(&type::name) */
403 /* Note: this doesn't work if name is a
404 static member! FIXME */
405 write_exp_elt_opcode (pstate, UNOP_ADDR);
406 write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
409 exp : exp ARROW_STAR exp
410 { write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
413 exp : exp '.' field_name
414 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
415 write_exp_string (pstate, $3);
416 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
419 exp : exp '.' field_name COMPLETE
420 { pstate->mark_struct_expression ();
421 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
422 write_exp_string (pstate, $3);
423 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
426 exp : exp '.' COMPLETE
428 pstate->mark_struct_expression ();
429 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
432 write_exp_string (pstate, s);
433 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
436 exp : exp '.' '~' name
437 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
438 write_destructor_name (pstate, $4);
439 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
442 exp : exp '.' '~' name COMPLETE
443 { pstate->mark_struct_expression ();
444 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
445 write_destructor_name (pstate, $4);
446 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
449 exp : exp '.' qualified_name
450 { /* exp.type::name becomes exp.*(&type::name) */
451 /* Note: this doesn't work if name is a
452 static member! FIXME */
453 write_exp_elt_opcode (pstate, UNOP_ADDR);
454 write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
457 exp : exp DOT_STAR exp
458 { write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
461 exp : exp '[' exp1 ']'
462 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
465 exp : exp OBJC_LBRAC exp1 ']'
466 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
470 * The rules below parse ObjC message calls of the form:
471 * '[' target selector {':' argument}* ']'
474 exp : OBJC_LBRAC TYPENAME
478 std::string copy = copy_name ($2.stoken);
479 theclass = lookup_objc_class (pstate->gdbarch (),
482 error (_("%s is not an ObjC Class"),
484 write_exp_elt_opcode (pstate, OP_LONG);
485 write_exp_elt_type (pstate,
486 parse_type (pstate)->builtin_int);
487 write_exp_elt_longcst (pstate, (LONGEST) theclass);
488 write_exp_elt_opcode (pstate, OP_LONG);
492 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
493 end_msglist (pstate);
494 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
498 exp : OBJC_LBRAC CLASSNAME
500 write_exp_elt_opcode (pstate, OP_LONG);
501 write_exp_elt_type (pstate,
502 parse_type (pstate)->builtin_int);
503 write_exp_elt_longcst (pstate, (LONGEST) $2.theclass);
504 write_exp_elt_opcode (pstate, OP_LONG);
508 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
509 end_msglist (pstate);
510 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
517 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
518 end_msglist (pstate);
519 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
524 { add_msglist(&$1, 0); }
532 msgarg : name ':' exp
533 { add_msglist(&$1, 1); }
534 | ':' exp /* Unnamed arg. */
535 { add_msglist(0, 1); }
536 | ',' exp /* Variable number of args. */
537 { add_msglist(0, 0); }
541 /* This is to save the value of arglist_len
542 being accumulated by an outer function call. */
543 { pstate->start_arglist (); }
544 arglist ')' %prec ARROW
545 { write_exp_elt_opcode (pstate, OP_FUNCALL);
546 write_exp_elt_longcst (pstate,
547 pstate->end_arglist ());
548 write_exp_elt_opcode (pstate, OP_FUNCALL); }
551 /* This is here to disambiguate with the production for
552 "func()::static_var" further below, which uses
553 function_method_void. */
554 exp : exp '(' ')' %prec ARROW
555 { pstate->start_arglist ();
556 write_exp_elt_opcode (pstate, OP_FUNCALL);
557 write_exp_elt_longcst (pstate,
558 pstate->end_arglist ());
559 write_exp_elt_opcode (pstate, OP_FUNCALL); }
563 exp : UNKNOWN_CPP_NAME '('
565 /* This could potentially be a an argument defined
566 lookup function (Koenig). */
567 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
569 (pstate, pstate->expression_context_block);
570 write_exp_elt_sym (pstate,
571 NULL); /* Placeholder. */
572 write_exp_string (pstate, $1.stoken);
573 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
575 /* This is to save the value of arglist_len
576 being accumulated by an outer function call. */
578 pstate->start_arglist ();
580 arglist ')' %prec ARROW
582 write_exp_elt_opcode (pstate, OP_FUNCALL);
583 write_exp_elt_longcst (pstate,
584 pstate->end_arglist ());
585 write_exp_elt_opcode (pstate, OP_FUNCALL);
590 { pstate->start_arglist (); }
597 { pstate->arglist_len = 1; }
600 arglist : arglist ',' exp %prec ABOVE_COMMA
601 { pstate->arglist_len++; }
604 function_method: exp '(' parameter_typelist ')' const_or_volatile
606 std::vector<struct type *> *type_list = $3;
607 LONGEST len = type_list->size ();
609 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
610 /* Save the const/volatile qualifiers as
611 recorded by the const_or_volatile
612 production's actions. */
613 write_exp_elt_longcst
616 .follow_type_instance_flags ()));
617 write_exp_elt_longcst (pstate, len);
618 for (type *type_elt : *type_list)
619 write_exp_elt_type (pstate, type_elt);
620 write_exp_elt_longcst(pstate, len);
621 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
625 function_method_void: exp '(' ')' const_or_volatile
626 { write_exp_elt_opcode (pstate, TYPE_INSTANCE);
628 write_exp_elt_longcst
630 cpstate->type_stack.follow_type_instance_flags ());
631 write_exp_elt_longcst (pstate, 0);
632 write_exp_elt_longcst (pstate, 0);
633 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
637 exp : function_method
640 /* Normally we must interpret "func()" as a function call, instead of
641 a type. The user needs to write func(void) to disambiguate.
642 However, in the "func()::static_var" case, there's no
644 function_method_void_or_typelist: function_method
645 | function_method_void
648 exp : function_method_void_or_typelist COLONCOLON name
650 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
651 write_exp_string (pstate, $3);
652 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
657 { $$ = pstate->end_arglist () - 1; }
659 exp : lcurly arglist rcurly %prec ARROW
660 { write_exp_elt_opcode (pstate, OP_ARRAY);
661 write_exp_elt_longcst (pstate, (LONGEST) 0);
662 write_exp_elt_longcst (pstate, (LONGEST) $3);
663 write_exp_elt_opcode (pstate, OP_ARRAY); }
666 exp : lcurly type_exp rcurly exp %prec UNARY
667 { write_exp_elt_opcode (pstate, UNOP_MEMVAL_TYPE); }
670 exp : '(' type_exp ')' exp %prec UNARY
671 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
678 /* Binary operators in order of decreasing precedence. */
681 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
685 { write_exp_elt_opcode (pstate, BINOP_MUL); }
689 { write_exp_elt_opcode (pstate, BINOP_DIV); }
693 { write_exp_elt_opcode (pstate, BINOP_REM); }
697 { write_exp_elt_opcode (pstate, BINOP_ADD); }
701 { write_exp_elt_opcode (pstate, BINOP_SUB); }
705 { write_exp_elt_opcode (pstate, BINOP_LSH); }
709 { write_exp_elt_opcode (pstate, BINOP_RSH); }
713 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
716 exp : exp NOTEQUAL exp
717 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
721 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
725 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
729 { write_exp_elt_opcode (pstate, BINOP_LESS); }
733 { write_exp_elt_opcode (pstate, BINOP_GTR); }
737 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
741 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
745 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
749 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
753 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
756 exp : exp '?' exp ':' exp %prec '?'
757 { write_exp_elt_opcode (pstate, TERNOP_COND); }
761 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
764 exp : exp ASSIGN_MODIFY exp
765 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
766 write_exp_elt_opcode (pstate, $2);
767 write_exp_elt_opcode (pstate,
768 BINOP_ASSIGN_MODIFY); }
772 { write_exp_elt_opcode (pstate, OP_LONG);
773 write_exp_elt_type (pstate, $1.type);
774 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
775 write_exp_elt_opcode (pstate, OP_LONG); }
780 struct stoken_vector vec;
783 write_exp_string_vector (pstate, $1.type, &vec);
789 parse_number (pstate, $1.stoken.ptr,
790 $1.stoken.length, 0, &val);
791 write_exp_elt_opcode (pstate, OP_LONG);
792 write_exp_elt_type (pstate, val.typed_val_int.type);
793 write_exp_elt_longcst (pstate,
794 (LONGEST) val.typed_val_int.val);
795 write_exp_elt_opcode (pstate, OP_LONG);
801 { write_exp_elt_opcode (pstate, OP_FLOAT);
802 write_exp_elt_type (pstate, $1.type);
803 write_exp_elt_floatcst (pstate, $1.val);
804 write_exp_elt_opcode (pstate, OP_FLOAT); }
810 exp : DOLLAR_VARIABLE
812 write_dollar_variable (pstate, $1);
816 exp : SELECTOR '(' name ')'
818 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR);
819 write_exp_string (pstate, $3);
820 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR); }
823 exp : SIZEOF '(' type ')' %prec UNARY
824 { struct type *type = $3;
825 write_exp_elt_opcode (pstate, OP_LONG);
826 write_exp_elt_type (pstate, lookup_signed_typename
827 (pstate->language (),
829 type = check_typedef (type);
831 /* $5.3.3/2 of the C++ Standard (n3290 draft)
832 says of sizeof: "When applied to a reference
833 or a reference type, the result is the size of
834 the referenced type." */
835 if (TYPE_IS_REFERENCE (type))
836 type = check_typedef (TYPE_TARGET_TYPE (type));
837 write_exp_elt_longcst (pstate,
838 (LONGEST) TYPE_LENGTH (type));
839 write_exp_elt_opcode (pstate, OP_LONG); }
842 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
843 { write_exp_elt_opcode (pstate,
844 UNOP_REINTERPRET_CAST); }
847 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
848 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
851 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
852 { write_exp_elt_opcode (pstate, UNOP_DYNAMIC_CAST); }
855 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
856 { /* We could do more error checking here, but
857 it doesn't seem worthwhile. */
858 write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
864 /* We copy the string here, and not in the
865 lexer, to guarantee that we do not leak a
866 string. Note that we follow the
867 NUL-termination convention of the
869 struct typed_stoken *vec = XNEW (struct typed_stoken);
874 vec->length = $1.length;
875 vec->ptr = (char *) malloc ($1.length + 1);
876 memcpy (vec->ptr, $1.ptr, $1.length + 1);
881 /* Note that we NUL-terminate here, but just
885 $$.tokens = XRESIZEVEC (struct typed_stoken,
888 p = (char *) malloc ($2.length + 1);
889 memcpy (p, $2.ptr, $2.length + 1);
891 $$.tokens[$$.len - 1].type = $2.type;
892 $$.tokens[$$.len - 1].length = $2.length;
893 $$.tokens[$$.len - 1].ptr = p;
900 c_string_type type = C_STRING;
902 for (i = 0; i < $1.len; ++i)
904 switch ($1.tokens[i].type)
912 && type != $1.tokens[i].type)
913 error (_("Undefined string concatenation."));
914 type = (enum c_string_type_values) $1.tokens[i].type;
918 internal_error (__FILE__, __LINE__,
919 "unrecognized type in string concatenation");
923 write_exp_string_vector (pstate, type, &$1);
924 for (i = 0; i < $1.len; ++i)
925 free ($1.tokens[i].ptr);
930 exp : NSSTRING /* ObjC NextStep NSString constant
931 * of the form '@' '"' string '"'.
933 { write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING);
934 write_exp_string (pstate, $1);
935 write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING); }
940 { write_exp_elt_opcode (pstate, OP_LONG);
941 write_exp_elt_type (pstate,
942 parse_type (pstate)->builtin_bool);
943 write_exp_elt_longcst (pstate, (LONGEST) 1);
944 write_exp_elt_opcode (pstate, OP_LONG); }
948 { write_exp_elt_opcode (pstate, OP_LONG);
949 write_exp_elt_type (pstate,
950 parse_type (pstate)->builtin_bool);
951 write_exp_elt_longcst (pstate, (LONGEST) 0);
952 write_exp_elt_opcode (pstate, OP_LONG); }
960 $$ = SYMBOL_BLOCK_VALUE ($1.sym.symbol);
962 error (_("No file or function \"%s\"."),
963 copy_name ($1.stoken).c_str ());
971 block : block COLONCOLON name
973 std::string copy = copy_name ($3);
975 = lookup_symbol (copy.c_str (), $1,
976 VAR_DOMAIN, NULL).symbol;
978 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
979 error (_("No function \"%s\" in specified context."),
981 $$ = SYMBOL_BLOCK_VALUE (tem); }
984 variable: name_not_typename ENTRY
985 { struct symbol *sym = $1.sym.symbol;
987 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
988 || !symbol_read_needs_frame (sym))
989 error (_("@entry can be used only for function "
990 "parameters, not for \"%s\""),
991 copy_name ($1.stoken).c_str ());
993 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
994 write_exp_elt_sym (pstate, sym);
995 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
999 variable: block COLONCOLON name
1001 std::string copy = copy_name ($3);
1002 struct block_symbol sym
1003 = lookup_symbol (copy.c_str (), $1,
1006 if (sym.symbol == 0)
1007 error (_("No symbol \"%s\" in specified context."),
1009 if (symbol_read_needs_frame (sym.symbol))
1010 pstate->block_tracker->update (sym);
1012 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1013 write_exp_elt_block (pstate, sym.block);
1014 write_exp_elt_sym (pstate, sym.symbol);
1015 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
1018 qualified_name: TYPENAME COLONCOLON name
1020 struct type *type = $1.type;
1021 type = check_typedef (type);
1022 if (!type_aggregate_p (type))
1023 error (_("`%s' is not defined as an aggregate type."),
1024 TYPE_SAFE_NAME (type));
1026 write_exp_elt_opcode (pstate, OP_SCOPE);
1027 write_exp_elt_type (pstate, type);
1028 write_exp_string (pstate, $3);
1029 write_exp_elt_opcode (pstate, OP_SCOPE);
1031 | TYPENAME COLONCOLON '~' name
1033 struct type *type = $1.type;
1034 struct stoken tmp_token;
1037 type = check_typedef (type);
1038 if (!type_aggregate_p (type))
1039 error (_("`%s' is not defined as an aggregate type."),
1040 TYPE_SAFE_NAME (type));
1041 buf = (char *) alloca ($4.length + 2);
1042 tmp_token.ptr = buf;
1043 tmp_token.length = $4.length + 1;
1045 memcpy (buf+1, $4.ptr, $4.length);
1046 buf[tmp_token.length] = 0;
1048 /* Check for valid destructor name. */
1049 destructor_name_p (tmp_token.ptr, $1.type);
1050 write_exp_elt_opcode (pstate, OP_SCOPE);
1051 write_exp_elt_type (pstate, type);
1052 write_exp_string (pstate, tmp_token);
1053 write_exp_elt_opcode (pstate, OP_SCOPE);
1055 | TYPENAME COLONCOLON name COLONCOLON name
1057 std::string copy = copy_name ($3);
1058 error (_("No type \"%s\" within class "
1059 "or namespace \"%s\"."),
1060 copy.c_str (), TYPE_SAFE_NAME ($1.type));
1064 variable: qualified_name
1065 | COLONCOLON name_not_typename
1067 std::string name = copy_name ($2.stoken);
1069 struct bound_minimal_symbol msymbol;
1072 = lookup_symbol (name.c_str (),
1073 (const struct block *) NULL,
1074 VAR_DOMAIN, NULL).symbol;
1077 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1078 write_exp_elt_block (pstate, NULL);
1079 write_exp_elt_sym (pstate, sym);
1080 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1084 msymbol = lookup_bound_minimal_symbol (name.c_str ());
1085 if (msymbol.minsym != NULL)
1086 write_exp_msymbol (pstate, msymbol);
1087 else if (!have_full_symbols () && !have_partial_symbols ())
1088 error (_("No symbol table is loaded. Use the \"file\" command."));
1090 error (_("No symbol \"%s\" in current context."),
1095 variable: name_not_typename
1096 { struct block_symbol sym = $1.sym;
1100 if (symbol_read_needs_frame (sym.symbol))
1101 pstate->block_tracker->update (sym);
1103 /* If we found a function, see if it's
1104 an ifunc resolver that has the same
1105 address as the ifunc symbol itself.
1106 If so, prefer the ifunc symbol. */
1108 bound_minimal_symbol resolver
1109 = find_gnu_ifunc (sym.symbol);
1110 if (resolver.minsym != NULL)
1111 write_exp_msymbol (pstate, resolver);
1114 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1115 write_exp_elt_block (pstate, sym.block);
1116 write_exp_elt_sym (pstate, sym.symbol);
1117 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1120 else if ($1.is_a_field_of_this)
1122 /* C++: it hangs off of `this'. Must
1123 not inadvertently convert from a method call
1125 pstate->block_tracker->update (sym);
1126 write_exp_elt_opcode (pstate, OP_THIS);
1127 write_exp_elt_opcode (pstate, OP_THIS);
1128 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1129 write_exp_string (pstate, $1.stoken);
1130 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1134 std::string arg = copy_name ($1.stoken);
1136 bound_minimal_symbol msymbol
1137 = lookup_bound_minimal_symbol (arg.c_str ());
1138 if (msymbol.minsym == NULL)
1140 if (!have_full_symbols () && !have_partial_symbols ())
1141 error (_("No symbol table is loaded. Use the \"file\" command."));
1143 error (_("No symbol \"%s\" in current context."),
1147 /* This minsym might be an alias for
1148 another function. See if we can find
1149 the debug symbol for the target, and
1150 if so, use it instead, since it has
1151 return type / prototype info. This
1152 is important for example for "p
1153 *__errno_location()". */
1154 symbol *alias_target
1155 = ((msymbol.minsym->type != mst_text_gnu_ifunc
1156 && msymbol.minsym->type != mst_data_gnu_ifunc)
1157 ? find_function_alias_target (msymbol)
1159 if (alias_target != NULL)
1161 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1163 (pstate, SYMBOL_BLOCK_VALUE (alias_target));
1164 write_exp_elt_sym (pstate, alias_target);
1165 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1168 write_exp_msymbol (pstate, msymbol);
1173 const_or_volatile: const_or_volatile_noopt
1179 { cpstate->type_stack.insert (tp_const); }
1181 { cpstate->type_stack.insert (tp_volatile); }
1183 { cpstate->type_stack.insert (tp_atomic); }
1185 { cpstate->type_stack.insert (tp_restrict); }
1188 cpstate->type_stack.insert (pstate,
1189 copy_name ($2.stoken).c_str ());
1193 qualifier_seq_noopt:
1195 | qualifier_seq single_qualifier
1205 { cpstate->type_stack.insert (tp_pointer); }
1208 { cpstate->type_stack.insert (tp_pointer); }
1211 { cpstate->type_stack.insert (tp_reference); }
1213 { cpstate->type_stack.insert (tp_reference); }
1215 { cpstate->type_stack.insert (tp_rvalue_reference); }
1216 | ANDAND ptr_operator
1217 { cpstate->type_stack.insert (tp_rvalue_reference); }
1220 ptr_operator_ts: ptr_operator
1222 $$ = cpstate->type_stack.create ();
1223 cpstate->type_stacks.emplace_back ($$);
1227 abs_decl: ptr_operator_ts direct_abs_decl
1228 { $$ = $2->append ($1); }
1233 direct_abs_decl: '(' abs_decl ')'
1235 | direct_abs_decl array_mod
1237 cpstate->type_stack.push ($1);
1238 cpstate->type_stack.push ($2);
1239 cpstate->type_stack.push (tp_array);
1240 $$ = cpstate->type_stack.create ();
1241 cpstate->type_stacks.emplace_back ($$);
1245 cpstate->type_stack.push ($1);
1246 cpstate->type_stack.push (tp_array);
1247 $$ = cpstate->type_stack.create ();
1248 cpstate->type_stacks.emplace_back ($$);
1251 | direct_abs_decl func_mod
1253 cpstate->type_stack.push ($1);
1254 cpstate->type_stack.push ($2);
1255 $$ = cpstate->type_stack.create ();
1256 cpstate->type_stacks.emplace_back ($$);
1260 cpstate->type_stack.push ($1);
1261 $$ = cpstate->type_stack.create ();
1262 cpstate->type_stacks.emplace_back ($$);
1272 | OBJC_LBRAC INT ']'
1278 $$ = new std::vector<struct type *>;
1279 cpstate->type_lists.emplace_back ($$);
1281 | '(' parameter_typelist ')'
1285 /* We used to try to recognize pointer to member types here, but
1286 that didn't work (shift/reduce conflicts meant that these rules never
1287 got executed). The problem is that
1288 int (foo::bar::baz::bizzle)
1289 is a function type but
1290 int (foo::bar::baz::bizzle::*)
1291 is a pointer to member type. Stroustrup loses again! */
1296 /* Implements (approximately): (type-qualifier)* type-specifier.
1298 When type-specifier is only ever a single word, like 'float' then these
1299 arrive as pre-built TYPENAME tokens thanks to the classify_name
1300 function. However, when a type-specifier can contain multiple words,
1301 for example 'double' can appear as just 'double' or 'long double', and
1302 similarly 'long' can appear as just 'long' or in 'long double', then
1303 these type-specifiers are parsed into their own tokens in the function
1304 lex_one_token and the ident_tokens array. These separate tokens are all
1310 { $$ = lookup_signed_typename (pstate->language (),
1313 { $$ = lookup_signed_typename (pstate->language (),
1316 { $$ = lookup_signed_typename (pstate->language (),
1319 { $$ = lookup_signed_typename (pstate->language (),
1321 | LONG SIGNED_KEYWORD INT_KEYWORD
1322 { $$ = lookup_signed_typename (pstate->language (),
1324 | LONG SIGNED_KEYWORD
1325 { $$ = lookup_signed_typename (pstate->language (),
1327 | SIGNED_KEYWORD LONG INT_KEYWORD
1328 { $$ = lookup_signed_typename (pstate->language (),
1330 | UNSIGNED LONG INT_KEYWORD
1331 { $$ = lookup_unsigned_typename (pstate->language (),
1333 | LONG UNSIGNED INT_KEYWORD
1334 { $$ = lookup_unsigned_typename (pstate->language (),
1337 { $$ = lookup_unsigned_typename (pstate->language (),
1340 { $$ = lookup_signed_typename (pstate->language (),
1342 | LONG LONG INT_KEYWORD
1343 { $$ = lookup_signed_typename (pstate->language (),
1345 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1346 { $$ = lookup_signed_typename (pstate->language (),
1348 | LONG LONG SIGNED_KEYWORD
1349 { $$ = lookup_signed_typename (pstate->language (),
1351 | SIGNED_KEYWORD LONG LONG
1352 { $$ = lookup_signed_typename (pstate->language (),
1354 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1355 { $$ = lookup_signed_typename (pstate->language (),
1357 | UNSIGNED LONG LONG
1358 { $$ = lookup_unsigned_typename (pstate->language (),
1360 | UNSIGNED LONG LONG INT_KEYWORD
1361 { $$ = lookup_unsigned_typename (pstate->language (),
1363 | LONG LONG UNSIGNED
1364 { $$ = lookup_unsigned_typename (pstate->language (),
1366 | LONG LONG UNSIGNED INT_KEYWORD
1367 { $$ = lookup_unsigned_typename (pstate->language (),
1370 { $$ = lookup_signed_typename (pstate->language (),
1372 | SHORT SIGNED_KEYWORD INT_KEYWORD
1373 { $$ = lookup_signed_typename (pstate->language (),
1375 | SHORT SIGNED_KEYWORD
1376 { $$ = lookup_signed_typename (pstate->language (),
1378 | UNSIGNED SHORT INT_KEYWORD
1379 { $$ = lookup_unsigned_typename (pstate->language (),
1382 { $$ = lookup_unsigned_typename (pstate->language (),
1384 | SHORT UNSIGNED INT_KEYWORD
1385 { $$ = lookup_unsigned_typename (pstate->language (),
1388 { $$ = lookup_typename (pstate->language (),
1392 | LONG DOUBLE_KEYWORD
1393 { $$ = lookup_typename (pstate->language (),
1399 = lookup_struct (copy_name ($2).c_str (),
1400 pstate->expression_context_block);
1404 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1408 | STRUCT name COMPLETE
1410 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1415 { $$ = lookup_struct
1416 (copy_name ($2).c_str (),
1417 pstate->expression_context_block);
1421 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1425 | CLASS name COMPLETE
1427 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1433 = lookup_union (copy_name ($2).c_str (),
1434 pstate->expression_context_block);
1438 pstate->mark_completion_tag (TYPE_CODE_UNION,
1442 | UNION name COMPLETE
1444 pstate->mark_completion_tag (TYPE_CODE_UNION,
1449 { $$ = lookup_enum (copy_name ($2).c_str (),
1450 pstate->expression_context_block);
1454 pstate->mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1457 | ENUM name COMPLETE
1459 pstate->mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1463 | UNSIGNED type_name
1464 { $$ = lookup_unsigned_typename (pstate->language (),
1465 TYPE_NAME($2.type)); }
1467 { $$ = lookup_unsigned_typename (pstate->language (),
1469 | SIGNED_KEYWORD type_name
1470 { $$ = lookup_signed_typename (pstate->language (),
1471 TYPE_NAME($2.type)); }
1473 { $$ = lookup_signed_typename (pstate->language (),
1475 /* It appears that this rule for templates is never
1476 reduced; template recognition happens by lookahead
1477 in the token processing code in yylex. */
1478 | TEMPLATE name '<' type '>'
1479 { $$ = lookup_template_type
1480 (copy_name($2).c_str (), $4,
1481 pstate->expression_context_block);
1483 | qualifier_seq_noopt typebase
1484 { $$ = cpstate->type_stack.follow_types ($2); }
1485 | typebase qualifier_seq_noopt
1486 { $$ = cpstate->type_stack.follow_types ($1); }
1492 $$.stoken.ptr = "int";
1493 $$.stoken.length = 3;
1494 $$.type = lookup_signed_typename (pstate->language (),
1499 $$.stoken.ptr = "long";
1500 $$.stoken.length = 4;
1501 $$.type = lookup_signed_typename (pstate->language (),
1506 $$.stoken.ptr = "short";
1507 $$.stoken.length = 5;
1508 $$.type = lookup_signed_typename (pstate->language (),
1515 { check_parameter_typelist ($1); }
1516 | nonempty_typelist ',' DOTDOTDOT
1518 $1->push_back (NULL);
1519 check_parameter_typelist ($1);
1527 std::vector<struct type *> *typelist
1528 = new std::vector<struct type *>;
1529 cpstate->type_lists.emplace_back (typelist);
1531 typelist->push_back ($1);
1534 | nonempty_typelist ',' type
1544 cpstate->type_stack.push ($2);
1545 $$ = cpstate->type_stack.follow_types ($1);
1549 conversion_type_id: typebase conversion_declarator
1550 { $$ = cpstate->type_stack.follow_types ($1); }
1553 conversion_declarator: /* Nothing. */
1554 | ptr_operator conversion_declarator
1557 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1558 | VOLATILE_KEYWORD CONST_KEYWORD
1561 const_or_volatile_noopt: const_and_volatile
1562 { cpstate->type_stack.insert (tp_const);
1563 cpstate->type_stack.insert (tp_volatile);
1566 { cpstate->type_stack.insert (tp_const); }
1568 { cpstate->type_stack.insert (tp_volatile); }
1572 { $$ = operator_stoken (" new"); }
1574 { $$ = operator_stoken (" delete"); }
1575 | OPERATOR NEW '[' ']'
1576 { $$ = operator_stoken (" new[]"); }
1577 | OPERATOR DELETE '[' ']'
1578 { $$ = operator_stoken (" delete[]"); }
1579 | OPERATOR NEW OBJC_LBRAC ']'
1580 { $$ = operator_stoken (" new[]"); }
1581 | OPERATOR DELETE OBJC_LBRAC ']'
1582 { $$ = operator_stoken (" delete[]"); }
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 ("~"); }
1602 { $$ = operator_stoken ("!"); }
1604 { $$ = operator_stoken ("="); }
1606 { $$ = operator_stoken ("<"); }
1608 { $$ = operator_stoken (">"); }
1609 | OPERATOR ASSIGN_MODIFY
1610 { const char *op = " unknown";
1634 case BINOP_BITWISE_IOR:
1637 case BINOP_BITWISE_AND:
1640 case BINOP_BITWISE_XOR:
1647 $$ = operator_stoken (op);
1650 { $$ = operator_stoken ("<<"); }
1652 { $$ = operator_stoken (">>"); }
1654 { $$ = operator_stoken ("=="); }
1656 { $$ = operator_stoken ("!="); }
1658 { $$ = operator_stoken ("<="); }
1660 { $$ = operator_stoken (">="); }
1662 { $$ = operator_stoken ("&&"); }
1664 { $$ = operator_stoken ("||"); }
1665 | OPERATOR INCREMENT
1666 { $$ = operator_stoken ("++"); }
1667 | OPERATOR DECREMENT
1668 { $$ = operator_stoken ("--"); }
1670 { $$ = operator_stoken (","); }
1671 | OPERATOR ARROW_STAR
1672 { $$ = operator_stoken ("->*"); }
1674 { $$ = operator_stoken ("->"); }
1676 { $$ = operator_stoken ("()"); }
1678 { $$ = operator_stoken ("[]"); }
1679 | OPERATOR OBJC_LBRAC ']'
1680 { $$ = operator_stoken ("[]"); }
1681 | OPERATOR conversion_type_id
1684 c_print_type ($2, NULL, &buf, -1, 0,
1685 &type_print_raw_options);
1687 /* This also needs canonicalization. */
1689 = cp_canonicalize_string (buf.c_str ());
1691 canon = std::move (buf.string ());
1692 $$ = operator_stoken ((" " + canon).c_str ());
1696 /* This rule exists in order to allow some tokens that would not normally
1697 match the 'name' rule to appear as fields within a struct. The example
1698 that initially motivated this was the RISC-V target which models the
1699 floating point registers as a union with fields called 'float' and
1700 'double'. The 'float' string becomes a TYPENAME token and can appear
1701 anywhere a 'name' can, however 'double' is its own token,
1702 DOUBLE_KEYWORD, and doesn't match the 'name' rule.*/
1705 | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
1706 | INT_KEYWORD { $$ = typename_stoken ("int"); }
1707 | LONG { $$ = typename_stoken ("long"); }
1708 | SHORT { $$ = typename_stoken ("short"); }
1709 | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
1710 | UNSIGNED { $$ = typename_stoken ("unsigned"); }
1713 name : NAME { $$ = $1.stoken; }
1714 | BLOCKNAME { $$ = $1.stoken; }
1715 | TYPENAME { $$ = $1.stoken; }
1716 | NAME_OR_INT { $$ = $1.stoken; }
1717 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1721 name_not_typename : NAME
1723 /* These would be useful if name_not_typename was useful, but it is just
1724 a fake for "variable", so these cause reduce/reduce conflicts because
1725 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1726 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1727 context where only a name could occur, this might be useful.
1732 struct field_of_this_result is_a_field_of_this;
1736 = lookup_symbol ($1.ptr,
1737 pstate->expression_context_block,
1739 &is_a_field_of_this);
1740 $$.is_a_field_of_this
1741 = is_a_field_of_this.type != NULL;
1748 /* Like write_exp_string, but prepends a '~'. */
1751 write_destructor_name (struct parser_state *par_state, struct stoken token)
1753 char *copy = (char *) alloca (token.length + 1);
1756 memcpy (©[1], token.ptr, token.length);
1761 write_exp_string (par_state, token);
1764 /* Returns a stoken of the operator name given by OP (which does not
1765 include the string "operator"). */
1767 static struct stoken
1768 operator_stoken (const char *op)
1770 struct stoken st = { NULL, 0 };
1773 st.length = CP_OPERATOR_LEN + strlen (op);
1774 buf = (char *) malloc (st.length + 1);
1775 strcpy (buf, CP_OPERATOR_STR);
1779 /* The toplevel (c_parse) will free the memory allocated here. */
1780 cpstate->strings.emplace_back (buf);
1784 /* Returns a stoken of the type named TYPE. */
1786 static struct stoken
1787 typename_stoken (const char *type)
1789 struct stoken st = { type, 0 };
1790 st.length = strlen (type);
1794 /* Return true if the type is aggregate-like. */
1797 type_aggregate_p (struct type *type)
1799 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
1800 || TYPE_CODE (type) == TYPE_CODE_UNION
1801 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE
1802 || (TYPE_CODE (type) == TYPE_CODE_ENUM
1803 && TYPE_DECLARED_CLASS (type)));
1806 /* Validate a parameter typelist. */
1809 check_parameter_typelist (std::vector<struct type *> *params)
1814 for (ix = 0; ix < params->size (); ++ix)
1816 type = (*params)[ix];
1817 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1821 if (params->size () == 1)
1826 error (_("parameter types following 'void'"));
1829 error (_("'void' invalid as parameter type"));
1834 /* Take care of parsing a number (anything that starts with a digit).
1835 Set yylval and return the token type; update lexptr.
1836 LEN is the number of characters in it. */
1838 /*** Needs some error checking for the float case ***/
1841 parse_number (struct parser_state *par_state,
1842 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1850 int base = input_radix;
1853 /* Number of "L" suffixes encountered. */
1856 /* We have found a "L" or "U" suffix. */
1857 int found_suffix = 0;
1860 struct type *signed_type;
1861 struct type *unsigned_type;
1864 p = (char *) alloca (len);
1865 memcpy (p, buf, len);
1869 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
1870 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1872 putithere->typed_val_float.type
1873 = parse_type (par_state)->builtin_decfloat;
1876 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1878 putithere->typed_val_float.type
1879 = parse_type (par_state)->builtin_decdouble;
1882 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1884 putithere->typed_val_float.type
1885 = parse_type (par_state)->builtin_declong;
1888 /* Handle suffixes: 'f' for float, 'l' for long double. */
1889 else if (len >= 1 && TOLOWER (p[len - 1]) == 'f')
1891 putithere->typed_val_float.type
1892 = parse_type (par_state)->builtin_float;
1895 else if (len >= 1 && TOLOWER (p[len - 1]) == 'l')
1897 putithere->typed_val_float.type
1898 = parse_type (par_state)->builtin_long_double;
1901 /* Default type for floating-point literals is double. */
1904 putithere->typed_val_float.type
1905 = parse_type (par_state)->builtin_double;
1908 if (!parse_float (p, len,
1909 putithere->typed_val_float.type,
1910 putithere->typed_val_float.val))
1915 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1916 if (p[0] == '0' && len > 1)
1959 if (c >= 'A' && c <= 'Z')
1961 if (c != 'l' && c != 'u')
1963 if (c >= '0' && c <= '9')
1971 if (base > 10 && c >= 'a' && c <= 'f')
1975 n += i = c - 'a' + 10;
1988 return ERROR; /* Char not a digit */
1991 return ERROR; /* Invalid digit in this base */
1993 /* Portably test for overflow (only works for nonzero values, so make
1994 a second check for zero). FIXME: Can't we just make n and prevn
1995 unsigned and avoid this? */
1996 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1997 unsigned_p = 1; /* Try something unsigned */
1999 /* Portably test for unsigned overflow.
2000 FIXME: This check is wrong; for example it doesn't find overflow
2001 on 0x123456789 when LONGEST is 32 bits. */
2002 if (c != 'l' && c != 'u' && n != 0)
2004 if (unsigned_p && prevn >= n)
2005 error (_("Numeric constant too large."));
2010 /* An integer constant is an int, a long, or a long long. An L
2011 suffix forces it to be long; an LL suffix forces it to be long
2012 long. If not forced to a larger size, it gets the first type of
2013 the above that it fits in. To figure out whether it fits, we
2014 shift it right and see whether anything remains. Note that we
2015 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
2016 operation, because many compilers will warn about such a shift
2017 (which always produces a zero result). Sometimes gdbarch_int_bit
2018 or gdbarch_long_bit will be that big, sometimes not. To deal with
2019 the case where it is we just always shift the value more than
2020 once, with fewer bits each time. */
2024 && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0)
2027 = ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
2029 /* A large decimal (not hex or octal) constant (between INT_MAX
2030 and UINT_MAX) is a long or unsigned long, according to ANSI,
2031 never an unsigned int, but this code treats it as unsigned
2032 int. This probably should be fixed. GCC gives a warning on
2035 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
2036 signed_type = parse_type (par_state)->builtin_int;
2038 else if (long_p <= 1
2039 && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0)
2042 = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1);
2043 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
2044 signed_type = parse_type (par_state)->builtin_long;
2049 if (sizeof (ULONGEST) * HOST_CHAR_BIT
2050 < gdbarch_long_long_bit (par_state->gdbarch ()))
2051 /* A long long does not fit in a LONGEST. */
2052 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
2054 shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1);
2055 high_bit = (ULONGEST) 1 << shift;
2056 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
2057 signed_type = parse_type (par_state)->builtin_long_long;
2060 putithere->typed_val_int.val = n;
2062 /* If the high bit of the worked out type is set then this number
2063 has to be unsigned. */
2065 if (unsigned_p || (n & high_bit))
2067 putithere->typed_val_int.type = unsigned_type;
2071 putithere->typed_val_int.type = signed_type;
2077 /* Temporary obstack used for holding strings. */
2078 static struct obstack tempbuf;
2079 static int tempbuf_init;
2081 /* Parse a C escape sequence. The initial backslash of the sequence
2082 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2083 last character of the sequence. If OUTPUT is not NULL, the
2084 translated form of the escape sequence will be written there. If
2085 OUTPUT is NULL, no output is written and the call will only affect
2086 *PTR. If an escape sequence is expressed in target bytes, then the
2087 entire sequence will simply be copied to OUTPUT. Return 1 if any
2088 character was emitted, 0 otherwise. */
2091 c_parse_escape (const char **ptr, struct obstack *output)
2093 const char *tokptr = *ptr;
2096 /* Some escape sequences undergo character set conversion. Those we
2100 /* Hex escapes do not undergo character set conversion, so keep
2101 the escape sequence for later. */
2104 obstack_grow_str (output, "\\x");
2106 if (!ISXDIGIT (*tokptr))
2107 error (_("\\x escape without a following hex digit"));
2108 while (ISXDIGIT (*tokptr))
2111 obstack_1grow (output, *tokptr);
2116 /* Octal escapes do not undergo character set conversion, so
2117 keep the escape sequence for later. */
2129 obstack_grow_str (output, "\\");
2131 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
2135 obstack_1grow (output, *tokptr);
2141 /* We handle UCNs later. We could handle them here, but that
2142 would mean a spurious error in the case where the UCN could
2143 be converted to the target charset but not the host
2149 int i, len = c == 'U' ? 8 : 4;
2152 obstack_1grow (output, '\\');
2153 obstack_1grow (output, *tokptr);
2156 if (!ISXDIGIT (*tokptr))
2157 error (_("\\%c escape without a following hex digit"), c);
2158 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
2161 obstack_1grow (output, *tokptr);
2167 /* We must pass backslash through so that it does not
2168 cause quoting during the second expansion. */
2171 obstack_grow_str (output, "\\\\");
2175 /* Escapes which undergo conversion. */
2178 obstack_1grow (output, '\a');
2183 obstack_1grow (output, '\b');
2188 obstack_1grow (output, '\f');
2193 obstack_1grow (output, '\n');
2198 obstack_1grow (output, '\r');
2203 obstack_1grow (output, '\t');
2208 obstack_1grow (output, '\v');
2212 /* GCC extension. */
2215 obstack_1grow (output, HOST_ESCAPE_CHAR);
2219 /* Backslash-newline expands to nothing at all. */
2225 /* A few escapes just expand to the character itself. */
2229 /* GCC extensions. */
2234 /* Unrecognized escapes turn into the character itself. */
2237 obstack_1grow (output, *tokptr);
2245 /* Parse a string or character literal from TOKPTR. The string or
2246 character may be wide or unicode. *OUTPTR is set to just after the
2247 end of the literal in the input string. The resulting token is
2248 stored in VALUE. This returns a token value, either STRING or
2249 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2250 number of host characters in the literal. */
2253 parse_string_or_char (const char *tokptr, const char **outptr,
2254 struct typed_stoken *value, int *host_chars)
2260 /* Build the gdb internal form of the input string in tempbuf. Note
2261 that the buffer is null byte terminated *only* for the
2262 convenience of debugging gdb itself and printing the buffer
2263 contents when the buffer contains no embedded nulls. Gdb does
2264 not depend upon the buffer being null byte terminated, it uses
2265 the length string instead. This allows gdb to handle C strings
2266 (as well as strings in other languages) with embedded null
2272 obstack_free (&tempbuf, NULL);
2273 obstack_init (&tempbuf);
2275 /* Record the string type. */
2278 type = C_WIDE_STRING;
2281 else if (*tokptr == 'u')
2286 else if (*tokptr == 'U')
2291 else if (*tokptr == '@')
2293 /* An Objective C string. */
2301 /* Skip the quote. */
2315 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2317 else if (c == quote)
2321 obstack_1grow (&tempbuf, c);
2323 /* FIXME: this does the wrong thing with multi-byte host
2324 characters. We could use mbrlen here, but that would
2325 make "set host-charset" a bit less useful. */
2330 if (*tokptr != quote)
2333 error (_("Unterminated string in expression."));
2335 error (_("Unmatched single quote."));
2340 value->ptr = (char *) obstack_base (&tempbuf);
2341 value->length = obstack_object_size (&tempbuf);
2345 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2348 /* This is used to associate some attributes with a token. */
2352 /* If this bit is set, the token is C++-only. */
2356 /* If this bit is set, the token is C-only. */
2360 /* If this bit is set, the token is conditional: if there is a
2361 symbol of the same name, then the token is a symbol; otherwise,
2362 the token is a keyword. */
2366 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2372 enum exp_opcode opcode;
2376 static const struct token tokentab3[] =
2378 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2379 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2380 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2381 {"...", DOTDOTDOT, BINOP_END, 0}
2384 static const struct token tokentab2[] =
2386 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2387 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2388 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2389 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2390 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2391 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2392 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2393 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2394 {"++", INCREMENT, BINOP_END, 0},
2395 {"--", DECREMENT, BINOP_END, 0},
2396 {"->", ARROW, BINOP_END, 0},
2397 {"&&", ANDAND, BINOP_END, 0},
2398 {"||", OROR, BINOP_END, 0},
2399 /* "::" is *not* only C++: gdb overrides its meaning in several
2400 different ways, e.g., 'filename'::func, function::variable. */
2401 {"::", COLONCOLON, BINOP_END, 0},
2402 {"<<", LSH, BINOP_END, 0},
2403 {">>", RSH, BINOP_END, 0},
2404 {"==", EQUAL, BINOP_END, 0},
2405 {"!=", NOTEQUAL, BINOP_END, 0},
2406 {"<=", LEQ, BINOP_END, 0},
2407 {">=", GEQ, BINOP_END, 0},
2408 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2411 /* Identifier-like tokens. Only type-specifiers than can appear in
2412 multi-word type names (for example 'double' can appear in 'long
2413 double') need to be listed here. type-specifiers that are only ever
2414 single word (like 'float') are handled by the classify_name function. */
2415 static const struct token ident_tokens[] =
2417 {"unsigned", UNSIGNED, OP_NULL, 0},
2418 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2419 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2420 {"struct", STRUCT, OP_NULL, 0},
2421 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2422 {"sizeof", SIZEOF, OP_NULL, 0},
2423 {"_Alignof", ALIGNOF, OP_NULL, 0},
2424 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
2425 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2426 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2427 {"class", CLASS, OP_NULL, FLAG_CXX},
2428 {"union", UNION, OP_NULL, 0},
2429 {"short", SHORT, OP_NULL, 0},
2430 {"const", CONST_KEYWORD, OP_NULL, 0},
2431 {"restrict", RESTRICT, OP_NULL, FLAG_C | FLAG_SHADOW},
2432 {"__restrict__", RESTRICT, OP_NULL, 0},
2433 {"__restrict", RESTRICT, OP_NULL, 0},
2434 {"_Atomic", ATOMIC, OP_NULL, 0},
2435 {"enum", ENUM, OP_NULL, 0},
2436 {"long", LONG, OP_NULL, 0},
2437 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2438 {"int", INT_KEYWORD, OP_NULL, 0},
2439 {"new", NEW, OP_NULL, FLAG_CXX},
2440 {"delete", DELETE, OP_NULL, FLAG_CXX},
2441 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2443 {"and", ANDAND, BINOP_END, FLAG_CXX},
2444 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2445 {"bitand", '&', OP_NULL, FLAG_CXX},
2446 {"bitor", '|', OP_NULL, FLAG_CXX},
2447 {"compl", '~', OP_NULL, FLAG_CXX},
2448 {"not", '!', OP_NULL, FLAG_CXX},
2449 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2450 {"or", OROR, BINOP_END, FLAG_CXX},
2451 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2452 {"xor", '^', OP_NULL, FLAG_CXX},
2453 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2455 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2456 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2457 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2458 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2460 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2461 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2462 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2463 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2464 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2466 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2471 scan_macro_expansion (char *expansion)
2475 /* We'd better not be trying to push the stack twice. */
2476 gdb_assert (! cpstate->macro_original_text);
2478 /* Copy to the obstack, and then free the intermediate
2480 copy = obstack_strdup (&cpstate->expansion_obstack, expansion);
2483 /* Save the old lexptr value, so we can return to it when we're done
2484 parsing the expanded text. */
2485 cpstate->macro_original_text = pstate->lexptr;
2486 pstate->lexptr = copy;
2490 scanning_macro_expansion (void)
2492 return cpstate->macro_original_text != 0;
2496 finished_macro_expansion (void)
2498 /* There'd better be something to pop back to. */
2499 gdb_assert (cpstate->macro_original_text);
2501 /* Pop back to the original text. */
2502 pstate->lexptr = cpstate->macro_original_text;
2503 cpstate->macro_original_text = 0;
2506 /* Return true iff the token represents a C++ cast operator. */
2509 is_cast_operator (const char *token, int len)
2511 return (! strncmp (token, "dynamic_cast", len)
2512 || ! strncmp (token, "static_cast", len)
2513 || ! strncmp (token, "reinterpret_cast", len)
2514 || ! strncmp (token, "const_cast", len));
2517 /* The scope used for macro expansion. */
2518 static struct macro_scope *expression_macro_scope;
2520 /* This is set if a NAME token appeared at the very end of the input
2521 string, with no whitespace separating the name from the EOF. This
2522 is used only when parsing to do field name completion. */
2523 static int saw_name_at_eof;
2525 /* This is set if the previously-returned token was a structure
2526 operator -- either '.' or ARROW. */
2527 static bool last_was_structop;
2529 /* Depth of parentheses. */
2530 static int paren_depth;
2532 /* Read one token, getting characters through lexptr. */
2535 lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
2540 const char *tokstart;
2541 bool saw_structop = last_was_structop;
2543 last_was_structop = false;
2544 *is_quoted_name = false;
2548 /* Check if this is a macro invocation that we need to expand. */
2549 if (! scanning_macro_expansion ())
2551 char *expanded = macro_expand_next (&pstate->lexptr,
2552 standard_macro_lookup,
2553 expression_macro_scope);
2556 scan_macro_expansion (expanded);
2559 pstate->prev_lexptr = pstate->lexptr;
2561 tokstart = pstate->lexptr;
2562 /* See if it is a special token of length 3. */
2563 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2564 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
2566 if ((tokentab3[i].flags & FLAG_CXX) != 0
2567 && par_state->language ()->la_language != language_cplus)
2569 gdb_assert ((tokentab3[i].flags & FLAG_C) == 0);
2571 pstate->lexptr += 3;
2572 yylval.opcode = tokentab3[i].opcode;
2573 return tokentab3[i].token;
2576 /* See if it is a special token of length 2. */
2577 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2578 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
2580 if ((tokentab2[i].flags & FLAG_CXX) != 0
2581 && par_state->language ()->la_language != language_cplus)
2583 gdb_assert ((tokentab3[i].flags & FLAG_C) == 0);
2585 pstate->lexptr += 2;
2586 yylval.opcode = tokentab2[i].opcode;
2587 if (tokentab2[i].token == ARROW)
2588 last_was_structop = 1;
2589 return tokentab2[i].token;
2592 switch (c = *tokstart)
2595 /* If we were just scanning the result of a macro expansion,
2596 then we need to resume scanning the original text.
2597 If we're parsing for field name completion, and the previous
2598 token allows such completion, return a COMPLETE token.
2599 Otherwise, we were already scanning the original text, and
2600 we're really done. */
2601 if (scanning_macro_expansion ())
2603 finished_macro_expansion ();
2606 else if (saw_name_at_eof)
2608 saw_name_at_eof = 0;
2611 else if (par_state->parse_completion && saw_structop)
2626 if (par_state->language ()->la_language == language_objc
2633 if (paren_depth == 0)
2640 if (pstate->comma_terminates
2642 && ! scanning_macro_expansion ())
2648 /* Might be a floating point number. */
2649 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
2651 last_was_structop = true;
2652 goto symbol; /* Nope, must be a symbol. */
2667 /* It's a number. */
2668 int got_dot = 0, got_e = 0, toktype;
2669 const char *p = tokstart;
2670 int hex = input_radix > 10;
2672 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2677 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2685 /* This test includes !hex because 'e' is a valid hex digit
2686 and thus does not indicate a floating point number when
2687 the radix is hex. */
2688 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2689 got_dot = got_e = 1;
2690 /* This test does not include !hex, because a '.' always indicates
2691 a decimal floating point number regardless of the radix. */
2692 else if (!got_dot && *p == '.')
2694 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2695 && (*p == '-' || *p == '+'))
2696 /* This is the sign of the exponent, not the end of the
2699 /* We will take any letters or digits. parse_number will
2700 complain if past the radix, or if L or U are not final. */
2701 else if ((*p < '0' || *p > '9')
2702 && ((*p < 'a' || *p > 'z')
2703 && (*p < 'A' || *p > 'Z')))
2706 toktype = parse_number (par_state, tokstart, p - tokstart,
2707 got_dot|got_e, &yylval);
2708 if (toktype == ERROR)
2710 char *err_copy = (char *) alloca (p - tokstart + 1);
2712 memcpy (err_copy, tokstart, p - tokstart);
2713 err_copy[p - tokstart] = 0;
2714 error (_("Invalid number \"%s\"."), err_copy);
2722 const char *p = &tokstart[1];
2724 if (par_state->language ()->la_language == language_objc)
2726 size_t len = strlen ("selector");
2728 if (strncmp (p, "selector", len) == 0
2729 && (p[len] == '\0' || ISSPACE (p[len])))
2731 pstate->lexptr = p + len;
2738 while (ISSPACE (*p))
2740 size_t len = strlen ("entry");
2741 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
2744 pstate->lexptr = &p[len];
2773 if (tokstart[1] != '"' && tokstart[1] != '\'')
2782 int result = parse_string_or_char (tokstart, &pstate->lexptr,
2783 &yylval.tsval, &host_len);
2787 error (_("Empty character constant."));
2788 else if (host_len > 2 && c == '\'')
2791 namelen = pstate->lexptr - tokstart - 1;
2792 *is_quoted_name = true;
2796 else if (host_len > 1)
2797 error (_("Invalid character constant."));
2803 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
2804 /* We must have come across a bad character (e.g. ';'). */
2805 error (_("Invalid character '%c' in expression."), c);
2807 /* It's a name. See how long it is. */
2809 for (c = tokstart[namelen];
2810 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
2812 /* Template parameter lists are part of the name.
2813 FIXME: This mishandles `print $a<4&&$a>3'. */
2817 if (! is_cast_operator (tokstart, namelen))
2819 /* Scan ahead to get rest of the template specification. Note
2820 that we look ahead only when the '<' adjoins non-whitespace
2821 characters; for comparison expressions, e.g. "a < b > c",
2822 there must be spaces before the '<', etc. */
2823 const char *p = find_template_name_end (tokstart + namelen);
2826 namelen = p - tokstart;
2830 c = tokstart[++namelen];
2833 /* The token "if" terminates the expression and is NOT removed from
2834 the input stream. It doesn't count if it appears in the
2835 expansion of a macro. */
2837 && tokstart[0] == 'i'
2838 && tokstart[1] == 'f'
2839 && ! scanning_macro_expansion ())
2844 /* For the same reason (breakpoint conditions), "thread N"
2845 terminates the expression. "thread" could be an identifier, but
2846 an identifier is never followed by a number without intervening
2847 punctuation. "task" is similar. Handle abbreviations of these,
2848 similarly to breakpoint.c:find_condition_and_thread. */
2850 && (strncmp (tokstart, "thread", namelen) == 0
2851 || strncmp (tokstart, "task", namelen) == 0)
2852 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2853 && ! scanning_macro_expansion ())
2855 const char *p = tokstart + namelen + 1;
2857 while (*p == ' ' || *p == '\t')
2859 if (*p >= '0' && *p <= '9')
2863 pstate->lexptr += namelen;
2867 yylval.sval.ptr = tokstart;
2868 yylval.sval.length = namelen;
2870 /* Catch specific keywords. */
2871 std::string copy = copy_name (yylval.sval);
2872 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2873 if (copy == ident_tokens[i].oper)
2875 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2876 && par_state->language ()->la_language != language_cplus)
2878 if ((ident_tokens[i].flags & FLAG_C) != 0
2879 && par_state->language ()->la_language != language_c
2880 && par_state->language ()->la_language != language_objc)
2883 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2885 struct field_of_this_result is_a_field_of_this;
2887 if (lookup_symbol (copy.c_str (),
2888 pstate->expression_context_block,
2890 (par_state->language ()->la_language
2891 == language_cplus ? &is_a_field_of_this
2895 /* The keyword is shadowed. */
2900 /* It is ok to always set this, even though we don't always
2901 strictly need to. */
2902 yylval.opcode = ident_tokens[i].opcode;
2903 return ident_tokens[i].token;
2906 if (*tokstart == '$')
2907 return DOLLAR_VARIABLE;
2909 if (pstate->parse_completion && *pstate->lexptr == '\0')
2910 saw_name_at_eof = 1;
2912 yylval.ssym.stoken = yylval.sval;
2913 yylval.ssym.sym.symbol = NULL;
2914 yylval.ssym.sym.block = NULL;
2915 yylval.ssym.is_a_field_of_this = 0;
2919 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2920 struct token_and_value
2926 /* A FIFO of tokens that have been read but not yet returned to the
2928 static std::vector<token_and_value> token_fifo;
2930 /* Non-zero if the lexer should return tokens from the FIFO. */
2933 /* Temporary storage for c_lex; this holds symbol names as they are
2935 auto_obstack name_obstack;
2937 /* Classify a NAME token. The contents of the token are in `yylval'.
2938 Updates yylval and returns the new token type. BLOCK is the block
2939 in which lookups start; this can be NULL to mean the global scope.
2940 IS_QUOTED_NAME is non-zero if the name token was originally quoted
2941 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
2942 a structure operator -- either '.' or ARROW */
2945 classify_name (struct parser_state *par_state, const struct block *block,
2946 bool is_quoted_name, bool is_after_structop)
2948 struct block_symbol bsym;
2949 struct field_of_this_result is_a_field_of_this;
2951 std::string copy = copy_name (yylval.sval);
2953 /* Initialize this in case we *don't* use it in this call; that way
2954 we can refer to it unconditionally below. */
2955 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
2957 bsym = lookup_symbol (copy.c_str (), block, VAR_DOMAIN,
2958 par_state->language ()->la_name_of_this
2959 ? &is_a_field_of_this : NULL);
2961 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK)
2963 yylval.ssym.sym = bsym;
2964 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2967 else if (!bsym.symbol)
2969 /* If we found a field of 'this', we might have erroneously
2970 found a constructor where we wanted a type name. Handle this
2971 case by noticing that we found a constructor and then look up
2972 the type tag instead. */
2973 if (is_a_field_of_this.type != NULL
2974 && is_a_field_of_this.fn_field != NULL
2975 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
2978 struct field_of_this_result inner_is_a_field_of_this;
2980 bsym = lookup_symbol (copy.c_str (), block, STRUCT_DOMAIN,
2981 &inner_is_a_field_of_this);
2982 if (bsym.symbol != NULL)
2984 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
2989 /* If we found a field on the "this" object, or we are looking
2990 up a field on a struct, then we want to prefer it over a
2991 filename. However, if the name was quoted, then it is better
2992 to check for a filename or a block, since this is the only
2993 way the user has of requiring the extension to be used. */
2994 if ((is_a_field_of_this.type == NULL && !is_after_structop)
2997 /* See if it's a file name. */
2998 struct symtab *symtab;
3000 symtab = lookup_symtab (copy.c_str ());
3003 yylval.bval = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab),
3010 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF)
3012 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3016 /* See if it's an ObjC classname. */
3017 if (par_state->language ()->la_language == language_objc && !bsym.symbol)
3019 CORE_ADDR Class = lookup_objc_class (par_state->gdbarch (),
3025 yylval.theclass.theclass = Class;
3026 sym = lookup_struct_typedef (copy.c_str (),
3027 par_state->expression_context_block, 1);
3029 yylval.theclass.type = SYMBOL_TYPE (sym);
3034 /* Input names that aren't symbols but ARE valid hex numbers, when
3035 the input radix permits them, can be names or numbers depending
3036 on the parse. Note we support radixes > 16 here. */
3038 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3039 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3041 YYSTYPE newlval; /* Its value is ignored. */
3042 int hextype = parse_number (par_state, copy.c_str (), yylval.sval.length,
3047 yylval.ssym.sym = bsym;
3048 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3053 /* Any other kind of symbol */
3054 yylval.ssym.sym = bsym;
3055 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3057 if (bsym.symbol == NULL
3058 && par_state->language ()->la_language == language_cplus
3059 && is_a_field_of_this.type == NULL
3060 && lookup_minimal_symbol (copy.c_str (), NULL, NULL).minsym == NULL)
3061 return UNKNOWN_CPP_NAME;
3066 /* Like classify_name, but used by the inner loop of the lexer, when a
3067 name might have already been seen. CONTEXT is the context type, or
3068 NULL if this is the first component of a name. */
3071 classify_inner_name (struct parser_state *par_state,
3072 const struct block *block, struct type *context)
3076 if (context == NULL)
3077 return classify_name (par_state, block, false, false);
3079 type = check_typedef (context);
3080 if (!type_aggregate_p (type))
3083 std::string copy = copy_name (yylval.ssym.stoken);
3084 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3085 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy.c_str (), block,
3088 /* If no symbol was found, search for a matching base class named
3089 COPY. This will allow users to enter qualified names of class members
3090 relative to the `this' pointer. */
3091 if (yylval.ssym.sym.symbol == NULL)
3093 struct type *base_type = cp_find_type_baseclass_by_name (type,
3096 if (base_type != NULL)
3098 yylval.tsym.type = base_type;
3105 switch (SYMBOL_CLASS (yylval.ssym.sym.symbol))
3109 /* cp_lookup_nested_symbol might have accidentally found a constructor
3110 named COPY when we really wanted a base class of the same name.
3111 Double-check this case by looking for a base class. */
3113 struct type *base_type
3114 = cp_find_type_baseclass_by_name (type, copy.c_str ());
3116 if (base_type != NULL)
3118 yylval.tsym.type = base_type;
3125 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
3131 internal_error (__FILE__, __LINE__, _("not reached"));
3134 /* The outer level of a two-level lexer. This calls the inner lexer
3135 to return tokens. It then either returns these tokens, or
3136 aggregates them into a larger token. This lets us work around a
3137 problem in our parsing approach, where the parser could not
3138 distinguish between qualified names and qualified types at the
3141 This approach is still not ideal, because it mishandles template
3142 types. See the comment in lex_one_token for an example. However,
3143 this is still an improvement over the earlier approach, and will
3144 suffice until we move to better parsing technology. */
3149 token_and_value current;
3150 int first_was_coloncolon, last_was_coloncolon;
3151 struct type *context_type = NULL;
3152 int last_to_examine, next_to_examine, checkpoint;
3153 const struct block *search_block;
3154 bool is_quoted_name, last_lex_was_structop;
3156 if (popping && !token_fifo.empty ())
3160 last_lex_was_structop = last_was_structop;
3162 /* Read the first token and decide what to do. Most of the
3163 subsequent code is C++-only; but also depends on seeing a "::" or
3165 current.token = lex_one_token (pstate, &is_quoted_name);
3166 if (current.token == NAME)
3167 current.token = classify_name (pstate, pstate->expression_context_block,
3168 is_quoted_name, last_lex_was_structop);
3169 if (pstate->language ()->la_language != language_cplus
3170 || (current.token != TYPENAME && current.token != COLONCOLON
3171 && current.token != FILENAME))
3172 return current.token;
3174 /* Read any sequence of alternating "::" and name-like tokens into
3176 current.value = yylval;
3177 token_fifo.push_back (current);
3178 last_was_coloncolon = current.token == COLONCOLON;
3183 /* We ignore quoted names other than the very first one.
3184 Subsequent ones do not have any special meaning. */
3185 current.token = lex_one_token (pstate, &ignore);
3186 current.value = yylval;
3187 token_fifo.push_back (current);
3189 if ((last_was_coloncolon && current.token != NAME)
3190 || (!last_was_coloncolon && current.token != COLONCOLON))
3192 last_was_coloncolon = !last_was_coloncolon;
3196 /* We always read one extra token, so compute the number of tokens
3197 to examine accordingly. */
3198 last_to_examine = token_fifo.size () - 2;
3199 next_to_examine = 0;
3201 current = token_fifo[next_to_examine];
3204 name_obstack.clear ();
3206 if (current.token == FILENAME)
3207 search_block = current.value.bval;
3208 else if (current.token == COLONCOLON)
3209 search_block = NULL;
3212 gdb_assert (current.token == TYPENAME);
3213 search_block = pstate->expression_context_block;
3214 obstack_grow (&name_obstack, current.value.sval.ptr,
3215 current.value.sval.length);
3216 context_type = current.value.tsym.type;
3220 first_was_coloncolon = current.token == COLONCOLON;
3221 last_was_coloncolon = first_was_coloncolon;
3223 while (next_to_examine <= last_to_examine)
3225 token_and_value next;
3227 next = token_fifo[next_to_examine];
3230 if (next.token == NAME && last_was_coloncolon)
3234 yylval = next.value;
3235 classification = classify_inner_name (pstate, search_block,
3237 /* We keep going until we either run out of names, or until
3238 we have a qualified name which is not a type. */
3239 if (classification != TYPENAME && classification != NAME)
3242 /* Accept up to this token. */
3243 checkpoint = next_to_examine;
3245 /* Update the partial name we are constructing. */
3246 if (context_type != NULL)
3248 /* We don't want to put a leading "::" into the name. */
3249 obstack_grow_str (&name_obstack, "::");
3251 obstack_grow (&name_obstack, next.value.sval.ptr,
3252 next.value.sval.length);
3254 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
3255 yylval.sval.length = obstack_object_size (&name_obstack);
3256 current.value = yylval;
3257 current.token = classification;
3259 last_was_coloncolon = 0;
3261 if (classification == NAME)
3264 context_type = yylval.tsym.type;
3266 else if (next.token == COLONCOLON && !last_was_coloncolon)
3267 last_was_coloncolon = 1;
3270 /* We've reached the end of the name. */
3275 /* If we have a replacement token, install it as the first token in
3276 the FIFO, and delete the other constituent tokens. */
3279 current.value.sval.ptr
3280 = obstack_strndup (&cpstate->expansion_obstack,
3281 current.value.sval.ptr,
3282 current.value.sval.length);
3284 token_fifo[0] = current;
3286 token_fifo.erase (token_fifo.begin () + 1,
3287 token_fifo.begin () + checkpoint);
3291 current = token_fifo[0];
3292 token_fifo.erase (token_fifo.begin ());
3293 yylval = current.value;
3294 return current.token;
3298 c_parse (struct parser_state *par_state)
3300 /* Setting up the parser state. */
3301 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3302 gdb_assert (par_state != NULL);
3305 c_parse_state cstate;
3306 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3308 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
3310 if (par_state->expression_context_block)
3312 = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
3314 macro_scope = default_macro_scope ();
3316 macro_scope = user_macro_scope ();
3318 scoped_restore restore_macro_scope
3319 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
3321 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3324 /* Initialize some state used by the lexer. */
3325 last_was_structop = false;
3326 saw_name_at_eof = 0;
3329 token_fifo.clear ();
3331 name_obstack.clear ();
3338 /* This is called via the YYPRINT macro when parser debugging is
3339 enabled. It prints a token's value. */
3342 c_print_token (FILE *file, int type, YYSTYPE value)
3347 parser_fprintf (file, "typed_val_int<%s, %s>",
3348 TYPE_SAFE_NAME (value.typed_val_int.type),
3349 pulongest (value.typed_val_int.val));
3355 char *copy = (char *) alloca (value.tsval.length + 1);
3357 memcpy (copy, value.tsval.ptr, value.tsval.length);
3358 copy[value.tsval.length] = '\0';
3360 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3365 case DOLLAR_VARIABLE:
3366 parser_fprintf (file, "sval<%s>", copy_name (value.sval).c_str ());
3370 parser_fprintf (file, "tsym<type=%s, name=%s>",
3371 TYPE_SAFE_NAME (value.tsym.type),
3372 copy_name (value.tsym.stoken).c_str ());
3376 case UNKNOWN_CPP_NAME:
3379 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3380 copy_name (value.ssym.stoken).c_str (),
3381 (value.ssym.sym.symbol == NULL
3382 ? "(null)" : value.ssym.sym.symbol->print_name ()),
3383 value.ssym.is_a_field_of_this);
3387 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3395 yyerror (const char *msg)
3397 if (pstate->prev_lexptr)
3398 pstate->lexptr = pstate->prev_lexptr;
3400 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);