Add an expr::operation_up to struct expression
[deliverable/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 if (exp->op != nullptr)
729 return;
730
731 fprintf_filtered (stream, "Dump of expression @ ");
732 gdb_print_host_address (exp, stream);
733 if (note)
734 fprintf_filtered (stream, ", %s:", note);
735 fprintf_filtered (stream, "\n\tLanguage %s, %d elements, %ld bytes each.\n",
736 exp->language_defn->name (), exp->nelts,
737 (long) sizeof (union exp_element));
738 fprintf_filtered (stream, "\t%5s %20s %16s %s\n", "Index", "Opcode",
739 "Hex Value", "String Value");
740 for (elt = 0; elt < exp->nelts; elt++)
741 {
742 fprintf_filtered (stream, "\t%5d ", elt);
743
744 const char *opcode_name = op_name (exp->elts[elt].opcode);
745 fprintf_filtered (stream, "%20s ", opcode_name);
746
747 print_longest (stream, 'd', 0, exp->elts[elt].longconst);
748 fprintf_filtered (stream, " ");
749
750 for (eltscan = (char *) &exp->elts[elt],
751 eltsize = sizeof (union exp_element);
752 eltsize-- > 0;
753 eltscan++)
754 {
755 fprintf_filtered (stream, "%c",
756 isprint (*eltscan) ? (*eltscan & 0xFF) : '.');
757 }
758 fprintf_filtered (stream, "\n");
759 }
760 }
761
762 /* Dump the subexpression of prefix expression EXP whose operator is at
763 position ELT onto STREAM. Returns the position of the next
764 subexpression in EXP. */
765
766 int
767 dump_subexp (struct expression *exp, struct ui_file *stream, int elt)
768 {
769 static int indent = 0;
770 int i;
771
772 fprintf_filtered (stream, "\n");
773 fprintf_filtered (stream, "\t%5d ", elt);
774
775 for (i = 1; i <= indent; i++)
776 fprintf_filtered (stream, " ");
777 indent += 2;
778
779 fprintf_filtered (stream, "%-20s ", op_name (exp->elts[elt].opcode));
780
781 elt = dump_subexp_body (exp, stream, elt);
782
783 indent -= 2;
784
785 return elt;
786 }
787
788 /* Dump the operands of prefix expression EXP whose opcode is at
789 position ELT onto STREAM. Returns the position of the next
790 subexpression in EXP. */
791
792 static int
793 dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
794 {
795 return exp->language_defn->expression_ops ()->dump_subexp_body (exp, stream,
796 elt);
797 }
798
799 /* See parser-defs.h. */
800
801 int
802 dump_subexp_body_funcall (struct expression *exp,
803 struct ui_file *stream, int elt)
804 {
805 int nargs = longest_to_int (exp->elts[elt].longconst);
806 fprintf_filtered (stream, "Number of args: %d", nargs);
807 elt += 2;
808
809 for (int i = 1; i <= nargs + 1; i++)
810 elt = dump_subexp (exp, stream, elt);
811
812 return elt;
813 }
814
815 /* Default value for subexp_body in exp_descriptor vector. */
816
817 int
818 dump_subexp_body_standard (struct expression *exp,
819 struct ui_file *stream, int elt)
820 {
821 int opcode = exp->elts[elt++].opcode;
822
823 switch (opcode)
824 {
825 case TERNOP_COND:
826 case TERNOP_SLICE:
827 elt = dump_subexp (exp, stream, elt);
828 /* FALL THROUGH */
829 case BINOP_ADD:
830 case BINOP_SUB:
831 case BINOP_MUL:
832 case BINOP_DIV:
833 case BINOP_REM:
834 case BINOP_MOD:
835 case BINOP_LSH:
836 case BINOP_RSH:
837 case BINOP_LOGICAL_AND:
838 case BINOP_LOGICAL_OR:
839 case BINOP_BITWISE_AND:
840 case BINOP_BITWISE_IOR:
841 case BINOP_BITWISE_XOR:
842 case BINOP_EQUAL:
843 case BINOP_NOTEQUAL:
844 case BINOP_LESS:
845 case BINOP_GTR:
846 case BINOP_LEQ:
847 case BINOP_GEQ:
848 case BINOP_REPEAT:
849 case BINOP_ASSIGN:
850 case BINOP_COMMA:
851 case BINOP_SUBSCRIPT:
852 case BINOP_EXP:
853 case BINOP_MIN:
854 case BINOP_MAX:
855 case BINOP_INTDIV:
856 case BINOP_ASSIGN_MODIFY:
857 case BINOP_VAL:
858 case BINOP_CONCAT:
859 case BINOP_END:
860 case STRUCTOP_MEMBER:
861 case STRUCTOP_MPTR:
862 elt = dump_subexp (exp, stream, elt);
863 /* FALL THROUGH */
864 case UNOP_NEG:
865 case UNOP_LOGICAL_NOT:
866 case UNOP_COMPLEMENT:
867 case UNOP_IND:
868 case UNOP_ADDR:
869 case UNOP_PREINCREMENT:
870 case UNOP_POSTINCREMENT:
871 case UNOP_PREDECREMENT:
872 case UNOP_POSTDECREMENT:
873 case UNOP_SIZEOF:
874 case UNOP_ALIGNOF:
875 case UNOP_PLUS:
876 case UNOP_CAP:
877 case UNOP_CHR:
878 case UNOP_ORD:
879 case UNOP_ABS:
880 case UNOP_FLOAT:
881 case UNOP_HIGH:
882 case UNOP_MAX:
883 case UNOP_MIN:
884 case UNOP_ODD:
885 case UNOP_TRUNC:
886 elt = dump_subexp (exp, stream, elt);
887 break;
888 case OP_LONG:
889 fprintf_filtered (stream, "Type @");
890 gdb_print_host_address (exp->elts[elt].type, stream);
891 fprintf_filtered (stream, " (");
892 type_print (exp->elts[elt].type, NULL, stream, 0);
893 fprintf_filtered (stream, "), value %ld (0x%lx)",
894 (long) exp->elts[elt + 1].longconst,
895 (long) exp->elts[elt + 1].longconst);
896 elt += 3;
897 break;
898 case OP_FLOAT:
899 fprintf_filtered (stream, "Type @");
900 gdb_print_host_address (exp->elts[elt].type, stream);
901 fprintf_filtered (stream, " (");
902 type_print (exp->elts[elt].type, NULL, stream, 0);
903 fprintf_filtered (stream, "), value ");
904 print_floating (exp->elts[elt + 1].floatconst,
905 exp->elts[elt].type, stream);
906 elt += 3;
907 break;
908 case OP_VAR_VALUE:
909 fprintf_filtered (stream, "Block @");
910 gdb_print_host_address (exp->elts[elt].block, stream);
911 fprintf_filtered (stream, ", symbol @");
912 gdb_print_host_address (exp->elts[elt + 1].symbol, stream);
913 fprintf_filtered (stream, " (%s)",
914 exp->elts[elt + 1].symbol->print_name ());
915 elt += 3;
916 break;
917 case OP_VAR_MSYM_VALUE:
918 fprintf_filtered (stream, "Objfile @");
919 gdb_print_host_address (exp->elts[elt].objfile, stream);
920 fprintf_filtered (stream, ", msymbol @");
921 gdb_print_host_address (exp->elts[elt + 1].msymbol, stream);
922 fprintf_filtered (stream, " (%s)",
923 exp->elts[elt + 1].msymbol->print_name ());
924 elt += 3;
925 break;
926 case OP_VAR_ENTRY_VALUE:
927 fprintf_filtered (stream, "Entry value of symbol @");
928 gdb_print_host_address (exp->elts[elt].symbol, stream);
929 fprintf_filtered (stream, " (%s)",
930 exp->elts[elt].symbol->print_name ());
931 elt += 2;
932 break;
933 case OP_LAST:
934 fprintf_filtered (stream, "History element %ld",
935 (long) exp->elts[elt].longconst);
936 elt += 2;
937 break;
938 case OP_REGISTER:
939 fprintf_filtered (stream, "Register $%s", &exp->elts[elt + 1].string);
940 elt += 3 + BYTES_TO_EXP_ELEM (exp->elts[elt].longconst + 1);
941 break;
942 case OP_INTERNALVAR:
943 fprintf_filtered (stream, "Internal var @");
944 gdb_print_host_address (exp->elts[elt].internalvar, stream);
945 fprintf_filtered (stream, " (%s)",
946 internalvar_name (exp->elts[elt].internalvar));
947 elt += 2;
948 break;
949 case OP_FUNCALL:
950 elt = dump_subexp_body_funcall (exp, stream, elt);
951 break;
952 case OP_ARRAY:
953 {
954 int lower, upper;
955 int i;
956
957 lower = longest_to_int (exp->elts[elt].longconst);
958 upper = longest_to_int (exp->elts[elt + 1].longconst);
959
960 fprintf_filtered (stream, "Bounds [%d:%d]", lower, upper);
961 elt += 3;
962
963 for (i = 1; i <= upper - lower + 1; i++)
964 elt = dump_subexp (exp, stream, elt);
965 }
966 break;
967 case UNOP_DYNAMIC_CAST:
968 case UNOP_REINTERPRET_CAST:
969 case UNOP_CAST_TYPE:
970 case UNOP_MEMVAL_TYPE:
971 fprintf_filtered (stream, " (");
972 elt = dump_subexp (exp, stream, elt);
973 fprintf_filtered (stream, ")");
974 elt = dump_subexp (exp, stream, elt);
975 break;
976 case UNOP_MEMVAL:
977 case UNOP_CAST:
978 fprintf_filtered (stream, "Type @");
979 gdb_print_host_address (exp->elts[elt].type, stream);
980 fprintf_filtered (stream, " (");
981 type_print (exp->elts[elt].type, NULL, stream, 0);
982 fprintf_filtered (stream, ")");
983 elt = dump_subexp (exp, stream, elt + 2);
984 break;
985 case OP_TYPE:
986 fprintf_filtered (stream, "Type @");
987 gdb_print_host_address (exp->elts[elt].type, stream);
988 fprintf_filtered (stream, " (");
989 type_print (exp->elts[elt].type, NULL, stream, 0);
990 fprintf_filtered (stream, ")");
991 elt += 2;
992 break;
993 case OP_TYPEOF:
994 case OP_DECLTYPE:
995 fprintf_filtered (stream, "Typeof (");
996 elt = dump_subexp (exp, stream, elt);
997 fprintf_filtered (stream, ")");
998 break;
999 case OP_TYPEID:
1000 fprintf_filtered (stream, "typeid (");
1001 elt = dump_subexp (exp, stream, elt);
1002 fprintf_filtered (stream, ")");
1003 break;
1004 case STRUCTOP_STRUCT:
1005 case STRUCTOP_PTR:
1006 {
1007 char *elem_name;
1008 int len;
1009
1010 len = longest_to_int (exp->elts[elt].longconst);
1011 elem_name = &exp->elts[elt + 1].string;
1012
1013 fprintf_filtered (stream, "Element name: `%.*s'", len, elem_name);
1014 elt = dump_subexp (exp, stream, elt + 3 + BYTES_TO_EXP_ELEM (len + 1));
1015 }
1016 break;
1017 case OP_SCOPE:
1018 {
1019 char *elem_name;
1020 int len;
1021
1022 fprintf_filtered (stream, "Type @");
1023 gdb_print_host_address (exp->elts[elt].type, stream);
1024 fprintf_filtered (stream, " (");
1025 type_print (exp->elts[elt].type, NULL, stream, 0);
1026 fprintf_filtered (stream, ") ");
1027
1028 len = longest_to_int (exp->elts[elt + 1].longconst);
1029 elem_name = &exp->elts[elt + 2].string;
1030
1031 fprintf_filtered (stream, "Field name: `%.*s'", len, elem_name);
1032 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1033 }
1034 break;
1035
1036 case OP_FUNC_STATIC_VAR:
1037 {
1038 int len = longest_to_int (exp->elts[elt].longconst);
1039 const char *var_name = &exp->elts[elt + 1].string;
1040 fprintf_filtered (stream, "Field name: `%.*s'", len, var_name);
1041 elt += 3 + BYTES_TO_EXP_ELEM (len + 1);
1042 }
1043 break;
1044
1045 case TYPE_INSTANCE:
1046 {
1047 type_instance_flags flags
1048 = (type_instance_flag_value) longest_to_int (exp->elts[elt++].longconst);
1049 LONGEST len = exp->elts[elt++].longconst;
1050 fprintf_filtered (stream, "%s TypeInstance: ", plongest (len));
1051 while (len-- > 0)
1052 {
1053 fprintf_filtered (stream, "Type @");
1054 gdb_print_host_address (exp->elts[elt].type, stream);
1055 fprintf_filtered (stream, " (");
1056 type_print (exp->elts[elt].type, NULL, stream, 0);
1057 fprintf_filtered (stream, ")");
1058 elt++;
1059 if (len > 0)
1060 fputs_filtered (", ", stream);
1061 }
1062
1063 fprintf_filtered (stream, " Flags: %s (", hex_string (flags));
1064 bool space = false;
1065 auto print_one = [&] (const char *mod)
1066 {
1067 if (space)
1068 fputs_filtered (" ", stream);
1069 space = true;
1070 fprintf_filtered (stream, "%s", mod);
1071 };
1072 if (flags & TYPE_INSTANCE_FLAG_CONST)
1073 print_one ("const");
1074 if (flags & TYPE_INSTANCE_FLAG_VOLATILE)
1075 print_one ("volatile");
1076 fprintf_filtered (stream, ")");
1077
1078 /* Ending LEN and ending TYPE_INSTANCE. */
1079 elt += 2;
1080 elt = dump_subexp (exp, stream, elt);
1081 }
1082 break;
1083 case OP_STRING:
1084 {
1085 LONGEST len = exp->elts[elt].longconst;
1086 LONGEST type = exp->elts[elt + 1].longconst;
1087
1088 fprintf_filtered (stream, "Language-specific string type: %s",
1089 plongest (type));
1090
1091 /* Skip length. */
1092 elt += 1;
1093
1094 /* Skip string content. */
1095 elt += BYTES_TO_EXP_ELEM (len);
1096
1097 /* Skip length and ending OP_STRING. */
1098 elt += 2;
1099 }
1100 break;
1101 case OP_RANGE:
1102 {
1103 enum range_flag range_flag;
1104
1105 range_flag = (enum range_flag)
1106 longest_to_int (exp->elts[elt].longconst);
1107 elt += 2;
1108
1109 if (range_flag & RANGE_HIGH_BOUND_EXCLUSIVE)
1110 fputs_filtered ("Exclusive", stream);
1111 fputs_filtered ("Range '", stream);
1112 if (!(range_flag & RANGE_LOW_BOUND_DEFAULT))
1113 fputs_filtered ("EXP", stream);
1114 fputs_filtered ("..", stream);
1115 if (!(range_flag & RANGE_HIGH_BOUND_DEFAULT))
1116 fputs_filtered ("EXP", stream);
1117 if (range_flag & RANGE_HAS_STRIDE)
1118 fputs_filtered (":EXP", stream);
1119 fputs_filtered ("'", stream);
1120
1121 if (!(range_flag & RANGE_LOW_BOUND_DEFAULT))
1122 elt = dump_subexp (exp, stream, elt);
1123 if (!(range_flag & RANGE_HIGH_BOUND_DEFAULT))
1124 elt = dump_subexp (exp, stream, elt);
1125 if (range_flag & RANGE_HAS_STRIDE)
1126 elt = dump_subexp (exp, stream, elt);
1127 }
1128 break;
1129
1130 case OP_BOOL:
1131 {
1132 bool val = (bool) (exp->elts[elt].longconst);
1133 fputs_filtered (val ? "TRUE" : "FALSE", stream);
1134 elt += 2;
1135 }
1136 break;
1137
1138 default:
1139 case OP_NULL:
1140 case MULTI_SUBSCRIPT:
1141 case OP_COMPLEX:
1142 case OP_M2_STRING:
1143 case OP_THIS:
1144 case OP_NAME:
1145 fprintf_filtered (stream, "Unknown format");
1146 }
1147
1148 return elt;
1149 }
1150
1151 void
1152 dump_prefix_expression (struct expression *exp, struct ui_file *stream)
1153 {
1154 int elt;
1155
1156 if (exp->op != nullptr)
1157 {
1158 exp->op->dump (stream, 0);
1159 return;
1160 }
1161
1162 fprintf_filtered (stream, "Dump of expression @ ");
1163 gdb_print_host_address (exp, stream);
1164 fputs_filtered (", after conversion to prefix form:\nExpression: `", stream);
1165
1166 if (exp->op != nullptr)
1167 {
1168 exp->op->dump (stream, 0);
1169 return;
1170 }
1171
1172 print_expression (exp, stream);
1173 fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
1174 exp->language_defn->name (), exp->nelts,
1175 (long) sizeof (union exp_element));
1176 fputs_filtered ("\n", stream);
1177
1178 for (elt = 0; elt < exp->nelts;)
1179 elt = dump_subexp (exp, stream, elt);
1180 fputs_filtered ("\n", stream);
1181 }
1182
1183 namespace expr
1184 {
1185
1186 void
1187 dump_for_expression (struct ui_file *stream, int depth, enum exp_opcode op)
1188 {
1189 fprintf_filtered (stream, _("%*sOperation: %s\n"), depth, "", op_name (op));
1190 }
1191
1192 void
1193 dump_for_expression (struct ui_file *stream, int depth, const std::string &str)
1194 {
1195 fprintf_filtered (stream, _("%*sString: %s\n"), depth, "", str.c_str ());
1196 }
1197
1198 void
1199 dump_for_expression (struct ui_file *stream, int depth, struct type *type)
1200 {
1201 fprintf_filtered (stream, _("%*sType: "), depth, "");
1202 type_print (type, nullptr, stream, 0);
1203 fprintf_filtered (stream, "\n");
1204 }
1205
1206 void
1207 dump_for_expression (struct ui_file *stream, int depth, CORE_ADDR addr)
1208 {
1209 fprintf_filtered (stream, _("%*sConstant: %s\n"), depth, "",
1210 core_addr_to_string (addr));
1211 }
1212
1213 void
1214 dump_for_expression (struct ui_file *stream, int depth, internalvar *ivar)
1215 {
1216 fprintf_filtered (stream, _("%*sInternalvar: $%s\n"), depth, "",
1217 internalvar_name (ivar));
1218 }
1219
1220 void
1221 dump_for_expression (struct ui_file *stream, int depth, symbol *sym)
1222 {
1223 fprintf_filtered (stream, _("%*sSymbol: %s\n"), depth, "",
1224 sym->print_name ());
1225 }
1226
1227 void
1228 dump_for_expression (struct ui_file *stream, int depth, minimal_symbol *msym)
1229 {
1230 fprintf_filtered (stream, _("%*sMinsym: %s\n"), depth, "",
1231 msym->print_name ());
1232 }
1233
1234 void
1235 dump_for_expression (struct ui_file *stream, int depth, const block *bl)
1236 {
1237 fprintf_filtered (stream, _("%*sBlock: %p\n"), depth, "", bl);
1238 }
1239
1240 void
1241 dump_for_expression (struct ui_file *stream, int depth,
1242 type_instance_flags flags)
1243 {
1244 fprintf_filtered (stream, _("%*sType flags: "), depth, "");
1245 if (flags & TYPE_INSTANCE_FLAG_CONST)
1246 fputs_unfiltered ("const ", stream);
1247 if (flags & TYPE_INSTANCE_FLAG_VOLATILE)
1248 fputs_unfiltered ("volatile", stream);
1249 fprintf_filtered (stream, "\n");
1250 }
1251
1252 void
1253 dump_for_expression (struct ui_file *stream, int depth,
1254 enum c_string_type_values flags)
1255 {
1256 fprintf_filtered (stream, _("%*sC string flags: "), depth, "");
1257 switch (flags & ~C_CHAR)
1258 {
1259 case C_WIDE_STRING:
1260 fputs_unfiltered (_("wide "), stream);
1261 break;
1262 case C_STRING_16:
1263 fputs_unfiltered (_("u16 "), stream);
1264 break;
1265 case C_STRING_32:
1266 fputs_unfiltered (_("u32 "), stream);
1267 break;
1268 default:
1269 fputs_unfiltered (_("ordinary "), stream);
1270 break;
1271 }
1272
1273 if ((flags & C_CHAR) != 0)
1274 fputs_unfiltered (_("char"), stream);
1275 else
1276 fputs_unfiltered (_("string"), stream);
1277 fputs_unfiltered ("\n", stream);
1278 }
1279
1280 void
1281 dump_for_expression (struct ui_file *stream, int depth, objfile *objf)
1282 {
1283 fprintf_filtered (stream, _("%*sObjfile: %s\n"), depth, "",
1284 objfile_name (objf));
1285 }
1286
1287 void
1288 dump_for_expression (struct ui_file *stream, int depth,
1289 enum range_flag flags)
1290 {
1291 fprintf_filtered (stream, _("%*sRange:"), depth, "");
1292 if ((flags & RANGE_LOW_BOUND_DEFAULT) != 0)
1293 fputs_unfiltered (_("low-default "), stream);
1294 if ((flags & RANGE_HIGH_BOUND_DEFAULT) != 0)
1295 fputs_unfiltered (_("high-default "), stream);
1296 if ((flags & RANGE_HIGH_BOUND_EXCLUSIVE) != 0)
1297 fputs_unfiltered (_("high-exclusive "), stream);
1298 if ((flags & RANGE_HAS_STRIDE) != 0)
1299 fputs_unfiltered (_("has-stride"), stream);
1300 fprintf_filtered (stream, "\n");
1301 }
1302
1303 void
1304 dump_for_expression (struct ui_file *stream, int depth,
1305 const std::unique_ptr<ada_component> &comp)
1306 {
1307 comp->dump (stream, depth);
1308 }
1309
1310 void
1311 float_const_operation::dump (struct ui_file *stream, int depth) const
1312 {
1313 fprintf_filtered (stream, _("%*sFloat: "), depth, "");
1314 print_floating (m_data.data (), m_type, stream);
1315 fprintf_filtered (stream, "\n");
1316 }
1317
1318 } /* namespace expr */
This page took 0.085862 seconds and 5 git commands to generate.