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