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