1 /* YACC parser for Pascal expressions, for GDB.
2 Copyright (C) 2000-2014 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. */
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(ps) builtin_type (parse_gdbarch (ps))
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_internal
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
122 /* The state of the parser, used internally when we are parsing the
125 static struct parser_state *pstate = NULL;
129 static int yylex (void);
131 void yyerror (char *);
133 static char *uptok (const char *, int);
136 /* Although the yacc "value" of an expression is not used,
137 since the result is stored in the structure being created,
138 other node types do have values. */
155 struct symtoken ssym;
158 enum exp_opcode opcode;
159 struct internalvar *ivar;
166 /* YYSTYPE gets defined by %union */
167 static int parse_number (struct parser_state *,
168 const char *, int, int, YYSTYPE *);
170 static struct type *current_type;
171 static struct internalvar *intvar;
172 static int leftdiv_is_integer;
173 static void push_current_type (void);
174 static void pop_current_type (void);
175 static int search_field;
178 %type <voidval> exp exp1 type_exp start normal_start variable qualified_name
179 %type <tval> type typebase
180 /* %type <bval> block */
182 /* Fancy type parsing. */
185 %token <typed_val_int> INT
186 %token <typed_val_float> FLOAT
188 /* Both NAME and TYPENAME tokens represent symbols in the input,
189 and both convey their data as strings.
190 But a TYPENAME is a string that happens to be defined as a typedef
191 or builtin type name (such as int or char)
192 and a NAME is any other symbol.
193 Contexts where this distinction is not important can use the
194 nonterminal "name", which matches either NAME or TYPENAME. */
197 %token <sval> FIELDNAME
198 %token <voidval> COMPLETE
199 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
200 %token <tsym> TYPENAME
202 %type <ssym> name_not_typename
204 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
205 but which would parse as a valid number in the current input radix.
206 E.g. "c" when input_radix==16. Depending on the parse, it will be
207 turned into a name or into a number. */
209 %token <ssym> NAME_OR_INT
211 %token STRUCT CLASS SIZEOF COLONCOLON
214 /* Special type cases, put in to allow the parser to distinguish different
217 %token <voidval> VARIABLE
222 %token <lval> TRUEKEYWORD FALSEKEYWORD
232 %left '<' '>' LEQ GEQ
233 %left LSH RSH DIV MOD
237 %right UNARY INCREMENT DECREMENT
238 %right ARROW '.' '[' '('
240 %token <ssym> BLOCKNAME
247 start : { current_type = NULL;
250 leftdiv_is_integer = 0;
261 { write_exp_elt_opcode (pstate, OP_TYPE);
262 write_exp_elt_type (pstate, $1);
263 write_exp_elt_opcode (pstate, OP_TYPE);
264 current_type = $1; } ;
266 /* Expressions, including the comma operator. */
269 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
272 /* Expressions, not including the comma operator. */
273 exp : exp '^' %prec UNARY
274 { write_exp_elt_opcode (pstate, UNOP_IND);
276 current_type = TYPE_TARGET_TYPE (current_type); }
279 exp : '@' exp %prec UNARY
280 { write_exp_elt_opcode (pstate, UNOP_ADDR);
282 current_type = TYPE_POINTER_TYPE (current_type); }
285 exp : '-' exp %prec UNARY
286 { write_exp_elt_opcode (pstate, UNOP_NEG); }
289 exp : NOT exp %prec UNARY
290 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
293 exp : INCREMENT '(' exp ')' %prec UNARY
294 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
297 exp : DECREMENT '(' exp ')' %prec UNARY
298 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
302 field_exp : exp '.' %prec UNARY
303 { search_field = 1; }
306 exp : field_exp FIELDNAME
307 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
308 write_exp_string (pstate, $2);
309 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
313 while (TYPE_CODE (current_type)
316 TYPE_TARGET_TYPE (current_type);
317 current_type = lookup_struct_elt_type (
318 current_type, $2.ptr, 0);
325 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
326 write_exp_string (pstate, $2);
327 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
331 while (TYPE_CODE (current_type)
334 TYPE_TARGET_TYPE (current_type);
335 current_type = lookup_struct_elt_type (
336 current_type, $2.ptr, 0);
340 exp : field_exp name COMPLETE
341 { mark_struct_expression (pstate);
342 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
343 write_exp_string (pstate, $2);
344 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
346 exp : field_exp COMPLETE
348 mark_struct_expression (pstate);
349 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
352 write_exp_string (pstate, s);
353 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
357 /* We need to save the current_type value. */
358 { const char *arrayname;
360 arrayfieldindex = is_pascal_string_type (
361 current_type, NULL, NULL,
362 NULL, NULL, &arrayname);
365 struct stoken stringsval;
368 buf = alloca (strlen (arrayname) + 1);
369 stringsval.ptr = buf;
370 stringsval.length = strlen (arrayname);
371 strcpy (buf, arrayname);
372 current_type = TYPE_FIELD_TYPE (current_type,
373 arrayfieldindex - 1);
374 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
375 write_exp_string (pstate, stringsval);
376 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
378 push_current_type (); }
380 { pop_current_type ();
381 write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT);
383 current_type = TYPE_TARGET_TYPE (current_type); }
387 /* This is to save the value of arglist_len
388 being accumulated by an outer function call. */
389 { push_current_type ();
391 arglist ')' %prec ARROW
392 { write_exp_elt_opcode (pstate, OP_FUNCALL);
393 write_exp_elt_longcst (pstate,
394 (LONGEST) end_arglist ());
395 write_exp_elt_opcode (pstate, OP_FUNCALL);
398 current_type = TYPE_TARGET_TYPE (current_type);
405 | arglist ',' exp %prec ABOVE_COMMA
409 exp : type '(' exp ')' %prec UNARY
412 /* Allow automatic dereference of classes. */
413 if ((TYPE_CODE (current_type) == TYPE_CODE_PTR)
414 && (TYPE_CODE (TYPE_TARGET_TYPE (current_type)) == TYPE_CODE_CLASS)
415 && (TYPE_CODE ($1) == TYPE_CODE_CLASS))
416 write_exp_elt_opcode (pstate, UNOP_IND);
418 write_exp_elt_opcode (pstate, UNOP_CAST);
419 write_exp_elt_type (pstate, $1);
420 write_exp_elt_opcode (pstate, UNOP_CAST);
428 /* Binary operators in order of decreasing precedence. */
431 { write_exp_elt_opcode (pstate, BINOP_MUL); }
435 if (current_type && is_integral_type (current_type))
436 leftdiv_is_integer = 1;
440 if (leftdiv_is_integer && current_type
441 && is_integral_type (current_type))
443 write_exp_elt_opcode (pstate, UNOP_CAST);
444 write_exp_elt_type (pstate,
446 ->builtin_long_double);
448 = parse_type (pstate)->builtin_long_double;
449 write_exp_elt_opcode (pstate, UNOP_CAST);
450 leftdiv_is_integer = 0;
453 write_exp_elt_opcode (pstate, BINOP_DIV);
458 { write_exp_elt_opcode (pstate, BINOP_INTDIV); }
462 { write_exp_elt_opcode (pstate, BINOP_REM); }
466 { write_exp_elt_opcode (pstate, BINOP_ADD); }
470 { write_exp_elt_opcode (pstate, BINOP_SUB); }
474 { write_exp_elt_opcode (pstate, BINOP_LSH); }
478 { write_exp_elt_opcode (pstate, BINOP_RSH); }
482 { write_exp_elt_opcode (pstate, BINOP_EQUAL);
483 current_type = parse_type (pstate)->builtin_bool;
487 exp : exp NOTEQUAL exp
488 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL);
489 current_type = parse_type (pstate)->builtin_bool;
494 { write_exp_elt_opcode (pstate, BINOP_LEQ);
495 current_type = parse_type (pstate)->builtin_bool;
500 { write_exp_elt_opcode (pstate, BINOP_GEQ);
501 current_type = parse_type (pstate)->builtin_bool;
506 { write_exp_elt_opcode (pstate, BINOP_LESS);
507 current_type = parse_type (pstate)->builtin_bool;
512 { write_exp_elt_opcode (pstate, BINOP_GTR);
513 current_type = parse_type (pstate)->builtin_bool;
518 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
522 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
526 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
530 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
534 { write_exp_elt_opcode (pstate, OP_BOOL);
535 write_exp_elt_longcst (pstate, (LONGEST) $1);
536 current_type = parse_type (pstate)->builtin_bool;
537 write_exp_elt_opcode (pstate, OP_BOOL); }
541 { write_exp_elt_opcode (pstate, OP_BOOL);
542 write_exp_elt_longcst (pstate, (LONGEST) $1);
543 current_type = parse_type (pstate)->builtin_bool;
544 write_exp_elt_opcode (pstate, OP_BOOL); }
548 { write_exp_elt_opcode (pstate, OP_LONG);
549 write_exp_elt_type (pstate, $1.type);
550 current_type = $1.type;
551 write_exp_elt_longcst (pstate, (LONGEST)($1.val));
552 write_exp_elt_opcode (pstate, OP_LONG); }
557 parse_number (pstate, $1.stoken.ptr,
558 $1.stoken.length, 0, &val);
559 write_exp_elt_opcode (pstate, OP_LONG);
560 write_exp_elt_type (pstate, val.typed_val_int.type);
561 current_type = val.typed_val_int.type;
562 write_exp_elt_longcst (pstate, (LONGEST)
563 val.typed_val_int.val);
564 write_exp_elt_opcode (pstate, OP_LONG);
570 { write_exp_elt_opcode (pstate, OP_DOUBLE);
571 write_exp_elt_type (pstate, $1.type);
572 current_type = $1.type;
573 write_exp_elt_dblcst (pstate, $1.dval);
574 write_exp_elt_opcode (pstate, OP_DOUBLE); }
581 /* Already written by write_dollar_variable.
582 Handle current_type. */
584 struct value * val, * mark;
586 mark = value_mark ();
587 val = value_of_internalvar (parse_gdbarch (pstate),
589 current_type = value_type (val);
590 value_release_to_mark (mark);
595 exp : SIZEOF '(' type ')' %prec UNARY
596 { write_exp_elt_opcode (pstate, OP_LONG);
597 write_exp_elt_type (pstate,
598 parse_type (pstate)->builtin_int);
599 current_type = parse_type (pstate)->builtin_int;
601 write_exp_elt_longcst (pstate,
602 (LONGEST) TYPE_LENGTH ($3));
603 write_exp_elt_opcode (pstate, OP_LONG); }
606 exp : SIZEOF '(' exp ')' %prec UNARY
607 { write_exp_elt_opcode (pstate, UNOP_SIZEOF);
608 current_type = parse_type (pstate)->builtin_int; }
611 { /* C strings are converted into array constants with
612 an explicit null byte added at the end. Thus
613 the array upper bound is the string length.
614 There is no such thing in C as a completely empty
616 const char *sp = $1.ptr; int count = $1.length;
620 write_exp_elt_opcode (pstate, OP_LONG);
621 write_exp_elt_type (pstate,
624 write_exp_elt_longcst (pstate,
626 write_exp_elt_opcode (pstate, OP_LONG);
628 write_exp_elt_opcode (pstate, OP_LONG);
629 write_exp_elt_type (pstate,
632 write_exp_elt_longcst (pstate, (LONGEST)'\0');
633 write_exp_elt_opcode (pstate, OP_LONG);
634 write_exp_elt_opcode (pstate, OP_ARRAY);
635 write_exp_elt_longcst (pstate, (LONGEST) 0);
636 write_exp_elt_longcst (pstate,
637 (LONGEST) ($1.length));
638 write_exp_elt_opcode (pstate, OP_ARRAY); }
644 struct value * this_val;
645 struct type * this_type;
646 write_exp_elt_opcode (pstate, OP_THIS);
647 write_exp_elt_opcode (pstate, OP_THIS);
648 /* We need type of this. */
650 = value_of_this_silent (parse_language (pstate));
652 this_type = value_type (this_val);
657 if (TYPE_CODE (this_type) == TYPE_CODE_PTR)
659 this_type = TYPE_TARGET_TYPE (this_type);
660 write_exp_elt_opcode (pstate, UNOP_IND);
664 current_type = this_type;
668 /* end of object pascal. */
673 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
677 lookup_symtab (copy_name ($1.stoken));
679 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem),
682 error (_("No file or function \"%s\"."),
683 copy_name ($1.stoken));
688 block : block COLONCOLON name
690 = lookup_symbol (copy_name ($3), $1,
692 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
693 error (_("No function \"%s\" in specified context."),
695 $$ = SYMBOL_BLOCK_VALUE (tem); }
698 variable: block COLONCOLON name
699 { struct symbol *sym;
700 sym = lookup_symbol (copy_name ($3), $1,
703 error (_("No symbol \"%s\" in specified context."),
706 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
707 /* block_found is set by lookup_symbol. */
708 write_exp_elt_block (pstate, block_found);
709 write_exp_elt_sym (pstate, sym);
710 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
713 qualified_name: typebase COLONCOLON name
715 struct type *type = $1;
716 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
717 && TYPE_CODE (type) != TYPE_CODE_UNION)
718 error (_("`%s' is not defined as an aggregate type."),
721 write_exp_elt_opcode (pstate, OP_SCOPE);
722 write_exp_elt_type (pstate, type);
723 write_exp_string (pstate, $3);
724 write_exp_elt_opcode (pstate, OP_SCOPE);
728 variable: qualified_name
731 char *name = copy_name ($2);
733 struct bound_minimal_symbol msymbol;
736 lookup_symbol (name, (const struct block *) NULL,
740 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
741 write_exp_elt_block (pstate, NULL);
742 write_exp_elt_sym (pstate, sym);
743 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
747 msymbol = lookup_bound_minimal_symbol (name);
748 if (msymbol.minsym != NULL)
749 write_exp_msymbol (pstate, msymbol);
750 else if (!have_full_symbols ()
751 && !have_partial_symbols ())
752 error (_("No symbol table is loaded. "
753 "Use the \"file\" command."));
755 error (_("No symbol \"%s\" in current context."),
760 variable: name_not_typename
761 { struct symbol *sym = $1.sym;
765 if (symbol_read_needs_frame (sym))
767 if (innermost_block == 0
768 || contained_in (block_found,
770 innermost_block = block_found;
773 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
774 /* We want to use the selected frame, not
775 another more inner frame which happens to
776 be in the same block. */
777 write_exp_elt_block (pstate, NULL);
778 write_exp_elt_sym (pstate, sym);
779 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
780 current_type = sym->type; }
781 else if ($1.is_a_field_of_this)
783 struct value * this_val;
784 struct type * this_type;
785 /* Object pascal: it hangs off of `this'. Must
786 not inadvertently convert from a method call
788 if (innermost_block == 0
789 || contained_in (block_found,
791 innermost_block = block_found;
792 write_exp_elt_opcode (pstate, OP_THIS);
793 write_exp_elt_opcode (pstate, OP_THIS);
794 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
795 write_exp_string (pstate, $1.stoken);
796 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
797 /* We need type of this. */
799 = value_of_this_silent (parse_language (pstate));
801 this_type = value_type (this_val);
805 current_type = lookup_struct_elt_type (
807 copy_name ($1.stoken), 0);
813 struct bound_minimal_symbol msymbol;
814 char *arg = copy_name ($1.stoken);
817 lookup_bound_minimal_symbol (arg);
818 if (msymbol.minsym != NULL)
819 write_exp_msymbol (pstate, msymbol);
820 else if (!have_full_symbols ()
821 && !have_partial_symbols ())
822 error (_("No symbol table is loaded. "
823 "Use the \"file\" command."));
825 error (_("No symbol \"%s\" in current context."),
826 copy_name ($1.stoken));
835 /* We used to try to recognize more pointer to member types here, but
836 that didn't work (shift/reduce conflicts meant that these rules never
837 got executed). The problem is that
838 int (foo::bar::baz::bizzle)
839 is a function type but
840 int (foo::bar::baz::bizzle::*)
841 is a pointer to member type. Stroustrup loses again! */
846 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
848 { $$ = lookup_pointer_type ($2); }
852 { $$ = lookup_struct (copy_name ($2),
853 expression_context_block); }
855 { $$ = lookup_struct (copy_name ($2),
856 expression_context_block); }
857 /* "const" and "volatile" are curently ignored. A type qualifier
858 after the type is handled in the ptype rule. I think these could
862 name : NAME { $$ = $1.stoken; }
863 | BLOCKNAME { $$ = $1.stoken; }
864 | TYPENAME { $$ = $1.stoken; }
865 | NAME_OR_INT { $$ = $1.stoken; }
868 name_not_typename : NAME
870 /* These would be useful if name_not_typename was useful, but it is just
871 a fake for "variable", so these cause reduce/reduce conflicts because
872 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
873 =exp) or just an exp. If name_not_typename was ever used in an lvalue
874 context where only a name could occur, this might be useful.
881 /* Take care of parsing a number (anything that starts with a digit).
882 Set yylval and return the token type; update lexptr.
883 LEN is the number of characters in it. */
885 /*** Needs some error checking for the float case ***/
888 parse_number (struct parser_state *par_state,
889 const char *p, int len, int parsed_float, YYSTYPE *putithere)
891 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
892 here, and we do kind of silly things like cast to unsigned. */
899 int base = input_radix;
902 /* Number of "L" suffixes encountered. */
905 /* We have found a "L" or "U" suffix. */
906 int found_suffix = 0;
909 struct type *signed_type;
910 struct type *unsigned_type;
914 if (! parse_c_float (parse_gdbarch (par_state), p, len,
915 &putithere->typed_val_float.dval,
916 &putithere->typed_val_float.type))
921 /* Handle base-switching prefixes 0x, 0t, 0d, 0. */
955 if (c >= 'A' && c <= 'Z')
957 if (c != 'l' && c != 'u')
959 if (c >= '0' && c <= '9')
967 if (base > 10 && c >= 'a' && c <= 'f')
971 n += i = c - 'a' + 10;
984 return ERROR; /* Char not a digit */
987 return ERROR; /* Invalid digit in this base. */
989 /* Portably test for overflow (only works for nonzero values, so make
990 a second check for zero). FIXME: Can't we just make n and prevn
991 unsigned and avoid this? */
992 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
993 unsigned_p = 1; /* Try something unsigned. */
995 /* Portably test for unsigned overflow.
996 FIXME: This check is wrong; for example it doesn't find overflow
997 on 0x123456789 when LONGEST is 32 bits. */
998 if (c != 'l' && c != 'u' && n != 0)
1000 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1001 error (_("Numeric constant too large."));
1006 /* An integer constant is an int, a long, or a long long. An L
1007 suffix forces it to be long; an LL suffix forces it to be long
1008 long. If not forced to a larger size, it gets the first type of
1009 the above that it fits in. To figure out whether it fits, we
1010 shift it right and see whether anything remains. Note that we
1011 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1012 operation, because many compilers will warn about such a shift
1013 (which always produces a zero result). Sometimes gdbarch_int_bit
1014 or gdbarch_long_bit will be that big, sometimes not. To deal with
1015 the case where it is we just always shift the value more than
1016 once, with fewer bits each time. */
1018 un = (ULONGEST)n >> 2;
1020 && (un >> (gdbarch_int_bit (parse_gdbarch (par_state)) - 2)) == 0)
1023 = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 1);
1025 /* A large decimal (not hex or octal) constant (between INT_MAX
1026 and UINT_MAX) is a long or unsigned long, according to ANSI,
1027 never an unsigned int, but this code treats it as unsigned
1028 int. This probably should be fixed. GCC gives a warning on
1031 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
1032 signed_type = parse_type (par_state)->builtin_int;
1034 else if (long_p <= 1
1035 && (un >> (gdbarch_long_bit (parse_gdbarch (par_state)) - 2)) == 0)
1038 = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch (par_state)) - 1);
1039 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
1040 signed_type = parse_type (par_state)->builtin_long;
1045 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1046 < gdbarch_long_long_bit (parse_gdbarch (par_state)))
1047 /* A long long does not fit in a LONGEST. */
1048 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1050 shift = (gdbarch_long_long_bit (parse_gdbarch (par_state)) - 1);
1051 high_bit = (ULONGEST) 1 << shift;
1052 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
1053 signed_type = parse_type (par_state)->builtin_long_long;
1056 putithere->typed_val_int.val = n;
1058 /* If the high bit of the worked out type is set then this number
1059 has to be unsigned. */
1061 if (unsigned_p || (n & high_bit))
1063 putithere->typed_val_int.type = unsigned_type;
1067 putithere->typed_val_int.type = signed_type;
1076 struct type *stored;
1077 struct type_push *next;
1080 static struct type_push *tp_top = NULL;
1083 push_current_type (void)
1085 struct type_push *tpnew;
1086 tpnew = (struct type_push *) malloc (sizeof (struct type_push));
1087 tpnew->next = tp_top;
1088 tpnew->stored = current_type;
1089 current_type = NULL;
1094 pop_current_type (void)
1096 struct type_push *tp = tp_top;
1099 current_type = tp->stored;
1109 enum exp_opcode opcode;
1112 static const struct token tokentab3[] =
1114 {"shr", RSH, BINOP_END},
1115 {"shl", LSH, BINOP_END},
1116 {"and", ANDAND, BINOP_END},
1117 {"div", DIV, BINOP_END},
1118 {"not", NOT, BINOP_END},
1119 {"mod", MOD, BINOP_END},
1120 {"inc", INCREMENT, BINOP_END},
1121 {"dec", DECREMENT, BINOP_END},
1122 {"xor", XOR, BINOP_END}
1125 static const struct token tokentab2[] =
1127 {"or", OR, BINOP_END},
1128 {"<>", NOTEQUAL, BINOP_END},
1129 {"<=", LEQ, BINOP_END},
1130 {">=", GEQ, BINOP_END},
1131 {":=", ASSIGN, BINOP_END},
1132 {"::", COLONCOLON, BINOP_END} };
1134 /* Allocate uppercased var: */
1135 /* make an uppercased copy of tokstart. */
1137 uptok (const char *tokstart, int namelen)
1140 char *uptokstart = (char *)malloc(namelen+1);
1141 for (i = 0;i <= namelen;i++)
1143 if ((tokstart[i]>='a' && tokstart[i]<='z'))
1144 uptokstart[i] = tokstart[i]-('a'-'A');
1146 uptokstart[i] = tokstart[i];
1148 uptokstart[namelen]='\0';
1152 /* Read one token, getting characters through lexptr. */
1160 const char *tokstart;
1163 int explen, tempbufindex;
1164 static char *tempbuf;
1165 static int tempbufsize;
1169 prev_lexptr = lexptr;
1172 explen = strlen (lexptr);
1174 /* See if it is a special token of length 3. */
1176 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1177 if (strncasecmp (tokstart, tokentab3[i].operator, 3) == 0
1178 && (!isalpha (tokentab3[i].operator[0]) || explen == 3
1179 || (!isalpha (tokstart[3])
1180 && !isdigit (tokstart[3]) && tokstart[3] != '_')))
1183 yylval.opcode = tokentab3[i].opcode;
1184 return tokentab3[i].token;
1187 /* See if it is a special token of length 2. */
1189 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1190 if (strncasecmp (tokstart, tokentab2[i].operator, 2) == 0
1191 && (!isalpha (tokentab2[i].operator[0]) || explen == 2
1192 || (!isalpha (tokstart[2])
1193 && !isdigit (tokstart[2]) && tokstart[2] != '_')))
1196 yylval.opcode = tokentab2[i].opcode;
1197 return tokentab2[i].token;
1200 switch (c = *tokstart)
1203 if (search_field && parse_completion)
1215 /* We either have a character constant ('0' or '\177' for example)
1216 or we have a quoted symbol reference ('foo(int,int)' in object pascal
1221 c = parse_escape (parse_gdbarch (pstate), &lexptr);
1223 error (_("Empty character constant."));
1225 yylval.typed_val_int.val = c;
1226 yylval.typed_val_int.type = parse_type (pstate)->builtin_char;
1231 namelen = skip_quoted (tokstart) - tokstart;
1234 lexptr = tokstart + namelen;
1235 if (lexptr[-1] != '\'')
1236 error (_("Unmatched single quote."));
1239 uptokstart = uptok(tokstart,namelen);
1242 error (_("Invalid character constant."));
1252 if (paren_depth == 0)
1259 if (comma_terminates && paren_depth == 0)
1265 /* Might be a floating point number. */
1266 if (lexptr[1] < '0' || lexptr[1] > '9')
1268 goto symbol; /* Nope, must be a symbol. */
1271 /* FALL THRU into number case. */
1284 /* It's a number. */
1285 int got_dot = 0, got_e = 0, toktype;
1286 const char *p = tokstart;
1287 int hex = input_radix > 10;
1289 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1294 else if (c == '0' && (p[1]=='t' || p[1]=='T'
1295 || p[1]=='d' || p[1]=='D'))
1303 /* This test includes !hex because 'e' is a valid hex digit
1304 and thus does not indicate a floating point number when
1305 the radix is hex. */
1306 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1307 got_dot = got_e = 1;
1308 /* This test does not include !hex, because a '.' always indicates
1309 a decimal floating point number regardless of the radix. */
1310 else if (!got_dot && *p == '.')
1312 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1313 && (*p == '-' || *p == '+'))
1314 /* This is the sign of the exponent, not the end of the
1317 /* We will take any letters or digits. parse_number will
1318 complain if past the radix, or if L or U are not final. */
1319 else if ((*p < '0' || *p > '9')
1320 && ((*p < 'a' || *p > 'z')
1321 && (*p < 'A' || *p > 'Z')))
1324 toktype = parse_number (pstate, tokstart,
1325 p - tokstart, got_dot | got_e, &yylval);
1326 if (toktype == ERROR)
1328 char *err_copy = (char *) alloca (p - tokstart + 1);
1330 memcpy (err_copy, tokstart, p - tokstart);
1331 err_copy[p - tokstart] = 0;
1332 error (_("Invalid number \"%s\"."), err_copy);
1363 /* Build the gdb internal form of the input string in tempbuf,
1364 translating any standard C escape forms seen. Note that the
1365 buffer is null byte terminated *only* for the convenience of
1366 debugging gdb itself and printing the buffer contents when
1367 the buffer contains no embedded nulls. Gdb does not depend
1368 upon the buffer being null byte terminated, it uses the length
1369 string instead. This allows gdb to handle C strings (as well
1370 as strings in other languages) with embedded null bytes. */
1372 tokptr = ++tokstart;
1376 /* Grow the static temp buffer if necessary, including allocating
1377 the first one on demand. */
1378 if (tempbufindex + 1 >= tempbufsize)
1380 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1387 /* Do nothing, loop will terminate. */
1391 c = parse_escape (parse_gdbarch (pstate), &tokptr);
1396 tempbuf[tempbufindex++] = c;
1399 tempbuf[tempbufindex++] = *tokptr++;
1402 } while ((*tokptr != '"') && (*tokptr != '\0'));
1403 if (*tokptr++ != '"')
1405 error (_("Unterminated string in expression."));
1407 tempbuf[tempbufindex] = '\0'; /* See note above. */
1408 yylval.sval.ptr = tempbuf;
1409 yylval.sval.length = tempbufindex;
1414 if (!(c == '_' || c == '$'
1415 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1416 /* We must have come across a bad character (e.g. ';'). */
1417 error (_("Invalid character '%c' in expression."), c);
1419 /* It's a name. See how long it is. */
1421 for (c = tokstart[namelen];
1422 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1423 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1425 /* Template parameter lists are part of the name.
1426 FIXME: This mishandles `print $a<4&&$a>3'. */
1430 int nesting_level = 1;
1431 while (tokstart[++i])
1433 if (tokstart[i] == '<')
1435 else if (tokstart[i] == '>')
1437 if (--nesting_level == 0)
1441 if (tokstart[i] == '>')
1447 /* do NOT uppercase internals because of registers !!! */
1448 c = tokstart[++namelen];
1451 uptokstart = uptok(tokstart,namelen);
1453 /* The token "if" terminates the expression and is NOT
1454 removed from the input stream. */
1455 if (namelen == 2 && uptokstart[0] == 'I' && uptokstart[1] == 'F')
1465 /* Catch specific keywords. Should be done with a data structure. */
1469 if (strcmp (uptokstart, "OBJECT") == 0)
1474 if (strcmp (uptokstart, "RECORD") == 0)
1479 if (strcmp (uptokstart, "SIZEOF") == 0)
1486 if (strcmp (uptokstart, "CLASS") == 0)
1491 if (strcmp (uptokstart, "FALSE") == 0)
1495 return FALSEKEYWORD;
1499 if (strcmp (uptokstart, "TRUE") == 0)
1505 if (strcmp (uptokstart, "SELF") == 0)
1507 /* Here we search for 'this' like
1508 inserted in FPC stabs debug info. */
1509 static const char this_name[] = "this";
1511 if (lookup_symbol (this_name, expression_context_block,
1523 yylval.sval.ptr = tokstart;
1524 yylval.sval.length = namelen;
1526 if (*tokstart == '$')
1530 /* $ is the normal prefix for pascal hexadecimal values
1531 but this conflicts with the GDB use for debugger variables
1532 so in expression to enter hexadecimal values
1533 we still need to use C syntax with 0xff */
1534 write_dollar_variable (pstate, yylval.sval);
1535 tmp = alloca (namelen + 1);
1536 memcpy (tmp, tokstart, namelen);
1537 tmp[namelen] = '\0';
1538 intvar = lookup_only_internalvar (tmp + 1);
1543 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1544 functions or symtabs. If this is not so, then ...
1545 Use token-type TYPENAME for symbols that happen to be defined
1546 currently as names of types; NAME for other symbols.
1547 The caller is not constrained to care about the distinction. */
1549 char *tmp = copy_name (yylval.sval);
1551 struct field_of_this_result is_a_field_of_this;
1556 if (search_field && current_type)
1557 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1561 sym = lookup_symbol (tmp, expression_context_block,
1562 VAR_DOMAIN, &is_a_field_of_this);
1563 /* second chance uppercased (as Free Pascal does). */
1564 if (!sym && is_a_field_of_this.type == NULL && !is_a_field)
1566 for (i = 0; i <= namelen; i++)
1568 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1569 tmp[i] -= ('a'-'A');
1571 if (search_field && current_type)
1572 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1576 sym = lookup_symbol (tmp, expression_context_block,
1577 VAR_DOMAIN, &is_a_field_of_this);
1579 /* Third chance Capitalized (as GPC does). */
1580 if (!sym && is_a_field_of_this.type == NULL && !is_a_field)
1582 for (i = 0; i <= namelen; i++)
1586 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1587 tmp[i] -= ('a'-'A');
1590 if ((tmp[i] >= 'A' && tmp[i] <= 'Z'))
1591 tmp[i] -= ('A'-'a');
1593 if (search_field && current_type)
1594 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1598 sym = lookup_symbol (tmp, expression_context_block,
1599 VAR_DOMAIN, &is_a_field_of_this);
1604 tempbuf = (char *) realloc (tempbuf, namelen + 1);
1605 strncpy (tempbuf, tmp, namelen);
1606 tempbuf [namelen] = 0;
1607 yylval.sval.ptr = tempbuf;
1608 yylval.sval.length = namelen;
1612 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1613 no psymtabs (coff, xcoff, or some future change to blow away the
1614 psymtabs once once symbols are read). */
1615 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1616 || lookup_symtab (tmp))
1618 yylval.ssym.sym = sym;
1619 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1623 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1626 /* Despite the following flaw, we need to keep this code enabled.
1627 Because we can get called from check_stub_method, if we don't
1628 handle nested types then it screws many operations in any
1629 program which uses nested types. */
1630 /* In "A::x", if x is a member function of A and there happens
1631 to be a type (nested or not, since the stabs don't make that
1632 distinction) named x, then this code incorrectly thinks we
1633 are dealing with nested types rather than a member function. */
1636 const char *namestart;
1637 struct symbol *best_sym;
1639 /* Look ahead to detect nested types. This probably should be
1640 done in the grammar, but trying seemed to introduce a lot
1641 of shift/reduce and reduce/reduce conflicts. It's possible
1642 that it could be done, though. Or perhaps a non-grammar, but
1643 less ad hoc, approach would work well. */
1645 /* Since we do not currently have any way of distinguishing
1646 a nested type from a non-nested one (the stabs don't tell
1647 us whether a type is nested), we just ignore the
1654 /* Skip whitespace. */
1655 while (*p == ' ' || *p == '\t' || *p == '\n')
1657 if (*p == ':' && p[1] == ':')
1659 /* Skip the `::'. */
1661 /* Skip whitespace. */
1662 while (*p == ' ' || *p == '\t' || *p == '\n')
1665 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1666 || (*p >= 'a' && *p <= 'z')
1667 || (*p >= 'A' && *p <= 'Z'))
1671 struct symbol *cur_sym;
1672 /* As big as the whole rest of the expression, which is
1673 at least big enough. */
1674 char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1678 memcpy (tmp1, tmp, strlen (tmp));
1679 tmp1 += strlen (tmp);
1680 memcpy (tmp1, "::", 2);
1682 memcpy (tmp1, namestart, p - namestart);
1683 tmp1[p - namestart] = '\0';
1684 cur_sym = lookup_symbol (ncopy, expression_context_block,
1688 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1706 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1708 yylval.tsym.type = SYMBOL_TYPE (sym);
1714 = language_lookup_primitive_type_by_name (parse_language (pstate),
1715 parse_gdbarch (pstate), tmp);
1716 if (yylval.tsym.type != NULL)
1722 /* Input names that aren't symbols but ARE valid hex numbers,
1723 when the input radix permits them, can be names or numbers
1724 depending on the parse. Note we support radixes > 16 here. */
1726 && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
1727 || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1729 YYSTYPE newlval; /* Its value is ignored. */
1730 hextype = parse_number (pstate, tokstart, namelen, 0, &newlval);
1733 yylval.ssym.sym = sym;
1734 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1741 /* Any other kind of symbol. */
1742 yylval.ssym.sym = sym;
1743 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1749 pascal_parse (struct parser_state *par_state)
1752 struct cleanup *c = make_cleanup_clear_parser_state (&pstate);
1754 /* Setting up the parser state. */
1755 gdb_assert (par_state != NULL);
1758 result = yyparse ();
1767 lexptr = prev_lexptr;
1769 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);