Remove path name from test case
[binutils-gdb.git] / gdb / c-exp.y
1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986-2023 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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/>. */
18
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.
27
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. */
35
36 %{
37
38 #include "defs.h"
39 #include <ctype.h>
40 #include "expression.h"
41 #include "value.h"
42 #include "parser-defs.h"
43 #include "language.h"
44 #include "c-lang.h"
45 #include "c-support.h"
46 #include "charset.h"
47 #include "block.h"
48 #include "cp-support.h"
49 #include "macroscope.h"
50 #include "objc-lang.h"
51 #include "typeprint.h"
52 #include "cp-abi.h"
53 #include "type-stack.h"
54 #include "target-float.h"
55 #include "c-exp.h"
56
57 #define parse_type(ps) builtin_type (ps->gdbarch ())
58
59 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
60 etc). */
61 #define GDB_YY_REMAP_PREFIX c_
62 #include "yy-remap.h"
63
64 /* The state of the parser, used internally when we are parsing the
65 expression. */
66
67 static struct parser_state *pstate = NULL;
68
69 /* Data that must be held for the duration of a parse. */
70
71 struct c_parse_state
72 {
73 /* These are used to hold type lists and type stacks that are
74 allocated during the parse. */
75 std::vector<std::unique_ptr<std::vector<struct type *>>> type_lists;
76 std::vector<std::unique_ptr<struct type_stack>> type_stacks;
77
78 /* Storage for some strings allocated during the parse. */
79 std::vector<gdb::unique_xmalloc_ptr<char>> strings;
80
81 /* When we find that lexptr (the global var defined in parse.c) is
82 pointing at a macro invocation, we expand the invocation, and call
83 scan_macro_expansion to save the old lexptr here and point lexptr
84 into the expanded text. When we reach the end of that, we call
85 end_macro_expansion to pop back to the value we saved here. The
86 macro expansion code promises to return only fully-expanded text,
87 so we don't need to "push" more than one level.
88
89 This is disgusting, of course. It would be cleaner to do all macro
90 expansion beforehand, and then hand that to lexptr. But we don't
91 really know where the expression ends. Remember, in a command like
92
93 (gdb) break *ADDRESS if CONDITION
94
95 we evaluate ADDRESS in the scope of the current frame, but we
96 evaluate CONDITION in the scope of the breakpoint's location. So
97 it's simply wrong to try to macro-expand the whole thing at once. */
98 const char *macro_original_text = nullptr;
99
100 /* We save all intermediate macro expansions on this obstack for the
101 duration of a single parse. The expansion text may sometimes have
102 to live past the end of the expansion, due to yacc lookahead.
103 Rather than try to be clever about saving the data for a single
104 token, we simply keep it all and delete it after parsing has
105 completed. */
106 auto_obstack expansion_obstack;
107
108 /* The type stack. */
109 struct type_stack type_stack;
110 };
111
112 /* This is set and cleared in c_parse. */
113
114 static struct c_parse_state *cpstate;
115
116 int yyparse (void);
117
118 static int yylex (void);
119
120 static void yyerror (const char *);
121
122 static int type_aggregate_p (struct type *);
123
124 using namespace expr;
125 %}
126
127 /* Although the yacc "value" of an expression is not used,
128 since the result is stored in the structure being created,
129 other node types do have values. */
130
131 %union
132 {
133 LONGEST lval;
134 struct {
135 LONGEST val;
136 struct type *type;
137 } typed_val_int;
138 struct {
139 gdb_byte val[16];
140 struct type *type;
141 } typed_val_float;
142 struct type *tval;
143 struct stoken sval;
144 struct typed_stoken tsval;
145 struct ttype tsym;
146 struct symtoken ssym;
147 int voidval;
148 const struct block *bval;
149 enum exp_opcode opcode;
150
151 struct stoken_vector svec;
152 std::vector<struct type *> *tvec;
153
154 struct type_stack *type_stack;
155
156 struct objc_class_str theclass;
157 }
158
159 %{
160 /* YYSTYPE gets defined by %union */
161 static int parse_number (struct parser_state *par_state,
162 const char *, int, int, YYSTYPE *);
163 static struct stoken operator_stoken (const char *);
164 static struct stoken typename_stoken (const char *);
165 static void check_parameter_typelist (std::vector<struct type *> *);
166
167 #if defined(YYBISON) && YYBISON < 30800
168 static void c_print_token (FILE *file, int type, YYSTYPE value);
169 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
170 #endif
171 %}
172
173 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly function_method
174 %type <lval> rcurly
175 %type <tval> type typebase scalar_type
176 %type <tvec> nonempty_typelist func_mod parameter_typelist
177 /* %type <bval> block */
178
179 /* Fancy type parsing. */
180 %type <tval> ptype
181 %type <lval> array_mod
182 %type <tval> conversion_type_id
183
184 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
185
186 %token <typed_val_int> INT COMPLEX_INT
187 %token <typed_val_float> FLOAT COMPLEX_FLOAT
188
189 /* Both NAME and TYPENAME tokens represent symbols in the input,
190 and both convey their data as strings.
191 But a TYPENAME is a string that happens to be defined as a typedef
192 or builtin type name (such as int or char)
193 and a NAME is any other symbol.
194 Contexts where this distinction is not important can use the
195 nonterminal "name", which matches either NAME or TYPENAME. */
196
197 %token <tsval> STRING
198 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
199 %token SELECTOR /* ObjC "@selector" pseudo-operator */
200 %token <tsval> CHAR
201 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
202 %token <ssym> UNKNOWN_CPP_NAME
203 %token <voidval> COMPLETE
204 %token <tsym> TYPENAME
205 %token <theclass> CLASSNAME /* ObjC Class name */
206 %type <sval> name field_name
207 %type <svec> string_exp
208 %type <ssym> name_not_typename
209 %type <tsym> type_name
210
211 /* This is like a '[' token, but is only generated when parsing
212 Objective C. This lets us reuse the same parser without
213 erroneously parsing ObjC-specific expressions in C. */
214 %token OBJC_LBRAC
215
216 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
217 but which would parse as a valid number in the current input radix.
218 E.g. "c" when input_radix==16. Depending on the parse, it will be
219 turned into a name or into a number. */
220
221 %token <ssym> NAME_OR_INT
222
223 %token OPERATOR
224 %token STRUCT CLASS UNION ENUM SIZEOF ALIGNOF UNSIGNED COLONCOLON
225 %token TEMPLATE
226 %token ERROR
227 %token NEW DELETE
228 %type <sval> oper
229 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
230 %token ENTRY
231 %token TYPEOF
232 %token DECLTYPE
233 %token TYPEID
234
235 /* Special type cases, put in to allow the parser to distinguish different
236 legal basetypes. */
237 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
238 %token RESTRICT ATOMIC
239 %token FLOAT_KEYWORD COMPLEX
240
241 %token <sval> DOLLAR_VARIABLE
242
243 %token <opcode> ASSIGN_MODIFY
244
245 /* C++ */
246 %token TRUEKEYWORD
247 %token FALSEKEYWORD
248
249
250 %left ','
251 %left ABOVE_COMMA
252 %right '=' ASSIGN_MODIFY
253 %right '?'
254 %left OROR
255 %left ANDAND
256 %left '|'
257 %left '^'
258 %left '&'
259 %left EQUAL NOTEQUAL
260 %left '<' '>' LEQ GEQ
261 %left LSH RSH
262 %left '@'
263 %left '+' '-'
264 %left '*' '/' '%'
265 %right UNARY INCREMENT DECREMENT
266 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
267 %token <ssym> BLOCKNAME
268 %token <bval> FILENAME
269 %type <bval> block
270 %left COLONCOLON
271
272 %token DOTDOTDOT
273
274 \f
275 %%
276
277 start : exp1
278 | type_exp
279 ;
280
281 type_exp: type
282 {
283 pstate->push_new<type_operation> ($1);
284 }
285 | TYPEOF '(' exp ')'
286 {
287 pstate->wrap<typeof_operation> ();
288 }
289 | TYPEOF '(' type ')'
290 {
291 pstate->push_new<type_operation> ($3);
292 }
293 | DECLTYPE '(' exp ')'
294 {
295 pstate->wrap<decltype_operation> ();
296 }
297 ;
298
299 /* Expressions, including the comma operator. */
300 exp1 : exp
301 | exp1 ',' exp
302 { pstate->wrap2<comma_operation> (); }
303 ;
304
305 /* Expressions, not including the comma operator. */
306 exp : '*' exp %prec UNARY
307 { pstate->wrap<unop_ind_operation> (); }
308 ;
309
310 exp : '&' exp %prec UNARY
311 { pstate->wrap<unop_addr_operation> (); }
312 ;
313
314 exp : '-' exp %prec UNARY
315 { pstate->wrap<unary_neg_operation> (); }
316 ;
317
318 exp : '+' exp %prec UNARY
319 { pstate->wrap<unary_plus_operation> (); }
320 ;
321
322 exp : '!' exp %prec UNARY
323 {
324 if (pstate->language ()->la_language
325 == language_opencl)
326 pstate->wrap<opencl_not_operation> ();
327 else
328 pstate->wrap<unary_logical_not_operation> ();
329 }
330 ;
331
332 exp : '~' exp %prec UNARY
333 { pstate->wrap<unary_complement_operation> (); }
334 ;
335
336 exp : INCREMENT exp %prec UNARY
337 { pstate->wrap<preinc_operation> (); }
338 ;
339
340 exp : DECREMENT exp %prec UNARY
341 { pstate->wrap<predec_operation> (); }
342 ;
343
344 exp : exp INCREMENT %prec UNARY
345 { pstate->wrap<postinc_operation> (); }
346 ;
347
348 exp : exp DECREMENT %prec UNARY
349 { pstate->wrap<postdec_operation> (); }
350 ;
351
352 exp : TYPEID '(' exp ')' %prec UNARY
353 { pstate->wrap<typeid_operation> (); }
354 ;
355
356 exp : TYPEID '(' type_exp ')' %prec UNARY
357 { pstate->wrap<typeid_operation> (); }
358 ;
359
360 exp : SIZEOF exp %prec UNARY
361 { pstate->wrap<unop_sizeof_operation> (); }
362 ;
363
364 exp : ALIGNOF '(' type_exp ')' %prec UNARY
365 { pstate->wrap<unop_alignof_operation> (); }
366 ;
367
368 exp : exp ARROW field_name
369 {
370 pstate->push_new<structop_ptr_operation>
371 (pstate->pop (), copy_name ($3));
372 }
373 ;
374
375 exp : exp ARROW field_name COMPLETE
376 {
377 structop_base_operation *op
378 = new structop_ptr_operation (pstate->pop (),
379 copy_name ($3));
380 pstate->mark_struct_expression (op);
381 pstate->push (operation_up (op));
382 }
383 ;
384
385 exp : exp ARROW COMPLETE
386 {
387 structop_base_operation *op
388 = new structop_ptr_operation (pstate->pop (), "");
389 pstate->mark_struct_expression (op);
390 pstate->push (operation_up (op));
391 }
392 ;
393
394 exp : exp ARROW '~' name
395 {
396 pstate->push_new<structop_ptr_operation>
397 (pstate->pop (), "~" + copy_name ($4));
398 }
399 ;
400
401 exp : exp ARROW '~' name COMPLETE
402 {
403 structop_base_operation *op
404 = new structop_ptr_operation (pstate->pop (),
405 "~" + copy_name ($4));
406 pstate->mark_struct_expression (op);
407 pstate->push (operation_up (op));
408 }
409 ;
410
411 exp : exp ARROW qualified_name
412 { /* exp->type::name becomes exp->*(&type::name) */
413 /* Note: this doesn't work if name is a
414 static member! FIXME */
415 pstate->wrap<unop_addr_operation> ();
416 pstate->wrap2<structop_mptr_operation> (); }
417 ;
418
419 exp : exp ARROW_STAR exp
420 { pstate->wrap2<structop_mptr_operation> (); }
421 ;
422
423 exp : exp '.' field_name
424 {
425 if (pstate->language ()->la_language
426 == language_opencl)
427 pstate->push_new<opencl_structop_operation>
428 (pstate->pop (), copy_name ($3));
429 else
430 pstate->push_new<structop_operation>
431 (pstate->pop (), copy_name ($3));
432 }
433 ;
434
435 exp : exp '.' field_name COMPLETE
436 {
437 structop_base_operation *op
438 = new structop_operation (pstate->pop (),
439 copy_name ($3));
440 pstate->mark_struct_expression (op);
441 pstate->push (operation_up (op));
442 }
443 ;
444
445 exp : exp '.' COMPLETE
446 {
447 structop_base_operation *op
448 = new structop_operation (pstate->pop (), "");
449 pstate->mark_struct_expression (op);
450 pstate->push (operation_up (op));
451 }
452 ;
453
454 exp : exp '.' '~' name
455 {
456 pstate->push_new<structop_operation>
457 (pstate->pop (), "~" + copy_name ($4));
458 }
459 ;
460
461 exp : exp '.' '~' name COMPLETE
462 {
463 structop_base_operation *op
464 = new structop_operation (pstate->pop (),
465 "~" + copy_name ($4));
466 pstate->mark_struct_expression (op);
467 pstate->push (operation_up (op));
468 }
469 ;
470
471 exp : exp '.' qualified_name
472 { /* exp.type::name becomes exp.*(&type::name) */
473 /* Note: this doesn't work if name is a
474 static member! FIXME */
475 pstate->wrap<unop_addr_operation> ();
476 pstate->wrap2<structop_member_operation> (); }
477 ;
478
479 exp : exp DOT_STAR exp
480 { pstate->wrap2<structop_member_operation> (); }
481 ;
482
483 exp : exp '[' exp1 ']'
484 { pstate->wrap2<subscript_operation> (); }
485 ;
486
487 exp : exp OBJC_LBRAC exp1 ']'
488 { pstate->wrap2<subscript_operation> (); }
489 ;
490
491 /*
492 * The rules below parse ObjC message calls of the form:
493 * '[' target selector {':' argument}* ']'
494 */
495
496 exp : OBJC_LBRAC TYPENAME
497 {
498 CORE_ADDR theclass;
499
500 std::string copy = copy_name ($2.stoken);
501 theclass = lookup_objc_class (pstate->gdbarch (),
502 copy.c_str ());
503 if (theclass == 0)
504 error (_("%s is not an ObjC Class"),
505 copy.c_str ());
506 pstate->push_new<long_const_operation>
507 (parse_type (pstate)->builtin_int,
508 (LONGEST) theclass);
509 start_msglist();
510 }
511 msglist ']'
512 { end_msglist (pstate); }
513 ;
514
515 exp : OBJC_LBRAC CLASSNAME
516 {
517 pstate->push_new<long_const_operation>
518 (parse_type (pstate)->builtin_int,
519 (LONGEST) $2.theclass);
520 start_msglist();
521 }
522 msglist ']'
523 { end_msglist (pstate); }
524 ;
525
526 exp : OBJC_LBRAC exp
527 { start_msglist(); }
528 msglist ']'
529 { end_msglist (pstate); }
530 ;
531
532 msglist : name
533 { add_msglist(&$1, 0); }
534 | msgarglist
535 ;
536
537 msgarglist : msgarg
538 | msgarglist msgarg
539 ;
540
541 msgarg : name ':' exp
542 { add_msglist(&$1, 1); }
543 | ':' exp /* Unnamed arg. */
544 { add_msglist(0, 1); }
545 | ',' exp /* Variable number of args. */
546 { add_msglist(0, 0); }
547 ;
548
549 exp : exp '('
550 /* This is to save the value of arglist_len
551 being accumulated by an outer function call. */
552 { pstate->start_arglist (); }
553 arglist ')' %prec ARROW
554 {
555 std::vector<operation_up> args
556 = pstate->pop_vector (pstate->end_arglist ());
557 pstate->push_new<funcall_operation>
558 (pstate->pop (), std::move (args));
559 }
560 ;
561
562 /* This is here to disambiguate with the production for
563 "func()::static_var" further below, which uses
564 function_method_void. */
565 exp : exp '(' ')' %prec ARROW
566 {
567 pstate->push_new<funcall_operation>
568 (pstate->pop (), std::vector<operation_up> ());
569 }
570 ;
571
572
573 exp : UNKNOWN_CPP_NAME '('
574 {
575 /* This could potentially be a an argument defined
576 lookup function (Koenig). */
577 /* This is to save the value of arglist_len
578 being accumulated by an outer function call. */
579 pstate->start_arglist ();
580 }
581 arglist ')' %prec ARROW
582 {
583 std::vector<operation_up> args
584 = pstate->pop_vector (pstate->end_arglist ());
585 pstate->push_new<adl_func_operation>
586 (copy_name ($1.stoken),
587 pstate->expression_context_block,
588 std::move (args));
589 }
590 ;
591
592 lcurly : '{'
593 { pstate->start_arglist (); }
594 ;
595
596 arglist :
597 ;
598
599 arglist : exp
600 { pstate->arglist_len = 1; }
601 ;
602
603 arglist : arglist ',' exp %prec ABOVE_COMMA
604 { pstate->arglist_len++; }
605 ;
606
607 function_method: exp '(' parameter_typelist ')' const_or_volatile
608 {
609 std::vector<struct type *> *type_list = $3;
610 /* Save the const/volatile qualifiers as
611 recorded by the const_or_volatile
612 production's actions. */
613 type_instance_flags flags
614 = (cpstate->type_stack
615 .follow_type_instance_flags ());
616 pstate->push_new<type_instance_operation>
617 (flags, std::move (*type_list),
618 pstate->pop ());
619 }
620 ;
621
622 function_method_void: exp '(' ')' const_or_volatile
623 {
624 type_instance_flags flags
625 = (cpstate->type_stack
626 .follow_type_instance_flags ());
627 pstate->push_new<type_instance_operation>
628 (flags, std::vector<type *> (), pstate->pop ());
629 }
630 ;
631
632 exp : function_method
633 ;
634
635 /* Normally we must interpret "func()" as a function call, instead of
636 a type. The user needs to write func(void) to disambiguate.
637 However, in the "func()::static_var" case, there's no
638 ambiguity. */
639 function_method_void_or_typelist: function_method
640 | function_method_void
641 ;
642
643 exp : function_method_void_or_typelist COLONCOLON name
644 {
645 pstate->push_new<func_static_var_operation>
646 (pstate->pop (), copy_name ($3));
647 }
648 ;
649
650 rcurly : '}'
651 { $$ = pstate->end_arglist () - 1; }
652 ;
653 exp : lcurly arglist rcurly %prec ARROW
654 {
655 std::vector<operation_up> args
656 = pstate->pop_vector ($3 + 1);
657 pstate->push_new<array_operation> (0, $3,
658 std::move (args));
659 }
660 ;
661
662 exp : lcurly type_exp rcurly exp %prec UNARY
663 { pstate->wrap2<unop_memval_type_operation> (); }
664 ;
665
666 exp : '(' type_exp ')' exp %prec UNARY
667 {
668 if (pstate->language ()->la_language
669 == language_opencl)
670 pstate->wrap2<opencl_cast_type_operation> ();
671 else
672 pstate->wrap2<unop_cast_type_operation> ();
673 }
674 ;
675
676 exp : '(' exp1 ')'
677 { }
678 ;
679
680 /* Binary operators in order of decreasing precedence. */
681
682 exp : exp '@' exp
683 { pstate->wrap2<repeat_operation> (); }
684 ;
685
686 exp : exp '*' exp
687 { pstate->wrap2<mul_operation> (); }
688 ;
689
690 exp : exp '/' exp
691 { pstate->wrap2<div_operation> (); }
692 ;
693
694 exp : exp '%' exp
695 { pstate->wrap2<rem_operation> (); }
696 ;
697
698 exp : exp '+' exp
699 { pstate->wrap2<add_operation> (); }
700 ;
701
702 exp : exp '-' exp
703 { pstate->wrap2<sub_operation> (); }
704 ;
705
706 exp : exp LSH exp
707 { pstate->wrap2<lsh_operation> (); }
708 ;
709
710 exp : exp RSH exp
711 { pstate->wrap2<rsh_operation> (); }
712 ;
713
714 exp : exp EQUAL exp
715 {
716 if (pstate->language ()->la_language
717 == language_opencl)
718 pstate->wrap2<opencl_equal_operation> ();
719 else
720 pstate->wrap2<equal_operation> ();
721 }
722 ;
723
724 exp : exp NOTEQUAL exp
725 {
726 if (pstate->language ()->la_language
727 == language_opencl)
728 pstate->wrap2<opencl_notequal_operation> ();
729 else
730 pstate->wrap2<notequal_operation> ();
731 }
732 ;
733
734 exp : exp LEQ exp
735 {
736 if (pstate->language ()->la_language
737 == language_opencl)
738 pstate->wrap2<opencl_leq_operation> ();
739 else
740 pstate->wrap2<leq_operation> ();
741 }
742 ;
743
744 exp : exp GEQ exp
745 {
746 if (pstate->language ()->la_language
747 == language_opencl)
748 pstate->wrap2<opencl_geq_operation> ();
749 else
750 pstate->wrap2<geq_operation> ();
751 }
752 ;
753
754 exp : exp '<' exp
755 {
756 if (pstate->language ()->la_language
757 == language_opencl)
758 pstate->wrap2<opencl_less_operation> ();
759 else
760 pstate->wrap2<less_operation> ();
761 }
762 ;
763
764 exp : exp '>' exp
765 {
766 if (pstate->language ()->la_language
767 == language_opencl)
768 pstate->wrap2<opencl_gtr_operation> ();
769 else
770 pstate->wrap2<gtr_operation> ();
771 }
772 ;
773
774 exp : exp '&' exp
775 { pstate->wrap2<bitwise_and_operation> (); }
776 ;
777
778 exp : exp '^' exp
779 { pstate->wrap2<bitwise_xor_operation> (); }
780 ;
781
782 exp : exp '|' exp
783 { pstate->wrap2<bitwise_ior_operation> (); }
784 ;
785
786 exp : exp ANDAND exp
787 {
788 if (pstate->language ()->la_language
789 == language_opencl)
790 {
791 operation_up rhs = pstate->pop ();
792 operation_up lhs = pstate->pop ();
793 pstate->push_new<opencl_logical_binop_operation>
794 (BINOP_LOGICAL_AND, std::move (lhs),
795 std::move (rhs));
796 }
797 else
798 pstate->wrap2<logical_and_operation> ();
799 }
800 ;
801
802 exp : exp OROR exp
803 {
804 if (pstate->language ()->la_language
805 == language_opencl)
806 {
807 operation_up rhs = pstate->pop ();
808 operation_up lhs = pstate->pop ();
809 pstate->push_new<opencl_logical_binop_operation>
810 (BINOP_LOGICAL_OR, std::move (lhs),
811 std::move (rhs));
812 }
813 else
814 pstate->wrap2<logical_or_operation> ();
815 }
816 ;
817
818 exp : exp '?' exp ':' exp %prec '?'
819 {
820 operation_up last = pstate->pop ();
821 operation_up mid = pstate->pop ();
822 operation_up first = pstate->pop ();
823 if (pstate->language ()->la_language
824 == language_opencl)
825 pstate->push_new<opencl_ternop_cond_operation>
826 (std::move (first), std::move (mid),
827 std::move (last));
828 else
829 pstate->push_new<ternop_cond_operation>
830 (std::move (first), std::move (mid),
831 std::move (last));
832 }
833 ;
834
835 exp : exp '=' exp
836 {
837 if (pstate->language ()->la_language
838 == language_opencl)
839 pstate->wrap2<opencl_assign_operation> ();
840 else
841 pstate->wrap2<assign_operation> ();
842 }
843 ;
844
845 exp : exp ASSIGN_MODIFY exp
846 {
847 operation_up rhs = pstate->pop ();
848 operation_up lhs = pstate->pop ();
849 pstate->push_new<assign_modify_operation>
850 ($2, std::move (lhs), std::move (rhs));
851 }
852 ;
853
854 exp : INT
855 {
856 pstate->push_new<long_const_operation>
857 ($1.type, $1.val);
858 }
859 ;
860
861 exp : COMPLEX_INT
862 {
863 operation_up real
864 = (make_operation<long_const_operation>
865 ($1.type->target_type (), 0));
866 operation_up imag
867 = (make_operation<long_const_operation>
868 ($1.type->target_type (), $1.val));
869 pstate->push_new<complex_operation>
870 (std::move (real), std::move (imag), $1.type);
871 }
872 ;
873
874 exp : CHAR
875 {
876 struct stoken_vector vec;
877 vec.len = 1;
878 vec.tokens = &$1;
879 pstate->push_c_string ($1.type, &vec);
880 }
881 ;
882
883 exp : NAME_OR_INT
884 { YYSTYPE val;
885 parse_number (pstate, $1.stoken.ptr,
886 $1.stoken.length, 0, &val);
887 pstate->push_new<long_const_operation>
888 (val.typed_val_int.type,
889 val.typed_val_int.val);
890 }
891 ;
892
893
894 exp : FLOAT
895 {
896 float_data data;
897 std::copy (std::begin ($1.val), std::end ($1.val),
898 std::begin (data));
899 pstate->push_new<float_const_operation> ($1.type, data);
900 }
901 ;
902
903 exp : COMPLEX_FLOAT
904 {
905 struct type *underlying = $1.type->target_type ();
906
907 float_data val;
908 target_float_from_host_double (val.data (),
909 underlying, 0);
910 operation_up real
911 = (make_operation<float_const_operation>
912 (underlying, val));
913
914 std::copy (std::begin ($1.val), std::end ($1.val),
915 std::begin (val));
916 operation_up imag
917 = (make_operation<float_const_operation>
918 (underlying, val));
919
920 pstate->push_new<complex_operation>
921 (std::move (real), std::move (imag),
922 $1.type);
923 }
924 ;
925
926 exp : variable
927 ;
928
929 exp : DOLLAR_VARIABLE
930 {
931 pstate->push_dollar ($1);
932 }
933 ;
934
935 exp : SELECTOR '(' name ')'
936 {
937 pstate->push_new<objc_selector_operation>
938 (copy_name ($3));
939 }
940 ;
941
942 exp : SIZEOF '(' type ')' %prec UNARY
943 { struct type *type = $3;
944 struct type *int_type
945 = lookup_signed_typename (pstate->language (),
946 "int");
947 type = check_typedef (type);
948
949 /* $5.3.3/2 of the C++ Standard (n3290 draft)
950 says of sizeof: "When applied to a reference
951 or a reference type, the result is the size of
952 the referenced type." */
953 if (TYPE_IS_REFERENCE (type))
954 type = check_typedef (type->target_type ());
955
956 pstate->push_new<long_const_operation>
957 (int_type, type->length ());
958 }
959 ;
960
961 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
962 { pstate->wrap2<reinterpret_cast_operation> (); }
963 ;
964
965 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
966 { pstate->wrap2<unop_cast_type_operation> (); }
967 ;
968
969 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
970 { pstate->wrap2<dynamic_cast_operation> (); }
971 ;
972
973 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
974 { /* We could do more error checking here, but
975 it doesn't seem worthwhile. */
976 pstate->wrap2<unop_cast_type_operation> (); }
977 ;
978
979 string_exp:
980 STRING
981 {
982 /* We copy the string here, and not in the
983 lexer, to guarantee that we do not leak a
984 string. Note that we follow the
985 NUL-termination convention of the
986 lexer. */
987 struct typed_stoken *vec = XNEW (struct typed_stoken);
988 $$.len = 1;
989 $$.tokens = vec;
990
991 vec->type = $1.type;
992 vec->length = $1.length;
993 vec->ptr = (char *) malloc ($1.length + 1);
994 memcpy (vec->ptr, $1.ptr, $1.length + 1);
995 }
996
997 | string_exp STRING
998 {
999 /* Note that we NUL-terminate here, but just
1000 for convenience. */
1001 char *p;
1002 ++$$.len;
1003 $$.tokens = XRESIZEVEC (struct typed_stoken,
1004 $$.tokens, $$.len);
1005
1006 p = (char *) malloc ($2.length + 1);
1007 memcpy (p, $2.ptr, $2.length + 1);
1008
1009 $$.tokens[$$.len - 1].type = $2.type;
1010 $$.tokens[$$.len - 1].length = $2.length;
1011 $$.tokens[$$.len - 1].ptr = p;
1012 }
1013 ;
1014
1015 exp : string_exp
1016 {
1017 int i;
1018 c_string_type type = C_STRING;
1019
1020 for (i = 0; i < $1.len; ++i)
1021 {
1022 switch ($1.tokens[i].type)
1023 {
1024 case C_STRING:
1025 break;
1026 case C_WIDE_STRING:
1027 case C_STRING_16:
1028 case C_STRING_32:
1029 if (type != C_STRING
1030 && type != $1.tokens[i].type)
1031 error (_("Undefined string concatenation."));
1032 type = (enum c_string_type_values) $1.tokens[i].type;
1033 break;
1034 default:
1035 /* internal error */
1036 internal_error ("unrecognized type in string concatenation");
1037 }
1038 }
1039
1040 pstate->push_c_string (type, &$1);
1041 for (i = 0; i < $1.len; ++i)
1042 free ($1.tokens[i].ptr);
1043 free ($1.tokens);
1044 }
1045 ;
1046
1047 exp : NSSTRING /* ObjC NextStep NSString constant
1048 * of the form '@' '"' string '"'.
1049 */
1050 {
1051 pstate->push_new<objc_nsstring_operation>
1052 (copy_name ($1));
1053 }
1054 ;
1055
1056 /* C++. */
1057 exp : TRUEKEYWORD
1058 { pstate->push_new<long_const_operation>
1059 (parse_type (pstate)->builtin_bool, 1);
1060 }
1061 ;
1062
1063 exp : FALSEKEYWORD
1064 { pstate->push_new<long_const_operation>
1065 (parse_type (pstate)->builtin_bool, 0);
1066 }
1067 ;
1068
1069 /* end of C++. */
1070
1071 block : BLOCKNAME
1072 {
1073 if ($1.sym.symbol)
1074 $$ = $1.sym.symbol->value_block ();
1075 else
1076 error (_("No file or function \"%s\"."),
1077 copy_name ($1.stoken).c_str ());
1078 }
1079 | FILENAME
1080 {
1081 $$ = $1;
1082 }
1083 ;
1084
1085 block : block COLONCOLON name
1086 {
1087 std::string copy = copy_name ($3);
1088 struct symbol *tem
1089 = lookup_symbol (copy.c_str (), $1,
1090 VAR_DOMAIN, NULL).symbol;
1091
1092 if (!tem || tem->aclass () != LOC_BLOCK)
1093 error (_("No function \"%s\" in specified context."),
1094 copy.c_str ());
1095 $$ = tem->value_block (); }
1096 ;
1097
1098 variable: name_not_typename ENTRY
1099 { struct symbol *sym = $1.sym.symbol;
1100
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 ());
1106
1107 pstate->push_new<var_entry_value_operation> (sym);
1108 }
1109 ;
1110
1111 variable: block COLONCOLON name
1112 {
1113 std::string copy = copy_name ($3);
1114 struct block_symbol sym
1115 = lookup_symbol (copy.c_str (), $1,
1116 VAR_DOMAIN, NULL);
1117
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);
1123
1124 pstate->push_new<var_value_operation> (sym);
1125 }
1126 ;
1127
1128 qualified_name: TYPENAME COLONCOLON name
1129 {
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));
1135
1136 pstate->push_new<scope_operation> (type,
1137 copy_name ($3));
1138 }
1139 | TYPENAME COLONCOLON '~' name
1140 {
1141 struct type *type = $1.type;
1142
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);
1149
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));
1154 }
1155 | TYPENAME COLONCOLON name COLONCOLON name
1156 {
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));
1161 }
1162 ;
1163
1164 variable: qualified_name
1165 | COLONCOLON name_not_typename
1166 {
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 VAR_DOMAIN, NULL);
1172 pstate->push_symbol (name.c_str (), sym);
1173 }
1174 ;
1175
1176 variable: name_not_typename
1177 { struct block_symbol sym = $1.sym;
1178
1179 if (sym.symbol)
1180 {
1181 if (symbol_read_needs_frame (sym.symbol))
1182 pstate->block_tracker->update (sym);
1183
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. */
1188
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);
1196 }
1197 else if ($1.is_a_field_of_this)
1198 {
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));
1207 }
1208 else
1209 {
1210 std::string arg = copy_name ($1.stoken);
1211
1212 bound_minimal_symbol msymbol
1213 = lookup_bound_minimal_symbol (arg.c_str ());
1214 if (msymbol.minsym == NULL)
1215 {
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 ());
1221 }
1222
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)
1236 {
1237 block_symbol bsym { alias_target,
1238 alias_target->value_block () };
1239 pstate->push_new<var_value_operation> (bsym);
1240 }
1241 else
1242 pstate->push_new<var_msym_value_operation>
1243 (msymbol);
1244 }
1245 }
1246 ;
1247
1248 const_or_volatile: const_or_volatile_noopt
1249 |
1250 ;
1251
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
1262 {
1263 cpstate->type_stack.insert (pstate,
1264 copy_name ($2.stoken).c_str ());
1265 }
1266 | '@' UNKNOWN_CPP_NAME
1267 {
1268 cpstate->type_stack.insert (pstate,
1269 copy_name ($2.stoken).c_str ());
1270 }
1271 ;
1272
1273 qualifier_seq_noopt:
1274 single_qualifier
1275 | qualifier_seq_noopt single_qualifier
1276 ;
1277
1278 qualifier_seq:
1279 qualifier_seq_noopt
1280 |
1281 ;
1282
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); }
1298 ;
1299
1300 ptr_operator_ts: ptr_operator
1301 {
1302 $$ = cpstate->type_stack.create ();
1303 cpstate->type_stacks.emplace_back ($$);
1304 }
1305 ;
1306
1307 abs_decl: ptr_operator_ts direct_abs_decl
1308 { $$ = $2->append ($1); }
1309 | ptr_operator_ts
1310 | direct_abs_decl
1311 ;
1312
1313 direct_abs_decl: '(' abs_decl ')'
1314 { $$ = $2; }
1315 | direct_abs_decl array_mod
1316 {
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 ($$);
1322 }
1323 | array_mod
1324 {
1325 cpstate->type_stack.push ($1);
1326 cpstate->type_stack.push (tp_array);
1327 $$ = cpstate->type_stack.create ();
1328 cpstate->type_stacks.emplace_back ($$);
1329 }
1330
1331 | direct_abs_decl func_mod
1332 {
1333 cpstate->type_stack.push ($1);
1334 cpstate->type_stack.push ($2);
1335 $$ = cpstate->type_stack.create ();
1336 cpstate->type_stacks.emplace_back ($$);
1337 }
1338 | func_mod
1339 {
1340 cpstate->type_stack.push ($1);
1341 $$ = cpstate->type_stack.create ();
1342 cpstate->type_stacks.emplace_back ($$);
1343 }
1344 ;
1345
1346 array_mod: '[' ']'
1347 { $$ = -1; }
1348 | OBJC_LBRAC ']'
1349 { $$ = -1; }
1350 | '[' INT ']'
1351 { $$ = $2.val; }
1352 | OBJC_LBRAC INT ']'
1353 { $$ = $2.val; }
1354 ;
1355
1356 func_mod: '(' ')'
1357 {
1358 $$ = new std::vector<struct type *>;
1359 cpstate->type_lists.emplace_back ($$);
1360 }
1361 | '(' parameter_typelist ')'
1362 { $$ = $2; }
1363 ;
1364
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! */
1372
1373 type : ptype
1374 ;
1375
1376 /* A helper production that recognizes scalar types that can validly
1377 be used with _Complex. */
1378
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"); }
1485 ;
1486
1487 /* Implements (approximately): (type-qualifier)* type-specifier.
1488
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
1503 {
1504 $$ = init_complex_type (nullptr, $2);
1505 }
1506 | STRUCT name
1507 { $$
1508 = lookup_struct (copy_name ($2).c_str (),
1509 pstate->expression_context_block);
1510 }
1511 | STRUCT COMPLETE
1512 {
1513 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1514 "", 0);
1515 $$ = NULL;
1516 }
1517 | STRUCT name COMPLETE
1518 {
1519 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1520 $2.ptr, $2.length);
1521 $$ = NULL;
1522 }
1523 | CLASS name
1524 { $$ = lookup_struct
1525 (copy_name ($2).c_str (),
1526 pstate->expression_context_block);
1527 }
1528 | CLASS COMPLETE
1529 {
1530 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1531 "", 0);
1532 $$ = NULL;
1533 }
1534 | CLASS name COMPLETE
1535 {
1536 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1537 $2.ptr, $2.length);
1538 $$ = NULL;
1539 }
1540 | UNION name
1541 { $$
1542 = lookup_union (copy_name ($2).c_str (),
1543 pstate->expression_context_block);
1544 }
1545 | UNION COMPLETE
1546 {
1547 pstate->mark_completion_tag (TYPE_CODE_UNION,
1548 "", 0);
1549 $$ = NULL;
1550 }
1551 | UNION name COMPLETE
1552 {
1553 pstate->mark_completion_tag (TYPE_CODE_UNION,
1554 $2.ptr, $2.length);
1555 $$ = NULL;
1556 }
1557 | ENUM name
1558 { $$ = lookup_enum (copy_name ($2).c_str (),
1559 pstate->expression_context_block);
1560 }
1561 | ENUM COMPLETE
1562 {
1563 pstate->mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1564 $$ = NULL;
1565 }
1566 | ENUM name COMPLETE
1567 {
1568 pstate->mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1569 $2.length);
1570 $$ = NULL;
1571 }
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);
1579 }
1580 | qualifier_seq_noopt typebase
1581 { $$ = cpstate->type_stack.follow_types ($2); }
1582 | typebase qualifier_seq_noopt
1583 { $$ = cpstate->type_stack.follow_types ($1); }
1584 ;
1585
1586 type_name: TYPENAME
1587 | INT_KEYWORD
1588 {
1589 $$.stoken.ptr = "int";
1590 $$.stoken.length = 3;
1591 $$.type = lookup_signed_typename (pstate->language (),
1592 "int");
1593 }
1594 | LONG
1595 {
1596 $$.stoken.ptr = "long";
1597 $$.stoken.length = 4;
1598 $$.type = lookup_signed_typename (pstate->language (),
1599 "long");
1600 }
1601 | SHORT
1602 {
1603 $$.stoken.ptr = "short";
1604 $$.stoken.length = 5;
1605 $$.type = lookup_signed_typename (pstate->language (),
1606 "short");
1607 }
1608 ;
1609
1610 parameter_typelist:
1611 nonempty_typelist
1612 { check_parameter_typelist ($1); }
1613 | nonempty_typelist ',' DOTDOTDOT
1614 {
1615 $1->push_back (NULL);
1616 check_parameter_typelist ($1);
1617 $$ = $1;
1618 }
1619 ;
1620
1621 nonempty_typelist
1622 : type
1623 {
1624 std::vector<struct type *> *typelist
1625 = new std::vector<struct type *>;
1626 cpstate->type_lists.emplace_back (typelist);
1627
1628 typelist->push_back ($1);
1629 $$ = typelist;
1630 }
1631 | nonempty_typelist ',' type
1632 {
1633 $1->push_back ($3);
1634 $$ = $1;
1635 }
1636 ;
1637
1638 ptype : typebase
1639 | ptype abs_decl
1640 {
1641 cpstate->type_stack.push ($2);
1642 $$ = cpstate->type_stack.follow_types ($1);
1643 }
1644 ;
1645
1646 conversion_type_id: typebase conversion_declarator
1647 { $$ = cpstate->type_stack.follow_types ($1); }
1648 ;
1649
1650 conversion_declarator: /* Nothing. */
1651 | ptr_operator conversion_declarator
1652 ;
1653
1654 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1655 | VOLATILE_KEYWORD CONST_KEYWORD
1656 ;
1657
1658 const_or_volatile_noopt: const_and_volatile
1659 { cpstate->type_stack.insert (tp_const);
1660 cpstate->type_stack.insert (tp_volatile);
1661 }
1662 | CONST_KEYWORD
1663 { cpstate->type_stack.insert (tp_const); }
1664 | VOLATILE_KEYWORD
1665 { cpstate->type_stack.insert (tp_volatile); }
1666 ;
1667
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)
1709 {
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;
1742 }
1743
1744 $$ = operator_stoken (op);
1745 }
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
1779 {
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 ();
1785
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 ());
1792 }
1793 ;
1794
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"); }
1809 ;
1810
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; }
1817 ;
1818
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
1827 */
1828 | oper
1829 {
1830 struct field_of_this_result is_a_field_of_this;
1831
1832 $$.stoken = $1;
1833 $$.sym
1834 = lookup_symbol ($1.ptr,
1835 pstate->expression_context_block,
1836 VAR_DOMAIN,
1837 &is_a_field_of_this);
1838 $$.is_a_field_of_this
1839 = is_a_field_of_this.type != NULL;
1840 }
1841 | UNKNOWN_CPP_NAME
1842 ;
1843
1844 %%
1845
1846 /* Returns a stoken of the operator name given by OP (which does not
1847 include the string "operator"). */
1848
1849 static struct stoken
1850 operator_stoken (const char *op)
1851 {
1852 struct stoken st = { NULL, 0 };
1853 char *buf;
1854
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;
1860
1861 /* The toplevel (c_parse) will free the memory allocated here. */
1862 cpstate->strings.emplace_back (buf);
1863 return st;
1864 };
1865
1866 /* Returns a stoken of the type named TYPE. */
1867
1868 static struct stoken
1869 typename_stoken (const char *type)
1870 {
1871 struct stoken st = { type, 0 };
1872 st.length = strlen (type);
1873 return st;
1874 };
1875
1876 /* Return true if the type is aggregate-like. */
1877
1878 static int
1879 type_aggregate_p (struct type *type)
1880 {
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 ()));
1886 }
1887
1888 /* Validate a parameter typelist. */
1889
1890 static void
1891 check_parameter_typelist (std::vector<struct type *> *params)
1892 {
1893 struct type *type;
1894 int ix;
1895
1896 for (ix = 0; ix < params->size (); ++ix)
1897 {
1898 type = (*params)[ix];
1899 if (type != NULL && check_typedef (type)->code () == TYPE_CODE_VOID)
1900 {
1901 if (ix == 0)
1902 {
1903 if (params->size () == 1)
1904 {
1905 /* Ok. */
1906 break;
1907 }
1908 error (_("parameter types following 'void'"));
1909 }
1910 else
1911 error (_("'void' invalid as parameter type"));
1912 }
1913 }
1914 }
1915
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. */
1919
1920 /*** Needs some error checking for the float case ***/
1921
1922 static int
1923 parse_number (struct parser_state *par_state,
1924 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1925 {
1926 ULONGEST n = 0;
1927 ULONGEST prevn = 0;
1928
1929 int i = 0;
1930 int c;
1931 int base = input_radix;
1932 int unsigned_p = 0;
1933
1934 /* Number of "L" suffixes encountered. */
1935 int long_p = 0;
1936
1937 /* Imaginary number. */
1938 bool imaginary_p = false;
1939
1940 /* We have found a "L" or "U" (or "i") suffix. */
1941 int found_suffix = 0;
1942
1943 char *p;
1944
1945 p = (char *) alloca (len);
1946 memcpy (p, buf, len);
1947
1948 if (parsed_float)
1949 {
1950 if (len >= 1 && p[len - 1] == 'i')
1951 {
1952 imaginary_p = true;
1953 --len;
1954 }
1955
1956 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
1957 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1958 {
1959 putithere->typed_val_float.type
1960 = parse_type (par_state)->builtin_decfloat;
1961 len -= 2;
1962 }
1963 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1964 {
1965 putithere->typed_val_float.type
1966 = parse_type (par_state)->builtin_decdouble;
1967 len -= 2;
1968 }
1969 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1970 {
1971 putithere->typed_val_float.type
1972 = parse_type (par_state)->builtin_declong;
1973 len -= 2;
1974 }
1975 /* Handle suffixes: 'f' for float, 'l' for long double. */
1976 else if (len >= 1 && TOLOWER (p[len - 1]) == 'f')
1977 {
1978 putithere->typed_val_float.type
1979 = parse_type (par_state)->builtin_float;
1980 len -= 1;
1981 }
1982 else if (len >= 1 && TOLOWER (p[len - 1]) == 'l')
1983 {
1984 putithere->typed_val_float.type
1985 = parse_type (par_state)->builtin_long_double;
1986 len -= 1;
1987 }
1988 /* Default type for floating-point literals is double. */
1989 else
1990 {
1991 putithere->typed_val_float.type
1992 = parse_type (par_state)->builtin_double;
1993 }
1994
1995 if (!parse_float (p, len,
1996 putithere->typed_val_float.type,
1997 putithere->typed_val_float.val))
1998 return ERROR;
1999
2000 if (imaginary_p)
2001 putithere->typed_val_float.type
2002 = init_complex_type (nullptr, putithere->typed_val_float.type);
2003
2004 return imaginary_p ? COMPLEX_FLOAT : FLOAT;
2005 }
2006
2007 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
2008 if (p[0] == '0' && len > 1)
2009 switch (p[1])
2010 {
2011 case 'x':
2012 case 'X':
2013 if (len >= 3)
2014 {
2015 p += 2;
2016 base = 16;
2017 len -= 2;
2018 }
2019 break;
2020
2021 case 'b':
2022 case 'B':
2023 if (len >= 3)
2024 {
2025 p += 2;
2026 base = 2;
2027 len -= 2;
2028 }
2029 break;
2030
2031 case 't':
2032 case 'T':
2033 case 'd':
2034 case 'D':
2035 if (len >= 3)
2036 {
2037 p += 2;
2038 base = 10;
2039 len -= 2;
2040 }
2041 break;
2042
2043 default:
2044 base = 8;
2045 break;
2046 }
2047
2048 while (len-- > 0)
2049 {
2050 c = *p++;
2051 if (c >= 'A' && c <= 'Z')
2052 c += 'a' - 'A';
2053 if (c != 'l' && c != 'u' && c != 'i')
2054 n *= base;
2055 if (c >= '0' && c <= '9')
2056 {
2057 if (found_suffix)
2058 return ERROR;
2059 n += i = c - '0';
2060 }
2061 else
2062 {
2063 if (base > 10 && c >= 'a' && c <= 'f')
2064 {
2065 if (found_suffix)
2066 return ERROR;
2067 n += i = c - 'a' + 10;
2068 }
2069 else if (c == 'l')
2070 {
2071 ++long_p;
2072 found_suffix = 1;
2073 }
2074 else if (c == 'u')
2075 {
2076 unsigned_p = 1;
2077 found_suffix = 1;
2078 }
2079 else if (c == 'i')
2080 {
2081 imaginary_p = true;
2082 found_suffix = 1;
2083 }
2084 else
2085 return ERROR; /* Char not a digit */
2086 }
2087 if (i >= base)
2088 return ERROR; /* Invalid digit in this base */
2089
2090 if (c != 'l' && c != 'u' && c != 'i')
2091 {
2092 /* Test for overflow. */
2093 if (prevn == 0 && n == 0)
2094 ;
2095 else if (prevn >= n)
2096 error (_("Numeric constant too large."));
2097 }
2098 prevn = n;
2099 }
2100
2101 /* An integer constant is an int, a long, or a long long. An L
2102 suffix forces it to be long; an LL suffix forces it to be long
2103 long. If not forced to a larger size, it gets the first type of
2104 the above that it fits in. To figure out whether it fits, we
2105 shift it right and see whether anything remains. Note that we
2106 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
2107 operation, because many compilers will warn about such a shift
2108 (which always produces a zero result). Sometimes gdbarch_int_bit
2109 or gdbarch_long_bit will be that big, sometimes not. To deal with
2110 the case where it is we just always shift the value more than
2111 once, with fewer bits each time. */
2112 int int_bits = gdbarch_int_bit (par_state->gdbarch ());
2113 int long_bits = gdbarch_long_bit (par_state->gdbarch ());
2114 int long_long_bits = gdbarch_long_long_bit (par_state->gdbarch ());
2115 bool have_signed
2116 /* No 'u' suffix. */
2117 = !unsigned_p;
2118 bool have_unsigned
2119 = ((/* 'u' suffix. */
2120 unsigned_p)
2121 || (/* Not a decimal. */
2122 base != 10)
2123 || (/* Allowed as a convenience, in case decimal doesn't fit in largest
2124 signed type. */
2125 !fits_in_type (1, n, long_long_bits, true)));
2126 bool have_int
2127 /* No 'l' or 'll' suffix. */
2128 = long_p == 0;
2129 bool have_long
2130 /* No 'll' suffix. */
2131 = long_p <= 1;
2132 if (have_int && have_signed && fits_in_type (1, n, int_bits, true))
2133 putithere->typed_val_int.type = parse_type (par_state)->builtin_int;
2134 else if (have_int && have_unsigned && fits_in_type (1, n, int_bits, false))
2135 putithere->typed_val_int.type
2136 = parse_type (par_state)->builtin_unsigned_int;
2137 else if (have_long && have_signed && fits_in_type (1, n, long_bits, true))
2138 putithere->typed_val_int.type = parse_type (par_state)->builtin_long;
2139 else if (have_long && have_unsigned && fits_in_type (1, n, long_bits, false))
2140 putithere->typed_val_int.type
2141 = parse_type (par_state)->builtin_unsigned_long;
2142 else if (have_signed && fits_in_type (1, n, long_long_bits, true))
2143 putithere->typed_val_int.type
2144 = parse_type (par_state)->builtin_long_long;
2145 else if (have_unsigned && fits_in_type (1, n, long_long_bits, false))
2146 putithere->typed_val_int.type
2147 = parse_type (par_state)->builtin_unsigned_long_long;
2148 else
2149 error (_("Numeric constant too large."));
2150 putithere->typed_val_int.val = n;
2151
2152 if (imaginary_p)
2153 putithere->typed_val_int.type
2154 = init_complex_type (nullptr, putithere->typed_val_int.type);
2155
2156 return imaginary_p ? COMPLEX_INT : INT;
2157 }
2158
2159 /* Temporary obstack used for holding strings. */
2160 static struct obstack tempbuf;
2161 static int tempbuf_init;
2162
2163 /* Parse a C escape sequence. The initial backslash of the sequence
2164 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2165 last character of the sequence. If OUTPUT is not NULL, the
2166 translated form of the escape sequence will be written there. If
2167 OUTPUT is NULL, no output is written and the call will only affect
2168 *PTR. If an escape sequence is expressed in target bytes, then the
2169 entire sequence will simply be copied to OUTPUT. Return 1 if any
2170 character was emitted, 0 otherwise. */
2171
2172 int
2173 c_parse_escape (const char **ptr, struct obstack *output)
2174 {
2175 const char *tokptr = *ptr;
2176 int result = 1;
2177
2178 /* Some escape sequences undergo character set conversion. Those we
2179 translate here. */
2180 switch (*tokptr)
2181 {
2182 /* Hex escapes do not undergo character set conversion, so keep
2183 the escape sequence for later. */
2184 case 'x':
2185 if (output)
2186 obstack_grow_str (output, "\\x");
2187 ++tokptr;
2188 if (!ISXDIGIT (*tokptr))
2189 error (_("\\x escape without a following hex digit"));
2190 while (ISXDIGIT (*tokptr))
2191 {
2192 if (output)
2193 obstack_1grow (output, *tokptr);
2194 ++tokptr;
2195 }
2196 break;
2197
2198 /* Octal escapes do not undergo character set conversion, so
2199 keep the escape sequence for later. */
2200 case '0':
2201 case '1':
2202 case '2':
2203 case '3':
2204 case '4':
2205 case '5':
2206 case '6':
2207 case '7':
2208 {
2209 int i;
2210 if (output)
2211 obstack_grow_str (output, "\\");
2212 for (i = 0;
2213 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
2214 ++i)
2215 {
2216 if (output)
2217 obstack_1grow (output, *tokptr);
2218 ++tokptr;
2219 }
2220 }
2221 break;
2222
2223 /* We handle UCNs later. We could handle them here, but that
2224 would mean a spurious error in the case where the UCN could
2225 be converted to the target charset but not the host
2226 charset. */
2227 case 'u':
2228 case 'U':
2229 {
2230 char c = *tokptr;
2231 int i, len = c == 'U' ? 8 : 4;
2232 if (output)
2233 {
2234 obstack_1grow (output, '\\');
2235 obstack_1grow (output, *tokptr);
2236 }
2237 ++tokptr;
2238 if (!ISXDIGIT (*tokptr))
2239 error (_("\\%c escape without a following hex digit"), c);
2240 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
2241 {
2242 if (output)
2243 obstack_1grow (output, *tokptr);
2244 ++tokptr;
2245 }
2246 }
2247 break;
2248
2249 /* We must pass backslash through so that it does not
2250 cause quoting during the second expansion. */
2251 case '\\':
2252 if (output)
2253 obstack_grow_str (output, "\\\\");
2254 ++tokptr;
2255 break;
2256
2257 /* Escapes which undergo conversion. */
2258 case 'a':
2259 if (output)
2260 obstack_1grow (output, '\a');
2261 ++tokptr;
2262 break;
2263 case 'b':
2264 if (output)
2265 obstack_1grow (output, '\b');
2266 ++tokptr;
2267 break;
2268 case 'f':
2269 if (output)
2270 obstack_1grow (output, '\f');
2271 ++tokptr;
2272 break;
2273 case 'n':
2274 if (output)
2275 obstack_1grow (output, '\n');
2276 ++tokptr;
2277 break;
2278 case 'r':
2279 if (output)
2280 obstack_1grow (output, '\r');
2281 ++tokptr;
2282 break;
2283 case 't':
2284 if (output)
2285 obstack_1grow (output, '\t');
2286 ++tokptr;
2287 break;
2288 case 'v':
2289 if (output)
2290 obstack_1grow (output, '\v');
2291 ++tokptr;
2292 break;
2293
2294 /* GCC extension. */
2295 case 'e':
2296 if (output)
2297 obstack_1grow (output, HOST_ESCAPE_CHAR);
2298 ++tokptr;
2299 break;
2300
2301 /* Backslash-newline expands to nothing at all. */
2302 case '\n':
2303 ++tokptr;
2304 result = 0;
2305 break;
2306
2307 /* A few escapes just expand to the character itself. */
2308 case '\'':
2309 case '\"':
2310 case '?':
2311 /* GCC extensions. */
2312 case '(':
2313 case '{':
2314 case '[':
2315 case '%':
2316 /* Unrecognized escapes turn into the character itself. */
2317 default:
2318 if (output)
2319 obstack_1grow (output, *tokptr);
2320 ++tokptr;
2321 break;
2322 }
2323 *ptr = tokptr;
2324 return result;
2325 }
2326
2327 /* Parse a string or character literal from TOKPTR. The string or
2328 character may be wide or unicode. *OUTPTR is set to just after the
2329 end of the literal in the input string. The resulting token is
2330 stored in VALUE. This returns a token value, either STRING or
2331 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2332 number of host characters in the literal. */
2333
2334 static int
2335 parse_string_or_char (const char *tokptr, const char **outptr,
2336 struct typed_stoken *value, int *host_chars)
2337 {
2338 int quote;
2339 c_string_type type;
2340 int is_objc = 0;
2341
2342 /* Build the gdb internal form of the input string in tempbuf. Note
2343 that the buffer is null byte terminated *only* for the
2344 convenience of debugging gdb itself and printing the buffer
2345 contents when the buffer contains no embedded nulls. Gdb does
2346 not depend upon the buffer being null byte terminated, it uses
2347 the length string instead. This allows gdb to handle C strings
2348 (as well as strings in other languages) with embedded null
2349 bytes */
2350
2351 if (!tempbuf_init)
2352 tempbuf_init = 1;
2353 else
2354 obstack_free (&tempbuf, NULL);
2355 obstack_init (&tempbuf);
2356
2357 /* Record the string type. */
2358 if (*tokptr == 'L')
2359 {
2360 type = C_WIDE_STRING;
2361 ++tokptr;
2362 }
2363 else if (*tokptr == 'u')
2364 {
2365 type = C_STRING_16;
2366 ++tokptr;
2367 }
2368 else if (*tokptr == 'U')
2369 {
2370 type = C_STRING_32;
2371 ++tokptr;
2372 }
2373 else if (*tokptr == '@')
2374 {
2375 /* An Objective C string. */
2376 is_objc = 1;
2377 type = C_STRING;
2378 ++tokptr;
2379 }
2380 else
2381 type = C_STRING;
2382
2383 /* Skip the quote. */
2384 quote = *tokptr;
2385 if (quote == '\'')
2386 type |= C_CHAR;
2387 ++tokptr;
2388
2389 *host_chars = 0;
2390
2391 while (*tokptr)
2392 {
2393 char c = *tokptr;
2394 if (c == '\\')
2395 {
2396 ++tokptr;
2397 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2398 }
2399 else if (c == quote)
2400 break;
2401 else
2402 {
2403 obstack_1grow (&tempbuf, c);
2404 ++tokptr;
2405 /* FIXME: this does the wrong thing with multi-byte host
2406 characters. We could use mbrlen here, but that would
2407 make "set host-charset" a bit less useful. */
2408 ++*host_chars;
2409 }
2410 }
2411
2412 if (*tokptr != quote)
2413 {
2414 if (quote == '"')
2415 error (_("Unterminated string in expression."));
2416 else
2417 error (_("Unmatched single quote."));
2418 }
2419 ++tokptr;
2420
2421 value->type = type;
2422 value->ptr = (char *) obstack_base (&tempbuf);
2423 value->length = obstack_object_size (&tempbuf);
2424
2425 *outptr = tokptr;
2426
2427 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2428 }
2429
2430 /* This is used to associate some attributes with a token. */
2431
2432 enum token_flag
2433 {
2434 /* If this bit is set, the token is C++-only. */
2435
2436 FLAG_CXX = 1,
2437
2438 /* If this bit is set, the token is C-only. */
2439
2440 FLAG_C = 2,
2441
2442 /* If this bit is set, the token is conditional: if there is a
2443 symbol of the same name, then the token is a symbol; otherwise,
2444 the token is a keyword. */
2445
2446 FLAG_SHADOW = 4
2447 };
2448 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2449
2450 struct c_token
2451 {
2452 const char *oper;
2453 int token;
2454 enum exp_opcode opcode;
2455 token_flags flags;
2456 };
2457
2458 static const struct c_token tokentab3[] =
2459 {
2460 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2461 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2462 {"->*", ARROW_STAR, OP_NULL, FLAG_CXX},
2463 {"...", DOTDOTDOT, OP_NULL, 0}
2464 };
2465
2466 static const struct c_token tokentab2[] =
2467 {
2468 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2469 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2470 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2471 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2472 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2473 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2474 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2475 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2476 {"++", INCREMENT, OP_NULL, 0},
2477 {"--", DECREMENT, OP_NULL, 0},
2478 {"->", ARROW, OP_NULL, 0},
2479 {"&&", ANDAND, OP_NULL, 0},
2480 {"||", OROR, OP_NULL, 0},
2481 /* "::" is *not* only C++: gdb overrides its meaning in several
2482 different ways, e.g., 'filename'::func, function::variable. */
2483 {"::", COLONCOLON, OP_NULL, 0},
2484 {"<<", LSH, OP_NULL, 0},
2485 {">>", RSH, OP_NULL, 0},
2486 {"==", EQUAL, OP_NULL, 0},
2487 {"!=", NOTEQUAL, OP_NULL, 0},
2488 {"<=", LEQ, OP_NULL, 0},
2489 {">=", GEQ, OP_NULL, 0},
2490 {".*", DOT_STAR, OP_NULL, FLAG_CXX}
2491 };
2492
2493 /* Identifier-like tokens. Only type-specifiers than can appear in
2494 multi-word type names (for example 'double' can appear in 'long
2495 double') need to be listed here. type-specifiers that are only ever
2496 single word (like 'char') are handled by the classify_name function. */
2497 static const struct c_token ident_tokens[] =
2498 {
2499 {"unsigned", UNSIGNED, OP_NULL, 0},
2500 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2501 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2502 {"struct", STRUCT, OP_NULL, 0},
2503 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2504 {"sizeof", SIZEOF, OP_NULL, 0},
2505 {"_Alignof", ALIGNOF, OP_NULL, 0},
2506 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
2507 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2508 {"float", FLOAT_KEYWORD, OP_NULL, 0},
2509 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2510 {"class", CLASS, OP_NULL, FLAG_CXX},
2511 {"union", UNION, OP_NULL, 0},
2512 {"short", SHORT, OP_NULL, 0},
2513 {"const", CONST_KEYWORD, OP_NULL, 0},
2514 {"restrict", RESTRICT, OP_NULL, FLAG_C | FLAG_SHADOW},
2515 {"__restrict__", RESTRICT, OP_NULL, 0},
2516 {"__restrict", RESTRICT, OP_NULL, 0},
2517 {"_Atomic", ATOMIC, OP_NULL, 0},
2518 {"enum", ENUM, OP_NULL, 0},
2519 {"long", LONG, OP_NULL, 0},
2520 {"_Complex", COMPLEX, OP_NULL, 0},
2521 {"__complex__", COMPLEX, OP_NULL, 0},
2522
2523 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2524 {"int", INT_KEYWORD, OP_NULL, 0},
2525 {"new", NEW, OP_NULL, FLAG_CXX},
2526 {"delete", DELETE, OP_NULL, FLAG_CXX},
2527 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2528
2529 {"and", ANDAND, OP_NULL, FLAG_CXX},
2530 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2531 {"bitand", '&', OP_NULL, FLAG_CXX},
2532 {"bitor", '|', OP_NULL, FLAG_CXX},
2533 {"compl", '~', OP_NULL, FLAG_CXX},
2534 {"not", '!', OP_NULL, FLAG_CXX},
2535 {"not_eq", NOTEQUAL, OP_NULL, FLAG_CXX},
2536 {"or", OROR, OP_NULL, FLAG_CXX},
2537 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2538 {"xor", '^', OP_NULL, FLAG_CXX},
2539 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2540
2541 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2542 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2543 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2544 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2545
2546 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2547 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2548 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2549 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2550 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2551
2552 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2553 };
2554
2555
2556 static void
2557 scan_macro_expansion (const char *expansion)
2558 {
2559 /* We'd better not be trying to push the stack twice. */
2560 gdb_assert (! cpstate->macro_original_text);
2561
2562 /* Copy to the obstack. */
2563 const char *copy = obstack_strdup (&cpstate->expansion_obstack, expansion);
2564
2565 /* Save the old lexptr value, so we can return to it when we're done
2566 parsing the expanded text. */
2567 cpstate->macro_original_text = pstate->lexptr;
2568 pstate->lexptr = copy;
2569 }
2570
2571 static int
2572 scanning_macro_expansion (void)
2573 {
2574 return cpstate->macro_original_text != 0;
2575 }
2576
2577 static void
2578 finished_macro_expansion (void)
2579 {
2580 /* There'd better be something to pop back to. */
2581 gdb_assert (cpstate->macro_original_text);
2582
2583 /* Pop back to the original text. */
2584 pstate->lexptr = cpstate->macro_original_text;
2585 cpstate->macro_original_text = 0;
2586 }
2587
2588 /* Return true iff the token represents a C++ cast operator. */
2589
2590 static int
2591 is_cast_operator (const char *token, int len)
2592 {
2593 return (! strncmp (token, "dynamic_cast", len)
2594 || ! strncmp (token, "static_cast", len)
2595 || ! strncmp (token, "reinterpret_cast", len)
2596 || ! strncmp (token, "const_cast", len));
2597 }
2598
2599 /* The scope used for macro expansion. */
2600 static struct macro_scope *expression_macro_scope;
2601
2602 /* This is set if a NAME token appeared at the very end of the input
2603 string, with no whitespace separating the name from the EOF. This
2604 is used only when parsing to do field name completion. */
2605 static int saw_name_at_eof;
2606
2607 /* This is set if the previously-returned token was a structure
2608 operator -- either '.' or ARROW. */
2609 static bool last_was_structop;
2610
2611 /* Depth of parentheses. */
2612 static int paren_depth;
2613
2614 /* Read one token, getting characters through lexptr. */
2615
2616 static int
2617 lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
2618 {
2619 int c;
2620 int namelen;
2621 const char *tokstart;
2622 bool saw_structop = last_was_structop;
2623
2624 last_was_structop = false;
2625 *is_quoted_name = false;
2626
2627 retry:
2628
2629 /* Check if this is a macro invocation that we need to expand. */
2630 if (! scanning_macro_expansion ())
2631 {
2632 gdb::unique_xmalloc_ptr<char> expanded
2633 = macro_expand_next (&pstate->lexptr, *expression_macro_scope);
2634
2635 if (expanded != nullptr)
2636 scan_macro_expansion (expanded.get ());
2637 }
2638
2639 pstate->prev_lexptr = pstate->lexptr;
2640
2641 tokstart = pstate->lexptr;
2642 /* See if it is a special token of length 3. */
2643 for (const auto &token : tokentab3)
2644 if (strncmp (tokstart, token.oper, 3) == 0)
2645 {
2646 if ((token.flags & FLAG_CXX) != 0
2647 && par_state->language ()->la_language != language_cplus)
2648 break;
2649 gdb_assert ((token.flags & FLAG_C) == 0);
2650
2651 pstate->lexptr += 3;
2652 yylval.opcode = token.opcode;
2653 return token.token;
2654 }
2655
2656 /* See if it is a special token of length 2. */
2657 for (const auto &token : tokentab2)
2658 if (strncmp (tokstart, token.oper, 2) == 0)
2659 {
2660 if ((token.flags & FLAG_CXX) != 0
2661 && par_state->language ()->la_language != language_cplus)
2662 break;
2663 gdb_assert ((token.flags & FLAG_C) == 0);
2664
2665 pstate->lexptr += 2;
2666 yylval.opcode = token.opcode;
2667 if (token.token == ARROW)
2668 last_was_structop = 1;
2669 return token.token;
2670 }
2671
2672 switch (c = *tokstart)
2673 {
2674 case 0:
2675 /* If we were just scanning the result of a macro expansion,
2676 then we need to resume scanning the original text.
2677 If we're parsing for field name completion, and the previous
2678 token allows such completion, return a COMPLETE token.
2679 Otherwise, we were already scanning the original text, and
2680 we're really done. */
2681 if (scanning_macro_expansion ())
2682 {
2683 finished_macro_expansion ();
2684 goto retry;
2685 }
2686 else if (saw_name_at_eof)
2687 {
2688 saw_name_at_eof = 0;
2689 return COMPLETE;
2690 }
2691 else if (par_state->parse_completion && saw_structop)
2692 return COMPLETE;
2693 else
2694 return 0;
2695
2696 case ' ':
2697 case '\t':
2698 case '\n':
2699 pstate->lexptr++;
2700 goto retry;
2701
2702 case '[':
2703 case '(':
2704 paren_depth++;
2705 pstate->lexptr++;
2706 if (par_state->language ()->la_language == language_objc
2707 && c == '[')
2708 return OBJC_LBRAC;
2709 return c;
2710
2711 case ']':
2712 case ')':
2713 if (paren_depth == 0)
2714 return 0;
2715 paren_depth--;
2716 pstate->lexptr++;
2717 return c;
2718
2719 case ',':
2720 if (pstate->comma_terminates
2721 && paren_depth == 0
2722 && ! scanning_macro_expansion ())
2723 return 0;
2724 pstate->lexptr++;
2725 return c;
2726
2727 case '.':
2728 /* Might be a floating point number. */
2729 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
2730 {
2731 last_was_structop = true;
2732 goto symbol; /* Nope, must be a symbol. */
2733 }
2734 /* FALL THRU. */
2735
2736 case '0':
2737 case '1':
2738 case '2':
2739 case '3':
2740 case '4':
2741 case '5':
2742 case '6':
2743 case '7':
2744 case '8':
2745 case '9':
2746 {
2747 /* It's a number. */
2748 int got_dot = 0, got_e = 0, got_p = 0, toktype;
2749 const char *p = tokstart;
2750 int hex = input_radix > 10;
2751
2752 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2753 {
2754 p += 2;
2755 hex = 1;
2756 }
2757 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2758 {
2759 p += 2;
2760 hex = 0;
2761 }
2762
2763 for (;; ++p)
2764 {
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 == '+'))
2779 /* This is the sign of the exponent, not the end of the
2780 number. */
2781 continue;
2782 /* We will take any letters or digits. parse_number will
2783 complain if past the radix, or if L or U are not final. */
2784 else if ((*p < '0' || *p > '9')
2785 && ((*p < 'a' || *p > 'z')
2786 && (*p < 'A' || *p > 'Z')))
2787 break;
2788 }
2789 toktype = parse_number (par_state, tokstart, p - tokstart,
2790 got_dot | got_e | got_p, &yylval);
2791 if (toktype == ERROR)
2792 {
2793 char *err_copy = (char *) alloca (p - tokstart + 1);
2794
2795 memcpy (err_copy, tokstart, p - tokstart);
2796 err_copy[p - tokstart] = 0;
2797 error (_("Invalid number \"%s\"."), err_copy);
2798 }
2799 pstate->lexptr = p;
2800 return toktype;
2801 }
2802
2803 case '@':
2804 {
2805 const char *p = &tokstart[1];
2806
2807 if (par_state->language ()->la_language == language_objc)
2808 {
2809 size_t len = strlen ("selector");
2810
2811 if (strncmp (p, "selector", len) == 0
2812 && (p[len] == '\0' || ISSPACE (p[len])))
2813 {
2814 pstate->lexptr = p + len;
2815 return SELECTOR;
2816 }
2817 else if (*p == '"')
2818 goto parse_string;
2819 }
2820
2821 while (ISSPACE (*p))
2822 p++;
2823 size_t len = strlen ("entry");
2824 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
2825 && p[len] != '_')
2826 {
2827 pstate->lexptr = &p[len];
2828 return ENTRY;
2829 }
2830 }
2831 /* FALLTHRU */
2832 case '+':
2833 case '-':
2834 case '*':
2835 case '/':
2836 case '%':
2837 case '|':
2838 case '&':
2839 case '^':
2840 case '~':
2841 case '!':
2842 case '<':
2843 case '>':
2844 case '?':
2845 case ':':
2846 case '=':
2847 case '{':
2848 case '}':
2849 symbol:
2850 pstate->lexptr++;
2851 return c;
2852
2853 case 'L':
2854 case 'u':
2855 case 'U':
2856 if (tokstart[1] != '"' && tokstart[1] != '\'')
2857 break;
2858 /* Fall through. */
2859 case '\'':
2860 case '"':
2861
2862 parse_string:
2863 {
2864 int host_len;
2865 int result = parse_string_or_char (tokstart, &pstate->lexptr,
2866 &yylval.tsval, &host_len);
2867 if (result == CHAR)
2868 {
2869 if (host_len == 0)
2870 error (_("Empty character constant."));
2871 else if (host_len > 2 && c == '\'')
2872 {
2873 ++tokstart;
2874 namelen = pstate->lexptr - tokstart - 1;
2875 *is_quoted_name = true;
2876
2877 goto tryname;
2878 }
2879 else if (host_len > 1)
2880 error (_("Invalid character constant."));
2881 }
2882 return result;
2883 }
2884 }
2885
2886 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
2887 /* We must have come across a bad character (e.g. ';'). */
2888 error (_("Invalid character '%c' in expression."), c);
2889
2890 /* It's a name. See how long it is. */
2891 namelen = 0;
2892 for (c = tokstart[namelen];
2893 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
2894 {
2895 /* Template parameter lists are part of the name.
2896 FIXME: This mishandles `print $a<4&&$a>3'. */
2897
2898 if (c == '<')
2899 {
2900 if (! is_cast_operator (tokstart, namelen))
2901 {
2902 /* Scan ahead to get rest of the template specification. Note
2903 that we look ahead only when the '<' adjoins non-whitespace
2904 characters; for comparison expressions, e.g. "a < b > c",
2905 there must be spaces before the '<', etc. */
2906 const char *p = find_template_name_end (tokstart + namelen);
2907
2908 if (p)
2909 namelen = p - tokstart;
2910 }
2911 break;
2912 }
2913 c = tokstart[++namelen];
2914 }
2915
2916 /* The token "if" terminates the expression and is NOT removed from
2917 the input stream. It doesn't count if it appears in the
2918 expansion of a macro. */
2919 if (namelen == 2
2920 && tokstart[0] == 'i'
2921 && tokstart[1] == 'f'
2922 && ! scanning_macro_expansion ())
2923 {
2924 return 0;
2925 }
2926
2927 /* For the same reason (breakpoint conditions), "thread N"
2928 terminates the expression. "thread" could be an identifier, but
2929 an identifier is never followed by a number without intervening
2930 punctuation. "task" is similar. Handle abbreviations of these,
2931 similarly to breakpoint.c:find_condition_and_thread. */
2932 if (namelen >= 1
2933 && (strncmp (tokstart, "thread", namelen) == 0
2934 || strncmp (tokstart, "task", namelen) == 0)
2935 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2936 && ! scanning_macro_expansion ())
2937 {
2938 const char *p = tokstart + namelen + 1;
2939
2940 while (*p == ' ' || *p == '\t')
2941 p++;
2942 if (*p >= '0' && *p <= '9')
2943 return 0;
2944 }
2945
2946 pstate->lexptr += namelen;
2947
2948 tryname:
2949
2950 yylval.sval.ptr = tokstart;
2951 yylval.sval.length = namelen;
2952
2953 /* Catch specific keywords. */
2954 std::string copy = copy_name (yylval.sval);
2955 for (const auto &token : ident_tokens)
2956 if (copy == token.oper)
2957 {
2958 if ((token.flags & FLAG_CXX) != 0
2959 && par_state->language ()->la_language != language_cplus)
2960 break;
2961 if ((token.flags & FLAG_C) != 0
2962 && par_state->language ()->la_language != language_c
2963 && par_state->language ()->la_language != language_objc)
2964 break;
2965
2966 if ((token.flags & FLAG_SHADOW) != 0)
2967 {
2968 struct field_of_this_result is_a_field_of_this;
2969
2970 if (lookup_symbol (copy.c_str (),
2971 pstate->expression_context_block,
2972 VAR_DOMAIN,
2973 (par_state->language ()->la_language
2974 == language_cplus ? &is_a_field_of_this
2975 : NULL)).symbol
2976 != NULL)
2977 {
2978 /* The keyword is shadowed. */
2979 break;
2980 }
2981 }
2982
2983 /* It is ok to always set this, even though we don't always
2984 strictly need to. */
2985 yylval.opcode = token.opcode;
2986 return token.token;
2987 }
2988
2989 if (*tokstart == '$')
2990 return DOLLAR_VARIABLE;
2991
2992 if (pstate->parse_completion && *pstate->lexptr == '\0')
2993 saw_name_at_eof = 1;
2994
2995 yylval.ssym.stoken = yylval.sval;
2996 yylval.ssym.sym.symbol = NULL;
2997 yylval.ssym.sym.block = NULL;
2998 yylval.ssym.is_a_field_of_this = 0;
2999 return NAME;
3000 }
3001
3002 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
3003 struct c_token_and_value
3004 {
3005 int token;
3006 YYSTYPE value;
3007 };
3008
3009 /* A FIFO of tokens that have been read but not yet returned to the
3010 parser. */
3011 static std::vector<c_token_and_value> token_fifo;
3012
3013 /* Non-zero if the lexer should return tokens from the FIFO. */
3014 static int popping;
3015
3016 /* Temporary storage for c_lex; this holds symbol names as they are
3017 built up. */
3018 static auto_obstack name_obstack;
3019
3020 /* Classify a NAME token. The contents of the token are in `yylval'.
3021 Updates yylval and returns the new token type. BLOCK is the block
3022 in which lookups start; this can be NULL to mean the global scope.
3023 IS_QUOTED_NAME is non-zero if the name token was originally quoted
3024 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
3025 a structure operator -- either '.' or ARROW */
3026
3027 static int
3028 classify_name (struct parser_state *par_state, const struct block *block,
3029 bool is_quoted_name, bool is_after_structop)
3030 {
3031 struct block_symbol bsym;
3032 struct field_of_this_result is_a_field_of_this;
3033
3034 std::string copy = copy_name (yylval.sval);
3035
3036 /* Initialize this in case we *don't* use it in this call; that way
3037 we can refer to it unconditionally below. */
3038 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
3039
3040 bsym = lookup_symbol (copy.c_str (), block, VAR_DOMAIN,
3041 par_state->language ()->name_of_this ()
3042 ? &is_a_field_of_this : NULL);
3043
3044 if (bsym.symbol && bsym.symbol->aclass () == LOC_BLOCK)
3045 {
3046 yylval.ssym.sym = bsym;
3047 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3048 return BLOCKNAME;
3049 }
3050 else if (!bsym.symbol)
3051 {
3052 /* If we found a field of 'this', we might have erroneously
3053 found a constructor where we wanted a type name. Handle this
3054 case by noticing that we found a constructor and then look up
3055 the type tag instead. */
3056 if (is_a_field_of_this.type != NULL
3057 && is_a_field_of_this.fn_field != NULL
3058 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
3059 0))
3060 {
3061 struct field_of_this_result inner_is_a_field_of_this;
3062
3063 bsym = lookup_symbol (copy.c_str (), block, STRUCT_DOMAIN,
3064 &inner_is_a_field_of_this);
3065 if (bsym.symbol != NULL)
3066 {
3067 yylval.tsym.type = bsym.symbol->type ();
3068 return TYPENAME;
3069 }
3070 }
3071
3072 /* If we found a field on the "this" object, or we are looking
3073 up a field on a struct, then we want to prefer it over a
3074 filename. However, if the name was quoted, then it is better
3075 to check for a filename or a block, since this is the only
3076 way the user has of requiring the extension to be used. */
3077 if ((is_a_field_of_this.type == NULL && !is_after_structop)
3078 || is_quoted_name)
3079 {
3080 /* See if it's a file name. */
3081 struct symtab *symtab;
3082
3083 symtab = lookup_symtab (copy.c_str ());
3084 if (symtab)
3085 {
3086 yylval.bval
3087 = symtab->compunit ()->blockvector ()->static_block ();
3088
3089 return FILENAME;
3090 }
3091 }
3092 }
3093
3094 if (bsym.symbol && bsym.symbol->aclass () == LOC_TYPEDEF)
3095 {
3096 yylval.tsym.type = bsym.symbol->type ();
3097 return TYPENAME;
3098 }
3099
3100 /* See if it's an ObjC classname. */
3101 if (par_state->language ()->la_language == language_objc && !bsym.symbol)
3102 {
3103 CORE_ADDR Class = lookup_objc_class (par_state->gdbarch (),
3104 copy.c_str ());
3105 if (Class)
3106 {
3107 struct symbol *sym;
3108
3109 yylval.theclass.theclass = Class;
3110 sym = lookup_struct_typedef (copy.c_str (),
3111 par_state->expression_context_block, 1);
3112 if (sym)
3113 yylval.theclass.type = sym->type ();
3114 return CLASSNAME;
3115 }
3116 }
3117
3118 /* Input names that aren't symbols but ARE valid hex numbers, when
3119 the input radix permits them, can be names or numbers depending
3120 on the parse. Note we support radixes > 16 here. */
3121 if (!bsym.symbol
3122 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3123 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3124 {
3125 YYSTYPE newlval; /* Its value is ignored. */
3126 int hextype = parse_number (par_state, copy.c_str (), yylval.sval.length,
3127 0, &newlval);
3128
3129 if (hextype == INT)
3130 {
3131 yylval.ssym.sym = bsym;
3132 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3133 return NAME_OR_INT;
3134 }
3135 }
3136
3137 /* Any other kind of symbol */
3138 yylval.ssym.sym = bsym;
3139 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3140
3141 if (bsym.symbol == NULL
3142 && par_state->language ()->la_language == language_cplus
3143 && is_a_field_of_this.type == NULL
3144 && lookup_minimal_symbol (copy.c_str (), NULL, NULL).minsym == NULL)
3145 return UNKNOWN_CPP_NAME;
3146
3147 return NAME;
3148 }
3149
3150 /* Like classify_name, but used by the inner loop of the lexer, when a
3151 name might have already been seen. CONTEXT is the context type, or
3152 NULL if this is the first component of a name. */
3153
3154 static int
3155 classify_inner_name (struct parser_state *par_state,
3156 const struct block *block, struct type *context)
3157 {
3158 struct type *type;
3159
3160 if (context == NULL)
3161 return classify_name (par_state, block, false, false);
3162
3163 type = check_typedef (context);
3164 if (!type_aggregate_p (type))
3165 return ERROR;
3166
3167 std::string copy = copy_name (yylval.ssym.stoken);
3168 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3169 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy.c_str (), block,
3170 VAR_DOMAIN);
3171
3172 /* If no symbol was found, search for a matching base class named
3173 COPY. This will allow users to enter qualified names of class members
3174 relative to the `this' pointer. */
3175 if (yylval.ssym.sym.symbol == NULL)
3176 {
3177 struct type *base_type = cp_find_type_baseclass_by_name (type,
3178 copy.c_str ());
3179
3180 if (base_type != NULL)
3181 {
3182 yylval.tsym.type = base_type;
3183 return TYPENAME;
3184 }
3185
3186 return ERROR;
3187 }
3188
3189 switch (yylval.ssym.sym.symbol->aclass ())
3190 {
3191 case LOC_BLOCK:
3192 case LOC_LABEL:
3193 /* cp_lookup_nested_symbol might have accidentally found a constructor
3194 named COPY when we really wanted a base class of the same name.
3195 Double-check this case by looking for a base class. */
3196 {
3197 struct type *base_type
3198 = cp_find_type_baseclass_by_name (type, copy.c_str ());
3199
3200 if (base_type != NULL)
3201 {
3202 yylval.tsym.type = base_type;
3203 return TYPENAME;
3204 }
3205 }
3206 return ERROR;
3207
3208 case LOC_TYPEDEF:
3209 yylval.tsym.type = yylval.ssym.sym.symbol->type ();
3210 return TYPENAME;
3211
3212 default:
3213 return NAME;
3214 }
3215 internal_error (_("not reached"));
3216 }
3217
3218 /* The outer level of a two-level lexer. This calls the inner lexer
3219 to return tokens. It then either returns these tokens, or
3220 aggregates them into a larger token. This lets us work around a
3221 problem in our parsing approach, where the parser could not
3222 distinguish between qualified names and qualified types at the
3223 right point.
3224
3225 This approach is still not ideal, because it mishandles template
3226 types. See the comment in lex_one_token for an example. However,
3227 this is still an improvement over the earlier approach, and will
3228 suffice until we move to better parsing technology. */
3229
3230 static int
3231 yylex (void)
3232 {
3233 c_token_and_value current;
3234 int first_was_coloncolon, last_was_coloncolon;
3235 struct type *context_type = NULL;
3236 int last_to_examine, next_to_examine, checkpoint;
3237 const struct block *search_block;
3238 bool is_quoted_name, last_lex_was_structop;
3239
3240 if (popping && !token_fifo.empty ())
3241 goto do_pop;
3242 popping = 0;
3243
3244 last_lex_was_structop = last_was_structop;
3245
3246 /* Read the first token and decide what to do. Most of the
3247 subsequent code is C++-only; but also depends on seeing a "::" or
3248 name-like token. */
3249 current.token = lex_one_token (pstate, &is_quoted_name);
3250 if (current.token == NAME)
3251 current.token = classify_name (pstate, pstate->expression_context_block,
3252 is_quoted_name, last_lex_was_structop);
3253 if (pstate->language ()->la_language != language_cplus
3254 || (current.token != TYPENAME && current.token != COLONCOLON
3255 && current.token != FILENAME))
3256 return current.token;
3257
3258 /* Read any sequence of alternating "::" and name-like tokens into
3259 the token FIFO. */
3260 current.value = yylval;
3261 token_fifo.push_back (current);
3262 last_was_coloncolon = current.token == COLONCOLON;
3263 while (1)
3264 {
3265 bool ignore;
3266
3267 /* We ignore quoted names other than the very first one.
3268 Subsequent ones do not have any special meaning. */
3269 current.token = lex_one_token (pstate, &ignore);
3270 current.value = yylval;
3271 token_fifo.push_back (current);
3272
3273 if ((last_was_coloncolon && current.token != NAME)
3274 || (!last_was_coloncolon && current.token != COLONCOLON))
3275 break;
3276 last_was_coloncolon = !last_was_coloncolon;
3277 }
3278 popping = 1;
3279
3280 /* We always read one extra token, so compute the number of tokens
3281 to examine accordingly. */
3282 last_to_examine = token_fifo.size () - 2;
3283 next_to_examine = 0;
3284
3285 current = token_fifo[next_to_examine];
3286 ++next_to_examine;
3287
3288 name_obstack.clear ();
3289 checkpoint = 0;
3290 if (current.token == FILENAME)
3291 search_block = current.value.bval;
3292 else if (current.token == COLONCOLON)
3293 search_block = NULL;
3294 else
3295 {
3296 gdb_assert (current.token == TYPENAME);
3297 search_block = pstate->expression_context_block;
3298 obstack_grow (&name_obstack, current.value.sval.ptr,
3299 current.value.sval.length);
3300 context_type = current.value.tsym.type;
3301 checkpoint = 1;
3302 }
3303
3304 first_was_coloncolon = current.token == COLONCOLON;
3305 last_was_coloncolon = first_was_coloncolon;
3306
3307 while (next_to_examine <= last_to_examine)
3308 {
3309 c_token_and_value next;
3310
3311 next = token_fifo[next_to_examine];
3312 ++next_to_examine;
3313
3314 if (next.token == NAME && last_was_coloncolon)
3315 {
3316 int classification;
3317
3318 yylval = next.value;
3319 classification = classify_inner_name (pstate, search_block,
3320 context_type);
3321 /* We keep going until we either run out of names, or until
3322 we have a qualified name which is not a type. */
3323 if (classification != TYPENAME && classification != NAME)
3324 break;
3325
3326 /* Accept up to this token. */
3327 checkpoint = next_to_examine;
3328
3329 /* Update the partial name we are constructing. */
3330 if (context_type != NULL)
3331 {
3332 /* We don't want to put a leading "::" into the name. */
3333 obstack_grow_str (&name_obstack, "::");
3334 }
3335 obstack_grow (&name_obstack, next.value.sval.ptr,
3336 next.value.sval.length);
3337
3338 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
3339 yylval.sval.length = obstack_object_size (&name_obstack);
3340 current.value = yylval;
3341 current.token = classification;
3342
3343 last_was_coloncolon = 0;
3344
3345 if (classification == NAME)
3346 break;
3347
3348 context_type = yylval.tsym.type;
3349 }
3350 else if (next.token == COLONCOLON && !last_was_coloncolon)
3351 last_was_coloncolon = 1;
3352 else
3353 {
3354 /* We've reached the end of the name. */
3355 break;
3356 }
3357 }
3358
3359 /* If we have a replacement token, install it as the first token in
3360 the FIFO, and delete the other constituent tokens. */
3361 if (checkpoint > 0)
3362 {
3363 current.value.sval.ptr
3364 = obstack_strndup (&cpstate->expansion_obstack,
3365 current.value.sval.ptr,
3366 current.value.sval.length);
3367
3368 token_fifo[0] = current;
3369 if (checkpoint > 1)
3370 token_fifo.erase (token_fifo.begin () + 1,
3371 token_fifo.begin () + checkpoint);
3372 }
3373
3374 do_pop:
3375 current = token_fifo[0];
3376 token_fifo.erase (token_fifo.begin ());
3377 yylval = current.value;
3378 return current.token;
3379 }
3380
3381 int
3382 c_parse (struct parser_state *par_state)
3383 {
3384 /* Setting up the parser state. */
3385 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3386 gdb_assert (par_state != NULL);
3387 pstate = par_state;
3388
3389 c_parse_state cstate;
3390 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3391
3392 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
3393
3394 if (par_state->expression_context_block)
3395 macro_scope
3396 = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
3397 else
3398 macro_scope = default_macro_scope ();
3399 if (! macro_scope)
3400 macro_scope = user_macro_scope ();
3401
3402 scoped_restore restore_macro_scope
3403 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
3404
3405 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3406 par_state->debug);
3407
3408 /* Initialize some state used by the lexer. */
3409 last_was_structop = false;
3410 saw_name_at_eof = 0;
3411 paren_depth = 0;
3412
3413 token_fifo.clear ();
3414 popping = 0;
3415 name_obstack.clear ();
3416
3417 int result = yyparse ();
3418 if (!result)
3419 pstate->set_operation (pstate->pop ());
3420 return result;
3421 }
3422
3423 #if defined(YYBISON) && YYBISON < 30800
3424
3425
3426 /* This is called via the YYPRINT macro when parser debugging is
3427 enabled. It prints a token's value. */
3428
3429 static void
3430 c_print_token (FILE *file, int type, YYSTYPE value)
3431 {
3432 switch (type)
3433 {
3434 case INT:
3435 parser_fprintf (file, "typed_val_int<%s, %s>",
3436 TYPE_SAFE_NAME (value.typed_val_int.type),
3437 pulongest (value.typed_val_int.val));
3438 break;
3439
3440 case CHAR:
3441 case STRING:
3442 {
3443 char *copy = (char *) alloca (value.tsval.length + 1);
3444
3445 memcpy (copy, value.tsval.ptr, value.tsval.length);
3446 copy[value.tsval.length] = '\0';
3447
3448 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3449 }
3450 break;
3451
3452 case NSSTRING:
3453 case DOLLAR_VARIABLE:
3454 parser_fprintf (file, "sval<%s>", copy_name (value.sval).c_str ());
3455 break;
3456
3457 case TYPENAME:
3458 parser_fprintf (file, "tsym<type=%s, name=%s>",
3459 TYPE_SAFE_NAME (value.tsym.type),
3460 copy_name (value.tsym.stoken).c_str ());
3461 break;
3462
3463 case NAME:
3464 case UNKNOWN_CPP_NAME:
3465 case NAME_OR_INT:
3466 case BLOCKNAME:
3467 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3468 copy_name (value.ssym.stoken).c_str (),
3469 (value.ssym.sym.symbol == NULL
3470 ? "(null)" : value.ssym.sym.symbol->print_name ()),
3471 value.ssym.is_a_field_of_this);
3472 break;
3473
3474 case FILENAME:
3475 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3476 break;
3477 }
3478 }
3479
3480 #endif
3481
3482 static void
3483 yyerror (const char *msg)
3484 {
3485 if (pstate->prev_lexptr)
3486 pstate->lexptr = pstate->prev_lexptr;
3487
3488 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
3489 }