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