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