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