gdb/testsuite: make test names unique in gdb.python/py-format-string.exp
[deliverable/binutils-gdb.git] / gdb / c-exp.y
1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986-2021 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 #ifdef YYBISON
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 $$ = SYMBOL_BLOCK_VALUE ($1.sym.symbol);
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 || SYMBOL_CLASS (tem) != LOC_BLOCK)
1097 error (_("No function \"%s\" in specified context."),
1098 copy.c_str ());
1099 $$ = SYMBOL_BLOCK_VALUE (tem); }
1100 ;
1101
1102 variable: name_not_typename ENTRY
1103 { struct symbol *sym = $1.sym.symbol;
1104
1105 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
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.symbol,
1129 sym.block);
1130 }
1131 ;
1132
1133 qualified_name: TYPENAME COLONCOLON name
1134 {
1135 struct type *type = $1.type;
1136 type = check_typedef (type);
1137 if (!type_aggregate_p (type))
1138 error (_("`%s' is not defined as an aggregate type."),
1139 TYPE_SAFE_NAME (type));
1140
1141 pstate->push_new<scope_operation> (type,
1142 copy_name ($3));
1143 }
1144 | TYPENAME COLONCOLON '~' name
1145 {
1146 struct type *type = $1.type;
1147
1148 type = check_typedef (type);
1149 if (!type_aggregate_p (type))
1150 error (_("`%s' is not defined as an aggregate type."),
1151 TYPE_SAFE_NAME (type));
1152 std::string name = "~" + std::string ($4.ptr,
1153 $4.length);
1154
1155 /* Check for valid destructor name. */
1156 destructor_name_p (name.c_str (), $1.type);
1157 pstate->push_new<scope_operation> (type,
1158 std::move (name));
1159 }
1160 | TYPENAME COLONCOLON name COLONCOLON name
1161 {
1162 std::string copy = copy_name ($3);
1163 error (_("No type \"%s\" within class "
1164 "or namespace \"%s\"."),
1165 copy.c_str (), TYPE_SAFE_NAME ($1.type));
1166 }
1167 ;
1168
1169 variable: qualified_name
1170 | COLONCOLON name_not_typename
1171 {
1172 std::string name = copy_name ($2.stoken);
1173 struct block_symbol sym
1174 = lookup_symbol (name.c_str (),
1175 (const struct block *) NULL,
1176 VAR_DOMAIN, NULL);
1177 pstate->push_symbol (name.c_str (), sym);
1178 }
1179 ;
1180
1181 variable: name_not_typename
1182 { struct block_symbol sym = $1.sym;
1183
1184 if (sym.symbol)
1185 {
1186 if (symbol_read_needs_frame (sym.symbol))
1187 pstate->block_tracker->update (sym);
1188
1189 /* If we found a function, see if it's
1190 an ifunc resolver that has the same
1191 address as the ifunc symbol itself.
1192 If so, prefer the ifunc symbol. */
1193
1194 bound_minimal_symbol resolver
1195 = find_gnu_ifunc (sym.symbol);
1196 if (resolver.minsym != NULL)
1197 pstate->push_new<var_msym_value_operation>
1198 (resolver);
1199 else
1200 pstate->push_new<var_value_operation>
1201 (sym.symbol, sym.block);
1202 }
1203 else if ($1.is_a_field_of_this)
1204 {
1205 /* C++: it hangs off of `this'. Must
1206 not inadvertently convert from a method call
1207 to data ref. */
1208 pstate->block_tracker->update (sym);
1209 operation_up thisop
1210 = make_operation<op_this_operation> ();
1211 pstate->push_new<structop_ptr_operation>
1212 (std::move (thisop), copy_name ($1.stoken));
1213 }
1214 else
1215 {
1216 std::string arg = copy_name ($1.stoken);
1217
1218 bound_minimal_symbol msymbol
1219 = lookup_bound_minimal_symbol (arg.c_str ());
1220 if (msymbol.minsym == NULL)
1221 {
1222 if (!have_full_symbols () && !have_partial_symbols ())
1223 error (_("No symbol table is loaded. Use the \"file\" command."));
1224 else
1225 error (_("No symbol \"%s\" in current context."),
1226 arg.c_str ());
1227 }
1228
1229 /* This minsym might be an alias for
1230 another function. See if we can find
1231 the debug symbol for the target, and
1232 if so, use it instead, since it has
1233 return type / prototype info. This
1234 is important for example for "p
1235 *__errno_location()". */
1236 symbol *alias_target
1237 = ((msymbol.minsym->type != mst_text_gnu_ifunc
1238 && msymbol.minsym->type != mst_data_gnu_ifunc)
1239 ? find_function_alias_target (msymbol)
1240 : NULL);
1241 if (alias_target != NULL)
1242 pstate->push_new<var_value_operation>
1243 (alias_target, SYMBOL_BLOCK_VALUE (alias_target));
1244 else
1245 pstate->push_new<var_msym_value_operation>
1246 (msymbol);
1247 }
1248 }
1249 ;
1250
1251 const_or_volatile: const_or_volatile_noopt
1252 |
1253 ;
1254
1255 single_qualifier:
1256 CONST_KEYWORD
1257 { cpstate->type_stack.insert (tp_const); }
1258 | VOLATILE_KEYWORD
1259 { cpstate->type_stack.insert (tp_volatile); }
1260 | ATOMIC
1261 { cpstate->type_stack.insert (tp_atomic); }
1262 | RESTRICT
1263 { cpstate->type_stack.insert (tp_restrict); }
1264 | '@' NAME
1265 {
1266 cpstate->type_stack.insert (pstate,
1267 copy_name ($2.stoken).c_str ());
1268 }
1269 ;
1270
1271 qualifier_seq_noopt:
1272 single_qualifier
1273 | qualifier_seq single_qualifier
1274 ;
1275
1276 qualifier_seq:
1277 qualifier_seq_noopt
1278 |
1279 ;
1280
1281 ptr_operator:
1282 ptr_operator '*'
1283 { cpstate->type_stack.insert (tp_pointer); }
1284 qualifier_seq
1285 | '*'
1286 { cpstate->type_stack.insert (tp_pointer); }
1287 qualifier_seq
1288 | '&'
1289 { cpstate->type_stack.insert (tp_reference); }
1290 | '&' ptr_operator
1291 { cpstate->type_stack.insert (tp_reference); }
1292 | ANDAND
1293 { cpstate->type_stack.insert (tp_rvalue_reference); }
1294 | ANDAND ptr_operator
1295 { cpstate->type_stack.insert (tp_rvalue_reference); }
1296 ;
1297
1298 ptr_operator_ts: ptr_operator
1299 {
1300 $$ = cpstate->type_stack.create ();
1301 cpstate->type_stacks.emplace_back ($$);
1302 }
1303 ;
1304
1305 abs_decl: ptr_operator_ts direct_abs_decl
1306 { $$ = $2->append ($1); }
1307 | ptr_operator_ts
1308 | direct_abs_decl
1309 ;
1310
1311 direct_abs_decl: '(' abs_decl ')'
1312 { $$ = $2; }
1313 | direct_abs_decl array_mod
1314 {
1315 cpstate->type_stack.push ($1);
1316 cpstate->type_stack.push ($2);
1317 cpstate->type_stack.push (tp_array);
1318 $$ = cpstate->type_stack.create ();
1319 cpstate->type_stacks.emplace_back ($$);
1320 }
1321 | array_mod
1322 {
1323 cpstate->type_stack.push ($1);
1324 cpstate->type_stack.push (tp_array);
1325 $$ = cpstate->type_stack.create ();
1326 cpstate->type_stacks.emplace_back ($$);
1327 }
1328
1329 | direct_abs_decl func_mod
1330 {
1331 cpstate->type_stack.push ($1);
1332 cpstate->type_stack.push ($2);
1333 $$ = cpstate->type_stack.create ();
1334 cpstate->type_stacks.emplace_back ($$);
1335 }
1336 | func_mod
1337 {
1338 cpstate->type_stack.push ($1);
1339 $$ = cpstate->type_stack.create ();
1340 cpstate->type_stacks.emplace_back ($$);
1341 }
1342 ;
1343
1344 array_mod: '[' ']'
1345 { $$ = -1; }
1346 | OBJC_LBRAC ']'
1347 { $$ = -1; }
1348 | '[' INT ']'
1349 { $$ = $2.val; }
1350 | OBJC_LBRAC INT ']'
1351 { $$ = $2.val; }
1352 ;
1353
1354 func_mod: '(' ')'
1355 {
1356 $$ = new std::vector<struct type *>;
1357 cpstate->type_lists.emplace_back ($$);
1358 }
1359 | '(' parameter_typelist ')'
1360 { $$ = $2; }
1361 ;
1362
1363 /* We used to try to recognize pointer to member types here, but
1364 that didn't work (shift/reduce conflicts meant that these rules never
1365 got executed). The problem is that
1366 int (foo::bar::baz::bizzle)
1367 is a function type but
1368 int (foo::bar::baz::bizzle::*)
1369 is a pointer to member type. Stroustrup loses again! */
1370
1371 type : ptype
1372 ;
1373
1374 /* A helper production that recognizes scalar types that can validly
1375 be used with _Complex. */
1376
1377 scalar_type:
1378 INT_KEYWORD
1379 { $$ = lookup_signed_typename (pstate->language (),
1380 "int"); }
1381 | LONG
1382 { $$ = lookup_signed_typename (pstate->language (),
1383 "long"); }
1384 | SHORT
1385 { $$ = lookup_signed_typename (pstate->language (),
1386 "short"); }
1387 | LONG INT_KEYWORD
1388 { $$ = lookup_signed_typename (pstate->language (),
1389 "long"); }
1390 | LONG SIGNED_KEYWORD INT_KEYWORD
1391 { $$ = lookup_signed_typename (pstate->language (),
1392 "long"); }
1393 | LONG SIGNED_KEYWORD
1394 { $$ = lookup_signed_typename (pstate->language (),
1395 "long"); }
1396 | SIGNED_KEYWORD LONG INT_KEYWORD
1397 { $$ = lookup_signed_typename (pstate->language (),
1398 "long"); }
1399 | UNSIGNED LONG INT_KEYWORD
1400 { $$ = lookup_unsigned_typename (pstate->language (),
1401 "long"); }
1402 | LONG UNSIGNED INT_KEYWORD
1403 { $$ = lookup_unsigned_typename (pstate->language (),
1404 "long"); }
1405 | LONG UNSIGNED
1406 { $$ = lookup_unsigned_typename (pstate->language (),
1407 "long"); }
1408 | LONG LONG
1409 { $$ = lookup_signed_typename (pstate->language (),
1410 "long long"); }
1411 | LONG LONG INT_KEYWORD
1412 { $$ = lookup_signed_typename (pstate->language (),
1413 "long long"); }
1414 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1415 { $$ = lookup_signed_typename (pstate->language (),
1416 "long long"); }
1417 | LONG LONG SIGNED_KEYWORD
1418 { $$ = lookup_signed_typename (pstate->language (),
1419 "long long"); }
1420 | SIGNED_KEYWORD LONG LONG
1421 { $$ = lookup_signed_typename (pstate->language (),
1422 "long long"); }
1423 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1424 { $$ = lookup_signed_typename (pstate->language (),
1425 "long long"); }
1426 | UNSIGNED LONG LONG
1427 { $$ = lookup_unsigned_typename (pstate->language (),
1428 "long long"); }
1429 | UNSIGNED LONG LONG INT_KEYWORD
1430 { $$ = lookup_unsigned_typename (pstate->language (),
1431 "long long"); }
1432 | LONG LONG UNSIGNED
1433 { $$ = lookup_unsigned_typename (pstate->language (),
1434 "long long"); }
1435 | LONG LONG UNSIGNED INT_KEYWORD
1436 { $$ = lookup_unsigned_typename (pstate->language (),
1437 "long long"); }
1438 | SHORT INT_KEYWORD
1439 { $$ = lookup_signed_typename (pstate->language (),
1440 "short"); }
1441 | SHORT SIGNED_KEYWORD INT_KEYWORD
1442 { $$ = lookup_signed_typename (pstate->language (),
1443 "short"); }
1444 | SHORT SIGNED_KEYWORD
1445 { $$ = lookup_signed_typename (pstate->language (),
1446 "short"); }
1447 | UNSIGNED SHORT INT_KEYWORD
1448 { $$ = lookup_unsigned_typename (pstate->language (),
1449 "short"); }
1450 | SHORT UNSIGNED
1451 { $$ = lookup_unsigned_typename (pstate->language (),
1452 "short"); }
1453 | SHORT UNSIGNED INT_KEYWORD
1454 { $$ = lookup_unsigned_typename (pstate->language (),
1455 "short"); }
1456 | DOUBLE_KEYWORD
1457 { $$ = lookup_typename (pstate->language (),
1458 "double",
1459 NULL,
1460 0); }
1461 | FLOAT_KEYWORD
1462 { $$ = lookup_typename (pstate->language (),
1463 "float",
1464 NULL,
1465 0); }
1466 | LONG DOUBLE_KEYWORD
1467 { $$ = lookup_typename (pstate->language (),
1468 "long double",
1469 NULL,
1470 0); }
1471 | UNSIGNED type_name
1472 { $$ = lookup_unsigned_typename (pstate->language (),
1473 $2.type->name ()); }
1474 | UNSIGNED
1475 { $$ = lookup_unsigned_typename (pstate->language (),
1476 "int"); }
1477 | SIGNED_KEYWORD type_name
1478 { $$ = lookup_signed_typename (pstate->language (),
1479 $2.type->name ()); }
1480 | SIGNED_KEYWORD
1481 { $$ = lookup_signed_typename (pstate->language (),
1482 "int"); }
1483 ;
1484
1485 /* Implements (approximately): (type-qualifier)* type-specifier.
1486
1487 When type-specifier is only ever a single word, like 'float' then these
1488 arrive as pre-built TYPENAME tokens thanks to the classify_name
1489 function. However, when a type-specifier can contain multiple words,
1490 for example 'double' can appear as just 'double' or 'long double', and
1491 similarly 'long' can appear as just 'long' or in 'long double', then
1492 these type-specifiers are parsed into their own tokens in the function
1493 lex_one_token and the ident_tokens array. These separate tokens are all
1494 recognised here. */
1495 typebase
1496 : TYPENAME
1497 { $$ = $1.type; }
1498 | scalar_type
1499 { $$ = $1; }
1500 | COMPLEX scalar_type
1501 {
1502 $$ = init_complex_type (nullptr, $2);
1503 }
1504 | STRUCT name
1505 { $$
1506 = lookup_struct (copy_name ($2).c_str (),
1507 pstate->expression_context_block);
1508 }
1509 | STRUCT COMPLETE
1510 {
1511 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1512 "", 0);
1513 $$ = NULL;
1514 }
1515 | STRUCT name COMPLETE
1516 {
1517 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1518 $2.ptr, $2.length);
1519 $$ = NULL;
1520 }
1521 | CLASS name
1522 { $$ = lookup_struct
1523 (copy_name ($2).c_str (),
1524 pstate->expression_context_block);
1525 }
1526 | CLASS COMPLETE
1527 {
1528 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1529 "", 0);
1530 $$ = NULL;
1531 }
1532 | CLASS name COMPLETE
1533 {
1534 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1535 $2.ptr, $2.length);
1536 $$ = NULL;
1537 }
1538 | UNION name
1539 { $$
1540 = lookup_union (copy_name ($2).c_str (),
1541 pstate->expression_context_block);
1542 }
1543 | UNION COMPLETE
1544 {
1545 pstate->mark_completion_tag (TYPE_CODE_UNION,
1546 "", 0);
1547 $$ = NULL;
1548 }
1549 | UNION name COMPLETE
1550 {
1551 pstate->mark_completion_tag (TYPE_CODE_UNION,
1552 $2.ptr, $2.length);
1553 $$ = NULL;
1554 }
1555 | ENUM name
1556 { $$ = lookup_enum (copy_name ($2).c_str (),
1557 pstate->expression_context_block);
1558 }
1559 | ENUM COMPLETE
1560 {
1561 pstate->mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1562 $$ = NULL;
1563 }
1564 | ENUM name COMPLETE
1565 {
1566 pstate->mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1567 $2.length);
1568 $$ = NULL;
1569 }
1570 /* It appears that this rule for templates is never
1571 reduced; template recognition happens by lookahead
1572 in the token processing code in yylex. */
1573 | TEMPLATE name '<' type '>'
1574 { $$ = lookup_template_type
1575 (copy_name($2).c_str (), $4,
1576 pstate->expression_context_block);
1577 }
1578 | qualifier_seq_noopt typebase
1579 { $$ = cpstate->type_stack.follow_types ($2); }
1580 | typebase qualifier_seq_noopt
1581 { $$ = cpstate->type_stack.follow_types ($1); }
1582 ;
1583
1584 type_name: TYPENAME
1585 | INT_KEYWORD
1586 {
1587 $$.stoken.ptr = "int";
1588 $$.stoken.length = 3;
1589 $$.type = lookup_signed_typename (pstate->language (),
1590 "int");
1591 }
1592 | LONG
1593 {
1594 $$.stoken.ptr = "long";
1595 $$.stoken.length = 4;
1596 $$.type = lookup_signed_typename (pstate->language (),
1597 "long");
1598 }
1599 | SHORT
1600 {
1601 $$.stoken.ptr = "short";
1602 $$.stoken.length = 5;
1603 $$.type = lookup_signed_typename (pstate->language (),
1604 "short");
1605 }
1606 ;
1607
1608 parameter_typelist:
1609 nonempty_typelist
1610 { check_parameter_typelist ($1); }
1611 | nonempty_typelist ',' DOTDOTDOT
1612 {
1613 $1->push_back (NULL);
1614 check_parameter_typelist ($1);
1615 $$ = $1;
1616 }
1617 ;
1618
1619 nonempty_typelist
1620 : type
1621 {
1622 std::vector<struct type *> *typelist
1623 = new std::vector<struct type *>;
1624 cpstate->type_lists.emplace_back (typelist);
1625
1626 typelist->push_back ($1);
1627 $$ = typelist;
1628 }
1629 | nonempty_typelist ',' type
1630 {
1631 $1->push_back ($3);
1632 $$ = $1;
1633 }
1634 ;
1635
1636 ptype : typebase
1637 | ptype abs_decl
1638 {
1639 cpstate->type_stack.push ($2);
1640 $$ = cpstate->type_stack.follow_types ($1);
1641 }
1642 ;
1643
1644 conversion_type_id: typebase conversion_declarator
1645 { $$ = cpstate->type_stack.follow_types ($1); }
1646 ;
1647
1648 conversion_declarator: /* Nothing. */
1649 | ptr_operator conversion_declarator
1650 ;
1651
1652 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1653 | VOLATILE_KEYWORD CONST_KEYWORD
1654 ;
1655
1656 const_or_volatile_noopt: const_and_volatile
1657 { cpstate->type_stack.insert (tp_const);
1658 cpstate->type_stack.insert (tp_volatile);
1659 }
1660 | CONST_KEYWORD
1661 { cpstate->type_stack.insert (tp_const); }
1662 | VOLATILE_KEYWORD
1663 { cpstate->type_stack.insert (tp_volatile); }
1664 ;
1665
1666 oper: OPERATOR NEW
1667 { $$ = operator_stoken (" new"); }
1668 | OPERATOR DELETE
1669 { $$ = operator_stoken (" delete"); }
1670 | OPERATOR NEW '[' ']'
1671 { $$ = operator_stoken (" new[]"); }
1672 | OPERATOR DELETE '[' ']'
1673 { $$ = operator_stoken (" delete[]"); }
1674 | OPERATOR NEW OBJC_LBRAC ']'
1675 { $$ = operator_stoken (" new[]"); }
1676 | OPERATOR DELETE OBJC_LBRAC ']'
1677 { $$ = operator_stoken (" delete[]"); }
1678 | OPERATOR '+'
1679 { $$ = operator_stoken ("+"); }
1680 | OPERATOR '-'
1681 { $$ = operator_stoken ("-"); }
1682 | OPERATOR '*'
1683 { $$ = operator_stoken ("*"); }
1684 | OPERATOR '/'
1685 { $$ = operator_stoken ("/"); }
1686 | OPERATOR '%'
1687 { $$ = operator_stoken ("%"); }
1688 | OPERATOR '^'
1689 { $$ = operator_stoken ("^"); }
1690 | OPERATOR '&'
1691 { $$ = operator_stoken ("&"); }
1692 | OPERATOR '|'
1693 { $$ = operator_stoken ("|"); }
1694 | OPERATOR '~'
1695 { $$ = operator_stoken ("~"); }
1696 | OPERATOR '!'
1697 { $$ = operator_stoken ("!"); }
1698 | OPERATOR '='
1699 { $$ = operator_stoken ("="); }
1700 | OPERATOR '<'
1701 { $$ = operator_stoken ("<"); }
1702 | OPERATOR '>'
1703 { $$ = operator_stoken (">"); }
1704 | OPERATOR ASSIGN_MODIFY
1705 { const char *op = " unknown";
1706 switch ($2)
1707 {
1708 case BINOP_RSH:
1709 op = ">>=";
1710 break;
1711 case BINOP_LSH:
1712 op = "<<=";
1713 break;
1714 case BINOP_ADD:
1715 op = "+=";
1716 break;
1717 case BINOP_SUB:
1718 op = "-=";
1719 break;
1720 case BINOP_MUL:
1721 op = "*=";
1722 break;
1723 case BINOP_DIV:
1724 op = "/=";
1725 break;
1726 case BINOP_REM:
1727 op = "%=";
1728 break;
1729 case BINOP_BITWISE_IOR:
1730 op = "|=";
1731 break;
1732 case BINOP_BITWISE_AND:
1733 op = "&=";
1734 break;
1735 case BINOP_BITWISE_XOR:
1736 op = "^=";
1737 break;
1738 default:
1739 break;
1740 }
1741
1742 $$ = operator_stoken (op);
1743 }
1744 | OPERATOR LSH
1745 { $$ = operator_stoken ("<<"); }
1746 | OPERATOR RSH
1747 { $$ = operator_stoken (">>"); }
1748 | OPERATOR EQUAL
1749 { $$ = operator_stoken ("=="); }
1750 | OPERATOR NOTEQUAL
1751 { $$ = operator_stoken ("!="); }
1752 | OPERATOR LEQ
1753 { $$ = operator_stoken ("<="); }
1754 | OPERATOR GEQ
1755 { $$ = operator_stoken (">="); }
1756 | OPERATOR ANDAND
1757 { $$ = operator_stoken ("&&"); }
1758 | OPERATOR OROR
1759 { $$ = operator_stoken ("||"); }
1760 | OPERATOR INCREMENT
1761 { $$ = operator_stoken ("++"); }
1762 | OPERATOR DECREMENT
1763 { $$ = operator_stoken ("--"); }
1764 | OPERATOR ','
1765 { $$ = operator_stoken (","); }
1766 | OPERATOR ARROW_STAR
1767 { $$ = operator_stoken ("->*"); }
1768 | OPERATOR ARROW
1769 { $$ = operator_stoken ("->"); }
1770 | OPERATOR '(' ')'
1771 { $$ = operator_stoken ("()"); }
1772 | OPERATOR '[' ']'
1773 { $$ = operator_stoken ("[]"); }
1774 | OPERATOR OBJC_LBRAC ']'
1775 { $$ = operator_stoken ("[]"); }
1776 | OPERATOR conversion_type_id
1777 { string_file buf;
1778
1779 c_print_type ($2, NULL, &buf, -1, 0,
1780 &type_print_raw_options);
1781 std::string name = std::move (buf.string ());
1782
1783 /* This also needs canonicalization. */
1784 gdb::unique_xmalloc_ptr<char> canon
1785 = cp_canonicalize_string (name.c_str ());
1786 if (canon != nullptr)
1787 name = canon.get ();
1788 $$ = operator_stoken ((" " + name).c_str ());
1789 }
1790 ;
1791
1792 /* This rule exists in order to allow some tokens that would not normally
1793 match the 'name' rule to appear as fields within a struct. The example
1794 that initially motivated this was the RISC-V target which models the
1795 floating point registers as a union with fields called 'float' and
1796 'double'. */
1797 field_name
1798 : name
1799 | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
1800 | FLOAT_KEYWORD { $$ = typename_stoken ("float"); }
1801 | INT_KEYWORD { $$ = typename_stoken ("int"); }
1802 | LONG { $$ = typename_stoken ("long"); }
1803 | SHORT { $$ = typename_stoken ("short"); }
1804 | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
1805 | UNSIGNED { $$ = typename_stoken ("unsigned"); }
1806 ;
1807
1808 name : NAME { $$ = $1.stoken; }
1809 | BLOCKNAME { $$ = $1.stoken; }
1810 | TYPENAME { $$ = $1.stoken; }
1811 | NAME_OR_INT { $$ = $1.stoken; }
1812 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1813 | oper { $$ = $1; }
1814 ;
1815
1816 name_not_typename : NAME
1817 | BLOCKNAME
1818 /* These would be useful if name_not_typename was useful, but it is just
1819 a fake for "variable", so these cause reduce/reduce conflicts because
1820 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1821 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1822 context where only a name could occur, this might be useful.
1823 | NAME_OR_INT
1824 */
1825 | oper
1826 {
1827 struct field_of_this_result is_a_field_of_this;
1828
1829 $$.stoken = $1;
1830 $$.sym
1831 = lookup_symbol ($1.ptr,
1832 pstate->expression_context_block,
1833 VAR_DOMAIN,
1834 &is_a_field_of_this);
1835 $$.is_a_field_of_this
1836 = is_a_field_of_this.type != NULL;
1837 }
1838 | UNKNOWN_CPP_NAME
1839 ;
1840
1841 %%
1842
1843 /* Returns a stoken of the operator name given by OP (which does not
1844 include the string "operator"). */
1845
1846 static struct stoken
1847 operator_stoken (const char *op)
1848 {
1849 struct stoken st = { NULL, 0 };
1850 char *buf;
1851
1852 st.length = CP_OPERATOR_LEN + strlen (op);
1853 buf = (char *) malloc (st.length + 1);
1854 strcpy (buf, CP_OPERATOR_STR);
1855 strcat (buf, op);
1856 st.ptr = buf;
1857
1858 /* The toplevel (c_parse) will free the memory allocated here. */
1859 cpstate->strings.emplace_back (buf);
1860 return st;
1861 };
1862
1863 /* Returns a stoken of the type named TYPE. */
1864
1865 static struct stoken
1866 typename_stoken (const char *type)
1867 {
1868 struct stoken st = { type, 0 };
1869 st.length = strlen (type);
1870 return st;
1871 };
1872
1873 /* Return true if the type is aggregate-like. */
1874
1875 static int
1876 type_aggregate_p (struct type *type)
1877 {
1878 return (type->code () == TYPE_CODE_STRUCT
1879 || type->code () == TYPE_CODE_UNION
1880 || type->code () == TYPE_CODE_NAMESPACE
1881 || (type->code () == TYPE_CODE_ENUM
1882 && TYPE_DECLARED_CLASS (type)));
1883 }
1884
1885 /* Validate a parameter typelist. */
1886
1887 static void
1888 check_parameter_typelist (std::vector<struct type *> *params)
1889 {
1890 struct type *type;
1891 int ix;
1892
1893 for (ix = 0; ix < params->size (); ++ix)
1894 {
1895 type = (*params)[ix];
1896 if (type != NULL && check_typedef (type)->code () == TYPE_CODE_VOID)
1897 {
1898 if (ix == 0)
1899 {
1900 if (params->size () == 1)
1901 {
1902 /* Ok. */
1903 break;
1904 }
1905 error (_("parameter types following 'void'"));
1906 }
1907 else
1908 error (_("'void' invalid as parameter type"));
1909 }
1910 }
1911 }
1912
1913 /* Take care of parsing a number (anything that starts with a digit).
1914 Set yylval and return the token type; update lexptr.
1915 LEN is the number of characters in it. */
1916
1917 /*** Needs some error checking for the float case ***/
1918
1919 static int
1920 parse_number (struct parser_state *par_state,
1921 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1922 {
1923 ULONGEST n = 0;
1924 ULONGEST prevn = 0;
1925 ULONGEST un;
1926
1927 int i = 0;
1928 int c;
1929 int base = input_radix;
1930 int unsigned_p = 0;
1931
1932 /* Number of "L" suffixes encountered. */
1933 int long_p = 0;
1934
1935 /* Imaginary number. */
1936 bool imaginary_p = false;
1937
1938 /* We have found a "L" or "U" (or "i") suffix. */
1939 int found_suffix = 0;
1940
1941 ULONGEST high_bit;
1942 struct type *signed_type;
1943 struct type *unsigned_type;
1944 char *p;
1945
1946 p = (char *) alloca (len);
1947 memcpy (p, buf, len);
1948
1949 if (parsed_float)
1950 {
1951 if (len >= 1 && p[len - 1] == 'i')
1952 {
1953 imaginary_p = true;
1954 --len;
1955 }
1956
1957 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
1958 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1959 {
1960 putithere->typed_val_float.type
1961 = parse_type (par_state)->builtin_decfloat;
1962 len -= 2;
1963 }
1964 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1965 {
1966 putithere->typed_val_float.type
1967 = parse_type (par_state)->builtin_decdouble;
1968 len -= 2;
1969 }
1970 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1971 {
1972 putithere->typed_val_float.type
1973 = parse_type (par_state)->builtin_declong;
1974 len -= 2;
1975 }
1976 /* Handle suffixes: 'f' for float, 'l' for long double. */
1977 else if (len >= 1 && TOLOWER (p[len - 1]) == 'f')
1978 {
1979 putithere->typed_val_float.type
1980 = parse_type (par_state)->builtin_float;
1981 len -= 1;
1982 }
1983 else if (len >= 1 && TOLOWER (p[len - 1]) == 'l')
1984 {
1985 putithere->typed_val_float.type
1986 = parse_type (par_state)->builtin_long_double;
1987 len -= 1;
1988 }
1989 /* Default type for floating-point literals is double. */
1990 else
1991 {
1992 putithere->typed_val_float.type
1993 = parse_type (par_state)->builtin_double;
1994 }
1995
1996 if (!parse_float (p, len,
1997 putithere->typed_val_float.type,
1998 putithere->typed_val_float.val))
1999 return ERROR;
2000
2001 if (imaginary_p)
2002 putithere->typed_val_float.type
2003 = init_complex_type (nullptr, putithere->typed_val_float.type);
2004
2005 return imaginary_p ? COMPLEX_FLOAT : FLOAT;
2006 }
2007
2008 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
2009 if (p[0] == '0' && len > 1)
2010 switch (p[1])
2011 {
2012 case 'x':
2013 case 'X':
2014 if (len >= 3)
2015 {
2016 p += 2;
2017 base = 16;
2018 len -= 2;
2019 }
2020 break;
2021
2022 case 'b':
2023 case 'B':
2024 if (len >= 3)
2025 {
2026 p += 2;
2027 base = 2;
2028 len -= 2;
2029 }
2030 break;
2031
2032 case 't':
2033 case 'T':
2034 case 'd':
2035 case 'D':
2036 if (len >= 3)
2037 {
2038 p += 2;
2039 base = 10;
2040 len -= 2;
2041 }
2042 break;
2043
2044 default:
2045 base = 8;
2046 break;
2047 }
2048
2049 while (len-- > 0)
2050 {
2051 c = *p++;
2052 if (c >= 'A' && c <= 'Z')
2053 c += 'a' - 'A';
2054 if (c != 'l' && c != 'u' && c != 'i')
2055 n *= base;
2056 if (c >= '0' && c <= '9')
2057 {
2058 if (found_suffix)
2059 return ERROR;
2060 n += i = c - '0';
2061 }
2062 else
2063 {
2064 if (base > 10 && c >= 'a' && c <= 'f')
2065 {
2066 if (found_suffix)
2067 return ERROR;
2068 n += i = c - 'a' + 10;
2069 }
2070 else if (c == 'l')
2071 {
2072 ++long_p;
2073 found_suffix = 1;
2074 }
2075 else if (c == 'u')
2076 {
2077 unsigned_p = 1;
2078 found_suffix = 1;
2079 }
2080 else if (c == 'i')
2081 {
2082 imaginary_p = true;
2083 found_suffix = 1;
2084 }
2085 else
2086 return ERROR; /* Char not a digit */
2087 }
2088 if (i >= base)
2089 return ERROR; /* Invalid digit in this base */
2090
2091 /* Portably test for overflow (only works for nonzero values, so make
2092 a second check for zero). FIXME: Can't we just make n and prevn
2093 unsigned and avoid this? */
2094 if (c != 'l' && c != 'u' && c != 'i' && (prevn >= n) && n != 0)
2095 unsigned_p = 1; /* Try something unsigned */
2096
2097 /* Portably test for unsigned overflow.
2098 FIXME: This check is wrong; for example it doesn't find overflow
2099 on 0x123456789 when LONGEST is 32 bits. */
2100 if (c != 'l' && c != 'u' && c != 'i' && n != 0)
2101 {
2102 if (unsigned_p && prevn >= n)
2103 error (_("Numeric constant too large."));
2104 }
2105 prevn = n;
2106 }
2107
2108 /* An integer constant is an int, a long, or a long long. An L
2109 suffix forces it to be long; an LL suffix forces it to be long
2110 long. If not forced to a larger size, it gets the first type of
2111 the above that it fits in. To figure out whether it fits, we
2112 shift it right and see whether anything remains. Note that we
2113 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
2114 operation, because many compilers will warn about such a shift
2115 (which always produces a zero result). Sometimes gdbarch_int_bit
2116 or gdbarch_long_bit will be that big, sometimes not. To deal with
2117 the case where it is we just always shift the value more than
2118 once, with fewer bits each time. */
2119
2120 un = n >> 2;
2121 if (long_p == 0
2122 && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0)
2123 {
2124 high_bit
2125 = ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
2126
2127 /* A large decimal (not hex or octal) constant (between INT_MAX
2128 and UINT_MAX) is a long or unsigned long, according to ANSI,
2129 never an unsigned int, but this code treats it as unsigned
2130 int. This probably should be fixed. GCC gives a warning on
2131 such constants. */
2132
2133 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
2134 signed_type = parse_type (par_state)->builtin_int;
2135 }
2136 else if (long_p <= 1
2137 && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0)
2138 {
2139 high_bit
2140 = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1);
2141 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
2142 signed_type = parse_type (par_state)->builtin_long;
2143 }
2144 else
2145 {
2146 int shift;
2147 if (sizeof (ULONGEST) * HOST_CHAR_BIT
2148 < gdbarch_long_long_bit (par_state->gdbarch ()))
2149 /* A long long does not fit in a LONGEST. */
2150 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
2151 else
2152 shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1);
2153 high_bit = (ULONGEST) 1 << shift;
2154 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
2155 signed_type = parse_type (par_state)->builtin_long_long;
2156 }
2157
2158 putithere->typed_val_int.val = n;
2159
2160 /* If the high bit of the worked out type is set then this number
2161 has to be unsigned. */
2162
2163 if (unsigned_p || (n & high_bit))
2164 {
2165 putithere->typed_val_int.type = unsigned_type;
2166 }
2167 else
2168 {
2169 putithere->typed_val_int.type = signed_type;
2170 }
2171
2172 if (imaginary_p)
2173 putithere->typed_val_int.type
2174 = init_complex_type (nullptr, putithere->typed_val_int.type);
2175
2176 return imaginary_p ? COMPLEX_INT : INT;
2177 }
2178
2179 /* Temporary obstack used for holding strings. */
2180 static struct obstack tempbuf;
2181 static int tempbuf_init;
2182
2183 /* Parse a C escape sequence. The initial backslash of the sequence
2184 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2185 last character of the sequence. If OUTPUT is not NULL, the
2186 translated form of the escape sequence will be written there. If
2187 OUTPUT is NULL, no output is written and the call will only affect
2188 *PTR. If an escape sequence is expressed in target bytes, then the
2189 entire sequence will simply be copied to OUTPUT. Return 1 if any
2190 character was emitted, 0 otherwise. */
2191
2192 int
2193 c_parse_escape (const char **ptr, struct obstack *output)
2194 {
2195 const char *tokptr = *ptr;
2196 int result = 1;
2197
2198 /* Some escape sequences undergo character set conversion. Those we
2199 translate here. */
2200 switch (*tokptr)
2201 {
2202 /* Hex escapes do not undergo character set conversion, so keep
2203 the escape sequence for later. */
2204 case 'x':
2205 if (output)
2206 obstack_grow_str (output, "\\x");
2207 ++tokptr;
2208 if (!ISXDIGIT (*tokptr))
2209 error (_("\\x escape without a following hex digit"));
2210 while (ISXDIGIT (*tokptr))
2211 {
2212 if (output)
2213 obstack_1grow (output, *tokptr);
2214 ++tokptr;
2215 }
2216 break;
2217
2218 /* Octal escapes do not undergo character set conversion, so
2219 keep the escape sequence for later. */
2220 case '0':
2221 case '1':
2222 case '2':
2223 case '3':
2224 case '4':
2225 case '5':
2226 case '6':
2227 case '7':
2228 {
2229 int i;
2230 if (output)
2231 obstack_grow_str (output, "\\");
2232 for (i = 0;
2233 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
2234 ++i)
2235 {
2236 if (output)
2237 obstack_1grow (output, *tokptr);
2238 ++tokptr;
2239 }
2240 }
2241 break;
2242
2243 /* We handle UCNs later. We could handle them here, but that
2244 would mean a spurious error in the case where the UCN could
2245 be converted to the target charset but not the host
2246 charset. */
2247 case 'u':
2248 case 'U':
2249 {
2250 char c = *tokptr;
2251 int i, len = c == 'U' ? 8 : 4;
2252 if (output)
2253 {
2254 obstack_1grow (output, '\\');
2255 obstack_1grow (output, *tokptr);
2256 }
2257 ++tokptr;
2258 if (!ISXDIGIT (*tokptr))
2259 error (_("\\%c escape without a following hex digit"), c);
2260 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
2261 {
2262 if (output)
2263 obstack_1grow (output, *tokptr);
2264 ++tokptr;
2265 }
2266 }
2267 break;
2268
2269 /* We must pass backslash through so that it does not
2270 cause quoting during the second expansion. */
2271 case '\\':
2272 if (output)
2273 obstack_grow_str (output, "\\\\");
2274 ++tokptr;
2275 break;
2276
2277 /* Escapes which undergo conversion. */
2278 case 'a':
2279 if (output)
2280 obstack_1grow (output, '\a');
2281 ++tokptr;
2282 break;
2283 case 'b':
2284 if (output)
2285 obstack_1grow (output, '\b');
2286 ++tokptr;
2287 break;
2288 case 'f':
2289 if (output)
2290 obstack_1grow (output, '\f');
2291 ++tokptr;
2292 break;
2293 case 'n':
2294 if (output)
2295 obstack_1grow (output, '\n');
2296 ++tokptr;
2297 break;
2298 case 'r':
2299 if (output)
2300 obstack_1grow (output, '\r');
2301 ++tokptr;
2302 break;
2303 case 't':
2304 if (output)
2305 obstack_1grow (output, '\t');
2306 ++tokptr;
2307 break;
2308 case 'v':
2309 if (output)
2310 obstack_1grow (output, '\v');
2311 ++tokptr;
2312 break;
2313
2314 /* GCC extension. */
2315 case 'e':
2316 if (output)
2317 obstack_1grow (output, HOST_ESCAPE_CHAR);
2318 ++tokptr;
2319 break;
2320
2321 /* Backslash-newline expands to nothing at all. */
2322 case '\n':
2323 ++tokptr;
2324 result = 0;
2325 break;
2326
2327 /* A few escapes just expand to the character itself. */
2328 case '\'':
2329 case '\"':
2330 case '?':
2331 /* GCC extensions. */
2332 case '(':
2333 case '{':
2334 case '[':
2335 case '%':
2336 /* Unrecognized escapes turn into the character itself. */
2337 default:
2338 if (output)
2339 obstack_1grow (output, *tokptr);
2340 ++tokptr;
2341 break;
2342 }
2343 *ptr = tokptr;
2344 return result;
2345 }
2346
2347 /* Parse a string or character literal from TOKPTR. The string or
2348 character may be wide or unicode. *OUTPTR is set to just after the
2349 end of the literal in the input string. The resulting token is
2350 stored in VALUE. This returns a token value, either STRING or
2351 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2352 number of host characters in the literal. */
2353
2354 static int
2355 parse_string_or_char (const char *tokptr, const char **outptr,
2356 struct typed_stoken *value, int *host_chars)
2357 {
2358 int quote;
2359 c_string_type type;
2360 int is_objc = 0;
2361
2362 /* Build the gdb internal form of the input string in tempbuf. Note
2363 that the buffer is null byte terminated *only* for the
2364 convenience of debugging gdb itself and printing the buffer
2365 contents when the buffer contains no embedded nulls. Gdb does
2366 not depend upon the buffer being null byte terminated, it uses
2367 the length string instead. This allows gdb to handle C strings
2368 (as well as strings in other languages) with embedded null
2369 bytes */
2370
2371 if (!tempbuf_init)
2372 tempbuf_init = 1;
2373 else
2374 obstack_free (&tempbuf, NULL);
2375 obstack_init (&tempbuf);
2376
2377 /* Record the string type. */
2378 if (*tokptr == 'L')
2379 {
2380 type = C_WIDE_STRING;
2381 ++tokptr;
2382 }
2383 else if (*tokptr == 'u')
2384 {
2385 type = C_STRING_16;
2386 ++tokptr;
2387 }
2388 else if (*tokptr == 'U')
2389 {
2390 type = C_STRING_32;
2391 ++tokptr;
2392 }
2393 else if (*tokptr == '@')
2394 {
2395 /* An Objective C string. */
2396 is_objc = 1;
2397 type = C_STRING;
2398 ++tokptr;
2399 }
2400 else
2401 type = C_STRING;
2402
2403 /* Skip the quote. */
2404 quote = *tokptr;
2405 if (quote == '\'')
2406 type |= C_CHAR;
2407 ++tokptr;
2408
2409 *host_chars = 0;
2410
2411 while (*tokptr)
2412 {
2413 char c = *tokptr;
2414 if (c == '\\')
2415 {
2416 ++tokptr;
2417 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2418 }
2419 else if (c == quote)
2420 break;
2421 else
2422 {
2423 obstack_1grow (&tempbuf, c);
2424 ++tokptr;
2425 /* FIXME: this does the wrong thing with multi-byte host
2426 characters. We could use mbrlen here, but that would
2427 make "set host-charset" a bit less useful. */
2428 ++*host_chars;
2429 }
2430 }
2431
2432 if (*tokptr != quote)
2433 {
2434 if (quote == '"')
2435 error (_("Unterminated string in expression."));
2436 else
2437 error (_("Unmatched single quote."));
2438 }
2439 ++tokptr;
2440
2441 value->type = type;
2442 value->ptr = (char *) obstack_base (&tempbuf);
2443 value->length = obstack_object_size (&tempbuf);
2444
2445 *outptr = tokptr;
2446
2447 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2448 }
2449
2450 /* This is used to associate some attributes with a token. */
2451
2452 enum token_flag
2453 {
2454 /* If this bit is set, the token is C++-only. */
2455
2456 FLAG_CXX = 1,
2457
2458 /* If this bit is set, the token is C-only. */
2459
2460 FLAG_C = 2,
2461
2462 /* If this bit is set, the token is conditional: if there is a
2463 symbol of the same name, then the token is a symbol; otherwise,
2464 the token is a keyword. */
2465
2466 FLAG_SHADOW = 4
2467 };
2468 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2469
2470 struct token
2471 {
2472 const char *oper;
2473 int token;
2474 enum exp_opcode opcode;
2475 token_flags flags;
2476 };
2477
2478 static const struct token tokentab3[] =
2479 {
2480 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2481 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2482 {"->*", ARROW_STAR, OP_NULL, FLAG_CXX},
2483 {"...", DOTDOTDOT, OP_NULL, 0}
2484 };
2485
2486 static const struct token tokentab2[] =
2487 {
2488 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2489 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2490 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2491 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2492 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2493 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2494 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2495 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2496 {"++", INCREMENT, OP_NULL, 0},
2497 {"--", DECREMENT, OP_NULL, 0},
2498 {"->", ARROW, OP_NULL, 0},
2499 {"&&", ANDAND, OP_NULL, 0},
2500 {"||", OROR, OP_NULL, 0},
2501 /* "::" is *not* only C++: gdb overrides its meaning in several
2502 different ways, e.g., 'filename'::func, function::variable. */
2503 {"::", COLONCOLON, OP_NULL, 0},
2504 {"<<", LSH, OP_NULL, 0},
2505 {">>", RSH, OP_NULL, 0},
2506 {"==", EQUAL, OP_NULL, 0},
2507 {"!=", NOTEQUAL, OP_NULL, 0},
2508 {"<=", LEQ, OP_NULL, 0},
2509 {">=", GEQ, OP_NULL, 0},
2510 {".*", DOT_STAR, OP_NULL, FLAG_CXX}
2511 };
2512
2513 /* Identifier-like tokens. Only type-specifiers than can appear in
2514 multi-word type names (for example 'double' can appear in 'long
2515 double') need to be listed here. type-specifiers that are only ever
2516 single word (like 'char') are handled by the classify_name function. */
2517 static const struct token ident_tokens[] =
2518 {
2519 {"unsigned", UNSIGNED, OP_NULL, 0},
2520 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2521 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2522 {"struct", STRUCT, OP_NULL, 0},
2523 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2524 {"sizeof", SIZEOF, OP_NULL, 0},
2525 {"_Alignof", ALIGNOF, OP_NULL, 0},
2526 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
2527 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2528 {"float", FLOAT_KEYWORD, OP_NULL, 0},
2529 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2530 {"class", CLASS, OP_NULL, FLAG_CXX},
2531 {"union", UNION, OP_NULL, 0},
2532 {"short", SHORT, OP_NULL, 0},
2533 {"const", CONST_KEYWORD, OP_NULL, 0},
2534 {"restrict", RESTRICT, OP_NULL, FLAG_C | FLAG_SHADOW},
2535 {"__restrict__", RESTRICT, OP_NULL, 0},
2536 {"__restrict", RESTRICT, OP_NULL, 0},
2537 {"_Atomic", ATOMIC, OP_NULL, 0},
2538 {"enum", ENUM, OP_NULL, 0},
2539 {"long", LONG, OP_NULL, 0},
2540 {"_Complex", COMPLEX, OP_NULL, 0},
2541 {"__complex__", COMPLEX, OP_NULL, 0},
2542
2543 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2544 {"int", INT_KEYWORD, OP_NULL, 0},
2545 {"new", NEW, OP_NULL, FLAG_CXX},
2546 {"delete", DELETE, OP_NULL, FLAG_CXX},
2547 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2548
2549 {"and", ANDAND, OP_NULL, FLAG_CXX},
2550 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2551 {"bitand", '&', OP_NULL, FLAG_CXX},
2552 {"bitor", '|', OP_NULL, FLAG_CXX},
2553 {"compl", '~', OP_NULL, FLAG_CXX},
2554 {"not", '!', OP_NULL, FLAG_CXX},
2555 {"not_eq", NOTEQUAL, OP_NULL, FLAG_CXX},
2556 {"or", OROR, OP_NULL, FLAG_CXX},
2557 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2558 {"xor", '^', OP_NULL, FLAG_CXX},
2559 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2560
2561 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2562 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2563 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2564 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2565
2566 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2567 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2568 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2569 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2570 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2571
2572 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2573 };
2574
2575
2576 static void
2577 scan_macro_expansion (const char *expansion)
2578 {
2579 /* We'd better not be trying to push the stack twice. */
2580 gdb_assert (! cpstate->macro_original_text);
2581
2582 /* Copy to the obstack. */
2583 const char *copy = obstack_strdup (&cpstate->expansion_obstack, expansion);
2584
2585 /* Save the old lexptr value, so we can return to it when we're done
2586 parsing the expanded text. */
2587 cpstate->macro_original_text = pstate->lexptr;
2588 pstate->lexptr = copy;
2589 }
2590
2591 static int
2592 scanning_macro_expansion (void)
2593 {
2594 return cpstate->macro_original_text != 0;
2595 }
2596
2597 static void
2598 finished_macro_expansion (void)
2599 {
2600 /* There'd better be something to pop back to. */
2601 gdb_assert (cpstate->macro_original_text);
2602
2603 /* Pop back to the original text. */
2604 pstate->lexptr = cpstate->macro_original_text;
2605 cpstate->macro_original_text = 0;
2606 }
2607
2608 /* Return true iff the token represents a C++ cast operator. */
2609
2610 static int
2611 is_cast_operator (const char *token, int len)
2612 {
2613 return (! strncmp (token, "dynamic_cast", len)
2614 || ! strncmp (token, "static_cast", len)
2615 || ! strncmp (token, "reinterpret_cast", len)
2616 || ! strncmp (token, "const_cast", len));
2617 }
2618
2619 /* The scope used for macro expansion. */
2620 static struct macro_scope *expression_macro_scope;
2621
2622 /* This is set if a NAME token appeared at the very end of the input
2623 string, with no whitespace separating the name from the EOF. This
2624 is used only when parsing to do field name completion. */
2625 static int saw_name_at_eof;
2626
2627 /* This is set if the previously-returned token was a structure
2628 operator -- either '.' or ARROW. */
2629 static bool last_was_structop;
2630
2631 /* Depth of parentheses. */
2632 static int paren_depth;
2633
2634 /* Read one token, getting characters through lexptr. */
2635
2636 static int
2637 lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
2638 {
2639 int c;
2640 int namelen;
2641 unsigned int i;
2642 const char *tokstart;
2643 bool saw_structop = last_was_structop;
2644
2645 last_was_structop = false;
2646 *is_quoted_name = false;
2647
2648 retry:
2649
2650 /* Check if this is a macro invocation that we need to expand. */
2651 if (! scanning_macro_expansion ())
2652 {
2653 gdb::unique_xmalloc_ptr<char> expanded
2654 = macro_expand_next (&pstate->lexptr, *expression_macro_scope);
2655
2656 if (expanded != nullptr)
2657 scan_macro_expansion (expanded.get ());
2658 }
2659
2660 pstate->prev_lexptr = pstate->lexptr;
2661
2662 tokstart = pstate->lexptr;
2663 /* See if it is a special token of length 3. */
2664 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2665 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
2666 {
2667 if ((tokentab3[i].flags & FLAG_CXX) != 0
2668 && par_state->language ()->la_language != language_cplus)
2669 break;
2670 gdb_assert ((tokentab3[i].flags & FLAG_C) == 0);
2671
2672 pstate->lexptr += 3;
2673 yylval.opcode = tokentab3[i].opcode;
2674 return tokentab3[i].token;
2675 }
2676
2677 /* See if it is a special token of length 2. */
2678 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2679 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
2680 {
2681 if ((tokentab2[i].flags & FLAG_CXX) != 0
2682 && par_state->language ()->la_language != language_cplus)
2683 break;
2684 gdb_assert ((tokentab2[i].flags & FLAG_C) == 0);
2685
2686 pstate->lexptr += 2;
2687 yylval.opcode = tokentab2[i].opcode;
2688 if (tokentab2[i].token == ARROW)
2689 last_was_structop = 1;
2690 return tokentab2[i].token;
2691 }
2692
2693 switch (c = *tokstart)
2694 {
2695 case 0:
2696 /* If we were just scanning the result of a macro expansion,
2697 then we need to resume scanning the original text.
2698 If we're parsing for field name completion, and the previous
2699 token allows such completion, return a COMPLETE token.
2700 Otherwise, we were already scanning the original text, and
2701 we're really done. */
2702 if (scanning_macro_expansion ())
2703 {
2704 finished_macro_expansion ();
2705 goto retry;
2706 }
2707 else if (saw_name_at_eof)
2708 {
2709 saw_name_at_eof = 0;
2710 return COMPLETE;
2711 }
2712 else if (par_state->parse_completion && saw_structop)
2713 return COMPLETE;
2714 else
2715 return 0;
2716
2717 case ' ':
2718 case '\t':
2719 case '\n':
2720 pstate->lexptr++;
2721 goto retry;
2722
2723 case '[':
2724 case '(':
2725 paren_depth++;
2726 pstate->lexptr++;
2727 if (par_state->language ()->la_language == language_objc
2728 && c == '[')
2729 return OBJC_LBRAC;
2730 return c;
2731
2732 case ']':
2733 case ')':
2734 if (paren_depth == 0)
2735 return 0;
2736 paren_depth--;
2737 pstate->lexptr++;
2738 return c;
2739
2740 case ',':
2741 if (pstate->comma_terminates
2742 && paren_depth == 0
2743 && ! scanning_macro_expansion ())
2744 return 0;
2745 pstate->lexptr++;
2746 return c;
2747
2748 case '.':
2749 /* Might be a floating point number. */
2750 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
2751 {
2752 last_was_structop = true;
2753 goto symbol; /* Nope, must be a symbol. */
2754 }
2755 /* FALL THRU. */
2756
2757 case '0':
2758 case '1':
2759 case '2':
2760 case '3':
2761 case '4':
2762 case '5':
2763 case '6':
2764 case '7':
2765 case '8':
2766 case '9':
2767 {
2768 /* It's a number. */
2769 int got_dot = 0, got_e = 0, got_p = 0, toktype;
2770 const char *p = tokstart;
2771 int hex = input_radix > 10;
2772
2773 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2774 {
2775 p += 2;
2776 hex = 1;
2777 }
2778 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2779 {
2780 p += 2;
2781 hex = 0;
2782 }
2783
2784 for (;; ++p)
2785 {
2786 /* This test includes !hex because 'e' is a valid hex digit
2787 and thus does not indicate a floating point number when
2788 the radix is hex. */
2789 if (!hex && !got_e && !got_p && (*p == 'e' || *p == 'E'))
2790 got_dot = got_e = 1;
2791 else if (!got_e && !got_p && (*p == 'p' || *p == 'P'))
2792 got_dot = got_p = 1;
2793 /* This test does not include !hex, because a '.' always indicates
2794 a decimal floating point number regardless of the radix. */
2795 else if (!got_dot && *p == '.')
2796 got_dot = 1;
2797 else if (((got_e && (p[-1] == 'e' || p[-1] == 'E'))
2798 || (got_p && (p[-1] == 'p' || p[-1] == 'P')))
2799 && (*p == '-' || *p == '+'))
2800 /* This is the sign of the exponent, not the end of the
2801 number. */
2802 continue;
2803 /* We will take any letters or digits. parse_number will
2804 complain if past the radix, or if L or U are not final. */
2805 else if ((*p < '0' || *p > '9')
2806 && ((*p < 'a' || *p > 'z')
2807 && (*p < 'A' || *p > 'Z')))
2808 break;
2809 }
2810 toktype = parse_number (par_state, tokstart, p - tokstart,
2811 got_dot | got_e | got_p, &yylval);
2812 if (toktype == ERROR)
2813 {
2814 char *err_copy = (char *) alloca (p - tokstart + 1);
2815
2816 memcpy (err_copy, tokstart, p - tokstart);
2817 err_copy[p - tokstart] = 0;
2818 error (_("Invalid number \"%s\"."), err_copy);
2819 }
2820 pstate->lexptr = p;
2821 return toktype;
2822 }
2823
2824 case '@':
2825 {
2826 const char *p = &tokstart[1];
2827
2828 if (par_state->language ()->la_language == language_objc)
2829 {
2830 size_t len = strlen ("selector");
2831
2832 if (strncmp (p, "selector", len) == 0
2833 && (p[len] == '\0' || ISSPACE (p[len])))
2834 {
2835 pstate->lexptr = p + len;
2836 return SELECTOR;
2837 }
2838 else if (*p == '"')
2839 goto parse_string;
2840 }
2841
2842 while (ISSPACE (*p))
2843 p++;
2844 size_t len = strlen ("entry");
2845 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
2846 && p[len] != '_')
2847 {
2848 pstate->lexptr = &p[len];
2849 return ENTRY;
2850 }
2851 }
2852 /* FALLTHRU */
2853 case '+':
2854 case '-':
2855 case '*':
2856 case '/':
2857 case '%':
2858 case '|':
2859 case '&':
2860 case '^':
2861 case '~':
2862 case '!':
2863 case '<':
2864 case '>':
2865 case '?':
2866 case ':':
2867 case '=':
2868 case '{':
2869 case '}':
2870 symbol:
2871 pstate->lexptr++;
2872 return c;
2873
2874 case 'L':
2875 case 'u':
2876 case 'U':
2877 if (tokstart[1] != '"' && tokstart[1] != '\'')
2878 break;
2879 /* Fall through. */
2880 case '\'':
2881 case '"':
2882
2883 parse_string:
2884 {
2885 int host_len;
2886 int result = parse_string_or_char (tokstart, &pstate->lexptr,
2887 &yylval.tsval, &host_len);
2888 if (result == CHAR)
2889 {
2890 if (host_len == 0)
2891 error (_("Empty character constant."));
2892 else if (host_len > 2 && c == '\'')
2893 {
2894 ++tokstart;
2895 namelen = pstate->lexptr - tokstart - 1;
2896 *is_quoted_name = true;
2897
2898 goto tryname;
2899 }
2900 else if (host_len > 1)
2901 error (_("Invalid character constant."));
2902 }
2903 return result;
2904 }
2905 }
2906
2907 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
2908 /* We must have come across a bad character (e.g. ';'). */
2909 error (_("Invalid character '%c' in expression."), c);
2910
2911 /* It's a name. See how long it is. */
2912 namelen = 0;
2913 for (c = tokstart[namelen];
2914 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
2915 {
2916 /* Template parameter lists are part of the name.
2917 FIXME: This mishandles `print $a<4&&$a>3'. */
2918
2919 if (c == '<')
2920 {
2921 if (! is_cast_operator (tokstart, namelen))
2922 {
2923 /* Scan ahead to get rest of the template specification. Note
2924 that we look ahead only when the '<' adjoins non-whitespace
2925 characters; for comparison expressions, e.g. "a < b > c",
2926 there must be spaces before the '<', etc. */
2927 const char *p = find_template_name_end (tokstart + namelen);
2928
2929 if (p)
2930 namelen = p - tokstart;
2931 }
2932 break;
2933 }
2934 c = tokstart[++namelen];
2935 }
2936
2937 /* The token "if" terminates the expression and is NOT removed from
2938 the input stream. It doesn't count if it appears in the
2939 expansion of a macro. */
2940 if (namelen == 2
2941 && tokstart[0] == 'i'
2942 && tokstart[1] == 'f'
2943 && ! scanning_macro_expansion ())
2944 {
2945 return 0;
2946 }
2947
2948 /* For the same reason (breakpoint conditions), "thread N"
2949 terminates the expression. "thread" could be an identifier, but
2950 an identifier is never followed by a number without intervening
2951 punctuation. "task" is similar. Handle abbreviations of these,
2952 similarly to breakpoint.c:find_condition_and_thread. */
2953 if (namelen >= 1
2954 && (strncmp (tokstart, "thread", namelen) == 0
2955 || strncmp (tokstart, "task", namelen) == 0)
2956 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2957 && ! scanning_macro_expansion ())
2958 {
2959 const char *p = tokstart + namelen + 1;
2960
2961 while (*p == ' ' || *p == '\t')
2962 p++;
2963 if (*p >= '0' && *p <= '9')
2964 return 0;
2965 }
2966
2967 pstate->lexptr += namelen;
2968
2969 tryname:
2970
2971 yylval.sval.ptr = tokstart;
2972 yylval.sval.length = namelen;
2973
2974 /* Catch specific keywords. */
2975 std::string copy = copy_name (yylval.sval);
2976 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2977 if (copy == ident_tokens[i].oper)
2978 {
2979 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2980 && par_state->language ()->la_language != language_cplus)
2981 break;
2982 if ((ident_tokens[i].flags & FLAG_C) != 0
2983 && par_state->language ()->la_language != language_c
2984 && par_state->language ()->la_language != language_objc)
2985 break;
2986
2987 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2988 {
2989 struct field_of_this_result is_a_field_of_this;
2990
2991 if (lookup_symbol (copy.c_str (),
2992 pstate->expression_context_block,
2993 VAR_DOMAIN,
2994 (par_state->language ()->la_language
2995 == language_cplus ? &is_a_field_of_this
2996 : NULL)).symbol
2997 != NULL)
2998 {
2999 /* The keyword is shadowed. */
3000 break;
3001 }
3002 }
3003
3004 /* It is ok to always set this, even though we don't always
3005 strictly need to. */
3006 yylval.opcode = ident_tokens[i].opcode;
3007 return ident_tokens[i].token;
3008 }
3009
3010 if (*tokstart == '$')
3011 return DOLLAR_VARIABLE;
3012
3013 if (pstate->parse_completion && *pstate->lexptr == '\0')
3014 saw_name_at_eof = 1;
3015
3016 yylval.ssym.stoken = yylval.sval;
3017 yylval.ssym.sym.symbol = NULL;
3018 yylval.ssym.sym.block = NULL;
3019 yylval.ssym.is_a_field_of_this = 0;
3020 return NAME;
3021 }
3022
3023 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
3024 struct token_and_value
3025 {
3026 int token;
3027 YYSTYPE value;
3028 };
3029
3030 /* A FIFO of tokens that have been read but not yet returned to the
3031 parser. */
3032 static std::vector<token_and_value> token_fifo;
3033
3034 /* Non-zero if the lexer should return tokens from the FIFO. */
3035 static int popping;
3036
3037 /* Temporary storage for c_lex; this holds symbol names as they are
3038 built up. */
3039 static auto_obstack name_obstack;
3040
3041 /* Classify a NAME token. The contents of the token are in `yylval'.
3042 Updates yylval and returns the new token type. BLOCK is the block
3043 in which lookups start; this can be NULL to mean the global scope.
3044 IS_QUOTED_NAME is non-zero if the name token was originally quoted
3045 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
3046 a structure operator -- either '.' or ARROW */
3047
3048 static int
3049 classify_name (struct parser_state *par_state, const struct block *block,
3050 bool is_quoted_name, bool is_after_structop)
3051 {
3052 struct block_symbol bsym;
3053 struct field_of_this_result is_a_field_of_this;
3054
3055 std::string copy = copy_name (yylval.sval);
3056
3057 /* Initialize this in case we *don't* use it in this call; that way
3058 we can refer to it unconditionally below. */
3059 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
3060
3061 bsym = lookup_symbol (copy.c_str (), block, VAR_DOMAIN,
3062 par_state->language ()->name_of_this ()
3063 ? &is_a_field_of_this : NULL);
3064
3065 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK)
3066 {
3067 yylval.ssym.sym = bsym;
3068 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3069 return BLOCKNAME;
3070 }
3071 else if (!bsym.symbol)
3072 {
3073 /* If we found a field of 'this', we might have erroneously
3074 found a constructor where we wanted a type name. Handle this
3075 case by noticing that we found a constructor and then look up
3076 the type tag instead. */
3077 if (is_a_field_of_this.type != NULL
3078 && is_a_field_of_this.fn_field != NULL
3079 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
3080 0))
3081 {
3082 struct field_of_this_result inner_is_a_field_of_this;
3083
3084 bsym = lookup_symbol (copy.c_str (), block, STRUCT_DOMAIN,
3085 &inner_is_a_field_of_this);
3086 if (bsym.symbol != NULL)
3087 {
3088 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3089 return TYPENAME;
3090 }
3091 }
3092
3093 /* If we found a field on the "this" object, or we are looking
3094 up a field on a struct, then we want to prefer it over a
3095 filename. However, if the name was quoted, then it is better
3096 to check for a filename or a block, since this is the only
3097 way the user has of requiring the extension to be used. */
3098 if ((is_a_field_of_this.type == NULL && !is_after_structop)
3099 || is_quoted_name)
3100 {
3101 /* See if it's a file name. */
3102 struct symtab *symtab;
3103
3104 symtab = lookup_symtab (copy.c_str ());
3105 if (symtab)
3106 {
3107 yylval.bval = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab),
3108 STATIC_BLOCK);
3109 return FILENAME;
3110 }
3111 }
3112 }
3113
3114 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF)
3115 {
3116 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3117 return TYPENAME;
3118 }
3119
3120 /* See if it's an ObjC classname. */
3121 if (par_state->language ()->la_language == language_objc && !bsym.symbol)
3122 {
3123 CORE_ADDR Class = lookup_objc_class (par_state->gdbarch (),
3124 copy.c_str ());
3125 if (Class)
3126 {
3127 struct symbol *sym;
3128
3129 yylval.theclass.theclass = Class;
3130 sym = lookup_struct_typedef (copy.c_str (),
3131 par_state->expression_context_block, 1);
3132 if (sym)
3133 yylval.theclass.type = SYMBOL_TYPE (sym);
3134 return CLASSNAME;
3135 }
3136 }
3137
3138 /* Input names that aren't symbols but ARE valid hex numbers, when
3139 the input radix permits them, can be names or numbers depending
3140 on the parse. Note we support radixes > 16 here. */
3141 if (!bsym.symbol
3142 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3143 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3144 {
3145 YYSTYPE newlval; /* Its value is ignored. */
3146 int hextype = parse_number (par_state, copy.c_str (), yylval.sval.length,
3147 0, &newlval);
3148
3149 if (hextype == INT)
3150 {
3151 yylval.ssym.sym = bsym;
3152 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3153 return NAME_OR_INT;
3154 }
3155 }
3156
3157 /* Any other kind of symbol */
3158 yylval.ssym.sym = bsym;
3159 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3160
3161 if (bsym.symbol == NULL
3162 && par_state->language ()->la_language == language_cplus
3163 && is_a_field_of_this.type == NULL
3164 && lookup_minimal_symbol (copy.c_str (), NULL, NULL).minsym == NULL)
3165 return UNKNOWN_CPP_NAME;
3166
3167 return NAME;
3168 }
3169
3170 /* Like classify_name, but used by the inner loop of the lexer, when a
3171 name might have already been seen. CONTEXT is the context type, or
3172 NULL if this is the first component of a name. */
3173
3174 static int
3175 classify_inner_name (struct parser_state *par_state,
3176 const struct block *block, struct type *context)
3177 {
3178 struct type *type;
3179
3180 if (context == NULL)
3181 return classify_name (par_state, block, false, false);
3182
3183 type = check_typedef (context);
3184 if (!type_aggregate_p (type))
3185 return ERROR;
3186
3187 std::string copy = copy_name (yylval.ssym.stoken);
3188 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3189 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy.c_str (), block,
3190 VAR_DOMAIN);
3191
3192 /* If no symbol was found, search for a matching base class named
3193 COPY. This will allow users to enter qualified names of class members
3194 relative to the `this' pointer. */
3195 if (yylval.ssym.sym.symbol == NULL)
3196 {
3197 struct type *base_type = cp_find_type_baseclass_by_name (type,
3198 copy.c_str ());
3199
3200 if (base_type != NULL)
3201 {
3202 yylval.tsym.type = base_type;
3203 return TYPENAME;
3204 }
3205
3206 return ERROR;
3207 }
3208
3209 switch (SYMBOL_CLASS (yylval.ssym.sym.symbol))
3210 {
3211 case LOC_BLOCK:
3212 case LOC_LABEL:
3213 /* cp_lookup_nested_symbol might have accidentally found a constructor
3214 named COPY when we really wanted a base class of the same name.
3215 Double-check this case by looking for a base class. */
3216 {
3217 struct type *base_type
3218 = cp_find_type_baseclass_by_name (type, copy.c_str ());
3219
3220 if (base_type != NULL)
3221 {
3222 yylval.tsym.type = base_type;
3223 return TYPENAME;
3224 }
3225 }
3226 return ERROR;
3227
3228 case LOC_TYPEDEF:
3229 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
3230 return TYPENAME;
3231
3232 default:
3233 return NAME;
3234 }
3235 internal_error (__FILE__, __LINE__, _("not reached"));
3236 }
3237
3238 /* The outer level of a two-level lexer. This calls the inner lexer
3239 to return tokens. It then either returns these tokens, or
3240 aggregates them into a larger token. This lets us work around a
3241 problem in our parsing approach, where the parser could not
3242 distinguish between qualified names and qualified types at the
3243 right point.
3244
3245 This approach is still not ideal, because it mishandles template
3246 types. See the comment in lex_one_token for an example. However,
3247 this is still an improvement over the earlier approach, and will
3248 suffice until we move to better parsing technology. */
3249
3250 static int
3251 yylex (void)
3252 {
3253 token_and_value current;
3254 int first_was_coloncolon, last_was_coloncolon;
3255 struct type *context_type = NULL;
3256 int last_to_examine, next_to_examine, checkpoint;
3257 const struct block *search_block;
3258 bool is_quoted_name, last_lex_was_structop;
3259
3260 if (popping && !token_fifo.empty ())
3261 goto do_pop;
3262 popping = 0;
3263
3264 last_lex_was_structop = last_was_structop;
3265
3266 /* Read the first token and decide what to do. Most of the
3267 subsequent code is C++-only; but also depends on seeing a "::" or
3268 name-like token. */
3269 current.token = lex_one_token (pstate, &is_quoted_name);
3270 if (current.token == NAME)
3271 current.token = classify_name (pstate, pstate->expression_context_block,
3272 is_quoted_name, last_lex_was_structop);
3273 if (pstate->language ()->la_language != language_cplus
3274 || (current.token != TYPENAME && current.token != COLONCOLON
3275 && current.token != FILENAME))
3276 return current.token;
3277
3278 /* Read any sequence of alternating "::" and name-like tokens into
3279 the token FIFO. */
3280 current.value = yylval;
3281 token_fifo.push_back (current);
3282 last_was_coloncolon = current.token == COLONCOLON;
3283 while (1)
3284 {
3285 bool ignore;
3286
3287 /* We ignore quoted names other than the very first one.
3288 Subsequent ones do not have any special meaning. */
3289 current.token = lex_one_token (pstate, &ignore);
3290 current.value = yylval;
3291 token_fifo.push_back (current);
3292
3293 if ((last_was_coloncolon && current.token != NAME)
3294 || (!last_was_coloncolon && current.token != COLONCOLON))
3295 break;
3296 last_was_coloncolon = !last_was_coloncolon;
3297 }
3298 popping = 1;
3299
3300 /* We always read one extra token, so compute the number of tokens
3301 to examine accordingly. */
3302 last_to_examine = token_fifo.size () - 2;
3303 next_to_examine = 0;
3304
3305 current = token_fifo[next_to_examine];
3306 ++next_to_examine;
3307
3308 name_obstack.clear ();
3309 checkpoint = 0;
3310 if (current.token == FILENAME)
3311 search_block = current.value.bval;
3312 else if (current.token == COLONCOLON)
3313 search_block = NULL;
3314 else
3315 {
3316 gdb_assert (current.token == TYPENAME);
3317 search_block = pstate->expression_context_block;
3318 obstack_grow (&name_obstack, current.value.sval.ptr,
3319 current.value.sval.length);
3320 context_type = current.value.tsym.type;
3321 checkpoint = 1;
3322 }
3323
3324 first_was_coloncolon = current.token == COLONCOLON;
3325 last_was_coloncolon = first_was_coloncolon;
3326
3327 while (next_to_examine <= last_to_examine)
3328 {
3329 token_and_value next;
3330
3331 next = token_fifo[next_to_examine];
3332 ++next_to_examine;
3333
3334 if (next.token == NAME && last_was_coloncolon)
3335 {
3336 int classification;
3337
3338 yylval = next.value;
3339 classification = classify_inner_name (pstate, search_block,
3340 context_type);
3341 /* We keep going until we either run out of names, or until
3342 we have a qualified name which is not a type. */
3343 if (classification != TYPENAME && classification != NAME)
3344 break;
3345
3346 /* Accept up to this token. */
3347 checkpoint = next_to_examine;
3348
3349 /* Update the partial name we are constructing. */
3350 if (context_type != NULL)
3351 {
3352 /* We don't want to put a leading "::" into the name. */
3353 obstack_grow_str (&name_obstack, "::");
3354 }
3355 obstack_grow (&name_obstack, next.value.sval.ptr,
3356 next.value.sval.length);
3357
3358 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
3359 yylval.sval.length = obstack_object_size (&name_obstack);
3360 current.value = yylval;
3361 current.token = classification;
3362
3363 last_was_coloncolon = 0;
3364
3365 if (classification == NAME)
3366 break;
3367
3368 context_type = yylval.tsym.type;
3369 }
3370 else if (next.token == COLONCOLON && !last_was_coloncolon)
3371 last_was_coloncolon = 1;
3372 else
3373 {
3374 /* We've reached the end of the name. */
3375 break;
3376 }
3377 }
3378
3379 /* If we have a replacement token, install it as the first token in
3380 the FIFO, and delete the other constituent tokens. */
3381 if (checkpoint > 0)
3382 {
3383 current.value.sval.ptr
3384 = obstack_strndup (&cpstate->expansion_obstack,
3385 current.value.sval.ptr,
3386 current.value.sval.length);
3387
3388 token_fifo[0] = current;
3389 if (checkpoint > 1)
3390 token_fifo.erase (token_fifo.begin () + 1,
3391 token_fifo.begin () + checkpoint);
3392 }
3393
3394 do_pop:
3395 current = token_fifo[0];
3396 token_fifo.erase (token_fifo.begin ());
3397 yylval = current.value;
3398 return current.token;
3399 }
3400
3401 int
3402 c_parse (struct parser_state *par_state)
3403 {
3404 /* Setting up the parser state. */
3405 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3406 gdb_assert (par_state != NULL);
3407 pstate = par_state;
3408
3409 c_parse_state cstate;
3410 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3411
3412 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
3413
3414 if (par_state->expression_context_block)
3415 macro_scope
3416 = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
3417 else
3418 macro_scope = default_macro_scope ();
3419 if (! macro_scope)
3420 macro_scope = user_macro_scope ();
3421
3422 scoped_restore restore_macro_scope
3423 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
3424
3425 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3426 parser_debug);
3427
3428 /* Initialize some state used by the lexer. */
3429 last_was_structop = false;
3430 saw_name_at_eof = 0;
3431 paren_depth = 0;
3432
3433 token_fifo.clear ();
3434 popping = 0;
3435 name_obstack.clear ();
3436
3437 int result = yyparse ();
3438 if (!result)
3439 pstate->set_operation (pstate->pop ());
3440 return result;
3441 }
3442
3443 #ifdef YYBISON
3444
3445 /* This is called via the YYPRINT macro when parser debugging is
3446 enabled. It prints a token's value. */
3447
3448 static void
3449 c_print_token (FILE *file, int type, YYSTYPE value)
3450 {
3451 switch (type)
3452 {
3453 case INT:
3454 parser_fprintf (file, "typed_val_int<%s, %s>",
3455 TYPE_SAFE_NAME (value.typed_val_int.type),
3456 pulongest (value.typed_val_int.val));
3457 break;
3458
3459 case CHAR:
3460 case STRING:
3461 {
3462 char *copy = (char *) alloca (value.tsval.length + 1);
3463
3464 memcpy (copy, value.tsval.ptr, value.tsval.length);
3465 copy[value.tsval.length] = '\0';
3466
3467 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3468 }
3469 break;
3470
3471 case NSSTRING:
3472 case DOLLAR_VARIABLE:
3473 parser_fprintf (file, "sval<%s>", copy_name (value.sval).c_str ());
3474 break;
3475
3476 case TYPENAME:
3477 parser_fprintf (file, "tsym<type=%s, name=%s>",
3478 TYPE_SAFE_NAME (value.tsym.type),
3479 copy_name (value.tsym.stoken).c_str ());
3480 break;
3481
3482 case NAME:
3483 case UNKNOWN_CPP_NAME:
3484 case NAME_OR_INT:
3485 case BLOCKNAME:
3486 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3487 copy_name (value.ssym.stoken).c_str (),
3488 (value.ssym.sym.symbol == NULL
3489 ? "(null)" : value.ssym.sym.symbol->print_name ()),
3490 value.ssym.is_a_field_of_this);
3491 break;
3492
3493 case FILENAME:
3494 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3495 break;
3496 }
3497 }
3498
3499 #endif
3500
3501 static void
3502 yyerror (const char *msg)
3503 {
3504 if (pstate->prev_lexptr)
3505 pstate->lexptr = pstate->prev_lexptr;
3506
3507 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
3508 }
This page took 0.102685 seconds and 4 git commands to generate.