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