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