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