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