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