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