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