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