1 /* Print in infix form a struct expression.
2 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GDB.
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
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
25 #include "expression.h"
28 #include "parser-defs.h"
34 /* Prototypes for local functions */
36 static void print_subexp (struct expression *, int *, struct ui_file *,
40 print_expression (struct expression *exp, struct ui_file *stream)
43 print_subexp (exp, &pc, stream, PREC_NULL);
46 /* Print the subexpression of EXP that starts in position POS, on STREAM.
47 PREC is the precedence of the surrounding operator;
48 if the precedence of the main operator of this subexpression is less,
49 parentheses are needed here. */
52 print_subexp (register struct expression *exp, register int *pos,
53 struct ui_file *stream, enum precedence prec)
55 register unsigned tem;
56 register const struct op_print *op_print_tab;
59 register char *op_str;
60 int assign_modify = 0;
61 enum exp_opcode opcode;
62 enum precedence myprec = PREC_NULL;
63 /* Set to 1 for a right-associative operator. */
68 op_print_tab = exp->language_defn->la_op_print_tab;
70 opcode = exp->elts[pc].opcode;
78 fputs_filtered (type_name_no_tag (exp->elts[pc + 1].type), stream);
79 fputs_filtered ("::", stream);
80 nargs = longest_to_int (exp->elts[pc + 2].longconst);
81 (*pos) += 4 + BYTES_TO_EXP_ELEM (nargs + 1);
82 fputs_filtered (&exp->elts[pc + 3].string, stream);
87 value_print (value_from_longest (exp->elts[pc + 1].type,
88 exp->elts[pc + 2].longconst),
89 stream, 0, Val_no_prettyprint);
94 value_print (value_from_double (exp->elts[pc + 1].type,
95 exp->elts[pc + 2].doubleconst),
96 stream, 0, Val_no_prettyprint);
103 b = exp->elts[pc + 1].block;
105 && BLOCK_FUNCTION (b) != NULL
106 && SYMBOL_SOURCE_NAME (BLOCK_FUNCTION (b)) != NULL)
108 fputs_filtered (SYMBOL_SOURCE_NAME (BLOCK_FUNCTION (b)), stream);
109 fputs_filtered ("::", stream);
111 fputs_filtered (SYMBOL_SOURCE_NAME (exp->elts[pc + 2].symbol), stream);
117 fprintf_filtered (stream, "$%d",
118 longest_to_int (exp->elts[pc + 1].longconst));
123 fprintf_filtered (stream, "$%s",
124 REGISTER_NAME (longest_to_int (exp->elts[pc + 1].longconst)));
129 fprintf_filtered (stream, "%s",
130 longest_to_int (exp->elts[pc + 1].longconst)
136 fprintf_filtered (stream, "$%s",
137 internalvar_name (exp->elts[pc + 1].internalvar));
142 nargs = longest_to_int (exp->elts[pc + 1].longconst);
143 print_subexp (exp, pos, stream, PREC_SUFFIX);
144 fputs_filtered (" (", stream);
145 for (tem = 0; tem < nargs; tem++)
148 fputs_filtered (", ", stream);
149 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
151 fputs_filtered (")", stream);
156 nargs = longest_to_int (exp->elts[pc + 1].longconst);
157 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
158 fputs_filtered (&exp->elts[pc + 2].string, stream);
162 nargs = longest_to_int (exp->elts[pc + 1].longconst);
163 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
164 /* LA_PRINT_STRING will print using the current repeat count threshold.
165 If necessary, we can temporarily set it to zero, or pass it as an
166 additional parameter to LA_PRINT_STRING. -fnf */
167 LA_PRINT_STRING (stream, &exp->elts[pc + 2].string, nargs, 1, 0);
171 nargs = longest_to_int (exp->elts[pc + 1].longconst);
173 += 3 + BYTES_TO_EXP_ELEM ((nargs + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT);
174 fprintf_unfiltered (stream, "B'<unimplemented>'");
179 nargs = longest_to_int (exp->elts[pc + 2].longconst);
180 nargs -= longest_to_int (exp->elts[pc + 1].longconst);
183 if (exp->elts[pc + 4].opcode == OP_LONG
184 && exp->elts[pc + 5].type == builtin_type_char
185 && exp->language_defn->la_language == language_c)
187 /* Attempt to print C character arrays using string syntax.
188 Walk through the args, picking up one character from each
189 of the OP_LONG expression elements. If any array element
190 does not match our expection of what we should find for
191 a simple string, revert back to array printing. Note that
192 the last expression element is an explicit null terminator
193 byte, which doesn't get printed. */
194 tempstr = alloca (nargs);
198 if (exp->elts[pc].opcode != OP_LONG
199 || exp->elts[pc + 1].type != builtin_type_char)
201 /* Not a simple array of char, use regular array printing. */
208 longest_to_int (exp->elts[pc + 2].longconst);
215 LA_PRINT_STRING (stream, tempstr, nargs - 1, 1, 0);
220 int is_chill = exp->language_defn->la_language == language_chill;
221 fputs_filtered (is_chill ? " [" : " {", stream);
222 for (tem = 0; tem < nargs; tem++)
226 fputs_filtered (", ", stream);
228 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
230 fputs_filtered (is_chill ? "]" : "}", stream);
235 tem = longest_to_int (exp->elts[pc + 1].longconst);
236 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
238 if (exp->language_defn->la_language == language_chill)
240 fputs_filtered (".", stream);
241 fputs_filtered (&exp->elts[pc + 2].string, stream);
242 fputs_filtered (exp->elts[*pos].opcode == OP_LABELED ? ", "
248 /* Gcc support both these syntaxes. Unsure which is preferred. */
250 fputs_filtered (&exp->elts[pc + 2].string, stream);
251 fputs_filtered (": ", stream);
253 fputs_filtered (".", stream);
254 fputs_filtered (&exp->elts[pc + 2].string, stream);
255 fputs_filtered ("=", stream);
258 print_subexp (exp, pos, stream, PREC_SUFFIX);
262 if ((int) prec > (int) PREC_COMMA)
263 fputs_filtered ("(", stream);
264 /* Print the subexpressions, forcing parentheses
265 around any binary operations within them.
266 This is more parentheses than are strictly necessary,
267 but it looks clearer. */
268 print_subexp (exp, pos, stream, PREC_HYPER);
269 fputs_filtered (" ? ", stream);
270 print_subexp (exp, pos, stream, PREC_HYPER);
271 fputs_filtered (" : ", stream);
272 print_subexp (exp, pos, stream, PREC_HYPER);
273 if ((int) prec > (int) PREC_COMMA)
274 fputs_filtered (")", stream);
278 case TERNOP_SLICE_COUNT:
279 print_subexp (exp, pos, stream, PREC_SUFFIX);
280 fputs_filtered ("(", stream);
281 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
282 fputs_filtered (opcode == TERNOP_SLICE ? " : " : " UP ", stream);
283 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
284 fputs_filtered (")", stream);
287 case STRUCTOP_STRUCT:
288 tem = longest_to_int (exp->elts[pc + 1].longconst);
289 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
290 print_subexp (exp, pos, stream, PREC_SUFFIX);
291 fputs_filtered (".", stream);
292 fputs_filtered (&exp->elts[pc + 2].string, stream);
295 /* Will not occur for Modula-2 */
297 tem = longest_to_int (exp->elts[pc + 1].longconst);
298 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
299 print_subexp (exp, pos, stream, PREC_SUFFIX);
300 fputs_filtered ("->", stream);
301 fputs_filtered (&exp->elts[pc + 2].string, stream);
304 case BINOP_SUBSCRIPT:
305 print_subexp (exp, pos, stream, PREC_SUFFIX);
306 fputs_filtered ("[", stream);
307 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
308 fputs_filtered ("]", stream);
311 case UNOP_POSTINCREMENT:
312 print_subexp (exp, pos, stream, PREC_SUFFIX);
313 fputs_filtered ("++", stream);
316 case UNOP_POSTDECREMENT:
317 print_subexp (exp, pos, stream, PREC_SUFFIX);
318 fputs_filtered ("--", stream);
323 if ((int) prec > (int) PREC_PREFIX)
324 fputs_filtered ("(", stream);
325 fputs_filtered ("(", stream);
326 type_print (exp->elts[pc + 1].type, "", stream, 0);
327 fputs_filtered (") ", stream);
328 print_subexp (exp, pos, stream, PREC_PREFIX);
329 if ((int) prec > (int) PREC_PREFIX)
330 fputs_filtered (")", stream);
335 if ((int) prec > (int) PREC_PREFIX)
336 fputs_filtered ("(", stream);
337 if (exp->elts[pc + 1].type->code == TYPE_CODE_FUNC &&
338 exp->elts[pc + 3].opcode == OP_LONG)
340 /* We have a minimal symbol fn, probably. It's encoded
341 as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
342 Swallow the OP_LONG (including both its opcodes); ignore
343 its type; print the value in the type of the MEMVAL. */
345 val = value_at_lazy (exp->elts[pc + 1].type,
346 (CORE_ADDR) exp->elts[pc + 5].longconst,
348 value_print (val, stream, 0, Val_no_prettyprint);
352 fputs_filtered ("{", stream);
353 type_print (exp->elts[pc + 1].type, "", stream, 0);
354 fputs_filtered ("} ", stream);
355 print_subexp (exp, pos, stream, PREC_PREFIX);
357 if ((int) prec > (int) PREC_PREFIX)
358 fputs_filtered (")", stream);
361 case BINOP_ASSIGN_MODIFY:
362 opcode = exp->elts[pc + 1].opcode;
364 myprec = PREC_ASSIGN;
368 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
369 if (op_print_tab[tem].opcode == opcode)
371 op_str = op_print_tab[tem].string;
374 if (op_print_tab[tem].opcode != opcode)
375 /* Not found; don't try to keep going because we don't know how
376 to interpret further elements. */
377 error ("Invalid expression");
384 fputs_filtered ("this", stream);
389 case MULTI_SUBSCRIPT:
391 nargs = longest_to_int (exp->elts[pc + 1].longconst);
392 print_subexp (exp, pos, stream, PREC_SUFFIX);
393 fprintf_unfiltered (stream, " [");
394 for (tem = 0; tem < nargs; tem++)
397 fprintf_unfiltered (stream, ", ");
398 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
400 fprintf_unfiltered (stream, "]");
405 fprintf_unfiltered (stream, "VAL(");
406 type_print (exp->elts[pc + 1].type, "", stream, 0);
407 fprintf_unfiltered (stream, ",");
408 print_subexp (exp, pos, stream, PREC_PREFIX);
409 fprintf_unfiltered (stream, ")");
414 error ("print_subexp: Not implemented.");
420 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
421 if (op_print_tab[tem].opcode == opcode)
423 op_str = op_print_tab[tem].string;
424 myprec = op_print_tab[tem].precedence;
425 assoc = op_print_tab[tem].right_assoc;
428 if (op_print_tab[tem].opcode != opcode)
429 /* Not found; don't try to keep going because we don't know how
430 to interpret further elements. For example, this happens
431 if opcode is OP_TYPE. */
432 error ("Invalid expression");
435 /* Note that PREC_BUILTIN will always emit parentheses. */
436 if ((int) myprec < (int) prec)
437 fputs_filtered ("(", stream);
438 if ((int) opcode > (int) BINOP_END)
442 /* Unary postfix operator. */
443 print_subexp (exp, pos, stream, PREC_SUFFIX);
444 fputs_filtered (op_str, stream);
448 /* Unary prefix operator. */
449 fputs_filtered (op_str, stream);
450 if (myprec == PREC_BUILTIN_FUNCTION)
451 fputs_filtered ("(", stream);
452 print_subexp (exp, pos, stream, PREC_PREFIX);
453 if (myprec == PREC_BUILTIN_FUNCTION)
454 fputs_filtered (")", stream);
459 /* Binary operator. */
460 /* Print left operand.
461 If operator is right-associative,
462 increment precedence for this operand. */
463 print_subexp (exp, pos, stream,
464 (enum precedence) ((int) myprec + assoc));
465 /* Print the operator itself. */
467 fprintf_filtered (stream, " %s= ", op_str);
468 else if (op_str[0] == ',')
469 fprintf_filtered (stream, "%s ", op_str);
471 fprintf_filtered (stream, " %s ", op_str);
472 /* Print right operand.
473 If operator is left-associative,
474 increment precedence for this operand. */
475 print_subexp (exp, pos, stream,
476 (enum precedence) ((int) myprec + !assoc));
479 if ((int) myprec < (int) prec)
480 fputs_filtered (")", stream);
483 /* Return the operator corresponding to opcode OP as
484 a string. NULL indicates that the opcode was not found in the
485 current language table. */
487 op_string (enum exp_opcode op)
490 register const struct op_print *op_print_tab;
492 op_print_tab = current_language->la_op_print_tab;
493 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
494 if (op_print_tab[tem].opcode == op)
495 return op_print_tab[tem].string;
499 /* Support for dumping the raw data from expressions in a human readable
502 static char *op_name (int opcode);
513 sprintf (buf, "<unknown %d>", opcode);
534 case BINOP_LOGICAL_AND:
535 return "BINOP_LOGICAL_AND";
536 case BINOP_LOGICAL_OR:
537 return "BINOP_LOGICAL_OR";
538 case BINOP_BITWISE_AND:
539 return "BINOP_BITWISE_AND";
540 case BINOP_BITWISE_IOR:
541 return "BINOP_BITWISE_IOR";
542 case BINOP_BITWISE_XOR:
543 return "BINOP_BITWISE_XOR";
545 return "BINOP_EQUAL";
547 return "BINOP_NOTEQUAL";
557 return "BINOP_REPEAT";
559 return "BINOP_ASSIGN";
561 return "BINOP_COMMA";
562 case BINOP_SUBSCRIPT:
563 return "BINOP_SUBSCRIPT";
564 case MULTI_SUBSCRIPT:
565 return "MULTI_SUBSCRIPT";
572 case STRUCTOP_MEMBER:
573 return "STRUCTOP_MEMBER";
575 return "STRUCTOP_MPTR";
577 return "BINOP_INTDIV";
578 case BINOP_ASSIGN_MODIFY:
579 return "BINOP_ASSIGN_MODIFY";
587 return "BINOP_CONCAT";
589 return "BINOP_RANGE";
593 return "TERNOP_COND";
595 return "TERNOP_SLICE";
596 case TERNOP_SLICE_COUNT:
597 return "TERNOP_SLICE_COUNT";
603 return "OP_VAR_VALUE";
607 return "OP_REGISTER";
609 return "OP_INTERNALVAR";
615 return "OP_BITSTRING";
621 return "UNOP_MEMVAL";
624 case UNOP_LOGICAL_NOT:
625 return "UNOP_LOGICAL_NOT";
626 case UNOP_COMPLEMENT:
627 return "UNOP_COMPLEMENT";
632 case UNOP_PREINCREMENT:
633 return "UNOP_PREINCREMENT";
634 case UNOP_POSTINCREMENT:
635 return "UNOP_POSTINCREMENT";
636 case UNOP_PREDECREMENT:
637 return "UNOP_PREDECREMENT";
638 case UNOP_POSTDECREMENT:
639 return "UNOP_POSTDECREMENT";
641 return "UNOP_SIZEOF";
647 return "UNOP_LENGTH";
673 return "OP_M2_STRING";
674 case STRUCTOP_STRUCT:
675 return "STRUCTOP_STRUCT";
677 return "STRUCTOP_PTR";
690 dump_prefix_expression (struct expression *exp, struct ui_file *stream,
698 fprintf_filtered (stream, "Dump of expression @ ");
699 gdb_print_host_address (exp, stream);
700 fprintf_filtered (stream, ", %s:\nExpression: `", note);
701 if (exp->elts[0].opcode != OP_TYPE)
702 print_expression (exp, stream);
704 fprintf_filtered (stream, "Type printing not yet supported....");
705 fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
706 exp->language_defn->la_name, exp->nelts,
707 (long) sizeof (union exp_element));
708 fprintf_filtered (stream, "\t%5s %20s %16s %s\n", "Index", "Opcode",
709 "Hex Value", "String Value");
710 for (elt = 0; elt < exp->nelts; elt++)
712 fprintf_filtered (stream, "\t%5d ", elt);
713 opcode_name = op_name (exp->elts[elt].opcode);
715 fprintf_filtered (stream, "%20s ", opcode_name);
716 print_longest (stream, 'd', 0, exp->elts[elt].longconst);
717 fprintf_filtered (stream, " ");
719 for (eltscan = (char *) &exp->elts[elt],
720 eltsize = sizeof (union exp_element);
724 fprintf_filtered (stream, "%c",
725 isprint (*eltscan) ? (*eltscan & 0xFF) : '.');
727 fprintf_filtered (stream, "\n");
731 static int dump_subexp (struct expression *exp, struct ui_file *stream,
735 dump_subexp (struct expression *exp, struct ui_file *stream, int elt)
737 static int indent = 0;
740 fprintf_filtered (stream, "\n");
741 fprintf_filtered (stream, "\t%5d ", elt);
743 for (i = 1; i <= indent; i++)
744 fprintf_filtered (stream, " ");
747 fprintf_filtered (stream, "%-20s ", op_name (exp->elts[elt].opcode));
749 switch (exp->elts[elt++].opcode)
753 case TERNOP_SLICE_COUNT:
754 elt = dump_subexp (exp, stream, elt);
763 case BINOP_LOGICAL_AND:
764 case BINOP_LOGICAL_OR:
765 case BINOP_BITWISE_AND:
766 case BINOP_BITWISE_IOR:
767 case BINOP_BITWISE_XOR:
777 case BINOP_SUBSCRIPT:
782 case BINOP_ASSIGN_MODIFY:
790 elt = dump_subexp (exp, stream, elt);
792 case UNOP_LOGICAL_NOT:
793 case UNOP_COMPLEMENT:
796 case UNOP_PREINCREMENT:
797 case UNOP_POSTINCREMENT:
798 case UNOP_PREDECREMENT:
799 case UNOP_POSTDECREMENT:
818 elt = dump_subexp (exp, stream, elt);
821 fprintf_filtered (stream, "Type @");
822 gdb_print_host_address (exp->elts[elt].type, stream);
823 fprintf_filtered (stream, " (");
824 type_print (exp->elts[elt].type, NULL, stream, 0);
825 fprintf_filtered (stream, "), value %ld (0x%lx)",
826 (long) exp->elts[elt + 1].longconst,
827 (long) exp->elts[elt + 1].longconst);
831 fprintf_filtered (stream, "Type @");
832 gdb_print_host_address (exp->elts[elt].type, stream);
833 fprintf_filtered (stream, " (");
834 type_print (exp->elts[elt].type, NULL, stream, 0);
835 fprintf_filtered (stream, "), value %g",
836 (double) exp->elts[elt + 1].doubleconst);
840 fprintf_filtered (stream, "Block @");
841 gdb_print_host_address (exp->elts[elt].block, stream);
842 fprintf_filtered (stream, ", symbol @");
843 gdb_print_host_address (exp->elts[elt + 1].symbol, stream);
844 fprintf_filtered (stream, " (%s)",
845 SYMBOL_NAME (exp->elts[elt + 1].symbol));
849 fprintf_filtered (stream, "History element %ld",
850 (long) exp->elts[elt].longconst);
854 fprintf_filtered (stream, "Register %ld",
855 (long) exp->elts[elt].longconst);
859 fprintf_filtered (stream, "Internal var @");
860 gdb_print_host_address (exp->elts[elt].internalvar, stream);
861 fprintf_filtered (stream, " (%s)",
862 exp->elts[elt].internalvar->name);
869 nargs = longest_to_int (exp->elts[elt].longconst);
871 fprintf_filtered (stream, "Number of args: %d", nargs);
874 for (i = 1; i <= nargs + 1; i++)
875 elt = dump_subexp (exp, stream, elt);
883 lower = longest_to_int (exp->elts[elt].longconst);
884 upper = longest_to_int (exp->elts[elt + 1].longconst);
886 fprintf_filtered (stream, "Bounds [%d:%d]", lower, upper);
889 for (i = 1; i <= upper - lower + 1; i++)
890 elt = dump_subexp (exp, stream, elt);
895 fprintf_filtered (stream, "Type @");
896 gdb_print_host_address (exp->elts[elt].type, stream);
897 fprintf_filtered (stream, " (");
898 type_print (exp->elts[elt].type, NULL, stream, 0);
899 fprintf_filtered (stream, ")");
900 elt = dump_subexp (exp, stream, elt + 2);
903 fprintf_filtered (stream, "Type @");
904 gdb_print_host_address (exp->elts[elt].type, stream);
905 fprintf_filtered (stream, " (");
906 type_print (exp->elts[elt].type, NULL, stream, 0);
907 fprintf_filtered (stream, ")");
910 case STRUCTOP_STRUCT:
916 len = longest_to_int (exp->elts[elt].longconst);
917 elem_name = &exp->elts[elt + 1].string;
919 fprintf_filtered (stream, "Element name: `%.*s'", len, elem_name);
920 elt = dump_subexp (exp, stream, elt + 3 + BYTES_TO_EXP_ELEM (len + 1));
928 fprintf_filtered (stream, "Type @");
929 gdb_print_host_address (exp->elts[elt].type, stream);
930 fprintf_filtered (stream, " (");
931 type_print (exp->elts[elt].type, NULL, stream, 0);
932 fprintf_filtered (stream, ") ");
934 len = longest_to_int (exp->elts[elt + 1].longconst);
935 elem_name = &exp->elts[elt + 2].string;
937 fprintf_filtered (stream, "Field name: `%.*s'", len, elem_name);
938 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
943 case STRUCTOP_MEMBER:
945 case MULTI_SUBSCRIPT:
946 case OP_F77_UNDETERMINED_ARGLIST:
956 fprintf_filtered (stream, "Unknown format");
965 dump_postfix_expression (struct expression *exp, struct ui_file *stream,
970 fprintf_filtered (stream, "Dump of expression @ ");
971 gdb_print_host_address (exp, stream);
972 fprintf_filtered (stream, ", %s:\nExpression: `", note);
973 if (exp->elts[0].opcode != OP_TYPE)
974 print_expression (exp, stream);
976 fputs_filtered ("Type printing not yet supported....", stream);
977 fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
978 exp->language_defn->la_name, exp->nelts,
979 (long) sizeof (union exp_element));
980 fputs_filtered ("\n", stream);
982 for (elt = 0; elt < exp->nelts;)
983 elt = dump_subexp (exp, stream, elt);
984 fputs_filtered ("\n", stream);