* aout64.h (BMAGIC): 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
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
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 */
19d0f3f4
FF
87
88#ifndef YYDEBUG
89#define YYDEBUG 0 /* Default to no yydebug support */
90#endif
3d6b6a90 91
d26b50b7 92int
1ab3bf1b
JG
93yyparse PARAMS ((void));
94
19d0f3f4 95static int
1ab3bf1b
JG
96yylex PARAMS ((void));
97
98void
99yyerror PARAMS ((char *));
3d6b6a90
JG
100
101%}
102
103/* Although the yacc "value" of an expression is not used,
104 since the result is stored in the structure being created,
105 other node types do have values. */
106
107%union
108 {
109 LONGEST lval;
2e6edad1
SC
110 struct {
111 LONGEST val;
112 struct type *type;
113 } typed_val;
3d6b6a90
JG
114 double dval;
115 struct symbol *sym;
116 struct type *tval;
117 struct stoken sval;
118 struct ttype tsym;
119 struct symtoken ssym;
120 int voidval;
121 struct block *bval;
122 enum exp_opcode opcode;
123 struct internalvar *ivar;
124
125 struct type **tvec;
126 int *ivec;
127 }
128
1ab3bf1b
JG
129%{
130/* YYSTYPE gets defined by %union */
131static int
132parse_number PARAMS ((char *, int, int, YYSTYPE *));
133%}
134
9da75ad3
FF
135%type <voidval> exp exp1 type_exp start variable qualified_name lcurly
136%type <lval> rcurly
2640f7e1 137%type <tval> type typebase
3d6b6a90
JG
138%type <tvec> nonempty_typelist
139/* %type <bval> block */
140
141/* Fancy type parsing. */
142%type <voidval> func_mod direct_abs_decl abs_decl
143%type <tval> ptype
144%type <lval> array_mod
145
2e6edad1 146%token <typed_val> INT
3d6b6a90
JG
147%token <dval> FLOAT
148
149/* Both NAME and TYPENAME tokens represent symbols in the input,
150 and both convey their data as strings.
151 But a TYPENAME is a string that happens to be defined as a typedef
152 or builtin type name (such as int or char)
153 and a NAME is any other symbol.
154 Contexts where this distinction is not important can use the
155 nonterminal "name", which matches either NAME or TYPENAME. */
156
157%token <sval> STRING
158%token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
159%token <tsym> TYPENAME
160%type <sval> name
161%type <ssym> name_not_typename
162%type <tsym> typename
163
164/* A NAME_OR_INT is a symbol which is not known in the symbol table,
165 but which would parse as a valid number in the current input radix.
166 E.g. "c" when input_radix==16. Depending on the parse, it will be
2e6edad1 167 turned into a name or into a number. */
3d6b6a90 168
2e6edad1 169%token <ssym> NAME_OR_INT
3d6b6a90 170
8050a57b 171%token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
4c53d9ca 172%token TEMPLATE
3d6b6a90
JG
173%token ERROR
174
175/* Special type cases, put in to allow the parser to distinguish different
176 legal basetypes. */
a252e715 177%token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD
3d6b6a90
JG
178%token <lval> LAST REGNAME
179
180%token <ivar> VARIABLE
181
182%token <opcode> ASSIGN_MODIFY
183
184/* C++ */
185%token THIS
186
187%left ','
188%left ABOVE_COMMA
189%right '=' ASSIGN_MODIFY
190%right '?'
088c3a0b
JG
191%left OROR
192%left ANDAND
3d6b6a90
JG
193%left '|'
194%left '^'
195%left '&'
196%left EQUAL NOTEQUAL
197%left '<' '>' LEQ GEQ
198%left LSH RSH
199%left '@'
200%left '+' '-'
201%left '*' '/' '%'
202%right UNARY INCREMENT DECREMENT
203%right ARROW '.' '[' '('
204%token <ssym> BLOCKNAME
205%type <bval> block
206%left COLONCOLON
36ce1b64 207
368c8614
MT
208\f
209%%
210
3d6b6a90
JG
211start : exp1
212 | type_exp
213 ;
214
215type_exp: type
216 { write_exp_elt_opcode(OP_TYPE);
217 write_exp_elt_type($1);
218 write_exp_elt_opcode(OP_TYPE);}
219 ;
220
221/* Expressions, including the comma operator. */
222exp1 : exp
223 | exp1 ',' exp
224 { write_exp_elt_opcode (BINOP_COMMA); }
225 ;
226
227/* Expressions, not including the comma operator. */
228exp : '*' exp %prec UNARY
229 { write_exp_elt_opcode (UNOP_IND); }
230
231exp : '&' exp %prec UNARY
232 { write_exp_elt_opcode (UNOP_ADDR); }
233
234exp : '-' exp %prec UNARY
235 { write_exp_elt_opcode (UNOP_NEG); }
236 ;
237
238exp : '!' exp %prec UNARY
e58de8a2 239 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
3d6b6a90
JG
240 ;
241
242exp : '~' exp %prec UNARY
e58de8a2 243 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
3d6b6a90
JG
244 ;
245
246exp : INCREMENT exp %prec UNARY
247 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
248 ;
249
250exp : DECREMENT exp %prec UNARY
251 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
252 ;
253
254exp : exp INCREMENT %prec UNARY
255 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
256 ;
257
258exp : exp DECREMENT %prec UNARY
259 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
260 ;
261
262exp : SIZEOF exp %prec UNARY
263 { write_exp_elt_opcode (UNOP_SIZEOF); }
264 ;
265
266exp : exp ARROW name
267 { write_exp_elt_opcode (STRUCTOP_PTR);
268 write_exp_string ($3);
269 write_exp_elt_opcode (STRUCTOP_PTR); }
270 ;
271
2640f7e1
JG
272exp : exp ARROW qualified_name
273 { /* exp->type::name becomes exp->*(&type::name) */
274 /* Note: this doesn't work if name is a
275 static member! FIXME */
276 write_exp_elt_opcode (UNOP_ADDR);
277 write_exp_elt_opcode (STRUCTOP_MPTR); }
01be6913 278 ;
3d6b6a90
JG
279exp : exp ARROW '*' exp
280 { write_exp_elt_opcode (STRUCTOP_MPTR); }
281 ;
282
283exp : exp '.' name
284 { write_exp_elt_opcode (STRUCTOP_STRUCT);
285 write_exp_string ($3);
286 write_exp_elt_opcode (STRUCTOP_STRUCT); }
287 ;
288
2640f7e1
JG
289exp : exp '.' qualified_name
290 { /* exp.type::name becomes exp.*(&type::name) */
291 /* Note: this doesn't work if name is a
292 static member! FIXME */
293 write_exp_elt_opcode (UNOP_ADDR);
294 write_exp_elt_opcode (STRUCTOP_MEMBER); }
01be6913
PB
295 ;
296
3d6b6a90
JG
297exp : exp '.' '*' exp
298 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
299 ;
300
301exp : exp '[' exp1 ']'
302 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
303 ;
304
305exp : exp '('
306 /* This is to save the value of arglist_len
307 being accumulated by an outer function call. */
308 { start_arglist (); }
309 arglist ')' %prec ARROW
310 { write_exp_elt_opcode (OP_FUNCALL);
311 write_exp_elt_longcst ((LONGEST) end_arglist ());
312 write_exp_elt_opcode (OP_FUNCALL); }
313 ;
314
9da75ad3
FF
315lcurly : '{'
316 { start_arglist (); }
317 ;
318
3d6b6a90
JG
319arglist :
320 ;
321
322arglist : exp
323 { arglist_len = 1; }
324 ;
325
326arglist : arglist ',' exp %prec ABOVE_COMMA
327 { arglist_len++; }
328 ;
329
9da75ad3
FF
330rcurly : '}'
331 { $$ = end_arglist () - 1; }
332 ;
333exp : lcurly arglist rcurly %prec ARROW
ec16f701
FF
334 { write_exp_elt_opcode (OP_ARRAY);
335 write_exp_elt_longcst ((LONGEST) 0);
9da75ad3 336 write_exp_elt_longcst ((LONGEST) $3);
ec16f701
FF
337 write_exp_elt_opcode (OP_ARRAY); }
338 ;
339
9da75ad3 340exp : lcurly type rcurly exp %prec UNARY
3d6b6a90
JG
341 { write_exp_elt_opcode (UNOP_MEMVAL);
342 write_exp_elt_type ($2);
343 write_exp_elt_opcode (UNOP_MEMVAL); }
344 ;
345
346exp : '(' type ')' exp %prec UNARY
347 { write_exp_elt_opcode (UNOP_CAST);
348 write_exp_elt_type ($2);
349 write_exp_elt_opcode (UNOP_CAST); }
350 ;
351
352exp : '(' exp1 ')'
353 { }
354 ;
355
356/* Binary operators in order of decreasing precedence. */
357
358exp : exp '@' exp
359 { write_exp_elt_opcode (BINOP_REPEAT); }
360 ;
361
362exp : exp '*' exp
363 { write_exp_elt_opcode (BINOP_MUL); }
364 ;
365
366exp : exp '/' exp
367 { write_exp_elt_opcode (BINOP_DIV); }
368 ;
369
370exp : exp '%' exp
371 { write_exp_elt_opcode (BINOP_REM); }
372 ;
373
374exp : exp '+' exp
375 { write_exp_elt_opcode (BINOP_ADD); }
376 ;
377
378exp : exp '-' exp
379 { write_exp_elt_opcode (BINOP_SUB); }
380 ;
381
382exp : exp LSH exp
383 { write_exp_elt_opcode (BINOP_LSH); }
384 ;
385
386exp : exp RSH exp
387 { write_exp_elt_opcode (BINOP_RSH); }
388 ;
389
390exp : exp EQUAL exp
391 { write_exp_elt_opcode (BINOP_EQUAL); }
392 ;
393
394exp : exp NOTEQUAL exp
395 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
396 ;
397
398exp : exp LEQ exp
399 { write_exp_elt_opcode (BINOP_LEQ); }
400 ;
401
402exp : exp GEQ exp
403 { write_exp_elt_opcode (BINOP_GEQ); }
404 ;
405
406exp : exp '<' exp
407 { write_exp_elt_opcode (BINOP_LESS); }
408 ;
409
410exp : exp '>' exp
411 { write_exp_elt_opcode (BINOP_GTR); }
412 ;
413
414exp : exp '&' exp
e58de8a2 415 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
3d6b6a90
JG
416 ;
417
418exp : exp '^' exp
e58de8a2 419 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
3d6b6a90
JG
420 ;
421
422exp : exp '|' exp
e58de8a2 423 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
3d6b6a90
JG
424 ;
425
088c3a0b 426exp : exp ANDAND exp
e58de8a2 427 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
3d6b6a90
JG
428 ;
429
088c3a0b 430exp : exp OROR exp
e58de8a2 431 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
3d6b6a90
JG
432 ;
433
434exp : exp '?' exp ':' exp %prec '?'
435 { write_exp_elt_opcode (TERNOP_COND); }
436 ;
437
438exp : exp '=' exp
439 { write_exp_elt_opcode (BINOP_ASSIGN); }
440 ;
441
442exp : exp ASSIGN_MODIFY exp
443 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
444 write_exp_elt_opcode ($2);
445 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
446 ;
447
448exp : INT
449 { write_exp_elt_opcode (OP_LONG);
2e6edad1
SC
450 write_exp_elt_type ($1.type);
451 write_exp_elt_longcst ((LONGEST)($1.val));
3d6b6a90
JG
452 write_exp_elt_opcode (OP_LONG); }
453 ;
454
455exp : NAME_OR_INT
456 { YYSTYPE val;
457 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
458 write_exp_elt_opcode (OP_LONG);
2e6edad1
SC
459 write_exp_elt_type (val.typed_val.type);
460 write_exp_elt_longcst ((LONGEST)val.typed_val.val);
3d6b6a90
JG
461 write_exp_elt_opcode (OP_LONG);
462 }
463 ;
464
3d6b6a90
JG
465
466exp : FLOAT
467 { write_exp_elt_opcode (OP_DOUBLE);
468 write_exp_elt_type (builtin_type_double);
469 write_exp_elt_dblcst ($1);
470 write_exp_elt_opcode (OP_DOUBLE); }
471 ;
472
473exp : variable
474 ;
475
476exp : LAST
477 { write_exp_elt_opcode (OP_LAST);
478 write_exp_elt_longcst ((LONGEST) $1);
479 write_exp_elt_opcode (OP_LAST); }
480 ;
481
482exp : REGNAME
483 { write_exp_elt_opcode (OP_REGISTER);
484 write_exp_elt_longcst ((LONGEST) $1);
485 write_exp_elt_opcode (OP_REGISTER); }
486 ;
487
488exp : VARIABLE
489 { write_exp_elt_opcode (OP_INTERNALVAR);
490 write_exp_elt_intern ($1);
491 write_exp_elt_opcode (OP_INTERNALVAR); }
492 ;
493
494exp : SIZEOF '(' type ')' %prec UNARY
495 { write_exp_elt_opcode (OP_LONG);
496 write_exp_elt_type (builtin_type_int);
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 {
535 if ($1.sym != 0)
536 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
537 else
538 {
539 struct symtab *tem =
540 lookup_symtab (copy_name ($1.stoken));
541 if (tem)
542 $$ = BLOCKVECTOR_BLOCK
543 (BLOCKVECTOR (tem), STATIC_BLOCK);
544 else
545 error ("No file or function \"%s\".",
546 copy_name ($1.stoken));
547 }
548 }
549 ;
550
551block : block COLONCOLON name
552 { struct symbol *tem
553 = lookup_symbol (copy_name ($3), $1,
bcca9a08
FF
554 VAR_NAMESPACE, (int *) NULL,
555 (struct symtab **) NULL);
3d6b6a90
JG
556 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
557 error ("No function \"%s\" in specified context.",
558 copy_name ($3));
559 $$ = SYMBOL_BLOCK_VALUE (tem); }
560 ;
561
562variable: block COLONCOLON name
563 { struct symbol *sym;
564 sym = lookup_symbol (copy_name ($3), $1,
bcca9a08
FF
565 VAR_NAMESPACE, (int *) NULL,
566 (struct symtab **) NULL);
3d6b6a90
JG
567 if (sym == 0)
568 error ("No symbol \"%s\" in specified context.",
569 copy_name ($3));
570
571 write_exp_elt_opcode (OP_VAR_VALUE);
479fdd26
JK
572 /* block_found is set by lookup_symbol. */
573 write_exp_elt_block (block_found);
3d6b6a90
JG
574 write_exp_elt_sym (sym);
575 write_exp_elt_opcode (OP_VAR_VALUE); }
576 ;
577
2640f7e1 578qualified_name: typebase COLONCOLON name
3d6b6a90
JG
579 {
580 struct type *type = $1;
581 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
582 && TYPE_CODE (type) != TYPE_CODE_UNION)
583 error ("`%s' is not defined as an aggregate type.",
584 TYPE_NAME (type));
585
586 write_exp_elt_opcode (OP_SCOPE);
587 write_exp_elt_type (type);
2640f7e1 588 write_exp_string ($3);
3d6b6a90
JG
589 write_exp_elt_opcode (OP_SCOPE);
590 }
2640f7e1 591 | typebase COLONCOLON '~' name
3d6b6a90
JG
592 {
593 struct type *type = $1;
01be6913 594 struct stoken tmp_token;
3d6b6a90
JG
595 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
596 && TYPE_CODE (type) != TYPE_CODE_UNION)
597 error ("`%s' is not defined as an aggregate type.",
598 TYPE_NAME (type));
599
2e4964ad 600 if (!STREQ (type_name_no_tag (type), $4.ptr))
3d6b6a90 601 error ("invalid destructor `%s::~%s'",
2640f7e1 602 type_name_no_tag (type), $4.ptr);
3d6b6a90 603
2640f7e1
JG
604 tmp_token.ptr = (char*) alloca ($4.length + 2);
605 tmp_token.length = $4.length + 1;
01be6913 606 tmp_token.ptr[0] = '~';
2640f7e1 607 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
01be6913 608 tmp_token.ptr[tmp_token.length] = 0;
3d6b6a90
JG
609 write_exp_elt_opcode (OP_SCOPE);
610 write_exp_elt_type (type);
01be6913 611 write_exp_string (tmp_token);
3d6b6a90 612 write_exp_elt_opcode (OP_SCOPE);
3d6b6a90 613 }
01be6913
PB
614 ;
615
616variable: qualified_name
3d6b6a90
JG
617 | COLONCOLON name
618 {
619 char *name = copy_name ($2);
620 struct symbol *sym;
1ab3bf1b 621 struct minimal_symbol *msymbol;
3d6b6a90
JG
622
623 sym =
bcca9a08
FF
624 lookup_symbol (name, (const struct block *) NULL,
625 VAR_NAMESPACE, (int *) NULL,
626 (struct symtab **) NULL);
3d6b6a90
JG
627 if (sym)
628 {
629 write_exp_elt_opcode (OP_VAR_VALUE);
479fdd26 630 write_exp_elt_block (NULL);
3d6b6a90
JG
631 write_exp_elt_sym (sym);
632 write_exp_elt_opcode (OP_VAR_VALUE);
633 break;
634 }
3d6b6a90 635
1ab3bf1b
JG
636 msymbol = lookup_minimal_symbol (name,
637 (struct objfile *) NULL);
638 if (msymbol != NULL)
3d6b6a90 639 {
abe28b92
JK
640 write_exp_msymbol (msymbol,
641 lookup_function_type (builtin_type_int),
642 builtin_type_int);
3d6b6a90
JG
643 }
644 else
1ab3bf1b 645 if (!have_full_symbols () && !have_partial_symbols ())
3d6b6a90
JG
646 error ("No symbol table is loaded. Use the \"file\" command.");
647 else
648 error ("No symbol \"%s\" in current context.", name);
649 }
650 ;
651
652variable: name_not_typename
653 { struct symbol *sym = $1.sym;
654
655 if (sym)
656 {
443abae1 657 if (symbol_read_needs_frame (sym))
3d6b6a90 658 {
3d6b6a90
JG
659 if (innermost_block == 0 ||
660 contained_in (block_found,
661 innermost_block))
662 innermost_block = block_found;
3d6b6a90 663 }
443abae1 664
3d6b6a90 665 write_exp_elt_opcode (OP_VAR_VALUE);
479fdd26
JK
666 /* We want to use the selected frame, not
667 another more inner frame which happens to
668 be in the same block. */
669 write_exp_elt_block (NULL);
3d6b6a90
JG
670 write_exp_elt_sym (sym);
671 write_exp_elt_opcode (OP_VAR_VALUE);
672 }
673 else if ($1.is_a_field_of_this)
674 {
675 /* C++: it hangs off of `this'. Must
676 not inadvertently convert from a method call
677 to data ref. */
678 if (innermost_block == 0 ||
679 contained_in (block_found, innermost_block))
680 innermost_block = block_found;
681 write_exp_elt_opcode (OP_THIS);
682 write_exp_elt_opcode (OP_THIS);
683 write_exp_elt_opcode (STRUCTOP_PTR);
684 write_exp_string ($1.stoken);
685 write_exp_elt_opcode (STRUCTOP_PTR);
686 }
687 else
688 {
1ab3bf1b 689 struct minimal_symbol *msymbol;
3d6b6a90
JG
690 register char *arg = copy_name ($1.stoken);
691
1ab3bf1b
JG
692 msymbol = lookup_minimal_symbol (arg,
693 (struct objfile *) NULL);
694 if (msymbol != NULL)
3d6b6a90 695 {
abe28b92
JK
696 write_exp_msymbol (msymbol,
697 lookup_function_type (builtin_type_int),
698 builtin_type_int);
3d6b6a90 699 }
1ab3bf1b 700 else if (!have_full_symbols () && !have_partial_symbols ())
3d6b6a90
JG
701 error ("No symbol table is loaded. Use the \"file\" command.");
702 else
703 error ("No symbol \"%s\" in current context.",
704 copy_name ($1.stoken));
705 }
706 }
707 ;
708
709
710ptype : typebase
f843c95f 711 /* "const" and "volatile" are curently ignored. A type qualifier
adee89e8
JK
712 before the type is currently handled in the typebase rule.
713 The reason for recognizing these here (shift/reduce conflicts)
714 might be obsolete now that some pointer to member rules have
715 been deleted. */
f843c95f
JK
716 | typebase CONST_KEYWORD
717 | typebase VOLATILE_KEYWORD
3d6b6a90 718 | typebase abs_decl
f843c95f
JK
719 { $$ = follow_types ($1); }
720 | typebase CONST_KEYWORD abs_decl
721 { $$ = follow_types ($1); }
722 | typebase VOLATILE_KEYWORD abs_decl
723 { $$ = follow_types ($1); }
3d6b6a90
JG
724 ;
725
726abs_decl: '*'
727 { push_type (tp_pointer); $$ = 0; }
728 | '*' abs_decl
729 { push_type (tp_pointer); $$ = $2; }
730 | '&'
731 { push_type (tp_reference); $$ = 0; }
732 | '&' abs_decl
733 { push_type (tp_reference); $$ = $2; }
734 | direct_abs_decl
735 ;
736
737direct_abs_decl: '(' abs_decl ')'
738 { $$ = $2; }
739 | direct_abs_decl array_mod
740 {
741 push_type_int ($2);
742 push_type (tp_array);
743 }
744 | array_mod
745 {
746 push_type_int ($1);
747 push_type (tp_array);
748 $$ = 0;
749 }
f843c95f 750
3d6b6a90
JG
751 | direct_abs_decl func_mod
752 { push_type (tp_function); }
753 | func_mod
754 { push_type (tp_function); }
755 ;
756
757array_mod: '[' ']'
758 { $$ = -1; }
759 | '[' INT ']'
2e6edad1 760 { $$ = $2.val; }
3d6b6a90
JG
761 ;
762
763func_mod: '(' ')'
764 { $$ = 0; }
0e2a896c 765 | '(' nonempty_typelist ')'
be772100 766 { free ((PTR)$2); $$ = 0; }
3d6b6a90
JG
767 ;
768
adee89e8
JK
769/* We used to try to recognize more pointer to member types here, but
770 that didn't work (shift/reduce conflicts meant that these rules never
771 got executed). The problem is that
772 int (foo::bar::baz::bizzle)
773 is a function type but
774 int (foo::bar::baz::bizzle::*)
775 is a pointer to member type. Stroustrup loses again! */
776
3d6b6a90 777type : ptype
2640f7e1
JG
778 | typebase COLONCOLON '*'
779 { $$ = lookup_member_type (builtin_type_int, $1); }
3d6b6a90
JG
780 ;
781
10a297b7 782typebase /* Implements (approximately): (type-qualifier)* type-specifier */
3d6b6a90
JG
783 : TYPENAME
784 { $$ = $1.type; }
785 | INT_KEYWORD
786 { $$ = builtin_type_int; }
787 | LONG
788 { $$ = builtin_type_long; }
789 | SHORT
790 { $$ = builtin_type_short; }
791 | LONG INT_KEYWORD
792 { $$ = builtin_type_long; }
793 | UNSIGNED LONG INT_KEYWORD
794 { $$ = builtin_type_unsigned_long; }
795 | LONG LONG
796 { $$ = builtin_type_long_long; }
797 | LONG LONG INT_KEYWORD
798 { $$ = builtin_type_long_long; }
799 | UNSIGNED LONG LONG
800 { $$ = builtin_type_unsigned_long_long; }
801 | UNSIGNED LONG LONG INT_KEYWORD
802 { $$ = builtin_type_unsigned_long_long; }
803 | SHORT INT_KEYWORD
804 { $$ = builtin_type_short; }
805 | UNSIGNED SHORT INT_KEYWORD
806 { $$ = builtin_type_unsigned_short; }
807 | STRUCT name
808 { $$ = lookup_struct (copy_name ($2),
809 expression_context_block); }
8050a57b
FF
810 | CLASS name
811 { $$ = lookup_struct (copy_name ($2),
812 expression_context_block); }
3d6b6a90
JG
813 | UNION name
814 { $$ = lookup_union (copy_name ($2),
815 expression_context_block); }
816 | ENUM name
817 { $$ = lookup_enum (copy_name ($2),
818 expression_context_block); }
819 | UNSIGNED typename
820 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
821 | UNSIGNED
822 { $$ = builtin_type_unsigned_int; }
088c3a0b 823 | SIGNED_KEYWORD typename
a252e715 824 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
088c3a0b 825 | SIGNED_KEYWORD
3d6b6a90 826 { $$ = builtin_type_int; }
4c53d9ca
DHW
827 | TEMPLATE name '<' type '>'
828 { $$ = lookup_template_type(copy_name($2), $4,
829 expression_context_block);
830 }
f843c95f
JK
831 /* "const" and "volatile" are curently ignored. A type qualifier
832 after the type is handled in the ptype rule. I think these could
833 be too. */
10a297b7
PB
834 | CONST_KEYWORD typebase { $$ = $2; }
835 | VOLATILE_KEYWORD typebase { $$ = $2; }
3d6b6a90
JG
836 ;
837
838typename: TYPENAME
839 | INT_KEYWORD
840 {
841 $$.stoken.ptr = "int";
842 $$.stoken.length = 3;
843 $$.type = builtin_type_int;
844 }
845 | LONG
846 {
847 $$.stoken.ptr = "long";
848 $$.stoken.length = 4;
849 $$.type = builtin_type_long;
850 }
851 | SHORT
852 {
853 $$.stoken.ptr = "short";
854 $$.stoken.length = 5;
855 $$.type = builtin_type_short;
856 }
857 ;
858
859nonempty_typelist
860 : type
e35843d4 861 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
6c316cfd 862 $<ivec>$[0] = 1; /* Number of types in vector */
3d6b6a90
JG
863 $$[1] = $1;
864 }
865 | nonempty_typelist ',' type
6c316cfd 866 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
e35843d4 867 $$ = (struct type **) realloc ((char *) $1, len);
3d6b6a90
JG
868 $$[$<ivec>$[0]] = $3;
869 }
870 ;
871
872name : NAME { $$ = $1.stoken; }
873 | BLOCKNAME { $$ = $1.stoken; }
874 | TYPENAME { $$ = $1.stoken; }
875 | NAME_OR_INT { $$ = $1.stoken; }
3d6b6a90
JG
876 ;
877
878name_not_typename : NAME
879 | BLOCKNAME
880/* These would be useful if name_not_typename was useful, but it is just
881 a fake for "variable", so these cause reduce/reduce conflicts because
882 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
883 =exp) or just an exp. If name_not_typename was ever used in an lvalue
884 context where only a name could occur, this might be useful.
885 | NAME_OR_INT
3d6b6a90
JG
886 */
887 ;
888
889%%
890
891/* Take care of parsing a number (anything that starts with a digit).
892 Set yylval and return the token type; update lexptr.
893 LEN is the number of characters in it. */
894
895/*** Needs some error checking for the float case ***/
896
897static int
898parse_number (p, len, parsed_float, putithere)
899 register char *p;
900 register int len;
901 int parsed_float;
902 YYSTYPE *putithere;
903{
a9b32d61
JK
904 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
905 here, and we do kind of silly things like cast to unsigned. */
3d6b6a90
JG
906 register LONGEST n = 0;
907 register LONGEST prevn = 0;
a39f7739 908 unsigned LONGEST un;
a9b32d61 909
72cd0384 910 register int i = 0;
3d6b6a90
JG
911 register int c;
912 register int base = input_radix;
913 int unsigned_p = 0;
a9b32d61
JK
914
915 /* Number of "L" suffixes encountered. */
2e6edad1 916 int long_p = 0;
a9b32d61
JK
917
918 /* We have found a "L" or "U" suffix. */
919 int found_suffix = 0;
920
45364c8a 921 unsigned LONGEST high_bit;
2e6edad1
SC
922 struct type *signed_type;
923 struct type *unsigned_type;
3d6b6a90 924
3d6b6a90
JG
925 if (parsed_float)
926 {
927 /* It's a float since it contains a point or an exponent. */
928 putithere->dval = atof (p);
929 return FLOAT;
930 }
931
932 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
933 if (p[0] == '0')
934 switch (p[1])
935 {
936 case 'x':
937 case 'X':
938 if (len >= 3)
939 {
940 p += 2;
941 base = 16;
942 len -= 2;
943 }
944 break;
945
946 case 't':
947 case 'T':
948 case 'd':
949 case 'D':
950 if (len >= 3)
951 {
952 p += 2;
953 base = 10;
954 len -= 2;
955 }
956 break;
957
958 default:
959 base = 8;
960 break;
961 }
962
963 while (len-- > 0)
964 {
965 c = *p++;
966 if (c >= 'A' && c <= 'Z')
967 c += 'a' - 'A';
968 if (c != 'l' && c != 'u')
969 n *= base;
970 if (c >= '0' && c <= '9')
a9b32d61
JK
971 {
972 if (found_suffix)
973 return ERROR;
974 n += i = c - '0';
975 }
3d6b6a90
JG
976 else
977 {
978 if (base > 10 && c >= 'a' && c <= 'f')
a9b32d61
JK
979 {
980 if (found_suffix)
981 return ERROR;
982 n += i = c - 'a' + 10;
983 }
984 else if (c == 'l')
985 {
986 ++long_p;
987 found_suffix = 1;
988 }
989 else if (c == 'u')
990 {
991 unsigned_p = 1;
992 found_suffix = 1;
993 }
3d6b6a90
JG
994 else
995 return ERROR; /* Char not a digit */
996 }
997 if (i >= base)
998 return ERROR; /* Invalid digit in this base */
2e6edad1 999
2a5ec41d 1000 /* Portably test for overflow (only works for nonzero values, so make
a9b32d61
JK
1001 a second check for zero). FIXME: Can't we just make n and prevn
1002 unsigned and avoid this? */
1003 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1004 unsigned_p = 1; /* Try something unsigned */
1005
1006 /* Portably test for unsigned overflow.
1007 FIXME: This check is wrong; for example it doesn't find overflow
1008 on 0x123456789 when LONGEST is 32 bits. */
1009 if (c != 'l' && c != 'u' && n != 0)
1010 {
1011 if ((unsigned_p && (unsigned LONGEST) prevn >= (unsigned LONGEST) n))
1012 error ("Numeric constant too large.");
1013 }
1014 prevn = n;
1015 }
1016
1017 /* An integer constant is an int, a long, or a long long. An L
1018 suffix forces it to be long; an LL suffix forces it to be long
1019 long. If not forced to a larger size, it gets the first type of
1020 the above that it fits in. To figure out whether it fits, we
1021 shift it right and see whether anything remains. Note that we
1022 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1023 operation, because many compilers will warn about such a shift
1024 (which always produces a zero result). Sometimes TARGET_INT_BIT
1025 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
1026 the case where it is we just always shift the value more than
1027 once, with fewer bits each time. */
1028
a39f7739 1029 un = (unsigned LONGEST)n >> 2;
a9b32d61 1030 if (long_p == 0
a39f7739 1031 && (un >> (TARGET_INT_BIT - 2)) == 0)
a9b32d61
JK
1032 {
1033 high_bit = ((unsigned LONGEST)1) << (TARGET_INT_BIT-1);
1034
1035 /* A large decimal (not hex or octal) constant (between INT_MAX
1036 and UINT_MAX) is a long or unsigned long, according to ANSI,
1037 never an unsigned int, but this code treats it as unsigned
1038 int. This probably should be fixed. GCC gives a warning on
1039 such constants. */
1040
1041 unsigned_type = builtin_type_unsigned_int;
1042 signed_type = builtin_type_int;
1043 }
1044 else if (long_p <= 1
a39f7739 1045 && (un >> (TARGET_LONG_BIT - 2)) == 0)
a9b32d61
JK
1046 {
1047 high_bit = ((unsigned LONGEST)1) << (TARGET_LONG_BIT-1);
1048 unsigned_type = builtin_type_unsigned_long;
1049 signed_type = builtin_type_long;
1050 }
1051 else
1052 {
bb1f42d4
JK
1053 high_bit = (((unsigned LONGEST)1)
1054 << (TARGET_LONG_LONG_BIT - 32 - 1)
1055 << 16
1056 << 16);
1057 if (high_bit == 0)
1058 /* A long long does not fit in a LONGEST. */
1c95d7ab
JK
1059 high_bit =
1060 (unsigned LONGEST)1 << (sizeof (LONGEST) * HOST_CHAR_BIT - 1);
a9b32d61
JK
1061 unsigned_type = builtin_type_unsigned_long_long;
1062 signed_type = builtin_type_long_long;
3d6b6a90 1063 }
2e6edad1
SC
1064
1065 putithere->typed_val.val = n;
1066
1067 /* If the high bit of the worked out type is set then this number
1068 has to be unsigned. */
1069
1070 if (unsigned_p || (n & high_bit))
1071 {
a9b32d61 1072 putithere->typed_val.type = unsigned_type;
2e6edad1
SC
1073 }
1074 else
1075 {
a9b32d61 1076 putithere->typed_val.type = signed_type;
2e6edad1
SC
1077 }
1078
1079 return INT;
3d6b6a90
JG
1080}
1081
1082struct token
1083{
1084 char *operator;
1085 int token;
1086 enum exp_opcode opcode;
1087};
1088
a8a69e63 1089static const struct token tokentab3[] =
3d6b6a90
JG
1090 {
1091 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1092 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1093 };
1094
a8a69e63 1095static const struct token tokentab2[] =
3d6b6a90
JG
1096 {
1097 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1098 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1099 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1100 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1101 {"%=", ASSIGN_MODIFY, BINOP_REM},
e58de8a2
FF
1102 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1103 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1104 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
3d6b6a90
JG
1105 {"++", INCREMENT, BINOP_END},
1106 {"--", DECREMENT, BINOP_END},
1107 {"->", ARROW, BINOP_END},
088c3a0b
JG
1108 {"&&", ANDAND, BINOP_END},
1109 {"||", OROR, BINOP_END},
3d6b6a90
JG
1110 {"::", COLONCOLON, BINOP_END},
1111 {"<<", LSH, BINOP_END},
1112 {">>", RSH, BINOP_END},
1113 {"==", EQUAL, BINOP_END},
1114 {"!=", NOTEQUAL, BINOP_END},
1115 {"<=", LEQ, BINOP_END},
1116 {">=", GEQ, BINOP_END}
1117 };
1118
1119/* Read one token, getting characters through lexptr. */
1120
533d1dc7 1121static int
3d6b6a90
JG
1122yylex ()
1123{
bac89d6c
FF
1124 int c;
1125 int namelen;
1126 unsigned int i;
1127 char *tokstart;
1128 char *tokptr;
1129 int tempbufindex;
1130 static char *tempbuf;
1131 static int tempbufsize;
1132
3d6b6a90
JG
1133 retry:
1134
1135 tokstart = lexptr;
1136 /* See if it is a special token of length 3. */
1137 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
45fe3db4 1138 if (STREQN (tokstart, tokentab3[i].operator, 3))
3d6b6a90
JG
1139 {
1140 lexptr += 3;
1141 yylval.opcode = tokentab3[i].opcode;
1142 return tokentab3[i].token;
1143 }
1144
1145 /* See if it is a special token of length 2. */
1146 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
45fe3db4 1147 if (STREQN (tokstart, tokentab2[i].operator, 2))
3d6b6a90
JG
1148 {
1149 lexptr += 2;
1150 yylval.opcode = tokentab2[i].opcode;
1151 return tokentab2[i].token;
1152 }
1153
1154 switch (c = *tokstart)
1155 {
1156 case 0:
1157 return 0;
1158
1159 case ' ':
1160 case '\t':
1161 case '\n':
1162 lexptr++;
1163 goto retry;
1164
1165 case '\'':
d630b615
FF
1166 /* We either have a character constant ('0' or '\177' for example)
1167 or we have a quoted symbol reference ('foo(int,int)' in C++
1168 for example). */
3d6b6a90
JG
1169 lexptr++;
1170 c = *lexptr++;
1171 if (c == '\\')
1172 c = parse_escape (&lexptr);
2e6edad1
SC
1173
1174 yylval.typed_val.val = c;
1175 yylval.typed_val.type = builtin_type_char;
1176
3d6b6a90
JG
1177 c = *lexptr++;
1178 if (c != '\'')
d630b615
FF
1179 {
1180 namelen = skip_quoted (tokstart) - tokstart;
1181 if (namelen > 2)
1182 {
1183 lexptr = tokstart + namelen;
c0bca41c
JK
1184 if (lexptr[-1] != '\'')
1185 error ("Unmatched single quote.");
d630b615
FF
1186 namelen -= 2;
1187 tokstart++;
1188 goto tryname;
1189 }
1190 error ("Invalid character constant.");
1191 }
2e6edad1 1192 return INT;
3d6b6a90
JG
1193
1194 case '(':
1195 paren_depth++;
1196 lexptr++;
1197 return c;
1198
1199 case ')':
1200 if (paren_depth == 0)
1201 return 0;
1202 paren_depth--;
1203 lexptr++;
1204 return c;
1205
1206 case ',':
1207 if (comma_terminates && paren_depth == 0)
1208 return 0;
1209 lexptr++;
1210 return c;
1211
1212 case '.':
1213 /* Might be a floating point number. */
1214 if (lexptr[1] < '0' || lexptr[1] > '9')
1215 goto symbol; /* Nope, must be a symbol. */
1216 /* FALL THRU into number case. */
1217
1218 case '0':
1219 case '1':
1220 case '2':
1221 case '3':
1222 case '4':
1223 case '5':
1224 case '6':
1225 case '7':
1226 case '8':
1227 case '9':
1228 {
1229 /* It's a number. */
1230 int got_dot = 0, got_e = 0, toktype;
1231 register char *p = tokstart;
1232 int hex = input_radix > 10;
1233
1234 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1235 {
1236 p += 2;
1237 hex = 1;
1238 }
1239 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1240 {
1241 p += 2;
1242 hex = 0;
1243 }
1244
1245 for (;; ++p)
1246 {
ce13daa7
FF
1247 /* This test includes !hex because 'e' is a valid hex digit
1248 and thus does not indicate a floating point number when
1249 the radix is hex. */
3d6b6a90
JG
1250 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1251 got_dot = got_e = 1;
ce13daa7
FF
1252 /* This test does not include !hex, because a '.' always indicates
1253 a decimal floating point number regardless of the radix. */
1254 else if (!got_dot && *p == '.')
3d6b6a90
JG
1255 got_dot = 1;
1256 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1257 && (*p == '-' || *p == '+'))
1258 /* This is the sign of the exponent, not the end of the
1259 number. */
1260 continue;
1261 /* We will take any letters or digits. parse_number will
1262 complain if past the radix, or if L or U are not final. */
1263 else if ((*p < '0' || *p > '9')
1264 && ((*p < 'a' || *p > 'z')
1265 && (*p < 'A' || *p > 'Z')))
1266 break;
1267 }
1268 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1269 if (toktype == ERROR)
1270 {
1271 char *err_copy = (char *) alloca (p - tokstart + 1);
1272
4ed3a9ea 1273 memcpy (err_copy, tokstart, p - tokstart);
3d6b6a90
JG
1274 err_copy[p - tokstart] = 0;
1275 error ("Invalid number \"%s\".", err_copy);
1276 }
1277 lexptr = p;
1278 return toktype;
1279 }
1280
1281 case '+':
1282 case '-':
1283 case '*':
1284 case '/':
1285 case '%':
1286 case '|':
1287 case '&':
1288 case '^':
1289 case '~':
1290 case '!':
1291 case '@':
1292 case '<':
1293 case '>':
1294 case '[':
1295 case ']':
1296 case '?':
1297 case ':':
1298 case '=':
1299 case '{':
1300 case '}':
1301 symbol:
1302 lexptr++;
1303 return c;
1304
1305 case '"':
bac89d6c
FF
1306
1307 /* Build the gdb internal form of the input string in tempbuf,
1308 translating any standard C escape forms seen. Note that the
1309 buffer is null byte terminated *only* for the convenience of
1310 debugging gdb itself and printing the buffer contents when
1311 the buffer contains no embedded nulls. Gdb does not depend
1312 upon the buffer being null byte terminated, it uses the length
1313 string instead. This allows gdb to handle C strings (as well
1314 as strings in other languages) with embedded null bytes */
1315
1316 tokptr = ++tokstart;
1317 tempbufindex = 0;
1318
1319 do {
1320 /* Grow the static temp buffer if necessary, including allocating
1321 the first one on demand. */
1322 if (tempbufindex + 1 >= tempbufsize)
3d6b6a90 1323 {
bac89d6c
FF
1324 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1325 }
1326 switch (*tokptr)
1327 {
1328 case '\0':
1329 case '"':
1330 /* Do nothing, loop will terminate. */
1331 break;
1332 case '\\':
1333 tokptr++;
1334 c = parse_escape (&tokptr);
1335 if (c == -1)
3d6b6a90 1336 {
bac89d6c 1337 continue;
3d6b6a90 1338 }
bac89d6c
FF
1339 tempbuf[tempbufindex++] = c;
1340 break;
1341 default:
1342 tempbuf[tempbufindex++] = *tokptr++;
1343 break;
3d6b6a90 1344 }
bac89d6c
FF
1345 } while ((*tokptr != '"') && (*tokptr != '\0'));
1346 if (*tokptr++ != '"')
1347 {
1348 error ("Unterminated string in expression.");
1349 }
1350 tempbuf[tempbufindex] = '\0'; /* See note above */
1351 yylval.sval.ptr = tempbuf;
1352 yylval.sval.length = tempbufindex;
1353 lexptr = tokptr;
1354 return (STRING);
3d6b6a90
JG
1355 }
1356
1357 if (!(c == '_' || c == '$'
1358 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1359 /* We must have come across a bad character (e.g. ';'). */
1360 error ("Invalid character '%c' in expression.", c);
1361
1362 /* It's a name. See how long it is. */
1363 namelen = 0;
1364 for (c = tokstart[namelen];
1365 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1366 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1367 c = tokstart[++namelen])
1368 ;
1369
1370 /* The token "if" terminates the expression and is NOT
1371 removed from the input stream. */
1372 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1373 {
1374 return 0;
1375 }
1376
1377 lexptr += namelen;
1378
1379 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1380 and $$digits (equivalent to $<-digits> if you could type that).
1381 Make token type LAST, and put the number (the digits) in yylval. */
1382
d630b615 1383 tryname:
3d6b6a90
JG
1384 if (*tokstart == '$')
1385 {
1386 register int negate = 0;
1387 c = 1;
1388 /* Double dollar means negate the number and add -1 as well.
1389 Thus $$ alone means -1. */
1390 if (namelen >= 2 && tokstart[1] == '$')
1391 {
1392 negate = 1;
1393 c = 2;
1394 }
1395 if (c == namelen)
1396 {
1397 /* Just dollars (one or two) */
1398 yylval.lval = - negate;
1399 return LAST;
1400 }
1401 /* Is the rest of the token digits? */
1402 for (; c < namelen; c++)
1403 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1404 break;
1405 if (c == namelen)
1406 {
1407 yylval.lval = atoi (tokstart + 1 + negate);
1408 if (negate)
1409 yylval.lval = - yylval.lval;
1410 return LAST;
1411 }
1412 }
1413
1414 /* Handle tokens that refer to machine registers:
1415 $ followed by a register name. */
1416
1417 if (*tokstart == '$') {
1418 for (c = 0; c < NUM_REGS; c++)
1419 if (namelen - 1 == strlen (reg_names[c])
45fe3db4 1420 && STREQN (tokstart + 1, reg_names[c], namelen - 1))
3d6b6a90
JG
1421 {
1422 yylval.lval = c;
1423 return REGNAME;
1424 }
1425 for (c = 0; c < num_std_regs; c++)
1426 if (namelen - 1 == strlen (std_regs[c].name)
45fe3db4 1427 && STREQN (tokstart + 1, std_regs[c].name, namelen - 1))
3d6b6a90
JG
1428 {
1429 yylval.lval = std_regs[c].regnum;
1430 return REGNAME;
1431 }
1432 }
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
JG
1451 return SIZEOF;
1452 break;
1453 case 5:
866ecded 1454 if (current_language->la_language == language_cplus
45fe3db4 1455 && STREQN (tokstart, "class", 5))
8050a57b 1456 return CLASS;
45fe3db4 1457 if (STREQN (tokstart, "union", 5))
3d6b6a90 1458 return UNION;
45fe3db4 1459 if (STREQN (tokstart, "short", 5))
3d6b6a90 1460 return SHORT;
45fe3db4 1461 if (STREQN (tokstart, "const", 5))
a252e715 1462 return CONST_KEYWORD;
3d6b6a90
JG
1463 break;
1464 case 4:
45fe3db4 1465 if (STREQN (tokstart, "enum", 4))
3d6b6a90 1466 return ENUM;
45fe3db4 1467 if (STREQN (tokstart, "long", 4))
3d6b6a90 1468 return LONG;
5a4e7215 1469 if (current_language->la_language == language_cplus
45fe3db4 1470 && STREQN (tokstart, "this", 4))
3d6b6a90
JG
1471 {
1472 static const char this_name[] =
1473 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1474
1475 if (lookup_symbol (this_name, expression_context_block,
bcca9a08
FF
1476 VAR_NAMESPACE, (int *) NULL,
1477 (struct symtab **) NULL))
3d6b6a90
JG
1478 return THIS;
1479 }
1480 break;
1481 case 3:
45fe3db4 1482 if (STREQN (tokstart, "int", 3))
3d6b6a90
JG
1483 return INT_KEYWORD;
1484 break;
1485 default:
1486 break;
1487 }
1488
1489 yylval.sval.ptr = tokstart;
1490 yylval.sval.length = namelen;
1491
1492 /* Any other names starting in $ are debugger internal variables. */
1493
1494 if (*tokstart == '$')
1495 {
1496 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
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. */
b427690a 1577 char *tmp = alloca (strlen (namestart)+1);
96c68efa
JK
1578
1579 memcpy (tmp, namestart, p - namestart);
1580 tmp[p - namestart] = '\0';
1581 cur_sym = lookup_symbol (tmp, expression_context_block,
bcca9a08
FF
1582 VAR_NAMESPACE, (int *) NULL,
1583 (struct symtab **) NULL);
96c68efa
JK
1584 if (cur_sym)
1585 {
1586 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1587 {
1588 best_sym = cur_sym;
1589 lexptr = p;
1590 }
1591 else
1592 break;
1593 }
1594 else
1595 break;
1596 }
1597 else
1598 break;
1599 }
1600 else
1601 break;
1602 }
1603
1604 yylval.tsym.type = SYMBOL_TYPE (best_sym);
dfb4a508
JK
1605#else /* not 0 */
1606 yylval.tsym.type = SYMBOL_TYPE (sym);
1607#endif /* not 0 */
3d6b6a90
JG
1608 return TYPENAME;
1609 }
1610 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1611 return TYPENAME;
1612
1613 /* Input names that aren't symbols but ARE valid hex numbers,
1614 when the input radix permits them, can be names or numbers
1615 depending on the parse. Note we support radixes > 16 here. */
1616 if (!sym &&
1617 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1618 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1619 {
1620 YYSTYPE newlval; /* Its value is ignored. */
1621 hextype = parse_number (tokstart, namelen, 0, &newlval);
1622 if (hextype == INT)
1623 {
1624 yylval.ssym.sym = sym;
1625 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1626 return NAME_OR_INT;
1627 }
3d6b6a90
JG
1628 }
1629
1630 /* Any other kind of symbol */
1631 yylval.ssym.sym = sym;
1632 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1633 return NAME;
1634 }
1635}
1636
1637void
1638yyerror (msg)
1639 char *msg;
1640{
8db1a922 1641 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
3d6b6a90 1642}
This page took 0.214781 seconds and 4 git commands to generate.