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