gdb/expprint.c - gdb

Functions defined

Macros defined

Source code

  1. /* Print in infix form a struct expression.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "gdbtypes.h"
  17. #include "expression.h"
  18. #include "value.h"
  19. #include "language.h"
  20. #include "parser-defs.h"
  21. #include "user-regs.h"                /* For user_reg_map_regnum_to_name.  */
  22. #include "target.h"
  23. #include "block.h"
  24. #include "objfiles.h"
  25. #include "valprint.h"

  26. #include <ctype.h>

  27. void
  28. print_expression (struct expression *exp, struct ui_file *stream)
  29. {
  30.   int pc = 0;

  31.   print_subexp (exp, &pc, stream, PREC_NULL);
  32. }

  33. /* Print the subexpression of EXP that starts in position POS, on STREAM.
  34.    PREC is the precedence of the surrounding operator;
  35.    if the precedence of the main operator of this subexpression is less,
  36.    parentheses are needed here.  */

  37. void
  38. print_subexp (struct expression *exp, int *pos,
  39.               struct ui_file *stream, enum precedence prec)
  40. {
  41.   exp->language_defn->la_exp_desc->print_subexp (exp, pos, stream, prec);
  42. }

  43. /* Standard implementation of print_subexp for use in language_defn
  44.    vectors.  */
  45. void
  46. print_subexp_standard (struct expression *exp, int *pos,
  47.                        struct ui_file *stream, enum precedence prec)
  48. {
  49.   unsigned tem;
  50.   const struct op_print *op_print_tab;
  51.   int pc;
  52.   unsigned nargs;
  53.   char *op_str;
  54.   int assign_modify = 0;
  55.   enum exp_opcode opcode;
  56.   enum precedence myprec = PREC_NULL;
  57.   /* Set to 1 for a right-associative operator.  */
  58.   int assoc = 0;
  59.   struct value *val;
  60.   char *tempstr = NULL;

  61.   op_print_tab = exp->language_defn->la_op_print_tab;
  62.   pc = (*pos)++;
  63.   opcode = exp->elts[pc].opcode;
  64.   switch (opcode)
  65.     {
  66.       /* Common ops */

  67.     case OP_TYPE:
  68.       (*pos) += 2;
  69.       type_print (exp->elts[pc + 1].type, "", stream, 0);
  70.       return;

  71.     case OP_SCOPE:
  72.       myprec = PREC_PREFIX;
  73.       assoc = 0;
  74.       fputs_filtered (type_name_no_tag (exp->elts[pc + 1].type), stream);
  75.       fputs_filtered ("::", stream);
  76.       nargs = longest_to_int (exp->elts[pc + 2].longconst);
  77.       (*pos) += 4 + BYTES_TO_EXP_ELEM (nargs + 1);
  78.       fputs_filtered (&exp->elts[pc + 3].string, stream);
  79.       return;

  80.     case OP_LONG:
  81.       {
  82.         struct value_print_options opts;

  83.         get_no_prettyformat_print_options (&opts);
  84.         (*pos) += 3;
  85.         value_print (value_from_longest (exp->elts[pc + 1].type,
  86.                                          exp->elts[pc + 2].longconst),
  87.                      stream, &opts);
  88.       }
  89.       return;

  90.     case OP_DOUBLE:
  91.       {
  92.         struct value_print_options opts;

  93.         get_no_prettyformat_print_options (&opts);
  94.         (*pos) += 3;
  95.         value_print (value_from_double (exp->elts[pc + 1].type,
  96.                                         exp->elts[pc + 2].doubleconst),
  97.                      stream, &opts);
  98.       }
  99.       return;

  100.     case OP_VAR_VALUE:
  101.       {
  102.         const struct block *b;

  103.         (*pos) += 3;
  104.         b = exp->elts[pc + 1].block;
  105.         if (b != NULL
  106.             && BLOCK_FUNCTION (b) != NULL
  107.             && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)) != NULL)
  108.           {
  109.             fputs_filtered (SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)), stream);
  110.             fputs_filtered ("::", stream);
  111.           }
  112.         fputs_filtered (SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol), stream);
  113.       }
  114.       return;

  115.     case OP_VAR_ENTRY_VALUE:
  116.       {
  117.         (*pos) += 2;
  118.         fprintf_filtered (stream, "%s@entry",
  119.                           SYMBOL_PRINT_NAME (exp->elts[pc + 1].symbol));
  120.       }
  121.       return;

  122.     case OP_LAST:
  123.       (*pos) += 2;
  124.       fprintf_filtered (stream, "$%d",
  125.                         longest_to_int (exp->elts[pc + 1].longconst));
  126.       return;

  127.     case OP_REGISTER:
  128.       {
  129.         const char *name = &exp->elts[pc + 2].string;

  130.         (*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
  131.         fprintf_filtered (stream, "$%s", name);
  132.         return;
  133.       }

  134.     case OP_BOOL:
  135.       (*pos) += 2;
  136.       fprintf_filtered (stream, "%s",
  137.                         longest_to_int (exp->elts[pc + 1].longconst)
  138.                         ? "TRUE" : "FALSE");
  139.       return;

  140.     case OP_INTERNALVAR:
  141.       (*pos) += 2;
  142.       fprintf_filtered (stream, "$%s",
  143.                         internalvar_name (exp->elts[pc + 1].internalvar));
  144.       return;

  145.     case OP_FUNCALL:
  146.       (*pos) += 2;
  147.       nargs = longest_to_int (exp->elts[pc + 1].longconst);
  148.       print_subexp (exp, pos, stream, PREC_SUFFIX);
  149.       fputs_filtered (" (", stream);
  150.       for (tem = 0; tem < nargs; tem++)
  151.         {
  152.           if (tem != 0)
  153.             fputs_filtered (", ", stream);
  154.           print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
  155.         }
  156.       fputs_filtered (")", stream);
  157.       return;

  158.     case OP_NAME:
  159.       nargs = longest_to_int (exp->elts[pc + 1].longconst);
  160.       (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
  161.       fputs_filtered (&exp->elts[pc + 2].string, stream);
  162.       return;

  163.     case OP_STRING:
  164.       {
  165.         struct value_print_options opts;

  166.         nargs = longest_to_int (exp->elts[pc + 1].longconst);
  167.         (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
  168.         /* LA_PRINT_STRING will print using the current repeat count threshold.
  169.            If necessary, we can temporarily set it to zero, or pass it as an
  170.            additional parameter to LA_PRINT_STRING.  -fnf */
  171.         get_user_print_options (&opts);
  172.         LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
  173.                          (gdb_byte *) &exp->elts[pc + 2].string, nargs,
  174.                          NULL, 0, &opts);
  175.       }
  176.       return;

  177.     case OP_OBJC_NSSTRING:        /* Objective-C Foundation Class
  178.                                    NSString constant.  */
  179.       {
  180.         struct value_print_options opts;

  181.         nargs = longest_to_int (exp->elts[pc + 1].longconst);
  182.         (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
  183.         fputs_filtered ("@\"", stream);
  184.         get_user_print_options (&opts);
  185.         LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
  186.                          (gdb_byte *) &exp->elts[pc + 2].string, nargs,
  187.                          NULL, 0, &opts);
  188.         fputs_filtered ("\"", stream);
  189.       }
  190.       return;

  191.     case OP_OBJC_MSGCALL:
  192.       {                        /* Objective C message (method) call.  */
  193.         char *selector;

  194.         (*pos) += 3;
  195.         nargs = longest_to_int (exp->elts[pc + 2].longconst);
  196.         fprintf_unfiltered (stream, "[");
  197.         print_subexp (exp, pos, stream, PREC_SUFFIX);
  198.         if (0 == target_read_string (exp->elts[pc + 1].longconst,
  199.                                      &selector, 1024, NULL))
  200.           {
  201.             error (_("bad selector"));
  202.             return;
  203.           }
  204.         if (nargs)
  205.           {
  206.             char *s, *nextS;

  207.             s = alloca (strlen (selector) + 1);
  208.             strcpy (s, selector);
  209.             for (tem = 0; tem < nargs; tem++)
  210.               {
  211.                 nextS = strchr (s, ':');
  212.                 gdb_assert (nextS);        /* Make sure we found ':'.  */
  213.                 *nextS = '\0';
  214.                 fprintf_unfiltered (stream, " %s: ", s);
  215.                 s = nextS + 1;
  216.                 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
  217.               }
  218.           }
  219.         else
  220.           {
  221.             fprintf_unfiltered (stream, " %s", selector);
  222.           }
  223.         fprintf_unfiltered (stream, "]");
  224.         /* "selector" was malloc'd by target_read_string.  Free it.  */
  225.         xfree (selector);
  226.         return;
  227.       }

  228.     case OP_ARRAY:
  229.       (*pos) += 3;
  230.       nargs = longest_to_int (exp->elts[pc + 2].longconst);
  231.       nargs -= longest_to_int (exp->elts[pc + 1].longconst);
  232.       nargs++;
  233.       tem = 0;
  234.       if (exp->elts[pc + 4].opcode == OP_LONG
  235.           && exp->elts[pc + 5].type
  236.              == builtin_type (exp->gdbarch)->builtin_char
  237.           && exp->language_defn->la_language == language_c)
  238.         {
  239.           /* Attempt to print C character arrays using string syntax.
  240.              Walk through the args, picking up one character from each
  241.              of the OP_LONG expression elements.  If any array element
  242.              does not match our expection of what we should find for
  243.              a simple string, revert back to array printing.  Note that
  244.              the last expression element is an explicit null terminator
  245.              byte, which doesn't get printed.  */
  246.           tempstr = alloca (nargs);
  247.           pc += 4;
  248.           while (tem < nargs)
  249.             {
  250.               if (exp->elts[pc].opcode != OP_LONG
  251.                   || exp->elts[pc + 1].type
  252.                      != builtin_type (exp->gdbarch)->builtin_char)
  253.                 {
  254.                   /* Not a simple array of char, use regular array
  255.                      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.           struct value_print_options opts;

  270.           get_user_print_options (&opts);
  271.           LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
  272.                            (gdb_byte *) tempstr, nargs - 1, NULL, 0, &opts);
  273.           (*pos) = pc;
  274.         }
  275.       else
  276.         {
  277.           fputs_filtered (" {", stream);
  278.           for (tem = 0; tem < nargs; tem++)
  279.             {
  280.               if (tem != 0)
  281.                 {
  282.                   fputs_filtered (", ", stream);
  283.                 }
  284.               print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
  285.             }
  286.           fputs_filtered ("}", stream);
  287.         }
  288.       return;

  289.     case TERNOP_COND:
  290.       if ((int) prec > (int) PREC_COMMA)
  291.         fputs_filtered ("(", stream);
  292.       /* Print the subexpressions, forcing parentheses
  293.          around any binary operations within them.
  294.          This is more parentheses than are strictly necessary,
  295.          but it looks clearer.  */
  296.       print_subexp (exp, pos, stream, PREC_HYPER);
  297.       fputs_filtered (" ? ", stream);
  298.       print_subexp (exp, pos, stream, PREC_HYPER);
  299.       fputs_filtered (" : ", stream);
  300.       print_subexp (exp, pos, stream, PREC_HYPER);
  301.       if ((int) prec > (int) PREC_COMMA)
  302.         fputs_filtered (")", stream);
  303.       return;

  304.     case TERNOP_SLICE:
  305.       print_subexp (exp, pos, stream, PREC_SUFFIX);
  306.       fputs_filtered ("(", stream);
  307.       print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
  308.       fputs_filtered (opcode == TERNOP_SLICE ? " : " : " UP ", stream);
  309.       print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
  310.       fputs_filtered (")", stream);
  311.       return;

  312.     case STRUCTOP_STRUCT:
  313.       tem = longest_to_int (exp->elts[pc + 1].longconst);
  314.       (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
  315.       print_subexp (exp, pos, stream, PREC_SUFFIX);
  316.       fputs_filtered (".", stream);
  317.       fputs_filtered (&exp->elts[pc + 2].string, stream);
  318.       return;

  319.       /* Will not occur for Modula-2.  */
  320.     case STRUCTOP_PTR:
  321.       tem = longest_to_int (exp->elts[pc + 1].longconst);
  322.       (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
  323.       print_subexp (exp, pos, stream, PREC_SUFFIX);
  324.       fputs_filtered ("->", stream);
  325.       fputs_filtered (&exp->elts[pc + 2].string, stream);
  326.       return;

  327.     case STRUCTOP_MEMBER:
  328.       print_subexp (exp, pos, stream, PREC_SUFFIX);
  329.       fputs_filtered (".*", stream);
  330.       print_subexp (exp, pos, stream, PREC_SUFFIX);
  331.       return;

  332.     case STRUCTOP_MPTR:
  333.       print_subexp (exp, pos, stream, PREC_SUFFIX);
  334.       fputs_filtered ("->*", stream);
  335.       print_subexp (exp, pos, stream, PREC_SUFFIX);
  336.       return;

  337.     case BINOP_SUBSCRIPT:
  338.       print_subexp (exp, pos, stream, PREC_SUFFIX);
  339.       fputs_filtered ("[", stream);
  340.       print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
  341.       fputs_filtered ("]", stream);
  342.       return;

  343.     case UNOP_POSTINCREMENT:
  344.       print_subexp (exp, pos, stream, PREC_SUFFIX);
  345.       fputs_filtered ("++", stream);
  346.       return;

  347.     case UNOP_POSTDECREMENT:
  348.       print_subexp (exp, pos, stream, PREC_SUFFIX);
  349.       fputs_filtered ("--", stream);
  350.       return;

  351.     case UNOP_CAST:
  352.       (*pos) += 2;
  353.       if ((int) prec > (int) PREC_PREFIX)
  354.         fputs_filtered ("(", stream);
  355.       fputs_filtered ("(", stream);
  356.       type_print (exp->elts[pc + 1].type, "", stream, 0);
  357.       fputs_filtered (") ", stream);
  358.       print_subexp (exp, pos, stream, PREC_PREFIX);
  359.       if ((int) prec > (int) PREC_PREFIX)
  360.         fputs_filtered (")", stream);
  361.       return;

  362.     case UNOP_CAST_TYPE:
  363.       if ((int) prec > (int) PREC_PREFIX)
  364.         fputs_filtered ("(", stream);
  365.       fputs_filtered ("(", stream);
  366.       print_subexp (exp, pos, stream, PREC_PREFIX);
  367.       fputs_filtered (") ", stream);
  368.       print_subexp (exp, pos, stream, PREC_PREFIX);
  369.       if ((int) prec > (int) PREC_PREFIX)
  370.         fputs_filtered (")", stream);
  371.       return;

  372.     case UNOP_DYNAMIC_CAST:
  373.     case UNOP_REINTERPRET_CAST:
  374.       fputs_filtered (opcode == UNOP_DYNAMIC_CAST ? "dynamic_cast"
  375.                       : "reinterpret_cast", stream);
  376.       fputs_filtered ("<", stream);
  377.       print_subexp (exp, pos, stream, PREC_PREFIX);
  378.       fputs_filtered ("> (", stream);
  379.       print_subexp (exp, pos, stream, PREC_PREFIX);
  380.       fputs_filtered (")", stream);
  381.       return;

  382.     case UNOP_MEMVAL:
  383.       (*pos) += 2;
  384.       if ((int) prec > (int) PREC_PREFIX)
  385.         fputs_filtered ("(", stream);
  386.       if (TYPE_CODE (exp->elts[pc + 1].type) == TYPE_CODE_FUNC
  387.           && exp->elts[pc + 3].opcode == OP_LONG)
  388.         {
  389.           struct value_print_options opts;

  390.           /* We have a minimal symbol fn, probably.  It's encoded
  391.              as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
  392.              Swallow the OP_LONG (including both its opcodes); ignore
  393.              its type; print the value in the type of the MEMVAL.  */
  394.           (*pos) += 4;
  395.           val = value_at_lazy (exp->elts[pc + 1].type,
  396.                                (CORE_ADDR) exp->elts[pc + 5].longconst);
  397.           get_no_prettyformat_print_options (&opts);
  398.           value_print (val, stream, &opts);
  399.         }
  400.       else
  401.         {
  402.           fputs_filtered ("{", stream);
  403.           type_print (exp->elts[pc + 1].type, "", stream, 0);
  404.           fputs_filtered ("} ", stream);
  405.           print_subexp (exp, pos, stream, PREC_PREFIX);
  406.         }
  407.       if ((int) prec > (int) PREC_PREFIX)
  408.         fputs_filtered (")", stream);
  409.       return;

  410.     case UNOP_MEMVAL_TYPE:
  411.       if ((int) prec > (int) PREC_PREFIX)
  412.         fputs_filtered ("(", stream);
  413.       fputs_filtered ("{", stream);
  414.       print_subexp (exp, pos, stream, PREC_PREFIX);
  415.       fputs_filtered ("} ", stream);
  416.       print_subexp (exp, pos, stream, PREC_PREFIX);
  417.       if ((int) prec > (int) PREC_PREFIX)
  418.         fputs_filtered (")", stream);
  419.       return;

  420.     case UNOP_MEMVAL_TLS:
  421.       (*pos) += 3;
  422.       if ((int) prec > (int) PREC_PREFIX)
  423.         fputs_filtered ("(", stream);
  424.       fputs_filtered ("{", stream);
  425.       type_print (exp->elts[pc + 2].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.     case BINOP_ASSIGN_MODIFY:
  432.       opcode = exp->elts[pc + 1].opcode;
  433.       (*pos) += 2;
  434.       myprec = PREC_ASSIGN;
  435.       assoc = 1;
  436.       assign_modify = 1;
  437.       op_str = "???";
  438.       for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
  439.         if (op_print_tab[tem].opcode == opcode)
  440.           {
  441.             op_str = op_print_tab[tem].string;
  442.             break;
  443.           }
  444.       if (op_print_tab[tem].opcode != opcode)
  445.         /* Not found; don't try to keep going because we don't know how
  446.            to interpret further elements.  */
  447.         error (_("Invalid expression"));
  448.       break;

  449.       /* C++ ops */

  450.     case OP_THIS:
  451.       ++(*pos);
  452.       if (exp->language_defn->la_name_of_this)
  453.         fputs_filtered (exp->language_defn->la_name_of_this, stream);
  454.       else
  455.         fprintf_filtered (stream, _("<language %s has no 'this'>"),
  456.                           exp->language_defn->la_name);
  457.       return;

  458.       /* Modula-2 ops */

  459.     case MULTI_SUBSCRIPT:
  460.       (*pos) += 2;
  461.       nargs = longest_to_int (exp->elts[pc + 1].longconst);
  462.       print_subexp (exp, pos, stream, PREC_SUFFIX);
  463.       fprintf_unfiltered (stream, " [");
  464.       for (tem = 0; tem < nargs; tem++)
  465.         {
  466.           if (tem != 0)
  467.             fprintf_unfiltered (stream, ", ");
  468.           print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
  469.         }
  470.       fprintf_unfiltered (stream, "]");
  471.       return;

  472.     case BINOP_VAL:
  473.       (*pos) += 2;
  474.       fprintf_unfiltered (stream, "VAL(");
  475.       type_print (exp->elts[pc + 1].type, "", stream, 0);
  476.       fprintf_unfiltered (stream, ",");
  477.       print_subexp (exp, pos, stream, PREC_PREFIX);
  478.       fprintf_unfiltered (stream, ")");
  479.       return;

  480.     case TYPE_INSTANCE:
  481.       {
  482.         LONGEST count = exp->elts[pc + 1].longconst;

  483.         /* The COUNT.  */
  484.         (*pos)++;
  485.         fputs_unfiltered ("TypesInstance(", stream);
  486.         while (count-- > 0)
  487.           {
  488.             type_print (exp->elts[(*pos)++].type, "", stream, 0);
  489.             if (count > 0)
  490.               fputs_unfiltered (",", stream);
  491.           }
  492.         fputs_unfiltered (",", stream);
  493.         /* Ending COUNT and ending TYPE_INSTANCE.  */
  494.         (*pos) += 2;
  495.         print_subexp (exp, pos, stream, PREC_PREFIX);
  496.         fputs_unfiltered (")", stream);
  497.         return;
  498.       }

  499.       /* Default ops */

  500.     default:
  501.       op_str = "???";
  502.       for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
  503.         if (op_print_tab[tem].opcode == opcode)
  504.           {
  505.             op_str = op_print_tab[tem].string;
  506.             myprec = op_print_tab[tem].precedence;
  507.             assoc = op_print_tab[tem].right_assoc;
  508.             break;
  509.           }
  510.       if (op_print_tab[tem].opcode != opcode)
  511.         /* Not found; don't try to keep going because we don't know how
  512.            to interpret further elements.  For example, this happens
  513.            if opcode is OP_TYPE.  */
  514.         error (_("Invalid expression"));
  515.     }

  516.   /* Note that PREC_BUILTIN will always emit parentheses.  */
  517.   if ((int) myprec < (int) prec)
  518.     fputs_filtered ("(", stream);
  519.   if ((int) opcode > (int) BINOP_END)
  520.     {
  521.       if (assoc)
  522.         {
  523.           /* Unary postfix operator.  */
  524.           print_subexp (exp, pos, stream, PREC_SUFFIX);
  525.           fputs_filtered (op_str, stream);
  526.         }
  527.       else
  528.         {
  529.           /* Unary prefix operator.  */
  530.           fputs_filtered (op_str, stream);
  531.           if (myprec == PREC_BUILTIN_FUNCTION)
  532.             fputs_filtered ("(", stream);
  533.           print_subexp (exp, pos, stream, PREC_PREFIX);
  534.           if (myprec == PREC_BUILTIN_FUNCTION)
  535.             fputs_filtered (")", stream);
  536.         }
  537.     }
  538.   else
  539.     {
  540.       /* Binary operator.  */
  541.       /* Print left operand.
  542.          If operator is right-associative,
  543.          increment precedence for this operand.  */
  544.       print_subexp (exp, pos, stream,
  545.                     (enum precedence) ((int) myprec + assoc));
  546.       /* Print the operator itself.  */
  547.       if (assign_modify)
  548.         fprintf_filtered (stream, " %s= ", op_str);
  549.       else if (op_str[0] == ',')
  550.         fprintf_filtered (stream, "%s ", op_str);
  551.       else
  552.         fprintf_filtered (stream, " %s ", op_str);
  553.       /* Print right operand.
  554.          If operator is left-associative,
  555.          increment precedence for this operand.  */
  556.       print_subexp (exp, pos, stream,
  557.                     (enum precedence) ((int) myprec + !assoc));
  558.     }

  559.   if ((int) myprec < (int) prec)
  560.     fputs_filtered (")", stream);
  561. }

  562. /* Return the operator corresponding to opcode OP as
  563.    a string.   NULL indicates that the opcode was not found in the
  564.    current language table.  */
  565. char *
  566. op_string (enum exp_opcode op)
  567. {
  568.   int tem;
  569.   const struct op_print *op_print_tab;

  570.   op_print_tab = current_language->la_op_print_tab;
  571.   for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
  572.     if (op_print_tab[tem].opcode == op)
  573.       return op_print_tab[tem].string;
  574.   return NULL;
  575. }

  576. /* Support for dumping the raw data from expressions in a human readable
  577.    form.  */

  578. static int dump_subexp_body (struct expression *exp, struct ui_file *, int);

  579. /* Name for OPCODE, when it appears in expression EXP.  */

  580. char *
  581. op_name (struct expression *exp, enum exp_opcode opcode)
  582. {
  583.   return exp->language_defn->la_exp_desc->op_name (opcode);
  584. }

  585. /* Default name for the standard operator OPCODE (i.e., one defined in
  586.    the definition of enum exp_opcode).  */

  587. char *
  588. op_name_standard (enum exp_opcode opcode)
  589. {
  590.   switch (opcode)
  591.     {
  592.     default:
  593.       {
  594.         static char buf[30];

  595.         xsnprintf (buf, sizeof (buf), "<unknown %d>", opcode);
  596.         return buf;
  597.       }
  598. #define OP(name)        \
  599.     case name:                \
  600.       return #name ;
  601. #include "std-operator.def"
  602. #undef OP
  603.     }
  604. }

  605. /* Print a raw dump of expression EXP to STREAM.
  606.    NOTE, if non-NULL, is printed as extra explanatory text.  */

  607. void
  608. dump_raw_expression (struct expression *exp, struct ui_file *stream,
  609.                      char *note)
  610. {
  611.   int elt;
  612.   char *opcode_name;
  613.   char *eltscan;
  614.   int eltsize;

  615.   fprintf_filtered (stream, "Dump of expression @ ");
  616.   gdb_print_host_address (exp, stream);
  617.   if (note)
  618.     fprintf_filtered (stream, ", %s:", note);
  619.   fprintf_filtered (stream, "\n\tLanguage %s, %d elements, %ld bytes each.\n",
  620.                     exp->language_defn->la_name, exp->nelts,
  621.                     (long) sizeof (union exp_element));
  622.   fprintf_filtered (stream, "\t%5s  %20s  %16s  %s\n", "Index", "Opcode",
  623.                     "Hex Value", "String Value");
  624.   for (elt = 0; elt < exp->nelts; elt++)
  625.     {
  626.       fprintf_filtered (stream, "\t%5d  ", elt);
  627.       opcode_name = op_name (exp, exp->elts[elt].opcode);

  628.       fprintf_filtered (stream, "%20s  ", opcode_name);
  629.       print_longest (stream, 'd', 0, exp->elts[elt].longconst);
  630.       fprintf_filtered (stream, "  ");

  631.       for (eltscan = (char *) &exp->elts[elt],
  632.            eltsize = sizeof (union exp_element);
  633.            eltsize-- > 0;
  634.            eltscan++)
  635.         {
  636.           fprintf_filtered (stream, "%c",
  637.                             isprint (*eltscan) ? (*eltscan & 0xFF) : '.');
  638.         }
  639.       fprintf_filtered (stream, "\n");
  640.     }
  641. }

  642. /* Dump the subexpression of prefix expression EXP whose operator is at
  643.    position ELT onto STREAM.  Returns the position of the next
  644.    subexpression in EXP.  */

  645. int
  646. dump_subexp (struct expression *exp, struct ui_file *stream, int elt)
  647. {
  648.   static int indent = 0;
  649.   int i;

  650.   fprintf_filtered (stream, "\n");
  651.   fprintf_filtered (stream, "\t%5d  ", elt);

  652.   for (i = 1; i <= indent; i++)
  653.     fprintf_filtered (stream, " ");
  654.   indent += 2;

  655.   fprintf_filtered (stream, "%-20s  ", op_name (exp, exp->elts[elt].opcode));

  656.   elt = dump_subexp_body (exp, stream, elt);

  657.   indent -= 2;

  658.   return elt;
  659. }

  660. /* Dump the operands of prefix expression EXP whose opcode is at
  661.    position ELT onto STREAM.  Returns the position of the next
  662.    subexpression in EXP.  */

  663. static int
  664. dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
  665. {
  666.   return exp->language_defn->la_exp_desc->dump_subexp_body (exp, stream, elt);
  667. }

  668. /* Default value for subexp_body in exp_descriptor vector.  */

  669. int
  670. dump_subexp_body_standard (struct expression *exp,
  671.                            struct ui_file *stream, int elt)
  672. {
  673.   int opcode = exp->elts[elt++].opcode;

  674.   switch (opcode)
  675.     {
  676.     case TERNOP_COND:
  677.     case TERNOP_SLICE:
  678.       elt = dump_subexp (exp, stream, elt);
  679.       /* FALL THROUGH */
  680.     case BINOP_ADD:
  681.     case BINOP_SUB:
  682.     case BINOP_MUL:
  683.     case BINOP_DIV:
  684.     case BINOP_REM:
  685.     case BINOP_MOD:
  686.     case BINOP_LSH:
  687.     case BINOP_RSH:
  688.     case BINOP_LOGICAL_AND:
  689.     case BINOP_LOGICAL_OR:
  690.     case BINOP_BITWISE_AND:
  691.     case BINOP_BITWISE_IOR:
  692.     case BINOP_BITWISE_XOR:
  693.     case BINOP_EQUAL:
  694.     case BINOP_NOTEQUAL:
  695.     case BINOP_LESS:
  696.     case BINOP_GTR:
  697.     case BINOP_LEQ:
  698.     case BINOP_GEQ:
  699.     case BINOP_REPEAT:
  700.     case BINOP_ASSIGN:
  701.     case BINOP_COMMA:
  702.     case BINOP_SUBSCRIPT:
  703.     case BINOP_EXP:
  704.     case BINOP_MIN:
  705.     case BINOP_MAX:
  706.     case BINOP_INTDIV:
  707.     case BINOP_ASSIGN_MODIFY:
  708.     case BINOP_VAL:
  709.     case BINOP_CONCAT:
  710.     case BINOP_END:
  711.     case STRUCTOP_MEMBER:
  712.     case STRUCTOP_MPTR:
  713.       elt = dump_subexp (exp, stream, elt);
  714.       /* FALL THROUGH */
  715.     case UNOP_NEG:
  716.     case UNOP_LOGICAL_NOT:
  717.     case UNOP_COMPLEMENT:
  718.     case UNOP_IND:
  719.     case UNOP_ADDR:
  720.     case UNOP_PREINCREMENT:
  721.     case UNOP_POSTINCREMENT:
  722.     case UNOP_PREDECREMENT:
  723.     case UNOP_POSTDECREMENT:
  724.     case UNOP_SIZEOF:
  725.     case UNOP_PLUS:
  726.     case UNOP_CAP:
  727.     case UNOP_CHR:
  728.     case UNOP_ORD:
  729.     case UNOP_ABS:
  730.     case UNOP_FLOAT:
  731.     case UNOP_HIGH:
  732.     case UNOP_MAX:
  733.     case UNOP_MIN:
  734.     case UNOP_ODD:
  735.     case UNOP_TRUNC:
  736.       elt = dump_subexp (exp, stream, elt);
  737.       break;
  738.     case OP_LONG:
  739.       fprintf_filtered (stream, "Type @");
  740.       gdb_print_host_address (exp->elts[elt].type, stream);
  741.       fprintf_filtered (stream, " (");
  742.       type_print (exp->elts[elt].type, NULL, stream, 0);
  743.       fprintf_filtered (stream, "), value %ld (0x%lx)",
  744.                         (long) exp->elts[elt + 1].longconst,
  745.                         (long) exp->elts[elt + 1].longconst);
  746.       elt += 3;
  747.       break;
  748.     case OP_DOUBLE:
  749.       fprintf_filtered (stream, "Type @");
  750.       gdb_print_host_address (exp->elts[elt].type, stream);
  751.       fprintf_filtered (stream, " (");
  752.       type_print (exp->elts[elt].type, NULL, stream, 0);
  753.       fprintf_filtered (stream, "), value %g",
  754.                         (double) exp->elts[elt + 1].doubleconst);
  755.       elt += 3;
  756.       break;
  757.     case OP_VAR_VALUE:
  758.       fprintf_filtered (stream, "Block @");
  759.       gdb_print_host_address (exp->elts[elt].block, stream);
  760.       fprintf_filtered (stream, ", symbol @");
  761.       gdb_print_host_address (exp->elts[elt + 1].symbol, stream);
  762.       fprintf_filtered (stream, " (%s)",
  763.                         SYMBOL_PRINT_NAME (exp->elts[elt + 1].symbol));
  764.       elt += 3;
  765.       break;
  766.     case OP_VAR_ENTRY_VALUE:
  767.       fprintf_filtered (stream, "Entry value of symbol @");
  768.       gdb_print_host_address (exp->elts[elt].symbol, stream);
  769.       fprintf_filtered (stream, " (%s)",
  770.                         SYMBOL_PRINT_NAME (exp->elts[elt].symbol));
  771.       elt += 2;
  772.       break;
  773.     case OP_LAST:
  774.       fprintf_filtered (stream, "History element %ld",
  775.                         (long) exp->elts[elt].longconst);
  776.       elt += 2;
  777.       break;
  778.     case OP_REGISTER:
  779.       fprintf_filtered (stream, "Register $%s", &exp->elts[elt + 1].string);
  780.       elt += 3 + BYTES_TO_EXP_ELEM (exp->elts[elt].longconst + 1);
  781.       break;
  782.     case OP_INTERNALVAR:
  783.       fprintf_filtered (stream, "Internal var @");
  784.       gdb_print_host_address (exp->elts[elt].internalvar, stream);
  785.       fprintf_filtered (stream, " (%s)",
  786.                         internalvar_name (exp->elts[elt].internalvar));
  787.       elt += 2;
  788.       break;
  789.     case OP_FUNCALL:
  790.       {
  791.         int i, nargs;

  792.         nargs = longest_to_int (exp->elts[elt].longconst);

  793.         fprintf_filtered (stream, "Number of args: %d", nargs);
  794.         elt += 2;

  795.         for (i = 1; i <= nargs + 1; i++)
  796.           elt = dump_subexp (exp, stream, elt);
  797.       }
  798.       break;
  799.     case OP_ARRAY:
  800.       {
  801.         int lower, upper;
  802.         int i;

  803.         lower = longest_to_int (exp->elts[elt].longconst);
  804.         upper = longest_to_int (exp->elts[elt + 1].longconst);

  805.         fprintf_filtered (stream, "Bounds [%d:%d]", lower, upper);
  806.         elt += 3;

  807.         for (i = 1; i <= upper - lower + 1; i++)
  808.           elt = dump_subexp (exp, stream, elt);
  809.       }
  810.       break;
  811.     case UNOP_DYNAMIC_CAST:
  812.     case UNOP_REINTERPRET_CAST:
  813.     case UNOP_CAST_TYPE:
  814.     case UNOP_MEMVAL_TYPE:
  815.       fprintf_filtered (stream, " (");
  816.       elt = dump_subexp (exp, stream, elt);
  817.       fprintf_filtered (stream, ")");
  818.       elt = dump_subexp (exp, stream, elt);
  819.       break;
  820.     case UNOP_MEMVAL:
  821.     case UNOP_CAST:
  822.       fprintf_filtered (stream, "Type @");
  823.       gdb_print_host_address (exp->elts[elt].type, stream);
  824.       fprintf_filtered (stream, " (");
  825.       type_print (exp->elts[elt].type, NULL, stream, 0);
  826.       fprintf_filtered (stream, ")");
  827.       elt = dump_subexp (exp, stream, elt + 2);
  828.       break;
  829.     case UNOP_MEMVAL_TLS:
  830.       fprintf_filtered (stream, "TLS type @");
  831.       gdb_print_host_address (exp->elts[elt + 1].type, stream);
  832.       fprintf_filtered (stream, " (__thread /* \"%s\" */ ",
  833.                         (exp->elts[elt].objfile == NULL ? "(null)"
  834.                          : objfile_name (exp->elts[elt].objfile)));
  835.       type_print (exp->elts[elt + 1].type, NULL, stream, 0);
  836.       fprintf_filtered (stream, ")");
  837.       elt = dump_subexp (exp, stream, elt + 3);
  838.       break;
  839.     case OP_TYPE:
  840.       fprintf_filtered (stream, "Type @");
  841.       gdb_print_host_address (exp->elts[elt].type, stream);
  842.       fprintf_filtered (stream, " (");
  843.       type_print (exp->elts[elt].type, NULL, stream, 0);
  844.       fprintf_filtered (stream, ")");
  845.       elt += 2;
  846.       break;
  847.     case OP_TYPEOF:
  848.     case OP_DECLTYPE:
  849.       fprintf_filtered (stream, "Typeof (");
  850.       elt = dump_subexp (exp, stream, elt);
  851.       fprintf_filtered (stream, ")");
  852.       break;
  853.     case OP_TYPEID:
  854.       fprintf_filtered (stream, "typeid (");
  855.       elt = dump_subexp (exp, stream, elt);
  856.       fprintf_filtered (stream, ")");
  857.       break;
  858.     case STRUCTOP_STRUCT:
  859.     case STRUCTOP_PTR:
  860.       {
  861.         char *elem_name;
  862.         int len;

  863.         len = longest_to_int (exp->elts[elt].longconst);
  864.         elem_name = &exp->elts[elt + 1].string;

  865.         fprintf_filtered (stream, "Element name: `%.*s'", len, elem_name);
  866.         elt = dump_subexp (exp, stream, elt + 3 + BYTES_TO_EXP_ELEM (len + 1));
  867.       }
  868.       break;
  869.     case OP_SCOPE:
  870.       {
  871.         char *elem_name;
  872.         int len;

  873.         fprintf_filtered (stream, "Type @");
  874.         gdb_print_host_address (exp->elts[elt].type, stream);
  875.         fprintf_filtered (stream, " (");
  876.         type_print (exp->elts[elt].type, NULL, stream, 0);
  877.         fprintf_filtered (stream, ") ");

  878.         len = longest_to_int (exp->elts[elt + 1].longconst);
  879.         elem_name = &exp->elts[elt + 2].string;

  880.         fprintf_filtered (stream, "Field name: `%.*s'", len, elem_name);
  881.         elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
  882.       }
  883.       break;
  884.     case TYPE_INSTANCE:
  885.       {
  886.         LONGEST len;

  887.         len = exp->elts[elt++].longconst;
  888.         fprintf_filtered (stream, "%s TypeInstance: ", plongest (len));
  889.         while (len-- > 0)
  890.           {
  891.             fprintf_filtered (stream, "Type @");
  892.             gdb_print_host_address (exp->elts[elt].type, stream);
  893.             fprintf_filtered (stream, " (");
  894.             type_print (exp->elts[elt].type, NULL, stream, 0);
  895.             fprintf_filtered (stream, ")");
  896.             elt++;
  897.             if (len > 0)
  898.               fputs_filtered (", ", stream);
  899.           }
  900.         /* Ending LEN and ending TYPE_INSTANCE.  */
  901.         elt += 2;
  902.         elt = dump_subexp (exp, stream, elt);
  903.       }
  904.       break;
  905.     case OP_STRING:
  906.       {
  907.         LONGEST len = exp->elts[elt].longconst;
  908.         LONGEST type = exp->elts[elt + 1].longconst;

  909.         fprintf_filtered (stream, "Language-specific string type: %s",
  910.                           plongest (type));

  911.         /* Skip length.  */
  912.         elt += 1;

  913.         /* Skip string content. */
  914.         elt += BYTES_TO_EXP_ELEM (len);

  915.         /* Skip length and ending OP_STRING. */
  916.         elt += 2;
  917.       }
  918.       break;
  919.     default:
  920.     case OP_NULL:
  921.     case MULTI_SUBSCRIPT:
  922.     case OP_F77_UNDETERMINED_ARGLIST:
  923.     case OP_COMPLEX:
  924.     case OP_BOOL:
  925.     case OP_M2_STRING:
  926.     case OP_THIS:
  927.     case OP_NAME:
  928.       fprintf_filtered (stream, "Unknown format");
  929.     }

  930.   return elt;
  931. }

  932. void
  933. dump_prefix_expression (struct expression *exp, struct ui_file *stream)
  934. {
  935.   int elt;

  936.   fprintf_filtered (stream, "Dump of expression @ ");
  937.   gdb_print_host_address (exp, stream);
  938.   fputs_filtered (", after conversion to prefix form:\nExpression: `", stream);
  939.   print_expression (exp, stream);
  940.   fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
  941.                     exp->language_defn->la_name, exp->nelts,
  942.                     (long) sizeof (union exp_element));
  943.   fputs_filtered ("\n", stream);

  944.   for (elt = 0; elt < exp->nelts;)
  945.     elt = dump_subexp (exp, stream, elt);
  946.   fputs_filtered ("\n", stream);
  947. }