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