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