gdb: move f_language class into a header file
[binutils-gdb.git] / gdb / parse.c
1 /* Parse expressions for GDB.
2
3 Copyright (C) 1986-2020 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
54 /* Standard set of definitions for printing, dumping, prefixifying,
55 * and evaluating expressions. */
56
57 const struct exp_descriptor exp_descriptor_standard =
58 {
59 print_subexp_standard,
60 operator_length_standard,
61 operator_check_standard,
62 op_name_standard,
63 dump_subexp_body_standard,
64 evaluate_subexp_standard
65 };
66 \f
67 static unsigned int expressiondebug = 0;
68 static void
69 show_expressiondebug (struct ui_file *file, int from_tty,
70 struct cmd_list_element *c, const char *value)
71 {
72 fprintf_filtered (file, _("Expression debugging is %s.\n"), value);
73 }
74
75
76 /* True if an expression parser should set yydebug. */
77 bool parser_debug;
78
79 static void
80 show_parserdebug (struct ui_file *file, int from_tty,
81 struct cmd_list_element *c, const char *value)
82 {
83 fprintf_filtered (file, _("Parser debugging is %s.\n"), value);
84 }
85
86
87 static int prefixify_subexp (struct expression *, struct expression *, int,
88 int, int);
89
90 static expression_up parse_exp_in_context (const char **, CORE_ADDR,
91 const struct block *, int,
92 int, int *,
93 innermost_block_tracker *,
94 expr_completion_state *);
95
96 static void increase_expout_size (struct expr_builder *ps, size_t lenelt);
97
98
99 /* Documented at it's declaration. */
100
101 void
102 innermost_block_tracker::update (const struct block *b,
103 innermost_block_tracker_types t)
104 {
105 if ((m_types & t) != 0
106 && (m_innermost_block == NULL
107 || contained_in (b, m_innermost_block)))
108 m_innermost_block = b;
109 }
110
111 \f
112
113 /* See definition in parser-defs.h. */
114
115 expr_builder::expr_builder (const struct language_defn *lang,
116 struct gdbarch *gdbarch)
117 : expout_size (10),
118 expout (XNEWVAR (expression,
119 (sizeof (expression)
120 + EXP_ELEM_TO_BYTES (expout_size)))),
121 expout_ptr (0)
122 {
123 expout->language_defn = lang;
124 expout->gdbarch = gdbarch;
125 }
126
127 expression_up
128 expr_builder::release ()
129 {
130 /* Record the actual number of expression elements, and then
131 reallocate the expression memory so that we free up any
132 excess elements. */
133
134 expout->nelts = expout_ptr;
135 expout.reset (XRESIZEVAR (expression, expout.release (),
136 (sizeof (expression)
137 + EXP_ELEM_TO_BYTES (expout_ptr))));
138
139 return std::move (expout);
140 }
141
142 /* This page contains the functions for adding data to the struct expression
143 being constructed. */
144
145 /* Add one element to the end of the expression. */
146
147 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
148 a register through here. */
149
150 static void
151 write_exp_elt (struct expr_builder *ps, const union exp_element *expelt)
152 {
153 if (ps->expout_ptr >= ps->expout_size)
154 {
155 ps->expout_size *= 2;
156 ps->expout.reset (XRESIZEVAR (expression, ps->expout.release (),
157 (sizeof (expression)
158 + EXP_ELEM_TO_BYTES (ps->expout_size))));
159 }
160 ps->expout->elts[ps->expout_ptr++] = *expelt;
161 }
162
163 void
164 write_exp_elt_opcode (struct expr_builder *ps, enum exp_opcode expelt)
165 {
166 union exp_element tmp;
167
168 memset (&tmp, 0, sizeof (union exp_element));
169 tmp.opcode = expelt;
170 write_exp_elt (ps, &tmp);
171 }
172
173 void
174 write_exp_elt_sym (struct expr_builder *ps, struct symbol *expelt)
175 {
176 union exp_element tmp;
177
178 memset (&tmp, 0, sizeof (union exp_element));
179 tmp.symbol = expelt;
180 write_exp_elt (ps, &tmp);
181 }
182
183 static void
184 write_exp_elt_msym (struct expr_builder *ps, minimal_symbol *expelt)
185 {
186 union exp_element tmp;
187
188 memset (&tmp, 0, sizeof (union exp_element));
189 tmp.msymbol = expelt;
190 write_exp_elt (ps, &tmp);
191 }
192
193 void
194 write_exp_elt_block (struct expr_builder *ps, const struct block *b)
195 {
196 union exp_element tmp;
197
198 memset (&tmp, 0, sizeof (union exp_element));
199 tmp.block = b;
200 write_exp_elt (ps, &tmp);
201 }
202
203 void
204 write_exp_elt_objfile (struct expr_builder *ps, struct objfile *objfile)
205 {
206 union exp_element tmp;
207
208 memset (&tmp, 0, sizeof (union exp_element));
209 tmp.objfile = objfile;
210 write_exp_elt (ps, &tmp);
211 }
212
213 void
214 write_exp_elt_longcst (struct expr_builder *ps, LONGEST expelt)
215 {
216 union exp_element tmp;
217
218 memset (&tmp, 0, sizeof (union exp_element));
219 tmp.longconst = expelt;
220 write_exp_elt (ps, &tmp);
221 }
222
223 void
224 write_exp_elt_floatcst (struct expr_builder *ps, const gdb_byte expelt[16])
225 {
226 union exp_element tmp;
227 int index;
228
229 for (index = 0; index < 16; index++)
230 tmp.floatconst[index] = expelt[index];
231
232 write_exp_elt (ps, &tmp);
233 }
234
235 void
236 write_exp_elt_type (struct expr_builder *ps, struct type *expelt)
237 {
238 union exp_element tmp;
239
240 memset (&tmp, 0, sizeof (union exp_element));
241 tmp.type = expelt;
242 write_exp_elt (ps, &tmp);
243 }
244
245 void
246 write_exp_elt_intern (struct expr_builder *ps, struct internalvar *expelt)
247 {
248 union exp_element tmp;
249
250 memset (&tmp, 0, sizeof (union exp_element));
251 tmp.internalvar = expelt;
252 write_exp_elt (ps, &tmp);
253 }
254
255 /* Add a string constant to the end of the expression.
256
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.
266
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
273 course. */
274
275
276 void
277 write_exp_string (struct expr_builder *ps, struct stoken str)
278 {
279 int len = str.length;
280 size_t lenelt;
281 char *strdata;
282
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). */
287
288 lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
289
290 increase_expout_size (ps, lenelt);
291
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
295 element. */
296
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);
303 }
304
305 /* Add a vector of string constants to the end of the expression.
306
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.
310
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.
314
315 Next, each string in VEC is written. The length is written as a
316 long constant, followed by the contents of the string. */
317
318 void
319 write_exp_string_vector (struct expr_builder *ps, int type,
320 struct stoken_vector *vec)
321 {
322 int i, len;
323 size_t n_slots;
324
325 /* Compute the size. We compute the size in number of slots to
326 avoid issues with string padding. */
327 n_slots = 0;
328 for (i = 0; i < vec->len; ++i)
329 {
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);
333 }
334
335 /* One more slot for the type of the string. */
336 ++n_slots;
337
338 /* Now compute a phony string length. */
339 len = EXP_ELEM_TO_BYTES (n_slots) - 1;
340
341 n_slots += 4;
342 increase_expout_size (ps, n_slots);
343
344 write_exp_elt_opcode (ps, OP_STRING);
345 write_exp_elt_longcst (ps, len);
346 write_exp_elt_longcst (ps, type);
347
348 for (i = 0; i < vec->len; ++i)
349 {
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);
354 }
355
356 write_exp_elt_longcst (ps, len);
357 write_exp_elt_opcode (ps, OP_STRING);
358 }
359
360 /* Add a bitstring constant to the end of the expression.
361
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. */
370
371 void
372 write_exp_bitstring (struct expr_builder *ps, struct stoken str)
373 {
374 int bits = str.length; /* length in bits */
375 int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
376 size_t lenelt;
377 char *strdata;
378
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. */
382
383 lenelt = 2 + BYTES_TO_EXP_ELEM (len);
384
385 increase_expout_size (ps, lenelt);
386
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. */
390
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);
396 }
397
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
400 address. */
401
402 type *
403 find_minsym_type_and_address (minimal_symbol *msymbol,
404 struct objfile *objfile,
405 CORE_ADDR *address_p)
406 {
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);
410
411 bool is_tls = (section != NULL
412 && section->the_bfd_section->flags & SEC_THREAD_LOCAL);
413
414 /* The minimal symbol might point to a function descriptor;
415 resolve it to the actual code address instead. */
416 CORE_ADDR addr;
417 if (is_tls)
418 {
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);
422 }
423 else if (msymbol_is_function (objfile, msymbol, &addr))
424 {
425 if (addr != BMSYMBOL_VALUE_ADDRESS (bound_msym))
426 {
427 /* This means we resolved a function descriptor, and we now
428 have an address for a code/text symbol instead of a data
429 symbol. */
430 if (MSYMBOL_TYPE (msymbol) == mst_data_gnu_ifunc)
431 type = mst_text_gnu_ifunc;
432 else
433 type = mst_text;
434 section = NULL;
435 }
436 }
437 else
438 addr = BMSYMBOL_VALUE_ADDRESS (bound_msym);
439
440 if (overlay_debugging)
441 addr = symbol_overlayed_address (addr, section);
442
443 if (is_tls)
444 {
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;
449 }
450
451 if (address_p != NULL)
452 *address_p = addr;
453
454 switch (type)
455 {
456 case mst_text:
457 case mst_file_text:
458 case mst_solib_trampoline:
459 return objfile_type (objfile)->nodebug_text_symbol;
460
461 case mst_text_gnu_ifunc:
462 return objfile_type (objfile)->nodebug_text_gnu_ifunc_symbol;
463
464 case mst_data:
465 case mst_file_data:
466 case mst_bss:
467 case mst_file_bss:
468 return objfile_type (objfile)->nodebug_data_symbol;
469
470 case mst_slot_got_plt:
471 return objfile_type (objfile)->nodebug_got_plt_symbol;
472
473 default:
474 return objfile_type (objfile)->nodebug_unknown_symbol;
475 }
476 }
477
478 /* Add the appropriate elements for a minimal symbol to the end of
479 the expression. */
480
481 void
482 write_exp_msymbol (struct expr_builder *ps,
483 struct bound_minimal_symbol bound_msym)
484 {
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);
489 }
490
491 /* See parser-defs.h. */
492
493 void
494 parser_state::mark_struct_expression ()
495 {
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;
500 }
501
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. */
505
506 void
507 parser_state::mark_completion_tag (enum type_code tag, const char *ptr,
508 int length)
509 {
510 gdb_assert (parse_completion
511 && (m_completion_state.expout_tag_completion_type
512 == TYPE_CODE_UNDEF)
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));
520 }
521
522 \f
523 /* Recognize tokens that start with '$'. These include:
524
525 $regname A native register name or a "standard
526 register name".
527
528 $variable A convenience variable with a name chosen
529 by the user.
530
531 $digits Value history with index <digits>, starting
532 from the first value which has index 1.
533
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.
538
539 $ | $0 | $$0 The last value in the value history.
540
541 $$ An abbreviation for the second to the last
542 value in the value history, I.e. $$1 */
543
544 void
545 write_dollar_variable (struct parser_state *ps, struct stoken str)
546 {
547 struct block_symbol sym;
548 struct bound_minimal_symbol msym;
549 struct internalvar *isym = NULL;
550 std::string copy;
551
552 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
553 and $$digits (equivalent to $<-digits> if you could type that). */
554
555 int negate = 0;
556 int i = 1;
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] == '$')
560 {
561 negate = 1;
562 i = 2;
563 }
564 if (i == str.length)
565 {
566 /* Just dollars (one or two). */
567 i = -negate;
568 goto handle_last;
569 }
570 /* Is the rest of the token digits? */
571 for (; i < str.length; i++)
572 if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
573 break;
574 if (i == str.length)
575 {
576 i = atoi (str.ptr + 1 + negate);
577 if (negate)
578 i = -i;
579 goto handle_last;
580 }
581
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);
586 if (i >= 0)
587 goto handle_register;
588
589 /* Any names starting with $ are probably debugger internal variables. */
590
591 copy = copy_name (str);
592 isym = lookup_only_internalvar (copy.c_str () + 1);
593 if (isym)
594 {
595 write_exp_elt_opcode (ps, OP_INTERNALVAR);
596 write_exp_elt_intern (ps, isym);
597 write_exp_elt_opcode (ps, OP_INTERNALVAR);
598 return;
599 }
600
601 /* On some systems, such as HP-UX and hppa-linux, certain system routines
602 have names beginning with $ or $$. Check for those, first. */
603
604 sym = lookup_symbol (copy.c_str (), NULL, VAR_DOMAIN, NULL);
605 if (sym.symbol)
606 {
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);
611 return;
612 }
613 msym = lookup_bound_minimal_symbol (copy.c_str ());
614 if (msym.minsym)
615 {
616 write_exp_msymbol (ps, msym);
617 return;
618 }
619
620 /* Any other names are assumed to be debugger internal variables. */
621
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);
625 return;
626 handle_last:
627 write_exp_elt_opcode (ps, OP_LAST);
628 write_exp_elt_longcst (ps, (LONGEST) i);
629 write_exp_elt_opcode (ps, OP_LAST);
630 return;
631 handle_register:
632 write_exp_elt_opcode (ps, OP_REGISTER);
633 str.length--;
634 str.ptr++;
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);
639 return;
640 }
641
642
643 const char *
644 find_template_name_end (const char *p)
645 {
646 int depth = 1;
647 int just_seen_right = 0;
648 int just_seen_colon = 0;
649 int just_seen_space = 0;
650
651 if (!p || (*p != '<'))
652 return 0;
653
654 while (*++p)
655 {
656 switch (*p)
657 {
658 case '\'':
659 case '\"':
660 case '{':
661 case '}':
662 /* In future, may want to allow these?? */
663 return 0;
664 case '<':
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 */
668 break;
669 case '>':
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 */
675 break;
676 case ':':
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 :::: */
680 break;
681 case ' ':
682 break;
683 default:
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 */
691 return 0;
692 }
693 if (*p != ' ')
694 just_seen_space = 0;
695 if (*p != ':')
696 just_seen_colon = 0;
697 if (*p != '>')
698 just_seen_right = 0;
699 }
700 return 0;
701 }
702 \f
703
704 /* Return a null-terminated temporary copy of the name of a string token.
705
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. */
711
712 std::string
713 copy_name (struct stoken token)
714 {
715 return std::string (token.ptr, token.length);
716 }
717 \f
718
719 /* See comments on parser-defs.h. */
720
721 int
722 prefixify_expression (struct expression *expr, int last_struct)
723 {
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;
728
729 temp = (struct expression *) alloca (len);
730
731 /* Copy the original expression into temp. */
732 memcpy (temp, expr, len);
733
734 return prefixify_subexp (temp, expr, inpos, outpos, last_struct);
735 }
736
737 /* Return the number of exp_elements in the postfix subexpression
738 of EXPR whose operator is at index ENDPOS - 1 in EXPR. */
739
740 static int
741 length_of_subexp (struct expression *expr, int endpos)
742 {
743 int oplen, args;
744
745 operator_length (expr, endpos, &oplen, &args);
746
747 while (args > 0)
748 {
749 oplen += length_of_subexp (expr, endpos - oplen);
750 args--;
751 }
752
753 return oplen;
754 }
755
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
758 operator takes. */
759
760 void
761 operator_length (const struct expression *expr, int endpos, int *oplenp,
762 int *argsp)
763 {
764 expr->language_defn->expression_ops ()->operator_length (expr, endpos,
765 oplenp, argsp);
766 }
767
768 /* Default value for operator_length in exp_descriptor vectors. */
769
770 void
771 operator_length_standard (const struct expression *expr, int endpos,
772 int *oplenp, int *argsp)
773 {
774 int oplen = 1;
775 int args = 0;
776 enum range_flag range_flag;
777 int i;
778
779 if (endpos < 1)
780 error (_("?error in operator_length_standard"));
781
782 i = (int) expr->elts[endpos - 1].opcode;
783
784 switch (i)
785 {
786 /* C++ */
787 case OP_SCOPE:
788 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
789 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
790 break;
791
792 case OP_LONG:
793 case OP_FLOAT:
794 case OP_VAR_VALUE:
795 case OP_VAR_MSYM_VALUE:
796 oplen = 4;
797 break;
798
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);
802 args = 1;
803 break;
804
805 case OP_TYPE:
806 case OP_BOOL:
807 case OP_LAST:
808 case OP_INTERNALVAR:
809 case OP_VAR_ENTRY_VALUE:
810 oplen = 3;
811 break;
812
813 case OP_COMPLEX:
814 oplen = 3;
815 args = 2;
816 break;
817
818 case OP_FUNCALL:
819 oplen = 3;
820 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
821 break;
822
823 case TYPE_INSTANCE:
824 oplen = 5 + longest_to_int (expr->elts[endpos - 2].longconst);
825 args = 1;
826 break;
827
828 case OP_OBJC_MSGCALL: /* Objective C message (method) call. */
829 oplen = 4;
830 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
831 break;
832
833 case UNOP_MAX:
834 case UNOP_MIN:
835 oplen = 3;
836 break;
837
838 case UNOP_CAST_TYPE:
839 case UNOP_DYNAMIC_CAST:
840 case UNOP_REINTERPRET_CAST:
841 case UNOP_MEMVAL_TYPE:
842 oplen = 1;
843 args = 2;
844 break;
845
846 case BINOP_VAL:
847 case UNOP_CAST:
848 case UNOP_MEMVAL:
849 oplen = 3;
850 args = 1;
851 break;
852
853 case UNOP_ABS:
854 case UNOP_CAP:
855 case UNOP_CHR:
856 case UNOP_FLOAT:
857 case UNOP_HIGH:
858 case UNOP_ODD:
859 case UNOP_ORD:
860 case UNOP_TRUNC:
861 case OP_TYPEOF:
862 case OP_DECLTYPE:
863 case OP_TYPEID:
864 oplen = 1;
865 args = 1;
866 break;
867
868 case OP_ADL_FUNC:
869 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
870 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
871 oplen++;
872 oplen++;
873 break;
874
875 case STRUCTOP_STRUCT:
876 case STRUCTOP_PTR:
877 args = 1;
878 /* fall through */
879 case OP_REGISTER:
880 case OP_M2_STRING:
881 case OP_STRING:
882 case OP_OBJC_NSSTRING: /* Objective C Foundation Class
883 NSString constant. */
884 case OP_OBJC_SELECTOR: /* Objective C "@selector" pseudo-op. */
885 case OP_NAME:
886 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
887 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
888 break;
889
890 case OP_ARRAY:
891 oplen = 4;
892 args = longest_to_int (expr->elts[endpos - 2].longconst);
893 args -= longest_to_int (expr->elts[endpos - 3].longconst);
894 args += 1;
895 break;
896
897 case TERNOP_COND:
898 case TERNOP_SLICE:
899 args = 3;
900 break;
901
902 /* Modula-2 */
903 case MULTI_SUBSCRIPT:
904 oplen = 3;
905 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
906 break;
907
908 case BINOP_ASSIGN_MODIFY:
909 oplen = 3;
910 args = 2;
911 break;
912
913 /* C++ */
914 case OP_THIS:
915 oplen = 2;
916 break;
917
918 case OP_RANGE:
919 oplen = 3;
920 range_flag = (enum range_flag)
921 longest_to_int (expr->elts[endpos - 2].longconst);
922
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. */
925 args = 2;
926 if (range_flag & RANGE_HAS_STRIDE)
927 ++args;
928 if (range_flag & RANGE_LOW_BOUND_DEFAULT)
929 --args;
930 if (range_flag & RANGE_HIGH_BOUND_DEFAULT)
931 --args;
932
933 break;
934
935 default:
936 args = 1 + (i < (int) BINOP_END);
937 }
938
939 *oplenp = oplen;
940 *argsp = args;
941 }
942
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. */
949
950 static int
951 prefixify_subexp (struct expression *inexpr,
952 struct expression *outexpr, int inend, int outbeg,
953 int last_struct)
954 {
955 int oplen;
956 int args;
957 int i;
958 int *arglens;
959 int result = -1;
960
961 operator_length (inexpr, inend, &oplen, &args);
962
963 /* Copy the final operator itself, from the end of the input
964 to the beginning of the output. */
965 inend -= oplen;
966 memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
967 EXP_ELEM_TO_BYTES (oplen));
968 outbeg += oplen;
969
970 if (last_struct == inend)
971 result = outbeg - oplen;
972
973 /* Find the lengths of the arg subexpressions. */
974 arglens = (int *) alloca (args * sizeof (int));
975 for (i = args - 1; i >= 0; i--)
976 {
977 oplen = length_of_subexp (inexpr, inend);
978 arglens[i] = oplen;
979 inend -= oplen;
980 }
981
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++)
989 {
990 int r;
991
992 oplen = arglens[i];
993 inend += oplen;
994 r = prefixify_subexp (inexpr, outexpr, inend, outbeg, last_struct);
995 if (r != -1)
996 {
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. */
1000 return r;
1001 }
1002 outbeg += oplen;
1003 }
1004
1005 return result;
1006 }
1007 \f
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).
1015
1016 If COMMA is nonzero, stop if a comma is reached. */
1017
1018 expression_up
1019 parse_exp_1 (const char **stringptr, CORE_ADDR pc, const struct block *block,
1020 int comma, innermost_block_tracker *tracker)
1021 {
1022 return parse_exp_in_context (stringptr, pc, block, comma, 0, NULL,
1023 tracker, nullptr);
1024 }
1025
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. */
1032
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)
1039 {
1040 const struct language_defn *lang = NULL;
1041 int subexp;
1042
1043 if (*stringptr == 0 || **stringptr == 0)
1044 error_no_arg (_("expression to compute"));
1045
1046 const struct block *expression_context_block = block;
1047 CORE_ADDR expression_context_pc = 0;
1048
1049 innermost_block_tracker local_tracker;
1050 if (tracker == nullptr)
1051 tracker = &local_tracker;
1052
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);
1056 else if (pc == 0)
1057 expression_context_pc = BLOCK_ENTRY_PC (expression_context_block);
1058 else
1059 expression_context_pc = pc;
1060
1061 /* Fall back to using the current source static context, if any. */
1062
1063 if (!expression_context_block)
1064 {
1065 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
1066 if (cursal.symtab)
1067 expression_context_block
1068 = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (cursal.symtab),
1069 STATIC_BLOCK);
1070 if (expression_context_block)
1071 expression_context_pc = BLOCK_ENTRY_PC (expression_context_block);
1072 }
1073
1074 if (language_mode == language_mode_auto && block != NULL)
1075 {
1076 /* Find the language associated to the given context block.
1077 Default to the current language if it can not be determined.
1078
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);
1091
1092 if (func != NULL)
1093 lang = language_def (func->language ());
1094 if (lang == NULL || lang->la_language == language_unknown)
1095 lang = current_language;
1096 }
1097 else
1098 lang = current_language;
1099
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. */
1104
1105 parser_state ps (lang, get_current_arch (), expression_context_block,
1106 expression_context_pc, comma, *stringptr,
1107 cstate != nullptr, tracker);
1108
1109 scoped_restore_current_language lang_saver;
1110 set_language (lang->la_language);
1111
1112 try
1113 {
1114 lang->parser (&ps);
1115 }
1116 catch (const gdb_exception &except)
1117 {
1118 /* If parsing for completion, allow this to succeed; but if no
1119 expression elements have been written, then there's nothing
1120 to do, so fail. */
1121 if (! ps.parse_completion || ps.expout_ptr == 0)
1122 throw;
1123 }
1124
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 ();
1128
1129 /* Convert expression from postfix form as generated by yacc
1130 parser, to a prefix form. */
1131
1132 if (expressiondebug)
1133 dump_raw_expression (result.get (), gdb_stdlog,
1134 "before conversion to prefix form");
1135
1136 subexp = prefixify_expression (result.get (),
1137 ps.m_completion_state.expout_last_struct);
1138 if (out_subexp)
1139 *out_subexp = subexp;
1140
1141 lang->post_parser (&result, void_context_p, ps.parse_completion, tracker);
1142
1143 if (expressiondebug)
1144 dump_prefix_expression (result.get (), gdb_stdlog);
1145
1146 if (cstate != nullptr)
1147 *cstate = std::move (ps.m_completion_state);
1148 *stringptr = ps.lexptr;
1149 return result;
1150 }
1151
1152 /* Parse STRING as an expression, and complain if this fails
1153 to use up all of the contents of STRING. */
1154
1155 expression_up
1156 parse_expression (const char *string, innermost_block_tracker *tracker)
1157 {
1158 expression_up exp = parse_exp_1 (&string, 0, 0, 0, tracker);
1159 if (*string)
1160 error (_("Junk after end of expression."));
1161 return exp;
1162 }
1163
1164 /* Same as parse_expression, but using the given language (LANG)
1165 to parse the expression. */
1166
1167 expression_up
1168 parse_expression_with_language (const char *string, enum language lang)
1169 {
1170 gdb::optional<scoped_restore_current_language> lang_saver;
1171 if (current_language->la_language != lang)
1172 {
1173 lang_saver.emplace ();
1174 set_language (lang);
1175 }
1176
1177 return parse_expression (string);
1178 }
1179
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. */
1186
1187 struct type *
1188 parse_expression_for_completion (const char *string,
1189 gdb::unique_xmalloc_ptr<char> *name,
1190 enum type_code *code)
1191 {
1192 expression_up exp;
1193 struct value *val;
1194 int subexp;
1195 expr_completion_state cstate;
1196
1197 try
1198 {
1199 exp = parse_exp_in_context (&string, 0, 0, 0, 0, &subexp,
1200 nullptr, &cstate);
1201 }
1202 catch (const gdb_exception_error &except)
1203 {
1204 /* Nothing, EXP remains NULL. */
1205 }
1206
1207 if (exp == NULL)
1208 return NULL;
1209
1210 if (cstate.expout_tag_completion_type != TYPE_CODE_UNDEF)
1211 {
1212 *code = cstate.expout_tag_completion_type;
1213 *name = std::move (cstate.expout_completion_name);
1214 return NULL;
1215 }
1216
1217 if (cstate.expout_last_struct == -1)
1218 return NULL;
1219
1220 const char *fieldname = extract_field_op (exp.get (), &subexp);
1221 if (fieldname == NULL)
1222 {
1223 name->reset ();
1224 return NULL;
1225 }
1226
1227 name->reset (xstrdup (fieldname));
1228 /* This might throw an exception. If so, we want to let it
1229 propagate. */
1230 val = evaluate_subexpression_type (exp.get (), subexp);
1231
1232 return value_type (val);
1233 }
1234
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.
1239
1240 NOTE: This accepts the floating point syntax that sscanf accepts. */
1241
1242 bool
1243 parse_float (const char *p, int len,
1244 const struct type *type, gdb_byte *data)
1245 {
1246 return target_float_from_string (data, type, std::string (p, len));
1247 }
1248 \f
1249 /* This function avoids direct calls to fprintf
1250 in the parser generated debug code. */
1251 void
1252 parser_fprintf (FILE *x, const char *y, ...)
1253 {
1254 va_list args;
1255
1256 va_start (args, y);
1257 if (x == stderr)
1258 vfprintf_unfiltered (gdb_stderr, y, args);
1259 else
1260 {
1261 fprintf_unfiltered (gdb_stderr, " Unknown FILE used.\n");
1262 vfprintf_unfiltered (gdb_stderr, y, args);
1263 }
1264 va_end (args);
1265 }
1266
1267 /* Implementation of the exp_descriptor method operator_check. */
1268
1269 int
1270 operator_check_standard (struct expression *exp, int pos,
1271 int (*objfile_func) (struct objfile *objfile,
1272 void *data),
1273 void *data)
1274 {
1275 const union exp_element *const elts = exp->elts;
1276 struct type *type = NULL;
1277 struct objfile *objfile = NULL;
1278
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);
1282
1283 /* Track the callers of write_exp_elt_type for this table. */
1284
1285 switch (elts[pos].opcode)
1286 {
1287 case BINOP_VAL:
1288 case OP_COMPLEX:
1289 case OP_FLOAT:
1290 case OP_LONG:
1291 case OP_SCOPE:
1292 case OP_TYPE:
1293 case UNOP_CAST:
1294 case UNOP_MAX:
1295 case UNOP_MEMVAL:
1296 case UNOP_MIN:
1297 type = elts[pos + 1].type;
1298 break;
1299
1300 case TYPE_INSTANCE:
1301 {
1302 LONGEST arg, nargs = elts[pos + 2].longconst;
1303
1304 for (arg = 0; arg < nargs; arg++)
1305 {
1306 struct type *inst_type = elts[pos + 3 + arg].type;
1307 struct objfile *inst_objfile = TYPE_OBJFILE (inst_type);
1308
1309 if (inst_objfile && (*objfile_func) (inst_objfile, data))
1310 return 1;
1311 }
1312 }
1313 break;
1314
1315 case OP_VAR_VALUE:
1316 {
1317 const struct block *const block = elts[pos + 1].block;
1318 const struct symbol *const symbol = elts[pos + 2].symbol;
1319
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))
1323 return 1;
1324
1325 /* Check objfile where is placed the code touching the variable. */
1326 objfile = block_objfile (block);
1327
1328 type = SYMBOL_TYPE (symbol);
1329 }
1330 break;
1331 case OP_VAR_MSYM_VALUE:
1332 objfile = elts[pos + 1].objfile;
1333 break;
1334 }
1335
1336 /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL. */
1337
1338 if (type && TYPE_OBJFILE (type)
1339 && (*objfile_func) (TYPE_OBJFILE (type), data))
1340 return 1;
1341 if (objfile && (*objfile_func) (objfile, data))
1342 return 1;
1343
1344 return 0;
1345 }
1346
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. */
1353
1354 static int
1355 exp_iterate (struct expression *exp,
1356 int (*objfile_func) (struct objfile *objfile, void *data),
1357 void *data)
1358 {
1359 int endpos;
1360
1361 for (endpos = exp->nelts; endpos > 0; )
1362 {
1363 int pos, args, oplen = 0;
1364
1365 operator_length (exp, endpos, &oplen, &args);
1366 gdb_assert (oplen > 0);
1367
1368 pos = endpos - oplen;
1369 if (exp->language_defn->expression_ops ()->operator_check (exp, pos,
1370 objfile_func,
1371 data))
1372 return 1;
1373
1374 endpos = pos;
1375 }
1376
1377 return 0;
1378 }
1379
1380 /* Helper for exp_uses_objfile. */
1381
1382 static int
1383 exp_uses_objfile_iter (struct objfile *exp_objfile, void *objfile_voidp)
1384 {
1385 struct objfile *objfile = (struct objfile *) objfile_voidp;
1386
1387 if (exp_objfile->separate_debug_objfile_backlink)
1388 exp_objfile = exp_objfile->separate_debug_objfile_backlink;
1389
1390 return exp_objfile == objfile;
1391 }
1392
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
1395 file. */
1396
1397 int
1398 exp_uses_objfile (struct expression *exp, struct objfile *objfile)
1399 {
1400 gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
1401
1402 return exp_iterate (exp, exp_uses_objfile_iter, objfile);
1403 }
1404
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. */
1408
1409 static void
1410 increase_expout_size (struct expr_builder *ps, size_t lenelt)
1411 {
1412 if ((ps->expout_ptr + lenelt) >= ps->expout_size)
1413 {
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))));
1420 }
1421 }
1422
1423 void _initialize_parse ();
1424 void
1425 _initialize_parse ()
1426 {
1427 add_setshow_zuinteger_cmd ("expression", class_maintenance,
1428 &expressiondebug,
1429 _("Set expression debugging."),
1430 _("Show expression debugging."),
1431 _("When non-zero, the internal representation "
1432 "of expressions will be printed."),
1433 NULL,
1434 show_expressiondebug,
1435 &setdebuglist, &showdebuglist);
1436 add_setshow_boolean_cmd ("parser", class_maintenance,
1437 &parser_debug,
1438 _("Set parser debugging."),
1439 _("Show parser debugging."),
1440 _("When non-zero, expression parser "
1441 "tracing will be enabled."),
1442 NULL,
1443 show_parserdebug,
1444 &setdebuglist, &showdebuglist);
1445 }