OSDN Git Service

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