RISC-V: Support SiFive extensions: xsfvqmaccdod, xsfvqmaccqoq and xsfvfnrclipxfqf
[binutils-gdb.git] / gdb / c-exp.y
blob645cb59b878142efb06174733bdfe276c897b2ed
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_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."));
1219 else
1220 error (_("No symbol \"%s\" in current context."),
1221 arg.c_str ());
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)
1235 : NULL);
1236 if (alias_target != NULL)
1238 block_symbol bsym { alias_target,
1239 alias_target->value_block () };
1240 pstate->push_new<var_value_operation> (bsym);
1242 else
1243 pstate->push_new<var_msym_value_operation>
1244 (msymbol);
1249 const_or_volatile: const_or_volatile_noopt
1253 single_qualifier:
1254 CONST_KEYWORD
1255 { cpstate->type_stack.insert (tp_const); }
1256 | VOLATILE_KEYWORD
1257 { cpstate->type_stack.insert (tp_volatile); }
1258 | ATOMIC
1259 { cpstate->type_stack.insert (tp_atomic); }
1260 | RESTRICT
1261 { cpstate->type_stack.insert (tp_restrict); }
1262 | '@' NAME
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:
1275 single_qualifier
1276 | qualifier_seq_noopt single_qualifier
1279 qualifier_seq:
1280 qualifier_seq_noopt
1284 ptr_operator:
1285 ptr_operator '*'
1286 { cpstate->type_stack.insert (tp_pointer); }
1287 qualifier_seq
1288 | '*'
1289 { cpstate->type_stack.insert (tp_pointer); }
1290 qualifier_seq
1291 | '&'
1292 { cpstate->type_stack.insert (tp_reference); }
1293 | '&' ptr_operator
1294 { cpstate->type_stack.insert (tp_reference); }
1295 | ANDAND
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); }
1310 | ptr_operator_ts
1311 | direct_abs_decl
1314 direct_abs_decl: '(' abs_decl ')'
1315 { $$ = $2; }
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 ($$);
1324 | array_mod
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 ($$);
1339 | func_mod
1341 cpstate->type_stack.push ($1);
1342 $$ = cpstate->type_stack.create ();
1343 cpstate->type_stacks.emplace_back ($$);
1347 array_mod: '[' ']'
1348 { $$ = -1; }
1349 | OBJC_LBRAC ']'
1350 { $$ = -1; }
1351 | '[' INT ']'
1352 { $$ = $2.val; }
1353 | OBJC_LBRAC INT ']'
1354 { $$ = $2.val; }
1357 func_mod: '(' ')'
1359 $$ = new std::vector<struct type *>;
1360 cpstate->type_lists.emplace_back ($$);
1362 | '(' parameter_typelist ')'
1363 { $$ = $2; }
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! */
1374 type : ptype
1377 /* A helper production that recognizes scalar types that can validly
1378 be used with _Complex. */
1380 scalar_type:
1381 INT_KEYWORD
1382 { $$ = lookup_signed_typename (pstate->language (),
1383 "int"); }
1384 | LONG
1385 { $$ = lookup_signed_typename (pstate->language (),
1386 "long"); }
1387 | SHORT
1388 { $$ = lookup_signed_typename (pstate->language (),
1389 "short"); }
1390 | LONG INT_KEYWORD
1391 { $$ = lookup_signed_typename (pstate->language (),
1392 "long"); }
1393 | LONG SIGNED_KEYWORD INT_KEYWORD
1394 { $$ = lookup_signed_typename (pstate->language (),
1395 "long"); }
1396 | LONG SIGNED_KEYWORD
1397 { $$ = lookup_signed_typename (pstate->language (),
1398 "long"); }
1399 | SIGNED_KEYWORD LONG INT_KEYWORD
1400 { $$ = lookup_signed_typename (pstate->language (),
1401 "long"); }
1402 | UNSIGNED LONG INT_KEYWORD
1403 { $$ = lookup_unsigned_typename (pstate->language (),
1404 "long"); }
1405 | LONG UNSIGNED INT_KEYWORD
1406 { $$ = lookup_unsigned_typename (pstate->language (),
1407 "long"); }
1408 | LONG UNSIGNED
1409 { $$ = lookup_unsigned_typename (pstate->language (),
1410 "long"); }
1411 | LONG LONG
1412 { $$ = lookup_signed_typename (pstate->language (),
1413 "long long"); }
1414 | LONG LONG INT_KEYWORD
1415 { $$ = lookup_signed_typename (pstate->language (),
1416 "long long"); }
1417 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1418 { $$ = lookup_signed_typename (pstate->language (),
1419 "long long"); }
1420 | LONG LONG SIGNED_KEYWORD
1421 { $$ = lookup_signed_typename (pstate->language (),
1422 "long long"); }
1423 | SIGNED_KEYWORD LONG LONG
1424 { $$ = lookup_signed_typename (pstate->language (),
1425 "long long"); }
1426 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1427 { $$ = lookup_signed_typename (pstate->language (),
1428 "long long"); }
1429 | UNSIGNED LONG LONG
1430 { $$ = lookup_unsigned_typename (pstate->language (),
1431 "long long"); }
1432 | UNSIGNED LONG LONG INT_KEYWORD
1433 { $$ = lookup_unsigned_typename (pstate->language (),
1434 "long long"); }
1435 | LONG LONG UNSIGNED
1436 { $$ = lookup_unsigned_typename (pstate->language (),
1437 "long long"); }
1438 | LONG LONG UNSIGNED INT_KEYWORD
1439 { $$ = lookup_unsigned_typename (pstate->language (),
1440 "long long"); }
1441 | SHORT INT_KEYWORD
1442 { $$ = lookup_signed_typename (pstate->language (),
1443 "short"); }
1444 | SHORT SIGNED_KEYWORD INT_KEYWORD
1445 { $$ = lookup_signed_typename (pstate->language (),
1446 "short"); }
1447 | SHORT SIGNED_KEYWORD
1448 { $$ = lookup_signed_typename (pstate->language (),
1449 "short"); }
1450 | UNSIGNED SHORT INT_KEYWORD
1451 { $$ = lookup_unsigned_typename (pstate->language (),
1452 "short"); }
1453 | SHORT UNSIGNED
1454 { $$ = lookup_unsigned_typename (pstate->language (),
1455 "short"); }
1456 | SHORT UNSIGNED INT_KEYWORD
1457 { $$ = lookup_unsigned_typename (pstate->language (),
1458 "short"); }
1459 | DOUBLE_KEYWORD
1460 { $$ = lookup_typename (pstate->language (),
1461 "double",
1462 NULL,
1463 0); }
1464 | FLOAT_KEYWORD
1465 { $$ = lookup_typename (pstate->language (),
1466 "float",
1467 NULL,
1468 0); }
1469 | LONG DOUBLE_KEYWORD
1470 { $$ = lookup_typename (pstate->language (),
1471 "long double",
1472 NULL,
1473 0); }
1474 | UNSIGNED type_name
1475 { $$ = lookup_unsigned_typename (pstate->language (),
1476 $2.type->name ()); }
1477 | UNSIGNED
1478 { $$ = lookup_unsigned_typename (pstate->language (),
1479 "int"); }
1480 | SIGNED_KEYWORD type_name
1481 { $$ = lookup_signed_typename (pstate->language (),
1482 $2.type->name ()); }
1483 | SIGNED_KEYWORD
1484 { $$ = lookup_signed_typename (pstate->language (),
1485 "int"); }
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
1497 recognised here. */
1498 typebase
1499 : TYPENAME
1500 { $$ = $1.type; }
1501 | scalar_type
1502 { $$ = $1; }
1503 | COMPLEX scalar_type
1505 $$ = init_complex_type (nullptr, $2);
1507 | STRUCT name
1508 { $$
1509 = lookup_struct (copy_name ($2).c_str (),
1510 pstate->expression_context_block);
1512 | STRUCT COMPLETE
1514 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1515 "", 0);
1516 $$ = NULL;
1518 | STRUCT name COMPLETE
1520 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1521 $2.ptr, $2.length);
1522 $$ = NULL;
1524 | CLASS name
1525 { $$ = lookup_struct
1526 (copy_name ($2).c_str (),
1527 pstate->expression_context_block);
1529 | CLASS COMPLETE
1531 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1532 "", 0);
1533 $$ = NULL;
1535 | CLASS name COMPLETE
1537 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1538 $2.ptr, $2.length);
1539 $$ = NULL;
1541 | UNION name
1542 { $$
1543 = lookup_union (copy_name ($2).c_str (),
1544 pstate->expression_context_block);
1546 | UNION COMPLETE
1548 pstate->mark_completion_tag (TYPE_CODE_UNION,
1549 "", 0);
1550 $$ = NULL;
1552 | UNION name COMPLETE
1554 pstate->mark_completion_tag (TYPE_CODE_UNION,
1555 $2.ptr, $2.length);
1556 $$ = NULL;
1558 | ENUM name
1559 { $$ = lookup_enum (copy_name ($2).c_str (),
1560 pstate->expression_context_block);
1562 | ENUM COMPLETE
1564 pstate->mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1565 $$ = NULL;
1567 | ENUM name COMPLETE
1569 pstate->mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1570 $2.length);
1571 $$ = NULL;
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); }
1587 type_name: TYPENAME
1588 | INT_KEYWORD
1590 $$.stoken.ptr = "int";
1591 $$.stoken.length = 3;
1592 $$.type = lookup_signed_typename (pstate->language (),
1593 "int");
1595 | LONG
1597 $$.stoken.ptr = "long";
1598 $$.stoken.length = 4;
1599 $$.type = lookup_signed_typename (pstate->language (),
1600 "long");
1602 | SHORT
1604 $$.stoken.ptr = "short";
1605 $$.stoken.length = 5;
1606 $$.type = lookup_signed_typename (pstate->language (),
1607 "short");
1611 parameter_typelist:
1612 nonempty_typelist
1613 { check_parameter_typelist ($1); }
1614 | nonempty_typelist ',' DOTDOTDOT
1616 $1->push_back (NULL);
1617 check_parameter_typelist ($1);
1618 $$ = $1;
1622 nonempty_typelist
1623 : type
1625 std::vector<struct type *> *typelist
1626 = new std::vector<struct type *>;
1627 cpstate->type_lists.emplace_back (typelist);
1629 typelist->push_back ($1);
1630 $$ = typelist;
1632 | nonempty_typelist ',' type
1634 $1->push_back ($3);
1635 $$ = $1;
1639 ptype : typebase
1640 | ptype abs_decl
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);
1663 | CONST_KEYWORD
1664 { cpstate->type_stack.insert (tp_const); }
1665 | VOLATILE_KEYWORD
1666 { cpstate->type_stack.insert (tp_volatile); }
1669 oper: OPERATOR NEW
1670 { $$ = operator_stoken (" new"); }
1671 | OPERATOR DELETE
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[]"); }
1681 | OPERATOR '+'
1682 { $$ = operator_stoken ("+"); }
1683 | OPERATOR '-'
1684 { $$ = operator_stoken ("-"); }
1685 | OPERATOR '*'
1686 { $$ = operator_stoken ("*"); }
1687 | OPERATOR '/'
1688 { $$ = operator_stoken ("/"); }
1689 | OPERATOR '%'
1690 { $$ = operator_stoken ("%"); }
1691 | OPERATOR '^'
1692 { $$ = operator_stoken ("^"); }
1693 | OPERATOR '&'
1694 { $$ = operator_stoken ("&"); }
1695 | OPERATOR '|'
1696 { $$ = operator_stoken ("|"); }
1697 | OPERATOR '~'
1698 { $$ = operator_stoken ("~"); }
1699 | OPERATOR '!'
1700 { $$ = operator_stoken ("!"); }
1701 | OPERATOR '='
1702 { $$ = operator_stoken ("="); }
1703 | OPERATOR '<'
1704 { $$ = operator_stoken ("<"); }
1705 | OPERATOR '>'
1706 { $$ = operator_stoken (">"); }
1707 | OPERATOR ASSIGN_MODIFY
1708 { const char *op = " unknown";
1709 switch ($2)
1711 case BINOP_RSH:
1712 op = ">>=";
1713 break;
1714 case BINOP_LSH:
1715 op = "<<=";
1716 break;
1717 case BINOP_ADD:
1718 op = "+=";
1719 break;
1720 case BINOP_SUB:
1721 op = "-=";
1722 break;
1723 case BINOP_MUL:
1724 op = "*=";
1725 break;
1726 case BINOP_DIV:
1727 op = "/=";
1728 break;
1729 case BINOP_REM:
1730 op = "%=";
1731 break;
1732 case BINOP_BITWISE_IOR:
1733 op = "|=";
1734 break;
1735 case BINOP_BITWISE_AND:
1736 op = "&=";
1737 break;
1738 case BINOP_BITWISE_XOR:
1739 op = "^=";
1740 break;
1741 default:
1742 break;
1745 $$ = operator_stoken (op);
1747 | OPERATOR LSH
1748 { $$ = operator_stoken ("<<"); }
1749 | OPERATOR RSH
1750 { $$ = operator_stoken (">>"); }
1751 | OPERATOR EQUAL
1752 { $$ = operator_stoken ("=="); }
1753 | OPERATOR NOTEQUAL
1754 { $$ = operator_stoken ("!="); }
1755 | OPERATOR LEQ
1756 { $$ = operator_stoken ("<="); }
1757 | OPERATOR GEQ
1758 { $$ = operator_stoken (">="); }
1759 | OPERATOR ANDAND
1760 { $$ = operator_stoken ("&&"); }
1761 | OPERATOR OROR
1762 { $$ = operator_stoken ("||"); }
1763 | OPERATOR INCREMENT
1764 { $$ = operator_stoken ("++"); }
1765 | OPERATOR DECREMENT
1766 { $$ = operator_stoken ("--"); }
1767 | OPERATOR ','
1768 { $$ = operator_stoken (","); }
1769 | OPERATOR ARROW_STAR
1770 { $$ = operator_stoken ("->*"); }
1771 | OPERATOR ARROW
1772 { $$ = operator_stoken ("->"); }
1773 | OPERATOR '(' ')'
1774 { $$ = operator_stoken ("()"); }
1775 | OPERATOR '[' ']'
1776 { $$ = operator_stoken ("[]"); }
1777 | OPERATOR OBJC_LBRAC ']'
1778 { $$ = operator_stoken ("[]"); }
1779 | OPERATOR conversion_type_id
1781 string_file buf;
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
1800 'double'. */
1801 field_name
1802 : name
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; }
1817 | oper { $$ = $1; }
1820 name_not_typename : NAME
1821 | BLOCKNAME
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.
1827 | NAME_OR_INT
1829 | oper
1831 struct field_of_this_result is_a_field_of_this;
1833 $$.stoken = $1;
1834 $$.sym
1835 = lookup_symbol ($1.ptr,
1836 pstate->expression_context_block,
1837 SEARCH_VFT,
1838 &is_a_field_of_this);
1839 $$.is_a_field_of_this
1840 = is_a_field_of_this.type != NULL;
1842 | UNKNOWN_CPP_NAME
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 };
1854 char *buf;
1856 st.length = CP_OPERATOR_LEN + strlen (op);
1857 buf = (char *) malloc (st.length + 1);
1858 strcpy (buf, CP_OPERATOR_STR);
1859 strcat (buf, op);
1860 st.ptr = buf;
1862 /* The toplevel (c_parse) will free the memory allocated here. */
1863 cpstate->strings.emplace_back (buf);
1864 return st;
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);
1874 return st;
1877 /* Return true if the type is aggregate-like. */
1879 static int
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. */
1891 static void
1892 check_parameter_typelist (std::vector<struct type *> *params)
1894 struct type *type;
1895 int ix;
1897 for (ix = 0; ix < params->size (); ++ix)
1899 type = (*params)[ix];
1900 if (type != NULL && check_typedef (type)->code () == TYPE_CODE_VOID)
1902 if (ix == 0)
1904 if (params->size () == 1)
1906 /* Ok. */
1907 break;
1909 error (_("parameter types following 'void'"));
1911 else
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 ***/
1923 static int
1924 parse_number (struct parser_state *par_state,
1925 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1927 ULONGEST n = 0;
1928 ULONGEST prevn = 0;
1930 int i = 0;
1931 int c;
1932 int base = input_radix;
1933 int unsigned_p = 0;
1935 /* Number of "L" suffixes encountered. */
1936 int long_p = 0;
1938 /* Imaginary number. */
1939 bool imaginary_p = false;
1941 /* We have found a "L" or "U" (or "i") suffix. */
1942 int found_suffix = 0;
1944 if (parsed_float)
1946 if (len >= 1 && buf[len - 1] == 'i')
1948 imaginary_p = true;
1949 --len;
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;
1957 len -= 2;
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;
1963 len -= 2;
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;
1969 len -= 2;
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;
1976 len -= 1;
1978 else if (len >= 1 && TOLOWER (buf[len - 1]) == 'l')
1980 putithere->typed_val_float.type
1981 = parse_type (par_state)->builtin_long_double;
1982 len -= 1;
1984 /* Default type for floating-point literals is double. */
1985 else
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))
1994 return ERROR;
1996 if (imaginary_p)
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)
2005 switch (buf[1])
2007 case 'x':
2008 case 'X':
2009 if (len >= 3)
2011 buf += 2;
2012 base = 16;
2013 len -= 2;
2015 break;
2017 case 'b':
2018 case 'B':
2019 if (len >= 3)
2021 buf += 2;
2022 base = 2;
2023 len -= 2;
2025 break;
2027 case 't':
2028 case 'T':
2029 case 'd':
2030 case 'D':
2031 if (len >= 3)
2033 buf += 2;
2034 base = 10;
2035 len -= 2;
2037 break;
2039 default:
2040 base = 8;
2041 break;
2044 while (len-- > 0)
2046 c = *buf++;
2047 if (c >= 'A' && c <= 'Z')
2048 c += 'a' - 'A';
2049 if (c != 'l' && c != 'u' && c != 'i')
2050 n *= base;
2051 if (c >= '0' && c <= '9')
2053 if (found_suffix)
2054 return ERROR;
2055 n += i = c - '0';
2057 else
2059 if (base > 10 && c >= 'a' && c <= 'f')
2061 if (found_suffix)
2062 return ERROR;
2063 n += i = c - 'a' + 10;
2065 else if (c == 'l')
2067 ++long_p;
2068 found_suffix = 1;
2070 else if (c == 'u')
2072 unsigned_p = 1;
2073 found_suffix = 1;
2075 else if (c == 'i')
2077 imaginary_p = true;
2078 found_suffix = 1;
2080 else
2081 return ERROR; /* Char not a digit */
2083 if (i >= base)
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."));
2094 prevn = n;
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 ());
2111 bool have_signed
2112 /* No 'u' suffix. */
2113 = !unsigned_p;
2114 bool have_unsigned
2115 = ((/* 'u' suffix. */
2116 unsigned_p)
2117 || (/* Not a decimal. */
2118 base != 10)
2119 || (/* Allowed as a convenience, in case decimal doesn't fit in largest
2120 signed type. */
2121 !fits_in_type (1, n, long_long_bits, true)));
2122 bool have_int
2123 /* No 'l' or 'll' suffix. */
2124 = long_p == 0;
2125 bool have_long
2126 /* No 'll' suffix. */
2127 = long_p <= 1;
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;
2144 else
2145 error (_("Numeric constant too large."));
2146 putithere->typed_val_int.val = n;
2148 if (imaginary_p)
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;
2172 int result = 1;
2174 /* Some escape sequences undergo character set conversion. Those we
2175 translate here. */
2176 switch (*tokptr)
2178 /* Hex escapes do not undergo character set conversion, so keep
2179 the escape sequence for later. */
2180 case 'x':
2181 if (output)
2182 obstack_grow_str (output, "\\x");
2183 ++tokptr;
2184 if (!ISXDIGIT (*tokptr))
2185 error (_("\\x escape without a following hex digit"));
2186 while (ISXDIGIT (*tokptr))
2188 if (output)
2189 obstack_1grow (output, *tokptr);
2190 ++tokptr;
2192 break;
2194 /* Octal escapes do not undergo character set conversion, so
2195 keep the escape sequence for later. */
2196 case '0':
2197 case '1':
2198 case '2':
2199 case '3':
2200 case '4':
2201 case '5':
2202 case '6':
2203 case '7':
2205 int i;
2206 if (output)
2207 obstack_grow_str (output, "\\");
2208 for (i = 0;
2209 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
2210 ++i)
2212 if (output)
2213 obstack_1grow (output, *tokptr);
2214 ++tokptr;
2217 break;
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
2222 charset. */
2223 case 'u':
2224 case 'U':
2226 char c = *tokptr;
2227 int i, len = c == 'U' ? 8 : 4;
2228 if (output)
2230 obstack_1grow (output, '\\');
2231 obstack_1grow (output, *tokptr);
2233 ++tokptr;
2234 if (!ISXDIGIT (*tokptr))
2235 error (_("\\%c escape without a following hex digit"), c);
2236 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
2238 if (output)
2239 obstack_1grow (output, *tokptr);
2240 ++tokptr;
2243 break;
2245 /* We must pass backslash through so that it does not
2246 cause quoting during the second expansion. */
2247 case '\\':
2248 if (output)
2249 obstack_grow_str (output, "\\\\");
2250 ++tokptr;
2251 break;
2253 /* Escapes which undergo conversion. */
2254 case 'a':
2255 if (output)
2256 obstack_1grow (output, '\a');
2257 ++tokptr;
2258 break;
2259 case 'b':
2260 if (output)
2261 obstack_1grow (output, '\b');
2262 ++tokptr;
2263 break;
2264 case 'f':
2265 if (output)
2266 obstack_1grow (output, '\f');
2267 ++tokptr;
2268 break;
2269 case 'n':
2270 if (output)
2271 obstack_1grow (output, '\n');
2272 ++tokptr;
2273 break;
2274 case 'r':
2275 if (output)
2276 obstack_1grow (output, '\r');
2277 ++tokptr;
2278 break;
2279 case 't':
2280 if (output)
2281 obstack_1grow (output, '\t');
2282 ++tokptr;
2283 break;
2284 case 'v':
2285 if (output)
2286 obstack_1grow (output, '\v');
2287 ++tokptr;
2288 break;
2290 /* GCC extension. */
2291 case 'e':
2292 if (output)
2293 obstack_1grow (output, HOST_ESCAPE_CHAR);
2294 ++tokptr;
2295 break;
2297 /* Backslash-newline expands to nothing at all. */
2298 case '\n':
2299 ++tokptr;
2300 result = 0;
2301 break;
2303 /* A few escapes just expand to the character itself. */
2304 case '\'':
2305 case '\"':
2306 case '?':
2307 /* GCC extensions. */
2308 case '(':
2309 case '{':
2310 case '[':
2311 case '%':
2312 /* Unrecognized escapes turn into the character itself. */
2313 default:
2314 if (output)
2315 obstack_1grow (output, *tokptr);
2316 ++tokptr;
2317 break;
2319 *ptr = tokptr;
2320 return result;
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. */
2330 static int
2331 parse_string_or_char (const char *tokptr, const char **outptr,
2332 struct typed_stoken *value, int *host_chars)
2334 int quote;
2335 c_string_type type;
2336 int is_objc = 0;
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
2345 bytes */
2347 if (!tempbuf_init)
2348 tempbuf_init = 1;
2349 else
2350 obstack_free (&tempbuf, NULL);
2351 obstack_init (&tempbuf);
2353 /* Record the string type. */
2354 if (*tokptr == 'L')
2356 type = C_WIDE_STRING;
2357 ++tokptr;
2359 else if (*tokptr == 'u')
2361 type = C_STRING_16;
2362 ++tokptr;
2364 else if (*tokptr == 'U')
2366 type = C_STRING_32;
2367 ++tokptr;
2369 else if (*tokptr == '@')
2371 /* An Objective C string. */
2372 is_objc = 1;
2373 type = C_STRING;
2374 ++tokptr;
2376 else
2377 type = C_STRING;
2379 /* Skip the quote. */
2380 quote = *tokptr;
2381 if (quote == '\'')
2382 type |= C_CHAR;
2383 ++tokptr;
2385 *host_chars = 0;
2387 while (*tokptr)
2389 char c = *tokptr;
2390 if (c == '\\')
2392 ++tokptr;
2393 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2395 else if (c == quote)
2396 break;
2397 else
2399 obstack_1grow (&tempbuf, c);
2400 ++tokptr;
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. */
2404 ++*host_chars;
2408 if (*tokptr != quote)
2410 if (quote == '"')
2411 error (_("Unterminated string in expression."));
2412 else
2413 error (_("Unmatched single quote."));
2415 ++tokptr;
2417 value->type = type;
2418 value->ptr = (char *) obstack_base (&tempbuf);
2419 value->length = obstack_object_size (&tempbuf);
2421 *outptr = tokptr;
2423 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2426 /* This is used to associate some attributes with a token. */
2428 enum token_flag
2430 /* If this bit is set, the token is C++-only. */
2432 FLAG_CXX = 1,
2434 /* If this bit is set, the token is C-only. */
2436 FLAG_C = 2,
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. */
2442 FLAG_SHADOW = 4
2444 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2446 struct c_token
2448 const char *oper;
2449 int token;
2450 enum exp_opcode opcode;
2451 token_flags flags;
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}
2552 static void
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;
2567 static int
2568 scanning_macro_expansion (void)
2570 return cpstate->macro_original_text != 0;
2573 static void
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. */
2586 static int
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. */
2612 static int
2613 lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
2615 int c;
2616 int namelen;
2617 const char *tokstart;
2618 bool saw_structop = last_was_structop;
2620 last_was_structop = false;
2621 *is_quoted_name = false;
2623 retry:
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)
2644 break;
2645 gdb_assert ((token.flags & FLAG_C) == 0);
2647 pstate->lexptr += 3;
2648 yylval.opcode = token.opcode;
2649 return token.token;
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)
2658 break;
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;
2665 return token.token;
2668 switch (c = *tokstart)
2670 case 0:
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 ();
2680 goto retry;
2682 else if (saw_name_at_eof)
2684 saw_name_at_eof = 0;
2685 return COMPLETE;
2687 else if (par_state->parse_completion && saw_structop)
2688 return COMPLETE;
2689 else
2690 return 0;
2692 case ' ':
2693 case '\t':
2694 case '\n':
2695 pstate->lexptr++;
2696 goto retry;
2698 case '[':
2699 case '(':
2700 paren_depth++;
2701 pstate->lexptr++;
2702 if (par_state->language ()->la_language == language_objc
2703 && c == '[')
2704 return OBJC_LBRAC;
2705 return c;
2707 case ']':
2708 case ')':
2709 if (paren_depth == 0)
2710 return 0;
2711 paren_depth--;
2712 pstate->lexptr++;
2713 return c;
2715 case ',':
2716 if (pstate->comma_terminates
2717 && paren_depth == 0
2718 && ! scanning_macro_expansion ())
2719 return 0;
2720 pstate->lexptr++;
2721 return c;
2723 case '.':
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. */
2730 [[fallthrough]];
2732 case '0':
2733 case '1':
2734 case '2':
2735 case '3':
2736 case '4':
2737 case '5':
2738 case '6':
2739 case '7':
2740 case '8':
2741 case '9':
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'))
2750 p += 2;
2751 hex = 1;
2753 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2755 p += 2;
2756 hex = 0;
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
2761 parse_number. */
2762 std::optional<std::string> no_tick;
2763 for (;; ++p)
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 == '.')
2775 got_dot = 1;
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
2781 the number. */
2783 else if (*p == '\'')
2785 if (!no_tick.has_value ())
2786 no_tick.emplace (tokstart, p);
2787 continue;
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')))
2794 break;
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 (),
2800 no_tick->length (),
2801 got_dot | got_e | got_p, &yylval);
2802 else
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),
2807 tokstart);
2808 pstate->lexptr = p;
2809 return toktype;
2812 case '@':
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;
2824 return SELECTOR;
2826 else if (*p == '"')
2827 goto parse_string;
2830 while (ISSPACE (*p))
2831 p++;
2832 size_t len = strlen ("entry");
2833 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
2834 && p[len] != '_')
2836 pstate->lexptr = &p[len];
2837 return ENTRY;
2840 [[fallthrough]];
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 case '}':
2858 symbol:
2859 pstate->lexptr++;
2860 return c;
2862 case 'L':
2863 case 'u':
2864 case 'U':
2865 if (tokstart[1] != '"' && tokstart[1] != '\'')
2866 break;
2867 [[fallthrough]];
2868 case '\'':
2869 case '"':
2871 parse_string:
2873 int host_len;
2874 int result = parse_string_or_char (tokstart, &pstate->lexptr,
2875 &yylval.tsval, &host_len);
2876 if (result == CHAR)
2878 if (host_len == 0)
2879 error (_("Empty character constant."));
2880 else if (host_len > 2 && c == '\'')
2882 ++tokstart;
2883 namelen = pstate->lexptr - tokstart - 1;
2884 *is_quoted_name = true;
2886 goto tryname;
2888 else if (host_len > 1)
2889 error (_("Invalid character constant."));
2891 return result;
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. */
2900 namelen = 0;
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'. */
2907 if (c == '<')
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);
2917 if (p)
2918 namelen = p - tokstart;
2920 break;
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. */
2928 if (namelen == 2
2929 && tokstart[0] == 'i'
2930 && tokstart[1] == 'f'
2931 && ! scanning_macro_expansion ())
2933 return 0;
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. */
2941 if (namelen >= 1
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')
2950 p++;
2951 if (*p >= '0' && *p <= '9')
2952 return 0;
2955 pstate->lexptr += namelen;
2957 tryname:
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)
2969 break;
2970 if ((token.flags & FLAG_C) != 0
2971 && par_state->language ()->la_language != language_c
2972 && par_state->language ()->la_language != language_objc)
2973 break;
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,
2981 SEARCH_VFT,
2982 (par_state->language ()->la_language
2983 == language_cplus ? &is_a_field_of_this
2984 : NULL)).symbol
2985 != NULL)
2987 /* The keyword is shadowed. */
2988 break;
2992 /* It is ok to always set this, even though we don't always
2993 strictly need to. */
2994 yylval.opcode = token.opcode;
2995 return token.token;
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;
3008 return NAME;
3011 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
3012 struct c_token_and_value
3014 int token;
3015 YYSTYPE value;
3018 /* A FIFO of tokens that have been read but not yet returned to the
3019 parser. */
3020 static std::vector<c_token_and_value> token_fifo;
3022 /* Non-zero if the lexer should return tokens from the FIFO. */
3023 static int popping;
3025 /* Temporary storage for c_lex; this holds symbol names as they are
3026 built up. */
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 */
3036 static int
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;
3057 return BLOCKNAME;
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 ();
3077 return TYPENAME;
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)
3087 || is_quoted_name)
3089 /* See if it's a file name. */
3090 if (auto symtab = lookup_symtab (current_program_space, copy.c_str ());
3091 symtab != nullptr)
3093 yylval.bval
3094 = symtab->compunit ()->blockvector ()->static_block ();
3096 return FILENAME;
3101 if (bsym.symbol && bsym.symbol->aclass () == LOC_TYPEDEF)
3103 yylval.tsym.type = bsym.symbol->type ();
3104 return TYPENAME;
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 (),
3111 copy.c_str ());
3112 if (Class)
3114 struct symbol *sym;
3116 yylval.theclass.theclass = Class;
3117 sym = lookup_struct_typedef (copy.c_str (),
3118 par_state->expression_context_block, 1);
3119 if (sym)
3120 yylval.theclass.type = sym->type ();
3121 return CLASSNAME;
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. */
3128 if (!bsym.symbol
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,
3134 0, &newlval);
3136 if (hextype == INT)
3138 yylval.ssym.sym = bsym;
3139 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3140 return NAME_OR_INT;
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;
3154 return 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. */
3161 static int
3162 classify_inner_name (struct parser_state *par_state,
3163 const struct block *block, struct type *context)
3165 struct type *type;
3167 if (context == NULL)
3168 return classify_name (par_state, block, false, false);
3170 type = check_typedef (context);
3171 if (!type_aggregate_p (type))
3172 return ERROR;
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,
3177 SEARCH_VFT);
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,
3185 copy.c_str ());
3187 if (base_type != NULL)
3189 yylval.tsym.type = base_type;
3190 return TYPENAME;
3193 return ERROR;
3196 switch (yylval.ssym.sym.symbol->aclass ())
3198 case LOC_BLOCK:
3199 case LOC_LABEL:
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;
3210 return TYPENAME;
3213 return ERROR;
3215 case LOC_TYPEDEF:
3216 yylval.tsym.type = yylval.ssym.sym.symbol->type ();
3217 return TYPENAME;
3219 default:
3220 return NAME;
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
3230 right point.
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. */
3237 static int
3238 yylex (void)
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 ())
3248 goto do_pop;
3249 popping = 0;
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
3255 name-like token. */
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
3266 the token FIFO. */
3267 current.value = yylval;
3268 token_fifo.push_back (current);
3269 last_was_coloncolon = current.token == COLONCOLON;
3270 while (1)
3272 bool ignore;
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))
3282 break;
3283 last_was_coloncolon = !last_was_coloncolon;
3285 popping = 1;
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];
3293 ++next_to_examine;
3295 name_obstack.clear ();
3296 checkpoint = 0;
3297 if (current.token == FILENAME)
3298 search_block = current.value.bval;
3299 else if (current.token == COLONCOLON)
3300 search_block = NULL;
3301 else
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;
3308 checkpoint = 1;
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];
3319 ++next_to_examine;
3321 if (next.token == NAME && last_was_coloncolon)
3323 int classification;
3325 yylval = next.value;
3326 classification = classify_inner_name (pstate, search_block,
3327 context_type);
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)
3331 break;
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)
3353 break;
3355 context_type = yylval.tsym.type;
3357 else if (next.token == COLONCOLON && !last_was_coloncolon)
3358 last_was_coloncolon = 1;
3359 else
3361 /* We've reached the end of the name. */
3362 break;
3366 /* If we have a replacement token, install it as the first token in
3367 the FIFO, and delete the other constituent tokens. */
3368 if (checkpoint > 0)
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;
3376 if (checkpoint > 1)
3377 token_fifo.erase (token_fifo.begin () + 1,
3378 token_fifo.begin () + checkpoint);
3381 do_pop:
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);
3394 pstate = par_state;
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)
3402 macro_scope
3403 = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
3404 else
3405 macro_scope = default_macro_scope ();
3406 if (! 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,
3413 par_state->debug);
3415 /* Initialize some state used by the lexer. */
3416 last_was_structop = false;
3417 saw_name_at_eof = 0;
3418 paren_depth = 0;
3420 token_fifo.clear ();
3421 popping = 0;
3422 name_obstack.clear ();
3424 int result = yyparse ();
3425 if (!result)
3426 pstate->set_operation (pstate->pop ());
3427 return result;
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. */
3436 static void
3437 c_print_token (FILE *file, int type, YYSTYPE value)
3439 switch (type)
3441 case INT:
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));
3445 break;
3447 case CHAR:
3448 case STRING:
3449 parser_fprintf (file, "tsval<type=%d, %.*s>", value.tsval.type,
3450 value.tsval.length, value.tsval.ptr);
3451 break;
3453 case NSSTRING:
3454 case DOLLAR_VARIABLE:
3455 parser_fprintf (file, "sval<%s>", copy_name (value.sval).c_str ());
3456 break;
3458 case TYPENAME:
3459 parser_fprintf (file, "tsym<type=%s, name=%s>",
3460 TYPE_SAFE_NAME (value.tsym.type),
3461 copy_name (value.tsym.stoken).c_str ());
3462 break;
3464 case NAME:
3465 case UNKNOWN_CPP_NAME:
3466 case NAME_OR_INT:
3467 case BLOCKNAME:
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);
3473 break;
3475 case FILENAME:
3476 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3477 break;
3481 #endif
3483 static void
3484 yyerror (const char *msg)
3486 pstate->parse_error (msg);