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