xref: /dragonfly/contrib/gdb-7/gdb/c-exp.y (revision 6e278935)
1 /* YACC parser for C expressions, for GDB.
2    Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3    1998, 1999, 2000, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5 
6    This file is part of GDB.
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20 
21 /* Parse a C expression from text in a string,
22    and return the result as a  struct expression  pointer.
23    That structure contains arithmetic operations in reverse polish,
24    with constants represented by operations that are followed by special data.
25    See expression.h for the details of the format.
26    What is important here is that it can be built up sequentially
27    during the process of parsing; the lower levels of the tree always
28    come first in the result.
29 
30    Note that malloc's and realloc's in this file are transformed to
31    xmalloc and xrealloc respectively by the same sed command in the
32    makefile that remaps any other malloc/realloc inserted by the parser
33    generator.  Doing this with #defines and trying to control the interaction
34    with include files (<malloc.h> and <stdlib.h> for example) just became
35    too messy, particularly when such includes can be inserted at random
36    times by the parser generator.  */
37 
38 %{
39 
40 #include "defs.h"
41 #include "gdb_string.h"
42 #include <ctype.h>
43 #include "expression.h"
44 #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 "charset.h"
52 #include "block.h"
53 #include "cp-support.h"
54 #include "dfp.h"
55 #include "gdb_assert.h"
56 #include "macroscope.h"
57 
58 #define parse_type builtin_type (parse_gdbarch)
59 
60 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
61    as well as gratuitiously global symbol names, so we can have multiple
62    yacc generated parsers in gdb.  Note that these are only the variables
63    produced by yacc.  If other parser generators (bison, byacc, etc) produce
64    additional global names that conflict at link time, then those parser
65    generators need to be fixed instead of adding those names to this list. */
66 
67 #define	yymaxdepth c_maxdepth
68 #define	yyparse	c_parse_internal
69 #define	yylex	c_lex
70 #define	yyerror	c_error
71 #define	yylval	c_lval
72 #define	yychar	c_char
73 #define	yydebug	c_debug
74 #define	yypact	c_pact
75 #define	yyr1	c_r1
76 #define	yyr2	c_r2
77 #define	yydef	c_def
78 #define	yychk	c_chk
79 #define	yypgo	c_pgo
80 #define	yyact	c_act
81 #define	yyexca	c_exca
82 #define yyerrflag c_errflag
83 #define yynerrs	c_nerrs
84 #define	yyps	c_ps
85 #define	yypv	c_pv
86 #define	yys	c_s
87 #define	yy_yys	c_yys
88 #define	yystate	c_state
89 #define	yytmp	c_tmp
90 #define	yyv	c_v
91 #define	yy_yyv	c_yyv
92 #define	yyval	c_val
93 #define	yylloc	c_lloc
94 #define yyreds	c_reds		/* With YYDEBUG defined */
95 #define yytoks	c_toks		/* With YYDEBUG defined */
96 #define yyname	c_name		/* With YYDEBUG defined */
97 #define yyrule	c_rule		/* With YYDEBUG defined */
98 #define yylhs	c_yylhs
99 #define yylen	c_yylen
100 #define yydefred c_yydefred
101 #define yydgoto	c_yydgoto
102 #define yysindex c_yysindex
103 #define yyrindex c_yyrindex
104 #define yygindex c_yygindex
105 #define yytable	 c_yytable
106 #define yycheck	 c_yycheck
107 
108 #ifndef YYDEBUG
109 #define	YYDEBUG 1		/* Default to yydebug support */
110 #endif
111 
112 #define YYFPRINTF parser_fprintf
113 
114 int yyparse (void);
115 
116 static int yylex (void);
117 
118 void yyerror (char *);
119 
120 %}
121 
122 /* Although the yacc "value" of an expression is not used,
123    since the result is stored in the structure being created,
124    other node types do have values.  */
125 
126 %union
127   {
128     LONGEST lval;
129     struct {
130       LONGEST val;
131       struct type *type;
132     } typed_val_int;
133     struct {
134       DOUBLEST dval;
135       struct type *type;
136     } typed_val_float;
137     struct {
138       gdb_byte val[16];
139       struct type *type;
140     } typed_val_decfloat;
141     struct symbol *sym;
142     struct type *tval;
143     struct stoken sval;
144     struct typed_stoken tsval;
145     struct ttype tsym;
146     struct symtoken ssym;
147     int voidval;
148     struct block *bval;
149     enum exp_opcode opcode;
150     struct internalvar *ivar;
151 
152     struct stoken_vector svec;
153     struct type **tvec;
154     int *ivec;
155   }
156 
157 %{
158 /* YYSTYPE gets defined by %union */
159 static int parse_number (char *, int, int, YYSTYPE *);
160 static struct stoken operator_stoken (const char *);
161 %}
162 
163 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
164 %type <lval> rcurly
165 %type <tval> type typebase
166 %type <tvec> nonempty_typelist
167 /* %type <bval> block */
168 
169 /* Fancy type parsing.  */
170 %type <voidval> func_mod direct_abs_decl abs_decl
171 %type <tval> ptype
172 %type <lval> array_mod
173 
174 %token <typed_val_int> INT
175 %token <typed_val_float> FLOAT
176 %token <typed_val_decfloat> DECFLOAT
177 
178 /* Both NAME and TYPENAME tokens represent symbols in the input,
179    and both convey their data as strings.
180    But a TYPENAME is a string that happens to be defined as a typedef
181    or builtin type name (such as int or char)
182    and a NAME is any other symbol.
183    Contexts where this distinction is not important can use the
184    nonterminal "name", which matches either NAME or TYPENAME.  */
185 
186 %token <tsval> STRING
187 %token <tsval> CHAR
188 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
189 %token <ssym> UNKNOWN_CPP_NAME
190 %token <voidval> COMPLETE
191 %token <tsym> TYPENAME
192 %type <sval> name
193 %type <svec> string_exp
194 %type <ssym> name_not_typename
195 %type <tsym> typename
196 
197 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
198    but which would parse as a valid number in the current input radix.
199    E.g. "c" when input_radix==16.  Depending on the parse, it will be
200    turned into a name or into a number.  */
201 
202 %token <ssym> NAME_OR_INT
203 
204 %token OPERATOR
205 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
206 %token TEMPLATE
207 %token ERROR
208 %token NEW DELETE
209 %type <sval> operator
210 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
211 
212 /* Special type cases, put in to allow the parser to distinguish different
213    legal basetypes.  */
214 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
215 
216 %token <sval> VARIABLE
217 
218 %token <opcode> ASSIGN_MODIFY
219 
220 /* C++ */
221 %token TRUEKEYWORD
222 %token FALSEKEYWORD
223 
224 
225 %left ','
226 %left ABOVE_COMMA
227 %right '=' ASSIGN_MODIFY
228 %right '?'
229 %left OROR
230 %left ANDAND
231 %left '|'
232 %left '^'
233 %left '&'
234 %left EQUAL NOTEQUAL
235 %left '<' '>' LEQ GEQ
236 %left LSH RSH
237 %left '@'
238 %left '+' '-'
239 %left '*' '/' '%'
240 %right UNARY INCREMENT DECREMENT
241 %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
242 %token <ssym> BLOCKNAME
243 %token <bval> FILENAME
244 %type <bval> block
245 %left COLONCOLON
246 
247 
248 %%
249 
250 start   :	exp1
251 	|	type_exp
252 	;
253 
254 type_exp:	type
255 			{ write_exp_elt_opcode(OP_TYPE);
256 			  write_exp_elt_type($1);
257 			  write_exp_elt_opcode(OP_TYPE);}
258 	;
259 
260 /* Expressions, including the comma operator.  */
261 exp1	:	exp
262 	|	exp1 ',' exp
263 			{ write_exp_elt_opcode (BINOP_COMMA); }
264 	;
265 
266 /* Expressions, not including the comma operator.  */
267 exp	:	'*' exp    %prec UNARY
268 			{ write_exp_elt_opcode (UNOP_IND); }
269 	;
270 
271 exp	:	'&' exp    %prec UNARY
272 			{ write_exp_elt_opcode (UNOP_ADDR); }
273 	;
274 
275 exp	:	'-' exp    %prec UNARY
276 			{ write_exp_elt_opcode (UNOP_NEG); }
277 	;
278 
279 exp	:	'+' exp    %prec UNARY
280 			{ write_exp_elt_opcode (UNOP_PLUS); }
281 	;
282 
283 exp	:	'!' exp    %prec UNARY
284 			{ write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
285 	;
286 
287 exp	:	'~' exp    %prec UNARY
288 			{ write_exp_elt_opcode (UNOP_COMPLEMENT); }
289 	;
290 
291 exp	:	INCREMENT exp    %prec UNARY
292 			{ write_exp_elt_opcode (UNOP_PREINCREMENT); }
293 	;
294 
295 exp	:	DECREMENT exp    %prec UNARY
296 			{ write_exp_elt_opcode (UNOP_PREDECREMENT); }
297 	;
298 
299 exp	:	exp INCREMENT    %prec UNARY
300 			{ write_exp_elt_opcode (UNOP_POSTINCREMENT); }
301 	;
302 
303 exp	:	exp DECREMENT    %prec UNARY
304 			{ write_exp_elt_opcode (UNOP_POSTDECREMENT); }
305 	;
306 
307 exp	:	SIZEOF exp       %prec UNARY
308 			{ write_exp_elt_opcode (UNOP_SIZEOF); }
309 	;
310 
311 exp	:	exp ARROW name
312 			{ write_exp_elt_opcode (STRUCTOP_PTR);
313 			  write_exp_string ($3);
314 			  write_exp_elt_opcode (STRUCTOP_PTR); }
315 	;
316 
317 exp	:	exp ARROW name COMPLETE
318 			{ mark_struct_expression ();
319 			  write_exp_elt_opcode (STRUCTOP_PTR);
320 			  write_exp_string ($3);
321 			  write_exp_elt_opcode (STRUCTOP_PTR); }
322 	;
323 
324 exp	:	exp ARROW COMPLETE
325 			{ struct stoken s;
326 			  mark_struct_expression ();
327 			  write_exp_elt_opcode (STRUCTOP_PTR);
328 			  s.ptr = "";
329 			  s.length = 0;
330 			  write_exp_string (s);
331 			  write_exp_elt_opcode (STRUCTOP_PTR); }
332 	;
333 
334 exp	:	exp ARROW qualified_name
335 			{ /* exp->type::name becomes exp->*(&type::name) */
336 			  /* Note: this doesn't work if name is a
337 			     static member!  FIXME */
338 			  write_exp_elt_opcode (UNOP_ADDR);
339 			  write_exp_elt_opcode (STRUCTOP_MPTR); }
340 	;
341 
342 exp	:	exp ARROW_STAR exp
343 			{ write_exp_elt_opcode (STRUCTOP_MPTR); }
344 	;
345 
346 exp	:	exp '.' name
347 			{ write_exp_elt_opcode (STRUCTOP_STRUCT);
348 			  write_exp_string ($3);
349 			  write_exp_elt_opcode (STRUCTOP_STRUCT); }
350 	;
351 
352 exp	:	exp '.' name COMPLETE
353 			{ mark_struct_expression ();
354 			  write_exp_elt_opcode (STRUCTOP_STRUCT);
355 			  write_exp_string ($3);
356 			  write_exp_elt_opcode (STRUCTOP_STRUCT); }
357 	;
358 
359 exp	:	exp '.' COMPLETE
360 			{ struct stoken s;
361 			  mark_struct_expression ();
362 			  write_exp_elt_opcode (STRUCTOP_STRUCT);
363 			  s.ptr = "";
364 			  s.length = 0;
365 			  write_exp_string (s);
366 			  write_exp_elt_opcode (STRUCTOP_STRUCT); }
367 	;
368 
369 exp	:	exp '.' qualified_name
370 			{ /* exp.type::name becomes exp.*(&type::name) */
371 			  /* Note: this doesn't work if name is a
372 			     static member!  FIXME */
373 			  write_exp_elt_opcode (UNOP_ADDR);
374 			  write_exp_elt_opcode (STRUCTOP_MEMBER); }
375 	;
376 
377 exp	:	exp DOT_STAR exp
378 			{ write_exp_elt_opcode (STRUCTOP_MEMBER); }
379 	;
380 
381 exp	:	exp '[' exp1 ']'
382 			{ write_exp_elt_opcode (BINOP_SUBSCRIPT); }
383 	;
384 
385 exp	:	exp '('
386 			/* This is to save the value of arglist_len
387 			   being accumulated by an outer function call.  */
388 			{ start_arglist (); }
389 		arglist ')'	%prec ARROW
390 			{ write_exp_elt_opcode (OP_FUNCALL);
391 			  write_exp_elt_longcst ((LONGEST) end_arglist ());
392 			  write_exp_elt_opcode (OP_FUNCALL); }
393 	;
394 
395 exp	:	UNKNOWN_CPP_NAME '('
396 			{
397 			  /* This could potentially be a an argument defined
398 			     lookup function (Koenig).  */
399 			  write_exp_elt_opcode (OP_ADL_FUNC);
400 			  write_exp_elt_block (expression_context_block);
401 			  write_exp_elt_sym (NULL); /* Placeholder.  */
402 			  write_exp_string ($1.stoken);
403 			  write_exp_elt_opcode (OP_ADL_FUNC);
404 
405 			/* This is to save the value of arglist_len
406 			   being accumulated by an outer function call.  */
407 
408 			  start_arglist ();
409 			}
410 		arglist ')'	%prec ARROW
411 			{
412 			  write_exp_elt_opcode (OP_FUNCALL);
413 			  write_exp_elt_longcst ((LONGEST) end_arglist ());
414 			  write_exp_elt_opcode (OP_FUNCALL);
415 			}
416 	;
417 
418 lcurly	:	'{'
419 			{ start_arglist (); }
420 	;
421 
422 arglist	:
423 	;
424 
425 arglist	:	exp
426 			{ arglist_len = 1; }
427 	;
428 
429 arglist	:	arglist ',' exp   %prec ABOVE_COMMA
430 			{ arglist_len++; }
431 	;
432 
433 exp     :       exp '(' nonempty_typelist ')' const_or_volatile
434 			{ int i;
435 			  write_exp_elt_opcode (TYPE_INSTANCE);
436 			  write_exp_elt_longcst ((LONGEST) $<ivec>3[0]);
437 			  for (i = 0; i < $<ivec>3[0]; ++i)
438 			    write_exp_elt_type ($<tvec>3[i + 1]);
439 			  write_exp_elt_longcst((LONGEST) $<ivec>3[0]);
440 			  write_exp_elt_opcode (TYPE_INSTANCE);
441 			  free ($3);
442 			}
443 	;
444 
445 rcurly	:	'}'
446 			{ $$ = end_arglist () - 1; }
447 	;
448 exp	:	lcurly arglist rcurly	%prec ARROW
449 			{ write_exp_elt_opcode (OP_ARRAY);
450 			  write_exp_elt_longcst ((LONGEST) 0);
451 			  write_exp_elt_longcst ((LONGEST) $3);
452 			  write_exp_elt_opcode (OP_ARRAY); }
453 	;
454 
455 exp	:	lcurly type rcurly exp  %prec UNARY
456 			{ write_exp_elt_opcode (UNOP_MEMVAL);
457 			  write_exp_elt_type ($2);
458 			  write_exp_elt_opcode (UNOP_MEMVAL); }
459 	;
460 
461 exp	:	'(' type ')' exp  %prec UNARY
462 			{ write_exp_elt_opcode (UNOP_CAST);
463 			  write_exp_elt_type ($2);
464 			  write_exp_elt_opcode (UNOP_CAST); }
465 	;
466 
467 exp	:	'(' exp1 ')'
468 			{ }
469 	;
470 
471 /* Binary operators in order of decreasing precedence.  */
472 
473 exp	:	exp '@' exp
474 			{ write_exp_elt_opcode (BINOP_REPEAT); }
475 	;
476 
477 exp	:	exp '*' exp
478 			{ write_exp_elt_opcode (BINOP_MUL); }
479 	;
480 
481 exp	:	exp '/' exp
482 			{ write_exp_elt_opcode (BINOP_DIV); }
483 	;
484 
485 exp	:	exp '%' exp
486 			{ write_exp_elt_opcode (BINOP_REM); }
487 	;
488 
489 exp	:	exp '+' exp
490 			{ write_exp_elt_opcode (BINOP_ADD); }
491 	;
492 
493 exp	:	exp '-' exp
494 			{ write_exp_elt_opcode (BINOP_SUB); }
495 	;
496 
497 exp	:	exp LSH exp
498 			{ write_exp_elt_opcode (BINOP_LSH); }
499 	;
500 
501 exp	:	exp RSH exp
502 			{ write_exp_elt_opcode (BINOP_RSH); }
503 	;
504 
505 exp	:	exp EQUAL exp
506 			{ write_exp_elt_opcode (BINOP_EQUAL); }
507 	;
508 
509 exp	:	exp NOTEQUAL exp
510 			{ write_exp_elt_opcode (BINOP_NOTEQUAL); }
511 	;
512 
513 exp	:	exp LEQ exp
514 			{ write_exp_elt_opcode (BINOP_LEQ); }
515 	;
516 
517 exp	:	exp GEQ exp
518 			{ write_exp_elt_opcode (BINOP_GEQ); }
519 	;
520 
521 exp	:	exp '<' exp
522 			{ write_exp_elt_opcode (BINOP_LESS); }
523 	;
524 
525 exp	:	exp '>' exp
526 			{ write_exp_elt_opcode (BINOP_GTR); }
527 	;
528 
529 exp	:	exp '&' exp
530 			{ write_exp_elt_opcode (BINOP_BITWISE_AND); }
531 	;
532 
533 exp	:	exp '^' exp
534 			{ write_exp_elt_opcode (BINOP_BITWISE_XOR); }
535 	;
536 
537 exp	:	exp '|' exp
538 			{ write_exp_elt_opcode (BINOP_BITWISE_IOR); }
539 	;
540 
541 exp	:	exp ANDAND exp
542 			{ write_exp_elt_opcode (BINOP_LOGICAL_AND); }
543 	;
544 
545 exp	:	exp OROR exp
546 			{ write_exp_elt_opcode (BINOP_LOGICAL_OR); }
547 	;
548 
549 exp	:	exp '?' exp ':' exp	%prec '?'
550 			{ write_exp_elt_opcode (TERNOP_COND); }
551 	;
552 
553 exp	:	exp '=' exp
554 			{ write_exp_elt_opcode (BINOP_ASSIGN); }
555 	;
556 
557 exp	:	exp ASSIGN_MODIFY exp
558 			{ write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
559 			  write_exp_elt_opcode ($2);
560 			  write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
561 	;
562 
563 exp	:	INT
564 			{ write_exp_elt_opcode (OP_LONG);
565 			  write_exp_elt_type ($1.type);
566 			  write_exp_elt_longcst ((LONGEST)($1.val));
567 			  write_exp_elt_opcode (OP_LONG); }
568 	;
569 
570 exp	:	CHAR
571 			{
572 			  struct stoken_vector vec;
573 			  vec.len = 1;
574 			  vec.tokens = &$1;
575 			  write_exp_string_vector ($1.type, &vec);
576 			}
577 	;
578 
579 exp	:	NAME_OR_INT
580 			{ YYSTYPE val;
581 			  parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
582 			  write_exp_elt_opcode (OP_LONG);
583 			  write_exp_elt_type (val.typed_val_int.type);
584 			  write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
585 			  write_exp_elt_opcode (OP_LONG);
586 			}
587 	;
588 
589 
590 exp	:	FLOAT
591 			{ write_exp_elt_opcode (OP_DOUBLE);
592 			  write_exp_elt_type ($1.type);
593 			  write_exp_elt_dblcst ($1.dval);
594 			  write_exp_elt_opcode (OP_DOUBLE); }
595 	;
596 
597 exp	:	DECFLOAT
598 			{ write_exp_elt_opcode (OP_DECFLOAT);
599 			  write_exp_elt_type ($1.type);
600 			  write_exp_elt_decfloatcst ($1.val);
601 			  write_exp_elt_opcode (OP_DECFLOAT); }
602 	;
603 
604 exp	:	variable
605 	;
606 
607 exp	:	VARIABLE
608 			{
609 			  write_dollar_variable ($1);
610 			}
611 	;
612 
613 exp	:	SIZEOF '(' type ')'	%prec UNARY
614 			{ write_exp_elt_opcode (OP_LONG);
615 			  write_exp_elt_type (lookup_signed_typename
616 					      (parse_language, parse_gdbarch,
617 					       "int"));
618 			  CHECK_TYPEDEF ($3);
619 			  write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
620 			  write_exp_elt_opcode (OP_LONG); }
621 	;
622 
623 exp	:	REINTERPRET_CAST '<' type '>' '(' exp ')' %prec UNARY
624 			{ write_exp_elt_opcode (UNOP_REINTERPRET_CAST);
625 			  write_exp_elt_type ($3);
626 			  write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
627 	;
628 
629 exp	:	STATIC_CAST '<' type '>' '(' exp ')' %prec UNARY
630 			{ write_exp_elt_opcode (UNOP_CAST);
631 			  write_exp_elt_type ($3);
632 			  write_exp_elt_opcode (UNOP_CAST); }
633 	;
634 
635 exp	:	DYNAMIC_CAST '<' type '>' '(' exp ')' %prec UNARY
636 			{ write_exp_elt_opcode (UNOP_DYNAMIC_CAST);
637 			  write_exp_elt_type ($3);
638 			  write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
639 	;
640 
641 exp	:	CONST_CAST '<' type '>' '(' exp ')' %prec UNARY
642 			{ /* We could do more error checking here, but
643 			     it doesn't seem worthwhile.  */
644 			  write_exp_elt_opcode (UNOP_CAST);
645 			  write_exp_elt_type ($3);
646 			  write_exp_elt_opcode (UNOP_CAST); }
647 	;
648 
649 string_exp:
650 		STRING
651 			{
652 			  /* We copy the string here, and not in the
653 			     lexer, to guarantee that we do not leak a
654 			     string.  Note that we follow the
655 			     NUL-termination convention of the
656 			     lexer.  */
657 			  struct typed_stoken *vec = XNEW (struct typed_stoken);
658 			  $$.len = 1;
659 			  $$.tokens = vec;
660 
661 			  vec->type = $1.type;
662 			  vec->length = $1.length;
663 			  vec->ptr = malloc ($1.length + 1);
664 			  memcpy (vec->ptr, $1.ptr, $1.length + 1);
665 			}
666 
667 	|	string_exp STRING
668 			{
669 			  /* Note that we NUL-terminate here, but just
670 			     for convenience.  */
671 			  char *p;
672 			  ++$$.len;
673 			  $$.tokens = realloc ($$.tokens,
674 					       $$.len * sizeof (struct typed_stoken));
675 
676 			  p = malloc ($2.length + 1);
677 			  memcpy (p, $2.ptr, $2.length + 1);
678 
679 			  $$.tokens[$$.len - 1].type = $2.type;
680 			  $$.tokens[$$.len - 1].length = $2.length;
681 			  $$.tokens[$$.len - 1].ptr = p;
682 			}
683 		;
684 
685 exp	:	string_exp
686 			{
687 			  int i;
688 			  enum c_string_type type = C_STRING;
689 
690 			  for (i = 0; i < $1.len; ++i)
691 			    {
692 			      switch ($1.tokens[i].type)
693 				{
694 				case C_STRING:
695 				  break;
696 				case C_WIDE_STRING:
697 				case C_STRING_16:
698 				case C_STRING_32:
699 				  if (type != C_STRING
700 				      && type != $1.tokens[i].type)
701 				    error (_("Undefined string concatenation."));
702 				  type = $1.tokens[i].type;
703 				  break;
704 				default:
705 				  /* internal error */
706 				  internal_error (__FILE__, __LINE__,
707 						  "unrecognized type in string concatenation");
708 				}
709 			    }
710 
711 			  write_exp_string_vector (type, &$1);
712 			  for (i = 0; i < $1.len; ++i)
713 			    free ($1.tokens[i].ptr);
714 			  free ($1.tokens);
715 			}
716 	;
717 
718 /* C++.  */
719 exp     :       TRUEKEYWORD
720                         { write_exp_elt_opcode (OP_LONG);
721                           write_exp_elt_type (parse_type->builtin_bool);
722                           write_exp_elt_longcst ((LONGEST) 1);
723                           write_exp_elt_opcode (OP_LONG); }
724 	;
725 
726 exp     :       FALSEKEYWORD
727                         { write_exp_elt_opcode (OP_LONG);
728                           write_exp_elt_type (parse_type->builtin_bool);
729                           write_exp_elt_longcst ((LONGEST) 0);
730                           write_exp_elt_opcode (OP_LONG); }
731 	;
732 
733 /* end of C++.  */
734 
735 block	:	BLOCKNAME
736 			{
737 			  if ($1.sym)
738 			    $$ = SYMBOL_BLOCK_VALUE ($1.sym);
739 			  else
740 			    error (_("No file or function \"%s\"."),
741 				   copy_name ($1.stoken));
742 			}
743 	|	FILENAME
744 			{
745 			  $$ = $1;
746 			}
747 	;
748 
749 block	:	block COLONCOLON name
750 			{ struct symbol *tem
751 			    = lookup_symbol (copy_name ($3), $1,
752 					     VAR_DOMAIN, (int *) NULL);
753 			  if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
754 			    error (_("No function \"%s\" in specified context."),
755 				   copy_name ($3));
756 			  $$ = SYMBOL_BLOCK_VALUE (tem); }
757 	;
758 
759 variable:	block COLONCOLON name
760 			{ struct symbol *sym;
761 			  sym = lookup_symbol (copy_name ($3), $1,
762 					       VAR_DOMAIN, (int *) NULL);
763 			  if (sym == 0)
764 			    error (_("No symbol \"%s\" in specified context."),
765 				   copy_name ($3));
766 
767 			  write_exp_elt_opcode (OP_VAR_VALUE);
768 			  /* block_found is set by lookup_symbol.  */
769 			  write_exp_elt_block (block_found);
770 			  write_exp_elt_sym (sym);
771 			  write_exp_elt_opcode (OP_VAR_VALUE); }
772 	;
773 
774 qualified_name:	TYPENAME COLONCOLON name
775 			{
776 			  struct type *type = $1.type;
777 			  CHECK_TYPEDEF (type);
778 			  if (TYPE_CODE (type) != TYPE_CODE_STRUCT
779 			      && TYPE_CODE (type) != TYPE_CODE_UNION
780 			      && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
781 			    error (_("`%s' is not defined as an aggregate type."),
782 				   TYPE_NAME (type));
783 
784 			  write_exp_elt_opcode (OP_SCOPE);
785 			  write_exp_elt_type (type);
786 			  write_exp_string ($3);
787 			  write_exp_elt_opcode (OP_SCOPE);
788 			}
789 	|	TYPENAME COLONCOLON '~' name
790 			{
791 			  struct type *type = $1.type;
792 			  struct stoken tmp_token;
793 			  CHECK_TYPEDEF (type);
794 			  if (TYPE_CODE (type) != TYPE_CODE_STRUCT
795 			      && TYPE_CODE (type) != TYPE_CODE_UNION
796 			      && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
797 			    error (_("`%s' is not defined as an aggregate type."),
798 				   TYPE_NAME (type));
799 
800 			  tmp_token.ptr = (char*) alloca ($4.length + 2);
801 			  tmp_token.length = $4.length + 1;
802 			  tmp_token.ptr[0] = '~';
803 			  memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
804 			  tmp_token.ptr[tmp_token.length] = 0;
805 
806 			  /* Check for valid destructor name.  */
807 			  destructor_name_p (tmp_token.ptr, type);
808 			  write_exp_elt_opcode (OP_SCOPE);
809 			  write_exp_elt_type (type);
810 			  write_exp_string (tmp_token);
811 			  write_exp_elt_opcode (OP_SCOPE);
812 			}
813 	|	TYPENAME COLONCOLON name COLONCOLON name
814 			{
815 			  char *copy = copy_name ($3);
816 			  error (_("No type \"%s\" within class "
817 				   "or namespace \"%s\"."),
818 				 copy, TYPE_NAME ($1.type));
819 			}
820 	;
821 
822 variable:	qualified_name
823 	|	COLONCOLON name_not_typename
824 			{
825 			  char *name = copy_name ($2.stoken);
826 			  struct symbol *sym;
827 			  struct minimal_symbol *msymbol;
828 
829 			  sym =
830 			    lookup_symbol (name, (const struct block *) NULL,
831 					   VAR_DOMAIN, (int *) NULL);
832 			  if (sym)
833 			    {
834 			      write_exp_elt_opcode (OP_VAR_VALUE);
835 			      write_exp_elt_block (NULL);
836 			      write_exp_elt_sym (sym);
837 			      write_exp_elt_opcode (OP_VAR_VALUE);
838 			      break;
839 			    }
840 
841 			  msymbol = lookup_minimal_symbol (name, NULL, NULL);
842 			  if (msymbol != NULL)
843 			    write_exp_msymbol (msymbol);
844 			  else if (!have_full_symbols () && !have_partial_symbols ())
845 			    error (_("No symbol table is loaded.  Use the \"file\" command."));
846 			  else
847 			    error (_("No symbol \"%s\" in current context."), name);
848 			}
849 	;
850 
851 variable:	name_not_typename
852 			{ struct symbol *sym = $1.sym;
853 
854 			  if (sym)
855 			    {
856 			      if (symbol_read_needs_frame (sym))
857 				{
858 				  if (innermost_block == 0
859 				      || contained_in (block_found,
860 						       innermost_block))
861 				    innermost_block = block_found;
862 				}
863 
864 			      write_exp_elt_opcode (OP_VAR_VALUE);
865 			      /* We want to use the selected frame, not
866 				 another more inner frame which happens to
867 				 be in the same block.  */
868 			      write_exp_elt_block (NULL);
869 			      write_exp_elt_sym (sym);
870 			      write_exp_elt_opcode (OP_VAR_VALUE);
871 			    }
872 			  else if ($1.is_a_field_of_this)
873 			    {
874 			      /* C++: it hangs off of `this'.  Must
875 			         not inadvertently convert from a method call
876 				 to data ref.  */
877 			      if (innermost_block == 0
878 				  || contained_in (block_found,
879 						   innermost_block))
880 				innermost_block = block_found;
881 			      write_exp_elt_opcode (OP_THIS);
882 			      write_exp_elt_opcode (OP_THIS);
883 			      write_exp_elt_opcode (STRUCTOP_PTR);
884 			      write_exp_string ($1.stoken);
885 			      write_exp_elt_opcode (STRUCTOP_PTR);
886 			    }
887 			  else
888 			    {
889 			      struct minimal_symbol *msymbol;
890 			      char *arg = copy_name ($1.stoken);
891 
892 			      msymbol =
893 				lookup_minimal_symbol (arg, NULL, NULL);
894 			      if (msymbol != NULL)
895 				write_exp_msymbol (msymbol);
896 			      else if (!have_full_symbols () && !have_partial_symbols ())
897 				error (_("No symbol table is loaded.  Use the \"file\" command."));
898 			      else
899 				error (_("No symbol \"%s\" in current context."),
900 				       copy_name ($1.stoken));
901 			    }
902 			}
903 	;
904 
905 space_identifier : '@' NAME
906 		{ push_type_address_space (copy_name ($2.stoken));
907 		  push_type (tp_space_identifier);
908 		}
909 	;
910 
911 const_or_volatile: const_or_volatile_noopt
912 	|
913 	;
914 
915 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
916 	;
917 
918 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
919 	| const_or_volatile_noopt
920 	;
921 
922 const_or_volatile_or_space_identifier:
923 		const_or_volatile_or_space_identifier_noopt
924 	|
925 	;
926 
927 abs_decl:	'*'
928 			{ push_type (tp_pointer); $$ = 0; }
929 	|	'*' abs_decl
930 			{ push_type (tp_pointer); $$ = $2; }
931 	|	'&'
932 			{ push_type (tp_reference); $$ = 0; }
933 	|	'&' abs_decl
934 			{ push_type (tp_reference); $$ = $2; }
935 	|	direct_abs_decl
936 	;
937 
938 direct_abs_decl: '(' abs_decl ')'
939 			{ $$ = $2; }
940 	|	direct_abs_decl array_mod
941 			{
942 			  push_type_int ($2);
943 			  push_type (tp_array);
944 			}
945 	|	array_mod
946 			{
947 			  push_type_int ($1);
948 			  push_type (tp_array);
949 			  $$ = 0;
950 			}
951 
952 	| 	direct_abs_decl func_mod
953 			{ push_type (tp_function); }
954 	|	func_mod
955 			{ push_type (tp_function); }
956 	;
957 
958 array_mod:	'[' ']'
959 			{ $$ = -1; }
960 	|	'[' INT ']'
961 			{ $$ = $2.val; }
962 	;
963 
964 func_mod:	'(' ')'
965 			{ $$ = 0; }
966 	|	'(' nonempty_typelist ')'
967 			{ free ($2); $$ = 0; }
968 	;
969 
970 /* We used to try to recognize pointer to member types here, but
971    that didn't work (shift/reduce conflicts meant that these rules never
972    got executed).  The problem is that
973      int (foo::bar::baz::bizzle)
974    is a function type but
975      int (foo::bar::baz::bizzle::*)
976    is a pointer to member type.  Stroustrup loses again!  */
977 
978 type	:	ptype
979 	;
980 
981 typebase  /* Implements (approximately): (type-qualifier)* type-specifier */
982 	:	TYPENAME
983 			{ $$ = $1.type; }
984 	|	INT_KEYWORD
985 			{ $$ = lookup_signed_typename (parse_language,
986 						       parse_gdbarch,
987 						       "int"); }
988 	|	LONG
989 			{ $$ = lookup_signed_typename (parse_language,
990 						       parse_gdbarch,
991 						       "long"); }
992 	|	SHORT
993 			{ $$ = lookup_signed_typename (parse_language,
994 						       parse_gdbarch,
995 						       "short"); }
996 	|	LONG INT_KEYWORD
997 			{ $$ = lookup_signed_typename (parse_language,
998 						       parse_gdbarch,
999 						       "long"); }
1000 	|	LONG SIGNED_KEYWORD INT_KEYWORD
1001 			{ $$ = lookup_signed_typename (parse_language,
1002 						       parse_gdbarch,
1003 						       "long"); }
1004 	|	LONG SIGNED_KEYWORD
1005 			{ $$ = lookup_signed_typename (parse_language,
1006 						       parse_gdbarch,
1007 						       "long"); }
1008 	|	SIGNED_KEYWORD LONG INT_KEYWORD
1009 			{ $$ = lookup_signed_typename (parse_language,
1010 						       parse_gdbarch,
1011 						       "long"); }
1012 	|	UNSIGNED LONG INT_KEYWORD
1013 			{ $$ = lookup_unsigned_typename (parse_language,
1014 							 parse_gdbarch,
1015 							 "long"); }
1016 	|	LONG UNSIGNED INT_KEYWORD
1017 			{ $$ = lookup_unsigned_typename (parse_language,
1018 							 parse_gdbarch,
1019 							 "long"); }
1020 	|	LONG UNSIGNED
1021 			{ $$ = lookup_unsigned_typename (parse_language,
1022 							 parse_gdbarch,
1023 							 "long"); }
1024 	|	LONG LONG
1025 			{ $$ = lookup_signed_typename (parse_language,
1026 						       parse_gdbarch,
1027 						       "long long"); }
1028 	|	LONG LONG INT_KEYWORD
1029 			{ $$ = lookup_signed_typename (parse_language,
1030 						       parse_gdbarch,
1031 						       "long long"); }
1032 	|	LONG LONG SIGNED_KEYWORD INT_KEYWORD
1033 			{ $$ = lookup_signed_typename (parse_language,
1034 						       parse_gdbarch,
1035 						       "long long"); }
1036 	|	LONG LONG SIGNED_KEYWORD
1037 			{ $$ = lookup_signed_typename (parse_language,
1038 						       parse_gdbarch,
1039 						       "long long"); }
1040 	|	SIGNED_KEYWORD LONG LONG
1041 			{ $$ = lookup_signed_typename (parse_language,
1042 						       parse_gdbarch,
1043 						       "long long"); }
1044 	|	SIGNED_KEYWORD LONG LONG INT_KEYWORD
1045 			{ $$ = lookup_signed_typename (parse_language,
1046 						       parse_gdbarch,
1047 						       "long long"); }
1048 	|	UNSIGNED LONG LONG
1049 			{ $$ = lookup_unsigned_typename (parse_language,
1050 							 parse_gdbarch,
1051 							 "long long"); }
1052 	|	UNSIGNED LONG LONG INT_KEYWORD
1053 			{ $$ = lookup_unsigned_typename (parse_language,
1054 							 parse_gdbarch,
1055 							 "long long"); }
1056 	|	LONG LONG UNSIGNED
1057 			{ $$ = lookup_unsigned_typename (parse_language,
1058 							 parse_gdbarch,
1059 							 "long long"); }
1060 	|	LONG LONG UNSIGNED INT_KEYWORD
1061 			{ $$ = lookup_unsigned_typename (parse_language,
1062 							 parse_gdbarch,
1063 							 "long long"); }
1064 	|	SHORT INT_KEYWORD
1065 			{ $$ = lookup_signed_typename (parse_language,
1066 						       parse_gdbarch,
1067 						       "short"); }
1068 	|	SHORT SIGNED_KEYWORD INT_KEYWORD
1069 			{ $$ = lookup_signed_typename (parse_language,
1070 						       parse_gdbarch,
1071 						       "short"); }
1072 	|	SHORT SIGNED_KEYWORD
1073 			{ $$ = lookup_signed_typename (parse_language,
1074 						       parse_gdbarch,
1075 						       "short"); }
1076 	|	UNSIGNED SHORT INT_KEYWORD
1077 			{ $$ = lookup_unsigned_typename (parse_language,
1078 							 parse_gdbarch,
1079 							 "short"); }
1080 	|	SHORT UNSIGNED
1081 			{ $$ = lookup_unsigned_typename (parse_language,
1082 							 parse_gdbarch,
1083 							 "short"); }
1084 	|	SHORT UNSIGNED INT_KEYWORD
1085 			{ $$ = lookup_unsigned_typename (parse_language,
1086 							 parse_gdbarch,
1087 							 "short"); }
1088 	|	DOUBLE_KEYWORD
1089 			{ $$ = lookup_typename (parse_language, parse_gdbarch,
1090 						"double", (struct block *) NULL,
1091 						0); }
1092 	|	LONG DOUBLE_KEYWORD
1093 			{ $$ = lookup_typename (parse_language, parse_gdbarch,
1094 						"long double",
1095 						(struct block *) NULL, 0); }
1096 	|	STRUCT name
1097 			{ $$ = lookup_struct (copy_name ($2),
1098 					      expression_context_block); }
1099 	|	CLASS name
1100 			{ $$ = lookup_struct (copy_name ($2),
1101 					      expression_context_block); }
1102 	|	UNION name
1103 			{ $$ = lookup_union (copy_name ($2),
1104 					     expression_context_block); }
1105 	|	ENUM name
1106 			{ $$ = lookup_enum (copy_name ($2),
1107 					    expression_context_block); }
1108 	|	UNSIGNED typename
1109 			{ $$ = lookup_unsigned_typename (parse_language,
1110 							 parse_gdbarch,
1111 							 TYPE_NAME($2.type)); }
1112 	|	UNSIGNED
1113 			{ $$ = lookup_unsigned_typename (parse_language,
1114 							 parse_gdbarch,
1115 							 "int"); }
1116 	|	SIGNED_KEYWORD typename
1117 			{ $$ = lookup_signed_typename (parse_language,
1118 						       parse_gdbarch,
1119 						       TYPE_NAME($2.type)); }
1120 	|	SIGNED_KEYWORD
1121 			{ $$ = lookup_signed_typename (parse_language,
1122 						       parse_gdbarch,
1123 						       "int"); }
1124                 /* It appears that this rule for templates is never
1125                    reduced; template recognition happens by lookahead
1126                    in the token processing code in yylex. */
1127 	|	TEMPLATE name '<' type '>'
1128 			{ $$ = lookup_template_type(copy_name($2), $4,
1129 						    expression_context_block);
1130 			}
1131 	| const_or_volatile_or_space_identifier_noopt typebase
1132 			{ $$ = follow_types ($2); }
1133 	| typebase const_or_volatile_or_space_identifier_noopt
1134 			{ $$ = follow_types ($1); }
1135 	;
1136 
1137 typename:	TYPENAME
1138 	|	INT_KEYWORD
1139 		{
1140 		  $$.stoken.ptr = "int";
1141 		  $$.stoken.length = 3;
1142 		  $$.type = lookup_signed_typename (parse_language,
1143 						    parse_gdbarch,
1144 						    "int");
1145 		}
1146 	|	LONG
1147 		{
1148 		  $$.stoken.ptr = "long";
1149 		  $$.stoken.length = 4;
1150 		  $$.type = lookup_signed_typename (parse_language,
1151 						    parse_gdbarch,
1152 						    "long");
1153 		}
1154 	|	SHORT
1155 		{
1156 		  $$.stoken.ptr = "short";
1157 		  $$.stoken.length = 5;
1158 		  $$.type = lookup_signed_typename (parse_language,
1159 						    parse_gdbarch,
1160 						    "short");
1161 		}
1162 	;
1163 
1164 nonempty_typelist
1165 	:	type
1166 		{ $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1167 		  $<ivec>$[0] = 1;	/* Number of types in vector */
1168 		  $$[1] = $1;
1169 		}
1170 	|	nonempty_typelist ',' type
1171 		{ int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1172 		  $$ = (struct type **) realloc ((char *) $1, len);
1173 		  $$[$<ivec>$[0]] = $3;
1174 		}
1175 	;
1176 
1177 ptype	:	typebase
1178 	|	ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1179 		{ $$ = follow_types ($1); }
1180 	;
1181 
1182 const_and_volatile: 	CONST_KEYWORD VOLATILE_KEYWORD
1183 	| 		VOLATILE_KEYWORD CONST_KEYWORD
1184 	;
1185 
1186 const_or_volatile_noopt:  	const_and_volatile
1187 			{ push_type (tp_const);
1188 			  push_type (tp_volatile);
1189 			}
1190 	| 		CONST_KEYWORD
1191 			{ push_type (tp_const); }
1192 	| 		VOLATILE_KEYWORD
1193 			{ push_type (tp_volatile); }
1194 	;
1195 
1196 operator:	OPERATOR NEW
1197 			{ $$ = operator_stoken (" new"); }
1198 	|	OPERATOR DELETE
1199 			{ $$ = operator_stoken (" delete"); }
1200 	|	OPERATOR NEW '[' ']'
1201 			{ $$ = operator_stoken (" new[]"); }
1202 	|	OPERATOR DELETE '[' ']'
1203 			{ $$ = operator_stoken (" delete[]"); }
1204 	|	OPERATOR '+'
1205 			{ $$ = operator_stoken ("+"); }
1206 	|	OPERATOR '-'
1207 			{ $$ = operator_stoken ("-"); }
1208 	|	OPERATOR '*'
1209 			{ $$ = operator_stoken ("*"); }
1210 	|	OPERATOR '/'
1211 			{ $$ = operator_stoken ("/"); }
1212 	|	OPERATOR '%'
1213 			{ $$ = operator_stoken ("%"); }
1214 	|	OPERATOR '^'
1215 			{ $$ = operator_stoken ("^"); }
1216 	|	OPERATOR '&'
1217 			{ $$ = operator_stoken ("&"); }
1218 	|	OPERATOR '|'
1219 			{ $$ = operator_stoken ("|"); }
1220 	|	OPERATOR '~'
1221 			{ $$ = operator_stoken ("~"); }
1222 	|	OPERATOR '!'
1223 			{ $$ = operator_stoken ("!"); }
1224 	|	OPERATOR '='
1225 			{ $$ = operator_stoken ("="); }
1226 	|	OPERATOR '<'
1227 			{ $$ = operator_stoken ("<"); }
1228 	|	OPERATOR '>'
1229 			{ $$ = operator_stoken (">"); }
1230 	|	OPERATOR ASSIGN_MODIFY
1231 			{ const char *op = "unknown";
1232 			  switch ($2)
1233 			    {
1234 			    case BINOP_RSH:
1235 			      op = ">>=";
1236 			      break;
1237 			    case BINOP_LSH:
1238 			      op = "<<=";
1239 			      break;
1240 			    case BINOP_ADD:
1241 			      op = "+=";
1242 			      break;
1243 			    case BINOP_SUB:
1244 			      op = "-=";
1245 			      break;
1246 			    case BINOP_MUL:
1247 			      op = "*=";
1248 			      break;
1249 			    case BINOP_DIV:
1250 			      op = "/=";
1251 			      break;
1252 			    case BINOP_REM:
1253 			      op = "%=";
1254 			      break;
1255 			    case BINOP_BITWISE_IOR:
1256 			      op = "|=";
1257 			      break;
1258 			    case BINOP_BITWISE_AND:
1259 			      op = "&=";
1260 			      break;
1261 			    case BINOP_BITWISE_XOR:
1262 			      op = "^=";
1263 			      break;
1264 			    default:
1265 			      break;
1266 			    }
1267 
1268 			  $$ = operator_stoken (op);
1269 			}
1270 	|	OPERATOR LSH
1271 			{ $$ = operator_stoken ("<<"); }
1272 	|	OPERATOR RSH
1273 			{ $$ = operator_stoken (">>"); }
1274 	|	OPERATOR EQUAL
1275 			{ $$ = operator_stoken ("=="); }
1276 	|	OPERATOR NOTEQUAL
1277 			{ $$ = operator_stoken ("!="); }
1278 	|	OPERATOR LEQ
1279 			{ $$ = operator_stoken ("<="); }
1280 	|	OPERATOR GEQ
1281 			{ $$ = operator_stoken (">="); }
1282 	|	OPERATOR ANDAND
1283 			{ $$ = operator_stoken ("&&"); }
1284 	|	OPERATOR OROR
1285 			{ $$ = operator_stoken ("||"); }
1286 	|	OPERATOR INCREMENT
1287 			{ $$ = operator_stoken ("++"); }
1288 	|	OPERATOR DECREMENT
1289 			{ $$ = operator_stoken ("--"); }
1290 	|	OPERATOR ','
1291 			{ $$ = operator_stoken (","); }
1292 	|	OPERATOR ARROW_STAR
1293 			{ $$ = operator_stoken ("->*"); }
1294 	|	OPERATOR ARROW
1295 			{ $$ = operator_stoken ("->"); }
1296 	|	OPERATOR '(' ')'
1297 			{ $$ = operator_stoken ("()"); }
1298 	|	OPERATOR '[' ']'
1299 			{ $$ = operator_stoken ("[]"); }
1300 	|	OPERATOR ptype
1301 			{ char *name;
1302 			  long length;
1303 			  struct ui_file *buf = mem_fileopen ();
1304 
1305 			  c_print_type ($2, NULL, buf, -1, 0);
1306 			  name = ui_file_xstrdup (buf, &length);
1307 			  ui_file_delete (buf);
1308 			  $$ = operator_stoken (name);
1309 			  free (name);
1310 			}
1311 	;
1312 
1313 
1314 
1315 name	:	NAME { $$ = $1.stoken; }
1316 	|	BLOCKNAME { $$ = $1.stoken; }
1317 	|	TYPENAME { $$ = $1.stoken; }
1318 	|	NAME_OR_INT  { $$ = $1.stoken; }
1319 	|	UNKNOWN_CPP_NAME  { $$ = $1.stoken; }
1320 	|	operator { $$ = $1; }
1321 	;
1322 
1323 name_not_typename :	NAME
1324 	|	BLOCKNAME
1325 /* These would be useful if name_not_typename was useful, but it is just
1326    a fake for "variable", so these cause reduce/reduce conflicts because
1327    the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1328    =exp) or just an exp.  If name_not_typename was ever used in an lvalue
1329    context where only a name could occur, this might be useful.
1330   	|	NAME_OR_INT
1331  */
1332 	|	operator
1333 			{
1334 			  $$.stoken = $1;
1335 			  $$.sym = lookup_symbol ($1.ptr,
1336 						  expression_context_block,
1337 						  VAR_DOMAIN,
1338 						  &$$.is_a_field_of_this);
1339 			}
1340 	|	UNKNOWN_CPP_NAME
1341 	;
1342 
1343 %%
1344 
1345 /* Returns a stoken of the operator name given by OP (which does not
1346    include the string "operator").  */
1347 static struct stoken
1348 operator_stoken (const char *op)
1349 {
1350   static const char *operator_string = "operator";
1351   struct stoken st = { NULL, 0 };
1352   st.length = strlen (operator_string) + strlen (op);
1353   st.ptr = malloc (st.length + 1);
1354   strcpy (st.ptr, operator_string);
1355   strcat (st.ptr, op);
1356 
1357   /* The toplevel (c_parse) will free the memory allocated here.  */
1358   make_cleanup (free, st.ptr);
1359   return st;
1360 };
1361 
1362 /* Take care of parsing a number (anything that starts with a digit).
1363    Set yylval and return the token type; update lexptr.
1364    LEN is the number of characters in it.  */
1365 
1366 /*** Needs some error checking for the float case ***/
1367 
1368 static int
1369 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1370 {
1371   /* FIXME: Shouldn't these be unsigned?  We don't deal with negative values
1372      here, and we do kind of silly things like cast to unsigned.  */
1373   LONGEST n = 0;
1374   LONGEST prevn = 0;
1375   ULONGEST un;
1376 
1377   int i = 0;
1378   int c;
1379   int base = input_radix;
1380   int unsigned_p = 0;
1381 
1382   /* Number of "L" suffixes encountered.  */
1383   int long_p = 0;
1384 
1385   /* We have found a "L" or "U" suffix.  */
1386   int found_suffix = 0;
1387 
1388   ULONGEST high_bit;
1389   struct type *signed_type;
1390   struct type *unsigned_type;
1391 
1392   if (parsed_float)
1393     {
1394       const char *suffix;
1395       int suffix_len;
1396 
1397       /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1398          point.  Return DECFLOAT.  */
1399 
1400       if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1401 	{
1402 	  p[len - 2] = '\0';
1403 	  putithere->typed_val_decfloat.type
1404 	    = parse_type->builtin_decfloat;
1405 	  decimal_from_string (putithere->typed_val_decfloat.val, 4,
1406 			       gdbarch_byte_order (parse_gdbarch), p);
1407 	  p[len - 2] = 'd';
1408 	  return DECFLOAT;
1409 	}
1410 
1411       if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1412 	{
1413 	  p[len - 2] = '\0';
1414 	  putithere->typed_val_decfloat.type
1415 	    = parse_type->builtin_decdouble;
1416 	  decimal_from_string (putithere->typed_val_decfloat.val, 8,
1417 			       gdbarch_byte_order (parse_gdbarch), p);
1418 	  p[len - 2] = 'd';
1419 	  return DECFLOAT;
1420 	}
1421 
1422       if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1423 	{
1424 	  p[len - 2] = '\0';
1425 	  putithere->typed_val_decfloat.type
1426 	    = parse_type->builtin_declong;
1427 	  decimal_from_string (putithere->typed_val_decfloat.val, 16,
1428 			       gdbarch_byte_order (parse_gdbarch), p);
1429 	  p[len - 2] = 'd';
1430 	  return DECFLOAT;
1431 	}
1432 
1433       if (! parse_c_float (parse_gdbarch, p, len,
1434 			   &putithere->typed_val_float.dval,
1435 			   &putithere->typed_val_float.type))
1436 	return ERROR;
1437       return FLOAT;
1438     }
1439 
1440   /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1441   if (p[0] == '0')
1442     switch (p[1])
1443       {
1444       case 'x':
1445       case 'X':
1446 	if (len >= 3)
1447 	  {
1448 	    p += 2;
1449 	    base = 16;
1450 	    len -= 2;
1451 	  }
1452 	break;
1453 
1454       case 'b':
1455       case 'B':
1456 	if (len >= 3)
1457 	  {
1458 	    p += 2;
1459 	    base = 2;
1460 	    len -= 2;
1461 	  }
1462 	break;
1463 
1464       case 't':
1465       case 'T':
1466       case 'd':
1467       case 'D':
1468 	if (len >= 3)
1469 	  {
1470 	    p += 2;
1471 	    base = 10;
1472 	    len -= 2;
1473 	  }
1474 	break;
1475 
1476       default:
1477 	base = 8;
1478 	break;
1479       }
1480 
1481   while (len-- > 0)
1482     {
1483       c = *p++;
1484       if (c >= 'A' && c <= 'Z')
1485 	c += 'a' - 'A';
1486       if (c != 'l' && c != 'u')
1487 	n *= base;
1488       if (c >= '0' && c <= '9')
1489 	{
1490 	  if (found_suffix)
1491 	    return ERROR;
1492 	  n += i = c - '0';
1493 	}
1494       else
1495 	{
1496 	  if (base > 10 && c >= 'a' && c <= 'f')
1497 	    {
1498 	      if (found_suffix)
1499 		return ERROR;
1500 	      n += i = c - 'a' + 10;
1501 	    }
1502 	  else if (c == 'l')
1503 	    {
1504 	      ++long_p;
1505 	      found_suffix = 1;
1506 	    }
1507 	  else if (c == 'u')
1508 	    {
1509 	      unsigned_p = 1;
1510 	      found_suffix = 1;
1511 	    }
1512 	  else
1513 	    return ERROR;	/* Char not a digit */
1514 	}
1515       if (i >= base)
1516 	return ERROR;		/* Invalid digit in this base */
1517 
1518       /* Portably test for overflow (only works for nonzero values, so make
1519 	 a second check for zero).  FIXME: Can't we just make n and prevn
1520 	 unsigned and avoid this?  */
1521       if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1522 	unsigned_p = 1;		/* Try something unsigned */
1523 
1524       /* Portably test for unsigned overflow.
1525 	 FIXME: This check is wrong; for example it doesn't find overflow
1526 	 on 0x123456789 when LONGEST is 32 bits.  */
1527       if (c != 'l' && c != 'u' && n != 0)
1528 	{
1529 	  if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1530 	    error (_("Numeric constant too large."));
1531 	}
1532       prevn = n;
1533     }
1534 
1535   /* An integer constant is an int, a long, or a long long.  An L
1536      suffix forces it to be long; an LL suffix forces it to be long
1537      long.  If not forced to a larger size, it gets the first type of
1538      the above that it fits in.  To figure out whether it fits, we
1539      shift it right and see whether anything remains.  Note that we
1540      can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1541      operation, because many compilers will warn about such a shift
1542      (which always produces a zero result).  Sometimes gdbarch_int_bit
1543      or gdbarch_long_bit will be that big, sometimes not.  To deal with
1544      the case where it is we just always shift the value more than
1545      once, with fewer bits each time.  */
1546 
1547   un = (ULONGEST)n >> 2;
1548   if (long_p == 0
1549       && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1550     {
1551       high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1552 
1553       /* A large decimal (not hex or octal) constant (between INT_MAX
1554 	 and UINT_MAX) is a long or unsigned long, according to ANSI,
1555 	 never an unsigned int, but this code treats it as unsigned
1556 	 int.  This probably should be fixed.  GCC gives a warning on
1557 	 such constants.  */
1558 
1559       unsigned_type = parse_type->builtin_unsigned_int;
1560       signed_type = parse_type->builtin_int;
1561     }
1562   else if (long_p <= 1
1563 	   && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1564     {
1565       high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1566       unsigned_type = parse_type->builtin_unsigned_long;
1567       signed_type = parse_type->builtin_long;
1568     }
1569   else
1570     {
1571       int shift;
1572       if (sizeof (ULONGEST) * HOST_CHAR_BIT
1573 	  < gdbarch_long_long_bit (parse_gdbarch))
1574 	/* A long long does not fit in a LONGEST.  */
1575 	shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1576       else
1577 	shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1578       high_bit = (ULONGEST) 1 << shift;
1579       unsigned_type = parse_type->builtin_unsigned_long_long;
1580       signed_type = parse_type->builtin_long_long;
1581     }
1582 
1583    putithere->typed_val_int.val = n;
1584 
1585    /* If the high bit of the worked out type is set then this number
1586       has to be unsigned. */
1587 
1588    if (unsigned_p || (n & high_bit))
1589      {
1590        putithere->typed_val_int.type = unsigned_type;
1591      }
1592    else
1593      {
1594        putithere->typed_val_int.type = signed_type;
1595      }
1596 
1597    return INT;
1598 }
1599 
1600 /* Temporary obstack used for holding strings.  */
1601 static struct obstack tempbuf;
1602 static int tempbuf_init;
1603 
1604 /* Parse a C escape sequence.  The initial backslash of the sequence
1605    is at (*PTR)[-1].  *PTR will be updated to point to just after the
1606    last character of the sequence.  If OUTPUT is not NULL, the
1607    translated form of the escape sequence will be written there.  If
1608    OUTPUT is NULL, no output is written and the call will only affect
1609    *PTR.  If an escape sequence is expressed in target bytes, then the
1610    entire sequence will simply be copied to OUTPUT.  Return 1 if any
1611    character was emitted, 0 otherwise.  */
1612 
1613 int
1614 c_parse_escape (char **ptr, struct obstack *output)
1615 {
1616   char *tokptr = *ptr;
1617   int result = 1;
1618 
1619   /* Some escape sequences undergo character set conversion.  Those we
1620      translate here.  */
1621   switch (*tokptr)
1622     {
1623       /* Hex escapes do not undergo character set conversion, so keep
1624 	 the escape sequence for later.  */
1625     case 'x':
1626       if (output)
1627 	obstack_grow_str (output, "\\x");
1628       ++tokptr;
1629       if (!isxdigit (*tokptr))
1630 	error (_("\\x escape without a following hex digit"));
1631       while (isxdigit (*tokptr))
1632 	{
1633 	  if (output)
1634 	    obstack_1grow (output, *tokptr);
1635 	  ++tokptr;
1636 	}
1637       break;
1638 
1639       /* Octal escapes do not undergo character set conversion, so
1640 	 keep the escape sequence for later.  */
1641     case '0':
1642     case '1':
1643     case '2':
1644     case '3':
1645     case '4':
1646     case '5':
1647     case '6':
1648     case '7':
1649       {
1650 	int i;
1651 	if (output)
1652 	  obstack_grow_str (output, "\\");
1653 	for (i = 0;
1654 	     i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1655 	     ++i)
1656 	  {
1657 	    if (output)
1658 	      obstack_1grow (output, *tokptr);
1659 	    ++tokptr;
1660 	  }
1661       }
1662       break;
1663 
1664       /* We handle UCNs later.  We could handle them here, but that
1665 	 would mean a spurious error in the case where the UCN could
1666 	 be converted to the target charset but not the host
1667 	 charset.  */
1668     case 'u':
1669     case 'U':
1670       {
1671 	char c = *tokptr;
1672 	int i, len = c == 'U' ? 8 : 4;
1673 	if (output)
1674 	  {
1675 	    obstack_1grow (output, '\\');
1676 	    obstack_1grow (output, *tokptr);
1677 	  }
1678 	++tokptr;
1679 	if (!isxdigit (*tokptr))
1680 	  error (_("\\%c escape without a following hex digit"), c);
1681 	for (i = 0; i < len && isxdigit (*tokptr); ++i)
1682 	  {
1683 	    if (output)
1684 	      obstack_1grow (output, *tokptr);
1685 	    ++tokptr;
1686 	  }
1687       }
1688       break;
1689 
1690       /* We must pass backslash through so that it does not
1691 	 cause quoting during the second expansion.  */
1692     case '\\':
1693       if (output)
1694 	obstack_grow_str (output, "\\\\");
1695       ++tokptr;
1696       break;
1697 
1698       /* Escapes which undergo conversion.  */
1699     case 'a':
1700       if (output)
1701 	obstack_1grow (output, '\a');
1702       ++tokptr;
1703       break;
1704     case 'b':
1705       if (output)
1706 	obstack_1grow (output, '\b');
1707       ++tokptr;
1708       break;
1709     case 'f':
1710       if (output)
1711 	obstack_1grow (output, '\f');
1712       ++tokptr;
1713       break;
1714     case 'n':
1715       if (output)
1716 	obstack_1grow (output, '\n');
1717       ++tokptr;
1718       break;
1719     case 'r':
1720       if (output)
1721 	obstack_1grow (output, '\r');
1722       ++tokptr;
1723       break;
1724     case 't':
1725       if (output)
1726 	obstack_1grow (output, '\t');
1727       ++tokptr;
1728       break;
1729     case 'v':
1730       if (output)
1731 	obstack_1grow (output, '\v');
1732       ++tokptr;
1733       break;
1734 
1735       /* GCC extension.  */
1736     case 'e':
1737       if (output)
1738 	obstack_1grow (output, HOST_ESCAPE_CHAR);
1739       ++tokptr;
1740       break;
1741 
1742       /* Backslash-newline expands to nothing at all.  */
1743     case '\n':
1744       ++tokptr;
1745       result = 0;
1746       break;
1747 
1748       /* A few escapes just expand to the character itself.  */
1749     case '\'':
1750     case '\"':
1751     case '?':
1752       /* GCC extensions.  */
1753     case '(':
1754     case '{':
1755     case '[':
1756     case '%':
1757       /* Unrecognized escapes turn into the character itself.  */
1758     default:
1759       if (output)
1760 	obstack_1grow (output, *tokptr);
1761       ++tokptr;
1762       break;
1763     }
1764   *ptr = tokptr;
1765   return result;
1766 }
1767 
1768 /* Parse a string or character literal from TOKPTR.  The string or
1769    character may be wide or unicode.  *OUTPTR is set to just after the
1770    end of the literal in the input string.  The resulting token is
1771    stored in VALUE.  This returns a token value, either STRING or
1772    CHAR, depending on what was parsed.  *HOST_CHARS is set to the
1773    number of host characters in the literal.  */
1774 static int
1775 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1776 		      int *host_chars)
1777 {
1778   int quote;
1779   enum c_string_type type;
1780 
1781   /* Build the gdb internal form of the input string in tempbuf.  Note
1782      that the buffer is null byte terminated *only* for the
1783      convenience of debugging gdb itself and printing the buffer
1784      contents when the buffer contains no embedded nulls.  Gdb does
1785      not depend upon the buffer being null byte terminated, it uses
1786      the length string instead.  This allows gdb to handle C strings
1787      (as well as strings in other languages) with embedded null
1788      bytes */
1789 
1790   if (!tempbuf_init)
1791     tempbuf_init = 1;
1792   else
1793     obstack_free (&tempbuf, NULL);
1794   obstack_init (&tempbuf);
1795 
1796   /* Record the string type.  */
1797   if (*tokptr == 'L')
1798     {
1799       type = C_WIDE_STRING;
1800       ++tokptr;
1801     }
1802   else if (*tokptr == 'u')
1803     {
1804       type = C_STRING_16;
1805       ++tokptr;
1806     }
1807   else if (*tokptr == 'U')
1808     {
1809       type = C_STRING_32;
1810       ++tokptr;
1811     }
1812   else
1813     type = C_STRING;
1814 
1815   /* Skip the quote.  */
1816   quote = *tokptr;
1817   if (quote == '\'')
1818     type |= C_CHAR;
1819   ++tokptr;
1820 
1821   *host_chars = 0;
1822 
1823   while (*tokptr)
1824     {
1825       char c = *tokptr;
1826       if (c == '\\')
1827 	{
1828 	  ++tokptr;
1829 	  *host_chars += c_parse_escape (&tokptr, &tempbuf);
1830 	}
1831       else if (c == quote)
1832 	break;
1833       else
1834 	{
1835 	  obstack_1grow (&tempbuf, c);
1836 	  ++tokptr;
1837 	  /* FIXME: this does the wrong thing with multi-byte host
1838 	     characters.  We could use mbrlen here, but that would
1839 	     make "set host-charset" a bit less useful.  */
1840 	  ++*host_chars;
1841 	}
1842     }
1843 
1844   if (*tokptr != quote)
1845     {
1846       if (quote == '"')
1847 	error (_("Unterminated string in expression."));
1848       else
1849 	error (_("Unmatched single quote."));
1850     }
1851   ++tokptr;
1852 
1853   value->type = type;
1854   value->ptr = obstack_base (&tempbuf);
1855   value->length = obstack_object_size (&tempbuf);
1856 
1857   *outptr = tokptr;
1858 
1859   return quote == '"' ? STRING : CHAR;
1860 }
1861 
1862 struct token
1863 {
1864   char *operator;
1865   int token;
1866   enum exp_opcode opcode;
1867   int cxx_only;
1868 };
1869 
1870 static const struct token tokentab3[] =
1871   {
1872     {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1873     {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
1874     {"->*", ARROW_STAR, BINOP_END, 1}
1875   };
1876 
1877 static const struct token tokentab2[] =
1878   {
1879     {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1880     {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1881     {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1882     {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
1883     {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
1884     {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
1885     {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
1886     {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
1887     {"++", INCREMENT, BINOP_END, 0},
1888     {"--", DECREMENT, BINOP_END, 0},
1889     {"->", ARROW, BINOP_END, 0},
1890     {"&&", ANDAND, BINOP_END, 0},
1891     {"||", OROR, BINOP_END, 0},
1892     /* "::" is *not* only C++: gdb overrides its meaning in several
1893        different ways, e.g., 'filename'::func, function::variable.  */
1894     {"::", COLONCOLON, BINOP_END, 0},
1895     {"<<", LSH, BINOP_END, 0},
1896     {">>", RSH, BINOP_END, 0},
1897     {"==", EQUAL, BINOP_END, 0},
1898     {"!=", NOTEQUAL, BINOP_END, 0},
1899     {"<=", LEQ, BINOP_END, 0},
1900     {">=", GEQ, BINOP_END, 0},
1901     {".*", DOT_STAR, BINOP_END, 1}
1902   };
1903 
1904 /* Identifier-like tokens.  */
1905 static const struct token ident_tokens[] =
1906   {
1907     {"unsigned", UNSIGNED, OP_NULL, 0},
1908     {"template", TEMPLATE, OP_NULL, 1},
1909     {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
1910     {"struct", STRUCT, OP_NULL, 0},
1911     {"signed", SIGNED_KEYWORD, OP_NULL, 0},
1912     {"sizeof", SIZEOF, OP_NULL, 0},
1913     {"double", DOUBLE_KEYWORD, OP_NULL, 0},
1914     {"false", FALSEKEYWORD, OP_NULL, 1},
1915     {"class", CLASS, OP_NULL, 1},
1916     {"union", UNION, OP_NULL, 0},
1917     {"short", SHORT, OP_NULL, 0},
1918     {"const", CONST_KEYWORD, OP_NULL, 0},
1919     {"enum", ENUM, OP_NULL, 0},
1920     {"long", LONG, OP_NULL, 0},
1921     {"true", TRUEKEYWORD, OP_NULL, 1},
1922     {"int", INT_KEYWORD, OP_NULL, 0},
1923     {"new", NEW, OP_NULL, 1},
1924     {"delete", DELETE, OP_NULL, 1},
1925     {"operator", OPERATOR, OP_NULL, 1},
1926 
1927     {"and", ANDAND, BINOP_END, 1},
1928     {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
1929     {"bitand", '&', OP_NULL, 1},
1930     {"bitor", '|', OP_NULL, 1},
1931     {"compl", '~', OP_NULL, 1},
1932     {"not", '!', OP_NULL, 1},
1933     {"not_eq", NOTEQUAL, BINOP_END, 1},
1934     {"or", OROR, BINOP_END, 1},
1935     {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
1936     {"xor", '^', OP_NULL, 1},
1937     {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1},
1938 
1939     {"const_cast", CONST_CAST, OP_NULL, 1 },
1940     {"dynamic_cast", DYNAMIC_CAST, OP_NULL, 1 },
1941     {"static_cast", STATIC_CAST, OP_NULL, 1 },
1942     {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, 1 }
1943   };
1944 
1945 /* When we find that lexptr (the global var defined in parse.c) is
1946    pointing at a macro invocation, we expand the invocation, and call
1947    scan_macro_expansion to save the old lexptr here and point lexptr
1948    into the expanded text.  When we reach the end of that, we call
1949    end_macro_expansion to pop back to the value we saved here.  The
1950    macro expansion code promises to return only fully-expanded text,
1951    so we don't need to "push" more than one level.
1952 
1953    This is disgusting, of course.  It would be cleaner to do all macro
1954    expansion beforehand, and then hand that to lexptr.  But we don't
1955    really know where the expression ends.  Remember, in a command like
1956 
1957      (gdb) break *ADDRESS if CONDITION
1958 
1959    we evaluate ADDRESS in the scope of the current frame, but we
1960    evaluate CONDITION in the scope of the breakpoint's location.  So
1961    it's simply wrong to try to macro-expand the whole thing at once.  */
1962 static char *macro_original_text;
1963 
1964 /* We save all intermediate macro expansions on this obstack for the
1965    duration of a single parse.  The expansion text may sometimes have
1966    to live past the end of the expansion, due to yacc lookahead.
1967    Rather than try to be clever about saving the data for a single
1968    token, we simply keep it all and delete it after parsing has
1969    completed.  */
1970 static struct obstack expansion_obstack;
1971 
1972 static void
1973 scan_macro_expansion (char *expansion)
1974 {
1975   char *copy;
1976 
1977   /* We'd better not be trying to push the stack twice.  */
1978   gdb_assert (! macro_original_text);
1979 
1980   /* Copy to the obstack, and then free the intermediate
1981      expansion.  */
1982   copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
1983   xfree (expansion);
1984 
1985   /* Save the old lexptr value, so we can return to it when we're done
1986      parsing the expanded text.  */
1987   macro_original_text = lexptr;
1988   lexptr = copy;
1989 }
1990 
1991 
1992 static int
1993 scanning_macro_expansion (void)
1994 {
1995   return macro_original_text != 0;
1996 }
1997 
1998 
1999 static void
2000 finished_macro_expansion (void)
2001 {
2002   /* There'd better be something to pop back to.  */
2003   gdb_assert (macro_original_text);
2004 
2005   /* Pop back to the original text.  */
2006   lexptr = macro_original_text;
2007   macro_original_text = 0;
2008 }
2009 
2010 
2011 static void
2012 scan_macro_cleanup (void *dummy)
2013 {
2014   if (macro_original_text)
2015     finished_macro_expansion ();
2016 
2017   obstack_free (&expansion_obstack, NULL);
2018 }
2019 
2020 /* Return true iff the token represents a C++ cast operator.  */
2021 
2022 static int
2023 is_cast_operator (const char *token, int len)
2024 {
2025   return (! strncmp (token, "dynamic_cast", len)
2026 	  || ! strncmp (token, "static_cast", len)
2027 	  || ! strncmp (token, "reinterpret_cast", len)
2028 	  || ! strncmp (token, "const_cast", len));
2029 }
2030 
2031 /* The scope used for macro expansion.  */
2032 static struct macro_scope *expression_macro_scope;
2033 
2034 /* This is set if a NAME token appeared at the very end of the input
2035    string, with no whitespace separating the name from the EOF.  This
2036    is used only when parsing to do field name completion.  */
2037 static int saw_name_at_eof;
2038 
2039 /* This is set if the previously-returned token was a structure
2040    operator -- either '.' or ARROW.  This is used only when parsing to
2041    do field name completion.  */
2042 static int last_was_structop;
2043 
2044 /* Read one token, getting characters through lexptr.  */
2045 
2046 static int
2047 lex_one_token (void)
2048 {
2049   int c;
2050   int namelen;
2051   unsigned int i;
2052   char *tokstart;
2053   int saw_structop = last_was_structop;
2054   char *copy;
2055 
2056   last_was_structop = 0;
2057 
2058  retry:
2059 
2060   /* Check if this is a macro invocation that we need to expand.  */
2061   if (! scanning_macro_expansion ())
2062     {
2063       char *expanded = macro_expand_next (&lexptr,
2064                                           standard_macro_lookup,
2065                                           expression_macro_scope);
2066 
2067       if (expanded)
2068         scan_macro_expansion (expanded);
2069     }
2070 
2071   prev_lexptr = lexptr;
2072 
2073   tokstart = lexptr;
2074   /* See if it is a special token of length 3.  */
2075   for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2076     if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2077       {
2078 	if (tokentab3[i].cxx_only
2079 	    && parse_language->la_language != language_cplus)
2080 	  break;
2081 
2082 	lexptr += 3;
2083 	yylval.opcode = tokentab3[i].opcode;
2084 	return tokentab3[i].token;
2085       }
2086 
2087   /* See if it is a special token of length 2.  */
2088   for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2089     if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2090       {
2091 	if (tokentab2[i].cxx_only
2092 	    && parse_language->la_language != language_cplus)
2093 	  break;
2094 
2095 	lexptr += 2;
2096 	yylval.opcode = tokentab2[i].opcode;
2097 	if (in_parse_field && tokentab2[i].token == ARROW)
2098 	  last_was_structop = 1;
2099 	return tokentab2[i].token;
2100       }
2101 
2102   switch (c = *tokstart)
2103     {
2104     case 0:
2105       /* If we were just scanning the result of a macro expansion,
2106          then we need to resume scanning the original text.
2107 	 If we're parsing for field name completion, and the previous
2108 	 token allows such completion, return a COMPLETE token.
2109          Otherwise, we were already scanning the original text, and
2110          we're really done.  */
2111       if (scanning_macro_expansion ())
2112         {
2113           finished_macro_expansion ();
2114           goto retry;
2115         }
2116       else if (saw_name_at_eof)
2117 	{
2118 	  saw_name_at_eof = 0;
2119 	  return COMPLETE;
2120 	}
2121       else if (saw_structop)
2122 	return COMPLETE;
2123       else
2124         return 0;
2125 
2126     case ' ':
2127     case '\t':
2128     case '\n':
2129       lexptr++;
2130       goto retry;
2131 
2132     case '[':
2133     case '(':
2134       paren_depth++;
2135       lexptr++;
2136       return c;
2137 
2138     case ']':
2139     case ')':
2140       if (paren_depth == 0)
2141 	return 0;
2142       paren_depth--;
2143       lexptr++;
2144       return c;
2145 
2146     case ',':
2147       if (comma_terminates
2148           && paren_depth == 0
2149           && ! scanning_macro_expansion ())
2150 	return 0;
2151       lexptr++;
2152       return c;
2153 
2154     case '.':
2155       /* Might be a floating point number.  */
2156       if (lexptr[1] < '0' || lexptr[1] > '9')
2157 	{
2158 	  if (in_parse_field)
2159 	    last_was_structop = 1;
2160 	  goto symbol;		/* Nope, must be a symbol. */
2161 	}
2162       /* FALL THRU into number case.  */
2163 
2164     case '0':
2165     case '1':
2166     case '2':
2167     case '3':
2168     case '4':
2169     case '5':
2170     case '6':
2171     case '7':
2172     case '8':
2173     case '9':
2174       {
2175 	/* It's a number.  */
2176 	int got_dot = 0, got_e = 0, toktype;
2177 	char *p = tokstart;
2178 	int hex = input_radix > 10;
2179 
2180 	if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2181 	  {
2182 	    p += 2;
2183 	    hex = 1;
2184 	  }
2185 	else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2186 	  {
2187 	    p += 2;
2188 	    hex = 0;
2189 	  }
2190 
2191 	for (;; ++p)
2192 	  {
2193 	    /* This test includes !hex because 'e' is a valid hex digit
2194 	       and thus does not indicate a floating point number when
2195 	       the radix is hex.  */
2196 	    if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2197 	      got_dot = got_e = 1;
2198 	    /* This test does not include !hex, because a '.' always indicates
2199 	       a decimal floating point number regardless of the radix.  */
2200 	    else if (!got_dot && *p == '.')
2201 	      got_dot = 1;
2202 	    else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2203 		     && (*p == '-' || *p == '+'))
2204 	      /* This is the sign of the exponent, not the end of the
2205 		 number.  */
2206 	      continue;
2207 	    /* We will take any letters or digits.  parse_number will
2208 	       complain if past the radix, or if L or U are not final.  */
2209 	    else if ((*p < '0' || *p > '9')
2210 		     && ((*p < 'a' || *p > 'z')
2211 				  && (*p < 'A' || *p > 'Z')))
2212 	      break;
2213 	  }
2214 	toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2215         if (toktype == ERROR)
2216 	  {
2217 	    char *err_copy = (char *) alloca (p - tokstart + 1);
2218 
2219 	    memcpy (err_copy, tokstart, p - tokstart);
2220 	    err_copy[p - tokstart] = 0;
2221 	    error (_("Invalid number \"%s\"."), err_copy);
2222 	  }
2223 	lexptr = p;
2224 	return toktype;
2225       }
2226 
2227     case '+':
2228     case '-':
2229     case '*':
2230     case '/':
2231     case '%':
2232     case '|':
2233     case '&':
2234     case '^':
2235     case '~':
2236     case '!':
2237     case '@':
2238     case '<':
2239     case '>':
2240     case '?':
2241     case ':':
2242     case '=':
2243     case '{':
2244     case '}':
2245     symbol:
2246       lexptr++;
2247       return c;
2248 
2249     case 'L':
2250     case 'u':
2251     case 'U':
2252       if (tokstart[1] != '"' && tokstart[1] != '\'')
2253 	break;
2254       /* Fall through.  */
2255     case '\'':
2256     case '"':
2257       {
2258 	int host_len;
2259 	int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2260 					   &host_len);
2261 	if (result == CHAR)
2262 	  {
2263 	    if (host_len == 0)
2264 	      error (_("Empty character constant."));
2265 	    else if (host_len > 2 && c == '\'')
2266 	      {
2267 		++tokstart;
2268 		namelen = lexptr - tokstart - 1;
2269 		goto tryname;
2270 	      }
2271 	    else if (host_len > 1)
2272 	      error (_("Invalid character constant."));
2273 	  }
2274 	return result;
2275       }
2276     }
2277 
2278   if (!(c == '_' || c == '$'
2279 	|| (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2280     /* We must have come across a bad character (e.g. ';').  */
2281     error (_("Invalid character '%c' in expression."), c);
2282 
2283   /* It's a name.  See how long it is.  */
2284   namelen = 0;
2285   for (c = tokstart[namelen];
2286        (c == '_' || c == '$' || (c >= '0' && c <= '9')
2287 	|| (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2288     {
2289       /* Template parameter lists are part of the name.
2290 	 FIXME: This mishandles `print $a<4&&$a>3'.  */
2291 
2292       if (c == '<')
2293 	{
2294 	  if (! is_cast_operator (tokstart, namelen))
2295 	    {
2296 	      /* Scan ahead to get rest of the template specification.  Note
2297 		 that we look ahead only when the '<' adjoins non-whitespace
2298 		 characters; for comparison expressions, e.g. "a < b > c",
2299 		 there must be spaces before the '<', etc. */
2300 
2301 	      char * p = find_template_name_end (tokstart + namelen);
2302 	      if (p)
2303 		namelen = p - tokstart;
2304 	    }
2305 	  break;
2306 	}
2307       c = tokstart[++namelen];
2308     }
2309 
2310   /* The token "if" terminates the expression and is NOT removed from
2311      the input stream.  It doesn't count if it appears in the
2312      expansion of a macro.  */
2313   if (namelen == 2
2314       && tokstart[0] == 'i'
2315       && tokstart[1] == 'f'
2316       && ! scanning_macro_expansion ())
2317     {
2318       return 0;
2319     }
2320 
2321   /* For the same reason (breakpoint conditions), "thread N"
2322      terminates the expression.  "thread" could be an identifier, but
2323      an identifier is never followed by a number without intervening
2324      punctuation.  "task" is similar.  Handle abbreviations of these,
2325      similarly to breakpoint.c:find_condition_and_thread.  */
2326   if (namelen >= 1
2327       && (strncmp (tokstart, "thread", namelen) == 0
2328 	  || strncmp (tokstart, "task", namelen) == 0)
2329       && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2330       && ! scanning_macro_expansion ())
2331     {
2332       char *p = tokstart + namelen + 1;
2333       while (*p == ' ' || *p == '\t')
2334 	p++;
2335       if (*p >= '0' && *p <= '9')
2336 	return 0;
2337     }
2338 
2339   lexptr += namelen;
2340 
2341   tryname:
2342 
2343   yylval.sval.ptr = tokstart;
2344   yylval.sval.length = namelen;
2345 
2346   /* Catch specific keywords.  */
2347   copy = copy_name (yylval.sval);
2348   for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2349     if (strcmp (copy, ident_tokens[i].operator) == 0)
2350       {
2351 	if (ident_tokens[i].cxx_only
2352 	    && parse_language->la_language != language_cplus)
2353 	  break;
2354 
2355 	/* It is ok to always set this, even though we don't always
2356 	   strictly need to.  */
2357 	yylval.opcode = ident_tokens[i].opcode;
2358 	return ident_tokens[i].token;
2359       }
2360 
2361   if (*tokstart == '$')
2362     return VARIABLE;
2363 
2364   if (in_parse_field && *lexptr == '\0')
2365     saw_name_at_eof = 1;
2366   return NAME;
2367 }
2368 
2369 /* An object of this type is pushed on a FIFO by the "outer" lexer.  */
2370 typedef struct
2371 {
2372   int token;
2373   YYSTYPE value;
2374 } token_and_value;
2375 
2376 DEF_VEC_O (token_and_value);
2377 
2378 /* A FIFO of tokens that have been read but not yet returned to the
2379    parser.  */
2380 static VEC (token_and_value) *token_fifo;
2381 
2382 /* Non-zero if the lexer should return tokens from the FIFO.  */
2383 static int popping;
2384 
2385 /* Temporary storage for c_lex; this holds symbol names as they are
2386    built up.  */
2387 static struct obstack name_obstack;
2388 
2389 /* Classify a NAME token.  The contents of the token are in `yylval'.
2390    Updates yylval and returns the new token type.  BLOCK is the block
2391    in which lookups start; this can be NULL to mean the global
2392    scope.  */
2393 static int
2394 classify_name (struct block *block)
2395 {
2396   struct symbol *sym;
2397   char *copy;
2398   int is_a_field_of_this = 0;
2399 
2400   copy = copy_name (yylval.sval);
2401 
2402   sym = lookup_symbol (copy, block, VAR_DOMAIN,
2403 		       parse_language->la_language == language_cplus
2404 		       ? &is_a_field_of_this : (int *) NULL);
2405 
2406   if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2407     {
2408       yylval.ssym.sym = sym;
2409       yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2410       return BLOCKNAME;
2411     }
2412   else if (!sym)
2413     {
2414       /* See if it's a file name. */
2415       struct symtab *symtab;
2416 
2417       symtab = lookup_symtab (copy);
2418       if (symtab)
2419 	{
2420 	  yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2421 	  return FILENAME;
2422 	}
2423     }
2424 
2425   if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2426     {
2427       yylval.tsym.type = SYMBOL_TYPE (sym);
2428       return TYPENAME;
2429     }
2430 
2431   yylval.tsym.type
2432     = language_lookup_primitive_type_by_name (parse_language,
2433 					      parse_gdbarch, copy);
2434   if (yylval.tsym.type != NULL)
2435     return TYPENAME;
2436 
2437   /* Input names that aren't symbols but ARE valid hex numbers, when
2438      the input radix permits them, can be names or numbers depending
2439      on the parse.  Note we support radixes > 16 here.  */
2440   if (!sym
2441       && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2442 	  || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2443     {
2444       YYSTYPE newlval;	/* Its value is ignored.  */
2445       int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2446       if (hextype == INT)
2447 	{
2448 	  yylval.ssym.sym = sym;
2449 	  yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2450 	  return NAME_OR_INT;
2451 	}
2452     }
2453 
2454   /* Any other kind of symbol */
2455   yylval.ssym.sym = sym;
2456   yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2457 
2458   if (sym == NULL
2459       && parse_language->la_language == language_cplus
2460       && !is_a_field_of_this
2461       && !lookup_minimal_symbol (copy, NULL, NULL))
2462     return UNKNOWN_CPP_NAME;
2463 
2464   return NAME;
2465 }
2466 
2467 /* Like classify_name, but used by the inner loop of the lexer, when a
2468    name might have already been seen.  FIRST_NAME is true if the token
2469    in `yylval' is the first component of a name, false otherwise.  If
2470    this function returns NAME, it might not have updated `yylval'.
2471    This is ok because the caller only cares about TYPENAME.  */
2472 static int
2473 classify_inner_name (struct block *block, int first_name)
2474 {
2475   struct type *type, *new_type;
2476   char *copy;
2477 
2478   if (first_name)
2479     return classify_name (block);
2480 
2481   type = check_typedef (yylval.tsym.type);
2482   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2483       && TYPE_CODE (type) != TYPE_CODE_UNION
2484       && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2485     /* We know the caller won't expect us to update yylval.  */
2486     return NAME;
2487 
2488   copy = copy_name (yylval.tsym.stoken);
2489   new_type = cp_lookup_nested_type (type, copy, block);
2490 
2491   if (new_type == NULL)
2492     /* We know the caller won't expect us to update yylval.  */
2493     return NAME;
2494 
2495   yylval.tsym.type = new_type;
2496   return TYPENAME;
2497 }
2498 
2499 /* The outer level of a two-level lexer.  This calls the inner lexer
2500    to return tokens.  It then either returns these tokens, or
2501    aggregates them into a larger token.  This lets us work around a
2502    problem in our parsing approach, where the parser could not
2503    distinguish between qualified names and qualified types at the
2504    right point.
2505 
2506    This approach is still not ideal, because it mishandles template
2507    types.  See the comment in lex_one_token for an example.  However,
2508    this is still an improvement over the earlier approach, and will
2509    suffice until we move to better parsing technology.  */
2510 static int
2511 yylex (void)
2512 {
2513   token_and_value current;
2514   int first_was_coloncolon, last_was_coloncolon, first_iter;
2515 
2516   if (popping && !VEC_empty (token_and_value, token_fifo))
2517     {
2518       token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2519       VEC_ordered_remove (token_and_value, token_fifo, 0);
2520       yylval = tv.value;
2521       return tv.token;
2522     }
2523   popping = 0;
2524 
2525   current.token = lex_one_token ();
2526   if (current.token == NAME)
2527     current.token = classify_name (expression_context_block);
2528   if (parse_language->la_language != language_cplus
2529       || (current.token != TYPENAME && current.token != COLONCOLON))
2530     return current.token;
2531 
2532   first_was_coloncolon = current.token == COLONCOLON;
2533   last_was_coloncolon = first_was_coloncolon;
2534   obstack_free (&name_obstack, obstack_base (&name_obstack));
2535   if (!last_was_coloncolon)
2536     obstack_grow (&name_obstack, yylval.sval.ptr, yylval.sval.length);
2537   current.value = yylval;
2538   first_iter = 1;
2539   while (1)
2540     {
2541       token_and_value next;
2542 
2543       next.token = lex_one_token ();
2544       next.value = yylval;
2545 
2546       if (next.token == NAME && last_was_coloncolon)
2547 	{
2548 	  int classification;
2549 
2550 	  classification = classify_inner_name (first_was_coloncolon
2551 						? NULL
2552 						: expression_context_block,
2553 						first_iter);
2554 	  /* We keep going until we either run out of names, or until
2555 	     we have a qualified name which is not a type.  */
2556 	  if (classification != TYPENAME)
2557 	    {
2558 	      /* Push the final component and leave the loop.  */
2559 	      VEC_safe_push (token_and_value, token_fifo, &next);
2560 	      break;
2561 	    }
2562 
2563 	  /* Update the partial name we are constructing.  */
2564 	  if (!first_iter)
2565 	    {
2566 	      /* We don't want to put a leading "::" into the name.  */
2567 	      obstack_grow_str (&name_obstack, "::");
2568 	    }
2569 	  obstack_grow (&name_obstack, next.value.sval.ptr,
2570 			next.value.sval.length);
2571 
2572 	  yylval.sval.ptr = obstack_base (&name_obstack);
2573 	  yylval.sval.length = obstack_object_size (&name_obstack);
2574 	  current.value = yylval;
2575 	  current.token = classification;
2576 
2577 	  last_was_coloncolon = 0;
2578 	}
2579       else if (next.token == COLONCOLON && !last_was_coloncolon)
2580 	last_was_coloncolon = 1;
2581       else
2582 	{
2583 	  /* We've reached the end of the name.  */
2584 	  VEC_safe_push (token_and_value, token_fifo, &next);
2585 	  break;
2586 	}
2587 
2588       first_iter = 0;
2589     }
2590 
2591   popping = 1;
2592 
2593   /* If we ended with a "::", insert it too.  */
2594   if (last_was_coloncolon)
2595     {
2596       token_and_value cc;
2597       memset (&cc, 0, sizeof (token_and_value));
2598       if (first_was_coloncolon && first_iter)
2599 	{
2600 	  yylval = cc.value;
2601 	  return COLONCOLON;
2602 	}
2603       cc.token = COLONCOLON;
2604       VEC_safe_insert (token_and_value, token_fifo, 0, &cc);
2605     }
2606 
2607   yylval = current.value;
2608   yylval.sval.ptr = obstack_copy0 (&expansion_obstack,
2609 				   yylval.sval.ptr,
2610 				   yylval.sval.length);
2611   return current.token;
2612 }
2613 
2614 int
2615 c_parse (void)
2616 {
2617   int result;
2618   struct cleanup *back_to = make_cleanup (free_current_contents,
2619 					  &expression_macro_scope);
2620 
2621   /* Set up the scope for macro expansion.  */
2622   expression_macro_scope = NULL;
2623 
2624   if (expression_context_block)
2625     expression_macro_scope
2626       = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2627   else
2628     expression_macro_scope = default_macro_scope ();
2629   if (! expression_macro_scope)
2630     expression_macro_scope = user_macro_scope ();
2631 
2632   /* Initialize macro expansion code.  */
2633   obstack_init (&expansion_obstack);
2634   gdb_assert (! macro_original_text);
2635   make_cleanup (scan_macro_cleanup, 0);
2636 
2637   make_cleanup_restore_integer (&yydebug);
2638   yydebug = parser_debug;
2639 
2640   /* Initialize some state used by the lexer.  */
2641   last_was_structop = 0;
2642   saw_name_at_eof = 0;
2643 
2644   VEC_free (token_and_value, token_fifo);
2645   popping = 0;
2646   obstack_init (&name_obstack);
2647   make_cleanup_obstack_free (&name_obstack);
2648 
2649   result = yyparse ();
2650   do_cleanups (back_to);
2651   return result;
2652 }
2653 
2654 
2655 void
2656 yyerror (char *msg)
2657 {
2658   if (prev_lexptr)
2659     lexptr = prev_lexptr;
2660 
2661   error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);
2662 }
2663