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