* configure.in (mips-sony-bsd*): Fix typo in AC_DEFINE.
[deliverable/binutils-gdb.git] / gdb / c-exp.y
CommitLineData
3d6b6a90 1/* YACC parser for C expressions, for GDB.
ba47c66a
PS
2 Copyright (C) 1986, 1989, 1990, 1991, 1993, 1994
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"
ba47c66a 41#include <string.h>
3d6b6a90 42#include "expression.h"
3d6b6a90 43#include "value.h"
abe28b92 44#include "parser-defs.h"
3d6b6a90 45#include "language.h"
22e39759 46#include "c-lang.h"
100f92e2
JK
47#include "bfd.h" /* Required by objfiles.h. */
48#include "symfile.h" /* Required by objfiles.h. */
49#include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
3d6b6a90 50
19d0f3f4
FF
51/* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
52 as well as gratuitiously global symbol names, so we can have multiple
53 yacc generated parsers in gdb. Note that these are only the variables
54 produced by yacc. If other parser generators (bison, byacc, etc) produce
55 additional global names that conflict at link time, then those parser
56 generators need to be fixed instead of adding those names to this list. */
57
d018c8a6 58#define yymaxdepth c_maxdepth
3d6b6a90
JG
59#define yyparse c_parse
60#define yylex c_lex
61#define yyerror c_error
62#define yylval c_lval
63#define yychar c_char
64#define yydebug c_debug
65#define yypact c_pact
66#define yyr1 c_r1
67#define yyr2 c_r2
68#define yydef c_def
69#define yychk c_chk
70#define yypgo c_pgo
71#define yyact c_act
72#define yyexca c_exca
9ce7cb7c
SG
73#define yyerrflag c_errflag
74#define yynerrs c_nerrs
39bf5952
JG
75#define yyps c_ps
76#define yypv c_pv
77#define yys c_s
d018c8a6 78#define yy_yys c_yys
39bf5952
JG
79#define yystate c_state
80#define yytmp c_tmp
81#define yyv c_v
d018c8a6 82#define yy_yyv c_yyv
39bf5952
JG
83#define yyval c_val
84#define yylloc c_lloc
45fe3db4
FF
85#define yyreds c_reds /* With YYDEBUG defined */
86#define yytoks c_toks /* With YYDEBUG defined */
ea082c0a
MM
87#define yylhs c_yylhs
88#define yylen c_yylen
89#define yydefred c_yydefred
90#define yydgoto c_yydgoto
91#define yysindex c_yysindex
92#define yyrindex c_yyrindex
93#define yygindex c_yygindex
94#define yytable c_yytable
95#define yycheck c_yycheck
19d0f3f4
FF
96
97#ifndef YYDEBUG
98#define YYDEBUG 0 /* Default to no yydebug support */
99#endif
3d6b6a90 100
d26b50b7 101int
1ab3bf1b
JG
102yyparse PARAMS ((void));
103
19d0f3f4 104static int
1ab3bf1b
JG
105yylex PARAMS ((void));
106
107void
108yyerror PARAMS ((char *));
3d6b6a90
JG
109
110%}
111
112/* Although the yacc "value" of an expression is not used,
113 since the result is stored in the structure being created,
114 other node types do have values. */
115
116%union
117 {
118 LONGEST lval;
2e6edad1
SC
119 struct {
120 LONGEST val;
121 struct type *type;
aa220473
SG
122 } typed_val_int;
123 struct {
124 DOUBLEST dval;
125 struct type *type;
126 } typed_val_float;
3d6b6a90
JG
127 struct symbol *sym;
128 struct type *tval;
129 struct stoken sval;
130 struct ttype tsym;
131 struct symtoken ssym;
132 int voidval;
133 struct block *bval;
134 enum exp_opcode opcode;
135 struct internalvar *ivar;
136
137 struct type **tvec;
138 int *ivec;
139 }
140
1ab3bf1b
JG
141%{
142/* YYSTYPE gets defined by %union */
143static int
144parse_number PARAMS ((char *, int, int, YYSTYPE *));
145%}
146
9da75ad3
FF
147%type <voidval> exp exp1 type_exp start variable qualified_name lcurly
148%type <lval> rcurly
2640f7e1 149%type <tval> type typebase
3d6b6a90
JG
150%type <tvec> nonempty_typelist
151/* %type <bval> block */
152
153/* Fancy type parsing. */
154%type <voidval> func_mod direct_abs_decl abs_decl
155%type <tval> ptype
156%type <lval> array_mod
157
aa220473
SG
158%token <typed_val_int> INT
159%token <typed_val_float> FLOAT
3d6b6a90
JG
160
161/* Both NAME and TYPENAME tokens represent symbols in the input,
162 and both convey their data as strings.
163 But a TYPENAME is a string that happens to be defined as a typedef
164 or builtin type name (such as int or char)
165 and a NAME is any other symbol.
166 Contexts where this distinction is not important can use the
167 nonterminal "name", which matches either NAME or TYPENAME. */
168
169%token <sval> STRING
170%token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
171%token <tsym> TYPENAME
172%type <sval> name
173%type <ssym> name_not_typename
174%type <tsym> typename
175
176/* A NAME_OR_INT is a symbol which is not known in the symbol table,
177 but which would parse as a valid number in the current input radix.
178 E.g. "c" when input_radix==16. Depending on the parse, it will be
2e6edad1 179 turned into a name or into a number. */
3d6b6a90 180
2e6edad1 181%token <ssym> NAME_OR_INT
3d6b6a90 182
8050a57b 183%token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
4c53d9ca 184%token TEMPLATE
3d6b6a90
JG
185%token ERROR
186
187/* Special type cases, put in to allow the parser to distinguish different
188 legal basetypes. */
aa220473 189%token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
3d6b6a90 190
c700638c 191%token <voidval> VARIABLE
3d6b6a90
JG
192
193%token <opcode> ASSIGN_MODIFY
194
195/* C++ */
196%token THIS
197
198%left ','
199%left ABOVE_COMMA
200%right '=' ASSIGN_MODIFY
201%right '?'
088c3a0b
JG
202%left OROR
203%left ANDAND
3d6b6a90
JG
204%left '|'
205%left '^'
206%left '&'
207%left EQUAL NOTEQUAL
208%left '<' '>' LEQ GEQ
209%left LSH RSH
210%left '@'
211%left '+' '-'
212%left '*' '/' '%'
213%right UNARY INCREMENT DECREMENT
214%right ARROW '.' '[' '('
215%token <ssym> BLOCKNAME
216%type <bval> block
217%left COLONCOLON
36ce1b64 218
368c8614
MT
219\f
220%%
221
3d6b6a90
JG
222start : exp1
223 | type_exp
224 ;
225
226type_exp: type
227 { write_exp_elt_opcode(OP_TYPE);
228 write_exp_elt_type($1);
229 write_exp_elt_opcode(OP_TYPE);}
230 ;
231
232/* Expressions, including the comma operator. */
233exp1 : exp
234 | exp1 ',' exp
235 { write_exp_elt_opcode (BINOP_COMMA); }
236 ;
237
238/* Expressions, not including the comma operator. */
239exp : '*' exp %prec UNARY
240 { write_exp_elt_opcode (UNOP_IND); }
241
242exp : '&' exp %prec UNARY
243 { write_exp_elt_opcode (UNOP_ADDR); }
244
245exp : '-' exp %prec UNARY
246 { write_exp_elt_opcode (UNOP_NEG); }
247 ;
248
249exp : '!' exp %prec UNARY
e58de8a2 250 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
3d6b6a90
JG
251 ;
252
253exp : '~' exp %prec UNARY
e58de8a2 254 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
3d6b6a90
JG
255 ;
256
257exp : INCREMENT exp %prec UNARY
258 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
259 ;
260
261exp : DECREMENT exp %prec UNARY
262 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
263 ;
264
265exp : exp INCREMENT %prec UNARY
266 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
267 ;
268
269exp : exp DECREMENT %prec UNARY
270 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
271 ;
272
273exp : SIZEOF exp %prec UNARY
274 { write_exp_elt_opcode (UNOP_SIZEOF); }
275 ;
276
277exp : exp ARROW name
278 { write_exp_elt_opcode (STRUCTOP_PTR);
279 write_exp_string ($3);
280 write_exp_elt_opcode (STRUCTOP_PTR); }
281 ;
282
2640f7e1
JG
283exp : exp ARROW qualified_name
284 { /* exp->type::name becomes exp->*(&type::name) */
285 /* Note: this doesn't work if name is a
286 static member! FIXME */
287 write_exp_elt_opcode (UNOP_ADDR);
288 write_exp_elt_opcode (STRUCTOP_MPTR); }
01be6913 289 ;
3d6b6a90
JG
290exp : exp ARROW '*' exp
291 { write_exp_elt_opcode (STRUCTOP_MPTR); }
292 ;
293
294exp : exp '.' name
295 { write_exp_elt_opcode (STRUCTOP_STRUCT);
296 write_exp_string ($3);
297 write_exp_elt_opcode (STRUCTOP_STRUCT); }
298 ;
299
cd10c7e3
SG
300/* start-sanitize-gm
301Need to find a better way to do this...
302exp : exp '@' name
303 { write_exp_elt_opcode (STRUCTOP_FIELD);
304 write_exp_string ($3);
305 write_exp_elt_opcode (STRUCTOP_FIELD);
306 }
307 end-sanitize-gm */
308
2640f7e1
JG
309exp : exp '.' qualified_name
310 { /* exp.type::name becomes exp.*(&type::name) */
311 /* Note: this doesn't work if name is a
312 static member! FIXME */
313 write_exp_elt_opcode (UNOP_ADDR);
314 write_exp_elt_opcode (STRUCTOP_MEMBER); }
01be6913
PB
315 ;
316
3d6b6a90
JG
317exp : exp '.' '*' exp
318 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
319 ;
320
321exp : exp '[' exp1 ']'
322 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
323 ;
324
325exp : exp '('
326 /* This is to save the value of arglist_len
327 being accumulated by an outer function call. */
328 { start_arglist (); }
329 arglist ')' %prec ARROW
330 { write_exp_elt_opcode (OP_FUNCALL);
331 write_exp_elt_longcst ((LONGEST) end_arglist ());
332 write_exp_elt_opcode (OP_FUNCALL); }
333 ;
334
9da75ad3
FF
335lcurly : '{'
336 { start_arglist (); }
337 ;
338
3d6b6a90
JG
339arglist :
340 ;
341
342arglist : exp
343 { arglist_len = 1; }
344 ;
345
346arglist : arglist ',' exp %prec ABOVE_COMMA
347 { arglist_len++; }
348 ;
349
9da75ad3
FF
350rcurly : '}'
351 { $$ = end_arglist () - 1; }
352 ;
353exp : lcurly arglist rcurly %prec ARROW
ec16f701
FF
354 { write_exp_elt_opcode (OP_ARRAY);
355 write_exp_elt_longcst ((LONGEST) 0);
9da75ad3 356 write_exp_elt_longcst ((LONGEST) $3);
ec16f701
FF
357 write_exp_elt_opcode (OP_ARRAY); }
358 ;
359
9da75ad3 360exp : lcurly type rcurly exp %prec UNARY
3d6b6a90
JG
361 { write_exp_elt_opcode (UNOP_MEMVAL);
362 write_exp_elt_type ($2);
363 write_exp_elt_opcode (UNOP_MEMVAL); }
364 ;
365
366exp : '(' type ')' exp %prec UNARY
367 { write_exp_elt_opcode (UNOP_CAST);
368 write_exp_elt_type ($2);
369 write_exp_elt_opcode (UNOP_CAST); }
370 ;
371
372exp : '(' exp1 ')'
373 { }
374 ;
375
376/* Binary operators in order of decreasing precedence. */
377
378exp : exp '@' exp
379 { write_exp_elt_opcode (BINOP_REPEAT); }
380 ;
381
382exp : exp '*' exp
383 { write_exp_elt_opcode (BINOP_MUL); }
384 ;
385
386exp : exp '/' exp
387 { write_exp_elt_opcode (BINOP_DIV); }
388 ;
389
390exp : exp '%' exp
391 { write_exp_elt_opcode (BINOP_REM); }
392 ;
393
394exp : exp '+' exp
395 { write_exp_elt_opcode (BINOP_ADD); }
396 ;
397
398exp : exp '-' exp
399 { write_exp_elt_opcode (BINOP_SUB); }
400 ;
401
402exp : exp LSH exp
403 { write_exp_elt_opcode (BINOP_LSH); }
404 ;
405
406exp : exp RSH exp
407 { write_exp_elt_opcode (BINOP_RSH); }
408 ;
409
410exp : exp EQUAL exp
411 { write_exp_elt_opcode (BINOP_EQUAL); }
412 ;
413
414exp : exp NOTEQUAL exp
415 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
416 ;
417
418exp : exp LEQ exp
419 { write_exp_elt_opcode (BINOP_LEQ); }
420 ;
421
422exp : exp GEQ exp
423 { write_exp_elt_opcode (BINOP_GEQ); }
424 ;
425
426exp : exp '<' exp
427 { write_exp_elt_opcode (BINOP_LESS); }
428 ;
429
430exp : exp '>' exp
431 { write_exp_elt_opcode (BINOP_GTR); }
432 ;
433
434exp : exp '&' exp
e58de8a2 435 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
3d6b6a90
JG
436 ;
437
438exp : exp '^' exp
e58de8a2 439 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
3d6b6a90
JG
440 ;
441
442exp : exp '|' exp
e58de8a2 443 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
3d6b6a90
JG
444 ;
445
088c3a0b 446exp : exp ANDAND exp
e58de8a2 447 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
3d6b6a90
JG
448 ;
449
088c3a0b 450exp : exp OROR exp
e58de8a2 451 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
3d6b6a90
JG
452 ;
453
454exp : exp '?' exp ':' exp %prec '?'
455 { write_exp_elt_opcode (TERNOP_COND); }
456 ;
457
458exp : exp '=' exp
459 { write_exp_elt_opcode (BINOP_ASSIGN); }
460 ;
461
462exp : exp ASSIGN_MODIFY exp
463 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
464 write_exp_elt_opcode ($2);
465 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
466 ;
467
468exp : INT
469 { write_exp_elt_opcode (OP_LONG);
2e6edad1
SC
470 write_exp_elt_type ($1.type);
471 write_exp_elt_longcst ((LONGEST)($1.val));
3d6b6a90
JG
472 write_exp_elt_opcode (OP_LONG); }
473 ;
474
475exp : NAME_OR_INT
476 { YYSTYPE val;
477 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
478 write_exp_elt_opcode (OP_LONG);
aa220473
SG
479 write_exp_elt_type (val.typed_val_int.type);
480 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
3d6b6a90
JG
481 write_exp_elt_opcode (OP_LONG);
482 }
483 ;
484
3d6b6a90
JG
485
486exp : FLOAT
487 { write_exp_elt_opcode (OP_DOUBLE);
aa220473
SG
488 write_exp_elt_type ($1.type);
489 write_exp_elt_dblcst ($1.dval);
3d6b6a90
JG
490 write_exp_elt_opcode (OP_DOUBLE); }
491 ;
492
493exp : variable
494 ;
495
3d6b6a90 496exp : VARIABLE
c700638c 497 /* Already written by write_dollar_variable. */
3d6b6a90
JG
498 ;
499
500exp : SIZEOF '(' type ')' %prec UNARY
501 { write_exp_elt_opcode (OP_LONG);
502 write_exp_elt_type (builtin_type_int);
940d5967 503 CHECK_TYPEDEF ($3);
3d6b6a90
JG
504 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
505 write_exp_elt_opcode (OP_LONG); }
506 ;
507
508exp : STRING
c4413e2c
FF
509 { /* C strings are converted into array constants with
510 an explicit null byte added at the end. Thus
511 the array upper bound is the string length.
512 There is no such thing in C as a completely empty
513 string. */
514 char *sp = $1.ptr; int count = $1.length;
515 while (count-- > 0)
516 {
517 write_exp_elt_opcode (OP_LONG);
518 write_exp_elt_type (builtin_type_char);
519 write_exp_elt_longcst ((LONGEST)(*sp++));
520 write_exp_elt_opcode (OP_LONG);
521 }
522 write_exp_elt_opcode (OP_LONG);
523 write_exp_elt_type (builtin_type_char);
524 write_exp_elt_longcst ((LONGEST)'\0');
525 write_exp_elt_opcode (OP_LONG);
526 write_exp_elt_opcode (OP_ARRAY);
527 write_exp_elt_longcst ((LONGEST) 0);
528 write_exp_elt_longcst ((LONGEST) ($1.length));
529 write_exp_elt_opcode (OP_ARRAY); }
3d6b6a90
JG
530 ;
531
532/* C++. */
533exp : THIS
534 { write_exp_elt_opcode (OP_THIS);
535 write_exp_elt_opcode (OP_THIS); }
536 ;
537
538/* end of C++. */
539
540block : BLOCKNAME
541 {
542 if ($1.sym != 0)
543 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
544 else
545 {
546 struct symtab *tem =
547 lookup_symtab (copy_name ($1.stoken));
548 if (tem)
13ffa6be 549 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), STATIC_BLOCK);
3d6b6a90
JG
550 else
551 error ("No file or function \"%s\".",
552 copy_name ($1.stoken));
553 }
554 }
555 ;
556
557block : block COLONCOLON name
558 { struct symbol *tem
559 = lookup_symbol (copy_name ($3), $1,
bcca9a08
FF
560 VAR_NAMESPACE, (int *) NULL,
561 (struct symtab **) NULL);
3d6b6a90
JG
562 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
563 error ("No function \"%s\" in specified context.",
564 copy_name ($3));
565 $$ = SYMBOL_BLOCK_VALUE (tem); }
566 ;
567
568variable: block COLONCOLON name
569 { struct symbol *sym;
570 sym = lookup_symbol (copy_name ($3), $1,
bcca9a08
FF
571 VAR_NAMESPACE, (int *) NULL,
572 (struct symtab **) NULL);
3d6b6a90
JG
573 if (sym == 0)
574 error ("No symbol \"%s\" in specified context.",
575 copy_name ($3));
576
577 write_exp_elt_opcode (OP_VAR_VALUE);
479fdd26
JK
578 /* block_found is set by lookup_symbol. */
579 write_exp_elt_block (block_found);
3d6b6a90
JG
580 write_exp_elt_sym (sym);
581 write_exp_elt_opcode (OP_VAR_VALUE); }
582 ;
583
2640f7e1 584qualified_name: typebase COLONCOLON name
3d6b6a90
JG
585 {
586 struct type *type = $1;
587 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
588 && TYPE_CODE (type) != TYPE_CODE_UNION)
589 error ("`%s' is not defined as an aggregate type.",
590 TYPE_NAME (type));
591
592 write_exp_elt_opcode (OP_SCOPE);
593 write_exp_elt_type (type);
2640f7e1 594 write_exp_string ($3);
3d6b6a90
JG
595 write_exp_elt_opcode (OP_SCOPE);
596 }
2640f7e1 597 | typebase COLONCOLON '~' name
3d6b6a90
JG
598 {
599 struct type *type = $1;
01be6913 600 struct stoken tmp_token;
3d6b6a90
JG
601 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
602 && TYPE_CODE (type) != TYPE_CODE_UNION)
603 error ("`%s' is not defined as an aggregate type.",
604 TYPE_NAME (type));
605
2e4964ad 606 if (!STREQ (type_name_no_tag (type), $4.ptr))
3d6b6a90 607 error ("invalid destructor `%s::~%s'",
2640f7e1 608 type_name_no_tag (type), $4.ptr);
3d6b6a90 609
2640f7e1
JG
610 tmp_token.ptr = (char*) alloca ($4.length + 2);
611 tmp_token.length = $4.length + 1;
01be6913 612 tmp_token.ptr[0] = '~';
2640f7e1 613 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
01be6913 614 tmp_token.ptr[tmp_token.length] = 0;
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;
a39f7739 917 unsigned LONGEST 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
45364c8a 930 unsigned LONGEST high_bit;
2e6edad1
SC
931 struct type *signed_type;
932 struct type *unsigned_type;
3d6b6a90 933
3d6b6a90
JG
934 if (parsed_float)
935 {
aa220473
SG
936 char c;
937
3d6b6a90 938 /* It's a float since it contains a point or an exponent. */
aa220473
SG
939
940 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
941 sscanf (p, "%g", &putithere->typed_val_float.dval);
942 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
943 sscanf (p, "%lg", &putithere->typed_val_float.dval);
944 else
07b77f5c
FF
945 {
946#ifdef PRINTF_HAS_LONG_DOUBLE
947 sscanf (p, "%Lg", &putithere->typed_val_float.dval);
948#else
949 /* Scan it into a double, then assign it to the long double.
950 This at least wins with values representable in the range
951 of doubles. */
952 double temp;
953 sscanf (p, "%lg", &temp);
954 putithere->typed_val_float.dval = temp;
955#endif
956 }
aa220473
SG
957
958 /* See if it has `f' or `l' suffix (float or long double). */
959
960 c = tolower (p[len - 1]);
961
962 if (c == 'f')
963 putithere->typed_val_float.type = builtin_type_float;
964 else if (c == 'l')
965 putithere->typed_val_float.type = builtin_type_long_double;
966 else if (isdigit (c) || c == '.')
967 putithere->typed_val_float.type = builtin_type_double;
968 else
969 return ERROR;
970
3d6b6a90
JG
971 return FLOAT;
972 }
973
974 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
975 if (p[0] == '0')
976 switch (p[1])
977 {
978 case 'x':
979 case 'X':
980 if (len >= 3)
981 {
982 p += 2;
983 base = 16;
984 len -= 2;
985 }
986 break;
987
988 case 't':
989 case 'T':
990 case 'd':
991 case 'D':
992 if (len >= 3)
993 {
994 p += 2;
995 base = 10;
996 len -= 2;
997 }
998 break;
999
1000 default:
1001 base = 8;
1002 break;
1003 }
1004
1005 while (len-- > 0)
1006 {
1007 c = *p++;
1008 if (c >= 'A' && c <= 'Z')
1009 c += 'a' - 'A';
1010 if (c != 'l' && c != 'u')
1011 n *= base;
1012 if (c >= '0' && c <= '9')
a9b32d61
JK
1013 {
1014 if (found_suffix)
1015 return ERROR;
1016 n += i = c - '0';
1017 }
3d6b6a90
JG
1018 else
1019 {
1020 if (base > 10 && c >= 'a' && c <= 'f')
a9b32d61
JK
1021 {
1022 if (found_suffix)
1023 return ERROR;
1024 n += i = c - 'a' + 10;
1025 }
1026 else if (c == 'l')
1027 {
1028 ++long_p;
1029 found_suffix = 1;
1030 }
1031 else if (c == 'u')
1032 {
1033 unsigned_p = 1;
1034 found_suffix = 1;
1035 }
3d6b6a90
JG
1036 else
1037 return ERROR; /* Char not a digit */
1038 }
1039 if (i >= base)
1040 return ERROR; /* Invalid digit in this base */
2e6edad1 1041
2a5ec41d 1042 /* Portably test for overflow (only works for nonzero values, so make
a9b32d61
JK
1043 a second check for zero). FIXME: Can't we just make n and prevn
1044 unsigned and avoid this? */
1045 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1046 unsigned_p = 1; /* Try something unsigned */
1047
1048 /* Portably test for unsigned overflow.
1049 FIXME: This check is wrong; for example it doesn't find overflow
1050 on 0x123456789 when LONGEST is 32 bits. */
1051 if (c != 'l' && c != 'u' && n != 0)
1052 {
1053 if ((unsigned_p && (unsigned LONGEST) prevn >= (unsigned LONGEST) n))
1054 error ("Numeric constant too large.");
1055 }
1056 prevn = n;
1057 }
1058
1059 /* An integer constant is an int, a long, or a long long. An L
1060 suffix forces it to be long; an LL suffix forces it to be long
1061 long. If not forced to a larger size, it gets the first type of
1062 the above that it fits in. To figure out whether it fits, we
1063 shift it right and see whether anything remains. Note that we
1064 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1065 operation, because many compilers will warn about such a shift
1066 (which always produces a zero result). Sometimes TARGET_INT_BIT
1067 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
1068 the case where it is we just always shift the value more than
1069 once, with fewer bits each time. */
1070
a39f7739 1071 un = (unsigned LONGEST)n >> 2;
a9b32d61 1072 if (long_p == 0
a39f7739 1073 && (un >> (TARGET_INT_BIT - 2)) == 0)
a9b32d61
JK
1074 {
1075 high_bit = ((unsigned LONGEST)1) << (TARGET_INT_BIT-1);
1076
1077 /* A large decimal (not hex or octal) constant (between INT_MAX
1078 and UINT_MAX) is a long or unsigned long, according to ANSI,
1079 never an unsigned int, but this code treats it as unsigned
1080 int. This probably should be fixed. GCC gives a warning on
1081 such constants. */
1082
1083 unsigned_type = builtin_type_unsigned_int;
1084 signed_type = builtin_type_int;
1085 }
1086 else if (long_p <= 1
a39f7739 1087 && (un >> (TARGET_LONG_BIT - 2)) == 0)
a9b32d61
JK
1088 {
1089 high_bit = ((unsigned LONGEST)1) << (TARGET_LONG_BIT-1);
1090 unsigned_type = builtin_type_unsigned_long;
1091 signed_type = builtin_type_long;
1092 }
1093 else
1094 {
bb1f42d4
JK
1095 high_bit = (((unsigned LONGEST)1)
1096 << (TARGET_LONG_LONG_BIT - 32 - 1)
1097 << 16
1098 << 16);
1099 if (high_bit == 0)
1100 /* A long long does not fit in a LONGEST. */
1c95d7ab
JK
1101 high_bit =
1102 (unsigned LONGEST)1 << (sizeof (LONGEST) * HOST_CHAR_BIT - 1);
a9b32d61
JK
1103 unsigned_type = builtin_type_unsigned_long_long;
1104 signed_type = builtin_type_long_long;
3d6b6a90 1105 }
2e6edad1 1106
aa220473 1107 putithere->typed_val_int.val = n;
2e6edad1
SC
1108
1109 /* If the high bit of the worked out type is set then this number
1110 has to be unsigned. */
1111
1112 if (unsigned_p || (n & high_bit))
1113 {
aa220473 1114 putithere->typed_val_int.type = unsigned_type;
2e6edad1
SC
1115 }
1116 else
1117 {
aa220473 1118 putithere->typed_val_int.type = signed_type;
2e6edad1
SC
1119 }
1120
1121 return INT;
3d6b6a90
JG
1122}
1123
1124struct token
1125{
1126 char *operator;
1127 int token;
1128 enum exp_opcode opcode;
1129};
1130
a8a69e63 1131static const struct token tokentab3[] =
3d6b6a90
JG
1132 {
1133 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1134 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1135 };
1136
a8a69e63 1137static const struct token tokentab2[] =
3d6b6a90
JG
1138 {
1139 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1140 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1141 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1142 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1143 {"%=", ASSIGN_MODIFY, BINOP_REM},
e58de8a2
FF
1144 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1145 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1146 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
3d6b6a90
JG
1147 {"++", INCREMENT, BINOP_END},
1148 {"--", DECREMENT, BINOP_END},
1149 {"->", ARROW, BINOP_END},
088c3a0b
JG
1150 {"&&", ANDAND, BINOP_END},
1151 {"||", OROR, BINOP_END},
3d6b6a90
JG
1152 {"::", COLONCOLON, BINOP_END},
1153 {"<<", LSH, BINOP_END},
1154 {">>", RSH, BINOP_END},
1155 {"==", EQUAL, BINOP_END},
1156 {"!=", NOTEQUAL, BINOP_END},
1157 {"<=", LEQ, BINOP_END},
1158 {">=", GEQ, BINOP_END}
1159 };
1160
1161/* Read one token, getting characters through lexptr. */
1162
533d1dc7 1163static int
3d6b6a90
JG
1164yylex ()
1165{
bac89d6c
FF
1166 int c;
1167 int namelen;
1168 unsigned int i;
1169 char *tokstart;
1170 char *tokptr;
1171 int tempbufindex;
1172 static char *tempbuf;
1173 static int tempbufsize;
1174
3d6b6a90
JG
1175 retry:
1176
1177 tokstart = lexptr;
1178 /* See if it is a special token of length 3. */
1179 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
45fe3db4 1180 if (STREQN (tokstart, tokentab3[i].operator, 3))
3d6b6a90
JG
1181 {
1182 lexptr += 3;
1183 yylval.opcode = tokentab3[i].opcode;
1184 return tokentab3[i].token;
1185 }
1186
1187 /* See if it is a special token of length 2. */
1188 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
45fe3db4 1189 if (STREQN (tokstart, tokentab2[i].operator, 2))
3d6b6a90
JG
1190 {
1191 lexptr += 2;
1192 yylval.opcode = tokentab2[i].opcode;
1193 return tokentab2[i].token;
1194 }
1195
1196 switch (c = *tokstart)
1197 {
1198 case 0:
1199 return 0;
1200
1201 case ' ':
1202 case '\t':
1203 case '\n':
1204 lexptr++;
1205 goto retry;
1206
1207 case '\'':
d630b615
FF
1208 /* We either have a character constant ('0' or '\177' for example)
1209 or we have a quoted symbol reference ('foo(int,int)' in C++
1210 for example). */
3d6b6a90
JG
1211 lexptr++;
1212 c = *lexptr++;
1213 if (c == '\\')
1214 c = parse_escape (&lexptr);
961b8ebd
JK
1215 else if (c == '\'')
1216 error ("Empty character constant.");
2e6edad1 1217
aa220473
SG
1218 yylval.typed_val_int.val = c;
1219 yylval.typed_val_int.type = builtin_type_char;
2e6edad1 1220
3d6b6a90
JG
1221 c = *lexptr++;
1222 if (c != '\'')
d630b615
FF
1223 {
1224 namelen = skip_quoted (tokstart) - tokstart;
1225 if (namelen > 2)
1226 {
1227 lexptr = tokstart + namelen;
c0bca41c
JK
1228 if (lexptr[-1] != '\'')
1229 error ("Unmatched single quote.");
d630b615
FF
1230 namelen -= 2;
1231 tokstart++;
1232 goto tryname;
1233 }
1234 error ("Invalid character constant.");
1235 }
2e6edad1 1236 return INT;
3d6b6a90
JG
1237
1238 case '(':
1239 paren_depth++;
1240 lexptr++;
1241 return c;
1242
1243 case ')':
1244 if (paren_depth == 0)
1245 return 0;
1246 paren_depth--;
1247 lexptr++;
1248 return c;
1249
1250 case ',':
1251 if (comma_terminates && paren_depth == 0)
1252 return 0;
1253 lexptr++;
1254 return c;
1255
1256 case '.':
1257 /* Might be a floating point number. */
1258 if (lexptr[1] < '0' || lexptr[1] > '9')
1259 goto symbol; /* Nope, must be a symbol. */
1260 /* FALL THRU into number case. */
1261
1262 case '0':
1263 case '1':
1264 case '2':
1265 case '3':
1266 case '4':
1267 case '5':
1268 case '6':
1269 case '7':
1270 case '8':
1271 case '9':
1272 {
1273 /* It's a number. */
1274 int got_dot = 0, got_e = 0, toktype;
1275 register char *p = tokstart;
1276 int hex = input_radix > 10;
1277
1278 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1279 {
1280 p += 2;
1281 hex = 1;
1282 }
1283 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1284 {
1285 p += 2;
1286 hex = 0;
1287 }
1288
1289 for (;; ++p)
1290 {
ce13daa7
FF
1291 /* This test includes !hex because 'e' is a valid hex digit
1292 and thus does not indicate a floating point number when
1293 the radix is hex. */
3d6b6a90
JG
1294 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1295 got_dot = got_e = 1;
ce13daa7
FF
1296 /* This test does not include !hex, because a '.' always indicates
1297 a decimal floating point number regardless of the radix. */
1298 else if (!got_dot && *p == '.')
3d6b6a90
JG
1299 got_dot = 1;
1300 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1301 && (*p == '-' || *p == '+'))
1302 /* This is the sign of the exponent, not the end of the
1303 number. */
1304 continue;
1305 /* We will take any letters or digits. parse_number will
1306 complain if past the radix, or if L or U are not final. */
1307 else if ((*p < '0' || *p > '9')
1308 && ((*p < 'a' || *p > 'z')
1309 && (*p < 'A' || *p > 'Z')))
1310 break;
1311 }
1312 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1313 if (toktype == ERROR)
1314 {
1315 char *err_copy = (char *) alloca (p - tokstart + 1);
1316
4ed3a9ea 1317 memcpy (err_copy, tokstart, p - tokstart);
3d6b6a90
JG
1318 err_copy[p - tokstart] = 0;
1319 error ("Invalid number \"%s\".", err_copy);
1320 }
1321 lexptr = p;
1322 return toktype;
1323 }
1324
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 case '=':
1343 case '{':
1344 case '}':
1345 symbol:
1346 lexptr++;
1347 return c;
1348
1349 case '"':
bac89d6c
FF
1350
1351 /* Build the gdb internal form of the input string in tempbuf,
1352 translating any standard C escape forms seen. Note that the
1353 buffer is null byte terminated *only* for the convenience of
1354 debugging gdb itself and printing the buffer contents when
1355 the buffer contains no embedded nulls. Gdb does not depend
1356 upon the buffer being null byte terminated, it uses the length
1357 string instead. This allows gdb to handle C strings (as well
1358 as strings in other languages) with embedded null bytes */
1359
1360 tokptr = ++tokstart;
1361 tempbufindex = 0;
1362
1363 do {
1364 /* Grow the static temp buffer if necessary, including allocating
1365 the first one on demand. */
1366 if (tempbufindex + 1 >= tempbufsize)
3d6b6a90 1367 {
bac89d6c
FF
1368 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1369 }
1370 switch (*tokptr)
1371 {
1372 case '\0':
1373 case '"':
1374 /* Do nothing, loop will terminate. */
1375 break;
1376 case '\\':
1377 tokptr++;
1378 c = parse_escape (&tokptr);
1379 if (c == -1)
3d6b6a90 1380 {
bac89d6c 1381 continue;
3d6b6a90 1382 }
bac89d6c
FF
1383 tempbuf[tempbufindex++] = c;
1384 break;
1385 default:
1386 tempbuf[tempbufindex++] = *tokptr++;
1387 break;
3d6b6a90 1388 }
bac89d6c
FF
1389 } while ((*tokptr != '"') && (*tokptr != '\0'));
1390 if (*tokptr++ != '"')
1391 {
1392 error ("Unterminated string in expression.");
1393 }
1394 tempbuf[tempbufindex] = '\0'; /* See note above */
1395 yylval.sval.ptr = tempbuf;
1396 yylval.sval.length = tempbufindex;
1397 lexptr = tokptr;
1398 return (STRING);
3d6b6a90
JG
1399 }
1400
1401 if (!(c == '_' || c == '$'
1402 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1403 /* We must have come across a bad character (e.g. ';'). */
1404 error ("Invalid character '%c' in expression.", c);
1405
1406 /* It's a name. See how long it is. */
1407 namelen = 0;
1408 for (c = tokstart[namelen];
1409 (c == '_' || c == '$' || (c >= '0' && c <= '9')
a8d23c73
KH
1410 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1411 {
1412 if (c == '<')
e38e7f47
KH
1413 {
1414 int i = namelen;
1415 while (tokstart[++i] && tokstart[i] != '>');
1416 if (tokstart[i] == '>')
1417 namelen = i;
1418 }
a8d23c73
KH
1419 c = tokstart[++namelen];
1420 }
3d6b6a90
JG
1421
1422 /* The token "if" terminates the expression and is NOT
1423 removed from the input stream. */
1424 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1425 {
1426 return 0;
1427 }
1428
1429 lexptr += namelen;
1430
d630b615 1431 tryname:
3d6b6a90 1432
3d6b6a90
JG
1433 /* Catch specific keywords. Should be done with a data structure. */
1434 switch (namelen)
1435 {
1436 case 8:
45fe3db4 1437 if (STREQN (tokstart, "unsigned", 8))
3d6b6a90 1438 return UNSIGNED;
5a4e7215 1439 if (current_language->la_language == language_cplus
45fe3db4 1440 && STREQN (tokstart, "template", 8))
4c53d9ca 1441 return TEMPLATE;
45fe3db4 1442 if (STREQN (tokstart, "volatile", 8))
a252e715 1443 return VOLATILE_KEYWORD;
3d6b6a90
JG
1444 break;
1445 case 6:
45fe3db4 1446 if (STREQN (tokstart, "struct", 6))
3d6b6a90 1447 return STRUCT;
45fe3db4 1448 if (STREQN (tokstart, "signed", 6))
088c3a0b 1449 return SIGNED_KEYWORD;
45fe3db4 1450 if (STREQN (tokstart, "sizeof", 6))
3d6b6a90 1451 return SIZEOF;
aa220473
SG
1452 if (STREQN (tokstart, "double", 6))
1453 return DOUBLE_KEYWORD;
3d6b6a90
JG
1454 break;
1455 case 5:
866ecded 1456 if (current_language->la_language == language_cplus
45fe3db4 1457 && STREQN (tokstart, "class", 5))
8050a57b 1458 return CLASS;
45fe3db4 1459 if (STREQN (tokstart, "union", 5))
3d6b6a90 1460 return UNION;
45fe3db4 1461 if (STREQN (tokstart, "short", 5))
3d6b6a90 1462 return SHORT;
45fe3db4 1463 if (STREQN (tokstart, "const", 5))
a252e715 1464 return CONST_KEYWORD;
3d6b6a90
JG
1465 break;
1466 case 4:
45fe3db4 1467 if (STREQN (tokstart, "enum", 4))
3d6b6a90 1468 return ENUM;
45fe3db4 1469 if (STREQN (tokstart, "long", 4))
3d6b6a90 1470 return LONG;
5a4e7215 1471 if (current_language->la_language == language_cplus
45fe3db4 1472 && STREQN (tokstart, "this", 4))
3d6b6a90
JG
1473 {
1474 static const char this_name[] =
1475 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1476
1477 if (lookup_symbol (this_name, expression_context_block,
bcca9a08
FF
1478 VAR_NAMESPACE, (int *) NULL,
1479 (struct symtab **) NULL))
3d6b6a90
JG
1480 return THIS;
1481 }
1482 break;
1483 case 3:
45fe3db4 1484 if (STREQN (tokstart, "int", 3))
3d6b6a90
JG
1485 return INT_KEYWORD;
1486 break;
1487 default:
1488 break;
1489 }
1490
1491 yylval.sval.ptr = tokstart;
1492 yylval.sval.length = namelen;
1493
3d6b6a90
JG
1494 if (*tokstart == '$')
1495 {
c700638c 1496 write_dollar_variable (yylval.sval);
3d6b6a90
JG
1497 return VARIABLE;
1498 }
1499
1500 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1501 functions or symtabs. If this is not so, then ...
1502 Use token-type TYPENAME for symbols that happen to be defined
1503 currently as names of types; NAME for other symbols.
1504 The caller is not constrained to care about the distinction. */
1505 {
1506 char *tmp = copy_name (yylval.sval);
1507 struct symbol *sym;
1508 int is_a_field_of_this = 0;
1509 int hextype;
1510
1511 sym = lookup_symbol (tmp, expression_context_block,
545af6ce
PB
1512 VAR_NAMESPACE,
1513 current_language->la_language == language_cplus
bcca9a08
FF
1514 ? &is_a_field_of_this : (int *) NULL,
1515 (struct symtab **) NULL);
963ee102
JK
1516 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1517 no psymtabs (coff, xcoff, or some future change to blow away the
1518 psymtabs once once symbols are read). */
3d6b6a90 1519 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
963ee102 1520 lookup_symtab (tmp))
3d6b6a90
JG
1521 {
1522 yylval.ssym.sym = sym;
1523 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1524 return BLOCKNAME;
1525 }
1526 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1527 {
f28c6e38
JK
1528#if 1
1529 /* Despite the following flaw, we need to keep this code enabled.
1530 Because we can get called from check_stub_method, if we don't
1531 handle nested types then it screws many operations in any
1532 program which uses nested types. */
dfb4a508
JK
1533 /* In "A::x", if x is a member function of A and there happens
1534 to be a type (nested or not, since the stabs don't make that
1535 distinction) named x, then this code incorrectly thinks we
1536 are dealing with nested types rather than a member function. */
1537
96c68efa
JK
1538 char *p;
1539 char *namestart;
1540 struct symbol *best_sym;
1541
1542 /* Look ahead to detect nested types. This probably should be
1543 done in the grammar, but trying seemed to introduce a lot
1544 of shift/reduce and reduce/reduce conflicts. It's possible
1545 that it could be done, though. Or perhaps a non-grammar, but
1546 less ad hoc, approach would work well. */
1547
1548 /* Since we do not currently have any way of distinguishing
1549 a nested type from a non-nested one (the stabs don't tell
1550 us whether a type is nested), we just ignore the
1551 containing type. */
1552
1553 p = lexptr;
1554 best_sym = sym;
1555 while (1)
1556 {
1557 /* Skip whitespace. */
1558 while (*p == ' ' || *p == '\t' || *p == '\n')
1559 ++p;
1560 if (*p == ':' && p[1] == ':')
1561 {
1562 /* Skip the `::'. */
1563 p += 2;
1564 /* Skip whitespace. */
1565 while (*p == ' ' || *p == '\t' || *p == '\n')
1566 ++p;
1567 namestart = p;
1568 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1569 || (*p >= 'a' && *p <= 'z')
1570 || (*p >= 'A' && *p <= 'Z'))
1571 ++p;
1572 if (p != namestart)
1573 {
1574 struct symbol *cur_sym;
1575 /* As big as the whole rest of the expression, which is
1576 at least big enough. */
a8d23c73
KH
1577 char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1578 char *tmp1;
1579
1580 tmp1 = ncopy;
1581 memcpy (tmp1, tmp, strlen (tmp));
1582 tmp1 += strlen (tmp);
1583 memcpy (tmp1, "::", 2);
1584 tmp1 += 2;
1585 memcpy (tmp1, namestart, p - namestart);
1586 tmp1[p - namestart] = '\0';
1587 cur_sym = lookup_symbol (ncopy, expression_context_block,
bcca9a08
FF
1588 VAR_NAMESPACE, (int *) NULL,
1589 (struct symtab **) NULL);
96c68efa
JK
1590 if (cur_sym)
1591 {
1592 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1593 {
1594 best_sym = cur_sym;
1595 lexptr = p;
1596 }
1597 else
1598 break;
1599 }
1600 else
1601 break;
1602 }
1603 else
1604 break;
1605 }
1606 else
1607 break;
1608 }
1609
1610 yylval.tsym.type = SYMBOL_TYPE (best_sym);
dfb4a508
JK
1611#else /* not 0 */
1612 yylval.tsym.type = SYMBOL_TYPE (sym);
1613#endif /* not 0 */
3d6b6a90
JG
1614 return TYPENAME;
1615 }
1616 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1617 return TYPENAME;
1618
1619 /* Input names that aren't symbols but ARE valid hex numbers,
1620 when the input radix permits them, can be names or numbers
1621 depending on the parse. Note we support radixes > 16 here. */
1622 if (!sym &&
1623 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1624 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1625 {
1626 YYSTYPE newlval; /* Its value is ignored. */
1627 hextype = parse_number (tokstart, namelen, 0, &newlval);
1628 if (hextype == INT)
1629 {
1630 yylval.ssym.sym = sym;
1631 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1632 return NAME_OR_INT;
1633 }
3d6b6a90
JG
1634 }
1635
1636 /* Any other kind of symbol */
1637 yylval.ssym.sym = sym;
1638 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1639 return NAME;
1640 }
1641}
1642
1643void
1644yyerror (msg)
1645 char *msg;
1646{
8db1a922 1647 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
3d6b6a90 1648}
This page took 0.272903 seconds and 4 git commands to generate.