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