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