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