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