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