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