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