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