* Makefile.in: Update license to GPLv3.
[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, 2009
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 /* Parse a C expression from text in a string,
22    and return the result as a  struct expression  pointer.
23    That structure contains arithmetic operations in reverse polish,
24    with constants represented by operations that are followed by special data.
25    See expression.h for the details of the format.
26    What is important here is that it can be built up sequentially
27    during the process of parsing; the lower levels of the tree always
28    come first in the result.
29
30    Note that malloc's and realloc's in this file are transformed to
31    xmalloc and xrealloc respectively by the same sed command in the
32    makefile that remaps any other malloc/realloc inserted by the parser
33    generator.  Doing this with #defines and trying to control the interaction
34    with include files (<malloc.h> and <stdlib.h> for example) just became
35    too messy, particularly when such includes can be inserted at random
36    times by the parser generator.  */
37    
38 %{
39
40 #include "defs.h"
41 #include "gdb_string.h"
42 #include <ctype.h>
43 #include "expression.h"
44 #include "value.h"
45 #include "parser-defs.h"
46 #include "language.h"
47 #include "c-lang.h"
48 #include "bfd.h" /* Required by objfiles.h.  */
49 #include "symfile.h" /* Required by objfiles.h.  */
50 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
51 #include "charset.h"
52 #include "block.h"
53 #include "cp-support.h"
54 #include "dfp.h"
55 #include "gdb_assert.h"
56 #include "macroscope.h"
57
58 #define parse_type builtin_type (parse_gdbarch)
59
60 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
61    as well as gratuitiously global symbol names, so we can have multiple
62    yacc generated parsers in gdb.  Note that these are only the variables
63    produced by yacc.  If other parser generators (bison, byacc, etc) produce
64    additional global names that conflict at link time, then those parser
65    generators need to be fixed instead of adding those names to this list. */
66
67 #define yymaxdepth c_maxdepth
68 #define yyparse c_parse_internal
69 #define yylex   c_lex
70 #define yyerror c_error
71 #define yylval  c_lval
72 #define yychar  c_char
73 #define yydebug c_debug
74 #define yypact  c_pact  
75 #define yyr1    c_r1                    
76 #define yyr2    c_r2                    
77 #define yydef   c_def           
78 #define yychk   c_chk           
79 #define yypgo   c_pgo           
80 #define yyact   c_act           
81 #define yyexca  c_exca
82 #define yyerrflag c_errflag
83 #define yynerrs c_nerrs
84 #define yyps    c_ps
85 #define yypv    c_pv
86 #define yys     c_s
87 #define yy_yys  c_yys
88 #define yystate c_state
89 #define yytmp   c_tmp
90 #define yyv     c_v
91 #define yy_yyv  c_yyv
92 #define yyval   c_val
93 #define yylloc  c_lloc
94 #define yyreds  c_reds          /* With YYDEBUG defined */
95 #define yytoks  c_toks          /* With YYDEBUG defined */
96 #define yyname  c_name          /* With YYDEBUG defined */
97 #define yyrule  c_rule          /* With YYDEBUG defined */
98 #define yylhs   c_yylhs
99 #define yylen   c_yylen
100 #define yydefred c_yydefred
101 #define yydgoto c_yydgoto
102 #define yysindex c_yysindex
103 #define yyrindex c_yyrindex
104 #define yygindex c_yygindex
105 #define yytable  c_yytable
106 #define yycheck  c_yycheck
107
108 #ifndef YYDEBUG
109 #define YYDEBUG 1               /* Default to yydebug support */
110 #endif
111
112 #define YYFPRINTF parser_fprintf
113
114 int yyparse (void);
115
116 static int yylex (void);
117
118 void yyerror (char *);
119
120 %}
121
122 /* Although the yacc "value" of an expression is not used,
123    since the result is stored in the structure being created,
124    other node types do have values.  */
125
126 %union
127   {
128     LONGEST lval;
129     struct {
130       LONGEST val;
131       struct type *type;
132     } typed_val_int;
133     struct {
134       DOUBLEST dval;
135       struct type *type;
136     } typed_val_float;
137     struct {
138       gdb_byte val[16];
139       struct type *type;
140     } typed_val_decfloat;
141     struct symbol *sym;
142     struct type *tval;
143     struct stoken sval;
144     struct 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 string_exp
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 string_exp:
566                 STRING
567                         {
568                           /* We copy the string here, and not in the
569                              lexer, to guarantee that we do not leak a
570                              string.  Note that we follow the
571                              NUL-termination convention of the
572                              lexer.  */
573                           $$.length = $1.length;
574                           $$.ptr = malloc ($1.length + 1);
575                           memcpy ($$.ptr, $1.ptr, $1.length + 1);
576                         }
577
578         |       string_exp STRING
579                         {
580                           /* Note that we NUL-terminate here, but just
581                              for convenience.  */
582                           struct stoken t;
583                           t.length = $1.length + $2.length;
584                           t.ptr = malloc (t.length + 1);
585                           memcpy (t.ptr, $1.ptr, $1.length);
586                           memcpy (t.ptr + $1.length, $2.ptr, $2.length + 1);
587                           free ($1.ptr);
588                           $$ = t;
589                         }
590                 ;
591
592 exp     :       string_exp
593                         { /* C strings are converted into array constants with
594                              an explicit null byte added at the end.  Thus
595                              the array upper bound is the string length.
596                              There is no such thing in C as a completely empty
597                              string. */
598                           char *sp = $1.ptr; int count = $1.length;
599                           while (count-- > 0)
600                             {
601                               write_exp_elt_opcode (OP_LONG);
602                               write_exp_elt_type (parse_type->builtin_char);
603                               write_exp_elt_longcst ((LONGEST)(*sp++));
604                               write_exp_elt_opcode (OP_LONG);
605                             }
606                           write_exp_elt_opcode (OP_LONG);
607                           write_exp_elt_type (parse_type->builtin_char);
608                           write_exp_elt_longcst ((LONGEST)'\0');
609                           write_exp_elt_opcode (OP_LONG);
610                           write_exp_elt_opcode (OP_ARRAY);
611                           write_exp_elt_longcst ((LONGEST) 0);
612                           write_exp_elt_longcst ((LONGEST) ($1.length));
613                           write_exp_elt_opcode (OP_ARRAY);
614                           free ($1.ptr);
615                         }
616         ;
617
618 /* C++.  */
619 exp     :       TRUEKEYWORD    
620                         { write_exp_elt_opcode (OP_LONG);
621                           write_exp_elt_type (parse_type->builtin_bool);
622                           write_exp_elt_longcst ((LONGEST) 1);
623                           write_exp_elt_opcode (OP_LONG); }
624         ;
625
626 exp     :       FALSEKEYWORD   
627                         { write_exp_elt_opcode (OP_LONG);
628                           write_exp_elt_type (parse_type->builtin_bool);
629                           write_exp_elt_longcst ((LONGEST) 0);
630                           write_exp_elt_opcode (OP_LONG); }
631         ;
632
633 /* end of C++.  */
634
635 block   :       BLOCKNAME
636                         {
637                           if ($1.sym)
638                             $$ = SYMBOL_BLOCK_VALUE ($1.sym);
639                           else
640                             error ("No file or function \"%s\".",
641                                    copy_name ($1.stoken));
642                         }
643         |       FILENAME
644                         {
645                           $$ = $1;
646                         }
647         ;
648
649 block   :       block COLONCOLON name
650                         { struct symbol *tem
651                             = lookup_symbol (copy_name ($3), $1,
652                                              VAR_DOMAIN, (int *) NULL);
653                           if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
654                             error ("No function \"%s\" in specified context.",
655                                    copy_name ($3));
656                           $$ = SYMBOL_BLOCK_VALUE (tem); }
657         ;
658
659 variable:       block COLONCOLON name
660                         { struct symbol *sym;
661                           sym = lookup_symbol (copy_name ($3), $1,
662                                                VAR_DOMAIN, (int *) NULL);
663                           if (sym == 0)
664                             error ("No symbol \"%s\" in specified context.",
665                                    copy_name ($3));
666
667                           write_exp_elt_opcode (OP_VAR_VALUE);
668                           /* block_found is set by lookup_symbol.  */
669                           write_exp_elt_block (block_found);
670                           write_exp_elt_sym (sym);
671                           write_exp_elt_opcode (OP_VAR_VALUE); }
672         ;
673
674 qualified_name: typebase COLONCOLON name
675                         {
676                           struct type *type = $1;
677                           if (TYPE_CODE (type) != TYPE_CODE_STRUCT
678                               && TYPE_CODE (type) != TYPE_CODE_UNION
679                               && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
680                             error ("`%s' is not defined as an aggregate type.",
681                                    TYPE_NAME (type));
682
683                           write_exp_elt_opcode (OP_SCOPE);
684                           write_exp_elt_type (type);
685                           write_exp_string ($3);
686                           write_exp_elt_opcode (OP_SCOPE);
687                         }
688         |       typebase COLONCOLON '~' name
689                         {
690                           struct type *type = $1;
691                           struct stoken tmp_token;
692                           if (TYPE_CODE (type) != TYPE_CODE_STRUCT
693                               && TYPE_CODE (type) != TYPE_CODE_UNION
694                               && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
695                             error ("`%s' is not defined as an aggregate type.",
696                                    TYPE_NAME (type));
697
698                           tmp_token.ptr = (char*) alloca ($4.length + 2);
699                           tmp_token.length = $4.length + 1;
700                           tmp_token.ptr[0] = '~';
701                           memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
702                           tmp_token.ptr[tmp_token.length] = 0;
703
704                           /* Check for valid destructor name.  */
705                           destructor_name_p (tmp_token.ptr, type);
706                           write_exp_elt_opcode (OP_SCOPE);
707                           write_exp_elt_type (type);
708                           write_exp_string (tmp_token);
709                           write_exp_elt_opcode (OP_SCOPE);
710                         }
711         ;
712
713 variable:       qualified_name
714         |       COLONCOLON name
715                         {
716                           char *name = copy_name ($2);
717                           struct symbol *sym;
718                           struct minimal_symbol *msymbol;
719
720                           sym =
721                             lookup_symbol (name, (const struct block *) NULL,
722                                            VAR_DOMAIN, (int *) NULL);
723                           if (sym)
724                             {
725                               write_exp_elt_opcode (OP_VAR_VALUE);
726                               write_exp_elt_block (NULL);
727                               write_exp_elt_sym (sym);
728                               write_exp_elt_opcode (OP_VAR_VALUE);
729                               break;
730                             }
731
732                           msymbol = lookup_minimal_symbol (name, NULL, NULL);
733                           if (msymbol != NULL)
734                             write_exp_msymbol (msymbol);
735                           else if (!have_full_symbols () && !have_partial_symbols ())
736                             error ("No symbol table is loaded.  Use the \"file\" command.");
737                           else
738                             error ("No symbol \"%s\" in current context.", name);
739                         }
740         ;
741
742 variable:       name_not_typename
743                         { struct symbol *sym = $1.sym;
744
745                           if (sym)
746                             {
747                               if (symbol_read_needs_frame (sym))
748                                 {
749                                   if (innermost_block == 0 ||
750                                       contained_in (block_found, 
751                                                     innermost_block))
752                                     innermost_block = block_found;
753                                 }
754
755                               write_exp_elt_opcode (OP_VAR_VALUE);
756                               /* We want to use the selected frame, not
757                                  another more inner frame which happens to
758                                  be in the same block.  */
759                               write_exp_elt_block (NULL);
760                               write_exp_elt_sym (sym);
761                               write_exp_elt_opcode (OP_VAR_VALUE);
762                             }
763                           else if ($1.is_a_field_of_this)
764                             {
765                               /* C++: it hangs off of `this'.  Must
766                                  not inadvertently convert from a method call
767                                  to data ref.  */
768                               if (innermost_block == 0 || 
769                                   contained_in (block_found, innermost_block))
770                                 innermost_block = block_found;
771                               write_exp_elt_opcode (OP_THIS);
772                               write_exp_elt_opcode (OP_THIS);
773                               write_exp_elt_opcode (STRUCTOP_PTR);
774                               write_exp_string ($1.stoken);
775                               write_exp_elt_opcode (STRUCTOP_PTR);
776                             }
777                           else
778                             {
779                               struct minimal_symbol *msymbol;
780                               char *arg = copy_name ($1.stoken);
781
782                               msymbol =
783                                 lookup_minimal_symbol (arg, NULL, NULL);
784                               if (msymbol != NULL)
785                                 write_exp_msymbol (msymbol);
786                               else if (!have_full_symbols () && !have_partial_symbols ())
787                                 error ("No symbol table is loaded.  Use the \"file\" command.");
788                               else
789                                 error ("No symbol \"%s\" in current context.",
790                                        copy_name ($1.stoken));
791                             }
792                         }
793         ;
794
795 space_identifier : '@' NAME
796                 { push_type_address_space (copy_name ($2.stoken));
797                   push_type (tp_space_identifier);
798                 }
799         ;
800
801 const_or_volatile: const_or_volatile_noopt
802         |
803         ;
804
805 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
806         ;
807
808 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
809         | const_or_volatile_noopt 
810         ;
811
812 const_or_volatile_or_space_identifier: 
813                 const_or_volatile_or_space_identifier_noopt
814         |
815         ;
816
817 abs_decl:       '*'
818                         { push_type (tp_pointer); $$ = 0; }
819         |       '*' abs_decl
820                         { push_type (tp_pointer); $$ = $2; }
821         |       '&'
822                         { push_type (tp_reference); $$ = 0; }
823         |       '&' abs_decl
824                         { push_type (tp_reference); $$ = $2; }
825         |       direct_abs_decl
826         ;
827
828 direct_abs_decl: '(' abs_decl ')'
829                         { $$ = $2; }
830         |       direct_abs_decl array_mod
831                         {
832                           push_type_int ($2);
833                           push_type (tp_array);
834                         }
835         |       array_mod
836                         {
837                           push_type_int ($1);
838                           push_type (tp_array);
839                           $$ = 0;
840                         }
841
842         |       direct_abs_decl func_mod
843                         { push_type (tp_function); }
844         |       func_mod
845                         { push_type (tp_function); }
846         ;
847
848 array_mod:      '[' ']'
849                         { $$ = -1; }
850         |       '[' INT ']'
851                         { $$ = $2.val; }
852         ;
853
854 func_mod:       '(' ')'
855                         { $$ = 0; }
856         |       '(' nonempty_typelist ')'
857                         { free ($2); $$ = 0; }
858         ;
859
860 /* We used to try to recognize pointer to member types here, but
861    that didn't work (shift/reduce conflicts meant that these rules never
862    got executed).  The problem is that
863      int (foo::bar::baz::bizzle)
864    is a function type but
865      int (foo::bar::baz::bizzle::*)
866    is a pointer to member type.  Stroustrup loses again!  */
867
868 type    :       ptype
869         ;
870
871 typebase  /* Implements (approximately): (type-qualifier)* type-specifier */
872         :       TYPENAME
873                         { $$ = $1.type; }
874         |       INT_KEYWORD
875                         { $$ = parse_type->builtin_int; }
876         |       LONG
877                         { $$ = parse_type->builtin_long; }
878         |       SHORT
879                         { $$ = parse_type->builtin_short; }
880         |       LONG INT_KEYWORD
881                         { $$ = parse_type->builtin_long; }
882         |       LONG SIGNED_KEYWORD INT_KEYWORD
883                         { $$ = parse_type->builtin_long; }
884         |       LONG SIGNED_KEYWORD
885                         { $$ = parse_type->builtin_long; }
886         |       SIGNED_KEYWORD LONG INT_KEYWORD
887                         { $$ = parse_type->builtin_long; }
888         |       UNSIGNED LONG INT_KEYWORD
889                         { $$ = parse_type->builtin_unsigned_long; }
890         |       LONG UNSIGNED INT_KEYWORD
891                         { $$ = parse_type->builtin_unsigned_long; }
892         |       LONG UNSIGNED
893                         { $$ = parse_type->builtin_unsigned_long; }
894         |       LONG LONG
895                         { $$ = parse_type->builtin_long_long; }
896         |       LONG LONG INT_KEYWORD
897                         { $$ = parse_type->builtin_long_long; }
898         |       LONG LONG SIGNED_KEYWORD INT_KEYWORD
899                         { $$ = parse_type->builtin_long_long; }
900         |       LONG LONG SIGNED_KEYWORD
901                         { $$ = parse_type->builtin_long_long; }
902         |       SIGNED_KEYWORD LONG LONG
903                         { $$ = parse_type->builtin_long_long; }
904         |       SIGNED_KEYWORD LONG LONG INT_KEYWORD
905                         { $$ = parse_type->builtin_long_long; }
906         |       UNSIGNED LONG LONG
907                         { $$ = parse_type->builtin_unsigned_long_long; }
908         |       UNSIGNED LONG LONG INT_KEYWORD
909                         { $$ = parse_type->builtin_unsigned_long_long; }
910         |       LONG LONG UNSIGNED
911                         { $$ = parse_type->builtin_unsigned_long_long; }
912         |       LONG LONG UNSIGNED INT_KEYWORD
913                         { $$ = parse_type->builtin_unsigned_long_long; }
914         |       SHORT INT_KEYWORD
915                         { $$ = parse_type->builtin_short; }
916         |       SHORT SIGNED_KEYWORD INT_KEYWORD
917                         { $$ = parse_type->builtin_short; }
918         |       SHORT SIGNED_KEYWORD
919                         { $$ = parse_type->builtin_short; }
920         |       UNSIGNED SHORT INT_KEYWORD
921                         { $$ = parse_type->builtin_unsigned_short; }
922         |       SHORT UNSIGNED 
923                         { $$ = parse_type->builtin_unsigned_short; }
924         |       SHORT UNSIGNED INT_KEYWORD
925                         { $$ = parse_type->builtin_unsigned_short; }
926         |       DOUBLE_KEYWORD
927                         { $$ = parse_type->builtin_double; }
928         |       LONG DOUBLE_KEYWORD
929                         { $$ = parse_type->builtin_long_double; }
930         |       STRUCT name
931                         { $$ = lookup_struct (copy_name ($2),
932                                               expression_context_block); }
933         |       CLASS name
934                         { $$ = lookup_struct (copy_name ($2),
935                                               expression_context_block); }
936         |       UNION name
937                         { $$ = lookup_union (copy_name ($2),
938                                              expression_context_block); }
939         |       ENUM name
940                         { $$ = lookup_enum (copy_name ($2),
941                                             expression_context_block); }
942         |       UNSIGNED typename
943                         { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
944         |       UNSIGNED
945                         { $$ = parse_type->builtin_unsigned_int; }
946         |       SIGNED_KEYWORD typename
947                         { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
948         |       SIGNED_KEYWORD
949                         { $$ = parse_type->builtin_int; }
950                 /* It appears that this rule for templates is never
951                    reduced; template recognition happens by lookahead
952                    in the token processing code in yylex. */         
953         |       TEMPLATE name '<' type '>'
954                         { $$ = lookup_template_type(copy_name($2), $4,
955                                                     expression_context_block);
956                         }
957         | const_or_volatile_or_space_identifier_noopt typebase 
958                         { $$ = follow_types ($2); }
959         | typebase const_or_volatile_or_space_identifier_noopt 
960                         { $$ = follow_types ($1); }
961         | qualified_type
962         ;
963
964 /* FIXME: carlton/2003-09-25: This next bit leads to lots of
965    reduce-reduce conflicts, because the parser doesn't know whether or
966    not to use qualified_name or qualified_type: the rules are
967    identical.  If the parser is parsing 'A::B::x', then, when it sees
968    the second '::', it knows that the expression to the left of it has
969    to be a type, so it uses qualified_type.  But if it is parsing just
970    'A::B', then it doesn't have any way of knowing which rule to use,
971    so there's a reduce-reduce conflict; it picks qualified_name, since
972    that occurs earlier in this file than qualified_type.
973
974    There's no good way to fix this with the grammar as it stands; as
975    far as I can tell, some of the problems arise from ambiguities that
976    GDB introduces ('start' can be either an expression or a type), but
977    some of it is inherent to the nature of C++ (you want to treat the
978    input "(FOO)" fairly differently depending on whether FOO is an
979    expression or a type, and if FOO is a complex expression, this can
980    be hard to determine at the right time).  Fortunately, it works
981    pretty well in most cases.  For example, if you do 'ptype A::B',
982    where A::B is a nested type, then the parser will mistakenly
983    misidentify it as an expression; but evaluate_subexp will get
984    called with 'noside' set to EVAL_AVOID_SIDE_EFFECTS, and everything
985    will work out anyways.  But there are situations where the parser
986    will get confused: the most common one that I've run into is when
987    you want to do
988
989      print *((A::B *) x)"
990
991    where the parser doesn't realize that A::B has to be a type until
992    it hits the first right paren, at which point it's too late.  (The
993    workaround is to type "print *(('A::B' *) x)" instead.)  (And
994    another solution is to fix our symbol-handling code so that the
995    user never wants to type something like that in the first place,
996    because we get all the types right without the user's help!)
997
998    Perhaps we could fix this by making the lexer smarter.  Some of
999    this functionality used to be in the lexer, but in a way that
1000    worked even less well than the current solution: that attempt
1001    involved having the parser sometimes handle '::' and having the
1002    lexer sometimes handle it, and without a clear division of
1003    responsibility, it quickly degenerated into a big mess.  Probably
1004    the eventual correct solution will give more of a role to the lexer
1005    (ideally via code that is shared between the lexer and
1006    decode_line_1), but I'm not holding my breath waiting for somebody
1007    to get around to cleaning this up...  */
1008
1009 qualified_type: typebase COLONCOLON name
1010                 {
1011                   struct type *type = $1;
1012                   struct type *new_type;
1013                   char *ncopy = alloca ($3.length + 1);
1014
1015                   memcpy (ncopy, $3.ptr, $3.length);
1016                   ncopy[$3.length] = '\0';
1017
1018                   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1019                       && TYPE_CODE (type) != TYPE_CODE_UNION
1020                       && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
1021                     error ("`%s' is not defined as an aggregate type.",
1022                            TYPE_NAME (type));
1023
1024                   new_type = cp_lookup_nested_type (type, ncopy,
1025                                                     expression_context_block);
1026                   if (new_type == NULL)
1027                     error ("No type \"%s\" within class or namespace \"%s\".",
1028                            ncopy, TYPE_NAME (type));
1029                   
1030                   $$ = new_type;
1031                 }
1032         ;
1033
1034 typename:       TYPENAME
1035         |       INT_KEYWORD
1036                 {
1037                   $$.stoken.ptr = "int";
1038                   $$.stoken.length = 3;
1039                   $$.type = parse_type->builtin_int;
1040                 }
1041         |       LONG
1042                 {
1043                   $$.stoken.ptr = "long";
1044                   $$.stoken.length = 4;
1045                   $$.type = parse_type->builtin_long;
1046                 }
1047         |       SHORT
1048                 {
1049                   $$.stoken.ptr = "short";
1050                   $$.stoken.length = 5;
1051                   $$.type = parse_type->builtin_short;
1052                 }
1053         ;
1054
1055 nonempty_typelist
1056         :       type
1057                 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1058                   $<ivec>$[0] = 1;      /* Number of types in vector */
1059                   $$[1] = $1;
1060                 }
1061         |       nonempty_typelist ',' type
1062                 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1063                   $$ = (struct type **) realloc ((char *) $1, len);
1064                   $$[$<ivec>$[0]] = $3;
1065                 }
1066         ;
1067
1068 ptype   :       typebase
1069         |       ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1070                 { $$ = follow_types ($1); }
1071         ;
1072
1073 const_and_volatile:     CONST_KEYWORD VOLATILE_KEYWORD
1074         |               VOLATILE_KEYWORD CONST_KEYWORD
1075         ;
1076
1077 const_or_volatile_noopt:        const_and_volatile 
1078                         { push_type (tp_const);
1079                           push_type (tp_volatile); 
1080                         }
1081         |               CONST_KEYWORD
1082                         { push_type (tp_const); }
1083         |               VOLATILE_KEYWORD
1084                         { push_type (tp_volatile); }
1085         ;
1086
1087 name    :       NAME { $$ = $1.stoken; }
1088         |       BLOCKNAME { $$ = $1.stoken; }
1089         |       TYPENAME { $$ = $1.stoken; }
1090         |       NAME_OR_INT  { $$ = $1.stoken; }
1091         ;
1092
1093 name_not_typename :     NAME
1094         |       BLOCKNAME
1095 /* These would be useful if name_not_typename was useful, but it is just
1096    a fake for "variable", so these cause reduce/reduce conflicts because
1097    the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1098    =exp) or just an exp.  If name_not_typename was ever used in an lvalue
1099    context where only a name could occur, this might be useful.
1100         |       NAME_OR_INT
1101  */
1102         ;
1103
1104 %%
1105
1106 /* Take care of parsing a number (anything that starts with a digit).
1107    Set yylval and return the token type; update lexptr.
1108    LEN is the number of characters in it.  */
1109
1110 /*** Needs some error checking for the float case ***/
1111
1112 static int
1113 parse_number (p, len, parsed_float, putithere)
1114      char *p;
1115      int len;
1116      int parsed_float;
1117      YYSTYPE *putithere;
1118 {
1119   /* FIXME: Shouldn't these be unsigned?  We don't deal with negative values
1120      here, and we do kind of silly things like cast to unsigned.  */
1121   LONGEST n = 0;
1122   LONGEST prevn = 0;
1123   ULONGEST un;
1124
1125   int i = 0;
1126   int c;
1127   int base = input_radix;
1128   int unsigned_p = 0;
1129
1130   /* Number of "L" suffixes encountered.  */
1131   int long_p = 0;
1132
1133   /* We have found a "L" or "U" suffix.  */
1134   int found_suffix = 0;
1135
1136   ULONGEST high_bit;
1137   struct type *signed_type;
1138   struct type *unsigned_type;
1139
1140   if (parsed_float)
1141     {
1142       /* It's a float since it contains a point or an exponent.  */
1143       char *s;
1144       int num;  /* number of tokens scanned by scanf */
1145       char saved_char;
1146
1147       /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1148          point.  Return DECFLOAT.  */
1149
1150       if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1151         {
1152           p[len - 2] = '\0';
1153           putithere->typed_val_decfloat.type
1154             = parse_type->builtin_decfloat;
1155           decimal_from_string (putithere->typed_val_decfloat.val, 4, p);
1156           p[len - 2] = 'd';
1157           return DECFLOAT;
1158         }
1159
1160       if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1161         {
1162           p[len - 2] = '\0';
1163           putithere->typed_val_decfloat.type
1164             = parse_type->builtin_decdouble;
1165           decimal_from_string (putithere->typed_val_decfloat.val, 8, p);
1166           p[len - 2] = 'd';
1167           return DECFLOAT;
1168         }
1169
1170       if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1171         {
1172           p[len - 2] = '\0';
1173           putithere->typed_val_decfloat.type
1174             = parse_type->builtin_declong;
1175           decimal_from_string (putithere->typed_val_decfloat.val, 16, p);
1176           p[len - 2] = 'd';
1177           return DECFLOAT;
1178         }
1179
1180       s = malloc (len);
1181       saved_char = p[len];
1182       p[len] = 0;       /* null-terminate the token */
1183       num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%s",
1184                     &putithere->typed_val_float.dval, s);
1185       p[len] = saved_char;      /* restore the input stream */
1186
1187       if (num == 1)
1188         putithere->typed_val_float.type = 
1189           parse_type->builtin_double;
1190
1191       if (num == 2 )
1192         {
1193           /* See if it has any float suffix: 'f' for float, 'l' for long 
1194              double.  */
1195           if (!strcasecmp (s, "f"))
1196             putithere->typed_val_float.type = 
1197               parse_type->builtin_float;
1198           else if (!strcasecmp (s, "l"))
1199             putithere->typed_val_float.type = 
1200               parse_type->builtin_long_double;
1201           else
1202             {
1203               free (s);
1204               return ERROR;
1205             }
1206         }
1207
1208       free (s);
1209       return FLOAT;
1210     }
1211
1212   /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1213   if (p[0] == '0')
1214     switch (p[1])
1215       {
1216       case 'x':
1217       case 'X':
1218         if (len >= 3)
1219           {
1220             p += 2;
1221             base = 16;
1222             len -= 2;
1223           }
1224         break;
1225
1226       case 't':
1227       case 'T':
1228       case 'd':
1229       case 'D':
1230         if (len >= 3)
1231           {
1232             p += 2;
1233             base = 10;
1234             len -= 2;
1235           }
1236         break;
1237
1238       default:
1239         base = 8;
1240         break;
1241       }
1242
1243   while (len-- > 0)
1244     {
1245       c = *p++;
1246       if (c >= 'A' && c <= 'Z')
1247         c += 'a' - 'A';
1248       if (c != 'l' && c != 'u')
1249         n *= base;
1250       if (c >= '0' && c <= '9')
1251         {
1252           if (found_suffix)
1253             return ERROR;
1254           n += i = c - '0';
1255         }
1256       else
1257         {
1258           if (base > 10 && c >= 'a' && c <= 'f')
1259             {
1260               if (found_suffix)
1261                 return ERROR;
1262               n += i = c - 'a' + 10;
1263             }
1264           else if (c == 'l')
1265             {
1266               ++long_p;
1267               found_suffix = 1;
1268             }
1269           else if (c == 'u')
1270             {
1271               unsigned_p = 1;
1272               found_suffix = 1;
1273             }
1274           else
1275             return ERROR;       /* Char not a digit */
1276         }
1277       if (i >= base)
1278         return ERROR;           /* Invalid digit in this base */
1279
1280       /* Portably test for overflow (only works for nonzero values, so make
1281          a second check for zero).  FIXME: Can't we just make n and prevn
1282          unsigned and avoid this?  */
1283       if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1284         unsigned_p = 1;         /* Try something unsigned */
1285
1286       /* Portably test for unsigned overflow.
1287          FIXME: This check is wrong; for example it doesn't find overflow
1288          on 0x123456789 when LONGEST is 32 bits.  */
1289       if (c != 'l' && c != 'u' && n != 0)
1290         {       
1291           if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1292             error ("Numeric constant too large.");
1293         }
1294       prevn = n;
1295     }
1296
1297   /* An integer constant is an int, a long, or a long long.  An L
1298      suffix forces it to be long; an LL suffix forces it to be long
1299      long.  If not forced to a larger size, it gets the first type of
1300      the above that it fits in.  To figure out whether it fits, we
1301      shift it right and see whether anything remains.  Note that we
1302      can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1303      operation, because many compilers will warn about such a shift
1304      (which always produces a zero result).  Sometimes gdbarch_int_bit
1305      or gdbarch_long_bit will be that big, sometimes not.  To deal with
1306      the case where it is we just always shift the value more than
1307      once, with fewer bits each time.  */
1308
1309   un = (ULONGEST)n >> 2;
1310   if (long_p == 0
1311       && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1312     {
1313       high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1314
1315       /* A large decimal (not hex or octal) constant (between INT_MAX
1316          and UINT_MAX) is a long or unsigned long, according to ANSI,
1317          never an unsigned int, but this code treats it as unsigned
1318          int.  This probably should be fixed.  GCC gives a warning on
1319          such constants.  */
1320
1321       unsigned_type = parse_type->builtin_unsigned_int;
1322       signed_type = parse_type->builtin_int;
1323     }
1324   else if (long_p <= 1
1325            && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1326     {
1327       high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1328       unsigned_type = parse_type->builtin_unsigned_long;
1329       signed_type = parse_type->builtin_long;
1330     }
1331   else
1332     {
1333       int shift;
1334       if (sizeof (ULONGEST) * HOST_CHAR_BIT 
1335           < gdbarch_long_long_bit (parse_gdbarch))
1336         /* A long long does not fit in a LONGEST.  */
1337         shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1338       else
1339         shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1340       high_bit = (ULONGEST) 1 << shift;
1341       unsigned_type = parse_type->builtin_unsigned_long_long;
1342       signed_type = parse_type->builtin_long_long;
1343     }
1344
1345    putithere->typed_val_int.val = n;
1346
1347    /* If the high bit of the worked out type is set then this number
1348       has to be unsigned. */
1349
1350    if (unsigned_p || (n & high_bit)) 
1351      {
1352        putithere->typed_val_int.type = unsigned_type;
1353      }
1354    else 
1355      {
1356        putithere->typed_val_int.type = signed_type;
1357      }
1358
1359    return INT;
1360 }
1361
1362 struct token
1363 {
1364   char *operator;
1365   int token;
1366   enum exp_opcode opcode;
1367   int cxx_only;
1368 };
1369
1370 static const struct token tokentab3[] =
1371   {
1372     {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1373     {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0}
1374   };
1375
1376 static const struct token tokentab2[] =
1377   {
1378     {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1379     {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1380     {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1381     {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
1382     {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
1383     {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
1384     {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
1385     {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
1386     {"++", INCREMENT, BINOP_END, 0},
1387     {"--", DECREMENT, BINOP_END, 0},
1388     {"->", ARROW, BINOP_END, 0},
1389     {"&&", ANDAND, BINOP_END, 0},
1390     {"||", OROR, BINOP_END, 0},
1391     {"::", COLONCOLON, BINOP_END, 0},
1392     {"<<", LSH, BINOP_END, 0},
1393     {">>", RSH, BINOP_END, 0},
1394     {"==", EQUAL, BINOP_END, 0},
1395     {"!=", NOTEQUAL, BINOP_END, 0},
1396     {"<=", LEQ, BINOP_END, 0},
1397     {">=", GEQ, BINOP_END, 0}
1398   };
1399
1400 /* Identifier-like tokens.  */
1401 static const struct token ident_tokens[] =
1402   {
1403     {"unsigned", UNSIGNED, OP_NULL, 0},
1404     {"template", TEMPLATE, OP_NULL, 1},
1405     {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
1406     {"struct", STRUCT, OP_NULL, 0},
1407     {"signed", SIGNED_KEYWORD, OP_NULL, 0},
1408     {"sizeof", SIZEOF, OP_NULL, 0},
1409     {"double", DOUBLE_KEYWORD, OP_NULL, 0},
1410     {"false", FALSEKEYWORD, OP_NULL, 1},
1411     {"class", CLASS, OP_NULL, 1},
1412     {"union", UNION, OP_NULL, 0},
1413     {"short", SHORT, OP_NULL, 0},
1414     {"const", CONST_KEYWORD, OP_NULL, 0},
1415     {"enum", ENUM, OP_NULL, 0},
1416     {"long", LONG, OP_NULL, 0},
1417     {"true", TRUEKEYWORD, OP_NULL, 1},
1418     {"int", INT_KEYWORD, OP_NULL, 0},
1419
1420     {"and", ANDAND, BINOP_END, 1},
1421     {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
1422     {"bitand", '&', OP_NULL, 1},
1423     {"bitor", '|', OP_NULL, 1},
1424     {"compl", '~', OP_NULL, 1},
1425     {"not", '!', OP_NULL, 1},
1426     {"not_eq", NOTEQUAL, BINOP_END, 1},
1427     {"or", OROR, BINOP_END, 1},
1428     {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
1429     {"xor", '^', OP_NULL, 1},
1430     {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1}
1431   };
1432
1433 /* When we find that lexptr (the global var defined in parse.c) is
1434    pointing at a macro invocation, we expand the invocation, and call
1435    scan_macro_expansion to save the old lexptr here and point lexptr
1436    into the expanded text.  When we reach the end of that, we call
1437    end_macro_expansion to pop back to the value we saved here.  The
1438    macro expansion code promises to return only fully-expanded text,
1439    so we don't need to "push" more than one level.
1440
1441    This is disgusting, of course.  It would be cleaner to do all macro
1442    expansion beforehand, and then hand that to lexptr.  But we don't
1443    really know where the expression ends.  Remember, in a command like
1444
1445      (gdb) break *ADDRESS if CONDITION
1446
1447    we evaluate ADDRESS in the scope of the current frame, but we
1448    evaluate CONDITION in the scope of the breakpoint's location.  So
1449    it's simply wrong to try to macro-expand the whole thing at once.  */
1450 static char *macro_original_text;
1451
1452 /* We save all intermediate macro expansions on this obstack for the
1453    duration of a single parse.  The expansion text may sometimes have
1454    to live past the end of the expansion, due to yacc lookahead.
1455    Rather than try to be clever about saving the data for a single
1456    token, we simply keep it all and delete it after parsing has
1457    completed.  */
1458 static struct obstack expansion_obstack;
1459
1460 static void
1461 scan_macro_expansion (char *expansion)
1462 {
1463   char *copy;
1464
1465   /* We'd better not be trying to push the stack twice.  */
1466   gdb_assert (! macro_original_text);
1467
1468   /* Copy to the obstack, and then free the intermediate
1469      expansion.  */
1470   copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
1471   xfree (expansion);
1472
1473   /* Save the old lexptr value, so we can return to it when we're done
1474      parsing the expanded text.  */
1475   macro_original_text = lexptr;
1476   lexptr = copy;
1477 }
1478
1479
1480 static int
1481 scanning_macro_expansion (void)
1482 {
1483   return macro_original_text != 0;
1484 }
1485
1486
1487 static void 
1488 finished_macro_expansion (void)
1489 {
1490   /* There'd better be something to pop back to.  */
1491   gdb_assert (macro_original_text);
1492
1493   /* Pop back to the original text.  */
1494   lexptr = macro_original_text;
1495   macro_original_text = 0;
1496 }
1497
1498
1499 static void
1500 scan_macro_cleanup (void *dummy)
1501 {
1502   if (macro_original_text)
1503     finished_macro_expansion ();
1504
1505   obstack_free (&expansion_obstack, NULL);
1506 }
1507
1508
1509 /* The scope used for macro expansion.  */
1510 static struct macro_scope *expression_macro_scope;
1511
1512 /* This is set if a NAME token appeared at the very end of the input
1513    string, with no whitespace separating the name from the EOF.  This
1514    is used only when parsing to do field name completion.  */
1515 static int saw_name_at_eof;
1516
1517 /* This is set if the previously-returned token was a structure
1518    operator -- either '.' or ARROW.  This is used only when parsing to
1519    do field name completion.  */
1520 static int last_was_structop;
1521
1522 /* Read one token, getting characters through lexptr.  */
1523
1524 static int
1525 yylex ()
1526 {
1527   int c;
1528   int namelen;
1529   unsigned int i;
1530   char *tokstart;
1531   char *tokptr;
1532   int tempbufindex;
1533   static char *tempbuf;
1534   static int tempbufsize;
1535   char * token_string = NULL;
1536   int class_prefix = 0;
1537   int saw_structop = last_was_structop;
1538   char *copy;
1539
1540   last_was_structop = 0;
1541
1542  retry:
1543
1544   /* Check if this is a macro invocation that we need to expand.  */
1545   if (! scanning_macro_expansion ())
1546     {
1547       char *expanded = macro_expand_next (&lexptr,
1548                                           standard_macro_lookup,
1549                                           expression_macro_scope);
1550
1551       if (expanded)
1552         scan_macro_expansion (expanded);
1553     }
1554
1555   prev_lexptr = lexptr;
1556
1557   tokstart = lexptr;
1558   /* See if it is a special token of length 3.  */
1559   for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1560     if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
1561       {
1562         lexptr += 3;
1563         yylval.opcode = tokentab3[i].opcode;
1564         return tokentab3[i].token;
1565       }
1566
1567   /* See if it is a special token of length 2.  */
1568   for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1569     if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
1570       {
1571         lexptr += 2;
1572         yylval.opcode = tokentab2[i].opcode;
1573         if (in_parse_field && tokentab2[i].token == ARROW)
1574           last_was_structop = 1;
1575         return tokentab2[i].token;
1576       }
1577
1578   switch (c = *tokstart)
1579     {
1580     case 0:
1581       /* If we were just scanning the result of a macro expansion,
1582          then we need to resume scanning the original text.
1583          If we're parsing for field name completion, and the previous
1584          token allows such completion, return a COMPLETE token.
1585          Otherwise, we were already scanning the original text, and
1586          we're really done.  */
1587       if (scanning_macro_expansion ())
1588         {
1589           finished_macro_expansion ();
1590           goto retry;
1591         }
1592       else if (saw_name_at_eof)
1593         {
1594           saw_name_at_eof = 0;
1595           return COMPLETE;
1596         }
1597       else if (saw_structop)
1598         return COMPLETE;
1599       else
1600         return 0;
1601
1602     case ' ':
1603     case '\t':
1604     case '\n':
1605       lexptr++;
1606       goto retry;
1607
1608     case '\'':
1609       /* We either have a character constant ('0' or '\177' for example)
1610          or we have a quoted symbol reference ('foo(int,int)' in C++
1611          for example). */
1612       lexptr++;
1613       c = *lexptr++;
1614       if (c == '\\')
1615         c = parse_escape (&lexptr);
1616       else if (c == '\'')
1617         error ("Empty character constant.");
1618       else if (! host_char_to_target (c, &c))
1619         {
1620           int toklen = lexptr - tokstart + 1;
1621           char *tok = alloca (toklen + 1);
1622           memcpy (tok, tokstart, toklen);
1623           tok[toklen] = '\0';
1624           error ("There is no character corresponding to %s in the target "
1625                  "character set `%s'.", tok, target_charset ());
1626         }
1627
1628       yylval.typed_val_int.val = c;
1629       yylval.typed_val_int.type = parse_type->builtin_char;
1630
1631       c = *lexptr++;
1632       if (c != '\'')
1633         {
1634           namelen = skip_quoted (tokstart) - tokstart;
1635           if (namelen > 2)
1636             {
1637               lexptr = tokstart + namelen;
1638               if (lexptr[-1] != '\'')
1639                 error ("Unmatched single quote.");
1640               namelen -= 2;
1641               tokstart++;
1642               goto tryname;
1643             }
1644           error ("Invalid character constant.");
1645         }
1646       return INT;
1647
1648     case '(':
1649       paren_depth++;
1650       lexptr++;
1651       return c;
1652
1653     case ')':
1654       if (paren_depth == 0)
1655         return 0;
1656       paren_depth--;
1657       lexptr++;
1658       return c;
1659
1660     case ',':
1661       if (comma_terminates
1662           && paren_depth == 0
1663           && ! scanning_macro_expansion ())
1664         return 0;
1665       lexptr++;
1666       return c;
1667
1668     case '.':
1669       /* Might be a floating point number.  */
1670       if (lexptr[1] < '0' || lexptr[1] > '9')
1671         {
1672           if (in_parse_field)
1673             last_was_structop = 1;
1674           goto symbol;          /* Nope, must be a symbol. */
1675         }
1676       /* FALL THRU into number case.  */
1677
1678     case '0':
1679     case '1':
1680     case '2':
1681     case '3':
1682     case '4':
1683     case '5':
1684     case '6':
1685     case '7':
1686     case '8':
1687     case '9':
1688       {
1689         /* It's a number.  */
1690         int got_dot = 0, got_e = 0, toktype;
1691         char *p = tokstart;
1692         int hex = input_radix > 10;
1693
1694         if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1695           {
1696             p += 2;
1697             hex = 1;
1698           }
1699         else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1700           {
1701             p += 2;
1702             hex = 0;
1703           }
1704
1705         for (;; ++p)
1706           {
1707             /* This test includes !hex because 'e' is a valid hex digit
1708                and thus does not indicate a floating point number when
1709                the radix is hex.  */
1710             if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1711               got_dot = got_e = 1;
1712             /* This test does not include !hex, because a '.' always indicates
1713                a decimal floating point number regardless of the radix.  */
1714             else if (!got_dot && *p == '.')
1715               got_dot = 1;
1716             else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1717                      && (*p == '-' || *p == '+'))
1718               /* This is the sign of the exponent, not the end of the
1719                  number.  */
1720               continue;
1721             /* We will take any letters or digits.  parse_number will
1722                complain if past the radix, or if L or U are not final.  */
1723             else if ((*p < '0' || *p > '9')
1724                      && ((*p < 'a' || *p > 'z')
1725                                   && (*p < 'A' || *p > 'Z')))
1726               break;
1727           }
1728         toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1729         if (toktype == ERROR)
1730           {
1731             char *err_copy = (char *) alloca (p - tokstart + 1);
1732
1733             memcpy (err_copy, tokstart, p - tokstart);
1734             err_copy[p - tokstart] = 0;
1735             error ("Invalid number \"%s\".", err_copy);
1736           }
1737         lexptr = p;
1738         return toktype;
1739       }
1740
1741     case '+':
1742     case '-':
1743     case '*':
1744     case '/':
1745     case '%':
1746     case '|':
1747     case '&':
1748     case '^':
1749     case '~':
1750     case '!':
1751     case '@':
1752     case '<':
1753     case '>':
1754     case '[':
1755     case ']':
1756     case '?':
1757     case ':':
1758     case '=':
1759     case '{':
1760     case '}':
1761     symbol:
1762       lexptr++;
1763       return c;
1764
1765     case '"':
1766
1767       /* Build the gdb internal form of the input string in tempbuf,
1768          translating any standard C escape forms seen.  Note that the
1769          buffer is null byte terminated *only* for the convenience of
1770          debugging gdb itself and printing the buffer contents when
1771          the buffer contains no embedded nulls.  Gdb does not depend
1772          upon the buffer being null byte terminated, it uses the length
1773          string instead.  This allows gdb to handle C strings (as well
1774          as strings in other languages) with embedded null bytes */
1775
1776       tokptr = ++tokstart;
1777       tempbufindex = 0;
1778
1779       do {
1780         char *char_start_pos = tokptr;
1781
1782         /* Grow the static temp buffer if necessary, including allocating
1783            the first one on demand. */
1784         if (tempbufindex + 1 >= tempbufsize)
1785           {
1786             tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1787           }
1788         switch (*tokptr)
1789           {
1790           case '\0':
1791           case '"':
1792             /* Do nothing, loop will terminate. */
1793             break;
1794           case '\\':
1795             tokptr++;
1796             c = parse_escape (&tokptr);
1797             if (c == -1)
1798               {
1799                 continue;
1800               }
1801             tempbuf[tempbufindex++] = c;
1802             break;
1803           default:
1804             c = *tokptr++;
1805             if (! host_char_to_target (c, &c))
1806               {
1807                 int len = tokptr - char_start_pos;
1808                 char *copy = alloca (len + 1);
1809                 memcpy (copy, char_start_pos, len);
1810                 copy[len] = '\0';
1811
1812                 error ("There is no character corresponding to `%s' "
1813                        "in the target character set `%s'.",
1814                        copy, target_charset ());
1815               }
1816             tempbuf[tempbufindex++] = c;
1817             break;
1818           }
1819       } while ((*tokptr != '"') && (*tokptr != '\0'));
1820       if (*tokptr++ != '"')
1821         {
1822           error ("Unterminated string in expression.");
1823         }
1824       tempbuf[tempbufindex] = '\0';     /* See note above */
1825       yylval.sval.ptr = tempbuf;
1826       yylval.sval.length = tempbufindex;
1827       lexptr = tokptr;
1828       return (STRING);
1829     }
1830
1831   if (!(c == '_' || c == '$'
1832         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1833     /* We must have come across a bad character (e.g. ';').  */
1834     error ("Invalid character '%c' in expression.", c);
1835
1836   /* It's a name.  See how long it is.  */
1837   namelen = 0;
1838   for (c = tokstart[namelen];
1839        (c == '_' || c == '$' || (c >= '0' && c <= '9')
1840         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1841     {
1842       /* Template parameter lists are part of the name.
1843          FIXME: This mishandles `print $a<4&&$a>3'.  */
1844
1845       if (c == '<')
1846         { 
1847                /* Scan ahead to get rest of the template specification.  Note
1848                   that we look ahead only when the '<' adjoins non-whitespace
1849                   characters; for comparison expressions, e.g. "a < b > c",
1850                   there must be spaces before the '<', etc. */
1851                
1852                char * p = find_template_name_end (tokstart + namelen);
1853                if (p)
1854                  namelen = p - tokstart;
1855                break;
1856         }
1857       c = tokstart[++namelen];
1858     }
1859
1860   /* The token "if" terminates the expression and is NOT removed from
1861      the input stream.  It doesn't count if it appears in the
1862      expansion of a macro.  */
1863   if (namelen == 2
1864       && tokstart[0] == 'i'
1865       && tokstart[1] == 'f'
1866       && ! scanning_macro_expansion ())
1867     {
1868       return 0;
1869     }
1870
1871   lexptr += namelen;
1872
1873   tryname:
1874
1875   yylval.sval.ptr = tokstart;
1876   yylval.sval.length = namelen;
1877
1878   /* Catch specific keywords.  */
1879   copy = copy_name (yylval.sval);
1880   for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
1881     if (strcmp (copy, ident_tokens[i].operator) == 0)
1882       {
1883         if (ident_tokens[i].cxx_only
1884             && parse_language->la_language != language_cplus)
1885           break;
1886
1887         /* It is ok to always set this, even though we don't always
1888            strictly need to.  */
1889         yylval.opcode = ident_tokens[i].opcode;
1890         return ident_tokens[i].token;
1891       }
1892
1893   if (*tokstart == '$')
1894     {
1895       write_dollar_variable (yylval.sval);
1896       return VARIABLE;
1897     }
1898   
1899   /* Use token-type BLOCKNAME for symbols that happen to be defined as
1900      functions or symtabs.  If this is not so, then ...
1901      Use token-type TYPENAME for symbols that happen to be defined
1902      currently as names of types; NAME for other symbols.
1903      The caller is not constrained to care about the distinction.  */
1904   {
1905     struct symbol *sym;
1906     int is_a_field_of_this = 0;
1907     int hextype;
1908
1909     sym = lookup_symbol (copy, expression_context_block,
1910                          VAR_DOMAIN,
1911                          parse_language->la_language == language_cplus
1912                          ? &is_a_field_of_this : (int *) NULL);
1913     /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1914        no psymtabs (coff, xcoff, or some future change to blow away the
1915        psymtabs once once symbols are read).  */
1916     if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1917       {
1918         yylval.ssym.sym = sym;
1919         yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1920         return BLOCKNAME;
1921       }
1922     else if (!sym)
1923       {                         /* See if it's a file name. */
1924         struct symtab *symtab;
1925
1926         symtab = lookup_symtab (copy);
1927
1928         if (symtab)
1929           {
1930             yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1931             return FILENAME;
1932           }
1933       }
1934
1935     if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1936         {
1937           /* NOTE: carlton/2003-09-25: There used to be code here to
1938              handle nested types.  It didn't work very well.  See the
1939              comment before qualified_type for more info.  */
1940           yylval.tsym.type = SYMBOL_TYPE (sym);
1941           return TYPENAME;
1942         }
1943     yylval.tsym.type
1944       = language_lookup_primitive_type_by_name (parse_language,
1945                                                 parse_gdbarch, copy);
1946     if (yylval.tsym.type != NULL)
1947       return TYPENAME;
1948
1949     /* Input names that aren't symbols but ARE valid hex numbers,
1950        when the input radix permits them, can be names or numbers
1951        depending on the parse.  Note we support radixes > 16 here.  */
1952     if (!sym && 
1953         ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1954          (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1955       {
1956         YYSTYPE newlval;        /* Its value is ignored.  */
1957         hextype = parse_number (tokstart, namelen, 0, &newlval);
1958         if (hextype == INT)
1959           {
1960             yylval.ssym.sym = sym;
1961             yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1962             return NAME_OR_INT;
1963           }
1964       }
1965
1966     /* Any other kind of symbol */
1967     yylval.ssym.sym = sym;
1968     yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1969     if (in_parse_field && *lexptr == '\0')
1970       saw_name_at_eof = 1;
1971     return NAME;
1972   }
1973 }
1974
1975 int
1976 c_parse (void)
1977 {
1978   int result;
1979   struct cleanup *back_to = make_cleanup (free_current_contents,
1980                                           &expression_macro_scope);
1981
1982   /* Set up the scope for macro expansion.  */
1983   expression_macro_scope = NULL;
1984
1985   if (expression_context_block)
1986     expression_macro_scope
1987       = sal_macro_scope (find_pc_line (expression_context_pc, 0));
1988   else
1989     expression_macro_scope = default_macro_scope ();
1990   if (! expression_macro_scope)
1991     expression_macro_scope = user_macro_scope ();
1992
1993   /* Initialize macro expansion code.  */
1994   obstack_init (&expansion_obstack);
1995   gdb_assert (! macro_original_text);
1996   make_cleanup (scan_macro_cleanup, 0);
1997
1998   /* Initialize some state used by the lexer.  */
1999   last_was_structop = 0;
2000   saw_name_at_eof = 0;
2001
2002   result = yyparse ();
2003   do_cleanups (back_to);
2004   return result;
2005 }
2006
2007
2008 void
2009 yyerror (msg)
2010      char *msg;
2011 {
2012   if (prev_lexptr)
2013     lexptr = prev_lexptr;
2014
2015   error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
2016 }