doc/gdbinv-s.m4.in: remove text on special procedures to continue after
[deliverable/binutils-gdb.git] / gdb / expprint.c
CommitLineData
bd5635a1 1/* Print in infix form a struct expression.
28de880c 2 Copyright (C) 1986, 1989, 1991 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
10147c02 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
10147c02
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
10147c02 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
10147c02
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 19
bd5635a1
RP
20#include "defs.h"
21#include "symtab.h"
28de880c 22#include "gdbtypes.h"
bd5635a1
RP
23#include "expression.h"
24#include "value.h"
28de880c
JG
25#include "language.h"
26#include "parser-defs.h"
bd5635a1 27
28de880c
JG
28/* Prototypes for local functions */
29
30static void
31print_subexp PARAMS ((struct expression *, int *, FILE *, enum precedence));
32
33static void
34print_simple_m2_func PARAMS ((char *, struct expression *, int *, FILE *));
bd5635a1
RP
35
36void
37print_expression (exp, stream)
38 struct expression *exp;
39 FILE *stream;
40{
41 int pc = 0;
42 print_subexp (exp, &pc, stream, PREC_NULL);
43}
44
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. */
49
50static void
51print_subexp (exp, pos, stream, prec)
52 register struct expression *exp;
53 register int *pos;
54 FILE *stream;
55 enum precedence prec;
56{
57 register unsigned tem;
28de880c 58 register const struct op_print *op_print_tab;
bd5635a1
RP
59 register int pc;
60 unsigned nargs;
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. */
66 int assoc;
28de880c 67 value val;
bd5635a1 68
28de880c 69 op_print_tab = exp->language_defn->la_op_print_tab;
bd5635a1
RP
70 pc = (*pos)++;
71 opcode = exp->elts[pc].opcode;
72 switch (opcode)
73 {
28de880c
JG
74 /* Common ops */
75
bd5635a1 76 case OP_SCOPE:
bd5635a1 77 (*pos) += 2;
35fcebce
PB
78 type_print (exp->elts[pc + 1].type, "", stream, 0);
79 fputs_filtered ("::", stream);
bd5635a1
RP
80 nargs = strlen (&exp->elts[pc + 2].string);
81 (*pos) += 1 + (nargs + sizeof (union exp_element)) / sizeof (union exp_element);
82
10147c02 83 fputs_filtered (&exp->elts[pc + 2].string, stream);
bd5635a1
RP
84 return;
85
86 case OP_LONG:
87 (*pos) += 3;
28de880c 88 value_print (value_from_longest (exp->elts[pc + 1].type,
bd5635a1
RP
89 exp->elts[pc + 2].longconst),
90 stream, 0, Val_no_prettyprint);
91 return;
92
93 case OP_DOUBLE:
94 (*pos) += 3;
95 value_print (value_from_double (exp->elts[pc + 1].type,
96 exp->elts[pc + 2].doubleconst),
97 stream, 0, Val_no_prettyprint);
98 return;
99
100 case OP_VAR_VALUE:
101 (*pos) += 2;
10147c02 102 fputs_filtered (SYMBOL_NAME (exp->elts[pc + 1].symbol), stream);
bd5635a1
RP
103 return;
104
105 case OP_LAST:
106 (*pos) += 2;
10147c02
JG
107 fprintf_filtered (stream, "$%d",
108 longest_to_int (exp->elts[pc + 1].longconst));
bd5635a1
RP
109 return;
110
111 case OP_REGISTER:
112 (*pos) += 2;
10147c02 113 fprintf_filtered (stream, "$%s",
35fcebce 114 reg_names[longest_to_int (exp->elts[pc + 1].longconst)]);
bd5635a1
RP
115 return;
116
117 case OP_INTERNALVAR:
118 (*pos) += 2;
10147c02 119 fprintf_filtered (stream, "$%s",
bd5635a1
RP
120 internalvar_name (exp->elts[pc + 1].internalvar));
121 return;
122
123 case OP_FUNCALL:
124 (*pos) += 2;
10147c02 125 nargs = longest_to_int (exp->elts[pc + 1].longconst);
bd5635a1 126 print_subexp (exp, pos, stream, PREC_SUFFIX);
10147c02 127 fputs_filtered (" (", stream);
bd5635a1
RP
128 for (tem = 0; tem < nargs; tem++)
129 {
130 if (tem != 0)
10147c02 131 fputs_filtered (", ", stream);
bd5635a1
RP
132 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
133 }
10147c02 134 fputs_filtered (")", stream);
bd5635a1
RP
135 return;
136
137 case OP_STRING:
138 nargs = strlen (&exp->elts[pc + 1].string);
139 (*pos) += 2 + (nargs + sizeof (union exp_element)) / sizeof (union exp_element);
10147c02 140 fputs_filtered ("\"", stream);
bd5635a1
RP
141 for (tem = 0; tem < nargs; tem++)
142 printchar ((&exp->elts[pc + 1].string)[tem], stream, '"');
10147c02 143 fputs_filtered ("\"", stream);
bd5635a1
RP
144 return;
145
146 case TERNOP_COND:
147 if ((int) prec > (int) PREC_COMMA)
10147c02 148 fputs_filtered ("(", stream);
bd5635a1
RP
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);
10147c02 154 fputs_filtered (" ? ", stream);
bd5635a1 155 print_subexp (exp, pos, stream, PREC_HYPER);
10147c02 156 fputs_filtered (" : ", stream);
bd5635a1
RP
157 print_subexp (exp, pos, stream, PREC_HYPER);
158 if ((int) prec > (int) PREC_COMMA)
10147c02 159 fputs_filtered (")", stream);
bd5635a1
RP
160 return;
161
162 case STRUCTOP_STRUCT:
35fcebce
PB
163 tem = strlen (&exp->elts[pc + 2].string);
164 (*pos) += 3 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
bd5635a1 165 print_subexp (exp, pos, stream, PREC_SUFFIX);
10147c02 166 fputs_filtered (".", stream);
35fcebce
PB
167 if (exp->elts[pc + 1].type)
168 {
169 type_print (exp->elts[pc + 1].type, "", stream, 0);
170 fputs_filtered ("::", stream);
171 }
172 fputs_filtered (&exp->elts[pc + 2].string, stream);
bd5635a1
RP
173 return;
174
28de880c 175 /* Will not occur for Modula-2 */
bd5635a1 176 case STRUCTOP_PTR:
35fcebce
PB
177 tem = strlen (&exp->elts[pc + 2].string);
178 (*pos) += 3 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
bd5635a1 179 print_subexp (exp, pos, stream, PREC_SUFFIX);
10147c02 180 fputs_filtered ("->", stream);
35fcebce
PB
181 if (exp->elts[pc + 1].type)
182 {
183 type_print (exp->elts[pc + 1].type, "", stream, 0);
184 fputs_filtered ("::", stream);
185 }
186 fputs_filtered (&exp->elts[pc + 2].string, stream);
bd5635a1
RP
187 return;
188
189 case BINOP_SUBSCRIPT:
190 print_subexp (exp, pos, stream, PREC_SUFFIX);
10147c02 191 fputs_filtered ("[", stream);
bd5635a1 192 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
10147c02 193 fputs_filtered ("]", stream);
bd5635a1
RP
194 return;
195
196 case UNOP_POSTINCREMENT:
197 print_subexp (exp, pos, stream, PREC_SUFFIX);
10147c02 198 fputs_filtered ("++", stream);
bd5635a1
RP
199 return;
200
201 case UNOP_POSTDECREMENT:
202 print_subexp (exp, pos, stream, PREC_SUFFIX);
10147c02 203 fputs_filtered ("--", stream);
bd5635a1
RP
204 return;
205
206 case UNOP_CAST:
207 (*pos) += 2;
208 if ((int) prec > (int) PREC_PREFIX)
10147c02
JG
209 fputs_filtered ("(", stream);
210 fputs_filtered ("(", stream);
bd5635a1 211 type_print (exp->elts[pc + 1].type, "", stream, 0);
10147c02 212 fputs_filtered (") ", stream);
bd5635a1
RP
213 print_subexp (exp, pos, stream, PREC_PREFIX);
214 if ((int) prec > (int) PREC_PREFIX)
10147c02 215 fputs_filtered (")", stream);
bd5635a1
RP
216 return;
217
218 case UNOP_MEMVAL:
219 (*pos) += 2;
220 if ((int) prec > (int) PREC_PREFIX)
10147c02 221 fputs_filtered ("(", stream);
28de880c
JG
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. */
228 (*pos) += 4;
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);
232 } else {
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);
237 }
bd5635a1 238 if ((int) prec > (int) PREC_PREFIX)
10147c02 239 fputs_filtered (")", stream);
bd5635a1
RP
240 return;
241
242 case BINOP_ASSIGN_MODIFY:
243 opcode = exp->elts[pc + 1].opcode;
244 (*pos) += 2;
245 myprec = PREC_ASSIGN;
246 assoc = 1;
247 assign_modify = 1;
28de880c
JG
248 op_str = "???";
249 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
bd5635a1
RP
250 if (op_print_tab[tem].opcode == opcode)
251 {
252 op_str = op_print_tab[tem].string;
253 break;
254 }
81919cc8 255 break;
bd5635a1 256
28de880c
JG
257 /* C++ ops */
258
bd5635a1
RP
259 case OP_THIS:
260 ++(*pos);
10147c02 261 fputs_filtered ("this", stream);
bd5635a1
RP
262 return;
263
28de880c
JG
264 /* Modula-2 ops */
265
266 case BINOP_MULTI_SUBSCRIPT:
267 (*pos) += 2;
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++)
272 {
273 if (tem != 0)
274 fprintf (stream, ", ");
275 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
276 }
277 fprintf (stream, "]");
278 return;
279
280 case BINOP_VAL:
281 (*pos)+=2;
282 fprintf(stream,"VAL(");
283 type_print(exp->elts[pc+1].type,"",stream,0);
284 fprintf(stream,",");
285 print_subexp(exp,pos,stream,PREC_PREFIX);
286 fprintf(stream,")");
287 return;
288
289 case UNOP_CAP:
290 print_simple_m2_func("CAP",exp,pos,stream);
291 return;
292
293 case UNOP_CHR:
294 print_simple_m2_func("CHR",exp,pos,stream);
295 return;
296
297 case UNOP_ORD:
298 print_simple_m2_func("ORD",exp,pos,stream);
299 return;
300
301 case UNOP_ABS:
302 print_simple_m2_func("ABS",exp,pos,stream);
303 return;
304
305 case UNOP_FLOAT:
306 print_simple_m2_func("FLOAT",exp,pos,stream);
307 return;
308
309 case UNOP_HIGH:
310 print_simple_m2_func("HIGH",exp,pos,stream);
311 return;
312
313 case UNOP_MAX:
314 print_simple_m2_func("MAX",exp,pos,stream);
315 return;
316
317 case UNOP_MIN:
318 print_simple_m2_func("MIN",exp,pos,stream);
319 return;
320
321 case UNOP_ODD:
322 print_simple_m2_func("ODD",exp,pos,stream);
323 return;
324
325 case UNOP_TRUNC:
326 print_simple_m2_func("TRUNC",exp,pos,stream);
327 return;
328
329 case BINOP_INCL:
330 case BINOP_EXCL:
331 error("print_subexp: Not implemented.");
332
333 /* Default ops */
334
bd5635a1 335 default:
28de880c
JG
336 op_str = "???";
337 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
bd5635a1
RP
338 if (op_print_tab[tem].opcode == opcode)
339 {
340 op_str = op_print_tab[tem].string;
341 myprec = op_print_tab[tem].precedence;
342 assoc = op_print_tab[tem].right_assoc;
343 break;
344 }
28de880c 345 }
bd5635a1
RP
346
347 if ((int) myprec < (int) prec)
10147c02 348 fputs_filtered ("(", stream);
bd5635a1
RP
349 if ((int) opcode > (int) BINOP_END)
350 {
351 /* Unary prefix operator. */
10147c02 352 fputs_filtered (op_str, stream);
bd5635a1
RP
353 print_subexp (exp, pos, stream, PREC_PREFIX);
354 }
355 else
356 {
357 /* Binary operator. */
358 /* Print left operand.
359 If operator is right-associative,
360 increment precedence for this operand. */
10147c02
JG
361 print_subexp (exp, pos, stream,
362 (enum precedence) ((int) myprec + assoc));
bd5635a1
RP
363 /* Print the operator itself. */
364 if (assign_modify)
10147c02 365 fprintf_filtered (stream, " %s= ", op_str);
bd5635a1 366 else if (op_str[0] == ',')
10147c02 367 fprintf_filtered (stream, "%s ", op_str);
bd5635a1 368 else
10147c02 369 fprintf_filtered (stream, " %s ", op_str);
bd5635a1
RP
370 /* Print right operand.
371 If operator is left-associative,
372 increment precedence for this operand. */
10147c02
JG
373 print_subexp (exp, pos, stream,
374 (enum precedence) ((int) myprec + !assoc));
bd5635a1 375 }
28de880c 376
bd5635a1 377 if ((int) myprec < (int) prec)
10147c02 378 fputs_filtered (")", stream);
bd5635a1 379}
28de880c
JG
380
381/* Print out something of the form <s>(<arg>).
382 This is used to print out some builtin Modula-2
383 functions.
384 FIXME: There is probably some way to get the precedence
385 rules to do this (print a unary operand with parens around it). */
386static void
387print_simple_m2_func(s,exp,pos,stream)
388 char *s;
389 register struct expression *exp;
390 register int *pos;
391 FILE *stream;
392{
393 fprintf(stream,"%s(",s);
394 print_subexp(exp,pos,stream,PREC_PREFIX);
395 fprintf(stream,")");
396}
397
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. */
401char *
402op_string(op)
403 enum exp_opcode op;
404{
405 int tem;
406 register const struct op_print *op_print_tab;
407
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;
412 return NULL;
413}
This page took 0.095288 seconds and 4 git commands to generate.