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