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