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