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