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