Sort includes for files gdb/[a-f]*.[chyl].
[external/binutils.git] / gdb / expprint.c
1 /* Print in infix form a struct expression.
2
3    Copyright (C) 1986-2019 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21
22 /* Standard C includes.  */
23 #include <ctype.h>
24
25 /* Local non-gdb includes.  */
26 #include "block.h"
27 #include "expression.h"
28 #include "gdbtypes.h"
29 #include "language.h"
30 #include "objfiles.h"
31 #include "parser-defs.h"
32 #include "symtab.h"
33 #include "target.h"
34 #include "user-regs.h"
35 #include "valprint.h"
36 #include "value.h"
37
38 void
39 print_expression (struct expression *exp, struct ui_file *stream)
40 {
41   int pc = 0;
42
43   print_subexp (exp, &pc, stream, PREC_NULL);
44 }
45
46 /* Print the subexpression of EXP that starts in position POS, on STREAM.
47    PREC is the precedence of the surrounding operator;
48    if the precedence of the main operator of this subexpression is less,
49    parentheses are needed here.  */
50
51 void
52 print_subexp (struct expression *exp, int *pos,
53               struct ui_file *stream, enum precedence prec)
54 {
55   exp->language_defn->la_exp_desc->print_subexp (exp, pos, stream, prec);
56 }
57
58 /* Standard implementation of print_subexp for use in language_defn
59    vectors.  */
60 void
61 print_subexp_standard (struct expression *exp, int *pos,
62                        struct ui_file *stream, enum precedence prec)
63 {
64   unsigned tem;
65   const struct op_print *op_print_tab;
66   int pc;
67   unsigned nargs;
68   const char *op_str;
69   int assign_modify = 0;
70   enum exp_opcode opcode;
71   enum precedence myprec = PREC_NULL;
72   /* Set to 1 for a right-associative operator.  */
73   int assoc = 0;
74   struct value *val;
75   char *tempstr = NULL;
76
77   op_print_tab = exp->language_defn->la_op_print_tab;
78   pc = (*pos)++;
79   opcode = exp->elts[pc].opcode;
80   switch (opcode)
81     {
82       /* Common ops */
83
84     case OP_TYPE:
85       (*pos) += 2;
86       type_print (exp->elts[pc + 1].type, "", stream, 0);
87       return;
88
89     case OP_SCOPE:
90       myprec = PREC_PREFIX;
91       assoc = 0;
92       fputs_filtered (TYPE_NAME (exp->elts[pc + 1].type), stream);
93       fputs_filtered ("::", stream);
94       nargs = longest_to_int (exp->elts[pc + 2].longconst);
95       (*pos) += 4 + BYTES_TO_EXP_ELEM (nargs + 1);
96       fputs_filtered (&exp->elts[pc + 3].string, stream);
97       return;
98
99     case OP_LONG:
100       {
101         struct value_print_options opts;
102
103         get_no_prettyformat_print_options (&opts);
104         (*pos) += 3;
105         value_print (value_from_longest (exp->elts[pc + 1].type,
106                                          exp->elts[pc + 2].longconst),
107                      stream, &opts);
108       }
109       return;
110
111     case OP_FLOAT:
112       {
113         struct value_print_options opts;
114
115         get_no_prettyformat_print_options (&opts);
116         (*pos) += 3;
117         value_print (value_from_contents (exp->elts[pc + 1].type,
118                                           exp->elts[pc + 2].floatconst),
119                      stream, &opts);
120       }
121       return;
122
123     case OP_VAR_VALUE:
124       {
125         const struct block *b;
126
127         (*pos) += 3;
128         b = exp->elts[pc + 1].block;
129         if (b != NULL
130             && BLOCK_FUNCTION (b) != NULL
131             && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)) != NULL)
132           {
133             fputs_filtered (SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)), stream);
134             fputs_filtered ("::", stream);
135           }
136         fputs_filtered (SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol), stream);
137       }
138       return;
139
140     case OP_VAR_MSYM_VALUE:
141       {
142         (*pos) += 3;
143         fputs_filtered (MSYMBOL_PRINT_NAME (exp->elts[pc + 2].msymbol), stream);
144       }
145       return;
146
147     case OP_FUNC_STATIC_VAR:
148       {
149         tem = longest_to_int (exp->elts[pc + 1].longconst);
150         (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
151         fputs_filtered (&exp->elts[pc + 1].string, stream);
152       }
153       return;
154
155     case OP_VAR_ENTRY_VALUE:
156       {
157         (*pos) += 2;
158         fprintf_filtered (stream, "%s@entry",
159                           SYMBOL_PRINT_NAME (exp->elts[pc + 1].symbol));
160       }
161       return;
162
163     case OP_LAST:
164       (*pos) += 2;
165       fprintf_filtered (stream, "$%d",
166                         longest_to_int (exp->elts[pc + 1].longconst));
167       return;
168
169     case OP_REGISTER:
170       {
171         const char *name = &exp->elts[pc + 2].string;
172
173         (*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
174         fprintf_filtered (stream, "$%s", name);
175         return;
176       }
177
178     case OP_BOOL:
179       (*pos) += 2;
180       fprintf_filtered (stream, "%s",
181                         longest_to_int (exp->elts[pc + 1].longconst)
182                         ? "TRUE" : "FALSE");
183       return;
184
185     case OP_INTERNALVAR:
186       (*pos) += 2;
187       fprintf_filtered (stream, "$%s",
188                         internalvar_name (exp->elts[pc + 1].internalvar));
189       return;
190
191     case OP_FUNCALL:
192     case OP_F77_UNDETERMINED_ARGLIST:
193       (*pos) += 2;
194       nargs = longest_to_int (exp->elts[pc + 1].longconst);
195       print_subexp (exp, pos, stream, PREC_SUFFIX);
196       fputs_filtered (" (", stream);
197       for (tem = 0; tem < nargs; tem++)
198         {
199           if (tem != 0)
200             fputs_filtered (", ", stream);
201           print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
202         }
203       fputs_filtered (")", stream);
204       return;
205
206     case OP_NAME:
207       nargs = longest_to_int (exp->elts[pc + 1].longconst);
208       (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
209       fputs_filtered (&exp->elts[pc + 2].string, stream);
210       return;
211
212     case OP_STRING:
213       {
214         struct value_print_options opts;
215
216         nargs = longest_to_int (exp->elts[pc + 1].longconst);
217         (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
218         /* LA_PRINT_STRING will print using the current repeat count threshold.
219            If necessary, we can temporarily set it to zero, or pass it as an
220            additional parameter to LA_PRINT_STRING.  -fnf */
221         get_user_print_options (&opts);
222         LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
223                          (gdb_byte *) &exp->elts[pc + 2].string, nargs,
224                          NULL, 0, &opts);
225       }
226       return;
227
228     case OP_OBJC_NSSTRING:      /* Objective-C Foundation Class
229                                    NSString constant.  */
230       {
231         struct value_print_options opts;
232
233         nargs = longest_to_int (exp->elts[pc + 1].longconst);
234         (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
235         fputs_filtered ("@\"", stream);
236         get_user_print_options (&opts);
237         LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
238                          (gdb_byte *) &exp->elts[pc + 2].string, nargs,
239                          NULL, 0, &opts);
240         fputs_filtered ("\"", stream);
241       }
242       return;
243
244     case OP_OBJC_MSGCALL:
245       {                 /* Objective C message (method) call.  */
246         gdb::unique_xmalloc_ptr<char> selector;
247
248         (*pos) += 3;
249         nargs = longest_to_int (exp->elts[pc + 2].longconst);
250         fprintf_unfiltered (stream, "[");
251         print_subexp (exp, pos, stream, PREC_SUFFIX);
252         if (0 == target_read_string (exp->elts[pc + 1].longconst,
253                                      &selector, 1024, NULL))
254           {
255             error (_("bad selector"));
256             return;
257           }
258         if (nargs)
259           {
260             char *s, *nextS;
261
262             s = selector.get ();
263             for (tem = 0; tem < nargs; tem++)
264               {
265                 nextS = strchr (s, ':');
266                 gdb_assert (nextS);     /* Make sure we found ':'.  */
267                 *nextS = '\0';
268                 fprintf_unfiltered (stream, " %s: ", s);
269                 s = nextS + 1;
270                 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
271               }
272           }
273         else
274           {
275             fprintf_unfiltered (stream, " %s", selector.get ());
276           }
277         fprintf_unfiltered (stream, "]");
278         return;
279       }
280
281     case OP_ARRAY:
282       (*pos) += 3;
283       nargs = longest_to_int (exp->elts[pc + 2].longconst);
284       nargs -= longest_to_int (exp->elts[pc + 1].longconst);
285       nargs++;
286       tem = 0;
287       if (exp->elts[pc + 4].opcode == OP_LONG
288           && exp->elts[pc + 5].type
289              == builtin_type (exp->gdbarch)->builtin_char
290           && exp->language_defn->la_language == language_c)
291         {
292           /* Attempt to print C character arrays using string syntax.
293              Walk through the args, picking up one character from each
294              of the OP_LONG expression elements.  If any array element
295              does not match our expection of what we should find for
296              a simple string, revert back to array printing.  Note that
297              the last expression element is an explicit null terminator
298              byte, which doesn't get printed.  */
299           tempstr = (char *) alloca (nargs);
300           pc += 4;
301           while (tem < nargs)
302             {
303               if (exp->elts[pc].opcode != OP_LONG
304                   || exp->elts[pc + 1].type
305                      != builtin_type (exp->gdbarch)->builtin_char)
306                 {
307                   /* Not a simple array of char, use regular array
308                      printing.  */
309                   tem = 0;
310                   break;
311                 }
312               else
313                 {
314                   tempstr[tem++] =
315                     longest_to_int (exp->elts[pc + 2].longconst);
316                   pc += 4;
317                 }
318             }
319         }
320       if (tem > 0)
321         {
322           struct value_print_options opts;
323
324           get_user_print_options (&opts);
325           LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
326                            (gdb_byte *) tempstr, nargs - 1, NULL, 0, &opts);
327           (*pos) = pc;
328         }
329       else
330         {
331           fputs_filtered (" {", stream);
332           for (tem = 0; tem < nargs; tem++)
333             {
334               if (tem != 0)
335                 {
336                   fputs_filtered (", ", stream);
337                 }
338               print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
339             }
340           fputs_filtered ("}", stream);
341         }
342       return;
343
344     case TERNOP_COND:
345       if ((int) prec > (int) PREC_COMMA)
346         fputs_filtered ("(", stream);
347       /* Print the subexpressions, forcing parentheses
348          around any binary operations within them.
349          This is more parentheses than are strictly necessary,
350          but it looks clearer.  */
351       print_subexp (exp, pos, stream, PREC_HYPER);
352       fputs_filtered (" ? ", stream);
353       print_subexp (exp, pos, stream, PREC_HYPER);
354       fputs_filtered (" : ", stream);
355       print_subexp (exp, pos, stream, PREC_HYPER);
356       if ((int) prec > (int) PREC_COMMA)
357         fputs_filtered (")", stream);
358       return;
359
360     case TERNOP_SLICE:
361       print_subexp (exp, pos, stream, PREC_SUFFIX);
362       fputs_filtered ("(", stream);
363       print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
364       fputs_filtered (opcode == TERNOP_SLICE ? " : " : " UP ", stream);
365       print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
366       fputs_filtered (")", stream);
367       return;
368
369     case STRUCTOP_STRUCT:
370       tem = longest_to_int (exp->elts[pc + 1].longconst);
371       (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
372       print_subexp (exp, pos, stream, PREC_SUFFIX);
373       fputs_filtered (".", stream);
374       fputs_filtered (&exp->elts[pc + 2].string, stream);
375       return;
376
377       /* Will not occur for Modula-2.  */
378     case STRUCTOP_PTR:
379       tem = longest_to_int (exp->elts[pc + 1].longconst);
380       (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
381       print_subexp (exp, pos, stream, PREC_SUFFIX);
382       fputs_filtered ("->", stream);
383       fputs_filtered (&exp->elts[pc + 2].string, stream);
384       return;
385
386     case STRUCTOP_MEMBER:
387       print_subexp (exp, pos, stream, PREC_SUFFIX);
388       fputs_filtered (".*", stream);
389       print_subexp (exp, pos, stream, PREC_SUFFIX);
390       return;
391
392     case STRUCTOP_MPTR:
393       print_subexp (exp, pos, stream, PREC_SUFFIX);
394       fputs_filtered ("->*", stream);
395       print_subexp (exp, pos, stream, PREC_SUFFIX);
396       return;
397
398     case BINOP_SUBSCRIPT:
399       print_subexp (exp, pos, stream, PREC_SUFFIX);
400       fputs_filtered ("[", stream);
401       print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
402       fputs_filtered ("]", stream);
403       return;
404
405     case UNOP_POSTINCREMENT:
406       print_subexp (exp, pos, stream, PREC_SUFFIX);
407       fputs_filtered ("++", stream);
408       return;
409
410     case UNOP_POSTDECREMENT:
411       print_subexp (exp, pos, stream, PREC_SUFFIX);
412       fputs_filtered ("--", stream);
413       return;
414
415     case UNOP_CAST:
416       (*pos) += 2;
417       if ((int) prec > (int) PREC_PREFIX)
418         fputs_filtered ("(", stream);
419       fputs_filtered ("(", stream);
420       type_print (exp->elts[pc + 1].type, "", stream, 0);
421       fputs_filtered (") ", stream);
422       print_subexp (exp, pos, stream, PREC_PREFIX);
423       if ((int) prec > (int) PREC_PREFIX)
424         fputs_filtered (")", stream);
425       return;
426
427     case UNOP_CAST_TYPE:
428       if ((int) prec > (int) PREC_PREFIX)
429         fputs_filtered ("(", stream);
430       fputs_filtered ("(", stream);
431       print_subexp (exp, pos, stream, PREC_PREFIX);
432       fputs_filtered (") ", stream);
433       print_subexp (exp, pos, stream, PREC_PREFIX);
434       if ((int) prec > (int) PREC_PREFIX)
435         fputs_filtered (")", stream);
436       return;
437
438     case UNOP_DYNAMIC_CAST:
439     case UNOP_REINTERPRET_CAST:
440       fputs_filtered (opcode == UNOP_DYNAMIC_CAST ? "dynamic_cast"
441                       : "reinterpret_cast", stream);
442       fputs_filtered ("<", stream);
443       print_subexp (exp, pos, stream, PREC_PREFIX);
444       fputs_filtered ("> (", stream);
445       print_subexp (exp, pos, stream, PREC_PREFIX);
446       fputs_filtered (")", stream);
447       return;
448
449     case UNOP_MEMVAL:
450       (*pos) += 2;
451       if ((int) prec > (int) PREC_PREFIX)
452         fputs_filtered ("(", stream);
453       if (TYPE_CODE (exp->elts[pc + 1].type) == TYPE_CODE_FUNC
454           && exp->elts[pc + 3].opcode == OP_LONG)
455         {
456           struct value_print_options opts;
457
458           /* We have a minimal symbol fn, probably.  It's encoded
459              as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
460              Swallow the OP_LONG (including both its opcodes); ignore
461              its type; print the value in the type of the MEMVAL.  */
462           (*pos) += 4;
463           val = value_at_lazy (exp->elts[pc + 1].type,
464                                (CORE_ADDR) exp->elts[pc + 5].longconst);
465           get_no_prettyformat_print_options (&opts);
466           value_print (val, stream, &opts);
467         }
468       else
469         {
470           fputs_filtered ("{", stream);
471           type_print (exp->elts[pc + 1].type, "", stream, 0);
472           fputs_filtered ("} ", stream);
473           print_subexp (exp, pos, stream, PREC_PREFIX);
474         }
475       if ((int) prec > (int) PREC_PREFIX)
476         fputs_filtered (")", stream);
477       return;
478
479     case UNOP_MEMVAL_TYPE:
480       if ((int) prec > (int) PREC_PREFIX)
481         fputs_filtered ("(", stream);
482       fputs_filtered ("{", stream);
483       print_subexp (exp, pos, stream, PREC_PREFIX);
484       fputs_filtered ("} ", stream);
485       print_subexp (exp, pos, stream, PREC_PREFIX);
486       if ((int) prec > (int) PREC_PREFIX)
487         fputs_filtered (")", stream);
488       return;
489
490     case BINOP_ASSIGN_MODIFY:
491       opcode = exp->elts[pc + 1].opcode;
492       (*pos) += 2;
493       myprec = PREC_ASSIGN;
494       assoc = 1;
495       assign_modify = 1;
496       op_str = "???";
497       for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
498         if (op_print_tab[tem].opcode == opcode)
499           {
500             op_str = op_print_tab[tem].string;
501             break;
502           }
503       if (op_print_tab[tem].opcode != opcode)
504         /* Not found; don't try to keep going because we don't know how
505            to interpret further elements.  */
506         error (_("Invalid expression"));
507       break;
508
509       /* C++ ops */
510
511     case OP_THIS:
512       ++(*pos);
513       if (exp->language_defn->la_name_of_this)
514         fputs_filtered (exp->language_defn->la_name_of_this, stream);
515       else
516         fprintf_filtered (stream, _("<language %s has no 'this'>"),
517                           exp->language_defn->la_name);
518       return;
519
520       /* Modula-2 ops */
521
522     case MULTI_SUBSCRIPT:
523       (*pos) += 2;
524       nargs = longest_to_int (exp->elts[pc + 1].longconst);
525       print_subexp (exp, pos, stream, PREC_SUFFIX);
526       fprintf_unfiltered (stream, " [");
527       for (tem = 0; tem < nargs; tem++)
528         {
529           if (tem != 0)
530             fprintf_unfiltered (stream, ", ");
531           print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
532         }
533       fprintf_unfiltered (stream, "]");
534       return;
535
536     case BINOP_VAL:
537       (*pos) += 2;
538       fprintf_unfiltered (stream, "VAL(");
539       type_print (exp->elts[pc + 1].type, "", stream, 0);
540       fprintf_unfiltered (stream, ",");
541       print_subexp (exp, pos, stream, PREC_PREFIX);
542       fprintf_unfiltered (stream, ")");
543       return;
544
545     case TYPE_INSTANCE:
546       {
547         type_instance_flags flags
548           = (type_instance_flag_value) longest_to_int (exp->elts[pc + 1].longconst);
549         LONGEST count = exp->elts[pc + 2].longconst;
550
551         /* The FLAGS.  */
552         (*pos)++;
553         /* The COUNT.  */
554         (*pos)++;
555         fputs_unfiltered ("TypeInstance(", stream);
556         while (count-- > 0)
557           {
558             type_print (exp->elts[(*pos)++].type, "", stream, 0);
559             if (count > 0)
560               fputs_unfiltered (",", stream);
561           }
562         fputs_unfiltered (",", stream);
563         /* Ending COUNT and ending TYPE_INSTANCE.  */
564         (*pos) += 2;
565         print_subexp (exp, pos, stream, PREC_PREFIX);
566
567         if (flags & TYPE_INSTANCE_FLAG_CONST)
568           fputs_unfiltered (",const", stream);
569         if (flags & TYPE_INSTANCE_FLAG_VOLATILE)
570           fputs_unfiltered (",volatile", stream);
571
572         fputs_unfiltered (")", stream);
573         return;
574       }
575
576     case OP_RANGE:
577       {
578         enum range_type range_type;
579
580         range_type = (enum range_type)
581           longest_to_int (exp->elts[pc + 1].longconst);
582         *pos += 2;
583
584         if (range_type == NONE_BOUND_DEFAULT_EXCLUSIVE
585             || range_type == LOW_BOUND_DEFAULT_EXCLUSIVE)
586           fputs_filtered ("EXCLUSIVE_", stream);
587         fputs_filtered ("RANGE(", stream);
588         if (range_type == HIGH_BOUND_DEFAULT
589             || range_type == NONE_BOUND_DEFAULT
590             || range_type == NONE_BOUND_DEFAULT_EXCLUSIVE)
591           print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
592         fputs_filtered ("..", stream);
593         if (range_type == LOW_BOUND_DEFAULT
594             || range_type == NONE_BOUND_DEFAULT)
595           print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
596         fputs_filtered (")", stream);
597         return;
598       }
599
600       /* Default ops */
601
602     default:
603       op_str = "???";
604       for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
605         if (op_print_tab[tem].opcode == opcode)
606           {
607             op_str = op_print_tab[tem].string;
608             myprec = op_print_tab[tem].precedence;
609             assoc = op_print_tab[tem].right_assoc;
610             break;
611           }
612       if (op_print_tab[tem].opcode != opcode)
613         /* Not found; don't try to keep going because we don't know how
614            to interpret further elements.  For example, this happens
615            if opcode is OP_TYPE.  */
616         error (_("Invalid expression"));
617     }
618
619   /* Note that PREC_BUILTIN will always emit parentheses.  */
620   if ((int) myprec < (int) prec)
621     fputs_filtered ("(", stream);
622   if ((int) opcode > (int) BINOP_END)
623     {
624       if (assoc)
625         {
626           /* Unary postfix operator.  */
627           print_subexp (exp, pos, stream, PREC_SUFFIX);
628           fputs_filtered (op_str, stream);
629         }
630       else
631         {
632           /* Unary prefix operator.  */
633           fputs_filtered (op_str, stream);
634           if (myprec == PREC_BUILTIN_FUNCTION)
635             fputs_filtered ("(", stream);
636           print_subexp (exp, pos, stream, PREC_PREFIX);
637           if (myprec == PREC_BUILTIN_FUNCTION)
638             fputs_filtered (")", stream);
639         }
640     }
641   else
642     {
643       /* Binary operator.  */
644       /* Print left operand.
645          If operator is right-associative,
646          increment precedence for this operand.  */
647       print_subexp (exp, pos, stream,
648                     (enum precedence) ((int) myprec + assoc));
649       /* Print the operator itself.  */
650       if (assign_modify)
651         fprintf_filtered (stream, " %s= ", op_str);
652       else if (op_str[0] == ',')
653         fprintf_filtered (stream, "%s ", op_str);
654       else
655         fprintf_filtered (stream, " %s ", op_str);
656       /* Print right operand.
657          If operator is left-associative,
658          increment precedence for this operand.  */
659       print_subexp (exp, pos, stream,
660                     (enum precedence) ((int) myprec + !assoc));
661     }
662
663   if ((int) myprec < (int) prec)
664     fputs_filtered (")", stream);
665 }
666
667 /* Return the operator corresponding to opcode OP as
668    a string.   NULL indicates that the opcode was not found in the
669    current language table.  */
670 const char *
671 op_string (enum exp_opcode op)
672 {
673   int tem;
674   const struct op_print *op_print_tab;
675
676   op_print_tab = current_language->la_op_print_tab;
677   for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
678     if (op_print_tab[tem].opcode == op)
679       return op_print_tab[tem].string;
680   return NULL;
681 }
682
683 /* Support for dumping the raw data from expressions in a human readable
684    form.  */
685
686 static int dump_subexp_body (struct expression *exp, struct ui_file *, int);
687
688 /* Name for OPCODE, when it appears in expression EXP.  */
689
690 const char *
691 op_name (struct expression *exp, enum exp_opcode opcode)
692 {
693   if (opcode >= OP_UNUSED_LAST)
694     {
695       char *cell = get_print_cell ();
696       xsnprintf (cell, PRINT_CELL_SIZE, "unknown opcode: %u",
697                  unsigned (opcode));
698       return cell;
699     }
700   return exp->language_defn->la_exp_desc->op_name (opcode);
701 }
702
703 /* Default name for the standard operator OPCODE (i.e., one defined in
704    the definition of enum exp_opcode).  */
705
706 const char *
707 op_name_standard (enum exp_opcode opcode)
708 {
709   switch (opcode)
710     {
711     default:
712       {
713         static char buf[30];
714
715         xsnprintf (buf, sizeof (buf), "<unknown %d>", opcode);
716         return buf;
717       }
718 #define OP(name)        \
719     case name:          \
720       return #name ;
721 #include "std-operator.def"
722 #undef OP
723     }
724 }
725
726 /* Print a raw dump of expression EXP to STREAM.
727    NOTE, if non-NULL, is printed as extra explanatory text.  */
728
729 void
730 dump_raw_expression (struct expression *exp, struct ui_file *stream,
731                      const char *note)
732 {
733   int elt;
734   char *eltscan;
735   int eltsize;
736
737   fprintf_filtered (stream, "Dump of expression @ ");
738   gdb_print_host_address (exp, stream);
739   if (note)
740     fprintf_filtered (stream, ", %s:", note);
741   fprintf_filtered (stream, "\n\tLanguage %s, %d elements, %ld bytes each.\n",
742                     exp->language_defn->la_name, exp->nelts,
743                     (long) sizeof (union exp_element));
744   fprintf_filtered (stream, "\t%5s  %20s  %16s  %s\n", "Index", "Opcode",
745                     "Hex Value", "String Value");
746   for (elt = 0; elt < exp->nelts; elt++)
747     {
748       fprintf_filtered (stream, "\t%5d  ", elt);
749
750       const char *opcode_name = op_name (exp, exp->elts[elt].opcode);
751       fprintf_filtered (stream, "%20s  ", opcode_name);
752
753       print_longest (stream, 'd', 0, exp->elts[elt].longconst);
754       fprintf_filtered (stream, "  ");
755
756       for (eltscan = (char *) &exp->elts[elt],
757            eltsize = sizeof (union exp_element);
758            eltsize-- > 0;
759            eltscan++)
760         {
761           fprintf_filtered (stream, "%c",
762                             isprint (*eltscan) ? (*eltscan & 0xFF) : '.');
763         }
764       fprintf_filtered (stream, "\n");
765     }
766 }
767
768 /* Dump the subexpression of prefix expression EXP whose operator is at
769    position ELT onto STREAM.  Returns the position of the next 
770    subexpression in EXP.  */
771
772 int
773 dump_subexp (struct expression *exp, struct ui_file *stream, int elt)
774 {
775   static int indent = 0;
776   int i;
777
778   fprintf_filtered (stream, "\n");
779   fprintf_filtered (stream, "\t%5d  ", elt);
780
781   for (i = 1; i <= indent; i++)
782     fprintf_filtered (stream, " ");
783   indent += 2;
784
785   fprintf_filtered (stream, "%-20s  ", op_name (exp, exp->elts[elt].opcode));
786
787   elt = dump_subexp_body (exp, stream, elt);
788
789   indent -= 2;
790
791   return elt;
792 }
793
794 /* Dump the operands of prefix expression EXP whose opcode is at
795    position ELT onto STREAM.  Returns the position of the next 
796    subexpression in EXP.  */
797
798 static int
799 dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
800 {
801   return exp->language_defn->la_exp_desc->dump_subexp_body (exp, stream, elt);
802 }
803
804 /* Default value for subexp_body in exp_descriptor vector.  */
805
806 int
807 dump_subexp_body_standard (struct expression *exp, 
808                            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       elt = dump_subexp (exp, stream, elt);
817       /* FALL THROUGH */
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_CONCAT:
848     case BINOP_END:
849     case STRUCTOP_MEMBER:
850     case STRUCTOP_MPTR:
851       elt = dump_subexp (exp, stream, elt);
852       /* FALL THROUGH */
853     case UNOP_NEG:
854     case UNOP_LOGICAL_NOT:
855     case UNOP_COMPLEMENT:
856     case UNOP_IND:
857     case UNOP_ADDR:
858     case UNOP_PREINCREMENT:
859     case UNOP_POSTINCREMENT:
860     case UNOP_PREDECREMENT:
861     case UNOP_POSTDECREMENT:
862     case UNOP_SIZEOF:
863     case UNOP_ALIGNOF:
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_KIND:
876       elt = dump_subexp (exp, stream, elt);
877       break;
878     case OP_LONG:
879       fprintf_filtered (stream, "Type @");
880       gdb_print_host_address (exp->elts[elt].type, stream);
881       fprintf_filtered (stream, " (");
882       type_print (exp->elts[elt].type, NULL, stream, 0);
883       fprintf_filtered (stream, "), value %ld (0x%lx)",
884                         (long) exp->elts[elt + 1].longconst,
885                         (long) exp->elts[elt + 1].longconst);
886       elt += 3;
887       break;
888     case OP_FLOAT:
889       fprintf_filtered (stream, "Type @");
890       gdb_print_host_address (exp->elts[elt].type, stream);
891       fprintf_filtered (stream, " (");
892       type_print (exp->elts[elt].type, NULL, stream, 0);
893       fprintf_filtered (stream, "), value ");
894       print_floating (exp->elts[elt + 1].floatconst,
895                       exp->elts[elt].type, stream);
896       elt += 3;
897       break;
898     case OP_VAR_VALUE:
899       fprintf_filtered (stream, "Block @");
900       gdb_print_host_address (exp->elts[elt].block, stream);
901       fprintf_filtered (stream, ", symbol @");
902       gdb_print_host_address (exp->elts[elt + 1].symbol, stream);
903       fprintf_filtered (stream, " (%s)",
904                         SYMBOL_PRINT_NAME (exp->elts[elt + 1].symbol));
905       elt += 3;
906       break;
907     case OP_VAR_MSYM_VALUE:
908       fprintf_filtered (stream, "Objfile @");
909       gdb_print_host_address (exp->elts[elt].objfile, stream);
910       fprintf_filtered (stream, ", msymbol @");
911       gdb_print_host_address (exp->elts[elt + 1].msymbol, stream);
912       fprintf_filtered (stream, " (%s)",
913                         MSYMBOL_PRINT_NAME (exp->elts[elt + 1].msymbol));
914       elt += 3;
915       break;
916     case OP_VAR_ENTRY_VALUE:
917       fprintf_filtered (stream, "Entry value of symbol @");
918       gdb_print_host_address (exp->elts[elt].symbol, stream);
919       fprintf_filtered (stream, " (%s)",
920                         SYMBOL_PRINT_NAME (exp->elts[elt].symbol));
921       elt += 2;
922       break;
923     case OP_LAST:
924       fprintf_filtered (stream, "History element %ld",
925                         (long) exp->elts[elt].longconst);
926       elt += 2;
927       break;
928     case OP_REGISTER:
929       fprintf_filtered (stream, "Register $%s", &exp->elts[elt + 1].string);
930       elt += 3 + BYTES_TO_EXP_ELEM (exp->elts[elt].longconst + 1);
931       break;
932     case OP_INTERNALVAR:
933       fprintf_filtered (stream, "Internal var @");
934       gdb_print_host_address (exp->elts[elt].internalvar, stream);
935       fprintf_filtered (stream, " (%s)",
936                         internalvar_name (exp->elts[elt].internalvar));
937       elt += 2;
938       break;
939     case OP_FUNCALL:
940     case OP_F77_UNDETERMINED_ARGLIST:
941       {
942         int i, nargs;
943
944         nargs = longest_to_int (exp->elts[elt].longconst);
945
946         fprintf_filtered (stream, "Number of args: %d", nargs);
947         elt += 2;
948
949         for (i = 1; i <= nargs + 1; i++)
950           elt = dump_subexp (exp, stream, elt);
951       }
952       break;
953     case OP_ARRAY:
954       {
955         int lower, upper;
956         int i;
957
958         lower = longest_to_int (exp->elts[elt].longconst);
959         upper = longest_to_int (exp->elts[elt + 1].longconst);
960
961         fprintf_filtered (stream, "Bounds [%d:%d]", lower, upper);
962         elt += 3;
963
964         for (i = 1; i <= upper - lower + 1; i++)
965           elt = dump_subexp (exp, stream, elt);
966       }
967       break;
968     case UNOP_DYNAMIC_CAST:
969     case UNOP_REINTERPRET_CAST:
970     case UNOP_CAST_TYPE:
971     case UNOP_MEMVAL_TYPE:
972       fprintf_filtered (stream, " (");
973       elt = dump_subexp (exp, stream, elt);
974       fprintf_filtered (stream, ")");
975       elt = dump_subexp (exp, stream, elt);
976       break;
977     case UNOP_MEMVAL:
978     case UNOP_CAST:
979       fprintf_filtered (stream, "Type @");
980       gdb_print_host_address (exp->elts[elt].type, stream);
981       fprintf_filtered (stream, " (");
982       type_print (exp->elts[elt].type, NULL, stream, 0);
983       fprintf_filtered (stream, ")");
984       elt = dump_subexp (exp, stream, elt + 2);
985       break;
986     case OP_TYPE:
987       fprintf_filtered (stream, "Type @");
988       gdb_print_host_address (exp->elts[elt].type, stream);
989       fprintf_filtered (stream, " (");
990       type_print (exp->elts[elt].type, NULL, stream, 0);
991       fprintf_filtered (stream, ")");
992       elt += 2;
993       break;
994     case OP_TYPEOF:
995     case OP_DECLTYPE:
996       fprintf_filtered (stream, "Typeof (");
997       elt = dump_subexp (exp, stream, elt);
998       fprintf_filtered (stream, ")");
999       break;
1000     case OP_TYPEID:
1001       fprintf_filtered (stream, "typeid (");
1002       elt = dump_subexp (exp, stream, elt);
1003       fprintf_filtered (stream, ")");
1004       break;
1005     case STRUCTOP_STRUCT:
1006     case STRUCTOP_PTR:
1007       {
1008         char *elem_name;
1009         int len;
1010
1011         len = longest_to_int (exp->elts[elt].longconst);
1012         elem_name = &exp->elts[elt + 1].string;
1013
1014         fprintf_filtered (stream, "Element name: `%.*s'", len, elem_name);
1015         elt = dump_subexp (exp, stream, elt + 3 + BYTES_TO_EXP_ELEM (len + 1));
1016       }
1017       break;
1018     case OP_SCOPE:
1019       {
1020         char *elem_name;
1021         int len;
1022
1023         fprintf_filtered (stream, "Type @");
1024         gdb_print_host_address (exp->elts[elt].type, stream);
1025         fprintf_filtered (stream, " (");
1026         type_print (exp->elts[elt].type, NULL, stream, 0);
1027         fprintf_filtered (stream, ") ");
1028
1029         len = longest_to_int (exp->elts[elt + 1].longconst);
1030         elem_name = &exp->elts[elt + 2].string;
1031
1032         fprintf_filtered (stream, "Field name: `%.*s'", len, elem_name);
1033         elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1034       }
1035       break;
1036
1037     case OP_FUNC_STATIC_VAR:
1038       {
1039         int len = longest_to_int (exp->elts[elt].longconst);
1040         const char *var_name = &exp->elts[elt + 1].string;
1041         fprintf_filtered (stream, "Field name: `%.*s'", len, var_name);
1042         elt += 3 + BYTES_TO_EXP_ELEM (len + 1);
1043       }
1044       break;
1045
1046     case TYPE_INSTANCE:
1047       {
1048         type_instance_flags flags
1049           = (type_instance_flag_value) longest_to_int (exp->elts[elt++].longconst);
1050         LONGEST len = exp->elts[elt++].longconst;
1051         fprintf_filtered (stream, "%s TypeInstance: ", plongest (len));
1052         while (len-- > 0)
1053           {
1054             fprintf_filtered (stream, "Type @");
1055             gdb_print_host_address (exp->elts[elt].type, stream);
1056             fprintf_filtered (stream, " (");
1057             type_print (exp->elts[elt].type, NULL, stream, 0);
1058             fprintf_filtered (stream, ")");
1059             elt++;
1060             if (len > 0)
1061               fputs_filtered (", ", stream);
1062           }
1063
1064         fprintf_filtered (stream, " Flags: %s (", hex_string (flags));
1065         bool space = false;
1066         auto print_one = [&] (const char *mod)
1067           {
1068             if (space)
1069               fputs_filtered (" ", stream);
1070             space = true;
1071             fprintf_filtered (stream, "%s", mod);
1072           };
1073         if (flags & TYPE_INSTANCE_FLAG_CONST)
1074           print_one ("const");
1075         if (flags & TYPE_INSTANCE_FLAG_VOLATILE)
1076           print_one ("volatile");
1077         fprintf_filtered (stream, ")");
1078
1079         /* Ending LEN and ending TYPE_INSTANCE.  */
1080         elt += 2;
1081         elt = dump_subexp (exp, stream, elt);
1082       }
1083       break;
1084     case OP_STRING:
1085       {
1086         LONGEST len = exp->elts[elt].longconst;
1087         LONGEST type = exp->elts[elt + 1].longconst;
1088
1089         fprintf_filtered (stream, "Language-specific string type: %s",
1090                           plongest (type));
1091
1092         /* Skip length.  */
1093         elt += 1;
1094
1095         /* Skip string content. */
1096         elt += BYTES_TO_EXP_ELEM (len);
1097
1098         /* Skip length and ending OP_STRING. */
1099         elt += 2;
1100       }
1101       break;
1102     case OP_RANGE:
1103       {
1104         enum range_type range_type;
1105
1106         range_type = (enum range_type)
1107           longest_to_int (exp->elts[elt].longconst);
1108         elt += 2;
1109
1110         switch (range_type)
1111           {
1112           case BOTH_BOUND_DEFAULT:
1113             fputs_filtered ("Range '..'", stream);
1114             break;
1115           case LOW_BOUND_DEFAULT:
1116             fputs_filtered ("Range '..EXP'", stream);
1117             break;
1118           case LOW_BOUND_DEFAULT_EXCLUSIVE:
1119             fputs_filtered ("ExclusiveRange '..EXP'", stream);
1120             break;
1121           case HIGH_BOUND_DEFAULT:
1122             fputs_filtered ("Range 'EXP..'", stream);
1123             break;
1124           case NONE_BOUND_DEFAULT:
1125             fputs_filtered ("Range 'EXP..EXP'", stream);
1126             break;
1127           case NONE_BOUND_DEFAULT_EXCLUSIVE:
1128             fputs_filtered ("ExclusiveRange 'EXP..EXP'", stream);
1129             break;
1130           default:
1131             fputs_filtered ("Invalid Range!", stream);
1132             break;
1133           }
1134
1135         if (range_type == HIGH_BOUND_DEFAULT
1136             || range_type == NONE_BOUND_DEFAULT)
1137           elt = dump_subexp (exp, stream, elt);
1138         if (range_type == LOW_BOUND_DEFAULT
1139             || range_type == NONE_BOUND_DEFAULT)
1140           elt = dump_subexp (exp, stream, elt);
1141       }
1142       break;
1143
1144     default:
1145     case OP_NULL:
1146     case MULTI_SUBSCRIPT:
1147     case OP_COMPLEX:
1148     case OP_BOOL:
1149     case OP_M2_STRING:
1150     case OP_THIS:
1151     case OP_NAME:
1152       fprintf_filtered (stream, "Unknown format");
1153     }
1154
1155   return elt;
1156 }
1157
1158 void
1159 dump_prefix_expression (struct expression *exp, struct ui_file *stream)
1160 {
1161   int elt;
1162
1163   fprintf_filtered (stream, "Dump of expression @ ");
1164   gdb_print_host_address (exp, stream);
1165   fputs_filtered (", after conversion to prefix form:\nExpression: `", stream);
1166   print_expression (exp, stream);
1167   fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
1168                     exp->language_defn->la_name, exp->nelts,
1169                     (long) sizeof (union exp_element));
1170   fputs_filtered ("\n", stream);
1171
1172   for (elt = 0; elt < exp->nelts;)
1173     elt = dump_subexp (exp, stream, elt);
1174   fputs_filtered ("\n", stream);
1175 }