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