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