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
;
80 print_subexp (exp
, pos
, stream
,
81 (enum precedence
) ((int) myprec
+ assoc
));
82 fputs_filtered (" :: ", stream
);
83 nargs
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
84 (*pos
) += 2 + (nargs
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
86 fputs_filtered (&exp
->elts
[pc
+ 3].string
, stream
);
91 value_print (value_from_longest (exp
->elts
[pc
+ 1].type
,
92 exp
->elts
[pc
+ 2].longconst
),
93 stream
, 0, Val_no_prettyprint
);
98 value_print (value_from_double (exp
->elts
[pc
+ 1].type
,
99 exp
->elts
[pc
+ 2].doubleconst
),
100 stream
, 0, Val_no_prettyprint
);
105 fputs_filtered (SYMBOL_NAME (exp
->elts
[pc
+ 1].symbol
), stream
);
110 fprintf_filtered (stream
, "$%d",
111 longest_to_int (exp
->elts
[pc
+ 1].longconst
));
116 fprintf_filtered (stream
, "$%s",
117 reg_names
[longest_to_int (exp
->elts
[pc
+ 1].longconst
)]);
122 fprintf_filtered (stream
, "%s",
123 longest_to_int (exp
->elts
[pc
+ 1].longconst
)
129 fprintf_filtered (stream
, "$%s",
130 internalvar_name (exp
->elts
[pc
+ 1].internalvar
));
135 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
136 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
137 fputs_filtered (" (", stream
);
138 for (tem
= 0; tem
< nargs
; tem
++)
141 fputs_filtered (", ", stream
);
142 print_subexp (exp
, pos
, stream
, PREC_ABOVE_COMMA
);
144 fputs_filtered (")", stream
);
148 nargs
= longest_to_int (exp
-> elts
[pc
+ 1].longconst
);
149 (*pos
) += 3 + (nargs
+ sizeof (union exp_element
))
150 / sizeof (union exp_element
);
151 /* LA_PRINT_STRING will print using the current repeat count threshold.
152 If necessary, we can temporarily set it to zero, or pass it as an
153 additional parameter to LA_PRINT_STRING. -fnf */
154 LA_PRINT_STRING (stream
, &exp
->elts
[pc
+ 2].string
, nargs
, 0);
158 if ((int) prec
> (int) PREC_COMMA
)
159 fputs_filtered ("(", stream
);
160 /* Print the subexpressions, forcing parentheses
161 around any binary operations within them.
162 This is more parentheses than are strictly necessary,
163 but it looks clearer. */
164 print_subexp (exp
, pos
, stream
, PREC_HYPER
);
165 fputs_filtered (" ? ", stream
);
166 print_subexp (exp
, pos
, stream
, PREC_HYPER
);
167 fputs_filtered (" : ", stream
);
168 print_subexp (exp
, pos
, stream
, PREC_HYPER
);
169 if ((int) prec
> (int) PREC_COMMA
)
170 fputs_filtered (")", stream
);
173 case STRUCTOP_STRUCT
:
174 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
175 (*pos
) += 3 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
176 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
177 fputs_filtered (".", stream
);
178 fputs_filtered (&exp
->elts
[pc
+ 2].string
, stream
);
181 /* Will not occur for Modula-2 */
183 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
184 (*pos
) += 3 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
185 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
186 fputs_filtered ("->", stream
);
187 fputs_filtered (&exp
->elts
[pc
+ 2].string
, stream
);
190 case BINOP_SUBSCRIPT
:
191 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
192 fputs_filtered ("[", stream
);
193 print_subexp (exp
, pos
, stream
, PREC_ABOVE_COMMA
);
194 fputs_filtered ("]", stream
);
197 case UNOP_POSTINCREMENT
:
198 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
199 fputs_filtered ("++", stream
);
202 case UNOP_POSTDECREMENT
:
203 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
204 fputs_filtered ("--", stream
);
209 if ((int) prec
> (int) PREC_PREFIX
)
210 fputs_filtered ("(", stream
);
211 fputs_filtered ("(", stream
);
212 type_print (exp
->elts
[pc
+ 1].type
, "", stream
, 0);
213 fputs_filtered (") ", stream
);
214 print_subexp (exp
, pos
, stream
, PREC_PREFIX
);
215 if ((int) prec
> (int) PREC_PREFIX
)
216 fputs_filtered (")", stream
);
221 if ((int) prec
> (int) PREC_PREFIX
)
222 fputs_filtered ("(", stream
);
223 if (exp
->elts
[pc
+ 1].type
->code
== TYPE_CODE_FUNC
&&
224 exp
->elts
[pc
+ 3].opcode
== OP_LONG
) {
225 /* We have a minimal symbol fn, probably. It's encoded
226 as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
227 Swallow the OP_LONG (including both its opcodes); ignore
228 its type; print the value in the type of the MEMVAL. */
230 val
= value_at_lazy (exp
->elts
[pc
+ 1].type
,
231 (CORE_ADDR
) exp
->elts
[pc
+ 5].longconst
);
232 value_print (val
, stream
, 0, Val_no_prettyprint
);
234 fputs_filtered ("{", stream
);
235 type_print (exp
->elts
[pc
+ 1].type
, "", stream
, 0);
236 fputs_filtered ("} ", stream
);
237 print_subexp (exp
, pos
, stream
, PREC_PREFIX
);
239 if ((int) prec
> (int) PREC_PREFIX
)
240 fputs_filtered (")", stream
);
243 case BINOP_ASSIGN_MODIFY
:
244 opcode
= exp
->elts
[pc
+ 1].opcode
;
246 myprec
= PREC_ASSIGN
;
250 for (tem
= 0; op_print_tab
[tem
].opcode
!= OP_NULL
; tem
++)
251 if (op_print_tab
[tem
].opcode
== opcode
)
253 op_str
= op_print_tab
[tem
].string
;
262 fputs_filtered ("this", stream
);
267 case BINOP_MULTI_SUBSCRIPT
:
269 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
270 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
271 fprintf (stream
, " [");
272 for (tem
= 0; tem
< nargs
; tem
++)
275 fprintf (stream
, ", ");
276 print_subexp (exp
, pos
, stream
, PREC_ABOVE_COMMA
);
278 fprintf (stream
, "]");
283 fprintf(stream
,"VAL(");
284 type_print(exp
->elts
[pc
+1].type
,"",stream
,0);
286 print_subexp(exp
,pos
,stream
,PREC_PREFIX
);
291 print_simple_m2_func("CAP",exp
,pos
,stream
);
295 print_simple_m2_func("CHR",exp
,pos
,stream
);
299 print_simple_m2_func("ORD",exp
,pos
,stream
);
303 print_simple_m2_func("ABS",exp
,pos
,stream
);
307 print_simple_m2_func("FLOAT",exp
,pos
,stream
);
311 print_simple_m2_func("HIGH",exp
,pos
,stream
);
315 print_simple_m2_func("MAX",exp
,pos
,stream
);
319 print_simple_m2_func("MIN",exp
,pos
,stream
);
323 print_simple_m2_func("ODD",exp
,pos
,stream
);
327 print_simple_m2_func("TRUNC",exp
,pos
,stream
);
332 error("print_subexp: Not implemented.");
338 for (tem
= 0; op_print_tab
[tem
].opcode
!= OP_NULL
; tem
++)
339 if (op_print_tab
[tem
].opcode
== opcode
)
341 op_str
= op_print_tab
[tem
].string
;
342 myprec
= op_print_tab
[tem
].precedence
;
343 assoc
= op_print_tab
[tem
].right_assoc
;
348 if ((int) myprec
< (int) prec
)
349 fputs_filtered ("(", stream
);
350 if ((int) opcode
> (int) BINOP_END
)
352 /* Unary prefix operator. */
353 fputs_filtered (op_str
, stream
);
354 print_subexp (exp
, pos
, stream
, PREC_PREFIX
);
358 /* Binary operator. */
359 /* Print left operand.
360 If operator is right-associative,
361 increment precedence for this operand. */
362 print_subexp (exp
, pos
, stream
,
363 (enum precedence
) ((int) myprec
+ assoc
));
364 /* Print the operator itself. */
366 fprintf_filtered (stream
, " %s= ", op_str
);
367 else if (op_str
[0] == ',')
368 fprintf_filtered (stream
, "%s ", op_str
);
370 fprintf_filtered (stream
, " %s ", op_str
);
371 /* Print right operand.
372 If operator is left-associative,
373 increment precedence for this operand. */
374 print_subexp (exp
, pos
, stream
,
375 (enum precedence
) ((int) myprec
+ !assoc
));
378 if ((int) myprec
< (int) prec
)
379 fputs_filtered (")", stream
);
382 /* Print out something of the form <s>(<arg>).
383 This is used to print out some builtin Modula-2
385 FIXME: There is probably some way to get the precedence
386 rules to do this (print a unary operand with parens around it). */
388 print_simple_m2_func(s
,exp
,pos
,stream
)
390 register struct expression
*exp
;
394 fprintf(stream
,"%s(",s
);
395 print_subexp(exp
,pos
,stream
,PREC_PREFIX
);
399 /* Return the operator corresponding to opcode OP as
400 a string. NULL indicates that the opcode was not found in the
401 current language table. */
407 register const struct op_print
*op_print_tab
;
409 op_print_tab
= current_language
->la_op_print_tab
;
410 for (tem
= 0; op_print_tab
[tem
].opcode
!= OP_NULL
; tem
++)
411 if (op_print_tab
[tem
].opcode
== op
)
412 return op_print_tab
[tem
].string
;
This page took 0.067536 seconds and 4 git commands to generate.