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