1 /* YACC parser for Pascal expressions, for GDB.
2 Copyright (C) 2000-2013 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 3 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, see <http://www.gnu.org/licenses/>. */
19 /* This file is derived from c-exp.y */
21 /* Parse a Pascal 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
28 come first in the result.
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. */
38 /* Known bugs or limitations:
39 - pascal string operations are not supported at all.
40 - there are some problems with boolean types.
41 - Pascal type hexadecimal constants are not supported
42 because they conflict with the internal variables format.
43 Probably also lots of other problems, less well defined PM. */
47 #include "gdb_string.h"
49 #include "expression.h"
51 #include "parser-defs.h"
54 #include "bfd.h" /* Required by objfiles.h. */
55 #include "symfile.h" /* Required by objfiles.h. */
56 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols. */
58 #include "completer.h"
60 #define parse_type builtin_type (parse_gdbarch)
62 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
63 as well as gratuitiously global symbol names, so we can have multiple
64 yacc generated parsers in gdb. Note that these are only the variables
65 produced by yacc. If other parser generators (bison, byacc, etc) produce
66 additional global names that conflict at link time, then those parser
67 generators need to be fixed instead of adding those names to this list. */
69 #define yymaxdepth pascal_maxdepth
70 #define yyparse pascal_parse
71 #define yylex pascal_lex
72 #define yyerror pascal_error
73 #define yylval pascal_lval
74 #define yychar pascal_char
75 #define yydebug pascal_debug
76 #define yypact pascal_pact
77 #define yyr1 pascal_r1
78 #define yyr2 pascal_r2
79 #define yydef pascal_def
80 #define yychk pascal_chk
81 #define yypgo pascal_pgo
82 #define yyact pascal_act
83 #define yyexca pascal_exca
84 #define yyerrflag pascal_errflag
85 #define yynerrs pascal_nerrs
86 #define yyps pascal_ps
87 #define yypv pascal_pv
89 #define yy_yys pascal_yys
90 #define yystate pascal_state
91 #define yytmp pascal_tmp
93 #define yy_yyv pascal_yyv
94 #define yyval pascal_val
95 #define yylloc pascal_lloc
96 #define yyreds pascal_reds /* With YYDEBUG defined */
97 #define yytoks pascal_toks /* With YYDEBUG defined */
98 #define yyname pascal_name /* With YYDEBUG defined */
99 #define yyrule pascal_rule /* With YYDEBUG defined */
100 #define yylhs pascal_yylhs
101 #define yylen pascal_yylen
102 #define yydefred pascal_yydefred
103 #define yydgoto pascal_yydgoto
104 #define yysindex pascal_yysindex
105 #define yyrindex pascal_yyrindex
106 #define yygindex pascal_yygindex
107 #define yytable pascal_yytable
108 #define yycheck pascal_yycheck
109 #define yyss pascal_yyss
110 #define yysslim pascal_yysslim
111 #define yyssp pascal_yyssp
112 #define yystacksize pascal_yystacksize
113 #define yyvs pascal_yyvs
114 #define yyvsp pascal_yyvsp
117 #define YYDEBUG 1 /* Default to yydebug support */
120 #define YYFPRINTF parser_fprintf
124 static int yylex (void);
126 void yyerror (char *);
128 static char * uptok (char *, int);
131 /* Although the yacc "value" of an expression is not used,
132 since the result is stored in the structure being created,
133 other node types do have values. */
150 struct symtoken ssym;
153 enum exp_opcode opcode;
154 struct internalvar *ivar;
161 /* YYSTYPE gets defined by %union */
162 static int parse_number (const char *, int, int, YYSTYPE *);
164 static struct type *current_type;
165 static struct internalvar *intvar;
166 static int leftdiv_is_integer;
167 static void push_current_type (void);
168 static void pop_current_type (void);
169 static int search_field;
172 %type <voidval> exp exp1 type_exp start normal_start variable qualified_name
173 %type <tval> type typebase
174 /* %type <bval> block */
176 /* Fancy type parsing. */
179 %token <typed_val_int> INT
180 %token <typed_val_float> FLOAT
182 /* Both NAME and TYPENAME tokens represent symbols in the input,
183 and both convey their data as strings.
184 But a TYPENAME is a string that happens to be defined as a typedef
185 or builtin type name (such as int or char)
186 and a NAME is any other symbol.
187 Contexts where this distinction is not important can use the
188 nonterminal "name", which matches either NAME or TYPENAME. */
191 %token <sval> FIELDNAME
192 %token <voidval> COMPLETE
193 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
194 %token <tsym> TYPENAME
196 %type <ssym> name_not_typename
198 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
199 but which would parse as a valid number in the current input radix.
200 E.g. "c" when input_radix==16. Depending on the parse, it will be
201 turned into a name or into a number. */
203 %token <ssym> NAME_OR_INT
205 %token STRUCT CLASS SIZEOF COLONCOLON
208 /* Special type cases, put in to allow the parser to distinguish different
211 %token <voidval> VARIABLE
216 %token <lval> TRUEKEYWORD FALSEKEYWORD
226 %left '<' '>' LEQ GEQ
227 %left LSH RSH DIV MOD
231 %right UNARY INCREMENT DECREMENT
232 %right ARROW '.' '[' '('
234 %token <ssym> BLOCKNAME
241 start : { current_type = NULL;
244 leftdiv_is_integer = 0;
255 { write_exp_elt_opcode(OP_TYPE);
256 write_exp_elt_type($1);
257 write_exp_elt_opcode(OP_TYPE);
258 current_type = $1; } ;
260 /* Expressions, including the comma operator. */
263 { write_exp_elt_opcode (BINOP_COMMA); }
266 /* Expressions, not including the comma operator. */
267 exp : exp '^' %prec UNARY
268 { write_exp_elt_opcode (UNOP_IND);
270 current_type = TYPE_TARGET_TYPE (current_type); }
273 exp : '@' exp %prec UNARY
274 { write_exp_elt_opcode (UNOP_ADDR);
276 current_type = TYPE_POINTER_TYPE (current_type); }
279 exp : '-' exp %prec UNARY
280 { write_exp_elt_opcode (UNOP_NEG); }
283 exp : NOT exp %prec UNARY
284 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
287 exp : INCREMENT '(' exp ')' %prec UNARY
288 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
291 exp : DECREMENT '(' exp ')' %prec UNARY
292 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
296 field_exp : exp '.' %prec UNARY
297 { search_field = 1; }
300 exp : field_exp FIELDNAME
301 { write_exp_elt_opcode (STRUCTOP_STRUCT);
302 write_exp_string ($2);
303 write_exp_elt_opcode (STRUCTOP_STRUCT);
307 while (TYPE_CODE (current_type)
310 TYPE_TARGET_TYPE (current_type);
311 current_type = lookup_struct_elt_type (
312 current_type, $2.ptr, 0);
319 { mark_struct_expression ();
320 write_exp_elt_opcode (STRUCTOP_STRUCT);
321 write_exp_string ($2);
322 write_exp_elt_opcode (STRUCTOP_STRUCT);
326 while (TYPE_CODE (current_type)
329 TYPE_TARGET_TYPE (current_type);
330 current_type = lookup_struct_elt_type (
331 current_type, $2.ptr, 0);
336 exp : field_exp COMPLETE
338 mark_struct_expression ();
339 write_exp_elt_opcode (STRUCTOP_STRUCT);
342 write_exp_string (s);
343 write_exp_elt_opcode (STRUCTOP_STRUCT); }
347 /* We need to save the current_type value. */
348 { const char *arrayname;
350 arrayfieldindex = is_pascal_string_type (
351 current_type, NULL, NULL,
352 NULL, NULL, &arrayname);
355 struct stoken stringsval;
358 buf = alloca (strlen (arrayname) + 1);
359 stringsval.ptr = buf;
360 stringsval.length = strlen (arrayname);
361 strcpy (buf, arrayname);
362 current_type = TYPE_FIELD_TYPE (current_type,
363 arrayfieldindex - 1);
364 write_exp_elt_opcode (STRUCTOP_STRUCT);
365 write_exp_string (stringsval);
366 write_exp_elt_opcode (STRUCTOP_STRUCT);
368 push_current_type (); }
370 { pop_current_type ();
371 write_exp_elt_opcode (BINOP_SUBSCRIPT);
373 current_type = TYPE_TARGET_TYPE (current_type); }
377 /* This is to save the value of arglist_len
378 being accumulated by an outer function call. */
379 { push_current_type ();
381 arglist ')' %prec ARROW
382 { write_exp_elt_opcode (OP_FUNCALL);
383 write_exp_elt_longcst ((LONGEST) end_arglist ());
384 write_exp_elt_opcode (OP_FUNCALL);
387 current_type = TYPE_TARGET_TYPE (current_type);
394 | arglist ',' exp %prec ABOVE_COMMA
398 exp : type '(' exp ')' %prec UNARY
401 /* Allow automatic dereference of classes. */
402 if ((TYPE_CODE (current_type) == TYPE_CODE_PTR)
403 && (TYPE_CODE (TYPE_TARGET_TYPE (current_type)) == TYPE_CODE_CLASS)
404 && (TYPE_CODE ($1) == TYPE_CODE_CLASS))
405 write_exp_elt_opcode (UNOP_IND);
407 write_exp_elt_opcode (UNOP_CAST);
408 write_exp_elt_type ($1);
409 write_exp_elt_opcode (UNOP_CAST);
417 /* Binary operators in order of decreasing precedence. */
420 { write_exp_elt_opcode (BINOP_MUL); }
424 if (current_type && is_integral_type (current_type))
425 leftdiv_is_integer = 1;
429 if (leftdiv_is_integer && current_type
430 && is_integral_type (current_type))
432 write_exp_elt_opcode (UNOP_CAST);
433 write_exp_elt_type (parse_type->builtin_long_double);
434 current_type = parse_type->builtin_long_double;
435 write_exp_elt_opcode (UNOP_CAST);
436 leftdiv_is_integer = 0;
439 write_exp_elt_opcode (BINOP_DIV);
444 { write_exp_elt_opcode (BINOP_INTDIV); }
448 { write_exp_elt_opcode (BINOP_REM); }
452 { write_exp_elt_opcode (BINOP_ADD); }
456 { write_exp_elt_opcode (BINOP_SUB); }
460 { write_exp_elt_opcode (BINOP_LSH); }
464 { write_exp_elt_opcode (BINOP_RSH); }
468 { write_exp_elt_opcode (BINOP_EQUAL);
469 current_type = parse_type->builtin_bool;
473 exp : exp NOTEQUAL exp
474 { write_exp_elt_opcode (BINOP_NOTEQUAL);
475 current_type = parse_type->builtin_bool;
480 { write_exp_elt_opcode (BINOP_LEQ);
481 current_type = parse_type->builtin_bool;
486 { write_exp_elt_opcode (BINOP_GEQ);
487 current_type = parse_type->builtin_bool;
492 { write_exp_elt_opcode (BINOP_LESS);
493 current_type = parse_type->builtin_bool;
498 { write_exp_elt_opcode (BINOP_GTR);
499 current_type = parse_type->builtin_bool;
504 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
508 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
512 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
516 { write_exp_elt_opcode (BINOP_ASSIGN); }
520 { write_exp_elt_opcode (OP_BOOL);
521 write_exp_elt_longcst ((LONGEST) $1);
522 current_type = parse_type->builtin_bool;
523 write_exp_elt_opcode (OP_BOOL); }
527 { write_exp_elt_opcode (OP_BOOL);
528 write_exp_elt_longcst ((LONGEST) $1);
529 current_type = parse_type->builtin_bool;
530 write_exp_elt_opcode (OP_BOOL); }
534 { write_exp_elt_opcode (OP_LONG);
535 write_exp_elt_type ($1.type);
536 current_type = $1.type;
537 write_exp_elt_longcst ((LONGEST)($1.val));
538 write_exp_elt_opcode (OP_LONG); }
543 parse_number ($1.stoken.ptr,
544 $1.stoken.length, 0, &val);
545 write_exp_elt_opcode (OP_LONG);
546 write_exp_elt_type (val.typed_val_int.type);
547 current_type = val.typed_val_int.type;
548 write_exp_elt_longcst ((LONGEST)
549 val.typed_val_int.val);
550 write_exp_elt_opcode (OP_LONG);
556 { write_exp_elt_opcode (OP_DOUBLE);
557 write_exp_elt_type ($1.type);
558 current_type = $1.type;
559 write_exp_elt_dblcst ($1.dval);
560 write_exp_elt_opcode (OP_DOUBLE); }
567 /* Already written by write_dollar_variable.
568 Handle current_type. */
570 struct value * val, * mark;
572 mark = value_mark ();
573 val = value_of_internalvar (parse_gdbarch,
575 current_type = value_type (val);
576 value_release_to_mark (mark);
581 exp : SIZEOF '(' type ')' %prec UNARY
582 { write_exp_elt_opcode (OP_LONG);
583 write_exp_elt_type (parse_type->builtin_int);
585 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
586 write_exp_elt_opcode (OP_LONG); }
589 exp : SIZEOF '(' exp ')' %prec UNARY
590 { write_exp_elt_opcode (UNOP_SIZEOF); }
593 { /* C strings are converted into array constants with
594 an explicit null byte added at the end. Thus
595 the array upper bound is the string length.
596 There is no such thing in C as a completely empty
598 const char *sp = $1.ptr; int count = $1.length;
602 write_exp_elt_opcode (OP_LONG);
603 write_exp_elt_type (parse_type->builtin_char);
604 write_exp_elt_longcst ((LONGEST)(*sp++));
605 write_exp_elt_opcode (OP_LONG);
607 write_exp_elt_opcode (OP_LONG);
608 write_exp_elt_type (parse_type->builtin_char);
609 write_exp_elt_longcst ((LONGEST)'\0');
610 write_exp_elt_opcode (OP_LONG);
611 write_exp_elt_opcode (OP_ARRAY);
612 write_exp_elt_longcst ((LONGEST) 0);
613 write_exp_elt_longcst ((LONGEST) ($1.length));
614 write_exp_elt_opcode (OP_ARRAY); }
620 struct value * this_val;
621 struct type * this_type;
622 write_exp_elt_opcode (OP_THIS);
623 write_exp_elt_opcode (OP_THIS);
624 /* We need type of this. */
625 this_val = value_of_this_silent (parse_language);
627 this_type = value_type (this_val);
632 if (TYPE_CODE (this_type) == TYPE_CODE_PTR)
634 this_type = TYPE_TARGET_TYPE (this_type);
635 write_exp_elt_opcode (UNOP_IND);
639 current_type = this_type;
643 /* end of object pascal. */
648 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
652 lookup_symtab (copy_name ($1.stoken));
654 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem),
657 error (_("No file or function \"%s\"."),
658 copy_name ($1.stoken));
663 block : block COLONCOLON name
665 = lookup_symbol (copy_name ($3), $1,
667 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
668 error (_("No function \"%s\" in specified context."),
670 $$ = SYMBOL_BLOCK_VALUE (tem); }
673 variable: block COLONCOLON name
674 { struct symbol *sym;
675 sym = lookup_symbol (copy_name ($3), $1,
678 error (_("No symbol \"%s\" in specified context."),
681 write_exp_elt_opcode (OP_VAR_VALUE);
682 /* block_found is set by lookup_symbol. */
683 write_exp_elt_block (block_found);
684 write_exp_elt_sym (sym);
685 write_exp_elt_opcode (OP_VAR_VALUE); }
688 qualified_name: typebase COLONCOLON name
690 struct type *type = $1;
691 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
692 && TYPE_CODE (type) != TYPE_CODE_UNION)
693 error (_("`%s' is not defined as an aggregate type."),
696 write_exp_elt_opcode (OP_SCOPE);
697 write_exp_elt_type (type);
698 write_exp_string ($3);
699 write_exp_elt_opcode (OP_SCOPE);
703 variable: qualified_name
706 char *name = copy_name ($2);
708 struct bound_minimal_symbol msymbol;
711 lookup_symbol (name, (const struct block *) NULL,
715 write_exp_elt_opcode (OP_VAR_VALUE);
716 write_exp_elt_block (NULL);
717 write_exp_elt_sym (sym);
718 write_exp_elt_opcode (OP_VAR_VALUE);
722 msymbol = lookup_bound_minimal_symbol (name);
723 if (msymbol.minsym != NULL)
724 write_exp_msymbol (msymbol);
725 else if (!have_full_symbols ()
726 && !have_partial_symbols ())
727 error (_("No symbol table is loaded. "
728 "Use the \"file\" command."));
730 error (_("No symbol \"%s\" in current context."),
735 variable: name_not_typename
736 { struct symbol *sym = $1.sym;
740 if (symbol_read_needs_frame (sym))
742 if (innermost_block == 0
743 || contained_in (block_found,
745 innermost_block = block_found;
748 write_exp_elt_opcode (OP_VAR_VALUE);
749 /* We want to use the selected frame, not
750 another more inner frame which happens to
751 be in the same block. */
752 write_exp_elt_block (NULL);
753 write_exp_elt_sym (sym);
754 write_exp_elt_opcode (OP_VAR_VALUE);
755 current_type = sym->type; }
756 else if ($1.is_a_field_of_this)
758 struct value * this_val;
759 struct type * this_type;
760 /* Object pascal: it hangs off of `this'. Must
761 not inadvertently convert from a method call
763 if (innermost_block == 0
764 || contained_in (block_found,
766 innermost_block = block_found;
767 write_exp_elt_opcode (OP_THIS);
768 write_exp_elt_opcode (OP_THIS);
769 write_exp_elt_opcode (STRUCTOP_PTR);
770 write_exp_string ($1.stoken);
771 write_exp_elt_opcode (STRUCTOP_PTR);
772 /* We need type of this. */
773 this_val = value_of_this_silent (parse_language);
775 this_type = value_type (this_val);
779 current_type = lookup_struct_elt_type (
781 copy_name ($1.stoken), 0);
787 struct bound_minimal_symbol msymbol;
788 char *arg = copy_name ($1.stoken);
791 lookup_bound_minimal_symbol (arg);
792 if (msymbol.minsym != NULL)
793 write_exp_msymbol (msymbol);
794 else if (!have_full_symbols ()
795 && !have_partial_symbols ())
796 error (_("No symbol table is loaded. "
797 "Use the \"file\" command."));
799 error (_("No symbol \"%s\" in current context."),
800 copy_name ($1.stoken));
809 /* We used to try to recognize more pointer to member types here, but
810 that didn't work (shift/reduce conflicts meant that these rules never
811 got executed). The problem is that
812 int (foo::bar::baz::bizzle)
813 is a function type but
814 int (foo::bar::baz::bizzle::*)
815 is a pointer to member type. Stroustrup loses again! */
820 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
822 { $$ = lookup_pointer_type ($2); }
826 { $$ = lookup_struct (copy_name ($2),
827 expression_context_block); }
829 { $$ = lookup_struct (copy_name ($2),
830 expression_context_block); }
831 /* "const" and "volatile" are curently ignored. A type qualifier
832 after the type is handled in the ptype rule. I think these could
836 name : NAME { $$ = $1.stoken; }
837 | BLOCKNAME { $$ = $1.stoken; }
838 | TYPENAME { $$ = $1.stoken; }
839 | NAME_OR_INT { $$ = $1.stoken; }
842 name_not_typename : NAME
844 /* These would be useful if name_not_typename was useful, but it is just
845 a fake for "variable", so these cause reduce/reduce conflicts because
846 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
847 =exp) or just an exp. If name_not_typename was ever used in an lvalue
848 context where only a name could occur, this might be useful.
855 /* Take care of parsing a number (anything that starts with a digit).
856 Set yylval and return the token type; update lexptr.
857 LEN is the number of characters in it. */
859 /*** Needs some error checking for the float case ***/
862 parse_number (const char *p, int len, int parsed_float, YYSTYPE *putithere)
864 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
865 here, and we do kind of silly things like cast to unsigned. */
872 int base = input_radix;
875 /* Number of "L" suffixes encountered. */
878 /* We have found a "L" or "U" suffix. */
879 int found_suffix = 0;
882 struct type *signed_type;
883 struct type *unsigned_type;
887 if (! parse_c_float (parse_gdbarch, p, len,
888 &putithere->typed_val_float.dval,
889 &putithere->typed_val_float.type))
894 /* Handle base-switching prefixes 0x, 0t, 0d, 0. */
928 if (c >= 'A' && c <= 'Z')
930 if (c != 'l' && c != 'u')
932 if (c >= '0' && c <= '9')
940 if (base > 10 && c >= 'a' && c <= 'f')
944 n += i = c - 'a' + 10;
957 return ERROR; /* Char not a digit */
960 return ERROR; /* Invalid digit in this base. */
962 /* Portably test for overflow (only works for nonzero values, so make
963 a second check for zero). FIXME: Can't we just make n and prevn
964 unsigned and avoid this? */
965 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
966 unsigned_p = 1; /* Try something unsigned. */
968 /* Portably test for unsigned overflow.
969 FIXME: This check is wrong; for example it doesn't find overflow
970 on 0x123456789 when LONGEST is 32 bits. */
971 if (c != 'l' && c != 'u' && n != 0)
973 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
974 error (_("Numeric constant too large."));
979 /* An integer constant is an int, a long, or a long long. An L
980 suffix forces it to be long; an LL suffix forces it to be long
981 long. If not forced to a larger size, it gets the first type of
982 the above that it fits in. To figure out whether it fits, we
983 shift it right and see whether anything remains. Note that we
984 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
985 operation, because many compilers will warn about such a shift
986 (which always produces a zero result). Sometimes gdbarch_int_bit
987 or gdbarch_long_bit will be that big, sometimes not. To deal with
988 the case where it is we just always shift the value more than
989 once, with fewer bits each time. */
991 un = (ULONGEST)n >> 2;
993 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
995 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
997 /* A large decimal (not hex or octal) constant (between INT_MAX
998 and UINT_MAX) is a long or unsigned long, according to ANSI,
999 never an unsigned int, but this code treats it as unsigned
1000 int. This probably should be fixed. GCC gives a warning on
1003 unsigned_type = parse_type->builtin_unsigned_int;
1004 signed_type = parse_type->builtin_int;
1006 else if (long_p <= 1
1007 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1009 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1010 unsigned_type = parse_type->builtin_unsigned_long;
1011 signed_type = parse_type->builtin_long;
1016 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1017 < gdbarch_long_long_bit (parse_gdbarch))
1018 /* A long long does not fit in a LONGEST. */
1019 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1021 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1022 high_bit = (ULONGEST) 1 << shift;
1023 unsigned_type = parse_type->builtin_unsigned_long_long;
1024 signed_type = parse_type->builtin_long_long;
1027 putithere->typed_val_int.val = n;
1029 /* If the high bit of the worked out type is set then this number
1030 has to be unsigned. */
1032 if (unsigned_p || (n & high_bit))
1034 putithere->typed_val_int.type = unsigned_type;
1038 putithere->typed_val_int.type = signed_type;
1047 struct type *stored;
1048 struct type_push *next;
1051 static struct type_push *tp_top = NULL;
1054 push_current_type (void)
1056 struct type_push *tpnew;
1057 tpnew = (struct type_push *) malloc (sizeof (struct type_push));
1058 tpnew->next = tp_top;
1059 tpnew->stored = current_type;
1060 current_type = NULL;
1065 pop_current_type (void)
1067 struct type_push *tp = tp_top;
1070 current_type = tp->stored;
1080 enum exp_opcode opcode;
1083 static const struct token tokentab3[] =
1085 {"shr", RSH, BINOP_END},
1086 {"shl", LSH, BINOP_END},
1087 {"and", ANDAND, BINOP_END},
1088 {"div", DIV, BINOP_END},
1089 {"not", NOT, BINOP_END},
1090 {"mod", MOD, BINOP_END},
1091 {"inc", INCREMENT, BINOP_END},
1092 {"dec", DECREMENT, BINOP_END},
1093 {"xor", XOR, BINOP_END}
1096 static const struct token tokentab2[] =
1098 {"or", OR, BINOP_END},
1099 {"<>", NOTEQUAL, BINOP_END},
1100 {"<=", LEQ, BINOP_END},
1101 {">=", GEQ, BINOP_END},
1102 {":=", ASSIGN, BINOP_END},
1103 {"::", COLONCOLON, BINOP_END} };
1105 /* Allocate uppercased var: */
1106 /* make an uppercased copy of tokstart. */
1108 uptok (char *tokstart, int namelen)
1111 char *uptokstart = (char *)malloc(namelen+1);
1112 for (i = 0;i <= namelen;i++)
1114 if ((tokstart[i]>='a' && tokstart[i]<='z'))
1115 uptokstart[i] = tokstart[i]-('a'-'A');
1117 uptokstart[i] = tokstart[i];
1119 uptokstart[namelen]='\0';
1123 /* This is set if the previously-returned token was a structure
1124 operator '.'. This is used only when parsing to
1125 do field name completion. */
1126 static int last_was_structop;
1128 /* Read one token, getting characters through lexptr. */
1139 int explen, tempbufindex;
1140 static char *tempbuf;
1141 static int tempbufsize;
1142 int saw_structop = last_was_structop;
1144 last_was_structop = 0;
1147 prev_lexptr = lexptr;
1149 explen = strlen (lexptr);
1150 tokstart = alloca (explen + 1);
1151 memcpy (tokstart, lexptr, explen + 1);
1153 /* See if it is a special token of length 3. */
1155 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1156 if (strncasecmp (tokstart, tokentab3[i].operator, 3) == 0
1157 && (!isalpha (tokentab3[i].operator[0]) || explen == 3
1158 || (!isalpha (tokstart[3])
1159 && !isdigit (tokstart[3]) && tokstart[3] != '_')))
1162 yylval.opcode = tokentab3[i].opcode;
1163 return tokentab3[i].token;
1166 /* See if it is a special token of length 2. */
1168 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1169 if (strncasecmp (tokstart, tokentab2[i].operator, 2) == 0
1170 && (!isalpha (tokentab2[i].operator[0]) || explen == 2
1171 || (!isalpha (tokstart[2])
1172 && !isdigit (tokstart[2]) && tokstart[2] != '_')))
1175 yylval.opcode = tokentab2[i].opcode;
1176 return tokentab2[i].token;
1179 switch (c = *tokstart)
1182 if (saw_structop && search_field)
1194 /* We either have a character constant ('0' or '\177' for example)
1195 or we have a quoted symbol reference ('foo(int,int)' in object pascal
1200 c = parse_escape (parse_gdbarch, &lexptr);
1202 error (_("Empty character constant."));
1204 yylval.typed_val_int.val = c;
1205 yylval.typed_val_int.type = parse_type->builtin_char;
1210 namelen = skip_quoted (tokstart) - tokstart;
1213 lexptr = tokstart + namelen;
1214 if (lexptr[-1] != '\'')
1215 error (_("Unmatched single quote."));
1218 uptokstart = uptok(tokstart,namelen);
1221 error (_("Invalid character constant."));
1231 if (paren_depth == 0)
1238 if (comma_terminates && paren_depth == 0)
1244 /* Might be a floating point number. */
1245 if (lexptr[1] < '0' || lexptr[1] > '9')
1247 if (parse_completion)
1248 last_was_structop = 1;
1249 goto symbol; /* Nope, must be a symbol. */
1252 /* FALL THRU into number case. */
1265 /* It's a number. */
1266 int got_dot = 0, got_e = 0, toktype;
1268 int hex = input_radix > 10;
1270 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1275 else if (c == '0' && (p[1]=='t' || p[1]=='T'
1276 || p[1]=='d' || p[1]=='D'))
1284 /* This test includes !hex because 'e' is a valid hex digit
1285 and thus does not indicate a floating point number when
1286 the radix is hex. */
1287 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1288 got_dot = got_e = 1;
1289 /* This test does not include !hex, because a '.' always indicates
1290 a decimal floating point number regardless of the radix. */
1291 else if (!got_dot && *p == '.')
1293 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1294 && (*p == '-' || *p == '+'))
1295 /* This is the sign of the exponent, not the end of the
1298 /* We will take any letters or digits. parse_number will
1299 complain if past the radix, or if L or U are not final. */
1300 else if ((*p < '0' || *p > '9')
1301 && ((*p < 'a' || *p > 'z')
1302 && (*p < 'A' || *p > 'Z')))
1305 toktype = parse_number (tokstart,
1306 p - tokstart, got_dot | got_e, &yylval);
1307 if (toktype == ERROR)
1309 char *err_copy = (char *) alloca (p - tokstart + 1);
1311 memcpy (err_copy, tokstart, p - tokstart);
1312 err_copy[p - tokstart] = 0;
1313 error (_("Invalid number \"%s\"."), err_copy);
1344 /* Build the gdb internal form of the input string in tempbuf,
1345 translating any standard C escape forms seen. Note that the
1346 buffer is null byte terminated *only* for the convenience of
1347 debugging gdb itself and printing the buffer contents when
1348 the buffer contains no embedded nulls. Gdb does not depend
1349 upon the buffer being null byte terminated, it uses the length
1350 string instead. This allows gdb to handle C strings (as well
1351 as strings in other languages) with embedded null bytes. */
1353 tokptr = ++tokstart;
1357 /* Grow the static temp buffer if necessary, including allocating
1358 the first one on demand. */
1359 if (tempbufindex + 1 >= tempbufsize)
1361 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1368 /* Do nothing, loop will terminate. */
1375 c = parse_escape (parse_gdbarch, &s);
1381 tempbuf[tempbufindex++] = c;
1385 tempbuf[tempbufindex++] = *tokptr++;
1388 } while ((*tokptr != '"') && (*tokptr != '\0'));
1389 if (*tokptr++ != '"')
1391 error (_("Unterminated string in expression."));
1393 tempbuf[tempbufindex] = '\0'; /* See note above. */
1394 yylval.sval.ptr = tempbuf;
1395 yylval.sval.length = tempbufindex;
1400 if (!(c == '_' || c == '$'
1401 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1402 /* We must have come across a bad character (e.g. ';'). */
1403 error (_("Invalid character '%c' in expression."), c);
1405 /* It's a name. See how long it is. */
1407 for (c = tokstart[namelen];
1408 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1409 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1411 /* Template parameter lists are part of the name.
1412 FIXME: This mishandles `print $a<4&&$a>3'. */
1416 int nesting_level = 1;
1417 while (tokstart[++i])
1419 if (tokstart[i] == '<')
1421 else if (tokstart[i] == '>')
1423 if (--nesting_level == 0)
1427 if (tokstart[i] == '>')
1433 /* do NOT uppercase internals because of registers !!! */
1434 c = tokstart[++namelen];
1437 uptokstart = uptok(tokstart,namelen);
1439 /* The token "if" terminates the expression and is NOT
1440 removed from the input stream. */
1441 if (namelen == 2 && uptokstart[0] == 'I' && uptokstart[1] == 'F')
1451 /* Catch specific keywords. Should be done with a data structure. */
1455 if (strcmp (uptokstart, "OBJECT") == 0)
1460 if (strcmp (uptokstart, "RECORD") == 0)
1465 if (strcmp (uptokstart, "SIZEOF") == 0)
1472 if (strcmp (uptokstart, "CLASS") == 0)
1477 if (strcmp (uptokstart, "FALSE") == 0)
1481 return FALSEKEYWORD;
1485 if (strcmp (uptokstart, "TRUE") == 0)
1491 if (strcmp (uptokstart, "SELF") == 0)
1493 /* Here we search for 'this' like
1494 inserted in FPC stabs debug info. */
1495 static const char this_name[] = "this";
1497 if (lookup_symbol (this_name, expression_context_block,
1509 yylval.sval.ptr = tokstart;
1510 yylval.sval.length = namelen;
1512 if (*tokstart == '$')
1515 /* $ is the normal prefix for pascal hexadecimal values
1516 but this conflicts with the GDB use for debugger variables
1517 so in expression to enter hexadecimal values
1518 we still need to use C syntax with 0xff */
1519 write_dollar_variable (yylval.sval);
1520 c = tokstart[namelen];
1521 tokstart[namelen] = 0;
1522 intvar = lookup_only_internalvar (++tokstart);
1524 tokstart[namelen] = c;
1529 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1530 functions or symtabs. If this is not so, then ...
1531 Use token-type TYPENAME for symbols that happen to be defined
1532 currently as names of types; NAME for other symbols.
1533 The caller is not constrained to care about the distinction. */
1535 char *tmp = copy_name (yylval.sval);
1537 struct field_of_this_result is_a_field_of_this;
1542 if (search_field && current_type)
1543 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1544 if (is_a_field || parse_completion)
1547 sym = lookup_symbol (tmp, expression_context_block,
1548 VAR_DOMAIN, &is_a_field_of_this);
1549 /* second chance uppercased (as Free Pascal does). */
1550 if (!sym && is_a_field_of_this.type == NULL && !is_a_field)
1552 for (i = 0; i <= namelen; i++)
1554 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1555 tmp[i] -= ('a'-'A');
1557 if (search_field && current_type)
1558 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1559 if (is_a_field || parse_completion)
1562 sym = lookup_symbol (tmp, expression_context_block,
1563 VAR_DOMAIN, &is_a_field_of_this);
1564 if (sym || is_a_field_of_this.type != NULL || is_a_field)
1565 for (i = 0; i <= namelen; i++)
1567 if ((tokstart[i] >= 'a' && tokstart[i] <= 'z'))
1568 tokstart[i] -= ('a'-'A');
1571 /* Third chance Capitalized (as GPC does). */
1572 if (!sym && is_a_field_of_this.type == NULL && !is_a_field)
1574 for (i = 0; i <= namelen; i++)
1578 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1579 tmp[i] -= ('a'-'A');
1582 if ((tmp[i] >= 'A' && tmp[i] <= 'Z'))
1583 tmp[i] -= ('A'-'a');
1585 if (search_field && current_type)
1586 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1587 if (is_a_field || parse_completion)
1590 sym = lookup_symbol (tmp, expression_context_block,
1591 VAR_DOMAIN, &is_a_field_of_this);
1592 if (sym || is_a_field_of_this.type != NULL || is_a_field)
1593 for (i = 0; i <= namelen; i++)
1597 if ((tokstart[i] >= 'a' && tokstart[i] <= 'z'))
1598 tokstart[i] -= ('a'-'A');
1601 if ((tokstart[i] >= 'A' && tokstart[i] <= 'Z'))
1602 tokstart[i] -= ('A'-'a');
1608 tempbuf = (char *) realloc (tempbuf, namelen + 1);
1609 strncpy (tempbuf, tokstart, namelen); tempbuf [namelen] = 0;
1610 yylval.sval.ptr = tempbuf;
1611 yylval.sval.length = namelen;
1615 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1616 no psymtabs (coff, xcoff, or some future change to blow away the
1617 psymtabs once once symbols are read). */
1618 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1619 || lookup_symtab (tmp))
1621 yylval.ssym.sym = sym;
1622 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1626 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1629 /* Despite the following flaw, we need to keep this code enabled.
1630 Because we can get called from check_stub_method, if we don't
1631 handle nested types then it screws many operations in any
1632 program which uses nested types. */
1633 /* In "A::x", if x is a member function of A and there happens
1634 to be a type (nested or not, since the stabs don't make that
1635 distinction) named x, then this code incorrectly thinks we
1636 are dealing with nested types rather than a member function. */
1639 const char *namestart;
1640 struct symbol *best_sym;
1642 /* Look ahead to detect nested types. This probably should be
1643 done in the grammar, but trying seemed to introduce a lot
1644 of shift/reduce and reduce/reduce conflicts. It's possible
1645 that it could be done, though. Or perhaps a non-grammar, but
1646 less ad hoc, approach would work well. */
1648 /* Since we do not currently have any way of distinguishing
1649 a nested type from a non-nested one (the stabs don't tell
1650 us whether a type is nested), we just ignore the
1657 /* Skip whitespace. */
1658 while (*p == ' ' || *p == '\t' || *p == '\n')
1660 if (*p == ':' && p[1] == ':')
1662 /* Skip the `::'. */
1664 /* Skip whitespace. */
1665 while (*p == ' ' || *p == '\t' || *p == '\n')
1668 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1669 || (*p >= 'a' && *p <= 'z')
1670 || (*p >= 'A' && *p <= 'Z'))
1674 struct symbol *cur_sym;
1675 /* As big as the whole rest of the expression, which is
1676 at least big enough. */
1677 char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1681 memcpy (tmp1, tmp, strlen (tmp));
1682 tmp1 += strlen (tmp);
1683 memcpy (tmp1, "::", 2);
1685 memcpy (tmp1, namestart, p - namestart);
1686 tmp1[p - namestart] = '\0';
1687 cur_sym = lookup_symbol (ncopy, expression_context_block,
1691 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1709 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1711 yylval.tsym.type = SYMBOL_TYPE (sym);
1717 = language_lookup_primitive_type_by_name (parse_language,
1718 parse_gdbarch, tmp);
1719 if (yylval.tsym.type != NULL)
1725 /* Input names that aren't symbols but ARE valid hex numbers,
1726 when the input radix permits them, can be names or numbers
1727 depending on the parse. Note we support radixes > 16 here. */
1729 && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
1730 || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1732 YYSTYPE newlval; /* Its value is ignored. */
1733 hextype = parse_number (tokstart, namelen, 0, &newlval);
1736 yylval.ssym.sym = sym;
1737 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1744 /* Any other kind of symbol. */
1745 yylval.ssym.sym = sym;
1746 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1755 lexptr = prev_lexptr;
1757 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);