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