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