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