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