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