[v3,075/206] Implement dumping

Message ID 20210220201609.838264-76-tom@tromey.com
State New
Headers show
Series
  • Refactor expressions
Related show

Commit Message

Tom Tromey Feb. 20, 2021, 8:13 p.m.
This patch implements the dumping methods for tuple_holding_operation.
A number of overloads are used.  Note that no default case is given.
This approach makes it simple to detect when a new overload is needed
-- compilation will fail.  (There is an example of this in a later
patch in the series.)

gdb/ChangeLog
2021-02-20  Tom Tromey  <tom@tromey.com>

	* expprint.c (expr::dump_for_expression): New functions.
	* expop.h (dump_for_expression): New overloads.
	(tuple_holding_operation::dump, tuple_holding_operation::do_dump):
	Update.
---
 gdb/ChangeLog  |   7 +++
 gdb/expop.h    |  55 +++++++++++++++++++++-
 gdb/expprint.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 185 insertions(+), 1 deletion(-)

-- 
2.26.2

Patch

diff --git a/gdb/expop.h b/gdb/expop.h
index 861e3c2ee6c..97848fd5aa2 100644
--- a/gdb/expop.h
+++ b/gdb/expop.h
@@ -133,6 +133,57 @@  check_objfile (const std::pair<S, T> &item, struct objfile *objfile)
 	  || check_objfile (item.second, objfile));
 }
 
+static inline void
+dump_for_expression (struct ui_file *stream, int depth,
+		     const operation_up &op)
+{
+  op->dump (stream, depth);
+}
+
+extern void dump_for_expression (struct ui_file *stream, int depth,
+				 enum exp_opcode op);
+extern void dump_for_expression (struct ui_file *stream, int depth,
+				 const std::string &str);
+extern void dump_for_expression (struct ui_file *stream, int depth,
+				 struct type *type);
+extern void dump_for_expression (struct ui_file *stream, int depth,
+				 CORE_ADDR addr);
+extern void dump_for_expression (struct ui_file *stream, int depth,
+				 internalvar *ivar);
+extern void dump_for_expression (struct ui_file *stream, int depth,
+				 symbol *sym);
+extern void dump_for_expression (struct ui_file *stream, int depth,
+				 minimal_symbol *msym);
+extern void dump_for_expression (struct ui_file *stream, int depth,
+				 const block *bl);
+extern void dump_for_expression (struct ui_file *stream, int depth,
+				 type_instance_flags flags);
+extern void dump_for_expression (struct ui_file *stream, int depth,
+				 enum c_string_type_values flags);
+extern void dump_for_expression (struct ui_file *stream, int depth,
+				 enum range_flag flags);
+extern void dump_for_expression (struct ui_file *stream, int depth,
+				 objfile *objf);
+
+template<typename T>
+void
+dump_for_expression (struct ui_file *stream, int depth,
+		     const std::vector<T> &vals)
+{
+  fprintf_filtered (stream, _("%*sVector:\n"), depth, "");
+  for (auto &item : vals)
+    dump_for_expression (stream, depth + 1, item);
+}
+
+template<typename X, typename Y>
+void
+dump_for_expression (struct ui_file *stream, int depth,
+		     const std::pair<X, Y> &vals)
+{
+  dump_for_expression (stream, depth, vals.first);
+  dump_for_expression (stream, depth, vals.second);
+}
+
 /* Base class for most concrete operations.  This class holds data,
    specified via template parameters, and supplies generic
    implementations of the 'dump' and 'uses_objfile' methods.  */
@@ -155,7 +206,8 @@  class tuple_holding_operation : public operation
 
   void dump (struct ui_file *stream, int depth) const override
   {
-    do_dump<0, Arg...> (stream, depth, m_storage);
+    dump_for_expression (stream, depth, opcode ());
+    do_dump<0, Arg...> (stream, depth + 1, m_storage);
   }
 
 protected:
@@ -178,6 +230,7 @@  class tuple_holding_operation : public operation
   do_dump (struct ui_file *stream, int depth, const std::tuple<T...> &value)
     const
   {
+    dump_for_expression (stream, depth, std::get<I> (value));
     do_dump<I + 1, T...> (stream, depth, value);
   }
 
diff --git a/gdb/expprint.c b/gdb/expprint.c
index d95835fc47d..92f12999472 100644
--- a/gdb/expprint.c
+++ b/gdb/expprint.c
@@ -30,6 +30,8 @@ 
 #include "objfiles.h"
 #include "valprint.h"
 #include "cli/cli-style.h"
+#include "c-lang.h"
+#include "expop.h"
 
 #include <ctype.h>
 
@@ -1160,3 +1162,125 @@  dump_prefix_expression (struct expression *exp, struct ui_file *stream)
     elt = dump_subexp (exp, stream, elt);
   fputs_filtered ("\n", stream);
 }
+
+namespace expr
+{
+
+void
+dump_for_expression (struct ui_file *stream, int depth, enum exp_opcode op)
+{
+  fprintf_filtered (stream, _("%*sOperation: %s\n"), depth, "", op_name (op));
+}
+
+void
+dump_for_expression (struct ui_file *stream, int depth, const std::string &str)
+{
+  fprintf_filtered (stream, _("%*sString: %s\n"), depth, "", str.c_str ());
+}
+
+void
+dump_for_expression (struct ui_file *stream, int depth, struct type *type)
+{
+  fprintf_filtered (stream, _("%*sType: "), depth, "");
+  type_print (type, nullptr, stream, 0);
+  fprintf_filtered (stream, "\n");
+}
+
+void
+dump_for_expression (struct ui_file *stream, int depth, CORE_ADDR addr)
+{
+  fprintf_filtered (stream, _("%*sConstant: %s\n"), depth, "",
+		     core_addr_to_string (addr));
+}
+
+void
+dump_for_expression (struct ui_file *stream, int depth, internalvar *ivar)
+{
+  fprintf_filtered (stream, _("%*sInternalvar: $%s\n"), depth, "",
+		     internalvar_name (ivar));
+}
+
+void
+dump_for_expression (struct ui_file *stream, int depth, symbol *sym)
+{
+  fprintf_filtered (stream, _("%*sSymbol: %s\n"), depth, "",
+		     sym->print_name ());
+}
+
+void
+dump_for_expression (struct ui_file *stream, int depth, minimal_symbol *msym)
+{
+  fprintf_filtered (stream, _("%*sMinsym: %s\n"), depth, "",
+		     msym->print_name ());
+}
+
+void
+dump_for_expression (struct ui_file *stream, int depth, const block *bl)
+{
+  fprintf_filtered (stream, _("%*sBlock: %p\n"), depth, "", bl);
+}
+
+void
+dump_for_expression (struct ui_file *stream, int depth,
+		     type_instance_flags flags)
+{
+  fprintf_filtered (stream, _("%*sType flags: "), depth, "");
+  if (flags & TYPE_INSTANCE_FLAG_CONST)
+    fputs_unfiltered ("const ", stream);
+  if (flags & TYPE_INSTANCE_FLAG_VOLATILE)
+    fputs_unfiltered ("volatile", stream);
+  fprintf_filtered (stream, "\n");
+}
+
+void
+dump_for_expression (struct ui_file *stream, int depth,
+		     enum c_string_type_values flags)
+{
+  fprintf_filtered (stream, _("%*sC string flags: "), depth, "");
+  switch (flags & ~C_CHAR)
+    {
+    case C_WIDE_STRING:
+      fputs_unfiltered (_("wide "), stream);
+      break;
+    case C_STRING_16:
+      fputs_unfiltered (_("u16 "), stream);
+      break;
+    case C_STRING_32:
+      fputs_unfiltered (_("u32 "), stream);
+      break;
+    default:
+      fputs_unfiltered (_("ordinary "), stream);
+      break;
+    }
+
+  if ((flags & C_CHAR) != 0)
+    fputs_unfiltered (_("char"), stream);
+  else
+    fputs_unfiltered (_("string"), stream);
+  fputs_unfiltered ("\n", stream);
+}
+
+void
+dump_for_expression (struct ui_file *stream, int depth, objfile *objf)
+{
+  fprintf_filtered (stream, _("%*sObjfile: %s\n"), depth, "",
+		     objfile_name (objf));
+}
+
+void
+dump_for_expression (struct ui_file *stream, int depth,
+		     enum range_flag flags)
+{
+  fprintf_filtered (stream, _("%*sRange:"), depth, "");
+  if ((flags & RANGE_LOW_BOUND_DEFAULT) != 0)
+    fputs_unfiltered (_("low-default "), stream);
+  if ((flags & RANGE_HIGH_BOUND_DEFAULT) != 0)
+    fputs_unfiltered (_("high-default "), stream);
+  if ((flags & RANGE_HIGH_BOUND_EXCLUSIVE) != 0)
+    fputs_unfiltered (_("high-exclusive "), stream);
+  if ((flags & RANGE_HAS_STRIDE) != 0)
+    fputs_unfiltered (_("has-stride"), stream);
+  fprintf_filtered (stream, "\n");
+}
+
+} /* namespace expr */