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