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