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