]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/c-exp.y
* gdbreplay.c (gdbreplay_version): Say gdbreplay in version
[thirdparty/binutils-gdb.git] / gdb / c-exp.y
CommitLineData
c906108c 1/* YACC parser for C expressions, for GDB.
197e01b6 2 Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
9b254dd1
DJ
3 1998, 1999, 2000, 2003, 2004, 2006, 2007, 2008
4 Free Software Foundation, Inc.
c906108c
SS
5
6This file is part of GDB.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
197e01b6
EZ
20Foundation, Inc., 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, USA. */
c906108c
SS
22
23/* Parse a C expression from text in a string,
24 and return the result as a struct expression pointer.
25 That structure contains arithmetic operations in reverse polish,
26 with constants represented by operations that are followed by special data.
27 See expression.h for the details of the format.
28 What is important here is that it can be built up sequentially
29 during the process of parsing; the lower levels of the tree always
30 come first in the result.
31
32 Note that malloc's and realloc's in this file are transformed to
33 xmalloc and xrealloc respectively by the same sed command in the
34 makefile that remaps any other malloc/realloc inserted by the parser
35 generator. Doing this with #defines and trying to control the interaction
36 with include files (<malloc.h> and <stdlib.h> for example) just became
37 too messy, particularly when such includes can be inserted at random
38 times by the parser generator. */
39
40%{
41
42#include "defs.h"
43#include "gdb_string.h"
44#include <ctype.h>
45#include "expression.h"
46#include "value.h"
47#include "parser-defs.h"
48#include "language.h"
49#include "c-lang.h"
50#include "bfd.h" /* Required by objfiles.h. */
51#include "symfile.h" /* Required by objfiles.h. */
52#include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
234b45d4 53#include "charset.h"
fe898f56 54#include "block.h"
79c2c32d 55#include "cp-support.h"
27bc4d80 56#include "dfp.h"
c906108c 57
c906108c
SS
58/* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
59 as well as gratuitiously global symbol names, so we can have multiple
60 yacc generated parsers in gdb. Note that these are only the variables
61 produced by yacc. If other parser generators (bison, byacc, etc) produce
62 additional global names that conflict at link time, then those parser
63 generators need to be fixed instead of adding those names to this list. */
64
65#define yymaxdepth c_maxdepth
66#define yyparse c_parse
67#define yylex c_lex
68#define yyerror c_error
69#define yylval c_lval
70#define yychar c_char
71#define yydebug c_debug
72#define yypact c_pact
73#define yyr1 c_r1
74#define yyr2 c_r2
75#define yydef c_def
76#define yychk c_chk
77#define yypgo c_pgo
78#define yyact c_act
79#define yyexca c_exca
80#define yyerrflag c_errflag
81#define yynerrs c_nerrs
82#define yyps c_ps
83#define yypv c_pv
84#define yys c_s
85#define yy_yys c_yys
86#define yystate c_state
87#define yytmp c_tmp
88#define yyv c_v
89#define yy_yyv c_yyv
90#define yyval c_val
91#define yylloc c_lloc
92#define yyreds c_reds /* With YYDEBUG defined */
93#define yytoks c_toks /* With YYDEBUG defined */
06891d83
JT
94#define yyname c_name /* With YYDEBUG defined */
95#define yyrule c_rule /* With YYDEBUG defined */
c906108c
SS
96#define yylhs c_yylhs
97#define yylen c_yylen
98#define yydefred c_yydefred
99#define yydgoto c_yydgoto
100#define yysindex c_yysindex
101#define yyrindex c_yyrindex
102#define yygindex c_yygindex
103#define yytable c_yytable
104#define yycheck c_yycheck
105
106#ifndef YYDEBUG
f461f5cf 107#define YYDEBUG 1 /* Default to yydebug support */
c906108c
SS
108#endif
109
f461f5cf
PM
110#define YYFPRINTF parser_fprintf
111
a14ed312 112int yyparse (void);
c906108c 113
a14ed312 114static int yylex (void);
c906108c 115
a14ed312 116void yyerror (char *);
c906108c
SS
117
118%}
119
120/* Although the yacc "value" of an expression is not used,
121 since the result is stored in the structure being created,
122 other node types do have values. */
123
124%union
125 {
126 LONGEST lval;
127 struct {
128 LONGEST val;
129 struct type *type;
130 } typed_val_int;
131 struct {
132 DOUBLEST dval;
133 struct type *type;
134 } typed_val_float;
27bc4d80
TJB
135 struct {
136 gdb_byte val[16];
137 struct type *type;
138 } typed_val_decfloat;
c906108c
SS
139 struct symbol *sym;
140 struct type *tval;
141 struct stoken sval;
142 struct ttype tsym;
143 struct symtoken ssym;
144 int voidval;
145 struct block *bval;
146 enum exp_opcode opcode;
147 struct internalvar *ivar;
148
149 struct type **tvec;
150 int *ivec;
151 }
152
153%{
154/* YYSTYPE gets defined by %union */
a14ed312 155static int parse_number (char *, int, int, YYSTYPE *);
c906108c
SS
156%}
157
158%type <voidval> exp exp1 type_exp start variable qualified_name lcurly
159%type <lval> rcurly
79c2c32d 160%type <tval> type typebase qualified_type
c906108c
SS
161%type <tvec> nonempty_typelist
162/* %type <bval> block */
163
164/* Fancy type parsing. */
165%type <voidval> func_mod direct_abs_decl abs_decl
166%type <tval> ptype
167%type <lval> array_mod
168
169%token <typed_val_int> INT
170%token <typed_val_float> FLOAT
27bc4d80 171%token <typed_val_decfloat> DECFLOAT
c906108c
SS
172
173/* Both NAME and TYPENAME tokens represent symbols in the input,
174 and both convey their data as strings.
175 But a TYPENAME is a string that happens to be defined as a typedef
176 or builtin type name (such as int or char)
177 and a NAME is any other symbol.
178 Contexts where this distinction is not important can use the
179 nonterminal "name", which matches either NAME or TYPENAME. */
180
181%token <sval> STRING
182%token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
183%token <tsym> TYPENAME
184%type <sval> name
185%type <ssym> name_not_typename
186%type <tsym> typename
187
188/* A NAME_OR_INT is a symbol which is not known in the symbol table,
189 but which would parse as a valid number in the current input radix.
190 E.g. "c" when input_radix==16. Depending on the parse, it will be
191 turned into a name or into a number. */
192
193%token <ssym> NAME_OR_INT
194
195%token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
196%token TEMPLATE
197%token ERROR
198
199/* Special type cases, put in to allow the parser to distinguish different
200 legal basetypes. */
201%token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
202
203%token <voidval> VARIABLE
204
205%token <opcode> ASSIGN_MODIFY
206
207/* C++ */
c906108c
SS
208%token TRUEKEYWORD
209%token FALSEKEYWORD
210
211
212%left ','
213%left ABOVE_COMMA
214%right '=' ASSIGN_MODIFY
215%right '?'
216%left OROR
217%left ANDAND
218%left '|'
219%left '^'
220%left '&'
221%left EQUAL NOTEQUAL
222%left '<' '>' LEQ GEQ
223%left LSH RSH
224%left '@'
225%left '+' '-'
226%left '*' '/' '%'
227%right UNARY INCREMENT DECREMENT
228%right ARROW '.' '[' '('
229%token <ssym> BLOCKNAME
230%token <bval> FILENAME
231%type <bval> block
232%left COLONCOLON
233
234\f
235%%
236
237start : exp1
238 | type_exp
239 ;
240
241type_exp: type
242 { write_exp_elt_opcode(OP_TYPE);
243 write_exp_elt_type($1);
244 write_exp_elt_opcode(OP_TYPE);}
245 ;
246
247/* Expressions, including the comma operator. */
248exp1 : exp
249 | exp1 ',' exp
250 { write_exp_elt_opcode (BINOP_COMMA); }
251 ;
252
253/* Expressions, not including the comma operator. */
254exp : '*' exp %prec UNARY
255 { write_exp_elt_opcode (UNOP_IND); }
ef944135 256 ;
c906108c
SS
257
258exp : '&' exp %prec UNARY
259 { write_exp_elt_opcode (UNOP_ADDR); }
ef944135 260 ;
c906108c
SS
261
262exp : '-' exp %prec UNARY
263 { write_exp_elt_opcode (UNOP_NEG); }
264 ;
265
36e9969c
NS
266exp : '+' exp %prec UNARY
267 { write_exp_elt_opcode (UNOP_PLUS); }
268 ;
269
c906108c
SS
270exp : '!' exp %prec UNARY
271 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
272 ;
273
274exp : '~' exp %prec UNARY
275 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
276 ;
277
278exp : INCREMENT exp %prec UNARY
279 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
280 ;
281
282exp : DECREMENT exp %prec UNARY
283 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
284 ;
285
286exp : exp INCREMENT %prec UNARY
287 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
288 ;
289
290exp : exp DECREMENT %prec UNARY
291 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
292 ;
293
294exp : SIZEOF exp %prec UNARY
295 { write_exp_elt_opcode (UNOP_SIZEOF); }
296 ;
297
298exp : exp ARROW name
299 { write_exp_elt_opcode (STRUCTOP_PTR);
300 write_exp_string ($3);
301 write_exp_elt_opcode (STRUCTOP_PTR); }
302 ;
303
304exp : exp ARROW qualified_name
305 { /* exp->type::name becomes exp->*(&type::name) */
306 /* Note: this doesn't work if name is a
307 static member! FIXME */
308 write_exp_elt_opcode (UNOP_ADDR);
309 write_exp_elt_opcode (STRUCTOP_MPTR); }
310 ;
311
312exp : exp ARROW '*' exp
313 { write_exp_elt_opcode (STRUCTOP_MPTR); }
314 ;
315
316exp : exp '.' name
317 { write_exp_elt_opcode (STRUCTOP_STRUCT);
318 write_exp_string ($3);
319 write_exp_elt_opcode (STRUCTOP_STRUCT); }
320 ;
321
322exp : exp '.' qualified_name
323 { /* exp.type::name becomes exp.*(&type::name) */
324 /* Note: this doesn't work if name is a
325 static member! FIXME */
326 write_exp_elt_opcode (UNOP_ADDR);
327 write_exp_elt_opcode (STRUCTOP_MEMBER); }
328 ;
329
330exp : exp '.' '*' exp
331 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
332 ;
333
334exp : exp '[' exp1 ']'
335 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
336 ;
337
338exp : exp '('
339 /* This is to save the value of arglist_len
340 being accumulated by an outer function call. */
341 { start_arglist (); }
342 arglist ')' %prec ARROW
343 { write_exp_elt_opcode (OP_FUNCALL);
344 write_exp_elt_longcst ((LONGEST) end_arglist ());
345 write_exp_elt_opcode (OP_FUNCALL); }
346 ;
347
348lcurly : '{'
349 { start_arglist (); }
350 ;
351
352arglist :
353 ;
354
355arglist : exp
356 { arglist_len = 1; }
357 ;
358
359arglist : arglist ',' exp %prec ABOVE_COMMA
360 { arglist_len++; }
361 ;
362
363rcurly : '}'
364 { $$ = end_arglist () - 1; }
365 ;
366exp : lcurly arglist rcurly %prec ARROW
367 { write_exp_elt_opcode (OP_ARRAY);
368 write_exp_elt_longcst ((LONGEST) 0);
369 write_exp_elt_longcst ((LONGEST) $3);
370 write_exp_elt_opcode (OP_ARRAY); }
371 ;
372
373exp : lcurly type rcurly exp %prec UNARY
374 { write_exp_elt_opcode (UNOP_MEMVAL);
375 write_exp_elt_type ($2);
376 write_exp_elt_opcode (UNOP_MEMVAL); }
377 ;
378
379exp : '(' type ')' exp %prec UNARY
380 { write_exp_elt_opcode (UNOP_CAST);
381 write_exp_elt_type ($2);
382 write_exp_elt_opcode (UNOP_CAST); }
383 ;
384
385exp : '(' exp1 ')'
386 { }
387 ;
388
389/* Binary operators in order of decreasing precedence. */
390
391exp : exp '@' exp
392 { write_exp_elt_opcode (BINOP_REPEAT); }
393 ;
394
395exp : exp '*' exp
396 { write_exp_elt_opcode (BINOP_MUL); }
397 ;
398
399exp : exp '/' exp
400 { write_exp_elt_opcode (BINOP_DIV); }
401 ;
402
403exp : exp '%' exp
404 { write_exp_elt_opcode (BINOP_REM); }
405 ;
406
407exp : exp '+' exp
408 { write_exp_elt_opcode (BINOP_ADD); }
409 ;
410
411exp : exp '-' exp
412 { write_exp_elt_opcode (BINOP_SUB); }
413 ;
414
415exp : exp LSH exp
416 { write_exp_elt_opcode (BINOP_LSH); }
417 ;
418
419exp : exp RSH exp
420 { write_exp_elt_opcode (BINOP_RSH); }
421 ;
422
423exp : exp EQUAL exp
424 { write_exp_elt_opcode (BINOP_EQUAL); }
425 ;
426
427exp : exp NOTEQUAL exp
428 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
429 ;
430
431exp : exp LEQ exp
432 { write_exp_elt_opcode (BINOP_LEQ); }
433 ;
434
435exp : exp GEQ exp
436 { write_exp_elt_opcode (BINOP_GEQ); }
437 ;
438
439exp : exp '<' exp
440 { write_exp_elt_opcode (BINOP_LESS); }
441 ;
442
443exp : exp '>' exp
444 { write_exp_elt_opcode (BINOP_GTR); }
445 ;
446
447exp : exp '&' exp
448 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
449 ;
450
451exp : exp '^' exp
452 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
453 ;
454
455exp : exp '|' exp
456 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
457 ;
458
459exp : exp ANDAND exp
460 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
461 ;
462
463exp : exp OROR exp
464 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
465 ;
466
467exp : exp '?' exp ':' exp %prec '?'
468 { write_exp_elt_opcode (TERNOP_COND); }
469 ;
470
471exp : exp '=' exp
472 { write_exp_elt_opcode (BINOP_ASSIGN); }
473 ;
474
475exp : exp ASSIGN_MODIFY exp
476 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
477 write_exp_elt_opcode ($2);
478 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
479 ;
480
481exp : INT
482 { write_exp_elt_opcode (OP_LONG);
483 write_exp_elt_type ($1.type);
484 write_exp_elt_longcst ((LONGEST)($1.val));
485 write_exp_elt_opcode (OP_LONG); }
486 ;
487
488exp : NAME_OR_INT
489 { YYSTYPE val;
490 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
491 write_exp_elt_opcode (OP_LONG);
492 write_exp_elt_type (val.typed_val_int.type);
493 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
494 write_exp_elt_opcode (OP_LONG);
495 }
496 ;
497
498
499exp : FLOAT
500 { write_exp_elt_opcode (OP_DOUBLE);
501 write_exp_elt_type ($1.type);
502 write_exp_elt_dblcst ($1.dval);
503 write_exp_elt_opcode (OP_DOUBLE); }
504 ;
505
27bc4d80
TJB
506exp : DECFLOAT
507 { write_exp_elt_opcode (OP_DECFLOAT);
508 write_exp_elt_type ($1.type);
509 write_exp_elt_decfloatcst ($1.val);
510 write_exp_elt_opcode (OP_DECFLOAT); }
511 ;
512
c906108c
SS
513exp : variable
514 ;
515
516exp : VARIABLE
517 /* Already written by write_dollar_variable. */
518 ;
519
520exp : SIZEOF '(' type ')' %prec UNARY
521 { write_exp_elt_opcode (OP_LONG);
64d2b26d 522 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_int);
c906108c
SS
523 CHECK_TYPEDEF ($3);
524 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
525 write_exp_elt_opcode (OP_LONG); }
526 ;
527
528exp : STRING
529 { /* C strings are converted into array constants with
530 an explicit null byte added at the end. Thus
531 the array upper bound is the string length.
532 There is no such thing in C as a completely empty
533 string. */
534 char *sp = $1.ptr; int count = $1.length;
535 while (count-- > 0)
536 {
537 write_exp_elt_opcode (OP_LONG);
64d2b26d 538 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_char);
c906108c
SS
539 write_exp_elt_longcst ((LONGEST)(*sp++));
540 write_exp_elt_opcode (OP_LONG);
541 }
542 write_exp_elt_opcode (OP_LONG);
64d2b26d 543 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_char);
c906108c
SS
544 write_exp_elt_longcst ((LONGEST)'\0');
545 write_exp_elt_opcode (OP_LONG);
546 write_exp_elt_opcode (OP_ARRAY);
547 write_exp_elt_longcst ((LONGEST) 0);
548 write_exp_elt_longcst ((LONGEST) ($1.length));
549 write_exp_elt_opcode (OP_ARRAY); }
550 ;
551
552/* C++. */
c906108c
SS
553exp : TRUEKEYWORD
554 { write_exp_elt_opcode (OP_LONG);
64d2b26d 555 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_bool);
c906108c
SS
556 write_exp_elt_longcst ((LONGEST) 1);
557 write_exp_elt_opcode (OP_LONG); }
558 ;
559
560exp : FALSEKEYWORD
561 { write_exp_elt_opcode (OP_LONG);
64d2b26d 562 write_exp_elt_type (builtin_type (current_gdbarch)->builtin_bool);
c906108c
SS
563 write_exp_elt_longcst ((LONGEST) 0);
564 write_exp_elt_opcode (OP_LONG); }
565 ;
566
567/* end of C++. */
568
569block : BLOCKNAME
570 {
571 if ($1.sym)
572 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
573 else
574 error ("No file or function \"%s\".",
575 copy_name ($1.stoken));
576 }
577 | FILENAME
578 {
579 $$ = $1;
580 }
581 ;
582
583block : block COLONCOLON name
584 { struct symbol *tem
585 = lookup_symbol (copy_name ($3), $1,
2570f2b7 586 VAR_DOMAIN, (int *) NULL);
c906108c
SS
587 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
588 error ("No function \"%s\" in specified context.",
589 copy_name ($3));
590 $$ = SYMBOL_BLOCK_VALUE (tem); }
591 ;
592
593variable: block COLONCOLON name
594 { struct symbol *sym;
595 sym = lookup_symbol (copy_name ($3), $1,
2570f2b7 596 VAR_DOMAIN, (int *) NULL);
c906108c
SS
597 if (sym == 0)
598 error ("No symbol \"%s\" in specified context.",
599 copy_name ($3));
600
601 write_exp_elt_opcode (OP_VAR_VALUE);
602 /* block_found is set by lookup_symbol. */
603 write_exp_elt_block (block_found);
604 write_exp_elt_sym (sym);
605 write_exp_elt_opcode (OP_VAR_VALUE); }
606 ;
607
608qualified_name: typebase COLONCOLON name
609 {
610 struct type *type = $1;
611 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
79c2c32d
DC
612 && TYPE_CODE (type) != TYPE_CODE_UNION
613 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
c906108c
SS
614 error ("`%s' is not defined as an aggregate type.",
615 TYPE_NAME (type));
616
617 write_exp_elt_opcode (OP_SCOPE);
618 write_exp_elt_type (type);
619 write_exp_string ($3);
620 write_exp_elt_opcode (OP_SCOPE);
621 }
622 | typebase COLONCOLON '~' name
623 {
624 struct type *type = $1;
625 struct stoken tmp_token;
626 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
79c2c32d
DC
627 && TYPE_CODE (type) != TYPE_CODE_UNION
628 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
c906108c
SS
629 error ("`%s' is not defined as an aggregate type.",
630 TYPE_NAME (type));
631
632 tmp_token.ptr = (char*) alloca ($4.length + 2);
633 tmp_token.length = $4.length + 1;
634 tmp_token.ptr[0] = '~';
635 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
636 tmp_token.ptr[tmp_token.length] = 0;
637
638 /* Check for valid destructor name. */
639 destructor_name_p (tmp_token.ptr, type);
640 write_exp_elt_opcode (OP_SCOPE);
641 write_exp_elt_type (type);
642 write_exp_string (tmp_token);
643 write_exp_elt_opcode (OP_SCOPE);
644 }
645 ;
646
647variable: qualified_name
648 | COLONCOLON name
649 {
650 char *name = copy_name ($2);
651 struct symbol *sym;
652 struct minimal_symbol *msymbol;
653
654 sym =
655 lookup_symbol (name, (const struct block *) NULL,
2570f2b7 656 VAR_DOMAIN, (int *) NULL);
c906108c
SS
657 if (sym)
658 {
659 write_exp_elt_opcode (OP_VAR_VALUE);
660 write_exp_elt_block (NULL);
661 write_exp_elt_sym (sym);
662 write_exp_elt_opcode (OP_VAR_VALUE);
663 break;
664 }
665
666 msymbol = lookup_minimal_symbol (name, NULL, NULL);
667 if (msymbol != NULL)
668 {
669 write_exp_msymbol (msymbol,
64d2b26d
AC
670 lookup_function_type (builtin_type (current_gdbarch)->builtin_int),
671 builtin_type (current_gdbarch)->builtin_int);
c906108c
SS
672 }
673 else
674 if (!have_full_symbols () && !have_partial_symbols ())
675 error ("No symbol table is loaded. Use the \"file\" command.");
676 else
677 error ("No symbol \"%s\" in current context.", name);
678 }
679 ;
680
681variable: name_not_typename
682 { struct symbol *sym = $1.sym;
683
684 if (sym)
685 {
686 if (symbol_read_needs_frame (sym))
687 {
688 if (innermost_block == 0 ||
689 contained_in (block_found,
690 innermost_block))
691 innermost_block = block_found;
692 }
693
694 write_exp_elt_opcode (OP_VAR_VALUE);
695 /* We want to use the selected frame, not
696 another more inner frame which happens to
697 be in the same block. */
698 write_exp_elt_block (NULL);
699 write_exp_elt_sym (sym);
700 write_exp_elt_opcode (OP_VAR_VALUE);
701 }
702 else if ($1.is_a_field_of_this)
703 {
704 /* C++: it hangs off of `this'. Must
705 not inadvertently convert from a method call
706 to data ref. */
707 if (innermost_block == 0 ||
708 contained_in (block_found, innermost_block))
709 innermost_block = block_found;
710 write_exp_elt_opcode (OP_THIS);
711 write_exp_elt_opcode (OP_THIS);
712 write_exp_elt_opcode (STRUCTOP_PTR);
713 write_exp_string ($1.stoken);
714 write_exp_elt_opcode (STRUCTOP_PTR);
715 }
716 else
717 {
718 struct minimal_symbol *msymbol;
710122da 719 char *arg = copy_name ($1.stoken);
c906108c
SS
720
721 msymbol =
722 lookup_minimal_symbol (arg, NULL, NULL);
723 if (msymbol != NULL)
724 {
725 write_exp_msymbol (msymbol,
64d2b26d
AC
726 lookup_function_type (builtin_type (current_gdbarch)->builtin_int),
727 builtin_type (current_gdbarch)->builtin_int);
c906108c
SS
728 }
729 else if (!have_full_symbols () && !have_partial_symbols ())
730 error ("No symbol table is loaded. Use the \"file\" command.");
731 else
732 error ("No symbol \"%s\" in current context.",
733 copy_name ($1.stoken));
734 }
735 }
736 ;
737
47663de5
MS
738space_identifier : '@' NAME
739 { push_type_address_space (copy_name ($2.stoken));
740 push_type (tp_space_identifier);
741 }
742 ;
c906108c 743
47663de5
MS
744const_or_volatile: const_or_volatile_noopt
745 |
c906108c 746 ;
47663de5
MS
747
748cv_with_space_id : const_or_volatile space_identifier const_or_volatile
56e2d25a 749 ;
47663de5
MS
750
751const_or_volatile_or_space_identifier_noopt: cv_with_space_id
752 | const_or_volatile_noopt
56e2d25a 753 ;
47663de5
MS
754
755const_or_volatile_or_space_identifier:
756 const_or_volatile_or_space_identifier_noopt
757 |
56e2d25a 758 ;
47663de5 759
c906108c
SS
760abs_decl: '*'
761 { push_type (tp_pointer); $$ = 0; }
762 | '*' abs_decl
763 { push_type (tp_pointer); $$ = $2; }
764 | '&'
765 { push_type (tp_reference); $$ = 0; }
766 | '&' abs_decl
767 { push_type (tp_reference); $$ = $2; }
768 | direct_abs_decl
769 ;
770
771direct_abs_decl: '(' abs_decl ')'
772 { $$ = $2; }
773 | direct_abs_decl array_mod
774 {
775 push_type_int ($2);
776 push_type (tp_array);
777 }
778 | array_mod
779 {
780 push_type_int ($1);
781 push_type (tp_array);
782 $$ = 0;
783 }
784
785 | direct_abs_decl func_mod
786 { push_type (tp_function); }
787 | func_mod
788 { push_type (tp_function); }
789 ;
790
791array_mod: '[' ']'
792 { $$ = -1; }
793 | '[' INT ']'
794 { $$ = $2.val; }
795 ;
796
797func_mod: '(' ')'
798 { $$ = 0; }
799 | '(' nonempty_typelist ')'
8dbb1c65 800 { free ($2); $$ = 0; }
c906108c
SS
801 ;
802
a22229c4 803/* We used to try to recognize pointer to member types here, but
c906108c
SS
804 that didn't work (shift/reduce conflicts meant that these rules never
805 got executed). The problem is that
806 int (foo::bar::baz::bizzle)
807 is a function type but
808 int (foo::bar::baz::bizzle::*)
809 is a pointer to member type. Stroustrup loses again! */
810
811type : ptype
c906108c
SS
812 ;
813
814typebase /* Implements (approximately): (type-qualifier)* type-specifier */
815 : TYPENAME
816 { $$ = $1.type; }
817 | INT_KEYWORD
64d2b26d 818 { $$ = builtin_type (current_gdbarch)->builtin_int; }
c906108c 819 | LONG
64d2b26d 820 { $$ = builtin_type (current_gdbarch)->builtin_long; }
c906108c 821 | SHORT
64d2b26d 822 { $$ = builtin_type (current_gdbarch)->builtin_short; }
c906108c 823 | LONG INT_KEYWORD
64d2b26d 824 { $$ = builtin_type (current_gdbarch)->builtin_long; }
b2c4da81 825 | LONG SIGNED_KEYWORD INT_KEYWORD
64d2b26d 826 { $$ = builtin_type (current_gdbarch)->builtin_long; }
b2c4da81 827 | LONG SIGNED_KEYWORD
64d2b26d 828 { $$ = builtin_type (current_gdbarch)->builtin_long; }
b2c4da81 829 | SIGNED_KEYWORD LONG INT_KEYWORD
64d2b26d 830 { $$ = builtin_type (current_gdbarch)->builtin_long; }
c906108c 831 | UNSIGNED LONG INT_KEYWORD
64d2b26d 832 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
b2c4da81 833 | LONG UNSIGNED INT_KEYWORD
64d2b26d 834 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
b2c4da81 835 | LONG UNSIGNED
64d2b26d 836 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long; }
c906108c 837 | LONG LONG
64d2b26d 838 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
c906108c 839 | LONG LONG INT_KEYWORD
64d2b26d 840 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
b2c4da81 841 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
64d2b26d 842 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
b2c4da81 843 | LONG LONG SIGNED_KEYWORD
64d2b26d 844 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
b2c4da81 845 | SIGNED_KEYWORD LONG LONG
64d2b26d 846 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
55baeb84 847 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
64d2b26d 848 { $$ = builtin_type (current_gdbarch)->builtin_long_long; }
c906108c 849 | UNSIGNED LONG LONG
64d2b26d 850 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
c906108c 851 | UNSIGNED LONG LONG INT_KEYWORD
64d2b26d 852 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
b2c4da81 853 | LONG LONG UNSIGNED
64d2b26d 854 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
b2c4da81 855 | LONG LONG UNSIGNED INT_KEYWORD
64d2b26d 856 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_long_long; }
c906108c 857 | SHORT INT_KEYWORD
64d2b26d 858 { $$ = builtin_type (current_gdbarch)->builtin_short; }
b2c4da81 859 | SHORT SIGNED_KEYWORD INT_KEYWORD
64d2b26d 860 { $$ = builtin_type (current_gdbarch)->builtin_short; }
b2c4da81 861 | SHORT SIGNED_KEYWORD
64d2b26d 862 { $$ = builtin_type (current_gdbarch)->builtin_short; }
c906108c 863 | UNSIGNED SHORT INT_KEYWORD
64d2b26d 864 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
b2c4da81 865 | SHORT UNSIGNED
64d2b26d 866 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
b2c4da81 867 | SHORT UNSIGNED INT_KEYWORD
64d2b26d 868 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_short; }
c906108c 869 | DOUBLE_KEYWORD
64d2b26d 870 { $$ = builtin_type (current_gdbarch)->builtin_double; }
c906108c 871 | LONG DOUBLE_KEYWORD
64d2b26d 872 { $$ = builtin_type (current_gdbarch)->builtin_long_double; }
c906108c
SS
873 | STRUCT name
874 { $$ = lookup_struct (copy_name ($2),
875 expression_context_block); }
876 | CLASS name
877 { $$ = lookup_struct (copy_name ($2),
878 expression_context_block); }
879 | UNION name
880 { $$ = lookup_union (copy_name ($2),
881 expression_context_block); }
882 | ENUM name
883 { $$ = lookup_enum (copy_name ($2),
884 expression_context_block); }
885 | UNSIGNED typename
886 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
887 | UNSIGNED
64d2b26d 888 { $$ = builtin_type (current_gdbarch)->builtin_unsigned_int; }
c906108c
SS
889 | SIGNED_KEYWORD typename
890 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
891 | SIGNED_KEYWORD
64d2b26d 892 { $$ = builtin_type (current_gdbarch)->builtin_int; }
c906108c
SS
893 /* It appears that this rule for templates is never
894 reduced; template recognition happens by lookahead
895 in the token processing code in yylex. */
896 | TEMPLATE name '<' type '>'
897 { $$ = lookup_template_type(copy_name($2), $4,
898 expression_context_block);
899 }
47663de5
MS
900 | const_or_volatile_or_space_identifier_noopt typebase
901 { $$ = follow_types ($2); }
902 | typebase const_or_volatile_or_space_identifier_noopt
903 { $$ = follow_types ($1); }
79c2c32d
DC
904 | qualified_type
905 ;
906
907/* FIXME: carlton/2003-09-25: This next bit leads to lots of
908 reduce-reduce conflicts, because the parser doesn't know whether or
909 not to use qualified_name or qualified_type: the rules are
910 identical. If the parser is parsing 'A::B::x', then, when it sees
911 the second '::', it knows that the expression to the left of it has
912 to be a type, so it uses qualified_type. But if it is parsing just
913 'A::B', then it doesn't have any way of knowing which rule to use,
914 so there's a reduce-reduce conflict; it picks qualified_name, since
915 that occurs earlier in this file than qualified_type.
916
917 There's no good way to fix this with the grammar as it stands; as
918 far as I can tell, some of the problems arise from ambiguities that
919 GDB introduces ('start' can be either an expression or a type), but
920 some of it is inherent to the nature of C++ (you want to treat the
921 input "(FOO)" fairly differently depending on whether FOO is an
922 expression or a type, and if FOO is a complex expression, this can
923 be hard to determine at the right time). Fortunately, it works
924 pretty well in most cases. For example, if you do 'ptype A::B',
925 where A::B is a nested type, then the parser will mistakenly
926 misidentify it as an expression; but evaluate_subexp will get
927 called with 'noside' set to EVAL_AVOID_SIDE_EFFECTS, and everything
928 will work out anyways. But there are situations where the parser
929 will get confused: the most common one that I've run into is when
930 you want to do
931
932 print *((A::B *) x)"
933
934 where the parser doesn't realize that A::B has to be a type until
935 it hits the first right paren, at which point it's too late. (The
936 workaround is to type "print *(('A::B' *) x)" instead.) (And
937 another solution is to fix our symbol-handling code so that the
938 user never wants to type something like that in the first place,
939 because we get all the types right without the user's help!)
940
941 Perhaps we could fix this by making the lexer smarter. Some of
942 this functionality used to be in the lexer, but in a way that
943 worked even less well than the current solution: that attempt
944 involved having the parser sometimes handle '::' and having the
945 lexer sometimes handle it, and without a clear division of
946 responsibility, it quickly degenerated into a big mess. Probably
947 the eventual correct solution will give more of a role to the lexer
948 (ideally via code that is shared between the lexer and
949 decode_line_1), but I'm not holding my breath waiting for somebody
950 to get around to cleaning this up... */
951
79c2c32d
DC
952qualified_type: typebase COLONCOLON name
953 {
954 struct type *type = $1;
955 struct type *new_type;
956 char *ncopy = alloca ($3.length + 1);
957
958 memcpy (ncopy, $3.ptr, $3.length);
959 ncopy[$3.length] = '\0';
960
63d06c5c
DC
961 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
962 && TYPE_CODE (type) != TYPE_CODE_UNION
963 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
964 error ("`%s' is not defined as an aggregate type.",
79c2c32d
DC
965 TYPE_NAME (type));
966
967 new_type = cp_lookup_nested_type (type, ncopy,
968 expression_context_block);
969 if (new_type == NULL)
63d06c5c 970 error ("No type \"%s\" within class or namespace \"%s\".",
79c2c32d
DC
971 ncopy, TYPE_NAME (type));
972
973 $$ = new_type;
974 }
c906108c
SS
975 ;
976
977typename: TYPENAME
978 | INT_KEYWORD
979 {
980 $$.stoken.ptr = "int";
981 $$.stoken.length = 3;
64d2b26d 982 $$.type = builtin_type (current_gdbarch)->builtin_int;
c906108c
SS
983 }
984 | LONG
985 {
986 $$.stoken.ptr = "long";
987 $$.stoken.length = 4;
64d2b26d 988 $$.type = builtin_type (current_gdbarch)->builtin_long;
c906108c
SS
989 }
990 | SHORT
991 {
992 $$.stoken.ptr = "short";
993 $$.stoken.length = 5;
64d2b26d 994 $$.type = builtin_type (current_gdbarch)->builtin_short;
c906108c
SS
995 }
996 ;
997
998nonempty_typelist
999 : type
1000 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1001 $<ivec>$[0] = 1; /* Number of types in vector */
1002 $$[1] = $1;
1003 }
1004 | nonempty_typelist ',' type
1005 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1006 $$ = (struct type **) realloc ((char *) $1, len);
1007 $$[$<ivec>$[0]] = $3;
1008 }
1009 ;
1010
47663de5
MS
1011ptype : typebase
1012 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1013 { $$ = follow_types ($1); }
1014 ;
1015
1016const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1017 | VOLATILE_KEYWORD CONST_KEYWORD
1018 ;
1019
1020const_or_volatile_noopt: const_and_volatile
1021 { push_type (tp_const);
1022 push_type (tp_volatile);
1023 }
1024 | CONST_KEYWORD
1025 { push_type (tp_const); }
1026 | VOLATILE_KEYWORD
1027 { push_type (tp_volatile); }
1028 ;
1029
c906108c
SS
1030name : NAME { $$ = $1.stoken; }
1031 | BLOCKNAME { $$ = $1.stoken; }
1032 | TYPENAME { $$ = $1.stoken; }
1033 | NAME_OR_INT { $$ = $1.stoken; }
1034 ;
1035
1036name_not_typename : NAME
1037 | BLOCKNAME
1038/* These would be useful if name_not_typename was useful, but it is just
1039 a fake for "variable", so these cause reduce/reduce conflicts because
1040 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1041 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1042 context where only a name could occur, this might be useful.
1043 | NAME_OR_INT
1044 */
1045 ;
1046
1047%%
1048
1049/* Take care of parsing a number (anything that starts with a digit).
1050 Set yylval and return the token type; update lexptr.
1051 LEN is the number of characters in it. */
1052
1053/*** Needs some error checking for the float case ***/
1054
1055static int
1056parse_number (p, len, parsed_float, putithere)
710122da
DC
1057 char *p;
1058 int len;
c906108c
SS
1059 int parsed_float;
1060 YYSTYPE *putithere;
1061{
1062 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1063 here, and we do kind of silly things like cast to unsigned. */
710122da
DC
1064 LONGEST n = 0;
1065 LONGEST prevn = 0;
c906108c
SS
1066 ULONGEST un;
1067
710122da
DC
1068 int i = 0;
1069 int c;
1070 int base = input_radix;
c906108c
SS
1071 int unsigned_p = 0;
1072
1073 /* Number of "L" suffixes encountered. */
1074 int long_p = 0;
1075
1076 /* We have found a "L" or "U" suffix. */
1077 int found_suffix = 0;
1078
1079 ULONGEST high_bit;
1080 struct type *signed_type;
1081 struct type *unsigned_type;
1082
1083 if (parsed_float)
1084 {
1085 /* It's a float since it contains a point or an exponent. */
42969d33 1086 char *s = malloc (len);
c906108c
SS
1087 int num = 0; /* number of tokens scanned by scanf */
1088 char saved_char = p[len];
1089
1090 p[len] = 0; /* null-terminate the token */
27bc4d80
TJB
1091
1092 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1093 point. Return DECFLOAT. */
1094
1095 if (p[len - 2] == 'd' && p[len - 1] == 'f')
1096 {
1097 p[len - 2] = '\0';
1098 putithere->typed_val_decfloat.type
1099 = builtin_type (current_gdbarch)->builtin_decfloat;
1100 decimal_from_string (putithere->typed_val_decfloat.val, 4, p);
1101 p[len] = saved_char;
1102 return (DECFLOAT);
1103 }
1104
1105 if (p[len - 2] == 'd' && p[len - 1] == 'd')
1106 {
1107 p[len - 2] = '\0';
1108 putithere->typed_val_decfloat.type
1109 = builtin_type (current_gdbarch)->builtin_decdouble;
1110 decimal_from_string (putithere->typed_val_decfloat.val, 8, p);
1111 p[len] = saved_char;
1112 return (DECFLOAT);
1113 }
1114
1115 if (p[len - 2] == 'd' && p[len - 1] == 'l')
1116 {
1117 p[len - 2] = '\0';
1118 putithere->typed_val_decfloat.type
1119 = builtin_type (current_gdbarch)->builtin_declong;
1120 decimal_from_string (putithere->typed_val_decfloat.val, 16, p);
1121 p[len] = saved_char;
1122 return (DECFLOAT);
1123 }
1124
689e4e2d 1125 num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%s",
96c1eda2 1126 &putithere->typed_val_float.dval, s);
c906108c 1127 p[len] = saved_char; /* restore the input stream */
42969d33
WZ
1128
1129 if (num == 1)
1130 putithere->typed_val_float.type =
1131 builtin_type (current_gdbarch)->builtin_double;
1132
1133 if (num == 2 )
1134 {
1135 /* See if it has any float suffix: 'f' for float, 'l' for long
1136 double. */
1137 if (!strcasecmp (s, "f"))
1138 putithere->typed_val_float.type =
1139 builtin_type (current_gdbarch)->builtin_float;
1140 else if (!strcasecmp (s, "l"))
1141 putithere->typed_val_float.type =
1142 builtin_type (current_gdbarch)->builtin_long_double;
1143 else
348038cd
MS
1144 {
1145 free (s);
1146 return ERROR;
1147 }
42969d33 1148 }
c906108c 1149
348038cd 1150 free (s);
c906108c
SS
1151 return FLOAT;
1152 }
1153
1154 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1155 if (p[0] == '0')
1156 switch (p[1])
1157 {
1158 case 'x':
1159 case 'X':
1160 if (len >= 3)
1161 {
1162 p += 2;
1163 base = 16;
1164 len -= 2;
1165 }
1166 break;
1167
1168 case 't':
1169 case 'T':
1170 case 'd':
1171 case 'D':
1172 if (len >= 3)
1173 {
1174 p += 2;
1175 base = 10;
1176 len -= 2;
1177 }
1178 break;
1179
1180 default:
1181 base = 8;
1182 break;
1183 }
1184
1185 while (len-- > 0)
1186 {
1187 c = *p++;
1188 if (c >= 'A' && c <= 'Z')
1189 c += 'a' - 'A';
1190 if (c != 'l' && c != 'u')
1191 n *= base;
1192 if (c >= '0' && c <= '9')
1193 {
1194 if (found_suffix)
1195 return ERROR;
1196 n += i = c - '0';
1197 }
1198 else
1199 {
1200 if (base > 10 && c >= 'a' && c <= 'f')
1201 {
1202 if (found_suffix)
1203 return ERROR;
1204 n += i = c - 'a' + 10;
1205 }
1206 else if (c == 'l')
1207 {
1208 ++long_p;
1209 found_suffix = 1;
1210 }
1211 else if (c == 'u')
1212 {
1213 unsigned_p = 1;
1214 found_suffix = 1;
1215 }
1216 else
1217 return ERROR; /* Char not a digit */
1218 }
1219 if (i >= base)
1220 return ERROR; /* Invalid digit in this base */
1221
1222 /* Portably test for overflow (only works for nonzero values, so make
1223 a second check for zero). FIXME: Can't we just make n and prevn
1224 unsigned and avoid this? */
1225 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1226 unsigned_p = 1; /* Try something unsigned */
1227
1228 /* Portably test for unsigned overflow.
1229 FIXME: This check is wrong; for example it doesn't find overflow
1230 on 0x123456789 when LONGEST is 32 bits. */
1231 if (c != 'l' && c != 'u' && n != 0)
1232 {
1233 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1234 error ("Numeric constant too large.");
1235 }
1236 prevn = n;
1237 }
1238
1239 /* An integer constant is an int, a long, or a long long. An L
1240 suffix forces it to be long; an LL suffix forces it to be long
1241 long. If not forced to a larger size, it gets the first type of
1242 the above that it fits in. To figure out whether it fits, we
1243 shift it right and see whether anything remains. Note that we
1244 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1245 operation, because many compilers will warn about such a shift
9a76efb6
UW
1246 (which always produces a zero result). Sometimes gdbarch_int_bit
1247 or gdbarch_long_bit will be that big, sometimes not. To deal with
c906108c
SS
1248 the case where it is we just always shift the value more than
1249 once, with fewer bits each time. */
1250
1251 un = (ULONGEST)n >> 2;
1252 if (long_p == 0
9a76efb6 1253 && (un >> (gdbarch_int_bit (current_gdbarch) - 2)) == 0)
c906108c 1254 {
9a76efb6 1255 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (current_gdbarch) - 1);
c906108c
SS
1256
1257 /* A large decimal (not hex or octal) constant (between INT_MAX
1258 and UINT_MAX) is a long or unsigned long, according to ANSI,
1259 never an unsigned int, but this code treats it as unsigned
1260 int. This probably should be fixed. GCC gives a warning on
1261 such constants. */
1262
64d2b26d
AC
1263 unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_int;
1264 signed_type = builtin_type (current_gdbarch)->builtin_int;
c906108c
SS
1265 }
1266 else if (long_p <= 1
9a76efb6 1267 && (un >> (gdbarch_long_bit (current_gdbarch) - 2)) == 0)
c906108c 1268 {
9a76efb6 1269 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (current_gdbarch) - 1);
64d2b26d
AC
1270 unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_long;
1271 signed_type = builtin_type (current_gdbarch)->builtin_long;
c906108c
SS
1272 }
1273 else
1274 {
1275 int shift;
9a76efb6
UW
1276 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1277 < gdbarch_long_long_bit (current_gdbarch))
c906108c
SS
1278 /* A long long does not fit in a LONGEST. */
1279 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1280 else
9a76efb6 1281 shift = (gdbarch_long_long_bit (current_gdbarch) - 1);
c906108c 1282 high_bit = (ULONGEST) 1 << shift;
64d2b26d
AC
1283 unsigned_type = builtin_type (current_gdbarch)->builtin_unsigned_long_long;
1284 signed_type = builtin_type (current_gdbarch)->builtin_long_long;
c906108c
SS
1285 }
1286
1287 putithere->typed_val_int.val = n;
1288
1289 /* If the high bit of the worked out type is set then this number
1290 has to be unsigned. */
1291
1292 if (unsigned_p || (n & high_bit))
1293 {
1294 putithere->typed_val_int.type = unsigned_type;
1295 }
1296 else
1297 {
1298 putithere->typed_val_int.type = signed_type;
1299 }
1300
1301 return INT;
1302}
1303
1304struct token
1305{
1306 char *operator;
1307 int token;
1308 enum exp_opcode opcode;
1309};
1310
1311static const struct token tokentab3[] =
1312 {
1313 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1314 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1315 };
1316
1317static const struct token tokentab2[] =
1318 {
1319 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1320 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1321 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1322 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1323 {"%=", ASSIGN_MODIFY, BINOP_REM},
1324 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1325 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1326 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1327 {"++", INCREMENT, BINOP_END},
1328 {"--", DECREMENT, BINOP_END},
1329 {"->", ARROW, BINOP_END},
1330 {"&&", ANDAND, BINOP_END},
1331 {"||", OROR, BINOP_END},
1332 {"::", COLONCOLON, BINOP_END},
1333 {"<<", LSH, BINOP_END},
1334 {">>", RSH, BINOP_END},
1335 {"==", EQUAL, BINOP_END},
1336 {"!=", NOTEQUAL, BINOP_END},
1337 {"<=", LEQ, BINOP_END},
1338 {">=", GEQ, BINOP_END}
1339 };
1340
1341/* Read one token, getting characters through lexptr. */
1342
1343static int
1344yylex ()
1345{
1346 int c;
1347 int namelen;
1348 unsigned int i;
1349 char *tokstart;
1350 char *tokptr;
1351 int tempbufindex;
1352 static char *tempbuf;
1353 static int tempbufsize;
c906108c
SS
1354 char * token_string = NULL;
1355 int class_prefix = 0;
c906108c
SS
1356
1357 retry:
1358
84f0252a
JB
1359 /* Check if this is a macro invocation that we need to expand. */
1360 if (! scanning_macro_expansion ())
1361 {
1362 char *expanded = macro_expand_next (&lexptr,
1363 expression_macro_lookup_func,
1364 expression_macro_lookup_baton);
1365
1366 if (expanded)
1367 scan_macro_expansion (expanded);
1368 }
1369
665132f9 1370 prev_lexptr = lexptr;
c906108c
SS
1371
1372 tokstart = lexptr;
1373 /* See if it is a special token of length 3. */
1374 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
bf896cb0 1375 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
c906108c
SS
1376 {
1377 lexptr += 3;
1378 yylval.opcode = tokentab3[i].opcode;
1379 return tokentab3[i].token;
1380 }
1381
1382 /* See if it is a special token of length 2. */
1383 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
bf896cb0 1384 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
c906108c
SS
1385 {
1386 lexptr += 2;
1387 yylval.opcode = tokentab2[i].opcode;
1388 return tokentab2[i].token;
1389 }
1390
1391 switch (c = *tokstart)
1392 {
1393 case 0:
84f0252a
JB
1394 /* If we were just scanning the result of a macro expansion,
1395 then we need to resume scanning the original text.
1396 Otherwise, we were already scanning the original text, and
1397 we're really done. */
1398 if (scanning_macro_expansion ())
1399 {
1400 finished_macro_expansion ();
1401 goto retry;
1402 }
1403 else
1404 return 0;
c906108c
SS
1405
1406 case ' ':
1407 case '\t':
1408 case '\n':
1409 lexptr++;
1410 goto retry;
1411
1412 case '\'':
1413 /* We either have a character constant ('0' or '\177' for example)
1414 or we have a quoted symbol reference ('foo(int,int)' in C++
1415 for example). */
1416 lexptr++;
1417 c = *lexptr++;
1418 if (c == '\\')
1419 c = parse_escape (&lexptr);
1420 else if (c == '\'')
1421 error ("Empty character constant.");
234b45d4
KB
1422 else if (! host_char_to_target (c, &c))
1423 {
1424 int toklen = lexptr - tokstart + 1;
1425 char *tok = alloca (toklen + 1);
1426 memcpy (tok, tokstart, toklen);
1427 tok[toklen] = '\0';
1428 error ("There is no character corresponding to %s in the target "
1429 "character set `%s'.", tok, target_charset ());
1430 }
c906108c
SS
1431
1432 yylval.typed_val_int.val = c;
64d2b26d 1433 yylval.typed_val_int.type = builtin_type (current_gdbarch)->builtin_char;
c906108c
SS
1434
1435 c = *lexptr++;
1436 if (c != '\'')
1437 {
1438 namelen = skip_quoted (tokstart) - tokstart;
1439 if (namelen > 2)
1440 {
1441 lexptr = tokstart + namelen;
c906108c
SS
1442 if (lexptr[-1] != '\'')
1443 error ("Unmatched single quote.");
1444 namelen -= 2;
1445 tokstart++;
1446 goto tryname;
1447 }
1448 error ("Invalid character constant.");
1449 }
1450 return INT;
1451
1452 case '(':
1453 paren_depth++;
1454 lexptr++;
1455 return c;
1456
1457 case ')':
1458 if (paren_depth == 0)
1459 return 0;
1460 paren_depth--;
1461 lexptr++;
1462 return c;
1463
1464 case ',':
84f0252a
JB
1465 if (comma_terminates
1466 && paren_depth == 0
1467 && ! scanning_macro_expansion ())
c906108c
SS
1468 return 0;
1469 lexptr++;
1470 return c;
1471
1472 case '.':
1473 /* Might be a floating point number. */
1474 if (lexptr[1] < '0' || lexptr[1] > '9')
1475 goto symbol; /* Nope, must be a symbol. */
1476 /* FALL THRU into number case. */
1477
1478 case '0':
1479 case '1':
1480 case '2':
1481 case '3':
1482 case '4':
1483 case '5':
1484 case '6':
1485 case '7':
1486 case '8':
1487 case '9':
1488 {
1489 /* It's a number. */
1490 int got_dot = 0, got_e = 0, toktype;
710122da 1491 char *p = tokstart;
c906108c
SS
1492 int hex = input_radix > 10;
1493
1494 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1495 {
1496 p += 2;
1497 hex = 1;
1498 }
1499 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1500 {
1501 p += 2;
1502 hex = 0;
1503 }
1504
1505 for (;; ++p)
1506 {
1507 /* This test includes !hex because 'e' is a valid hex digit
1508 and thus does not indicate a floating point number when
1509 the radix is hex. */
1510 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1511 got_dot = got_e = 1;
1512 /* This test does not include !hex, because a '.' always indicates
1513 a decimal floating point number regardless of the radix. */
1514 else if (!got_dot && *p == '.')
1515 got_dot = 1;
1516 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1517 && (*p == '-' || *p == '+'))
1518 /* This is the sign of the exponent, not the end of the
1519 number. */
1520 continue;
1521 /* We will take any letters or digits. parse_number will
1522 complain if past the radix, or if L or U are not final. */
1523 else if ((*p < '0' || *p > '9')
1524 && ((*p < 'a' || *p > 'z')
1525 && (*p < 'A' || *p > 'Z')))
1526 break;
1527 }
1528 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1529 if (toktype == ERROR)
1530 {
1531 char *err_copy = (char *) alloca (p - tokstart + 1);
1532
1533 memcpy (err_copy, tokstart, p - tokstart);
1534 err_copy[p - tokstart] = 0;
1535 error ("Invalid number \"%s\".", err_copy);
1536 }
1537 lexptr = p;
1538 return toktype;
1539 }
1540
1541 case '+':
1542 case '-':
1543 case '*':
1544 case '/':
1545 case '%':
1546 case '|':
1547 case '&':
1548 case '^':
1549 case '~':
1550 case '!':
1551 case '@':
1552 case '<':
1553 case '>':
1554 case '[':
1555 case ']':
1556 case '?':
1557 case ':':
1558 case '=':
1559 case '{':
1560 case '}':
1561 symbol:
1562 lexptr++;
1563 return c;
1564
1565 case '"':
1566
1567 /* Build the gdb internal form of the input string in tempbuf,
1568 translating any standard C escape forms seen. Note that the
1569 buffer is null byte terminated *only* for the convenience of
1570 debugging gdb itself and printing the buffer contents when
1571 the buffer contains no embedded nulls. Gdb does not depend
1572 upon the buffer being null byte terminated, it uses the length
1573 string instead. This allows gdb to handle C strings (as well
1574 as strings in other languages) with embedded null bytes */
1575
1576 tokptr = ++tokstart;
1577 tempbufindex = 0;
1578
1579 do {
234b45d4
KB
1580 char *char_start_pos = tokptr;
1581
c906108c
SS
1582 /* Grow the static temp buffer if necessary, including allocating
1583 the first one on demand. */
1584 if (tempbufindex + 1 >= tempbufsize)
1585 {
1586 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1587 }
1588 switch (*tokptr)
1589 {
1590 case '\0':
1591 case '"':
1592 /* Do nothing, loop will terminate. */
1593 break;
1594 case '\\':
1595 tokptr++;
1596 c = parse_escape (&tokptr);
1597 if (c == -1)
1598 {
1599 continue;
1600 }
1601 tempbuf[tempbufindex++] = c;
1602 break;
1603 default:
234b45d4
KB
1604 c = *tokptr++;
1605 if (! host_char_to_target (c, &c))
1606 {
1607 int len = tokptr - char_start_pos;
1608 char *copy = alloca (len + 1);
1609 memcpy (copy, char_start_pos, len);
1610 copy[len] = '\0';
1611
1612 error ("There is no character corresponding to `%s' "
1613 "in the target character set `%s'.",
1614 copy, target_charset ());
1615 }
1616 tempbuf[tempbufindex++] = c;
c906108c
SS
1617 break;
1618 }
1619 } while ((*tokptr != '"') && (*tokptr != '\0'));
1620 if (*tokptr++ != '"')
1621 {
1622 error ("Unterminated string in expression.");
1623 }
1624 tempbuf[tempbufindex] = '\0'; /* See note above */
1625 yylval.sval.ptr = tempbuf;
1626 yylval.sval.length = tempbufindex;
1627 lexptr = tokptr;
1628 return (STRING);
1629 }
1630
1631 if (!(c == '_' || c == '$'
1632 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1633 /* We must have come across a bad character (e.g. ';'). */
1634 error ("Invalid character '%c' in expression.", c);
1635
1636 /* It's a name. See how long it is. */
1637 namelen = 0;
1638 for (c = tokstart[namelen];
1639 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1640 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1641 {
1642 /* Template parameter lists are part of the name.
1643 FIXME: This mishandles `print $a<4&&$a>3'. */
1644
1645 if (c == '<')
1646 {
c906108c
SS
1647 /* Scan ahead to get rest of the template specification. Note
1648 that we look ahead only when the '<' adjoins non-whitespace
1649 characters; for comparison expressions, e.g. "a < b > c",
1650 there must be spaces before the '<', etc. */
1651
1652 char * p = find_template_name_end (tokstart + namelen);
1653 if (p)
1654 namelen = p - tokstart;
1655 break;
c906108c
SS
1656 }
1657 c = tokstart[++namelen];
1658 }
1659
84f0252a
JB
1660 /* The token "if" terminates the expression and is NOT removed from
1661 the input stream. It doesn't count if it appears in the
1662 expansion of a macro. */
1663 if (namelen == 2
1664 && tokstart[0] == 'i'
1665 && tokstart[1] == 'f'
1666 && ! scanning_macro_expansion ())
c906108c
SS
1667 {
1668 return 0;
1669 }
1670
1671 lexptr += namelen;
1672
1673 tryname:
1674
1675 /* Catch specific keywords. Should be done with a data structure. */
1676 switch (namelen)
1677 {
1678 case 8:
bf896cb0 1679 if (strncmp (tokstart, "unsigned", 8) == 0)
c906108c
SS
1680 return UNSIGNED;
1681 if (current_language->la_language == language_cplus
bf896cb0 1682 && strncmp (tokstart, "template", 8) == 0)
c906108c 1683 return TEMPLATE;
bf896cb0 1684 if (strncmp (tokstart, "volatile", 8) == 0)
c906108c
SS
1685 return VOLATILE_KEYWORD;
1686 break;
1687 case 6:
bf896cb0 1688 if (strncmp (tokstart, "struct", 6) == 0)
c906108c 1689 return STRUCT;
bf896cb0 1690 if (strncmp (tokstart, "signed", 6) == 0)
c906108c 1691 return SIGNED_KEYWORD;
bf896cb0 1692 if (strncmp (tokstart, "sizeof", 6) == 0)
c906108c 1693 return SIZEOF;
bf896cb0 1694 if (strncmp (tokstart, "double", 6) == 0)
c906108c
SS
1695 return DOUBLE_KEYWORD;
1696 break;
1697 case 5:
1698 if (current_language->la_language == language_cplus)
1699 {
bf896cb0 1700 if (strncmp (tokstart, "false", 5) == 0)
c906108c 1701 return FALSEKEYWORD;
bf896cb0 1702 if (strncmp (tokstart, "class", 5) == 0)
c906108c
SS
1703 return CLASS;
1704 }
bf896cb0 1705 if (strncmp (tokstart, "union", 5) == 0)
c906108c 1706 return UNION;
bf896cb0 1707 if (strncmp (tokstart, "short", 5) == 0)
c906108c 1708 return SHORT;
bf896cb0 1709 if (strncmp (tokstart, "const", 5) == 0)
c906108c
SS
1710 return CONST_KEYWORD;
1711 break;
1712 case 4:
bf896cb0 1713 if (strncmp (tokstart, "enum", 4) == 0)
c906108c 1714 return ENUM;
bf896cb0 1715 if (strncmp (tokstart, "long", 4) == 0)
c906108c
SS
1716 return LONG;
1717 if (current_language->la_language == language_cplus)
1718 {
bf896cb0 1719 if (strncmp (tokstart, "true", 4) == 0)
c906108c 1720 return TRUEKEYWORD;
c906108c
SS
1721 }
1722 break;
1723 case 3:
bf896cb0 1724 if (strncmp (tokstart, "int", 3) == 0)
c906108c
SS
1725 return INT_KEYWORD;
1726 break;
1727 default:
1728 break;
1729 }
1730
1731 yylval.sval.ptr = tokstart;
1732 yylval.sval.length = namelen;
1733
1734 if (*tokstart == '$')
1735 {
1736 write_dollar_variable (yylval.sval);
1737 return VARIABLE;
1738 }
1739
c906108c
SS
1740 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1741 functions or symtabs. If this is not so, then ...
1742 Use token-type TYPENAME for symbols that happen to be defined
1743 currently as names of types; NAME for other symbols.
1744 The caller is not constrained to care about the distinction. */
1745 {
1746 char *tmp = copy_name (yylval.sval);
1747 struct symbol *sym;
1748 int is_a_field_of_this = 0;
1749 int hextype;
1750
1751 sym = lookup_symbol (tmp, expression_context_block,
176620f1 1752 VAR_DOMAIN,
c906108c 1753 current_language->la_language == language_cplus
2570f2b7 1754 ? &is_a_field_of_this : (int *) NULL);
c906108c
SS
1755 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1756 no psymtabs (coff, xcoff, or some future change to blow away the
1757 psymtabs once once symbols are read). */
1758 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1759 {
1760 yylval.ssym.sym = sym;
1761 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1762 return BLOCKNAME;
1763 }
1764 else if (!sym)
1765 { /* See if it's a file name. */
1766 struct symtab *symtab;
1767
1768 symtab = lookup_symtab (tmp);
1769
1770 if (symtab)
1771 {
1772 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1773 return FILENAME;
1774 }
1775 }
1776
1777 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1778 {
79c2c32d
DC
1779 /* NOTE: carlton/2003-09-25: There used to be code here to
1780 handle nested types. It didn't work very well. See the
1781 comment before qualified_type for more info. */
c906108c 1782 yylval.tsym.type = SYMBOL_TYPE (sym);
c906108c
SS
1783 return TYPENAME;
1784 }
54a5b07d
AC
1785 yylval.tsym.type
1786 = language_lookup_primitive_type_by_name (current_language,
1787 current_gdbarch, tmp);
1788 if (yylval.tsym.type != NULL)
47663de5 1789 return TYPENAME;
c906108c
SS
1790
1791 /* Input names that aren't symbols but ARE valid hex numbers,
1792 when the input radix permits them, can be names or numbers
1793 depending on the parse. Note we support radixes > 16 here. */
1794 if (!sym &&
1795 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1796 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1797 {
1798 YYSTYPE newlval; /* Its value is ignored. */
1799 hextype = parse_number (tokstart, namelen, 0, &newlval);
1800 if (hextype == INT)
1801 {
1802 yylval.ssym.sym = sym;
1803 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1804 return NAME_OR_INT;
1805 }
1806 }
1807
1808 /* Any other kind of symbol */
1809 yylval.ssym.sym = sym;
1810 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1811 return NAME;
1812 }
1813}
1814
1815void
1816yyerror (msg)
1817 char *msg;
1818{
665132f9
MS
1819 if (prev_lexptr)
1820 lexptr = prev_lexptr;
1821
c906108c
SS
1822 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
1823}