gdb/
[binutils-gdb.git] / gdb / expprint.c
1 /* Print in infix form a struct expression.
2
3 Copyright (C) 1986, 1988-1989, 1991-2000, 2003, 2007-2012 Free
4 Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "value.h"
26 #include "language.h"
27 #include "parser-defs.h"
28 #include "user-regs.h" /* For user_reg_map_regnum_to_name. */
29 #include "target.h"
30 #include "gdb_string.h"
31 #include "block.h"
32 #include "objfiles.h"
33 #include "gdb_assert.h"
34 #include "valprint.h"
35
36 #ifdef HAVE_CTYPE_H
37 #include <ctype.h>
38 #endif
39
40 void
41 print_expression (struct expression *exp, struct ui_file *stream)
42 {
43 int pc = 0;
44
45 print_subexp (exp, &pc, stream, PREC_NULL);
46 }
47
48 /* Print the subexpression of EXP that starts in position POS, on STREAM.
49 PREC is the precedence of the surrounding operator;
50 if the precedence of the main operator of this subexpression is less,
51 parentheses are needed here. */
52
53 void
54 print_subexp (struct expression *exp, int *pos,
55 struct ui_file *stream, enum precedence prec)
56 {
57 exp->language_defn->la_exp_desc->print_subexp (exp, pos, stream, prec);
58 }
59
60 /* Standard implementation of print_subexp for use in language_defn
61 vectors. */
62 void
63 print_subexp_standard (struct expression *exp, int *pos,
64 struct ui_file *stream, enum precedence prec)
65 {
66 unsigned tem;
67 const struct op_print *op_print_tab;
68 int pc;
69 unsigned nargs;
70 char *op_str;
71 int assign_modify = 0;
72 enum exp_opcode opcode;
73 enum precedence myprec = PREC_NULL;
74 /* Set to 1 for a right-associative operator. */
75 int assoc = 0;
76 struct value *val;
77 char *tempstr = NULL;
78
79 op_print_tab = exp->language_defn->la_op_print_tab;
80 pc = (*pos)++;
81 opcode = exp->elts[pc].opcode;
82 switch (opcode)
83 {
84 /* Common ops */
85
86 case OP_TYPE:
87 (*pos) += 2;
88 type_print (exp->elts[pc + 1].type, "", stream, 0);
89 return;
90
91 case OP_SCOPE:
92 myprec = PREC_PREFIX;
93 assoc = 0;
94 fputs_filtered (type_name_no_tag (exp->elts[pc + 1].type), stream);
95 fputs_filtered ("::", stream);
96 nargs = longest_to_int (exp->elts[pc + 2].longconst);
97 (*pos) += 4 + BYTES_TO_EXP_ELEM (nargs + 1);
98 fputs_filtered (&exp->elts[pc + 3].string, stream);
99 return;
100
101 case OP_LONG:
102 {
103 struct value_print_options opts;
104
105 get_raw_print_options (&opts);
106 (*pos) += 3;
107 value_print (value_from_longest (exp->elts[pc + 1].type,
108 exp->elts[pc + 2].longconst),
109 stream, &opts);
110 }
111 return;
112
113 case OP_DOUBLE:
114 {
115 struct value_print_options opts;
116
117 get_raw_print_options (&opts);
118 (*pos) += 3;
119 value_print (value_from_double (exp->elts[pc + 1].type,
120 exp->elts[pc + 2].doubleconst),
121 stream, &opts);
122 }
123 return;
124
125 case OP_VAR_VALUE:
126 {
127 struct block *b;
128
129 (*pos) += 3;
130 b = exp->elts[pc + 1].block;
131 if (b != NULL
132 && BLOCK_FUNCTION (b) != NULL
133 && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)) != NULL)
134 {
135 fputs_filtered (SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)), stream);
136 fputs_filtered ("::", stream);
137 }
138 fputs_filtered (SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol), stream);
139 }
140 return;
141
142 case OP_VAR_ENTRY_VALUE:
143 {
144 (*pos) += 2;
145 fprintf_filtered (stream, "%s@entry",
146 SYMBOL_PRINT_NAME (exp->elts[pc + 1].symbol));
147 }
148 return;
149
150 case OP_LAST:
151 (*pos) += 2;
152 fprintf_filtered (stream, "$%d",
153 longest_to_int (exp->elts[pc + 1].longconst));
154 return;
155
156 case OP_REGISTER:
157 {
158 const char *name = &exp->elts[pc + 2].string;
159
160 (*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
161 fprintf_filtered (stream, "$%s", name);
162 return;
163 }
164
165 case OP_BOOL:
166 (*pos) += 2;
167 fprintf_filtered (stream, "%s",
168 longest_to_int (exp->elts[pc + 1].longconst)
169 ? "TRUE" : "FALSE");
170 return;
171
172 case OP_INTERNALVAR:
173 (*pos) += 2;
174 fprintf_filtered (stream, "$%s",
175 internalvar_name (exp->elts[pc + 1].internalvar));
176 return;
177
178 case OP_FUNCALL:
179 (*pos) += 2;
180 nargs = longest_to_int (exp->elts[pc + 1].longconst);
181 print_subexp (exp, pos, stream, PREC_SUFFIX);
182 fputs_filtered (" (", stream);
183 for (tem = 0; tem < nargs; tem++)
184 {
185 if (tem != 0)
186 fputs_filtered (", ", stream);
187 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
188 }
189 fputs_filtered (")", stream);
190 return;
191
192 case OP_NAME:
193 nargs = longest_to_int (exp->elts[pc + 1].longconst);
194 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
195 fputs_filtered (&exp->elts[pc + 2].string, stream);
196 return;
197
198 case OP_STRING:
199 {
200 struct value_print_options opts;
201
202 nargs = longest_to_int (exp->elts[pc + 1].longconst);
203 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
204 /* LA_PRINT_STRING will print using the current repeat count threshold.
205 If necessary, we can temporarily set it to zero, or pass it as an
206 additional parameter to LA_PRINT_STRING. -fnf */
207 get_user_print_options (&opts);
208 LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
209 &exp->elts[pc + 2].string, nargs, NULL, 0, &opts);
210 }
211 return;
212
213 case OP_BITSTRING:
214 nargs = longest_to_int (exp->elts[pc + 1].longconst);
215 (*pos)
216 += 3 + BYTES_TO_EXP_ELEM ((nargs + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT);
217 fprintf_unfiltered (stream, "B'<unimplemented>'");
218 return;
219
220 case OP_OBJC_NSSTRING: /* Objective-C Foundation Class
221 NSString constant. */
222 {
223 struct value_print_options opts;
224
225 nargs = longest_to_int (exp->elts[pc + 1].longconst);
226 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
227 fputs_filtered ("@\"", stream);
228 get_user_print_options (&opts);
229 LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
230 &exp->elts[pc + 2].string, nargs, NULL, 0, &opts);
231 fputs_filtered ("\"", stream);
232 }
233 return;
234
235 case OP_OBJC_MSGCALL:
236 { /* Objective C message (method) call. */
237 char *selector;
238
239 (*pos) += 3;
240 nargs = longest_to_int (exp->elts[pc + 2].longconst);
241 fprintf_unfiltered (stream, "[");
242 print_subexp (exp, pos, stream, PREC_SUFFIX);
243 if (0 == target_read_string (exp->elts[pc + 1].longconst,
244 &selector, 1024, NULL))
245 {
246 error (_("bad selector"));
247 return;
248 }
249 if (nargs)
250 {
251 char *s, *nextS;
252
253 s = alloca (strlen (selector) + 1);
254 strcpy (s, selector);
255 for (tem = 0; tem < nargs; tem++)
256 {
257 nextS = strchr (s, ':');
258 gdb_assert (nextS); /* Make sure we found ':'. */
259 *nextS = '\0';
260 fprintf_unfiltered (stream, " %s: ", s);
261 s = nextS + 1;
262 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
263 }
264 }
265 else
266 {
267 fprintf_unfiltered (stream, " %s", selector);
268 }
269 fprintf_unfiltered (stream, "]");
270 /* "selector" was malloc'd by target_read_string. Free it. */
271 xfree (selector);
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 = 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 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 OP_LABELED:
339 tem = longest_to_int (exp->elts[pc + 1].longconst);
340 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
341 /* Gcc support both these syntaxes. Unsure which is preferred. */
342 #if 1
343 fputs_filtered (&exp->elts[pc + 2].string, stream);
344 fputs_filtered (": ", stream);
345 #else
346 fputs_filtered (".", stream);
347 fputs_filtered (&exp->elts[pc + 2].string, stream);
348 fputs_filtered ("=", stream);
349 #endif
350 print_subexp (exp, pos, stream, PREC_SUFFIX);
351 return;
352
353 case TERNOP_COND:
354 if ((int) prec > (int) PREC_COMMA)
355 fputs_filtered ("(", stream);
356 /* Print the subexpressions, forcing parentheses
357 around any binary operations within them.
358 This is more parentheses than are strictly necessary,
359 but it looks clearer. */
360 print_subexp (exp, pos, stream, PREC_HYPER);
361 fputs_filtered (" ? ", stream);
362 print_subexp (exp, pos, stream, PREC_HYPER);
363 fputs_filtered (" : ", stream);
364 print_subexp (exp, pos, stream, PREC_HYPER);
365 if ((int) prec > (int) PREC_COMMA)
366 fputs_filtered (")", stream);
367 return;
368
369 case TERNOP_SLICE:
370 case TERNOP_SLICE_COUNT:
371 print_subexp (exp, pos, stream, PREC_SUFFIX);
372 fputs_filtered ("(", stream);
373 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
374 fputs_filtered (opcode == TERNOP_SLICE ? " : " : " UP ", stream);
375 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
376 fputs_filtered (")", stream);
377 return;
378
379 case STRUCTOP_STRUCT:
380 tem = longest_to_int (exp->elts[pc + 1].longconst);
381 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
382 print_subexp (exp, pos, stream, PREC_SUFFIX);
383 fputs_filtered (".", stream);
384 fputs_filtered (&exp->elts[pc + 2].string, stream);
385 return;
386
387 /* Will not occur for Modula-2. */
388 case STRUCTOP_PTR:
389 tem = longest_to_int (exp->elts[pc + 1].longconst);
390 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
391 print_subexp (exp, pos, stream, PREC_SUFFIX);
392 fputs_filtered ("->", stream);
393 fputs_filtered (&exp->elts[pc + 2].string, stream);
394 return;
395
396 case STRUCTOP_MEMBER:
397 print_subexp (exp, pos, stream, PREC_SUFFIX);
398 fputs_filtered (".*", stream);
399 print_subexp (exp, pos, stream, PREC_SUFFIX);
400 return;
401
402 case STRUCTOP_MPTR:
403 print_subexp (exp, pos, stream, PREC_SUFFIX);
404 fputs_filtered ("->*", stream);
405 print_subexp (exp, pos, stream, PREC_SUFFIX);
406 return;
407
408 case BINOP_SUBSCRIPT:
409 print_subexp (exp, pos, stream, PREC_SUFFIX);
410 fputs_filtered ("[", stream);
411 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
412 fputs_filtered ("]", stream);
413 return;
414
415 case UNOP_POSTINCREMENT:
416 print_subexp (exp, pos, stream, PREC_SUFFIX);
417 fputs_filtered ("++", stream);
418 return;
419
420 case UNOP_POSTDECREMENT:
421 print_subexp (exp, pos, stream, PREC_SUFFIX);
422 fputs_filtered ("--", stream);
423 return;
424
425 case UNOP_CAST:
426 (*pos) += 2;
427 if ((int) prec > (int) PREC_PREFIX)
428 fputs_filtered ("(", stream);
429 fputs_filtered ("(", stream);
430 type_print (exp->elts[pc + 1].type, "", stream, 0);
431 fputs_filtered (") ", stream);
432 print_subexp (exp, pos, stream, PREC_PREFIX);
433 if ((int) prec > (int) PREC_PREFIX)
434 fputs_filtered (")", stream);
435 return;
436
437 case UNOP_CAST_TYPE:
438 if ((int) prec > (int) PREC_PREFIX)
439 fputs_filtered ("(", 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 if ((int) prec > (int) PREC_PREFIX)
445 fputs_filtered (")", stream);
446 return;
447
448 case UNOP_DYNAMIC_CAST:
449 case UNOP_REINTERPRET_CAST:
450 fputs_filtered (opcode == UNOP_DYNAMIC_CAST ? "dynamic_cast"
451 : "reinterpret_cast", stream);
452 fputs_filtered ("<", stream);
453 print_subexp (exp, pos, stream, PREC_PREFIX);
454 fputs_filtered ("> (", stream);
455 print_subexp (exp, pos, stream, PREC_PREFIX);
456 fputs_filtered (")", stream);
457 return;
458
459 case UNOP_MEMVAL:
460 (*pos) += 2;
461 if ((int) prec > (int) PREC_PREFIX)
462 fputs_filtered ("(", stream);
463 if (TYPE_CODE (exp->elts[pc + 1].type) == TYPE_CODE_FUNC
464 && exp->elts[pc + 3].opcode == OP_LONG)
465 {
466 struct value_print_options opts;
467
468 /* We have a minimal symbol fn, probably. It's encoded
469 as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
470 Swallow the OP_LONG (including both its opcodes); ignore
471 its type; print the value in the type of the MEMVAL. */
472 (*pos) += 4;
473 val = value_at_lazy (exp->elts[pc + 1].type,
474 (CORE_ADDR) exp->elts[pc + 5].longconst);
475 get_raw_print_options (&opts);
476 value_print (val, stream, &opts);
477 }
478 else
479 {
480 fputs_filtered ("{", stream);
481 type_print (exp->elts[pc + 1].type, "", stream, 0);
482 fputs_filtered ("} ", stream);
483 print_subexp (exp, pos, stream, PREC_PREFIX);
484 }
485 if ((int) prec > (int) PREC_PREFIX)
486 fputs_filtered (")", stream);
487 return;
488
489 case UNOP_MEMVAL_TYPE:
490 if ((int) prec > (int) PREC_PREFIX)
491 fputs_filtered ("(", stream);
492 fputs_filtered ("{", stream);
493 print_subexp (exp, pos, stream, PREC_PREFIX);
494 fputs_filtered ("} ", stream);
495 print_subexp (exp, pos, stream, PREC_PREFIX);
496 if ((int) prec > (int) PREC_PREFIX)
497 fputs_filtered (")", stream);
498 return;
499
500 case UNOP_MEMVAL_TLS:
501 (*pos) += 3;
502 if ((int) prec > (int) PREC_PREFIX)
503 fputs_filtered ("(", stream);
504 fputs_filtered ("{", stream);
505 type_print (exp->elts[pc + 2].type, "", stream, 0);
506 fputs_filtered ("} ", stream);
507 print_subexp (exp, pos, stream, PREC_PREFIX);
508 if ((int) prec > (int) PREC_PREFIX)
509 fputs_filtered (")", stream);
510 return;
511
512 case BINOP_ASSIGN_MODIFY:
513 opcode = exp->elts[pc + 1].opcode;
514 (*pos) += 2;
515 myprec = PREC_ASSIGN;
516 assoc = 1;
517 assign_modify = 1;
518 op_str = "???";
519 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
520 if (op_print_tab[tem].opcode == opcode)
521 {
522 op_str = op_print_tab[tem].string;
523 break;
524 }
525 if (op_print_tab[tem].opcode != opcode)
526 /* Not found; don't try to keep going because we don't know how
527 to interpret further elements. */
528 error (_("Invalid expression"));
529 break;
530
531 /* C++ ops */
532
533 case OP_THIS:
534 ++(*pos);
535 if (exp->language_defn->la_name_of_this)
536 fputs_filtered (exp->language_defn->la_name_of_this, stream);
537 else
538 fprintf_filtered (stream, _("<language %s has no 'this'>"),
539 exp->language_defn->la_name);
540 return;
541
542 /* Modula-2 ops */
543
544 case MULTI_SUBSCRIPT:
545 (*pos) += 2;
546 nargs = longest_to_int (exp->elts[pc + 1].longconst);
547 print_subexp (exp, pos, stream, PREC_SUFFIX);
548 fprintf_unfiltered (stream, " [");
549 for (tem = 0; tem < nargs; tem++)
550 {
551 if (tem != 0)
552 fprintf_unfiltered (stream, ", ");
553 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
554 }
555 fprintf_unfiltered (stream, "]");
556 return;
557
558 case BINOP_VAL:
559 (*pos) += 2;
560 fprintf_unfiltered (stream, "VAL(");
561 type_print (exp->elts[pc + 1].type, "", stream, 0);
562 fprintf_unfiltered (stream, ",");
563 print_subexp (exp, pos, stream, PREC_PREFIX);
564 fprintf_unfiltered (stream, ")");
565 return;
566
567 case TYPE_INSTANCE:
568 {
569 LONGEST count = exp->elts[pc + 1].longconst;
570
571 /* The COUNT. */
572 (*pos)++;
573 fputs_unfiltered ("TypesInstance(", stream);
574 while (count-- > 0)
575 {
576 type_print (exp->elts[(*pos)++].type, "", stream, 0);
577 if (count > 0)
578 fputs_unfiltered (",", stream);
579 }
580 fputs_unfiltered (",", stream);
581 /* Ending COUNT and ending TYPE_INSTANCE. */
582 (*pos) += 2;
583 print_subexp (exp, pos, stream, PREC_PREFIX);
584 fputs_unfiltered (")", stream);
585 return;
586 }
587
588 /* Default ops */
589
590 default:
591 op_str = "???";
592 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
593 if (op_print_tab[tem].opcode == opcode)
594 {
595 op_str = op_print_tab[tem].string;
596 myprec = op_print_tab[tem].precedence;
597 assoc = op_print_tab[tem].right_assoc;
598 break;
599 }
600 if (op_print_tab[tem].opcode != opcode)
601 /* Not found; don't try to keep going because we don't know how
602 to interpret further elements. For example, this happens
603 if opcode is OP_TYPE. */
604 error (_("Invalid expression"));
605 }
606
607 /* Note that PREC_BUILTIN will always emit parentheses. */
608 if ((int) myprec < (int) prec)
609 fputs_filtered ("(", stream);
610 if ((int) opcode > (int) BINOP_END)
611 {
612 if (assoc)
613 {
614 /* Unary postfix operator. */
615 print_subexp (exp, pos, stream, PREC_SUFFIX);
616 fputs_filtered (op_str, stream);
617 }
618 else
619 {
620 /* Unary prefix operator. */
621 fputs_filtered (op_str, stream);
622 if (myprec == PREC_BUILTIN_FUNCTION)
623 fputs_filtered ("(", stream);
624 print_subexp (exp, pos, stream, PREC_PREFIX);
625 if (myprec == PREC_BUILTIN_FUNCTION)
626 fputs_filtered (")", stream);
627 }
628 }
629 else
630 {
631 /* Binary operator. */
632 /* Print left operand.
633 If operator is right-associative,
634 increment precedence for this operand. */
635 print_subexp (exp, pos, stream,
636 (enum precedence) ((int) myprec + assoc));
637 /* Print the operator itself. */
638 if (assign_modify)
639 fprintf_filtered (stream, " %s= ", op_str);
640 else if (op_str[0] == ',')
641 fprintf_filtered (stream, "%s ", op_str);
642 else
643 fprintf_filtered (stream, " %s ", op_str);
644 /* Print right operand.
645 If operator is left-associative,
646 increment precedence for this operand. */
647 print_subexp (exp, pos, stream,
648 (enum precedence) ((int) myprec + !assoc));
649 }
650
651 if ((int) myprec < (int) prec)
652 fputs_filtered (")", stream);
653 }
654
655 /* Return the operator corresponding to opcode OP as
656 a string. NULL indicates that the opcode was not found in the
657 current language table. */
658 char *
659 op_string (enum exp_opcode op)
660 {
661 int tem;
662 const struct op_print *op_print_tab;
663
664 op_print_tab = current_language->la_op_print_tab;
665 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
666 if (op_print_tab[tem].opcode == op)
667 return op_print_tab[tem].string;
668 return NULL;
669 }
670
671 /* Support for dumping the raw data from expressions in a human readable
672 form. */
673
674 static int dump_subexp_body (struct expression *exp, struct ui_file *, int);
675
676 /* Name for OPCODE, when it appears in expression EXP. */
677
678 char *
679 op_name (struct expression *exp, enum exp_opcode opcode)
680 {
681 return exp->language_defn->la_exp_desc->op_name (opcode);
682 }
683
684 /* Default name for the standard operator OPCODE (i.e., one defined in
685 the definition of enum exp_opcode). */
686
687 char *
688 op_name_standard (enum exp_opcode opcode)
689 {
690 switch (opcode)
691 {
692 default:
693 {
694 static char buf[30];
695
696 sprintf (buf, "<unknown %d>", opcode);
697 return buf;
698 }
699 #define OP(name) \
700 case name: \
701 return #name ;
702 #include "std-operator.def"
703 #undef OP
704 }
705 }
706
707 /* Print a raw dump of expression EXP to STREAM.
708 NOTE, if non-NULL, is printed as extra explanatory text. */
709
710 void
711 dump_raw_expression (struct expression *exp, struct ui_file *stream,
712 char *note)
713 {
714 int elt;
715 char *opcode_name;
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 opcode_name = op_name (exp, exp->elts[elt].opcode);
732
733 fprintf_filtered (stream, "%20s ", opcode_name);
734 print_longest (stream, 'd', 0, exp->elts[elt].longconst);
735 fprintf_filtered (stream, " ");
736
737 for (eltscan = (char *) &exp->elts[elt],
738 eltsize = sizeof (union exp_element);
739 eltsize-- > 0;
740 eltscan++)
741 {
742 fprintf_filtered (stream, "%c",
743 isprint (*eltscan) ? (*eltscan & 0xFF) : '.');
744 }
745 fprintf_filtered (stream, "\n");
746 }
747 }
748
749 /* Dump the subexpression of prefix expression EXP whose operator is at
750 position ELT onto STREAM. Returns the position of the next
751 subexpression in EXP. */
752
753 int
754 dump_subexp (struct expression *exp, struct ui_file *stream, int elt)
755 {
756 static int indent = 0;
757 int i;
758
759 fprintf_filtered (stream, "\n");
760 fprintf_filtered (stream, "\t%5d ", elt);
761
762 for (i = 1; i <= indent; i++)
763 fprintf_filtered (stream, " ");
764 indent += 2;
765
766 fprintf_filtered (stream, "%-20s ", op_name (exp, exp->elts[elt].opcode));
767
768 elt = dump_subexp_body (exp, stream, elt);
769
770 indent -= 2;
771
772 return elt;
773 }
774
775 /* Dump the operands of prefix expression EXP whose opcode is at
776 position ELT onto STREAM. Returns the position of the next
777 subexpression in EXP. */
778
779 static int
780 dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
781 {
782 return exp->language_defn->la_exp_desc->dump_subexp_body (exp, stream, elt);
783 }
784
785 /* Default value for subexp_body in exp_descriptor vector. */
786
787 int
788 dump_subexp_body_standard (struct expression *exp,
789 struct ui_file *stream, int elt)
790 {
791 int opcode = exp->elts[elt++].opcode;
792
793 switch (opcode)
794 {
795 case TERNOP_COND:
796 case TERNOP_SLICE:
797 case TERNOP_SLICE_COUNT:
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_IN:
831 case BINOP_RANGE:
832 case BINOP_END:
833 case STRUCTOP_MEMBER:
834 case STRUCTOP_MPTR:
835 elt = dump_subexp (exp, stream, elt);
836 /* FALL THROUGH */
837 case UNOP_NEG:
838 case UNOP_LOGICAL_NOT:
839 case UNOP_COMPLEMENT:
840 case UNOP_IND:
841 case UNOP_ADDR:
842 case UNOP_PREINCREMENT:
843 case UNOP_POSTINCREMENT:
844 case UNOP_PREDECREMENT:
845 case UNOP_POSTDECREMENT:
846 case UNOP_SIZEOF:
847 case UNOP_PLUS:
848 case UNOP_CAP:
849 case UNOP_CHR:
850 case UNOP_ORD:
851 case UNOP_ABS:
852 case UNOP_FLOAT:
853 case UNOP_HIGH:
854 case UNOP_MAX:
855 case UNOP_MIN:
856 case UNOP_ODD:
857 case UNOP_TRUNC:
858 elt = dump_subexp (exp, stream, elt);
859 break;
860 case OP_LONG:
861 fprintf_filtered (stream, "Type @");
862 gdb_print_host_address (exp->elts[elt].type, stream);
863 fprintf_filtered (stream, " (");
864 type_print (exp->elts[elt].type, NULL, stream, 0);
865 fprintf_filtered (stream, "), value %ld (0x%lx)",
866 (long) exp->elts[elt + 1].longconst,
867 (long) exp->elts[elt + 1].longconst);
868 elt += 3;
869 break;
870 case OP_DOUBLE:
871 fprintf_filtered (stream, "Type @");
872 gdb_print_host_address (exp->elts[elt].type, stream);
873 fprintf_filtered (stream, " (");
874 type_print (exp->elts[elt].type, NULL, stream, 0);
875 fprintf_filtered (stream, "), value %g",
876 (double) exp->elts[elt + 1].doubleconst);
877 elt += 3;
878 break;
879 case OP_VAR_VALUE:
880 fprintf_filtered (stream, "Block @");
881 gdb_print_host_address (exp->elts[elt].block, stream);
882 fprintf_filtered (stream, ", symbol @");
883 gdb_print_host_address (exp->elts[elt + 1].symbol, stream);
884 fprintf_filtered (stream, " (%s)",
885 SYMBOL_PRINT_NAME (exp->elts[elt + 1].symbol));
886 elt += 3;
887 break;
888 case OP_VAR_ENTRY_VALUE:
889 fprintf_filtered (stream, "Entry value of symbol @");
890 gdb_print_host_address (exp->elts[elt].symbol, stream);
891 fprintf_filtered (stream, " (%s)",
892 SYMBOL_PRINT_NAME (exp->elts[elt].symbol));
893 elt += 2;
894 break;
895 case OP_LAST:
896 fprintf_filtered (stream, "History element %ld",
897 (long) exp->elts[elt].longconst);
898 elt += 2;
899 break;
900 case OP_REGISTER:
901 fprintf_filtered (stream, "Register $%s", &exp->elts[elt + 1].string);
902 elt += 3 + BYTES_TO_EXP_ELEM (exp->elts[elt].longconst + 1);
903 break;
904 case OP_INTERNALVAR:
905 fprintf_filtered (stream, "Internal var @");
906 gdb_print_host_address (exp->elts[elt].internalvar, stream);
907 fprintf_filtered (stream, " (%s)",
908 internalvar_name (exp->elts[elt].internalvar));
909 elt += 2;
910 break;
911 case OP_FUNCALL:
912 {
913 int i, nargs;
914
915 nargs = longest_to_int (exp->elts[elt].longconst);
916
917 fprintf_filtered (stream, "Number of args: %d", nargs);
918 elt += 2;
919
920 for (i = 1; i <= nargs + 1; i++)
921 elt = dump_subexp (exp, stream, elt);
922 }
923 break;
924 case OP_ARRAY:
925 {
926 int lower, upper;
927 int i;
928
929 lower = longest_to_int (exp->elts[elt].longconst);
930 upper = longest_to_int (exp->elts[elt + 1].longconst);
931
932 fprintf_filtered (stream, "Bounds [%d:%d]", lower, upper);
933 elt += 3;
934
935 for (i = 1; i <= upper - lower + 1; i++)
936 elt = dump_subexp (exp, stream, elt);
937 }
938 break;
939 case UNOP_DYNAMIC_CAST:
940 case UNOP_REINTERPRET_CAST:
941 case UNOP_CAST_TYPE:
942 case UNOP_MEMVAL_TYPE:
943 fprintf_filtered (stream, " (");
944 elt = dump_subexp (exp, stream, elt);
945 fprintf_filtered (stream, ")");
946 elt = dump_subexp (exp, stream, elt);
947 break;
948 case UNOP_MEMVAL:
949 case UNOP_CAST:
950 fprintf_filtered (stream, "Type @");
951 gdb_print_host_address (exp->elts[elt].type, stream);
952 fprintf_filtered (stream, " (");
953 type_print (exp->elts[elt].type, NULL, stream, 0);
954 fprintf_filtered (stream, ")");
955 elt = dump_subexp (exp, stream, elt + 2);
956 break;
957 case UNOP_MEMVAL_TLS:
958 fprintf_filtered (stream, "TLS type @");
959 gdb_print_host_address (exp->elts[elt + 1].type, stream);
960 fprintf_filtered (stream, " (__thread /* \"%s\" */ ",
961 (exp->elts[elt].objfile == NULL ? "(null)"
962 : exp->elts[elt].objfile->name));
963 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
964 fprintf_filtered (stream, ")");
965 elt = dump_subexp (exp, stream, elt + 3);
966 break;
967 case OP_TYPE:
968 fprintf_filtered (stream, "Type @");
969 gdb_print_host_address (exp->elts[elt].type, stream);
970 fprintf_filtered (stream, " (");
971 type_print (exp->elts[elt].type, NULL, stream, 0);
972 fprintf_filtered (stream, ")");
973 elt += 2;
974 break;
975 case OP_TYPEOF:
976 case OP_DECLTYPE:
977 fprintf_filtered (stream, "Typeof (");
978 elt = dump_subexp (exp, stream, elt);
979 fprintf_filtered (stream, ")");
980 break;
981 case STRUCTOP_STRUCT:
982 case STRUCTOP_PTR:
983 {
984 char *elem_name;
985 int len;
986
987 len = longest_to_int (exp->elts[elt].longconst);
988 elem_name = &exp->elts[elt + 1].string;
989
990 fprintf_filtered (stream, "Element name: `%.*s'", len, elem_name);
991 elt = dump_subexp (exp, stream, elt + 3 + BYTES_TO_EXP_ELEM (len + 1));
992 }
993 break;
994 case OP_SCOPE:
995 {
996 char *elem_name;
997 int len;
998
999 fprintf_filtered (stream, "Type @");
1000 gdb_print_host_address (exp->elts[elt].type, stream);
1001 fprintf_filtered (stream, " (");
1002 type_print (exp->elts[elt].type, NULL, stream, 0);
1003 fprintf_filtered (stream, ") ");
1004
1005 len = longest_to_int (exp->elts[elt + 1].longconst);
1006 elem_name = &exp->elts[elt + 2].string;
1007
1008 fprintf_filtered (stream, "Field name: `%.*s'", len, elem_name);
1009 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1010 }
1011 break;
1012 case TYPE_INSTANCE:
1013 {
1014 LONGEST len;
1015
1016 len = exp->elts[elt++].longconst;
1017 fprintf_filtered (stream, "%s TypeInstance: ", plongest (len));
1018 while (len-- > 0)
1019 {
1020 fprintf_filtered (stream, "Type @");
1021 gdb_print_host_address (exp->elts[elt].type, stream);
1022 fprintf_filtered (stream, " (");
1023 type_print (exp->elts[elt].type, NULL, stream, 0);
1024 fprintf_filtered (stream, ")");
1025 elt++;
1026 if (len > 0)
1027 fputs_filtered (", ", stream);
1028 }
1029 /* Ending LEN and ending TYPE_INSTANCE. */
1030 elt += 2;
1031 elt = dump_subexp (exp, stream, elt);
1032 }
1033 break;
1034 default:
1035 case OP_NULL:
1036 case MULTI_SUBSCRIPT:
1037 case OP_F77_UNDETERMINED_ARGLIST:
1038 case OP_COMPLEX:
1039 case OP_STRING:
1040 case OP_BITSTRING:
1041 case OP_BOOL:
1042 case OP_M2_STRING:
1043 case OP_THIS:
1044 case OP_LABELED:
1045 case OP_NAME:
1046 fprintf_filtered (stream, "Unknown format");
1047 }
1048
1049 return elt;
1050 }
1051
1052 void
1053 dump_prefix_expression (struct expression *exp, struct ui_file *stream)
1054 {
1055 int elt;
1056
1057 fprintf_filtered (stream, "Dump of expression @ ");
1058 gdb_print_host_address (exp, stream);
1059 fputs_filtered (", after conversion to prefix form:\nExpression: `", stream);
1060 print_expression (exp, stream);
1061 fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
1062 exp->language_defn->la_name, exp->nelts,
1063 (long) sizeof (union exp_element));
1064 fputs_filtered ("\n", stream);
1065
1066 for (elt = 0; elt < exp->nelts;)
1067 elt = dump_subexp (exp, stream, elt);
1068 fputs_filtered ("\n", stream);
1069 }