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