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 TERNOP_COND:
332       if ((int) prec > (int) PREC_COMMA)
333         fputs_filtered ("(", stream);
334       /* Print the subexpressions, forcing parentheses
335          around any binary operations within them.
336          This is more parentheses than are strictly necessary,
337          but it looks clearer.  */
338       print_subexp (exp, pos, stream, PREC_HYPER);
339       fputs_filtered (" ? ", stream);
340       print_subexp (exp, pos, stream, PREC_HYPER);
341       fputs_filtered (" : ", stream);
342       print_subexp (exp, pos, stream, PREC_HYPER);
343       if ((int) prec > (int) PREC_COMMA)
344         fputs_filtered (")", stream);
345       return;
346
347     case TERNOP_SLICE:
348       print_subexp (exp, pos, stream, PREC_SUFFIX);
349       fputs_filtered ("(", stream);
350       print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
351       fputs_filtered (opcode == TERNOP_SLICE ? " : " : " UP ", stream);
352       print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
353       fputs_filtered (")", stream);
354       return;
355
356     case STRUCTOP_STRUCT:
357       tem = longest_to_int (exp->elts[pc + 1].longconst);
358       (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
359       print_subexp (exp, pos, stream, PREC_SUFFIX);
360       fputs_filtered (".", stream);
361       fputs_filtered (&exp->elts[pc + 2].string, stream);
362       return;
363
364       /* Will not occur for Modula-2.  */
365     case STRUCTOP_PTR:
366       tem = longest_to_int (exp->elts[pc + 1].longconst);
367       (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
368       print_subexp (exp, pos, stream, PREC_SUFFIX);
369       fputs_filtered ("->", stream);
370       fputs_filtered (&exp->elts[pc + 2].string, stream);
371       return;
372
373     case STRUCTOP_MEMBER:
374       print_subexp (exp, pos, stream, PREC_SUFFIX);
375       fputs_filtered (".*", stream);
376       print_subexp (exp, pos, stream, PREC_SUFFIX);
377       return;
378
379     case STRUCTOP_MPTR:
380       print_subexp (exp, pos, stream, PREC_SUFFIX);
381       fputs_filtered ("->*", stream);
382       print_subexp (exp, pos, stream, PREC_SUFFIX);
383       return;
384
385     case BINOP_SUBSCRIPT:
386       print_subexp (exp, pos, stream, PREC_SUFFIX);
387       fputs_filtered ("[", stream);
388       print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
389       fputs_filtered ("]", stream);
390       return;
391
392     case UNOP_POSTINCREMENT:
393       print_subexp (exp, pos, stream, PREC_SUFFIX);
394       fputs_filtered ("++", stream);
395       return;
396
397     case UNOP_POSTDECREMENT:
398       print_subexp (exp, pos, stream, PREC_SUFFIX);
399       fputs_filtered ("--", stream);
400       return;
401
402     case UNOP_CAST:
403       (*pos) += 2;
404       if ((int) prec > (int) PREC_PREFIX)
405         fputs_filtered ("(", stream);
406       fputs_filtered ("(", stream);
407       type_print (exp->elts[pc + 1].type, "", stream, 0);
408       fputs_filtered (") ", stream);
409       print_subexp (exp, pos, stream, PREC_PREFIX);
410       if ((int) prec > (int) PREC_PREFIX)
411         fputs_filtered (")", stream);
412       return;
413
414     case UNOP_CAST_TYPE:
415       if ((int) prec > (int) PREC_PREFIX)
416         fputs_filtered ("(", stream);
417       fputs_filtered ("(", stream);
418       print_subexp (exp, pos, stream, PREC_PREFIX);
419       fputs_filtered (") ", stream);
420       print_subexp (exp, pos, stream, PREC_PREFIX);
421       if ((int) prec > (int) PREC_PREFIX)
422         fputs_filtered (")", stream);
423       return;
424
425     case UNOP_DYNAMIC_CAST:
426     case UNOP_REINTERPRET_CAST:
427       fputs_filtered (opcode == UNOP_DYNAMIC_CAST ? "dynamic_cast"
428                       : "reinterpret_cast", stream);
429       fputs_filtered ("<", stream);
430       print_subexp (exp, pos, stream, PREC_PREFIX);
431       fputs_filtered ("> (", stream);
432       print_subexp (exp, pos, stream, PREC_PREFIX);
433       fputs_filtered (")", stream);
434       return;
435
436     case UNOP_MEMVAL:
437       (*pos) += 2;
438       if ((int) prec > (int) PREC_PREFIX)
439         fputs_filtered ("(", stream);
440       if (TYPE_CODE (exp->elts[pc + 1].type) == TYPE_CODE_FUNC
441           && exp->elts[pc + 3].opcode == OP_LONG)
442         {
443           struct value_print_options opts;
444
445           /* We have a minimal symbol fn, probably.  It's encoded
446              as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
447              Swallow the OP_LONG (including both its opcodes); ignore
448              its type; print the value in the type of the MEMVAL.  */
449           (*pos) += 4;
450           val = value_at_lazy (exp->elts[pc + 1].type,
451                                (CORE_ADDR) exp->elts[pc + 5].longconst);
452           get_raw_print_options (&opts);
453           value_print (val, stream, &opts);
454         }
455       else
456         {
457           fputs_filtered ("{", stream);
458           type_print (exp->elts[pc + 1].type, "", stream, 0);
459           fputs_filtered ("} ", stream);
460           print_subexp (exp, pos, stream, PREC_PREFIX);
461         }
462       if ((int) prec > (int) PREC_PREFIX)
463         fputs_filtered (")", stream);
464       return;
465
466     case UNOP_MEMVAL_TYPE:
467       if ((int) prec > (int) PREC_PREFIX)
468         fputs_filtered ("(", stream);
469       fputs_filtered ("{", stream);
470       print_subexp (exp, pos, stream, PREC_PREFIX);
471       fputs_filtered ("} ", stream);
472       print_subexp (exp, pos, stream, PREC_PREFIX);
473       if ((int) prec > (int) PREC_PREFIX)
474         fputs_filtered (")", stream);
475       return;
476
477     case UNOP_MEMVAL_TLS:
478       (*pos) += 3;
479       if ((int) prec > (int) PREC_PREFIX)
480         fputs_filtered ("(", stream);
481       fputs_filtered ("{", stream);
482       type_print (exp->elts[pc + 2].type, "", stream, 0);
483       fputs_filtered ("} ", stream);
484       print_subexp (exp, pos, stream, PREC_PREFIX);
485       if ((int) prec > (int) PREC_PREFIX)
486         fputs_filtered (")", stream);
487       return;
488
489     case BINOP_ASSIGN_MODIFY:
490       opcode = exp->elts[pc + 1].opcode;
491       (*pos) += 2;
492       myprec = PREC_ASSIGN;
493       assoc = 1;
494       assign_modify = 1;
495       op_str = "???";
496       for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
497         if (op_print_tab[tem].opcode == opcode)
498           {
499             op_str = op_print_tab[tem].string;
500             break;
501           }
502       if (op_print_tab[tem].opcode != opcode)
503         /* Not found; don't try to keep going because we don't know how
504            to interpret further elements.  */
505         error (_("Invalid expression"));
506       break;
507
508       /* C++ ops */
509
510     case OP_THIS:
511       ++(*pos);
512       if (exp->language_defn->la_name_of_this)
513         fputs_filtered (exp->language_defn->la_name_of_this, stream);
514       else
515         fprintf_filtered (stream, _("<language %s has no 'this'>"),
516                           exp->language_defn->la_name);
517       return;
518
519       /* Modula-2 ops */
520
521     case MULTI_SUBSCRIPT:
522       (*pos) += 2;
523       nargs = longest_to_int (exp->elts[pc + 1].longconst);
524       print_subexp (exp, pos, stream, PREC_SUFFIX);
525       fprintf_unfiltered (stream, " [");
526       for (tem = 0; tem < nargs; tem++)
527         {
528           if (tem != 0)
529             fprintf_unfiltered (stream, ", ");
530           print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
531         }
532       fprintf_unfiltered (stream, "]");
533       return;
534
535     case BINOP_VAL:
536       (*pos) += 2;
537       fprintf_unfiltered (stream, "VAL(");
538       type_print (exp->elts[pc + 1].type, "", stream, 0);
539       fprintf_unfiltered (stream, ",");
540       print_subexp (exp, pos, stream, PREC_PREFIX);
541       fprintf_unfiltered (stream, ")");
542       return;
543
544     case TYPE_INSTANCE:
545       {
546         LONGEST count = exp->elts[pc + 1].longconst;
547
548         /* The COUNT.  */
549         (*pos)++;
550         fputs_unfiltered ("TypesInstance(", stream);
551         while (count-- > 0)
552           {
553             type_print (exp->elts[(*pos)++].type, "", stream, 0);
554             if (count > 0)
555               fputs_unfiltered (",", stream);
556           }
557         fputs_unfiltered (",", stream);
558         /* Ending COUNT and ending TYPE_INSTANCE.  */
559         (*pos) += 2;
560         print_subexp (exp, pos, stream, PREC_PREFIX);
561         fputs_unfiltered (")", stream);
562         return;
563       }
564
565       /* Default ops */
566
567     default:
568       op_str = "???";
569       for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
570         if (op_print_tab[tem].opcode == opcode)
571           {
572             op_str = op_print_tab[tem].string;
573             myprec = op_print_tab[tem].precedence;
574             assoc = op_print_tab[tem].right_assoc;
575             break;
576           }
577       if (op_print_tab[tem].opcode != opcode)
578         /* Not found; don't try to keep going because we don't know how
579            to interpret further elements.  For example, this happens
580            if opcode is OP_TYPE.  */
581         error (_("Invalid expression"));
582     }
583
584   /* Note that PREC_BUILTIN will always emit parentheses.  */
585   if ((int) myprec < (int) prec)
586     fputs_filtered ("(", stream);
587   if ((int) opcode > (int) BINOP_END)
588     {
589       if (assoc)
590         {
591           /* Unary postfix operator.  */
592           print_subexp (exp, pos, stream, PREC_SUFFIX);
593           fputs_filtered (op_str, stream);
594         }
595       else
596         {
597           /* Unary prefix operator.  */
598           fputs_filtered (op_str, stream);
599           if (myprec == PREC_BUILTIN_FUNCTION)
600             fputs_filtered ("(", stream);
601           print_subexp (exp, pos, stream, PREC_PREFIX);
602           if (myprec == PREC_BUILTIN_FUNCTION)
603             fputs_filtered (")", stream);
604         }
605     }
606   else
607     {
608       /* Binary operator.  */
609       /* Print left operand.
610          If operator is right-associative,
611          increment precedence for this operand.  */
612       print_subexp (exp, pos, stream,
613                     (enum precedence) ((int) myprec + assoc));
614       /* Print the operator itself.  */
615       if (assign_modify)
616         fprintf_filtered (stream, " %s= ", op_str);
617       else if (op_str[0] == ',')
618         fprintf_filtered (stream, "%s ", op_str);
619       else
620         fprintf_filtered (stream, " %s ", op_str);
621       /* Print right operand.
622          If operator is left-associative,
623          increment precedence for this operand.  */
624       print_subexp (exp, pos, stream,
625                     (enum precedence) ((int) myprec + !assoc));
626     }
627
628   if ((int) myprec < (int) prec)
629     fputs_filtered (")", stream);
630 }
631
632 /* Return the operator corresponding to opcode OP as
633    a string.   NULL indicates that the opcode was not found in the
634    current language table.  */
635 char *
636 op_string (enum exp_opcode op)
637 {
638   int tem;
639   const struct op_print *op_print_tab;
640
641   op_print_tab = current_language->la_op_print_tab;
642   for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
643     if (op_print_tab[tem].opcode == op)
644       return op_print_tab[tem].string;
645   return NULL;
646 }
647
648 /* Support for dumping the raw data from expressions in a human readable
649    form.  */
650
651 static int dump_subexp_body (struct expression *exp, struct ui_file *, int);
652
653 /* Name for OPCODE, when it appears in expression EXP.  */
654
655 char *
656 op_name (struct expression *exp, enum exp_opcode opcode)
657 {
658   return exp->language_defn->la_exp_desc->op_name (opcode);
659 }
660
661 /* Default name for the standard operator OPCODE (i.e., one defined in
662    the definition of enum exp_opcode).  */
663
664 char *
665 op_name_standard (enum exp_opcode opcode)
666 {
667   switch (opcode)
668     {
669     default:
670       {
671         static char buf[30];
672
673         sprintf (buf, "<unknown %d>", opcode);
674         return buf;
675       }
676 #define OP(name)        \
677     case name:          \
678       return #name ;
679 #include "std-operator.def"
680 #undef OP
681     }
682 }
683
684 /* Print a raw dump of expression EXP to STREAM.
685    NOTE, if non-NULL, is printed as extra explanatory text.  */
686
687 void
688 dump_raw_expression (struct expression *exp, struct ui_file *stream,
689                      char *note)
690 {
691   int elt;
692   char *opcode_name;
693   char *eltscan;
694   int eltsize;
695
696   fprintf_filtered (stream, "Dump of expression @ ");
697   gdb_print_host_address (exp, stream);
698   if (note)
699     fprintf_filtered (stream, ", %s:", note);
700   fprintf_filtered (stream, "\n\tLanguage %s, %d elements, %ld bytes each.\n",
701                     exp->language_defn->la_name, exp->nelts,
702                     (long) sizeof (union exp_element));
703   fprintf_filtered (stream, "\t%5s  %20s  %16s  %s\n", "Index", "Opcode",
704                     "Hex Value", "String Value");
705   for (elt = 0; elt < exp->nelts; elt++)
706     {
707       fprintf_filtered (stream, "\t%5d  ", elt);
708       opcode_name = op_name (exp, exp->elts[elt].opcode);
709
710       fprintf_filtered (stream, "%20s  ", opcode_name);
711       print_longest (stream, 'd', 0, exp->elts[elt].longconst);
712       fprintf_filtered (stream, "  ");
713
714       for (eltscan = (char *) &exp->elts[elt],
715            eltsize = sizeof (union exp_element);
716            eltsize-- > 0;
717            eltscan++)
718         {
719           fprintf_filtered (stream, "%c",
720                             isprint (*eltscan) ? (*eltscan & 0xFF) : '.');
721         }
722       fprintf_filtered (stream, "\n");
723     }
724 }
725
726 /* Dump the subexpression of prefix expression EXP whose operator is at
727    position ELT onto STREAM.  Returns the position of the next 
728    subexpression in EXP.  */
729
730 int
731 dump_subexp (struct expression *exp, struct ui_file *stream, int elt)
732 {
733   static int indent = 0;
734   int i;
735
736   fprintf_filtered (stream, "\n");
737   fprintf_filtered (stream, "\t%5d  ", elt);
738
739   for (i = 1; i <= indent; i++)
740     fprintf_filtered (stream, " ");
741   indent += 2;
742
743   fprintf_filtered (stream, "%-20s  ", op_name (exp, exp->elts[elt].opcode));
744
745   elt = dump_subexp_body (exp, stream, elt);
746
747   indent -= 2;
748
749   return elt;
750 }
751
752 /* Dump the operands of prefix expression EXP whose opcode is at
753    position ELT onto STREAM.  Returns the position of the next 
754    subexpression in EXP.  */
755
756 static int
757 dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
758 {
759   return exp->language_defn->la_exp_desc->dump_subexp_body (exp, stream, elt);
760 }
761
762 /* Default value for subexp_body in exp_descriptor vector.  */
763
764 int
765 dump_subexp_body_standard (struct expression *exp, 
766                            struct ui_file *stream, int elt)
767 {
768   int opcode = exp->elts[elt++].opcode;
769
770   switch (opcode)
771     {
772     case TERNOP_COND:
773     case TERNOP_SLICE:
774       elt = dump_subexp (exp, stream, elt);
775       /* FALL THROUGH */
776     case BINOP_ADD:
777     case BINOP_SUB:
778     case BINOP_MUL:
779     case BINOP_DIV:
780     case BINOP_REM:
781     case BINOP_MOD:
782     case BINOP_LSH:
783     case BINOP_RSH:
784     case BINOP_LOGICAL_AND:
785     case BINOP_LOGICAL_OR:
786     case BINOP_BITWISE_AND:
787     case BINOP_BITWISE_IOR:
788     case BINOP_BITWISE_XOR:
789     case BINOP_EQUAL:
790     case BINOP_NOTEQUAL:
791     case BINOP_LESS:
792     case BINOP_GTR:
793     case BINOP_LEQ:
794     case BINOP_GEQ:
795     case BINOP_REPEAT:
796     case BINOP_ASSIGN:
797     case BINOP_COMMA:
798     case BINOP_SUBSCRIPT:
799     case BINOP_EXP:
800     case BINOP_MIN:
801     case BINOP_MAX:
802     case BINOP_INTDIV:
803     case BINOP_ASSIGN_MODIFY:
804     case BINOP_VAL:
805     case BINOP_CONCAT:
806     case BINOP_IN:
807     case BINOP_RANGE:
808     case BINOP_END:
809     case STRUCTOP_MEMBER:
810     case STRUCTOP_MPTR:
811       elt = dump_subexp (exp, stream, elt);
812       /* FALL THROUGH */
813     case UNOP_NEG:
814     case UNOP_LOGICAL_NOT:
815     case UNOP_COMPLEMENT:
816     case UNOP_IND:
817     case UNOP_ADDR:
818     case UNOP_PREINCREMENT:
819     case UNOP_POSTINCREMENT:
820     case UNOP_PREDECREMENT:
821     case UNOP_POSTDECREMENT:
822     case UNOP_SIZEOF:
823     case UNOP_PLUS:
824     case UNOP_CAP:
825     case UNOP_CHR:
826     case UNOP_ORD:
827     case UNOP_ABS:
828     case UNOP_FLOAT:
829     case UNOP_HIGH:
830     case UNOP_MAX:
831     case UNOP_MIN:
832     case UNOP_ODD:
833     case UNOP_TRUNC:
834       elt = dump_subexp (exp, stream, elt);
835       break;
836     case OP_LONG:
837       fprintf_filtered (stream, "Type @");
838       gdb_print_host_address (exp->elts[elt].type, stream);
839       fprintf_filtered (stream, " (");
840       type_print (exp->elts[elt].type, NULL, stream, 0);
841       fprintf_filtered (stream, "), value %ld (0x%lx)",
842                         (long) exp->elts[elt + 1].longconst,
843                         (long) exp->elts[elt + 1].longconst);
844       elt += 3;
845       break;
846     case OP_DOUBLE:
847       fprintf_filtered (stream, "Type @");
848       gdb_print_host_address (exp->elts[elt].type, stream);
849       fprintf_filtered (stream, " (");
850       type_print (exp->elts[elt].type, NULL, stream, 0);
851       fprintf_filtered (stream, "), value %g",
852                         (double) exp->elts[elt + 1].doubleconst);
853       elt += 3;
854       break;
855     case OP_VAR_VALUE:
856       fprintf_filtered (stream, "Block @");
857       gdb_print_host_address (exp->elts[elt].block, stream);
858       fprintf_filtered (stream, ", symbol @");
859       gdb_print_host_address (exp->elts[elt + 1].symbol, stream);
860       fprintf_filtered (stream, " (%s)",
861                         SYMBOL_PRINT_NAME (exp->elts[elt + 1].symbol));
862       elt += 3;
863       break;
864     case OP_VAR_ENTRY_VALUE:
865       fprintf_filtered (stream, "Entry value of symbol @");
866       gdb_print_host_address (exp->elts[elt].symbol, stream);
867       fprintf_filtered (stream, " (%s)",
868                         SYMBOL_PRINT_NAME (exp->elts[elt].symbol));
869       elt += 2;
870       break;
871     case OP_LAST:
872       fprintf_filtered (stream, "History element %ld",
873                         (long) exp->elts[elt].longconst);
874       elt += 2;
875       break;
876     case OP_REGISTER:
877       fprintf_filtered (stream, "Register $%s", &exp->elts[elt + 1].string);
878       elt += 3 + BYTES_TO_EXP_ELEM (exp->elts[elt].longconst + 1);
879       break;
880     case OP_INTERNALVAR:
881       fprintf_filtered (stream, "Internal var @");
882       gdb_print_host_address (exp->elts[elt].internalvar, stream);
883       fprintf_filtered (stream, " (%s)",
884                         internalvar_name (exp->elts[elt].internalvar));
885       elt += 2;
886       break;
887     case OP_FUNCALL:
888       {
889         int i, nargs;
890
891         nargs = longest_to_int (exp->elts[elt].longconst);
892
893         fprintf_filtered (stream, "Number of args: %d", nargs);
894         elt += 2;
895
896         for (i = 1; i <= nargs + 1; i++)
897           elt = dump_subexp (exp, stream, elt);
898       }
899       break;
900     case OP_ARRAY:
901       {
902         int lower, upper;
903         int i;
904
905         lower = longest_to_int (exp->elts[elt].longconst);
906         upper = longest_to_int (exp->elts[elt + 1].longconst);
907
908         fprintf_filtered (stream, "Bounds [%d:%d]", lower, upper);
909         elt += 3;
910
911         for (i = 1; i <= upper - lower + 1; i++)
912           elt = dump_subexp (exp, stream, elt);
913       }
914       break;
915     case UNOP_DYNAMIC_CAST:
916     case UNOP_REINTERPRET_CAST:
917     case UNOP_CAST_TYPE:
918     case UNOP_MEMVAL_TYPE:
919       fprintf_filtered (stream, " (");
920       elt = dump_subexp (exp, stream, elt);
921       fprintf_filtered (stream, ")");
922       elt = dump_subexp (exp, stream, elt);
923       break;
924     case UNOP_MEMVAL:
925     case UNOP_CAST:
926       fprintf_filtered (stream, "Type @");
927       gdb_print_host_address (exp->elts[elt].type, stream);
928       fprintf_filtered (stream, " (");
929       type_print (exp->elts[elt].type, NULL, stream, 0);
930       fprintf_filtered (stream, ")");
931       elt = dump_subexp (exp, stream, elt + 2);
932       break;
933     case UNOP_MEMVAL_TLS:
934       fprintf_filtered (stream, "TLS type @");
935       gdb_print_host_address (exp->elts[elt + 1].type, stream);
936       fprintf_filtered (stream, " (__thread /* \"%s\" */ ",
937                         (exp->elts[elt].objfile == NULL ? "(null)"
938                          : exp->elts[elt].objfile->name));
939       type_print (exp->elts[elt + 1].type, NULL, stream, 0);
940       fprintf_filtered (stream, ")");
941       elt = dump_subexp (exp, stream, elt + 3);
942       break;
943     case OP_TYPE:
944       fprintf_filtered (stream, "Type @");
945       gdb_print_host_address (exp->elts[elt].type, stream);
946       fprintf_filtered (stream, " (");
947       type_print (exp->elts[elt].type, NULL, stream, 0);
948       fprintf_filtered (stream, ")");
949       elt += 2;
950       break;
951     case OP_TYPEOF:
952     case OP_DECLTYPE:
953       fprintf_filtered (stream, "Typeof (");
954       elt = dump_subexp (exp, stream, elt);
955       fprintf_filtered (stream, ")");
956       break;
957     case STRUCTOP_STRUCT:
958     case STRUCTOP_PTR:
959       {
960         char *elem_name;
961         int len;
962
963         len = longest_to_int (exp->elts[elt].longconst);
964         elem_name = &exp->elts[elt + 1].string;
965
966         fprintf_filtered (stream, "Element name: `%.*s'", len, elem_name);
967         elt = dump_subexp (exp, stream, elt + 3 + BYTES_TO_EXP_ELEM (len + 1));
968       }
969       break;
970     case OP_SCOPE:
971       {
972         char *elem_name;
973         int len;
974
975         fprintf_filtered (stream, "Type @");
976         gdb_print_host_address (exp->elts[elt].type, stream);
977         fprintf_filtered (stream, " (");
978         type_print (exp->elts[elt].type, NULL, stream, 0);
979         fprintf_filtered (stream, ") ");
980
981         len = longest_to_int (exp->elts[elt + 1].longconst);
982         elem_name = &exp->elts[elt + 2].string;
983
984         fprintf_filtered (stream, "Field name: `%.*s'", len, elem_name);
985         elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
986       }
987       break;
988     case TYPE_INSTANCE:
989       {
990         LONGEST len;
991
992         len = exp->elts[elt++].longconst;
993         fprintf_filtered (stream, "%s TypeInstance: ", plongest (len));
994         while (len-- > 0)
995           {
996             fprintf_filtered (stream, "Type @");
997             gdb_print_host_address (exp->elts[elt].type, stream);
998             fprintf_filtered (stream, " (");
999             type_print (exp->elts[elt].type, NULL, stream, 0);
1000             fprintf_filtered (stream, ")");
1001             elt++;
1002             if (len > 0)
1003               fputs_filtered (", ", stream);
1004           }
1005         /* Ending LEN and ending TYPE_INSTANCE.  */
1006         elt += 2;
1007         elt = dump_subexp (exp, stream, elt);
1008       }
1009       break;
1010     default:
1011     case OP_NULL:
1012     case MULTI_SUBSCRIPT:
1013     case OP_F77_UNDETERMINED_ARGLIST:
1014     case OP_COMPLEX:
1015     case OP_STRING:
1016     case OP_BOOL:
1017     case OP_M2_STRING:
1018     case OP_THIS:
1019     case OP_NAME:
1020       fprintf_filtered (stream, "Unknown format");
1021     }
1022
1023   return elt;
1024 }
1025
1026 void
1027 dump_prefix_expression (struct expression *exp, struct ui_file *stream)
1028 {
1029   int elt;
1030
1031   fprintf_filtered (stream, "Dump of expression @ ");
1032   gdb_print_host_address (exp, stream);
1033   fputs_filtered (", after conversion to prefix form:\nExpression: `", stream);
1034   print_expression (exp, stream);
1035   fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
1036                     exp->language_defn->la_name, exp->nelts,
1037                     (long) sizeof (union exp_element));
1038   fputs_filtered ("\n", stream);
1039
1040   for (elt = 0; elt < exp->nelts;)
1041     elt = dump_subexp (exp, stream, elt);
1042   fputs_filtered ("\n", stream);
1043 }