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