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"
57 #define parse_type(ps) builtin_type (parse_gdbarch (ps))
59 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
61 #define GDB_YY_REMAP_PREFIX c_
64 /* The state of the parser, used internally when we are parsing the
67 static struct parser_state *pstate = NULL;
69 /* Data that must be held for the duration of a parse. */
73 /* These are used to hold type lists and type stacks that are
74 allocated during the parse. */
75 std::vector<std::unique_ptr<std::vector<struct type *>>> type_lists;
76 std::vector<std::unique_ptr<struct type_stack>> type_stacks;
78 /* Storage for some strings allocated during the parse. */
79 std::vector<gdb::unique_xmalloc_ptr<char>> strings;
81 /* When we find that lexptr (the global var defined in parse.c) is
82 pointing at a macro invocation, we expand the invocation, and call
83 scan_macro_expansion to save the old lexptr here and point lexptr
84 into the expanded text. When we reach the end of that, we call
85 end_macro_expansion to pop back to the value we saved here. The
86 macro expansion code promises to return only fully-expanded text,
87 so we don't need to "push" more than one level.
89 This is disgusting, of course. It would be cleaner to do all macro
90 expansion beforehand, and then hand that to lexptr. But we don't
91 really know where the expression ends. Remember, in a command like
93 (gdb) break *ADDRESS if CONDITION
95 we evaluate ADDRESS in the scope of the current frame, but we
96 evaluate CONDITION in the scope of the breakpoint's location. So
97 it's simply wrong to try to macro-expand the whole thing at once. */
98 const char *macro_original_text = nullptr;
100 /* We save all intermediate macro expansions on this obstack for the
101 duration of a single parse. The expansion text may sometimes have
102 to live past the end of the expansion, due to yacc lookahead.
103 Rather than try to be clever about saving the data for a single
104 token, we simply keep it all and delete it after parsing has
106 auto_obstack expansion_obstack;
109 /* This is set and cleared in c_parse. */
111 static struct c_parse_state *cpstate;
115 static int yylex (void);
117 static void yyerror (const char *);
119 static int type_aggregate_p (struct type *);
123 /* Although the yacc "value" of an expression is not used,
124 since the result is stored in the structure being created,
125 other node types do have values. */
140 struct typed_stoken tsval;
142 struct symtoken ssym;
144 const struct block *bval;
145 enum exp_opcode opcode;
147 struct stoken_vector svec;
148 std::vector<struct type *> *tvec;
150 struct type_stack *type_stack;
152 struct objc_class_str theclass;
156 /* YYSTYPE gets defined by %union */
157 static int parse_number (struct parser_state *par_state,
158 const char *, int, int, YYSTYPE *);
159 static struct stoken operator_stoken (const char *);
160 static struct stoken typename_stoken (const char *);
161 static void check_parameter_typelist (std::vector<struct type *> *);
162 static void write_destructor_name (struct parser_state *par_state,
166 static void c_print_token (FILE *file, int type, YYSTYPE value);
167 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
171 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly function_method
173 %type <tval> type typebase
174 %type <tvec> nonempty_typelist func_mod parameter_typelist
175 /* %type <bval> block */
177 /* Fancy type parsing. */
179 %type <lval> array_mod
180 %type <tval> conversion_type_id
182 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
184 %token <typed_val_int> INT
185 %token <typed_val_float> FLOAT
187 /* Both NAME and TYPENAME tokens represent symbols in the input,
188 and both convey their data as strings.
189 But a TYPENAME is a string that happens to be defined as a typedef
190 or builtin type name (such as int or char)
191 and a NAME is any other symbol.
192 Contexts where this distinction is not important can use the
193 nonterminal "name", which matches either NAME or TYPENAME. */
195 %token <tsval> STRING
196 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
197 %token SELECTOR /* ObjC "@selector" pseudo-operator */
199 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
200 %token <ssym> UNKNOWN_CPP_NAME
201 %token <voidval> COMPLETE
202 %token <tsym> TYPENAME
203 %token <theclass> CLASSNAME /* ObjC Class name */
204 %type <sval> name field_name
205 %type <svec> string_exp
206 %type <ssym> name_not_typename
207 %type <tsym> type_name
209 /* This is like a '[' token, but is only generated when parsing
210 Objective C. This lets us reuse the same parser without
211 erroneously parsing ObjC-specific expressions in C. */
214 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
215 but which would parse as a valid number in the current input radix.
216 E.g. "c" when input_radix==16. Depending on the parse, it will be
217 turned into a name or into a number. */
219 %token <ssym> NAME_OR_INT
222 %token STRUCT CLASS UNION ENUM SIZEOF ALIGNOF UNSIGNED COLONCOLON
227 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
233 /* Special type cases, put in to allow the parser to distinguish different
235 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
237 %token <sval> DOLLAR_VARIABLE
239 %token <opcode> ASSIGN_MODIFY
248 %right '=' ASSIGN_MODIFY
256 %left '<' '>' LEQ GEQ
261 %right UNARY INCREMENT DECREMENT
262 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
263 %token <ssym> BLOCKNAME
264 %token <bval> FILENAME
278 { write_exp_elt_opcode(pstate, OP_TYPE);
279 write_exp_elt_type(pstate, $1);
280 write_exp_elt_opcode(pstate, OP_TYPE);}
283 write_exp_elt_opcode (pstate, OP_TYPEOF);
285 | TYPEOF '(' type ')'
287 write_exp_elt_opcode (pstate, OP_TYPE);
288 write_exp_elt_type (pstate, $3);
289 write_exp_elt_opcode (pstate, OP_TYPE);
291 | DECLTYPE '(' exp ')'
293 write_exp_elt_opcode (pstate, OP_DECLTYPE);
297 /* Expressions, including the comma operator. */
300 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
303 /* Expressions, not including the comma operator. */
304 exp : '*' exp %prec UNARY
305 { write_exp_elt_opcode (pstate, UNOP_IND); }
308 exp : '&' exp %prec UNARY
309 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
312 exp : '-' exp %prec UNARY
313 { write_exp_elt_opcode (pstate, UNOP_NEG); }
316 exp : '+' exp %prec UNARY
317 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
320 exp : '!' exp %prec UNARY
321 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
324 exp : '~' exp %prec UNARY
325 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
328 exp : INCREMENT exp %prec UNARY
329 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
332 exp : DECREMENT exp %prec UNARY
333 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
336 exp : exp INCREMENT %prec UNARY
337 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
340 exp : exp DECREMENT %prec UNARY
341 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
344 exp : TYPEID '(' exp ')' %prec UNARY
345 { write_exp_elt_opcode (pstate, OP_TYPEID); }
348 exp : TYPEID '(' type_exp ')' %prec UNARY
349 { write_exp_elt_opcode (pstate, OP_TYPEID); }
352 exp : SIZEOF exp %prec UNARY
353 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
356 exp : ALIGNOF '(' type_exp ')' %prec UNARY
357 { write_exp_elt_opcode (pstate, UNOP_ALIGNOF); }
360 exp : exp ARROW field_name
361 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
362 write_exp_string (pstate, $3);
363 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
366 exp : exp ARROW field_name COMPLETE
367 { mark_struct_expression (pstate);
368 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
369 write_exp_string (pstate, $3);
370 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
373 exp : exp ARROW COMPLETE
375 mark_struct_expression (pstate);
376 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
379 write_exp_string (pstate, s);
380 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
383 exp : exp ARROW '~' name
384 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
385 write_destructor_name (pstate, $4);
386 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
389 exp : exp ARROW '~' name COMPLETE
390 { mark_struct_expression (pstate);
391 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
392 write_destructor_name (pstate, $4);
393 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
396 exp : exp ARROW qualified_name
397 { /* exp->type::name becomes exp->*(&type::name) */
398 /* Note: this doesn't work if name is a
399 static member! FIXME */
400 write_exp_elt_opcode (pstate, UNOP_ADDR);
401 write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
404 exp : exp ARROW_STAR exp
405 { write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
408 exp : exp '.' field_name
409 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
410 write_exp_string (pstate, $3);
411 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
414 exp : exp '.' field_name COMPLETE
415 { mark_struct_expression (pstate);
416 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
417 write_exp_string (pstate, $3);
418 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
421 exp : exp '.' COMPLETE
423 mark_struct_expression (pstate);
424 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
427 write_exp_string (pstate, s);
428 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
431 exp : exp '.' '~' name
432 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
433 write_destructor_name (pstate, $4);
434 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
437 exp : exp '.' '~' name COMPLETE
438 { mark_struct_expression (pstate);
439 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
440 write_destructor_name (pstate, $4);
441 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
444 exp : exp '.' qualified_name
445 { /* exp.type::name becomes exp.*(&type::name) */
446 /* Note: this doesn't work if name is a
447 static member! FIXME */
448 write_exp_elt_opcode (pstate, UNOP_ADDR);
449 write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
452 exp : exp DOT_STAR exp
453 { write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
456 exp : exp '[' exp1 ']'
457 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
460 exp : exp OBJC_LBRAC exp1 ']'
461 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
465 * The rules below parse ObjC message calls of the form:
466 * '[' target selector {':' argument}* ']'
469 exp : OBJC_LBRAC TYPENAME
473 theclass = lookup_objc_class (parse_gdbarch (pstate),
474 copy_name ($2.stoken));
476 error (_("%s is not an ObjC Class"),
477 copy_name ($2.stoken));
478 write_exp_elt_opcode (pstate, OP_LONG);
479 write_exp_elt_type (pstate,
480 parse_type (pstate)->builtin_int);
481 write_exp_elt_longcst (pstate, (LONGEST) theclass);
482 write_exp_elt_opcode (pstate, OP_LONG);
486 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
487 end_msglist (pstate);
488 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
492 exp : OBJC_LBRAC CLASSNAME
494 write_exp_elt_opcode (pstate, OP_LONG);
495 write_exp_elt_type (pstate,
496 parse_type (pstate)->builtin_int);
497 write_exp_elt_longcst (pstate, (LONGEST) $2.theclass);
498 write_exp_elt_opcode (pstate, OP_LONG);
502 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
503 end_msglist (pstate);
504 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
511 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
512 end_msglist (pstate);
513 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
518 { add_msglist(&$1, 0); }
526 msgarg : name ':' exp
527 { add_msglist(&$1, 1); }
528 | ':' exp /* Unnamed arg. */
529 { add_msglist(0, 1); }
530 | ',' exp /* Variable number of args. */
531 { add_msglist(0, 0); }
535 /* This is to save the value of arglist_len
536 being accumulated by an outer function call. */
537 { start_arglist (); }
538 arglist ')' %prec ARROW
539 { write_exp_elt_opcode (pstate, OP_FUNCALL);
540 write_exp_elt_longcst (pstate,
541 (LONGEST) end_arglist ());
542 write_exp_elt_opcode (pstate, OP_FUNCALL); }
545 /* This is here to disambiguate with the production for
546 "func()::static_var" further below, which uses
547 function_method_void. */
548 exp : exp '(' ')' %prec ARROW
550 write_exp_elt_opcode (pstate, OP_FUNCALL);
551 write_exp_elt_longcst (pstate,
552 (LONGEST) end_arglist ());
553 write_exp_elt_opcode (pstate, OP_FUNCALL); }
557 exp : UNKNOWN_CPP_NAME '('
559 /* This could potentially be a an argument defined
560 lookup function (Koenig). */
561 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
562 write_exp_elt_block (pstate,
563 expression_context_block);
564 write_exp_elt_sym (pstate,
565 NULL); /* Placeholder. */
566 write_exp_string (pstate, $1.stoken);
567 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
569 /* This is to save the value of arglist_len
570 being accumulated by an outer function call. */
574 arglist ')' %prec ARROW
576 write_exp_elt_opcode (pstate, OP_FUNCALL);
577 write_exp_elt_longcst (pstate,
578 (LONGEST) end_arglist ());
579 write_exp_elt_opcode (pstate, OP_FUNCALL);
584 { start_arglist (); }
594 arglist : arglist ',' exp %prec ABOVE_COMMA
598 function_method: exp '(' parameter_typelist ')' const_or_volatile
600 std::vector<struct type *> *type_list = $3;
601 LONGEST len = type_list->size ();
603 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
604 /* Save the const/volatile qualifiers as
605 recorded by the const_or_volatile
606 production's actions. */
607 write_exp_elt_longcst (pstate,
608 follow_type_instance_flags ());
609 write_exp_elt_longcst (pstate, len);
610 for (type *type_elt : *type_list)
611 write_exp_elt_type (pstate, type_elt);
612 write_exp_elt_longcst(pstate, len);
613 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
617 function_method_void: exp '(' ')' const_or_volatile
618 { write_exp_elt_opcode (pstate, TYPE_INSTANCE);
620 write_exp_elt_longcst (pstate,
621 follow_type_instance_flags ());
622 write_exp_elt_longcst (pstate, 0);
623 write_exp_elt_longcst (pstate, 0);
624 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
628 exp : function_method
631 /* Normally we must interpret "func()" as a function call, instead of
632 a type. The user needs to write func(void) to disambiguate.
633 However, in the "func()::static_var" case, there's no
635 function_method_void_or_typelist: function_method
636 | function_method_void
639 exp : function_method_void_or_typelist COLONCOLON name
641 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
642 write_exp_string (pstate, $3);
643 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
648 { $$ = end_arglist () - 1; }
650 exp : lcurly arglist rcurly %prec ARROW
651 { write_exp_elt_opcode (pstate, OP_ARRAY);
652 write_exp_elt_longcst (pstate, (LONGEST) 0);
653 write_exp_elt_longcst (pstate, (LONGEST) $3);
654 write_exp_elt_opcode (pstate, OP_ARRAY); }
657 exp : lcurly type_exp rcurly exp %prec UNARY
658 { write_exp_elt_opcode (pstate, UNOP_MEMVAL_TYPE); }
661 exp : '(' type_exp ')' exp %prec UNARY
662 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
669 /* Binary operators in order of decreasing precedence. */
672 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
676 { write_exp_elt_opcode (pstate, BINOP_MUL); }
680 { write_exp_elt_opcode (pstate, BINOP_DIV); }
684 { write_exp_elt_opcode (pstate, BINOP_REM); }
688 { write_exp_elt_opcode (pstate, BINOP_ADD); }
692 { write_exp_elt_opcode (pstate, BINOP_SUB); }
696 { write_exp_elt_opcode (pstate, BINOP_LSH); }
700 { write_exp_elt_opcode (pstate, BINOP_RSH); }
704 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
707 exp : exp NOTEQUAL exp
708 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
712 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
716 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
720 { write_exp_elt_opcode (pstate, BINOP_LESS); }
724 { write_exp_elt_opcode (pstate, BINOP_GTR); }
728 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
732 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
736 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
740 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
744 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
747 exp : exp '?' exp ':' exp %prec '?'
748 { write_exp_elt_opcode (pstate, TERNOP_COND); }
752 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
755 exp : exp ASSIGN_MODIFY exp
756 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
757 write_exp_elt_opcode (pstate, $2);
758 write_exp_elt_opcode (pstate,
759 BINOP_ASSIGN_MODIFY); }
763 { write_exp_elt_opcode (pstate, OP_LONG);
764 write_exp_elt_type (pstate, $1.type);
765 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
766 write_exp_elt_opcode (pstate, OP_LONG); }
771 struct stoken_vector vec;
774 write_exp_string_vector (pstate, $1.type, &vec);
780 parse_number (pstate, $1.stoken.ptr,
781 $1.stoken.length, 0, &val);
782 write_exp_elt_opcode (pstate, OP_LONG);
783 write_exp_elt_type (pstate, val.typed_val_int.type);
784 write_exp_elt_longcst (pstate,
785 (LONGEST) val.typed_val_int.val);
786 write_exp_elt_opcode (pstate, OP_LONG);
792 { write_exp_elt_opcode (pstate, OP_FLOAT);
793 write_exp_elt_type (pstate, $1.type);
794 write_exp_elt_floatcst (pstate, $1.val);
795 write_exp_elt_opcode (pstate, OP_FLOAT); }
801 exp : DOLLAR_VARIABLE
803 write_dollar_variable (pstate, $1);
807 exp : SELECTOR '(' name ')'
809 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR);
810 write_exp_string (pstate, $3);
811 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR); }
814 exp : SIZEOF '(' type ')' %prec UNARY
815 { struct type *type = $3;
816 write_exp_elt_opcode (pstate, OP_LONG);
817 write_exp_elt_type (pstate, lookup_signed_typename
818 (parse_language (pstate),
819 parse_gdbarch (pstate),
821 type = check_typedef (type);
823 /* $5.3.3/2 of the C++ Standard (n3290 draft)
824 says of sizeof: "When applied to a reference
825 or a reference type, the result is the size of
826 the referenced type." */
827 if (TYPE_IS_REFERENCE (type))
828 type = check_typedef (TYPE_TARGET_TYPE (type));
829 write_exp_elt_longcst (pstate,
830 (LONGEST) TYPE_LENGTH (type));
831 write_exp_elt_opcode (pstate, OP_LONG); }
834 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
835 { write_exp_elt_opcode (pstate,
836 UNOP_REINTERPRET_CAST); }
839 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
840 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
843 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
844 { write_exp_elt_opcode (pstate, UNOP_DYNAMIC_CAST); }
847 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
848 { /* We could do more error checking here, but
849 it doesn't seem worthwhile. */
850 write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
856 /* We copy the string here, and not in the
857 lexer, to guarantee that we do not leak a
858 string. Note that we follow the
859 NUL-termination convention of the
861 struct typed_stoken *vec = XNEW (struct typed_stoken);
866 vec->length = $1.length;
867 vec->ptr = (char *) malloc ($1.length + 1);
868 memcpy (vec->ptr, $1.ptr, $1.length + 1);
873 /* Note that we NUL-terminate here, but just
877 $$.tokens = XRESIZEVEC (struct typed_stoken,
880 p = (char *) malloc ($2.length + 1);
881 memcpy (p, $2.ptr, $2.length + 1);
883 $$.tokens[$$.len - 1].type = $2.type;
884 $$.tokens[$$.len - 1].length = $2.length;
885 $$.tokens[$$.len - 1].ptr = p;
892 c_string_type type = C_STRING;
894 for (i = 0; i < $1.len; ++i)
896 switch ($1.tokens[i].type)
904 && type != $1.tokens[i].type)
905 error (_("Undefined string concatenation."));
906 type = (enum c_string_type_values) $1.tokens[i].type;
910 internal_error (__FILE__, __LINE__,
911 "unrecognized type in string concatenation");
915 write_exp_string_vector (pstate, type, &$1);
916 for (i = 0; i < $1.len; ++i)
917 free ($1.tokens[i].ptr);
922 exp : NSSTRING /* ObjC NextStep NSString constant
923 * of the form '@' '"' string '"'.
925 { write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING);
926 write_exp_string (pstate, $1);
927 write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING); }
932 { write_exp_elt_opcode (pstate, OP_LONG);
933 write_exp_elt_type (pstate,
934 parse_type (pstate)->builtin_bool);
935 write_exp_elt_longcst (pstate, (LONGEST) 1);
936 write_exp_elt_opcode (pstate, OP_LONG); }
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) 0);
944 write_exp_elt_opcode (pstate, OP_LONG); }
952 $$ = SYMBOL_BLOCK_VALUE ($1.sym.symbol);
954 error (_("No file or function \"%s\"."),
955 copy_name ($1.stoken));
963 block : block COLONCOLON name
965 = lookup_symbol (copy_name ($3), $1,
966 VAR_DOMAIN, NULL).symbol;
968 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
969 error (_("No function \"%s\" in specified context."),
971 $$ = SYMBOL_BLOCK_VALUE (tem); }
974 variable: name_not_typename ENTRY
975 { struct symbol *sym = $1.sym.symbol;
977 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
978 || !symbol_read_needs_frame (sym))
979 error (_("@entry can be used only for function "
980 "parameters, not for \"%s\""),
981 copy_name ($1.stoken));
983 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
984 write_exp_elt_sym (pstate, sym);
985 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
989 variable: block COLONCOLON name
990 { struct block_symbol sym
991 = lookup_symbol (copy_name ($3), $1,
995 error (_("No symbol \"%s\" in specified context."),
997 if (symbol_read_needs_frame (sym.symbol))
999 innermost_block.update (sym);
1001 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1002 write_exp_elt_block (pstate, sym.block);
1003 write_exp_elt_sym (pstate, sym.symbol);
1004 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
1007 qualified_name: TYPENAME COLONCOLON name
1009 struct type *type = $1.type;
1010 type = check_typedef (type);
1011 if (!type_aggregate_p (type))
1012 error (_("`%s' is not defined as an aggregate type."),
1013 TYPE_SAFE_NAME (type));
1015 write_exp_elt_opcode (pstate, OP_SCOPE);
1016 write_exp_elt_type (pstate, type);
1017 write_exp_string (pstate, $3);
1018 write_exp_elt_opcode (pstate, OP_SCOPE);
1020 | TYPENAME COLONCOLON '~' name
1022 struct type *type = $1.type;
1023 struct stoken tmp_token;
1026 type = check_typedef (type);
1027 if (!type_aggregate_p (type))
1028 error (_("`%s' is not defined as an aggregate type."),
1029 TYPE_SAFE_NAME (type));
1030 buf = (char *) alloca ($4.length + 2);
1031 tmp_token.ptr = buf;
1032 tmp_token.length = $4.length + 1;
1034 memcpy (buf+1, $4.ptr, $4.length);
1035 buf[tmp_token.length] = 0;
1037 /* Check for valid destructor name. */
1038 destructor_name_p (tmp_token.ptr, $1.type);
1039 write_exp_elt_opcode (pstate, OP_SCOPE);
1040 write_exp_elt_type (pstate, type);
1041 write_exp_string (pstate, tmp_token);
1042 write_exp_elt_opcode (pstate, OP_SCOPE);
1044 | TYPENAME COLONCOLON name COLONCOLON name
1046 char *copy = copy_name ($3);
1047 error (_("No type \"%s\" within class "
1048 "or namespace \"%s\"."),
1049 copy, TYPE_SAFE_NAME ($1.type));
1053 variable: qualified_name
1054 | COLONCOLON name_not_typename
1056 char *name = copy_name ($2.stoken);
1058 struct bound_minimal_symbol msymbol;
1061 = lookup_symbol (name, (const struct block *) NULL,
1062 VAR_DOMAIN, NULL).symbol;
1065 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1066 write_exp_elt_block (pstate, NULL);
1067 write_exp_elt_sym (pstate, sym);
1068 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1072 msymbol = lookup_bound_minimal_symbol (name);
1073 if (msymbol.minsym != NULL)
1074 write_exp_msymbol (pstate, msymbol);
1075 else if (!have_full_symbols () && !have_partial_symbols ())
1076 error (_("No symbol table is loaded. Use the \"file\" command."));
1078 error (_("No symbol \"%s\" in current context."), name);
1082 variable: name_not_typename
1083 { struct block_symbol sym = $1.sym;
1087 if (symbol_read_needs_frame (sym.symbol))
1088 innermost_block.update (sym);
1090 /* If we found a function, see if it's
1091 an ifunc resolver that has the same
1092 address as the ifunc symbol itself.
1093 If so, prefer the ifunc symbol. */
1095 bound_minimal_symbol resolver
1096 = find_gnu_ifunc (sym.symbol);
1097 if (resolver.minsym != NULL)
1098 write_exp_msymbol (pstate, resolver);
1101 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1102 write_exp_elt_block (pstate, sym.block);
1103 write_exp_elt_sym (pstate, sym.symbol);
1104 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1107 else if ($1.is_a_field_of_this)
1109 /* C++: it hangs off of `this'. Must
1110 not inadvertently convert from a method call
1112 innermost_block.update (sym);
1113 write_exp_elt_opcode (pstate, OP_THIS);
1114 write_exp_elt_opcode (pstate, OP_THIS);
1115 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1116 write_exp_string (pstate, $1.stoken);
1117 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1121 char *arg = copy_name ($1.stoken);
1123 bound_minimal_symbol msymbol
1124 = lookup_bound_minimal_symbol (arg);
1125 if (msymbol.minsym == NULL)
1127 if (!have_full_symbols () && !have_partial_symbols ())
1128 error (_("No symbol table is loaded. Use the \"file\" command."));
1130 error (_("No symbol \"%s\" in current context."),
1131 copy_name ($1.stoken));
1134 /* This minsym might be an alias for
1135 another function. See if we can find
1136 the debug symbol for the target, and
1137 if so, use it instead, since it has
1138 return type / prototype info. This
1139 is important for example for "p
1140 *__errno_location()". */
1141 symbol *alias_target
1142 = ((msymbol.minsym->type != mst_text_gnu_ifunc
1143 && msymbol.minsym->type != mst_data_gnu_ifunc)
1144 ? find_function_alias_target (msymbol)
1146 if (alias_target != NULL)
1148 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1150 (pstate, SYMBOL_BLOCK_VALUE (alias_target));
1151 write_exp_elt_sym (pstate, alias_target);
1152 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1155 write_exp_msymbol (pstate, msymbol);
1160 space_identifier : '@' NAME
1161 { insert_type_address_space (pstate, copy_name ($2.stoken)); }
1164 const_or_volatile: const_or_volatile_noopt
1168 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
1171 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
1172 | const_or_volatile_noopt
1175 const_or_volatile_or_space_identifier:
1176 const_or_volatile_or_space_identifier_noopt
1182 { insert_type (tp_pointer); }
1183 const_or_volatile_or_space_identifier
1185 { insert_type (tp_pointer); }
1186 const_or_volatile_or_space_identifier
1188 { insert_type (tp_reference); }
1190 { insert_type (tp_reference); }
1192 { insert_type (tp_rvalue_reference); }
1193 | ANDAND ptr_operator
1194 { insert_type (tp_rvalue_reference); }
1197 ptr_operator_ts: ptr_operator
1199 $$ = get_type_stack ();
1200 cpstate->type_stacks.emplace_back ($$);
1204 abs_decl: ptr_operator_ts direct_abs_decl
1205 { $$ = append_type_stack ($2, $1); }
1210 direct_abs_decl: '(' abs_decl ')'
1212 | direct_abs_decl array_mod
1214 push_type_stack ($1);
1216 push_type (tp_array);
1217 $$ = get_type_stack ();
1218 cpstate->type_stacks.emplace_back ($$);
1223 push_type (tp_array);
1224 $$ = get_type_stack ();
1225 cpstate->type_stacks.emplace_back ($$);
1228 | direct_abs_decl func_mod
1230 push_type_stack ($1);
1232 $$ = get_type_stack ();
1233 cpstate->type_stacks.emplace_back ($$);
1238 $$ = get_type_stack ();
1239 cpstate->type_stacks.emplace_back ($$);
1249 | OBJC_LBRAC INT ']'
1255 $$ = new std::vector<struct type *>;
1256 cpstate->type_lists.emplace_back ($$);
1258 | '(' parameter_typelist ')'
1262 /* We used to try to recognize pointer to member types here, but
1263 that didn't work (shift/reduce conflicts meant that these rules never
1264 got executed). The problem is that
1265 int (foo::bar::baz::bizzle)
1266 is a function type but
1267 int (foo::bar::baz::bizzle::*)
1268 is a pointer to member type. Stroustrup loses again! */
1273 /* Implements (approximately): (type-qualifier)* type-specifier.
1275 When type-specifier is only ever a single word, like 'float' then these
1276 arrive as pre-built TYPENAME tokens thanks to the classify_name
1277 function. However, when a type-specifier can contain multiple words,
1278 for example 'double' can appear as just 'double' or 'long double', and
1279 similarly 'long' can appear as just 'long' or in 'long double', then
1280 these type-specifiers are parsed into their own tokens in the function
1281 lex_one_token and the ident_tokens array. These separate tokens are all
1287 { $$ = lookup_signed_typename (parse_language (pstate),
1288 parse_gdbarch (pstate),
1291 { $$ = lookup_signed_typename (parse_language (pstate),
1292 parse_gdbarch (pstate),
1295 { $$ = lookup_signed_typename (parse_language (pstate),
1296 parse_gdbarch (pstate),
1299 { $$ = lookup_signed_typename (parse_language (pstate),
1300 parse_gdbarch (pstate),
1302 | LONG SIGNED_KEYWORD INT_KEYWORD
1303 { $$ = lookup_signed_typename (parse_language (pstate),
1304 parse_gdbarch (pstate),
1306 | LONG SIGNED_KEYWORD
1307 { $$ = lookup_signed_typename (parse_language (pstate),
1308 parse_gdbarch (pstate),
1310 | SIGNED_KEYWORD LONG INT_KEYWORD
1311 { $$ = lookup_signed_typename (parse_language (pstate),
1312 parse_gdbarch (pstate),
1314 | UNSIGNED LONG INT_KEYWORD
1315 { $$ = lookup_unsigned_typename (parse_language (pstate),
1316 parse_gdbarch (pstate),
1318 | LONG UNSIGNED INT_KEYWORD
1319 { $$ = lookup_unsigned_typename (parse_language (pstate),
1320 parse_gdbarch (pstate),
1323 { $$ = lookup_unsigned_typename (parse_language (pstate),
1324 parse_gdbarch (pstate),
1327 { $$ = lookup_signed_typename (parse_language (pstate),
1328 parse_gdbarch (pstate),
1330 | LONG LONG INT_KEYWORD
1331 { $$ = lookup_signed_typename (parse_language (pstate),
1332 parse_gdbarch (pstate),
1334 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1335 { $$ = lookup_signed_typename (parse_language (pstate),
1336 parse_gdbarch (pstate),
1338 | LONG LONG SIGNED_KEYWORD
1339 { $$ = lookup_signed_typename (parse_language (pstate),
1340 parse_gdbarch (pstate),
1342 | SIGNED_KEYWORD LONG LONG
1343 { $$ = lookup_signed_typename (parse_language (pstate),
1344 parse_gdbarch (pstate),
1346 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1347 { $$ = lookup_signed_typename (parse_language (pstate),
1348 parse_gdbarch (pstate),
1350 | UNSIGNED LONG LONG
1351 { $$ = lookup_unsigned_typename (parse_language (pstate),
1352 parse_gdbarch (pstate),
1354 | UNSIGNED LONG LONG INT_KEYWORD
1355 { $$ = lookup_unsigned_typename (parse_language (pstate),
1356 parse_gdbarch (pstate),
1358 | LONG LONG UNSIGNED
1359 { $$ = lookup_unsigned_typename (parse_language (pstate),
1360 parse_gdbarch (pstate),
1362 | LONG LONG UNSIGNED INT_KEYWORD
1363 { $$ = lookup_unsigned_typename (parse_language (pstate),
1364 parse_gdbarch (pstate),
1367 { $$ = lookup_signed_typename (parse_language (pstate),
1368 parse_gdbarch (pstate),
1370 | SHORT SIGNED_KEYWORD INT_KEYWORD
1371 { $$ = lookup_signed_typename (parse_language (pstate),
1372 parse_gdbarch (pstate),
1374 | SHORT SIGNED_KEYWORD
1375 { $$ = lookup_signed_typename (parse_language (pstate),
1376 parse_gdbarch (pstate),
1378 | UNSIGNED SHORT INT_KEYWORD
1379 { $$ = lookup_unsigned_typename (parse_language (pstate),
1380 parse_gdbarch (pstate),
1383 { $$ = lookup_unsigned_typename (parse_language (pstate),
1384 parse_gdbarch (pstate),
1386 | SHORT UNSIGNED INT_KEYWORD
1387 { $$ = lookup_unsigned_typename (parse_language (pstate),
1388 parse_gdbarch (pstate),
1391 { $$ = lookup_typename (parse_language (pstate),
1392 parse_gdbarch (pstate),
1394 (struct block *) NULL,
1396 | LONG DOUBLE_KEYWORD
1397 { $$ = lookup_typename (parse_language (pstate),
1398 parse_gdbarch (pstate),
1400 (struct block *) NULL,
1403 { $$ = lookup_struct (copy_name ($2),
1404 expression_context_block); }
1407 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
1410 | STRUCT name COMPLETE
1412 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
1417 { $$ = lookup_struct (copy_name ($2),
1418 expression_context_block); }
1421 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
1424 | CLASS name COMPLETE
1426 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
1431 { $$ = lookup_union (copy_name ($2),
1432 expression_context_block); }
1435 mark_completion_tag (TYPE_CODE_UNION, "", 0);
1438 | UNION name COMPLETE
1440 mark_completion_tag (TYPE_CODE_UNION, $2.ptr,
1445 { $$ = lookup_enum (copy_name ($2),
1446 expression_context_block); }
1449 mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1452 | ENUM name COMPLETE
1454 mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1458 | UNSIGNED type_name
1459 { $$ = lookup_unsigned_typename (parse_language (pstate),
1460 parse_gdbarch (pstate),
1461 TYPE_NAME($2.type)); }
1463 { $$ = lookup_unsigned_typename (parse_language (pstate),
1464 parse_gdbarch (pstate),
1466 | SIGNED_KEYWORD type_name
1467 { $$ = lookup_signed_typename (parse_language (pstate),
1468 parse_gdbarch (pstate),
1469 TYPE_NAME($2.type)); }
1471 { $$ = lookup_signed_typename (parse_language (pstate),
1472 parse_gdbarch (pstate),
1474 /* It appears that this rule for templates is never
1475 reduced; template recognition happens by lookahead
1476 in the token processing code in yylex. */
1477 | TEMPLATE name '<' type '>'
1478 { $$ = lookup_template_type(copy_name($2), $4,
1479 expression_context_block);
1481 | const_or_volatile_or_space_identifier_noopt typebase
1482 { $$ = follow_types ($2); }
1483 | typebase const_or_volatile_or_space_identifier_noopt
1484 { $$ = follow_types ($1); }
1490 $$.stoken.ptr = "int";
1491 $$.stoken.length = 3;
1492 $$.type = lookup_signed_typename (parse_language (pstate),
1493 parse_gdbarch (pstate),
1498 $$.stoken.ptr = "long";
1499 $$.stoken.length = 4;
1500 $$.type = lookup_signed_typename (parse_language (pstate),
1501 parse_gdbarch (pstate),
1506 $$.stoken.ptr = "short";
1507 $$.stoken.length = 5;
1508 $$.type = lookup_signed_typename (parse_language (pstate),
1509 parse_gdbarch (pstate),
1516 { check_parameter_typelist ($1); }
1517 | nonempty_typelist ',' DOTDOTDOT
1519 $1->push_back (NULL);
1520 check_parameter_typelist ($1);
1528 std::vector<struct type *> *typelist
1529 = new std::vector<struct type *>;
1530 cpstate->type_lists.emplace_back (typelist);
1532 typelist->push_back ($1);
1535 | nonempty_typelist ',' type
1545 push_type_stack ($2);
1546 $$ = follow_types ($1);
1550 conversion_type_id: typebase conversion_declarator
1551 { $$ = follow_types ($1); }
1554 conversion_declarator: /* Nothing. */
1555 | ptr_operator conversion_declarator
1558 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1559 | VOLATILE_KEYWORD CONST_KEYWORD
1562 const_or_volatile_noopt: const_and_volatile
1563 { insert_type (tp_const);
1564 insert_type (tp_volatile);
1567 { insert_type (tp_const); }
1569 { insert_type (tp_volatile); }
1573 { $$ = operator_stoken (" new"); }
1575 { $$ = operator_stoken (" delete"); }
1576 | OPERATOR NEW '[' ']'
1577 { $$ = operator_stoken (" new[]"); }
1578 | OPERATOR DELETE '[' ']'
1579 { $$ = operator_stoken (" delete[]"); }
1580 | OPERATOR NEW OBJC_LBRAC ']'
1581 { $$ = operator_stoken (" new[]"); }
1582 | OPERATOR DELETE OBJC_LBRAC ']'
1583 { $$ = operator_stoken (" delete[]"); }
1585 { $$ = operator_stoken ("+"); }
1587 { $$ = operator_stoken ("-"); }
1589 { $$ = operator_stoken ("*"); }
1591 { $$ = operator_stoken ("/"); }
1593 { $$ = operator_stoken ("%"); }
1595 { $$ = operator_stoken ("^"); }
1597 { $$ = operator_stoken ("&"); }
1599 { $$ = operator_stoken ("|"); }
1601 { $$ = operator_stoken ("~"); }
1603 { $$ = operator_stoken ("!"); }
1605 { $$ = operator_stoken ("="); }
1607 { $$ = operator_stoken ("<"); }
1609 { $$ = operator_stoken (">"); }
1610 | OPERATOR ASSIGN_MODIFY
1611 { const char *op = " unknown";
1635 case BINOP_BITWISE_IOR:
1638 case BINOP_BITWISE_AND:
1641 case BINOP_BITWISE_XOR:
1648 $$ = operator_stoken (op);
1651 { $$ = operator_stoken ("<<"); }
1653 { $$ = operator_stoken (">>"); }
1655 { $$ = operator_stoken ("=="); }
1657 { $$ = operator_stoken ("!="); }
1659 { $$ = operator_stoken ("<="); }
1661 { $$ = operator_stoken (">="); }
1663 { $$ = operator_stoken ("&&"); }
1665 { $$ = operator_stoken ("||"); }
1666 | OPERATOR INCREMENT
1667 { $$ = operator_stoken ("++"); }
1668 | OPERATOR DECREMENT
1669 { $$ = operator_stoken ("--"); }
1671 { $$ = operator_stoken (","); }
1672 | OPERATOR ARROW_STAR
1673 { $$ = operator_stoken ("->*"); }
1675 { $$ = operator_stoken ("->"); }
1677 { $$ = operator_stoken ("()"); }
1679 { $$ = operator_stoken ("[]"); }
1680 | OPERATOR OBJC_LBRAC ']'
1681 { $$ = operator_stoken ("[]"); }
1682 | OPERATOR conversion_type_id
1685 c_print_type ($2, NULL, &buf, -1, 0,
1686 &type_print_raw_options);
1688 /* This also needs canonicalization. */
1690 = cp_canonicalize_string (buf.c_str ());
1692 canon = std::move (buf.string ());
1693 $$ = operator_stoken ((" " + canon).c_str ());
1697 /* This rule exists in order to allow some tokens that would not normally
1698 match the 'name' rule to appear as fields within a struct. The example
1699 that initially motivated this was the RISC-V target which models the
1700 floating point registers as a union with fields called 'float' and
1701 'double'. The 'float' string becomes a TYPENAME token and can appear
1702 anywhere a 'name' can, however 'double' is its own token,
1703 DOUBLE_KEYWORD, and doesn't match the 'name' rule.*/
1706 | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
1707 | INT_KEYWORD { $$ = typename_stoken ("int"); }
1708 | LONG { $$ = typename_stoken ("long"); }
1709 | SHORT { $$ = typename_stoken ("short"); }
1710 | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
1711 | UNSIGNED { $$ = typename_stoken ("unsigned"); }
1714 name : NAME { $$ = $1.stoken; }
1715 | BLOCKNAME { $$ = $1.stoken; }
1716 | TYPENAME { $$ = $1.stoken; }
1717 | NAME_OR_INT { $$ = $1.stoken; }
1718 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1722 name_not_typename : NAME
1724 /* These would be useful if name_not_typename was useful, but it is just
1725 a fake for "variable", so these cause reduce/reduce conflicts because
1726 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1727 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1728 context where only a name could occur, this might be useful.
1733 struct field_of_this_result is_a_field_of_this;
1736 $$.sym = lookup_symbol ($1.ptr,
1737 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 (parse_gdbarch (par_state)) - 2)) == 0)
2027 = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 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 (parse_gdbarch (par_state)) - 2)) == 0)
2042 = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch (par_state)) - 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 (parse_gdbarch (par_state)))
2051 /* A long long does not fit in a LONGEST. */
2052 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
2054 shift = (gdbarch_long_long_bit (parse_gdbarch (par_state)) - 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 conditional: if there is a
2357 symbol of the same name, then the token is a symbol; otherwise,
2358 the token is a keyword. */
2362 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2368 enum exp_opcode opcode;
2372 static const struct token tokentab3[] =
2374 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2375 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2376 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2377 {"...", DOTDOTDOT, BINOP_END, 0}
2380 static const struct token tokentab2[] =
2382 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2383 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2384 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2385 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2386 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2387 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2388 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2389 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2390 {"++", INCREMENT, BINOP_END, 0},
2391 {"--", DECREMENT, BINOP_END, 0},
2392 {"->", ARROW, BINOP_END, 0},
2393 {"&&", ANDAND, BINOP_END, 0},
2394 {"||", OROR, BINOP_END, 0},
2395 /* "::" is *not* only C++: gdb overrides its meaning in several
2396 different ways, e.g., 'filename'::func, function::variable. */
2397 {"::", COLONCOLON, BINOP_END, 0},
2398 {"<<", LSH, BINOP_END, 0},
2399 {">>", RSH, BINOP_END, 0},
2400 {"==", EQUAL, BINOP_END, 0},
2401 {"!=", NOTEQUAL, BINOP_END, 0},
2402 {"<=", LEQ, BINOP_END, 0},
2403 {">=", GEQ, BINOP_END, 0},
2404 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2407 /* Identifier-like tokens. Only type-specifiers than can appear in
2408 multi-word type names (for example 'double' can appear in 'long
2409 double') need to be listed here. type-specifiers that are only ever
2410 single word (like 'float') are handled by the classify_name function. */
2411 static const struct token ident_tokens[] =
2413 {"unsigned", UNSIGNED, OP_NULL, 0},
2414 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2415 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2416 {"struct", STRUCT, OP_NULL, 0},
2417 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2418 {"sizeof", SIZEOF, OP_NULL, 0},
2419 {"_Alignof", ALIGNOF, OP_NULL, 0},
2420 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
2421 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2422 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2423 {"class", CLASS, OP_NULL, FLAG_CXX},
2424 {"union", UNION, OP_NULL, 0},
2425 {"short", SHORT, OP_NULL, 0},
2426 {"const", CONST_KEYWORD, OP_NULL, 0},
2427 {"enum", ENUM, OP_NULL, 0},
2428 {"long", LONG, OP_NULL, 0},
2429 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2430 {"int", INT_KEYWORD, OP_NULL, 0},
2431 {"new", NEW, OP_NULL, FLAG_CXX},
2432 {"delete", DELETE, OP_NULL, FLAG_CXX},
2433 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2435 {"and", ANDAND, BINOP_END, FLAG_CXX},
2436 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2437 {"bitand", '&', OP_NULL, FLAG_CXX},
2438 {"bitor", '|', OP_NULL, FLAG_CXX},
2439 {"compl", '~', OP_NULL, FLAG_CXX},
2440 {"not", '!', OP_NULL, FLAG_CXX},
2441 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2442 {"or", OROR, BINOP_END, FLAG_CXX},
2443 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2444 {"xor", '^', OP_NULL, FLAG_CXX},
2445 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2447 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2448 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2449 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2450 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2452 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2453 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2454 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2455 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2456 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2458 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2463 scan_macro_expansion (char *expansion)
2467 /* We'd better not be trying to push the stack twice. */
2468 gdb_assert (! cpstate->macro_original_text);
2470 /* Copy to the obstack, and then free the intermediate
2472 copy = (char *) obstack_copy0 (&cpstate->expansion_obstack, expansion,
2473 strlen (expansion));
2476 /* Save the old lexptr value, so we can return to it when we're done
2477 parsing the expanded text. */
2478 cpstate->macro_original_text = lexptr;
2483 scanning_macro_expansion (void)
2485 return cpstate->macro_original_text != 0;
2489 finished_macro_expansion (void)
2491 /* There'd better be something to pop back to. */
2492 gdb_assert (cpstate->macro_original_text);
2494 /* Pop back to the original text. */
2495 lexptr = cpstate->macro_original_text;
2496 cpstate->macro_original_text = 0;
2499 /* Return true iff the token represents a C++ cast operator. */
2502 is_cast_operator (const char *token, int len)
2504 return (! strncmp (token, "dynamic_cast", len)
2505 || ! strncmp (token, "static_cast", len)
2506 || ! strncmp (token, "reinterpret_cast", len)
2507 || ! strncmp (token, "const_cast", len));
2510 /* The scope used for macro expansion. */
2511 static struct macro_scope *expression_macro_scope;
2513 /* This is set if a NAME token appeared at the very end of the input
2514 string, with no whitespace separating the name from the EOF. This
2515 is used only when parsing to do field name completion. */
2516 static int saw_name_at_eof;
2518 /* This is set if the previously-returned token was a structure
2519 operator -- either '.' or ARROW. */
2520 static bool last_was_structop;
2522 /* Read one token, getting characters through lexptr. */
2525 lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
2530 const char *tokstart;
2531 bool saw_structop = last_was_structop;
2534 last_was_structop = false;
2535 *is_quoted_name = false;
2539 /* Check if this is a macro invocation that we need to expand. */
2540 if (! scanning_macro_expansion ())
2542 char *expanded = macro_expand_next (&lexptr,
2543 standard_macro_lookup,
2544 expression_macro_scope);
2547 scan_macro_expansion (expanded);
2550 prev_lexptr = lexptr;
2553 /* See if it is a special token of length 3. */
2554 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2555 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
2557 if ((tokentab3[i].flags & FLAG_CXX) != 0
2558 && parse_language (par_state)->la_language != language_cplus)
2562 yylval.opcode = tokentab3[i].opcode;
2563 return tokentab3[i].token;
2566 /* See if it is a special token of length 2. */
2567 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2568 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
2570 if ((tokentab2[i].flags & FLAG_CXX) != 0
2571 && parse_language (par_state)->la_language != language_cplus)
2575 yylval.opcode = tokentab2[i].opcode;
2576 if (tokentab2[i].token == ARROW)
2577 last_was_structop = 1;
2578 return tokentab2[i].token;
2581 switch (c = *tokstart)
2584 /* If we were just scanning the result of a macro expansion,
2585 then we need to resume scanning the original text.
2586 If we're parsing for field name completion, and the previous
2587 token allows such completion, return a COMPLETE token.
2588 Otherwise, we were already scanning the original text, and
2589 we're really done. */
2590 if (scanning_macro_expansion ())
2592 finished_macro_expansion ();
2595 else if (saw_name_at_eof)
2597 saw_name_at_eof = 0;
2600 else if (parse_completion && saw_structop)
2615 if (parse_language (par_state)->la_language == language_objc
2622 if (paren_depth == 0)
2629 if (comma_terminates
2631 && ! scanning_macro_expansion ())
2637 /* Might be a floating point number. */
2638 if (lexptr[1] < '0' || lexptr[1] > '9')
2640 last_was_structop = true;
2641 goto symbol; /* Nope, must be a symbol. */
2656 /* It's a number. */
2657 int got_dot = 0, got_e = 0, toktype;
2658 const char *p = tokstart;
2659 int hex = input_radix > 10;
2661 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2666 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2674 /* This test includes !hex because 'e' is a valid hex digit
2675 and thus does not indicate a floating point number when
2676 the radix is hex. */
2677 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2678 got_dot = got_e = 1;
2679 /* This test does not include !hex, because a '.' always indicates
2680 a decimal floating point number regardless of the radix. */
2681 else if (!got_dot && *p == '.')
2683 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2684 && (*p == '-' || *p == '+'))
2685 /* This is the sign of the exponent, not the end of the
2688 /* We will take any letters or digits. parse_number will
2689 complain if past the radix, or if L or U are not final. */
2690 else if ((*p < '0' || *p > '9')
2691 && ((*p < 'a' || *p > 'z')
2692 && (*p < 'A' || *p > 'Z')))
2695 toktype = parse_number (par_state, tokstart, p - tokstart,
2696 got_dot|got_e, &yylval);
2697 if (toktype == ERROR)
2699 char *err_copy = (char *) alloca (p - tokstart + 1);
2701 memcpy (err_copy, tokstart, p - tokstart);
2702 err_copy[p - tokstart] = 0;
2703 error (_("Invalid number \"%s\"."), err_copy);
2711 const char *p = &tokstart[1];
2713 if (parse_language (par_state)->la_language == language_objc)
2715 size_t len = strlen ("selector");
2717 if (strncmp (p, "selector", len) == 0
2718 && (p[len] == '\0' || ISSPACE (p[len])))
2727 while (ISSPACE (*p))
2729 size_t len = strlen ("entry");
2730 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
2762 if (tokstart[1] != '"' && tokstart[1] != '\'')
2771 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2776 error (_("Empty character constant."));
2777 else if (host_len > 2 && c == '\'')
2780 namelen = lexptr - tokstart - 1;
2781 *is_quoted_name = true;
2785 else if (host_len > 1)
2786 error (_("Invalid character constant."));
2792 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
2793 /* We must have come across a bad character (e.g. ';'). */
2794 error (_("Invalid character '%c' in expression."), c);
2796 /* It's a name. See how long it is. */
2798 for (c = tokstart[namelen];
2799 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
2801 /* Template parameter lists are part of the name.
2802 FIXME: This mishandles `print $a<4&&$a>3'. */
2806 if (! is_cast_operator (tokstart, namelen))
2808 /* Scan ahead to get rest of the template specification. Note
2809 that we look ahead only when the '<' adjoins non-whitespace
2810 characters; for comparison expressions, e.g. "a < b > c",
2811 there must be spaces before the '<', etc. */
2812 const char *p = find_template_name_end (tokstart + namelen);
2815 namelen = p - tokstart;
2819 c = tokstart[++namelen];
2822 /* The token "if" terminates the expression and is NOT removed from
2823 the input stream. It doesn't count if it appears in the
2824 expansion of a macro. */
2826 && tokstart[0] == 'i'
2827 && tokstart[1] == 'f'
2828 && ! scanning_macro_expansion ())
2833 /* For the same reason (breakpoint conditions), "thread N"
2834 terminates the expression. "thread" could be an identifier, but
2835 an identifier is never followed by a number without intervening
2836 punctuation. "task" is similar. Handle abbreviations of these,
2837 similarly to breakpoint.c:find_condition_and_thread. */
2839 && (strncmp (tokstart, "thread", namelen) == 0
2840 || strncmp (tokstart, "task", namelen) == 0)
2841 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2842 && ! scanning_macro_expansion ())
2844 const char *p = tokstart + namelen + 1;
2846 while (*p == ' ' || *p == '\t')
2848 if (*p >= '0' && *p <= '9')
2856 yylval.sval.ptr = tokstart;
2857 yylval.sval.length = namelen;
2859 /* Catch specific keywords. */
2860 copy = copy_name (yylval.sval);
2861 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2862 if (strcmp (copy, ident_tokens[i].oper) == 0)
2864 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2865 && parse_language (par_state)->la_language != language_cplus)
2868 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2870 struct field_of_this_result is_a_field_of_this;
2872 if (lookup_symbol (copy, expression_context_block,
2874 (parse_language (par_state)->la_language
2875 == language_cplus ? &is_a_field_of_this
2879 /* The keyword is shadowed. */
2884 /* It is ok to always set this, even though we don't always
2885 strictly need to. */
2886 yylval.opcode = ident_tokens[i].opcode;
2887 return ident_tokens[i].token;
2890 if (*tokstart == '$')
2891 return DOLLAR_VARIABLE;
2893 if (parse_completion && *lexptr == '\0')
2894 saw_name_at_eof = 1;
2896 yylval.ssym.stoken = yylval.sval;
2897 yylval.ssym.sym.symbol = NULL;
2898 yylval.ssym.sym.block = NULL;
2899 yylval.ssym.is_a_field_of_this = 0;
2903 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2904 struct token_and_value
2910 /* A FIFO of tokens that have been read but not yet returned to the
2912 static std::vector<token_and_value> token_fifo;
2914 /* Non-zero if the lexer should return tokens from the FIFO. */
2917 /* Temporary storage for c_lex; this holds symbol names as they are
2919 auto_obstack name_obstack;
2921 /* Classify a NAME token. The contents of the token are in `yylval'.
2922 Updates yylval and returns the new token type. BLOCK is the block
2923 in which lookups start; this can be NULL to mean the global scope.
2924 IS_QUOTED_NAME is non-zero if the name token was originally quoted
2925 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
2926 a structure operator -- either '.' or ARROW */
2929 classify_name (struct parser_state *par_state, const struct block *block,
2930 bool is_quoted_name, bool is_after_structop)
2932 struct block_symbol bsym;
2934 struct field_of_this_result is_a_field_of_this;
2936 copy = copy_name (yylval.sval);
2938 /* Initialize this in case we *don't* use it in this call; that way
2939 we can refer to it unconditionally below. */
2940 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
2942 bsym = lookup_symbol (copy, block, VAR_DOMAIN,
2943 parse_language (par_state)->la_name_of_this
2944 ? &is_a_field_of_this : NULL);
2946 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK)
2948 yylval.ssym.sym = bsym;
2949 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2952 else if (!bsym.symbol)
2954 /* If we found a field of 'this', we might have erroneously
2955 found a constructor where we wanted a type name. Handle this
2956 case by noticing that we found a constructor and then look up
2957 the type tag instead. */
2958 if (is_a_field_of_this.type != NULL
2959 && is_a_field_of_this.fn_field != NULL
2960 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
2963 struct field_of_this_result inner_is_a_field_of_this;
2965 bsym = lookup_symbol (copy, block, STRUCT_DOMAIN,
2966 &inner_is_a_field_of_this);
2967 if (bsym.symbol != NULL)
2969 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
2974 /* If we found a field on the "this" object, or we are looking
2975 up a field on a struct, then we want to prefer it over a
2976 filename. However, if the name was quoted, then it is better
2977 to check for a filename or a block, since this is the only
2978 way the user has of requiring the extension to be used. */
2979 if ((is_a_field_of_this.type == NULL && !is_after_structop)
2982 /* See if it's a file name. */
2983 struct symtab *symtab;
2985 symtab = lookup_symtab (copy);
2988 yylval.bval = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab),
2995 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF)
2997 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3001 /* See if it's an ObjC classname. */
3002 if (parse_language (par_state)->la_language == language_objc && !bsym.symbol)
3004 CORE_ADDR Class = lookup_objc_class (parse_gdbarch (par_state), copy);
3009 yylval.theclass.theclass = Class;
3010 sym = lookup_struct_typedef (copy, expression_context_block, 1);
3012 yylval.theclass.type = SYMBOL_TYPE (sym);
3017 /* Input names that aren't symbols but ARE valid hex numbers, when
3018 the input radix permits them, can be names or numbers depending
3019 on the parse. Note we support radixes > 16 here. */
3021 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3022 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3024 YYSTYPE newlval; /* Its value is ignored. */
3025 int hextype = parse_number (par_state, copy, yylval.sval.length,
3030 yylval.ssym.sym = bsym;
3031 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3036 /* Any other kind of symbol */
3037 yylval.ssym.sym = bsym;
3038 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3040 if (bsym.symbol == NULL
3041 && parse_language (par_state)->la_language == language_cplus
3042 && is_a_field_of_this.type == NULL
3043 && lookup_minimal_symbol (copy, NULL, NULL).minsym == NULL)
3044 return UNKNOWN_CPP_NAME;
3049 /* Like classify_name, but used by the inner loop of the lexer, when a
3050 name might have already been seen. CONTEXT is the context type, or
3051 NULL if this is the first component of a name. */
3054 classify_inner_name (struct parser_state *par_state,
3055 const struct block *block, struct type *context)
3060 if (context == NULL)
3061 return classify_name (par_state, block, false, false);
3063 type = check_typedef (context);
3064 if (!type_aggregate_p (type))
3067 copy = copy_name (yylval.ssym.stoken);
3068 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3069 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy, block, VAR_DOMAIN);
3071 /* If no symbol was found, search for a matching base class named
3072 COPY. This will allow users to enter qualified names of class members
3073 relative to the `this' pointer. */
3074 if (yylval.ssym.sym.symbol == NULL)
3076 struct type *base_type = cp_find_type_baseclass_by_name (type, copy);
3078 if (base_type != NULL)
3080 yylval.tsym.type = base_type;
3087 switch (SYMBOL_CLASS (yylval.ssym.sym.symbol))
3091 /* cp_lookup_nested_symbol might have accidentally found a constructor
3092 named COPY when we really wanted a base class of the same name.
3093 Double-check this case by looking for a base class. */
3095 struct type *base_type = cp_find_type_baseclass_by_name (type, copy);
3097 if (base_type != NULL)
3099 yylval.tsym.type = base_type;
3106 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
3112 internal_error (__FILE__, __LINE__, _("not reached"));
3115 /* The outer level of a two-level lexer. This calls the inner lexer
3116 to return tokens. It then either returns these tokens, or
3117 aggregates them into a larger token. This lets us work around a
3118 problem in our parsing approach, where the parser could not
3119 distinguish between qualified names and qualified types at the
3122 This approach is still not ideal, because it mishandles template
3123 types. See the comment in lex_one_token for an example. However,
3124 this is still an improvement over the earlier approach, and will
3125 suffice until we move to better parsing technology. */
3130 token_and_value current;
3131 int first_was_coloncolon, last_was_coloncolon;
3132 struct type *context_type = NULL;
3133 int last_to_examine, next_to_examine, checkpoint;
3134 const struct block *search_block;
3135 bool is_quoted_name, last_lex_was_structop;
3137 if (popping && !token_fifo.empty ())
3141 last_lex_was_structop = last_was_structop;
3143 /* Read the first token and decide what to do. Most of the
3144 subsequent code is C++-only; but also depends on seeing a "::" or
3146 current.token = lex_one_token (pstate, &is_quoted_name);
3147 if (current.token == NAME)
3148 current.token = classify_name (pstate, expression_context_block,
3149 is_quoted_name, last_lex_was_structop);
3150 if (parse_language (pstate)->la_language != language_cplus
3151 || (current.token != TYPENAME && current.token != COLONCOLON
3152 && current.token != FILENAME))
3153 return current.token;
3155 /* Read any sequence of alternating "::" and name-like tokens into
3157 current.value = yylval;
3158 token_fifo.push_back (current);
3159 last_was_coloncolon = current.token == COLONCOLON;
3164 /* We ignore quoted names other than the very first one.
3165 Subsequent ones do not have any special meaning. */
3166 current.token = lex_one_token (pstate, &ignore);
3167 current.value = yylval;
3168 token_fifo.push_back (current);
3170 if ((last_was_coloncolon && current.token != NAME)
3171 || (!last_was_coloncolon && current.token != COLONCOLON))
3173 last_was_coloncolon = !last_was_coloncolon;
3177 /* We always read one extra token, so compute the number of tokens
3178 to examine accordingly. */
3179 last_to_examine = token_fifo.size () - 2;
3180 next_to_examine = 0;
3182 current = token_fifo[next_to_examine];
3185 name_obstack.clear ();
3187 if (current.token == FILENAME)
3188 search_block = current.value.bval;
3189 else if (current.token == COLONCOLON)
3190 search_block = NULL;
3193 gdb_assert (current.token == TYPENAME);
3194 search_block = expression_context_block;
3195 obstack_grow (&name_obstack, current.value.sval.ptr,
3196 current.value.sval.length);
3197 context_type = current.value.tsym.type;
3201 first_was_coloncolon = current.token == COLONCOLON;
3202 last_was_coloncolon = first_was_coloncolon;
3204 while (next_to_examine <= last_to_examine)
3206 token_and_value next;
3208 next = token_fifo[next_to_examine];
3211 if (next.token == NAME && last_was_coloncolon)
3215 yylval = next.value;
3216 classification = classify_inner_name (pstate, search_block,
3218 /* We keep going until we either run out of names, or until
3219 we have a qualified name which is not a type. */
3220 if (classification != TYPENAME && classification != NAME)
3223 /* Accept up to this token. */
3224 checkpoint = next_to_examine;
3226 /* Update the partial name we are constructing. */
3227 if (context_type != NULL)
3229 /* We don't want to put a leading "::" into the name. */
3230 obstack_grow_str (&name_obstack, "::");
3232 obstack_grow (&name_obstack, next.value.sval.ptr,
3233 next.value.sval.length);
3235 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
3236 yylval.sval.length = obstack_object_size (&name_obstack);
3237 current.value = yylval;
3238 current.token = classification;
3240 last_was_coloncolon = 0;
3242 if (classification == NAME)
3245 context_type = yylval.tsym.type;
3247 else if (next.token == COLONCOLON && !last_was_coloncolon)
3248 last_was_coloncolon = 1;
3251 /* We've reached the end of the name. */
3256 /* If we have a replacement token, install it as the first token in
3257 the FIFO, and delete the other constituent tokens. */
3260 current.value.sval.ptr
3261 = (const char *) obstack_copy0 (&cpstate->expansion_obstack,
3262 current.value.sval.ptr,
3263 current.value.sval.length);
3265 token_fifo[0] = current;
3267 token_fifo.erase (token_fifo.begin () + 1,
3268 token_fifo.begin () + checkpoint);
3272 current = token_fifo[0];
3273 token_fifo.erase (token_fifo.begin ());
3274 yylval = current.value;
3275 return current.token;
3279 c_parse (struct parser_state *par_state)
3281 /* Setting up the parser state. */
3282 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3283 gdb_assert (par_state != NULL);
3286 c_parse_state cstate;
3287 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3289 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
3291 if (expression_context_block)
3292 macro_scope = sal_macro_scope (find_pc_line (expression_context_pc, 0));
3294 macro_scope = default_macro_scope ();
3296 macro_scope = user_macro_scope ();
3298 scoped_restore restore_macro_scope
3299 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
3301 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3304 /* Initialize some state used by the lexer. */
3305 last_was_structop = false;
3306 saw_name_at_eof = 0;
3308 token_fifo.clear ();
3310 name_obstack.clear ();
3317 /* This is called via the YYPRINT macro when parser debugging is
3318 enabled. It prints a token's value. */
3321 c_print_token (FILE *file, int type, YYSTYPE value)
3326 parser_fprintf (file, "typed_val_int<%s, %s>",
3327 TYPE_SAFE_NAME (value.typed_val_int.type),
3328 pulongest (value.typed_val_int.val));
3334 char *copy = (char *) alloca (value.tsval.length + 1);
3336 memcpy (copy, value.tsval.ptr, value.tsval.length);
3337 copy[value.tsval.length] = '\0';
3339 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3344 case DOLLAR_VARIABLE:
3345 parser_fprintf (file, "sval<%s>", copy_name (value.sval));
3349 parser_fprintf (file, "tsym<type=%s, name=%s>",
3350 TYPE_SAFE_NAME (value.tsym.type),
3351 copy_name (value.tsym.stoken));
3355 case UNKNOWN_CPP_NAME:
3358 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3359 copy_name (value.ssym.stoken),
3360 (value.ssym.sym.symbol == NULL
3361 ? "(null)" : SYMBOL_PRINT_NAME (value.ssym.sym.symbol)),
3362 value.ssym.is_a_field_of_this);
3366 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3374 yyerror (const char *msg)
3377 lexptr = prev_lexptr;
3379 error (_("A %s in expression, near `%s'."), msg, lexptr);