1 /* YACC parser for Pascal expressions, for GDB.
2 Copyright (C) 2000, 2006, 2007, 2008 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 /* This file is derived from c-exp.y */
23 /* Parse a Pascal expression from text in a string,
24 and return the result as a struct expression pointer.
25 That structure contains arithmetic operations in reverse polish,
26 with constants represented by operations that are followed by special data.
27 See expression.h for the details of the format.
28 What is important here is that it can be built up sequentially
29 during the process of parsing; the lower levels of the tree always
30 come first in the result.
32 Note that malloc's and realloc's in this file are transformed to
33 xmalloc and xrealloc respectively by the same sed command in the
34 makefile that remaps any other malloc/realloc inserted by the parser
35 generator. Doing this with #defines and trying to control the interaction
36 with include files (<malloc.h> and <stdlib.h> for example) just became
37 too messy, particularly when such includes can be inserted at random
38 times by the parser generator. */
40 /* Known bugs or limitations:
41 - pascal string operations are not supported at all.
42 - there are some problems with boolean types.
43 - Pascal type hexadecimal constants are not supported
44 because they conflict with the internal variables format.
45 Probably also lots of other problems, less well defined PM */
49 #include "gdb_string.h"
51 #include "expression.h"
53 #include "parser-defs.h"
56 #include "bfd.h" /* Required by objfiles.h. */
57 #include "symfile.h" /* Required by objfiles.h. */
58 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
61 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
62 as well as gratuitiously global symbol names, so we can have multiple
63 yacc generated parsers in gdb. Note that these are only the variables
64 produced by yacc. If other parser generators (bison, byacc, etc) produce
65 additional global names that conflict at link time, then those parser
66 generators need to be fixed instead of adding those names to this list. */
68 #define yymaxdepth pascal_maxdepth
69 #define yyparse pascal_parse
70 #define yylex pascal_lex
71 #define yyerror pascal_error
72 #define yylval pascal_lval
73 #define yychar pascal_char
74 #define yydebug pascal_debug
75 #define yypact pascal_pact
76 #define yyr1 pascal_r1
77 #define yyr2 pascal_r2
78 #define yydef pascal_def
79 #define yychk pascal_chk
80 #define yypgo pascal_pgo
81 #define yyact pascal_act
82 #define yyexca pascal_exca
83 #define yyerrflag pascal_errflag
84 #define yynerrs pascal_nerrs
85 #define yyps pascal_ps
86 #define yypv pascal_pv
88 #define yy_yys pascal_yys
89 #define yystate pascal_state
90 #define yytmp pascal_tmp
92 #define yy_yyv pascal_yyv
93 #define yyval pascal_val
94 #define yylloc pascal_lloc
95 #define yyreds pascal_reds /* With YYDEBUG defined */
96 #define yytoks pascal_toks /* With YYDEBUG defined */
97 #define yyname pascal_name /* With YYDEBUG defined */
98 #define yyrule pascal_rule /* With YYDEBUG defined */
99 #define yylhs pascal_yylhs
100 #define yylen pascal_yylen
101 #define yydefred pascal_yydefred
102 #define yydgoto pascal_yydgoto
103 #define yysindex pascal_yysindex
104 #define yyrindex pascal_yyrindex
105 #define yygindex pascal_yygindex
106 #define yytable pascal_yytable
107 #define yycheck pascal_yycheck
110 #define YYDEBUG 1 /* Default to yydebug support */
113 #define YYFPRINTF parser_fprintf
117 static int yylex (void);
122 static char * uptok
(char *, int);
125 /* Although the yacc "value" of an expression is not used,
126 since the result is stored in the structure being created,
127 other node types do have values. */
144 struct symtoken ssym
;
147 enum exp_opcode opcode
;
148 struct internalvar
*ivar
;
155 /* YYSTYPE gets defined by %union */
157 parse_number
(char *, int, int, YYSTYPE *);
159 static struct type
*current_type
;
160 static int leftdiv_is_integer
;
161 static void push_current_type
(void);
162 static void pop_current_type
(void);
163 static int search_field
;
166 %type
<voidval
> exp exp1 type_exp start normal_start variable qualified_name
167 %type
<tval
> type typebase
168 /* %type <bval> block */
170 /* Fancy type parsing. */
173 %token
<typed_val_int
> INT
174 %token
<typed_val_float
> FLOAT
176 /* Both NAME and TYPENAME tokens represent symbols in the input,
177 and both convey their data as strings.
178 But a TYPENAME is a string that happens to be defined as a typedef
179 or builtin type name (such as int or char)
180 and a NAME is any other symbol.
181 Contexts where this distinction is not important can use the
182 nonterminal "name", which matches either NAME or TYPENAME. */
185 %token
<sval
> FIELDNAME
186 %token
<ssym
> NAME
/* BLOCKNAME defined below to give it higher precedence. */
187 %token
<tsym
> TYPENAME
189 %type
<ssym
> name_not_typename
191 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
192 but which would parse as a valid number in the current input radix.
193 E.g. "c" when input_radix==16. Depending on the parse, it will be
194 turned into a name or into a number. */
196 %token
<ssym
> NAME_OR_INT
198 %token STRUCT CLASS SIZEOF COLONCOLON
201 /* Special type cases, put in to allow the parser to distinguish different
204 %token
<voidval
> VARIABLE
209 %token
<lval
> TRUEKEYWORD FALSEKEYWORD
219 %left
'<' '>' LEQ GEQ
220 %left LSH RSH DIV MOD
224 %right UNARY INCREMENT DECREMENT
225 %right ARROW
'.' '[' '('
227 %token
<ssym
> BLOCKNAME
234 start
: { current_type
= NULL
;
236 leftdiv_is_integer
= 0;
247 { write_exp_elt_opcode
(OP_TYPE
);
248 write_exp_elt_type
($1);
249 write_exp_elt_opcode
(OP_TYPE
);
250 current_type
= $1; } ;
252 /* Expressions, including the comma operator. */
255 { write_exp_elt_opcode
(BINOP_COMMA
); }
258 /* Expressions, not including the comma operator. */
259 exp
: exp
'^' %prec UNARY
260 { write_exp_elt_opcode
(UNOP_IND
);
262 current_type
= TYPE_TARGET_TYPE
(current_type
); }
265 exp
: '@' exp %prec UNARY
266 { write_exp_elt_opcode
(UNOP_ADDR
);
268 current_type
= TYPE_POINTER_TYPE
(current_type
); }
271 exp
: '-' exp %prec UNARY
272 { write_exp_elt_opcode
(UNOP_NEG
); }
275 exp
: NOT exp %prec UNARY
276 { write_exp_elt_opcode
(UNOP_LOGICAL_NOT
); }
279 exp
: INCREMENT
'(' exp
')' %prec UNARY
280 { write_exp_elt_opcode
(UNOP_PREINCREMENT
); }
283 exp
: DECREMENT
'(' exp
')' %prec UNARY
284 { write_exp_elt_opcode
(UNOP_PREDECREMENT
); }
287 exp
: exp
'.' { search_field
= 1; }
290 { write_exp_elt_opcode
(STRUCTOP_STRUCT
);
291 write_exp_string
($4);
292 write_exp_elt_opcode
(STRUCTOP_STRUCT
);
295 { while
(TYPE_CODE
(current_type
) == TYPE_CODE_PTR
)
296 current_type
= TYPE_TARGET_TYPE
(current_type
);
297 current_type
= lookup_struct_elt_type
(
298 current_type
, $4.ptr
, 0); };
301 /* We need to save the current_type value */
304 arrayfieldindex
= is_pascal_string_type
(
305 current_type
, NULL
, NULL
,
306 NULL
, NULL
, &arrayname
);
309 struct stoken stringsval
;
310 stringsval.ptr
= alloca
(strlen
(arrayname
) + 1);
311 stringsval.length
= strlen
(arrayname
);
312 strcpy
(stringsval.ptr
, arrayname
);
313 current_type
= TYPE_FIELD_TYPE
(current_type
,
314 arrayfieldindex
- 1);
315 write_exp_elt_opcode
(STRUCTOP_STRUCT
);
316 write_exp_string
(stringsval
);
317 write_exp_elt_opcode
(STRUCTOP_STRUCT
);
319 push_current_type
(); }
321 { pop_current_type
();
322 write_exp_elt_opcode
(BINOP_SUBSCRIPT
);
324 current_type
= TYPE_TARGET_TYPE
(current_type
); }
328 /* This is to save the value of arglist_len
329 being accumulated by an outer function call. */
330 { push_current_type
();
332 arglist
')' %prec ARROW
333 { write_exp_elt_opcode
(OP_FUNCALL
);
334 write_exp_elt_longcst
((LONGEST
) end_arglist
());
335 write_exp_elt_opcode
(OP_FUNCALL
);
338 current_type
= TYPE_TARGET_TYPE
(current_type
);
345 | arglist
',' exp %prec ABOVE_COMMA
349 exp
: type
'(' exp
')' %prec UNARY
352 /* Allow automatic dereference of classes. */
353 if
((TYPE_CODE
(current_type
) == TYPE_CODE_PTR
)
354 && (TYPE_CODE
(TYPE_TARGET_TYPE
(current_type
)) == TYPE_CODE_CLASS
)
355 && (TYPE_CODE
($1) == TYPE_CODE_CLASS
))
356 write_exp_elt_opcode
(UNOP_IND
);
358 write_exp_elt_opcode
(UNOP_CAST
);
359 write_exp_elt_type
($1);
360 write_exp_elt_opcode
(UNOP_CAST
);
368 /* Binary operators in order of decreasing precedence. */
371 { write_exp_elt_opcode
(BINOP_MUL
); }
375 if
(current_type
&& is_integral_type
(current_type
))
376 leftdiv_is_integer
= 1;
380 if
(leftdiv_is_integer
&& current_type
381 && is_integral_type
(current_type
))
383 write_exp_elt_opcode
(UNOP_CAST
);
384 write_exp_elt_type
(builtin_type_long_double
);
385 current_type
= builtin_type_long_double
;
386 write_exp_elt_opcode
(UNOP_CAST
);
387 leftdiv_is_integer
= 0;
390 write_exp_elt_opcode
(BINOP_DIV
);
395 { write_exp_elt_opcode
(BINOP_INTDIV
); }
399 { write_exp_elt_opcode
(BINOP_REM
); }
403 { write_exp_elt_opcode
(BINOP_ADD
); }
407 { write_exp_elt_opcode
(BINOP_SUB
); }
411 { write_exp_elt_opcode
(BINOP_LSH
); }
415 { write_exp_elt_opcode
(BINOP_RSH
); }
419 { write_exp_elt_opcode
(BINOP_EQUAL
);
420 current_type
= builtin_type_bool
;
424 exp
: exp NOTEQUAL exp
425 { write_exp_elt_opcode
(BINOP_NOTEQUAL
);
426 current_type
= builtin_type_bool
;
431 { write_exp_elt_opcode
(BINOP_LEQ
);
432 current_type
= builtin_type_bool
;
437 { write_exp_elt_opcode
(BINOP_GEQ
);
438 current_type
= builtin_type_bool
;
443 { write_exp_elt_opcode
(BINOP_LESS
);
444 current_type
= builtin_type_bool
;
449 { write_exp_elt_opcode
(BINOP_GTR
);
450 current_type
= builtin_type_bool
;
455 { write_exp_elt_opcode
(BINOP_BITWISE_AND
); }
459 { write_exp_elt_opcode
(BINOP_BITWISE_XOR
); }
463 { write_exp_elt_opcode
(BINOP_BITWISE_IOR
); }
467 { write_exp_elt_opcode
(BINOP_ASSIGN
); }
471 { write_exp_elt_opcode
(OP_BOOL
);
472 write_exp_elt_longcst
((LONGEST
) $1);
473 current_type
= builtin_type_bool
;
474 write_exp_elt_opcode
(OP_BOOL
); }
478 { write_exp_elt_opcode
(OP_BOOL
);
479 write_exp_elt_longcst
((LONGEST
) $1);
480 current_type
= builtin_type_bool
;
481 write_exp_elt_opcode
(OP_BOOL
); }
485 { write_exp_elt_opcode
(OP_LONG
);
486 write_exp_elt_type
($1.type
);
487 current_type
= $1.type
;
488 write_exp_elt_longcst
((LONGEST
)($1.val
));
489 write_exp_elt_opcode
(OP_LONG
); }
494 parse_number
($1.stoken.ptr
, $1.stoken.length
, 0, &val
);
495 write_exp_elt_opcode
(OP_LONG
);
496 write_exp_elt_type
(val.typed_val_int.type
);
497 current_type
= val.typed_val_int.type
;
498 write_exp_elt_longcst
((LONGEST
)val.typed_val_int.val
);
499 write_exp_elt_opcode
(OP_LONG
);
505 { write_exp_elt_opcode
(OP_DOUBLE
);
506 write_exp_elt_type
($1.type
);
507 current_type
= $1.type
;
508 write_exp_elt_dblcst
($1.dval
);
509 write_exp_elt_opcode
(OP_DOUBLE
); }
516 /* Already written by write_dollar_variable. */
519 exp
: SIZEOF
'(' type
')' %prec UNARY
520 { write_exp_elt_opcode
(OP_LONG
);
521 write_exp_elt_type
(builtin_type_int
);
523 write_exp_elt_longcst
((LONGEST
) TYPE_LENGTH
($3));
524 write_exp_elt_opcode
(OP_LONG
); }
528 { /* C strings are converted into array constants with
529 an explicit null byte added at the end. Thus
530 the array upper bound is the string length.
531 There is no such thing in C as a completely empty
533 char *sp
= $1.ptr
; int count
= $1.length
;
536 write_exp_elt_opcode
(OP_LONG
);
537 write_exp_elt_type
(builtin_type_char
);
538 write_exp_elt_longcst
((LONGEST
)(*sp
++));
539 write_exp_elt_opcode
(OP_LONG
);
541 write_exp_elt_opcode
(OP_LONG
);
542 write_exp_elt_type
(builtin_type_char
);
543 write_exp_elt_longcst
((LONGEST
)'\0');
544 write_exp_elt_opcode
(OP_LONG
);
545 write_exp_elt_opcode
(OP_ARRAY
);
546 write_exp_elt_longcst
((LONGEST
) 0);
547 write_exp_elt_longcst
((LONGEST
) ($1.length
));
548 write_exp_elt_opcode
(OP_ARRAY
); }
554 struct value
* this_val
;
555 struct type
* this_type
;
556 write_exp_elt_opcode
(OP_THIS
);
557 write_exp_elt_opcode
(OP_THIS
);
558 /* we need type of this */
559 this_val
= value_of_this
(0);
561 this_type
= value_type
(this_val
);
566 if
(TYPE_CODE
(this_type
) == TYPE_CODE_PTR
)
568 this_type
= TYPE_TARGET_TYPE
(this_type
);
569 write_exp_elt_opcode
(UNOP_IND
);
573 current_type
= this_type
;
577 /* end of object pascal. */
582 $$
= SYMBOL_BLOCK_VALUE
($1.sym
);
586 lookup_symtab
(copy_name
($1.stoken
));
588 $$
= BLOCKVECTOR_BLOCK
(BLOCKVECTOR
(tem
), STATIC_BLOCK
);
590 error ("No file or function \"%s\".",
591 copy_name
($1.stoken
));
596 block
: block COLONCOLON name
598 = lookup_symbol
(copy_name
($3), $1,
599 VAR_DOMAIN
, (int *) NULL
,
600 (struct symtab
**) NULL
);
601 if
(!tem || SYMBOL_CLASS
(tem
) != LOC_BLOCK
)
602 error ("No function \"%s\" in specified context.",
604 $$
= SYMBOL_BLOCK_VALUE
(tem
); }
607 variable: block COLONCOLON name
608 { struct symbol
*sym
;
609 sym
= lookup_symbol
(copy_name
($3), $1,
610 VAR_DOMAIN
, (int *) NULL
,
611 (struct symtab
**) NULL
);
613 error ("No symbol \"%s\" in specified context.",
616 write_exp_elt_opcode
(OP_VAR_VALUE
);
617 /* block_found is set by lookup_symbol. */
618 write_exp_elt_block
(block_found
);
619 write_exp_elt_sym
(sym
);
620 write_exp_elt_opcode
(OP_VAR_VALUE
); }
623 qualified_name: typebase COLONCOLON name
625 struct type
*type
= $1;
626 if
(TYPE_CODE
(type
) != TYPE_CODE_STRUCT
627 && TYPE_CODE
(type
) != TYPE_CODE_UNION
)
628 error ("`%s' is not defined as an aggregate type.",
631 write_exp_elt_opcode
(OP_SCOPE
);
632 write_exp_elt_type
(type
);
633 write_exp_string
($3);
634 write_exp_elt_opcode
(OP_SCOPE
);
638 variable: qualified_name
641 char *name
= copy_name
($2);
643 struct minimal_symbol
*msymbol
;
646 lookup_symbol
(name
, (const struct block
*) NULL
,
647 VAR_DOMAIN
, (int *) NULL
,
648 (struct symtab
**) NULL
);
651 write_exp_elt_opcode
(OP_VAR_VALUE
);
652 write_exp_elt_block
(NULL
);
653 write_exp_elt_sym
(sym
);
654 write_exp_elt_opcode
(OP_VAR_VALUE
);
658 msymbol
= lookup_minimal_symbol
(name
, NULL
, NULL
);
661 write_exp_msymbol
(msymbol
,
662 lookup_function_type
(builtin_type_int
),
666 if
(!have_full_symbols
() && !have_partial_symbols
())
667 error ("No symbol table is loaded. Use the \"file\" command.");
669 error ("No symbol \"%s\" in current context.", name
);
673 variable: name_not_typename
674 { struct symbol
*sym
= $1.sym
;
678 if
(symbol_read_needs_frame
(sym
))
680 if
(innermost_block
== 0
681 || contained_in
(block_found
,
683 innermost_block
= block_found
;
686 write_exp_elt_opcode
(OP_VAR_VALUE
);
687 /* We want to use the selected frame, not
688 another more inner frame which happens to
689 be in the same block. */
690 write_exp_elt_block
(NULL
);
691 write_exp_elt_sym
(sym
);
692 write_exp_elt_opcode
(OP_VAR_VALUE
);
693 current_type
= sym
->type
; }
694 else if
($1.is_a_field_of_this
)
696 struct value
* this_val
;
697 struct type
* this_type
;
698 /* Object pascal: it hangs off of `this'. Must
699 not inadvertently convert from a method call
701 if
(innermost_block
== 0
702 || contained_in
(block_found
,
704 innermost_block
= block_found
;
705 write_exp_elt_opcode
(OP_THIS
);
706 write_exp_elt_opcode
(OP_THIS
);
707 write_exp_elt_opcode
(STRUCTOP_PTR
);
708 write_exp_string
($1.stoken
);
709 write_exp_elt_opcode
(STRUCTOP_PTR
);
710 /* we need type of this */
711 this_val
= value_of_this
(0);
713 this_type
= value_type
(this_val
);
717 current_type
= lookup_struct_elt_type
(
719 copy_name
($1.stoken
), 0);
725 struct minimal_symbol
*msymbol
;
726 char *arg
= copy_name
($1.stoken
);
729 lookup_minimal_symbol
(arg
, NULL
, NULL
);
732 write_exp_msymbol
(msymbol
,
733 lookup_function_type
(builtin_type_int
),
736 else if
(!have_full_symbols
() && !have_partial_symbols
())
737 error ("No symbol table is loaded. Use the \"file\" command.");
739 error ("No symbol \"%s\" in current context.",
740 copy_name
($1.stoken
));
749 /* We used to try to recognize more pointer to member types here, but
750 that didn't work (shift/reduce conflicts meant that these rules never
751 got executed). The problem is that
752 int (foo::bar::baz::bizzle)
753 is a function type but
754 int (foo::bar::baz::bizzle::*)
755 is a pointer to member type. Stroustrup loses again! */
760 typebase
/* Implements (approximately): (type-qualifier)* type-specifier */
762 { $$
= lookup_pointer_type
($2); }
766 { $$
= lookup_struct
(copy_name
($2),
767 expression_context_block
); }
769 { $$
= lookup_struct
(copy_name
($2),
770 expression_context_block
); }
771 /* "const" and "volatile" are curently ignored. A type qualifier
772 after the type is handled in the ptype rule. I think these could
776 name
: NAME
{ $$
= $1.stoken
; }
777 | BLOCKNAME
{ $$
= $1.stoken
; }
778 | TYPENAME
{ $$
= $1.stoken
; }
779 | NAME_OR_INT
{ $$
= $1.stoken
; }
782 name_not_typename
: NAME
784 /* These would be useful if name_not_typename was useful, but it is just
785 a fake for "variable", so these cause reduce/reduce conflicts because
786 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
787 =exp) or just an exp. If name_not_typename was ever used in an lvalue
788 context where only a name could occur, this might be useful.
795 /* Take care of parsing a number (anything that starts with a digit).
796 Set yylval and return the token type; update lexptr.
797 LEN is the number of characters in it. */
799 /*** Needs some error checking for the float case ***/
802 parse_number
(p
, len
, parsed_float
, putithere
)
808 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
809 here, and we do kind of silly things like cast to unsigned. */
816 int base
= input_radix
;
819 /* Number of "L" suffixes encountered. */
822 /* We have found a "L" or "U" suffix. */
823 int found_suffix
= 0;
826 struct type
*signed_type
;
827 struct type
*unsigned_type
;
831 /* It's a float since it contains a point or an exponent. */
833 int num
= 0; /* number of tokens scanned by scanf */
834 char saved_char
= p
[len
];
836 p
[len
] = 0; /* null-terminate the token */
837 num
= sscanf
(p
, "%" DOUBLEST_SCAN_FORMAT
"%c",
838 &putithere
->typed_val_float.dval
, &c
);
839 p
[len
] = saved_char
; /* restore the input stream */
840 if
(num
!= 1) /* check scanf found ONLY a float ... */
842 /* See if it has `f' or `l' suffix (float or long double). */
844 c
= tolower
(p
[len
- 1]);
847 putithere
->typed_val_float.type
= builtin_type_float
;
849 putithere
->typed_val_float.type
= builtin_type_long_double
;
850 else if
(isdigit
(c
) || c
== '.')
851 putithere
->typed_val_float.type
= builtin_type_double
;
858 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
892 if
(c
>= 'A' && c
<= 'Z')
894 if
(c
!= 'l' && c
!= 'u')
896 if
(c
>= '0' && c
<= '9')
904 if
(base
> 10 && c
>= 'a' && c
<= 'f')
908 n
+= i
= c
- 'a' + 10;
921 return ERROR
; /* Char not a digit */
924 return ERROR
; /* Invalid digit in this base */
926 /* Portably test for overflow (only works for nonzero values, so make
927 a second check for zero). FIXME: Can't we just make n and prevn
928 unsigned and avoid this? */
929 if
(c
!= 'l' && c
!= 'u' && (prevn
>= n
) && n
!= 0)
930 unsigned_p
= 1; /* Try something unsigned */
932 /* Portably test for unsigned overflow.
933 FIXME: This check is wrong; for example it doesn't find overflow
934 on 0x123456789 when LONGEST is 32 bits. */
935 if
(c
!= 'l' && c
!= 'u' && n
!= 0)
937 if
((unsigned_p
&& (ULONGEST
) prevn
>= (ULONGEST
) n
))
938 error ("Numeric constant too large.");
943 /* An integer constant is an int, a long, or a long long. An L
944 suffix forces it to be long; an LL suffix forces it to be long
945 long. If not forced to a larger size, it gets the first type of
946 the above that it fits in. To figure out whether it fits, we
947 shift it right and see whether anything remains. Note that we
948 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
949 operation, because many compilers will warn about such a shift
950 (which always produces a zero result). Sometimes gdbarch_int_bit
951 or gdbarch_long_bit will be that big, sometimes not. To deal with
952 the case where it is we just always shift the value more than
953 once, with fewer bits each time. */
955 un
= (ULONGEST
)n
>> 2;
957 && (un
>> (gdbarch_int_bit
(current_gdbarch
) - 2)) == 0)
959 high_bit
= ((ULONGEST
)1) << (gdbarch_int_bit
(current_gdbarch
) - 1);
961 /* A large decimal (not hex or octal) constant (between INT_MAX
962 and UINT_MAX) is a long or unsigned long, according to ANSI,
963 never an unsigned int, but this code treats it as unsigned
964 int. This probably should be fixed. GCC gives a warning on
967 unsigned_type
= builtin_type_unsigned_int
;
968 signed_type
= builtin_type_int
;
971 && (un
>> (gdbarch_long_bit
(current_gdbarch
) - 2)) == 0)
973 high_bit
= ((ULONGEST
)1) << (gdbarch_long_bit
(current_gdbarch
) - 1);
974 unsigned_type
= builtin_type_unsigned_long
;
975 signed_type
= builtin_type_long
;
980 if
(sizeof
(ULONGEST
) * HOST_CHAR_BIT
981 < gdbarch_long_long_bit
(current_gdbarch
))
982 /* A long long does not fit in a LONGEST. */
983 shift
= (sizeof
(ULONGEST
) * HOST_CHAR_BIT
- 1);
985 shift
= (gdbarch_long_long_bit
(current_gdbarch
) - 1);
986 high_bit
= (ULONGEST
) 1 << shift
;
987 unsigned_type
= builtin_type_unsigned_long_long
;
988 signed_type
= builtin_type_long_long
;
991 putithere
->typed_val_int.val
= n
;
993 /* If the high bit of the worked out type is set then this number
994 has to be unsigned. */
996 if
(unsigned_p ||
(n
& high_bit
))
998 putithere
->typed_val_int.type
= unsigned_type
;
1002 putithere
->typed_val_int.type
= signed_type
;
1011 struct type
*stored
;
1012 struct type_push
*next
;
1015 static struct type_push
*tp_top
= NULL
;
1018 push_current_type
(void)
1020 struct type_push
*tpnew
;
1021 tpnew
= (struct type_push
*) malloc
(sizeof
(struct type_push
));
1022 tpnew
->next
= tp_top
;
1023 tpnew
->stored
= current_type
;
1024 current_type
= NULL
;
1029 pop_current_type
(void)
1031 struct type_push
*tp
= tp_top
;
1034 current_type
= tp
->stored
;
1044 enum exp_opcode opcode
;
1047 static const struct token tokentab3
[] =
1049 {"shr", RSH
, BINOP_END
},
1050 {"shl", LSH
, BINOP_END
},
1051 {"and", ANDAND
, BINOP_END
},
1052 {"div", DIV
, BINOP_END
},
1053 {"not", NOT
, BINOP_END
},
1054 {"mod", MOD
, BINOP_END
},
1055 {"inc", INCREMENT
, BINOP_END
},
1056 {"dec", DECREMENT
, BINOP_END
},
1057 {"xor", XOR
, BINOP_END
}
1060 static const struct token tokentab2
[] =
1062 {"or", OR
, BINOP_END
},
1063 {"<>", NOTEQUAL
, BINOP_END
},
1064 {"<=", LEQ
, BINOP_END
},
1065 {">=", GEQ
, BINOP_END
},
1066 {":=", ASSIGN
, BINOP_END
},
1067 {"::", COLONCOLON
, BINOP_END
} };
1069 /* Allocate uppercased var */
1070 /* make an uppercased copy of tokstart */
1071 static char * uptok
(tokstart
, namelen
)
1076 char *uptokstart
= (char *)malloc
(namelen
+1);
1077 for
(i
= 0;i
<= namelen
;i
++)
1079 if
((tokstart
[i
]>='a' && tokstart
[i
]<='z'))
1080 uptokstart
[i
] = tokstart
[i
]-('a'-'A');
1082 uptokstart
[i
] = tokstart
[i
];
1084 uptokstart
[namelen
]='\0';
1087 /* Read one token, getting characters through lexptr. */
1100 int explen
, tempbufindex
;
1101 static char *tempbuf
;
1102 static int tempbufsize
;
1106 prev_lexptr
= lexptr
;
1109 explen
= strlen
(lexptr
);
1110 /* See if it is a special token of length 3. */
1112 for
(i
= 0; i
< sizeof
(tokentab3
) / sizeof
(tokentab3
[0]); i
++)
1113 if
(strncasecmp
(tokstart
, tokentab3
[i
].operator
, 3) == 0
1114 && (!isalpha
(tokentab3
[i
].operator
[0]) || explen
== 3
1115 ||
(!isalpha
(tokstart
[3]) && !isdigit
(tokstart
[3]) && tokstart
[3] != '_')))
1118 yylval.opcode
= tokentab3
[i
].opcode
;
1119 return tokentab3
[i
].token
;
1122 /* See if it is a special token of length 2. */
1124 for
(i
= 0; i
< sizeof
(tokentab2
) / sizeof
(tokentab2
[0]); i
++)
1125 if
(strncasecmp
(tokstart
, tokentab2
[i
].operator
, 2) == 0
1126 && (!isalpha
(tokentab2
[i
].operator
[0]) || explen
== 2
1127 ||
(!isalpha
(tokstart
[2]) && !isdigit
(tokstart
[2]) && tokstart
[2] != '_')))
1130 yylval.opcode
= tokentab2
[i
].opcode
;
1131 return tokentab2
[i
].token
;
1134 switch
(c
= *tokstart
)
1146 /* We either have a character constant ('0' or '\177' for example)
1147 or we have a quoted symbol reference ('foo(int,int)' in object pascal
1152 c
= parse_escape
(&lexptr
);
1154 error ("Empty character constant.");
1156 yylval.typed_val_int.val
= c
;
1157 yylval.typed_val_int.type
= builtin_type_char
;
1162 namelen
= skip_quoted
(tokstart
) - tokstart
;
1165 lexptr
= tokstart
+ namelen
;
1166 if
(lexptr
[-1] != '\'')
1167 error ("Unmatched single quote.");
1170 uptokstart
= uptok
(tokstart
,namelen
);
1173 error ("Invalid character constant.");
1183 if
(paren_depth
== 0)
1190 if
(comma_terminates
&& paren_depth
== 0)
1196 /* Might be a floating point number. */
1197 if
(lexptr
[1] < '0' || lexptr
[1] > '9')
1198 goto symbol
; /* Nope, must be a symbol. */
1199 /* FALL THRU into number case. */
1212 /* It's a number. */
1213 int got_dot
= 0, got_e
= 0, toktype
;
1215 int hex
= input_radix
> 10;
1217 if
(c
== '0' && (p
[1] == 'x' || p
[1] == 'X'))
1222 else if
(c
== '0' && (p
[1]=='t' || p
[1]=='T' || p
[1]=='d' || p
[1]=='D'))
1230 /* This test includes !hex because 'e' is a valid hex digit
1231 and thus does not indicate a floating point number when
1232 the radix is hex. */
1233 if
(!hex
&& !got_e
&& (*p
== 'e' ||
*p
== 'E'))
1234 got_dot
= got_e
= 1;
1235 /* This test does not include !hex, because a '.' always indicates
1236 a decimal floating point number regardless of the radix. */
1237 else if
(!got_dot
&& *p
== '.')
1239 else if
(got_e
&& (p
[-1] == 'e' || p
[-1] == 'E')
1240 && (*p
== '-' ||
*p
== '+'))
1241 /* This is the sign of the exponent, not the end of the
1244 /* We will take any letters or digits. parse_number will
1245 complain if past the radix, or if L or U are not final. */
1246 else if
((*p
< '0' ||
*p
> '9')
1247 && ((*p
< 'a' ||
*p
> 'z')
1248 && (*p
< 'A' ||
*p
> 'Z')))
1251 toktype
= parse_number
(tokstart
, p
- tokstart
, got_dot|got_e
, &yylval);
1252 if
(toktype
== ERROR
)
1254 char *err_copy
= (char *) alloca
(p
- tokstart
+ 1);
1256 memcpy
(err_copy
, tokstart
, p
- tokstart
);
1257 err_copy
[p
- tokstart
] = 0;
1258 error ("Invalid number \"%s\".", err_copy
);
1289 /* Build the gdb internal form of the input string in tempbuf,
1290 translating any standard C escape forms seen. Note that the
1291 buffer is null byte terminated *only* for the convenience of
1292 debugging gdb itself and printing the buffer contents when
1293 the buffer contains no embedded nulls. Gdb does not depend
1294 upon the buffer being null byte terminated, it uses the length
1295 string instead. This allows gdb to handle C strings (as well
1296 as strings in other languages) with embedded null bytes */
1298 tokptr
= ++tokstart
;
1302 /* Grow the static temp buffer if necessary, including allocating
1303 the first one on demand. */
1304 if
(tempbufindex
+ 1 >= tempbufsize
)
1306 tempbuf
= (char *) realloc
(tempbuf
, tempbufsize
+= 64);
1313 /* Do nothing, loop will terminate. */
1317 c
= parse_escape
(&tokptr
);
1322 tempbuf
[tempbufindex
++] = c
;
1325 tempbuf
[tempbufindex
++] = *tokptr
++;
1328 } while
((*tokptr
!= '"') && (*tokptr
!= '\0'));
1329 if
(*tokptr
++ != '"')
1331 error ("Unterminated string in expression.");
1333 tempbuf
[tempbufindex
] = '\0'; /* See note above */
1334 yylval.sval.ptr
= tempbuf
;
1335 yylval.sval.length
= tempbufindex
;
1340 if
(!(c
== '_' || c
== '$'
1341 ||
(c
>= 'a' && c
<= 'z') ||
(c
>= 'A' && c
<= 'Z')))
1342 /* We must have come across a bad character (e.g. ';'). */
1343 error ("Invalid character '%c' in expression.", c
);
1345 /* It's a name. See how long it is. */
1347 for
(c
= tokstart
[namelen
];
1348 (c
== '_' || c
== '$' ||
(c
>= '0' && c
<= '9')
1349 ||
(c
>= 'a' && c
<= 'z') ||
(c
>= 'A' && c
<= 'Z') || c
== '<');)
1351 /* Template parameter lists are part of the name.
1352 FIXME: This mishandles `print $a<4&&$a>3'. */
1356 int nesting_level
= 1;
1357 while
(tokstart
[++i
])
1359 if
(tokstart
[i
] == '<')
1361 else if
(tokstart
[i
] == '>')
1363 if
(--nesting_level
== 0)
1367 if
(tokstart
[i
] == '>')
1373 /* do NOT uppercase internals because of registers !!! */
1374 c
= tokstart
[++namelen
];
1377 uptokstart
= uptok
(tokstart
,namelen
);
1379 /* The token "if" terminates the expression and is NOT
1380 removed from the input stream. */
1381 if
(namelen
== 2 && uptokstart
[0] == 'I' && uptokstart
[1] == 'F')
1391 /* Catch specific keywords. Should be done with a data structure. */
1395 if
(strcmp
(uptokstart
, "OBJECT") == 0)
1400 if
(strcmp
(uptokstart
, "RECORD") == 0)
1405 if
(strcmp
(uptokstart
, "SIZEOF") == 0)
1412 if
(strcmp
(uptokstart
, "CLASS") == 0)
1417 if
(strcmp
(uptokstart
, "FALSE") == 0)
1421 return FALSEKEYWORD
;
1425 if
(strcmp
(uptokstart
, "TRUE") == 0)
1431 if
(strcmp
(uptokstart
, "SELF") == 0)
1433 /* here we search for 'this' like
1434 inserted in FPC stabs debug info */
1435 static const char this_name
[] = "this";
1437 if
(lookup_symbol
(this_name
, expression_context_block
,
1438 VAR_DOMAIN
, (int *) NULL
,
1439 (struct symtab
**) NULL
))
1450 yylval.sval.ptr
= tokstart
;
1451 yylval.sval.length
= namelen
;
1453 if
(*tokstart
== '$')
1455 /* $ is the normal prefix for pascal hexadecimal values
1456 but this conflicts with the GDB use for debugger variables
1457 so in expression to enter hexadecimal values
1458 we still need to use C syntax with 0xff */
1459 write_dollar_variable
(yylval.sval
);
1464 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1465 functions or symtabs. If this is not so, then ...
1466 Use token-type TYPENAME for symbols that happen to be defined
1467 currently as names of types; NAME for other symbols.
1468 The caller is not constrained to care about the distinction. */
1470 char *tmp
= copy_name
(yylval.sval
);
1472 int is_a_field_of_this
= 0;
1477 if
(search_field
&& current_type
)
1478 is_a_field
= (lookup_struct_elt_type
(current_type
, tmp
, 1) != NULL
);
1482 sym
= lookup_symbol
(tmp
, expression_context_block
,
1484 &is_a_field_of_this
,
1485 (struct symtab
**) NULL
);
1486 /* second chance uppercased (as Free Pascal does). */
1487 if
(!sym
&& !is_a_field_of_this
&& !is_a_field
)
1489 for
(i
= 0; i
<= namelen
; i
++)
1491 if
((tmp
[i
] >= 'a' && tmp
[i
] <= 'z'))
1492 tmp
[i
] -= ('a'-'A');
1494 if
(search_field
&& current_type
)
1495 is_a_field
= (lookup_struct_elt_type
(current_type
, tmp
, 1) != NULL
);
1499 sym
= lookup_symbol
(tmp
, expression_context_block
,
1501 &is_a_field_of_this
,
1502 (struct symtab
**) NULL
);
1503 if
(sym || is_a_field_of_this || is_a_field
)
1504 for
(i
= 0; i
<= namelen
; i
++)
1506 if
((tokstart
[i
] >= 'a' && tokstart
[i
] <= 'z'))
1507 tokstart
[i
] -= ('a'-'A');
1510 /* Third chance Capitalized (as GPC does). */
1511 if
(!sym
&& !is_a_field_of_this
&& !is_a_field
)
1513 for
(i
= 0; i
<= namelen
; i
++)
1517 if
((tmp
[i
] >= 'a' && tmp
[i
] <= 'z'))
1518 tmp
[i
] -= ('a'-'A');
1521 if
((tmp
[i
] >= 'A' && tmp
[i
] <= 'Z'))
1522 tmp
[i
] -= ('A'-'a');
1524 if
(search_field
&& current_type
)
1525 is_a_field
= (lookup_struct_elt_type
(current_type
, tmp
, 1) != NULL
);
1529 sym
= lookup_symbol
(tmp
, expression_context_block
,
1531 &is_a_field_of_this
,
1532 (struct symtab
**) NULL
);
1533 if
(sym || is_a_field_of_this || is_a_field
)
1534 for
(i
= 0; i
<= namelen
; i
++)
1538 if
((tokstart
[i
] >= 'a' && tokstart
[i
] <= 'z'))
1539 tokstart
[i
] -= ('a'-'A');
1542 if
((tokstart
[i
] >= 'A' && tokstart
[i
] <= 'Z'))
1543 tokstart
[i
] -= ('A'-'a');
1549 tempbuf
= (char *) realloc
(tempbuf
, namelen
+ 1);
1550 strncpy
(tempbuf
, tokstart
, namelen
); tempbuf
[namelen
] = 0;
1551 yylval.sval.ptr
= tempbuf
;
1552 yylval.sval.length
= namelen
;
1556 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1557 no psymtabs (coff, xcoff, or some future change to blow away the
1558 psymtabs once once symbols are read). */
1559 if
((sym
&& SYMBOL_CLASS
(sym
) == LOC_BLOCK
)
1560 || lookup_symtab
(tmp
))
1562 yylval.ssym.sym
= sym
;
1563 yylval.ssym.is_a_field_of_this
= is_a_field_of_this
;
1567 if
(sym
&& SYMBOL_CLASS
(sym
) == LOC_TYPEDEF
)
1570 /* Despite the following flaw, we need to keep this code enabled.
1571 Because we can get called from check_stub_method, if we don't
1572 handle nested types then it screws many operations in any
1573 program which uses nested types. */
1574 /* In "A::x", if x is a member function of A and there happens
1575 to be a type (nested or not, since the stabs don't make that
1576 distinction) named x, then this code incorrectly thinks we
1577 are dealing with nested types rather than a member function. */
1581 struct symbol
*best_sym
;
1583 /* Look ahead to detect nested types. This probably should be
1584 done in the grammar, but trying seemed to introduce a lot
1585 of shift/reduce and reduce/reduce conflicts. It's possible
1586 that it could be done, though. Or perhaps a non-grammar, but
1587 less ad hoc, approach would work well. */
1589 /* Since we do not currently have any way of distinguishing
1590 a nested type from a non-nested one (the stabs don't tell
1591 us whether a type is nested), we just ignore the
1598 /* Skip whitespace. */
1599 while
(*p
== ' ' ||
*p
== '\t' ||
*p
== '\n')
1601 if
(*p
== ':' && p
[1] == ':')
1603 /* Skip the `::'. */
1605 /* Skip whitespace. */
1606 while
(*p
== ' ' ||
*p
== '\t' ||
*p
== '\n')
1609 while
(*p
== '_' ||
*p
== '$' ||
(*p
>= '0' && *p
<= '9')
1610 ||
(*p
>= 'a' && *p
<= 'z')
1611 ||
(*p
>= 'A' && *p
<= 'Z'))
1615 struct symbol
*cur_sym
;
1616 /* As big as the whole rest of the expression, which is
1617 at least big enough. */
1618 char *ncopy
= alloca
(strlen
(tmp
)+strlen
(namestart
)+3);
1622 memcpy
(tmp1
, tmp
, strlen
(tmp
));
1623 tmp1
+= strlen
(tmp
);
1624 memcpy
(tmp1
, "::", 2);
1626 memcpy
(tmp1
, namestart
, p
- namestart
);
1627 tmp1
[p
- namestart
] = '\0';
1628 cur_sym
= lookup_symbol
(ncopy
, expression_context_block
,
1629 VAR_DOMAIN
, (int *) NULL
,
1630 (struct symtab
**) NULL
);
1633 if
(SYMBOL_CLASS
(cur_sym
) == LOC_TYPEDEF
)
1651 yylval.tsym.type
= SYMBOL_TYPE
(best_sym
);
1653 yylval.tsym.type
= SYMBOL_TYPE
(sym
);
1659 = language_lookup_primitive_type_by_name
(current_language
,
1660 current_gdbarch
, tmp
);
1661 if
(yylval.tsym.type
!= NULL
)
1667 /* Input names that aren't symbols but ARE valid hex numbers,
1668 when the input radix permits them, can be names or numbers
1669 depending on the parse. Note we support radixes > 16 here. */
1671 && ((tokstart
[0] >= 'a' && tokstart
[0] < 'a' + input_radix
- 10)
1672 ||
(tokstart
[0] >= 'A' && tokstart
[0] < 'A' + input_radix
- 10)))
1674 YYSTYPE newlval
; /* Its value is ignored. */
1675 hextype
= parse_number
(tokstart
, namelen
, 0, &newlval
);
1678 yylval.ssym.sym
= sym
;
1679 yylval.ssym.is_a_field_of_this
= is_a_field_of_this
;
1686 /* Any other kind of symbol */
1687 yylval.ssym.sym
= sym
;
1688 yylval.ssym.is_a_field_of_this
= is_a_field_of_this
;
1698 lexptr
= prev_lexptr
;
1700 error ("A %s in expression, near `%s'.", (msg ? msg
: "error"), lexptr
);