2007-10-25 Wu Zhou <woodzltc@cn.ibm.com>
[platform/upstream/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 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.  */
21
22 /* Parse a C expression from text in a string,
23    and return the result as a  struct expression  pointer.
24    That structure contains arithmetic operations in reverse polish,
25    with constants represented by operations that are followed by special data.
26    See expression.h for the details of the format.
27    What is important here is that it can be built up sequentially
28    during the process of parsing; the lower levels of the tree always
29    come first in the result.
30
31    Note that malloc's and realloc's in this file are transformed to
32    xmalloc and xrealloc respectively by the same sed command in the
33    makefile that remaps any other malloc/realloc inserted by the parser
34    generator.  Doing this with #defines and trying to control the interaction
35    with include files (<malloc.h> and <stdlib.h> for example) just became
36    too messy, particularly when such includes can be inserted at random
37    times by the parser generator.  */
38    
39 %{
40
41 #include "defs.h"
42 #include "gdb_string.h"
43 #include <ctype.h>
44 #include "expression.h"
45 #include "value.h"
46 #include "parser-defs.h"
47 #include "language.h"
48 #include "c-lang.h"
49 #include "bfd.h" /* Required by objfiles.h.  */
50 #include "symfile.h" /* Required by objfiles.h.  */
51 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
52 #include "charset.h"
53 #include "block.h"
54 #include "cp-support.h"
55 #include "dfp.h"
56
57 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
58    as well as gratuitiously global symbol names, so we can have multiple
59    yacc generated parsers in gdb.  Note that these are only the variables
60    produced by yacc.  If other parser generators (bison, byacc, etc) produce
61    additional global names that conflict at link time, then those parser
62    generators need to be fixed instead of adding those names to this list. */
63
64 #define yymaxdepth c_maxdepth
65 #define yyparse c_parse
66 #define yylex   c_lex
67 #define yyerror c_error
68 #define yylval  c_lval
69 #define yychar  c_char
70 #define yydebug c_debug
71 #define yypact  c_pact  
72 #define yyr1    c_r1                    
73 #define yyr2    c_r2                    
74 #define yydef   c_def           
75 #define yychk   c_chk           
76 #define yypgo   c_pgo           
77 #define yyact   c_act           
78 #define yyexca  c_exca
79 #define yyerrflag c_errflag
80 #define yynerrs c_nerrs
81 #define yyps    c_ps
82 #define yypv    c_pv
83 #define yys     c_s
84 #define yy_yys  c_yys
85 #define yystate c_state
86 #define yytmp   c_tmp
87 #define yyv     c_v
88 #define yy_yyv  c_yyv
89 #define yyval   c_val
90 #define yylloc  c_lloc
91 #define yyreds  c_reds          /* With YYDEBUG defined */
92 #define yytoks  c_toks          /* With YYDEBUG defined */
93 #define yyname  c_name          /* With YYDEBUG defined */
94 #define yyrule  c_rule          /* With YYDEBUG defined */
95 #define yylhs   c_yylhs
96 #define yylen   c_yylen
97 #define yydefred c_yydefred
98 #define yydgoto c_yydgoto
99 #define yysindex c_yysindex
100 #define yyrindex c_yyrindex
101 #define yygindex c_yygindex
102 #define yytable  c_yytable
103 #define yycheck  c_yycheck
104
105 #ifndef YYDEBUG
106 #define YYDEBUG 1               /* Default to yydebug support */
107 #endif
108
109 #define YYFPRINTF parser_fprintf
110
111 int yyparse (void);
112
113 static int yylex (void);
114
115 void yyerror (char *);
116
117 %}
118
119 /* Although the yacc "value" of an expression is not used,
120    since the result is stored in the structure being created,
121    other node types do have values.  */
122
123 %union
124   {
125     LONGEST lval;
126     struct {
127       LONGEST val;
128       struct type *type;
129     } typed_val_int;
130     struct {
131       DOUBLEST dval;
132       struct type *type;
133     } typed_val_float;
134     struct {
135       gdb_byte val[16];
136       struct type *type;
137     } typed_val_decfloat;
138     struct symbol *sym;
139     struct type *tval;
140     struct stoken sval;
141     struct ttype tsym;
142     struct symtoken ssym;
143     int voidval;
144     struct block *bval;
145     enum exp_opcode opcode;
146     struct internalvar *ivar;
147
148     struct type **tvec;
149     int *ivec;
150   }
151
152 %{
153 /* YYSTYPE gets defined by %union */
154 static int parse_number (char *, int, int, YYSTYPE *);
155 %}
156
157 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
158 %type <lval> rcurly
159 %type <tval> type typebase qualified_type
160 %type <tvec> nonempty_typelist
161 /* %type <bval> block */
162
163 /* Fancy type parsing.  */
164 %type <voidval> func_mod direct_abs_decl abs_decl
165 %type <tval> ptype
166 %type <lval> array_mod
167
168 %token <typed_val_int> INT
169 %token <typed_val_float> FLOAT
170 %token <typed_val_decfloat> DECFLOAT
171
172 /* Both NAME and TYPENAME tokens represent symbols in the input,
173    and both convey their data as strings.
174    But a TYPENAME is a string that happens to be defined as a typedef
175    or builtin type name (such as int or char)
176    and a NAME is any other symbol.
177    Contexts where this distinction is not important can use the
178    nonterminal "name", which matches either NAME or TYPENAME.  */
179
180 %token <sval> STRING
181 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
182 %token <tsym> TYPENAME
183 %type <sval> name
184 %type <ssym> name_not_typename
185 %type <tsym> typename
186
187 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
188    but which would parse as a valid number in the current input radix.
189    E.g. "c" when input_radix==16.  Depending on the parse, it will be
190    turned into a name or into a number.  */
191
192 %token <ssym> NAME_OR_INT 
193
194 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
195 %token TEMPLATE
196 %token ERROR
197
198 /* Special type cases, put in to allow the parser to distinguish different
199    legal basetypes.  */
200 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
201
202 %token <voidval> VARIABLE
203
204 %token <opcode> ASSIGN_MODIFY
205
206 /* C++ */
207 %token TRUEKEYWORD
208 %token FALSEKEYWORD
209
210
211 %left ','
212 %left ABOVE_COMMA
213 %right '=' ASSIGN_MODIFY
214 %right '?'
215 %left OROR
216 %left ANDAND
217 %left '|'
218 %left '^'
219 %left '&'
220 %left EQUAL NOTEQUAL
221 %left '<' '>' LEQ GEQ
222 %left LSH RSH
223 %left '@'
224 %left '+' '-'
225 %left '*' '/' '%'
226 %right UNARY INCREMENT DECREMENT
227 %right ARROW '.' '[' '('
228 %token <ssym> BLOCKNAME 
229 %token <bval> FILENAME
230 %type <bval> block
231 %left COLONCOLON
232
233 \f
234 %%
235
236 start   :       exp1
237         |       type_exp
238         ;
239
240 type_exp:       type
241                         { write_exp_elt_opcode(OP_TYPE);
242                           write_exp_elt_type($1);
243                           write_exp_elt_opcode(OP_TYPE);}
244         ;
245
246 /* Expressions, including the comma operator.  */
247 exp1    :       exp
248         |       exp1 ',' exp
249                         { write_exp_elt_opcode (BINOP_COMMA); }
250         ;
251
252 /* Expressions, not including the comma operator.  */
253 exp     :       '*' exp    %prec UNARY
254                         { write_exp_elt_opcode (UNOP_IND); }
255         ;
256
257 exp     :       '&' exp    %prec UNARY
258                         { write_exp_elt_opcode (UNOP_ADDR); }
259         ;
260
261 exp     :       '-' exp    %prec UNARY
262                         { write_exp_elt_opcode (UNOP_NEG); }
263         ;
264
265 exp     :       '+' exp    %prec UNARY
266                         { write_exp_elt_opcode (UNOP_PLUS); }
267         ;
268
269 exp     :       '!' exp    %prec UNARY
270                         { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
271         ;
272
273 exp     :       '~' exp    %prec UNARY
274                         { write_exp_elt_opcode (UNOP_COMPLEMENT); }
275         ;
276
277 exp     :       INCREMENT exp    %prec UNARY
278                         { write_exp_elt_opcode (UNOP_PREINCREMENT); }
279         ;
280
281 exp     :       DECREMENT exp    %prec UNARY
282                         { write_exp_elt_opcode (UNOP_PREDECREMENT); }
283         ;
284
285 exp     :       exp INCREMENT    %prec UNARY
286                         { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
287         ;
288
289 exp     :       exp DECREMENT    %prec UNARY
290                         { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
291         ;
292
293 exp     :       SIZEOF exp       %prec UNARY
294                         { write_exp_elt_opcode (UNOP_SIZEOF); }
295         ;
296
297 exp     :       exp ARROW name
298                         { write_exp_elt_opcode (STRUCTOP_PTR);
299                           write_exp_string ($3);
300                           write_exp_elt_opcode (STRUCTOP_PTR); }
301         ;
302
303 exp     :       exp ARROW qualified_name
304                         { /* exp->type::name becomes exp->*(&type::name) */
305                           /* Note: this doesn't work if name is a
306                              static member!  FIXME */
307                           write_exp_elt_opcode (UNOP_ADDR);
308                           write_exp_elt_opcode (STRUCTOP_MPTR); }
309         ;
310
311 exp     :       exp ARROW '*' exp
312                         { write_exp_elt_opcode (STRUCTOP_MPTR); }
313         ;
314
315 exp     :       exp '.' name
316                         { write_exp_elt_opcode (STRUCTOP_STRUCT);
317                           write_exp_string ($3);
318                           write_exp_elt_opcode (STRUCTOP_STRUCT); }
319         ;
320
321 exp     :       exp '.' qualified_name
322                         { /* exp.type::name becomes exp.*(&type::name) */
323                           /* Note: this doesn't work if name is a
324                              static member!  FIXME */
325                           write_exp_elt_opcode (UNOP_ADDR);
326                           write_exp_elt_opcode (STRUCTOP_MEMBER); }
327         ;
328
329 exp     :       exp '.' '*' exp
330                         { write_exp_elt_opcode (STRUCTOP_MEMBER); }
331         ;
332
333 exp     :       exp '[' exp1 ']'
334                         { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
335         ;
336
337 exp     :       exp '(' 
338                         /* This is to save the value of arglist_len
339                            being accumulated by an outer function call.  */
340                         { start_arglist (); }
341                 arglist ')'     %prec ARROW
342                         { write_exp_elt_opcode (OP_FUNCALL);
343                           write_exp_elt_longcst ((LONGEST) end_arglist ());
344                           write_exp_elt_opcode (OP_FUNCALL); }
345         ;
346
347 lcurly  :       '{'
348                         { start_arglist (); }
349         ;
350
351 arglist :
352         ;
353
354 arglist :       exp
355                         { arglist_len = 1; }
356         ;
357
358 arglist :       arglist ',' exp   %prec ABOVE_COMMA
359                         { arglist_len++; }
360         ;
361
362 rcurly  :       '}'
363                         { $$ = end_arglist () - 1; }
364         ;
365 exp     :       lcurly arglist rcurly   %prec ARROW
366                         { write_exp_elt_opcode (OP_ARRAY);
367                           write_exp_elt_longcst ((LONGEST) 0);
368                           write_exp_elt_longcst ((LONGEST) $3);
369                           write_exp_elt_opcode (OP_ARRAY); }
370         ;
371
372 exp     :       lcurly type rcurly exp  %prec UNARY
373                         { write_exp_elt_opcode (UNOP_MEMVAL);
374                           write_exp_elt_type ($2);
375                           write_exp_elt_opcode (UNOP_MEMVAL); }
376         ;
377
378 exp     :       '(' type ')' exp  %prec UNARY
379                         { write_exp_elt_opcode (UNOP_CAST);
380                           write_exp_elt_type ($2);
381                           write_exp_elt_opcode (UNOP_CAST); }
382         ;
383
384 exp     :       '(' exp1 ')'
385                         { }
386         ;
387
388 /* Binary operators in order of decreasing precedence.  */
389
390 exp     :       exp '@' exp
391                         { write_exp_elt_opcode (BINOP_REPEAT); }
392         ;
393
394 exp     :       exp '*' exp
395                         { write_exp_elt_opcode (BINOP_MUL); }
396         ;
397
398 exp     :       exp '/' exp
399                         { write_exp_elt_opcode (BINOP_DIV); }
400         ;
401
402 exp     :       exp '%' exp
403                         { write_exp_elt_opcode (BINOP_REM); }
404         ;
405
406 exp     :       exp '+' exp
407                         { write_exp_elt_opcode (BINOP_ADD); }
408         ;
409
410 exp     :       exp '-' exp
411                         { write_exp_elt_opcode (BINOP_SUB); }
412         ;
413
414 exp     :       exp LSH exp
415                         { write_exp_elt_opcode (BINOP_LSH); }
416         ;
417
418 exp     :       exp RSH exp
419                         { write_exp_elt_opcode (BINOP_RSH); }
420         ;
421
422 exp     :       exp EQUAL exp
423                         { write_exp_elt_opcode (BINOP_EQUAL); }
424         ;
425
426 exp     :       exp NOTEQUAL exp
427                         { write_exp_elt_opcode (BINOP_NOTEQUAL); }
428         ;
429
430 exp     :       exp LEQ exp
431                         { write_exp_elt_opcode (BINOP_LEQ); }
432         ;
433
434 exp     :       exp GEQ exp
435                         { write_exp_elt_opcode (BINOP_GEQ); }
436         ;
437
438 exp     :       exp '<' exp
439                         { write_exp_elt_opcode (BINOP_LESS); }
440         ;
441
442 exp     :       exp '>' exp
443                         { write_exp_elt_opcode (BINOP_GTR); }
444         ;
445
446 exp     :       exp '&' exp
447                         { write_exp_elt_opcode (BINOP_BITWISE_AND); }
448         ;
449
450 exp     :       exp '^' exp
451                         { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
452         ;
453
454 exp     :       exp '|' exp
455                         { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
456         ;
457
458 exp     :       exp ANDAND exp
459                         { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
460         ;
461
462 exp     :       exp OROR exp
463                         { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
464         ;
465
466 exp     :       exp '?' exp ':' exp     %prec '?'
467                         { write_exp_elt_opcode (TERNOP_COND); }
468         ;
469                           
470 exp     :       exp '=' exp
471                         { write_exp_elt_opcode (BINOP_ASSIGN); }
472         ;
473
474 exp     :       exp ASSIGN_MODIFY exp
475                         { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
476                           write_exp_elt_opcode ($2);
477                           write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
478         ;
479
480 exp     :       INT
481                         { write_exp_elt_opcode (OP_LONG);
482                           write_exp_elt_type ($1.type);
483                           write_exp_elt_longcst ((LONGEST)($1.val));
484                           write_exp_elt_opcode (OP_LONG); }
485         ;
486
487 exp     :       NAME_OR_INT
488                         { YYSTYPE val;
489                           parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
490                           write_exp_elt_opcode (OP_LONG);
491                           write_exp_elt_type (val.typed_val_int.type);
492                           write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
493                           write_exp_elt_opcode (OP_LONG);
494                         }
495         ;
496
497
498 exp     :       FLOAT
499                         { write_exp_elt_opcode (OP_DOUBLE);
500                           write_exp_elt_type ($1.type);
501                           write_exp_elt_dblcst ($1.dval);
502                           write_exp_elt_opcode (OP_DOUBLE); }
503         ;
504
505 exp     :       DECFLOAT
506                         { write_exp_elt_opcode (OP_DECFLOAT);
507                           write_exp_elt_type ($1.type);
508                           write_exp_elt_decfloatcst ($1.val);
509                           write_exp_elt_opcode (OP_DECFLOAT); }
510         ;
511
512 exp     :       variable
513         ;
514
515 exp     :       VARIABLE
516                         /* Already written by write_dollar_variable. */
517         ;
518
519 exp     :       SIZEOF '(' type ')'     %prec UNARY
520                         { write_exp_elt_opcode (OP_LONG);
521                           write_exp_elt_type (builtin_type (current_gdbarch)->builtin_int);
522                           CHECK_TYPEDEF ($3);
523                           write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
524                           write_exp_elt_opcode (OP_LONG); }
525         ;
526
527 exp     :       STRING
528                         { /* C strings are converted into array constants with
529                              an explicit null byte added at the end.  Thus
530                              the array upper bound is the string length.
531                              There is no such thing in C as a completely empty
532                              string. */
533                           char *sp = $1.ptr; int count = $1.length;
534                           while (count-- > 0)
535                             {
536                               write_exp_elt_opcode (OP_LONG);
537                               write_exp_elt_type (builtin_type (current_gdbarch)->builtin_char);
538                               write_exp_elt_longcst ((LONGEST)(*sp++));
539                               write_exp_elt_opcode (OP_LONG);
540                             }
541                           write_exp_elt_opcode (OP_LONG);
542                           write_exp_elt_type (builtin_type (current_gdbarch)->builtin_char);
543                           write_exp_elt_longcst ((LONGEST)'\0');
544                           write_exp_elt_opcode (OP_LONG);
545                           write_exp_elt_opcode (OP_ARRAY);
546                           write_exp_elt_longcst ((LONGEST) 0);
547                           write_exp_elt_longcst ((LONGEST) ($1.length));
548                           write_exp_elt_opcode (OP_ARRAY); }
549         ;
550
551 /* C++.  */
552 exp     :       TRUEKEYWORD    
553                         { write_exp_elt_opcode (OP_LONG);
554                           write_exp_elt_type (builtin_type (current_gdbarch)->builtin_bool);
555                           write_exp_elt_longcst ((LONGEST) 1);
556                           write_exp_elt_opcode (OP_LONG); }
557         ;
558
559 exp     :       FALSEKEYWORD   
560                         { write_exp_elt_opcode (OP_LONG);
561                           write_exp_elt_type (builtin_type (current_gdbarch)->builtin_bool);
562                           write_exp_elt_longcst ((LONGEST) 0);
563                           write_exp_elt_opcode (OP_LONG); }
564         ;
565
566 /* end of C++.  */
567
568 block   :       BLOCKNAME
569                         {
570                           if ($1.sym)
571                             $$ = SYMBOL_BLOCK_VALUE ($1.sym);
572                           else
573                             error ("No file or function \"%s\".",
574                                    copy_name ($1.stoken));
575                         }
576         |       FILENAME
577                         {
578                           $$ = $1;
579                         }
580         ;
581
582 block   :       block COLONCOLON name
583                         { struct symbol *tem
584                             = lookup_symbol (copy_name ($3), $1,
585                                              VAR_DOMAIN, (int *) NULL,
586                                              (struct symtab **) NULL);
587                           if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
588                             error ("No function \"%s\" in specified context.",
589                                    copy_name ($3));
590                           $$ = SYMBOL_BLOCK_VALUE (tem); }
591         ;
592
593 variable:       block COLONCOLON name
594                         { struct symbol *sym;
595                           sym = lookup_symbol (copy_name ($3), $1,
596                                                VAR_DOMAIN, (int *) NULL,
597                                                (struct symtab **) NULL);
598                           if (sym == 0)
599                             error ("No symbol \"%s\" in specified context.",
600                                    copy_name ($3));
601
602                           write_exp_elt_opcode (OP_VAR_VALUE);
603                           /* block_found is set by lookup_symbol.  */
604                           write_exp_elt_block (block_found);
605                           write_exp_elt_sym (sym);
606                           write_exp_elt_opcode (OP_VAR_VALUE); }
607         ;
608
609 qualified_name: typebase COLONCOLON name
610                         {
611                           struct type *type = $1;
612                           if (TYPE_CODE (type) != TYPE_CODE_STRUCT
613                               && TYPE_CODE (type) != TYPE_CODE_UNION
614                               && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
615                             error ("`%s' is not defined as an aggregate type.",
616                                    TYPE_NAME (type));
617
618                           write_exp_elt_opcode (OP_SCOPE);
619                           write_exp_elt_type (type);
620                           write_exp_string ($3);
621                           write_exp_elt_opcode (OP_SCOPE);
622                         }
623         |       typebase COLONCOLON '~' name
624                         {
625                           struct type *type = $1;
626                           struct stoken tmp_token;
627                           if (TYPE_CODE (type) != TYPE_CODE_STRUCT
628                               && TYPE_CODE (type) != TYPE_CODE_UNION
629                               && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
630                             error ("`%s' is not defined as an aggregate type.",
631                                    TYPE_NAME (type));
632
633                           tmp_token.ptr = (char*) alloca ($4.length + 2);
634                           tmp_token.length = $4.length + 1;
635                           tmp_token.ptr[0] = '~';
636                           memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
637                           tmp_token.ptr[tmp_token.length] = 0;
638
639                           /* Check for valid destructor name.  */
640                           destructor_name_p (tmp_token.ptr, type);
641                           write_exp_elt_opcode (OP_SCOPE);
642                           write_exp_elt_type (type);
643                           write_exp_string (tmp_token);
644                           write_exp_elt_opcode (OP_SCOPE);
645                         }
646         ;
647
648 variable:       qualified_name
649         |       COLONCOLON name
650                         {
651                           char *name = copy_name ($2);
652                           struct symbol *sym;
653                           struct minimal_symbol *msymbol;
654
655                           sym =
656                             lookup_symbol (name, (const struct block *) NULL,
657                                            VAR_DOMAIN, (int *) NULL,
658                                            (struct symtab **) NULL);
659                           if (sym)
660                             {
661                               write_exp_elt_opcode (OP_VAR_VALUE);
662                               write_exp_elt_block (NULL);
663                               write_exp_elt_sym (sym);
664                               write_exp_elt_opcode (OP_VAR_VALUE);
665                               break;
666                             }
667
668                           msymbol = lookup_minimal_symbol (name, NULL, NULL);
669                           if (msymbol != NULL)
670                             {
671                               write_exp_msymbol (msymbol,
672                                                  lookup_function_type (builtin_type (current_gdbarch)->builtin_int),
673                                                  builtin_type (current_gdbarch)->builtin_int);
674                             }
675                           else
676                             if (!have_full_symbols () && !have_partial_symbols ())
677                               error ("No symbol table is loaded.  Use the \"file\" command.");
678                             else
679                               error ("No symbol \"%s\" in current context.", name);
680                         }
681         ;
682
683 variable:       name_not_typename
684                         { struct symbol *sym = $1.sym;
685
686                           if (sym)
687                             {
688                               if (symbol_read_needs_frame (sym))
689                                 {
690                                   if (innermost_block == 0 ||
691                                       contained_in (block_found, 
692                                                     innermost_block))
693                                     innermost_block = block_found;
694                                 }
695
696                               write_exp_elt_opcode (OP_VAR_VALUE);
697                               /* We want to use the selected frame, not
698                                  another more inner frame which happens to
699                                  be in the same block.  */
700                               write_exp_elt_block (NULL);
701                               write_exp_elt_sym (sym);
702                               write_exp_elt_opcode (OP_VAR_VALUE);
703                             }
704                           else if ($1.is_a_field_of_this)
705                             {
706                               /* C++: it hangs off of `this'.  Must
707                                  not inadvertently convert from a method call
708                                  to data ref.  */
709                               if (innermost_block == 0 || 
710                                   contained_in (block_found, innermost_block))
711                                 innermost_block = block_found;
712                               write_exp_elt_opcode (OP_THIS);
713                               write_exp_elt_opcode (OP_THIS);
714                               write_exp_elt_opcode (STRUCTOP_PTR);
715                               write_exp_string ($1.stoken);
716                               write_exp_elt_opcode (STRUCTOP_PTR);
717                             }
718                           else
719                             {
720                               struct minimal_symbol *msymbol;
721                               char *arg = copy_name ($1.stoken);
722
723                               msymbol =
724                                 lookup_minimal_symbol (arg, NULL, NULL);
725                               if (msymbol != NULL)
726                                 {
727                                   write_exp_msymbol (msymbol,
728                                                      lookup_function_type (builtin_type (current_gdbarch)->builtin_int),
729                                                      builtin_type (current_gdbarch)->builtin_int);
730                                 }
731                               else if (!have_full_symbols () && !have_partial_symbols ())
732                                 error ("No symbol table is loaded.  Use the \"file\" command.");
733                               else
734                                 error ("No symbol \"%s\" in current context.",
735                                        copy_name ($1.stoken));
736                             }
737                         }
738         ;
739
740 space_identifier : '@' NAME
741                 { push_type_address_space (copy_name ($2.stoken));
742                   push_type (tp_space_identifier);
743                 }
744         ;
745
746 const_or_volatile: const_or_volatile_noopt
747         |
748         ;
749
750 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
751         ;
752
753 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
754         | const_or_volatile_noopt 
755         ;
756
757 const_or_volatile_or_space_identifier: 
758                 const_or_volatile_or_space_identifier_noopt
759         |
760         ;
761
762 abs_decl:       '*'
763                         { push_type (tp_pointer); $$ = 0; }
764         |       '*' abs_decl
765                         { push_type (tp_pointer); $$ = $2; }
766         |       '&'
767                         { push_type (tp_reference); $$ = 0; }
768         |       '&' abs_decl
769                         { push_type (tp_reference); $$ = $2; }
770         |       direct_abs_decl
771         ;
772
773 direct_abs_decl: '(' abs_decl ')'
774                         { $$ = $2; }
775         |       direct_abs_decl array_mod
776                         {
777                           push_type_int ($2);
778                           push_type (tp_array);
779                         }
780         |       array_mod
781                         {
782                           push_type_int ($1);
783                           push_type (tp_array);
784                           $$ = 0;
785                         }
786
787         |       direct_abs_decl func_mod
788                         { push_type (tp_function); }
789         |       func_mod
790                         { push_type (tp_function); }
791         ;
792
793 array_mod:      '[' ']'
794                         { $$ = -1; }
795         |       '[' INT ']'
796                         { $$ = $2.val; }
797         ;
798
799 func_mod:       '(' ')'
800                         { $$ = 0; }
801         |       '(' nonempty_typelist ')'
802                         { free ($2); $$ = 0; }
803         ;
804
805 /* We used to try to recognize pointer to member types here, but
806    that didn't work (shift/reduce conflicts meant that these rules never
807    got executed).  The problem is that
808      int (foo::bar::baz::bizzle)
809    is a function type but
810      int (foo::bar::baz::bizzle::*)
811    is a pointer to member type.  Stroustrup loses again!  */
812
813 type    :       ptype
814         ;
815
816 typebase  /* Implements (approximately): (type-qualifier)* type-specifier */
817         :       TYPENAME
818                         { $$ = $1.type; }
819         |       INT_KEYWORD
820                         { $$ = builtin_type (current_gdbarch)->builtin_int; }
821         |       LONG
822                         { $$ = builtin_type (current_gdbarch)->builtin_long; }
823         |       SHORT
824                         { $$ = builtin_type (current_gdbarch)->builtin_short; }
825         |       LONG INT_KEYWORD
826                         { $$ = builtin_type (current_gdbarch)->builtin_long; }
827         |       LONG SIGNED_KEYWORD INT_KEYWORD
828                         { $$ = builtin_type (current_gdbarch)->builtin_long; }
829         |       LONG SIGNED_KEYWORD
830                         { $$ = builtin_type (current_gdbarch)->builtin_long; }
831         |       SIGNED_KEYWORD LONG INT_KEYWORD
832                         { $$ = builtin_type (current_gdbarch)->builtin_long; }
833         |       UNSIGNED LONG INT_KEYWORD
834                         { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
835         |       LONG UNSIGNED INT_KEYWORD
836                         { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
837         |       LONG UNSIGNED
838                         { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
839         |       LONG LONG
840                         { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
841         |       LONG LONG INT_KEYWORD
842                         { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
843         |       LONG LONG SIGNED_KEYWORD INT_KEYWORD
844                         { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
845         |       LONG LONG SIGNED_KEYWORD
846                         { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
847         |       SIGNED_KEYWORD LONG LONG
848                         { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
849         |       SIGNED_KEYWORD LONG LONG INT_KEYWORD
850                         { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
851         |       UNSIGNED LONG LONG
852                         { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
853         |       UNSIGNED LONG LONG INT_KEYWORD
854                         { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
855         |       LONG LONG UNSIGNED
856                         { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
857         |       LONG LONG UNSIGNED INT_KEYWORD
858                         { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
859         |       SHORT INT_KEYWORD
860                         { $$ = builtin_type (current_gdbarch)->builtin_short; }
861         |       SHORT SIGNED_KEYWORD INT_KEYWORD
862                         { $$ = builtin_type (current_gdbarch)->builtin_short; }
863         |       SHORT SIGNED_KEYWORD
864                         { $$ = builtin_type (current_gdbarch)->builtin_short; }
865         |       UNSIGNED SHORT INT_KEYWORD
866                         { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
867         |       SHORT UNSIGNED 
868                         { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
869         |       SHORT UNSIGNED INT_KEYWORD
870                         { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
871         |       DOUBLE_KEYWORD
872                         { $$ = builtin_type (current_gdbarch)->builtin_double; }
873         |       LONG DOUBLE_KEYWORD
874                         { $$ = builtin_type (current_gdbarch)->builtin_long_double; }
875         |       STRUCT name
876                         { $$ = lookup_struct (copy_name ($2),
877                                               expression_context_block); }
878         |       CLASS name
879                         { $$ = lookup_struct (copy_name ($2),
880                                               expression_context_block); }
881         |       UNION name
882                         { $$ = lookup_union (copy_name ($2),
883                                              expression_context_block); }
884         |       ENUM name
885                         { $$ = lookup_enum (copy_name ($2),
886                                             expression_context_block); }
887         |       UNSIGNED typename
888                         { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
889         |       UNSIGNED
890                         { $$ = builtin_type (current_gdbarch)->builtin_unsigned_int; }
891         |       SIGNED_KEYWORD typename
892                         { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
893         |       SIGNED_KEYWORD
894                         { $$ = builtin_type (current_gdbarch)->builtin_int; }
895                 /* It appears that this rule for templates is never
896                    reduced; template recognition happens by lookahead
897                    in the token processing code in yylex. */         
898         |       TEMPLATE name '<' type '>'
899                         { $$ = lookup_template_type(copy_name($2), $4,
900                                                     expression_context_block);
901                         }
902         | const_or_volatile_or_space_identifier_noopt typebase 
903                         { $$ = follow_types ($2); }
904         | typebase const_or_volatile_or_space_identifier_noopt 
905                         { $$ = follow_types ($1); }
906         | qualified_type
907         ;
908
909 /* FIXME: carlton/2003-09-25: This next bit leads to lots of
910    reduce-reduce conflicts, because the parser doesn't know whether or
911    not to use qualified_name or qualified_type: the rules are
912    identical.  If the parser is parsing 'A::B::x', then, when it sees
913    the second '::', it knows that the expression to the left of it has
914    to be a type, so it uses qualified_type.  But if it is parsing just
915    'A::B', then it doesn't have any way of knowing which rule to use,
916    so there's a reduce-reduce conflict; it picks qualified_name, since
917    that occurs earlier in this file than qualified_type.
918
919    There's no good way to fix this with the grammar as it stands; as
920    far as I can tell, some of the problems arise from ambiguities that
921    GDB introduces ('start' can be either an expression or a type), but
922    some of it is inherent to the nature of C++ (you want to treat the
923    input "(FOO)" fairly differently depending on whether FOO is an
924    expression or a type, and if FOO is a complex expression, this can
925    be hard to determine at the right time).  Fortunately, it works
926    pretty well in most cases.  For example, if you do 'ptype A::B',
927    where A::B is a nested type, then the parser will mistakenly
928    misidentify it as an expression; but evaluate_subexp will get
929    called with 'noside' set to EVAL_AVOID_SIDE_EFFECTS, and everything
930    will work out anyways.  But there are situations where the parser
931    will get confused: the most common one that I've run into is when
932    you want to do
933
934      print *((A::B *) x)"
935
936    where the parser doesn't realize that A::B has to be a type until
937    it hits the first right paren, at which point it's too late.  (The
938    workaround is to type "print *(('A::B' *) x)" instead.)  (And
939    another solution is to fix our symbol-handling code so that the
940    user never wants to type something like that in the first place,
941    because we get all the types right without the user's help!)
942
943    Perhaps we could fix this by making the lexer smarter.  Some of
944    this functionality used to be in the lexer, but in a way that
945    worked even less well than the current solution: that attempt
946    involved having the parser sometimes handle '::' and having the
947    lexer sometimes handle it, and without a clear division of
948    responsibility, it quickly degenerated into a big mess.  Probably
949    the eventual correct solution will give more of a role to the lexer
950    (ideally via code that is shared between the lexer and
951    decode_line_1), but I'm not holding my breath waiting for somebody
952    to get around to cleaning this up...  */
953
954 qualified_type: typebase COLONCOLON name
955                 {
956                   struct type *type = $1;
957                   struct type *new_type;
958                   char *ncopy = alloca ($3.length + 1);
959
960                   memcpy (ncopy, $3.ptr, $3.length);
961                   ncopy[$3.length] = '\0';
962
963                   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
964                       && TYPE_CODE (type) != TYPE_CODE_UNION
965                       && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
966                     error ("`%s' is not defined as an aggregate type.",
967                            TYPE_NAME (type));
968
969                   new_type = cp_lookup_nested_type (type, ncopy,
970                                                     expression_context_block);
971                   if (new_type == NULL)
972                     error ("No type \"%s\" within class or namespace \"%s\".",
973                            ncopy, TYPE_NAME (type));
974                   
975                   $$ = new_type;
976                 }
977         ;
978
979 typename:       TYPENAME
980         |       INT_KEYWORD
981                 {
982                   $$.stoken.ptr = "int";
983                   $$.stoken.length = 3;
984                   $$.type = builtin_type (current_gdbarch)->builtin_int;
985                 }
986         |       LONG
987                 {
988                   $$.stoken.ptr = "long";
989                   $$.stoken.length = 4;
990                   $$.type = builtin_type (current_gdbarch)->builtin_long;
991                 }
992         |       SHORT
993                 {
994                   $$.stoken.ptr = "short";
995                   $$.stoken.length = 5;
996                   $$.type = builtin_type (current_gdbarch)->builtin_short;
997                 }
998         ;
999
1000 nonempty_typelist
1001         :       type
1002                 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1003                   $<ivec>$[0] = 1;      /* Number of types in vector */
1004                   $$[1] = $1;
1005                 }
1006         |       nonempty_typelist ',' type
1007                 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1008                   $$ = (struct type **) realloc ((char *) $1, len);
1009                   $$[$<ivec>$[0]] = $3;
1010                 }
1011         ;
1012
1013 ptype   :       typebase
1014         |       ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1015                 { $$ = follow_types ($1); }
1016         ;
1017
1018 const_and_volatile:     CONST_KEYWORD VOLATILE_KEYWORD
1019         |               VOLATILE_KEYWORD CONST_KEYWORD
1020         ;
1021
1022 const_or_volatile_noopt:        const_and_volatile 
1023                         { push_type (tp_const);
1024                           push_type (tp_volatile); 
1025                         }
1026         |               CONST_KEYWORD
1027                         { push_type (tp_const); }
1028         |               VOLATILE_KEYWORD
1029                         { push_type (tp_volatile); }
1030         ;
1031
1032 name    :       NAME { $$ = $1.stoken; }
1033         |       BLOCKNAME { $$ = $1.stoken; }
1034         |       TYPENAME { $$ = $1.stoken; }
1035         |       NAME_OR_INT  { $$ = $1.stoken; }
1036         ;
1037
1038 name_not_typename :     NAME
1039         |       BLOCKNAME
1040 /* These would be useful if name_not_typename was useful, but it is just
1041    a fake for "variable", so these cause reduce/reduce conflicts because
1042    the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1043    =exp) or just an exp.  If name_not_typename was ever used in an lvalue
1044    context where only a name could occur, this might be useful.
1045         |       NAME_OR_INT
1046  */
1047         ;
1048
1049 %%
1050
1051 /* Take care of parsing a number (anything that starts with a digit).
1052    Set yylval and return the token type; update lexptr.
1053    LEN is the number of characters in it.  */
1054
1055 /*** Needs some error checking for the float case ***/
1056
1057 static int
1058 parse_number (p, len, parsed_float, putithere)
1059      char *p;
1060      int len;
1061      int parsed_float;
1062      YYSTYPE *putithere;
1063 {
1064   /* FIXME: Shouldn't these be unsigned?  We don't deal with negative values
1065      here, and we do kind of silly things like cast to unsigned.  */
1066   LONGEST n = 0;
1067   LONGEST prevn = 0;
1068   ULONGEST un;
1069
1070   int i = 0;
1071   int c;
1072   int base = input_radix;
1073   int unsigned_p = 0;
1074
1075   /* Number of "L" suffixes encountered.  */
1076   int long_p = 0;
1077
1078   /* We have found a "L" or "U" suffix.  */
1079   int found_suffix = 0;
1080
1081   ULONGEST high_bit;
1082   struct type *signed_type;
1083   struct type *unsigned_type;
1084
1085   if (parsed_float)
1086     {
1087       /* It's a float since it contains a point or an exponent.  */
1088       char *s = malloc (len);
1089       int num = 0;      /* number of tokens scanned by scanf */
1090       char saved_char = p[len];
1091
1092       p[len] = 0;       /* null-terminate the token */
1093
1094       /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1095          point.  Return DECFLOAT.  */
1096
1097       if (p[len - 2] == 'd' && p[len - 1] == 'f')
1098         {
1099           p[len - 2] = '\0';
1100           putithere->typed_val_decfloat.type
1101             = builtin_type (current_gdbarch)->builtin_decfloat;
1102           decimal_from_string (putithere->typed_val_decfloat.val, 4, p);
1103           p[len] = saved_char;
1104           return (DECFLOAT);
1105         }
1106
1107       if (p[len - 2] == 'd' && p[len - 1] == 'd')
1108         {
1109           p[len - 2] = '\0';
1110           putithere->typed_val_decfloat.type
1111             = builtin_type (current_gdbarch)->builtin_decdouble;
1112           decimal_from_string (putithere->typed_val_decfloat.val, 8, p);
1113           p[len] = saved_char;
1114           return (DECFLOAT);
1115         }
1116
1117       if (p[len - 2] == 'd' && p[len - 1] == 'l')
1118         {
1119           p[len - 2] = '\0';
1120           putithere->typed_val_decfloat.type
1121             = builtin_type (current_gdbarch)->builtin_declong;
1122           decimal_from_string (putithere->typed_val_decfloat.val, 16, p);
1123           p[len] = saved_char;
1124           return (DECFLOAT);
1125         }
1126
1127       num = sscanf (p, DOUBLEST_SCAN_FORMAT "%s",
1128                     &putithere->typed_val_float.dval, s);
1129       p[len] = saved_char;      /* restore the input stream */
1130
1131       if (num == 1)
1132         putithere->typed_val_float.type = 
1133           builtin_type (current_gdbarch)->builtin_double;
1134
1135       if (num == 2 )
1136         {
1137           /* See if it has any float suffix: 'f' for float, 'l' for long 
1138              double.  */
1139           if (!strcasecmp (s, "f"))
1140             putithere->typed_val_float.type = 
1141               builtin_type (current_gdbarch)->builtin_float;
1142           else if (!strcasecmp (s, "l"))
1143             putithere->typed_val_float.type = 
1144               builtin_type (current_gdbarch)->builtin_long_double;
1145           else
1146             {
1147               free (s);
1148               return ERROR;
1149             }
1150         }
1151
1152       free (s);
1153       return FLOAT;
1154     }
1155
1156   /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1157   if (p[0] == '0')
1158     switch (p[1])
1159       {
1160       case 'x':
1161       case 'X':
1162         if (len >= 3)
1163           {
1164             p += 2;
1165             base = 16;
1166             len -= 2;
1167           }
1168         break;
1169
1170       case 't':
1171       case 'T':
1172       case 'd':
1173       case 'D':
1174         if (len >= 3)
1175           {
1176             p += 2;
1177             base = 10;
1178             len -= 2;
1179           }
1180         break;
1181
1182       default:
1183         base = 8;
1184         break;
1185       }
1186
1187   while (len-- > 0)
1188     {
1189       c = *p++;
1190       if (c >= 'A' && c <= 'Z')
1191         c += 'a' - 'A';
1192       if (c != 'l' && c != 'u')
1193         n *= base;
1194       if (c >= '0' && c <= '9')
1195         {
1196           if (found_suffix)
1197             return ERROR;
1198           n += i = c - '0';
1199         }
1200       else
1201         {
1202           if (base > 10 && c >= 'a' && c <= 'f')
1203             {
1204               if (found_suffix)
1205                 return ERROR;
1206               n += i = c - 'a' + 10;
1207             }
1208           else if (c == 'l')
1209             {
1210               ++long_p;
1211               found_suffix = 1;
1212             }
1213           else if (c == 'u')
1214             {
1215               unsigned_p = 1;
1216               found_suffix = 1;
1217             }
1218           else
1219             return ERROR;       /* Char not a digit */
1220         }
1221       if (i >= base)
1222         return ERROR;           /* Invalid digit in this base */
1223
1224       /* Portably test for overflow (only works for nonzero values, so make
1225          a second check for zero).  FIXME: Can't we just make n and prevn
1226          unsigned and avoid this?  */
1227       if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1228         unsigned_p = 1;         /* Try something unsigned */
1229
1230       /* Portably test for unsigned overflow.
1231          FIXME: This check is wrong; for example it doesn't find overflow
1232          on 0x123456789 when LONGEST is 32 bits.  */
1233       if (c != 'l' && c != 'u' && n != 0)
1234         {       
1235           if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1236             error ("Numeric constant too large.");
1237         }
1238       prevn = n;
1239     }
1240
1241   /* An integer constant is an int, a long, or a long long.  An L
1242      suffix forces it to be long; an LL suffix forces it to be long
1243      long.  If not forced to a larger size, it gets the first type of
1244      the above that it fits in.  To figure out whether it fits, we
1245      shift it right and see whether anything remains.  Note that we
1246      can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1247      operation, because many compilers will warn about such a shift
1248      (which always produces a zero result).  Sometimes gdbarch_int_bit
1249      or gdbarch_long_bit will be that big, sometimes not.  To deal with
1250      the case where it is we just always shift the value more than
1251      once, with fewer bits each time.  */
1252
1253   un = (ULONGEST)n >> 2;
1254   if (long_p == 0
1255       && (un >> (gdbarch_int_bit (current_gdbarch) - 2)) == 0)
1256     {
1257       high_bit = ((ULONGEST)1) << (gdbarch_int_bit (current_gdbarch) - 1);
1258
1259       /* A large decimal (not hex or octal) constant (between INT_MAX
1260          and UINT_MAX) is a long or unsigned long, according to ANSI,
1261          never an unsigned int, but this code treats it as unsigned
1262          int.  This probably should be fixed.  GCC gives a warning on
1263          such constants.  */
1264
1265       unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_int;
1266       signed_type = builtin_type (current_gdbarch)->builtin_int;
1267     }
1268   else if (long_p <= 1
1269            && (un >> (gdbarch_long_bit (current_gdbarch) - 2)) == 0)
1270     {
1271       high_bit = ((ULONGEST)1) << (gdbarch_long_bit (current_gdbarch) - 1);
1272       unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_long;
1273       signed_type = builtin_type (current_gdbarch)->builtin_long;
1274     }
1275   else
1276     {
1277       int shift;
1278       if (sizeof (ULONGEST) * HOST_CHAR_BIT 
1279           < gdbarch_long_long_bit (current_gdbarch))
1280         /* A long long does not fit in a LONGEST.  */
1281         shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1282       else
1283         shift = (gdbarch_long_long_bit (current_gdbarch) - 1);
1284       high_bit = (ULONGEST) 1 << shift;
1285       unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_long_long;
1286       signed_type = builtin_type (current_gdbarch)->builtin_long_long;
1287     }
1288
1289    putithere->typed_val_int.val = n;
1290
1291    /* If the high bit of the worked out type is set then this number
1292       has to be unsigned. */
1293
1294    if (unsigned_p || (n & high_bit)) 
1295      {
1296        putithere->typed_val_int.type = unsigned_type;
1297      }
1298    else 
1299      {
1300        putithere->typed_val_int.type = signed_type;
1301      }
1302
1303    return INT;
1304 }
1305
1306 struct token
1307 {
1308   char *operator;
1309   int token;
1310   enum exp_opcode opcode;
1311 };
1312
1313 static const struct token tokentab3[] =
1314   {
1315     {">>=", ASSIGN_MODIFY, BINOP_RSH},
1316     {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1317   };
1318
1319 static const struct token tokentab2[] =
1320   {
1321     {"+=", ASSIGN_MODIFY, BINOP_ADD},
1322     {"-=", ASSIGN_MODIFY, BINOP_SUB},
1323     {"*=", ASSIGN_MODIFY, BINOP_MUL},
1324     {"/=", ASSIGN_MODIFY, BINOP_DIV},
1325     {"%=", ASSIGN_MODIFY, BINOP_REM},
1326     {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1327     {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1328     {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1329     {"++", INCREMENT, BINOP_END},
1330     {"--", DECREMENT, BINOP_END},
1331     {"->", ARROW, BINOP_END},
1332     {"&&", ANDAND, BINOP_END},
1333     {"||", OROR, BINOP_END},
1334     {"::", COLONCOLON, BINOP_END},
1335     {"<<", LSH, BINOP_END},
1336     {">>", RSH, BINOP_END},
1337     {"==", EQUAL, BINOP_END},
1338     {"!=", NOTEQUAL, BINOP_END},
1339     {"<=", LEQ, BINOP_END},
1340     {">=", GEQ, BINOP_END}
1341   };
1342
1343 /* Read one token, getting characters through lexptr.  */
1344
1345 static int
1346 yylex ()
1347 {
1348   int c;
1349   int namelen;
1350   unsigned int i;
1351   char *tokstart;
1352   char *tokptr;
1353   int tempbufindex;
1354   static char *tempbuf;
1355   static int tempbufsize;
1356   char * token_string = NULL;
1357   int class_prefix = 0;
1358    
1359  retry:
1360
1361   /* Check if this is a macro invocation that we need to expand.  */
1362   if (! scanning_macro_expansion ())
1363     {
1364       char *expanded = macro_expand_next (&lexptr,
1365                                           expression_macro_lookup_func,
1366                                           expression_macro_lookup_baton);
1367
1368       if (expanded)
1369         scan_macro_expansion (expanded);
1370     }
1371
1372   prev_lexptr = lexptr;
1373
1374   tokstart = lexptr;
1375   /* See if it is a special token of length 3.  */
1376   for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1377     if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
1378       {
1379         lexptr += 3;
1380         yylval.opcode = tokentab3[i].opcode;
1381         return tokentab3[i].token;
1382       }
1383
1384   /* See if it is a special token of length 2.  */
1385   for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1386     if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
1387       {
1388         lexptr += 2;
1389         yylval.opcode = tokentab2[i].opcode;
1390         return tokentab2[i].token;
1391       }
1392
1393   switch (c = *tokstart)
1394     {
1395     case 0:
1396       /* If we were just scanning the result of a macro expansion,
1397          then we need to resume scanning the original text.
1398          Otherwise, we were already scanning the original text, and
1399          we're really done.  */
1400       if (scanning_macro_expansion ())
1401         {
1402           finished_macro_expansion ();
1403           goto retry;
1404         }
1405       else
1406         return 0;
1407
1408     case ' ':
1409     case '\t':
1410     case '\n':
1411       lexptr++;
1412       goto retry;
1413
1414     case '\'':
1415       /* We either have a character constant ('0' or '\177' for example)
1416          or we have a quoted symbol reference ('foo(int,int)' in C++
1417          for example). */
1418       lexptr++;
1419       c = *lexptr++;
1420       if (c == '\\')
1421         c = parse_escape (&lexptr);
1422       else if (c == '\'')
1423         error ("Empty character constant.");
1424       else if (! host_char_to_target (c, &c))
1425         {
1426           int toklen = lexptr - tokstart + 1;
1427           char *tok = alloca (toklen + 1);
1428           memcpy (tok, tokstart, toklen);
1429           tok[toklen] = '\0';
1430           error ("There is no character corresponding to %s in the target "
1431                  "character set `%s'.", tok, target_charset ());
1432         }
1433
1434       yylval.typed_val_int.val = c;
1435       yylval.typed_val_int.type = builtin_type (current_gdbarch)->builtin_char;
1436
1437       c = *lexptr++;
1438       if (c != '\'')
1439         {
1440           namelen = skip_quoted (tokstart) - tokstart;
1441           if (namelen > 2)
1442             {
1443               lexptr = tokstart + namelen;
1444               if (lexptr[-1] != '\'')
1445                 error ("Unmatched single quote.");
1446               namelen -= 2;
1447               tokstart++;
1448               goto tryname;
1449             }
1450           error ("Invalid character constant.");
1451         }
1452       return INT;
1453
1454     case '(':
1455       paren_depth++;
1456       lexptr++;
1457       return c;
1458
1459     case ')':
1460       if (paren_depth == 0)
1461         return 0;
1462       paren_depth--;
1463       lexptr++;
1464       return c;
1465
1466     case ',':
1467       if (comma_terminates
1468           && paren_depth == 0
1469           && ! scanning_macro_expansion ())
1470         return 0;
1471       lexptr++;
1472       return c;
1473
1474     case '.':
1475       /* Might be a floating point number.  */
1476       if (lexptr[1] < '0' || lexptr[1] > '9')
1477         goto symbol;            /* Nope, must be a symbol. */
1478       /* FALL THRU into number case.  */
1479
1480     case '0':
1481     case '1':
1482     case '2':
1483     case '3':
1484     case '4':
1485     case '5':
1486     case '6':
1487     case '7':
1488     case '8':
1489     case '9':
1490       {
1491         /* It's a number.  */
1492         int got_dot = 0, got_e = 0, toktype;
1493         char *p = tokstart;
1494         int hex = input_radix > 10;
1495
1496         if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1497           {
1498             p += 2;
1499             hex = 1;
1500           }
1501         else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1502           {
1503             p += 2;
1504             hex = 0;
1505           }
1506
1507         for (;; ++p)
1508           {
1509             /* This test includes !hex because 'e' is a valid hex digit
1510                and thus does not indicate a floating point number when
1511                the radix is hex.  */
1512             if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1513               got_dot = got_e = 1;
1514             /* This test does not include !hex, because a '.' always indicates
1515                a decimal floating point number regardless of the radix.  */
1516             else if (!got_dot && *p == '.')
1517               got_dot = 1;
1518             else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1519                      && (*p == '-' || *p == '+'))
1520               /* This is the sign of the exponent, not the end of the
1521                  number.  */
1522               continue;
1523             /* We will take any letters or digits.  parse_number will
1524                complain if past the radix, or if L or U are not final.  */
1525             else if ((*p < '0' || *p > '9')
1526                      && ((*p < 'a' || *p > 'z')
1527                                   && (*p < 'A' || *p > 'Z')))
1528               break;
1529           }
1530         toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1531         if (toktype == ERROR)
1532           {
1533             char *err_copy = (char *) alloca (p - tokstart + 1);
1534
1535             memcpy (err_copy, tokstart, p - tokstart);
1536             err_copy[p - tokstart] = 0;
1537             error ("Invalid number \"%s\".", err_copy);
1538           }
1539         lexptr = p;
1540         return toktype;
1541       }
1542
1543     case '+':
1544     case '-':
1545     case '*':
1546     case '/':
1547     case '%':
1548     case '|':
1549     case '&':
1550     case '^':
1551     case '~':
1552     case '!':
1553     case '@':
1554     case '<':
1555     case '>':
1556     case '[':
1557     case ']':
1558     case '?':
1559     case ':':
1560     case '=':
1561     case '{':
1562     case '}':
1563     symbol:
1564       lexptr++;
1565       return c;
1566
1567     case '"':
1568
1569       /* Build the gdb internal form of the input string in tempbuf,
1570          translating any standard C escape forms seen.  Note that the
1571          buffer is null byte terminated *only* for the convenience of
1572          debugging gdb itself and printing the buffer contents when
1573          the buffer contains no embedded nulls.  Gdb does not depend
1574          upon the buffer being null byte terminated, it uses the length
1575          string instead.  This allows gdb to handle C strings (as well
1576          as strings in other languages) with embedded null bytes */
1577
1578       tokptr = ++tokstart;
1579       tempbufindex = 0;
1580
1581       do {
1582         char *char_start_pos = tokptr;
1583
1584         /* Grow the static temp buffer if necessary, including allocating
1585            the first one on demand. */
1586         if (tempbufindex + 1 >= tempbufsize)
1587           {
1588             tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1589           }
1590         switch (*tokptr)
1591           {
1592           case '\0':
1593           case '"':
1594             /* Do nothing, loop will terminate. */
1595             break;
1596           case '\\':
1597             tokptr++;
1598             c = parse_escape (&tokptr);
1599             if (c == -1)
1600               {
1601                 continue;
1602               }
1603             tempbuf[tempbufindex++] = c;
1604             break;
1605           default:
1606             c = *tokptr++;
1607             if (! host_char_to_target (c, &c))
1608               {
1609                 int len = tokptr - char_start_pos;
1610                 char *copy = alloca (len + 1);
1611                 memcpy (copy, char_start_pos, len);
1612                 copy[len] = '\0';
1613
1614                 error ("There is no character corresponding to `%s' "
1615                        "in the target character set `%s'.",
1616                        copy, target_charset ());
1617               }
1618             tempbuf[tempbufindex++] = c;
1619             break;
1620           }
1621       } while ((*tokptr != '"') && (*tokptr != '\0'));
1622       if (*tokptr++ != '"')
1623         {
1624           error ("Unterminated string in expression.");
1625         }
1626       tempbuf[tempbufindex] = '\0';     /* See note above */
1627       yylval.sval.ptr = tempbuf;
1628       yylval.sval.length = tempbufindex;
1629       lexptr = tokptr;
1630       return (STRING);
1631     }
1632
1633   if (!(c == '_' || c == '$'
1634         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1635     /* We must have come across a bad character (e.g. ';').  */
1636     error ("Invalid character '%c' in expression.", c);
1637
1638   /* It's a name.  See how long it is.  */
1639   namelen = 0;
1640   for (c = tokstart[namelen];
1641        (c == '_' || c == '$' || (c >= '0' && c <= '9')
1642         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1643     {
1644       /* Template parameter lists are part of the name.
1645          FIXME: This mishandles `print $a<4&&$a>3'.  */
1646
1647       if (c == '<')
1648         { 
1649                /* Scan ahead to get rest of the template specification.  Note
1650                   that we look ahead only when the '<' adjoins non-whitespace
1651                   characters; for comparison expressions, e.g. "a < b > c",
1652                   there must be spaces before the '<', etc. */
1653                
1654                char * p = find_template_name_end (tokstart + namelen);
1655                if (p)
1656                  namelen = p - tokstart;
1657                break;
1658         }
1659       c = tokstart[++namelen];
1660     }
1661
1662   /* The token "if" terminates the expression and is NOT removed from
1663      the input stream.  It doesn't count if it appears in the
1664      expansion of a macro.  */
1665   if (namelen == 2
1666       && tokstart[0] == 'i'
1667       && tokstart[1] == 'f'
1668       && ! scanning_macro_expansion ())
1669     {
1670       return 0;
1671     }
1672
1673   lexptr += namelen;
1674
1675   tryname:
1676
1677   /* Catch specific keywords.  Should be done with a data structure.  */
1678   switch (namelen)
1679     {
1680     case 8:
1681       if (strncmp (tokstart, "unsigned", 8) == 0)
1682         return UNSIGNED;
1683       if (current_language->la_language == language_cplus
1684           && strncmp (tokstart, "template", 8) == 0)
1685         return TEMPLATE;
1686       if (strncmp (tokstart, "volatile", 8) == 0)
1687         return VOLATILE_KEYWORD;
1688       break;
1689     case 6:
1690       if (strncmp (tokstart, "struct", 6) == 0)
1691         return STRUCT;
1692       if (strncmp (tokstart, "signed", 6) == 0)
1693         return SIGNED_KEYWORD;
1694       if (strncmp (tokstart, "sizeof", 6) == 0)
1695         return SIZEOF;
1696       if (strncmp (tokstart, "double", 6) == 0)
1697         return DOUBLE_KEYWORD;
1698       break;
1699     case 5:
1700       if (current_language->la_language == language_cplus)
1701         {
1702           if (strncmp (tokstart, "false", 5) == 0)
1703             return FALSEKEYWORD;
1704           if (strncmp (tokstart, "class", 5) == 0)
1705             return CLASS;
1706         }
1707       if (strncmp (tokstart, "union", 5) == 0)
1708         return UNION;
1709       if (strncmp (tokstart, "short", 5) == 0)
1710         return SHORT;
1711       if (strncmp (tokstart, "const", 5) == 0)
1712         return CONST_KEYWORD;
1713       break;
1714     case 4:
1715       if (strncmp (tokstart, "enum", 4) == 0)
1716         return ENUM;
1717       if (strncmp (tokstart, "long", 4) == 0)
1718         return LONG;
1719       if (current_language->la_language == language_cplus)
1720           {
1721             if (strncmp (tokstart, "true", 4) == 0)
1722               return TRUEKEYWORD;
1723           }
1724       break;
1725     case 3:
1726       if (strncmp (tokstart, "int", 3) == 0)
1727         return INT_KEYWORD;
1728       break;
1729     default:
1730       break;
1731     }
1732
1733   yylval.sval.ptr = tokstart;
1734   yylval.sval.length = namelen;
1735
1736   if (*tokstart == '$')
1737     {
1738       write_dollar_variable (yylval.sval);
1739       return VARIABLE;
1740     }
1741   
1742   /* Use token-type BLOCKNAME for symbols that happen to be defined as
1743      functions or symtabs.  If this is not so, then ...
1744      Use token-type TYPENAME for symbols that happen to be defined
1745      currently as names of types; NAME for other symbols.
1746      The caller is not constrained to care about the distinction.  */
1747   {
1748     char *tmp = copy_name (yylval.sval);
1749     struct symbol *sym;
1750     int is_a_field_of_this = 0;
1751     int hextype;
1752
1753     sym = lookup_symbol (tmp, expression_context_block,
1754                          VAR_DOMAIN,
1755                          current_language->la_language == language_cplus
1756                          ? &is_a_field_of_this : (int *) NULL,
1757                          (struct symtab **) NULL);
1758     /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1759        no psymtabs (coff, xcoff, or some future change to blow away the
1760        psymtabs once once symbols are read).  */
1761     if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1762       {
1763         yylval.ssym.sym = sym;
1764         yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1765         return BLOCKNAME;
1766       }
1767     else if (!sym)
1768       {                         /* See if it's a file name. */
1769         struct symtab *symtab;
1770
1771         symtab = lookup_symtab (tmp);
1772
1773         if (symtab)
1774           {
1775             yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1776             return FILENAME;
1777           }
1778       }
1779
1780     if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1781         {
1782           /* NOTE: carlton/2003-09-25: There used to be code here to
1783              handle nested types.  It didn't work very well.  See the
1784              comment before qualified_type for more info.  */
1785           yylval.tsym.type = SYMBOL_TYPE (sym);
1786           return TYPENAME;
1787         }
1788     yylval.tsym.type
1789       = language_lookup_primitive_type_by_name (current_language,
1790                                                 current_gdbarch, tmp);
1791     if (yylval.tsym.type != NULL)
1792       return TYPENAME;
1793
1794     /* Input names that aren't symbols but ARE valid hex numbers,
1795        when the input radix permits them, can be names or numbers
1796        depending on the parse.  Note we support radixes > 16 here.  */
1797     if (!sym && 
1798         ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1799          (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1800       {
1801         YYSTYPE newlval;        /* Its value is ignored.  */
1802         hextype = parse_number (tokstart, namelen, 0, &newlval);
1803         if (hextype == INT)
1804           {
1805             yylval.ssym.sym = sym;
1806             yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1807             return NAME_OR_INT;
1808           }
1809       }
1810
1811     /* Any other kind of symbol */
1812     yylval.ssym.sym = sym;
1813     yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1814     return NAME;
1815   }
1816 }
1817
1818 void
1819 yyerror (msg)
1820      char *msg;
1821 {
1822   if (prev_lexptr)
1823     lexptr = prev_lexptr;
1824
1825   error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
1826 }