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