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