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