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