* gdb.texinfo (Packets): Fix typos "alligned".
[deliverable/binutils-gdb.git] / gdb / objc-exp.y
1 /* YACC parser for C expressions, for GDB.
2
3 Copyright 1986, 1989, 1990, 1991, 1993, 1994, 2002 Free Software
4 Foundation, Inc.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 /* Parse a C expression from text in a string, and return the result
22 as a struct expression pointer. That structure contains arithmetic
23 operations in reverse polish, with constants represented by
24 operations that are followed by special data. See expression.h for
25 the details of the format. What is important here is that it can
26 be built up sequentially during the process of parsing; the lower
27 levels of the tree always come first in the result.
28
29 Note that malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the
32 parser generator. Doing this with #defines and trying to control
33 the interaction with include files (<malloc.h> and <stdlib.h> for
34 example) just became too messy, particularly when such includes can
35 be inserted at random times by the parser generator. */
36
37 %{
38
39 #include "defs.h"
40 #include "gdb_string.h"
41 #include <ctype.h>
42 #include "expression.h"
43
44 #include "objc-lang.h" /* For objc language constructs. */
45
46 #include "value.h"
47 #include "parser-defs.h"
48 #include "language.h"
49 #include "c-lang.h"
50 #include "bfd.h" /* Required by objfiles.h. */
51 #include "symfile.h" /* Required by objfiles.h. */
52 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols. */
53 #include "top.h"
54 #include "completer.h" /* For skip_quoted(). */
55
56 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
57 etc), as well as gratuitiously global symbol names, so we can have
58 multiple yacc generated parsers in gdb. Note that these are only
59 the variables produced by yacc. If other parser generators (bison,
60 byacc, etc) produce additional global names that conflict at link
61 time, then those parser generators need to be fixed instead of
62 adding those names to this list. */
63
64 #define yymaxdepth objc_maxdepth
65 #define yyparse objc_parse
66 #define yylex objc_lex
67 #define yyerror objc_error
68 #define yylval objc_lval
69 #define yychar objc_char
70 #define yydebug objc_debug
71 #define yypact objc_pact
72 #define yyr1 objc_r1
73 #define yyr2 objc_r2
74 #define yydef objc_def
75 #define yychk objc_chk
76 #define yypgo objc_pgo
77 #define yyact objc_act
78 #define yyexca objc_exca
79 #define yyerrflag objc_errflag
80 #define yynerrs objc_nerrs
81 #define yyps objc_ps
82 #define yypv objc_pv
83 #define yys objc_s
84 #define yy_yys objc_yys
85 #define yystate objc_state
86 #define yytmp objc_tmp
87 #define yyv objc_v
88 #define yy_yyv objc_yyv
89 #define yyval objc_val
90 #define yylloc objc_lloc
91 #define yyreds objc_reds /* With YYDEBUG defined */
92 #define yytoks objc_toks /* With YYDEBUG defined */
93 #define yyname objc_name /* With YYDEBUG defined */
94 #define yyrule objc_rule /* With YYDEBUG defined */
95 #define yylhs objc_yylhs
96 #define yylen objc_yylen
97 #define yydefred objc_yydefred
98 #define yydgoto objc_yydgoto
99 #define yysindex objc_yysindex
100 #define yyrindex objc_yyrindex
101 #define yygindex objc_yygindex
102 #define yytable objc_yytable
103 #define yycheck objc_yycheck
104
105 #ifndef YYDEBUG
106 #define YYDEBUG 0 /* Default to no yydebug support. */
107 #endif
108
109 int
110 yyparse PARAMS ((void));
111
112 static int
113 yylex PARAMS ((void));
114
115 void
116 yyerror PARAMS ((char *));
117
118 %}
119
120 /* Although the yacc "value" of an expression is not used,
121 since the result is stored in the structure being created,
122 other node types do have values. */
123
124 %union
125 {
126 LONGEST lval;
127 struct {
128 LONGEST val;
129 struct type *type;
130 } typed_val_int;
131 struct {
132 DOUBLEST dval;
133 struct type *type;
134 } typed_val_float;
135 struct symbol *sym;
136 struct type *tval;
137 struct stoken sval;
138 struct ttype tsym;
139 struct symtoken ssym;
140 int voidval;
141 struct block *bval;
142 enum exp_opcode opcode;
143 struct internalvar *ivar;
144 struct objc_class_str class;
145
146 struct type **tvec;
147 int *ivec;
148 }
149
150 %{
151 /* YYSTYPE gets defined by %union. */
152 static int
153 parse_number PARAMS ((char *, int, int, YYSTYPE *));
154 %}
155
156 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
157 %type <lval> rcurly
158 %type <tval> type typebase
159 %type <tvec> nonempty_typelist
160 /* %type <bval> block */
161
162 /* Fancy type parsing. */
163 %type <voidval> func_mod direct_abs_decl abs_decl
164 %type <tval> ptype
165 %type <lval> array_mod
166
167 %token <typed_val_int> INT
168 %token <typed_val_float> FLOAT
169
170 /* Both NAME and TYPENAME tokens represent symbols in the input, and
171 both convey their data as strings. But a TYPENAME is a string that
172 happens to be defined as a typedef or builtin type name (such as
173 int or char) and a NAME is any other symbol. Contexts where this
174 distinction is not important can use the nonterminal "name", which
175 matches either NAME or TYPENAME. */
176
177 %token <sval> STRING
178 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
179 %token <sval> SELECTOR /* ObjC "@selector" pseudo-operator */
180 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
181 %token <tsym> TYPENAME
182 %token <class> CLASSNAME /* ObjC Class name */
183 %type <sval> name
184 %type <ssym> name_not_typename
185 %type <tsym> typename
186
187 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
188 but which would parse as a valid number in the current input radix.
189 E.g. "c" when input_radix==16. Depending on the parse, it will be
190 turned into a name or into a number. */
191
192 %token <ssym> NAME_OR_INT
193
194 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
195 %token TEMPLATE
196 %token ERROR
197
198 /* Special type cases, put in to allow the parser to distinguish
199 different legal basetypes. */
200 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
201
202 %token <voidval> VARIABLE
203
204 %token <opcode> ASSIGN_MODIFY
205
206 /* C++ */
207 %token THIS
208
209 %left ','
210 %left ABOVE_COMMA
211 %right '=' ASSIGN_MODIFY
212 %right '?'
213 %left OROR
214 %left ANDAND
215 %left '|'
216 %left '^'
217 %left '&'
218 %left EQUAL NOTEQUAL
219 %left '<' '>' LEQ GEQ
220 %left LSH RSH
221 %left '@'
222 %left '+' '-'
223 %left '*' '/' '%'
224 %right UNARY INCREMENT DECREMENT
225 %right ARROW '.' '[' '('
226 %token <ssym> BLOCKNAME
227 %type <bval> block
228 %left COLONCOLON
229
230 \f
231 %%
232
233 start : exp1
234 | type_exp
235 ;
236
237 type_exp: type
238 { write_exp_elt_opcode(OP_TYPE);
239 write_exp_elt_type($1);
240 write_exp_elt_opcode(OP_TYPE);}
241 ;
242
243 /* Expressions, including the comma operator. */
244 exp1 : exp
245 | exp1 ',' exp
246 { write_exp_elt_opcode (BINOP_COMMA); }
247 ;
248
249 /* Expressions, not including the comma operator. */
250 exp : '*' exp %prec UNARY
251 { write_exp_elt_opcode (UNOP_IND); }
252
253 exp : '&' exp %prec UNARY
254 { write_exp_elt_opcode (UNOP_ADDR); }
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 (builtin_type_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_MSGCALL);
348 end_msglist();
349 write_exp_elt_opcode (OP_MSGCALL);
350 }
351 ;
352
353 exp : '[' CLASSNAME
354 {
355 write_exp_elt_opcode (OP_LONG);
356 write_exp_elt_type (builtin_type_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_MSGCALL);
363 end_msglist();
364 write_exp_elt_opcode (OP_MSGCALL);
365 }
366 ;
367
368 exp : '[' exp
369 { start_msglist(); }
370 msglist ']'
371 { write_exp_elt_opcode (OP_MSGCALL);
372 end_msglist();
373 write_exp_elt_opcode (OP_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_SELECTOR);
572 write_exp_string ($1);
573 write_exp_elt_opcode (OP_SELECTOR); }
574
575 exp : SIZEOF '(' type ')' %prec UNARY
576 { write_exp_elt_opcode (OP_LONG);
577 write_exp_elt_type (builtin_type_int);
578 CHECK_TYPEDEF ($3);
579 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
580 write_exp_elt_opcode (OP_LONG); }
581 ;
582
583 exp : STRING
584 { /* C strings are converted into array
585 constants with an explicit null byte
586 added at the end. Thus the array upper
587 bound is the string length. There is no
588 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
802 qualifier before the type is currently handled in the
803 typebase rule. The reason for recognizing these here
804 (shift/reduce conflicts) might be obsolete now that some
805 pointer to member rules have 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
861 never 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
934 qualifier after the type is handled in the ptype rule. I
935 think these could 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
984 just a fake for "variable", so these cause reduce/reduce conflicts
985 because the parser can't tell whether NAME_OR_INT is a
986 name_not_typename (=variable, =exp) or just an exp. If
987 name_not_typename was ever used in an lvalue context where only a
988 name could occur, this might be useful. */
989 | NAME_OR_INT */
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. LEN is the
996 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
1008 values here, and we do kind of silly things like cast to
1009 unsigned. */
1010 register LONGEST n = 0;
1011 register LONGEST prevn = 0;
1012 unsigned LONGEST un;
1013
1014 register int i = 0;
1015 register int c;
1016 register int base = input_radix;
1017 int unsigned_p = 0;
1018
1019 /* Number of "L" suffixes encountered. */
1020 int long_p = 0;
1021
1022 /* We have found a "L" or "U" suffix. */
1023 int found_suffix = 0;
1024
1025 unsigned LONGEST high_bit;
1026 struct type *signed_type;
1027 struct type *unsigned_type;
1028
1029 if (parsed_float)
1030 {
1031 char c;
1032
1033 /* It's a float since it contains a point or an exponent. */
1034
1035 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
1036 sscanf (p, "%g", &putithere->typed_val_float.dval);
1037 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
1038 sscanf (p, "%lg", &putithere->typed_val_float.dval);
1039 else
1040 {
1041 #ifdef PRINTF_HAS_LONG_DOUBLE
1042 sscanf (p, "%Lg", &putithere->typed_val_float.dval);
1043 #else
1044 /* Scan it into a double, then assign it to the long double.
1045 This at least wins with values representable in the range
1046 of doubles. */
1047 double temp;
1048 sscanf (p, "%lg", &temp);
1049 putithere->typed_val_float.dval = temp;
1050 #endif
1051 }
1052
1053 /* See if it has `f' or `l' suffix (float or long double). */
1054
1055 c = tolower (p[len - 1]);
1056
1057 if (c == 'f')
1058 putithere->typed_val_float.type = builtin_type_float;
1059 else if (c == 'l')
1060 putithere->typed_val_float.type = builtin_type_long_double;
1061 else if (isdigit (c) || c == '.')
1062 putithere->typed_val_float.type = builtin_type_double;
1063 else
1064 return ERROR;
1065
1066 return FLOAT;
1067 }
1068
1069 /* Handle base-switching prefixes 0x, 0t, 0d, and 0. */
1070 if (p[0] == '0')
1071 switch (p[1])
1072 {
1073 case 'x':
1074 case 'X':
1075 if (len >= 3)
1076 {
1077 p += 2;
1078 base = 16;
1079 len -= 2;
1080 }
1081 break;
1082
1083 case 't':
1084 case 'T':
1085 case 'd':
1086 case 'D':
1087 if (len >= 3)
1088 {
1089 p += 2;
1090 base = 10;
1091 len -= 2;
1092 }
1093 break;
1094
1095 default:
1096 base = 8;
1097 break;
1098 }
1099
1100 while (len-- > 0)
1101 {
1102 c = *p++;
1103 if (c >= 'A' && c <= 'Z')
1104 c += 'a' - 'A';
1105 if (c != 'l' && c != 'u')
1106 n *= base;
1107 if (c >= '0' && c <= '9')
1108 {
1109 if (found_suffix)
1110 return ERROR;
1111 n += i = c - '0';
1112 }
1113 else
1114 {
1115 if (base > 10 && c >= 'a' && c <= 'f')
1116 {
1117 if (found_suffix)
1118 return ERROR;
1119 n += i = c - 'a' + 10;
1120 }
1121 else if (c == 'l')
1122 {
1123 ++long_p;
1124 found_suffix = 1;
1125 }
1126 else if (c == 'u')
1127 {
1128 unsigned_p = 1;
1129 found_suffix = 1;
1130 }
1131 else
1132 return ERROR; /* Char not a digit. */
1133 }
1134 if (i >= base)
1135 return ERROR; /* Invalid digit in this base. */
1136
1137 /* Portably test for overflow (only works for nonzero values, so
1138 make a second check for zero). FIXME: Can't we just make n
1139 and prevn unsigned and avoid this? */
1140 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1141 unsigned_p = 1; /* Try something unsigned. */
1142
1143 /* Portably test for unsigned overflow.
1144 FIXME: This check is wrong; for example it doesn't find
1145 overflow on 0x123456789 when LONGEST is 32 bits. */
1146 if (c != 'l' && c != 'u' && n != 0)
1147 {
1148 if ((unsigned_p && (unsigned LONGEST) prevn >= (unsigned LONGEST) n))
1149 error ("Numeric constant too large.");
1150 }
1151 prevn = n;
1152 }
1153
1154 /* An integer constant is an int, a long, or a long long. An L
1155 suffix forces it to be long; an LL suffix forces it to be long
1156 long. If not forced to a larger size, it gets the first type of
1157 the above that it fits in. To figure out whether it fits, we
1158 shift it right and see whether anything remains. Note that we
1159 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1160 operation, because many compilers will warn about such a shift
1161 (which always produces a zero result). Sometimes TARGET_INT_BIT
1162 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
1163 the case where it is we just always shift the value more than
1164 once, with fewer bits each time. */
1165
1166 un = (unsigned LONGEST)n >> 2;
1167 if (long_p == 0
1168 && (un >> (TARGET_INT_BIT - 2)) == 0)
1169 {
1170 high_bit = ((unsigned LONGEST)1) << (TARGET_INT_BIT-1);
1171
1172 /* A large decimal (not hex or octal) constant (between INT_MAX
1173 and UINT_MAX) is a long or unsigned long, according to ANSI,
1174 never an unsigned int, but this code treats it as unsigned
1175 int. This probably should be fixed. GCC gives a warning on
1176 such constants. */
1177
1178 unsigned_type = builtin_type_unsigned_int;
1179 signed_type = builtin_type_int;
1180 }
1181 else if (long_p <= 1
1182 && (un >> (TARGET_LONG_BIT - 2)) == 0)
1183 {
1184 high_bit = ((unsigned LONGEST)1) << (TARGET_LONG_BIT-1);
1185 unsigned_type = builtin_type_unsigned_long;
1186 signed_type = builtin_type_long;
1187 }
1188 else
1189 {
1190 high_bit = (((unsigned LONGEST)1)
1191 << (TARGET_LONG_LONG_BIT - 32 - 1)
1192 << 16
1193 << 16);
1194 if (high_bit == 0)
1195 /* A long long does not fit in a LONGEST. */
1196 high_bit =
1197 (unsigned LONGEST)1 << (sizeof (LONGEST) * HOST_CHAR_BIT - 1);
1198 unsigned_type = builtin_type_unsigned_long_long;
1199 signed_type = builtin_type_long_long;
1200 }
1201
1202 putithere->typed_val_int.val = n;
1203
1204 /* If the high bit of the worked out type is set then this number
1205 has to be unsigned. */
1206
1207 if (unsigned_p || (n & high_bit))
1208 {
1209 putithere->typed_val_int.type = unsigned_type;
1210 }
1211 else
1212 {
1213 putithere->typed_val_int.type = signed_type;
1214 }
1215
1216 return INT;
1217 }
1218
1219 struct token
1220 {
1221 char *operator;
1222 int token;
1223 enum exp_opcode opcode;
1224 };
1225
1226 static const struct token tokentab3[] =
1227 {
1228 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1229 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1230 };
1231
1232 static const struct token tokentab2[] =
1233 {
1234 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1235 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1236 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1237 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1238 {"%=", ASSIGN_MODIFY, BINOP_REM},
1239 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1240 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1241 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1242 {"++", INCREMENT, BINOP_END},
1243 {"--", DECREMENT, BINOP_END},
1244 {"->", ARROW, BINOP_END},
1245 {"&&", ANDAND, BINOP_END},
1246 {"||", OROR, BINOP_END},
1247 {"::", COLONCOLON, BINOP_END},
1248 {"<<", LSH, BINOP_END},
1249 {">>", RSH, BINOP_END},
1250 {"==", EQUAL, BINOP_END},
1251 {"!=", NOTEQUAL, BINOP_END},
1252 {"<=", LEQ, BINOP_END},
1253 {">=", GEQ, BINOP_END}
1254 };
1255
1256 /* Read one token, getting characters through lexptr. */
1257
1258 static int
1259 yylex ()
1260 {
1261 int c, tokchr;
1262 int namelen;
1263 unsigned int i;
1264 char *tokstart;
1265 char *tokptr;
1266 int tempbufindex;
1267 static char *tempbuf;
1268 static int tempbufsize;
1269
1270 retry:
1271
1272 tokstart = lexptr;
1273 /* See if it is a special token of length 3. */
1274 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1275 if (STREQN (tokstart, tokentab3[i].operator, 3))
1276 {
1277 lexptr += 3;
1278 yylval.opcode = tokentab3[i].opcode;
1279 return tokentab3[i].token;
1280 }
1281
1282 /* See if it is a special token of length 2. */
1283 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1284 if (STREQN (tokstart, tokentab2[i].operator, 2))
1285 {
1286 lexptr += 2;
1287 yylval.opcode = tokentab2[i].opcode;
1288 return tokentab2[i].token;
1289 }
1290
1291 switch (tokchr = *tokstart)
1292 {
1293 case 0:
1294 return 0;
1295
1296 case ' ':
1297 case '\t':
1298 case '\n':
1299 lexptr++;
1300 goto retry;
1301
1302 case '\'':
1303 /* We either have a character constant ('0' or '\177' for
1304 example) or we have a quoted symbol reference ('foo(int,int)'
1305 in C++ for example). */
1306 lexptr++;
1307 c = *lexptr++;
1308 if (c == '\\')
1309 c = parse_escape (&lexptr);
1310 else if (c == '\'')
1311 error ("Empty character constant.");
1312
1313 yylval.typed_val_int.val = c;
1314 yylval.typed_val_int.type = builtin_type_char;
1315
1316 c = *lexptr++;
1317 if (c != '\'')
1318 {
1319 namelen = skip_quoted (tokstart,
1320 get_gdb_completer_word_break_characters())
1321 - tokstart;
1322 if (namelen > 2)
1323 {
1324 lexptr = tokstart + namelen;
1325 if (lexptr[-1] != '\'')
1326 error ("Unmatched single quote.");
1327 namelen -= 2;
1328 tokstart++;
1329 goto tryname;
1330 }
1331 error ("Invalid character constant.");
1332 }
1333 return INT;
1334
1335 case '(':
1336 paren_depth++;
1337 lexptr++;
1338 return '(';
1339
1340 case ')':
1341 if (paren_depth == 0)
1342 return 0;
1343 paren_depth--;
1344 lexptr++;
1345 return ')';
1346
1347 case ',':
1348 if (comma_terminates && paren_depth == 0)
1349 return 0;
1350 lexptr++;
1351 return ',';
1352
1353 case '.':
1354 /* Might be a floating point number. */
1355 if (lexptr[1] < '0' || lexptr[1] > '9')
1356 goto symbol; /* Nope, must be a symbol. */
1357 /* FALL THRU into number case. */
1358
1359 case '0':
1360 case '1':
1361 case '2':
1362 case '3':
1363 case '4':
1364 case '5':
1365 case '6':
1366 case '7':
1367 case '8':
1368 case '9':
1369 {
1370 /* It's a number. */
1371 int got_dot = 0, got_e = 0, toktype = FLOAT;
1372 /* Initialize toktype to anything other than ERROR. */
1373 register char *p = tokstart;
1374 int hex = input_radix > 10;
1375 int local_radix = input_radix;
1376 if (tokchr == '0' && (p[1] == 'x' || p[1] == 'X'))
1377 {
1378 p += 2;
1379 hex = 1;
1380 local_radix = 16;
1381 }
1382 else if (tokchr == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1383 {
1384 p += 2;
1385 hex = 0;
1386 local_radix = 10;
1387 }
1388
1389 for (;; ++p)
1390 {
1391 /* This test includes !hex because 'e' is a valid hex digit
1392 and thus does not indicate a floating point number when
1393 the radix is hex. */
1394
1395 if (!hex && (*p == 'e' || *p == 'E'))
1396 if (got_e)
1397 toktype = ERROR; /* Only one 'e' in a float. */
1398 else
1399 got_e = 1;
1400 /* This test does not include !hex, because a '.' always
1401 indicates a decimal floating point number regardless of
1402 the radix. */
1403 else if (*p == '.')
1404 if (got_dot)
1405 toktype = ERROR; /* Only one '.' in a float. */
1406 else
1407 got_dot = 1;
1408 else if (got_e && (p[-1] == 'e' || p[-1] == 'E') &&
1409 (*p == '-' || *p == '+'))
1410 /* This is the sign of the exponent, not the end of the
1411 number. */
1412 continue;
1413 /* Always take decimal digits; parse_number handles radix
1414 error. */
1415 else if (*p >= '0' && *p <= '9')
1416 continue;
1417 /* We will take letters only if hex is true, and only up
1418 to what the input radix would permit. FSF was content
1419 to rely on parse_number to validate; but it leaks. */
1420 else if (*p >= 'a' && *p <= 'z')
1421 {
1422 if (!hex || *p >= ('a' + local_radix - 10))
1423 toktype = ERROR;
1424 }
1425 else if (*p >= 'A' && *p <= 'Z')
1426 {
1427 if (!hex || *p >= ('A' + local_radix - 10))
1428 toktype = ERROR;
1429 }
1430 else break;
1431 }
1432 if (toktype != ERROR)
1433 toktype = parse_number (tokstart, p - tokstart,
1434 got_dot | got_e, &yylval);
1435 if (toktype == ERROR)
1436 {
1437 char *err_copy = (char *) alloca (p - tokstart + 1);
1438
1439 memcpy (err_copy, tokstart, p - tokstart);
1440 err_copy[p - tokstart] = 0;
1441 error ("Invalid number \"%s\".", err_copy);
1442 }
1443 lexptr = p;
1444 return toktype;
1445 }
1446
1447 case '+':
1448 case '-':
1449 case '*':
1450 case '/':
1451 case '%':
1452 case '|':
1453 case '&':
1454 case '^':
1455 case '~':
1456 case '!':
1457 #if 0
1458 case '@': /* Moved out below. */
1459 #endif
1460 case '<':
1461 case '>':
1462 case '[':
1463 case ']':
1464 case '?':
1465 case ':':
1466 case '=':
1467 case '{':
1468 case '}':
1469 symbol:
1470 lexptr++;
1471 return tokchr;
1472
1473 case '@':
1474 if (strncmp(tokstart, "@selector", 9) == 0)
1475 {
1476 tokptr = strchr(tokstart, '(');
1477 if (tokptr == NULL)
1478 {
1479 error ("Missing '(' in @selector(...)");
1480 }
1481 tempbufindex = 0;
1482 tokptr++; /* Skip the '('. */
1483 do {
1484 /* Grow the static temp buffer if necessary, including
1485 allocating the first one on demand. */
1486 if (tempbufindex + 1 >= tempbufsize)
1487 {
1488 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1489 }
1490 tempbuf[tempbufindex++] = *tokptr++;
1491 } while ((*tokptr != ')') && (*tokptr != '\0'));
1492 if (*tokptr++ != ')')
1493 {
1494 error ("Missing ')' in @selector(...)");
1495 }
1496 tempbuf[tempbufindex] = '\0';
1497 yylval.sval.ptr = tempbuf;
1498 yylval.sval.length = tempbufindex;
1499 lexptr = tokptr;
1500 return SELECTOR;
1501 }
1502 if (tokstart[1] != '"')
1503 {
1504 lexptr++;
1505 return tokchr;
1506 }
1507 /* ObjC NextStep NSString constant: fall thru and parse like
1508 STRING. */
1509 tokstart++;
1510
1511 case '"':
1512
1513 /* Build the gdb internal form of the input string in tempbuf,
1514 translating any standard C escape forms seen. Note that the
1515 buffer is null byte terminated *only* for the convenience of
1516 debugging gdb itself and printing the buffer contents when
1517 the buffer contains no embedded nulls. Gdb does not depend
1518 upon the buffer being null byte terminated, it uses the
1519 length string instead. This allows gdb to handle C strings
1520 (as well as strings in other languages) with embedded null
1521 bytes. */
1522
1523 tokptr = ++tokstart;
1524 tempbufindex = 0;
1525
1526 do {
1527 /* Grow the static temp buffer if necessary, including
1528 allocating the first one on demand. */
1529 if (tempbufindex + 1 >= tempbufsize)
1530 {
1531 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1532 }
1533 switch (*tokptr)
1534 {
1535 case '\0':
1536 case '"':
1537 /* Do nothing, loop will terminate. */
1538 break;
1539 case '\\':
1540 tokptr++;
1541 c = parse_escape (&tokptr);
1542 if (c == -1)
1543 {
1544 continue;
1545 }
1546 tempbuf[tempbufindex++] = c;
1547 break;
1548 default:
1549 tempbuf[tempbufindex++] = *tokptr++;
1550 break;
1551 }
1552 } while ((*tokptr != '"') && (*tokptr != '\0'));
1553 if (*tokptr++ != '"')
1554 {
1555 error ("Unterminated string in expression.");
1556 }
1557 tempbuf[tempbufindex] = '\0'; /* See note above. */
1558 yylval.sval.ptr = tempbuf;
1559 yylval.sval.length = tempbufindex;
1560 lexptr = tokptr;
1561 return (tokchr == '@' ? NSSTRING : STRING);
1562 }
1563
1564 if (!(tokchr == '_' || tokchr == '$' ||
1565 (tokchr >= 'a' && tokchr <= 'z') || (tokchr >= 'A' && tokchr <= 'Z')))
1566 /* We must have come across a bad character (e.g. ';'). */
1567 error ("Invalid character '%c' in expression.", c);
1568
1569 /* It's a name. See how long it is. */
1570 namelen = 0;
1571 for (c = tokstart[namelen];
1572 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1573 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1574 {
1575 if (c == '<')
1576 {
1577 int i = namelen;
1578 while (tokstart[++i] && tokstart[i] != '>');
1579 if (tokstart[i] == '>')
1580 namelen = i;
1581 }
1582 c = tokstart[++namelen];
1583 }
1584
1585 /* The token "if" terminates the expression and is NOT
1586 removed from the input stream. */
1587 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1588 {
1589 return 0;
1590 }
1591
1592 lexptr += namelen;
1593
1594 tryname:
1595
1596 /* Catch specific keywords. Should be done with a data structure. */
1597 switch (namelen)
1598 {
1599 case 8:
1600 if (STREQN (tokstart, "unsigned", 8))
1601 return UNSIGNED;
1602 if (current_language->la_language == language_cplus
1603 && STREQN (tokstart, "template", 8))
1604 return TEMPLATE;
1605 if (STREQN (tokstart, "volatile", 8))
1606 return VOLATILE_KEYWORD;
1607 break;
1608 case 6:
1609 if (STREQN (tokstart, "struct", 6))
1610 return STRUCT;
1611 if (STREQN (tokstart, "signed", 6))
1612 return SIGNED_KEYWORD;
1613 if (STREQN (tokstart, "sizeof", 6))
1614 return SIZEOF;
1615 if (STREQN (tokstart, "double", 6))
1616 return DOUBLE_KEYWORD;
1617 break;
1618 case 5:
1619 if ((current_language->la_language == language_cplus)
1620 && STREQN (tokstart, "class", 5))
1621 return CLASS;
1622 if (STREQN (tokstart, "union", 5))
1623 return UNION;
1624 if (STREQN (tokstart, "short", 5))
1625 return SHORT;
1626 if (STREQN (tokstart, "const", 5))
1627 return CONST_KEYWORD;
1628 break;
1629 case 4:
1630 if (STREQN (tokstart, "enum", 4))
1631 return ENUM;
1632 if (STREQN (tokstart, "long", 4))
1633 return LONG;
1634 if (current_language->la_language == language_cplus
1635 && STREQN (tokstart, "this", 4))
1636 {
1637 static const char this_name[] = {
1638 CPLUS_MARKER, 't', 'h', 'i', 's', '\0'
1639 };
1640
1641 if (lookup_symbol (this_name, expression_context_block,
1642 VAR_NAMESPACE, (int *) NULL,
1643 (struct symtab **) NULL))
1644 return THIS;
1645 }
1646 break;
1647 case 3:
1648 if (STREQN (tokstart, "int", 3))
1649 return INT_KEYWORD;
1650 break;
1651 default:
1652 break;
1653 }
1654
1655 yylval.sval.ptr = tokstart;
1656 yylval.sval.length = namelen;
1657
1658 if (*tokstart == '$')
1659 {
1660 write_dollar_variable (yylval.sval);
1661 return VARIABLE;
1662 }
1663
1664 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1665 functions or symtabs. If this is not so, then ...
1666 Use token-type TYPENAME for symbols that happen to be defined
1667 currently as names of types; NAME for other symbols.
1668 The caller is not constrained to care about the distinction. */
1669 {
1670 char *tmp = copy_name (yylval.sval);
1671 struct symbol *sym;
1672 int is_a_field_of_this = 0, *need_this;
1673 int hextype;
1674
1675 if (current_language->la_language == language_cplus ||
1676 current_language->la_language == language_objc)
1677 need_this = &is_a_field_of_this;
1678 else
1679 need_this = (int *) NULL;
1680
1681 sym = lookup_symbol (tmp, expression_context_block,
1682 VAR_NAMESPACE,
1683 need_this,
1684 (struct symtab **) NULL);
1685 /* Call lookup_symtab, not lookup_partial_symtab, in case there
1686 are no psymtabs (coff, xcoff, or some future change to blow
1687 away the psymtabs once symbols are read). */
1688 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1689 lookup_symtab (tmp))
1690 {
1691 yylval.ssym.sym = sym;
1692 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1693 return BLOCKNAME;
1694 }
1695 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1696 {
1697 #if 1
1698 /* Despite the following flaw, we need to keep this code
1699 enabled. Because we can get called from
1700 check_stub_method, if we don't handle nested types then
1701 it screws many operations in any program which uses
1702 nested types. */
1703 /* In "A::x", if x is a member function of A and there
1704 happens to be a type (nested or not, since the stabs
1705 don't make that distinction) named x, then this code
1706 incorrectly thinks we are dealing with nested types
1707 rather than a member function. */
1708
1709 char *p;
1710 char *namestart;
1711 struct symbol *best_sym;
1712
1713 /* Look ahead to detect nested types. This probably should
1714 be done in the grammar, but trying seemed to introduce a
1715 lot of shift/reduce and reduce/reduce conflicts. It's
1716 possible that it could be done, though. Or perhaps a
1717 non-grammar, but less ad hoc, approach would work well. */
1718
1719 /* Since we do not currently have any way of distinguishing
1720 a nested type from a non-nested one (the stabs don't tell
1721 us whether a type is nested), we just ignore the
1722 containing type. */
1723
1724 p = lexptr;
1725 best_sym = sym;
1726 while (1)
1727 {
1728 /* Skip whitespace. */
1729 while (*p == ' ' || *p == '\t' || *p == '\n')
1730 ++p;
1731 if (*p == ':' && p[1] == ':')
1732 {
1733 /* Skip the `::'. */
1734 p += 2;
1735 /* Skip whitespace. */
1736 while (*p == ' ' || *p == '\t' || *p == '\n')
1737 ++p;
1738 namestart = p;
1739 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1740 || (*p >= 'a' && *p <= 'z')
1741 || (*p >= 'A' && *p <= 'Z'))
1742 ++p;
1743 if (p != namestart)
1744 {
1745 struct symbol *cur_sym;
1746 /* As big as the whole rest of the expression,
1747 which is at least big enough. */
1748 char *ncopy = alloca (strlen (tmp) +
1749 strlen (namestart) + 3);
1750 char *tmp1;
1751
1752 tmp1 = ncopy;
1753 memcpy (tmp1, tmp, strlen (tmp));
1754 tmp1 += strlen (tmp);
1755 memcpy (tmp1, "::", 2);
1756 tmp1 += 2;
1757 memcpy (tmp1, namestart, p - namestart);
1758 tmp1[p - namestart] = '\0';
1759 cur_sym = lookup_symbol (ncopy,
1760 expression_context_block,
1761 VAR_NAMESPACE, (int *) NULL,
1762 (struct symtab **) NULL);
1763 if (cur_sym)
1764 {
1765 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1766 {
1767 best_sym = cur_sym;
1768 lexptr = p;
1769 }
1770 else
1771 break;
1772 }
1773 else
1774 break;
1775 }
1776 else
1777 break;
1778 }
1779 else
1780 break;
1781 }
1782
1783 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1784 #else /* not 0 */
1785 yylval.tsym.type = SYMBOL_TYPE (sym);
1786 #endif /* not 0 */
1787 return TYPENAME;
1788 }
1789 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1790 return TYPENAME;
1791
1792 /* See if it's an ObjC classname. */
1793 if (!sym)
1794 {
1795 CORE_ADDR Class = lookup_objc_class(tmp);
1796 if (Class)
1797 {
1798 extern struct symbol *lookup_struct_typedef();
1799 yylval.class.class = Class;
1800 if ((sym = lookup_struct_typedef (tmp,
1801 expression_context_block,
1802 1)))
1803 yylval.class.type = SYMBOL_TYPE (sym);
1804 return CLASSNAME;
1805 }
1806 }
1807
1808 /* Input names that aren't symbols but ARE valid hex numbers,
1809 when the input radix permits them, can be names or numbers
1810 depending on the parse. Note we support radixes > 16 here. */
1811 if (!sym &&
1812 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1813 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1814 {
1815 YYSTYPE newlval; /* Its value is ignored. */
1816 hextype = parse_number (tokstart, namelen, 0, &newlval);
1817 if (hextype == INT)
1818 {
1819 yylval.ssym.sym = sym;
1820 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1821 return NAME_OR_INT;
1822 }
1823 }
1824
1825 /* Any other kind of symbol. */
1826 yylval.ssym.sym = sym;
1827 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1828 return NAME;
1829 }
1830 }
1831
1832 void
1833 yyerror (msg)
1834 char *msg;
1835 {
1836 if (*lexptr == '\0')
1837 error("A %s near end of expression.", (msg ? msg : "error"));
1838 else
1839 error ("A %s in expression, near `%s'.", (msg ? msg : "error"),
1840 lexptr);
1841 }
This page took 0.066965 seconds and 4 git commands to generate.