Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Print in infix form a struct expression. |
1bac305b | 2 | |
b6ba6518 | 3 | Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
1bac305b | 4 | 1998, 1999, 2000, 2003 Free Software Foundation, Inc. |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b JM |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
24 | #include "symtab.h" | |
25 | #include "gdbtypes.h" | |
26 | #include "expression.h" | |
27 | #include "value.h" | |
28 | #include "language.h" | |
29 | #include "parser-defs.h" | |
eb8bc282 | 30 | #include "user-regs.h" /* For user_reg_map_regnum_to_name. */ |
82eeeb94 AF |
31 | #include "target.h" |
32 | #include "gdb_string.h" | |
fe898f56 | 33 | #include "block.h" |
c906108c SS |
34 | |
35 | #ifdef HAVE_CTYPE_H | |
36 | #include <ctype.h> | |
37 | #endif | |
38 | ||
39 | /* Prototypes for local functions */ | |
40 | ||
d9fcf2fb JM |
41 | static void print_subexp (struct expression *, int *, struct ui_file *, |
42 | enum precedence); | |
c906108c SS |
43 | |
44 | void | |
fba45db2 | 45 | print_expression (struct expression *exp, struct ui_file *stream) |
c906108c SS |
46 | { |
47 | int pc = 0; | |
48 | print_subexp (exp, &pc, stream, PREC_NULL); | |
49 | } | |
50 | ||
51 | /* Print the subexpression of EXP that starts in position POS, on STREAM. | |
52 | PREC is the precedence of the surrounding operator; | |
53 | if the precedence of the main operator of this subexpression is less, | |
54 | parentheses are needed here. */ | |
55 | ||
56 | static void | |
fba45db2 KB |
57 | print_subexp (register struct expression *exp, register int *pos, |
58 | struct ui_file *stream, enum precedence prec) | |
c906108c SS |
59 | { |
60 | register unsigned tem; | |
61 | register const struct op_print *op_print_tab; | |
62 | register int pc; | |
63 | unsigned nargs; | |
64 | register char *op_str; | |
65 | int assign_modify = 0; | |
66 | enum exp_opcode opcode; | |
67 | enum precedence myprec = PREC_NULL; | |
68 | /* Set to 1 for a right-associative operator. */ | |
69 | int assoc = 0; | |
3d6d86c6 | 70 | struct value *val; |
c906108c SS |
71 | char *tempstr = NULL; |
72 | ||
73 | op_print_tab = exp->language_defn->la_op_print_tab; | |
74 | pc = (*pos)++; | |
75 | opcode = exp->elts[pc].opcode; | |
76 | switch (opcode) | |
77 | { | |
c5aa993b | 78 | /* Common ops */ |
c906108c SS |
79 | |
80 | case OP_SCOPE: | |
81 | myprec = PREC_PREFIX; | |
82 | assoc = 0; | |
83 | fputs_filtered (type_name_no_tag (exp->elts[pc + 1].type), stream); | |
84 | fputs_filtered ("::", stream); | |
85 | nargs = longest_to_int (exp->elts[pc + 2].longconst); | |
86 | (*pos) += 4 + BYTES_TO_EXP_ELEM (nargs + 1); | |
87 | fputs_filtered (&exp->elts[pc + 3].string, stream); | |
88 | return; | |
89 | ||
90 | case OP_LONG: | |
91 | (*pos) += 3; | |
92 | value_print (value_from_longest (exp->elts[pc + 1].type, | |
93 | exp->elts[pc + 2].longconst), | |
94 | stream, 0, Val_no_prettyprint); | |
95 | return; | |
96 | ||
97 | case OP_DOUBLE: | |
98 | (*pos) += 3; | |
99 | value_print (value_from_double (exp->elts[pc + 1].type, | |
100 | exp->elts[pc + 2].doubleconst), | |
101 | stream, 0, Val_no_prettyprint); | |
102 | return; | |
103 | ||
104 | case OP_VAR_VALUE: | |
105 | { | |
106 | struct block *b; | |
107 | (*pos) += 3; | |
108 | b = exp->elts[pc + 1].block; | |
109 | if (b != NULL | |
110 | && BLOCK_FUNCTION (b) != NULL | |
de5ad195 | 111 | && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)) != NULL) |
c906108c | 112 | { |
de5ad195 | 113 | fputs_filtered (SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)), stream); |
c906108c SS |
114 | fputs_filtered ("::", stream); |
115 | } | |
de5ad195 | 116 | fputs_filtered (SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol), stream); |
c906108c SS |
117 | } |
118 | return; | |
119 | ||
120 | case OP_LAST: | |
121 | (*pos) += 2; | |
122 | fprintf_filtered (stream, "$%d", | |
123 | longest_to_int (exp->elts[pc + 1].longconst)); | |
124 | return; | |
125 | ||
126 | case OP_REGISTER: | |
e36180d7 AC |
127 | { |
128 | int regnum = longest_to_int (exp->elts[pc + 1].longconst); | |
eb8bc282 AC |
129 | const char *name = user_reg_map_regnum_to_name (current_gdbarch, |
130 | regnum); | |
e36180d7 | 131 | (*pos) += 2; |
eb8bc282 | 132 | fprintf_filtered (stream, "$%s", name); |
e36180d7 AC |
133 | return; |
134 | } | |
c906108c SS |
135 | |
136 | case OP_BOOL: | |
137 | (*pos) += 2; | |
138 | fprintf_filtered (stream, "%s", | |
139 | longest_to_int (exp->elts[pc + 1].longconst) | |
140 | ? "TRUE" : "FALSE"); | |
141 | return; | |
142 | ||
143 | case OP_INTERNALVAR: | |
144 | (*pos) += 2; | |
145 | fprintf_filtered (stream, "$%s", | |
c5aa993b | 146 | internalvar_name (exp->elts[pc + 1].internalvar)); |
c906108c SS |
147 | return; |
148 | ||
149 | case OP_FUNCALL: | |
150 | (*pos) += 2; | |
151 | nargs = longest_to_int (exp->elts[pc + 1].longconst); | |
152 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
153 | fputs_filtered (" (", stream); | |
154 | for (tem = 0; tem < nargs; tem++) | |
155 | { | |
156 | if (tem != 0) | |
157 | fputs_filtered (", ", stream); | |
158 | print_subexp (exp, pos, stream, PREC_ABOVE_COMMA); | |
159 | } | |
160 | fputs_filtered (")", stream); | |
161 | return; | |
162 | ||
163 | case OP_NAME: | |
164 | case OP_EXPRSTRING: | |
c5aa993b | 165 | nargs = longest_to_int (exp->elts[pc + 1].longconst); |
c906108c SS |
166 | (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1); |
167 | fputs_filtered (&exp->elts[pc + 2].string, stream); | |
168 | return; | |
169 | ||
170 | case OP_STRING: | |
c5aa993b | 171 | nargs = longest_to_int (exp->elts[pc + 1].longconst); |
c906108c SS |
172 | (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1); |
173 | /* LA_PRINT_STRING will print using the current repeat count threshold. | |
c5aa993b JM |
174 | If necessary, we can temporarily set it to zero, or pass it as an |
175 | additional parameter to LA_PRINT_STRING. -fnf */ | |
c906108c SS |
176 | LA_PRINT_STRING (stream, &exp->elts[pc + 2].string, nargs, 1, 0); |
177 | return; | |
178 | ||
179 | case OP_BITSTRING: | |
c5aa993b | 180 | nargs = longest_to_int (exp->elts[pc + 1].longconst); |
c906108c SS |
181 | (*pos) |
182 | += 3 + BYTES_TO_EXP_ELEM ((nargs + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT); | |
183 | fprintf_unfiltered (stream, "B'<unimplemented>'"); | |
184 | return; | |
185 | ||
82eeeb94 AF |
186 | case OP_OBJC_NSSTRING: /* Objective-C Foundation Class NSString constant. */ |
187 | nargs = longest_to_int (exp->elts[pc + 1].longconst); | |
188 | (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1); | |
189 | fputs_filtered ("@\"", stream); | |
190 | LA_PRINT_STRING (stream, &exp->elts[pc + 2].string, nargs, 1, 0); | |
191 | fputs_filtered ("\"", stream); | |
192 | return; | |
193 | ||
194 | case OP_OBJC_MSGCALL: | |
195 | { /* Objective C message (method) call. */ | |
196 | char *selector; | |
197 | (*pos) += 3; | |
198 | nargs = longest_to_int (exp->elts[pc + 2].longconst); | |
199 | fprintf_unfiltered (stream, "["); | |
200 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
201 | if (0 == target_read_string (exp->elts[pc + 1].longconst, | |
202 | &selector, 1024, NULL)) | |
203 | { | |
204 | error ("bad selector"); | |
205 | return; | |
206 | } | |
207 | if (nargs) | |
208 | { | |
209 | char *s, *nextS; | |
210 | s = alloca (strlen (selector) + 1); | |
211 | strcpy (s, selector); | |
212 | for (tem = 0; tem < nargs; tem++) | |
213 | { | |
214 | nextS = strchr (s, ':'); | |
215 | *nextS = '\0'; | |
216 | fprintf_unfiltered (stream, " %s: ", s); | |
217 | s = nextS + 1; | |
218 | print_subexp (exp, pos, stream, PREC_ABOVE_COMMA); | |
219 | } | |
220 | } | |
221 | else | |
222 | { | |
223 | fprintf_unfiltered (stream, " %s", selector); | |
224 | } | |
225 | fprintf_unfiltered (stream, "]"); | |
226 | /* "selector" was malloc'd by target_read_string. Free it. */ | |
4ef3f3be | 227 | xfree (selector); |
82eeeb94 AF |
228 | return; |
229 | } | |
230 | ||
c906108c SS |
231 | case OP_ARRAY: |
232 | (*pos) += 3; | |
233 | nargs = longest_to_int (exp->elts[pc + 2].longconst); | |
234 | nargs -= longest_to_int (exp->elts[pc + 1].longconst); | |
235 | nargs++; | |
236 | tem = 0; | |
237 | if (exp->elts[pc + 4].opcode == OP_LONG | |
238 | && exp->elts[pc + 5].type == builtin_type_char | |
239 | && exp->language_defn->la_language == language_c) | |
240 | { | |
241 | /* Attempt to print C character arrays using string syntax. | |
242 | Walk through the args, picking up one character from each | |
243 | of the OP_LONG expression elements. If any array element | |
244 | does not match our expection of what we should find for | |
245 | a simple string, revert back to array printing. Note that | |
246 | the last expression element is an explicit null terminator | |
247 | byte, which doesn't get printed. */ | |
248 | tempstr = alloca (nargs); | |
249 | pc += 4; | |
250 | while (tem < nargs) | |
251 | { | |
252 | if (exp->elts[pc].opcode != OP_LONG | |
253 | || exp->elts[pc + 1].type != builtin_type_char) | |
254 | { | |
255 | /* Not a simple array of char, use regular array printing. */ | |
256 | tem = 0; | |
257 | break; | |
258 | } | |
259 | else | |
260 | { | |
261 | tempstr[tem++] = | |
262 | longest_to_int (exp->elts[pc + 2].longconst); | |
263 | pc += 4; | |
264 | } | |
265 | } | |
266 | } | |
267 | if (tem > 0) | |
268 | { | |
269 | LA_PRINT_STRING (stream, tempstr, nargs - 1, 1, 0); | |
270 | (*pos) = pc; | |
271 | } | |
272 | else | |
273 | { | |
db034ac5 | 274 | fputs_filtered (" {", stream); |
c906108c SS |
275 | for (tem = 0; tem < nargs; tem++) |
276 | { | |
277 | if (tem != 0) | |
278 | { | |
279 | fputs_filtered (", ", stream); | |
280 | } | |
281 | print_subexp (exp, pos, stream, PREC_ABOVE_COMMA); | |
282 | } | |
db034ac5 | 283 | fputs_filtered ("}", stream); |
c906108c SS |
284 | } |
285 | return; | |
286 | ||
287 | case OP_LABELED: | |
288 | tem = longest_to_int (exp->elts[pc + 1].longconst); | |
289 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
1b831c93 | 290 | /* Gcc support both these syntaxes. Unsure which is preferred. */ |
c906108c | 291 | #if 1 |
1b831c93 AC |
292 | fputs_filtered (&exp->elts[pc + 2].string, stream); |
293 | fputs_filtered (": ", stream); | |
c906108c | 294 | #else |
1b831c93 AC |
295 | fputs_filtered (".", stream); |
296 | fputs_filtered (&exp->elts[pc + 2].string, stream); | |
297 | fputs_filtered ("=", stream); | |
c906108c | 298 | #endif |
c906108c SS |
299 | print_subexp (exp, pos, stream, PREC_SUFFIX); |
300 | return; | |
301 | ||
302 | case TERNOP_COND: | |
303 | if ((int) prec > (int) PREC_COMMA) | |
304 | fputs_filtered ("(", stream); | |
305 | /* Print the subexpressions, forcing parentheses | |
c5aa993b JM |
306 | around any binary operations within them. |
307 | This is more parentheses than are strictly necessary, | |
308 | but it looks clearer. */ | |
c906108c SS |
309 | print_subexp (exp, pos, stream, PREC_HYPER); |
310 | fputs_filtered (" ? ", stream); | |
311 | print_subexp (exp, pos, stream, PREC_HYPER); | |
312 | fputs_filtered (" : ", stream); | |
313 | print_subexp (exp, pos, stream, PREC_HYPER); | |
314 | if ((int) prec > (int) PREC_COMMA) | |
315 | fputs_filtered (")", stream); | |
316 | return; | |
317 | ||
318 | case TERNOP_SLICE: | |
319 | case TERNOP_SLICE_COUNT: | |
320 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
321 | fputs_filtered ("(", stream); | |
322 | print_subexp (exp, pos, stream, PREC_ABOVE_COMMA); | |
323 | fputs_filtered (opcode == TERNOP_SLICE ? " : " : " UP ", stream); | |
324 | print_subexp (exp, pos, stream, PREC_ABOVE_COMMA); | |
325 | fputs_filtered (")", stream); | |
326 | return; | |
327 | ||
328 | case STRUCTOP_STRUCT: | |
329 | tem = longest_to_int (exp->elts[pc + 1].longconst); | |
330 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
331 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
332 | fputs_filtered (".", stream); | |
333 | fputs_filtered (&exp->elts[pc + 2].string, stream); | |
334 | return; | |
335 | ||
c5aa993b | 336 | /* Will not occur for Modula-2 */ |
c906108c SS |
337 | case STRUCTOP_PTR: |
338 | tem = longest_to_int (exp->elts[pc + 1].longconst); | |
339 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
340 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
341 | fputs_filtered ("->", stream); | |
342 | fputs_filtered (&exp->elts[pc + 2].string, stream); | |
343 | return; | |
344 | ||
345 | case BINOP_SUBSCRIPT: | |
346 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
347 | fputs_filtered ("[", stream); | |
348 | print_subexp (exp, pos, stream, PREC_ABOVE_COMMA); | |
349 | fputs_filtered ("]", stream); | |
350 | return; | |
351 | ||
352 | case UNOP_POSTINCREMENT: | |
353 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
354 | fputs_filtered ("++", stream); | |
355 | return; | |
356 | ||
357 | case UNOP_POSTDECREMENT: | |
358 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
359 | fputs_filtered ("--", stream); | |
360 | return; | |
361 | ||
362 | case UNOP_CAST: | |
363 | (*pos) += 2; | |
364 | if ((int) prec > (int) PREC_PREFIX) | |
c5aa993b | 365 | fputs_filtered ("(", stream); |
c906108c SS |
366 | fputs_filtered ("(", stream); |
367 | type_print (exp->elts[pc + 1].type, "", stream, 0); | |
368 | fputs_filtered (") ", stream); | |
369 | print_subexp (exp, pos, stream, PREC_PREFIX); | |
370 | if ((int) prec > (int) PREC_PREFIX) | |
c5aa993b | 371 | fputs_filtered (")", stream); |
c906108c SS |
372 | return; |
373 | ||
374 | case UNOP_MEMVAL: | |
375 | (*pos) += 2; | |
376 | if ((int) prec > (int) PREC_PREFIX) | |
c5aa993b | 377 | fputs_filtered ("(", stream); |
0004e5a2 | 378 | if (TYPE_CODE (exp->elts[pc + 1].type) == TYPE_CODE_FUNC && |
c5aa993b JM |
379 | exp->elts[pc + 3].opcode == OP_LONG) |
380 | { | |
381 | /* We have a minimal symbol fn, probably. It's encoded | |
382 | as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address). | |
383 | Swallow the OP_LONG (including both its opcodes); ignore | |
384 | its type; print the value in the type of the MEMVAL. */ | |
385 | (*pos) += 4; | |
386 | val = value_at_lazy (exp->elts[pc + 1].type, | |
387 | (CORE_ADDR) exp->elts[pc + 5].longconst, | |
388 | NULL); | |
389 | value_print (val, stream, 0, Val_no_prettyprint); | |
390 | } | |
391 | else | |
392 | { | |
393 | fputs_filtered ("{", stream); | |
394 | type_print (exp->elts[pc + 1].type, "", stream, 0); | |
395 | fputs_filtered ("} ", stream); | |
396 | print_subexp (exp, pos, stream, PREC_PREFIX); | |
397 | } | |
c906108c | 398 | if ((int) prec > (int) PREC_PREFIX) |
c5aa993b | 399 | fputs_filtered (")", stream); |
c906108c SS |
400 | return; |
401 | ||
402 | case BINOP_ASSIGN_MODIFY: | |
403 | opcode = exp->elts[pc + 1].opcode; | |
404 | (*pos) += 2; | |
405 | myprec = PREC_ASSIGN; | |
406 | assoc = 1; | |
407 | assign_modify = 1; | |
408 | op_str = "???"; | |
409 | for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++) | |
410 | if (op_print_tab[tem].opcode == opcode) | |
411 | { | |
412 | op_str = op_print_tab[tem].string; | |
413 | break; | |
414 | } | |
415 | if (op_print_tab[tem].opcode != opcode) | |
416 | /* Not found; don't try to keep going because we don't know how | |
417 | to interpret further elements. */ | |
418 | error ("Invalid expression"); | |
419 | break; | |
420 | ||
c5aa993b | 421 | /* C++ ops */ |
c906108c SS |
422 | |
423 | case OP_THIS: | |
424 | ++(*pos); | |
425 | fputs_filtered ("this", stream); | |
426 | return; | |
427 | ||
82eeeb94 AF |
428 | /* Objective-C ops */ |
429 | ||
430 | case OP_OBJC_SELF: | |
431 | ++(*pos); | |
432 | fputs_filtered ("self", stream); /* The ObjC equivalent of "this". */ | |
433 | return; | |
434 | ||
c5aa993b | 435 | /* Modula-2 ops */ |
c906108c SS |
436 | |
437 | case MULTI_SUBSCRIPT: | |
438 | (*pos) += 2; | |
439 | nargs = longest_to_int (exp->elts[pc + 1].longconst); | |
440 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
441 | fprintf_unfiltered (stream, " ["); | |
442 | for (tem = 0; tem < nargs; tem++) | |
443 | { | |
444 | if (tem != 0) | |
445 | fprintf_unfiltered (stream, ", "); | |
446 | print_subexp (exp, pos, stream, PREC_ABOVE_COMMA); | |
447 | } | |
448 | fprintf_unfiltered (stream, "]"); | |
449 | return; | |
450 | ||
451 | case BINOP_VAL: | |
c5aa993b JM |
452 | (*pos) += 2; |
453 | fprintf_unfiltered (stream, "VAL("); | |
454 | type_print (exp->elts[pc + 1].type, "", stream, 0); | |
455 | fprintf_unfiltered (stream, ","); | |
456 | print_subexp (exp, pos, stream, PREC_PREFIX); | |
457 | fprintf_unfiltered (stream, ")"); | |
c906108c | 458 | return; |
c5aa993b | 459 | |
c906108c SS |
460 | case BINOP_INCL: |
461 | case BINOP_EXCL: | |
c5aa993b | 462 | error ("print_subexp: Not implemented."); |
c906108c | 463 | |
c5aa993b | 464 | /* Default ops */ |
c906108c SS |
465 | |
466 | default: | |
467 | op_str = "???"; | |
468 | for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++) | |
469 | if (op_print_tab[tem].opcode == opcode) | |
470 | { | |
471 | op_str = op_print_tab[tem].string; | |
472 | myprec = op_print_tab[tem].precedence; | |
473 | assoc = op_print_tab[tem].right_assoc; | |
474 | break; | |
475 | } | |
476 | if (op_print_tab[tem].opcode != opcode) | |
477 | /* Not found; don't try to keep going because we don't know how | |
478 | to interpret further elements. For example, this happens | |
479 | if opcode is OP_TYPE. */ | |
480 | error ("Invalid expression"); | |
c5aa993b | 481 | } |
c906108c SS |
482 | |
483 | /* Note that PREC_BUILTIN will always emit parentheses. */ | |
484 | if ((int) myprec < (int) prec) | |
485 | fputs_filtered ("(", stream); | |
486 | if ((int) opcode > (int) BINOP_END) | |
487 | { | |
488 | if (assoc) | |
489 | { | |
490 | /* Unary postfix operator. */ | |
491 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
492 | fputs_filtered (op_str, stream); | |
493 | } | |
494 | else | |
495 | { | |
496 | /* Unary prefix operator. */ | |
497 | fputs_filtered (op_str, stream); | |
498 | if (myprec == PREC_BUILTIN_FUNCTION) | |
499 | fputs_filtered ("(", stream); | |
500 | print_subexp (exp, pos, stream, PREC_PREFIX); | |
501 | if (myprec == PREC_BUILTIN_FUNCTION) | |
502 | fputs_filtered (")", stream); | |
503 | } | |
504 | } | |
505 | else | |
506 | { | |
507 | /* Binary operator. */ | |
508 | /* Print left operand. | |
c5aa993b JM |
509 | If operator is right-associative, |
510 | increment precedence for this operand. */ | |
c906108c SS |
511 | print_subexp (exp, pos, stream, |
512 | (enum precedence) ((int) myprec + assoc)); | |
513 | /* Print the operator itself. */ | |
514 | if (assign_modify) | |
515 | fprintf_filtered (stream, " %s= ", op_str); | |
516 | else if (op_str[0] == ',') | |
517 | fprintf_filtered (stream, "%s ", op_str); | |
518 | else | |
519 | fprintf_filtered (stream, " %s ", op_str); | |
520 | /* Print right operand. | |
c5aa993b JM |
521 | If operator is left-associative, |
522 | increment precedence for this operand. */ | |
c906108c SS |
523 | print_subexp (exp, pos, stream, |
524 | (enum precedence) ((int) myprec + !assoc)); | |
525 | } | |
526 | ||
527 | if ((int) myprec < (int) prec) | |
528 | fputs_filtered (")", stream); | |
529 | } | |
530 | ||
531 | /* Return the operator corresponding to opcode OP as | |
532 | a string. NULL indicates that the opcode was not found in the | |
533 | current language table. */ | |
534 | char * | |
fba45db2 | 535 | op_string (enum exp_opcode op) |
c906108c SS |
536 | { |
537 | int tem; | |
538 | register const struct op_print *op_print_tab; | |
539 | ||
540 | op_print_tab = current_language->la_op_print_tab; | |
541 | for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++) | |
542 | if (op_print_tab[tem].opcode == op) | |
543 | return op_print_tab[tem].string; | |
544 | return NULL; | |
545 | } | |
546 | ||
c906108c SS |
547 | /* Support for dumping the raw data from expressions in a human readable |
548 | form. */ | |
549 | ||
a14ed312 | 550 | static char *op_name (int opcode); |
c906108c SS |
551 | |
552 | static char * | |
fba45db2 | 553 | op_name (int opcode) |
c906108c SS |
554 | { |
555 | switch (opcode) | |
556 | { | |
557 | default: | |
558 | { | |
559 | static char buf[30]; | |
560 | ||
561 | sprintf (buf, "<unknown %d>", opcode); | |
562 | return buf; | |
563 | } | |
c5aa993b JM |
564 | case OP_NULL: |
565 | return "OP_NULL"; | |
566 | case BINOP_ADD: | |
567 | return "BINOP_ADD"; | |
568 | case BINOP_SUB: | |
569 | return "BINOP_SUB"; | |
570 | case BINOP_MUL: | |
571 | return "BINOP_MUL"; | |
572 | case BINOP_DIV: | |
573 | return "BINOP_DIV"; | |
574 | case BINOP_REM: | |
575 | return "BINOP_REM"; | |
576 | case BINOP_MOD: | |
577 | return "BINOP_MOD"; | |
578 | case BINOP_LSH: | |
579 | return "BINOP_LSH"; | |
580 | case BINOP_RSH: | |
581 | return "BINOP_RSH"; | |
582 | case BINOP_LOGICAL_AND: | |
583 | return "BINOP_LOGICAL_AND"; | |
584 | case BINOP_LOGICAL_OR: | |
585 | return "BINOP_LOGICAL_OR"; | |
586 | case BINOP_BITWISE_AND: | |
587 | return "BINOP_BITWISE_AND"; | |
588 | case BINOP_BITWISE_IOR: | |
589 | return "BINOP_BITWISE_IOR"; | |
590 | case BINOP_BITWISE_XOR: | |
591 | return "BINOP_BITWISE_XOR"; | |
592 | case BINOP_EQUAL: | |
593 | return "BINOP_EQUAL"; | |
594 | case BINOP_NOTEQUAL: | |
595 | return "BINOP_NOTEQUAL"; | |
596 | case BINOP_LESS: | |
597 | return "BINOP_LESS"; | |
598 | case BINOP_GTR: | |
599 | return "BINOP_GTR"; | |
600 | case BINOP_LEQ: | |
601 | return "BINOP_LEQ"; | |
602 | case BINOP_GEQ: | |
603 | return "BINOP_GEQ"; | |
604 | case BINOP_REPEAT: | |
605 | return "BINOP_REPEAT"; | |
606 | case BINOP_ASSIGN: | |
607 | return "BINOP_ASSIGN"; | |
608 | case BINOP_COMMA: | |
609 | return "BINOP_COMMA"; | |
610 | case BINOP_SUBSCRIPT: | |
611 | return "BINOP_SUBSCRIPT"; | |
612 | case MULTI_SUBSCRIPT: | |
613 | return "MULTI_SUBSCRIPT"; | |
614 | case BINOP_EXP: | |
615 | return "BINOP_EXP"; | |
616 | case BINOP_MIN: | |
617 | return "BINOP_MIN"; | |
618 | case BINOP_MAX: | |
619 | return "BINOP_MAX"; | |
c5aa993b JM |
620 | case STRUCTOP_MEMBER: |
621 | return "STRUCTOP_MEMBER"; | |
622 | case STRUCTOP_MPTR: | |
623 | return "STRUCTOP_MPTR"; | |
624 | case BINOP_INTDIV: | |
625 | return "BINOP_INTDIV"; | |
626 | case BINOP_ASSIGN_MODIFY: | |
627 | return "BINOP_ASSIGN_MODIFY"; | |
628 | case BINOP_VAL: | |
629 | return "BINOP_VAL"; | |
630 | case BINOP_INCL: | |
631 | return "BINOP_INCL"; | |
632 | case BINOP_EXCL: | |
633 | return "BINOP_EXCL"; | |
634 | case BINOP_CONCAT: | |
635 | return "BINOP_CONCAT"; | |
636 | case BINOP_RANGE: | |
637 | return "BINOP_RANGE"; | |
638 | case BINOP_END: | |
639 | return "BINOP_END"; | |
640 | case TERNOP_COND: | |
641 | return "TERNOP_COND"; | |
642 | case TERNOP_SLICE: | |
643 | return "TERNOP_SLICE"; | |
644 | case TERNOP_SLICE_COUNT: | |
645 | return "TERNOP_SLICE_COUNT"; | |
646 | case OP_LONG: | |
647 | return "OP_LONG"; | |
648 | case OP_DOUBLE: | |
649 | return "OP_DOUBLE"; | |
650 | case OP_VAR_VALUE: | |
651 | return "OP_VAR_VALUE"; | |
652 | case OP_LAST: | |
653 | return "OP_LAST"; | |
654 | case OP_REGISTER: | |
655 | return "OP_REGISTER"; | |
656 | case OP_INTERNALVAR: | |
657 | return "OP_INTERNALVAR"; | |
658 | case OP_FUNCALL: | |
659 | return "OP_FUNCALL"; | |
660 | case OP_STRING: | |
661 | return "OP_STRING"; | |
662 | case OP_BITSTRING: | |
663 | return "OP_BITSTRING"; | |
664 | case OP_ARRAY: | |
665 | return "OP_ARRAY"; | |
666 | case UNOP_CAST: | |
667 | return "UNOP_CAST"; | |
668 | case UNOP_MEMVAL: | |
669 | return "UNOP_MEMVAL"; | |
670 | case UNOP_NEG: | |
671 | return "UNOP_NEG"; | |
672 | case UNOP_LOGICAL_NOT: | |
673 | return "UNOP_LOGICAL_NOT"; | |
674 | case UNOP_COMPLEMENT: | |
675 | return "UNOP_COMPLEMENT"; | |
676 | case UNOP_IND: | |
677 | return "UNOP_IND"; | |
678 | case UNOP_ADDR: | |
679 | return "UNOP_ADDR"; | |
680 | case UNOP_PREINCREMENT: | |
681 | return "UNOP_PREINCREMENT"; | |
682 | case UNOP_POSTINCREMENT: | |
683 | return "UNOP_POSTINCREMENT"; | |
684 | case UNOP_PREDECREMENT: | |
685 | return "UNOP_PREDECREMENT"; | |
686 | case UNOP_POSTDECREMENT: | |
687 | return "UNOP_POSTDECREMENT"; | |
688 | case UNOP_SIZEOF: | |
689 | return "UNOP_SIZEOF"; | |
690 | case UNOP_LOWER: | |
691 | return "UNOP_LOWER"; | |
692 | case UNOP_UPPER: | |
693 | return "UNOP_UPPER"; | |
694 | case UNOP_LENGTH: | |
695 | return "UNOP_LENGTH"; | |
696 | case UNOP_PLUS: | |
697 | return "UNOP_PLUS"; | |
698 | case UNOP_CAP: | |
699 | return "UNOP_CAP"; | |
700 | case UNOP_CHR: | |
701 | return "UNOP_CHR"; | |
702 | case UNOP_ORD: | |
703 | return "UNOP_ORD"; | |
704 | case UNOP_ABS: | |
705 | return "UNOP_ABS"; | |
706 | case UNOP_FLOAT: | |
707 | return "UNOP_FLOAT"; | |
708 | case UNOP_HIGH: | |
709 | return "UNOP_HIGH"; | |
710 | case UNOP_MAX: | |
711 | return "UNOP_MAX"; | |
712 | case UNOP_MIN: | |
713 | return "UNOP_MIN"; | |
714 | case UNOP_ODD: | |
715 | return "UNOP_ODD"; | |
716 | case UNOP_TRUNC: | |
717 | return "UNOP_TRUNC"; | |
718 | case OP_BOOL: | |
719 | return "OP_BOOL"; | |
720 | case OP_M2_STRING: | |
721 | return "OP_M2_STRING"; | |
722 | case STRUCTOP_STRUCT: | |
723 | return "STRUCTOP_STRUCT"; | |
724 | case STRUCTOP_PTR: | |
725 | return "STRUCTOP_PTR"; | |
726 | case OP_THIS: | |
727 | return "OP_THIS"; | |
82eeeb94 AF |
728 | case OP_OBJC_SELF: |
729 | return "OP_OBJC_SELF"; | |
c5aa993b JM |
730 | case OP_SCOPE: |
731 | return "OP_SCOPE"; | |
732 | case OP_TYPE: | |
733 | return "OP_TYPE"; | |
734 | case OP_LABELED: | |
735 | return "OP_LABELED"; | |
c906108c SS |
736 | } |
737 | } | |
738 | ||
739 | void | |
fba45db2 KB |
740 | dump_prefix_expression (struct expression *exp, struct ui_file *stream, |
741 | char *note) | |
c906108c SS |
742 | { |
743 | int elt; | |
744 | char *opcode_name; | |
745 | char *eltscan; | |
746 | int eltsize; | |
747 | ||
748 | fprintf_filtered (stream, "Dump of expression @ "); | |
d4f3574e | 749 | gdb_print_host_address (exp, stream); |
c906108c SS |
750 | fprintf_filtered (stream, ", %s:\nExpression: `", note); |
751 | if (exp->elts[0].opcode != OP_TYPE) | |
752 | print_expression (exp, stream); | |
753 | else | |
754 | fprintf_filtered (stream, "Type printing not yet supported...."); | |
9d271fd8 | 755 | fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n", |
c5aa993b | 756 | exp->language_defn->la_name, exp->nelts, |
9d271fd8 | 757 | (long) sizeof (union exp_element)); |
c906108c SS |
758 | fprintf_filtered (stream, "\t%5s %20s %16s %s\n", "Index", "Opcode", |
759 | "Hex Value", "String Value"); | |
c5aa993b | 760 | for (elt = 0; elt < exp->nelts; elt++) |
c906108c SS |
761 | { |
762 | fprintf_filtered (stream, "\t%5d ", elt); | |
c5aa993b | 763 | opcode_name = op_name (exp->elts[elt].opcode); |
c906108c SS |
764 | |
765 | fprintf_filtered (stream, "%20s ", opcode_name); | |
c5aa993b | 766 | print_longest (stream, 'd', 0, exp->elts[elt].longconst); |
c906108c SS |
767 | fprintf_filtered (stream, " "); |
768 | ||
769 | for (eltscan = (char *) &exp->elts[elt], | |
c5aa993b | 770 | eltsize = sizeof (union exp_element); |
c906108c SS |
771 | eltsize-- > 0; |
772 | eltscan++) | |
773 | { | |
774 | fprintf_filtered (stream, "%c", | |
775 | isprint (*eltscan) ? (*eltscan & 0xFF) : '.'); | |
776 | } | |
777 | fprintf_filtered (stream, "\n"); | |
778 | } | |
779 | } | |
780 | ||
a14ed312 KB |
781 | static int dump_subexp (struct expression *exp, struct ui_file *stream, |
782 | int elt); | |
c906108c SS |
783 | |
784 | static int | |
fba45db2 | 785 | dump_subexp (struct expression *exp, struct ui_file *stream, int elt) |
c906108c SS |
786 | { |
787 | static int indent = 0; | |
788 | int i; | |
789 | ||
790 | fprintf_filtered (stream, "\n"); | |
791 | fprintf_filtered (stream, "\t%5d ", elt); | |
792 | ||
793 | for (i = 1; i <= indent; i++) | |
794 | fprintf_filtered (stream, " "); | |
795 | indent += 2; | |
796 | ||
797 | fprintf_filtered (stream, "%-20s ", op_name (exp->elts[elt].opcode)); | |
798 | ||
c5aa993b | 799 | switch (exp->elts[elt++].opcode) |
c906108c SS |
800 | { |
801 | case TERNOP_COND: | |
802 | case TERNOP_SLICE: | |
803 | case TERNOP_SLICE_COUNT: | |
804 | elt = dump_subexp (exp, stream, elt); | |
805 | case BINOP_ADD: | |
806 | case BINOP_SUB: | |
807 | case BINOP_MUL: | |
808 | case BINOP_DIV: | |
809 | case BINOP_REM: | |
810 | case BINOP_MOD: | |
811 | case BINOP_LSH: | |
812 | case BINOP_RSH: | |
813 | case BINOP_LOGICAL_AND: | |
814 | case BINOP_LOGICAL_OR: | |
815 | case BINOP_BITWISE_AND: | |
816 | case BINOP_BITWISE_IOR: | |
817 | case BINOP_BITWISE_XOR: | |
818 | case BINOP_EQUAL: | |
819 | case BINOP_NOTEQUAL: | |
820 | case BINOP_LESS: | |
821 | case BINOP_GTR: | |
822 | case BINOP_LEQ: | |
823 | case BINOP_GEQ: | |
824 | case BINOP_REPEAT: | |
825 | case BINOP_ASSIGN: | |
826 | case BINOP_COMMA: | |
827 | case BINOP_SUBSCRIPT: | |
828 | case BINOP_EXP: | |
829 | case BINOP_MIN: | |
830 | case BINOP_MAX: | |
c906108c SS |
831 | case BINOP_INTDIV: |
832 | case BINOP_ASSIGN_MODIFY: | |
833 | case BINOP_VAL: | |
834 | case BINOP_INCL: | |
835 | case BINOP_EXCL: | |
836 | case BINOP_CONCAT: | |
837 | case BINOP_IN: | |
838 | case BINOP_RANGE: | |
839 | case BINOP_END: | |
840 | elt = dump_subexp (exp, stream, elt); | |
841 | case UNOP_NEG: | |
842 | case UNOP_LOGICAL_NOT: | |
843 | case UNOP_COMPLEMENT: | |
844 | case UNOP_IND: | |
845 | case UNOP_ADDR: | |
846 | case UNOP_PREINCREMENT: | |
847 | case UNOP_POSTINCREMENT: | |
848 | case UNOP_PREDECREMENT: | |
849 | case UNOP_POSTDECREMENT: | |
850 | case UNOP_SIZEOF: | |
851 | case UNOP_PLUS: | |
852 | case UNOP_CAP: | |
853 | case UNOP_CHR: | |
854 | case UNOP_ORD: | |
855 | case UNOP_ABS: | |
856 | case UNOP_FLOAT: | |
857 | case UNOP_HIGH: | |
858 | case UNOP_MAX: | |
859 | case UNOP_MIN: | |
860 | case UNOP_ODD: | |
861 | case UNOP_TRUNC: | |
862 | case UNOP_LOWER: | |
863 | case UNOP_UPPER: | |
864 | case UNOP_LENGTH: | |
865 | case UNOP_CARD: | |
866 | case UNOP_CHMAX: | |
867 | case UNOP_CHMIN: | |
868 | elt = dump_subexp (exp, stream, elt); | |
869 | break; | |
870 | case OP_LONG: | |
d4f3574e SS |
871 | fprintf_filtered (stream, "Type @"); |
872 | gdb_print_host_address (exp->elts[elt].type, stream); | |
873 | fprintf_filtered (stream, " ("); | |
c906108c SS |
874 | type_print (exp->elts[elt].type, NULL, stream, 0); |
875 | fprintf_filtered (stream, "), value %ld (0x%lx)", | |
c5aa993b JM |
876 | (long) exp->elts[elt + 1].longconst, |
877 | (long) exp->elts[elt + 1].longconst); | |
c906108c SS |
878 | elt += 3; |
879 | break; | |
880 | case OP_DOUBLE: | |
d4f3574e SS |
881 | fprintf_filtered (stream, "Type @"); |
882 | gdb_print_host_address (exp->elts[elt].type, stream); | |
883 | fprintf_filtered (stream, " ("); | |
c906108c SS |
884 | type_print (exp->elts[elt].type, NULL, stream, 0); |
885 | fprintf_filtered (stream, "), value %g", | |
c5aa993b | 886 | (double) exp->elts[elt + 1].doubleconst); |
c906108c SS |
887 | elt += 3; |
888 | break; | |
889 | case OP_VAR_VALUE: | |
d4f3574e SS |
890 | fprintf_filtered (stream, "Block @"); |
891 | gdb_print_host_address (exp->elts[elt].block, stream); | |
892 | fprintf_filtered (stream, ", symbol @"); | |
893 | gdb_print_host_address (exp->elts[elt + 1].symbol, stream); | |
894 | fprintf_filtered (stream, " (%s)", | |
22abf04a | 895 | DEPRECATED_SYMBOL_NAME (exp->elts[elt + 1].symbol)); |
c906108c SS |
896 | elt += 3; |
897 | break; | |
898 | case OP_LAST: | |
899 | fprintf_filtered (stream, "History element %ld", | |
c5aa993b | 900 | (long) exp->elts[elt].longconst); |
c906108c SS |
901 | elt += 2; |
902 | break; | |
903 | case OP_REGISTER: | |
904 | fprintf_filtered (stream, "Register %ld", | |
c5aa993b | 905 | (long) exp->elts[elt].longconst); |
c906108c SS |
906 | elt += 2; |
907 | break; | |
908 | case OP_INTERNALVAR: | |
d4f3574e SS |
909 | fprintf_filtered (stream, "Internal var @"); |
910 | gdb_print_host_address (exp->elts[elt].internalvar, stream); | |
911 | fprintf_filtered (stream, " (%s)", | |
c906108c SS |
912 | exp->elts[elt].internalvar->name); |
913 | elt += 2; | |
914 | break; | |
915 | case OP_FUNCALL: | |
916 | { | |
917 | int nargs; | |
918 | ||
919 | nargs = longest_to_int (exp->elts[elt].longconst); | |
920 | ||
921 | fprintf_filtered (stream, "Number of args: %d", nargs); | |
922 | elt += 2; | |
923 | ||
924 | for (i = 1; i <= nargs + 1; i++) | |
925 | elt = dump_subexp (exp, stream, elt); | |
926 | } | |
927 | break; | |
928 | case OP_ARRAY: | |
929 | { | |
930 | int lower, upper; | |
931 | int i; | |
932 | ||
933 | lower = longest_to_int (exp->elts[elt].longconst); | |
934 | upper = longest_to_int (exp->elts[elt + 1].longconst); | |
935 | ||
936 | fprintf_filtered (stream, "Bounds [%d:%d]", lower, upper); | |
937 | elt += 3; | |
938 | ||
939 | for (i = 1; i <= upper - lower + 1; i++) | |
940 | elt = dump_subexp (exp, stream, elt); | |
941 | } | |
942 | break; | |
943 | case UNOP_MEMVAL: | |
944 | case UNOP_CAST: | |
d4f3574e SS |
945 | fprintf_filtered (stream, "Type @"); |
946 | gdb_print_host_address (exp->elts[elt].type, stream); | |
947 | fprintf_filtered (stream, " ("); | |
c906108c SS |
948 | type_print (exp->elts[elt].type, NULL, stream, 0); |
949 | fprintf_filtered (stream, ")"); | |
950 | elt = dump_subexp (exp, stream, elt + 2); | |
951 | break; | |
952 | case OP_TYPE: | |
d4f3574e SS |
953 | fprintf_filtered (stream, "Type @"); |
954 | gdb_print_host_address (exp->elts[elt].type, stream); | |
955 | fprintf_filtered (stream, " ("); | |
c906108c SS |
956 | type_print (exp->elts[elt].type, NULL, stream, 0); |
957 | fprintf_filtered (stream, ")"); | |
958 | elt += 2; | |
959 | break; | |
960 | case STRUCTOP_STRUCT: | |
961 | case STRUCTOP_PTR: | |
962 | { | |
963 | char *elem_name; | |
964 | int len; | |
965 | ||
966 | len = longest_to_int (exp->elts[elt].longconst); | |
967 | elem_name = &exp->elts[elt + 1].string; | |
968 | ||
969 | fprintf_filtered (stream, "Element name: `%.*s'", len, elem_name); | |
970 | elt = dump_subexp (exp, stream, elt + 3 + BYTES_TO_EXP_ELEM (len + 1)); | |
971 | } | |
972 | break; | |
973 | case OP_SCOPE: | |
974 | { | |
975 | char *elem_name; | |
976 | int len; | |
977 | ||
d4f3574e SS |
978 | fprintf_filtered (stream, "Type @"); |
979 | gdb_print_host_address (exp->elts[elt].type, stream); | |
980 | fprintf_filtered (stream, " ("); | |
c906108c SS |
981 | type_print (exp->elts[elt].type, NULL, stream, 0); |
982 | fprintf_filtered (stream, ") "); | |
983 | ||
984 | len = longest_to_int (exp->elts[elt + 1].longconst); | |
985 | elem_name = &exp->elts[elt + 2].string; | |
986 | ||
987 | fprintf_filtered (stream, "Field name: `%.*s'", len, elem_name); | |
988 | elt += 4 + BYTES_TO_EXP_ELEM (len + 1); | |
989 | } | |
990 | break; | |
991 | default: | |
992 | case OP_NULL: | |
993 | case STRUCTOP_MEMBER: | |
994 | case STRUCTOP_MPTR: | |
995 | case MULTI_SUBSCRIPT: | |
996 | case OP_F77_UNDETERMINED_ARGLIST: | |
997 | case OP_COMPLEX: | |
998 | case OP_STRING: | |
999 | case OP_BITSTRING: | |
1000 | case OP_BOOL: | |
1001 | case OP_M2_STRING: | |
1002 | case OP_THIS: | |
1003 | case OP_LABELED: | |
1004 | case OP_NAME: | |
1005 | case OP_EXPRSTRING: | |
1006 | fprintf_filtered (stream, "Unknown format"); | |
1007 | } | |
1008 | ||
1009 | indent -= 2; | |
1010 | ||
1011 | return elt; | |
1012 | } | |
1013 | ||
1014 | void | |
fba45db2 KB |
1015 | dump_postfix_expression (struct expression *exp, struct ui_file *stream, |
1016 | char *note) | |
c906108c SS |
1017 | { |
1018 | int elt; | |
1019 | ||
1020 | fprintf_filtered (stream, "Dump of expression @ "); | |
d4f3574e | 1021 | gdb_print_host_address (exp, stream); |
c906108c SS |
1022 | fprintf_filtered (stream, ", %s:\nExpression: `", note); |
1023 | if (exp->elts[0].opcode != OP_TYPE) | |
1024 | print_expression (exp, stream); | |
1025 | else | |
1026 | fputs_filtered ("Type printing not yet supported....", stream); | |
9d271fd8 | 1027 | fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n", |
c5aa993b | 1028 | exp->language_defn->la_name, exp->nelts, |
9d271fd8 | 1029 | (long) sizeof (union exp_element)); |
c906108c SS |
1030 | fputs_filtered ("\n", stream); |
1031 | ||
c5aa993b | 1032 | for (elt = 0; elt < exp->nelts;) |
c906108c SS |
1033 | elt = dump_subexp (exp, stream, elt); |
1034 | fputs_filtered ("\n", stream); | |
1035 | } |