1 /* Parse expressions for GDB.
3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
5 Modified from expread.y by the Department of Computer Science at the
6 State University of New York at Buffalo, 1991.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* Parse an expression from text in a string,
24 and return the result as a struct expression pointer.
25 That structure contains arithmetic operations in reverse polish,
26 with constants represented by operations that are followed by special data.
27 See expression.h for the details of the format.
28 What is important here is that it can be built up sequentially
29 during the process of parsing; the lower levels of the tree always
30 come first in the result. */
34 #include "arch-utils.h"
38 #include "expression.h"
42 #include "parser-defs.h"
44 #include "symfile.h" /* for overlay functions */
46 #include "target-float.h"
50 #include "user-regs.h"
52 #include "gdbsupport/gdb_optional.h"
54 /* Standard set of definitions for printing, dumping, prefixifying,
55 * and evaluating expressions. */
57 const struct exp_descriptor exp_descriptor_standard
=
59 print_subexp_standard
,
60 operator_length_standard
,
61 operator_check_standard
,
63 dump_subexp_body_standard
,
64 evaluate_subexp_standard
67 static unsigned int expressiondebug
= 0;
69 show_expressiondebug (struct ui_file
*file
, int from_tty
,
70 struct cmd_list_element
*c
, const char *value
)
72 fprintf_filtered (file
, _("Expression debugging is %s.\n"), value
);
76 /* True if an expression parser should set yydebug. */
80 show_parserdebug (struct ui_file
*file
, int from_tty
,
81 struct cmd_list_element
*c
, const char *value
)
83 fprintf_filtered (file
, _("Parser debugging is %s.\n"), value
);
87 static int prefixify_subexp (struct expression
*, struct expression
*, int,
90 static expression_up
parse_exp_in_context (const char **, CORE_ADDR
,
91 const struct block
*, int,
93 innermost_block_tracker
*,
94 expr_completion_state
*);
96 static void increase_expout_size (struct expr_builder
*ps
, size_t lenelt
);
99 /* Documented at it's declaration. */
102 innermost_block_tracker::update (const struct block
*b
,
103 innermost_block_tracker_types t
)
105 if ((m_types
& t
) != 0
106 && (m_innermost_block
== NULL
107 || contained_in (b
, m_innermost_block
)))
108 m_innermost_block
= b
;
113 /* See definition in parser-defs.h. */
115 expr_builder::expr_builder (const struct language_defn
*lang
,
116 struct gdbarch
*gdbarch
)
118 expout (XNEWVAR (expression
,
120 + EXP_ELEM_TO_BYTES (expout_size
)))),
123 expout
->language_defn
= lang
;
124 expout
->gdbarch
= gdbarch
;
128 expr_builder::release ()
130 /* Record the actual number of expression elements, and then
131 reallocate the expression memory so that we free up any
134 expout
->nelts
= expout_ptr
;
135 expout
.reset (XRESIZEVAR (expression
, expout
.release (),
137 + EXP_ELEM_TO_BYTES (expout_ptr
))));
139 return std::move (expout
);
142 /* This page contains the functions for adding data to the struct expression
143 being constructed. */
145 /* Add one element to the end of the expression. */
147 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
148 a register through here. */
151 write_exp_elt (struct expr_builder
*ps
, const union exp_element
*expelt
)
153 if (ps
->expout_ptr
>= ps
->expout_size
)
155 ps
->expout_size
*= 2;
156 ps
->expout
.reset (XRESIZEVAR (expression
, ps
->expout
.release (),
158 + EXP_ELEM_TO_BYTES (ps
->expout_size
))));
160 ps
->expout
->elts
[ps
->expout_ptr
++] = *expelt
;
164 write_exp_elt_opcode (struct expr_builder
*ps
, enum exp_opcode expelt
)
166 union exp_element tmp
;
168 memset (&tmp
, 0, sizeof (union exp_element
));
170 write_exp_elt (ps
, &tmp
);
174 write_exp_elt_sym (struct expr_builder
*ps
, struct symbol
*expelt
)
176 union exp_element tmp
;
178 memset (&tmp
, 0, sizeof (union exp_element
));
180 write_exp_elt (ps
, &tmp
);
184 write_exp_elt_msym (struct expr_builder
*ps
, minimal_symbol
*expelt
)
186 union exp_element tmp
;
188 memset (&tmp
, 0, sizeof (union exp_element
));
189 tmp
.msymbol
= expelt
;
190 write_exp_elt (ps
, &tmp
);
194 write_exp_elt_block (struct expr_builder
*ps
, const struct block
*b
)
196 union exp_element tmp
;
198 memset (&tmp
, 0, sizeof (union exp_element
));
200 write_exp_elt (ps
, &tmp
);
204 write_exp_elt_objfile (struct expr_builder
*ps
, struct objfile
*objfile
)
206 union exp_element tmp
;
208 memset (&tmp
, 0, sizeof (union exp_element
));
209 tmp
.objfile
= objfile
;
210 write_exp_elt (ps
, &tmp
);
214 write_exp_elt_longcst (struct expr_builder
*ps
, LONGEST expelt
)
216 union exp_element tmp
;
218 memset (&tmp
, 0, sizeof (union exp_element
));
219 tmp
.longconst
= expelt
;
220 write_exp_elt (ps
, &tmp
);
224 write_exp_elt_floatcst (struct expr_builder
*ps
, const gdb_byte expelt
[16])
226 union exp_element tmp
;
229 for (index
= 0; index
< 16; index
++)
230 tmp
.floatconst
[index
] = expelt
[index
];
232 write_exp_elt (ps
, &tmp
);
236 write_exp_elt_type (struct expr_builder
*ps
, struct type
*expelt
)
238 union exp_element tmp
;
240 memset (&tmp
, 0, sizeof (union exp_element
));
242 write_exp_elt (ps
, &tmp
);
246 write_exp_elt_intern (struct expr_builder
*ps
, struct internalvar
*expelt
)
248 union exp_element tmp
;
250 memset (&tmp
, 0, sizeof (union exp_element
));
251 tmp
.internalvar
= expelt
;
252 write_exp_elt (ps
, &tmp
);
255 /* Add a string constant to the end of the expression.
257 String constants are stored by first writing an expression element
258 that contains the length of the string, then stuffing the string
259 constant itself into however many expression elements are needed
260 to hold it, and then writing another expression element that contains
261 the length of the string. I.e. an expression element at each end of
262 the string records the string length, so you can skip over the
263 expression elements containing the actual string bytes from either
264 end of the string. Note that this also allows gdb to handle
265 strings with embedded null bytes, as is required for some languages.
267 Don't be fooled by the fact that the string is null byte terminated,
268 this is strictly for the convenience of debugging gdb itself.
269 Gdb does not depend up the string being null terminated, since the
270 actual length is recorded in expression elements at each end of the
271 string. The null byte is taken into consideration when computing how
272 many expression elements are required to hold the string constant, of
277 write_exp_string (struct expr_builder
*ps
, struct stoken str
)
279 int len
= str
.length
;
283 /* Compute the number of expression elements required to hold the string
284 (including a null byte terminator), along with one expression element
285 at each end to record the actual string length (not including the
286 null byte terminator). */
288 lenelt
= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
290 increase_expout_size (ps
, lenelt
);
292 /* Write the leading length expression element (which advances the current
293 expression element index), then write the string constant followed by a
294 terminating null byte, and then write the trailing length expression
297 write_exp_elt_longcst (ps
, (LONGEST
) len
);
298 strdata
= (char *) &ps
->expout
->elts
[ps
->expout_ptr
];
299 memcpy (strdata
, str
.ptr
, len
);
300 *(strdata
+ len
) = '\0';
301 ps
->expout_ptr
+= lenelt
- 2;
302 write_exp_elt_longcst (ps
, (LONGEST
) len
);
305 /* Add a vector of string constants to the end of the expression.
307 This adds an OP_STRING operation, but encodes the contents
308 differently from write_exp_string. The language is expected to
309 handle evaluation of this expression itself.
311 After the usual OP_STRING header, TYPE is written into the
312 expression as a long constant. The interpretation of this field is
313 up to the language evaluator.
315 Next, each string in VEC is written. The length is written as a
316 long constant, followed by the contents of the string. */
319 write_exp_string_vector (struct expr_builder
*ps
, int type
,
320 struct stoken_vector
*vec
)
325 /* Compute the size. We compute the size in number of slots to
326 avoid issues with string padding. */
328 for (i
= 0; i
< vec
->len
; ++i
)
330 /* One slot for the length of this element, plus the number of
331 slots needed for this string. */
332 n_slots
+= 1 + BYTES_TO_EXP_ELEM (vec
->tokens
[i
].length
);
335 /* One more slot for the type of the string. */
338 /* Now compute a phony string length. */
339 len
= EXP_ELEM_TO_BYTES (n_slots
) - 1;
342 increase_expout_size (ps
, n_slots
);
344 write_exp_elt_opcode (ps
, OP_STRING
);
345 write_exp_elt_longcst (ps
, len
);
346 write_exp_elt_longcst (ps
, type
);
348 for (i
= 0; i
< vec
->len
; ++i
)
350 write_exp_elt_longcst (ps
, vec
->tokens
[i
].length
);
351 memcpy (&ps
->expout
->elts
[ps
->expout_ptr
], vec
->tokens
[i
].ptr
,
352 vec
->tokens
[i
].length
);
353 ps
->expout_ptr
+= BYTES_TO_EXP_ELEM (vec
->tokens
[i
].length
);
356 write_exp_elt_longcst (ps
, len
);
357 write_exp_elt_opcode (ps
, OP_STRING
);
360 /* Add a bitstring constant to the end of the expression.
362 Bitstring constants are stored by first writing an expression element
363 that contains the length of the bitstring (in bits), then stuffing the
364 bitstring constant itself into however many expression elements are
365 needed to hold it, and then writing another expression element that
366 contains the length of the bitstring. I.e. an expression element at
367 each end of the bitstring records the bitstring length, so you can skip
368 over the expression elements containing the actual bitstring bytes from
369 either end of the bitstring. */
372 write_exp_bitstring (struct expr_builder
*ps
, struct stoken str
)
374 int bits
= str
.length
; /* length in bits */
375 int len
= (bits
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
379 /* Compute the number of expression elements required to hold the bitstring,
380 along with one expression element at each end to record the actual
381 bitstring length in bits. */
383 lenelt
= 2 + BYTES_TO_EXP_ELEM (len
);
385 increase_expout_size (ps
, lenelt
);
387 /* Write the leading length expression element (which advances the current
388 expression element index), then write the bitstring constant, and then
389 write the trailing length expression element. */
391 write_exp_elt_longcst (ps
, (LONGEST
) bits
);
392 strdata
= (char *) &ps
->expout
->elts
[ps
->expout_ptr
];
393 memcpy (strdata
, str
.ptr
, len
);
394 ps
->expout_ptr
+= lenelt
- 2;
395 write_exp_elt_longcst (ps
, (LONGEST
) bits
);
398 /* Return the type of MSYMBOL, a minimal symbol of OBJFILE. If
399 ADDRESS_P is not NULL, set it to the MSYMBOL's resolved
403 find_minsym_type_and_address (minimal_symbol
*msymbol
,
404 struct objfile
*objfile
,
405 CORE_ADDR
*address_p
)
407 bound_minimal_symbol bound_msym
= {msymbol
, objfile
};
408 struct obj_section
*section
= MSYMBOL_OBJ_SECTION (objfile
, msymbol
);
409 enum minimal_symbol_type type
= MSYMBOL_TYPE (msymbol
);
411 bool is_tls
= (section
!= NULL
412 && section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
);
414 /* The minimal symbol might point to a function descriptor;
415 resolve it to the actual code address instead. */
419 /* Addresses of TLS symbols are really offsets into a
420 per-objfile/per-thread storage block. */
421 addr
= MSYMBOL_VALUE_RAW_ADDRESS (bound_msym
.minsym
);
423 else if (msymbol_is_function (objfile
, msymbol
, &addr
))
425 if (addr
!= BMSYMBOL_VALUE_ADDRESS (bound_msym
))
427 /* This means we resolved a function descriptor, and we now
428 have an address for a code/text symbol instead of a data
430 if (MSYMBOL_TYPE (msymbol
) == mst_data_gnu_ifunc
)
431 type
= mst_text_gnu_ifunc
;
438 addr
= BMSYMBOL_VALUE_ADDRESS (bound_msym
);
440 if (overlay_debugging
)
441 addr
= symbol_overlayed_address (addr
, section
);
445 /* Skip translation if caller does not need the address. */
446 if (address_p
!= NULL
)
447 *address_p
= target_translate_tls_address (objfile
, addr
);
448 return objfile_type (objfile
)->nodebug_tls_symbol
;
451 if (address_p
!= NULL
)
458 case mst_solib_trampoline
:
459 return objfile_type (objfile
)->nodebug_text_symbol
;
461 case mst_text_gnu_ifunc
:
462 return objfile_type (objfile
)->nodebug_text_gnu_ifunc_symbol
;
468 return objfile_type (objfile
)->nodebug_data_symbol
;
470 case mst_slot_got_plt
:
471 return objfile_type (objfile
)->nodebug_got_plt_symbol
;
474 return objfile_type (objfile
)->nodebug_unknown_symbol
;
478 /* Add the appropriate elements for a minimal symbol to the end of
482 write_exp_msymbol (struct expr_builder
*ps
,
483 struct bound_minimal_symbol bound_msym
)
485 write_exp_elt_opcode (ps
, OP_VAR_MSYM_VALUE
);
486 write_exp_elt_objfile (ps
, bound_msym
.objfile
);
487 write_exp_elt_msym (ps
, bound_msym
.minsym
);
488 write_exp_elt_opcode (ps
, OP_VAR_MSYM_VALUE
);
491 /* See parser-defs.h. */
494 parser_state::mark_struct_expression ()
496 gdb_assert (parse_completion
497 && (m_completion_state
.expout_tag_completion_type
498 == TYPE_CODE_UNDEF
));
499 m_completion_state
.expout_last_struct
= expout_ptr
;
502 /* Indicate that the current parser invocation is completing a tag.
503 TAG is the type code of the tag, and PTR and LENGTH represent the
504 start of the tag name. */
507 parser_state::mark_completion_tag (enum type_code tag
, const char *ptr
,
510 gdb_assert (parse_completion
511 && (m_completion_state
.expout_tag_completion_type
513 && m_completion_state
.expout_completion_name
== NULL
514 && m_completion_state
.expout_last_struct
== -1);
515 gdb_assert (tag
== TYPE_CODE_UNION
516 || tag
== TYPE_CODE_STRUCT
517 || tag
== TYPE_CODE_ENUM
);
518 m_completion_state
.expout_tag_completion_type
= tag
;
519 m_completion_state
.expout_completion_name
.reset (xstrndup (ptr
, length
));
523 /* Recognize tokens that start with '$'. These include:
525 $regname A native register name or a "standard
528 $variable A convenience variable with a name chosen
531 $digits Value history with index <digits>, starting
532 from the first value which has index 1.
534 $$digits Value history with index <digits> relative
535 to the last value. I.e. $$0 is the last
536 value, $$1 is the one previous to that, $$2
537 is the one previous to $$1, etc.
539 $ | $0 | $$0 The last value in the value history.
541 $$ An abbreviation for the second to the last
542 value in the value history, I.e. $$1 */
545 write_dollar_variable (struct parser_state
*ps
, struct stoken str
)
547 struct block_symbol sym
;
548 struct bound_minimal_symbol msym
;
549 struct internalvar
*isym
= NULL
;
552 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
553 and $$digits (equivalent to $<-digits> if you could type that). */
557 /* Double dollar means negate the number and add -1 as well.
558 Thus $$ alone means -1. */
559 if (str
.length
>= 2 && str
.ptr
[1] == '$')
566 /* Just dollars (one or two). */
570 /* Is the rest of the token digits? */
571 for (; i
< str
.length
; i
++)
572 if (!(str
.ptr
[i
] >= '0' && str
.ptr
[i
] <= '9'))
576 i
= atoi (str
.ptr
+ 1 + negate
);
582 /* Handle tokens that refer to machine registers:
583 $ followed by a register name. */
584 i
= user_reg_map_name_to_regnum (ps
->gdbarch (),
585 str
.ptr
+ 1, str
.length
- 1);
587 goto handle_register
;
589 /* Any names starting with $ are probably debugger internal variables. */
591 copy
= copy_name (str
);
592 isym
= lookup_only_internalvar (copy
.c_str () + 1);
595 write_exp_elt_opcode (ps
, OP_INTERNALVAR
);
596 write_exp_elt_intern (ps
, isym
);
597 write_exp_elt_opcode (ps
, OP_INTERNALVAR
);
601 /* On some systems, such as HP-UX and hppa-linux, certain system routines
602 have names beginning with $ or $$. Check for those, first. */
604 sym
= lookup_symbol (copy
.c_str (), NULL
, VAR_DOMAIN
, NULL
);
607 write_exp_elt_opcode (ps
, OP_VAR_VALUE
);
608 write_exp_elt_block (ps
, sym
.block
);
609 write_exp_elt_sym (ps
, sym
.symbol
);
610 write_exp_elt_opcode (ps
, OP_VAR_VALUE
);
613 msym
= lookup_bound_minimal_symbol (copy
.c_str ());
616 write_exp_msymbol (ps
, msym
);
620 /* Any other names are assumed to be debugger internal variables. */
622 write_exp_elt_opcode (ps
, OP_INTERNALVAR
);
623 write_exp_elt_intern (ps
, create_internalvar (copy
.c_str () + 1));
624 write_exp_elt_opcode (ps
, OP_INTERNALVAR
);
627 write_exp_elt_opcode (ps
, OP_LAST
);
628 write_exp_elt_longcst (ps
, (LONGEST
) i
);
629 write_exp_elt_opcode (ps
, OP_LAST
);
632 write_exp_elt_opcode (ps
, OP_REGISTER
);
635 write_exp_string (ps
, str
);
636 write_exp_elt_opcode (ps
, OP_REGISTER
);
637 ps
->block_tracker
->update (ps
->expression_context_block
,
638 INNERMOST_BLOCK_FOR_REGISTERS
);
644 find_template_name_end (const char *p
)
647 int just_seen_right
= 0;
648 int just_seen_colon
= 0;
649 int just_seen_space
= 0;
651 if (!p
|| (*p
!= '<'))
662 /* In future, may want to allow these?? */
665 depth
++; /* start nested template */
666 if (just_seen_colon
|| just_seen_right
|| just_seen_space
)
667 return 0; /* but not after : or :: or > or space */
670 if (just_seen_colon
|| just_seen_right
)
671 return 0; /* end a (nested?) template */
672 just_seen_right
= 1; /* but not after : or :: */
673 if (--depth
== 0) /* also disallow >>, insist on > > */
674 return ++p
; /* if outermost ended, return */
677 if (just_seen_space
|| (just_seen_colon
> 1))
678 return 0; /* nested class spec coming up */
679 just_seen_colon
++; /* we allow :: but not :::: */
684 if (!((*p
>= 'a' && *p
<= 'z') || /* allow token chars */
685 (*p
>= 'A' && *p
<= 'Z') ||
686 (*p
>= '0' && *p
<= '9') ||
687 (*p
== '_') || (*p
== ',') || /* commas for template args */
688 (*p
== '&') || (*p
== '*') || /* pointer and ref types */
689 (*p
== '(') || (*p
== ')') || /* function types */
690 (*p
== '[') || (*p
== ']'))) /* array types */
704 /* Return a null-terminated temporary copy of the name of a string token.
706 Tokens that refer to names do so with explicit pointer and length,
707 so they can share the storage that lexptr is parsing.
708 When it is necessary to pass a name to a function that expects
709 a null-terminated string, the substring is copied out
710 into a separate block of storage. */
713 copy_name (struct stoken token
)
715 return std::string (token
.ptr
, token
.length
);
719 /* See comments on parser-defs.h. */
722 prefixify_expression (struct expression
*expr
, int last_struct
)
724 gdb_assert (expr
->nelts
> 0);
725 int len
= sizeof (struct expression
) + EXP_ELEM_TO_BYTES (expr
->nelts
);
726 struct expression
*temp
;
727 int inpos
= expr
->nelts
, outpos
= 0;
729 temp
= (struct expression
*) alloca (len
);
731 /* Copy the original expression into temp. */
732 memcpy (temp
, expr
, len
);
734 return prefixify_subexp (temp
, expr
, inpos
, outpos
, last_struct
);
737 /* Return the number of exp_elements in the postfix subexpression
738 of EXPR whose operator is at index ENDPOS - 1 in EXPR. */
741 length_of_subexp (struct expression
*expr
, int endpos
)
745 operator_length (expr
, endpos
, &oplen
, &args
);
749 oplen
+= length_of_subexp (expr
, endpos
- oplen
);
756 /* Sets *OPLENP to the length of the operator whose (last) index is
757 ENDPOS - 1 in EXPR, and sets *ARGSP to the number of arguments that
761 operator_length (const struct expression
*expr
, int endpos
, int *oplenp
,
764 expr
->language_defn
->expression_ops ()->operator_length (expr
, endpos
,
768 /* Default value for operator_length in exp_descriptor vectors. */
771 operator_length_standard (const struct expression
*expr
, int endpos
,
772 int *oplenp
, int *argsp
)
776 enum range_flag range_flag
;
780 error (_("?error in operator_length_standard"));
782 i
= (int) expr
->elts
[endpos
- 1].opcode
;
788 oplen
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
789 oplen
= 5 + BYTES_TO_EXP_ELEM (oplen
+ 1);
795 case OP_VAR_MSYM_VALUE
:
799 case OP_FUNC_STATIC_VAR
:
800 oplen
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
801 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
+ 1);
809 case OP_VAR_ENTRY_VALUE
:
820 args
= 1 + longest_to_int (expr
->elts
[endpos
- 2].longconst
);
824 oplen
= 5 + longest_to_int (expr
->elts
[endpos
- 2].longconst
);
828 case OP_OBJC_MSGCALL
: /* Objective C message (method) call. */
830 args
= 1 + longest_to_int (expr
->elts
[endpos
- 2].longconst
);
839 case UNOP_DYNAMIC_CAST
:
840 case UNOP_REINTERPRET_CAST
:
841 case UNOP_MEMVAL_TYPE
:
869 oplen
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
870 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
+ 1);
875 case STRUCTOP_STRUCT
:
882 case OP_OBJC_NSSTRING
: /* Objective C Foundation Class
883 NSString constant. */
884 case OP_OBJC_SELECTOR
: /* Objective C "@selector" pseudo-op. */
886 oplen
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
887 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
+ 1);
892 args
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
893 args
-= longest_to_int (expr
->elts
[endpos
- 3].longconst
);
903 case MULTI_SUBSCRIPT
:
905 args
= 1 + longest_to_int (expr
->elts
[endpos
- 2].longconst
);
908 case BINOP_ASSIGN_MODIFY
:
920 range_flag
= (enum range_flag
)
921 longest_to_int (expr
->elts
[endpos
- 2].longconst
);
923 /* Assume the range has 2 arguments (low bound and high bound), then
924 reduce the argument count if any bounds are set to default. */
926 if (range_flag
& RANGE_HAS_STRIDE
)
928 if (range_flag
& RANGE_LOW_BOUND_DEFAULT
)
930 if (range_flag
& RANGE_HIGH_BOUND_DEFAULT
)
936 args
= 1 + (i
< (int) BINOP_END
);
943 /* Copy the subexpression ending just before index INEND in INEXPR
944 into OUTEXPR, starting at index OUTBEG.
945 In the process, convert it from suffix to prefix form.
946 If LAST_STRUCT is -1, then this function always returns -1.
947 Otherwise, it returns the index of the subexpression which is the
948 left-hand-side of the expression at LAST_STRUCT. */
951 prefixify_subexp (struct expression
*inexpr
,
952 struct expression
*outexpr
, int inend
, int outbeg
,
961 operator_length (inexpr
, inend
, &oplen
, &args
);
963 /* Copy the final operator itself, from the end of the input
964 to the beginning of the output. */
966 memcpy (&outexpr
->elts
[outbeg
], &inexpr
->elts
[inend
],
967 EXP_ELEM_TO_BYTES (oplen
));
970 if (last_struct
== inend
)
971 result
= outbeg
- oplen
;
973 /* Find the lengths of the arg subexpressions. */
974 arglens
= (int *) alloca (args
* sizeof (int));
975 for (i
= args
- 1; i
>= 0; i
--)
977 oplen
= length_of_subexp (inexpr
, inend
);
982 /* Now copy each subexpression, preserving the order of
983 the subexpressions, but prefixifying each one.
984 In this loop, inend starts at the beginning of
985 the expression this level is working on
986 and marches forward over the arguments.
987 outbeg does similarly in the output. */
988 for (i
= 0; i
< args
; i
++)
994 r
= prefixify_subexp (inexpr
, outexpr
, inend
, outbeg
, last_struct
);
997 /* Return immediately. We probably have only parsed a
998 partial expression, so we don't want to try to reverse
999 the other operands. */
1008 /* Read an expression from the string *STRINGPTR points to,
1009 parse it, and return a pointer to a struct expression that we malloc.
1010 Use block BLOCK as the lexical context for variable names;
1011 if BLOCK is zero, use the block of the selected stack frame.
1012 Meanwhile, advance *STRINGPTR to point after the expression,
1013 at the first nonwhite character that is not part of the expression
1014 (possibly a null character).
1016 If COMMA is nonzero, stop if a comma is reached. */
1019 parse_exp_1 (const char **stringptr
, CORE_ADDR pc
, const struct block
*block
,
1020 int comma
, innermost_block_tracker
*tracker
)
1022 return parse_exp_in_context (stringptr
, pc
, block
, comma
, 0, NULL
,
1026 /* As for parse_exp_1, except that if VOID_CONTEXT_P, then
1027 no value is expected from the expression.
1028 OUT_SUBEXP is set when attempting to complete a field name; in this
1029 case it is set to the index of the subexpression on the
1030 left-hand-side of the struct op. If not doing such completion, it
1031 is left untouched. */
1033 static expression_up
1034 parse_exp_in_context (const char **stringptr
, CORE_ADDR pc
,
1035 const struct block
*block
,
1036 int comma
, int void_context_p
, int *out_subexp
,
1037 innermost_block_tracker
*tracker
,
1038 expr_completion_state
*cstate
)
1040 const struct language_defn
*lang
= NULL
;
1043 if (*stringptr
== 0 || **stringptr
== 0)
1044 error_no_arg (_("expression to compute"));
1046 const struct block
*expression_context_block
= block
;
1047 CORE_ADDR expression_context_pc
= 0;
1049 innermost_block_tracker local_tracker
;
1050 if (tracker
== nullptr)
1051 tracker
= &local_tracker
;
1053 /* If no context specified, try using the current frame, if any. */
1054 if (!expression_context_block
)
1055 expression_context_block
= get_selected_block (&expression_context_pc
);
1057 expression_context_pc
= BLOCK_ENTRY_PC (expression_context_block
);
1059 expression_context_pc
= pc
;
1061 /* Fall back to using the current source static context, if any. */
1063 if (!expression_context_block
)
1065 struct symtab_and_line cursal
= get_current_source_symtab_and_line ();
1067 expression_context_block
1068 = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (cursal
.symtab
),
1070 if (expression_context_block
)
1071 expression_context_pc
= BLOCK_ENTRY_PC (expression_context_block
);
1074 if (language_mode
== language_mode_auto
&& block
!= NULL
)
1076 /* Find the language associated to the given context block.
1077 Default to the current language if it can not be determined.
1079 Note that using the language corresponding to the current frame
1080 can sometimes give unexpected results. For instance, this
1081 routine is often called several times during the inferior
1082 startup phase to re-parse breakpoint expressions after
1083 a new shared library has been loaded. The language associated
1084 to the current frame at this moment is not relevant for
1085 the breakpoint. Using it would therefore be silly, so it seems
1086 better to rely on the current language rather than relying on
1087 the current frame language to parse the expression. That's why
1088 we do the following language detection only if the context block
1089 has been specifically provided. */
1090 struct symbol
*func
= block_linkage_function (block
);
1093 lang
= language_def (func
->language ());
1094 if (lang
== NULL
|| lang
->la_language
== language_unknown
)
1095 lang
= current_language
;
1098 lang
= current_language
;
1100 /* get_current_arch may reset CURRENT_LANGUAGE via select_frame.
1101 While we need CURRENT_LANGUAGE to be set to LANG (for lookup_symbol
1102 and others called from *.y) ensure CURRENT_LANGUAGE gets restored
1103 to the value matching SELECTED_FRAME as set by get_current_arch. */
1105 parser_state
ps (lang
, get_current_arch (), expression_context_block
,
1106 expression_context_pc
, comma
, *stringptr
,
1107 cstate
!= nullptr, tracker
);
1109 scoped_restore_current_language lang_saver
;
1110 set_language (lang
->la_language
);
1116 catch (const gdb_exception
&except
)
1118 /* If parsing for completion, allow this to succeed; but if no
1119 expression elements have been written, then there's nothing
1121 if (! ps
.parse_completion
|| ps
.expout_ptr
== 0)
1125 /* We have to operate on an "expression *", due to la_post_parser,
1126 which explains this funny-looking double release. */
1127 expression_up result
= ps
.release ();
1129 /* Convert expression from postfix form as generated by yacc
1130 parser, to a prefix form. */
1132 if (expressiondebug
)
1133 dump_raw_expression (result
.get (), gdb_stdlog
,
1134 "before conversion to prefix form");
1136 subexp
= prefixify_expression (result
.get (),
1137 ps
.m_completion_state
.expout_last_struct
);
1139 *out_subexp
= subexp
;
1141 lang
->post_parser (&result
, void_context_p
, ps
.parse_completion
, tracker
);
1143 if (expressiondebug
)
1144 dump_prefix_expression (result
.get (), gdb_stdlog
);
1146 if (cstate
!= nullptr)
1147 *cstate
= std::move (ps
.m_completion_state
);
1148 *stringptr
= ps
.lexptr
;
1152 /* Parse STRING as an expression, and complain if this fails
1153 to use up all of the contents of STRING. */
1156 parse_expression (const char *string
, innermost_block_tracker
*tracker
)
1158 expression_up exp
= parse_exp_1 (&string
, 0, 0, 0, tracker
);
1160 error (_("Junk after end of expression."));
1164 /* Same as parse_expression, but using the given language (LANG)
1165 to parse the expression. */
1168 parse_expression_with_language (const char *string
, enum language lang
)
1170 gdb::optional
<scoped_restore_current_language
> lang_saver
;
1171 if (current_language
->la_language
!= lang
)
1173 lang_saver
.emplace ();
1174 set_language (lang
);
1177 return parse_expression (string
);
1180 /* Parse STRING as an expression. If parsing ends in the middle of a
1181 field reference, return the type of the left-hand-side of the
1182 reference; furthermore, if the parsing ends in the field name,
1183 return the field name in *NAME. If the parsing ends in the middle
1184 of a field reference, but the reference is somehow invalid, throw
1185 an exception. In all other cases, return NULL. */
1188 parse_expression_for_completion (const char *string
,
1189 gdb::unique_xmalloc_ptr
<char> *name
,
1190 enum type_code
*code
)
1195 expr_completion_state cstate
;
1199 exp
= parse_exp_in_context (&string
, 0, 0, 0, 0, &subexp
,
1202 catch (const gdb_exception_error
&except
)
1204 /* Nothing, EXP remains NULL. */
1210 if (cstate
.expout_tag_completion_type
!= TYPE_CODE_UNDEF
)
1212 *code
= cstate
.expout_tag_completion_type
;
1213 *name
= std::move (cstate
.expout_completion_name
);
1217 if (cstate
.expout_last_struct
== -1)
1220 const char *fieldname
= extract_field_op (exp
.get (), &subexp
);
1221 if (fieldname
== NULL
)
1227 name
->reset (xstrdup (fieldname
));
1228 /* This might throw an exception. If so, we want to let it
1230 val
= evaluate_subexpression_type (exp
.get (), subexp
);
1232 return value_type (val
);
1235 /* Parse floating point value P of length LEN.
1236 Return false if invalid, true if valid.
1237 The successfully parsed number is stored in DATA in
1238 target format for floating-point type TYPE.
1240 NOTE: This accepts the floating point syntax that sscanf accepts. */
1243 parse_float (const char *p
, int len
,
1244 const struct type
*type
, gdb_byte
*data
)
1246 return target_float_from_string (data
, type
, std::string (p
, len
));
1249 /* This function avoids direct calls to fprintf
1250 in the parser generated debug code. */
1252 parser_fprintf (FILE *x
, const char *y
, ...)
1258 vfprintf_unfiltered (gdb_stderr
, y
, args
);
1261 fprintf_unfiltered (gdb_stderr
, " Unknown FILE used.\n");
1262 vfprintf_unfiltered (gdb_stderr
, y
, args
);
1267 /* Implementation of the exp_descriptor method operator_check. */
1270 operator_check_standard (struct expression
*exp
, int pos
,
1271 int (*objfile_func
) (struct objfile
*objfile
,
1275 const union exp_element
*const elts
= exp
->elts
;
1276 struct type
*type
= NULL
;
1277 struct objfile
*objfile
= NULL
;
1279 /* Extended operators should have been already handled by exp_descriptor
1280 iterate method of its specific language. */
1281 gdb_assert (elts
[pos
].opcode
< OP_EXTENDED0
);
1283 /* Track the callers of write_exp_elt_type for this table. */
1285 switch (elts
[pos
].opcode
)
1297 type
= elts
[pos
+ 1].type
;
1302 LONGEST arg
, nargs
= elts
[pos
+ 2].longconst
;
1304 for (arg
= 0; arg
< nargs
; arg
++)
1306 struct type
*inst_type
= elts
[pos
+ 3 + arg
].type
;
1307 struct objfile
*inst_objfile
= TYPE_OBJFILE (inst_type
);
1309 if (inst_objfile
&& (*objfile_func
) (inst_objfile
, data
))
1317 const struct block
*const block
= elts
[pos
+ 1].block
;
1318 const struct symbol
*const symbol
= elts
[pos
+ 2].symbol
;
1320 /* Check objfile where the variable itself is placed.
1321 SYMBOL_OBJ_SECTION (symbol) may be NULL. */
1322 if ((*objfile_func
) (symbol_objfile (symbol
), data
))
1325 /* Check objfile where is placed the code touching the variable. */
1326 objfile
= block_objfile (block
);
1328 type
= SYMBOL_TYPE (symbol
);
1331 case OP_VAR_MSYM_VALUE
:
1332 objfile
= elts
[pos
+ 1].objfile
;
1336 /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL. */
1338 if (type
&& TYPE_OBJFILE (type
)
1339 && (*objfile_func
) (TYPE_OBJFILE (type
), data
))
1341 if (objfile
&& (*objfile_func
) (objfile
, data
))
1347 /* Call OBJFILE_FUNC for any objfile found being referenced by EXP.
1348 OBJFILE_FUNC is never called with NULL OBJFILE. OBJFILE_FUNC get
1349 passed an arbitrary caller supplied DATA pointer. If OBJFILE_FUNC
1350 returns non-zero value then (any other) non-zero value is immediately
1351 returned to the caller. Otherwise zero is returned after iterating
1352 through whole EXP. */
1355 exp_iterate (struct expression
*exp
,
1356 int (*objfile_func
) (struct objfile
*objfile
, void *data
),
1361 for (endpos
= exp
->nelts
; endpos
> 0; )
1363 int pos
, args
, oplen
= 0;
1365 operator_length (exp
, endpos
, &oplen
, &args
);
1366 gdb_assert (oplen
> 0);
1368 pos
= endpos
- oplen
;
1369 if (exp
->language_defn
->expression_ops ()->operator_check (exp
, pos
,
1380 /* Helper for exp_uses_objfile. */
1383 exp_uses_objfile_iter (struct objfile
*exp_objfile
, void *objfile_voidp
)
1385 struct objfile
*objfile
= (struct objfile
*) objfile_voidp
;
1387 if (exp_objfile
->separate_debug_objfile_backlink
)
1388 exp_objfile
= exp_objfile
->separate_debug_objfile_backlink
;
1390 return exp_objfile
== objfile
;
1393 /* Return 1 if EXP uses OBJFILE (and will become dangling when OBJFILE
1394 is unloaded), otherwise return 0. OBJFILE must not be a separate debug info
1398 exp_uses_objfile (struct expression
*exp
, struct objfile
*objfile
)
1400 gdb_assert (objfile
->separate_debug_objfile_backlink
== NULL
);
1402 return exp_iterate (exp
, exp_uses_objfile_iter
, objfile
);
1405 /* Reallocate the `expout' pointer inside PS so that it can accommodate
1406 at least LENELT expression elements. This function does nothing if
1407 there is enough room for the elements. */
1410 increase_expout_size (struct expr_builder
*ps
, size_t lenelt
)
1412 if ((ps
->expout_ptr
+ lenelt
) >= ps
->expout_size
)
1414 ps
->expout_size
= std::max (ps
->expout_size
* 2,
1415 ps
->expout_ptr
+ lenelt
+ 10);
1416 ps
->expout
.reset (XRESIZEVAR (expression
,
1417 ps
->expout
.release (),
1418 (sizeof (struct expression
)
1419 + EXP_ELEM_TO_BYTES (ps
->expout_size
))));
1423 void _initialize_parse ();
1425 _initialize_parse ()
1427 add_setshow_zuinteger_cmd ("expression", class_maintenance
,
1429 _("Set expression debugging."),
1430 _("Show expression debugging."),
1431 _("When non-zero, the internal representation "
1432 "of expressions will be printed."),
1434 show_expressiondebug
,
1435 &setdebuglist
, &showdebuglist
);
1436 add_setshow_boolean_cmd ("parser", class_maintenance
,
1438 _("Set parser debugging."),
1439 _("Show parser debugging."),
1440 _("When non-zero, expression parser "
1441 "tracing will be enabled."),
1444 &setdebuglist
, &showdebuglist
);