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