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