note name change of cdtest-bar.cc
[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. */
1059 high_bit = (unsigned LONGEST)1 << sizeof (LONGEST) * HOST_CHAR_BIT - 1;
a9b32d61
JK
1060 unsigned_type = builtin_type_unsigned_long_long;
1061 signed_type = builtin_type_long_long;
3d6b6a90 1062 }
2e6edad1
SC
1063
1064 putithere->typed_val.val = n;
1065
1066 /* If the high bit of the worked out type is set then this number
1067 has to be unsigned. */
1068
1069 if (unsigned_p || (n & high_bit))
1070 {
a9b32d61 1071 putithere->typed_val.type = unsigned_type;
2e6edad1
SC
1072 }
1073 else
1074 {
a9b32d61 1075 putithere->typed_val.type = signed_type;
2e6edad1
SC
1076 }
1077
1078 return INT;
3d6b6a90
JG
1079}
1080
1081struct token
1082{
1083 char *operator;
1084 int token;
1085 enum exp_opcode opcode;
1086};
1087
a8a69e63 1088static const struct token tokentab3[] =
3d6b6a90
JG
1089 {
1090 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1091 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1092 };
1093
a8a69e63 1094static const struct token tokentab2[] =
3d6b6a90
JG
1095 {
1096 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1097 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1098 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1099 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1100 {"%=", ASSIGN_MODIFY, BINOP_REM},
e58de8a2
FF
1101 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1102 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1103 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
3d6b6a90
JG
1104 {"++", INCREMENT, BINOP_END},
1105 {"--", DECREMENT, BINOP_END},
1106 {"->", ARROW, BINOP_END},
088c3a0b
JG
1107 {"&&", ANDAND, BINOP_END},
1108 {"||", OROR, BINOP_END},
3d6b6a90
JG
1109 {"::", COLONCOLON, BINOP_END},
1110 {"<<", LSH, BINOP_END},
1111 {">>", RSH, BINOP_END},
1112 {"==", EQUAL, BINOP_END},
1113 {"!=", NOTEQUAL, BINOP_END},
1114 {"<=", LEQ, BINOP_END},
1115 {">=", GEQ, BINOP_END}
1116 };
1117
1118/* Read one token, getting characters through lexptr. */
1119
533d1dc7 1120static int
3d6b6a90
JG
1121yylex ()
1122{
bac89d6c
FF
1123 int c;
1124 int namelen;
1125 unsigned int i;
1126 char *tokstart;
1127 char *tokptr;
1128 int tempbufindex;
1129 static char *tempbuf;
1130 static int tempbufsize;
1131
3d6b6a90
JG
1132 retry:
1133
1134 tokstart = lexptr;
1135 /* See if it is a special token of length 3. */
1136 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
45fe3db4 1137 if (STREQN (tokstart, tokentab3[i].operator, 3))
3d6b6a90
JG
1138 {
1139 lexptr += 3;
1140 yylval.opcode = tokentab3[i].opcode;
1141 return tokentab3[i].token;
1142 }
1143
1144 /* See if it is a special token of length 2. */
1145 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
45fe3db4 1146 if (STREQN (tokstart, tokentab2[i].operator, 2))
3d6b6a90
JG
1147 {
1148 lexptr += 2;
1149 yylval.opcode = tokentab2[i].opcode;
1150 return tokentab2[i].token;
1151 }
1152
1153 switch (c = *tokstart)
1154 {
1155 case 0:
1156 return 0;
1157
1158 case ' ':
1159 case '\t':
1160 case '\n':
1161 lexptr++;
1162 goto retry;
1163
1164 case '\'':
d630b615
FF
1165 /* We either have a character constant ('0' or '\177' for example)
1166 or we have a quoted symbol reference ('foo(int,int)' in C++
1167 for example). */
3d6b6a90
JG
1168 lexptr++;
1169 c = *lexptr++;
1170 if (c == '\\')
1171 c = parse_escape (&lexptr);
2e6edad1
SC
1172
1173 yylval.typed_val.val = c;
1174 yylval.typed_val.type = builtin_type_char;
1175
3d6b6a90
JG
1176 c = *lexptr++;
1177 if (c != '\'')
d630b615
FF
1178 {
1179 namelen = skip_quoted (tokstart) - tokstart;
1180 if (namelen > 2)
1181 {
1182 lexptr = tokstart + namelen;
c0bca41c
JK
1183 if (lexptr[-1] != '\'')
1184 error ("Unmatched single quote.");
d630b615
FF
1185 namelen -= 2;
1186 tokstart++;
1187 goto tryname;
1188 }
1189 error ("Invalid character constant.");
1190 }
2e6edad1 1191 return INT;
3d6b6a90
JG
1192
1193 case '(':
1194 paren_depth++;
1195 lexptr++;
1196 return c;
1197
1198 case ')':
1199 if (paren_depth == 0)
1200 return 0;
1201 paren_depth--;
1202 lexptr++;
1203 return c;
1204
1205 case ',':
1206 if (comma_terminates && paren_depth == 0)
1207 return 0;
1208 lexptr++;
1209 return c;
1210
1211 case '.':
1212 /* Might be a floating point number. */
1213 if (lexptr[1] < '0' || lexptr[1] > '9')
1214 goto symbol; /* Nope, must be a symbol. */
1215 /* FALL THRU into number case. */
1216
1217 case '0':
1218 case '1':
1219 case '2':
1220 case '3':
1221 case '4':
1222 case '5':
1223 case '6':
1224 case '7':
1225 case '8':
1226 case '9':
1227 {
1228 /* It's a number. */
1229 int got_dot = 0, got_e = 0, toktype;
1230 register char *p = tokstart;
1231 int hex = input_radix > 10;
1232
1233 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1234 {
1235 p += 2;
1236 hex = 1;
1237 }
1238 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1239 {
1240 p += 2;
1241 hex = 0;
1242 }
1243
1244 for (;; ++p)
1245 {
ce13daa7
FF
1246 /* This test includes !hex because 'e' is a valid hex digit
1247 and thus does not indicate a floating point number when
1248 the radix is hex. */
3d6b6a90
JG
1249 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1250 got_dot = got_e = 1;
ce13daa7
FF
1251 /* This test does not include !hex, because a '.' always indicates
1252 a decimal floating point number regardless of the radix. */
1253 else if (!got_dot && *p == '.')
3d6b6a90
JG
1254 got_dot = 1;
1255 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1256 && (*p == '-' || *p == '+'))
1257 /* This is the sign of the exponent, not the end of the
1258 number. */
1259 continue;
1260 /* We will take any letters or digits. parse_number will
1261 complain if past the radix, or if L or U are not final. */
1262 else if ((*p < '0' || *p > '9')
1263 && ((*p < 'a' || *p > 'z')
1264 && (*p < 'A' || *p > 'Z')))
1265 break;
1266 }
1267 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1268 if (toktype == ERROR)
1269 {
1270 char *err_copy = (char *) alloca (p - tokstart + 1);
1271
4ed3a9ea 1272 memcpy (err_copy, tokstart, p - tokstart);
3d6b6a90
JG
1273 err_copy[p - tokstart] = 0;
1274 error ("Invalid number \"%s\".", err_copy);
1275 }
1276 lexptr = p;
1277 return toktype;
1278 }
1279
1280 case '+':
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 symbol:
1301 lexptr++;
1302 return c;
1303
1304 case '"':
bac89d6c
FF
1305
1306 /* Build the gdb internal form of the input string in tempbuf,
1307 translating any standard C escape forms seen. Note that the
1308 buffer is null byte terminated *only* for the convenience of
1309 debugging gdb itself and printing the buffer contents when
1310 the buffer contains no embedded nulls. Gdb does not depend
1311 upon the buffer being null byte terminated, it uses the length
1312 string instead. This allows gdb to handle C strings (as well
1313 as strings in other languages) with embedded null bytes */
1314
1315 tokptr = ++tokstart;
1316 tempbufindex = 0;
1317
1318 do {
1319 /* Grow the static temp buffer if necessary, including allocating
1320 the first one on demand. */
1321 if (tempbufindex + 1 >= tempbufsize)
3d6b6a90 1322 {
bac89d6c
FF
1323 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1324 }
1325 switch (*tokptr)
1326 {
1327 case '\0':
1328 case '"':
1329 /* Do nothing, loop will terminate. */
1330 break;
1331 case '\\':
1332 tokptr++;
1333 c = parse_escape (&tokptr);
1334 if (c == -1)
3d6b6a90 1335 {
bac89d6c 1336 continue;
3d6b6a90 1337 }
bac89d6c
FF
1338 tempbuf[tempbufindex++] = c;
1339 break;
1340 default:
1341 tempbuf[tempbufindex++] = *tokptr++;
1342 break;
3d6b6a90 1343 }
bac89d6c
FF
1344 } while ((*tokptr != '"') && (*tokptr != '\0'));
1345 if (*tokptr++ != '"')
1346 {
1347 error ("Unterminated string in expression.");
1348 }
1349 tempbuf[tempbufindex] = '\0'; /* See note above */
1350 yylval.sval.ptr = tempbuf;
1351 yylval.sval.length = tempbufindex;
1352 lexptr = tokptr;
1353 return (STRING);
3d6b6a90
JG
1354 }
1355
1356 if (!(c == '_' || c == '$'
1357 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1358 /* We must have come across a bad character (e.g. ';'). */
1359 error ("Invalid character '%c' in expression.", c);
1360
1361 /* It's a name. See how long it is. */
1362 namelen = 0;
1363 for (c = tokstart[namelen];
1364 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1365 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1366 c = tokstart[++namelen])
1367 ;
1368
1369 /* The token "if" terminates the expression and is NOT
1370 removed from the input stream. */
1371 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1372 {
1373 return 0;
1374 }
1375
1376 lexptr += namelen;
1377
1378 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1379 and $$digits (equivalent to $<-digits> if you could type that).
1380 Make token type LAST, and put the number (the digits) in yylval. */
1381
d630b615 1382 tryname:
3d6b6a90
JG
1383 if (*tokstart == '$')
1384 {
1385 register int negate = 0;
1386 c = 1;
1387 /* Double dollar means negate the number and add -1 as well.
1388 Thus $$ alone means -1. */
1389 if (namelen >= 2 && tokstart[1] == '$')
1390 {
1391 negate = 1;
1392 c = 2;
1393 }
1394 if (c == namelen)
1395 {
1396 /* Just dollars (one or two) */
1397 yylval.lval = - negate;
1398 return LAST;
1399 }
1400 /* Is the rest of the token digits? */
1401 for (; c < namelen; c++)
1402 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1403 break;
1404 if (c == namelen)
1405 {
1406 yylval.lval = atoi (tokstart + 1 + negate);
1407 if (negate)
1408 yylval.lval = - yylval.lval;
1409 return LAST;
1410 }
1411 }
1412
1413 /* Handle tokens that refer to machine registers:
1414 $ followed by a register name. */
1415
1416 if (*tokstart == '$') {
1417 for (c = 0; c < NUM_REGS; c++)
1418 if (namelen - 1 == strlen (reg_names[c])
45fe3db4 1419 && STREQN (tokstart + 1, reg_names[c], namelen - 1))
3d6b6a90
JG
1420 {
1421 yylval.lval = c;
1422 return REGNAME;
1423 }
1424 for (c = 0; c < num_std_regs; c++)
1425 if (namelen - 1 == strlen (std_regs[c].name)
45fe3db4 1426 && STREQN (tokstart + 1, std_regs[c].name, namelen - 1))
3d6b6a90
JG
1427 {
1428 yylval.lval = std_regs[c].regnum;
1429 return REGNAME;
1430 }
1431 }
1432 /* Catch specific keywords. Should be done with a data structure. */
1433 switch (namelen)
1434 {
1435 case 8:
45fe3db4 1436 if (STREQN (tokstart, "unsigned", 8))
3d6b6a90 1437 return UNSIGNED;
5a4e7215 1438 if (current_language->la_language == language_cplus
45fe3db4 1439 && STREQN (tokstart, "template", 8))
4c53d9ca 1440 return TEMPLATE;
45fe3db4 1441 if (STREQN (tokstart, "volatile", 8))
a252e715 1442 return VOLATILE_KEYWORD;
3d6b6a90
JG
1443 break;
1444 case 6:
45fe3db4 1445 if (STREQN (tokstart, "struct", 6))
3d6b6a90 1446 return STRUCT;
45fe3db4 1447 if (STREQN (tokstart, "signed", 6))
088c3a0b 1448 return SIGNED_KEYWORD;
45fe3db4 1449 if (STREQN (tokstart, "sizeof", 6))
3d6b6a90
JG
1450 return SIZEOF;
1451 break;
1452 case 5:
866ecded 1453 if (current_language->la_language == language_cplus
45fe3db4 1454 && STREQN (tokstart, "class", 5))
8050a57b 1455 return CLASS;
45fe3db4 1456 if (STREQN (tokstart, "union", 5))
3d6b6a90 1457 return UNION;
45fe3db4 1458 if (STREQN (tokstart, "short", 5))
3d6b6a90 1459 return SHORT;
45fe3db4 1460 if (STREQN (tokstart, "const", 5))
a252e715 1461 return CONST_KEYWORD;
3d6b6a90
JG
1462 break;
1463 case 4:
45fe3db4 1464 if (STREQN (tokstart, "enum", 4))
3d6b6a90 1465 return ENUM;
45fe3db4 1466 if (STREQN (tokstart, "long", 4))
3d6b6a90 1467 return LONG;
5a4e7215 1468 if (current_language->la_language == language_cplus
45fe3db4 1469 && STREQN (tokstart, "this", 4))
3d6b6a90
JG
1470 {
1471 static const char this_name[] =
1472 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1473
1474 if (lookup_symbol (this_name, expression_context_block,
bcca9a08
FF
1475 VAR_NAMESPACE, (int *) NULL,
1476 (struct symtab **) NULL))
3d6b6a90
JG
1477 return THIS;
1478 }
1479 break;
1480 case 3:
45fe3db4 1481 if (STREQN (tokstart, "int", 3))
3d6b6a90
JG
1482 return INT_KEYWORD;
1483 break;
1484 default:
1485 break;
1486 }
1487
1488 yylval.sval.ptr = tokstart;
1489 yylval.sval.length = namelen;
1490
1491 /* Any other names starting in $ are debugger internal variables. */
1492
1493 if (*tokstart == '$')
1494 {
1495 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1496 return VARIABLE;
1497 }
1498
1499 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1500 functions or symtabs. If this is not so, then ...
1501 Use token-type TYPENAME for symbols that happen to be defined
1502 currently as names of types; NAME for other symbols.
1503 The caller is not constrained to care about the distinction. */
1504 {
1505 char *tmp = copy_name (yylval.sval);
1506 struct symbol *sym;
1507 int is_a_field_of_this = 0;
1508 int hextype;
1509
1510 sym = lookup_symbol (tmp, expression_context_block,
545af6ce
PB
1511 VAR_NAMESPACE,
1512 current_language->la_language == language_cplus
bcca9a08
FF
1513 ? &is_a_field_of_this : (int *) NULL,
1514 (struct symtab **) NULL);
963ee102
JK
1515 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1516 no psymtabs (coff, xcoff, or some future change to blow away the
1517 psymtabs once once symbols are read). */
3d6b6a90 1518 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
963ee102 1519 lookup_symtab (tmp))
3d6b6a90
JG
1520 {
1521 yylval.ssym.sym = sym;
1522 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1523 return BLOCKNAME;
1524 }
1525 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1526 {
f28c6e38
JK
1527#if 1
1528 /* Despite the following flaw, we need to keep this code enabled.
1529 Because we can get called from check_stub_method, if we don't
1530 handle nested types then it screws many operations in any
1531 program which uses nested types. */
dfb4a508
JK
1532 /* In "A::x", if x is a member function of A and there happens
1533 to be a type (nested or not, since the stabs don't make that
1534 distinction) named x, then this code incorrectly thinks we
1535 are dealing with nested types rather than a member function. */
1536
96c68efa
JK
1537 char *p;
1538 char *namestart;
1539 struct symbol *best_sym;
1540
1541 /* Look ahead to detect nested types. This probably should be
1542 done in the grammar, but trying seemed to introduce a lot
1543 of shift/reduce and reduce/reduce conflicts. It's possible
1544 that it could be done, though. Or perhaps a non-grammar, but
1545 less ad hoc, approach would work well. */
1546
1547 /* Since we do not currently have any way of distinguishing
1548 a nested type from a non-nested one (the stabs don't tell
1549 us whether a type is nested), we just ignore the
1550 containing type. */
1551
1552 p = lexptr;
1553 best_sym = sym;
1554 while (1)
1555 {
1556 /* Skip whitespace. */
1557 while (*p == ' ' || *p == '\t' || *p == '\n')
1558 ++p;
1559 if (*p == ':' && p[1] == ':')
1560 {
1561 /* Skip the `::'. */
1562 p += 2;
1563 /* Skip whitespace. */
1564 while (*p == ' ' || *p == '\t' || *p == '\n')
1565 ++p;
1566 namestart = p;
1567 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1568 || (*p >= 'a' && *p <= 'z')
1569 || (*p >= 'A' && *p <= 'Z'))
1570 ++p;
1571 if (p != namestart)
1572 {
1573 struct symbol *cur_sym;
1574 /* As big as the whole rest of the expression, which is
1575 at least big enough. */
b427690a 1576 char *tmp = alloca (strlen (namestart)+1);
96c68efa
JK
1577
1578 memcpy (tmp, namestart, p - namestart);
1579 tmp[p - namestart] = '\0';
1580 cur_sym = lookup_symbol (tmp, expression_context_block,
bcca9a08
FF
1581 VAR_NAMESPACE, (int *) NULL,
1582 (struct symtab **) NULL);
96c68efa
JK
1583 if (cur_sym)
1584 {
1585 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1586 {
1587 best_sym = cur_sym;
1588 lexptr = p;
1589 }
1590 else
1591 break;
1592 }
1593 else
1594 break;
1595 }
1596 else
1597 break;
1598 }
1599 else
1600 break;
1601 }
1602
1603 yylval.tsym.type = SYMBOL_TYPE (best_sym);
dfb4a508
JK
1604#else /* not 0 */
1605 yylval.tsym.type = SYMBOL_TYPE (sym);
1606#endif /* not 0 */
3d6b6a90
JG
1607 return TYPENAME;
1608 }
1609 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1610 return TYPENAME;
1611
1612 /* Input names that aren't symbols but ARE valid hex numbers,
1613 when the input radix permits them, can be names or numbers
1614 depending on the parse. Note we support radixes > 16 here. */
1615 if (!sym &&
1616 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1617 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1618 {
1619 YYSTYPE newlval; /* Its value is ignored. */
1620 hextype = parse_number (tokstart, namelen, 0, &newlval);
1621 if (hextype == INT)
1622 {
1623 yylval.ssym.sym = sym;
1624 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1625 return NAME_OR_INT;
1626 }
3d6b6a90
JG
1627 }
1628
1629 /* Any other kind of symbol */
1630 yylval.ssym.sym = sym;
1631 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1632 return NAME;
1633 }
1634}
1635
1636void
1637yyerror (msg)
1638 char *msg;
1639{
d671e293 1640 error (msg ? msg : "Invalid syntax in expression.");
3d6b6a90 1641}
This page took 0.21114 seconds and 4 git commands to generate.