]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/parse.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / parse.c
1 /* Parse expressions for GDB.
2
3 Copyright (C) 1986-2023 Free Software Foundation, Inc.
4
5 Modified from expread.y by the Department of Computer Science at the
6 State University of New York at Buffalo, 1991.
7
8 This file is part of GDB.
9
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.
14
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.
19
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/>. */
22
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. */
31
32 #include "defs.h"
33 #include <ctype.h>
34 #include "arch-utils.h"
35 #include "symtab.h"
36 #include "gdbtypes.h"
37 #include "frame.h"
38 #include "expression.h"
39 #include "value.h"
40 #include "command.h"
41 #include "language.h"
42 #include "parser-defs.h"
43 #include "gdbcmd.h"
44 #include "symfile.h" /* for overlay functions */
45 #include "inferior.h"
46 #include "target-float.h"
47 #include "block.h"
48 #include "source.h"
49 #include "objfiles.h"
50 #include "user-regs.h"
51 #include <algorithm>
52 #include "gdbsupport/gdb_optional.h"
53 #include "c-exp.h"
54
55 static unsigned int expressiondebug = 0;
56 static void
57 show_expressiondebug (struct ui_file *file, int from_tty,
58 struct cmd_list_element *c, const char *value)
59 {
60 gdb_printf (file, _("Expression debugging is %s.\n"), value);
61 }
62
63
64 /* True if an expression parser should set yydebug. */
65 bool parser_debug;
66
67 static void
68 show_parserdebug (struct ui_file *file, int from_tty,
69 struct cmd_list_element *c, const char *value)
70 {
71 gdb_printf (file, _("Parser debugging is %s.\n"), value);
72 }
73
74
75 static expression_up parse_exp_in_context
76 (const char **, CORE_ADDR,
77 const struct block *, int,
78 bool, innermost_block_tracker *,
79 std::unique_ptr<expr_completion_base> *);
80
81 /* Documented at it's declaration. */
82
83 void
84 innermost_block_tracker::update (const struct block *b,
85 innermost_block_tracker_types t)
86 {
87 if ((m_types & t) != 0
88 && (m_innermost_block == NULL
89 || contained_in (b, m_innermost_block)))
90 m_innermost_block = b;
91 }
92
93 \f
94
95 /* Return the type of MSYMBOL, a minimal symbol of OBJFILE. If
96 ADDRESS_P is not NULL, set it to the MSYMBOL's resolved
97 address. */
98
99 type *
100 find_minsym_type_and_address (minimal_symbol *msymbol,
101 struct objfile *objfile,
102 CORE_ADDR *address_p)
103 {
104 bound_minimal_symbol bound_msym = {msymbol, objfile};
105 struct obj_section *section = msymbol->obj_section (objfile);
106 enum minimal_symbol_type type = msymbol->type ();
107
108 bool is_tls = (section != NULL
109 && section->the_bfd_section->flags & SEC_THREAD_LOCAL);
110
111 /* The minimal symbol might point to a function descriptor;
112 resolve it to the actual code address instead. */
113 CORE_ADDR addr;
114 if (is_tls)
115 {
116 /* Addresses of TLS symbols are really offsets into a
117 per-objfile/per-thread storage block. */
118 addr = bound_msym.minsym->value_raw_address ();
119 }
120 else if (msymbol_is_function (objfile, msymbol, &addr))
121 {
122 if (addr != bound_msym.value_address ())
123 {
124 /* This means we resolved a function descriptor, and we now
125 have an address for a code/text symbol instead of a data
126 symbol. */
127 if (msymbol->type () == mst_data_gnu_ifunc)
128 type = mst_text_gnu_ifunc;
129 else
130 type = mst_text;
131 section = NULL;
132 }
133 }
134 else
135 addr = bound_msym.value_address ();
136
137 if (overlay_debugging)
138 addr = symbol_overlayed_address (addr, section);
139
140 if (is_tls)
141 {
142 /* Skip translation if caller does not need the address. */
143 if (address_p != NULL)
144 *address_p = target_translate_tls_address (objfile, addr);
145 return objfile_type (objfile)->nodebug_tls_symbol;
146 }
147
148 if (address_p != NULL)
149 *address_p = addr;
150
151 switch (type)
152 {
153 case mst_text:
154 case mst_file_text:
155 case mst_solib_trampoline:
156 return objfile_type (objfile)->nodebug_text_symbol;
157
158 case mst_text_gnu_ifunc:
159 return objfile_type (objfile)->nodebug_text_gnu_ifunc_symbol;
160
161 case mst_data:
162 case mst_file_data:
163 case mst_bss:
164 case mst_file_bss:
165 return objfile_type (objfile)->nodebug_data_symbol;
166
167 case mst_slot_got_plt:
168 return objfile_type (objfile)->nodebug_got_plt_symbol;
169
170 default:
171 return objfile_type (objfile)->nodebug_unknown_symbol;
172 }
173 }
174
175 bool
176 expr_complete_tag::complete (struct expression *exp,
177 completion_tracker &tracker)
178 {
179 collect_symbol_completion_matches_type (tracker, m_name.get (),
180 m_name.get (), m_code);
181 return true;
182 }
183
184 /* See parser-defs.h. */
185
186 void
187 parser_state::mark_struct_expression (expr::structop_base_operation *op)
188 {
189 gdb_assert (parse_completion && m_completion_state == nullptr);
190 m_completion_state.reset (new expr_complete_structop (op));
191 }
192
193 /* Indicate that the current parser invocation is completing a tag.
194 TAG is the type code of the tag, and PTR and LENGTH represent the
195 start of the tag name. */
196
197 void
198 parser_state::mark_completion_tag (enum type_code tag, const char *ptr,
199 int length)
200 {
201 gdb_assert (parse_completion && m_completion_state == nullptr);
202 gdb_assert (tag == TYPE_CODE_UNION
203 || tag == TYPE_CODE_STRUCT
204 || tag == TYPE_CODE_ENUM);
205 m_completion_state.reset
206 (new expr_complete_tag (tag, make_unique_xstrndup (ptr, length)));
207 }
208
209 /* See parser-defs.h. */
210
211 void
212 parser_state::push_c_string (int kind, struct stoken_vector *vec)
213 {
214 std::vector<std::string> data (vec->len);
215 for (int i = 0; i < vec->len; ++i)
216 data[i] = std::string (vec->tokens[i].ptr, vec->tokens[i].length);
217
218 push_new<expr::c_string_operation> ((enum c_string_type_values) kind,
219 std::move (data));
220 }
221
222 /* See parser-defs.h. */
223
224 void
225 parser_state::push_symbol (const char *name, block_symbol sym)
226 {
227 if (sym.symbol != nullptr)
228 {
229 if (symbol_read_needs_frame (sym.symbol))
230 block_tracker->update (sym);
231 push_new<expr::var_value_operation> (sym);
232 }
233 else
234 {
235 struct bound_minimal_symbol msymbol = lookup_bound_minimal_symbol (name);
236 if (msymbol.minsym != NULL)
237 push_new<expr::var_msym_value_operation> (msymbol);
238 else if (!have_full_symbols () && !have_partial_symbols ())
239 error (_("No symbol table is loaded. Use the \"file\" command."));
240 else
241 error (_("No symbol \"%s\" in current context."), name);
242 }
243 }
244
245 /* See parser-defs.h. */
246
247 void
248 parser_state::push_dollar (struct stoken str)
249 {
250 struct block_symbol sym;
251 struct bound_minimal_symbol msym;
252 struct internalvar *isym = NULL;
253 std::string copy;
254
255 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
256 and $$digits (equivalent to $<-digits> if you could type that). */
257
258 int negate = 0;
259 int i = 1;
260 /* Double dollar means negate the number and add -1 as well.
261 Thus $$ alone means -1. */
262 if (str.length >= 2 && str.ptr[1] == '$')
263 {
264 negate = 1;
265 i = 2;
266 }
267 if (i == str.length)
268 {
269 /* Just dollars (one or two). */
270 i = -negate;
271 goto handle_last;
272 }
273 /* Is the rest of the token digits? */
274 for (; i < str.length; i++)
275 if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
276 break;
277 if (i == str.length)
278 {
279 i = atoi (str.ptr + 1 + negate);
280 if (negate)
281 i = -i;
282 goto handle_last;
283 }
284
285 /* Handle tokens that refer to machine registers:
286 $ followed by a register name. */
287 i = user_reg_map_name_to_regnum (gdbarch (),
288 str.ptr + 1, str.length - 1);
289 if (i >= 0)
290 goto handle_register;
291
292 /* Any names starting with $ are probably debugger internal variables. */
293
294 copy = copy_name (str);
295 isym = lookup_only_internalvar (copy.c_str () + 1);
296 if (isym)
297 {
298 push_new<expr::internalvar_operation> (isym);
299 return;
300 }
301
302 /* On some systems, such as HP-UX and hppa-linux, certain system routines
303 have names beginning with $ or $$. Check for those, first. */
304
305 sym = lookup_symbol (copy.c_str (), NULL, VAR_DOMAIN, NULL);
306 if (sym.symbol)
307 {
308 push_new<expr::var_value_operation> (sym);
309 return;
310 }
311 msym = lookup_bound_minimal_symbol (copy.c_str ());
312 if (msym.minsym)
313 {
314 push_new<expr::var_msym_value_operation> (msym);
315 return;
316 }
317
318 /* Any other names are assumed to be debugger internal variables. */
319
320 push_new<expr::internalvar_operation>
321 (create_internalvar (copy.c_str () + 1));
322 return;
323 handle_last:
324 push_new<expr::last_operation> (i);
325 return;
326 handle_register:
327 str.length--;
328 str.ptr++;
329 push_new<expr::register_operation> (copy_name (str));
330 block_tracker->update (expression_context_block,
331 INNERMOST_BLOCK_FOR_REGISTERS);
332 return;
333 }
334
335 \f
336
337 const char *
338 find_template_name_end (const char *p)
339 {
340 int depth = 1;
341 int just_seen_right = 0;
342 int just_seen_colon = 0;
343 int just_seen_space = 0;
344
345 if (!p || (*p != '<'))
346 return 0;
347
348 while (*++p)
349 {
350 switch (*p)
351 {
352 case '\'':
353 case '\"':
354 case '{':
355 case '}':
356 /* In future, may want to allow these?? */
357 return 0;
358 case '<':
359 depth++; /* start nested template */
360 if (just_seen_colon || just_seen_right || just_seen_space)
361 return 0; /* but not after : or :: or > or space */
362 break;
363 case '>':
364 if (just_seen_colon || just_seen_right)
365 return 0; /* end a (nested?) template */
366 just_seen_right = 1; /* but not after : or :: */
367 if (--depth == 0) /* also disallow >>, insist on > > */
368 return ++p; /* if outermost ended, return */
369 break;
370 case ':':
371 if (just_seen_space || (just_seen_colon > 1))
372 return 0; /* nested class spec coming up */
373 just_seen_colon++; /* we allow :: but not :::: */
374 break;
375 case ' ':
376 break;
377 default:
378 if (!((*p >= 'a' && *p <= 'z') || /* allow token chars */
379 (*p >= 'A' && *p <= 'Z') ||
380 (*p >= '0' && *p <= '9') ||
381 (*p == '_') || (*p == ',') || /* commas for template args */
382 (*p == '&') || (*p == '*') || /* pointer and ref types */
383 (*p == '(') || (*p == ')') || /* function types */
384 (*p == '[') || (*p == ']'))) /* array types */
385 return 0;
386 }
387 if (*p != ' ')
388 just_seen_space = 0;
389 if (*p != ':')
390 just_seen_colon = 0;
391 if (*p != '>')
392 just_seen_right = 0;
393 }
394 return 0;
395 }
396 \f
397
398 /* Return a null-terminated temporary copy of the name of a string token.
399
400 Tokens that refer to names do so with explicit pointer and length,
401 so they can share the storage that lexptr is parsing.
402 When it is necessary to pass a name to a function that expects
403 a null-terminated string, the substring is copied out
404 into a separate block of storage. */
405
406 std::string
407 copy_name (struct stoken token)
408 {
409 return std::string (token.ptr, token.length);
410 }
411 \f
412
413 /* Read an expression from the string *STRINGPTR points to,
414 parse it, and return a pointer to a struct expression that we malloc.
415 Use block BLOCK as the lexical context for variable names;
416 if BLOCK is zero, use the block of the selected stack frame.
417 Meanwhile, advance *STRINGPTR to point after the expression,
418 at the first nonwhite character that is not part of the expression
419 (possibly a null character).
420
421 If COMMA is nonzero, stop if a comma is reached. */
422
423 expression_up
424 parse_exp_1 (const char **stringptr, CORE_ADDR pc, const struct block *block,
425 int comma, innermost_block_tracker *tracker)
426 {
427 return parse_exp_in_context (stringptr, pc, block, comma, false,
428 tracker, nullptr);
429 }
430
431 /* As for parse_exp_1, except that if VOID_CONTEXT_P, then
432 no value is expected from the expression. */
433
434 static expression_up
435 parse_exp_in_context (const char **stringptr, CORE_ADDR pc,
436 const struct block *block,
437 int comma, bool void_context_p,
438 innermost_block_tracker *tracker,
439 std::unique_ptr<expr_completion_base> *completer)
440 {
441 const struct language_defn *lang = NULL;
442
443 if (*stringptr == 0 || **stringptr == 0)
444 error_no_arg (_("expression to compute"));
445
446 const struct block *expression_context_block = block;
447 CORE_ADDR expression_context_pc = 0;
448
449 innermost_block_tracker local_tracker;
450 if (tracker == nullptr)
451 tracker = &local_tracker;
452
453 /* If no context specified, try using the current frame, if any. */
454 if (!expression_context_block)
455 expression_context_block = get_selected_block (&expression_context_pc);
456 else if (pc == 0)
457 expression_context_pc = expression_context_block->entry_pc ();
458 else
459 expression_context_pc = pc;
460
461 /* Fall back to using the current source static context, if any. */
462
463 if (!expression_context_block)
464 {
465 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
466
467 if (cursal.symtab)
468 expression_context_block
469 = cursal.symtab->compunit ()->blockvector ()->static_block ();
470
471 if (expression_context_block)
472 expression_context_pc = expression_context_block->entry_pc ();
473 }
474
475 if (language_mode == language_mode_auto && block != NULL)
476 {
477 /* Find the language associated to the given context block.
478 Default to the current language if it can not be determined.
479
480 Note that using the language corresponding to the current frame
481 can sometimes give unexpected results. For instance, this
482 routine is often called several times during the inferior
483 startup phase to re-parse breakpoint expressions after
484 a new shared library has been loaded. The language associated
485 to the current frame at this moment is not relevant for
486 the breakpoint. Using it would therefore be silly, so it seems
487 better to rely on the current language rather than relying on
488 the current frame language to parse the expression. That's why
489 we do the following language detection only if the context block
490 has been specifically provided. */
491 struct symbol *func = block_linkage_function (block);
492
493 if (func != NULL)
494 lang = language_def (func->language ());
495 if (lang == NULL || lang->la_language == language_unknown)
496 lang = current_language;
497 }
498 else
499 lang = current_language;
500
501 /* get_current_arch may reset CURRENT_LANGUAGE via select_frame.
502 While we need CURRENT_LANGUAGE to be set to LANG (for lookup_symbol
503 and others called from *.y) ensure CURRENT_LANGUAGE gets restored
504 to the value matching SELECTED_FRAME as set by get_current_arch. */
505
506 parser_state ps (lang, get_current_arch (), expression_context_block,
507 expression_context_pc, comma, *stringptr,
508 completer != nullptr, tracker, void_context_p);
509
510 scoped_restore_current_language lang_saver;
511 set_language (lang->la_language);
512
513 try
514 {
515 lang->parser (&ps);
516 }
517 catch (const gdb_exception &except)
518 {
519 /* If parsing for completion, allow this to succeed; but if no
520 expression elements have been written, then there's nothing
521 to do, so fail. */
522 if (! ps.parse_completion || ps.expout->op == nullptr)
523 throw;
524 }
525
526 expression_up result = ps.release ();
527 result->op->set_outermost ();
528
529 if (expressiondebug)
530 result->dump (gdb_stdlog);
531
532 if (completer != nullptr)
533 *completer = std::move (ps.m_completion_state);
534 *stringptr = ps.lexptr;
535 return result;
536 }
537
538 /* Parse STRING as an expression, and complain if this fails to use up
539 all of the contents of STRING. TRACKER, if non-null, will be
540 updated by the parser. VOID_CONTEXT_P should be true to indicate
541 that the expression may be expected to return a value with void
542 type. Parsers are free to ignore this, or to use it to help with
543 overload resolution decisions. */
544
545 expression_up
546 parse_expression (const char *string, innermost_block_tracker *tracker,
547 bool void_context_p)
548 {
549 expression_up exp = parse_exp_in_context (&string, 0, nullptr, 0,
550 void_context_p,
551 tracker, nullptr);
552 if (*string)
553 error (_("Junk after end of expression."));
554 return exp;
555 }
556
557 /* Same as parse_expression, but using the given language (LANG)
558 to parse the expression. */
559
560 expression_up
561 parse_expression_with_language (const char *string, enum language lang)
562 {
563 gdb::optional<scoped_restore_current_language> lang_saver;
564 if (current_language->la_language != lang)
565 {
566 lang_saver.emplace ();
567 set_language (lang);
568 }
569
570 return parse_expression (string);
571 }
572
573 /* Parse STRING as an expression. If the parse is marked for
574 completion, set COMPLETER and return the expression. In all other
575 cases, return NULL. */
576
577 expression_up
578 parse_expression_for_completion
579 (const char *string,
580 std::unique_ptr<expr_completion_base> *completer)
581 {
582 expression_up exp;
583
584 try
585 {
586 exp = parse_exp_in_context (&string, 0, 0, 0, false, nullptr, completer);
587 }
588 catch (const gdb_exception_error &except)
589 {
590 /* Nothing, EXP remains NULL. */
591 }
592
593 /* If we didn't get a completion result, be sure to also not return
594 an expression to our caller. */
595 if (*completer == nullptr)
596 return nullptr;
597
598 return exp;
599 }
600
601 /* Parse floating point value P of length LEN.
602 Return false if invalid, true if valid.
603 The successfully parsed number is stored in DATA in
604 target format for floating-point type TYPE.
605
606 NOTE: This accepts the floating point syntax that sscanf accepts. */
607
608 bool
609 parse_float (const char *p, int len,
610 const struct type *type, gdb_byte *data)
611 {
612 return target_float_from_string (data, type, std::string (p, len));
613 }
614
615 /* Return true if the number N_SIGN * N fits in a type with TYPE_BITS and
616 TYPE_SIGNED_P. N_SIGNED is either 1 or -1. */
617
618 bool
619 fits_in_type (int n_sign, ULONGEST n, int type_bits, bool type_signed_p)
620 {
621 /* Normalize -0. */
622 if (n == 0 && n_sign == -1)
623 n_sign = 1;
624
625 if (n_sign == -1 && !type_signed_p)
626 /* Can't fit a negative number in an unsigned type. */
627 return false;
628
629 if (type_bits > sizeof (ULONGEST) * 8)
630 return true;
631
632 ULONGEST smax = (ULONGEST)1 << (type_bits - 1);
633 if (n_sign == -1)
634 {
635 /* Negative number, signed type. */
636 return (n <= smax);
637 }
638 else if (n_sign == 1 && type_signed_p)
639 {
640 /* Positive number, signed type. */
641 return (n < smax);
642 }
643 else if (n_sign == 1 && !type_signed_p)
644 {
645 /* Positive number, unsigned type. */
646 return ((n >> 1) >> (type_bits - 1)) == 0;
647 }
648 else
649 gdb_assert_not_reached ("");
650 }
651 \f
652 /* This function avoids direct calls to fprintf
653 in the parser generated debug code. */
654 void
655 parser_fprintf (FILE *x, const char *y, ...)
656 {
657 va_list args;
658
659 va_start (args, y);
660 if (x == stderr)
661 gdb_vprintf (gdb_stderr, y, args);
662 else
663 {
664 gdb_printf (gdb_stderr, " Unknown FILE used.\n");
665 gdb_vprintf (gdb_stderr, y, args);
666 }
667 va_end (args);
668 }
669
670 /* Return rue if EXP uses OBJFILE (and will become dangling when
671 OBJFILE is unloaded), otherwise return false. OBJFILE must not be
672 a separate debug info file. */
673
674 bool
675 exp_uses_objfile (struct expression *exp, struct objfile *objfile)
676 {
677 gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
678
679 return exp->op->uses_objfile (objfile);
680 }
681
682 void _initialize_parse ();
683 void
684 _initialize_parse ()
685 {
686 add_setshow_zuinteger_cmd ("expression", class_maintenance,
687 &expressiondebug,
688 _("Set expression debugging."),
689 _("Show expression debugging."),
690 _("When non-zero, the internal representation "
691 "of expressions will be printed."),
692 NULL,
693 show_expressiondebug,
694 &setdebuglist, &showdebuglist);
695 add_setshow_boolean_cmd ("parser", class_maintenance,
696 &parser_debug,
697 _("Set parser debugging."),
698 _("Show parser debugging."),
699 _("When non-zero, expression parser "
700 "tracing will be enabled."),
701 NULL,
702 show_parserdebug,
703 &setdebuglist, &showdebuglist);
704 }