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