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