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