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