]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame_incremental - gdb/expprint.c
gdb/copyright.py: Also update sources in "gdbserver" and "gdbsupport"
[thirdparty/binutils-gdb.git] / gdb / expprint.c
... / ...
CommitLineData
1/* Print in infix form a struct expression.
2
3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "symtab.h"
22#include "gdbtypes.h"
23#include "expression.h"
24#include "value.h"
25#include "language.h"
26#include "parser-defs.h"
27#include "user-regs.h" /* For user_reg_map_regnum_to_name. */
28#include "target.h"
29#include "block.h"
30#include "objfiles.h"
31#include "valprint.h"
32#include "cli/cli-style.h"
33
34#include <ctype.h>
35
36void
37print_expression (struct expression *exp, struct ui_file *stream)
38{
39 int pc = 0;
40
41 print_subexp (exp, &pc, stream, PREC_NULL);
42}
43
44/* Print the subexpression of EXP that starts in position POS, on STREAM.
45 PREC is the precedence of the surrounding operator;
46 if the precedence of the main operator of this subexpression is less,
47 parentheses are needed here. */
48
49void
50print_subexp (struct expression *exp, int *pos,
51 struct ui_file *stream, enum precedence prec)
52{
53 exp->language_defn->expression_ops ()->print_subexp (exp, pos, stream,
54 prec);
55}
56
57/* See parser-defs.h. */
58
59void
60print_subexp_funcall (struct expression *exp, int *pos,
61 struct ui_file *stream)
62{
63 unsigned nargs = longest_to_int (exp->elts[*pos].longconst);
64 (*pos) += 2;
65 print_subexp (exp, pos, stream, PREC_SUFFIX);
66 fputs_filtered (" (", stream);
67 for (unsigned tem = 0; tem < nargs; tem++)
68 {
69 if (tem != 0)
70 fputs_filtered (", ", stream);
71 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
72 }
73 fputs_filtered (")", stream);
74}
75
76/* Standard implementation of print_subexp for use in language_defn
77 vectors. */
78void
79print_subexp_standard (struct expression *exp, int *pos,
80 struct ui_file *stream, enum precedence prec)
81{
82 unsigned tem;
83 const struct op_print *op_print_tab;
84 int pc;
85 unsigned nargs;
86 const char *op_str;
87 int assign_modify = 0;
88 enum exp_opcode opcode;
89 enum precedence myprec = PREC_NULL;
90 /* Set to 1 for a right-associative operator. */
91 int assoc = 0;
92 struct value *val;
93 char *tempstr = NULL;
94
95 op_print_tab = exp->language_defn->opcode_print_table ();
96 pc = (*pos)++;
97 opcode = exp->elts[pc].opcode;
98 switch (opcode)
99 {
100 /* Common ops */
101
102 case OP_TYPE:
103 (*pos) += 2;
104 type_print (exp->elts[pc + 1].type, "", stream, 0);
105 return;
106
107 case OP_SCOPE:
108 myprec = PREC_PREFIX;
109 assoc = 0;
110 fputs_filtered (exp->elts[pc + 1].type->name (), stream);
111 fputs_filtered ("::", stream);
112 nargs = longest_to_int (exp->elts[pc + 2].longconst);
113 (*pos) += 4 + BYTES_TO_EXP_ELEM (nargs + 1);
114 fputs_filtered (&exp->elts[pc + 3].string, stream);
115 return;
116
117 case OP_LONG:
118 {
119 struct value_print_options opts;
120
121 get_no_prettyformat_print_options (&opts);
122 (*pos) += 3;
123 value_print (value_from_longest (exp->elts[pc + 1].type,
124 exp->elts[pc + 2].longconst),
125 stream, &opts);
126 }
127 return;
128
129 case OP_FLOAT:
130 {
131 struct value_print_options opts;
132
133 get_no_prettyformat_print_options (&opts);
134 (*pos) += 3;
135 value_print (value_from_contents (exp->elts[pc + 1].type,
136 exp->elts[pc + 2].floatconst),
137 stream, &opts);
138 }
139 return;
140
141 case OP_VAR_VALUE:
142 {
143 const struct block *b;
144
145 (*pos) += 3;
146 b = exp->elts[pc + 1].block;
147 if (b != NULL
148 && BLOCK_FUNCTION (b) != NULL
149 && BLOCK_FUNCTION (b)->print_name () != NULL)
150 {
151 fputs_filtered (BLOCK_FUNCTION (b)->print_name (), stream);
152 fputs_filtered ("::", stream);
153 }
154 fputs_filtered (exp->elts[pc + 2].symbol->print_name (), stream);
155 }
156 return;
157
158 case OP_VAR_MSYM_VALUE:
159 {
160 (*pos) += 3;
161 fputs_filtered (exp->elts[pc + 2].msymbol->print_name (), stream);
162 }
163 return;
164
165 case OP_FUNC_STATIC_VAR:
166 {
167 tem = longest_to_int (exp->elts[pc + 1].longconst);
168 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
169 fputs_filtered (&exp->elts[pc + 1].string, stream);
170 }
171 return;
172
173 case OP_VAR_ENTRY_VALUE:
174 {
175 (*pos) += 2;
176 fprintf_filtered (stream, "%s@entry",
177 exp->elts[pc + 1].symbol->print_name ());
178 }
179 return;
180
181 case OP_LAST:
182 (*pos) += 2;
183 fprintf_filtered (stream, "$%d",
184 longest_to_int (exp->elts[pc + 1].longconst));
185 return;
186
187 case OP_REGISTER:
188 {
189 const char *name = &exp->elts[pc + 2].string;
190
191 (*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
192 fprintf_filtered (stream, "$%s", name);
193 return;
194 }
195
196 case OP_BOOL:
197 (*pos) += 2;
198 fprintf_filtered (stream, "%s",
199 longest_to_int (exp->elts[pc + 1].longconst)
200 ? "TRUE" : "FALSE");
201 return;
202
203 case OP_INTERNALVAR:
204 (*pos) += 2;
205 fprintf_filtered (stream, "$%s",
206 internalvar_name (exp->elts[pc + 1].internalvar));
207 return;
208
209 case OP_FUNCALL:
210 print_subexp_funcall (exp, pos, stream);
211 return;
212
213 case OP_NAME:
214 nargs = longest_to_int (exp->elts[pc + 1].longconst);
215 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
216 fputs_filtered (&exp->elts[pc + 2].string, stream);
217 return;
218
219 case OP_STRING:
220 {
221 struct value_print_options opts;
222
223 nargs = longest_to_int (exp->elts[pc + 1].longconst);
224 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
225 /* LA_PRINT_STRING will print using the current repeat count threshold.
226 If necessary, we can temporarily set it to zero, or pass it as an
227 additional parameter to LA_PRINT_STRING. -fnf */
228 get_user_print_options (&opts);
229 exp->language_defn
230 ->printstr (stream, builtin_type (exp->gdbarch)->builtin_char,
231 (gdb_byte *) &exp->elts[pc + 2].string, nargs,
232 NULL, 0, &opts);
233 }
234 return;
235
236 case OP_OBJC_NSSTRING: /* Objective-C Foundation Class
237 NSString constant. */
238 {
239 struct value_print_options opts;
240
241 nargs = longest_to_int (exp->elts[pc + 1].longconst);
242 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
243 fputs_filtered ("@\"", stream);
244 get_user_print_options (&opts);
245 exp->language_defn
246 ->printstr (stream, builtin_type (exp->gdbarch)->builtin_char,
247 (gdb_byte *) &exp->elts[pc + 2].string, nargs,
248 NULL, 0, &opts);
249 fputs_filtered ("\"", stream);
250 }
251 return;
252
253 case OP_OBJC_MSGCALL:
254 { /* Objective C message (method) call. */
255 (*pos) += 3;
256 nargs = longest_to_int (exp->elts[pc + 2].longconst);
257 fprintf_unfiltered (stream, "[");
258 print_subexp (exp, pos, stream, PREC_SUFFIX);
259 gdb::unique_xmalloc_ptr<char> selector
260 = target_read_string (exp->elts[pc + 1].longconst, 1024);
261 if (selector == nullptr)
262 error (_("bad selector"));
263 if (nargs)
264 {
265 char *s, *nextS;
266
267 s = selector.get ();
268 for (tem = 0; tem < nargs; tem++)
269 {
270 nextS = strchr (s, ':');
271 gdb_assert (nextS); /* Make sure we found ':'. */
272 *nextS = '\0';
273 fprintf_unfiltered (stream, " %s: ", s);
274 s = nextS + 1;
275 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
276 }
277 }
278 else
279 {
280 fprintf_unfiltered (stream, " %s", selector.get ());
281 }
282 fprintf_unfiltered (stream, "]");
283 return;
284 }
285
286 case OP_ARRAY:
287 (*pos) += 3;
288 nargs = longest_to_int (exp->elts[pc + 2].longconst);
289 nargs -= longest_to_int (exp->elts[pc + 1].longconst);
290 nargs++;
291 tem = 0;
292 if (exp->elts[pc + 4].opcode == OP_LONG
293 && exp->elts[pc + 5].type
294 == builtin_type (exp->gdbarch)->builtin_char
295 && exp->language_defn->la_language == language_c)
296 {
297 /* Attempt to print C character arrays using string syntax.
298 Walk through the args, picking up one character from each
299 of the OP_LONG expression elements. If any array element
300 does not match our expection of what we should find for
301 a simple string, revert back to array printing. Note that
302 the last expression element is an explicit null terminator
303 byte, which doesn't get printed. */
304 tempstr = (char *) alloca (nargs);
305 pc += 4;
306 while (tem < nargs)
307 {
308 if (exp->elts[pc].opcode != OP_LONG
309 || exp->elts[pc + 1].type
310 != builtin_type (exp->gdbarch)->builtin_char)
311 {
312 /* Not a simple array of char, use regular array
313 printing. */
314 tem = 0;
315 break;
316 }
317 else
318 {
319 tempstr[tem++] =
320 longest_to_int (exp->elts[pc + 2].longconst);
321 pc += 4;
322 }
323 }
324 }
325 if (tem > 0)
326 {
327 struct value_print_options opts;
328
329 get_user_print_options (&opts);
330 exp->language_defn
331 ->printstr (stream, builtin_type (exp->gdbarch)->builtin_char,
332 (gdb_byte *) tempstr, nargs - 1, NULL, 0, &opts);
333 (*pos) = pc;
334 }
335 else
336 {
337 fputs_filtered (" {", stream);
338 for (tem = 0; tem < nargs; tem++)
339 {
340 if (tem != 0)
341 {
342 fputs_filtered (", ", stream);
343 }
344 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
345 }
346 fputs_filtered ("}", stream);
347 }
348 return;
349
350 case TERNOP_COND:
351 if ((int) prec > (int) PREC_COMMA)
352 fputs_filtered ("(", stream);
353 /* Print the subexpressions, forcing parentheses
354 around any binary operations within them.
355 This is more parentheses than are strictly necessary,
356 but it looks clearer. */
357 print_subexp (exp, pos, stream, PREC_HYPER);
358 fputs_filtered (" ? ", stream);
359 print_subexp (exp, pos, stream, PREC_HYPER);
360 fputs_filtered (" : ", stream);
361 print_subexp (exp, pos, stream, PREC_HYPER);
362 if ((int) prec > (int) PREC_COMMA)
363 fputs_filtered (")", stream);
364 return;
365
366 case TERNOP_SLICE:
367 print_subexp (exp, pos, stream, PREC_SUFFIX);
368 fputs_filtered ("(", stream);
369 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
370 fputs_filtered (opcode == TERNOP_SLICE ? " : " : " UP ", stream);
371 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
372 fputs_filtered (")", stream);
373 return;
374
375 case STRUCTOP_STRUCT:
376 tem = longest_to_int (exp->elts[pc + 1].longconst);
377 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
378 print_subexp (exp, pos, stream, PREC_SUFFIX);
379 fputs_filtered (".", stream);
380 fputs_filtered (&exp->elts[pc + 2].string, stream);
381 return;
382
383 /* Will not occur for Modula-2. */
384 case STRUCTOP_PTR:
385 tem = longest_to_int (exp->elts[pc + 1].longconst);
386 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
387 print_subexp (exp, pos, stream, PREC_SUFFIX);
388 fputs_filtered ("->", stream);
389 fputs_filtered (&exp->elts[pc + 2].string, stream);
390 return;
391
392 case STRUCTOP_MEMBER:
393 print_subexp (exp, pos, stream, PREC_SUFFIX);
394 fputs_filtered (".*", stream);
395 print_subexp (exp, pos, stream, PREC_SUFFIX);
396 return;
397
398 case STRUCTOP_MPTR:
399 print_subexp (exp, pos, stream, PREC_SUFFIX);
400 fputs_filtered ("->*", stream);
401 print_subexp (exp, pos, stream, PREC_SUFFIX);
402 return;
403
404 case BINOP_SUBSCRIPT:
405 print_subexp (exp, pos, stream, PREC_SUFFIX);
406 fputs_filtered ("[", stream);
407 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
408 fputs_filtered ("]", stream);
409 return;
410
411 case UNOP_POSTINCREMENT:
412 print_subexp (exp, pos, stream, PREC_SUFFIX);
413 fputs_filtered ("++", stream);
414 return;
415
416 case UNOP_POSTDECREMENT:
417 print_subexp (exp, pos, stream, PREC_SUFFIX);
418 fputs_filtered ("--", stream);
419 return;
420
421 case UNOP_CAST:
422 (*pos) += 2;
423 if ((int) prec > (int) PREC_PREFIX)
424 fputs_filtered ("(", stream);
425 fputs_filtered ("(", stream);
426 type_print (exp->elts[pc + 1].type, "", stream, 0);
427 fputs_filtered (") ", stream);
428 print_subexp (exp, pos, stream, PREC_PREFIX);
429 if ((int) prec > (int) PREC_PREFIX)
430 fputs_filtered (")", stream);
431 return;
432
433 case UNOP_CAST_TYPE:
434 if ((int) prec > (int) PREC_PREFIX)
435 fputs_filtered ("(", stream);
436 fputs_filtered ("(", stream);
437 print_subexp (exp, pos, stream, PREC_PREFIX);
438 fputs_filtered (") ", stream);
439 print_subexp (exp, pos, stream, PREC_PREFIX);
440 if ((int) prec > (int) PREC_PREFIX)
441 fputs_filtered (")", stream);
442 return;
443
444 case UNOP_DYNAMIC_CAST:
445 case UNOP_REINTERPRET_CAST:
446 fputs_filtered (opcode == UNOP_DYNAMIC_CAST ? "dynamic_cast"
447 : "reinterpret_cast", stream);
448 fputs_filtered ("<", stream);
449 print_subexp (exp, pos, stream, PREC_PREFIX);
450 fputs_filtered ("> (", stream);
451 print_subexp (exp, pos, stream, PREC_PREFIX);
452 fputs_filtered (")", stream);
453 return;
454
455 case UNOP_MEMVAL:
456 (*pos) += 2;
457 if ((int) prec > (int) PREC_PREFIX)
458 fputs_filtered ("(", stream);
459 if (exp->elts[pc + 1].type->code () == TYPE_CODE_FUNC
460 && exp->elts[pc + 3].opcode == OP_LONG)
461 {
462 struct value_print_options opts;
463
464 /* We have a minimal symbol fn, probably. It's encoded
465 as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
466 Swallow the OP_LONG (including both its opcodes); ignore
467 its type; print the value in the type of the MEMVAL. */
468 (*pos) += 4;
469 val = value_at_lazy (exp->elts[pc + 1].type,
470 (CORE_ADDR) exp->elts[pc + 5].longconst);
471 get_no_prettyformat_print_options (&opts);
472 value_print (val, stream, &opts);
473 }
474 else
475 {
476 fputs_filtered ("{", stream);
477 type_print (exp->elts[pc + 1].type, "", stream, 0);
478 fputs_filtered ("} ", stream);
479 print_subexp (exp, pos, stream, PREC_PREFIX);
480 }
481 if ((int) prec > (int) PREC_PREFIX)
482 fputs_filtered (")", stream);
483 return;
484
485 case UNOP_MEMVAL_TYPE:
486 if ((int) prec > (int) PREC_PREFIX)
487 fputs_filtered ("(", stream);
488 fputs_filtered ("{", stream);
489 print_subexp (exp, pos, stream, PREC_PREFIX);
490 fputs_filtered ("} ", stream);
491 print_subexp (exp, pos, stream, PREC_PREFIX);
492 if ((int) prec > (int) PREC_PREFIX)
493 fputs_filtered (")", stream);
494 return;
495
496 case BINOP_ASSIGN_MODIFY:
497 opcode = exp->elts[pc + 1].opcode;
498 (*pos) += 2;
499 myprec = PREC_ASSIGN;
500 assoc = 1;
501 assign_modify = 1;
502 op_str = "???";
503 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
504 if (op_print_tab[tem].opcode == opcode)
505 {
506 op_str = op_print_tab[tem].string;
507 break;
508 }
509 if (op_print_tab[tem].opcode != opcode)
510 /* Not found; don't try to keep going because we don't know how
511 to interpret further elements. */
512 error (_("Invalid expression"));
513 break;
514
515 /* C++ ops */
516
517 case OP_THIS:
518 ++(*pos);
519 if (exp->language_defn->name_of_this () != NULL)
520 fputs_filtered (exp->language_defn->name_of_this (), stream);
521 else
522 fprintf_styled (stream, metadata_style.style (),
523 _("<language %s has no 'this'>"),
524 exp->language_defn->name ());
525 return;
526
527 /* Modula-2 ops */
528
529 case MULTI_SUBSCRIPT:
530 (*pos) += 2;
531 nargs = longest_to_int (exp->elts[pc + 1].longconst);
532 print_subexp (exp, pos, stream, PREC_SUFFIX);
533 fprintf_unfiltered (stream, " [");
534 for (tem = 0; tem < nargs; tem++)
535 {
536 if (tem != 0)
537 fprintf_unfiltered (stream, ", ");
538 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
539 }
540 fprintf_unfiltered (stream, "]");
541 return;
542
543 case BINOP_VAL:
544 (*pos) += 2;
545 fprintf_unfiltered (stream, "VAL(");
546 type_print (exp->elts[pc + 1].type, "", stream, 0);
547 fprintf_unfiltered (stream, ",");
548 print_subexp (exp, pos, stream, PREC_PREFIX);
549 fprintf_unfiltered (stream, ")");
550 return;
551
552 case TYPE_INSTANCE:
553 {
554 type_instance_flags flags
555 = (type_instance_flag_value) longest_to_int (exp->elts[pc + 1].longconst);
556 LONGEST count = exp->elts[pc + 2].longconst;
557
558 /* The FLAGS. */
559 (*pos)++;
560 /* The COUNT. */
561 (*pos)++;
562 fputs_unfiltered ("TypeInstance(", stream);
563 while (count-- > 0)
564 {
565 type_print (exp->elts[(*pos)++].type, "", stream, 0);
566 if (count > 0)
567 fputs_unfiltered (",", stream);
568 }
569 fputs_unfiltered (",", stream);
570 /* Ending COUNT and ending TYPE_INSTANCE. */
571 (*pos) += 2;
572 print_subexp (exp, pos, stream, PREC_PREFIX);
573
574 if (flags & TYPE_INSTANCE_FLAG_CONST)
575 fputs_unfiltered (",const", stream);
576 if (flags & TYPE_INSTANCE_FLAG_VOLATILE)
577 fputs_unfiltered (",volatile", stream);
578
579 fputs_unfiltered (")", stream);
580 return;
581 }
582
583 case OP_RANGE:
584 {
585 enum range_flag range_flag;
586
587 range_flag = (enum range_flag)
588 longest_to_int (exp->elts[pc + 1].longconst);
589 *pos += 2;
590
591 if (range_flag & RANGE_HIGH_BOUND_EXCLUSIVE)
592 fputs_filtered ("EXCLUSIVE_", stream);
593 fputs_filtered ("RANGE(", stream);
594 if (!(range_flag & RANGE_LOW_BOUND_DEFAULT))
595 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
596 fputs_filtered ("..", stream);
597 if (!(range_flag & RANGE_HIGH_BOUND_DEFAULT))
598 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
599 fputs_filtered (")", stream);
600 return;
601 }
602
603 /* Default ops */
604
605 default:
606 op_str = "???";
607 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
608 if (op_print_tab[tem].opcode == opcode)
609 {
610 op_str = op_print_tab[tem].string;
611 myprec = op_print_tab[tem].precedence;
612 assoc = op_print_tab[tem].right_assoc;
613 break;
614 }
615 if (op_print_tab[tem].opcode != opcode)
616 /* Not found; don't try to keep going because we don't know how
617 to interpret further elements. For example, this happens
618 if opcode is OP_TYPE. */
619 error (_("Invalid expression"));
620 }
621
622 /* Note that PREC_BUILTIN will always emit parentheses. */
623 if ((int) myprec < (int) prec)
624 fputs_filtered ("(", stream);
625 if ((int) opcode > (int) BINOP_END)
626 {
627 if (assoc)
628 {
629 /* Unary postfix operator. */
630 print_subexp (exp, pos, stream, PREC_SUFFIX);
631 fputs_filtered (op_str, stream);
632 }
633 else
634 {
635 /* Unary prefix operator. */
636 fputs_filtered (op_str, stream);
637 if (myprec == PREC_BUILTIN_FUNCTION)
638 fputs_filtered ("(", stream);
639 print_subexp (exp, pos, stream, PREC_PREFIX);
640 if (myprec == PREC_BUILTIN_FUNCTION)
641 fputs_filtered (")", stream);
642 }
643 }
644 else
645 {
646 /* Binary operator. */
647 /* Print left operand.
648 If operator is right-associative,
649 increment precedence for this operand. */
650 print_subexp (exp, pos, stream,
651 (enum precedence) ((int) myprec + assoc));
652 /* Print the operator itself. */
653 if (assign_modify)
654 fprintf_filtered (stream, " %s= ", op_str);
655 else if (op_str[0] == ',')
656 fprintf_filtered (stream, "%s ", op_str);
657 else
658 fprintf_filtered (stream, " %s ", op_str);
659 /* Print right operand.
660 If operator is left-associative,
661 increment precedence for this operand. */
662 print_subexp (exp, pos, stream,
663 (enum precedence) ((int) myprec + !assoc));
664 }
665
666 if ((int) myprec < (int) prec)
667 fputs_filtered (")", stream);
668}
669
670/* Return the operator corresponding to opcode OP as
671 a string. NULL indicates that the opcode was not found in the
672 current language table. */
673const char *
674op_string (enum exp_opcode op)
675{
676 int tem;
677 const struct op_print *op_print_tab;
678
679 op_print_tab = current_language->opcode_print_table ();
680 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
681 if (op_print_tab[tem].opcode == op)
682 return op_print_tab[tem].string;
683 return NULL;
684}
685
686/* Support for dumping the raw data from expressions in a human readable
687 form. */
688
689static int dump_subexp_body (struct expression *exp, struct ui_file *, int);
690
691/* Default name for the standard operator OPCODE (i.e., one defined in
692 the definition of enum exp_opcode). */
693
694const char *
695op_name (enum exp_opcode opcode)
696{
697 switch (opcode)
698 {
699 default:
700 {
701 static char buf[30];
702
703 xsnprintf (buf, sizeof (buf), "<unknown %d>", opcode);
704 return buf;
705 }
706#define OP(name) \
707 case name: \
708 return #name ;
709#include "std-operator.def"
710#undef OP
711 }
712}
713
714/* Print a raw dump of expression EXP to STREAM.
715 NOTE, if non-NULL, is printed as extra explanatory text. */
716
717void
718dump_raw_expression (struct expression *exp, struct ui_file *stream,
719 const char *note)
720{
721 int elt;
722 char *eltscan;
723 int eltsize;
724
725 fprintf_filtered (stream, "Dump of expression @ ");
726 gdb_print_host_address (exp, stream);
727 if (note)
728 fprintf_filtered (stream, ", %s:", note);
729 fprintf_filtered (stream, "\n\tLanguage %s, %d elements, %ld bytes each.\n",
730 exp->language_defn->name (), exp->nelts,
731 (long) sizeof (union exp_element));
732 fprintf_filtered (stream, "\t%5s %20s %16s %s\n", "Index", "Opcode",
733 "Hex Value", "String Value");
734 for (elt = 0; elt < exp->nelts; elt++)
735 {
736 fprintf_filtered (stream, "\t%5d ", elt);
737
738 const char *opcode_name = op_name (exp->elts[elt].opcode);
739 fprintf_filtered (stream, "%20s ", opcode_name);
740
741 print_longest (stream, 'd', 0, exp->elts[elt].longconst);
742 fprintf_filtered (stream, " ");
743
744 for (eltscan = (char *) &exp->elts[elt],
745 eltsize = sizeof (union exp_element);
746 eltsize-- > 0;
747 eltscan++)
748 {
749 fprintf_filtered (stream, "%c",
750 isprint (*eltscan) ? (*eltscan & 0xFF) : '.');
751 }
752 fprintf_filtered (stream, "\n");
753 }
754}
755
756/* Dump the subexpression of prefix expression EXP whose operator is at
757 position ELT onto STREAM. Returns the position of the next
758 subexpression in EXP. */
759
760int
761dump_subexp (struct expression *exp, struct ui_file *stream, int elt)
762{
763 static int indent = 0;
764 int i;
765
766 fprintf_filtered (stream, "\n");
767 fprintf_filtered (stream, "\t%5d ", elt);
768
769 for (i = 1; i <= indent; i++)
770 fprintf_filtered (stream, " ");
771 indent += 2;
772
773 fprintf_filtered (stream, "%-20s ", op_name (exp->elts[elt].opcode));
774
775 elt = dump_subexp_body (exp, stream, elt);
776
777 indent -= 2;
778
779 return elt;
780}
781
782/* Dump the operands of prefix expression EXP whose opcode is at
783 position ELT onto STREAM. Returns the position of the next
784 subexpression in EXP. */
785
786static int
787dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
788{
789 return exp->language_defn->expression_ops ()->dump_subexp_body (exp, stream,
790 elt);
791}
792
793/* See parser-defs.h. */
794
795int
796dump_subexp_body_funcall (struct expression *exp,
797 struct ui_file *stream, int elt)
798{
799 int nargs = longest_to_int (exp->elts[elt].longconst);
800 fprintf_filtered (stream, "Number of args: %d", nargs);
801 elt += 2;
802
803 for (int i = 1; i <= nargs + 1; i++)
804 elt = dump_subexp (exp, stream, elt);
805
806 return elt;
807}
808
809/* Default value for subexp_body in exp_descriptor vector. */
810
811int
812dump_subexp_body_standard (struct expression *exp,
813 struct ui_file *stream, int elt)
814{
815 int opcode = exp->elts[elt++].opcode;
816
817 switch (opcode)
818 {
819 case TERNOP_COND:
820 case TERNOP_SLICE:
821 elt = dump_subexp (exp, stream, elt);
822 /* FALL THROUGH */
823 case BINOP_ADD:
824 case BINOP_SUB:
825 case BINOP_MUL:
826 case BINOP_DIV:
827 case BINOP_REM:
828 case BINOP_MOD:
829 case BINOP_LSH:
830 case BINOP_RSH:
831 case BINOP_LOGICAL_AND:
832 case BINOP_LOGICAL_OR:
833 case BINOP_BITWISE_AND:
834 case BINOP_BITWISE_IOR:
835 case BINOP_BITWISE_XOR:
836 case BINOP_EQUAL:
837 case BINOP_NOTEQUAL:
838 case BINOP_LESS:
839 case BINOP_GTR:
840 case BINOP_LEQ:
841 case BINOP_GEQ:
842 case BINOP_REPEAT:
843 case BINOP_ASSIGN:
844 case BINOP_COMMA:
845 case BINOP_SUBSCRIPT:
846 case BINOP_EXP:
847 case BINOP_MIN:
848 case BINOP_MAX:
849 case BINOP_INTDIV:
850 case BINOP_ASSIGN_MODIFY:
851 case BINOP_VAL:
852 case BINOP_CONCAT:
853 case BINOP_END:
854 case STRUCTOP_MEMBER:
855 case STRUCTOP_MPTR:
856 elt = dump_subexp (exp, stream, elt);
857 /* FALL THROUGH */
858 case UNOP_NEG:
859 case UNOP_LOGICAL_NOT:
860 case UNOP_COMPLEMENT:
861 case UNOP_IND:
862 case UNOP_ADDR:
863 case UNOP_PREINCREMENT:
864 case UNOP_POSTINCREMENT:
865 case UNOP_PREDECREMENT:
866 case UNOP_POSTDECREMENT:
867 case UNOP_SIZEOF:
868 case UNOP_ALIGNOF:
869 case UNOP_PLUS:
870 case UNOP_CAP:
871 case UNOP_CHR:
872 case UNOP_ORD:
873 case UNOP_ABS:
874 case UNOP_FLOAT:
875 case UNOP_HIGH:
876 case UNOP_MAX:
877 case UNOP_MIN:
878 case UNOP_ODD:
879 case UNOP_TRUNC:
880 elt = dump_subexp (exp, stream, elt);
881 break;
882 case OP_LONG:
883 fprintf_filtered (stream, "Type @");
884 gdb_print_host_address (exp->elts[elt].type, stream);
885 fprintf_filtered (stream, " (");
886 type_print (exp->elts[elt].type, NULL, stream, 0);
887 fprintf_filtered (stream, "), value %ld (0x%lx)",
888 (long) exp->elts[elt + 1].longconst,
889 (long) exp->elts[elt + 1].longconst);
890 elt += 3;
891 break;
892 case OP_FLOAT:
893 fprintf_filtered (stream, "Type @");
894 gdb_print_host_address (exp->elts[elt].type, stream);
895 fprintf_filtered (stream, " (");
896 type_print (exp->elts[elt].type, NULL, stream, 0);
897 fprintf_filtered (stream, "), value ");
898 print_floating (exp->elts[elt + 1].floatconst,
899 exp->elts[elt].type, stream);
900 elt += 3;
901 break;
902 case OP_VAR_VALUE:
903 fprintf_filtered (stream, "Block @");
904 gdb_print_host_address (exp->elts[elt].block, stream);
905 fprintf_filtered (stream, ", symbol @");
906 gdb_print_host_address (exp->elts[elt + 1].symbol, stream);
907 fprintf_filtered (stream, " (%s)",
908 exp->elts[elt + 1].symbol->print_name ());
909 elt += 3;
910 break;
911 case OP_VAR_MSYM_VALUE:
912 fprintf_filtered (stream, "Objfile @");
913 gdb_print_host_address (exp->elts[elt].objfile, stream);
914 fprintf_filtered (stream, ", msymbol @");
915 gdb_print_host_address (exp->elts[elt + 1].msymbol, stream);
916 fprintf_filtered (stream, " (%s)",
917 exp->elts[elt + 1].msymbol->print_name ());
918 elt += 3;
919 break;
920 case OP_VAR_ENTRY_VALUE:
921 fprintf_filtered (stream, "Entry value of symbol @");
922 gdb_print_host_address (exp->elts[elt].symbol, stream);
923 fprintf_filtered (stream, " (%s)",
924 exp->elts[elt].symbol->print_name ());
925 elt += 2;
926 break;
927 case OP_LAST:
928 fprintf_filtered (stream, "History element %ld",
929 (long) exp->elts[elt].longconst);
930 elt += 2;
931 break;
932 case OP_REGISTER:
933 fprintf_filtered (stream, "Register $%s", &exp->elts[elt + 1].string);
934 elt += 3 + BYTES_TO_EXP_ELEM (exp->elts[elt].longconst + 1);
935 break;
936 case OP_INTERNALVAR:
937 fprintf_filtered (stream, "Internal var @");
938 gdb_print_host_address (exp->elts[elt].internalvar, stream);
939 fprintf_filtered (stream, " (%s)",
940 internalvar_name (exp->elts[elt].internalvar));
941 elt += 2;
942 break;
943 case OP_FUNCALL:
944 elt = dump_subexp_body_funcall (exp, stream, elt);
945 break;
946 case OP_ARRAY:
947 {
948 int lower, upper;
949 int i;
950
951 lower = longest_to_int (exp->elts[elt].longconst);
952 upper = longest_to_int (exp->elts[elt + 1].longconst);
953
954 fprintf_filtered (stream, "Bounds [%d:%d]", lower, upper);
955 elt += 3;
956
957 for (i = 1; i <= upper - lower + 1; i++)
958 elt = dump_subexp (exp, stream, elt);
959 }
960 break;
961 case UNOP_DYNAMIC_CAST:
962 case UNOP_REINTERPRET_CAST:
963 case UNOP_CAST_TYPE:
964 case UNOP_MEMVAL_TYPE:
965 fprintf_filtered (stream, " (");
966 elt = dump_subexp (exp, stream, elt);
967 fprintf_filtered (stream, ")");
968 elt = dump_subexp (exp, stream, elt);
969 break;
970 case UNOP_MEMVAL:
971 case UNOP_CAST:
972 fprintf_filtered (stream, "Type @");
973 gdb_print_host_address (exp->elts[elt].type, stream);
974 fprintf_filtered (stream, " (");
975 type_print (exp->elts[elt].type, NULL, stream, 0);
976 fprintf_filtered (stream, ")");
977 elt = dump_subexp (exp, stream, elt + 2);
978 break;
979 case OP_TYPE:
980 fprintf_filtered (stream, "Type @");
981 gdb_print_host_address (exp->elts[elt].type, stream);
982 fprintf_filtered (stream, " (");
983 type_print (exp->elts[elt].type, NULL, stream, 0);
984 fprintf_filtered (stream, ")");
985 elt += 2;
986 break;
987 case OP_TYPEOF:
988 case OP_DECLTYPE:
989 fprintf_filtered (stream, "Typeof (");
990 elt = dump_subexp (exp, stream, elt);
991 fprintf_filtered (stream, ")");
992 break;
993 case OP_TYPEID:
994 fprintf_filtered (stream, "typeid (");
995 elt = dump_subexp (exp, stream, elt);
996 fprintf_filtered (stream, ")");
997 break;
998 case STRUCTOP_STRUCT:
999 case STRUCTOP_PTR:
1000 {
1001 char *elem_name;
1002 int len;
1003
1004 len = longest_to_int (exp->elts[elt].longconst);
1005 elem_name = &exp->elts[elt + 1].string;
1006
1007 fprintf_filtered (stream, "Element name: `%.*s'", len, elem_name);
1008 elt = dump_subexp (exp, stream, elt + 3 + BYTES_TO_EXP_ELEM (len + 1));
1009 }
1010 break;
1011 case OP_SCOPE:
1012 {
1013 char *elem_name;
1014 int len;
1015
1016 fprintf_filtered (stream, "Type @");
1017 gdb_print_host_address (exp->elts[elt].type, stream);
1018 fprintf_filtered (stream, " (");
1019 type_print (exp->elts[elt].type, NULL, stream, 0);
1020 fprintf_filtered (stream, ") ");
1021
1022 len = longest_to_int (exp->elts[elt + 1].longconst);
1023 elem_name = &exp->elts[elt + 2].string;
1024
1025 fprintf_filtered (stream, "Field name: `%.*s'", len, elem_name);
1026 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1027 }
1028 break;
1029
1030 case OP_FUNC_STATIC_VAR:
1031 {
1032 int len = longest_to_int (exp->elts[elt].longconst);
1033 const char *var_name = &exp->elts[elt + 1].string;
1034 fprintf_filtered (stream, "Field name: `%.*s'", len, var_name);
1035 elt += 3 + BYTES_TO_EXP_ELEM (len + 1);
1036 }
1037 break;
1038
1039 case TYPE_INSTANCE:
1040 {
1041 type_instance_flags flags
1042 = (type_instance_flag_value) longest_to_int (exp->elts[elt++].longconst);
1043 LONGEST len = exp->elts[elt++].longconst;
1044 fprintf_filtered (stream, "%s TypeInstance: ", plongest (len));
1045 while (len-- > 0)
1046 {
1047 fprintf_filtered (stream, "Type @");
1048 gdb_print_host_address (exp->elts[elt].type, stream);
1049 fprintf_filtered (stream, " (");
1050 type_print (exp->elts[elt].type, NULL, stream, 0);
1051 fprintf_filtered (stream, ")");
1052 elt++;
1053 if (len > 0)
1054 fputs_filtered (", ", stream);
1055 }
1056
1057 fprintf_filtered (stream, " Flags: %s (", hex_string (flags));
1058 bool space = false;
1059 auto print_one = [&] (const char *mod)
1060 {
1061 if (space)
1062 fputs_filtered (" ", stream);
1063 space = true;
1064 fprintf_filtered (stream, "%s", mod);
1065 };
1066 if (flags & TYPE_INSTANCE_FLAG_CONST)
1067 print_one ("const");
1068 if (flags & TYPE_INSTANCE_FLAG_VOLATILE)
1069 print_one ("volatile");
1070 fprintf_filtered (stream, ")");
1071
1072 /* Ending LEN and ending TYPE_INSTANCE. */
1073 elt += 2;
1074 elt = dump_subexp (exp, stream, elt);
1075 }
1076 break;
1077 case OP_STRING:
1078 {
1079 LONGEST len = exp->elts[elt].longconst;
1080 LONGEST type = exp->elts[elt + 1].longconst;
1081
1082 fprintf_filtered (stream, "Language-specific string type: %s",
1083 plongest (type));
1084
1085 /* Skip length. */
1086 elt += 1;
1087
1088 /* Skip string content. */
1089 elt += BYTES_TO_EXP_ELEM (len);
1090
1091 /* Skip length and ending OP_STRING. */
1092 elt += 2;
1093 }
1094 break;
1095 case OP_RANGE:
1096 {
1097 enum range_flag range_flag;
1098
1099 range_flag = (enum range_flag)
1100 longest_to_int (exp->elts[elt].longconst);
1101 elt += 2;
1102
1103 if (range_flag & RANGE_HIGH_BOUND_EXCLUSIVE)
1104 fputs_filtered ("Exclusive", stream);
1105 fputs_filtered ("Range '", stream);
1106 if (!(range_flag & RANGE_LOW_BOUND_DEFAULT))
1107 fputs_filtered ("EXP", stream);
1108 fputs_filtered ("..", stream);
1109 if (!(range_flag & RANGE_HIGH_BOUND_DEFAULT))
1110 fputs_filtered ("EXP", stream);
1111 if (range_flag & RANGE_HAS_STRIDE)
1112 fputs_filtered (":EXP", stream);
1113 fputs_filtered ("'", stream);
1114
1115 if (!(range_flag & RANGE_LOW_BOUND_DEFAULT))
1116 elt = dump_subexp (exp, stream, elt);
1117 if (!(range_flag & RANGE_HIGH_BOUND_DEFAULT))
1118 elt = dump_subexp (exp, stream, elt);
1119 if (range_flag & RANGE_HAS_STRIDE)
1120 elt = dump_subexp (exp, stream, elt);
1121 }
1122 break;
1123
1124 default:
1125 case OP_NULL:
1126 case MULTI_SUBSCRIPT:
1127 case OP_COMPLEX:
1128 case OP_BOOL:
1129 case OP_M2_STRING:
1130 case OP_THIS:
1131 case OP_NAME:
1132 fprintf_filtered (stream, "Unknown format");
1133 }
1134
1135 return elt;
1136}
1137
1138void
1139dump_prefix_expression (struct expression *exp, struct ui_file *stream)
1140{
1141 int elt;
1142
1143 fprintf_filtered (stream, "Dump of expression @ ");
1144 gdb_print_host_address (exp, stream);
1145 fputs_filtered (", after conversion to prefix form:\nExpression: `", stream);
1146 print_expression (exp, stream);
1147 fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
1148 exp->language_defn->name (), exp->nelts,
1149 (long) sizeof (union exp_element));
1150 fputs_filtered ("\n", stream);
1151
1152 for (elt = 0; elt < exp->nelts;)
1153 elt = dump_subexp (exp, stream, elt);
1154 fputs_filtered ("\n", stream);
1155}