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