1 /* Print in infix form a struct expression.
2 Copyright (C) 1986, 1989, 1991 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "expression.h"
26 #include "parser-defs.h"
28 /* Prototypes for local functions */
31 print_subexp
PARAMS ((struct expression
*, int *, FILE *, enum precedence
));
34 print_simple_m2_func
PARAMS ((char *, struct expression
*, int *, FILE *));
37 print_expression (exp
, stream
)
38 struct expression
*exp
;
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 (exp
, pos
, stream
, prec
)
52 register struct expression
*exp
;
57 register unsigned tem
;
58 register const struct op_print
*op_print_tab
;
61 register char *op_str
;
62 int assign_modify
= 0;
63 enum exp_opcode opcode
;
64 enum precedence myprec
;
65 /* Set to 1 for a right-associative operator. */
69 op_print_tab
= exp
->language_defn
->la_op_print_tab
;
71 opcode
= exp
->elts
[pc
].opcode
;
78 type_print (exp
->elts
[pc
+ 1].type
, "", stream
, 0);
79 fputs_filtered ("::", stream
);
80 nargs
= strlen (&exp
->elts
[pc
+ 2].string
);
81 (*pos
) += 1 + (nargs
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
83 fputs_filtered (&exp
->elts
[pc
+ 2].string
, stream
);
88 value_print (value_from_longest (exp
->elts
[pc
+ 1].type
,
89 exp
->elts
[pc
+ 2].longconst
),
90 stream
, 0, Val_no_prettyprint
);
95 value_print (value_from_double (exp
->elts
[pc
+ 1].type
,
96 exp
->elts
[pc
+ 2].doubleconst
),
97 stream
, 0, Val_no_prettyprint
);
102 fputs_filtered (SYMBOL_NAME (exp
->elts
[pc
+ 1].symbol
), stream
);
107 fprintf_filtered (stream
, "$%d",
108 longest_to_int (exp
->elts
[pc
+ 1].longconst
));
113 fprintf_filtered (stream
, "$%s",
114 reg_names
[longest_to_int (exp
->elts
[pc
+ 1].longconst
)]);
119 fprintf_filtered (stream
, "$%s",
120 internalvar_name (exp
->elts
[pc
+ 1].internalvar
));
125 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
126 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
127 fputs_filtered (" (", stream
);
128 for (tem
= 0; tem
< nargs
; tem
++)
131 fputs_filtered (", ", stream
);
132 print_subexp (exp
, pos
, stream
, PREC_ABOVE_COMMA
);
134 fputs_filtered (")", stream
);
138 nargs
= strlen (&exp
->elts
[pc
+ 1].string
);
139 (*pos
) += 2 + (nargs
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
140 fputs_filtered ("\"", stream
);
141 for (tem
= 0; tem
< nargs
; tem
++)
142 printchar ((&exp
->elts
[pc
+ 1].string
)[tem
], stream
, '"');
143 fputs_filtered ("\"", stream
);
147 if ((int) prec
> (int) PREC_COMMA
)
148 fputs_filtered ("(", stream
);
149 /* Print the subexpressions, forcing parentheses
150 around any binary operations within them.
151 This is more parentheses than are strictly necessary,
152 but it looks clearer. */
153 print_subexp (exp
, pos
, stream
, PREC_HYPER
);
154 fputs_filtered (" ? ", stream
);
155 print_subexp (exp
, pos
, stream
, PREC_HYPER
);
156 fputs_filtered (" : ", stream
);
157 print_subexp (exp
, pos
, stream
, PREC_HYPER
);
158 if ((int) prec
> (int) PREC_COMMA
)
159 fputs_filtered (")", stream
);
162 case STRUCTOP_STRUCT
:
163 tem
= strlen (&exp
->elts
[pc
+ 2].string
);
164 (*pos
) += 3 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
165 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
166 fputs_filtered (".", stream
);
167 if (exp
->elts
[pc
+ 1].type
)
169 type_print (exp
->elts
[pc
+ 1].type
, "", stream
, 0);
170 fputs_filtered ("::", stream
);
172 fputs_filtered (&exp
->elts
[pc
+ 2].string
, stream
);
175 /* Will not occur for Modula-2 */
177 tem
= strlen (&exp
->elts
[pc
+ 2].string
);
178 (*pos
) += 3 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
179 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
180 fputs_filtered ("->", stream
);
181 if (exp
->elts
[pc
+ 1].type
)
183 type_print (exp
->elts
[pc
+ 1].type
, "", stream
, 0);
184 fputs_filtered ("::", stream
);
186 fputs_filtered (&exp
->elts
[pc
+ 2].string
, stream
);
189 case BINOP_SUBSCRIPT
:
190 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
191 fputs_filtered ("[", stream
);
192 print_subexp (exp
, pos
, stream
, PREC_ABOVE_COMMA
);
193 fputs_filtered ("]", stream
);
196 case UNOP_POSTINCREMENT
:
197 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
198 fputs_filtered ("++", stream
);
201 case UNOP_POSTDECREMENT
:
202 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
203 fputs_filtered ("--", stream
);
208 if ((int) prec
> (int) PREC_PREFIX
)
209 fputs_filtered ("(", stream
);
210 fputs_filtered ("(", stream
);
211 type_print (exp
->elts
[pc
+ 1].type
, "", stream
, 0);
212 fputs_filtered (") ", stream
);
213 print_subexp (exp
, pos
, stream
, PREC_PREFIX
);
214 if ((int) prec
> (int) PREC_PREFIX
)
215 fputs_filtered (")", stream
);
220 if ((int) prec
> (int) PREC_PREFIX
)
221 fputs_filtered ("(", stream
);
222 if (exp
->elts
[pc
+ 1].type
->code
== TYPE_CODE_FUNC
&&
223 exp
->elts
[pc
+ 3].opcode
== OP_LONG
) {
224 /* We have a minimal symbol fn, probably. It's encoded
225 as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
226 Swallow the OP_LONG (including both its opcodes); ignore
227 its type; print the value in the type of the MEMVAL. */
229 val
= value_at_lazy (exp
->elts
[pc
+ 1].type
,
230 (CORE_ADDR
) exp
->elts
[pc
+ 5].longconst
);
231 value_print (val
, stream
, 0, Val_no_prettyprint
);
233 fputs_filtered ("{", stream
);
234 type_print (exp
->elts
[pc
+ 1].type
, "", stream
, 0);
235 fputs_filtered ("} ", stream
);
236 print_subexp (exp
, pos
, stream
, PREC_PREFIX
);
238 if ((int) prec
> (int) PREC_PREFIX
)
239 fputs_filtered (")", stream
);
242 case BINOP_ASSIGN_MODIFY
:
243 opcode
= exp
->elts
[pc
+ 1].opcode
;
245 myprec
= PREC_ASSIGN
;
249 for (tem
= 0; op_print_tab
[tem
].opcode
!= OP_NULL
; tem
++)
250 if (op_print_tab
[tem
].opcode
== opcode
)
252 op_str
= op_print_tab
[tem
].string
;
261 fputs_filtered ("this", stream
);
266 case BINOP_MULTI_SUBSCRIPT
:
268 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
269 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
270 fprintf (stream
, " [");
271 for (tem
= 0; tem
< nargs
; tem
++)
274 fprintf (stream
, ", ");
275 print_subexp (exp
, pos
, stream
, PREC_ABOVE_COMMA
);
277 fprintf (stream
, "]");
282 fprintf(stream
,"VAL(");
283 type_print(exp
->elts
[pc
+1].type
,"",stream
,0);
285 print_subexp(exp
,pos
,stream
,PREC_PREFIX
);
290 print_simple_m2_func("CAP",exp
,pos
,stream
);
294 print_simple_m2_func("CHR",exp
,pos
,stream
);
298 print_simple_m2_func("ORD",exp
,pos
,stream
);
302 print_simple_m2_func("ABS",exp
,pos
,stream
);
306 print_simple_m2_func("FLOAT",exp
,pos
,stream
);
310 print_simple_m2_func("HIGH",exp
,pos
,stream
);
314 print_simple_m2_func("MAX",exp
,pos
,stream
);
318 print_simple_m2_func("MIN",exp
,pos
,stream
);
322 print_simple_m2_func("ODD",exp
,pos
,stream
);
326 print_simple_m2_func("TRUNC",exp
,pos
,stream
);
331 error("print_subexp: Not implemented.");
337 for (tem
= 0; op_print_tab
[tem
].opcode
!= OP_NULL
; tem
++)
338 if (op_print_tab
[tem
].opcode
== opcode
)
340 op_str
= op_print_tab
[tem
].string
;
341 myprec
= op_print_tab
[tem
].precedence
;
342 assoc
= op_print_tab
[tem
].right_assoc
;
347 if ((int) myprec
< (int) prec
)
348 fputs_filtered ("(", stream
);
349 if ((int) opcode
> (int) BINOP_END
)
351 /* Unary prefix operator. */
352 fputs_filtered (op_str
, stream
);
353 print_subexp (exp
, pos
, stream
, PREC_PREFIX
);
357 /* Binary operator. */
358 /* Print left operand.
359 If operator is right-associative,
360 increment precedence for this operand. */
361 print_subexp (exp
, pos
, stream
,
362 (enum precedence
) ((int) myprec
+ assoc
));
363 /* Print the operator itself. */
365 fprintf_filtered (stream
, " %s= ", op_str
);
366 else if (op_str
[0] == ',')
367 fprintf_filtered (stream
, "%s ", op_str
);
369 fprintf_filtered (stream
, " %s ", op_str
);
370 /* Print right operand.
371 If operator is left-associative,
372 increment precedence for this operand. */
373 print_subexp (exp
, pos
, stream
,
374 (enum precedence
) ((int) myprec
+ !assoc
));
377 if ((int) myprec
< (int) prec
)
378 fputs_filtered (")", stream
);
381 /* Print out something of the form <s>(<arg>).
382 This is used to print out some builtin Modula-2
384 FIXME: There is probably some way to get the precedence
385 rules to do this (print a unary operand with parens around it). */
387 print_simple_m2_func(s
,exp
,pos
,stream
)
389 register struct expression
*exp
;
393 fprintf(stream
,"%s(",s
);
394 print_subexp(exp
,pos
,stream
,PREC_PREFIX
);
398 /* Return the operator corresponding to opcode OP as
399 a string. NULL indicates that the opcode was not found in the
400 current language table. */
406 register const struct op_print
*op_print_tab
;
408 op_print_tab
= current_language
->la_op_print_tab
;
409 for (tem
= 0; op_print_tab
[tem
].opcode
!= OP_NULL
; tem
++)
410 if (op_print_tab
[tem
].opcode
== op
)
411 return op_print_tab
[tem
].string
;