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