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