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