7316ae8a1b9d3276aa1cbddc1c52713932e7cfdb
[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, 1996, 1997
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 exp     :       exp '.' qualified_name
302                         { /* exp.type::name becomes exp.*(&type::name) */
303                           /* Note: this doesn't work if name is a
304                              static member!  FIXME */
305                           write_exp_elt_opcode (UNOP_ADDR);
306                           write_exp_elt_opcode (STRUCTOP_MEMBER); }
307         ;
308
309 exp     :       exp '.' '*' exp
310                         { write_exp_elt_opcode (STRUCTOP_MEMBER); }
311         ;
312
313 exp     :       exp '[' exp1 ']'
314                         { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
315         ;
316
317 exp     :       exp '(' 
318                         /* This is to save the value of arglist_len
319                            being accumulated by an outer function call.  */
320                         { start_arglist (); }
321                 arglist ')'     %prec ARROW
322                         { write_exp_elt_opcode (OP_FUNCALL);
323                           write_exp_elt_longcst ((LONGEST) end_arglist ());
324                           write_exp_elt_opcode (OP_FUNCALL); }
325         ;
326
327 lcurly  :       '{'
328                         { start_arglist (); }
329         ;
330
331 arglist :
332         ;
333
334 arglist :       exp
335                         { arglist_len = 1; }
336         ;
337
338 arglist :       arglist ',' exp   %prec ABOVE_COMMA
339                         { arglist_len++; }
340         ;
341
342 rcurly  :       '}'
343                         { $$ = end_arglist () - 1; }
344         ;
345 exp     :       lcurly arglist rcurly   %prec ARROW
346                         { write_exp_elt_opcode (OP_ARRAY);
347                           write_exp_elt_longcst ((LONGEST) 0);
348                           write_exp_elt_longcst ((LONGEST) $3);
349                           write_exp_elt_opcode (OP_ARRAY); }
350         ;
351
352 exp     :       lcurly type rcurly exp  %prec UNARY
353                         { write_exp_elt_opcode (UNOP_MEMVAL);
354                           write_exp_elt_type ($2);
355                           write_exp_elt_opcode (UNOP_MEMVAL); }
356         ;
357
358 exp     :       '(' type ')' exp  %prec UNARY
359                         { write_exp_elt_opcode (UNOP_CAST);
360                           write_exp_elt_type ($2);
361                           write_exp_elt_opcode (UNOP_CAST); }
362         ;
363
364 exp     :       '(' exp1 ')'
365                         { }
366         ;
367
368 /* Binary operators in order of decreasing precedence.  */
369
370 exp     :       exp '@' exp
371                         { write_exp_elt_opcode (BINOP_REPEAT); }
372         ;
373
374 exp     :       exp '*' exp
375                         { write_exp_elt_opcode (BINOP_MUL); }
376         ;
377
378 exp     :       exp '/' exp
379                         { write_exp_elt_opcode (BINOP_DIV); }
380         ;
381
382 exp     :       exp '%' exp
383                         { write_exp_elt_opcode (BINOP_REM); }
384         ;
385
386 exp     :       exp '+' exp
387                         { write_exp_elt_opcode (BINOP_ADD); }
388         ;
389
390 exp     :       exp '-' exp
391                         { write_exp_elt_opcode (BINOP_SUB); }
392         ;
393
394 exp     :       exp LSH exp
395                         { write_exp_elt_opcode (BINOP_LSH); }
396         ;
397
398 exp     :       exp RSH exp
399                         { write_exp_elt_opcode (BINOP_RSH); }
400         ;
401
402 exp     :       exp EQUAL exp
403                         { write_exp_elt_opcode (BINOP_EQUAL); }
404         ;
405
406 exp     :       exp NOTEQUAL exp
407                         { write_exp_elt_opcode (BINOP_NOTEQUAL); }
408         ;
409
410 exp     :       exp LEQ exp
411                         { write_exp_elt_opcode (BINOP_LEQ); }
412         ;
413
414 exp     :       exp GEQ exp
415                         { write_exp_elt_opcode (BINOP_GEQ); }
416         ;
417
418 exp     :       exp '<' exp
419                         { write_exp_elt_opcode (BINOP_LESS); }
420         ;
421
422 exp     :       exp '>' exp
423                         { write_exp_elt_opcode (BINOP_GTR); }
424         ;
425
426 exp     :       exp '&' exp
427                         { write_exp_elt_opcode (BINOP_BITWISE_AND); }
428         ;
429
430 exp     :       exp '^' exp
431                         { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
432         ;
433
434 exp     :       exp '|' exp
435                         { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
436         ;
437
438 exp     :       exp ANDAND exp
439                         { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
440         ;
441
442 exp     :       exp OROR exp
443                         { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
444         ;
445
446 exp     :       exp '?' exp ':' exp     %prec '?'
447                         { write_exp_elt_opcode (TERNOP_COND); }
448         ;
449                           
450 exp     :       exp '=' exp
451                         { write_exp_elt_opcode (BINOP_ASSIGN); }
452         ;
453
454 exp     :       exp ASSIGN_MODIFY exp
455                         { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
456                           write_exp_elt_opcode ($2);
457                           write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
458         ;
459
460 exp     :       INT
461                         { write_exp_elt_opcode (OP_LONG);
462                           write_exp_elt_type ($1.type);
463                           write_exp_elt_longcst ((LONGEST)($1.val));
464                           write_exp_elt_opcode (OP_LONG); }
465         ;
466
467 exp     :       NAME_OR_INT
468                         { YYSTYPE val;
469                           parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
470                           write_exp_elt_opcode (OP_LONG);
471                           write_exp_elt_type (val.typed_val_int.type);
472                           write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
473                           write_exp_elt_opcode (OP_LONG);
474                         }
475         ;
476
477
478 exp     :       FLOAT
479                         { write_exp_elt_opcode (OP_DOUBLE);
480                           write_exp_elt_type ($1.type);
481                           write_exp_elt_dblcst ($1.dval);
482                           write_exp_elt_opcode (OP_DOUBLE); }
483         ;
484
485 exp     :       variable
486         ;
487
488 exp     :       VARIABLE
489                         /* Already written by write_dollar_variable. */
490         ;
491
492 exp     :       SIZEOF '(' type ')'     %prec UNARY
493                         { write_exp_elt_opcode (OP_LONG);
494                           write_exp_elt_type (builtin_type_int);
495                           CHECK_TYPEDEF ($3);
496                           write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
497                           write_exp_elt_opcode (OP_LONG); }
498         ;
499
500 exp     :       STRING
501                         { /* C strings are converted into array constants with
502                              an explicit null byte added at the end.  Thus
503                              the array upper bound is the string length.
504                              There is no such thing in C as a completely empty
505                              string. */
506                           char *sp = $1.ptr; int count = $1.length;
507                           while (count-- > 0)
508                             {
509                               write_exp_elt_opcode (OP_LONG);
510                               write_exp_elt_type (builtin_type_char);
511                               write_exp_elt_longcst ((LONGEST)(*sp++));
512                               write_exp_elt_opcode (OP_LONG);
513                             }
514                           write_exp_elt_opcode (OP_LONG);
515                           write_exp_elt_type (builtin_type_char);
516                           write_exp_elt_longcst ((LONGEST)'\0');
517                           write_exp_elt_opcode (OP_LONG);
518                           write_exp_elt_opcode (OP_ARRAY);
519                           write_exp_elt_longcst ((LONGEST) 0);
520                           write_exp_elt_longcst ((LONGEST) ($1.length));
521                           write_exp_elt_opcode (OP_ARRAY); }
522         ;
523
524 /* C++.  */
525 exp     :       THIS
526                         { write_exp_elt_opcode (OP_THIS);
527                           write_exp_elt_opcode (OP_THIS); }
528         ;
529
530 /* end of C++.  */
531
532 block   :       BLOCKNAME
533                         {
534                           if ($1.sym != 0)
535                               $$ = SYMBOL_BLOCK_VALUE ($1.sym);
536                           else
537                             {
538                               struct symtab *tem =
539                                   lookup_symtab (copy_name ($1.stoken));
540                               if (tem)
541                                 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), STATIC_BLOCK);
542                               else
543                                 error ("No file or function \"%s\".",
544                                        copy_name ($1.stoken));
545                             }
546                         }
547         ;
548
549 block   :       block COLONCOLON name
550                         { struct symbol *tem
551                             = lookup_symbol (copy_name ($3), $1,
552                                              VAR_NAMESPACE, (int *) NULL,
553                                              (struct symtab **) NULL);
554                           if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
555                             error ("No function \"%s\" in specified context.",
556                                    copy_name ($3));
557                           $$ = SYMBOL_BLOCK_VALUE (tem); }
558         ;
559
560 variable:       block COLONCOLON name
561                         { struct symbol *sym;
562                           sym = lookup_symbol (copy_name ($3), $1,
563                                                VAR_NAMESPACE, (int *) NULL,
564                                                (struct symtab **) NULL);
565                           if (sym == 0)
566                             error ("No symbol \"%s\" in specified context.",
567                                    copy_name ($3));
568
569                           write_exp_elt_opcode (OP_VAR_VALUE);
570                           /* block_found is set by lookup_symbol.  */
571                           write_exp_elt_block (block_found);
572                           write_exp_elt_sym (sym);
573                           write_exp_elt_opcode (OP_VAR_VALUE); }
574         ;
575
576 qualified_name: typebase COLONCOLON name
577                         {
578                           struct type *type = $1;
579                           if (TYPE_CODE (type) != TYPE_CODE_STRUCT
580                               && TYPE_CODE (type) != TYPE_CODE_UNION)
581                             error ("`%s' is not defined as an aggregate type.",
582                                    TYPE_NAME (type));
583
584                           write_exp_elt_opcode (OP_SCOPE);
585                           write_exp_elt_type (type);
586                           write_exp_string ($3);
587                           write_exp_elt_opcode (OP_SCOPE);
588                         }
589         |       typebase COLONCOLON '~' name
590                         {
591                           struct type *type = $1;
592                           struct stoken tmp_token;
593                           if (TYPE_CODE (type) != TYPE_CODE_STRUCT
594                               && TYPE_CODE (type) != TYPE_CODE_UNION)
595                             error ("`%s' is not defined as an aggregate type.",
596                                    TYPE_NAME (type));
597
598                           tmp_token.ptr = (char*) alloca ($4.length + 2);
599                           tmp_token.length = $4.length + 1;
600                           tmp_token.ptr[0] = '~';
601                           memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
602                           tmp_token.ptr[tmp_token.length] = 0;
603
604                           /* Check for valid destructor name.  */
605                           destructor_name_p (tmp_token.ptr, type);
606                           write_exp_elt_opcode (OP_SCOPE);
607                           write_exp_elt_type (type);
608                           write_exp_string (tmp_token);
609                           write_exp_elt_opcode (OP_SCOPE);
610                         }
611         ;
612
613 variable:       qualified_name
614         |       COLONCOLON name
615                         {
616                           char *name = copy_name ($2);
617                           struct symbol *sym;
618                           struct minimal_symbol *msymbol;
619
620                           sym =
621                             lookup_symbol (name, (const struct block *) NULL,
622                                            VAR_NAMESPACE, (int *) NULL,
623                                            (struct symtab **) NULL);
624                           if (sym)
625                             {
626                               write_exp_elt_opcode (OP_VAR_VALUE);
627                               write_exp_elt_block (NULL);
628                               write_exp_elt_sym (sym);
629                               write_exp_elt_opcode (OP_VAR_VALUE);
630                               break;
631                             }
632
633                           msymbol = lookup_minimal_symbol (name, NULL, NULL);
634                           if (msymbol != NULL)
635                             {
636                               write_exp_msymbol (msymbol,
637                                                  lookup_function_type (builtin_type_int),
638                                                  builtin_type_int);
639                             }
640                           else
641                             if (!have_full_symbols () && !have_partial_symbols ())
642                               error ("No symbol table is loaded.  Use the \"file\" command.");
643                             else
644                               error ("No symbol \"%s\" in current context.", name);
645                         }
646         ;
647
648 variable:       name_not_typename
649                         { struct symbol *sym = $1.sym;
650
651                           if (sym)
652                             {
653                               if (symbol_read_needs_frame (sym))
654                                 {
655                                   if (innermost_block == 0 ||
656                                       contained_in (block_found, 
657                                                     innermost_block))
658                                     innermost_block = block_found;
659                                 }
660
661                               write_exp_elt_opcode (OP_VAR_VALUE);
662                               /* We want to use the selected frame, not
663                                  another more inner frame which happens to
664                                  be in the same block.  */
665                               write_exp_elt_block (NULL);
666                               write_exp_elt_sym (sym);
667                               write_exp_elt_opcode (OP_VAR_VALUE);
668                             }
669                           else if ($1.is_a_field_of_this)
670                             {
671                               /* C++: it hangs off of `this'.  Must
672                                  not inadvertently convert from a method call
673                                  to data ref.  */
674                               if (innermost_block == 0 || 
675                                   contained_in (block_found, innermost_block))
676                                 innermost_block = block_found;
677                               write_exp_elt_opcode (OP_THIS);
678                               write_exp_elt_opcode (OP_THIS);
679                               write_exp_elt_opcode (STRUCTOP_PTR);
680                               write_exp_string ($1.stoken);
681                               write_exp_elt_opcode (STRUCTOP_PTR);
682                             }
683                           else
684                             {
685                               struct minimal_symbol *msymbol;
686                               register char *arg = copy_name ($1.stoken);
687
688                               msymbol =
689                                 lookup_minimal_symbol (arg, NULL, NULL);
690                               if (msymbol != NULL)
691                                 {
692                                   write_exp_msymbol (msymbol,
693                                                      lookup_function_type (builtin_type_int),
694                                                      builtin_type_int);
695                                 }
696                               else if (!have_full_symbols () && !have_partial_symbols ())
697                                 error ("No symbol table is loaded.  Use the \"file\" command.");
698                               else
699                                 error ("No symbol \"%s\" in current context.",
700                                        copy_name ($1.stoken));
701                             }
702                         }
703         ;
704
705
706 ptype   :       typebase
707         /* "const" and "volatile" are curently ignored.  A type qualifier
708            before the type is currently handled in the typebase rule.
709            The reason for recognizing these here (shift/reduce conflicts)
710            might be obsolete now that some pointer to member rules have
711            been deleted.  */
712         |       typebase CONST_KEYWORD
713         |       typebase VOLATILE_KEYWORD
714         |       typebase abs_decl
715                 { $$ = follow_types ($1); }
716         |       typebase CONST_KEYWORD abs_decl
717                 { $$ = follow_types ($1); }
718         |       typebase VOLATILE_KEYWORD abs_decl
719                 { $$ = follow_types ($1); }
720         ;
721
722 abs_decl:       '*'
723                         { push_type (tp_pointer); $$ = 0; }
724         |       '*' abs_decl
725                         { push_type (tp_pointer); $$ = $2; }
726         |       '&'
727                         { push_type (tp_reference); $$ = 0; }
728         |       '&' abs_decl
729                         { push_type (tp_reference); $$ = $2; }
730         |       direct_abs_decl
731         ;
732
733 direct_abs_decl: '(' abs_decl ')'
734                         { $$ = $2; }
735         |       direct_abs_decl array_mod
736                         {
737                           push_type_int ($2);
738                           push_type (tp_array);
739                         }
740         |       array_mod
741                         {
742                           push_type_int ($1);
743                           push_type (tp_array);
744                           $$ = 0;
745                         }
746
747         |       direct_abs_decl func_mod
748                         { push_type (tp_function); }
749         |       func_mod
750                         { push_type (tp_function); }
751         ;
752
753 array_mod:      '[' ']'
754                         { $$ = -1; }
755         |       '[' INT ']'
756                         { $$ = $2.val; }
757         ;
758
759 func_mod:       '(' ')'
760                         { $$ = 0; }
761         |       '(' nonempty_typelist ')'
762                         { free ((PTR)$2); $$ = 0; }
763         ;
764
765 /* We used to try to recognize more pointer to member types here, but
766    that didn't work (shift/reduce conflicts meant that these rules never
767    got executed).  The problem is that
768      int (foo::bar::baz::bizzle)
769    is a function type but
770      int (foo::bar::baz::bizzle::*)
771    is a pointer to member type.  Stroustrup loses again!  */
772
773 type    :       ptype
774         |       typebase COLONCOLON '*'
775                         { $$ = lookup_member_type (builtin_type_int, $1); }
776         ;
777
778 typebase  /* Implements (approximately): (type-qualifier)* type-specifier */
779         :       TYPENAME
780                         { $$ = $1.type; }
781         |       INT_KEYWORD
782                         { $$ = builtin_type_int; }
783         |       LONG
784                         { $$ = builtin_type_long; }
785         |       SHORT
786                         { $$ = builtin_type_short; }
787         |       LONG INT_KEYWORD
788                         { $$ = builtin_type_long; }
789         |       UNSIGNED LONG INT_KEYWORD
790                         { $$ = builtin_type_unsigned_long; }
791         |       LONG LONG
792                         { $$ = builtin_type_long_long; }
793         |       LONG LONG INT_KEYWORD
794                         { $$ = builtin_type_long_long; }
795         |       UNSIGNED LONG LONG
796                         { $$ = builtin_type_unsigned_long_long; }
797         |       UNSIGNED LONG LONG INT_KEYWORD
798                         { $$ = builtin_type_unsigned_long_long; }
799         |       SHORT INT_KEYWORD
800                         { $$ = builtin_type_short; }
801         |       UNSIGNED SHORT INT_KEYWORD
802                         { $$ = builtin_type_unsigned_short; }
803         |       DOUBLE_KEYWORD
804                         { $$ = builtin_type_double; }
805         |       LONG DOUBLE_KEYWORD
806                         { $$ = builtin_type_long_double; }
807         |       STRUCT name
808                         { $$ = lookup_struct (copy_name ($2),
809                                               expression_context_block); }
810         |       CLASS name
811                         { $$ = lookup_struct (copy_name ($2),
812                                               expression_context_block); }
813         |       UNION name
814                         { $$ = lookup_union (copy_name ($2),
815                                              expression_context_block); }
816         |       ENUM name
817                         { $$ = lookup_enum (copy_name ($2),
818                                             expression_context_block); }
819         |       UNSIGNED typename
820                         { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
821         |       UNSIGNED
822                         { $$ = builtin_type_unsigned_int; }
823         |       SIGNED_KEYWORD typename
824                         { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
825         |       SIGNED_KEYWORD
826                         { $$ = builtin_type_int; }
827         |       TEMPLATE name '<' type '>'
828                         { $$ = lookup_template_type(copy_name($2), $4,
829                                                     expression_context_block);
830                         }
831         /* "const" and "volatile" are curently ignored.  A type qualifier
832            after the type is handled in the ptype rule.  I think these could
833            be too.  */
834         |       CONST_KEYWORD typebase { $$ = $2; }
835         |       VOLATILE_KEYWORD typebase { $$ = $2; }
836         ;
837
838 typename:       TYPENAME
839         |       INT_KEYWORD
840                 {
841                   $$.stoken.ptr = "int";
842                   $$.stoken.length = 3;
843                   $$.type = builtin_type_int;
844                 }
845         |       LONG
846                 {
847                   $$.stoken.ptr = "long";
848                   $$.stoken.length = 4;
849                   $$.type = builtin_type_long;
850                 }
851         |       SHORT
852                 {
853                   $$.stoken.ptr = "short";
854                   $$.stoken.length = 5;
855                   $$.type = builtin_type_short;
856                 }
857         ;
858
859 nonempty_typelist
860         :       type
861                 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
862                   $<ivec>$[0] = 1;      /* Number of types in vector */
863                   $$[1] = $1;
864                 }
865         |       nonempty_typelist ',' type
866                 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
867                   $$ = (struct type **) realloc ((char *) $1, len);
868                   $$[$<ivec>$[0]] = $3;
869                 }
870         ;
871
872 name    :       NAME { $$ = $1.stoken; }
873         |       BLOCKNAME { $$ = $1.stoken; }
874         |       TYPENAME { $$ = $1.stoken; }
875         |       NAME_OR_INT  { $$ = $1.stoken; }
876         ;
877
878 name_not_typename :     NAME
879         |       BLOCKNAME
880 /* These would be useful if name_not_typename was useful, but it is just
881    a fake for "variable", so these cause reduce/reduce conflicts because
882    the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
883    =exp) or just an exp.  If name_not_typename was ever used in an lvalue
884    context where only a name could occur, this might be useful.
885         |       NAME_OR_INT
886  */
887         ;
888
889 %%
890
891 /* Take care of parsing a number (anything that starts with a digit).
892    Set yylval and return the token type; update lexptr.
893    LEN is the number of characters in it.  */
894
895 /*** Needs some error checking for the float case ***/
896
897 static int
898 parse_number (p, len, parsed_float, putithere)
899      register char *p;
900      register int len;
901      int parsed_float;
902      YYSTYPE *putithere;
903 {
904   /* FIXME: Shouldn't these be unsigned?  We don't deal with negative values
905      here, and we do kind of silly things like cast to unsigned.  */
906   register LONGEST n = 0;
907   register LONGEST prevn = 0;
908   ULONGEST un;
909
910   register int i = 0;
911   register int c;
912   register int base = input_radix;
913   int unsigned_p = 0;
914
915   /* Number of "L" suffixes encountered.  */
916   int long_p = 0;
917
918   /* We have found a "L" or "U" suffix.  */
919   int found_suffix = 0;
920
921   ULONGEST high_bit;
922   struct type *signed_type;
923   struct type *unsigned_type;
924
925   if (parsed_float)
926     {
927       /* It's a float since it contains a point or an exponent.  */
928       char c;
929       int num = 0;      /* number of tokens scanned by scanf */
930       char saved_char = p[len];
931
932       p[len] = 0;       /* null-terminate the token */
933       if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
934         num = sscanf (p, "%g%c", (float *) &putithere->typed_val_float.dval,&c);
935       else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
936         num = sscanf (p, "%lg%c", (double *) &putithere->typed_val_float.dval,&c);
937       else
938         {
939 #ifdef SCANF_HAS_LONG_DOUBLE
940           num = sscanf (p, "%Lg%c", &putithere->typed_val_float.dval,&c);
941 #else
942           /* Scan it into a double, then assign it to the long double.
943              This at least wins with values representable in the range
944              of doubles. */
945           double temp;
946           num = sscanf (p, "%lg%c", &temp,&c);
947           putithere->typed_val_float.dval = temp;
948 #endif
949         }
950       p[len] = saved_char;      /* restore the input stream */
951       if (num != 1)             /* check scanf found ONLY a float ... */
952         return ERROR;
953       /* See if it has `f' or `l' suffix (float or long double).  */
954
955       c = tolower (p[len - 1]);
956
957       if (c == 'f')
958         putithere->typed_val_float.type = builtin_type_float;
959       else if (c == 'l')
960         putithere->typed_val_float.type = builtin_type_long_double;
961       else if (isdigit (c) || c == '.')
962         putithere->typed_val_float.type = builtin_type_double;
963       else
964         return ERROR;
965
966       return FLOAT;
967     }
968
969   /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
970   if (p[0] == '0')
971     switch (p[1])
972       {
973       case 'x':
974       case 'X':
975         if (len >= 3)
976           {
977             p += 2;
978             base = 16;
979             len -= 2;
980           }
981         break;
982
983       case 't':
984       case 'T':
985       case 'd':
986       case 'D':
987         if (len >= 3)
988           {
989             p += 2;
990             base = 10;
991             len -= 2;
992           }
993         break;
994
995       default:
996         base = 8;
997         break;
998       }
999
1000   while (len-- > 0)
1001     {
1002       c = *p++;
1003       if (c >= 'A' && c <= 'Z')
1004         c += 'a' - 'A';
1005       if (c != 'l' && c != 'u')
1006         n *= base;
1007       if (c >= '0' && c <= '9')
1008         {
1009           if (found_suffix)
1010             return ERROR;
1011           n += i = c - '0';
1012         }
1013       else
1014         {
1015           if (base > 10 && c >= 'a' && c <= 'f')
1016             {
1017               if (found_suffix)
1018                 return ERROR;
1019               n += i = c - 'a' + 10;
1020             }
1021           else if (c == 'l')
1022             {
1023               ++long_p;
1024               found_suffix = 1;
1025             }
1026           else if (c == 'u')
1027             {
1028               unsigned_p = 1;
1029               found_suffix = 1;
1030             }
1031           else
1032             return ERROR;       /* Char not a digit */
1033         }
1034       if (i >= base)
1035         return ERROR;           /* Invalid digit in this base */
1036
1037       /* Portably test for overflow (only works for nonzero values, so make
1038          a second check for zero).  FIXME: Can't we just make n and prevn
1039          unsigned and avoid this?  */
1040       if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1041         unsigned_p = 1;         /* Try something unsigned */
1042
1043       /* Portably test for unsigned overflow.
1044          FIXME: This check is wrong; for example it doesn't find overflow
1045          on 0x123456789 when LONGEST is 32 bits.  */
1046       if (c != 'l' && c != 'u' && n != 0)
1047         {       
1048           if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1049             error ("Numeric constant too large.");
1050         }
1051       prevn = n;
1052     }
1053
1054   /* An integer constant is an int, a long, or a long long.  An L
1055      suffix forces it to be long; an LL suffix forces it to be long
1056      long.  If not forced to a larger size, it gets the first type of
1057      the above that it fits in.  To figure out whether it fits, we
1058      shift it right and see whether anything remains.  Note that we
1059      can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1060      operation, because many compilers will warn about such a shift
1061      (which always produces a zero result).  Sometimes TARGET_INT_BIT
1062      or TARGET_LONG_BIT will be that big, sometimes not.  To deal with
1063      the case where it is we just always shift the value more than
1064      once, with fewer bits each time.  */
1065
1066   un = (ULONGEST)n >> 2;
1067   if (long_p == 0
1068       && (un >> (TARGET_INT_BIT - 2)) == 0)
1069     {
1070       high_bit = ((ULONGEST)1) << (TARGET_INT_BIT-1);
1071
1072       /* A large decimal (not hex or octal) constant (between INT_MAX
1073          and UINT_MAX) is a long or unsigned long, according to ANSI,
1074          never an unsigned int, but this code treats it as unsigned
1075          int.  This probably should be fixed.  GCC gives a warning on
1076          such constants.  */
1077
1078       unsigned_type = builtin_type_unsigned_int;
1079       signed_type = builtin_type_int;
1080     }
1081   else if (long_p <= 1
1082            && (un >> (TARGET_LONG_BIT - 2)) == 0)
1083     {
1084       high_bit = ((ULONGEST)1) << (TARGET_LONG_BIT-1);
1085       unsigned_type = builtin_type_unsigned_long;
1086       signed_type = builtin_type_long;
1087     }
1088   else
1089     {
1090       high_bit = (((ULONGEST)1)
1091                   << (TARGET_LONG_LONG_BIT - 32 - 1)
1092                   << 16
1093                   << 16);
1094       if (high_bit == 0)
1095         /* A long long does not fit in a LONGEST.  */
1096         high_bit =
1097           (ULONGEST)1 << (sizeof (LONGEST) * HOST_CHAR_BIT - 1);
1098       unsigned_type = builtin_type_unsigned_long_long;
1099       signed_type = builtin_type_long_long;
1100     }
1101
1102    putithere->typed_val_int.val = n;
1103
1104    /* If the high bit of the worked out type is set then this number
1105       has to be unsigned. */
1106
1107    if (unsigned_p || (n & high_bit)) 
1108      {
1109        putithere->typed_val_int.type = unsigned_type;
1110      }
1111    else 
1112      {
1113        putithere->typed_val_int.type = signed_type;
1114      }
1115
1116    return INT;
1117 }
1118
1119 struct token
1120 {
1121   char *operator;
1122   int token;
1123   enum exp_opcode opcode;
1124 };
1125
1126 static const struct token tokentab3[] =
1127   {
1128     {">>=", ASSIGN_MODIFY, BINOP_RSH},
1129     {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1130   };
1131
1132 static const struct token tokentab2[] =
1133   {
1134     {"+=", ASSIGN_MODIFY, BINOP_ADD},
1135     {"-=", ASSIGN_MODIFY, BINOP_SUB},
1136     {"*=", ASSIGN_MODIFY, BINOP_MUL},
1137     {"/=", ASSIGN_MODIFY, BINOP_DIV},
1138     {"%=", ASSIGN_MODIFY, BINOP_REM},
1139     {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1140     {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1141     {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1142     {"++", INCREMENT, BINOP_END},
1143     {"--", DECREMENT, BINOP_END},
1144     {"->", ARROW, BINOP_END},
1145     {"&&", ANDAND, BINOP_END},
1146     {"||", OROR, BINOP_END},
1147     {"::", COLONCOLON, BINOP_END},
1148     {"<<", LSH, BINOP_END},
1149     {">>", RSH, BINOP_END},
1150     {"==", EQUAL, BINOP_END},
1151     {"!=", NOTEQUAL, BINOP_END},
1152     {"<=", LEQ, BINOP_END},
1153     {">=", GEQ, BINOP_END}
1154   };
1155
1156 /* Read one token, getting characters through lexptr.  */
1157
1158 static int
1159 yylex ()
1160 {
1161   int c;
1162   int namelen;
1163   unsigned int i;
1164   char *tokstart;
1165   char *tokptr;
1166   int tempbufindex;
1167   static char *tempbuf;
1168   static int tempbufsize;
1169   
1170  retry:
1171
1172   tokstart = lexptr;
1173   /* See if it is a special token of length 3.  */
1174   for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1175     if (STREQN (tokstart, tokentab3[i].operator, 3))
1176       {
1177         lexptr += 3;
1178         yylval.opcode = tokentab3[i].opcode;
1179         return tokentab3[i].token;
1180       }
1181
1182   /* See if it is a special token of length 2.  */
1183   for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1184     if (STREQN (tokstart, tokentab2[i].operator, 2))
1185       {
1186         lexptr += 2;
1187         yylval.opcode = tokentab2[i].opcode;
1188         return tokentab2[i].token;
1189       }
1190
1191   switch (c = *tokstart)
1192     {
1193     case 0:
1194       return 0;
1195
1196     case ' ':
1197     case '\t':
1198     case '\n':
1199       lexptr++;
1200       goto retry;
1201
1202     case '\'':
1203       /* We either have a character constant ('0' or '\177' for example)
1204          or we have a quoted symbol reference ('foo(int,int)' in C++
1205          for example). */
1206       lexptr++;
1207       c = *lexptr++;
1208       if (c == '\\')
1209         c = parse_escape (&lexptr);
1210       else if (c == '\'')
1211         error ("Empty character constant.");
1212
1213       yylval.typed_val_int.val = c;
1214       yylval.typed_val_int.type = builtin_type_char;
1215
1216       c = *lexptr++;
1217       if (c != '\'')
1218         {
1219           namelen = skip_quoted (tokstart) - tokstart;
1220           if (namelen > 2)
1221             {
1222               lexptr = tokstart + namelen;
1223               if (lexptr[-1] != '\'')
1224                 error ("Unmatched single quote.");
1225               namelen -= 2;
1226               tokstart++;
1227               goto tryname;
1228             }
1229           error ("Invalid character constant.");
1230         }
1231       return INT;
1232
1233     case '(':
1234       paren_depth++;
1235       lexptr++;
1236       return c;
1237
1238     case ')':
1239       if (paren_depth == 0)
1240         return 0;
1241       paren_depth--;
1242       lexptr++;
1243       return c;
1244
1245     case ',':
1246       if (comma_terminates && paren_depth == 0)
1247         return 0;
1248       lexptr++;
1249       return c;
1250
1251     case '.':
1252       /* Might be a floating point number.  */
1253       if (lexptr[1] < '0' || lexptr[1] > '9')
1254         goto symbol;            /* Nope, must be a symbol. */
1255       /* FALL THRU into number case.  */
1256
1257     case '0':
1258     case '1':
1259     case '2':
1260     case '3':
1261     case '4':
1262     case '5':
1263     case '6':
1264     case '7':
1265     case '8':
1266     case '9':
1267       {
1268         /* It's a number.  */
1269         int got_dot = 0, got_e = 0, toktype;
1270         register char *p = tokstart;
1271         int hex = input_radix > 10;
1272
1273         if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1274           {
1275             p += 2;
1276             hex = 1;
1277           }
1278         else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1279           {
1280             p += 2;
1281             hex = 0;
1282           }
1283
1284         for (;; ++p)
1285           {
1286             /* This test includes !hex because 'e' is a valid hex digit
1287                and thus does not indicate a floating point number when
1288                the radix is hex.  */
1289             if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1290               got_dot = got_e = 1;
1291             /* This test does not include !hex, because a '.' always indicates
1292                a decimal floating point number regardless of the radix.  */
1293             else if (!got_dot && *p == '.')
1294               got_dot = 1;
1295             else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1296                      && (*p == '-' || *p == '+'))
1297               /* This is the sign of the exponent, not the end of the
1298                  number.  */
1299               continue;
1300             /* We will take any letters or digits.  parse_number will
1301                complain if past the radix, or if L or U are not final.  */
1302             else if ((*p < '0' || *p > '9')
1303                      && ((*p < 'a' || *p > 'z')
1304                                   && (*p < 'A' || *p > 'Z')))
1305               break;
1306           }
1307         toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1308         if (toktype == ERROR)
1309           {
1310             char *err_copy = (char *) alloca (p - tokstart + 1);
1311
1312             memcpy (err_copy, tokstart, p - tokstart);
1313             err_copy[p - tokstart] = 0;
1314             error ("Invalid number \"%s\".", err_copy);
1315           }
1316         lexptr = p;
1317         return toktype;
1318       }
1319
1320     case '+':
1321     case '-':
1322     case '*':
1323     case '/':
1324     case '%':
1325     case '|':
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     symbol:
1341       lexptr++;
1342       return c;
1343
1344     case '"':
1345
1346       /* Build the gdb internal form of the input string in tempbuf,
1347          translating any standard C escape forms seen.  Note that the
1348          buffer is null byte terminated *only* for the convenience of
1349          debugging gdb itself and printing the buffer contents when
1350          the buffer contains no embedded nulls.  Gdb does not depend
1351          upon the buffer being null byte terminated, it uses the length
1352          string instead.  This allows gdb to handle C strings (as well
1353          as strings in other languages) with embedded null bytes */
1354
1355       tokptr = ++tokstart;
1356       tempbufindex = 0;
1357
1358       do {
1359         /* Grow the static temp buffer if necessary, including allocating
1360            the first one on demand. */
1361         if (tempbufindex + 1 >= tempbufsize)
1362           {
1363             tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1364           }
1365         switch (*tokptr)
1366           {
1367           case '\0':
1368           case '"':
1369             /* Do nothing, loop will terminate. */
1370             break;
1371           case '\\':
1372             tokptr++;
1373             c = parse_escape (&tokptr);
1374             if (c == -1)
1375               {
1376                 continue;
1377               }
1378             tempbuf[tempbufindex++] = c;
1379             break;
1380           default:
1381             tempbuf[tempbufindex++] = *tokptr++;
1382             break;
1383           }
1384       } while ((*tokptr != '"') && (*tokptr != '\0'));
1385       if (*tokptr++ != '"')
1386         {
1387           error ("Unterminated string in expression.");
1388         }
1389       tempbuf[tempbufindex] = '\0';     /* See note above */
1390       yylval.sval.ptr = tempbuf;
1391       yylval.sval.length = tempbufindex;
1392       lexptr = tokptr;
1393       return (STRING);
1394     }
1395
1396   if (!(c == '_' || c == '$'
1397         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1398     /* We must have come across a bad character (e.g. ';').  */
1399     error ("Invalid character '%c' in expression.", c);
1400
1401   /* It's a name.  See how long it is.  */
1402   namelen = 0;
1403   for (c = tokstart[namelen];
1404        (c == '_' || c == '$' || (c >= '0' && c <= '9')
1405         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1406     {
1407       /* Template parameter lists are part of the name.
1408          FIXME: This mishandles `print $a<4&&$a>3'.  */
1409
1410       if (c == '<')
1411         {
1412           int i = namelen;
1413           int nesting_level = 1;
1414           while (tokstart[++i])
1415             {
1416               if (tokstart[i] == '<')
1417                 nesting_level++;
1418               else if (tokstart[i] == '>')
1419                 {
1420                   if (--nesting_level == 0)
1421                     break;
1422                 }
1423             }
1424           if (tokstart[i] == '>')
1425             namelen = i;
1426           else
1427             break;
1428         }
1429       c = tokstart[++namelen];
1430     }
1431
1432   /* The token "if" terminates the expression and is NOT 
1433      removed from the input stream.  */
1434   if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1435     {
1436       return 0;
1437     }
1438
1439   lexptr += namelen;
1440
1441   tryname:
1442
1443   /* Catch specific keywords.  Should be done with a data structure.  */
1444   switch (namelen)
1445     {
1446     case 8:
1447       if (STREQN (tokstart, "unsigned", 8))
1448         return UNSIGNED;
1449       if (current_language->la_language == language_cplus
1450           && STREQN (tokstart, "template", 8))
1451         return TEMPLATE;
1452       if (STREQN (tokstart, "volatile", 8))
1453         return VOLATILE_KEYWORD;
1454       break;
1455     case 6:
1456       if (STREQN (tokstart, "struct", 6))
1457         return STRUCT;
1458       if (STREQN (tokstart, "signed", 6))
1459         return SIGNED_KEYWORD;
1460       if (STREQN (tokstart, "sizeof", 6))      
1461         return SIZEOF;
1462       if (STREQN (tokstart, "double", 6))      
1463         return DOUBLE_KEYWORD;
1464       break;
1465     case 5:
1466       if (current_language->la_language == language_cplus
1467           && STREQN (tokstart, "class", 5))
1468         return CLASS;
1469       if (STREQN (tokstart, "union", 5))
1470         return UNION;
1471       if (STREQN (tokstart, "short", 5))
1472         return SHORT;
1473       if (STREQN (tokstart, "const", 5))
1474         return CONST_KEYWORD;
1475       break;
1476     case 4:
1477       if (STREQN (tokstart, "enum", 4))
1478         return ENUM;
1479       if (STREQN (tokstart, "long", 4))
1480         return LONG;
1481       if (current_language->la_language == language_cplus
1482           && STREQN (tokstart, "this", 4))
1483         {
1484           static const char this_name[] =
1485                                  { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1486
1487           if (lookup_symbol (this_name, expression_context_block,
1488                              VAR_NAMESPACE, (int *) NULL,
1489                              (struct symtab **) NULL))
1490             return THIS;
1491         }
1492       break;
1493     case 3:
1494       if (STREQN (tokstart, "int", 3))
1495         return INT_KEYWORD;
1496       break;
1497     default:
1498       break;
1499     }
1500
1501   yylval.sval.ptr = tokstart;
1502   yylval.sval.length = namelen;
1503
1504   if (*tokstart == '$')
1505     {
1506       write_dollar_variable (yylval.sval);
1507       return VARIABLE;
1508     }
1509
1510   /* Use token-type BLOCKNAME for symbols that happen to be defined as
1511      functions or symtabs.  If this is not so, then ...
1512      Use token-type TYPENAME for symbols that happen to be defined
1513      currently as names of types; NAME for other symbols.
1514      The caller is not constrained to care about the distinction.  */
1515   {
1516     char *tmp = copy_name (yylval.sval);
1517     struct symbol *sym;
1518     int is_a_field_of_this = 0;
1519     int hextype;
1520
1521     sym = lookup_symbol (tmp, expression_context_block,
1522                          VAR_NAMESPACE,
1523                          current_language->la_language == language_cplus
1524                          ? &is_a_field_of_this : (int *) NULL,
1525                          (struct symtab **) NULL);
1526     /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1527        no psymtabs (coff, xcoff, or some future change to blow away the
1528        psymtabs once once symbols are read).  */
1529     if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1530         lookup_symtab (tmp))
1531       {
1532         yylval.ssym.sym = sym;
1533         yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1534         return BLOCKNAME;
1535       }
1536     if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1537         {
1538 #if 1
1539           /* Despite the following flaw, we need to keep this code enabled.
1540              Because we can get called from check_stub_method, if we don't
1541              handle nested types then it screws many operations in any
1542              program which uses nested types.  */
1543           /* In "A::x", if x is a member function of A and there happens
1544              to be a type (nested or not, since the stabs don't make that
1545              distinction) named x, then this code incorrectly thinks we
1546              are dealing with nested types rather than a member function.  */
1547
1548           char *p;
1549           char *namestart;
1550           struct symbol *best_sym;
1551
1552           /* Look ahead to detect nested types.  This probably should be
1553              done in the grammar, but trying seemed to introduce a lot
1554              of shift/reduce and reduce/reduce conflicts.  It's possible
1555              that it could be done, though.  Or perhaps a non-grammar, but
1556              less ad hoc, approach would work well.  */
1557
1558           /* Since we do not currently have any way of distinguishing
1559              a nested type from a non-nested one (the stabs don't tell
1560              us whether a type is nested), we just ignore the
1561              containing type.  */
1562
1563           p = lexptr;
1564           best_sym = sym;
1565           while (1)
1566             {
1567               /* Skip whitespace.  */
1568               while (*p == ' ' || *p == '\t' || *p == '\n')
1569                 ++p;
1570               if (*p == ':' && p[1] == ':')
1571                 {
1572                   /* Skip the `::'.  */
1573                   p += 2;
1574                   /* Skip whitespace.  */
1575                   while (*p == ' ' || *p == '\t' || *p == '\n')
1576                     ++p;
1577                   namestart = p;
1578                   while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1579                          || (*p >= 'a' && *p <= 'z')
1580                          || (*p >= 'A' && *p <= 'Z'))
1581                     ++p;
1582                   if (p != namestart)
1583                     {
1584                       struct symbol *cur_sym;
1585                       /* As big as the whole rest of the expression, which is
1586                          at least big enough.  */
1587                       char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1588                       char *tmp1;
1589
1590                       tmp1 = ncopy;
1591                       memcpy (tmp1, tmp, strlen (tmp));
1592                       tmp1 += strlen (tmp);
1593                       memcpy (tmp1, "::", 2);
1594                       tmp1 += 2;
1595                       memcpy (tmp1, namestart, p - namestart);
1596                       tmp1[p - namestart] = '\0';
1597                       cur_sym = lookup_symbol (ncopy, expression_context_block,
1598                                                VAR_NAMESPACE, (int *) NULL,
1599                                                (struct symtab **) NULL);
1600                       if (cur_sym)
1601                         {
1602                           if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1603                             {
1604                               best_sym = cur_sym;
1605                               lexptr = p;
1606                             }
1607                           else
1608                             break;
1609                         }
1610                       else
1611                         break;
1612                     }
1613                   else
1614                     break;
1615                 }
1616               else
1617                 break;
1618             }
1619
1620           yylval.tsym.type = SYMBOL_TYPE (best_sym);
1621 #else /* not 0 */
1622           yylval.tsym.type = SYMBOL_TYPE (sym);
1623 #endif /* not 0 */
1624           return TYPENAME;
1625         }
1626     if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1627         return TYPENAME;
1628
1629     /* Input names that aren't symbols but ARE valid hex numbers,
1630        when the input radix permits them, can be names or numbers
1631        depending on the parse.  Note we support radixes > 16 here.  */
1632     if (!sym && 
1633         ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1634          (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1635       {
1636         YYSTYPE newlval;        /* Its value is ignored.  */
1637         hextype = parse_number (tokstart, namelen, 0, &newlval);
1638         if (hextype == INT)
1639           {
1640             yylval.ssym.sym = sym;
1641             yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1642             return NAME_OR_INT;
1643           }
1644       }
1645
1646     /* Any other kind of symbol */
1647     yylval.ssym.sym = sym;
1648     yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1649     return NAME;
1650   }
1651 }
1652
1653 void
1654 yyerror (msg)
1655      char *msg;
1656 {
1657   error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
1658 }