Eliminate UNOP_MEMVAL_TLS
[deliverable/binutils-gdb.git] / gdb / expprint.c
CommitLineData
c906108c 1/* Print in infix form a struct expression.
1bac305b 2
61baf725 3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
21#include "symtab.h"
22#include "gdbtypes.h"
23#include "expression.h"
24#include "value.h"
25#include "language.h"
26#include "parser-defs.h"
eb8bc282 27#include "user-regs.h" /* For user_reg_map_regnum_to_name. */
82eeeb94 28#include "target.h"
fe898f56 29#include "block.h"
9e35dae4 30#include "objfiles.h"
79a45b7d 31#include "valprint.h"
c906108c 32
c906108c 33#include <ctype.h>
c906108c 34
c906108c 35void
fba45db2 36print_expression (struct expression *exp, struct ui_file *stream)
c906108c
SS
37{
38 int pc = 0;
d7f9d729 39
c906108c
SS
40 print_subexp (exp, &pc, stream, PREC_NULL);
41}
42
43/* Print the subexpression of EXP that starts in position POS, on STREAM.
44 PREC is the precedence of the surrounding operator;
45 if the precedence of the main operator of this subexpression is less,
46 parentheses are needed here. */
47
5f9769d1 48void
f86f5ca3 49print_subexp (struct expression *exp, int *pos,
fba45db2 50 struct ui_file *stream, enum precedence prec)
5f9769d1
PH
51{
52 exp->language_defn->la_exp_desc->print_subexp (exp, pos, stream, prec);
53}
54
55/* Standard implementation of print_subexp for use in language_defn
56 vectors. */
57void
58print_subexp_standard (struct expression *exp, int *pos,
59 struct ui_file *stream, enum precedence prec)
c906108c 60{
f86f5ca3
PH
61 unsigned tem;
62 const struct op_print *op_print_tab;
63 int pc;
c906108c 64 unsigned nargs;
a121b7c1 65 const char *op_str;
c906108c
SS
66 int assign_modify = 0;
67 enum exp_opcode opcode;
68 enum precedence myprec = PREC_NULL;
69 /* Set to 1 for a right-associative operator. */
70 int assoc = 0;
3d6d86c6 71 struct value *val;
c906108c
SS
72 char *tempstr = NULL;
73
74 op_print_tab = exp->language_defn->la_op_print_tab;
75 pc = (*pos)++;
76 opcode = exp->elts[pc].opcode;
77 switch (opcode)
78 {
c5aa993b 79 /* Common ops */
c906108c 80
4f485ebc
DE
81 case OP_TYPE:
82 (*pos) += 2;
83 type_print (exp->elts[pc + 1].type, "", stream, 0);
84 return;
85
c906108c
SS
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
2a998fc0 100 get_no_prettyformat_print_options (&opts);
79a45b7d
TT
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
2a998fc0 112 get_no_prettyformat_print_options (&opts);
79a45b7d
TT
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 {
270140bd 122 const 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
74ea4be4
PA
137 case OP_VAR_MSYM_VALUE:
138 {
139 (*pos) += 3;
140 fputs_filtered (MSYMBOL_PRINT_NAME (exp->elts[pc + 2].msymbol), stream);
141 }
142 return;
143
36b11add
JK
144 case OP_VAR_ENTRY_VALUE:
145 {
36b11add
JK
146 (*pos) += 2;
147 fprintf_filtered (stream, "%s@entry",
148 SYMBOL_PRINT_NAME (exp->elts[pc + 1].symbol));
149 }
150 return;
151
c906108c
SS
152 case OP_LAST:
153 (*pos) += 2;
154 fprintf_filtered (stream, "$%d",
155 longest_to_int (exp->elts[pc + 1].longconst));
156 return;
157
158 case OP_REGISTER:
e36180d7 159 {
67f3407f 160 const char *name = &exp->elts[pc + 2].string;
d7f9d729 161
67f3407f 162 (*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
eb8bc282 163 fprintf_filtered (stream, "$%s", name);
e36180d7
AC
164 return;
165 }
c906108c
SS
166
167 case OP_BOOL:
168 (*pos) += 2;
169 fprintf_filtered (stream, "%s",
170 longest_to_int (exp->elts[pc + 1].longconst)
171 ? "TRUE" : "FALSE");
172 return;
173
174 case OP_INTERNALVAR:
175 (*pos) += 2;
176 fprintf_filtered (stream, "$%s",
c5aa993b 177 internalvar_name (exp->elts[pc + 1].internalvar));
c906108c
SS
178 return;
179
180 case OP_FUNCALL:
181 (*pos) += 2;
182 nargs = longest_to_int (exp->elts[pc + 1].longconst);
183 print_subexp (exp, pos, stream, PREC_SUFFIX);
184 fputs_filtered (" (", stream);
185 for (tem = 0; tem < nargs; tem++)
186 {
187 if (tem != 0)
188 fputs_filtered (", ", stream);
189 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
190 }
191 fputs_filtered (")", stream);
192 return;
193
194 case OP_NAME:
c5aa993b 195 nargs = longest_to_int (exp->elts[pc + 1].longconst);
c906108c
SS
196 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
197 fputs_filtered (&exp->elts[pc + 2].string, stream);
198 return;
199
200 case OP_STRING:
79a45b7d
TT
201 {
202 struct value_print_options opts;
d7f9d729 203
79a45b7d
TT
204 nargs = longest_to_int (exp->elts[pc + 1].longconst);
205 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
206 /* LA_PRINT_STRING will print using the current repeat count threshold.
207 If necessary, we can temporarily set it to zero, or pass it as an
208 additional parameter to LA_PRINT_STRING. -fnf */
209 get_user_print_options (&opts);
6c7a06a3 210 LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
ac91cd70
PA
211 (gdb_byte *) &exp->elts[pc + 2].string, nargs,
212 NULL, 0, &opts);
79a45b7d 213 }
c906108c
SS
214 return;
215
3e43a32a
MS
216 case OP_OBJC_NSSTRING: /* Objective-C Foundation Class
217 NSString constant. */
79a45b7d
TT
218 {
219 struct value_print_options opts;
d7f9d729 220
79a45b7d
TT
221 nargs = longest_to_int (exp->elts[pc + 1].longconst);
222 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
223 fputs_filtered ("@\"", stream);
224 get_user_print_options (&opts);
6c7a06a3 225 LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
ac91cd70
PA
226 (gdb_byte *) &exp->elts[pc + 2].string, nargs,
227 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
224c3ddb 250 s = (char *) alloca (strlen (selector) + 1);
82eeeb94
AF
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. */
224c3ddb 290 tempstr = (char *) alloca (nargs);
c906108c
SS
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,
ac91cd70 317 (gdb_byte *) 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
c906108c
SS
335 case TERNOP_COND:
336 if ((int) prec > (int) PREC_COMMA)
337 fputs_filtered ("(", stream);
338 /* Print the subexpressions, forcing parentheses
c5aa993b
JM
339 around any binary operations within them.
340 This is more parentheses than are strictly necessary,
341 but it looks clearer. */
c906108c
SS
342 print_subexp (exp, pos, stream, PREC_HYPER);
343 fputs_filtered (" ? ", stream);
344 print_subexp (exp, pos, stream, PREC_HYPER);
345 fputs_filtered (" : ", stream);
346 print_subexp (exp, pos, stream, PREC_HYPER);
347 if ((int) prec > (int) PREC_COMMA)
348 fputs_filtered (")", stream);
349 return;
350
351 case TERNOP_SLICE:
c906108c
SS
352 print_subexp (exp, pos, stream, PREC_SUFFIX);
353 fputs_filtered ("(", stream);
354 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
355 fputs_filtered (opcode == TERNOP_SLICE ? " : " : " UP ", stream);
356 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
357 fputs_filtered (")", stream);
358 return;
359
360 case STRUCTOP_STRUCT:
361 tem = longest_to_int (exp->elts[pc + 1].longconst);
362 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
363 print_subexp (exp, pos, stream, PREC_SUFFIX);
364 fputs_filtered (".", stream);
365 fputs_filtered (&exp->elts[pc + 2].string, stream);
366 return;
367
0963b4bd 368 /* Will not occur for Modula-2. */
c906108c
SS
369 case STRUCTOP_PTR:
370 tem = longest_to_int (exp->elts[pc + 1].longconst);
371 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
372 print_subexp (exp, pos, stream, PREC_SUFFIX);
373 fputs_filtered ("->", stream);
374 fputs_filtered (&exp->elts[pc + 2].string, stream);
375 return;
376
0534816d
DJ
377 case STRUCTOP_MEMBER:
378 print_subexp (exp, pos, stream, PREC_SUFFIX);
379 fputs_filtered (".*", stream);
380 print_subexp (exp, pos, stream, PREC_SUFFIX);
381 return;
382
383 case STRUCTOP_MPTR:
384 print_subexp (exp, pos, stream, PREC_SUFFIX);
385 fputs_filtered ("->*", stream);
386 print_subexp (exp, pos, stream, PREC_SUFFIX);
387 return;
388
c906108c
SS
389 case BINOP_SUBSCRIPT:
390 print_subexp (exp, pos, stream, PREC_SUFFIX);
391 fputs_filtered ("[", stream);
392 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
393 fputs_filtered ("]", stream);
394 return;
395
396 case UNOP_POSTINCREMENT:
397 print_subexp (exp, pos, stream, PREC_SUFFIX);
398 fputs_filtered ("++", stream);
399 return;
400
401 case UNOP_POSTDECREMENT:
402 print_subexp (exp, pos, stream, PREC_SUFFIX);
403 fputs_filtered ("--", stream);
404 return;
405
406 case UNOP_CAST:
407 (*pos) += 2;
408 if ((int) prec > (int) PREC_PREFIX)
c5aa993b 409 fputs_filtered ("(", stream);
c906108c
SS
410 fputs_filtered ("(", stream);
411 type_print (exp->elts[pc + 1].type, "", stream, 0);
412 fputs_filtered (") ", stream);
413 print_subexp (exp, pos, stream, PREC_PREFIX);
414 if ((int) prec > (int) PREC_PREFIX)
c5aa993b 415 fputs_filtered (")", stream);
c906108c
SS
416 return;
417
9eaf6705 418 case UNOP_CAST_TYPE:
9eaf6705
TT
419 if ((int) prec > (int) PREC_PREFIX)
420 fputs_filtered ("(", stream);
421 fputs_filtered ("(", stream);
422 print_subexp (exp, pos, stream, PREC_PREFIX);
423 fputs_filtered (") ", stream);
424 print_subexp (exp, pos, stream, PREC_PREFIX);
425 if ((int) prec > (int) PREC_PREFIX)
426 fputs_filtered (")", stream);
427 return;
428
4e8f195d
TT
429 case UNOP_DYNAMIC_CAST:
430 case UNOP_REINTERPRET_CAST:
431 fputs_filtered (opcode == UNOP_DYNAMIC_CAST ? "dynamic_cast"
432 : "reinterpret_cast", stream);
433 fputs_filtered ("<", stream);
9eaf6705 434 print_subexp (exp, pos, stream, PREC_PREFIX);
4e8f195d
TT
435 fputs_filtered ("> (", stream);
436 print_subexp (exp, pos, stream, PREC_PREFIX);
437 fputs_filtered (")", stream);
438 return;
439
c906108c
SS
440 case UNOP_MEMVAL:
441 (*pos) += 2;
442 if ((int) prec > (int) PREC_PREFIX)
c5aa993b 443 fputs_filtered ("(", stream);
905e0470
PM
444 if (TYPE_CODE (exp->elts[pc + 1].type) == TYPE_CODE_FUNC
445 && exp->elts[pc + 3].opcode == OP_LONG)
c5aa993b 446 {
79a45b7d
TT
447 struct value_print_options opts;
448
c5aa993b
JM
449 /* We have a minimal symbol fn, probably. It's encoded
450 as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
451 Swallow the OP_LONG (including both its opcodes); ignore
452 its type; print the value in the type of the MEMVAL. */
453 (*pos) += 4;
454 val = value_at_lazy (exp->elts[pc + 1].type,
00a4c844 455 (CORE_ADDR) exp->elts[pc + 5].longconst);
2a998fc0 456 get_no_prettyformat_print_options (&opts);
79a45b7d 457 value_print (val, stream, &opts);
c5aa993b
JM
458 }
459 else
460 {
461 fputs_filtered ("{", stream);
462 type_print (exp->elts[pc + 1].type, "", stream, 0);
463 fputs_filtered ("} ", stream);
464 print_subexp (exp, pos, stream, PREC_PREFIX);
465 }
c906108c 466 if ((int) prec > (int) PREC_PREFIX)
c5aa993b 467 fputs_filtered (")", stream);
c906108c
SS
468 return;
469
9eaf6705 470 case UNOP_MEMVAL_TYPE:
9eaf6705
TT
471 if ((int) prec > (int) PREC_PREFIX)
472 fputs_filtered ("(", stream);
473 fputs_filtered ("{", stream);
474 print_subexp (exp, pos, stream, PREC_PREFIX);
475 fputs_filtered ("} ", stream);
476 print_subexp (exp, pos, stream, PREC_PREFIX);
477 if ((int) prec > (int) PREC_PREFIX)
478 fputs_filtered (")", stream);
479 return;
480
c906108c
SS
481 case BINOP_ASSIGN_MODIFY:
482 opcode = exp->elts[pc + 1].opcode;
483 (*pos) += 2;
484 myprec = PREC_ASSIGN;
485 assoc = 1;
486 assign_modify = 1;
487 op_str = "???";
488 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
489 if (op_print_tab[tem].opcode == opcode)
490 {
491 op_str = op_print_tab[tem].string;
492 break;
493 }
494 if (op_print_tab[tem].opcode != opcode)
495 /* Not found; don't try to keep going because we don't know how
496 to interpret further elements. */
8a3fe4f8 497 error (_("Invalid expression"));
c906108c
SS
498 break;
499
c5aa993b 500 /* C++ ops */
c906108c
SS
501
502 case OP_THIS:
503 ++(*pos);
aee28ec6
TT
504 if (exp->language_defn->la_name_of_this)
505 fputs_filtered (exp->language_defn->la_name_of_this, stream);
506 else
507 fprintf_filtered (stream, _("<language %s has no 'this'>"),
508 exp->language_defn->la_name);
82eeeb94
AF
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
600ea1be
JK
536 case TYPE_INSTANCE:
537 {
538 LONGEST count = exp->elts[pc + 1].longconst;
539
540 /* The COUNT. */
541 (*pos)++;
542 fputs_unfiltered ("TypesInstance(", stream);
543 while (count-- > 0)
544 {
545 type_print (exp->elts[(*pos)++].type, "", stream, 0);
546 if (count > 0)
547 fputs_unfiltered (",", stream);
548 }
549 fputs_unfiltered (",", stream);
550 /* Ending COUNT and ending TYPE_INSTANCE. */
551 (*pos) += 2;
552 print_subexp (exp, pos, stream, PREC_PREFIX);
553 fputs_unfiltered (")", stream);
554 return;
555 }
556
01739a3b 557 case OP_RANGE:
e4b8a1c8 558 {
01739a3b 559 enum range_type range_type;
e4b8a1c8 560
01739a3b 561 range_type = (enum range_type)
e4b8a1c8
TT
562 longest_to_int (exp->elts[pc + 1].longconst);
563 *pos += 2;
564
565 fputs_filtered ("RANGE(", stream);
566 if (range_type == HIGH_BOUND_DEFAULT
567 || range_type == NONE_BOUND_DEFAULT)
568 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
569 fputs_filtered ("..", stream);
570 if (range_type == LOW_BOUND_DEFAULT
571 || range_type == NONE_BOUND_DEFAULT)
572 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
573 fputs_filtered (")", stream);
574 return;
575 }
576
c5aa993b 577 /* Default ops */
c906108c
SS
578
579 default:
580 op_str = "???";
581 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
582 if (op_print_tab[tem].opcode == opcode)
583 {
584 op_str = op_print_tab[tem].string;
585 myprec = op_print_tab[tem].precedence;
586 assoc = op_print_tab[tem].right_assoc;
587 break;
588 }
589 if (op_print_tab[tem].opcode != opcode)
590 /* Not found; don't try to keep going because we don't know how
591 to interpret further elements. For example, this happens
592 if opcode is OP_TYPE. */
8a3fe4f8 593 error (_("Invalid expression"));
c5aa993b 594 }
c906108c 595
0963b4bd 596 /* Note that PREC_BUILTIN will always emit parentheses. */
c906108c
SS
597 if ((int) myprec < (int) prec)
598 fputs_filtered ("(", stream);
599 if ((int) opcode > (int) BINOP_END)
600 {
601 if (assoc)
602 {
603 /* Unary postfix operator. */
604 print_subexp (exp, pos, stream, PREC_SUFFIX);
605 fputs_filtered (op_str, stream);
606 }
607 else
608 {
609 /* Unary prefix operator. */
610 fputs_filtered (op_str, stream);
611 if (myprec == PREC_BUILTIN_FUNCTION)
612 fputs_filtered ("(", stream);
613 print_subexp (exp, pos, stream, PREC_PREFIX);
614 if (myprec == PREC_BUILTIN_FUNCTION)
615 fputs_filtered (")", stream);
616 }
617 }
618 else
619 {
620 /* Binary operator. */
621 /* Print left operand.
c5aa993b
JM
622 If operator is right-associative,
623 increment precedence for this operand. */
c906108c
SS
624 print_subexp (exp, pos, stream,
625 (enum precedence) ((int) myprec + assoc));
626 /* Print the operator itself. */
627 if (assign_modify)
628 fprintf_filtered (stream, " %s= ", op_str);
629 else if (op_str[0] == ',')
630 fprintf_filtered (stream, "%s ", op_str);
631 else
632 fprintf_filtered (stream, " %s ", op_str);
633 /* Print right operand.
c5aa993b
JM
634 If operator is left-associative,
635 increment precedence for this operand. */
c906108c
SS
636 print_subexp (exp, pos, stream,
637 (enum precedence) ((int) myprec + !assoc));
638 }
639
640 if ((int) myprec < (int) prec)
641 fputs_filtered (")", stream);
642}
643
644/* Return the operator corresponding to opcode OP as
645 a string. NULL indicates that the opcode was not found in the
646 current language table. */
a121b7c1 647const char *
fba45db2 648op_string (enum exp_opcode op)
c906108c
SS
649{
650 int tem;
f86f5ca3 651 const struct op_print *op_print_tab;
c906108c
SS
652
653 op_print_tab = current_language->la_op_print_tab;
654 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
655 if (op_print_tab[tem].opcode == op)
656 return op_print_tab[tem].string;
657 return NULL;
658}
659
c906108c
SS
660/* Support for dumping the raw data from expressions in a human readable
661 form. */
662
24daaebc 663static int dump_subexp_body (struct expression *exp, struct ui_file *, int);
c906108c 664
0963b4bd 665/* Name for OPCODE, when it appears in expression EXP. */
5f9769d1 666
a121b7c1 667const char *
5f9769d1
PH
668op_name (struct expression *exp, enum exp_opcode opcode)
669{
670 return exp->language_defn->la_exp_desc->op_name (opcode);
671}
672
673/* Default name for the standard operator OPCODE (i.e., one defined in
674 the definition of enum exp_opcode). */
675
a121b7c1 676const char *
5f9769d1 677op_name_standard (enum exp_opcode opcode)
c906108c
SS
678{
679 switch (opcode)
680 {
681 default:
682 {
683 static char buf[30];
684
08850b56 685 xsnprintf (buf, sizeof (buf), "<unknown %d>", opcode);
c906108c
SS
686 return buf;
687 }
56c12414
JK
688#define OP(name) \
689 case name: \
690 return #name ;
691#include "std-operator.def"
692#undef OP
c906108c
SS
693 }
694}
695
a411cd0e
DE
696/* Print a raw dump of expression EXP to STREAM.
697 NOTE, if non-NULL, is printed as extra explanatory text. */
698
c906108c 699void
24daaebc 700dump_raw_expression (struct expression *exp, struct ui_file *stream,
a121b7c1 701 const char *note)
c906108c
SS
702{
703 int elt;
c906108c
SS
704 char *eltscan;
705 int eltsize;
706
707 fprintf_filtered (stream, "Dump of expression @ ");
d4f3574e 708 gdb_print_host_address (exp, stream);
a411cd0e
DE
709 if (note)
710 fprintf_filtered (stream, ", %s:", note);
711 fprintf_filtered (stream, "\n\tLanguage %s, %d elements, %ld bytes each.\n",
c5aa993b 712 exp->language_defn->la_name, exp->nelts,
9d271fd8 713 (long) sizeof (union exp_element));
c906108c
SS
714 fprintf_filtered (stream, "\t%5s %20s %16s %s\n", "Index", "Opcode",
715 "Hex Value", "String Value");
c5aa993b 716 for (elt = 0; elt < exp->nelts; elt++)
c906108c
SS
717 {
718 fprintf_filtered (stream, "\t%5d ", elt);
c906108c 719
a121b7c1 720 const char *opcode_name = op_name (exp, exp->elts[elt].opcode);
c906108c 721 fprintf_filtered (stream, "%20s ", opcode_name);
a121b7c1 722
c5aa993b 723 print_longest (stream, 'd', 0, exp->elts[elt].longconst);
c906108c
SS
724 fprintf_filtered (stream, " ");
725
726 for (eltscan = (char *) &exp->elts[elt],
c5aa993b 727 eltsize = sizeof (union exp_element);
c906108c
SS
728 eltsize-- > 0;
729 eltscan++)
730 {
731 fprintf_filtered (stream, "%c",
732 isprint (*eltscan) ? (*eltscan & 0xFF) : '.');
733 }
734 fprintf_filtered (stream, "\n");
735 }
736}
737
24daaebc
PH
738/* Dump the subexpression of prefix expression EXP whose operator is at
739 position ELT onto STREAM. Returns the position of the next
740 subexpression in EXP. */
c906108c 741
24daaebc 742int
fba45db2 743dump_subexp (struct expression *exp, struct ui_file *stream, int elt)
c906108c
SS
744{
745 static int indent = 0;
746 int i;
747
748 fprintf_filtered (stream, "\n");
749 fprintf_filtered (stream, "\t%5d ", elt);
750
751 for (i = 1; i <= indent; i++)
752 fprintf_filtered (stream, " ");
753 indent += 2;
754
5f9769d1 755 fprintf_filtered (stream, "%-20s ", op_name (exp, exp->elts[elt].opcode));
c906108c 756
24daaebc
PH
757 elt = dump_subexp_body (exp, stream, elt);
758
759 indent -= 2;
760
761 return elt;
762}
763
764/* Dump the operands of prefix expression EXP whose opcode is at
765 position ELT onto STREAM. Returns the position of the next
766 subexpression in EXP. */
767
768static int
769dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
5f9769d1
PH
770{
771 return exp->language_defn->la_exp_desc->dump_subexp_body (exp, stream, elt);
772}
773
774/* Default value for subexp_body in exp_descriptor vector. */
775
776int
777dump_subexp_body_standard (struct expression *exp,
778 struct ui_file *stream, int elt)
24daaebc
PH
779{
780 int opcode = exp->elts[elt++].opcode;
781
782 switch (opcode)
c906108c
SS
783 {
784 case TERNOP_COND:
785 case TERNOP_SLICE:
c906108c 786 elt = dump_subexp (exp, stream, elt);
cb969d61 787 /* FALL THROUGH */
c906108c
SS
788 case BINOP_ADD:
789 case BINOP_SUB:
790 case BINOP_MUL:
791 case BINOP_DIV:
792 case BINOP_REM:
793 case BINOP_MOD:
794 case BINOP_LSH:
795 case BINOP_RSH:
796 case BINOP_LOGICAL_AND:
797 case BINOP_LOGICAL_OR:
798 case BINOP_BITWISE_AND:
799 case BINOP_BITWISE_IOR:
800 case BINOP_BITWISE_XOR:
801 case BINOP_EQUAL:
802 case BINOP_NOTEQUAL:
803 case BINOP_LESS:
804 case BINOP_GTR:
805 case BINOP_LEQ:
806 case BINOP_GEQ:
807 case BINOP_REPEAT:
808 case BINOP_ASSIGN:
809 case BINOP_COMMA:
810 case BINOP_SUBSCRIPT:
811 case BINOP_EXP:
812 case BINOP_MIN:
813 case BINOP_MAX:
c906108c
SS
814 case BINOP_INTDIV:
815 case BINOP_ASSIGN_MODIFY:
816 case BINOP_VAL:
c906108c 817 case BINOP_CONCAT:
c906108c 818 case BINOP_END:
0534816d
DJ
819 case STRUCTOP_MEMBER:
820 case STRUCTOP_MPTR:
c906108c 821 elt = dump_subexp (exp, stream, elt);
cb969d61 822 /* FALL THROUGH */
c906108c
SS
823 case UNOP_NEG:
824 case UNOP_LOGICAL_NOT:
825 case UNOP_COMPLEMENT:
826 case UNOP_IND:
827 case UNOP_ADDR:
828 case UNOP_PREINCREMENT:
829 case UNOP_POSTINCREMENT:
830 case UNOP_PREDECREMENT:
831 case UNOP_POSTDECREMENT:
832 case UNOP_SIZEOF:
833 case UNOP_PLUS:
834 case UNOP_CAP:
835 case UNOP_CHR:
836 case UNOP_ORD:
837 case UNOP_ABS:
838 case UNOP_FLOAT:
839 case UNOP_HIGH:
840 case UNOP_MAX:
841 case UNOP_MIN:
842 case UNOP_ODD:
843 case UNOP_TRUNC:
c906108c
SS
844 elt = dump_subexp (exp, stream, elt);
845 break;
846 case OP_LONG:
d4f3574e
SS
847 fprintf_filtered (stream, "Type @");
848 gdb_print_host_address (exp->elts[elt].type, stream);
849 fprintf_filtered (stream, " (");
c906108c
SS
850 type_print (exp->elts[elt].type, NULL, stream, 0);
851 fprintf_filtered (stream, "), value %ld (0x%lx)",
c5aa993b
JM
852 (long) exp->elts[elt + 1].longconst,
853 (long) exp->elts[elt + 1].longconst);
c906108c
SS
854 elt += 3;
855 break;
856 case OP_DOUBLE:
d4f3574e
SS
857 fprintf_filtered (stream, "Type @");
858 gdb_print_host_address (exp->elts[elt].type, stream);
859 fprintf_filtered (stream, " (");
c906108c
SS
860 type_print (exp->elts[elt].type, NULL, stream, 0);
861 fprintf_filtered (stream, "), value %g",
c5aa993b 862 (double) exp->elts[elt + 1].doubleconst);
c906108c
SS
863 elt += 3;
864 break;
865 case OP_VAR_VALUE:
d4f3574e
SS
866 fprintf_filtered (stream, "Block @");
867 gdb_print_host_address (exp->elts[elt].block, stream);
868 fprintf_filtered (stream, ", symbol @");
869 gdb_print_host_address (exp->elts[elt + 1].symbol, stream);
870 fprintf_filtered (stream, " (%s)",
3567439c 871 SYMBOL_PRINT_NAME (exp->elts[elt + 1].symbol));
c906108c
SS
872 elt += 3;
873 break;
74ea4be4
PA
874 case OP_VAR_MSYM_VALUE:
875 fprintf_filtered (stream, "Objfile @");
876 gdb_print_host_address (exp->elts[elt].objfile, stream);
877 fprintf_filtered (stream, ", msymbol @");
878 gdb_print_host_address (exp->elts[elt + 1].msymbol, stream);
879 fprintf_filtered (stream, " (%s)",
880 MSYMBOL_PRINT_NAME (exp->elts[elt + 1].msymbol));
881 elt += 3;
882 break;
36b11add
JK
883 case OP_VAR_ENTRY_VALUE:
884 fprintf_filtered (stream, "Entry value of symbol @");
885 gdb_print_host_address (exp->elts[elt].symbol, stream);
886 fprintf_filtered (stream, " (%s)",
887 SYMBOL_PRINT_NAME (exp->elts[elt].symbol));
888 elt += 2;
889 break;
c906108c
SS
890 case OP_LAST:
891 fprintf_filtered (stream, "History element %ld",
c5aa993b 892 (long) exp->elts[elt].longconst);
c906108c
SS
893 elt += 2;
894 break;
895 case OP_REGISTER:
67f3407f
DJ
896 fprintf_filtered (stream, "Register $%s", &exp->elts[elt + 1].string);
897 elt += 3 + BYTES_TO_EXP_ELEM (exp->elts[elt].longconst + 1);
c906108c
SS
898 break;
899 case OP_INTERNALVAR:
d4f3574e
SS
900 fprintf_filtered (stream, "Internal var @");
901 gdb_print_host_address (exp->elts[elt].internalvar, stream);
902 fprintf_filtered (stream, " (%s)",
4fa62494 903 internalvar_name (exp->elts[elt].internalvar));
c906108c
SS
904 elt += 2;
905 break;
906 case OP_FUNCALL:
907 {
24daaebc 908 int i, nargs;
c906108c
SS
909
910 nargs = longest_to_int (exp->elts[elt].longconst);
911
912 fprintf_filtered (stream, "Number of args: %d", nargs);
913 elt += 2;
914
915 for (i = 1; i <= nargs + 1; i++)
916 elt = dump_subexp (exp, stream, elt);
917 }
918 break;
919 case OP_ARRAY:
920 {
921 int lower, upper;
922 int i;
923
924 lower = longest_to_int (exp->elts[elt].longconst);
925 upper = longest_to_int (exp->elts[elt + 1].longconst);
926
927 fprintf_filtered (stream, "Bounds [%d:%d]", lower, upper);
928 elt += 3;
929
930 for (i = 1; i <= upper - lower + 1; i++)
931 elt = dump_subexp (exp, stream, elt);
932 }
933 break;
4e8f195d
TT
934 case UNOP_DYNAMIC_CAST:
935 case UNOP_REINTERPRET_CAST:
9eaf6705
TT
936 case UNOP_CAST_TYPE:
937 case UNOP_MEMVAL_TYPE:
9eaf6705
TT
938 fprintf_filtered (stream, " (");
939 elt = dump_subexp (exp, stream, elt);
940 fprintf_filtered (stream, ")");
941 elt = dump_subexp (exp, stream, elt);
942 break;
943 case UNOP_MEMVAL:
944 case UNOP_CAST:
d4f3574e
SS
945 fprintf_filtered (stream, "Type @");
946 gdb_print_host_address (exp->elts[elt].type, stream);
947 fprintf_filtered (stream, " (");
c906108c
SS
948 type_print (exp->elts[elt].type, NULL, stream, 0);
949 fprintf_filtered (stream, ")");
950 elt = dump_subexp (exp, stream, elt + 2);
951 break;
952 case OP_TYPE:
d4f3574e
SS
953 fprintf_filtered (stream, "Type @");
954 gdb_print_host_address (exp->elts[elt].type, stream);
955 fprintf_filtered (stream, " (");
c906108c
SS
956 type_print (exp->elts[elt].type, NULL, stream, 0);
957 fprintf_filtered (stream, ")");
958 elt += 2;
959 break;
608b4967
TT
960 case OP_TYPEOF:
961 case OP_DECLTYPE:
962 fprintf_filtered (stream, "Typeof (");
963 elt = dump_subexp (exp, stream, elt);
964 fprintf_filtered (stream, ")");
965 break;
6e72ca20
TT
966 case OP_TYPEID:
967 fprintf_filtered (stream, "typeid (");
968 elt = dump_subexp (exp, stream, elt);
969 fprintf_filtered (stream, ")");
970 break;
c906108c
SS
971 case STRUCTOP_STRUCT:
972 case STRUCTOP_PTR:
973 {
974 char *elem_name;
975 int len;
976
977 len = longest_to_int (exp->elts[elt].longconst);
978 elem_name = &exp->elts[elt + 1].string;
979
980 fprintf_filtered (stream, "Element name: `%.*s'", len, elem_name);
981 elt = dump_subexp (exp, stream, elt + 3 + BYTES_TO_EXP_ELEM (len + 1));
982 }
983 break;
984 case OP_SCOPE:
985 {
986 char *elem_name;
987 int len;
988
d4f3574e
SS
989 fprintf_filtered (stream, "Type @");
990 gdb_print_host_address (exp->elts[elt].type, stream);
991 fprintf_filtered (stream, " (");
c906108c
SS
992 type_print (exp->elts[elt].type, NULL, stream, 0);
993 fprintf_filtered (stream, ") ");
994
995 len = longest_to_int (exp->elts[elt + 1].longconst);
996 elem_name = &exp->elts[elt + 2].string;
997
998 fprintf_filtered (stream, "Field name: `%.*s'", len, elem_name);
999 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1000 }
1001 break;
600ea1be
JK
1002 case TYPE_INSTANCE:
1003 {
600ea1be
JK
1004 LONGEST len;
1005
1006 len = exp->elts[elt++].longconst;
1007 fprintf_filtered (stream, "%s TypeInstance: ", plongest (len));
1008 while (len-- > 0)
1009 {
1010 fprintf_filtered (stream, "Type @");
1011 gdb_print_host_address (exp->elts[elt].type, stream);
1012 fprintf_filtered (stream, " (");
1013 type_print (exp->elts[elt].type, NULL, stream, 0);
1014 fprintf_filtered (stream, ")");
1015 elt++;
1016 if (len > 0)
1017 fputs_filtered (", ", stream);
1018 }
1019 /* Ending LEN and ending TYPE_INSTANCE. */
1020 elt += 2;
1021 elt = dump_subexp (exp, stream, elt);
1022 }
1023 break;
2d40be18
SM
1024 case OP_STRING:
1025 {
1026 LONGEST len = exp->elts[elt].longconst;
1027 LONGEST type = exp->elts[elt + 1].longconst;
1028
1029 fprintf_filtered (stream, "Language-specific string type: %s",
1030 plongest (type));
1031
1032 /* Skip length. */
1033 elt += 1;
1034
1035 /* Skip string content. */
1036 elt += BYTES_TO_EXP_ELEM (len);
1037
1038 /* Skip length and ending OP_STRING. */
1039 elt += 2;
1040 }
1041 break;
01739a3b 1042 case OP_RANGE:
e4b8a1c8 1043 {
01739a3b 1044 enum range_type range_type;
e4b8a1c8 1045
01739a3b 1046 range_type = (enum range_type)
e4b8a1c8
TT
1047 longest_to_int (exp->elts[elt].longconst);
1048 elt += 2;
1049
1050 switch (range_type)
1051 {
1052 case BOTH_BOUND_DEFAULT:
1053 fputs_filtered ("Range '..'", stream);
1054 break;
1055 case LOW_BOUND_DEFAULT:
1056 fputs_filtered ("Range '..EXP'", stream);
1057 break;
1058 case HIGH_BOUND_DEFAULT:
1059 fputs_filtered ("Range 'EXP..'", stream);
1060 break;
1061 case NONE_BOUND_DEFAULT:
1062 fputs_filtered ("Range 'EXP..EXP'", stream);
1063 break;
1064 default:
1065 fputs_filtered ("Invalid Range!", stream);
1066 break;
1067 }
1068
1069 if (range_type == HIGH_BOUND_DEFAULT
1070 || range_type == NONE_BOUND_DEFAULT)
1071 elt = dump_subexp (exp, stream, elt);
1072 if (range_type == LOW_BOUND_DEFAULT
1073 || range_type == NONE_BOUND_DEFAULT)
1074 elt = dump_subexp (exp, stream, elt);
1075 }
1076 break;
1077
c906108c
SS
1078 default:
1079 case OP_NULL:
c906108c
SS
1080 case MULTI_SUBSCRIPT:
1081 case OP_F77_UNDETERMINED_ARGLIST:
1082 case OP_COMPLEX:
c906108c
SS
1083 case OP_BOOL:
1084 case OP_M2_STRING:
1085 case OP_THIS:
c906108c 1086 case OP_NAME:
c906108c
SS
1087 fprintf_filtered (stream, "Unknown format");
1088 }
1089
c906108c
SS
1090 return elt;
1091}
1092
1093void
24daaebc 1094dump_prefix_expression (struct expression *exp, struct ui_file *stream)
c906108c
SS
1095{
1096 int elt;
1097
1098 fprintf_filtered (stream, "Dump of expression @ ");
d4f3574e 1099 gdb_print_host_address (exp, stream);
24daaebc 1100 fputs_filtered (", after conversion to prefix form:\nExpression: `", stream);
4f485ebc 1101 print_expression (exp, stream);
9d271fd8 1102 fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
c5aa993b 1103 exp->language_defn->la_name, exp->nelts,
9d271fd8 1104 (long) sizeof (union exp_element));
c906108c
SS
1105 fputs_filtered ("\n", stream);
1106
c5aa993b 1107 for (elt = 0; elt < exp->nelts;)
c906108c
SS
1108 elt = dump_subexp (exp, stream, elt);
1109 fputs_filtered ("\n", stream);
1110}
This page took 1.205104 seconds and 4 git commands to generate.