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