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