1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986-2024 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. */
39 #include "expression.h"
41 #include "parser-defs.h"
44 #include "c-support.h"
47 #include "cp-support.h"
48 #include "macroscope.h"
49 #include "objc-lang.h"
50 #include "typeprint.h"
52 #include "type-stack.h"
53 #include "target-float.h"
56 #define parse_type(ps) builtin_type (ps->gdbarch ())
58 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
60 #define GDB_YY_REMAP_PREFIX c_
63 /* The state of the parser, used internally when we are parsing the
66 static struct parser_state
*pstate
= NULL
;
68 /* Data that must be held for the duration of a parse. */
72 /* These are used to hold type lists and type stacks that are
73 allocated during the parse. */
74 std
::vector
<std
::unique_ptr
<std
::vector
<struct type
*>>> type_lists
;
75 std
::vector
<std
::unique_ptr
<struct type_stack
>> type_stacks
;
77 /* Storage for some strings allocated during the parse. */
78 std
::vector
<gdb
::unique_xmalloc_ptr
<char>> strings
;
80 /* When we find that lexptr (the global var defined in parse.c) is
81 pointing at a macro invocation, we expand the invocation, and call
82 scan_macro_expansion to save the old lexptr here and point lexptr
83 into the expanded text. When we reach the end of that, we call
84 end_macro_expansion to pop back to the value we saved here. The
85 macro expansion code promises to return only fully-expanded text,
86 so we don't need to "push" more than one level.
88 This is disgusting, of course. It would be cleaner to do all macro
89 expansion beforehand, and then hand that to lexptr. But we don't
90 really know where the expression ends. Remember, in a command like
92 (gdb) break *ADDRESS if CONDITION
94 we evaluate ADDRESS in the scope of the current frame, but we
95 evaluate CONDITION in the scope of the breakpoint's location. So
96 it's simply wrong to try to macro-expand the whole thing at once. */
97 const char *macro_original_text
= nullptr
;
99 /* We save all intermediate macro expansions on this obstack for the
100 duration of a single parse. The expansion text may sometimes have
101 to live past the end of the expansion, due to yacc lookahead.
102 Rather than try to be clever about saving the data for a single
103 token, we simply keep it all and delete it after parsing has
105 auto_obstack expansion_obstack
;
107 /* The type stack. */
108 struct type_stack type_stack
;
111 /* This is set and cleared in c_parse. */
113 static struct c_parse_state
*cpstate
;
117 static int yylex (void);
119 static void yyerror (const char *);
121 static int type_aggregate_p
(struct type
*);
123 using namespace expr
;
126 /* Although the yacc "value" of an expression is not used,
127 since the result is stored in the structure being created,
128 other node types do have values. */
143 struct typed_stoken tsval
;
145 struct symtoken ssym
;
147 const struct block
*bval
;
148 enum exp_opcode opcode
;
150 struct stoken_vector svec
;
151 std
::vector
<struct type
*> *tvec
;
153 struct type_stack
*type_stack
;
155 struct objc_class_str theclass
;
159 /* YYSTYPE gets defined by %union */
160 static int parse_number
(struct parser_state
*par_state
,
161 const char *, int, int, YYSTYPE *);
162 static struct stoken operator_stoken
(const char *);
163 static struct stoken typename_stoken
(const char *);
164 static void check_parameter_typelist
(std
::vector
<struct type
*> *);
166 #if defined(YYBISON) && YYBISON < 30800
167 static void c_print_token
(FILE *file
, int type
, YYSTYPE value
);
168 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
172 %type
<voidval
> exp exp1 type_exp start variable qualified_name lcurly function_method
174 %type
<tval
> type typebase scalar_type
175 %type
<tvec
> nonempty_typelist func_mod parameter_typelist
176 /* %type <bval> block */
178 /* Fancy type parsing. */
180 %type
<lval
> array_mod
181 %type
<tval
> conversion_type_id
183 %type
<type_stack
> ptr_operator_ts abs_decl direct_abs_decl
185 %token
<typed_val_int
> INT COMPLEX_INT
186 %token
<typed_val_float
> FLOAT COMPLEX_FLOAT
188 /* Both NAME and TYPENAME tokens represent symbols in the input,
189 and both convey their data as strings.
190 But a TYPENAME is a string that happens to be defined as a typedef
191 or builtin type name (such as int or char)
192 and a NAME is any other symbol.
193 Contexts where this distinction is not important can use the
194 nonterminal "name", which matches either NAME or TYPENAME. */
196 %token
<tsval
> STRING
197 %token
<sval
> NSSTRING
/* ObjC Foundation "NSString" literal */
198 %token SELECTOR
/* ObjC "@selector" pseudo-operator */
200 %token
<ssym
> NAME
/* BLOCKNAME defined below to give it higher precedence. */
201 %token
<ssym
> UNKNOWN_CPP_NAME
202 %token
<voidval
> COMPLETE
203 %token
<tsym
> TYPENAME
204 %token
<theclass
> CLASSNAME
/* ObjC Class name */
205 %type
<sval
> name field_name
206 %type
<svec
> string_exp
207 %type
<ssym
> name_not_typename
208 %type
<tsym
> type_name
210 /* This is like a '[' token, but is only generated when parsing
211 Objective C. This lets us reuse the same parser without
212 erroneously parsing ObjC-specific expressions in C. */
215 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
216 but which would parse as a valid number in the current input radix.
217 E.g. "c" when input_radix==16. Depending on the parse, it will be
218 turned into a name or into a number. */
220 %token
<ssym
> NAME_OR_INT
223 %token STRUCT CLASS UNION ENUM SIZEOF ALIGNOF UNSIGNED COLONCOLON
228 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
234 /* Special type cases, put in to allow the parser to distinguish different
236 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
237 %token RESTRICT ATOMIC
238 %token FLOAT_KEYWORD COMPLEX
240 %token
<sval
> DOLLAR_VARIABLE
242 %token
<opcode
> ASSIGN_MODIFY
251 %right
'=' ASSIGN_MODIFY
259 %left
'<' '>' LEQ GEQ
264 %right UNARY INCREMENT DECREMENT
265 %right ARROW ARROW_STAR
'.' DOT_STAR
'[' OBJC_LBRAC
'('
266 %token
<ssym
> BLOCKNAME
267 %token
<bval
> FILENAME
282 pstate
->push_new
<type_operation
> ($1);
286 pstate
->wrap
<typeof_operation
> ();
288 | TYPEOF
'(' type
')'
290 pstate
->push_new
<type_operation
> ($3);
292 | DECLTYPE
'(' exp
')'
294 pstate
->wrap
<decltype_operation
> ();
298 /* Expressions, including the comma operator. */
301 { pstate
->wrap2
<comma_operation
> (); }
304 /* Expressions, not including the comma operator. */
305 exp
: '*' exp %prec UNARY
306 { pstate
->wrap
<unop_ind_operation
> (); }
309 exp
: '&' exp %prec UNARY
310 { pstate
->wrap
<unop_addr_operation
> (); }
313 exp
: '-' exp %prec UNARY
314 { pstate
->wrap
<unary_neg_operation
> (); }
317 exp
: '+' exp %prec UNARY
318 { pstate
->wrap
<unary_plus_operation
> (); }
321 exp
: '!' exp %prec UNARY
323 if
(pstate
->language
()->la_language
325 pstate
->wrap
<opencl_not_operation
> ();
327 pstate
->wrap
<unary_logical_not_operation
> ();
331 exp
: '~' exp %prec UNARY
332 { pstate
->wrap
<unary_complement_operation
> (); }
335 exp
: INCREMENT exp %prec UNARY
336 { pstate
->wrap
<preinc_operation
> (); }
339 exp
: DECREMENT exp %prec UNARY
340 { pstate
->wrap
<predec_operation
> (); }
343 exp
: exp INCREMENT %prec UNARY
344 { pstate
->wrap
<postinc_operation
> (); }
347 exp
: exp DECREMENT %prec UNARY
348 { pstate
->wrap
<postdec_operation
> (); }
351 exp
: TYPEID
'(' exp
')' %prec UNARY
352 { pstate
->wrap
<typeid_operation
> (); }
355 exp
: TYPEID
'(' type_exp
')' %prec UNARY
356 { pstate
->wrap
<typeid_operation
> (); }
359 exp
: SIZEOF exp %prec UNARY
360 { pstate
->wrap
<unop_sizeof_operation
> (); }
363 exp
: ALIGNOF
'(' type_exp
')' %prec UNARY
364 { pstate
->wrap
<unop_alignof_operation
> (); }
367 exp
: exp ARROW field_name
369 pstate
->push_new
<structop_ptr_operation
>
370 (pstate
->pop
(), copy_name
($3));
374 exp
: exp ARROW field_name COMPLETE
376 structop_base_operation
*op
377 = new structop_ptr_operation
(pstate
->pop
(),
379 pstate
->mark_struct_expression
(op
);
380 pstate
->push
(operation_up
(op
));
384 exp
: exp ARROW COMPLETE
386 structop_base_operation
*op
387 = new structop_ptr_operation
(pstate
->pop
(), "");
388 pstate
->mark_struct_expression
(op
);
389 pstate
->push
(operation_up
(op
));
393 exp
: exp ARROW
'~' name
395 pstate
->push_new
<structop_ptr_operation
>
396 (pstate
->pop
(), "~" + copy_name
($4));
400 exp
: exp ARROW
'~' name COMPLETE
402 structop_base_operation
*op
403 = new structop_ptr_operation
(pstate
->pop
(),
404 "~" + copy_name
($4));
405 pstate
->mark_struct_expression
(op
);
406 pstate
->push
(operation_up
(op
));
410 exp
: exp ARROW qualified_name
411 { /* exp->type::name becomes exp->*(&type::name) */
412 /* Note: this doesn't work if name is a
413 static member! FIXME */
414 pstate
->wrap
<unop_addr_operation
> ();
415 pstate
->wrap2
<structop_mptr_operation
> (); }
418 exp
: exp ARROW_STAR exp
419 { pstate
->wrap2
<structop_mptr_operation
> (); }
422 exp
: exp
'.' field_name
424 if
(pstate
->language
()->la_language
426 pstate
->push_new
<opencl_structop_operation
>
427 (pstate
->pop
(), copy_name
($3));
429 pstate
->push_new
<structop_operation
>
430 (pstate
->pop
(), copy_name
($3));
434 exp
: exp
'.' field_name COMPLETE
436 structop_base_operation
*op
437 = new structop_operation
(pstate
->pop
(),
439 pstate
->mark_struct_expression
(op
);
440 pstate
->push
(operation_up
(op
));
444 exp
: exp
'.' COMPLETE
446 structop_base_operation
*op
447 = new structop_operation
(pstate
->pop
(), "");
448 pstate
->mark_struct_expression
(op
);
449 pstate
->push
(operation_up
(op
));
453 exp
: exp
'.' '~' name
455 pstate
->push_new
<structop_operation
>
456 (pstate
->pop
(), "~" + copy_name
($4));
460 exp
: exp
'.' '~' name COMPLETE
462 structop_base_operation
*op
463 = new structop_operation
(pstate
->pop
(),
464 "~" + copy_name
($4));
465 pstate
->mark_struct_expression
(op
);
466 pstate
->push
(operation_up
(op
));
470 exp
: exp
'.' qualified_name
471 { /* exp.type::name becomes exp.*(&type::name) */
472 /* Note: this doesn't work if name is a
473 static member! FIXME */
474 pstate
->wrap
<unop_addr_operation
> ();
475 pstate
->wrap2
<structop_member_operation
> (); }
478 exp
: exp DOT_STAR exp
479 { pstate
->wrap2
<structop_member_operation
> (); }
482 exp
: exp
'[' exp1
']'
483 { pstate
->wrap2
<subscript_operation
> (); }
486 exp
: exp OBJC_LBRAC exp1
']'
487 { pstate
->wrap2
<subscript_operation
> (); }
491 * The rules below parse ObjC message calls of the form:
492 * '[' target selector {':' argument}* ']'
495 exp
: OBJC_LBRAC TYPENAME
499 std
::string copy
= copy_name
($2.stoken
);
500 theclass
= lookup_objc_class
(pstate
->gdbarch
(),
503 error (_
("%s is not an ObjC Class"),
505 pstate
->push_new
<long_const_operation
>
506 (parse_type
(pstate
)->builtin_int
,
511 { end_msglist
(pstate
); }
514 exp
: OBJC_LBRAC CLASSNAME
516 pstate
->push_new
<long_const_operation
>
517 (parse_type
(pstate
)->builtin_int
,
518 (LONGEST
) $2.theclass
);
522 { end_msglist
(pstate
); }
528 { end_msglist
(pstate
); }
532 { add_msglist
(&$1, 0); }
540 msgarg
: name
':' exp
541 { add_msglist
(&$1, 1); }
542 |
':' exp
/* Unnamed arg. */
543 { add_msglist
(0, 1); }
544 |
',' exp
/* Variable number of args. */
545 { add_msglist
(0, 0); }
549 /* This is to save the value of arglist_len
550 being accumulated by an outer function call. */
551 { pstate
->start_arglist
(); }
552 arglist
')' %prec ARROW
554 std
::vector
<operation_up
> args
555 = pstate
->pop_vector
(pstate
->end_arglist
());
556 pstate
->push_new
<funcall_operation
>
557 (pstate
->pop
(), std
::move
(args
));
561 /* This is here to disambiguate with the production for
562 "func()::static_var" further below, which uses
563 function_method_void. */
564 exp
: exp
'(' ')' %prec ARROW
566 pstate
->push_new
<funcall_operation
>
567 (pstate
->pop
(), std
::vector
<operation_up
> ());
572 exp
: UNKNOWN_CPP_NAME
'('
574 /* This could potentially be a an argument defined
575 lookup function (Koenig). */
576 /* This is to save the value of arglist_len
577 being accumulated by an outer function call. */
578 pstate
->start_arglist
();
580 arglist
')' %prec ARROW
582 std
::vector
<operation_up
> args
583 = pstate
->pop_vector
(pstate
->end_arglist
());
584 pstate
->push_new
<adl_func_operation
>
585 (copy_name
($1.stoken
),
586 pstate
->expression_context_block
,
592 { pstate
->start_arglist
(); }
599 { pstate
->arglist_len
= 1; }
602 arglist
: arglist
',' exp %prec ABOVE_COMMA
603 { pstate
->arglist_len
++; }
606 function_method: exp
'(' parameter_typelist
')' const_or_volatile
608 std
::vector
<struct type
*> *type_list
= $3;
609 /* Save the const/volatile qualifiers as
610 recorded by the const_or_volatile
611 production's actions. */
612 type_instance_flags flags
613 = (cpstate
->type_stack
614 .follow_type_instance_flags
());
615 pstate
->push_new
<type_instance_operation
>
616 (flags
, std
::move
(*type_list
),
621 function_method_void: exp
'(' ')' const_or_volatile
623 type_instance_flags flags
624 = (cpstate
->type_stack
625 .follow_type_instance_flags
());
626 pstate
->push_new
<type_instance_operation
>
627 (flags
, std
::vector
<type
*> (), pstate
->pop
());
631 exp
: function_method
634 /* Normally we must interpret "func()" as a function call, instead of
635 a type. The user needs to write func(void) to disambiguate.
636 However, in the "func()::static_var" case, there's no
638 function_method_void_or_typelist: function_method
639 | function_method_void
642 exp
: function_method_void_or_typelist COLONCOLON name
644 pstate
->push_new
<func_static_var_operation
>
645 (pstate
->pop
(), copy_name
($3));
650 { $$
= pstate
->end_arglist
() - 1; }
652 exp
: lcurly arglist rcurly %prec ARROW
654 std
::vector
<operation_up
> args
655 = pstate
->pop_vector
($3 + 1);
656 pstate
->push_new
<array_operation
> (0, $3,
661 exp
: lcurly type_exp rcurly exp %prec UNARY
662 { pstate
->wrap2
<unop_memval_type_operation
> (); }
665 exp
: '(' type_exp
')' exp %prec UNARY
667 if
(pstate
->language
()->la_language
669 pstate
->wrap2
<opencl_cast_type_operation
> ();
671 pstate
->wrap2
<unop_cast_type_operation
> ();
679 /* Binary operators in order of decreasing precedence. */
682 { pstate
->wrap2
<repeat_operation
> (); }
686 { pstate
->wrap2
<mul_operation
> (); }
690 { pstate
->wrap2
<div_operation
> (); }
694 { pstate
->wrap2
<rem_operation
> (); }
698 { pstate
->wrap2
<add_operation
> (); }
702 { pstate
->wrap2
<sub_operation
> (); }
706 { pstate
->wrap2
<lsh_operation
> (); }
710 { pstate
->wrap2
<rsh_operation
> (); }
715 if
(pstate
->language
()->la_language
717 pstate
->wrap2
<opencl_equal_operation
> ();
719 pstate
->wrap2
<equal_operation
> ();
723 exp
: exp NOTEQUAL exp
725 if
(pstate
->language
()->la_language
727 pstate
->wrap2
<opencl_notequal_operation
> ();
729 pstate
->wrap2
<notequal_operation
> ();
735 if
(pstate
->language
()->la_language
737 pstate
->wrap2
<opencl_leq_operation
> ();
739 pstate
->wrap2
<leq_operation
> ();
745 if
(pstate
->language
()->la_language
747 pstate
->wrap2
<opencl_geq_operation
> ();
749 pstate
->wrap2
<geq_operation
> ();
755 if
(pstate
->language
()->la_language
757 pstate
->wrap2
<opencl_less_operation
> ();
759 pstate
->wrap2
<less_operation
> ();
765 if
(pstate
->language
()->la_language
767 pstate
->wrap2
<opencl_gtr_operation
> ();
769 pstate
->wrap2
<gtr_operation
> ();
774 { pstate
->wrap2
<bitwise_and_operation
> (); }
778 { pstate
->wrap2
<bitwise_xor_operation
> (); }
782 { pstate
->wrap2
<bitwise_ior_operation
> (); }
787 if
(pstate
->language
()->la_language
790 operation_up rhs
= pstate
->pop
();
791 operation_up lhs
= pstate
->pop
();
792 pstate
->push_new
<opencl_logical_binop_operation
>
793 (BINOP_LOGICAL_AND
, std
::move
(lhs
),
797 pstate
->wrap2
<logical_and_operation
> ();
803 if
(pstate
->language
()->la_language
806 operation_up rhs
= pstate
->pop
();
807 operation_up lhs
= pstate
->pop
();
808 pstate
->push_new
<opencl_logical_binop_operation
>
809 (BINOP_LOGICAL_OR
, std
::move
(lhs
),
813 pstate
->wrap2
<logical_or_operation
> ();
817 exp
: exp
'?' exp
':' exp %prec
'?'
819 operation_up last
= pstate
->pop
();
820 operation_up mid
= pstate
->pop
();
821 operation_up first
= pstate
->pop
();
822 if
(pstate
->language
()->la_language
824 pstate
->push_new
<opencl_ternop_cond_operation
>
825 (std
::move
(first
), std
::move
(mid
),
828 pstate
->push_new
<ternop_cond_operation
>
829 (std
::move
(first
), std
::move
(mid
),
836 if
(pstate
->language
()->la_language
838 pstate
->wrap2
<opencl_assign_operation
> ();
840 pstate
->wrap2
<assign_operation
> ();
844 exp
: exp ASSIGN_MODIFY exp
846 operation_up rhs
= pstate
->pop
();
847 operation_up lhs
= pstate
->pop
();
848 pstate
->push_new
<assign_modify_operation
>
849 ($2, std
::move
(lhs
), std
::move
(rhs
));
855 pstate
->push_new
<long_const_operation
>
863 = (make_operation
<long_const_operation
>
864 ($1.type
->target_type
(), 0));
866 = (make_operation
<long_const_operation
>
867 ($1.type
->target_type
(), $1.val
));
868 pstate
->push_new
<complex_operation
>
869 (std
::move
(real
), std
::move
(imag
), $1.type
);
875 struct stoken_vector vec
;
878 pstate
->push_c_string
($1.type
, &vec
);
884 parse_number
(pstate
, $1.stoken.ptr
,
885 $1.stoken.length
, 0, &val
);
886 pstate
->push_new
<long_const_operation
>
887 (val.typed_val_int.type
,
888 val.typed_val_int.val
);
896 std
::copy
(std
::begin
($1.val
), std
::end
($1.val
),
898 pstate
->push_new
<float_const_operation
> ($1.type
, data
);
904 struct type
*underlying
= $1.type
->target_type
();
907 target_float_from_host_double
(val.data
(),
910 = (make_operation
<float_const_operation
>
913 std
::copy
(std
::begin
($1.val
), std
::end
($1.val
),
916 = (make_operation
<float_const_operation
>
919 pstate
->push_new
<complex_operation
>
920 (std
::move
(real
), std
::move
(imag
),
928 exp
: DOLLAR_VARIABLE
930 pstate
->push_dollar
($1);
934 exp
: SELECTOR
'(' name
')'
936 pstate
->push_new
<objc_selector_operation
>
941 exp
: SIZEOF
'(' type
')' %prec UNARY
942 { struct type
*type
= $3;
943 struct type
*int_type
944 = lookup_signed_typename
(pstate
->language
(),
946 type
= check_typedef
(type
);
948 /* $5.3.3/2 of the C++ Standard (n3290 draft)
949 says of sizeof: "When applied to a reference
950 or a reference type, the result is the size of
951 the referenced type." */
952 if
(TYPE_IS_REFERENCE
(type
))
953 type
= check_typedef
(type
->target_type
());
955 pstate
->push_new
<long_const_operation
>
956 (int_type
, type
->length
());
960 exp
: REINTERPRET_CAST
'<' type_exp
'>' '(' exp
')' %prec UNARY
961 { pstate
->wrap2
<reinterpret_cast_operation
> (); }
964 exp
: STATIC_CAST
'<' type_exp
'>' '(' exp
')' %prec UNARY
965 { pstate
->wrap2
<unop_cast_type_operation
> (); }
968 exp
: DYNAMIC_CAST
'<' type_exp
'>' '(' exp
')' %prec UNARY
969 { pstate
->wrap2
<dynamic_cast_operation
> (); }
972 exp
: CONST_CAST
'<' type_exp
'>' '(' exp
')' %prec UNARY
973 { /* We could do more error checking here, but
974 it doesn't seem worthwhile. */
975 pstate
->wrap2
<unop_cast_type_operation
> (); }
981 /* We copy the string here, and not in the
982 lexer, to guarantee that we do not leak a
983 string. Note that we follow the
984 NUL-termination convention of the
986 struct typed_stoken
*vec
= XNEW
(struct typed_stoken
);
991 vec
->length
= $1.length
;
992 vec
->ptr
= (char *) malloc
($1.length
+ 1);
993 memcpy
(vec
->ptr
, $1.ptr
, $1.length
+ 1);
998 /* Note that we NUL-terminate here, but just
1002 $$.tokens
= XRESIZEVEC
(struct typed_stoken
,
1005 p
= (char *) malloc
($2.length
+ 1);
1006 memcpy
(p
, $2.ptr
, $2.length
+ 1);
1008 $$.tokens
[$$.len
- 1].type
= $2.type
;
1009 $$.tokens
[$$.len
- 1].length
= $2.length
;
1010 $$.tokens
[$$.len
- 1].ptr
= p
;
1017 c_string_type type
= C_STRING
;
1019 for
(i
= 0; i
< $1.len
; ++i
)
1021 switch
($1.tokens
[i
].type
)
1028 if
(type
!= C_STRING
1029 && type
!= $1.tokens
[i
].type
)
1030 error (_
("Undefined string concatenation."));
1031 type
= (enum c_string_type_values
) $1.tokens
[i
].type
;
1034 /* internal error */
1035 internal_error
("unrecognized type in string concatenation");
1039 pstate
->push_c_string
(type
, &$1);
1040 for
(i
= 0; i
< $1.len
; ++i
)
1041 free
($1.tokens
[i
].ptr
);
1046 exp
: NSSTRING
/* ObjC NextStep NSString constant
1047 * of the form '@' '"' string '"'.
1050 pstate
->push_new
<objc_nsstring_operation
>
1057 { pstate
->push_new
<long_const_operation
>
1058 (parse_type
(pstate
)->builtin_bool
, 1);
1063 { pstate
->push_new
<long_const_operation
>
1064 (parse_type
(pstate
)->builtin_bool
, 0);
1073 $$
= $1.sym.symbol
->value_block
();
1075 error (_
("No file or function \"%s\"."),
1076 copy_name
($1.stoken
).c_str
());
1084 block
: block COLONCOLON name
1086 std
::string copy
= copy_name
($3);
1088 = lookup_symbol
(copy.c_str
(), $1,
1089 SEARCH_FUNCTION_DOMAIN
,
1093 error (_
("No function \"%s\" in specified context."),
1095 $$
= tem
->value_block
(); }
1098 variable: name_not_typename ENTRY
1099 { struct symbol
*sym
= $1.sym.symbol
;
1101 if
(sym
== NULL ||
!sym
->is_argument
()
1102 ||
!symbol_read_needs_frame
(sym
))
1103 error (_
("@entry can be used only for function "
1104 "parameters, not for \"%s\""),
1105 copy_name
($1.stoken
).c_str
());
1107 pstate
->push_new
<var_entry_value_operation
> (sym
);
1111 variable: block COLONCOLON name
1113 std
::string copy
= copy_name
($3);
1114 struct block_symbol sym
1115 = lookup_symbol
(copy.c_str
(), $1,
1118 if
(sym.symbol
== 0)
1119 error (_
("No symbol \"%s\" in specified context."),
1121 if
(symbol_read_needs_frame
(sym.symbol
))
1122 pstate
->block_tracker
->update
(sym
);
1124 pstate
->push_new
<var_value_operation
> (sym
);
1128 qualified_name: TYPENAME COLONCOLON name
1130 struct type
*type
= $1.type
;
1131 type
= check_typedef
(type
);
1132 if
(!type_aggregate_p
(type
))
1133 error (_
("`%s' is not defined as an aggregate type."),
1134 TYPE_SAFE_NAME
(type
));
1136 pstate
->push_new
<scope_operation
> (type
,
1139 | TYPENAME COLONCOLON
'~' name
1141 struct type
*type
= $1.type
;
1143 type
= check_typedef
(type
);
1144 if
(!type_aggregate_p
(type
))
1145 error (_
("`%s' is not defined as an aggregate type."),
1146 TYPE_SAFE_NAME
(type
));
1147 std
::string name
= "~" + std
::string ($4.ptr
,
1150 /* Check for valid destructor name. */
1151 destructor_name_p
(name.c_str
(), $1.type
);
1152 pstate
->push_new
<scope_operation
> (type
,
1155 | TYPENAME COLONCOLON name COLONCOLON name
1157 std
::string copy
= copy_name
($3);
1158 error (_
("No type \"%s\" within class "
1159 "or namespace \"%s\"."),
1160 copy.c_str
(), TYPE_SAFE_NAME
($1.type
));
1164 variable: qualified_name
1165 | COLONCOLON name_not_typename
1167 std
::string name
= copy_name
($2.stoken
);
1168 struct block_symbol sym
1169 = lookup_symbol
(name.c_str
(),
1170 (const struct block
*) NULL
,
1172 pstate
->push_symbol
(name.c_str
(), sym
);
1176 variable: name_not_typename
1177 { struct block_symbol sym
= $1.sym
;
1181 if
(symbol_read_needs_frame
(sym.symbol
))
1182 pstate
->block_tracker
->update
(sym
);
1184 /* If we found a function, see if it's
1185 an ifunc resolver that has the same
1186 address as the ifunc symbol itself.
1187 If so, prefer the ifunc symbol. */
1189 bound_minimal_symbol resolver
1190 = find_gnu_ifunc
(sym.symbol
);
1191 if
(resolver.minsym
!= NULL
)
1192 pstate
->push_new
<var_msym_value_operation
>
1195 pstate
->push_new
<var_value_operation
> (sym
);
1197 else if
($1.is_a_field_of_this
)
1199 /* C++: it hangs off of `this'. Must
1200 not inadvertently convert from a method call
1202 pstate
->block_tracker
->update
(sym
);
1204 = make_operation
<op_this_operation
> ();
1205 pstate
->push_new
<structop_ptr_operation
>
1206 (std
::move
(thisop
), copy_name
($1.stoken
));
1210 std
::string arg
= copy_name
($1.stoken
);
1212 bound_minimal_symbol msymbol
1213 = lookup_minimal_symbol
(current_program_space
, arg.c_str
());
1214 if
(msymbol.minsym
== NULL
)
1216 if
(!have_full_symbols
(current_program_space
)
1217 && !have_partial_symbols
(current_program_space
))
1218 error (_
("No symbol table is loaded. Use the \"file\" command."));
1220 error (_
("No symbol \"%s\" in current context."),
1224 /* This minsym might be an alias for
1225 another function. See if we can find
1226 the debug symbol for the target, and
1227 if so, use it instead, since it has
1228 return type / prototype info. This
1229 is important for example for "p
1230 *__errno_location()". */
1231 symbol
*alias_target
1232 = ((msymbol.minsym
->type
() != mst_text_gnu_ifunc
1233 && msymbol.minsym
->type
() != mst_data_gnu_ifunc
)
1234 ? find_function_alias_target
(msymbol
)
1236 if
(alias_target
!= NULL
)
1238 block_symbol bsym
{ alias_target
,
1239 alias_target
->value_block
() };
1240 pstate
->push_new
<var_value_operation
> (bsym
);
1243 pstate
->push_new
<var_msym_value_operation
>
1249 const_or_volatile: const_or_volatile_noopt
1255 { cpstate
->type_stack.insert
(tp_const
); }
1257 { cpstate
->type_stack.insert
(tp_volatile
); }
1259 { cpstate
->type_stack.insert
(tp_atomic
); }
1261 { cpstate
->type_stack.insert
(tp_restrict
); }
1264 cpstate
->type_stack.insert
(pstate
,
1265 copy_name
($2.stoken
).c_str
());
1267 |
'@' UNKNOWN_CPP_NAME
1269 cpstate
->type_stack.insert
(pstate
,
1270 copy_name
($2.stoken
).c_str
());
1274 qualifier_seq_noopt:
1276 | qualifier_seq_noopt single_qualifier
1286 { cpstate
->type_stack.insert
(tp_pointer
); }
1289 { cpstate
->type_stack.insert
(tp_pointer
); }
1292 { cpstate
->type_stack.insert
(tp_reference
); }
1294 { cpstate
->type_stack.insert
(tp_reference
); }
1296 { cpstate
->type_stack.insert
(tp_rvalue_reference
); }
1297 | ANDAND ptr_operator
1298 { cpstate
->type_stack.insert
(tp_rvalue_reference
); }
1301 ptr_operator_ts: ptr_operator
1303 $$
= cpstate
->type_stack.create
();
1304 cpstate
->type_stacks.emplace_back
($$
);
1308 abs_decl: ptr_operator_ts direct_abs_decl
1309 { $$
= $2->append
($1); }
1314 direct_abs_decl: '(' abs_decl
')'
1316 | direct_abs_decl array_mod
1318 cpstate
->type_stack.push
($1);
1319 cpstate
->type_stack.push
($2);
1320 cpstate
->type_stack.push
(tp_array
);
1321 $$
= cpstate
->type_stack.create
();
1322 cpstate
->type_stacks.emplace_back
($$
);
1326 cpstate
->type_stack.push
($1);
1327 cpstate
->type_stack.push
(tp_array
);
1328 $$
= cpstate
->type_stack.create
();
1329 cpstate
->type_stacks.emplace_back
($$
);
1332 | direct_abs_decl func_mod
1334 cpstate
->type_stack.push
($1);
1335 cpstate
->type_stack.push
($2);
1336 $$
= cpstate
->type_stack.create
();
1337 cpstate
->type_stacks.emplace_back
($$
);
1341 cpstate
->type_stack.push
($1);
1342 $$
= cpstate
->type_stack.create
();
1343 cpstate
->type_stacks.emplace_back
($$
);
1353 | OBJC_LBRAC INT
']'
1359 $$
= new std
::vector
<struct type
*>;
1360 cpstate
->type_lists.emplace_back
($$
);
1362 |
'(' parameter_typelist
')'
1366 /* We used to try to recognize pointer to member types here, but
1367 that didn't work (shift/reduce conflicts meant that these rules never
1368 got executed). The problem is that
1369 int (foo::bar::baz::bizzle)
1370 is a function type but
1371 int (foo::bar::baz::bizzle::*)
1372 is a pointer to member type. Stroustrup loses again! */
1377 /* A helper production that recognizes scalar types that can validly
1378 be used with _Complex. */
1382 { $$
= lookup_signed_typename
(pstate
->language
(),
1385 { $$
= lookup_signed_typename
(pstate
->language
(),
1388 { $$
= lookup_signed_typename
(pstate
->language
(),
1391 { $$
= lookup_signed_typename
(pstate
->language
(),
1393 | LONG SIGNED_KEYWORD INT_KEYWORD
1394 { $$
= lookup_signed_typename
(pstate
->language
(),
1396 | LONG SIGNED_KEYWORD
1397 { $$
= lookup_signed_typename
(pstate
->language
(),
1399 | SIGNED_KEYWORD LONG INT_KEYWORD
1400 { $$
= lookup_signed_typename
(pstate
->language
(),
1402 | UNSIGNED LONG INT_KEYWORD
1403 { $$
= lookup_unsigned_typename
(pstate
->language
(),
1405 | LONG UNSIGNED INT_KEYWORD
1406 { $$
= lookup_unsigned_typename
(pstate
->language
(),
1409 { $$
= lookup_unsigned_typename
(pstate
->language
(),
1412 { $$
= lookup_signed_typename
(pstate
->language
(),
1414 | LONG LONG INT_KEYWORD
1415 { $$
= lookup_signed_typename
(pstate
->language
(),
1417 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1418 { $$
= lookup_signed_typename
(pstate
->language
(),
1420 | LONG LONG SIGNED_KEYWORD
1421 { $$
= lookup_signed_typename
(pstate
->language
(),
1423 | SIGNED_KEYWORD LONG LONG
1424 { $$
= lookup_signed_typename
(pstate
->language
(),
1426 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1427 { $$
= lookup_signed_typename
(pstate
->language
(),
1429 | UNSIGNED LONG LONG
1430 { $$
= lookup_unsigned_typename
(pstate
->language
(),
1432 | UNSIGNED LONG LONG INT_KEYWORD
1433 { $$
= lookup_unsigned_typename
(pstate
->language
(),
1435 | LONG LONG UNSIGNED
1436 { $$
= lookup_unsigned_typename
(pstate
->language
(),
1438 | LONG LONG UNSIGNED INT_KEYWORD
1439 { $$
= lookup_unsigned_typename
(pstate
->language
(),
1442 { $$
= lookup_signed_typename
(pstate
->language
(),
1444 | SHORT SIGNED_KEYWORD INT_KEYWORD
1445 { $$
= lookup_signed_typename
(pstate
->language
(),
1447 | SHORT SIGNED_KEYWORD
1448 { $$
= lookup_signed_typename
(pstate
->language
(),
1450 | UNSIGNED SHORT INT_KEYWORD
1451 { $$
= lookup_unsigned_typename
(pstate
->language
(),
1454 { $$
= lookup_unsigned_typename
(pstate
->language
(),
1456 | SHORT UNSIGNED INT_KEYWORD
1457 { $$
= lookup_unsigned_typename
(pstate
->language
(),
1460 { $$
= lookup_typename
(pstate
->language
(),
1465 { $$
= lookup_typename
(pstate
->language
(),
1469 | LONG DOUBLE_KEYWORD
1470 { $$
= lookup_typename
(pstate
->language
(),
1474 | UNSIGNED type_name
1475 { $$
= lookup_unsigned_typename
(pstate
->language
(),
1476 $2.type
->name
()); }
1478 { $$
= lookup_unsigned_typename
(pstate
->language
(),
1480 | SIGNED_KEYWORD type_name
1481 { $$
= lookup_signed_typename
(pstate
->language
(),
1482 $2.type
->name
()); }
1484 { $$
= lookup_signed_typename
(pstate
->language
(),
1488 /* Implements (approximately): (type-qualifier)* type-specifier.
1490 When type-specifier is only ever a single word, like 'float' then these
1491 arrive as pre-built TYPENAME tokens thanks to the classify_name
1492 function. However, when a type-specifier can contain multiple words,
1493 for example 'double' can appear as just 'double' or 'long double', and
1494 similarly 'long' can appear as just 'long' or in 'long double', then
1495 these type-specifiers are parsed into their own tokens in the function
1496 lex_one_token and the ident_tokens array. These separate tokens are all
1503 | COMPLEX scalar_type
1505 $$
= init_complex_type
(nullptr
, $2);
1509 = lookup_struct
(copy_name
($2).c_str
(),
1510 pstate
->expression_context_block
);
1514 pstate
->mark_completion_tag
(TYPE_CODE_STRUCT
,
1518 | STRUCT name COMPLETE
1520 pstate
->mark_completion_tag
(TYPE_CODE_STRUCT
,
1525 { $$
= lookup_struct
1526 (copy_name
($2).c_str
(),
1527 pstate
->expression_context_block
);
1531 pstate
->mark_completion_tag
(TYPE_CODE_STRUCT
,
1535 | CLASS name COMPLETE
1537 pstate
->mark_completion_tag
(TYPE_CODE_STRUCT
,
1543 = lookup_union
(copy_name
($2).c_str
(),
1544 pstate
->expression_context_block
);
1548 pstate
->mark_completion_tag
(TYPE_CODE_UNION
,
1552 | UNION name COMPLETE
1554 pstate
->mark_completion_tag
(TYPE_CODE_UNION
,
1559 { $$
= lookup_enum
(copy_name
($2).c_str
(),
1560 pstate
->expression_context_block
);
1564 pstate
->mark_completion_tag
(TYPE_CODE_ENUM
, "", 0);
1567 | ENUM name COMPLETE
1569 pstate
->mark_completion_tag
(TYPE_CODE_ENUM
, $2.ptr
,
1573 /* It appears that this rule for templates is never
1574 reduced; template recognition happens by lookahead
1575 in the token processing code in yylex. */
1576 | TEMPLATE name
'<' type
'>'
1577 { $$
= lookup_template_type
1578 (copy_name
($2).c_str
(), $4,
1579 pstate
->expression_context_block
);
1581 | qualifier_seq_noopt typebase
1582 { $$
= cpstate
->type_stack.follow_types
($2); }
1583 | typebase qualifier_seq_noopt
1584 { $$
= cpstate
->type_stack.follow_types
($1); }
1590 $$.stoken.ptr
= "int";
1591 $$.stoken.length
= 3;
1592 $$.type
= lookup_signed_typename
(pstate
->language
(),
1597 $$.stoken.ptr
= "long";
1598 $$.stoken.length
= 4;
1599 $$.type
= lookup_signed_typename
(pstate
->language
(),
1604 $$.stoken.ptr
= "short";
1605 $$.stoken.length
= 5;
1606 $$.type
= lookup_signed_typename
(pstate
->language
(),
1613 { check_parameter_typelist
($1); }
1614 | nonempty_typelist
',' DOTDOTDOT
1616 $1->push_back
(NULL
);
1617 check_parameter_typelist
($1);
1625 std
::vector
<struct type
*> *typelist
1626 = new std
::vector
<struct type
*>;
1627 cpstate
->type_lists.emplace_back
(typelist
);
1629 typelist
->push_back
($1);
1632 | nonempty_typelist
',' type
1642 cpstate
->type_stack.push
($2);
1643 $$
= cpstate
->type_stack.follow_types
($1);
1647 conversion_type_id: typebase conversion_declarator
1648 { $$
= cpstate
->type_stack.follow_types
($1); }
1651 conversion_declarator: /* Nothing. */
1652 | ptr_operator conversion_declarator
1655 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1656 | VOLATILE_KEYWORD CONST_KEYWORD
1659 const_or_volatile_noopt: const_and_volatile
1660 { cpstate
->type_stack.insert
(tp_const
);
1661 cpstate
->type_stack.insert
(tp_volatile
);
1664 { cpstate
->type_stack.insert
(tp_const
); }
1666 { cpstate
->type_stack.insert
(tp_volatile
); }
1670 { $$
= operator_stoken
(" new"); }
1672 { $$
= operator_stoken
(" delete"); }
1673 | OPERATOR NEW
'[' ']'
1674 { $$
= operator_stoken
(" new[]"); }
1675 | OPERATOR DELETE
'[' ']'
1676 { $$
= operator_stoken
(" delete[]"); }
1677 | OPERATOR NEW OBJC_LBRAC
']'
1678 { $$
= operator_stoken
(" new[]"); }
1679 | OPERATOR DELETE OBJC_LBRAC
']'
1680 { $$
= operator_stoken
(" delete[]"); }
1682 { $$
= operator_stoken
("+"); }
1684 { $$
= operator_stoken
("-"); }
1686 { $$
= operator_stoken
("*"); }
1688 { $$
= operator_stoken
("/"); }
1690 { $$
= operator_stoken
("%"); }
1692 { $$
= operator_stoken
("^"); }
1694 { $$
= operator_stoken
("&"); }
1696 { $$
= operator_stoken
("|"); }
1698 { $$
= operator_stoken
("~"); }
1700 { $$
= operator_stoken
("!"); }
1702 { $$
= operator_stoken
("="); }
1704 { $$
= operator_stoken
("<"); }
1706 { $$
= operator_stoken
(">"); }
1707 | OPERATOR ASSIGN_MODIFY
1708 { const char *op
= " unknown";
1732 case BINOP_BITWISE_IOR
:
1735 case BINOP_BITWISE_AND
:
1738 case BINOP_BITWISE_XOR
:
1745 $$
= operator_stoken
(op
);
1748 { $$
= operator_stoken
("<<"); }
1750 { $$
= operator_stoken
(">>"); }
1752 { $$
= operator_stoken
("=="); }
1754 { $$
= operator_stoken
("!="); }
1756 { $$
= operator_stoken
("<="); }
1758 { $$
= operator_stoken
(">="); }
1760 { $$
= operator_stoken
("&&"); }
1762 { $$
= operator_stoken
("||"); }
1763 | OPERATOR INCREMENT
1764 { $$
= operator_stoken
("++"); }
1765 | OPERATOR DECREMENT
1766 { $$
= operator_stoken
("--"); }
1768 { $$
= operator_stoken
(","); }
1769 | OPERATOR ARROW_STAR
1770 { $$
= operator_stoken
("->*"); }
1772 { $$
= operator_stoken
("->"); }
1774 { $$
= operator_stoken
("()"); }
1776 { $$
= operator_stoken
("[]"); }
1777 | OPERATOR OBJC_LBRAC
']'
1778 { $$
= operator_stoken
("[]"); }
1779 | OPERATOR conversion_type_id
1782 c_print_type
($2, NULL
, &buf
, -1, 0,
1783 pstate
->language
()->la_language
,
1784 &type_print_raw_options
);
1785 std
::string name
= buf.release
();
1787 /* This also needs canonicalization. */
1788 gdb
::unique_xmalloc_ptr
<char> canon
1789 = cp_canonicalize_string
(name.c_str
());
1790 if
(canon
!= nullptr
)
1791 name
= canon.get
();
1792 $$
= operator_stoken
((" " + name
).c_str
());
1796 /* This rule exists in order to allow some tokens that would not normally
1797 match the 'name' rule to appear as fields within a struct. The example
1798 that initially motivated this was the RISC-V target which models the
1799 floating point registers as a union with fields called 'float' and
1803 | DOUBLE_KEYWORD
{ $$
= typename_stoken
("double"); }
1804 | FLOAT_KEYWORD
{ $$
= typename_stoken
("float"); }
1805 | INT_KEYWORD
{ $$
= typename_stoken
("int"); }
1806 | LONG
{ $$
= typename_stoken
("long"); }
1807 | SHORT
{ $$
= typename_stoken
("short"); }
1808 | SIGNED_KEYWORD
{ $$
= typename_stoken
("signed"); }
1809 | UNSIGNED
{ $$
= typename_stoken
("unsigned"); }
1812 name
: NAME
{ $$
= $1.stoken
; }
1813 | BLOCKNAME
{ $$
= $1.stoken
; }
1814 | TYPENAME
{ $$
= $1.stoken
; }
1815 | NAME_OR_INT
{ $$
= $1.stoken
; }
1816 | UNKNOWN_CPP_NAME
{ $$
= $1.stoken
; }
1820 name_not_typename
: NAME
1822 /* These would be useful if name_not_typename was useful, but it is just
1823 a fake for "variable", so these cause reduce/reduce conflicts because
1824 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1825 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1826 context where only a name could occur, this might be useful.
1831 struct field_of_this_result is_a_field_of_this
;
1835 = lookup_symbol
($1.ptr
,
1836 pstate
->expression_context_block
,
1838 &is_a_field_of_this
);
1839 $$.is_a_field_of_this
1840 = is_a_field_of_this.type
!= NULL
;
1847 /* Returns a stoken of the operator name given by OP (which does not
1848 include the string "operator"). */
1850 static struct stoken
1851 operator_stoken
(const char *op
)
1853 struct stoken st
= { NULL
, 0 };
1856 st.length
= CP_OPERATOR_LEN
+ strlen
(op
);
1857 buf
= (char *) malloc
(st.length
+ 1);
1858 strcpy
(buf
, CP_OPERATOR_STR
);
1862 /* The toplevel (c_parse) will free the memory allocated here. */
1863 cpstate
->strings.emplace_back
(buf
);
1867 /* Returns a stoken of the type named TYPE. */
1869 static struct stoken
1870 typename_stoken
(const char *type
)
1872 struct stoken st
= { type
, 0 };
1873 st.length
= strlen
(type
);
1877 /* Return true if the type is aggregate-like. */
1880 type_aggregate_p
(struct type
*type
)
1882 return
(type
->code
() == TYPE_CODE_STRUCT
1883 || type
->code
() == TYPE_CODE_UNION
1884 || type
->code
() == TYPE_CODE_NAMESPACE
1885 ||
(type
->code
() == TYPE_CODE_ENUM
1886 && type
->is_declared_class
()));
1889 /* Validate a parameter typelist. */
1892 check_parameter_typelist
(std
::vector
<struct type
*> *params
)
1897 for
(ix
= 0; ix
< params
->size
(); ++ix
)
1899 type
= (*params
)[ix
];
1900 if
(type
!= NULL
&& check_typedef
(type
)->code
() == TYPE_CODE_VOID
)
1904 if
(params
->size
() == 1)
1909 error (_
("parameter types following 'void'"));
1912 error (_
("'void' invalid as parameter type"));
1917 /* Take care of parsing a number (anything that starts with a digit).
1918 Set yylval and return the token type; update lexptr.
1919 LEN is the number of characters in it. */
1921 /*** Needs some error checking for the float case ***/
1924 parse_number
(struct parser_state
*par_state
,
1925 const char *buf
, int len
, int parsed_float
, YYSTYPE *putithere
)
1932 int base
= input_radix
;
1935 /* Number of "L" suffixes encountered. */
1938 /* Imaginary number. */
1939 bool imaginary_p
= false
;
1941 /* We have found a "L" or "U" (or "i") suffix. */
1942 int found_suffix
= 0;
1946 if
(len
>= 1 && buf
[len
- 1] == 'i')
1952 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
1953 if
(len
>= 2 && buf
[len
- 2] == 'd' && buf
[len
- 1] == 'f')
1955 putithere
->typed_val_float.type
1956 = parse_type
(par_state
)->builtin_decfloat
;
1959 else if
(len
>= 2 && buf
[len
- 2] == 'd' && buf
[len
- 1] == 'd')
1961 putithere
->typed_val_float.type
1962 = parse_type
(par_state
)->builtin_decdouble
;
1965 else if
(len
>= 2 && buf
[len
- 2] == 'd' && buf
[len
- 1] == 'l')
1967 putithere
->typed_val_float.type
1968 = parse_type
(par_state
)->builtin_declong
;
1971 /* Handle suffixes: 'f' for float, 'l' for long double. */
1972 else if
(len
>= 1 && TOLOWER
(buf
[len
- 1]) == 'f')
1974 putithere
->typed_val_float.type
1975 = parse_type
(par_state
)->builtin_float
;
1978 else if
(len
>= 1 && TOLOWER
(buf
[len
- 1]) == 'l')
1980 putithere
->typed_val_float.type
1981 = parse_type
(par_state
)->builtin_long_double
;
1984 /* Default type for floating-point literals is double. */
1987 putithere
->typed_val_float.type
1988 = parse_type
(par_state
)->builtin_double
;
1991 if
(!parse_float
(buf
, len
,
1992 putithere
->typed_val_float.type
,
1993 putithere
->typed_val_float.val
))
1997 putithere
->typed_val_float.type
1998 = init_complex_type
(nullptr
, putithere
->typed_val_float.type
);
2000 return imaginary_p ? COMPLEX_FLOAT
: FLOAT
;
2003 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
2004 if
(buf
[0] == '0' && len
> 1)
2047 if
(c
>= 'A' && c
<= 'Z')
2049 if
(c
!= 'l' && c
!= 'u' && c
!= 'i')
2051 if
(c
>= '0' && c
<= '9')
2059 if
(base
> 10 && c
>= 'a' && c
<= 'f')
2063 n
+= i
= c
- 'a' + 10;
2081 return ERROR
; /* Char not a digit */
2084 return ERROR
; /* Invalid digit in this base */
2086 if
(c
!= 'l' && c
!= 'u' && c
!= 'i')
2088 /* Test for overflow. */
2089 if
(prevn
== 0 && n
== 0)
2091 else if
(prevn
>= n
)
2092 error (_
("Numeric constant too large."));
2097 /* An integer constant is an int, a long, or a long long. An L
2098 suffix forces it to be long; an LL suffix forces it to be long
2099 long. If not forced to a larger size, it gets the first type of
2100 the above that it fits in. To figure out whether it fits, we
2101 shift it right and see whether anything remains. Note that we
2102 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
2103 operation, because many compilers will warn about such a shift
2104 (which always produces a zero result). Sometimes gdbarch_int_bit
2105 or gdbarch_long_bit will be that big, sometimes not. To deal with
2106 the case where it is we just always shift the value more than
2107 once, with fewer bits each time. */
2108 int int_bits
= gdbarch_int_bit
(par_state
->gdbarch
());
2109 int long_bits
= gdbarch_long_bit
(par_state
->gdbarch
());
2110 int long_long_bits
= gdbarch_long_long_bit
(par_state
->gdbarch
());
2112 /* No 'u' suffix. */
2115 = ((/* 'u' suffix. */
2117 ||
(/* Not a decimal. */
2119 ||
(/* Allowed as a convenience, in case decimal doesn't fit in largest
2121 !fits_in_type
(1, n
, long_long_bits
, true
)));
2123 /* No 'l' or 'll' suffix. */
2126 /* No 'll' suffix. */
2128 if
(have_int
&& have_signed
&& fits_in_type
(1, n
, int_bits
, true
))
2129 putithere
->typed_val_int.type
= parse_type
(par_state
)->builtin_int
;
2130 else if
(have_int
&& have_unsigned
&& fits_in_type
(1, n
, int_bits
, false
))
2131 putithere
->typed_val_int.type
2132 = parse_type
(par_state
)->builtin_unsigned_int
;
2133 else if
(have_long
&& have_signed
&& fits_in_type
(1, n
, long_bits
, true
))
2134 putithere
->typed_val_int.type
= parse_type
(par_state
)->builtin_long
;
2135 else if
(have_long
&& have_unsigned
&& fits_in_type
(1, n
, long_bits
, false
))
2136 putithere
->typed_val_int.type
2137 = parse_type
(par_state
)->builtin_unsigned_long
;
2138 else if
(have_signed
&& fits_in_type
(1, n
, long_long_bits
, true
))
2139 putithere
->typed_val_int.type
2140 = parse_type
(par_state
)->builtin_long_long
;
2141 else if
(have_unsigned
&& fits_in_type
(1, n
, long_long_bits
, false
))
2142 putithere
->typed_val_int.type
2143 = parse_type
(par_state
)->builtin_unsigned_long_long
;
2145 error (_
("Numeric constant too large."));
2146 putithere
->typed_val_int.val
= n
;
2149 putithere
->typed_val_int.type
2150 = init_complex_type
(nullptr
, putithere
->typed_val_int.type
);
2152 return imaginary_p ? COMPLEX_INT
: INT
;
2155 /* Temporary obstack used for holding strings. */
2156 static struct obstack tempbuf
;
2157 static int tempbuf_init
;
2159 /* Parse a C escape sequence. The initial backslash of the sequence
2160 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2161 last character of the sequence. If OUTPUT is not NULL, the
2162 translated form of the escape sequence will be written there. If
2163 OUTPUT is NULL, no output is written and the call will only affect
2164 *PTR. If an escape sequence is expressed in target bytes, then the
2165 entire sequence will simply be copied to OUTPUT. Return 1 if any
2166 character was emitted, 0 otherwise. */
2169 c_parse_escape
(const char **ptr
, struct obstack
*output
)
2171 const char *tokptr
= *ptr
;
2174 /* Some escape sequences undergo character set conversion. Those we
2178 /* Hex escapes do not undergo character set conversion, so keep
2179 the escape sequence for later. */
2182 obstack_grow_str
(output
, "\\x");
2184 if
(!ISXDIGIT
(*tokptr
))
2185 error (_
("\\x escape without a following hex digit"));
2186 while
(ISXDIGIT
(*tokptr
))
2189 obstack_1grow
(output
, *tokptr
);
2194 /* Octal escapes do not undergo character set conversion, so
2195 keep the escape sequence for later. */
2207 obstack_grow_str
(output
, "\\");
2209 i
< 3 && ISDIGIT
(*tokptr
) && *tokptr
!= '8' && *tokptr
!= '9';
2213 obstack_1grow
(output
, *tokptr
);
2219 /* We handle UCNs later. We could handle them here, but that
2220 would mean a spurious error in the case where the UCN could
2221 be converted to the target charset but not the host
2227 int i
, len
= c
== 'U' ?
8 : 4;
2230 obstack_1grow
(output
, '\\');
2231 obstack_1grow
(output
, *tokptr
);
2234 if
(!ISXDIGIT
(*tokptr
))
2235 error (_
("\\%c escape without a following hex digit"), c
);
2236 for
(i
= 0; i
< len
&& ISXDIGIT
(*tokptr
); ++i
)
2239 obstack_1grow
(output
, *tokptr
);
2245 /* We must pass backslash through so that it does not
2246 cause quoting during the second expansion. */
2249 obstack_grow_str
(output
, "\\\\");
2253 /* Escapes which undergo conversion. */
2256 obstack_1grow
(output
, '\a');
2261 obstack_1grow
(output
, '\b');
2266 obstack_1grow
(output
, '\f');
2271 obstack_1grow
(output
, '\n');
2276 obstack_1grow
(output
, '\r');
2281 obstack_1grow
(output
, '\t');
2286 obstack_1grow
(output
, '\v');
2290 /* GCC extension. */
2293 obstack_1grow
(output
, HOST_ESCAPE_CHAR
);
2297 /* Backslash-newline expands to nothing at all. */
2303 /* A few escapes just expand to the character itself. */
2307 /* GCC extensions. */
2312 /* Unrecognized escapes turn into the character itself. */
2315 obstack_1grow
(output
, *tokptr
);
2323 /* Parse a string or character literal from TOKPTR. The string or
2324 character may be wide or unicode. *OUTPTR is set to just after the
2325 end of the literal in the input string. The resulting token is
2326 stored in VALUE. This returns a token value, either STRING or
2327 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2328 number of host characters in the literal. */
2331 parse_string_or_char
(const char *tokptr
, const char **outptr
,
2332 struct typed_stoken
*value
, int *host_chars
)
2338 /* Build the gdb internal form of the input string in tempbuf. Note
2339 that the buffer is null byte terminated *only* for the
2340 convenience of debugging gdb itself and printing the buffer
2341 contents when the buffer contains no embedded nulls. Gdb does
2342 not depend upon the buffer being null byte terminated, it uses
2343 the length string instead. This allows gdb to handle C strings
2344 (as well as strings in other languages) with embedded null
2350 obstack_free
(&tempbuf
, NULL
);
2351 obstack_init
(&tempbuf
);
2353 /* Record the string type. */
2356 type
= C_WIDE_STRING
;
2359 else if
(*tokptr
== 'u')
2364 else if
(*tokptr
== 'U')
2369 else if
(*tokptr
== '@')
2371 /* An Objective C string. */
2379 /* Skip the quote. */
2393 *host_chars
+= c_parse_escape
(&tokptr
, &tempbuf
);
2395 else if
(c
== quote
)
2399 obstack_1grow
(&tempbuf
, c
);
2401 /* FIXME: this does the wrong thing with multi-byte host
2402 characters. We could use mbrlen here, but that would
2403 make "set host-charset" a bit less useful. */
2408 if
(*tokptr
!= quote
)
2411 error (_
("Unterminated string in expression."));
2413 error (_
("Unmatched single quote."));
2418 value
->ptr
= (char *) obstack_base
(&tempbuf
);
2419 value
->length
= obstack_object_size
(&tempbuf
);
2423 return quote
== '"' ?
(is_objc ? NSSTRING
: STRING
) : CHAR
;
2426 /* This is used to associate some attributes with a token. */
2430 /* If this bit is set, the token is C++-only. */
2434 /* If this bit is set, the token is C-only. */
2438 /* If this bit is set, the token is conditional: if there is a
2439 symbol of the same name, then the token is a symbol; otherwise,
2440 the token is a keyword. */
2444 DEF_ENUM_FLAGS_TYPE
(enum token_flag
, token_flags
);
2450 enum exp_opcode opcode
;
2454 static const struct c_token tokentab3
[] =
2456 {">>=", ASSIGN_MODIFY
, BINOP_RSH
, 0},
2457 {"<<=", ASSIGN_MODIFY
, BINOP_LSH
, 0},
2458 {"->*", ARROW_STAR
, OP_NULL
, FLAG_CXX
},
2459 {"...", DOTDOTDOT
, OP_NULL
, 0}
2462 static const struct c_token tokentab2
[] =
2464 {"+=", ASSIGN_MODIFY
, BINOP_ADD
, 0},
2465 {"-=", ASSIGN_MODIFY
, BINOP_SUB
, 0},
2466 {"*=", ASSIGN_MODIFY
, BINOP_MUL
, 0},
2467 {"/=", ASSIGN_MODIFY
, BINOP_DIV
, 0},
2468 {"%=", ASSIGN_MODIFY
, BINOP_REM
, 0},
2469 {"|=", ASSIGN_MODIFY
, BINOP_BITWISE_IOR
, 0},
2470 {"&=", ASSIGN_MODIFY
, BINOP_BITWISE_AND
, 0},
2471 {"^=", ASSIGN_MODIFY
, BINOP_BITWISE_XOR
, 0},
2472 {"++", INCREMENT
, OP_NULL
, 0},
2473 {"--", DECREMENT
, OP_NULL
, 0},
2474 {"->", ARROW
, OP_NULL
, 0},
2475 {"&&", ANDAND
, OP_NULL
, 0},
2476 {"||", OROR
, OP_NULL
, 0},
2477 /* "::" is *not* only C++: gdb overrides its meaning in several
2478 different ways, e.g., 'filename'::func, function::variable. */
2479 {"::", COLONCOLON
, OP_NULL
, 0},
2480 {"<<", LSH
, OP_NULL
, 0},
2481 {">>", RSH
, OP_NULL
, 0},
2482 {"==", EQUAL
, OP_NULL
, 0},
2483 {"!=", NOTEQUAL
, OP_NULL
, 0},
2484 {"<=", LEQ
, OP_NULL
, 0},
2485 {">=", GEQ
, OP_NULL
, 0},
2486 {".*", DOT_STAR
, OP_NULL
, FLAG_CXX
}
2489 /* Identifier-like tokens. Only type-specifiers than can appear in
2490 multi-word type names (for example 'double' can appear in 'long
2491 double') need to be listed here. type-specifiers that are only ever
2492 single word (like 'char') are handled by the classify_name function. */
2493 static const struct c_token ident_tokens
[] =
2495 {"unsigned", UNSIGNED
, OP_NULL
, 0},
2496 {"template", TEMPLATE
, OP_NULL
, FLAG_CXX
},
2497 {"volatile", VOLATILE_KEYWORD
, OP_NULL
, 0},
2498 {"struct", STRUCT
, OP_NULL
, 0},
2499 {"signed", SIGNED_KEYWORD
, OP_NULL
, 0},
2500 {"sizeof", SIZEOF
, OP_NULL
, 0},
2501 {"_Alignof", ALIGNOF
, OP_NULL
, 0},
2502 {"alignof", ALIGNOF
, OP_NULL
, FLAG_CXX
},
2503 {"double", DOUBLE_KEYWORD
, OP_NULL
, 0},
2504 {"float", FLOAT_KEYWORD
, OP_NULL
, 0},
2505 {"false", FALSEKEYWORD
, OP_NULL
, FLAG_CXX
},
2506 {"class", CLASS
, OP_NULL
, FLAG_CXX
},
2507 {"union", UNION
, OP_NULL
, 0},
2508 {"short", SHORT
, OP_NULL
, 0},
2509 {"const", CONST_KEYWORD
, OP_NULL
, 0},
2510 {"restrict", RESTRICT
, OP_NULL
, FLAG_C | FLAG_SHADOW
},
2511 {"__restrict__", RESTRICT
, OP_NULL
, 0},
2512 {"__restrict", RESTRICT
, OP_NULL
, 0},
2513 {"_Atomic", ATOMIC
, OP_NULL
, 0},
2514 {"enum", ENUM
, OP_NULL
, 0},
2515 {"long", LONG
, OP_NULL
, 0},
2516 {"_Complex", COMPLEX
, OP_NULL
, 0},
2517 {"__complex__", COMPLEX
, OP_NULL
, 0},
2519 {"true", TRUEKEYWORD
, OP_NULL
, FLAG_CXX
},
2520 {"int", INT_KEYWORD
, OP_NULL
, 0},
2521 {"new", NEW
, OP_NULL
, FLAG_CXX
},
2522 {"delete", DELETE
, OP_NULL
, FLAG_CXX
},
2523 {"operator", OPERATOR
, OP_NULL
, FLAG_CXX
},
2525 {"and", ANDAND
, OP_NULL
, FLAG_CXX
},
2526 {"and_eq", ASSIGN_MODIFY
, BINOP_BITWISE_AND
, FLAG_CXX
},
2527 {"bitand", '&', OP_NULL
, FLAG_CXX
},
2528 {"bitor", '|', OP_NULL
, FLAG_CXX
},
2529 {"compl", '~', OP_NULL
, FLAG_CXX
},
2530 {"not", '!', OP_NULL
, FLAG_CXX
},
2531 {"not_eq", NOTEQUAL
, OP_NULL
, FLAG_CXX
},
2532 {"or", OROR
, OP_NULL
, FLAG_CXX
},
2533 {"or_eq", ASSIGN_MODIFY
, BINOP_BITWISE_IOR
, FLAG_CXX
},
2534 {"xor", '^', OP_NULL
, FLAG_CXX
},
2535 {"xor_eq", ASSIGN_MODIFY
, BINOP_BITWISE_XOR
, FLAG_CXX
},
2537 {"const_cast", CONST_CAST
, OP_NULL
, FLAG_CXX
},
2538 {"dynamic_cast", DYNAMIC_CAST
, OP_NULL
, FLAG_CXX
},
2539 {"static_cast", STATIC_CAST
, OP_NULL
, FLAG_CXX
},
2540 {"reinterpret_cast", REINTERPRET_CAST
, OP_NULL
, FLAG_CXX
},
2542 {"__typeof__", TYPEOF
, OP_TYPEOF
, 0 },
2543 {"__typeof", TYPEOF
, OP_TYPEOF
, 0 },
2544 {"typeof", TYPEOF
, OP_TYPEOF
, FLAG_SHADOW
},
2545 {"__decltype", DECLTYPE
, OP_DECLTYPE
, FLAG_CXX
},
2546 {"decltype", DECLTYPE
, OP_DECLTYPE
, FLAG_CXX | FLAG_SHADOW
},
2548 {"typeid", TYPEID
, OP_TYPEID
, FLAG_CXX
}
2553 scan_macro_expansion
(const char *expansion
)
2555 /* We'd better not be trying to push the stack twice. */
2556 gdb_assert
(! cpstate
->macro_original_text
);
2558 /* Copy to the obstack. */
2559 const char *copy
= obstack_strdup
(&cpstate
->expansion_obstack
, expansion
);
2561 /* Save the old lexptr value, so we can return to it when we're done
2562 parsing the expanded text. */
2563 cpstate
->macro_original_text
= pstate
->lexptr
;
2564 pstate
->lexptr
= copy
;
2568 scanning_macro_expansion
(void)
2570 return cpstate
->macro_original_text
!= 0;
2574 finished_macro_expansion
(void)
2576 /* There'd better be something to pop back to. */
2577 gdb_assert
(cpstate
->macro_original_text
);
2579 /* Pop back to the original text. */
2580 pstate
->lexptr
= cpstate
->macro_original_text
;
2581 cpstate
->macro_original_text
= 0;
2584 /* Return true iff the token represents a C++ cast operator. */
2587 is_cast_operator
(const char *token
, int len
)
2589 return
(! strncmp
(token
, "dynamic_cast", len
)
2590 ||
! strncmp
(token
, "static_cast", len
)
2591 ||
! strncmp
(token
, "reinterpret_cast", len
)
2592 ||
! strncmp
(token
, "const_cast", len
));
2595 /* The scope used for macro expansion. */
2596 static struct macro_scope
*expression_macro_scope
;
2598 /* This is set if a NAME token appeared at the very end of the input
2599 string, with no whitespace separating the name from the EOF. This
2600 is used only when parsing to do field name completion. */
2601 static int saw_name_at_eof
;
2603 /* This is set if the previously-returned token was a structure
2604 operator -- either '.' or ARROW. */
2605 static bool last_was_structop
;
2607 /* Depth of parentheses. */
2608 static int paren_depth
;
2610 /* Read one token, getting characters through lexptr. */
2613 lex_one_token
(struct parser_state
*par_state
, bool *is_quoted_name
)
2617 const char *tokstart
;
2618 bool saw_structop
= last_was_structop
;
2620 last_was_structop
= false
;
2621 *is_quoted_name
= false
;
2625 /* Check if this is a macro invocation that we need to expand. */
2626 if
(! scanning_macro_expansion
())
2628 gdb
::unique_xmalloc_ptr
<char> expanded
2629 = macro_expand_next
(&pstate
->lexptr
, *expression_macro_scope
);
2631 if
(expanded
!= nullptr
)
2632 scan_macro_expansion
(expanded.get
());
2635 pstate
->prev_lexptr
= pstate
->lexptr
;
2637 tokstart
= pstate
->lexptr
;
2638 /* See if it is a special token of length 3. */
2639 for
(const auto
&token
: tokentab3
)
2640 if
(strncmp
(tokstart
, token.oper
, 3) == 0)
2642 if
((token.flags
& FLAG_CXX
) != 0
2643 && par_state
->language
()->la_language
!= language_cplus
)
2645 gdb_assert
((token.flags
& FLAG_C
) == 0);
2647 pstate
->lexptr
+= 3;
2648 yylval.opcode
= token.opcode
;
2652 /* See if it is a special token of length 2. */
2653 for
(const auto
&token
: tokentab2
)
2654 if
(strncmp
(tokstart
, token.oper
, 2) == 0)
2656 if
((token.flags
& FLAG_CXX
) != 0
2657 && par_state
->language
()->la_language
!= language_cplus
)
2659 gdb_assert
((token.flags
& FLAG_C
) == 0);
2661 pstate
->lexptr
+= 2;
2662 yylval.opcode
= token.opcode
;
2663 if
(token.token
== ARROW
)
2664 last_was_structop
= 1;
2668 switch
(c
= *tokstart
)
2671 /* If we were just scanning the result of a macro expansion,
2672 then we need to resume scanning the original text.
2673 If we're parsing for field name completion, and the previous
2674 token allows such completion, return a COMPLETE token.
2675 Otherwise, we were already scanning the original text, and
2676 we're really done. */
2677 if
(scanning_macro_expansion
())
2679 finished_macro_expansion
();
2682 else if
(saw_name_at_eof
)
2684 saw_name_at_eof
= 0;
2687 else if
(par_state
->parse_completion
&& saw_structop
)
2702 if
(par_state
->language
()->la_language
== language_objc
2709 if
(paren_depth
== 0)
2716 if
(pstate
->comma_terminates
2718 && ! scanning_macro_expansion
())
2724 /* Might be a floating point number. */
2725 if
(pstate
->lexptr
[1] < '0' || pstate
->lexptr
[1] > '9')
2727 last_was_structop
= true
;
2728 goto symbol
; /* Nope, must be a symbol. */
2743 /* It's a number. */
2744 int got_dot
= 0, got_e
= 0, got_p
= 0, toktype
;
2745 const char *p
= tokstart
;
2746 int hex
= input_radix
> 10;
2748 if
(c
== '0' && (p
[1] == 'x' || p
[1] == 'X'))
2753 else if
(c
== '0' && (p
[1]=='t' || p
[1]=='T' || p
[1]=='d' || p
[1]=='D'))
2759 /* If the token includes the C++14 digits separator, we make a
2760 copy so that we don't have to handle the separator in
2762 std
::optional
<std
::string> no_tick
;
2765 /* This test includes !hex because 'e' is a valid hex digit
2766 and thus does not indicate a floating point number when
2767 the radix is hex. */
2768 if
(!hex
&& !got_e
&& !got_p
&& (*p
== 'e' ||
*p
== 'E'))
2769 got_dot
= got_e
= 1;
2770 else if
(!got_e
&& !got_p
&& (*p
== 'p' ||
*p
== 'P'))
2771 got_dot
= got_p
= 1;
2772 /* This test does not include !hex, because a '.' always indicates
2773 a decimal floating point number regardless of the radix. */
2774 else if
(!got_dot
&& *p
== '.')
2776 else if
(((got_e
&& (p
[-1] == 'e' || p
[-1] == 'E'))
2777 ||
(got_p
&& (p
[-1] == 'p' || p
[-1] == 'P')))
2778 && (*p
== '-' ||
*p
== '+'))
2780 /* This is the sign of the exponent, not the end of
2783 else if
(*p
== '\'')
2785 if
(!no_tick.has_value
())
2786 no_tick.emplace
(tokstart
, p
);
2789 /* We will take any letters or digits. parse_number will
2790 complain if past the radix, or if L or U are not final. */
2791 else if
((*p
< '0' ||
*p
> '9')
2792 && ((*p
< 'a' ||
*p
> 'z')
2793 && (*p
< 'A' ||
*p
> 'Z')))
2795 if
(no_tick.has_value
())
2796 no_tick
->push_back
(*p
);
2798 if
(no_tick.has_value
())
2799 toktype
= parse_number
(par_state
, no_tick
->c_str
(),
2801 got_dot | got_e | got_p
, &yylval);
2803 toktype
= parse_number
(par_state
, tokstart
, p
- tokstart
,
2804 got_dot | got_e | got_p
, &yylval);
2805 if
(toktype
== ERROR
)
2806 error (_
("Invalid number \"%.*s\"."), (int) (p
- tokstart
),
2814 const char *p
= &tokstart
[1];
2816 if
(par_state
->language
()->la_language
== language_objc
)
2818 size_t len
= strlen
("selector");
2820 if
(strncmp
(p
, "selector", len
) == 0
2821 && (p
[len
] == '\0' || ISSPACE
(p
[len
])))
2823 pstate
->lexptr
= p
+ len
;
2830 while
(ISSPACE
(*p
))
2832 size_t len
= strlen
("entry");
2833 if
(strncmp
(p
, "entry", len
) == 0 && !c_ident_is_alnum
(p
[len
])
2836 pstate
->lexptr
= &p
[len
];
2865 if
(tokstart
[1] != '"' && tokstart
[1] != '\'')
2874 int result
= parse_string_or_char
(tokstart
, &pstate
->lexptr
,
2875 &yylval.tsval
, &host_len
);
2879 error (_
("Empty character constant."));
2880 else if
(host_len
> 2 && c
== '\'')
2883 namelen
= pstate
->lexptr
- tokstart
- 1;
2884 *is_quoted_name
= true
;
2888 else if
(host_len
> 1)
2889 error (_
("Invalid character constant."));
2895 if
(!(c
== '_' || c
== '$' || c_ident_is_alpha
(c
)))
2896 /* We must have come across a bad character (e.g. ';'). */
2897 error (_
("Invalid character '%c' in expression."), c
);
2899 /* It's a name. See how long it is. */
2901 for
(c
= tokstart
[namelen
];
2902 (c
== '_' || c
== '$' || c_ident_is_alnum
(c
) || c
== '<');)
2904 /* Template parameter lists are part of the name.
2905 FIXME: This mishandles `print $a<4&&$a>3'. */
2909 if
(! is_cast_operator
(tokstart
, namelen
))
2911 /* Scan ahead to get rest of the template specification. Note
2912 that we look ahead only when the '<' adjoins non-whitespace
2913 characters; for comparison expressions, e.g. "a < b > c",
2914 there must be spaces before the '<', etc. */
2915 const char *p
= find_template_name_end
(tokstart
+ namelen
);
2918 namelen
= p
- tokstart
;
2922 c
= tokstart
[++namelen
];
2925 /* The token "if" terminates the expression and is NOT removed from
2926 the input stream. It doesn't count if it appears in the
2927 expansion of a macro. */
2929 && tokstart
[0] == 'i'
2930 && tokstart
[1] == 'f'
2931 && ! scanning_macro_expansion
())
2936 /* For the same reason (breakpoint conditions), "thread N"
2937 terminates the expression. "thread" could be an identifier, but
2938 an identifier is never followed by a number without intervening
2939 punctuation. "task" is similar. Handle abbreviations of these,
2940 similarly to breakpoint.c:find_condition_and_thread. */
2942 && (strncmp
(tokstart
, "thread", namelen
) == 0
2943 || strncmp
(tokstart
, "task", namelen
) == 0)
2944 && (tokstart
[namelen
] == ' ' || tokstart
[namelen
] == '\t')
2945 && ! scanning_macro_expansion
())
2947 const char *p
= tokstart
+ namelen
+ 1;
2949 while
(*p
== ' ' ||
*p
== '\t')
2951 if
(*p
>= '0' && *p
<= '9')
2955 pstate
->lexptr
+= namelen
;
2959 yylval.sval.ptr
= tokstart
;
2960 yylval.sval.length
= namelen
;
2962 /* Catch specific keywords. */
2963 std
::string copy
= copy_name
(yylval.sval
);
2964 for
(const auto
&token
: ident_tokens
)
2965 if
(copy
== token.oper
)
2967 if
((token.flags
& FLAG_CXX
) != 0
2968 && par_state
->language
()->la_language
!= language_cplus
)
2970 if
((token.flags
& FLAG_C
) != 0
2971 && par_state
->language
()->la_language
!= language_c
2972 && par_state
->language
()->la_language
!= language_objc
)
2975 if
((token.flags
& FLAG_SHADOW
) != 0)
2977 struct field_of_this_result is_a_field_of_this
;
2979 if
(lookup_symbol
(copy.c_str
(),
2980 pstate
->expression_context_block
,
2982 (par_state
->language
()->la_language
2983 == language_cplus ?
&is_a_field_of_this
2987 /* The keyword is shadowed. */
2992 /* It is ok to always set this, even though we don't always
2993 strictly need to. */
2994 yylval.opcode
= token.opcode
;
2998 if
(*tokstart
== '$')
2999 return DOLLAR_VARIABLE
;
3001 if
(pstate
->parse_completion
&& *pstate
->lexptr
== '\0')
3002 saw_name_at_eof
= 1;
3004 yylval.ssym.stoken
= yylval.sval
;
3005 yylval.ssym.sym.symbol
= NULL
;
3006 yylval.ssym.sym.block
= NULL
;
3007 yylval.ssym.is_a_field_of_this
= 0;
3011 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
3012 struct c_token_and_value
3018 /* A FIFO of tokens that have been read but not yet returned to the
3020 static std
::vector
<c_token_and_value
> token_fifo
;
3022 /* Non-zero if the lexer should return tokens from the FIFO. */
3025 /* Temporary storage for c_lex; this holds symbol names as they are
3027 static auto_obstack name_obstack
;
3029 /* Classify a NAME token. The contents of the token are in `yylval'.
3030 Updates yylval and returns the new token type. BLOCK is the block
3031 in which lookups start; this can be NULL to mean the global scope.
3032 IS_QUOTED_NAME is non-zero if the name token was originally quoted
3033 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
3034 a structure operator -- either '.' or ARROW */
3037 classify_name
(struct parser_state
*par_state
, const struct block
*block
,
3038 bool is_quoted_name
, bool is_after_structop
)
3040 struct block_symbol bsym
;
3041 struct field_of_this_result is_a_field_of_this
;
3043 std
::string copy
= copy_name
(yylval.sval
);
3045 /* Initialize this in case we *don't* use it in this call; that way
3046 we can refer to it unconditionally below. */
3047 memset
(&is_a_field_of_this
, 0, sizeof
(is_a_field_of_this
));
3049 bsym
= lookup_symbol
(copy.c_str
(), block
, SEARCH_VFT
,
3050 par_state
->language
()->name_of_this
()
3051 ?
&is_a_field_of_this
: NULL
);
3053 if
(bsym.symbol
&& bsym.symbol
->aclass
() == LOC_BLOCK
)
3055 yylval.ssym.sym
= bsym
;
3056 yylval.ssym.is_a_field_of_this
= is_a_field_of_this.type
!= NULL
;
3059 else if
(!bsym.symbol
)
3061 /* If we found a field of 'this', we might have erroneously
3062 found a constructor where we wanted a type name. Handle this
3063 case by noticing that we found a constructor and then look up
3064 the type tag instead. */
3065 if
(is_a_field_of_this.type
!= NULL
3066 && is_a_field_of_this.fn_field
!= NULL
3067 && TYPE_FN_FIELD_CONSTRUCTOR
(is_a_field_of_this.fn_field
->fn_fields
,
3070 struct field_of_this_result inner_is_a_field_of_this
;
3072 bsym
= lookup_symbol
(copy.c_str
(), block
, SEARCH_STRUCT_DOMAIN
,
3073 &inner_is_a_field_of_this
);
3074 if
(bsym.symbol
!= NULL
)
3076 yylval.tsym.type
= bsym.symbol
->type
();
3081 /* If we found a field on the "this" object, or we are looking
3082 up a field on a struct, then we want to prefer it over a
3083 filename. However, if the name was quoted, then it is better
3084 to check for a filename or a block, since this is the only
3085 way the user has of requiring the extension to be used. */
3086 if
((is_a_field_of_this.type
== NULL
&& !is_after_structop
)
3089 /* See if it's a file name. */
3090 if
(auto symtab
= lookup_symtab
(current_program_space
, copy.c_str
());
3094 = symtab
->compunit
()->blockvector
()->static_block
();
3101 if
(bsym.symbol
&& bsym.symbol
->aclass
() == LOC_TYPEDEF
)
3103 yylval.tsym.type
= bsym.symbol
->type
();
3107 /* See if it's an ObjC classname. */
3108 if
(par_state
->language
()->la_language
== language_objc
&& !bsym.symbol
)
3110 CORE_ADDR Class
= lookup_objc_class
(par_state
->gdbarch
(),
3116 yylval.theclass.theclass
= Class
;
3117 sym
= lookup_struct_typedef
(copy.c_str
(),
3118 par_state
->expression_context_block
, 1);
3120 yylval.theclass.type
= sym
->type
();
3125 /* Input names that aren't symbols but ARE valid hex numbers, when
3126 the input radix permits them, can be names or numbers depending
3127 on the parse. Note we support radixes > 16 here. */
3129 && ((copy
[0] >= 'a' && copy
[0] < 'a' + input_radix
- 10)
3130 ||
(copy
[0] >= 'A' && copy
[0] < 'A' + input_radix
- 10)))
3132 YYSTYPE newlval
; /* Its value is ignored. */
3133 int hextype
= parse_number
(par_state
, copy.c_str
(), yylval.sval.length
,
3138 yylval.ssym.sym
= bsym
;
3139 yylval.ssym.is_a_field_of_this
= is_a_field_of_this.type
!= NULL
;
3144 /* Any other kind of symbol */
3145 yylval.ssym.sym
= bsym
;
3146 yylval.ssym.is_a_field_of_this
= is_a_field_of_this.type
!= NULL
;
3148 if
(bsym.symbol
== NULL
3149 && par_state
->language
()->la_language
== language_cplus
3150 && is_a_field_of_this.type
== NULL
3151 && lookup_minimal_symbol
(current_program_space
, copy.c_str
()).minsym
== nullptr
)
3152 return UNKNOWN_CPP_NAME
;
3157 /* Like classify_name, but used by the inner loop of the lexer, when a
3158 name might have already been seen. CONTEXT is the context type, or
3159 NULL if this is the first component of a name. */
3162 classify_inner_name
(struct parser_state
*par_state
,
3163 const struct block
*block
, struct type
*context
)
3167 if
(context
== NULL
)
3168 return classify_name
(par_state
, block
, false
, false
);
3170 type
= check_typedef
(context
);
3171 if
(!type_aggregate_p
(type
))
3174 std
::string copy
= copy_name
(yylval.ssym.stoken
);
3175 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3176 yylval.ssym.sym
= cp_lookup_nested_symbol
(type
, copy.c_str
(), block
,
3179 /* If no symbol was found, search for a matching base class named
3180 COPY. This will allow users to enter qualified names of class members
3181 relative to the `this' pointer. */
3182 if
(yylval.ssym.sym.symbol
== NULL
)
3184 struct type
*base_type
= cp_find_type_baseclass_by_name
(type
,
3187 if
(base_type
!= NULL
)
3189 yylval.tsym.type
= base_type
;
3196 switch
(yylval.ssym.sym.symbol
->aclass
())
3200 /* cp_lookup_nested_symbol might have accidentally found a constructor
3201 named COPY when we really wanted a base class of the same name.
3202 Double-check this case by looking for a base class. */
3204 struct type
*base_type
3205 = cp_find_type_baseclass_by_name
(type
, copy.c_str
());
3207 if
(base_type
!= NULL
)
3209 yylval.tsym.type
= base_type
;
3216 yylval.tsym.type
= yylval.ssym.sym.symbol
->type
();
3222 internal_error
(_
("not reached"));
3225 /* The outer level of a two-level lexer. This calls the inner lexer
3226 to return tokens. It then either returns these tokens, or
3227 aggregates them into a larger token. This lets us work around a
3228 problem in our parsing approach, where the parser could not
3229 distinguish between qualified names and qualified types at the
3232 This approach is still not ideal, because it mishandles template
3233 types. See the comment in lex_one_token for an example. However,
3234 this is still an improvement over the earlier approach, and will
3235 suffice until we move to better parsing technology. */
3240 c_token_and_value current
;
3241 int first_was_coloncolon
, last_was_coloncolon
;
3242 struct type
*context_type
= NULL
;
3243 int last_to_examine
, next_to_examine
, checkpoint
;
3244 const struct block
*search_block
;
3245 bool is_quoted_name
, last_lex_was_structop
;
3247 if
(popping
&& !token_fifo.empty
())
3251 last_lex_was_structop
= last_was_structop
;
3253 /* Read the first token and decide what to do. Most of the
3254 subsequent code is C++-only; but also depends on seeing a "::" or
3256 current.token
= lex_one_token
(pstate
, &is_quoted_name
);
3257 if
(current.token
== NAME
)
3258 current.token
= classify_name
(pstate
, pstate
->expression_context_block
,
3259 is_quoted_name
, last_lex_was_structop
);
3260 if
(pstate
->language
()->la_language
!= language_cplus
3261 ||
(current.token
!= TYPENAME
&& current.token
!= COLONCOLON
3262 && current.token
!= FILENAME
))
3263 return current.token
;
3265 /* Read any sequence of alternating "::" and name-like tokens into
3267 current.value
= yylval;
3268 token_fifo.push_back
(current
);
3269 last_was_coloncolon
= current.token
== COLONCOLON
;
3274 /* We ignore quoted names other than the very first one.
3275 Subsequent ones do not have any special meaning. */
3276 current.token
= lex_one_token
(pstate
, &ignore
);
3277 current.value
= yylval;
3278 token_fifo.push_back
(current
);
3280 if
((last_was_coloncolon
&& current.token
!= NAME
)
3281 ||
(!last_was_coloncolon
&& current.token
!= COLONCOLON
))
3283 last_was_coloncolon
= !last_was_coloncolon
;
3287 /* We always read one extra token, so compute the number of tokens
3288 to examine accordingly. */
3289 last_to_examine
= token_fifo.size
() - 2;
3290 next_to_examine
= 0;
3292 current
= token_fifo
[next_to_examine
];
3295 name_obstack.clear
();
3297 if
(current.token
== FILENAME
)
3298 search_block
= current.value.bval
;
3299 else if
(current.token
== COLONCOLON
)
3300 search_block
= NULL
;
3303 gdb_assert
(current.token
== TYPENAME
);
3304 search_block
= pstate
->expression_context_block
;
3305 obstack_grow
(&name_obstack
, current.value.sval.ptr
,
3306 current.value.sval.length
);
3307 context_type
= current.value.tsym.type
;
3311 first_was_coloncolon
= current.token
== COLONCOLON
;
3312 last_was_coloncolon
= first_was_coloncolon
;
3314 while
(next_to_examine
<= last_to_examine
)
3316 c_token_and_value next
;
3318 next
= token_fifo
[next_to_examine
];
3321 if
(next.token
== NAME
&& last_was_coloncolon
)
3325 yylval = next.value
;
3326 classification
= classify_inner_name
(pstate
, search_block
,
3328 /* We keep going until we either run out of names, or until
3329 we have a qualified name which is not a type. */
3330 if
(classification
!= TYPENAME
&& classification
!= NAME
)
3333 /* Accept up to this token. */
3334 checkpoint
= next_to_examine
;
3336 /* Update the partial name we are constructing. */
3337 if
(context_type
!= NULL
)
3339 /* We don't want to put a leading "::" into the name. */
3340 obstack_grow_str
(&name_obstack
, "::");
3342 obstack_grow
(&name_obstack
, next.value.sval.ptr
,
3343 next.value.sval.length
);
3345 yylval.sval.ptr
= (const char *) obstack_base
(&name_obstack
);
3346 yylval.sval.length
= obstack_object_size
(&name_obstack
);
3347 current.value
= yylval;
3348 current.token
= classification
;
3350 last_was_coloncolon
= 0;
3352 if
(classification
== NAME
)
3355 context_type
= yylval.tsym.type
;
3357 else if
(next.token
== COLONCOLON
&& !last_was_coloncolon
)
3358 last_was_coloncolon
= 1;
3361 /* We've reached the end of the name. */
3366 /* If we have a replacement token, install it as the first token in
3367 the FIFO, and delete the other constituent tokens. */
3370 current.value.sval.ptr
3371 = obstack_strndup
(&cpstate
->expansion_obstack
,
3372 current.value.sval.ptr
,
3373 current.value.sval.length
);
3375 token_fifo
[0] = current
;
3377 token_fifo.erase
(token_fifo.begin
() + 1,
3378 token_fifo.begin
() + checkpoint
);
3382 current
= token_fifo
[0];
3383 token_fifo.erase
(token_fifo.begin
());
3384 yylval = current.value
;
3385 return current.token
;
3389 c_parse
(struct parser_state
*par_state
)
3391 /* Setting up the parser state. */
3392 scoped_restore pstate_restore
= make_scoped_restore
(&pstate
);
3393 gdb_assert
(par_state
!= NULL
);
3396 c_parse_state cstate
;
3397 scoped_restore cstate_restore
= make_scoped_restore
(&cpstate
, &cstate
);
3399 gdb
::unique_xmalloc_ptr
<struct macro_scope
> macro_scope
;
3401 if
(par_state
->expression_context_block
)
3403 = sal_macro_scope
(find_pc_line
(par_state
->expression_context_pc
, 0));
3405 macro_scope
= default_macro_scope
();
3407 macro_scope
= user_macro_scope
();
3409 scoped_restore restore_macro_scope
3410 = make_scoped_restore
(&expression_macro_scope
, macro_scope.get
());
3412 scoped_restore restore_yydebug
= make_scoped_restore
(&yydebug,
3415 /* Initialize some state used by the lexer. */
3416 last_was_structop
= false
;
3417 saw_name_at_eof
= 0;
3420 token_fifo.clear
();
3422 name_obstack.clear
();
3424 int result
= yyparse ();
3426 pstate
->set_operation
(pstate
->pop
());
3430 #if defined(YYBISON) && YYBISON < 30800
3433 /* This is called via the YYPRINT macro when parser debugging is
3434 enabled. It prints a token's value. */
3437 c_print_token
(FILE *file
, int type
, YYSTYPE value
)
3442 parser_fprintf
(file
, "typed_val_int<%s, %s>",
3443 TYPE_SAFE_NAME
(value.typed_val_int.type
),
3444 pulongest
(value.typed_val_int.val
));
3449 parser_fprintf
(file
, "tsval<type=%d, %.*s>", value.tsval.type
,
3450 value.tsval.length
, value.tsval.ptr
);
3454 case DOLLAR_VARIABLE
:
3455 parser_fprintf
(file
, "sval<%s>", copy_name
(value.sval
).c_str
());
3459 parser_fprintf
(file
, "tsym<type=%s, name=%s>",
3460 TYPE_SAFE_NAME
(value.tsym.type
),
3461 copy_name
(value.tsym.stoken
).c_str
());
3465 case UNKNOWN_CPP_NAME
:
3468 parser_fprintf
(file
, "ssym<name=%s, sym=%s, field_of_this=%d>",
3469 copy_name
(value.ssym.stoken
).c_str
(),
3470 (value.ssym.sym.symbol
== NULL
3471 ?
"(null)" : value.ssym.sym.symbol
->print_name
()),
3472 value.ssym.is_a_field_of_this
);
3476 parser_fprintf
(file
, "bval<%s>", host_address_to_string
(value.bval
));
3484 yyerror (const char *msg
)
3486 pstate
->parse_error
(msg
);