Bump GDB's version number to 15.0.91.DATE-git.
[binutils-gdb.git] / gdb / c-exp.y
blob60223172a23c91899b481aecb46187490b388d88
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. */
38 #include <ctype.h>
39 #include "expression.h"
40 #include "value.h"
41 #include "parser-defs.h"
42 #include "language.h"
43 #include "c-lang.h"
44 #include "c-support.h"
45 #include "charset.h"
46 #include "block.h"
47 #include "cp-support.h"
48 #include "macroscope.h"
49 #include "objc-lang.h"
50 #include "typeprint.h"
51 #include "cp-abi.h"
52 #include "type-stack.h"
53 #include "target-float.h"
54 #include "c-exp.h"
56 #define parse_type(ps) builtin_type (ps->gdbarch ())
58 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
59 etc). */
60 #define GDB_YY_REMAP_PREFIX c_
61 #include "yy-remap.h"
63 /* The state of the parser, used internally when we are parsing the
64 expression. */
66 static struct parser_state *pstate = NULL;
68 /* Data that must be held for the duration of a parse. */
70 struct c_parse_state
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
104 completed. */
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;
115 int yyparse (void);
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. */
130 %union
132 LONGEST lval;
133 struct {
134 LONGEST val;
135 struct type *type;
136 } typed_val_int;
137 struct {
138 gdb_byte val[16];
139 struct type *type;
140 } typed_val_float;
141 struct type *tval;
142 struct stoken sval;
143 struct typed_stoken tsval;
144 struct ttype tsym;
145 struct symtoken ssym;
146 int voidval;
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)
169 #endif
172 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly function_method
173 %type <lval> rcurly
174 %type <tval> type typebase scalar_type
175 %type <tvec> nonempty_typelist func_mod parameter_typelist
176 /* %type <bval> block */
178 /* Fancy type parsing. */
179 %type <tval> ptype
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 */
199 %token <tsval> CHAR
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. */
213 %token OBJC_LBRAC
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
222 %token OPERATOR
223 %token STRUCT CLASS UNION ENUM SIZEOF ALIGNOF UNSIGNED COLONCOLON
224 %token TEMPLATE
225 %token ERROR
226 %token NEW DELETE
227 %type <sval> oper
228 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
229 %token ENTRY
230 %token TYPEOF
231 %token DECLTYPE
232 %token TYPEID
234 /* Special type cases, put in to allow the parser to distinguish different
235 legal basetypes. */
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
244 /* C++ */
245 %token TRUEKEYWORD
246 %token FALSEKEYWORD
249 %left ','
250 %left ABOVE_COMMA
251 %right '=' ASSIGN_MODIFY
252 %right '?'
253 %left OROR
254 %left ANDAND
255 %left '|'
256 %left '^'
257 %left '&'
258 %left EQUAL NOTEQUAL
259 %left '<' '>' LEQ GEQ
260 %left LSH RSH
261 %left '@'
262 %left '+' '-'
263 %left '*' '/' '%'
264 %right UNARY INCREMENT DECREMENT
265 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
266 %token <ssym> BLOCKNAME
267 %token <bval> FILENAME
268 %type <bval> block
269 %left COLONCOLON
271 %token DOTDOTDOT
276 start : exp1
277 | type_exp
280 type_exp: type
282 pstate->push_new<type_operation> ($1);
284 | TYPEOF '(' exp ')'
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. */
299 exp1 : exp
300 | exp1 ',' exp
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
324 == language_opencl)
325 pstate->wrap<opencl_not_operation> ();
326 else
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 (),
378 copy_name ($3));
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
425 == language_opencl)
426 pstate->push_new<opencl_structop_operation>
427 (pstate->pop (), copy_name ($3));
428 else
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 (),
438 copy_name ($3));
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
497 CORE_ADDR theclass;
499 std::string copy = copy_name ($2.stoken);
500 theclass = lookup_objc_class (pstate->gdbarch (),
501 copy.c_str ());
502 if (theclass == 0)
503 error (_("%s is not an ObjC Class"),
504 copy.c_str ());
505 pstate->push_new<long_const_operation>
506 (parse_type (pstate)->builtin_int,
507 (LONGEST) theclass);
508 start_msglist();
510 msglist ']'
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);
519 start_msglist();
521 msglist ']'
522 { end_msglist (pstate); }
525 exp : OBJC_LBRAC exp
526 { start_msglist(); }
527 msglist ']'
528 { end_msglist (pstate); }
531 msglist : name
532 { add_msglist(&$1, 0); }
533 | msgarglist
536 msgarglist : msgarg
537 | msgarglist msgarg
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); }
548 exp : exp '('
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,
587 std::move (args));
591 lcurly : '{'
592 { pstate->start_arglist (); }
595 arglist :
598 arglist : exp
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),
617 pstate->pop ());
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
637 ambiguity. */
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));
649 rcurly : '}'
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,
657 std::move (args));
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
668 == language_opencl)
669 pstate->wrap2<opencl_cast_type_operation> ();
670 else
671 pstate->wrap2<unop_cast_type_operation> ();
675 exp : '(' exp1 ')'
679 /* Binary operators in order of decreasing precedence. */
681 exp : exp '@' exp
682 { pstate->wrap2<repeat_operation> (); }
685 exp : exp '*' exp
686 { pstate->wrap2<mul_operation> (); }
689 exp : exp '/' exp
690 { pstate->wrap2<div_operation> (); }
693 exp : exp '%' exp
694 { pstate->wrap2<rem_operation> (); }
697 exp : exp '+' exp
698 { pstate->wrap2<add_operation> (); }
701 exp : exp '-' exp
702 { pstate->wrap2<sub_operation> (); }
705 exp : exp LSH exp
706 { pstate->wrap2<lsh_operation> (); }
709 exp : exp RSH exp
710 { pstate->wrap2<rsh_operation> (); }
713 exp : exp EQUAL exp
715 if (pstate->language ()->la_language
716 == language_opencl)
717 pstate->wrap2<opencl_equal_operation> ();
718 else
719 pstate->wrap2<equal_operation> ();
723 exp : exp NOTEQUAL exp
725 if (pstate->language ()->la_language
726 == language_opencl)
727 pstate->wrap2<opencl_notequal_operation> ();
728 else
729 pstate->wrap2<notequal_operation> ();
733 exp : exp LEQ exp
735 if (pstate->language ()->la_language
736 == language_opencl)
737 pstate->wrap2<opencl_leq_operation> ();
738 else
739 pstate->wrap2<leq_operation> ();
743 exp : exp GEQ exp
745 if (pstate->language ()->la_language
746 == language_opencl)
747 pstate->wrap2<opencl_geq_operation> ();
748 else
749 pstate->wrap2<geq_operation> ();
753 exp : exp '<' exp
755 if (pstate->language ()->la_language
756 == language_opencl)
757 pstate->wrap2<opencl_less_operation> ();
758 else
759 pstate->wrap2<less_operation> ();
763 exp : exp '>' exp
765 if (pstate->language ()->la_language
766 == language_opencl)
767 pstate->wrap2<opencl_gtr_operation> ();
768 else
769 pstate->wrap2<gtr_operation> ();
773 exp : exp '&' exp
774 { pstate->wrap2<bitwise_and_operation> (); }
777 exp : exp '^' exp
778 { pstate->wrap2<bitwise_xor_operation> (); }
781 exp : exp '|' exp
782 { pstate->wrap2<bitwise_ior_operation> (); }
785 exp : exp ANDAND exp
787 if (pstate->language ()->la_language
788 == language_opencl)
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),
794 std::move (rhs));
796 else
797 pstate->wrap2<logical_and_operation> ();
801 exp : exp OROR exp
803 if (pstate->language ()->la_language
804 == language_opencl)
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),
810 std::move (rhs));
812 else
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
823 == language_opencl)
824 pstate->push_new<opencl_ternop_cond_operation>
825 (std::move (first), std::move (mid),
826 std::move (last));
827 else
828 pstate->push_new<ternop_cond_operation>
829 (std::move (first), std::move (mid),
830 std::move (last));
834 exp : exp '=' exp
836 if (pstate->language ()->la_language
837 == language_opencl)
838 pstate->wrap2<opencl_assign_operation> ();
839 else
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));
853 exp : INT
855 pstate->push_new<long_const_operation>
856 ($1.type, $1.val);
860 exp : COMPLEX_INT
862 operation_up real
863 = (make_operation<long_const_operation>
864 ($1.type->target_type (), 0));
865 operation_up imag
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);
873 exp : CHAR
875 struct stoken_vector vec;
876 vec.len = 1;
877 vec.tokens = &$1;
878 pstate->push_c_string ($1.type, &vec);
882 exp : NAME_OR_INT
883 { YYSTYPE val;
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);
893 exp : FLOAT
895 float_data data;
896 std::copy (std::begin ($1.val), std::end ($1.val),
897 std::begin (data));
898 pstate->push_new<float_const_operation> ($1.type, data);
902 exp : COMPLEX_FLOAT
904 struct type *underlying = $1.type->target_type ();
906 float_data val;
907 target_float_from_host_double (val.data (),
908 underlying, 0);
909 operation_up real
910 = (make_operation<float_const_operation>
911 (underlying, val));
913 std::copy (std::begin ($1.val), std::end ($1.val),
914 std::begin (val));
915 operation_up imag
916 = (make_operation<float_const_operation>
917 (underlying, val));
919 pstate->push_new<complex_operation>
920 (std::move (real), std::move (imag),
921 $1.type);
925 exp : variable
928 exp : DOLLAR_VARIABLE
930 pstate->push_dollar ($1);
934 exp : SELECTOR '(' name ')'
936 pstate->push_new<objc_selector_operation>
937 (copy_name ($3));
941 exp : SIZEOF '(' type ')' %prec UNARY
942 { struct type *type = $3;
943 struct type *int_type
944 = lookup_signed_typename (pstate->language (),
945 "int");
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> (); }
978 string_exp:
979 STRING
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
985 lexer. */
986 struct typed_stoken *vec = XNEW (struct typed_stoken);
987 $$.len = 1;
988 $$.tokens = vec;
990 vec->type = $1.type;
991 vec->length = $1.length;
992 vec->ptr = (char *) malloc ($1.length + 1);
993 memcpy (vec->ptr, $1.ptr, $1.length + 1);
996 | string_exp STRING
998 /* Note that we NUL-terminate here, but just
999 for convenience. */
1000 char *p;
1001 ++$$.len;
1002 $$.tokens = XRESIZEVEC (struct typed_stoken,
1003 $$.tokens, $$.len);
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;
1014 exp : string_exp
1016 int i;
1017 c_string_type type = C_STRING;
1019 for (i = 0; i < $1.len; ++i)
1021 switch ($1.tokens[i].type)
1023 case C_STRING:
1024 break;
1025 case C_WIDE_STRING:
1026 case C_STRING_16:
1027 case C_STRING_32:
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;
1032 break;
1033 default:
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);
1042 free ($1.tokens);
1046 exp : NSSTRING /* ObjC NextStep NSString constant
1047 * of the form '@' '"' string '"'.
1050 pstate->push_new<objc_nsstring_operation>
1051 (copy_name ($1));
1055 /* C++. */
1056 exp : TRUEKEYWORD
1057 { pstate->push_new<long_const_operation>
1058 (parse_type (pstate)->builtin_bool, 1);
1062 exp : FALSEKEYWORD
1063 { pstate->push_new<long_const_operation>
1064 (parse_type (pstate)->builtin_bool, 0);
1068 /* end of C++. */
1070 block : BLOCKNAME
1072 if ($1.sym.symbol)
1073 $$ = $1.sym.symbol->value_block ();
1074 else
1075 error (_("No file or function \"%s\"."),
1076 copy_name ($1.stoken).c_str ());
1078 | FILENAME
1080 $$ = $1;
1084 block : block COLONCOLON name
1086 std::string copy = copy_name ($3);
1087 struct symbol *tem
1088 = lookup_symbol (copy.c_str (), $1,
1089 SEARCH_FUNCTION_DOMAIN,
1090 nullptr).symbol;
1092 if (tem == nullptr)
1093 error (_("No function \"%s\" in specified context."),
1094 copy.c_str ());
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,
1116 SEARCH_VFT, NULL);
1118 if (sym.symbol == 0)
1119 error (_("No symbol \"%s\" in specified context."),
1120 copy.c_str ());
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,
1137 copy_name ($3));
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,
1148 $4.length);
1150 /* Check for valid destructor name. */
1151 destructor_name_p (name.c_str (), $1.type);
1152 pstate->push_new<scope_operation> (type,
1153 std::move (name));
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,
1171 SEARCH_VFT, NULL);
1172 pstate->push_symbol (name.c_str (), sym);
1176 variable: name_not_typename
1177 { struct block_symbol sym = $1.sym;
1179 if (sym.symbol)
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>
1193 (resolver);
1194 else
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
1201 to data ref. */
1202 pstate->block_tracker->update (sym);
1203 operation_up thisop
1204 = make_operation<op_this_operation> ();
1205 pstate->push_new<structop_ptr_operation>
1206 (std::move (thisop), copy_name ($1.stoken));
1208 else
1210 std::string arg = copy_name ($1.stoken);
1212 bound_minimal_symbol msymbol
1213 = lookup_bound_minimal_symbol (arg.c_str ());
1214 if (msymbol.minsym == NULL)
1216 if (!have_full_symbols () && !have_partial_symbols ())
1217 error (_("No symbol table is loaded. Use the \"file\" command."));
1218 else
1219 error (_("No symbol \"%s\" in current context."),
1220 arg.c_str ());
1223 /* This minsym might be an alias for
1224 another function. See if we can find
1225 the debug symbol for the target, and
1226 if so, use it instead, since it has
1227 return type / prototype info. This
1228 is important for example for "p
1229 *__errno_location()". */
1230 symbol *alias_target
1231 = ((msymbol.minsym->type () != mst_text_gnu_ifunc
1232 && msymbol.minsym->type () != mst_data_gnu_ifunc)
1233 ? find_function_alias_target (msymbol)
1234 : NULL);
1235 if (alias_target != NULL)
1237 block_symbol bsym { alias_target,
1238 alias_target->value_block () };
1239 pstate->push_new<var_value_operation> (bsym);
1241 else
1242 pstate->push_new<var_msym_value_operation>
1243 (msymbol);
1248 const_or_volatile: const_or_volatile_noopt
1252 single_qualifier:
1253 CONST_KEYWORD
1254 { cpstate->type_stack.insert (tp_const); }
1255 | VOLATILE_KEYWORD
1256 { cpstate->type_stack.insert (tp_volatile); }
1257 | ATOMIC
1258 { cpstate->type_stack.insert (tp_atomic); }
1259 | RESTRICT
1260 { cpstate->type_stack.insert (tp_restrict); }
1261 | '@' NAME
1263 cpstate->type_stack.insert (pstate,
1264 copy_name ($2.stoken).c_str ());
1266 | '@' UNKNOWN_CPP_NAME
1268 cpstate->type_stack.insert (pstate,
1269 copy_name ($2.stoken).c_str ());
1273 qualifier_seq_noopt:
1274 single_qualifier
1275 | qualifier_seq_noopt single_qualifier
1278 qualifier_seq:
1279 qualifier_seq_noopt
1283 ptr_operator:
1284 ptr_operator '*'
1285 { cpstate->type_stack.insert (tp_pointer); }
1286 qualifier_seq
1287 | '*'
1288 { cpstate->type_stack.insert (tp_pointer); }
1289 qualifier_seq
1290 | '&'
1291 { cpstate->type_stack.insert (tp_reference); }
1292 | '&' ptr_operator
1293 { cpstate->type_stack.insert (tp_reference); }
1294 | ANDAND
1295 { cpstate->type_stack.insert (tp_rvalue_reference); }
1296 | ANDAND ptr_operator
1297 { cpstate->type_stack.insert (tp_rvalue_reference); }
1300 ptr_operator_ts: ptr_operator
1302 $$ = cpstate->type_stack.create ();
1303 cpstate->type_stacks.emplace_back ($$);
1307 abs_decl: ptr_operator_ts direct_abs_decl
1308 { $$ = $2->append ($1); }
1309 | ptr_operator_ts
1310 | direct_abs_decl
1313 direct_abs_decl: '(' abs_decl ')'
1314 { $$ = $2; }
1315 | direct_abs_decl array_mod
1317 cpstate->type_stack.push ($1);
1318 cpstate->type_stack.push ($2);
1319 cpstate->type_stack.push (tp_array);
1320 $$ = cpstate->type_stack.create ();
1321 cpstate->type_stacks.emplace_back ($$);
1323 | array_mod
1325 cpstate->type_stack.push ($1);
1326 cpstate->type_stack.push (tp_array);
1327 $$ = cpstate->type_stack.create ();
1328 cpstate->type_stacks.emplace_back ($$);
1331 | direct_abs_decl func_mod
1333 cpstate->type_stack.push ($1);
1334 cpstate->type_stack.push ($2);
1335 $$ = cpstate->type_stack.create ();
1336 cpstate->type_stacks.emplace_back ($$);
1338 | func_mod
1340 cpstate->type_stack.push ($1);
1341 $$ = cpstate->type_stack.create ();
1342 cpstate->type_stacks.emplace_back ($$);
1346 array_mod: '[' ']'
1347 { $$ = -1; }
1348 | OBJC_LBRAC ']'
1349 { $$ = -1; }
1350 | '[' INT ']'
1351 { $$ = $2.val; }
1352 | OBJC_LBRAC INT ']'
1353 { $$ = $2.val; }
1356 func_mod: '(' ')'
1358 $$ = new std::vector<struct type *>;
1359 cpstate->type_lists.emplace_back ($$);
1361 | '(' parameter_typelist ')'
1362 { $$ = $2; }
1365 /* We used to try to recognize pointer to member types here, but
1366 that didn't work (shift/reduce conflicts meant that these rules never
1367 got executed). The problem is that
1368 int (foo::bar::baz::bizzle)
1369 is a function type but
1370 int (foo::bar::baz::bizzle::*)
1371 is a pointer to member type. Stroustrup loses again! */
1373 type : ptype
1376 /* A helper production that recognizes scalar types that can validly
1377 be used with _Complex. */
1379 scalar_type:
1380 INT_KEYWORD
1381 { $$ = lookup_signed_typename (pstate->language (),
1382 "int"); }
1383 | LONG
1384 { $$ = lookup_signed_typename (pstate->language (),
1385 "long"); }
1386 | SHORT
1387 { $$ = lookup_signed_typename (pstate->language (),
1388 "short"); }
1389 | LONG INT_KEYWORD
1390 { $$ = lookup_signed_typename (pstate->language (),
1391 "long"); }
1392 | LONG SIGNED_KEYWORD INT_KEYWORD
1393 { $$ = lookup_signed_typename (pstate->language (),
1394 "long"); }
1395 | LONG SIGNED_KEYWORD
1396 { $$ = lookup_signed_typename (pstate->language (),
1397 "long"); }
1398 | SIGNED_KEYWORD LONG INT_KEYWORD
1399 { $$ = lookup_signed_typename (pstate->language (),
1400 "long"); }
1401 | UNSIGNED LONG INT_KEYWORD
1402 { $$ = lookup_unsigned_typename (pstate->language (),
1403 "long"); }
1404 | LONG UNSIGNED INT_KEYWORD
1405 { $$ = lookup_unsigned_typename (pstate->language (),
1406 "long"); }
1407 | LONG UNSIGNED
1408 { $$ = lookup_unsigned_typename (pstate->language (),
1409 "long"); }
1410 | LONG LONG
1411 { $$ = lookup_signed_typename (pstate->language (),
1412 "long long"); }
1413 | LONG LONG INT_KEYWORD
1414 { $$ = lookup_signed_typename (pstate->language (),
1415 "long long"); }
1416 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1417 { $$ = lookup_signed_typename (pstate->language (),
1418 "long long"); }
1419 | LONG LONG SIGNED_KEYWORD
1420 { $$ = lookup_signed_typename (pstate->language (),
1421 "long long"); }
1422 | SIGNED_KEYWORD LONG LONG
1423 { $$ = lookup_signed_typename (pstate->language (),
1424 "long long"); }
1425 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1426 { $$ = lookup_signed_typename (pstate->language (),
1427 "long long"); }
1428 | UNSIGNED LONG LONG
1429 { $$ = lookup_unsigned_typename (pstate->language (),
1430 "long long"); }
1431 | UNSIGNED LONG LONG INT_KEYWORD
1432 { $$ = lookup_unsigned_typename (pstate->language (),
1433 "long long"); }
1434 | LONG LONG UNSIGNED
1435 { $$ = lookup_unsigned_typename (pstate->language (),
1436 "long long"); }
1437 | LONG LONG UNSIGNED INT_KEYWORD
1438 { $$ = lookup_unsigned_typename (pstate->language (),
1439 "long long"); }
1440 | SHORT INT_KEYWORD
1441 { $$ = lookup_signed_typename (pstate->language (),
1442 "short"); }
1443 | SHORT SIGNED_KEYWORD INT_KEYWORD
1444 { $$ = lookup_signed_typename (pstate->language (),
1445 "short"); }
1446 | SHORT SIGNED_KEYWORD
1447 { $$ = lookup_signed_typename (pstate->language (),
1448 "short"); }
1449 | UNSIGNED SHORT INT_KEYWORD
1450 { $$ = lookup_unsigned_typename (pstate->language (),
1451 "short"); }
1452 | SHORT UNSIGNED
1453 { $$ = lookup_unsigned_typename (pstate->language (),
1454 "short"); }
1455 | SHORT UNSIGNED INT_KEYWORD
1456 { $$ = lookup_unsigned_typename (pstate->language (),
1457 "short"); }
1458 | DOUBLE_KEYWORD
1459 { $$ = lookup_typename (pstate->language (),
1460 "double",
1461 NULL,
1462 0); }
1463 | FLOAT_KEYWORD
1464 { $$ = lookup_typename (pstate->language (),
1465 "float",
1466 NULL,
1467 0); }
1468 | LONG DOUBLE_KEYWORD
1469 { $$ = lookup_typename (pstate->language (),
1470 "long double",
1471 NULL,
1472 0); }
1473 | UNSIGNED type_name
1474 { $$ = lookup_unsigned_typename (pstate->language (),
1475 $2.type->name ()); }
1476 | UNSIGNED
1477 { $$ = lookup_unsigned_typename (pstate->language (),
1478 "int"); }
1479 | SIGNED_KEYWORD type_name
1480 { $$ = lookup_signed_typename (pstate->language (),
1481 $2.type->name ()); }
1482 | SIGNED_KEYWORD
1483 { $$ = lookup_signed_typename (pstate->language (),
1484 "int"); }
1487 /* Implements (approximately): (type-qualifier)* type-specifier.
1489 When type-specifier is only ever a single word, like 'float' then these
1490 arrive as pre-built TYPENAME tokens thanks to the classify_name
1491 function. However, when a type-specifier can contain multiple words,
1492 for example 'double' can appear as just 'double' or 'long double', and
1493 similarly 'long' can appear as just 'long' or in 'long double', then
1494 these type-specifiers are parsed into their own tokens in the function
1495 lex_one_token and the ident_tokens array. These separate tokens are all
1496 recognised here. */
1497 typebase
1498 : TYPENAME
1499 { $$ = $1.type; }
1500 | scalar_type
1501 { $$ = $1; }
1502 | COMPLEX scalar_type
1504 $$ = init_complex_type (nullptr, $2);
1506 | STRUCT name
1507 { $$
1508 = lookup_struct (copy_name ($2).c_str (),
1509 pstate->expression_context_block);
1511 | STRUCT COMPLETE
1513 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1514 "", 0);
1515 $$ = NULL;
1517 | STRUCT name COMPLETE
1519 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1520 $2.ptr, $2.length);
1521 $$ = NULL;
1523 | CLASS name
1524 { $$ = lookup_struct
1525 (copy_name ($2).c_str (),
1526 pstate->expression_context_block);
1528 | CLASS COMPLETE
1530 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1531 "", 0);
1532 $$ = NULL;
1534 | CLASS name COMPLETE
1536 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1537 $2.ptr, $2.length);
1538 $$ = NULL;
1540 | UNION name
1541 { $$
1542 = lookup_union (copy_name ($2).c_str (),
1543 pstate->expression_context_block);
1545 | UNION COMPLETE
1547 pstate->mark_completion_tag (TYPE_CODE_UNION,
1548 "", 0);
1549 $$ = NULL;
1551 | UNION name COMPLETE
1553 pstate->mark_completion_tag (TYPE_CODE_UNION,
1554 $2.ptr, $2.length);
1555 $$ = NULL;
1557 | ENUM name
1558 { $$ = lookup_enum (copy_name ($2).c_str (),
1559 pstate->expression_context_block);
1561 | ENUM COMPLETE
1563 pstate->mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1564 $$ = NULL;
1566 | ENUM name COMPLETE
1568 pstate->mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1569 $2.length);
1570 $$ = NULL;
1572 /* It appears that this rule for templates is never
1573 reduced; template recognition happens by lookahead
1574 in the token processing code in yylex. */
1575 | TEMPLATE name '<' type '>'
1576 { $$ = lookup_template_type
1577 (copy_name($2).c_str (), $4,
1578 pstate->expression_context_block);
1580 | qualifier_seq_noopt typebase
1581 { $$ = cpstate->type_stack.follow_types ($2); }
1582 | typebase qualifier_seq_noopt
1583 { $$ = cpstate->type_stack.follow_types ($1); }
1586 type_name: TYPENAME
1587 | INT_KEYWORD
1589 $$.stoken.ptr = "int";
1590 $$.stoken.length = 3;
1591 $$.type = lookup_signed_typename (pstate->language (),
1592 "int");
1594 | LONG
1596 $$.stoken.ptr = "long";
1597 $$.stoken.length = 4;
1598 $$.type = lookup_signed_typename (pstate->language (),
1599 "long");
1601 | SHORT
1603 $$.stoken.ptr = "short";
1604 $$.stoken.length = 5;
1605 $$.type = lookup_signed_typename (pstate->language (),
1606 "short");
1610 parameter_typelist:
1611 nonempty_typelist
1612 { check_parameter_typelist ($1); }
1613 | nonempty_typelist ',' DOTDOTDOT
1615 $1->push_back (NULL);
1616 check_parameter_typelist ($1);
1617 $$ = $1;
1621 nonempty_typelist
1622 : type
1624 std::vector<struct type *> *typelist
1625 = new std::vector<struct type *>;
1626 cpstate->type_lists.emplace_back (typelist);
1628 typelist->push_back ($1);
1629 $$ = typelist;
1631 | nonempty_typelist ',' type
1633 $1->push_back ($3);
1634 $$ = $1;
1638 ptype : typebase
1639 | ptype abs_decl
1641 cpstate->type_stack.push ($2);
1642 $$ = cpstate->type_stack.follow_types ($1);
1646 conversion_type_id: typebase conversion_declarator
1647 { $$ = cpstate->type_stack.follow_types ($1); }
1650 conversion_declarator: /* Nothing. */
1651 | ptr_operator conversion_declarator
1654 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1655 | VOLATILE_KEYWORD CONST_KEYWORD
1658 const_or_volatile_noopt: const_and_volatile
1659 { cpstate->type_stack.insert (tp_const);
1660 cpstate->type_stack.insert (tp_volatile);
1662 | CONST_KEYWORD
1663 { cpstate->type_stack.insert (tp_const); }
1664 | VOLATILE_KEYWORD
1665 { cpstate->type_stack.insert (tp_volatile); }
1668 oper: OPERATOR NEW
1669 { $$ = operator_stoken (" new"); }
1670 | OPERATOR DELETE
1671 { $$ = operator_stoken (" delete"); }
1672 | OPERATOR NEW '[' ']'
1673 { $$ = operator_stoken (" new[]"); }
1674 | OPERATOR DELETE '[' ']'
1675 { $$ = operator_stoken (" delete[]"); }
1676 | OPERATOR NEW OBJC_LBRAC ']'
1677 { $$ = operator_stoken (" new[]"); }
1678 | OPERATOR DELETE OBJC_LBRAC ']'
1679 { $$ = operator_stoken (" delete[]"); }
1680 | OPERATOR '+'
1681 { $$ = operator_stoken ("+"); }
1682 | OPERATOR '-'
1683 { $$ = operator_stoken ("-"); }
1684 | OPERATOR '*'
1685 { $$ = operator_stoken ("*"); }
1686 | OPERATOR '/'
1687 { $$ = operator_stoken ("/"); }
1688 | OPERATOR '%'
1689 { $$ = operator_stoken ("%"); }
1690 | OPERATOR '^'
1691 { $$ = operator_stoken ("^"); }
1692 | OPERATOR '&'
1693 { $$ = operator_stoken ("&"); }
1694 | OPERATOR '|'
1695 { $$ = operator_stoken ("|"); }
1696 | OPERATOR '~'
1697 { $$ = operator_stoken ("~"); }
1698 | OPERATOR '!'
1699 { $$ = operator_stoken ("!"); }
1700 | OPERATOR '='
1701 { $$ = operator_stoken ("="); }
1702 | OPERATOR '<'
1703 { $$ = operator_stoken ("<"); }
1704 | OPERATOR '>'
1705 { $$ = operator_stoken (">"); }
1706 | OPERATOR ASSIGN_MODIFY
1707 { const char *op = " unknown";
1708 switch ($2)
1710 case BINOP_RSH:
1711 op = ">>=";
1712 break;
1713 case BINOP_LSH:
1714 op = "<<=";
1715 break;
1716 case BINOP_ADD:
1717 op = "+=";
1718 break;
1719 case BINOP_SUB:
1720 op = "-=";
1721 break;
1722 case BINOP_MUL:
1723 op = "*=";
1724 break;
1725 case BINOP_DIV:
1726 op = "/=";
1727 break;
1728 case BINOP_REM:
1729 op = "%=";
1730 break;
1731 case BINOP_BITWISE_IOR:
1732 op = "|=";
1733 break;
1734 case BINOP_BITWISE_AND:
1735 op = "&=";
1736 break;
1737 case BINOP_BITWISE_XOR:
1738 op = "^=";
1739 break;
1740 default:
1741 break;
1744 $$ = operator_stoken (op);
1746 | OPERATOR LSH
1747 { $$ = operator_stoken ("<<"); }
1748 | OPERATOR RSH
1749 { $$ = operator_stoken (">>"); }
1750 | OPERATOR EQUAL
1751 { $$ = operator_stoken ("=="); }
1752 | OPERATOR NOTEQUAL
1753 { $$ = operator_stoken ("!="); }
1754 | OPERATOR LEQ
1755 { $$ = operator_stoken ("<="); }
1756 | OPERATOR GEQ
1757 { $$ = operator_stoken (">="); }
1758 | OPERATOR ANDAND
1759 { $$ = operator_stoken ("&&"); }
1760 | OPERATOR OROR
1761 { $$ = operator_stoken ("||"); }
1762 | OPERATOR INCREMENT
1763 { $$ = operator_stoken ("++"); }
1764 | OPERATOR DECREMENT
1765 { $$ = operator_stoken ("--"); }
1766 | OPERATOR ','
1767 { $$ = operator_stoken (","); }
1768 | OPERATOR ARROW_STAR
1769 { $$ = operator_stoken ("->*"); }
1770 | OPERATOR ARROW
1771 { $$ = operator_stoken ("->"); }
1772 | OPERATOR '(' ')'
1773 { $$ = operator_stoken ("()"); }
1774 | OPERATOR '[' ']'
1775 { $$ = operator_stoken ("[]"); }
1776 | OPERATOR OBJC_LBRAC ']'
1777 { $$ = operator_stoken ("[]"); }
1778 | OPERATOR conversion_type_id
1780 string_file buf;
1781 c_print_type ($2, NULL, &buf, -1, 0,
1782 pstate->language ()->la_language,
1783 &type_print_raw_options);
1784 std::string name = buf.release ();
1786 /* This also needs canonicalization. */
1787 gdb::unique_xmalloc_ptr<char> canon
1788 = cp_canonicalize_string (name.c_str ());
1789 if (canon != nullptr)
1790 name = canon.get ();
1791 $$ = operator_stoken ((" " + name).c_str ());
1795 /* This rule exists in order to allow some tokens that would not normally
1796 match the 'name' rule to appear as fields within a struct. The example
1797 that initially motivated this was the RISC-V target which models the
1798 floating point registers as a union with fields called 'float' and
1799 'double'. */
1800 field_name
1801 : name
1802 | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
1803 | FLOAT_KEYWORD { $$ = typename_stoken ("float"); }
1804 | INT_KEYWORD { $$ = typename_stoken ("int"); }
1805 | LONG { $$ = typename_stoken ("long"); }
1806 | SHORT { $$ = typename_stoken ("short"); }
1807 | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
1808 | UNSIGNED { $$ = typename_stoken ("unsigned"); }
1811 name : NAME { $$ = $1.stoken; }
1812 | BLOCKNAME { $$ = $1.stoken; }
1813 | TYPENAME { $$ = $1.stoken; }
1814 | NAME_OR_INT { $$ = $1.stoken; }
1815 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1816 | oper { $$ = $1; }
1819 name_not_typename : NAME
1820 | BLOCKNAME
1821 /* These would be useful if name_not_typename was useful, but it is just
1822 a fake for "variable", so these cause reduce/reduce conflicts because
1823 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1824 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1825 context where only a name could occur, this might be useful.
1826 | NAME_OR_INT
1828 | oper
1830 struct field_of_this_result is_a_field_of_this;
1832 $$.stoken = $1;
1833 $$.sym
1834 = lookup_symbol ($1.ptr,
1835 pstate->expression_context_block,
1836 SEARCH_VFT,
1837 &is_a_field_of_this);
1838 $$.is_a_field_of_this
1839 = is_a_field_of_this.type != NULL;
1841 | UNKNOWN_CPP_NAME
1846 /* Returns a stoken of the operator name given by OP (which does not
1847 include the string "operator"). */
1849 static struct stoken
1850 operator_stoken (const char *op)
1852 struct stoken st = { NULL, 0 };
1853 char *buf;
1855 st.length = CP_OPERATOR_LEN + strlen (op);
1856 buf = (char *) malloc (st.length + 1);
1857 strcpy (buf, CP_OPERATOR_STR);
1858 strcat (buf, op);
1859 st.ptr = buf;
1861 /* The toplevel (c_parse) will free the memory allocated here. */
1862 cpstate->strings.emplace_back (buf);
1863 return st;
1866 /* Returns a stoken of the type named TYPE. */
1868 static struct stoken
1869 typename_stoken (const char *type)
1871 struct stoken st = { type, 0 };
1872 st.length = strlen (type);
1873 return st;
1876 /* Return true if the type is aggregate-like. */
1878 static int
1879 type_aggregate_p (struct type *type)
1881 return (type->code () == TYPE_CODE_STRUCT
1882 || type->code () == TYPE_CODE_UNION
1883 || type->code () == TYPE_CODE_NAMESPACE
1884 || (type->code () == TYPE_CODE_ENUM
1885 && type->is_declared_class ()));
1888 /* Validate a parameter typelist. */
1890 static void
1891 check_parameter_typelist (std::vector<struct type *> *params)
1893 struct type *type;
1894 int ix;
1896 for (ix = 0; ix < params->size (); ++ix)
1898 type = (*params)[ix];
1899 if (type != NULL && check_typedef (type)->code () == TYPE_CODE_VOID)
1901 if (ix == 0)
1903 if (params->size () == 1)
1905 /* Ok. */
1906 break;
1908 error (_("parameter types following 'void'"));
1910 else
1911 error (_("'void' invalid as parameter type"));
1916 /* Take care of parsing a number (anything that starts with a digit).
1917 Set yylval and return the token type; update lexptr.
1918 LEN is the number of characters in it. */
1920 /*** Needs some error checking for the float case ***/
1922 static int
1923 parse_number (struct parser_state *par_state,
1924 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1926 ULONGEST n = 0;
1927 ULONGEST prevn = 0;
1929 int i = 0;
1930 int c;
1931 int base = input_radix;
1932 int unsigned_p = 0;
1934 /* Number of "L" suffixes encountered. */
1935 int long_p = 0;
1937 /* Imaginary number. */
1938 bool imaginary_p = false;
1940 /* We have found a "L" or "U" (or "i") suffix. */
1941 int found_suffix = 0;
1943 if (parsed_float)
1945 if (len >= 1 && buf[len - 1] == 'i')
1947 imaginary_p = true;
1948 --len;
1951 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
1952 if (len >= 2 && buf[len - 2] == 'd' && buf[len - 1] == 'f')
1954 putithere->typed_val_float.type
1955 = parse_type (par_state)->builtin_decfloat;
1956 len -= 2;
1958 else if (len >= 2 && buf[len - 2] == 'd' && buf[len - 1] == 'd')
1960 putithere->typed_val_float.type
1961 = parse_type (par_state)->builtin_decdouble;
1962 len -= 2;
1964 else if (len >= 2 && buf[len - 2] == 'd' && buf[len - 1] == 'l')
1966 putithere->typed_val_float.type
1967 = parse_type (par_state)->builtin_declong;
1968 len -= 2;
1970 /* Handle suffixes: 'f' for float, 'l' for long double. */
1971 else if (len >= 1 && TOLOWER (buf[len - 1]) == 'f')
1973 putithere->typed_val_float.type
1974 = parse_type (par_state)->builtin_float;
1975 len -= 1;
1977 else if (len >= 1 && TOLOWER (buf[len - 1]) == 'l')
1979 putithere->typed_val_float.type
1980 = parse_type (par_state)->builtin_long_double;
1981 len -= 1;
1983 /* Default type for floating-point literals is double. */
1984 else
1986 putithere->typed_val_float.type
1987 = parse_type (par_state)->builtin_double;
1990 if (!parse_float (buf, len,
1991 putithere->typed_val_float.type,
1992 putithere->typed_val_float.val))
1993 return ERROR;
1995 if (imaginary_p)
1996 putithere->typed_val_float.type
1997 = init_complex_type (nullptr, putithere->typed_val_float.type);
1999 return imaginary_p ? COMPLEX_FLOAT : FLOAT;
2002 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
2003 if (buf[0] == '0' && len > 1)
2004 switch (buf[1])
2006 case 'x':
2007 case 'X':
2008 if (len >= 3)
2010 buf += 2;
2011 base = 16;
2012 len -= 2;
2014 break;
2016 case 'b':
2017 case 'B':
2018 if (len >= 3)
2020 buf += 2;
2021 base = 2;
2022 len -= 2;
2024 break;
2026 case 't':
2027 case 'T':
2028 case 'd':
2029 case 'D':
2030 if (len >= 3)
2032 buf += 2;
2033 base = 10;
2034 len -= 2;
2036 break;
2038 default:
2039 base = 8;
2040 break;
2043 while (len-- > 0)
2045 c = *buf++;
2046 if (c >= 'A' && c <= 'Z')
2047 c += 'a' - 'A';
2048 if (c != 'l' && c != 'u' && c != 'i')
2049 n *= base;
2050 if (c >= '0' && c <= '9')
2052 if (found_suffix)
2053 return ERROR;
2054 n += i = c - '0';
2056 else
2058 if (base > 10 && c >= 'a' && c <= 'f')
2060 if (found_suffix)
2061 return ERROR;
2062 n += i = c - 'a' + 10;
2064 else if (c == 'l')
2066 ++long_p;
2067 found_suffix = 1;
2069 else if (c == 'u')
2071 unsigned_p = 1;
2072 found_suffix = 1;
2074 else if (c == 'i')
2076 imaginary_p = true;
2077 found_suffix = 1;
2079 else
2080 return ERROR; /* Char not a digit */
2082 if (i >= base)
2083 return ERROR; /* Invalid digit in this base */
2085 if (c != 'l' && c != 'u' && c != 'i')
2087 /* Test for overflow. */
2088 if (prevn == 0 && n == 0)
2090 else if (prevn >= n)
2091 error (_("Numeric constant too large."));
2093 prevn = n;
2096 /* An integer constant is an int, a long, or a long long. An L
2097 suffix forces it to be long; an LL suffix forces it to be long
2098 long. If not forced to a larger size, it gets the first type of
2099 the above that it fits in. To figure out whether it fits, we
2100 shift it right and see whether anything remains. Note that we
2101 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
2102 operation, because many compilers will warn about such a shift
2103 (which always produces a zero result). Sometimes gdbarch_int_bit
2104 or gdbarch_long_bit will be that big, sometimes not. To deal with
2105 the case where it is we just always shift the value more than
2106 once, with fewer bits each time. */
2107 int int_bits = gdbarch_int_bit (par_state->gdbarch ());
2108 int long_bits = gdbarch_long_bit (par_state->gdbarch ());
2109 int long_long_bits = gdbarch_long_long_bit (par_state->gdbarch ());
2110 bool have_signed
2111 /* No 'u' suffix. */
2112 = !unsigned_p;
2113 bool have_unsigned
2114 = ((/* 'u' suffix. */
2115 unsigned_p)
2116 || (/* Not a decimal. */
2117 base != 10)
2118 || (/* Allowed as a convenience, in case decimal doesn't fit in largest
2119 signed type. */
2120 !fits_in_type (1, n, long_long_bits, true)));
2121 bool have_int
2122 /* No 'l' or 'll' suffix. */
2123 = long_p == 0;
2124 bool have_long
2125 /* No 'll' suffix. */
2126 = long_p <= 1;
2127 if (have_int && have_signed && fits_in_type (1, n, int_bits, true))
2128 putithere->typed_val_int.type = parse_type (par_state)->builtin_int;
2129 else if (have_int && have_unsigned && fits_in_type (1, n, int_bits, false))
2130 putithere->typed_val_int.type
2131 = parse_type (par_state)->builtin_unsigned_int;
2132 else if (have_long && have_signed && fits_in_type (1, n, long_bits, true))
2133 putithere->typed_val_int.type = parse_type (par_state)->builtin_long;
2134 else if (have_long && have_unsigned && fits_in_type (1, n, long_bits, false))
2135 putithere->typed_val_int.type
2136 = parse_type (par_state)->builtin_unsigned_long;
2137 else if (have_signed && fits_in_type (1, n, long_long_bits, true))
2138 putithere->typed_val_int.type
2139 = parse_type (par_state)->builtin_long_long;
2140 else if (have_unsigned && fits_in_type (1, n, long_long_bits, false))
2141 putithere->typed_val_int.type
2142 = parse_type (par_state)->builtin_unsigned_long_long;
2143 else
2144 error (_("Numeric constant too large."));
2145 putithere->typed_val_int.val = n;
2147 if (imaginary_p)
2148 putithere->typed_val_int.type
2149 = init_complex_type (nullptr, putithere->typed_val_int.type);
2151 return imaginary_p ? COMPLEX_INT : INT;
2154 /* Temporary obstack used for holding strings. */
2155 static struct obstack tempbuf;
2156 static int tempbuf_init;
2158 /* Parse a C escape sequence. The initial backslash of the sequence
2159 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2160 last character of the sequence. If OUTPUT is not NULL, the
2161 translated form of the escape sequence will be written there. If
2162 OUTPUT is NULL, no output is written and the call will only affect
2163 *PTR. If an escape sequence is expressed in target bytes, then the
2164 entire sequence will simply be copied to OUTPUT. Return 1 if any
2165 character was emitted, 0 otherwise. */
2168 c_parse_escape (const char **ptr, struct obstack *output)
2170 const char *tokptr = *ptr;
2171 int result = 1;
2173 /* Some escape sequences undergo character set conversion. Those we
2174 translate here. */
2175 switch (*tokptr)
2177 /* Hex escapes do not undergo character set conversion, so keep
2178 the escape sequence for later. */
2179 case 'x':
2180 if (output)
2181 obstack_grow_str (output, "\\x");
2182 ++tokptr;
2183 if (!ISXDIGIT (*tokptr))
2184 error (_("\\x escape without a following hex digit"));
2185 while (ISXDIGIT (*tokptr))
2187 if (output)
2188 obstack_1grow (output, *tokptr);
2189 ++tokptr;
2191 break;
2193 /* Octal escapes do not undergo character set conversion, so
2194 keep the escape sequence for later. */
2195 case '0':
2196 case '1':
2197 case '2':
2198 case '3':
2199 case '4':
2200 case '5':
2201 case '6':
2202 case '7':
2204 int i;
2205 if (output)
2206 obstack_grow_str (output, "\\");
2207 for (i = 0;
2208 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
2209 ++i)
2211 if (output)
2212 obstack_1grow (output, *tokptr);
2213 ++tokptr;
2216 break;
2218 /* We handle UCNs later. We could handle them here, but that
2219 would mean a spurious error in the case where the UCN could
2220 be converted to the target charset but not the host
2221 charset. */
2222 case 'u':
2223 case 'U':
2225 char c = *tokptr;
2226 int i, len = c == 'U' ? 8 : 4;
2227 if (output)
2229 obstack_1grow (output, '\\');
2230 obstack_1grow (output, *tokptr);
2232 ++tokptr;
2233 if (!ISXDIGIT (*tokptr))
2234 error (_("\\%c escape without a following hex digit"), c);
2235 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
2237 if (output)
2238 obstack_1grow (output, *tokptr);
2239 ++tokptr;
2242 break;
2244 /* We must pass backslash through so that it does not
2245 cause quoting during the second expansion. */
2246 case '\\':
2247 if (output)
2248 obstack_grow_str (output, "\\\\");
2249 ++tokptr;
2250 break;
2252 /* Escapes which undergo conversion. */
2253 case 'a':
2254 if (output)
2255 obstack_1grow (output, '\a');
2256 ++tokptr;
2257 break;
2258 case 'b':
2259 if (output)
2260 obstack_1grow (output, '\b');
2261 ++tokptr;
2262 break;
2263 case 'f':
2264 if (output)
2265 obstack_1grow (output, '\f');
2266 ++tokptr;
2267 break;
2268 case 'n':
2269 if (output)
2270 obstack_1grow (output, '\n');
2271 ++tokptr;
2272 break;
2273 case 'r':
2274 if (output)
2275 obstack_1grow (output, '\r');
2276 ++tokptr;
2277 break;
2278 case 't':
2279 if (output)
2280 obstack_1grow (output, '\t');
2281 ++tokptr;
2282 break;
2283 case 'v':
2284 if (output)
2285 obstack_1grow (output, '\v');
2286 ++tokptr;
2287 break;
2289 /* GCC extension. */
2290 case 'e':
2291 if (output)
2292 obstack_1grow (output, HOST_ESCAPE_CHAR);
2293 ++tokptr;
2294 break;
2296 /* Backslash-newline expands to nothing at all. */
2297 case '\n':
2298 ++tokptr;
2299 result = 0;
2300 break;
2302 /* A few escapes just expand to the character itself. */
2303 case '\'':
2304 case '\"':
2305 case '?':
2306 /* GCC extensions. */
2307 case '(':
2308 case '{':
2309 case '[':
2310 case '%':
2311 /* Unrecognized escapes turn into the character itself. */
2312 default:
2313 if (output)
2314 obstack_1grow (output, *tokptr);
2315 ++tokptr;
2316 break;
2318 *ptr = tokptr;
2319 return result;
2322 /* Parse a string or character literal from TOKPTR. The string or
2323 character may be wide or unicode. *OUTPTR is set to just after the
2324 end of the literal in the input string. The resulting token is
2325 stored in VALUE. This returns a token value, either STRING or
2326 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2327 number of host characters in the literal. */
2329 static int
2330 parse_string_or_char (const char *tokptr, const char **outptr,
2331 struct typed_stoken *value, int *host_chars)
2333 int quote;
2334 c_string_type type;
2335 int is_objc = 0;
2337 /* Build the gdb internal form of the input string in tempbuf. Note
2338 that the buffer is null byte terminated *only* for the
2339 convenience of debugging gdb itself and printing the buffer
2340 contents when the buffer contains no embedded nulls. Gdb does
2341 not depend upon the buffer being null byte terminated, it uses
2342 the length string instead. This allows gdb to handle C strings
2343 (as well as strings in other languages) with embedded null
2344 bytes */
2346 if (!tempbuf_init)
2347 tempbuf_init = 1;
2348 else
2349 obstack_free (&tempbuf, NULL);
2350 obstack_init (&tempbuf);
2352 /* Record the string type. */
2353 if (*tokptr == 'L')
2355 type = C_WIDE_STRING;
2356 ++tokptr;
2358 else if (*tokptr == 'u')
2360 type = C_STRING_16;
2361 ++tokptr;
2363 else if (*tokptr == 'U')
2365 type = C_STRING_32;
2366 ++tokptr;
2368 else if (*tokptr == '@')
2370 /* An Objective C string. */
2371 is_objc = 1;
2372 type = C_STRING;
2373 ++tokptr;
2375 else
2376 type = C_STRING;
2378 /* Skip the quote. */
2379 quote = *tokptr;
2380 if (quote == '\'')
2381 type |= C_CHAR;
2382 ++tokptr;
2384 *host_chars = 0;
2386 while (*tokptr)
2388 char c = *tokptr;
2389 if (c == '\\')
2391 ++tokptr;
2392 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2394 else if (c == quote)
2395 break;
2396 else
2398 obstack_1grow (&tempbuf, c);
2399 ++tokptr;
2400 /* FIXME: this does the wrong thing with multi-byte host
2401 characters. We could use mbrlen here, but that would
2402 make "set host-charset" a bit less useful. */
2403 ++*host_chars;
2407 if (*tokptr != quote)
2409 if (quote == '"')
2410 error (_("Unterminated string in expression."));
2411 else
2412 error (_("Unmatched single quote."));
2414 ++tokptr;
2416 value->type = type;
2417 value->ptr = (char *) obstack_base (&tempbuf);
2418 value->length = obstack_object_size (&tempbuf);
2420 *outptr = tokptr;
2422 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2425 /* This is used to associate some attributes with a token. */
2427 enum token_flag
2429 /* If this bit is set, the token is C++-only. */
2431 FLAG_CXX = 1,
2433 /* If this bit is set, the token is C-only. */
2435 FLAG_C = 2,
2437 /* If this bit is set, the token is conditional: if there is a
2438 symbol of the same name, then the token is a symbol; otherwise,
2439 the token is a keyword. */
2441 FLAG_SHADOW = 4
2443 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2445 struct c_token
2447 const char *oper;
2448 int token;
2449 enum exp_opcode opcode;
2450 token_flags flags;
2453 static const struct c_token tokentab3[] =
2455 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2456 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2457 {"->*", ARROW_STAR, OP_NULL, FLAG_CXX},
2458 {"...", DOTDOTDOT, OP_NULL, 0}
2461 static const struct c_token tokentab2[] =
2463 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2464 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2465 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2466 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2467 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2468 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2469 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2470 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2471 {"++", INCREMENT, OP_NULL, 0},
2472 {"--", DECREMENT, OP_NULL, 0},
2473 {"->", ARROW, OP_NULL, 0},
2474 {"&&", ANDAND, OP_NULL, 0},
2475 {"||", OROR, OP_NULL, 0},
2476 /* "::" is *not* only C++: gdb overrides its meaning in several
2477 different ways, e.g., 'filename'::func, function::variable. */
2478 {"::", COLONCOLON, OP_NULL, 0},
2479 {"<<", LSH, OP_NULL, 0},
2480 {">>", RSH, OP_NULL, 0},
2481 {"==", EQUAL, OP_NULL, 0},
2482 {"!=", NOTEQUAL, OP_NULL, 0},
2483 {"<=", LEQ, OP_NULL, 0},
2484 {">=", GEQ, OP_NULL, 0},
2485 {".*", DOT_STAR, OP_NULL, FLAG_CXX}
2488 /* Identifier-like tokens. Only type-specifiers than can appear in
2489 multi-word type names (for example 'double' can appear in 'long
2490 double') need to be listed here. type-specifiers that are only ever
2491 single word (like 'char') are handled by the classify_name function. */
2492 static const struct c_token ident_tokens[] =
2494 {"unsigned", UNSIGNED, OP_NULL, 0},
2495 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2496 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2497 {"struct", STRUCT, OP_NULL, 0},
2498 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2499 {"sizeof", SIZEOF, OP_NULL, 0},
2500 {"_Alignof", ALIGNOF, OP_NULL, 0},
2501 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
2502 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2503 {"float", FLOAT_KEYWORD, OP_NULL, 0},
2504 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2505 {"class", CLASS, OP_NULL, FLAG_CXX},
2506 {"union", UNION, OP_NULL, 0},
2507 {"short", SHORT, OP_NULL, 0},
2508 {"const", CONST_KEYWORD, OP_NULL, 0},
2509 {"restrict", RESTRICT, OP_NULL, FLAG_C | FLAG_SHADOW},
2510 {"__restrict__", RESTRICT, OP_NULL, 0},
2511 {"__restrict", RESTRICT, OP_NULL, 0},
2512 {"_Atomic", ATOMIC, OP_NULL, 0},
2513 {"enum", ENUM, OP_NULL, 0},
2514 {"long", LONG, OP_NULL, 0},
2515 {"_Complex", COMPLEX, OP_NULL, 0},
2516 {"__complex__", COMPLEX, OP_NULL, 0},
2518 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2519 {"int", INT_KEYWORD, OP_NULL, 0},
2520 {"new", NEW, OP_NULL, FLAG_CXX},
2521 {"delete", DELETE, OP_NULL, FLAG_CXX},
2522 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2524 {"and", ANDAND, OP_NULL, FLAG_CXX},
2525 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2526 {"bitand", '&', OP_NULL, FLAG_CXX},
2527 {"bitor", '|', OP_NULL, FLAG_CXX},
2528 {"compl", '~', OP_NULL, FLAG_CXX},
2529 {"not", '!', OP_NULL, FLAG_CXX},
2530 {"not_eq", NOTEQUAL, OP_NULL, FLAG_CXX},
2531 {"or", OROR, OP_NULL, FLAG_CXX},
2532 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2533 {"xor", '^', OP_NULL, FLAG_CXX},
2534 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2536 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2537 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2538 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2539 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2541 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2542 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2543 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2544 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2545 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2547 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2551 static void
2552 scan_macro_expansion (const char *expansion)
2554 /* We'd better not be trying to push the stack twice. */
2555 gdb_assert (! cpstate->macro_original_text);
2557 /* Copy to the obstack. */
2558 const char *copy = obstack_strdup (&cpstate->expansion_obstack, expansion);
2560 /* Save the old lexptr value, so we can return to it when we're done
2561 parsing the expanded text. */
2562 cpstate->macro_original_text = pstate->lexptr;
2563 pstate->lexptr = copy;
2566 static int
2567 scanning_macro_expansion (void)
2569 return cpstate->macro_original_text != 0;
2572 static void
2573 finished_macro_expansion (void)
2575 /* There'd better be something to pop back to. */
2576 gdb_assert (cpstate->macro_original_text);
2578 /* Pop back to the original text. */
2579 pstate->lexptr = cpstate->macro_original_text;
2580 cpstate->macro_original_text = 0;
2583 /* Return true iff the token represents a C++ cast operator. */
2585 static int
2586 is_cast_operator (const char *token, int len)
2588 return (! strncmp (token, "dynamic_cast", len)
2589 || ! strncmp (token, "static_cast", len)
2590 || ! strncmp (token, "reinterpret_cast", len)
2591 || ! strncmp (token, "const_cast", len));
2594 /* The scope used for macro expansion. */
2595 static struct macro_scope *expression_macro_scope;
2597 /* This is set if a NAME token appeared at the very end of the input
2598 string, with no whitespace separating the name from the EOF. This
2599 is used only when parsing to do field name completion. */
2600 static int saw_name_at_eof;
2602 /* This is set if the previously-returned token was a structure
2603 operator -- either '.' or ARROW. */
2604 static bool last_was_structop;
2606 /* Depth of parentheses. */
2607 static int paren_depth;
2609 /* Read one token, getting characters through lexptr. */
2611 static int
2612 lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
2614 int c;
2615 int namelen;
2616 const char *tokstart;
2617 bool saw_structop = last_was_structop;
2619 last_was_structop = false;
2620 *is_quoted_name = false;
2622 retry:
2624 /* Check if this is a macro invocation that we need to expand. */
2625 if (! scanning_macro_expansion ())
2627 gdb::unique_xmalloc_ptr<char> expanded
2628 = macro_expand_next (&pstate->lexptr, *expression_macro_scope);
2630 if (expanded != nullptr)
2631 scan_macro_expansion (expanded.get ());
2634 pstate->prev_lexptr = pstate->lexptr;
2636 tokstart = pstate->lexptr;
2637 /* See if it is a special token of length 3. */
2638 for (const auto &token : tokentab3)
2639 if (strncmp (tokstart, token.oper, 3) == 0)
2641 if ((token.flags & FLAG_CXX) != 0
2642 && par_state->language ()->la_language != language_cplus)
2643 break;
2644 gdb_assert ((token.flags & FLAG_C) == 0);
2646 pstate->lexptr += 3;
2647 yylval.opcode = token.opcode;
2648 return token.token;
2651 /* See if it is a special token of length 2. */
2652 for (const auto &token : tokentab2)
2653 if (strncmp (tokstart, token.oper, 2) == 0)
2655 if ((token.flags & FLAG_CXX) != 0
2656 && par_state->language ()->la_language != language_cplus)
2657 break;
2658 gdb_assert ((token.flags & FLAG_C) == 0);
2660 pstate->lexptr += 2;
2661 yylval.opcode = token.opcode;
2662 if (token.token == ARROW)
2663 last_was_structop = 1;
2664 return token.token;
2667 switch (c = *tokstart)
2669 case 0:
2670 /* If we were just scanning the result of a macro expansion,
2671 then we need to resume scanning the original text.
2672 If we're parsing for field name completion, and the previous
2673 token allows such completion, return a COMPLETE token.
2674 Otherwise, we were already scanning the original text, and
2675 we're really done. */
2676 if (scanning_macro_expansion ())
2678 finished_macro_expansion ();
2679 goto retry;
2681 else if (saw_name_at_eof)
2683 saw_name_at_eof = 0;
2684 return COMPLETE;
2686 else if (par_state->parse_completion && saw_structop)
2687 return COMPLETE;
2688 else
2689 return 0;
2691 case ' ':
2692 case '\t':
2693 case '\n':
2694 pstate->lexptr++;
2695 goto retry;
2697 case '[':
2698 case '(':
2699 paren_depth++;
2700 pstate->lexptr++;
2701 if (par_state->language ()->la_language == language_objc
2702 && c == '[')
2703 return OBJC_LBRAC;
2704 return c;
2706 case ']':
2707 case ')':
2708 if (paren_depth == 0)
2709 return 0;
2710 paren_depth--;
2711 pstate->lexptr++;
2712 return c;
2714 case ',':
2715 if (pstate->comma_terminates
2716 && paren_depth == 0
2717 && ! scanning_macro_expansion ())
2718 return 0;
2719 pstate->lexptr++;
2720 return c;
2722 case '.':
2723 /* Might be a floating point number. */
2724 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
2726 last_was_structop = true;
2727 goto symbol; /* Nope, must be a symbol. */
2729 [[fallthrough]];
2731 case '0':
2732 case '1':
2733 case '2':
2734 case '3':
2735 case '4':
2736 case '5':
2737 case '6':
2738 case '7':
2739 case '8':
2740 case '9':
2742 /* It's a number. */
2743 int got_dot = 0, got_e = 0, got_p = 0, toktype;
2744 const char *p = tokstart;
2745 int hex = input_radix > 10;
2747 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2749 p += 2;
2750 hex = 1;
2752 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2754 p += 2;
2755 hex = 0;
2758 /* If the token includes the C++14 digits separator, we make a
2759 copy so that we don't have to handle the separator in
2760 parse_number. */
2761 std::optional<std::string> no_tick;
2762 for (;; ++p)
2764 /* This test includes !hex because 'e' is a valid hex digit
2765 and thus does not indicate a floating point number when
2766 the radix is hex. */
2767 if (!hex && !got_e && !got_p && (*p == 'e' || *p == 'E'))
2768 got_dot = got_e = 1;
2769 else if (!got_e && !got_p && (*p == 'p' || *p == 'P'))
2770 got_dot = got_p = 1;
2771 /* This test does not include !hex, because a '.' always indicates
2772 a decimal floating point number regardless of the radix. */
2773 else if (!got_dot && *p == '.')
2774 got_dot = 1;
2775 else if (((got_e && (p[-1] == 'e' || p[-1] == 'E'))
2776 || (got_p && (p[-1] == 'p' || p[-1] == 'P')))
2777 && (*p == '-' || *p == '+'))
2779 /* This is the sign of the exponent, not the end of
2780 the number. */
2782 else if (*p == '\'')
2784 if (!no_tick.has_value ())
2785 no_tick.emplace (tokstart, p);
2786 continue;
2788 /* We will take any letters or digits. parse_number will
2789 complain if past the radix, or if L or U are not final. */
2790 else if ((*p < '0' || *p > '9')
2791 && ((*p < 'a' || *p > 'z')
2792 && (*p < 'A' || *p > 'Z')))
2793 break;
2794 if (no_tick.has_value ())
2795 no_tick->push_back (*p);
2797 if (no_tick.has_value ())
2798 toktype = parse_number (par_state, no_tick->c_str (),
2799 no_tick->length (),
2800 got_dot | got_e | got_p, &yylval);
2801 else
2802 toktype = parse_number (par_state, tokstart, p - tokstart,
2803 got_dot | got_e | got_p, &yylval);
2804 if (toktype == ERROR)
2805 error (_("Invalid number \"%.*s\"."), (int) (p - tokstart),
2806 tokstart);
2807 pstate->lexptr = p;
2808 return toktype;
2811 case '@':
2813 const char *p = &tokstart[1];
2815 if (par_state->language ()->la_language == language_objc)
2817 size_t len = strlen ("selector");
2819 if (strncmp (p, "selector", len) == 0
2820 && (p[len] == '\0' || ISSPACE (p[len])))
2822 pstate->lexptr = p + len;
2823 return SELECTOR;
2825 else if (*p == '"')
2826 goto parse_string;
2829 while (ISSPACE (*p))
2830 p++;
2831 size_t len = strlen ("entry");
2832 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
2833 && p[len] != '_')
2835 pstate->lexptr = &p[len];
2836 return ENTRY;
2839 [[fallthrough]];
2840 case '+':
2841 case '-':
2842 case '*':
2843 case '/':
2844 case '%':
2845 case '|':
2846 case '&':
2847 case '^':
2848 case '~':
2849 case '!':
2850 case '<':
2851 case '>':
2852 case '?':
2853 case ':':
2854 case '=':
2855 case '{':
2856 case '}':
2857 symbol:
2858 pstate->lexptr++;
2859 return c;
2861 case 'L':
2862 case 'u':
2863 case 'U':
2864 if (tokstart[1] != '"' && tokstart[1] != '\'')
2865 break;
2866 [[fallthrough]];
2867 case '\'':
2868 case '"':
2870 parse_string:
2872 int host_len;
2873 int result = parse_string_or_char (tokstart, &pstate->lexptr,
2874 &yylval.tsval, &host_len);
2875 if (result == CHAR)
2877 if (host_len == 0)
2878 error (_("Empty character constant."));
2879 else if (host_len > 2 && c == '\'')
2881 ++tokstart;
2882 namelen = pstate->lexptr - tokstart - 1;
2883 *is_quoted_name = true;
2885 goto tryname;
2887 else if (host_len > 1)
2888 error (_("Invalid character constant."));
2890 return result;
2894 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
2895 /* We must have come across a bad character (e.g. ';'). */
2896 error (_("Invalid character '%c' in expression."), c);
2898 /* It's a name. See how long it is. */
2899 namelen = 0;
2900 for (c = tokstart[namelen];
2901 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
2903 /* Template parameter lists are part of the name.
2904 FIXME: This mishandles `print $a<4&&$a>3'. */
2906 if (c == '<')
2908 if (! is_cast_operator (tokstart, namelen))
2910 /* Scan ahead to get rest of the template specification. Note
2911 that we look ahead only when the '<' adjoins non-whitespace
2912 characters; for comparison expressions, e.g. "a < b > c",
2913 there must be spaces before the '<', etc. */
2914 const char *p = find_template_name_end (tokstart + namelen);
2916 if (p)
2917 namelen = p - tokstart;
2919 break;
2921 c = tokstart[++namelen];
2924 /* The token "if" terminates the expression and is NOT removed from
2925 the input stream. It doesn't count if it appears in the
2926 expansion of a macro. */
2927 if (namelen == 2
2928 && tokstart[0] == 'i'
2929 && tokstart[1] == 'f'
2930 && ! scanning_macro_expansion ())
2932 return 0;
2935 /* For the same reason (breakpoint conditions), "thread N"
2936 terminates the expression. "thread" could be an identifier, but
2937 an identifier is never followed by a number without intervening
2938 punctuation. "task" is similar. Handle abbreviations of these,
2939 similarly to breakpoint.c:find_condition_and_thread. */
2940 if (namelen >= 1
2941 && (strncmp (tokstart, "thread", namelen) == 0
2942 || strncmp (tokstart, "task", namelen) == 0)
2943 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2944 && ! scanning_macro_expansion ())
2946 const char *p = tokstart + namelen + 1;
2948 while (*p == ' ' || *p == '\t')
2949 p++;
2950 if (*p >= '0' && *p <= '9')
2951 return 0;
2954 pstate->lexptr += namelen;
2956 tryname:
2958 yylval.sval.ptr = tokstart;
2959 yylval.sval.length = namelen;
2961 /* Catch specific keywords. */
2962 std::string copy = copy_name (yylval.sval);
2963 for (const auto &token : ident_tokens)
2964 if (copy == token.oper)
2966 if ((token.flags & FLAG_CXX) != 0
2967 && par_state->language ()->la_language != language_cplus)
2968 break;
2969 if ((token.flags & FLAG_C) != 0
2970 && par_state->language ()->la_language != language_c
2971 && par_state->language ()->la_language != language_objc)
2972 break;
2974 if ((token.flags & FLAG_SHADOW) != 0)
2976 struct field_of_this_result is_a_field_of_this;
2978 if (lookup_symbol (copy.c_str (),
2979 pstate->expression_context_block,
2980 SEARCH_VFT,
2981 (par_state->language ()->la_language
2982 == language_cplus ? &is_a_field_of_this
2983 : NULL)).symbol
2984 != NULL)
2986 /* The keyword is shadowed. */
2987 break;
2991 /* It is ok to always set this, even though we don't always
2992 strictly need to. */
2993 yylval.opcode = token.opcode;
2994 return token.token;
2997 if (*tokstart == '$')
2998 return DOLLAR_VARIABLE;
3000 if (pstate->parse_completion && *pstate->lexptr == '\0')
3001 saw_name_at_eof = 1;
3003 yylval.ssym.stoken = yylval.sval;
3004 yylval.ssym.sym.symbol = NULL;
3005 yylval.ssym.sym.block = NULL;
3006 yylval.ssym.is_a_field_of_this = 0;
3007 return NAME;
3010 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
3011 struct c_token_and_value
3013 int token;
3014 YYSTYPE value;
3017 /* A FIFO of tokens that have been read but not yet returned to the
3018 parser. */
3019 static std::vector<c_token_and_value> token_fifo;
3021 /* Non-zero if the lexer should return tokens from the FIFO. */
3022 static int popping;
3024 /* Temporary storage for c_lex; this holds symbol names as they are
3025 built up. */
3026 static auto_obstack name_obstack;
3028 /* Classify a NAME token. The contents of the token are in `yylval'.
3029 Updates yylval and returns the new token type. BLOCK is the block
3030 in which lookups start; this can be NULL to mean the global scope.
3031 IS_QUOTED_NAME is non-zero if the name token was originally quoted
3032 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
3033 a structure operator -- either '.' or ARROW */
3035 static int
3036 classify_name (struct parser_state *par_state, const struct block *block,
3037 bool is_quoted_name, bool is_after_structop)
3039 struct block_symbol bsym;
3040 struct field_of_this_result is_a_field_of_this;
3042 std::string copy = copy_name (yylval.sval);
3044 /* Initialize this in case we *don't* use it in this call; that way
3045 we can refer to it unconditionally below. */
3046 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
3048 bsym = lookup_symbol (copy.c_str (), block, SEARCH_VFT,
3049 par_state->language ()->name_of_this ()
3050 ? &is_a_field_of_this : NULL);
3052 if (bsym.symbol && bsym.symbol->aclass () == LOC_BLOCK)
3054 yylval.ssym.sym = bsym;
3055 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3056 return BLOCKNAME;
3058 else if (!bsym.symbol)
3060 /* If we found a field of 'this', we might have erroneously
3061 found a constructor where we wanted a type name. Handle this
3062 case by noticing that we found a constructor and then look up
3063 the type tag instead. */
3064 if (is_a_field_of_this.type != NULL
3065 && is_a_field_of_this.fn_field != NULL
3066 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
3069 struct field_of_this_result inner_is_a_field_of_this;
3071 bsym = lookup_symbol (copy.c_str (), block, SEARCH_STRUCT_DOMAIN,
3072 &inner_is_a_field_of_this);
3073 if (bsym.symbol != NULL)
3075 yylval.tsym.type = bsym.symbol->type ();
3076 return TYPENAME;
3080 /* If we found a field on the "this" object, or we are looking
3081 up a field on a struct, then we want to prefer it over a
3082 filename. However, if the name was quoted, then it is better
3083 to check for a filename or a block, since this is the only
3084 way the user has of requiring the extension to be used. */
3085 if ((is_a_field_of_this.type == NULL && !is_after_structop)
3086 || is_quoted_name)
3088 /* See if it's a file name. */
3089 struct symtab *symtab;
3091 symtab = lookup_symtab (copy.c_str ());
3092 if (symtab)
3094 yylval.bval
3095 = symtab->compunit ()->blockvector ()->static_block ();
3097 return FILENAME;
3102 if (bsym.symbol && bsym.symbol->aclass () == LOC_TYPEDEF)
3104 yylval.tsym.type = bsym.symbol->type ();
3105 return TYPENAME;
3108 /* See if it's an ObjC classname. */
3109 if (par_state->language ()->la_language == language_objc && !bsym.symbol)
3111 CORE_ADDR Class = lookup_objc_class (par_state->gdbarch (),
3112 copy.c_str ());
3113 if (Class)
3115 struct symbol *sym;
3117 yylval.theclass.theclass = Class;
3118 sym = lookup_struct_typedef (copy.c_str (),
3119 par_state->expression_context_block, 1);
3120 if (sym)
3121 yylval.theclass.type = sym->type ();
3122 return CLASSNAME;
3126 /* Input names that aren't symbols but ARE valid hex numbers, when
3127 the input radix permits them, can be names or numbers depending
3128 on the parse. Note we support radixes > 16 here. */
3129 if (!bsym.symbol
3130 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3131 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3133 YYSTYPE newlval; /* Its value is ignored. */
3134 int hextype = parse_number (par_state, copy.c_str (), yylval.sval.length,
3135 0, &newlval);
3137 if (hextype == INT)
3139 yylval.ssym.sym = bsym;
3140 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3141 return NAME_OR_INT;
3145 /* Any other kind of symbol */
3146 yylval.ssym.sym = bsym;
3147 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3149 if (bsym.symbol == NULL
3150 && par_state->language ()->la_language == language_cplus
3151 && is_a_field_of_this.type == NULL
3152 && lookup_minimal_symbol (copy.c_str (), NULL, NULL).minsym == NULL)
3153 return UNKNOWN_CPP_NAME;
3155 return NAME;
3158 /* Like classify_name, but used by the inner loop of the lexer, when a
3159 name might have already been seen. CONTEXT is the context type, or
3160 NULL if this is the first component of a name. */
3162 static int
3163 classify_inner_name (struct parser_state *par_state,
3164 const struct block *block, struct type *context)
3166 struct type *type;
3168 if (context == NULL)
3169 return classify_name (par_state, block, false, false);
3171 type = check_typedef (context);
3172 if (!type_aggregate_p (type))
3173 return ERROR;
3175 std::string copy = copy_name (yylval.ssym.stoken);
3176 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3177 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy.c_str (), block,
3178 SEARCH_VFT);
3180 /* If no symbol was found, search for a matching base class named
3181 COPY. This will allow users to enter qualified names of class members
3182 relative to the `this' pointer. */
3183 if (yylval.ssym.sym.symbol == NULL)
3185 struct type *base_type = cp_find_type_baseclass_by_name (type,
3186 copy.c_str ());
3188 if (base_type != NULL)
3190 yylval.tsym.type = base_type;
3191 return TYPENAME;
3194 return ERROR;
3197 switch (yylval.ssym.sym.symbol->aclass ())
3199 case LOC_BLOCK:
3200 case LOC_LABEL:
3201 /* cp_lookup_nested_symbol might have accidentally found a constructor
3202 named COPY when we really wanted a base class of the same name.
3203 Double-check this case by looking for a base class. */
3205 struct type *base_type
3206 = cp_find_type_baseclass_by_name (type, copy.c_str ());
3208 if (base_type != NULL)
3210 yylval.tsym.type = base_type;
3211 return TYPENAME;
3214 return ERROR;
3216 case LOC_TYPEDEF:
3217 yylval.tsym.type = yylval.ssym.sym.symbol->type ();
3218 return TYPENAME;
3220 default:
3221 return NAME;
3223 internal_error (_("not reached"));
3226 /* The outer level of a two-level lexer. This calls the inner lexer
3227 to return tokens. It then either returns these tokens, or
3228 aggregates them into a larger token. This lets us work around a
3229 problem in our parsing approach, where the parser could not
3230 distinguish between qualified names and qualified types at the
3231 right point.
3233 This approach is still not ideal, because it mishandles template
3234 types. See the comment in lex_one_token for an example. However,
3235 this is still an improvement over the earlier approach, and will
3236 suffice until we move to better parsing technology. */
3238 static int
3239 yylex (void)
3241 c_token_and_value current;
3242 int first_was_coloncolon, last_was_coloncolon;
3243 struct type *context_type = NULL;
3244 int last_to_examine, next_to_examine, checkpoint;
3245 const struct block *search_block;
3246 bool is_quoted_name, last_lex_was_structop;
3248 if (popping && !token_fifo.empty ())
3249 goto do_pop;
3250 popping = 0;
3252 last_lex_was_structop = last_was_structop;
3254 /* Read the first token and decide what to do. Most of the
3255 subsequent code is C++-only; but also depends on seeing a "::" or
3256 name-like token. */
3257 current.token = lex_one_token (pstate, &is_quoted_name);
3258 if (current.token == NAME)
3259 current.token = classify_name (pstate, pstate->expression_context_block,
3260 is_quoted_name, last_lex_was_structop);
3261 if (pstate->language ()->la_language != language_cplus
3262 || (current.token != TYPENAME && current.token != COLONCOLON
3263 && current.token != FILENAME))
3264 return current.token;
3266 /* Read any sequence of alternating "::" and name-like tokens into
3267 the token FIFO. */
3268 current.value = yylval;
3269 token_fifo.push_back (current);
3270 last_was_coloncolon = current.token == COLONCOLON;
3271 while (1)
3273 bool ignore;
3275 /* We ignore quoted names other than the very first one.
3276 Subsequent ones do not have any special meaning. */
3277 current.token = lex_one_token (pstate, &ignore);
3278 current.value = yylval;
3279 token_fifo.push_back (current);
3281 if ((last_was_coloncolon && current.token != NAME)
3282 || (!last_was_coloncolon && current.token != COLONCOLON))
3283 break;
3284 last_was_coloncolon = !last_was_coloncolon;
3286 popping = 1;
3288 /* We always read one extra token, so compute the number of tokens
3289 to examine accordingly. */
3290 last_to_examine = token_fifo.size () - 2;
3291 next_to_examine = 0;
3293 current = token_fifo[next_to_examine];
3294 ++next_to_examine;
3296 name_obstack.clear ();
3297 checkpoint = 0;
3298 if (current.token == FILENAME)
3299 search_block = current.value.bval;
3300 else if (current.token == COLONCOLON)
3301 search_block = NULL;
3302 else
3304 gdb_assert (current.token == TYPENAME);
3305 search_block = pstate->expression_context_block;
3306 obstack_grow (&name_obstack, current.value.sval.ptr,
3307 current.value.sval.length);
3308 context_type = current.value.tsym.type;
3309 checkpoint = 1;
3312 first_was_coloncolon = current.token == COLONCOLON;
3313 last_was_coloncolon = first_was_coloncolon;
3315 while (next_to_examine <= last_to_examine)
3317 c_token_and_value next;
3319 next = token_fifo[next_to_examine];
3320 ++next_to_examine;
3322 if (next.token == NAME && last_was_coloncolon)
3324 int classification;
3326 yylval = next.value;
3327 classification = classify_inner_name (pstate, search_block,
3328 context_type);
3329 /* We keep going until we either run out of names, or until
3330 we have a qualified name which is not a type. */
3331 if (classification != TYPENAME && classification != NAME)
3332 break;
3334 /* Accept up to this token. */
3335 checkpoint = next_to_examine;
3337 /* Update the partial name we are constructing. */
3338 if (context_type != NULL)
3340 /* We don't want to put a leading "::" into the name. */
3341 obstack_grow_str (&name_obstack, "::");
3343 obstack_grow (&name_obstack, next.value.sval.ptr,
3344 next.value.sval.length);
3346 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
3347 yylval.sval.length = obstack_object_size (&name_obstack);
3348 current.value = yylval;
3349 current.token = classification;
3351 last_was_coloncolon = 0;
3353 if (classification == NAME)
3354 break;
3356 context_type = yylval.tsym.type;
3358 else if (next.token == COLONCOLON && !last_was_coloncolon)
3359 last_was_coloncolon = 1;
3360 else
3362 /* We've reached the end of the name. */
3363 break;
3367 /* If we have a replacement token, install it as the first token in
3368 the FIFO, and delete the other constituent tokens. */
3369 if (checkpoint > 0)
3371 current.value.sval.ptr
3372 = obstack_strndup (&cpstate->expansion_obstack,
3373 current.value.sval.ptr,
3374 current.value.sval.length);
3376 token_fifo[0] = current;
3377 if (checkpoint > 1)
3378 token_fifo.erase (token_fifo.begin () + 1,
3379 token_fifo.begin () + checkpoint);
3382 do_pop:
3383 current = token_fifo[0];
3384 token_fifo.erase (token_fifo.begin ());
3385 yylval = current.value;
3386 return current.token;
3390 c_parse (struct parser_state *par_state)
3392 /* Setting up the parser state. */
3393 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3394 gdb_assert (par_state != NULL);
3395 pstate = par_state;
3397 c_parse_state cstate;
3398 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3400 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
3402 if (par_state->expression_context_block)
3403 macro_scope
3404 = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
3405 else
3406 macro_scope = default_macro_scope ();
3407 if (! macro_scope)
3408 macro_scope = user_macro_scope ();
3410 scoped_restore restore_macro_scope
3411 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
3413 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3414 par_state->debug);
3416 /* Initialize some state used by the lexer. */
3417 last_was_structop = false;
3418 saw_name_at_eof = 0;
3419 paren_depth = 0;
3421 token_fifo.clear ();
3422 popping = 0;
3423 name_obstack.clear ();
3425 int result = yyparse ();
3426 if (!result)
3427 pstate->set_operation (pstate->pop ());
3428 return result;
3431 #if defined(YYBISON) && YYBISON < 30800
3434 /* This is called via the YYPRINT macro when parser debugging is
3435 enabled. It prints a token's value. */
3437 static void
3438 c_print_token (FILE *file, int type, YYSTYPE value)
3440 switch (type)
3442 case INT:
3443 parser_fprintf (file, "typed_val_int<%s, %s>",
3444 TYPE_SAFE_NAME (value.typed_val_int.type),
3445 pulongest (value.typed_val_int.val));
3446 break;
3448 case CHAR:
3449 case STRING:
3450 parser_fprintf (file, "tsval<type=%d, %.*s>", value.tsval.type,
3451 value.tsval.length, value.tsval.ptr);
3452 break;
3454 case NSSTRING:
3455 case DOLLAR_VARIABLE:
3456 parser_fprintf (file, "sval<%s>", copy_name (value.sval).c_str ());
3457 break;
3459 case TYPENAME:
3460 parser_fprintf (file, "tsym<type=%s, name=%s>",
3461 TYPE_SAFE_NAME (value.tsym.type),
3462 copy_name (value.tsym.stoken).c_str ());
3463 break;
3465 case NAME:
3466 case UNKNOWN_CPP_NAME:
3467 case NAME_OR_INT:
3468 case BLOCKNAME:
3469 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3470 copy_name (value.ssym.stoken).c_str (),
3471 (value.ssym.sym.symbol == NULL
3472 ? "(null)" : value.ssym.sym.symbol->print_name ()),
3473 value.ssym.is_a_field_of_this);
3474 break;
3476 case FILENAME:
3477 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3478 break;
3482 #endif
3484 static void
3485 yyerror (const char *msg)
3487 pstate->parse_error (msg);