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