]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/c-exp.y
* configure.in (targargs): Pass --build if we're doing
[thirdparty/binutils-gdb.git] / gdb / c-exp.y
CommitLineData
3d6b6a90 1/* YACC parser for C expressions, for GDB.
07422705 2 Copyright (C) 1986, 1989, 1990, 1991, 1993, 1994, 1996, 1997
ba47c66a 3 Free Software Foundation, Inc.
3d6b6a90
JG
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
6c9638b4 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
3d6b6a90
JG
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
e35843d4
FF
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. */
3d6b6a90
JG
37
38%{
39
3d6b6a90 40#include "defs.h"
72ae15f6
FF
41#include "gdb_string.h"
42#include <ctype.h>
3d6b6a90 43#include "expression.h"
3d6b6a90 44#include "value.h"
abe28b92 45#include "parser-defs.h"
3d6b6a90 46#include "language.h"
22e39759 47#include "c-lang.h"
100f92e2
JK
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 */
3d6b6a90 51
19d0f3f4
FF
52/* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
53 as well as gratuitiously global symbol names, so we can have multiple
54 yacc generated parsers in gdb. Note that these are only the variables
55 produced by yacc. If other parser generators (bison, byacc, etc) produce
56 additional global names that conflict at link time, then those parser
57 generators need to be fixed instead of adding those names to this list. */
58
d018c8a6 59#define yymaxdepth c_maxdepth
3d6b6a90
JG
60#define yyparse c_parse
61#define yylex c_lex
62#define yyerror c_error
63#define yylval c_lval
64#define yychar c_char
65#define yydebug c_debug
66#define yypact c_pact
67#define yyr1 c_r1
68#define yyr2 c_r2
69#define yydef c_def
70#define yychk c_chk
71#define yypgo c_pgo
72#define yyact c_act
73#define yyexca c_exca
9ce7cb7c
SG
74#define yyerrflag c_errflag
75#define yynerrs c_nerrs
39bf5952
JG
76#define yyps c_ps
77#define yypv c_pv
78#define yys c_s
d018c8a6 79#define yy_yys c_yys
39bf5952
JG
80#define yystate c_state
81#define yytmp c_tmp
82#define yyv c_v
d018c8a6 83#define yy_yyv c_yyv
39bf5952
JG
84#define yyval c_val
85#define yylloc c_lloc
45fe3db4
FF
86#define yyreds c_reds /* With YYDEBUG defined */
87#define yytoks c_toks /* With YYDEBUG defined */
ea082c0a
MM
88#define yylhs c_yylhs
89#define yylen c_yylen
90#define yydefred c_yydefred
91#define yydgoto c_yydgoto
92#define yysindex c_yysindex
93#define yyrindex c_yyrindex
94#define yygindex c_yygindex
95#define yytable c_yytable
96#define yycheck c_yycheck
19d0f3f4
FF
97
98#ifndef YYDEBUG
99#define YYDEBUG 0 /* Default to no yydebug support */
100#endif
3d6b6a90 101
d26b50b7 102int
1ab3bf1b
JG
103yyparse PARAMS ((void));
104
19d0f3f4 105static int
1ab3bf1b
JG
106yylex PARAMS ((void));
107
108void
109yyerror PARAMS ((char *));
3d6b6a90
JG
110
111%}
112
113/* Although the yacc "value" of an expression is not used,
114 since the result is stored in the structure being created,
115 other node types do have values. */
116
117%union
118 {
119 LONGEST lval;
2e6edad1
SC
120 struct {
121 LONGEST val;
122 struct type *type;
aa220473
SG
123 } typed_val_int;
124 struct {
125 DOUBLEST dval;
126 struct type *type;
127 } typed_val_float;
3d6b6a90
JG
128 struct symbol *sym;
129 struct type *tval;
130 struct stoken sval;
131 struct ttype tsym;
132 struct symtoken ssym;
133 int voidval;
134 struct block *bval;
135 enum exp_opcode opcode;
136 struct internalvar *ivar;
137
138 struct type **tvec;
139 int *ivec;
140 }
141
1ab3bf1b
JG
142%{
143/* YYSTYPE gets defined by %union */
144static int
145parse_number PARAMS ((char *, int, int, YYSTYPE *));
146%}
147
9da75ad3
FF
148%type <voidval> exp exp1 type_exp start variable qualified_name lcurly
149%type <lval> rcurly
2640f7e1 150%type <tval> type typebase
3d6b6a90
JG
151%type <tvec> nonempty_typelist
152/* %type <bval> block */
153
154/* Fancy type parsing. */
155%type <voidval> func_mod direct_abs_decl abs_decl
156%type <tval> ptype
157%type <lval> array_mod
158
aa220473
SG
159%token <typed_val_int> INT
160%token <typed_val_float> FLOAT
3d6b6a90
JG
161
162/* Both NAME and TYPENAME tokens represent symbols in the input,
163 and both convey their data as strings.
164 But a TYPENAME is a string that happens to be defined as a typedef
165 or builtin type name (such as int or char)
166 and a NAME is any other symbol.
167 Contexts where this distinction is not important can use the
168 nonterminal "name", which matches either NAME or TYPENAME. */
169
170%token <sval> STRING
171%token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
172%token <tsym> TYPENAME
173%type <sval> name
174%type <ssym> name_not_typename
175%type <tsym> typename
176
177/* A NAME_OR_INT is a symbol which is not known in the symbol table,
178 but which would parse as a valid number in the current input radix.
179 E.g. "c" when input_radix==16. Depending on the parse, it will be
2e6edad1 180 turned into a name or into a number. */
3d6b6a90 181
2e6edad1 182%token <ssym> NAME_OR_INT
3d6b6a90 183
8050a57b 184%token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
4c53d9ca 185%token TEMPLATE
3d6b6a90
JG
186%token ERROR
187
188/* Special type cases, put in to allow the parser to distinguish different
189 legal basetypes. */
aa220473 190%token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
3d6b6a90 191
c700638c 192%token <voidval> VARIABLE
3d6b6a90
JG
193
194%token <opcode> ASSIGN_MODIFY
195
196/* C++ */
197%token THIS
198
199%left ','
200%left ABOVE_COMMA
201%right '=' ASSIGN_MODIFY
202%right '?'
088c3a0b
JG
203%left OROR
204%left ANDAND
3d6b6a90
JG
205%left '|'
206%left '^'
207%left '&'
208%left EQUAL NOTEQUAL
209%left '<' '>' LEQ GEQ
210%left LSH RSH
211%left '@'
212%left '+' '-'
213%left '*' '/' '%'
214%right UNARY INCREMENT DECREMENT
215%right ARROW '.' '[' '('
216%token <ssym> BLOCKNAME
217%type <bval> block
218%left COLONCOLON
36ce1b64 219
368c8614
MT
220\f
221%%
222
3d6b6a90
JG
223start : exp1
224 | type_exp
225 ;
226
227type_exp: type
228 { write_exp_elt_opcode(OP_TYPE);
229 write_exp_elt_type($1);
230 write_exp_elt_opcode(OP_TYPE);}
231 ;
232
233/* Expressions, including the comma operator. */
234exp1 : exp
235 | exp1 ',' exp
236 { write_exp_elt_opcode (BINOP_COMMA); }
237 ;
238
239/* Expressions, not including the comma operator. */
240exp : '*' exp %prec UNARY
241 { write_exp_elt_opcode (UNOP_IND); }
242
243exp : '&' exp %prec UNARY
244 { write_exp_elt_opcode (UNOP_ADDR); }
245
246exp : '-' exp %prec UNARY
247 { write_exp_elt_opcode (UNOP_NEG); }
248 ;
249
250exp : '!' exp %prec UNARY
e58de8a2 251 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
3d6b6a90
JG
252 ;
253
254exp : '~' exp %prec UNARY
e58de8a2 255 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
3d6b6a90
JG
256 ;
257
258exp : INCREMENT exp %prec UNARY
259 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
260 ;
261
262exp : DECREMENT exp %prec UNARY
263 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
264 ;
265
266exp : exp INCREMENT %prec UNARY
267 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
268 ;
269
270exp : exp DECREMENT %prec UNARY
271 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
272 ;
273
274exp : SIZEOF exp %prec UNARY
275 { write_exp_elt_opcode (UNOP_SIZEOF); }
276 ;
277
278exp : exp ARROW name
279 { write_exp_elt_opcode (STRUCTOP_PTR);
280 write_exp_string ($3);
281 write_exp_elt_opcode (STRUCTOP_PTR); }
282 ;
283
2640f7e1
JG
284exp : exp ARROW qualified_name
285 { /* exp->type::name becomes exp->*(&type::name) */
286 /* Note: this doesn't work if name is a
287 static member! FIXME */
288 write_exp_elt_opcode (UNOP_ADDR);
289 write_exp_elt_opcode (STRUCTOP_MPTR); }
01be6913 290 ;
3d6b6a90
JG
291exp : exp ARROW '*' exp
292 { write_exp_elt_opcode (STRUCTOP_MPTR); }
293 ;
294
295exp : exp '.' name
296 { write_exp_elt_opcode (STRUCTOP_STRUCT);
297 write_exp_string ($3);
298 write_exp_elt_opcode (STRUCTOP_STRUCT); }
299 ;
300
cd10c7e3
SG
301/* start-sanitize-gm
302Need to find a better way to do this...
303exp : exp '@' name
304 { write_exp_elt_opcode (STRUCTOP_FIELD);
305 write_exp_string ($3);
306 write_exp_elt_opcode (STRUCTOP_FIELD);
307 }
308 end-sanitize-gm */
309
2640f7e1
JG
310exp : exp '.' qualified_name
311 { /* exp.type::name becomes exp.*(&type::name) */
312 /* Note: this doesn't work if name is a
313 static member! FIXME */
314 write_exp_elt_opcode (UNOP_ADDR);
315 write_exp_elt_opcode (STRUCTOP_MEMBER); }
01be6913
PB
316 ;
317
3d6b6a90
JG
318exp : exp '.' '*' exp
319 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
320 ;
321
322exp : exp '[' exp1 ']'
323 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
324 ;
325
326exp : exp '('
327 /* This is to save the value of arglist_len
328 being accumulated by an outer function call. */
329 { start_arglist (); }
330 arglist ')' %prec ARROW
331 { write_exp_elt_opcode (OP_FUNCALL);
332 write_exp_elt_longcst ((LONGEST) end_arglist ());
333 write_exp_elt_opcode (OP_FUNCALL); }
334 ;
335
9da75ad3
FF
336lcurly : '{'
337 { start_arglist (); }
338 ;
339
3d6b6a90
JG
340arglist :
341 ;
342
343arglist : exp
344 { arglist_len = 1; }
345 ;
346
347arglist : arglist ',' exp %prec ABOVE_COMMA
348 { arglist_len++; }
349 ;
350
9da75ad3
FF
351rcurly : '}'
352 { $$ = end_arglist () - 1; }
353 ;
354exp : lcurly arglist rcurly %prec ARROW
ec16f701
FF
355 { write_exp_elt_opcode (OP_ARRAY);
356 write_exp_elt_longcst ((LONGEST) 0);
9da75ad3 357 write_exp_elt_longcst ((LONGEST) $3);
ec16f701
FF
358 write_exp_elt_opcode (OP_ARRAY); }
359 ;
360
9da75ad3 361exp : lcurly type rcurly exp %prec UNARY
3d6b6a90
JG
362 { write_exp_elt_opcode (UNOP_MEMVAL);
363 write_exp_elt_type ($2);
364 write_exp_elt_opcode (UNOP_MEMVAL); }
365 ;
366
367exp : '(' type ')' exp %prec UNARY
368 { write_exp_elt_opcode (UNOP_CAST);
369 write_exp_elt_type ($2);
370 write_exp_elt_opcode (UNOP_CAST); }
371 ;
372
373exp : '(' exp1 ')'
374 { }
375 ;
376
377/* Binary operators in order of decreasing precedence. */
378
379exp : exp '@' exp
380 { write_exp_elt_opcode (BINOP_REPEAT); }
381 ;
382
383exp : exp '*' exp
384 { write_exp_elt_opcode (BINOP_MUL); }
385 ;
386
387exp : exp '/' exp
388 { write_exp_elt_opcode (BINOP_DIV); }
389 ;
390
391exp : exp '%' exp
392 { write_exp_elt_opcode (BINOP_REM); }
393 ;
394
395exp : exp '+' exp
396 { write_exp_elt_opcode (BINOP_ADD); }
397 ;
398
399exp : exp '-' exp
400 { write_exp_elt_opcode (BINOP_SUB); }
401 ;
402
403exp : exp LSH exp
404 { write_exp_elt_opcode (BINOP_LSH); }
405 ;
406
407exp : exp RSH exp
408 { write_exp_elt_opcode (BINOP_RSH); }
409 ;
410
411exp : exp EQUAL exp
412 { write_exp_elt_opcode (BINOP_EQUAL); }
413 ;
414
415exp : exp NOTEQUAL exp
416 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
417 ;
418
419exp : exp LEQ exp
420 { write_exp_elt_opcode (BINOP_LEQ); }
421 ;
422
423exp : exp GEQ exp
424 { write_exp_elt_opcode (BINOP_GEQ); }
425 ;
426
427exp : exp '<' exp
428 { write_exp_elt_opcode (BINOP_LESS); }
429 ;
430
431exp : exp '>' exp
432 { write_exp_elt_opcode (BINOP_GTR); }
433 ;
434
435exp : exp '&' exp
e58de8a2 436 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
3d6b6a90
JG
437 ;
438
439exp : exp '^' exp
e58de8a2 440 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
3d6b6a90
JG
441 ;
442
443exp : exp '|' exp
e58de8a2 444 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
3d6b6a90
JG
445 ;
446
088c3a0b 447exp : exp ANDAND exp
e58de8a2 448 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
3d6b6a90
JG
449 ;
450
088c3a0b 451exp : exp OROR exp
e58de8a2 452 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
3d6b6a90
JG
453 ;
454
455exp : exp '?' exp ':' exp %prec '?'
456 { write_exp_elt_opcode (TERNOP_COND); }
457 ;
458
459exp : exp '=' exp
460 { write_exp_elt_opcode (BINOP_ASSIGN); }
461 ;
462
463exp : exp ASSIGN_MODIFY exp
464 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
465 write_exp_elt_opcode ($2);
466 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
467 ;
468
469exp : INT
470 { write_exp_elt_opcode (OP_LONG);
2e6edad1
SC
471 write_exp_elt_type ($1.type);
472 write_exp_elt_longcst ((LONGEST)($1.val));
3d6b6a90
JG
473 write_exp_elt_opcode (OP_LONG); }
474 ;
475
476exp : NAME_OR_INT
477 { YYSTYPE val;
478 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
479 write_exp_elt_opcode (OP_LONG);
aa220473
SG
480 write_exp_elt_type (val.typed_val_int.type);
481 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
3d6b6a90
JG
482 write_exp_elt_opcode (OP_LONG);
483 }
484 ;
485
3d6b6a90
JG
486
487exp : FLOAT
488 { write_exp_elt_opcode (OP_DOUBLE);
aa220473
SG
489 write_exp_elt_type ($1.type);
490 write_exp_elt_dblcst ($1.dval);
3d6b6a90
JG
491 write_exp_elt_opcode (OP_DOUBLE); }
492 ;
493
494exp : variable
495 ;
496
3d6b6a90 497exp : VARIABLE
c700638c 498 /* Already written by write_dollar_variable. */
3d6b6a90
JG
499 ;
500
501exp : SIZEOF '(' type ')' %prec UNARY
502 { write_exp_elt_opcode (OP_LONG);
503 write_exp_elt_type (builtin_type_int);
940d5967 504 CHECK_TYPEDEF ($3);
3d6b6a90
JG
505 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
506 write_exp_elt_opcode (OP_LONG); }
507 ;
508
509exp : STRING
c4413e2c
FF
510 { /* C strings are converted into array constants with
511 an explicit null byte added at the end. Thus
512 the array upper bound is the string length.
513 There is no such thing in C as a completely empty
514 string. */
515 char *sp = $1.ptr; int count = $1.length;
516 while (count-- > 0)
517 {
518 write_exp_elt_opcode (OP_LONG);
519 write_exp_elt_type (builtin_type_char);
520 write_exp_elt_longcst ((LONGEST)(*sp++));
521 write_exp_elt_opcode (OP_LONG);
522 }
523 write_exp_elt_opcode (OP_LONG);
524 write_exp_elt_type (builtin_type_char);
525 write_exp_elt_longcst ((LONGEST)'\0');
526 write_exp_elt_opcode (OP_LONG);
527 write_exp_elt_opcode (OP_ARRAY);
528 write_exp_elt_longcst ((LONGEST) 0);
529 write_exp_elt_longcst ((LONGEST) ($1.length));
530 write_exp_elt_opcode (OP_ARRAY); }
3d6b6a90
JG
531 ;
532
533/* C++. */
534exp : THIS
535 { write_exp_elt_opcode (OP_THIS);
536 write_exp_elt_opcode (OP_THIS); }
537 ;
538
539/* end of C++. */
540
541block : BLOCKNAME
542 {
543 if ($1.sym != 0)
544 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
545 else
546 {
547 struct symtab *tem =
548 lookup_symtab (copy_name ($1.stoken));
549 if (tem)
13ffa6be 550 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), STATIC_BLOCK);
3d6b6a90
JG
551 else
552 error ("No file or function \"%s\".",
553 copy_name ($1.stoken));
554 }
555 }
556 ;
557
558block : block COLONCOLON name
559 { struct symbol *tem
560 = lookup_symbol (copy_name ($3), $1,
bcca9a08
FF
561 VAR_NAMESPACE, (int *) NULL,
562 (struct symtab **) NULL);
3d6b6a90
JG
563 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
564 error ("No function \"%s\" in specified context.",
565 copy_name ($3));
566 $$ = SYMBOL_BLOCK_VALUE (tem); }
567 ;
568
569variable: block COLONCOLON name
570 { struct symbol *sym;
571 sym = lookup_symbol (copy_name ($3), $1,
bcca9a08
FF
572 VAR_NAMESPACE, (int *) NULL,
573 (struct symtab **) NULL);
3d6b6a90
JG
574 if (sym == 0)
575 error ("No symbol \"%s\" in specified context.",
576 copy_name ($3));
577
578 write_exp_elt_opcode (OP_VAR_VALUE);
479fdd26
JK
579 /* block_found is set by lookup_symbol. */
580 write_exp_elt_block (block_found);
3d6b6a90
JG
581 write_exp_elt_sym (sym);
582 write_exp_elt_opcode (OP_VAR_VALUE); }
583 ;
584
2640f7e1 585qualified_name: typebase COLONCOLON name
3d6b6a90
JG
586 {
587 struct type *type = $1;
588 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
589 && TYPE_CODE (type) != TYPE_CODE_UNION)
590 error ("`%s' is not defined as an aggregate type.",
591 TYPE_NAME (type));
592
593 write_exp_elt_opcode (OP_SCOPE);
594 write_exp_elt_type (type);
2640f7e1 595 write_exp_string ($3);
3d6b6a90
JG
596 write_exp_elt_opcode (OP_SCOPE);
597 }
2640f7e1 598 | typebase COLONCOLON '~' name
3d6b6a90
JG
599 {
600 struct type *type = $1;
01be6913 601 struct stoken tmp_token;
3d6b6a90
JG
602 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
603 && TYPE_CODE (type) != TYPE_CODE_UNION)
604 error ("`%s' is not defined as an aggregate type.",
605 TYPE_NAME (type));
606
2640f7e1
JG
607 tmp_token.ptr = (char*) alloca ($4.length + 2);
608 tmp_token.length = $4.length + 1;
01be6913 609 tmp_token.ptr[0] = '~';
2640f7e1 610 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
01be6913 611 tmp_token.ptr[tmp_token.length] = 0;
a46d92a7
PS
612
613 /* Check for valid destructor name. */
614 destructor_name_p (tmp_token.ptr, type);
3d6b6a90
JG
615 write_exp_elt_opcode (OP_SCOPE);
616 write_exp_elt_type (type);
01be6913 617 write_exp_string (tmp_token);
3d6b6a90 618 write_exp_elt_opcode (OP_SCOPE);
3d6b6a90 619 }
01be6913
PB
620 ;
621
622variable: qualified_name
3d6b6a90
JG
623 | COLONCOLON name
624 {
625 char *name = copy_name ($2);
626 struct symbol *sym;
1ab3bf1b 627 struct minimal_symbol *msymbol;
3d6b6a90
JG
628
629 sym =
bcca9a08
FF
630 lookup_symbol (name, (const struct block *) NULL,
631 VAR_NAMESPACE, (int *) NULL,
632 (struct symtab **) NULL);
3d6b6a90
JG
633 if (sym)
634 {
635 write_exp_elt_opcode (OP_VAR_VALUE);
479fdd26 636 write_exp_elt_block (NULL);
3d6b6a90
JG
637 write_exp_elt_sym (sym);
638 write_exp_elt_opcode (OP_VAR_VALUE);
639 break;
640 }
3d6b6a90 641
2d336b1b 642 msymbol = lookup_minimal_symbol (name, NULL, NULL);
1ab3bf1b 643 if (msymbol != NULL)
3d6b6a90 644 {
abe28b92
JK
645 write_exp_msymbol (msymbol,
646 lookup_function_type (builtin_type_int),
647 builtin_type_int);
3d6b6a90
JG
648 }
649 else
1ab3bf1b 650 if (!have_full_symbols () && !have_partial_symbols ())
3d6b6a90
JG
651 error ("No symbol table is loaded. Use the \"file\" command.");
652 else
653 error ("No symbol \"%s\" in current context.", name);
654 }
655 ;
656
657variable: name_not_typename
658 { struct symbol *sym = $1.sym;
659
660 if (sym)
661 {
443abae1 662 if (symbol_read_needs_frame (sym))
3d6b6a90 663 {
3d6b6a90
JG
664 if (innermost_block == 0 ||
665 contained_in (block_found,
666 innermost_block))
667 innermost_block = block_found;
3d6b6a90 668 }
443abae1 669
3d6b6a90 670 write_exp_elt_opcode (OP_VAR_VALUE);
479fdd26
JK
671 /* We want to use the selected frame, not
672 another more inner frame which happens to
673 be in the same block. */
674 write_exp_elt_block (NULL);
3d6b6a90
JG
675 write_exp_elt_sym (sym);
676 write_exp_elt_opcode (OP_VAR_VALUE);
677 }
678 else if ($1.is_a_field_of_this)
679 {
680 /* C++: it hangs off of `this'. Must
681 not inadvertently convert from a method call
682 to data ref. */
683 if (innermost_block == 0 ||
684 contained_in (block_found, innermost_block))
685 innermost_block = block_found;
686 write_exp_elt_opcode (OP_THIS);
687 write_exp_elt_opcode (OP_THIS);
688 write_exp_elt_opcode (STRUCTOP_PTR);
689 write_exp_string ($1.stoken);
690 write_exp_elt_opcode (STRUCTOP_PTR);
691 }
692 else
693 {
1ab3bf1b 694 struct minimal_symbol *msymbol;
3d6b6a90
JG
695 register char *arg = copy_name ($1.stoken);
696
2d336b1b
JK
697 msymbol =
698 lookup_minimal_symbol (arg, NULL, NULL);
1ab3bf1b 699 if (msymbol != NULL)
3d6b6a90 700 {
abe28b92
JK
701 write_exp_msymbol (msymbol,
702 lookup_function_type (builtin_type_int),
703 builtin_type_int);
3d6b6a90 704 }
1ab3bf1b 705 else if (!have_full_symbols () && !have_partial_symbols ())
3d6b6a90
JG
706 error ("No symbol table is loaded. Use the \"file\" command.");
707 else
708 error ("No symbol \"%s\" in current context.",
709 copy_name ($1.stoken));
710 }
711 }
712 ;
713
714
715ptype : typebase
f843c95f 716 /* "const" and "volatile" are curently ignored. A type qualifier
adee89e8
JK
717 before the type is currently handled in the typebase rule.
718 The reason for recognizing these here (shift/reduce conflicts)
719 might be obsolete now that some pointer to member rules have
720 been deleted. */
f843c95f
JK
721 | typebase CONST_KEYWORD
722 | typebase VOLATILE_KEYWORD
3d6b6a90 723 | typebase abs_decl
f843c95f
JK
724 { $$ = follow_types ($1); }
725 | typebase CONST_KEYWORD abs_decl
726 { $$ = follow_types ($1); }
727 | typebase VOLATILE_KEYWORD abs_decl
728 { $$ = follow_types ($1); }
3d6b6a90
JG
729 ;
730
731abs_decl: '*'
732 { push_type (tp_pointer); $$ = 0; }
733 | '*' abs_decl
734 { push_type (tp_pointer); $$ = $2; }
735 | '&'
736 { push_type (tp_reference); $$ = 0; }
737 | '&' abs_decl
738 { push_type (tp_reference); $$ = $2; }
739 | direct_abs_decl
740 ;
741
742direct_abs_decl: '(' abs_decl ')'
743 { $$ = $2; }
744 | direct_abs_decl array_mod
745 {
746 push_type_int ($2);
747 push_type (tp_array);
748 }
749 | array_mod
750 {
751 push_type_int ($1);
752 push_type (tp_array);
753 $$ = 0;
754 }
f843c95f 755
3d6b6a90
JG
756 | direct_abs_decl func_mod
757 { push_type (tp_function); }
758 | func_mod
759 { push_type (tp_function); }
760 ;
761
762array_mod: '[' ']'
763 { $$ = -1; }
764 | '[' INT ']'
2e6edad1 765 { $$ = $2.val; }
3d6b6a90
JG
766 ;
767
768func_mod: '(' ')'
769 { $$ = 0; }
0e2a896c 770 | '(' nonempty_typelist ')'
be772100 771 { free ((PTR)$2); $$ = 0; }
3d6b6a90
JG
772 ;
773
adee89e8
JK
774/* We used to try to recognize more pointer to member types here, but
775 that didn't work (shift/reduce conflicts meant that these rules never
776 got executed). The problem is that
777 int (foo::bar::baz::bizzle)
778 is a function type but
779 int (foo::bar::baz::bizzle::*)
780 is a pointer to member type. Stroustrup loses again! */
781
3d6b6a90 782type : ptype
2640f7e1
JG
783 | typebase COLONCOLON '*'
784 { $$ = lookup_member_type (builtin_type_int, $1); }
3d6b6a90
JG
785 ;
786
10a297b7 787typebase /* Implements (approximately): (type-qualifier)* type-specifier */
3d6b6a90
JG
788 : TYPENAME
789 { $$ = $1.type; }
790 | INT_KEYWORD
791 { $$ = builtin_type_int; }
792 | LONG
793 { $$ = builtin_type_long; }
794 | SHORT
795 { $$ = builtin_type_short; }
796 | LONG INT_KEYWORD
797 { $$ = builtin_type_long; }
798 | UNSIGNED LONG INT_KEYWORD
799 { $$ = builtin_type_unsigned_long; }
800 | LONG LONG
801 { $$ = builtin_type_long_long; }
802 | LONG LONG INT_KEYWORD
803 { $$ = builtin_type_long_long; }
804 | UNSIGNED LONG LONG
805 { $$ = builtin_type_unsigned_long_long; }
806 | UNSIGNED LONG LONG INT_KEYWORD
807 { $$ = builtin_type_unsigned_long_long; }
808 | SHORT INT_KEYWORD
809 { $$ = builtin_type_short; }
810 | UNSIGNED SHORT INT_KEYWORD
811 { $$ = builtin_type_unsigned_short; }
aa220473
SG
812 | DOUBLE_KEYWORD
813 { $$ = builtin_type_double; }
814 | LONG DOUBLE_KEYWORD
815 { $$ = builtin_type_long_double; }
3d6b6a90
JG
816 | STRUCT name
817 { $$ = lookup_struct (copy_name ($2),
818 expression_context_block); }
8050a57b
FF
819 | CLASS name
820 { $$ = lookup_struct (copy_name ($2),
821 expression_context_block); }
3d6b6a90
JG
822 | UNION name
823 { $$ = lookup_union (copy_name ($2),
824 expression_context_block); }
825 | ENUM name
826 { $$ = lookup_enum (copy_name ($2),
827 expression_context_block); }
828 | UNSIGNED typename
829 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
830 | UNSIGNED
831 { $$ = builtin_type_unsigned_int; }
088c3a0b 832 | SIGNED_KEYWORD typename
a252e715 833 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
088c3a0b 834 | SIGNED_KEYWORD
3d6b6a90 835 { $$ = builtin_type_int; }
4c53d9ca
DHW
836 | TEMPLATE name '<' type '>'
837 { $$ = lookup_template_type(copy_name($2), $4,
838 expression_context_block);
839 }
f843c95f
JK
840 /* "const" and "volatile" are curently ignored. A type qualifier
841 after the type is handled in the ptype rule. I think these could
842 be too. */
10a297b7
PB
843 | CONST_KEYWORD typebase { $$ = $2; }
844 | VOLATILE_KEYWORD typebase { $$ = $2; }
3d6b6a90
JG
845 ;
846
847typename: TYPENAME
848 | INT_KEYWORD
849 {
850 $$.stoken.ptr = "int";
851 $$.stoken.length = 3;
852 $$.type = builtin_type_int;
853 }
854 | LONG
855 {
856 $$.stoken.ptr = "long";
857 $$.stoken.length = 4;
858 $$.type = builtin_type_long;
859 }
860 | SHORT
861 {
862 $$.stoken.ptr = "short";
863 $$.stoken.length = 5;
864 $$.type = builtin_type_short;
865 }
866 ;
867
868nonempty_typelist
869 : type
e35843d4 870 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
6c316cfd 871 $<ivec>$[0] = 1; /* Number of types in vector */
3d6b6a90
JG
872 $$[1] = $1;
873 }
874 | nonempty_typelist ',' type
6c316cfd 875 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
e35843d4 876 $$ = (struct type **) realloc ((char *) $1, len);
3d6b6a90
JG
877 $$[$<ivec>$[0]] = $3;
878 }
879 ;
880
881name : NAME { $$ = $1.stoken; }
882 | BLOCKNAME { $$ = $1.stoken; }
883 | TYPENAME { $$ = $1.stoken; }
884 | NAME_OR_INT { $$ = $1.stoken; }
3d6b6a90
JG
885 ;
886
887name_not_typename : NAME
888 | BLOCKNAME
889/* These would be useful if name_not_typename was useful, but it is just
890 a fake for "variable", so these cause reduce/reduce conflicts because
891 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
892 =exp) or just an exp. If name_not_typename was ever used in an lvalue
893 context where only a name could occur, this might be useful.
894 | NAME_OR_INT
3d6b6a90
JG
895 */
896 ;
897
898%%
899
900/* Take care of parsing a number (anything that starts with a digit).
901 Set yylval and return the token type; update lexptr.
902 LEN is the number of characters in it. */
903
904/*** Needs some error checking for the float case ***/
905
906static int
907parse_number (p, len, parsed_float, putithere)
908 register char *p;
909 register int len;
910 int parsed_float;
911 YYSTYPE *putithere;
912{
a9b32d61
JK
913 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
914 here, and we do kind of silly things like cast to unsigned. */
3d6b6a90
JG
915 register LONGEST n = 0;
916 register LONGEST prevn = 0;
119dfbb7 917 ULONGEST un;
a9b32d61 918
72cd0384 919 register int i = 0;
3d6b6a90
JG
920 register int c;
921 register int base = input_radix;
922 int unsigned_p = 0;
a9b32d61
JK
923
924 /* Number of "L" suffixes encountered. */
2e6edad1 925 int long_p = 0;
a9b32d61
JK
926
927 /* We have found a "L" or "U" suffix. */
928 int found_suffix = 0;
929
119dfbb7 930 ULONGEST high_bit;
2e6edad1
SC
931 struct type *signed_type;
932 struct type *unsigned_type;
3d6b6a90 933
3d6b6a90
JG
934 if (parsed_float)
935 {
936 /* It's a float since it contains a point or an exponent. */
d030f469
MS
937 char c;
938 int num = 0; /* number of tokens scanned by scanf */
939 char saved_char = p[len];
aa220473 940
d030f469 941 p[len] = 0; /* null-terminate the token */
aa220473 942 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
d030f469 943 num = sscanf (p, "%g%c", &putithere->typed_val_float.dval,&c);
aa220473 944 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
d030f469 945 num = sscanf (p, "%lg%c", &putithere->typed_val_float.dval,&c);
aa220473 946 else
07b77f5c
FF
947 {
948#ifdef PRINTF_HAS_LONG_DOUBLE
d030f469 949 num = sscanf (p, "%Lg%c", &putithere->typed_val_float.dval,&c);
07b77f5c
FF
950#else
951 /* Scan it into a double, then assign it to the long double.
952 This at least wins with values representable in the range
953 of doubles. */
954 double temp;
d030f469 955 num = sscanf (p, "%lg%c", &temp,&c);
07b77f5c
FF
956 putithere->typed_val_float.dval = temp;
957#endif
958 }
d030f469
MS
959 p[len] = saved_char; /* restore the input stream */
960 if (num != 1) /* check scanf found ONLY a float ... */
961 return ERROR;
aa220473
SG
962 /* See if it has `f' or `l' suffix (float or long double). */
963
964 c = tolower (p[len - 1]);
965
966 if (c == 'f')
967 putithere->typed_val_float.type = builtin_type_float;
968 else if (c == 'l')
969 putithere->typed_val_float.type = builtin_type_long_double;
970 else if (isdigit (c) || c == '.')
971 putithere->typed_val_float.type = builtin_type_double;
972 else
973 return ERROR;
974
3d6b6a90
JG
975 return FLOAT;
976 }
977
978 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
979 if (p[0] == '0')
980 switch (p[1])
981 {
982 case 'x':
983 case 'X':
984 if (len >= 3)
985 {
986 p += 2;
987 base = 16;
988 len -= 2;
989 }
990 break;
991
992 case 't':
993 case 'T':
994 case 'd':
995 case 'D':
996 if (len >= 3)
997 {
998 p += 2;
999 base = 10;
1000 len -= 2;
1001 }
1002 break;
1003
1004 default:
1005 base = 8;
1006 break;
1007 }
1008
1009 while (len-- > 0)
1010 {
1011 c = *p++;
1012 if (c >= 'A' && c <= 'Z')
1013 c += 'a' - 'A';
1014 if (c != 'l' && c != 'u')
1015 n *= base;
1016 if (c >= '0' && c <= '9')
a9b32d61
JK
1017 {
1018 if (found_suffix)
1019 return ERROR;
1020 n += i = c - '0';
1021 }
3d6b6a90
JG
1022 else
1023 {
1024 if (base > 10 && c >= 'a' && c <= 'f')
a9b32d61
JK
1025 {
1026 if (found_suffix)
1027 return ERROR;
1028 n += i = c - 'a' + 10;
1029 }
1030 else if (c == 'l')
1031 {
1032 ++long_p;
1033 found_suffix = 1;
1034 }
1035 else if (c == 'u')
1036 {
1037 unsigned_p = 1;
1038 found_suffix = 1;
1039 }
3d6b6a90
JG
1040 else
1041 return ERROR; /* Char not a digit */
1042 }
1043 if (i >= base)
1044 return ERROR; /* Invalid digit in this base */
2e6edad1 1045
2a5ec41d 1046 /* Portably test for overflow (only works for nonzero values, so make
a9b32d61
JK
1047 a second check for zero). FIXME: Can't we just make n and prevn
1048 unsigned and avoid this? */
1049 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1050 unsigned_p = 1; /* Try something unsigned */
1051
1052 /* Portably test for unsigned overflow.
1053 FIXME: This check is wrong; for example it doesn't find overflow
1054 on 0x123456789 when LONGEST is 32 bits. */
1055 if (c != 'l' && c != 'u' && n != 0)
1056 {
119dfbb7 1057 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
a9b32d61
JK
1058 error ("Numeric constant too large.");
1059 }
1060 prevn = n;
1061 }
1062
1063 /* An integer constant is an int, a long, or a long long. An L
1064 suffix forces it to be long; an LL suffix forces it to be long
1065 long. If not forced to a larger size, it gets the first type of
1066 the above that it fits in. To figure out whether it fits, we
1067 shift it right and see whether anything remains. Note that we
1068 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1069 operation, because many compilers will warn about such a shift
1070 (which always produces a zero result). Sometimes TARGET_INT_BIT
1071 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
1072 the case where it is we just always shift the value more than
1073 once, with fewer bits each time. */
1074
119dfbb7 1075 un = (ULONGEST)n >> 2;
a9b32d61 1076 if (long_p == 0
a39f7739 1077 && (un >> (TARGET_INT_BIT - 2)) == 0)
a9b32d61 1078 {
119dfbb7 1079 high_bit = ((ULONGEST)1) << (TARGET_INT_BIT-1);
a9b32d61
JK
1080
1081 /* A large decimal (not hex or octal) constant (between INT_MAX
1082 and UINT_MAX) is a long or unsigned long, according to ANSI,
1083 never an unsigned int, but this code treats it as unsigned
1084 int. This probably should be fixed. GCC gives a warning on
1085 such constants. */
1086
1087 unsigned_type = builtin_type_unsigned_int;
1088 signed_type = builtin_type_int;
1089 }
1090 else if (long_p <= 1
a39f7739 1091 && (un >> (TARGET_LONG_BIT - 2)) == 0)
a9b32d61 1092 {
119dfbb7 1093 high_bit = ((ULONGEST)1) << (TARGET_LONG_BIT-1);
a9b32d61
JK
1094 unsigned_type = builtin_type_unsigned_long;
1095 signed_type = builtin_type_long;
1096 }
1097 else
1098 {
119dfbb7 1099 high_bit = (((ULONGEST)1)
bb1f42d4
JK
1100 << (TARGET_LONG_LONG_BIT - 32 - 1)
1101 << 16
1102 << 16);
1103 if (high_bit == 0)
1104 /* A long long does not fit in a LONGEST. */
1c95d7ab 1105 high_bit =
119dfbb7 1106 (ULONGEST)1 << (sizeof (LONGEST) * HOST_CHAR_BIT - 1);
a9b32d61
JK
1107 unsigned_type = builtin_type_unsigned_long_long;
1108 signed_type = builtin_type_long_long;
3d6b6a90 1109 }
2e6edad1 1110
aa220473 1111 putithere->typed_val_int.val = n;
2e6edad1
SC
1112
1113 /* If the high bit of the worked out type is set then this number
1114 has to be unsigned. */
1115
1116 if (unsigned_p || (n & high_bit))
1117 {
aa220473 1118 putithere->typed_val_int.type = unsigned_type;
2e6edad1
SC
1119 }
1120 else
1121 {
aa220473 1122 putithere->typed_val_int.type = signed_type;
2e6edad1
SC
1123 }
1124
1125 return INT;
3d6b6a90
JG
1126}
1127
1128struct token
1129{
1130 char *operator;
1131 int token;
1132 enum exp_opcode opcode;
1133};
1134
a8a69e63 1135static const struct token tokentab3[] =
3d6b6a90
JG
1136 {
1137 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1138 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1139 };
1140
a8a69e63 1141static const struct token tokentab2[] =
3d6b6a90
JG
1142 {
1143 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1144 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1145 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1146 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1147 {"%=", ASSIGN_MODIFY, BINOP_REM},
e58de8a2
FF
1148 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1149 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1150 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
3d6b6a90
JG
1151 {"++", INCREMENT, BINOP_END},
1152 {"--", DECREMENT, BINOP_END},
1153 {"->", ARROW, BINOP_END},
088c3a0b
JG
1154 {"&&", ANDAND, BINOP_END},
1155 {"||", OROR, BINOP_END},
3d6b6a90
JG
1156 {"::", COLONCOLON, BINOP_END},
1157 {"<<", LSH, BINOP_END},
1158 {">>", RSH, BINOP_END},
1159 {"==", EQUAL, BINOP_END},
1160 {"!=", NOTEQUAL, BINOP_END},
1161 {"<=", LEQ, BINOP_END},
1162 {">=", GEQ, BINOP_END}
1163 };
1164
1165/* Read one token, getting characters through lexptr. */
1166
533d1dc7 1167static int
3d6b6a90
JG
1168yylex ()
1169{
bac89d6c
FF
1170 int c;
1171 int namelen;
1172 unsigned int i;
1173 char *tokstart;
1174 char *tokptr;
1175 int tempbufindex;
1176 static char *tempbuf;
1177 static int tempbufsize;
1178
3d6b6a90
JG
1179 retry:
1180
1181 tokstart = lexptr;
1182 /* See if it is a special token of length 3. */
1183 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
45fe3db4 1184 if (STREQN (tokstart, tokentab3[i].operator, 3))
3d6b6a90
JG
1185 {
1186 lexptr += 3;
1187 yylval.opcode = tokentab3[i].opcode;
1188 return tokentab3[i].token;
1189 }
1190
1191 /* See if it is a special token of length 2. */
1192 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
45fe3db4 1193 if (STREQN (tokstart, tokentab2[i].operator, 2))
3d6b6a90
JG
1194 {
1195 lexptr += 2;
1196 yylval.opcode = tokentab2[i].opcode;
1197 return tokentab2[i].token;
1198 }
1199
1200 switch (c = *tokstart)
1201 {
1202 case 0:
1203 return 0;
1204
1205 case ' ':
1206 case '\t':
1207 case '\n':
1208 lexptr++;
1209 goto retry;
1210
1211 case '\'':
d630b615
FF
1212 /* We either have a character constant ('0' or '\177' for example)
1213 or we have a quoted symbol reference ('foo(int,int)' in C++
1214 for example). */
3d6b6a90
JG
1215 lexptr++;
1216 c = *lexptr++;
1217 if (c == '\\')
1218 c = parse_escape (&lexptr);
961b8ebd
JK
1219 else if (c == '\'')
1220 error ("Empty character constant.");
2e6edad1 1221
aa220473
SG
1222 yylval.typed_val_int.val = c;
1223 yylval.typed_val_int.type = builtin_type_char;
2e6edad1 1224
3d6b6a90
JG
1225 c = *lexptr++;
1226 if (c != '\'')
d630b615
FF
1227 {
1228 namelen = skip_quoted (tokstart) - tokstart;
1229 if (namelen > 2)
1230 {
1231 lexptr = tokstart + namelen;
c0bca41c
JK
1232 if (lexptr[-1] != '\'')
1233 error ("Unmatched single quote.");
d630b615
FF
1234 namelen -= 2;
1235 tokstart++;
1236 goto tryname;
1237 }
1238 error ("Invalid character constant.");
1239 }
2e6edad1 1240 return INT;
3d6b6a90
JG
1241
1242 case '(':
1243 paren_depth++;
1244 lexptr++;
1245 return c;
1246
1247 case ')':
1248 if (paren_depth == 0)
1249 return 0;
1250 paren_depth--;
1251 lexptr++;
1252 return c;
1253
1254 case ',':
1255 if (comma_terminates && paren_depth == 0)
1256 return 0;
1257 lexptr++;
1258 return c;
1259
1260 case '.':
1261 /* Might be a floating point number. */
1262 if (lexptr[1] < '0' || lexptr[1] > '9')
1263 goto symbol; /* Nope, must be a symbol. */
1264 /* FALL THRU into number case. */
1265
1266 case '0':
1267 case '1':
1268 case '2':
1269 case '3':
1270 case '4':
1271 case '5':
1272 case '6':
1273 case '7':
1274 case '8':
1275 case '9':
1276 {
1277 /* It's a number. */
1278 int got_dot = 0, got_e = 0, toktype;
1279 register char *p = tokstart;
1280 int hex = input_radix > 10;
1281
1282 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1283 {
1284 p += 2;
1285 hex = 1;
1286 }
1287 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1288 {
1289 p += 2;
1290 hex = 0;
1291 }
1292
1293 for (;; ++p)
1294 {
ce13daa7
FF
1295 /* This test includes !hex because 'e' is a valid hex digit
1296 and thus does not indicate a floating point number when
1297 the radix is hex. */
3d6b6a90
JG
1298 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1299 got_dot = got_e = 1;
ce13daa7
FF
1300 /* This test does not include !hex, because a '.' always indicates
1301 a decimal floating point number regardless of the radix. */
1302 else if (!got_dot && *p == '.')
3d6b6a90
JG
1303 got_dot = 1;
1304 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1305 && (*p == '-' || *p == '+'))
1306 /* This is the sign of the exponent, not the end of the
1307 number. */
1308 continue;
1309 /* We will take any letters or digits. parse_number will
1310 complain if past the radix, or if L or U are not final. */
1311 else if ((*p < '0' || *p > '9')
1312 && ((*p < 'a' || *p > 'z')
1313 && (*p < 'A' || *p > 'Z')))
1314 break;
1315 }
1316 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1317 if (toktype == ERROR)
1318 {
1319 char *err_copy = (char *) alloca (p - tokstart + 1);
1320
4ed3a9ea 1321 memcpy (err_copy, tokstart, p - tokstart);
3d6b6a90
JG
1322 err_copy[p - tokstart] = 0;
1323 error ("Invalid number \"%s\".", err_copy);
1324 }
1325 lexptr = p;
1326 return toktype;
1327 }
1328
1329 case '+':
1330 case '-':
1331 case '*':
1332 case '/':
1333 case '%':
1334 case '|':
1335 case '&':
1336 case '^':
1337 case '~':
1338 case '!':
1339 case '@':
1340 case '<':
1341 case '>':
1342 case '[':
1343 case ']':
1344 case '?':
1345 case ':':
1346 case '=':
1347 case '{':
1348 case '}':
1349 symbol:
1350 lexptr++;
1351 return c;
1352
1353 case '"':
bac89d6c
FF
1354
1355 /* Build the gdb internal form of the input string in tempbuf,
1356 translating any standard C escape forms seen. Note that the
1357 buffer is null byte terminated *only* for the convenience of
1358 debugging gdb itself and printing the buffer contents when
1359 the buffer contains no embedded nulls. Gdb does not depend
1360 upon the buffer being null byte terminated, it uses the length
1361 string instead. This allows gdb to handle C strings (as well
1362 as strings in other languages) with embedded null bytes */
1363
1364 tokptr = ++tokstart;
1365 tempbufindex = 0;
1366
1367 do {
1368 /* Grow the static temp buffer if necessary, including allocating
1369 the first one on demand. */
1370 if (tempbufindex + 1 >= tempbufsize)
3d6b6a90 1371 {
bac89d6c
FF
1372 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1373 }
1374 switch (*tokptr)
1375 {
1376 case '\0':
1377 case '"':
1378 /* Do nothing, loop will terminate. */
1379 break;
1380 case '\\':
1381 tokptr++;
1382 c = parse_escape (&tokptr);
1383 if (c == -1)
3d6b6a90 1384 {
bac89d6c 1385 continue;
3d6b6a90 1386 }
bac89d6c
FF
1387 tempbuf[tempbufindex++] = c;
1388 break;
1389 default:
1390 tempbuf[tempbufindex++] = *tokptr++;
1391 break;
3d6b6a90 1392 }
bac89d6c
FF
1393 } while ((*tokptr != '"') && (*tokptr != '\0'));
1394 if (*tokptr++ != '"')
1395 {
1396 error ("Unterminated string in expression.");
1397 }
1398 tempbuf[tempbufindex] = '\0'; /* See note above */
1399 yylval.sval.ptr = tempbuf;
1400 yylval.sval.length = tempbufindex;
1401 lexptr = tokptr;
1402 return (STRING);
3d6b6a90
JG
1403 }
1404
1405 if (!(c == '_' || c == '$'
1406 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1407 /* We must have come across a bad character (e.g. ';'). */
1408 error ("Invalid character '%c' in expression.", c);
1409
1410 /* It's a name. See how long it is. */
1411 namelen = 0;
1412 for (c = tokstart[namelen];
1413 (c == '_' || c == '$' || (c >= '0' && c <= '9')
a8d23c73
KH
1414 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1415 {
07422705
PS
1416 /* Template parameter lists are part of the name.
1417 FIXME: This mishandles `print $a<4&&$a>3'. */
1418
1419 if (c == '<')
1420 {
1421 int i = namelen;
1422 int nesting_level = 1;
1423 while (tokstart[++i])
1424 {
1425 if (tokstart[i] == '<')
1426 nesting_level++;
1427 else if (tokstart[i] == '>')
1428 {
1429 if (--nesting_level == 0)
1430 break;
1431 }
1432 }
1433 if (tokstart[i] == '>')
1434 namelen = i;
1435 else
1436 break;
1437 }
1438 c = tokstart[++namelen];
1439 }
3d6b6a90
JG
1440
1441 /* The token "if" terminates the expression and is NOT
1442 removed from the input stream. */
1443 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1444 {
1445 return 0;
1446 }
1447
1448 lexptr += namelen;
1449
d630b615 1450 tryname:
3d6b6a90 1451
3d6b6a90
JG
1452 /* Catch specific keywords. Should be done with a data structure. */
1453 switch (namelen)
1454 {
1455 case 8:
45fe3db4 1456 if (STREQN (tokstart, "unsigned", 8))
3d6b6a90 1457 return UNSIGNED;
5a4e7215 1458 if (current_language->la_language == language_cplus
45fe3db4 1459 && STREQN (tokstart, "template", 8))
4c53d9ca 1460 return TEMPLATE;
45fe3db4 1461 if (STREQN (tokstart, "volatile", 8))
a252e715 1462 return VOLATILE_KEYWORD;
3d6b6a90
JG
1463 break;
1464 case 6:
45fe3db4 1465 if (STREQN (tokstart, "struct", 6))
3d6b6a90 1466 return STRUCT;
45fe3db4 1467 if (STREQN (tokstart, "signed", 6))
088c3a0b 1468 return SIGNED_KEYWORD;
45fe3db4 1469 if (STREQN (tokstart, "sizeof", 6))
3d6b6a90 1470 return SIZEOF;
aa220473
SG
1471 if (STREQN (tokstart, "double", 6))
1472 return DOUBLE_KEYWORD;
3d6b6a90
JG
1473 break;
1474 case 5:
866ecded 1475 if (current_language->la_language == language_cplus
45fe3db4 1476 && STREQN (tokstart, "class", 5))
8050a57b 1477 return CLASS;
45fe3db4 1478 if (STREQN (tokstart, "union", 5))
3d6b6a90 1479 return UNION;
45fe3db4 1480 if (STREQN (tokstart, "short", 5))
3d6b6a90 1481 return SHORT;
45fe3db4 1482 if (STREQN (tokstart, "const", 5))
a252e715 1483 return CONST_KEYWORD;
3d6b6a90
JG
1484 break;
1485 case 4:
45fe3db4 1486 if (STREQN (tokstart, "enum", 4))
3d6b6a90 1487 return ENUM;
45fe3db4 1488 if (STREQN (tokstart, "long", 4))
3d6b6a90 1489 return LONG;
5a4e7215 1490 if (current_language->la_language == language_cplus
45fe3db4 1491 && STREQN (tokstart, "this", 4))
3d6b6a90
JG
1492 {
1493 static const char this_name[] =
1494 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1495
1496 if (lookup_symbol (this_name, expression_context_block,
bcca9a08
FF
1497 VAR_NAMESPACE, (int *) NULL,
1498 (struct symtab **) NULL))
3d6b6a90
JG
1499 return THIS;
1500 }
1501 break;
1502 case 3:
45fe3db4 1503 if (STREQN (tokstart, "int", 3))
3d6b6a90
JG
1504 return INT_KEYWORD;
1505 break;
1506 default:
1507 break;
1508 }
1509
1510 yylval.sval.ptr = tokstart;
1511 yylval.sval.length = namelen;
1512
3d6b6a90
JG
1513 if (*tokstart == '$')
1514 {
c700638c 1515 write_dollar_variable (yylval.sval);
3d6b6a90
JG
1516 return VARIABLE;
1517 }
1518
1519 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1520 functions or symtabs. If this is not so, then ...
1521 Use token-type TYPENAME for symbols that happen to be defined
1522 currently as names of types; NAME for other symbols.
1523 The caller is not constrained to care about the distinction. */
1524 {
1525 char *tmp = copy_name (yylval.sval);
1526 struct symbol *sym;
1527 int is_a_field_of_this = 0;
1528 int hextype;
1529
1530 sym = lookup_symbol (tmp, expression_context_block,
545af6ce
PB
1531 VAR_NAMESPACE,
1532 current_language->la_language == language_cplus
bcca9a08
FF
1533 ? &is_a_field_of_this : (int *) NULL,
1534 (struct symtab **) NULL);
963ee102
JK
1535 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1536 no psymtabs (coff, xcoff, or some future change to blow away the
1537 psymtabs once once symbols are read). */
3d6b6a90 1538 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
963ee102 1539 lookup_symtab (tmp))
3d6b6a90
JG
1540 {
1541 yylval.ssym.sym = sym;
1542 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1543 return BLOCKNAME;
1544 }
1545 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1546 {
f28c6e38
JK
1547#if 1
1548 /* Despite the following flaw, we need to keep this code enabled.
1549 Because we can get called from check_stub_method, if we don't
1550 handle nested types then it screws many operations in any
1551 program which uses nested types. */
dfb4a508
JK
1552 /* In "A::x", if x is a member function of A and there happens
1553 to be a type (nested or not, since the stabs don't make that
1554 distinction) named x, then this code incorrectly thinks we
1555 are dealing with nested types rather than a member function. */
1556
96c68efa
JK
1557 char *p;
1558 char *namestart;
1559 struct symbol *best_sym;
1560
1561 /* Look ahead to detect nested types. This probably should be
1562 done in the grammar, but trying seemed to introduce a lot
1563 of shift/reduce and reduce/reduce conflicts. It's possible
1564 that it could be done, though. Or perhaps a non-grammar, but
1565 less ad hoc, approach would work well. */
1566
1567 /* Since we do not currently have any way of distinguishing
1568 a nested type from a non-nested one (the stabs don't tell
1569 us whether a type is nested), we just ignore the
1570 containing type. */
1571
1572 p = lexptr;
1573 best_sym = sym;
1574 while (1)
1575 {
1576 /* Skip whitespace. */
1577 while (*p == ' ' || *p == '\t' || *p == '\n')
1578 ++p;
1579 if (*p == ':' && p[1] == ':')
1580 {
1581 /* Skip the `::'. */
1582 p += 2;
1583 /* Skip whitespace. */
1584 while (*p == ' ' || *p == '\t' || *p == '\n')
1585 ++p;
1586 namestart = p;
1587 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1588 || (*p >= 'a' && *p <= 'z')
1589 || (*p >= 'A' && *p <= 'Z'))
1590 ++p;
1591 if (p != namestart)
1592 {
1593 struct symbol *cur_sym;
1594 /* As big as the whole rest of the expression, which is
1595 at least big enough. */
a8d23c73
KH
1596 char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1597 char *tmp1;
1598
1599 tmp1 = ncopy;
1600 memcpy (tmp1, tmp, strlen (tmp));
1601 tmp1 += strlen (tmp);
1602 memcpy (tmp1, "::", 2);
1603 tmp1 += 2;
1604 memcpy (tmp1, namestart, p - namestart);
1605 tmp1[p - namestart] = '\0';
1606 cur_sym = lookup_symbol (ncopy, expression_context_block,
bcca9a08
FF
1607 VAR_NAMESPACE, (int *) NULL,
1608 (struct symtab **) NULL);
96c68efa
JK
1609 if (cur_sym)
1610 {
1611 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1612 {
1613 best_sym = cur_sym;
1614 lexptr = p;
1615 }
1616 else
1617 break;
1618 }
1619 else
1620 break;
1621 }
1622 else
1623 break;
1624 }
1625 else
1626 break;
1627 }
1628
1629 yylval.tsym.type = SYMBOL_TYPE (best_sym);
dfb4a508
JK
1630#else /* not 0 */
1631 yylval.tsym.type = SYMBOL_TYPE (sym);
1632#endif /* not 0 */
3d6b6a90
JG
1633 return TYPENAME;
1634 }
1635 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1636 return TYPENAME;
1637
1638 /* Input names that aren't symbols but ARE valid hex numbers,
1639 when the input radix permits them, can be names or numbers
1640 depending on the parse. Note we support radixes > 16 here. */
1641 if (!sym &&
1642 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1643 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1644 {
1645 YYSTYPE newlval; /* Its value is ignored. */
1646 hextype = parse_number (tokstart, namelen, 0, &newlval);
1647 if (hextype == INT)
1648 {
1649 yylval.ssym.sym = sym;
1650 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1651 return NAME_OR_INT;
1652 }
3d6b6a90
JG
1653 }
1654
1655 /* Any other kind of symbol */
1656 yylval.ssym.sym = sym;
1657 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1658 return NAME;
1659 }
1660}
1661
1662void
1663yyerror (msg)
1664 char *msg;
1665{
8db1a922 1666 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
3d6b6a90 1667}