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