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