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