Automatic date update in version.in
[binutils-gdb.git] / gdb / p-exp.y
blob2df41b80ec7dbc842fdbd4e43720cd484255e021
1 /* YACC parser for Pascal expressions, for GDB.
2 Copyright (C) 2000-2022 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. */
46 #include "defs.h"
47 #include <ctype.h>
48 #include "expression.h"
49 #include "value.h"
50 #include "parser-defs.h"
51 #include "language.h"
52 #include "p-lang.h"
53 #include "bfd.h" /* Required by objfiles.h. */
54 #include "symfile.h" /* Required by objfiles.h. */
55 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols. */
56 #include "block.h"
57 #include "completer.h"
58 #include "expop.h"
60 #define parse_type(ps) builtin_type (ps->gdbarch ())
62 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
63 etc). */
64 #define GDB_YY_REMAP_PREFIX pascal_
65 #include "yy-remap.h"
67 /* The state of the parser, used internally when we are parsing the
68 expression. */
70 static struct parser_state *pstate = NULL;
72 /* Depth of parentheses. */
73 static int paren_depth;
75 int yyparse (void);
77 static int yylex (void);
79 static void yyerror (const char *);
81 static char *uptok (const char *, int);
83 using namespace expr;
86 /* Although the yacc "value" of an expression is not used,
87 since the result is stored in the structure being created,
88 other node types do have values. */
90 %union
92 LONGEST lval;
93 struct {
94 LONGEST val;
95 struct type *type;
96 } typed_val_int;
97 struct {
98 gdb_byte val[16];
99 struct type *type;
100 } typed_val_float;
101 struct symbol *sym;
102 struct type *tval;
103 struct stoken sval;
104 struct ttype tsym;
105 struct symtoken ssym;
106 int voidval;
107 const struct block *bval;
108 enum exp_opcode opcode;
109 struct internalvar *ivar;
111 struct type **tvec;
112 int *ivec;
116 /* YYSTYPE gets defined by %union */
117 static int parse_number (struct parser_state *,
118 const char *, int, int, YYSTYPE *);
120 static struct type *current_type;
121 static int leftdiv_is_integer;
122 static void push_current_type (void);
123 static void pop_current_type (void);
124 static int search_field;
127 %type <voidval> exp exp1 type_exp start normal_start variable qualified_name
128 %type <tval> type typebase
129 /* %type <bval> block */
131 /* Fancy type parsing. */
132 %type <tval> ptype
134 %token <typed_val_int> INT
135 %token <typed_val_float> FLOAT
137 /* Both NAME and TYPENAME tokens represent symbols in the input,
138 and both convey their data as strings.
139 But a TYPENAME is a string that happens to be defined as a typedef
140 or builtin type name (such as int or char)
141 and a NAME is any other symbol.
142 Contexts where this distinction is not important can use the
143 nonterminal "name", which matches either NAME or TYPENAME. */
145 %token <sval> STRING
146 %token <sval> FIELDNAME
147 %token <voidval> COMPLETE
148 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
149 %token <tsym> TYPENAME
150 %type <sval> name
151 %type <ssym> name_not_typename
153 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
154 but which would parse as a valid number in the current input radix.
155 E.g. "c" when input_radix==16. Depending on the parse, it will be
156 turned into a name or into a number. */
158 %token <ssym> NAME_OR_INT
160 %token STRUCT CLASS SIZEOF COLONCOLON
161 %token ERROR
163 /* Special type cases, put in to allow the parser to distinguish different
164 legal basetypes. */
166 %token <sval> DOLLAR_VARIABLE
169 /* Object pascal */
170 %token THIS
171 %token <lval> TRUEKEYWORD FALSEKEYWORD
173 %left ','
174 %left ABOVE_COMMA
175 %right ASSIGN
176 %left NOT
177 %left OR
178 %left XOR
179 %left ANDAND
180 %left '=' NOTEQUAL
181 %left '<' '>' LEQ GEQ
182 %left LSH RSH DIV MOD
183 %left '@'
184 %left '+' '-'
185 %left '*' '/'
186 %right UNARY INCREMENT DECREMENT
187 %right ARROW '.' '[' '('
188 %left '^'
189 %token <ssym> BLOCKNAME
190 %type <bval> block
191 %left COLONCOLON
196 start : { current_type = NULL;
197 search_field = 0;
198 leftdiv_is_integer = 0;
200 normal_start {}
203 normal_start :
204 exp1
205 | type_exp
208 type_exp: type
210 pstate->push_new<type_operation> ($1);
211 current_type = $1; } ;
213 /* Expressions, including the comma operator. */
214 exp1 : exp
215 | exp1 ',' exp
216 { pstate->wrap2<comma_operation> (); }
219 /* Expressions, not including the comma operator. */
220 exp : exp '^' %prec UNARY
221 { pstate->wrap<unop_ind_operation> ();
222 if (current_type)
223 current_type = TYPE_TARGET_TYPE (current_type); }
226 exp : '@' exp %prec UNARY
227 { pstate->wrap<unop_addr_operation> ();
228 if (current_type)
229 current_type = TYPE_POINTER_TYPE (current_type); }
232 exp : '-' exp %prec UNARY
233 { pstate->wrap<unary_neg_operation> (); }
236 exp : NOT exp %prec UNARY
237 { pstate->wrap<unary_logical_not_operation> (); }
240 exp : INCREMENT '(' exp ')' %prec UNARY
241 { pstate->wrap<preinc_operation> (); }
244 exp : DECREMENT '(' exp ')' %prec UNARY
245 { pstate->wrap<predec_operation> (); }
249 field_exp : exp '.' %prec UNARY
250 { search_field = 1; }
253 exp : field_exp FIELDNAME
255 pstate->push_new<structop_operation>
256 (pstate->pop (), copy_name ($2));
257 search_field = 0;
258 if (current_type)
260 while (current_type->code ()
261 == TYPE_CODE_PTR)
262 current_type =
263 TYPE_TARGET_TYPE (current_type);
264 current_type = lookup_struct_elt_type (
265 current_type, $2.ptr, 0);
271 exp : field_exp name
273 pstate->push_new<structop_operation>
274 (pstate->pop (), copy_name ($2));
275 search_field = 0;
276 if (current_type)
278 while (current_type->code ()
279 == TYPE_CODE_PTR)
280 current_type =
281 TYPE_TARGET_TYPE (current_type);
282 current_type = lookup_struct_elt_type (
283 current_type, $2.ptr, 0);
287 exp : field_exp name COMPLETE
289 structop_base_operation *op
290 = new structop_ptr_operation (pstate->pop (),
291 copy_name ($2));
292 pstate->mark_struct_expression (op);
293 pstate->push (operation_up (op));
296 exp : field_exp COMPLETE
298 structop_base_operation *op
299 = new structop_ptr_operation (pstate->pop (), "");
300 pstate->mark_struct_expression (op);
301 pstate->push (operation_up (op));
305 exp : exp '['
306 /* We need to save the current_type value. */
307 { const char *arrayname;
308 int arrayfieldindex
309 = pascal_is_string_type (current_type, NULL, NULL,
310 NULL, NULL, &arrayname);
311 if (arrayfieldindex)
313 current_type
314 = (current_type
315 ->field (arrayfieldindex - 1).type ());
316 pstate->push_new<structop_operation>
317 (pstate->pop (), arrayname);
319 push_current_type (); }
320 exp1 ']'
321 { pop_current_type ();
322 pstate->wrap2<subscript_operation> ();
323 if (current_type)
324 current_type = TYPE_TARGET_TYPE (current_type); }
327 exp : exp '('
328 /* This is to save the value of arglist_len
329 being accumulated by an outer function call. */
330 { push_current_type ();
331 pstate->start_arglist (); }
332 arglist ')' %prec ARROW
334 std::vector<operation_up> args
335 = pstate->pop_vector (pstate->end_arglist ());
336 pstate->push_new<funcall_operation>
337 (pstate->pop (), std::move (args));
338 pop_current_type ();
339 if (current_type)
340 current_type = TYPE_TARGET_TYPE (current_type);
344 arglist :
345 | exp
346 { pstate->arglist_len = 1; }
347 | arglist ',' exp %prec ABOVE_COMMA
348 { pstate->arglist_len++; }
351 exp : type '(' exp ')' %prec UNARY
352 { if (current_type)
354 /* Allow automatic dereference of classes. */
355 if ((current_type->code () == TYPE_CODE_PTR)
356 && (TYPE_TARGET_TYPE (current_type)->code () == TYPE_CODE_STRUCT)
357 && (($1)->code () == TYPE_CODE_STRUCT))
358 pstate->wrap<unop_ind_operation> ();
360 pstate->push_new<unop_cast_operation>
361 (pstate->pop (), $1);
362 current_type = $1; }
365 exp : '(' exp1 ')'
369 /* Binary operators in order of decreasing precedence. */
371 exp : exp '*' exp
372 { pstate->wrap2<mul_operation> (); }
375 exp : exp '/' {
376 if (current_type && is_integral_type (current_type))
377 leftdiv_is_integer = 1;
381 if (leftdiv_is_integer && current_type
382 && is_integral_type (current_type))
384 pstate->push_new<unop_cast_operation>
385 (pstate->pop (),
386 parse_type (pstate)->builtin_long_double);
387 current_type
388 = parse_type (pstate)->builtin_long_double;
389 leftdiv_is_integer = 0;
392 pstate->wrap2<div_operation> ();
396 exp : exp DIV exp
397 { pstate->wrap2<intdiv_operation> (); }
400 exp : exp MOD exp
401 { pstate->wrap2<rem_operation> (); }
404 exp : exp '+' exp
405 { pstate->wrap2<add_operation> (); }
408 exp : exp '-' exp
409 { pstate->wrap2<sub_operation> (); }
412 exp : exp LSH exp
413 { pstate->wrap2<lsh_operation> (); }
416 exp : exp RSH exp
417 { pstate->wrap2<rsh_operation> (); }
420 exp : exp '=' exp
422 pstate->wrap2<equal_operation> ();
423 current_type = parse_type (pstate)->builtin_bool;
427 exp : exp NOTEQUAL exp
429 pstate->wrap2<notequal_operation> ();
430 current_type = parse_type (pstate)->builtin_bool;
434 exp : exp LEQ exp
436 pstate->wrap2<leq_operation> ();
437 current_type = parse_type (pstate)->builtin_bool;
441 exp : exp GEQ exp
443 pstate->wrap2<geq_operation> ();
444 current_type = parse_type (pstate)->builtin_bool;
448 exp : exp '<' exp
450 pstate->wrap2<less_operation> ();
451 current_type = parse_type (pstate)->builtin_bool;
455 exp : exp '>' exp
457 pstate->wrap2<gtr_operation> ();
458 current_type = parse_type (pstate)->builtin_bool;
462 exp : exp ANDAND exp
463 { pstate->wrap2<bitwise_and_operation> (); }
466 exp : exp XOR exp
467 { pstate->wrap2<bitwise_xor_operation> (); }
470 exp : exp OR exp
471 { pstate->wrap2<bitwise_ior_operation> (); }
474 exp : exp ASSIGN exp
475 { pstate->wrap2<assign_operation> (); }
478 exp : TRUEKEYWORD
480 pstate->push_new<bool_operation> ($1);
481 current_type = parse_type (pstate)->builtin_bool;
485 exp : FALSEKEYWORD
487 pstate->push_new<bool_operation> ($1);
488 current_type = parse_type (pstate)->builtin_bool;
492 exp : INT
494 pstate->push_new<long_const_operation>
495 ($1.type, $1.val);
496 current_type = $1.type;
500 exp : NAME_OR_INT
501 { YYSTYPE val;
502 parse_number (pstate, $1.stoken.ptr,
503 $1.stoken.length, 0, &val);
504 pstate->push_new<long_const_operation>
505 (val.typed_val_int.type,
506 val.typed_val_int.val);
507 current_type = val.typed_val_int.type;
512 exp : FLOAT
514 float_data data;
515 std::copy (std::begin ($1.val), std::end ($1.val),
516 std::begin (data));
517 pstate->push_new<float_const_operation> ($1.type, data);
521 exp : variable
524 exp : DOLLAR_VARIABLE
526 pstate->push_dollar ($1);
528 /* $ is the normal prefix for pascal
529 hexadecimal values but this conflicts
530 with the GDB use for debugger variables
531 so in expression to enter hexadecimal
532 values we still need to use C syntax with
533 0xff */
534 std::string tmp ($1.ptr, $1.length);
535 /* Handle current_type. */
536 struct internalvar *intvar
537 = lookup_only_internalvar (tmp.c_str () + 1);
538 if (intvar != nullptr)
540 scoped_value_mark mark;
542 value *val
543 = value_of_internalvar (pstate->gdbarch (),
544 intvar);
545 current_type = value_type (val);
550 exp : SIZEOF '(' type ')' %prec UNARY
552 current_type = parse_type (pstate)->builtin_int;
553 $3 = check_typedef ($3);
554 pstate->push_new<long_const_operation>
555 (parse_type (pstate)->builtin_int,
556 TYPE_LENGTH ($3)); }
559 exp : SIZEOF '(' exp ')' %prec UNARY
560 { pstate->wrap<unop_sizeof_operation> ();
561 current_type = parse_type (pstate)->builtin_int; }
563 exp : STRING
564 { /* C strings are converted into array constants with
565 an explicit null byte added at the end. Thus
566 the array upper bound is the string length.
567 There is no such thing in C as a completely empty
568 string. */
569 const char *sp = $1.ptr; int count = $1.length;
571 std::vector<operation_up> args (count + 1);
572 for (int i = 0; i < count; ++i)
573 args[i] = (make_operation<long_const_operation>
574 (parse_type (pstate)->builtin_char,
575 *sp++));
576 args[count] = (make_operation<long_const_operation>
577 (parse_type (pstate)->builtin_char,
578 '\0'));
579 pstate->push_new<array_operation>
580 (0, $1.length, std::move (args));
584 /* Object pascal */
585 exp : THIS
587 struct value * this_val;
588 struct type * this_type;
589 pstate->push_new<op_this_operation> ();
590 /* We need type of this. */
591 this_val
592 = value_of_this_silent (pstate->language ());
593 if (this_val)
594 this_type = value_type (this_val);
595 else
596 this_type = NULL;
597 if (this_type)
599 if (this_type->code () == TYPE_CODE_PTR)
601 this_type = TYPE_TARGET_TYPE (this_type);
602 pstate->wrap<unop_ind_operation> ();
606 current_type = this_type;
610 /* end of object pascal. */
612 block : BLOCKNAME
614 if ($1.sym.symbol != 0)
615 $$ = $1.sym.symbol->value_block ();
616 else
618 std::string copy = copy_name ($1.stoken);
619 struct symtab *tem =
620 lookup_symtab (copy.c_str ());
621 if (tem)
622 $$ = (tem->compunit ()->blockvector ()
623 ->static_block ());
624 else
625 error (_("No file or function \"%s\"."),
626 copy.c_str ());
631 block : block COLONCOLON name
633 std::string copy = copy_name ($3);
634 struct symbol *tem
635 = lookup_symbol (copy.c_str (), $1,
636 VAR_DOMAIN, NULL).symbol;
638 if (!tem || tem->aclass () != LOC_BLOCK)
639 error (_("No function \"%s\" in specified context."),
640 copy.c_str ());
641 $$ = tem->value_block (); }
644 variable: block COLONCOLON name
645 { struct block_symbol sym;
647 std::string copy = copy_name ($3);
648 sym = lookup_symbol (copy.c_str (), $1,
649 VAR_DOMAIN, NULL);
650 if (sym.symbol == 0)
651 error (_("No symbol \"%s\" in specified context."),
652 copy.c_str ());
654 pstate->push_new<var_value_operation> (sym);
658 qualified_name: typebase COLONCOLON name
660 struct type *type = $1;
662 if (type->code () != TYPE_CODE_STRUCT
663 && type->code () != TYPE_CODE_UNION)
664 error (_("`%s' is not defined as an aggregate type."),
665 type->name ());
667 pstate->push_new<scope_operation>
668 (type, copy_name ($3));
672 variable: qualified_name
673 | COLONCOLON name
675 std::string name = copy_name ($2);
677 struct block_symbol sym
678 = lookup_symbol (name.c_str (), nullptr,
679 VAR_DOMAIN, nullptr);
680 pstate->push_symbol (name.c_str (), sym);
684 variable: name_not_typename
685 { struct block_symbol sym = $1.sym;
687 if (sym.symbol)
689 if (symbol_read_needs_frame (sym.symbol))
690 pstate->block_tracker->update (sym);
692 pstate->push_new<var_value_operation> (sym);
693 current_type = sym.symbol->type (); }
694 else if ($1.is_a_field_of_this)
696 struct value * this_val;
697 struct type * this_type;
698 /* Object pascal: it hangs off of `this'. Must
699 not inadvertently convert from a method call
700 to data ref. */
701 pstate->block_tracker->update (sym);
702 operation_up thisop
703 = make_operation<op_this_operation> ();
704 pstate->push_new<structop_operation>
705 (std::move (thisop), copy_name ($1.stoken));
706 /* We need type of this. */
707 this_val
708 = value_of_this_silent (pstate->language ());
709 if (this_val)
710 this_type = value_type (this_val);
711 else
712 this_type = NULL;
713 if (this_type)
714 current_type = lookup_struct_elt_type (
715 this_type,
716 copy_name ($1.stoken).c_str (), 0);
717 else
718 current_type = NULL;
720 else
722 struct bound_minimal_symbol msymbol;
723 std::string arg = copy_name ($1.stoken);
725 msymbol =
726 lookup_bound_minimal_symbol (arg.c_str ());
727 if (msymbol.minsym != NULL)
728 pstate->push_new<var_msym_value_operation>
729 (msymbol);
730 else if (!have_full_symbols ()
731 && !have_partial_symbols ())
732 error (_("No symbol table is loaded. "
733 "Use the \"file\" command."));
734 else
735 error (_("No symbol \"%s\" in current context."),
736 arg.c_str ());
742 ptype : typebase
745 /* We used to try to recognize more pointer to member types here, but
746 that didn't work (shift/reduce conflicts meant that these rules never
747 got executed). The problem is that
748 int (foo::bar::baz::bizzle)
749 is a function type but
750 int (foo::bar::baz::bizzle::*)
751 is a pointer to member type. Stroustrup loses again! */
753 type : ptype
756 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
757 : '^' typebase
758 { $$ = lookup_pointer_type ($2); }
759 | TYPENAME
760 { $$ = $1.type; }
761 | STRUCT name
762 { $$
763 = lookup_struct (copy_name ($2).c_str (),
764 pstate->expression_context_block);
766 | CLASS name
767 { $$
768 = lookup_struct (copy_name ($2).c_str (),
769 pstate->expression_context_block);
771 /* "const" and "volatile" are curently ignored. A type qualifier
772 after the type is handled in the ptype rule. I think these could
773 be too. */
776 name : NAME { $$ = $1.stoken; }
777 | BLOCKNAME { $$ = $1.stoken; }
778 | TYPENAME { $$ = $1.stoken; }
779 | NAME_OR_INT { $$ = $1.stoken; }
782 name_not_typename : NAME
783 | BLOCKNAME
784 /* These would be useful if name_not_typename was useful, but it is just
785 a fake for "variable", so these cause reduce/reduce conflicts because
786 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
787 =exp) or just an exp. If name_not_typename was ever used in an lvalue
788 context where only a name could occur, this might be useful.
789 | NAME_OR_INT
795 /* Take care of parsing a number (anything that starts with a digit).
796 Set yylval and return the token type; update lexptr.
797 LEN is the number of characters in it. */
799 /*** Needs some error checking for the float case ***/
801 static int
802 parse_number (struct parser_state *par_state,
803 const char *p, int len, int parsed_float, YYSTYPE *putithere)
805 ULONGEST n = 0;
806 ULONGEST prevn = 0;
808 int i = 0;
809 int c;
810 int base = input_radix;
811 int unsigned_p = 0;
813 /* Number of "L" suffixes encountered. */
814 int long_p = 0;
816 /* We have found a "L" or "U" suffix. */
817 int found_suffix = 0;
819 if (parsed_float)
821 /* Handle suffixes: 'f' for float, 'l' for long double.
822 FIXME: This appears to be an extension -- do we want this? */
823 if (len >= 1 && tolower (p[len - 1]) == 'f')
825 putithere->typed_val_float.type
826 = parse_type (par_state)->builtin_float;
827 len--;
829 else if (len >= 1 && tolower (p[len - 1]) == 'l')
831 putithere->typed_val_float.type
832 = parse_type (par_state)->builtin_long_double;
833 len--;
835 /* Default type for floating-point literals is double. */
836 else
838 putithere->typed_val_float.type
839 = parse_type (par_state)->builtin_double;
842 if (!parse_float (p, len,
843 putithere->typed_val_float.type,
844 putithere->typed_val_float.val))
845 return ERROR;
846 return FLOAT;
849 /* Handle base-switching prefixes 0x, 0t, 0d, 0. */
850 if (p[0] == '0' && len > 1)
851 switch (p[1])
853 case 'x':
854 case 'X':
855 if (len >= 3)
857 p += 2;
858 base = 16;
859 len -= 2;
861 break;
863 case 't':
864 case 'T':
865 case 'd':
866 case 'D':
867 if (len >= 3)
869 p += 2;
870 base = 10;
871 len -= 2;
873 break;
875 default:
876 base = 8;
877 break;
880 while (len-- > 0)
882 c = *p++;
883 if (c >= 'A' && c <= 'Z')
884 c += 'a' - 'A';
885 if (c != 'l' && c != 'u')
886 n *= base;
887 if (c >= '0' && c <= '9')
889 if (found_suffix)
890 return ERROR;
891 n += i = c - '0';
893 else
895 if (base > 10 && c >= 'a' && c <= 'f')
897 if (found_suffix)
898 return ERROR;
899 n += i = c - 'a' + 10;
901 else if (c == 'l')
903 ++long_p;
904 found_suffix = 1;
906 else if (c == 'u')
908 unsigned_p = 1;
909 found_suffix = 1;
911 else
912 return ERROR; /* Char not a digit */
914 if (i >= base)
915 return ERROR; /* Invalid digit in this base. */
917 if (c != 'l' && c != 'u')
919 /* Test for overflow. */
920 if (prevn == 0 && n == 0)
922 else if (prevn >= n)
923 error (_("Numeric constant too large."));
925 prevn = n;
928 /* An integer constant is an int, a long, or a long long. An L
929 suffix forces it to be long; an LL suffix forces it to be long
930 long. If not forced to a larger size, it gets the first type of
931 the above that it fits in. To figure out whether it fits, we
932 shift it right and see whether anything remains. Note that we
933 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
934 operation, because many compilers will warn about such a shift
935 (which always produces a zero result). Sometimes gdbarch_int_bit
936 or gdbarch_long_bit will be that big, sometimes not. To deal with
937 the case where it is we just always shift the value more than
938 once, with fewer bits each time. */
940 int int_bits = gdbarch_int_bit (par_state->gdbarch ());
941 int long_bits = gdbarch_long_bit (par_state->gdbarch ());
942 int long_long_bits = gdbarch_long_long_bit (par_state->gdbarch ());
943 bool have_signed = !unsigned_p;
944 bool have_int = long_p == 0;
945 bool have_long = long_p <= 1;
946 if (have_int && have_signed && fits_in_type (1, n, int_bits, true))
947 putithere->typed_val_int.type = parse_type (par_state)->builtin_int;
948 else if (have_int && fits_in_type (1, n, int_bits, false))
949 putithere->typed_val_int.type
950 = parse_type (par_state)->builtin_unsigned_int;
951 else if (have_long && have_signed && fits_in_type (1, n, long_bits, true))
952 putithere->typed_val_int.type = parse_type (par_state)->builtin_long;
953 else if (have_long && fits_in_type (1, n, long_bits, false))
954 putithere->typed_val_int.type
955 = parse_type (par_state)->builtin_unsigned_long;
956 else if (have_signed && fits_in_type (1, n, long_long_bits, true))
957 putithere->typed_val_int.type
958 = parse_type (par_state)->builtin_long_long;
959 else if (fits_in_type (1, n, long_long_bits, false))
960 putithere->typed_val_int.type
961 = parse_type (par_state)->builtin_unsigned_long_long;
962 else
963 error (_("Numeric constant too large."));
964 putithere->typed_val_int.val = n;
966 return INT;
970 struct type_push
972 struct type *stored;
973 struct type_push *next;
976 static struct type_push *tp_top = NULL;
978 static void
979 push_current_type (void)
981 struct type_push *tpnew;
982 tpnew = (struct type_push *) malloc (sizeof (struct type_push));
983 tpnew->next = tp_top;
984 tpnew->stored = current_type;
985 current_type = NULL;
986 tp_top = tpnew;
989 static void
990 pop_current_type (void)
992 struct type_push *tp = tp_top;
993 if (tp)
995 current_type = tp->stored;
996 tp_top = tp->next;
997 free (tp);
1001 struct token
1003 const char *oper;
1004 int token;
1005 enum exp_opcode opcode;
1008 static const struct token tokentab3[] =
1010 {"shr", RSH, OP_NULL},
1011 {"shl", LSH, OP_NULL},
1012 {"and", ANDAND, OP_NULL},
1013 {"div", DIV, OP_NULL},
1014 {"not", NOT, OP_NULL},
1015 {"mod", MOD, OP_NULL},
1016 {"inc", INCREMENT, OP_NULL},
1017 {"dec", DECREMENT, OP_NULL},
1018 {"xor", XOR, OP_NULL}
1021 static const struct token tokentab2[] =
1023 {"or", OR, OP_NULL},
1024 {"<>", NOTEQUAL, OP_NULL},
1025 {"<=", LEQ, OP_NULL},
1026 {">=", GEQ, OP_NULL},
1027 {":=", ASSIGN, OP_NULL},
1028 {"::", COLONCOLON, OP_NULL} };
1030 /* Allocate uppercased var: */
1031 /* make an uppercased copy of tokstart. */
1032 static char *
1033 uptok (const char *tokstart, int namelen)
1035 int i;
1036 char *uptokstart = (char *)malloc(namelen+1);
1037 for (i = 0;i <= namelen;i++)
1039 if ((tokstart[i]>='a' && tokstart[i]<='z'))
1040 uptokstart[i] = tokstart[i]-('a'-'A');
1041 else
1042 uptokstart[i] = tokstart[i];
1044 uptokstart[namelen]='\0';
1045 return uptokstart;
1048 /* Read one token, getting characters through lexptr. */
1050 static int
1051 yylex (void)
1053 int c;
1054 int namelen;
1055 const char *tokstart;
1056 char *uptokstart;
1057 const char *tokptr;
1058 int explen, tempbufindex;
1059 static char *tempbuf;
1060 static int tempbufsize;
1062 retry:
1064 pstate->prev_lexptr = pstate->lexptr;
1066 tokstart = pstate->lexptr;
1067 explen = strlen (pstate->lexptr);
1069 /* See if it is a special token of length 3. */
1070 if (explen > 2)
1071 for (const auto &token : tokentab3)
1072 if (strncasecmp (tokstart, token.oper, 3) == 0
1073 && (!isalpha (token.oper[0]) || explen == 3
1074 || (!isalpha (tokstart[3])
1075 && !isdigit (tokstart[3]) && tokstart[3] != '_')))
1077 pstate->lexptr += 3;
1078 yylval.opcode = token.opcode;
1079 return token.token;
1082 /* See if it is a special token of length 2. */
1083 if (explen > 1)
1084 for (const auto &token : tokentab2)
1085 if (strncasecmp (tokstart, token.oper, 2) == 0
1086 && (!isalpha (token.oper[0]) || explen == 2
1087 || (!isalpha (tokstart[2])
1088 && !isdigit (tokstart[2]) && tokstart[2] != '_')))
1090 pstate->lexptr += 2;
1091 yylval.opcode = token.opcode;
1092 return token.token;
1095 switch (c = *tokstart)
1097 case 0:
1098 if (search_field && pstate->parse_completion)
1099 return COMPLETE;
1100 else
1101 return 0;
1103 case ' ':
1104 case '\t':
1105 case '\n':
1106 pstate->lexptr++;
1107 goto retry;
1109 case '\'':
1110 /* We either have a character constant ('0' or '\177' for example)
1111 or we have a quoted symbol reference ('foo(int,int)' in object pascal
1112 for example). */
1113 pstate->lexptr++;
1114 c = *pstate->lexptr++;
1115 if (c == '\\')
1116 c = parse_escape (pstate->gdbarch (), &pstate->lexptr);
1117 else if (c == '\'')
1118 error (_("Empty character constant."));
1120 yylval.typed_val_int.val = c;
1121 yylval.typed_val_int.type = parse_type (pstate)->builtin_char;
1123 c = *pstate->lexptr++;
1124 if (c != '\'')
1126 namelen = skip_quoted (tokstart) - tokstart;
1127 if (namelen > 2)
1129 pstate->lexptr = tokstart + namelen;
1130 if (pstate->lexptr[-1] != '\'')
1131 error (_("Unmatched single quote."));
1132 namelen -= 2;
1133 tokstart++;
1134 uptokstart = uptok(tokstart,namelen);
1135 goto tryname;
1137 error (_("Invalid character constant."));
1139 return INT;
1141 case '(':
1142 paren_depth++;
1143 pstate->lexptr++;
1144 return c;
1146 case ')':
1147 if (paren_depth == 0)
1148 return 0;
1149 paren_depth--;
1150 pstate->lexptr++;
1151 return c;
1153 case ',':
1154 if (pstate->comma_terminates && paren_depth == 0)
1155 return 0;
1156 pstate->lexptr++;
1157 return c;
1159 case '.':
1160 /* Might be a floating point number. */
1161 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
1163 goto symbol; /* Nope, must be a symbol. */
1166 /* FALL THRU. */
1168 case '0':
1169 case '1':
1170 case '2':
1171 case '3':
1172 case '4':
1173 case '5':
1174 case '6':
1175 case '7':
1176 case '8':
1177 case '9':
1179 /* It's a number. */
1180 int got_dot = 0, got_e = 0, toktype;
1181 const char *p = tokstart;
1182 int hex = input_radix > 10;
1184 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1186 p += 2;
1187 hex = 1;
1189 else if (c == '0' && (p[1]=='t' || p[1]=='T'
1190 || p[1]=='d' || p[1]=='D'))
1192 p += 2;
1193 hex = 0;
1196 for (;; ++p)
1198 /* This test includes !hex because 'e' is a valid hex digit
1199 and thus does not indicate a floating point number when
1200 the radix is hex. */
1201 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1202 got_dot = got_e = 1;
1203 /* This test does not include !hex, because a '.' always indicates
1204 a decimal floating point number regardless of the radix. */
1205 else if (!got_dot && *p == '.')
1206 got_dot = 1;
1207 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1208 && (*p == '-' || *p == '+'))
1209 /* This is the sign of the exponent, not the end of the
1210 number. */
1211 continue;
1212 /* We will take any letters or digits. parse_number will
1213 complain if past the radix, or if L or U are not final. */
1214 else if ((*p < '0' || *p > '9')
1215 && ((*p < 'a' || *p > 'z')
1216 && (*p < 'A' || *p > 'Z')))
1217 break;
1219 toktype = parse_number (pstate, tokstart,
1220 p - tokstart, got_dot | got_e, &yylval);
1221 if (toktype == ERROR)
1223 char *err_copy = (char *) alloca (p - tokstart + 1);
1225 memcpy (err_copy, tokstart, p - tokstart);
1226 err_copy[p - tokstart] = 0;
1227 error (_("Invalid number \"%s\"."), err_copy);
1229 pstate->lexptr = p;
1230 return toktype;
1233 case '+':
1234 case '-':
1235 case '*':
1236 case '/':
1237 case '|':
1238 case '&':
1239 case '^':
1240 case '~':
1241 case '!':
1242 case '@':
1243 case '<':
1244 case '>':
1245 case '[':
1246 case ']':
1247 case '?':
1248 case ':':
1249 case '=':
1250 case '{':
1251 case '}':
1252 symbol:
1253 pstate->lexptr++;
1254 return c;
1256 case '"':
1258 /* Build the gdb internal form of the input string in tempbuf,
1259 translating any standard C escape forms seen. Note that the
1260 buffer is null byte terminated *only* for the convenience of
1261 debugging gdb itself and printing the buffer contents when
1262 the buffer contains no embedded nulls. Gdb does not depend
1263 upon the buffer being null byte terminated, it uses the length
1264 string instead. This allows gdb to handle C strings (as well
1265 as strings in other languages) with embedded null bytes. */
1267 tokptr = ++tokstart;
1268 tempbufindex = 0;
1270 do {
1271 /* Grow the static temp buffer if necessary, including allocating
1272 the first one on demand. */
1273 if (tempbufindex + 1 >= tempbufsize)
1275 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1278 switch (*tokptr)
1280 case '\0':
1281 case '"':
1282 /* Do nothing, loop will terminate. */
1283 break;
1284 case '\\':
1285 ++tokptr;
1286 c = parse_escape (pstate->gdbarch (), &tokptr);
1287 if (c == -1)
1289 continue;
1291 tempbuf[tempbufindex++] = c;
1292 break;
1293 default:
1294 tempbuf[tempbufindex++] = *tokptr++;
1295 break;
1297 } while ((*tokptr != '"') && (*tokptr != '\0'));
1298 if (*tokptr++ != '"')
1300 error (_("Unterminated string in expression."));
1302 tempbuf[tempbufindex] = '\0'; /* See note above. */
1303 yylval.sval.ptr = tempbuf;
1304 yylval.sval.length = tempbufindex;
1305 pstate->lexptr = tokptr;
1306 return (STRING);
1309 if (!(c == '_' || c == '$'
1310 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1311 /* We must have come across a bad character (e.g. ';'). */
1312 error (_("Invalid character '%c' in expression."), c);
1314 /* It's a name. See how long it is. */
1315 namelen = 0;
1316 for (c = tokstart[namelen];
1317 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1318 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1320 /* Template parameter lists are part of the name.
1321 FIXME: This mishandles `print $a<4&&$a>3'. */
1322 if (c == '<')
1324 int i = namelen;
1325 int nesting_level = 1;
1326 while (tokstart[++i])
1328 if (tokstart[i] == '<')
1329 nesting_level++;
1330 else if (tokstart[i] == '>')
1332 if (--nesting_level == 0)
1333 break;
1336 if (tokstart[i] == '>')
1337 namelen = i;
1338 else
1339 break;
1342 /* do NOT uppercase internals because of registers !!! */
1343 c = tokstart[++namelen];
1346 uptokstart = uptok(tokstart,namelen);
1348 /* The token "if" terminates the expression and is NOT
1349 removed from the input stream. */
1350 if (namelen == 2 && uptokstart[0] == 'I' && uptokstart[1] == 'F')
1352 free (uptokstart);
1353 return 0;
1356 pstate->lexptr += namelen;
1358 tryname:
1360 /* Catch specific keywords. Should be done with a data structure. */
1361 switch (namelen)
1363 case 6:
1364 if (strcmp (uptokstart, "OBJECT") == 0)
1366 free (uptokstart);
1367 return CLASS;
1369 if (strcmp (uptokstart, "RECORD") == 0)
1371 free (uptokstart);
1372 return STRUCT;
1374 if (strcmp (uptokstart, "SIZEOF") == 0)
1376 free (uptokstart);
1377 return SIZEOF;
1379 break;
1380 case 5:
1381 if (strcmp (uptokstart, "CLASS") == 0)
1383 free (uptokstart);
1384 return CLASS;
1386 if (strcmp (uptokstart, "FALSE") == 0)
1388 yylval.lval = 0;
1389 free (uptokstart);
1390 return FALSEKEYWORD;
1392 break;
1393 case 4:
1394 if (strcmp (uptokstart, "TRUE") == 0)
1396 yylval.lval = 1;
1397 free (uptokstart);
1398 return TRUEKEYWORD;
1400 if (strcmp (uptokstart, "SELF") == 0)
1402 /* Here we search for 'this' like
1403 inserted in FPC stabs debug info. */
1404 static const char this_name[] = "this";
1406 if (lookup_symbol (this_name, pstate->expression_context_block,
1407 VAR_DOMAIN, NULL).symbol)
1409 free (uptokstart);
1410 return THIS;
1413 break;
1414 default:
1415 break;
1418 yylval.sval.ptr = tokstart;
1419 yylval.sval.length = namelen;
1421 if (*tokstart == '$')
1423 free (uptokstart);
1424 return DOLLAR_VARIABLE;
1427 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1428 functions or symtabs. If this is not so, then ...
1429 Use token-type TYPENAME for symbols that happen to be defined
1430 currently as names of types; NAME for other symbols.
1431 The caller is not constrained to care about the distinction. */
1433 std::string tmp = copy_name (yylval.sval);
1434 struct symbol *sym;
1435 struct field_of_this_result is_a_field_of_this;
1436 int is_a_field = 0;
1437 int hextype;
1439 is_a_field_of_this.type = NULL;
1440 if (search_field && current_type)
1441 is_a_field = (lookup_struct_elt_type (current_type,
1442 tmp.c_str (), 1) != NULL);
1443 if (is_a_field)
1444 sym = NULL;
1445 else
1446 sym = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
1447 VAR_DOMAIN, &is_a_field_of_this).symbol;
1448 /* second chance uppercased (as Free Pascal does). */
1449 if (!sym && is_a_field_of_this.type == NULL && !is_a_field)
1451 for (int i = 0; i <= namelen; i++)
1453 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1454 tmp[i] -= ('a'-'A');
1456 if (search_field && current_type)
1457 is_a_field = (lookup_struct_elt_type (current_type,
1458 tmp.c_str (), 1) != NULL);
1459 if (is_a_field)
1460 sym = NULL;
1461 else
1462 sym = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
1463 VAR_DOMAIN, &is_a_field_of_this).symbol;
1465 /* Third chance Capitalized (as GPC does). */
1466 if (!sym && is_a_field_of_this.type == NULL && !is_a_field)
1468 for (int i = 0; i <= namelen; i++)
1470 if (i == 0)
1472 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1473 tmp[i] -= ('a'-'A');
1475 else
1476 if ((tmp[i] >= 'A' && tmp[i] <= 'Z'))
1477 tmp[i] -= ('A'-'a');
1479 if (search_field && current_type)
1480 is_a_field = (lookup_struct_elt_type (current_type,
1481 tmp.c_str (), 1) != NULL);
1482 if (is_a_field)
1483 sym = NULL;
1484 else
1485 sym = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
1486 VAR_DOMAIN, &is_a_field_of_this).symbol;
1489 if (is_a_field || (is_a_field_of_this.type != NULL))
1491 tempbuf = (char *) realloc (tempbuf, namelen + 1);
1492 strncpy (tempbuf, tmp.c_str (), namelen);
1493 tempbuf [namelen] = 0;
1494 yylval.sval.ptr = tempbuf;
1495 yylval.sval.length = namelen;
1496 yylval.ssym.sym.symbol = NULL;
1497 yylval.ssym.sym.block = NULL;
1498 free (uptokstart);
1499 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1500 if (is_a_field)
1501 return FIELDNAME;
1502 else
1503 return NAME;
1505 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1506 no psymtabs (coff, xcoff, or some future change to blow away the
1507 psymtabs once once symbols are read). */
1508 if ((sym && sym->aclass () == LOC_BLOCK)
1509 || lookup_symtab (tmp.c_str ()))
1511 yylval.ssym.sym.symbol = sym;
1512 yylval.ssym.sym.block = NULL;
1513 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1514 free (uptokstart);
1515 return BLOCKNAME;
1517 if (sym && sym->aclass () == LOC_TYPEDEF)
1519 #if 1
1520 /* Despite the following flaw, we need to keep this code enabled.
1521 Because we can get called from check_stub_method, if we don't
1522 handle nested types then it screws many operations in any
1523 program which uses nested types. */
1524 /* In "A::x", if x is a member function of A and there happens
1525 to be a type (nested or not, since the stabs don't make that
1526 distinction) named x, then this code incorrectly thinks we
1527 are dealing with nested types rather than a member function. */
1529 const char *p;
1530 const char *namestart;
1531 struct symbol *best_sym;
1533 /* Look ahead to detect nested types. This probably should be
1534 done in the grammar, but trying seemed to introduce a lot
1535 of shift/reduce and reduce/reduce conflicts. It's possible
1536 that it could be done, though. Or perhaps a non-grammar, but
1537 less ad hoc, approach would work well. */
1539 /* Since we do not currently have any way of distinguishing
1540 a nested type from a non-nested one (the stabs don't tell
1541 us whether a type is nested), we just ignore the
1542 containing type. */
1544 p = pstate->lexptr;
1545 best_sym = sym;
1546 while (1)
1548 /* Skip whitespace. */
1549 while (*p == ' ' || *p == '\t' || *p == '\n')
1550 ++p;
1551 if (*p == ':' && p[1] == ':')
1553 /* Skip the `::'. */
1554 p += 2;
1555 /* Skip whitespace. */
1556 while (*p == ' ' || *p == '\t' || *p == '\n')
1557 ++p;
1558 namestart = p;
1559 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1560 || (*p >= 'a' && *p <= 'z')
1561 || (*p >= 'A' && *p <= 'Z'))
1562 ++p;
1563 if (p != namestart)
1565 struct symbol *cur_sym;
1566 /* As big as the whole rest of the expression, which is
1567 at least big enough. */
1568 char *ncopy
1569 = (char *) alloca (tmp.size () + strlen (namestart)
1570 + 3);
1571 char *tmp1;
1573 tmp1 = ncopy;
1574 memcpy (tmp1, tmp.c_str (), tmp.size ());
1575 tmp1 += tmp.size ();
1576 memcpy (tmp1, "::", 2);
1577 tmp1 += 2;
1578 memcpy (tmp1, namestart, p - namestart);
1579 tmp1[p - namestart] = '\0';
1580 cur_sym
1581 = lookup_symbol (ncopy,
1582 pstate->expression_context_block,
1583 VAR_DOMAIN, NULL).symbol;
1584 if (cur_sym)
1586 if (cur_sym->aclass () == LOC_TYPEDEF)
1588 best_sym = cur_sym;
1589 pstate->lexptr = p;
1591 else
1592 break;
1594 else
1595 break;
1597 else
1598 break;
1600 else
1601 break;
1604 yylval.tsym.type = best_sym->type ();
1605 #else /* not 0 */
1606 yylval.tsym.type = sym->type ();
1607 #endif /* not 0 */
1608 free (uptokstart);
1609 return TYPENAME;
1611 yylval.tsym.type
1612 = language_lookup_primitive_type (pstate->language (),
1613 pstate->gdbarch (), tmp.c_str ());
1614 if (yylval.tsym.type != NULL)
1616 free (uptokstart);
1617 return TYPENAME;
1620 /* Input names that aren't symbols but ARE valid hex numbers,
1621 when the input radix permits them, can be names or numbers
1622 depending on the parse. Note we support radixes > 16 here. */
1623 if (!sym
1624 && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
1625 || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1627 YYSTYPE newlval; /* Its value is ignored. */
1628 hextype = parse_number (pstate, tokstart, namelen, 0, &newlval);
1629 if (hextype == INT)
1631 yylval.ssym.sym.symbol = sym;
1632 yylval.ssym.sym.block = NULL;
1633 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1634 free (uptokstart);
1635 return NAME_OR_INT;
1639 free(uptokstart);
1640 /* Any other kind of symbol. */
1641 yylval.ssym.sym.symbol = sym;
1642 yylval.ssym.sym.block = NULL;
1643 return NAME;
1647 /* See language.h. */
1650 pascal_language::parser (struct parser_state *par_state) const
1652 /* Setting up the parser state. */
1653 scoped_restore pstate_restore = make_scoped_restore (&pstate);
1654 gdb_assert (par_state != NULL);
1655 pstate = par_state;
1656 paren_depth = 0;
1658 int result = yyparse ();
1659 if (!result)
1660 pstate->set_operation (pstate->pop ());
1661 return result;
1664 static void
1665 yyerror (const char *msg)
1667 if (pstate->prev_lexptr)
1668 pstate->lexptr = pstate->prev_lexptr;
1670 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);