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