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