1 /* Print in infix form a struct expression.
2 Copyright (C) 1986, 1989, 1991, 2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "expression.h"
27 #include "parser-defs.h"
33 /* Prototypes for local functions */
35 static void print_subexp (struct expression *, int *, struct ui_file *,
39 print_expression (struct expression *exp, struct ui_file *stream)
42 print_subexp (exp, &pc, stream, PREC_NULL);
45 /* Print the subexpression of EXP that starts in position POS, on STREAM.
46 PREC is the precedence of the surrounding operator;
47 if the precedence of the main operator of this subexpression is less,
48 parentheses are needed here. */
51 print_subexp (register struct expression *exp, register int *pos,
52 struct ui_file *stream, enum precedence prec)
54 register unsigned tem;
55 register const struct op_print *op_print_tab;
58 register char *op_str;
59 int assign_modify = 0;
60 enum exp_opcode opcode;
61 enum precedence myprec = PREC_NULL;
62 /* Set to 1 for a right-associative operator. */
67 op_print_tab = exp->language_defn->la_op_print_tab;
69 opcode = exp->elts[pc].opcode;
77 fputs_filtered (type_name_no_tag (exp->elts[pc + 1].type), stream);
78 fputs_filtered ("::", stream);
79 nargs = longest_to_int (exp->elts[pc + 2].longconst);
80 (*pos) += 4 + BYTES_TO_EXP_ELEM (nargs + 1);
81 fputs_filtered (&exp->elts[pc + 3].string, stream);
86 value_print (value_from_longest (exp->elts[pc + 1].type,
87 exp->elts[pc + 2].longconst),
88 stream, 0, Val_no_prettyprint);
93 value_print (value_from_double (exp->elts[pc + 1].type,
94 exp->elts[pc + 2].doubleconst),
95 stream, 0, Val_no_prettyprint);
102 b = exp->elts[pc + 1].block;
104 && BLOCK_FUNCTION (b) != NULL
105 && SYMBOL_SOURCE_NAME (BLOCK_FUNCTION (b)) != NULL)
107 fputs_filtered (SYMBOL_SOURCE_NAME (BLOCK_FUNCTION (b)), stream);
108 fputs_filtered ("::", stream);
110 fputs_filtered (SYMBOL_SOURCE_NAME (exp->elts[pc + 2].symbol), stream);
116 fprintf_filtered (stream, "$%d",
117 longest_to_int (exp->elts[pc + 1].longconst));
122 fprintf_filtered (stream, "$%s",
123 REGISTER_NAME (longest_to_int (exp->elts[pc + 1].longconst)));
128 fprintf_filtered (stream, "%s",
129 longest_to_int (exp->elts[pc + 1].longconst)
135 fprintf_filtered (stream, "$%s",
136 internalvar_name (exp->elts[pc + 1].internalvar));
141 nargs = longest_to_int (exp->elts[pc + 1].longconst);
142 print_subexp (exp, pos, stream, PREC_SUFFIX);
143 fputs_filtered (" (", stream);
144 for (tem = 0; tem < nargs; tem++)
147 fputs_filtered (", ", stream);
148 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
150 fputs_filtered (")", stream);
155 nargs = longest_to_int (exp->elts[pc + 1].longconst);
156 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
157 fputs_filtered (&exp->elts[pc + 2].string, stream);
161 nargs = longest_to_int (exp->elts[pc + 1].longconst);
162 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
163 /* LA_PRINT_STRING will print using the current repeat count threshold.
164 If necessary, we can temporarily set it to zero, or pass it as an
165 additional parameter to LA_PRINT_STRING. -fnf */
166 LA_PRINT_STRING (stream, &exp->elts[pc + 2].string, nargs, 1, 0);
170 nargs = longest_to_int (exp->elts[pc + 1].longconst);
172 += 3 + BYTES_TO_EXP_ELEM ((nargs + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT);
173 fprintf_unfiltered (stream, "B'<unimplemented>'");
178 nargs = longest_to_int (exp->elts[pc + 2].longconst);
179 nargs -= longest_to_int (exp->elts[pc + 1].longconst);
182 if (exp->elts[pc + 4].opcode == OP_LONG
183 && exp->elts[pc + 5].type == builtin_type_char
184 && exp->language_defn->la_language == language_c)
186 /* Attempt to print C character arrays using string syntax.
187 Walk through the args, picking up one character from each
188 of the OP_LONG expression elements. If any array element
189 does not match our expection of what we should find for
190 a simple string, revert back to array printing. Note that
191 the last expression element is an explicit null terminator
192 byte, which doesn't get printed. */
193 tempstr = alloca (nargs);
197 if (exp->elts[pc].opcode != OP_LONG
198 || exp->elts[pc + 1].type != builtin_type_char)
200 /* Not a simple array of char, use regular array printing. */
207 longest_to_int (exp->elts[pc + 2].longconst);
214 LA_PRINT_STRING (stream, tempstr, nargs - 1, 1, 0);
219 int is_chill = exp->language_defn->la_language == language_chill;
220 fputs_filtered (is_chill ? " [" : " {", stream);
221 for (tem = 0; tem < nargs; tem++)
225 fputs_filtered (", ", stream);
227 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
229 fputs_filtered (is_chill ? "]" : "}", stream);
234 tem = longest_to_int (exp->elts[pc + 1].longconst);
235 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
237 if (exp->language_defn->la_language == language_chill)
239 fputs_filtered (".", stream);
240 fputs_filtered (&exp->elts[pc + 2].string, stream);
241 fputs_filtered (exp->elts[*pos].opcode == OP_LABELED ? ", "
247 /* Gcc support both these syntaxes. Unsure which is preferred. */
249 fputs_filtered (&exp->elts[pc + 2].string, stream);
250 fputs_filtered (": ", stream);
252 fputs_filtered (".", stream);
253 fputs_filtered (&exp->elts[pc + 2].string, stream);
254 fputs_filtered ("=", stream);
257 print_subexp (exp, pos, stream, PREC_SUFFIX);
261 if ((int) prec > (int) PREC_COMMA)
262 fputs_filtered ("(", stream);
263 /* Print the subexpressions, forcing parentheses
264 around any binary operations within them.
265 This is more parentheses than are strictly necessary,
266 but it looks clearer. */
267 print_subexp (exp, pos, stream, PREC_HYPER);
268 fputs_filtered (" ? ", stream);
269 print_subexp (exp, pos, stream, PREC_HYPER);
270 fputs_filtered (" : ", stream);
271 print_subexp (exp, pos, stream, PREC_HYPER);
272 if ((int) prec > (int) PREC_COMMA)
273 fputs_filtered (")", stream);
277 case TERNOP_SLICE_COUNT:
278 print_subexp (exp, pos, stream, PREC_SUFFIX);
279 fputs_filtered ("(", stream);
280 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
281 fputs_filtered (opcode == TERNOP_SLICE ? " : " : " UP ", stream);
282 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
283 fputs_filtered (")", stream);
286 case STRUCTOP_STRUCT:
287 tem = longest_to_int (exp->elts[pc + 1].longconst);
288 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
289 print_subexp (exp, pos, stream, PREC_SUFFIX);
290 fputs_filtered (".", stream);
291 fputs_filtered (&exp->elts[pc + 2].string, stream);
294 /* Will not occur for Modula-2 */
296 tem = longest_to_int (exp->elts[pc + 1].longconst);
297 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
298 print_subexp (exp, pos, stream, PREC_SUFFIX);
299 fputs_filtered ("->", stream);
300 fputs_filtered (&exp->elts[pc + 2].string, stream);
303 case BINOP_SUBSCRIPT:
304 print_subexp (exp, pos, stream, PREC_SUFFIX);
305 fputs_filtered ("[", stream);
306 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
307 fputs_filtered ("]", stream);
310 case UNOP_POSTINCREMENT:
311 print_subexp (exp, pos, stream, PREC_SUFFIX);
312 fputs_filtered ("++", stream);
315 case UNOP_POSTDECREMENT:
316 print_subexp (exp, pos, stream, PREC_SUFFIX);
317 fputs_filtered ("--", stream);
322 if ((int) prec > (int) PREC_PREFIX)
323 fputs_filtered ("(", stream);
324 fputs_filtered ("(", stream);
325 type_print (exp->elts[pc + 1].type, "", stream, 0);
326 fputs_filtered (") ", stream);
327 print_subexp (exp, pos, stream, PREC_PREFIX);
328 if ((int) prec > (int) PREC_PREFIX)
329 fputs_filtered (")", stream);
334 if ((int) prec > (int) PREC_PREFIX)
335 fputs_filtered ("(", stream);
336 if (exp->elts[pc + 1].type->code == TYPE_CODE_FUNC &&
337 exp->elts[pc + 3].opcode == OP_LONG)
339 /* We have a minimal symbol fn, probably. It's encoded
340 as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
341 Swallow the OP_LONG (including both its opcodes); ignore
342 its type; print the value in the type of the MEMVAL. */
344 val = value_at_lazy (exp->elts[pc + 1].type,
345 (CORE_ADDR) exp->elts[pc + 5].longconst,
347 value_print (val, stream, 0, Val_no_prettyprint);
351 fputs_filtered ("{", stream);
352 type_print (exp->elts[pc + 1].type, "", stream, 0);
353 fputs_filtered ("} ", stream);
354 print_subexp (exp, pos, stream, PREC_PREFIX);
356 if ((int) prec > (int) PREC_PREFIX)
357 fputs_filtered (")", stream);
360 case BINOP_ASSIGN_MODIFY:
361 opcode = exp->elts[pc + 1].opcode;
363 myprec = PREC_ASSIGN;
367 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
368 if (op_print_tab[tem].opcode == opcode)
370 op_str = op_print_tab[tem].string;
373 if (op_print_tab[tem].opcode != opcode)
374 /* Not found; don't try to keep going because we don't know how
375 to interpret further elements. */
376 error ("Invalid expression");
383 fputs_filtered ("this", stream);
388 case MULTI_SUBSCRIPT:
390 nargs = longest_to_int (exp->elts[pc + 1].longconst);
391 print_subexp (exp, pos, stream, PREC_SUFFIX);
392 fprintf_unfiltered (stream, " [");
393 for (tem = 0; tem < nargs; tem++)
396 fprintf_unfiltered (stream, ", ");
397 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
399 fprintf_unfiltered (stream, "]");
404 fprintf_unfiltered (stream, "VAL(");
405 type_print (exp->elts[pc + 1].type, "", stream, 0);
406 fprintf_unfiltered (stream, ",");
407 print_subexp (exp, pos, stream, PREC_PREFIX);
408 fprintf_unfiltered (stream, ")");
413 error ("print_subexp: Not implemented.");
419 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
420 if (op_print_tab[tem].opcode == opcode)
422 op_str = op_print_tab[tem].string;
423 myprec = op_print_tab[tem].precedence;
424 assoc = op_print_tab[tem].right_assoc;
427 if (op_print_tab[tem].opcode != opcode)
428 /* Not found; don't try to keep going because we don't know how
429 to interpret further elements. For example, this happens
430 if opcode is OP_TYPE. */
431 error ("Invalid expression");
434 /* Note that PREC_BUILTIN will always emit parentheses. */
435 if ((int) myprec < (int) prec)
436 fputs_filtered ("(", stream);
437 if ((int) opcode > (int) BINOP_END)
441 /* Unary postfix operator. */
442 print_subexp (exp, pos, stream, PREC_SUFFIX);
443 fputs_filtered (op_str, stream);
447 /* Unary prefix operator. */
448 fputs_filtered (op_str, stream);
449 if (myprec == PREC_BUILTIN_FUNCTION)
450 fputs_filtered ("(", stream);
451 print_subexp (exp, pos, stream, PREC_PREFIX);
452 if (myprec == PREC_BUILTIN_FUNCTION)
453 fputs_filtered (")", stream);
458 /* Binary operator. */
459 /* Print left operand.
460 If operator is right-associative,
461 increment precedence for this operand. */
462 print_subexp (exp, pos, stream,
463 (enum precedence) ((int) myprec + assoc));
464 /* Print the operator itself. */
466 fprintf_filtered (stream, " %s= ", op_str);
467 else if (op_str[0] == ',')
468 fprintf_filtered (stream, "%s ", op_str);
470 fprintf_filtered (stream, " %s ", op_str);
471 /* Print right operand.
472 If operator is left-associative,
473 increment precedence for this operand. */
474 print_subexp (exp, pos, stream,
475 (enum precedence) ((int) myprec + !assoc));
478 if ((int) myprec < (int) prec)
479 fputs_filtered (")", stream);
482 /* Return the operator corresponding to opcode OP as
483 a string. NULL indicates that the opcode was not found in the
484 current language table. */
486 op_string (enum exp_opcode op)
489 register const struct op_print *op_print_tab;
491 op_print_tab = current_language->la_op_print_tab;
492 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
493 if (op_print_tab[tem].opcode == op)
494 return op_print_tab[tem].string;
498 /* Support for dumping the raw data from expressions in a human readable
501 static char *op_name (int opcode);
512 sprintf (buf, "<unknown %d>", opcode);
533 case BINOP_LOGICAL_AND:
534 return "BINOP_LOGICAL_AND";
535 case BINOP_LOGICAL_OR:
536 return "BINOP_LOGICAL_OR";
537 case BINOP_BITWISE_AND:
538 return "BINOP_BITWISE_AND";
539 case BINOP_BITWISE_IOR:
540 return "BINOP_BITWISE_IOR";
541 case BINOP_BITWISE_XOR:
542 return "BINOP_BITWISE_XOR";
544 return "BINOP_EQUAL";
546 return "BINOP_NOTEQUAL";
556 return "BINOP_REPEAT";
558 return "BINOP_ASSIGN";
560 return "BINOP_COMMA";
561 case BINOP_SUBSCRIPT:
562 return "BINOP_SUBSCRIPT";
563 case MULTI_SUBSCRIPT:
564 return "MULTI_SUBSCRIPT";
571 case STRUCTOP_MEMBER:
572 return "STRUCTOP_MEMBER";
574 return "STRUCTOP_MPTR";
576 return "BINOP_INTDIV";
577 case BINOP_ASSIGN_MODIFY:
578 return "BINOP_ASSIGN_MODIFY";
586 return "BINOP_CONCAT";
588 return "BINOP_RANGE";
592 return "TERNOP_COND";
594 return "TERNOP_SLICE";
595 case TERNOP_SLICE_COUNT:
596 return "TERNOP_SLICE_COUNT";
602 return "OP_VAR_VALUE";
606 return "OP_REGISTER";
608 return "OP_INTERNALVAR";
614 return "OP_BITSTRING";
620 return "UNOP_MEMVAL";
623 case UNOP_LOGICAL_NOT:
624 return "UNOP_LOGICAL_NOT";
625 case UNOP_COMPLEMENT:
626 return "UNOP_COMPLEMENT";
631 case UNOP_PREINCREMENT:
632 return "UNOP_PREINCREMENT";
633 case UNOP_POSTINCREMENT:
634 return "UNOP_POSTINCREMENT";
635 case UNOP_PREDECREMENT:
636 return "UNOP_PREDECREMENT";
637 case UNOP_POSTDECREMENT:
638 return "UNOP_POSTDECREMENT";
640 return "UNOP_SIZEOF";
646 return "UNOP_LENGTH";
672 return "OP_M2_STRING";
673 case STRUCTOP_STRUCT:
674 return "STRUCTOP_STRUCT";
676 return "STRUCTOP_PTR";
689 dump_prefix_expression (struct expression *exp, struct ui_file *stream,
697 fprintf_filtered (stream, "Dump of expression @ ");
698 gdb_print_host_address (exp, stream);
699 fprintf_filtered (stream, ", %s:\nExpression: `", note);
700 if (exp->elts[0].opcode != OP_TYPE)
701 print_expression (exp, stream);
703 fprintf_filtered (stream, "Type printing not yet supported....");
704 fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
705 exp->language_defn->la_name, exp->nelts,
706 (long) sizeof (union exp_element));
707 fprintf_filtered (stream, "\t%5s %20s %16s %s\n", "Index", "Opcode",
708 "Hex Value", "String Value");
709 for (elt = 0; elt < exp->nelts; elt++)
711 fprintf_filtered (stream, "\t%5d ", elt);
712 opcode_name = op_name (exp->elts[elt].opcode);
714 fprintf_filtered (stream, "%20s ", opcode_name);
715 print_longest (stream, 'd', 0, exp->elts[elt].longconst);
716 fprintf_filtered (stream, " ");
718 for (eltscan = (char *) &exp->elts[elt],
719 eltsize = sizeof (union exp_element);
723 fprintf_filtered (stream, "%c",
724 isprint (*eltscan) ? (*eltscan & 0xFF) : '.');
726 fprintf_filtered (stream, "\n");
730 static int dump_subexp (struct expression *exp, struct ui_file *stream,
734 dump_subexp (struct expression *exp, struct ui_file *stream, int elt)
736 static int indent = 0;
739 fprintf_filtered (stream, "\n");
740 fprintf_filtered (stream, "\t%5d ", elt);
742 for (i = 1; i <= indent; i++)
743 fprintf_filtered (stream, " ");
746 fprintf_filtered (stream, "%-20s ", op_name (exp->elts[elt].opcode));
748 switch (exp->elts[elt++].opcode)
752 case TERNOP_SLICE_COUNT:
753 elt = dump_subexp (exp, stream, elt);
762 case BINOP_LOGICAL_AND:
763 case BINOP_LOGICAL_OR:
764 case BINOP_BITWISE_AND:
765 case BINOP_BITWISE_IOR:
766 case BINOP_BITWISE_XOR:
776 case BINOP_SUBSCRIPT:
781 case BINOP_ASSIGN_MODIFY:
789 elt = dump_subexp (exp, stream, elt);
791 case UNOP_LOGICAL_NOT:
792 case UNOP_COMPLEMENT:
795 case UNOP_PREINCREMENT:
796 case UNOP_POSTINCREMENT:
797 case UNOP_PREDECREMENT:
798 case UNOP_POSTDECREMENT:
817 elt = dump_subexp (exp, stream, elt);
820 fprintf_filtered (stream, "Type @");
821 gdb_print_host_address (exp->elts[elt].type, stream);
822 fprintf_filtered (stream, " (");
823 type_print (exp->elts[elt].type, NULL, stream, 0);
824 fprintf_filtered (stream, "), value %ld (0x%lx)",
825 (long) exp->elts[elt + 1].longconst,
826 (long) exp->elts[elt + 1].longconst);
830 fprintf_filtered (stream, "Type @");
831 gdb_print_host_address (exp->elts[elt].type, stream);
832 fprintf_filtered (stream, " (");
833 type_print (exp->elts[elt].type, NULL, stream, 0);
834 fprintf_filtered (stream, "), value %g",
835 (double) exp->elts[elt + 1].doubleconst);
839 fprintf_filtered (stream, "Block @");
840 gdb_print_host_address (exp->elts[elt].block, stream);
841 fprintf_filtered (stream, ", symbol @");
842 gdb_print_host_address (exp->elts[elt + 1].symbol, stream);
843 fprintf_filtered (stream, " (%s)",
844 SYMBOL_NAME (exp->elts[elt + 1].symbol));
848 fprintf_filtered (stream, "History element %ld",
849 (long) exp->elts[elt].longconst);
853 fprintf_filtered (stream, "Register %ld",
854 (long) exp->elts[elt].longconst);
858 fprintf_filtered (stream, "Internal var @");
859 gdb_print_host_address (exp->elts[elt].internalvar, stream);
860 fprintf_filtered (stream, " (%s)",
861 exp->elts[elt].internalvar->name);
868 nargs = longest_to_int (exp->elts[elt].longconst);
870 fprintf_filtered (stream, "Number of args: %d", nargs);
873 for (i = 1; i <= nargs + 1; i++)
874 elt = dump_subexp (exp, stream, elt);
882 lower = longest_to_int (exp->elts[elt].longconst);
883 upper = longest_to_int (exp->elts[elt + 1].longconst);
885 fprintf_filtered (stream, "Bounds [%d:%d]", lower, upper);
888 for (i = 1; i <= upper - lower + 1; i++)
889 elt = dump_subexp (exp, stream, elt);
894 fprintf_filtered (stream, "Type @");
895 gdb_print_host_address (exp->elts[elt].type, stream);
896 fprintf_filtered (stream, " (");
897 type_print (exp->elts[elt].type, NULL, stream, 0);
898 fprintf_filtered (stream, ")");
899 elt = dump_subexp (exp, stream, elt + 2);
902 fprintf_filtered (stream, "Type @");
903 gdb_print_host_address (exp->elts[elt].type, stream);
904 fprintf_filtered (stream, " (");
905 type_print (exp->elts[elt].type, NULL, stream, 0);
906 fprintf_filtered (stream, ")");
909 case STRUCTOP_STRUCT:
915 len = longest_to_int (exp->elts[elt].longconst);
916 elem_name = &exp->elts[elt + 1].string;
918 fprintf_filtered (stream, "Element name: `%.*s'", len, elem_name);
919 elt = dump_subexp (exp, stream, elt + 3 + BYTES_TO_EXP_ELEM (len + 1));
927 fprintf_filtered (stream, "Type @");
928 gdb_print_host_address (exp->elts[elt].type, stream);
929 fprintf_filtered (stream, " (");
930 type_print (exp->elts[elt].type, NULL, stream, 0);
931 fprintf_filtered (stream, ") ");
933 len = longest_to_int (exp->elts[elt + 1].longconst);
934 elem_name = &exp->elts[elt + 2].string;
936 fprintf_filtered (stream, "Field name: `%.*s'", len, elem_name);
937 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
942 case STRUCTOP_MEMBER:
944 case MULTI_SUBSCRIPT:
945 case OP_F77_UNDETERMINED_ARGLIST:
955 fprintf_filtered (stream, "Unknown format");
964 dump_postfix_expression (struct expression *exp, struct ui_file *stream,
969 fprintf_filtered (stream, "Dump of expression @ ");
970 gdb_print_host_address (exp, stream);
971 fprintf_filtered (stream, ", %s:\nExpression: `", note);
972 if (exp->elts[0].opcode != OP_TYPE)
973 print_expression (exp, stream);
975 fputs_filtered ("Type printing not yet supported....", stream);
976 fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
977 exp->language_defn->la_name, exp->nelts,
978 (long) sizeof (union exp_element));
979 fputs_filtered ("\n", stream);
981 for (elt = 0; elt < exp->nelts;)
982 elt = dump_subexp (exp, stream, elt);
983 fputs_filtered ("\n", stream);