1 /* YACC parser for Pascal expressions, for GDB.
2 Copyright (C) 2000, 2006-2012 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. */
59 #define parse_type builtin_type (parse_gdbarch)
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);
119 void yyerror (char *);
121 static char * uptok (char *, int);
124 /* Although the yacc "value" of an expression is not used,
125 since the result is stored in the structure being created,
126 other node types do have values. */
143 struct symtoken ssym;
146 enum exp_opcode opcode;
147 struct internalvar *ivar;
154 /* YYSTYPE gets defined by %union */
155 static int parse_number (char *, int, int, YYSTYPE *);
157 static struct type *current_type;
158 static struct internalvar *intvar;
159 static int leftdiv_is_integer;
160 static void push_current_type (void);
161 static void pop_current_type (void);
162 static int search_field;
165 %type <voidval> exp exp1 type_exp start normal_start variable qualified_name
166 %type <tval> type typebase
167 /* %type <bval> block */
169 /* Fancy type parsing. */
172 %token <typed_val_int> INT
173 %token <typed_val_float> FLOAT
175 /* Both NAME and TYPENAME tokens represent symbols in the input,
176 and both convey their data as strings.
177 But a TYPENAME is a string that happens to be defined as a typedef
178 or builtin type name (such as int or char)
179 and a NAME is any other symbol.
180 Contexts where this distinction is not important can use the
181 nonterminal "name", which matches either NAME or TYPENAME. */
184 %token <sval> FIELDNAME
185 %token <voidval> COMPLETE
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;
237 leftdiv_is_integer = 0;
248 { write_exp_elt_opcode(OP_TYPE);
249 write_exp_elt_type($1);
250 write_exp_elt_opcode(OP_TYPE);
251 current_type = $1; } ;
253 /* Expressions, including the comma operator. */
256 { write_exp_elt_opcode (BINOP_COMMA); }
259 /* Expressions, not including the comma operator. */
260 exp : exp '^' %prec UNARY
261 { write_exp_elt_opcode (UNOP_IND);
263 current_type = TYPE_TARGET_TYPE (current_type); }
266 exp : '@' exp %prec UNARY
267 { write_exp_elt_opcode (UNOP_ADDR);
269 current_type = TYPE_POINTER_TYPE (current_type); }
272 exp : '-' exp %prec UNARY
273 { write_exp_elt_opcode (UNOP_NEG); }
276 exp : NOT exp %prec UNARY
277 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
280 exp : INCREMENT '(' exp ')' %prec UNARY
281 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
284 exp : DECREMENT '(' exp ')' %prec UNARY
285 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
289 field_exp : exp '.' %prec UNARY
290 { search_field = 1; }
293 exp : field_exp FIELDNAME
294 { write_exp_elt_opcode (STRUCTOP_STRUCT);
295 write_exp_string ($2);
296 write_exp_elt_opcode (STRUCTOP_STRUCT);
300 while (TYPE_CODE (current_type)
303 TYPE_TARGET_TYPE (current_type);
304 current_type = lookup_struct_elt_type (
305 current_type, $2.ptr, 0);
312 { mark_struct_expression ();
313 write_exp_elt_opcode (STRUCTOP_STRUCT);
314 write_exp_string ($2);
315 write_exp_elt_opcode (STRUCTOP_STRUCT);
319 while (TYPE_CODE (current_type)
322 TYPE_TARGET_TYPE (current_type);
323 current_type = lookup_struct_elt_type (
324 current_type, $2.ptr, 0);
329 exp : field_exp COMPLETE
331 mark_struct_expression ();
332 write_exp_elt_opcode (STRUCTOP_STRUCT);
335 write_exp_string (s);
336 write_exp_elt_opcode (STRUCTOP_STRUCT); }
340 /* We need to save the current_type value. */
343 arrayfieldindex = is_pascal_string_type (
344 current_type, NULL, NULL,
345 NULL, NULL, &arrayname);
348 struct stoken stringsval;
349 stringsval.ptr = alloca (strlen (arrayname) + 1);
350 stringsval.length = strlen (arrayname);
351 strcpy (stringsval.ptr, arrayname);
352 current_type = TYPE_FIELD_TYPE (current_type,
353 arrayfieldindex - 1);
354 write_exp_elt_opcode (STRUCTOP_STRUCT);
355 write_exp_string (stringsval);
356 write_exp_elt_opcode (STRUCTOP_STRUCT);
358 push_current_type (); }
360 { pop_current_type ();
361 write_exp_elt_opcode (BINOP_SUBSCRIPT);
363 current_type = TYPE_TARGET_TYPE (current_type); }
367 /* This is to save the value of arglist_len
368 being accumulated by an outer function call. */
369 { push_current_type ();
371 arglist ')' %prec ARROW
372 { write_exp_elt_opcode (OP_FUNCALL);
373 write_exp_elt_longcst ((LONGEST) end_arglist ());
374 write_exp_elt_opcode (OP_FUNCALL);
377 current_type = TYPE_TARGET_TYPE (current_type);
384 | arglist ',' exp %prec ABOVE_COMMA
388 exp : type '(' exp ')' %prec UNARY
391 /* Allow automatic dereference of classes. */
392 if ((TYPE_CODE (current_type) == TYPE_CODE_PTR)
393 && (TYPE_CODE (TYPE_TARGET_TYPE (current_type)) == TYPE_CODE_CLASS)
394 && (TYPE_CODE ($1) == TYPE_CODE_CLASS))
395 write_exp_elt_opcode (UNOP_IND);
397 write_exp_elt_opcode (UNOP_CAST);
398 write_exp_elt_type ($1);
399 write_exp_elt_opcode (UNOP_CAST);
407 /* Binary operators in order of decreasing precedence. */
410 { write_exp_elt_opcode (BINOP_MUL); }
414 if (current_type && is_integral_type (current_type))
415 leftdiv_is_integer = 1;
419 if (leftdiv_is_integer && current_type
420 && is_integral_type (current_type))
422 write_exp_elt_opcode (UNOP_CAST);
423 write_exp_elt_type (parse_type->builtin_long_double);
424 current_type = parse_type->builtin_long_double;
425 write_exp_elt_opcode (UNOP_CAST);
426 leftdiv_is_integer = 0;
429 write_exp_elt_opcode (BINOP_DIV);
434 { write_exp_elt_opcode (BINOP_INTDIV); }
438 { write_exp_elt_opcode (BINOP_REM); }
442 { write_exp_elt_opcode (BINOP_ADD); }
446 { write_exp_elt_opcode (BINOP_SUB); }
450 { write_exp_elt_opcode (BINOP_LSH); }
454 { write_exp_elt_opcode (BINOP_RSH); }
458 { write_exp_elt_opcode (BINOP_EQUAL);
459 current_type = parse_type->builtin_bool;
463 exp : exp NOTEQUAL exp
464 { write_exp_elt_opcode (BINOP_NOTEQUAL);
465 current_type = parse_type->builtin_bool;
470 { write_exp_elt_opcode (BINOP_LEQ);
471 current_type = parse_type->builtin_bool;
476 { write_exp_elt_opcode (BINOP_GEQ);
477 current_type = parse_type->builtin_bool;
482 { write_exp_elt_opcode (BINOP_LESS);
483 current_type = parse_type->builtin_bool;
488 { write_exp_elt_opcode (BINOP_GTR);
489 current_type = parse_type->builtin_bool;
494 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
498 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
502 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
506 { write_exp_elt_opcode (BINOP_ASSIGN); }
510 { write_exp_elt_opcode (OP_BOOL);
511 write_exp_elt_longcst ((LONGEST) $1);
512 current_type = parse_type->builtin_bool;
513 write_exp_elt_opcode (OP_BOOL); }
517 { write_exp_elt_opcode (OP_BOOL);
518 write_exp_elt_longcst ((LONGEST) $1);
519 current_type = parse_type->builtin_bool;
520 write_exp_elt_opcode (OP_BOOL); }
524 { write_exp_elt_opcode (OP_LONG);
525 write_exp_elt_type ($1.type);
526 current_type = $1.type;
527 write_exp_elt_longcst ((LONGEST)($1.val));
528 write_exp_elt_opcode (OP_LONG); }
533 parse_number ($1.stoken.ptr,
534 $1.stoken.length, 0, &val);
535 write_exp_elt_opcode (OP_LONG);
536 write_exp_elt_type (val.typed_val_int.type);
537 current_type = val.typed_val_int.type;
538 write_exp_elt_longcst ((LONGEST)
539 val.typed_val_int.val);
540 write_exp_elt_opcode (OP_LONG);
546 { write_exp_elt_opcode (OP_DOUBLE);
547 write_exp_elt_type ($1.type);
548 current_type = $1.type;
549 write_exp_elt_dblcst ($1.dval);
550 write_exp_elt_opcode (OP_DOUBLE); }
557 /* Already written by write_dollar_variable.
558 Handle current_type. */
560 struct value * val, * mark;
562 mark = value_mark ();
563 val = value_of_internalvar (parse_gdbarch,
565 current_type = value_type (val);
566 value_release_to_mark (mark);
571 exp : SIZEOF '(' type ')' %prec UNARY
572 { write_exp_elt_opcode (OP_LONG);
573 write_exp_elt_type (parse_type->builtin_int);
575 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
576 write_exp_elt_opcode (OP_LONG); }
579 exp : SIZEOF '(' exp ')' %prec UNARY
580 { write_exp_elt_opcode (UNOP_SIZEOF); }
583 { /* C strings are converted into array constants with
584 an explicit null byte added at the end. Thus
585 the array upper bound is the string length.
586 There is no such thing in C as a completely empty
588 char *sp = $1.ptr; int count = $1.length;
591 write_exp_elt_opcode (OP_LONG);
592 write_exp_elt_type (parse_type->builtin_char);
593 write_exp_elt_longcst ((LONGEST)(*sp++));
594 write_exp_elt_opcode (OP_LONG);
596 write_exp_elt_opcode (OP_LONG);
597 write_exp_elt_type (parse_type->builtin_char);
598 write_exp_elt_longcst ((LONGEST)'\0');
599 write_exp_elt_opcode (OP_LONG);
600 write_exp_elt_opcode (OP_ARRAY);
601 write_exp_elt_longcst ((LONGEST) 0);
602 write_exp_elt_longcst ((LONGEST) ($1.length));
603 write_exp_elt_opcode (OP_ARRAY); }
609 struct value * this_val;
610 struct type * this_type;
611 write_exp_elt_opcode (OP_THIS);
612 write_exp_elt_opcode (OP_THIS);
613 /* We need type of this. */
614 this_val = value_of_this_silent (parse_language);
616 this_type = value_type (this_val);
621 if (TYPE_CODE (this_type) == TYPE_CODE_PTR)
623 this_type = TYPE_TARGET_TYPE (this_type);
624 write_exp_elt_opcode (UNOP_IND);
628 current_type = this_type;
632 /* end of object pascal. */
637 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
641 lookup_symtab (copy_name ($1.stoken));
643 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem),
646 error (_("No file or function \"%s\"."),
647 copy_name ($1.stoken));
652 block : block COLONCOLON name
654 = lookup_symbol (copy_name ($3), $1,
655 VAR_DOMAIN, (int *) NULL);
656 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
657 error (_("No function \"%s\" in specified context."),
659 $$ = SYMBOL_BLOCK_VALUE (tem); }
662 variable: block COLONCOLON name
663 { struct symbol *sym;
664 sym = lookup_symbol (copy_name ($3), $1,
665 VAR_DOMAIN, (int *) NULL);
667 error (_("No symbol \"%s\" in specified context."),
670 write_exp_elt_opcode (OP_VAR_VALUE);
671 /* block_found is set by lookup_symbol. */
672 write_exp_elt_block (block_found);
673 write_exp_elt_sym (sym);
674 write_exp_elt_opcode (OP_VAR_VALUE); }
677 qualified_name: typebase COLONCOLON name
679 struct type *type = $1;
680 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
681 && TYPE_CODE (type) != TYPE_CODE_UNION)
682 error (_("`%s' is not defined as an aggregate type."),
685 write_exp_elt_opcode (OP_SCOPE);
686 write_exp_elt_type (type);
687 write_exp_string ($3);
688 write_exp_elt_opcode (OP_SCOPE);
692 variable: qualified_name
695 char *name = copy_name ($2);
697 struct minimal_symbol *msymbol;
700 lookup_symbol (name, (const struct block *) NULL,
701 VAR_DOMAIN, (int *) NULL);
704 write_exp_elt_opcode (OP_VAR_VALUE);
705 write_exp_elt_block (NULL);
706 write_exp_elt_sym (sym);
707 write_exp_elt_opcode (OP_VAR_VALUE);
711 msymbol = lookup_minimal_symbol (name, NULL, NULL);
713 write_exp_msymbol (msymbol);
714 else if (!have_full_symbols ()
715 && !have_partial_symbols ())
716 error (_("No symbol table is loaded. "
717 "Use the \"file\" command."));
719 error (_("No symbol \"%s\" in current context."),
724 variable: name_not_typename
725 { struct symbol *sym = $1.sym;
729 if (symbol_read_needs_frame (sym))
731 if (innermost_block == 0
732 || contained_in (block_found,
734 innermost_block = block_found;
737 write_exp_elt_opcode (OP_VAR_VALUE);
738 /* We want to use the selected frame, not
739 another more inner frame which happens to
740 be in the same block. */
741 write_exp_elt_block (NULL);
742 write_exp_elt_sym (sym);
743 write_exp_elt_opcode (OP_VAR_VALUE);
744 current_type = sym->type; }
745 else if ($1.is_a_field_of_this)
747 struct value * this_val;
748 struct type * this_type;
749 /* Object pascal: it hangs off of `this'. Must
750 not inadvertently convert from a method call
752 if (innermost_block == 0
753 || contained_in (block_found,
755 innermost_block = block_found;
756 write_exp_elt_opcode (OP_THIS);
757 write_exp_elt_opcode (OP_THIS);
758 write_exp_elt_opcode (STRUCTOP_PTR);
759 write_exp_string ($1.stoken);
760 write_exp_elt_opcode (STRUCTOP_PTR);
761 /* We need type of this. */
762 this_val = value_of_this_silent (parse_language);
764 this_type = value_type (this_val);
768 current_type = lookup_struct_elt_type (
770 copy_name ($1.stoken), 0);
776 struct minimal_symbol *msymbol;
777 char *arg = copy_name ($1.stoken);
780 lookup_minimal_symbol (arg, NULL, NULL);
782 write_exp_msymbol (msymbol);
783 else if (!have_full_symbols ()
784 && !have_partial_symbols ())
785 error (_("No symbol table is loaded. "
786 "Use the \"file\" command."));
788 error (_("No symbol \"%s\" in current context."),
789 copy_name ($1.stoken));
798 /* We used to try to recognize more pointer to member types here, but
799 that didn't work (shift/reduce conflicts meant that these rules never
800 got executed). The problem is that
801 int (foo::bar::baz::bizzle)
802 is a function type but
803 int (foo::bar::baz::bizzle::*)
804 is a pointer to member type. Stroustrup loses again! */
809 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
811 { $$ = lookup_pointer_type ($2); }
815 { $$ = lookup_struct (copy_name ($2),
816 expression_context_block); }
818 { $$ = lookup_struct (copy_name ($2),
819 expression_context_block); }
820 /* "const" and "volatile" are curently ignored. A type qualifier
821 after the type is handled in the ptype rule. I think these could
825 name : NAME { $$ = $1.stoken; }
826 | BLOCKNAME { $$ = $1.stoken; }
827 | TYPENAME { $$ = $1.stoken; }
828 | NAME_OR_INT { $$ = $1.stoken; }
831 name_not_typename : NAME
833 /* These would be useful if name_not_typename was useful, but it is just
834 a fake for "variable", so these cause reduce/reduce conflicts because
835 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
836 =exp) or just an exp. If name_not_typename was ever used in an lvalue
837 context where only a name could occur, this might be useful.
844 /* Take care of parsing a number (anything that starts with a digit).
845 Set yylval and return the token type; update lexptr.
846 LEN is the number of characters in it. */
848 /*** Needs some error checking for the float case ***/
851 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
853 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
854 here, and we do kind of silly things like cast to unsigned. */
861 int base = input_radix;
864 /* Number of "L" suffixes encountered. */
867 /* We have found a "L" or "U" suffix. */
868 int found_suffix = 0;
871 struct type *signed_type;
872 struct type *unsigned_type;
876 if (! parse_c_float (parse_gdbarch, p, len,
877 &putithere->typed_val_float.dval,
878 &putithere->typed_val_float.type))
883 /* Handle base-switching prefixes 0x, 0t, 0d, 0. */
917 if (c >= 'A' && c <= 'Z')
919 if (c != 'l' && c != 'u')
921 if (c >= '0' && c <= '9')
929 if (base > 10 && c >= 'a' && c <= 'f')
933 n += i = c - 'a' + 10;
946 return ERROR; /* Char not a digit */
949 return ERROR; /* Invalid digit in this base. */
951 /* Portably test for overflow (only works for nonzero values, so make
952 a second check for zero). FIXME: Can't we just make n and prevn
953 unsigned and avoid this? */
954 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
955 unsigned_p = 1; /* Try something unsigned. */
957 /* Portably test for unsigned overflow.
958 FIXME: This check is wrong; for example it doesn't find overflow
959 on 0x123456789 when LONGEST is 32 bits. */
960 if (c != 'l' && c != 'u' && n != 0)
962 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
963 error (_("Numeric constant too large."));
968 /* An integer constant is an int, a long, or a long long. An L
969 suffix forces it to be long; an LL suffix forces it to be long
970 long. If not forced to a larger size, it gets the first type of
971 the above that it fits in. To figure out whether it fits, we
972 shift it right and see whether anything remains. Note that we
973 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
974 operation, because many compilers will warn about such a shift
975 (which always produces a zero result). Sometimes gdbarch_int_bit
976 or gdbarch_long_bit will be that big, sometimes not. To deal with
977 the case where it is we just always shift the value more than
978 once, with fewer bits each time. */
980 un = (ULONGEST)n >> 2;
982 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
984 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
986 /* A large decimal (not hex or octal) constant (between INT_MAX
987 and UINT_MAX) is a long or unsigned long, according to ANSI,
988 never an unsigned int, but this code treats it as unsigned
989 int. This probably should be fixed. GCC gives a warning on
992 unsigned_type = parse_type->builtin_unsigned_int;
993 signed_type = parse_type->builtin_int;
996 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
998 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
999 unsigned_type = parse_type->builtin_unsigned_long;
1000 signed_type = parse_type->builtin_long;
1005 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1006 < gdbarch_long_long_bit (parse_gdbarch))
1007 /* A long long does not fit in a LONGEST. */
1008 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1010 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1011 high_bit = (ULONGEST) 1 << shift;
1012 unsigned_type = parse_type->builtin_unsigned_long_long;
1013 signed_type = parse_type->builtin_long_long;
1016 putithere->typed_val_int.val = n;
1018 /* If the high bit of the worked out type is set then this number
1019 has to be unsigned. */
1021 if (unsigned_p || (n & high_bit))
1023 putithere->typed_val_int.type = unsigned_type;
1027 putithere->typed_val_int.type = signed_type;
1036 struct type *stored;
1037 struct type_push *next;
1040 static struct type_push *tp_top = NULL;
1043 push_current_type (void)
1045 struct type_push *tpnew;
1046 tpnew = (struct type_push *) malloc (sizeof (struct type_push));
1047 tpnew->next = tp_top;
1048 tpnew->stored = current_type;
1049 current_type = NULL;
1054 pop_current_type (void)
1056 struct type_push *tp = tp_top;
1059 current_type = tp->stored;
1069 enum exp_opcode opcode;
1072 static const struct token tokentab3[] =
1074 {"shr", RSH, BINOP_END},
1075 {"shl", LSH, BINOP_END},
1076 {"and", ANDAND, BINOP_END},
1077 {"div", DIV, BINOP_END},
1078 {"not", NOT, BINOP_END},
1079 {"mod", MOD, BINOP_END},
1080 {"inc", INCREMENT, BINOP_END},
1081 {"dec", DECREMENT, BINOP_END},
1082 {"xor", XOR, BINOP_END}
1085 static const struct token tokentab2[] =
1087 {"or", OR, BINOP_END},
1088 {"<>", NOTEQUAL, BINOP_END},
1089 {"<=", LEQ, BINOP_END},
1090 {">=", GEQ, BINOP_END},
1091 {":=", ASSIGN, BINOP_END},
1092 {"::", COLONCOLON, BINOP_END} };
1094 /* Allocate uppercased var: */
1095 /* make an uppercased copy of tokstart. */
1096 static char * uptok (tokstart, namelen)
1101 char *uptokstart = (char *)malloc(namelen+1);
1102 for (i = 0;i <= namelen;i++)
1104 if ((tokstart[i]>='a' && tokstart[i]<='z'))
1105 uptokstart[i] = tokstart[i]-('a'-'A');
1107 uptokstart[i] = tokstart[i];
1109 uptokstart[namelen]='\0';
1113 /* This is set if the previously-returned token was a structure
1114 operator '.'. This is used only when parsing to
1115 do field name completion. */
1116 static int last_was_structop;
1118 /* Read one token, getting characters through lexptr. */
1129 int explen, tempbufindex;
1130 static char *tempbuf;
1131 static int tempbufsize;
1132 int saw_structop = last_was_structop;
1134 last_was_structop = 0;
1137 prev_lexptr = lexptr;
1140 explen = strlen (lexptr);
1141 /* See if it is a special token of length 3. */
1143 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1144 if (strncasecmp (tokstart, tokentab3[i].operator, 3) == 0
1145 && (!isalpha (tokentab3[i].operator[0]) || explen == 3
1146 || (!isalpha (tokstart[3])
1147 && !isdigit (tokstart[3]) && tokstart[3] != '_')))
1150 yylval.opcode = tokentab3[i].opcode;
1151 return tokentab3[i].token;
1154 /* See if it is a special token of length 2. */
1156 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1157 if (strncasecmp (tokstart, tokentab2[i].operator, 2) == 0
1158 && (!isalpha (tokentab2[i].operator[0]) || explen == 2
1159 || (!isalpha (tokstart[2])
1160 && !isdigit (tokstart[2]) && tokstart[2] != '_')))
1163 yylval.opcode = tokentab2[i].opcode;
1164 return tokentab2[i].token;
1167 switch (c = *tokstart)
1170 if (saw_structop && search_field)
1182 /* We either have a character constant ('0' or '\177' for example)
1183 or we have a quoted symbol reference ('foo(int,int)' in object pascal
1188 c = parse_escape (parse_gdbarch, &lexptr);
1190 error (_("Empty character constant."));
1192 yylval.typed_val_int.val = c;
1193 yylval.typed_val_int.type = parse_type->builtin_char;
1198 namelen = skip_quoted (tokstart) - tokstart;
1201 lexptr = tokstart + namelen;
1202 if (lexptr[-1] != '\'')
1203 error (_("Unmatched single quote."));
1206 uptokstart = uptok(tokstart,namelen);
1209 error (_("Invalid character constant."));
1219 if (paren_depth == 0)
1226 if (comma_terminates && paren_depth == 0)
1232 /* Might be a floating point number. */
1233 if (lexptr[1] < '0' || lexptr[1] > '9')
1236 last_was_structop = 1;
1237 goto symbol; /* Nope, must be a symbol. */
1240 /* FALL THRU into number case. */
1253 /* It's a number. */
1254 int got_dot = 0, got_e = 0, toktype;
1256 int hex = input_radix > 10;
1258 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1263 else if (c == '0' && (p[1]=='t' || p[1]=='T'
1264 || p[1]=='d' || p[1]=='D'))
1272 /* This test includes !hex because 'e' is a valid hex digit
1273 and thus does not indicate a floating point number when
1274 the radix is hex. */
1275 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1276 got_dot = got_e = 1;
1277 /* This test does not include !hex, because a '.' always indicates
1278 a decimal floating point number regardless of the radix. */
1279 else if (!got_dot && *p == '.')
1281 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1282 && (*p == '-' || *p == '+'))
1283 /* This is the sign of the exponent, not the end of the
1286 /* We will take any letters or digits. parse_number will
1287 complain if past the radix, or if L or U are not final. */
1288 else if ((*p < '0' || *p > '9')
1289 && ((*p < 'a' || *p > 'z')
1290 && (*p < 'A' || *p > 'Z')))
1293 toktype = parse_number (tokstart,
1294 p - tokstart, got_dot | got_e, &yylval);
1295 if (toktype == ERROR)
1297 char *err_copy = (char *) alloca (p - tokstart + 1);
1299 memcpy (err_copy, tokstart, p - tokstart);
1300 err_copy[p - tokstart] = 0;
1301 error (_("Invalid number \"%s\"."), err_copy);
1332 /* Build the gdb internal form of the input string in tempbuf,
1333 translating any standard C escape forms seen. Note that the
1334 buffer is null byte terminated *only* for the convenience of
1335 debugging gdb itself and printing the buffer contents when
1336 the buffer contains no embedded nulls. Gdb does not depend
1337 upon the buffer being null byte terminated, it uses the length
1338 string instead. This allows gdb to handle C strings (as well
1339 as strings in other languages) with embedded null bytes. */
1341 tokptr = ++tokstart;
1345 /* Grow the static temp buffer if necessary, including allocating
1346 the first one on demand. */
1347 if (tempbufindex + 1 >= tempbufsize)
1349 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1356 /* Do nothing, loop will terminate. */
1360 c = parse_escape (parse_gdbarch, &tokptr);
1365 tempbuf[tempbufindex++] = c;
1368 tempbuf[tempbufindex++] = *tokptr++;
1371 } while ((*tokptr != '"') && (*tokptr != '\0'));
1372 if (*tokptr++ != '"')
1374 error (_("Unterminated string in expression."));
1376 tempbuf[tempbufindex] = '\0'; /* See note above. */
1377 yylval.sval.ptr = tempbuf;
1378 yylval.sval.length = tempbufindex;
1383 if (!(c == '_' || c == '$'
1384 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1385 /* We must have come across a bad character (e.g. ';'). */
1386 error (_("Invalid character '%c' in expression."), c);
1388 /* It's a name. See how long it is. */
1390 for (c = tokstart[namelen];
1391 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1392 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1394 /* Template parameter lists are part of the name.
1395 FIXME: This mishandles `print $a<4&&$a>3'. */
1399 int nesting_level = 1;
1400 while (tokstart[++i])
1402 if (tokstart[i] == '<')
1404 else if (tokstart[i] == '>')
1406 if (--nesting_level == 0)
1410 if (tokstart[i] == '>')
1416 /* do NOT uppercase internals because of registers !!! */
1417 c = tokstart[++namelen];
1420 uptokstart = uptok(tokstart,namelen);
1422 /* The token "if" terminates the expression and is NOT
1423 removed from the input stream. */
1424 if (namelen == 2 && uptokstart[0] == 'I' && uptokstart[1] == 'F')
1434 /* Catch specific keywords. Should be done with a data structure. */
1438 if (strcmp (uptokstart, "OBJECT") == 0)
1443 if (strcmp (uptokstart, "RECORD") == 0)
1448 if (strcmp (uptokstart, "SIZEOF") == 0)
1455 if (strcmp (uptokstart, "CLASS") == 0)
1460 if (strcmp (uptokstart, "FALSE") == 0)
1464 return FALSEKEYWORD;
1468 if (strcmp (uptokstart, "TRUE") == 0)
1474 if (strcmp (uptokstart, "SELF") == 0)
1476 /* Here we search for 'this' like
1477 inserted in FPC stabs debug info. */
1478 static const char this_name[] = "this";
1480 if (lookup_symbol (this_name, expression_context_block,
1481 VAR_DOMAIN, (int *) NULL))
1492 yylval.sval.ptr = tokstart;
1493 yylval.sval.length = namelen;
1495 if (*tokstart == '$')
1498 /* $ is the normal prefix for pascal hexadecimal values
1499 but this conflicts with the GDB use for debugger variables
1500 so in expression to enter hexadecimal values
1501 we still need to use C syntax with 0xff */
1502 write_dollar_variable (yylval.sval);
1503 c = tokstart[namelen];
1504 tokstart[namelen] = 0;
1505 intvar = lookup_only_internalvar (++tokstart);
1507 tokstart[namelen] = c;
1512 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1513 functions or symtabs. If this is not so, then ...
1514 Use token-type TYPENAME for symbols that happen to be defined
1515 currently as names of types; NAME for other symbols.
1516 The caller is not constrained to care about the distinction. */
1518 char *tmp = copy_name (yylval.sval);
1520 int is_a_field_of_this = 0;
1525 if (search_field && current_type)
1526 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1527 if (is_a_field || in_parse_field)
1530 sym = lookup_symbol (tmp, expression_context_block,
1531 VAR_DOMAIN, &is_a_field_of_this);
1532 /* second chance uppercased (as Free Pascal does). */
1533 if (!sym && !is_a_field_of_this && !is_a_field)
1535 for (i = 0; i <= namelen; i++)
1537 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1538 tmp[i] -= ('a'-'A');
1540 if (search_field && current_type)
1541 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1542 if (is_a_field || in_parse_field)
1545 sym = lookup_symbol (tmp, expression_context_block,
1546 VAR_DOMAIN, &is_a_field_of_this);
1547 if (sym || is_a_field_of_this || is_a_field)
1548 for (i = 0; i <= namelen; i++)
1550 if ((tokstart[i] >= 'a' && tokstart[i] <= 'z'))
1551 tokstart[i] -= ('a'-'A');
1554 /* Third chance Capitalized (as GPC does). */
1555 if (!sym && !is_a_field_of_this && !is_a_field)
1557 for (i = 0; i <= namelen; i++)
1561 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1562 tmp[i] -= ('a'-'A');
1565 if ((tmp[i] >= 'A' && tmp[i] <= 'Z'))
1566 tmp[i] -= ('A'-'a');
1568 if (search_field && current_type)
1569 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1570 if (is_a_field || in_parse_field)
1573 sym = lookup_symbol (tmp, expression_context_block,
1574 VAR_DOMAIN, &is_a_field_of_this);
1575 if (sym || is_a_field_of_this || is_a_field)
1576 for (i = 0; i <= namelen; i++)
1580 if ((tokstart[i] >= 'a' && tokstart[i] <= 'z'))
1581 tokstart[i] -= ('a'-'A');
1584 if ((tokstart[i] >= 'A' && tokstart[i] <= 'Z'))
1585 tokstart[i] -= ('A'-'a');
1591 tempbuf = (char *) realloc (tempbuf, namelen + 1);
1592 strncpy (tempbuf, tokstart, namelen); tempbuf [namelen] = 0;
1593 yylval.sval.ptr = tempbuf;
1594 yylval.sval.length = namelen;
1598 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1599 no psymtabs (coff, xcoff, or some future change to blow away the
1600 psymtabs once once symbols are read). */
1601 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1602 || lookup_symtab (tmp))
1604 yylval.ssym.sym = sym;
1605 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1609 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1612 /* Despite the following flaw, we need to keep this code enabled.
1613 Because we can get called from check_stub_method, if we don't
1614 handle nested types then it screws many operations in any
1615 program which uses nested types. */
1616 /* In "A::x", if x is a member function of A and there happens
1617 to be a type (nested or not, since the stabs don't make that
1618 distinction) named x, then this code incorrectly thinks we
1619 are dealing with nested types rather than a member function. */
1623 struct symbol *best_sym;
1625 /* Look ahead to detect nested types. This probably should be
1626 done in the grammar, but trying seemed to introduce a lot
1627 of shift/reduce and reduce/reduce conflicts. It's possible
1628 that it could be done, though. Or perhaps a non-grammar, but
1629 less ad hoc, approach would work well. */
1631 /* Since we do not currently have any way of distinguishing
1632 a nested type from a non-nested one (the stabs don't tell
1633 us whether a type is nested), we just ignore the
1640 /* Skip whitespace. */
1641 while (*p == ' ' || *p == '\t' || *p == '\n')
1643 if (*p == ':' && p[1] == ':')
1645 /* Skip the `::'. */
1647 /* Skip whitespace. */
1648 while (*p == ' ' || *p == '\t' || *p == '\n')
1651 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1652 || (*p >= 'a' && *p <= 'z')
1653 || (*p >= 'A' && *p <= 'Z'))
1657 struct symbol *cur_sym;
1658 /* As big as the whole rest of the expression, which is
1659 at least big enough. */
1660 char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1664 memcpy (tmp1, tmp, strlen (tmp));
1665 tmp1 += strlen (tmp);
1666 memcpy (tmp1, "::", 2);
1668 memcpy (tmp1, namestart, p - namestart);
1669 tmp1[p - namestart] = '\0';
1670 cur_sym = lookup_symbol (ncopy, expression_context_block,
1671 VAR_DOMAIN, (int *) NULL);
1674 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1692 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1694 yylval.tsym.type = SYMBOL_TYPE (sym);
1700 = language_lookup_primitive_type_by_name (parse_language,
1701 parse_gdbarch, tmp);
1702 if (yylval.tsym.type != NULL)
1708 /* Input names that aren't symbols but ARE valid hex numbers,
1709 when the input radix permits them, can be names or numbers
1710 depending on the parse. Note we support radixes > 16 here. */
1712 && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
1713 || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1715 YYSTYPE newlval; /* Its value is ignored. */
1716 hextype = parse_number (tokstart, namelen, 0, &newlval);
1719 yylval.ssym.sym = sym;
1720 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1727 /* Any other kind of symbol. */
1728 yylval.ssym.sym = sym;
1729 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1739 lexptr = prev_lexptr;
1741 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);