* gdbtypes.h (struct cplus_struct_type) <n_baseclasses>: Fix comment.
[deliverable/binutils-gdb.git] / gdb / typeprint.c
index 0e1c93c0a8c870c30320c9635105d6878e749b05..5a97acecbea9f04bf936bda196779e63e2ed2282 100644 (file)
@@ -1,7 +1,6 @@
 /* Language independent support for printing types for GDB, the GNU debugger.
 
-   Copyright (C) 1986, 1988-1989, 1991-1995, 1998-2001, 2003, 2006-2012
-   Free Software Foundation, Inc.
+   Copyright (C) 1986-2015 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 #include "language.h"
 #include "cp-abi.h"
 #include "typeprint.h"
-#include "gdb_string.h"
-#include "exceptions.h"
 #include "valprint.h"
-#include <errno.h>
 #include <ctype.h>
 #include "cli/cli-utils.h"
+#include "extension.h"
+#include "completer.h"
 
 extern void _initialize_typeprint (void);
 
@@ -52,7 +50,9 @@ const struct type_print_options type_print_raw_options =
   1,                           /* raw */
   1,                           /* print_methods */
   1,                           /* print_typedefs */
-  NULL                         /* local_typedefs */
+  NULL,                                /* local_typedefs */
+  NULL,                                /* global_table */
+  NULL                         /* global_printers */
 };
 
 /* The default flags for 'ptype' and 'whatis'.  */
@@ -62,7 +62,9 @@ static struct type_print_options default_ptype_flags =
   0,                           /* raw */
   1,                           /* print_methods */
   1,                           /* print_typedefs */
-  NULL                         /* local_typedefs */
+  NULL,                                /* local_typedefs */
+  NULL,                                /* global_table */
+  NULL                         /* global_printers */
 };
 
 \f
@@ -235,6 +237,74 @@ copy_typedef_hash (struct typedef_hash_table *table)
   return result;
 }
 
+/* A cleanup to free the global typedef hash.  */
+
+static void
+do_free_global_table (void *arg)
+{
+  struct type_print_options *flags = arg;
+
+  free_typedef_hash (flags->global_typedefs);
+  free_ext_lang_type_printers (flags->global_printers);
+}
+
+/* Create the global typedef hash.  */
+
+static struct cleanup *
+create_global_typedef_table (struct type_print_options *flags)
+{
+  gdb_assert (flags->global_typedefs == NULL && flags->global_printers == NULL);
+  flags->global_typedefs = create_typedef_hash ();
+  flags->global_printers = start_ext_lang_type_printers ();
+  return make_cleanup (do_free_global_table, flags);
+}
+
+/* Look up the type T in the global typedef hash.  If it is found,
+   return the typedef name.  If it is not found, apply the
+   type-printers, if any, given by start_script_type_printers and return the
+   result.  A NULL return means that the name was not found.  */
+
+static const char *
+find_global_typedef (const struct type_print_options *flags,
+                    struct type *t)
+{
+  char *applied;
+  void **slot;
+  struct typedef_field tf, *new_tf;
+
+  if (flags->global_typedefs == NULL)
+    return NULL;
+
+  tf.name = NULL;
+  tf.type = t;
+
+  slot = htab_find_slot (flags->global_typedefs->table, &tf, INSERT);
+  if (*slot != NULL)
+    {
+      new_tf = *slot;
+      return new_tf->name;
+    }
+
+  /* Put an entry into the hash table now, in case
+     apply_ext_lang_type_printers recurses.  */
+  new_tf = XOBNEW (&flags->global_typedefs->storage, struct typedef_field);
+  new_tf->name = NULL;
+  new_tf->type = t;
+
+  *slot = new_tf;
+
+  applied = apply_ext_lang_type_printers (flags->global_printers, t);
+
+  if (applied != NULL)
+    {
+      new_tf->name = obstack_copy0 (&flags->global_typedefs->storage, applied,
+                                   strlen (applied));
+      xfree (applied);
+    }
+
+  return new_tf->name;
+}
+
 /* Look up the type T in the typedef hash table in with FLAGS.  If T
    is in the table, return its short (class-relative) typedef name.
    Otherwise return NULL.  If the table is NULL, this always returns
@@ -243,16 +313,19 @@ copy_typedef_hash (struct typedef_hash_table *table)
 const char *
 find_typedef_in_hash (const struct type_print_options *flags, struct type *t)
 {
-  struct typedef_field tf, *found;
+  if (flags->local_typedefs != NULL)
+    {
+      struct typedef_field tf, *found;
 
-  if (flags->local_typedefs == NULL)
-    return NULL;
+      tf.name = NULL;
+      tf.type = t;
+      found = htab_find (flags->local_typedefs->table, &tf);
 
-  tf.name = NULL;
-  tf.type = t;
-  found = htab_find (flags->local_typedefs->table, &tf);
+      if (found != NULL)
+       return found->name;
+    }
 
-  return found == NULL ? NULL : found->name;
+  return find_global_typedef (flags, t);
 }
 
 \f
@@ -262,9 +335,9 @@ find_typedef_in_hash (const struct type_print_options *flags, struct type *t)
    NEW is the new name for a type TYPE.  */
 
 void
-typedef_print (struct type *type, struct symbol *new, struct ui_file *stream)
+typedef_print (struct type *type, struct symbol *newobj, struct ui_file *stream)
 {
-  LA_PRINT_TYPEDEF (type, new, stream);
+  LA_PRINT_TYPEDEF (type, newobj, stream);
 }
 
 /* The default way to print a typedef.  */
@@ -299,18 +372,20 @@ type_to_string (struct type *type)
   char *s = NULL;
   struct ui_file *stb;
   struct cleanup *old_chain;
-  volatile struct gdb_exception except;
 
   stb = mem_fileopen ();
   old_chain = make_cleanup_ui_file_delete (stb);
 
-  TRY_CATCH (except, RETURN_MASK_ALL)
+  TRY
     {
       type_print (type, "", stb, -1);
       s = ui_file_xstrdup (stb, NULL);
     }
-  if (except.reason < 0)
-    s = NULL;
+  CATCH (except, RETURN_MASK_ALL)
+    {
+      s = NULL;
+    }
+  END_CATCH
 
   do_cleanups (old_chain);
 
@@ -325,7 +400,7 @@ whatis_exp (char *exp, int show)
 {
   struct expression *expr;
   struct value *val;
-  struct cleanup *old_chain = NULL;
+  struct cleanup *old_chain;
   struct type *real_type = NULL;
   struct type *type;
   int full = 0;
@@ -334,6 +409,8 @@ whatis_exp (char *exp, int show)
   struct value_print_options opts;
   struct type_print_options flags = default_ptype_flags;
 
+  old_chain = make_cleanup (null_cleanup, NULL);
+
   if (exp)
     {
       if (*exp == '/')
@@ -373,7 +450,7 @@ whatis_exp (char *exp, int show)
        }
 
       expr = parse_expression (exp);
-      old_chain = make_cleanup (free_current_contents, &expr);
+      make_cleanup (free_current_contents, &expr);
       val = evaluate_type (expr);
     }
   else
@@ -386,14 +463,17 @@ whatis_exp (char *exp, int show)
     {
       if (((TYPE_CODE (type) == TYPE_CODE_PTR)
           || (TYPE_CODE (type) == TYPE_CODE_REF))
-         && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
+         && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT))
         real_type = value_rtti_indirect_type (val, &full, &top, &using_enc);
-      else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
+      else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
        real_type = value_rtti_type (val, &full, &top, &using_enc);
     }
 
   printf_filtered ("type = ");
 
+  if (!flags.raw)
+    create_global_typedef_table (&flags);
+
   if (real_type)
     {
       printf_filtered ("/* real type = ");
@@ -406,8 +486,7 @@ whatis_exp (char *exp, int show)
   LA_PRINT_TYPE (type, "", gdb_stdout, show, 0, &flags);
   printf_filtered ("\n");
 
-  if (exp)
-    do_cleanups (old_chain);
+  do_cleanups (old_chain);
 }
 
 static void
@@ -422,9 +501,9 @@ whatis_command (char *exp, int from_tty)
 /* TYPENAME is either the name of a type, or an expression.  */
 
 static void
-ptype_command (char *typename, int from_tty)
+ptype_command (char *type_name, int from_tty)
 {
-  whatis_exp (typename, 1);
+  whatis_exp (type_name, 1);
 }
 
 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
@@ -515,16 +594,16 @@ print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
    and whatis_command().  */
 
 void
-maintenance_print_type (char *typename, int from_tty)
+maintenance_print_type (char *type_name, int from_tty)
 {
   struct value *val;
   struct type *type;
   struct cleanup *old_chain;
   struct expression *expr;
 
-  if (typename != NULL)
+  if (type_name != NULL)
     {
-      expr = parse_expression (typename);
+      expr = parse_expression (type_name);
       old_chain = make_cleanup (free_current_contents, &expr);
       if (expr->elts[0].opcode == OP_TYPE)
        {
@@ -556,7 +635,7 @@ set_print_type (char *arg, int from_tty)
 {
   printf_unfiltered (
      "\"set print type\" must be followed by the name of a subcommand.\n");
-  help_list (setprintlist, "set print type ", -1, gdb_stdout);
+  help_list (setprintlist, "set print type ", all_commands, gdb_stdout);
 }
 
 static void
@@ -600,11 +679,14 @@ show_print_type_typedefs (struct ui_file *file, int from_tty,
 void
 _initialize_typeprint (void)
 {
-  add_com ("ptype", class_vars, ptype_command, _("\
+  struct cmd_list_element *c;
+
+  c = add_com ("ptype", class_vars, ptype_command, _("\
 Print definition of type TYPE.\n\
-Usage: ptype[/FLAGS] TYPE-NAME | EXPRESSION\n\
-Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\
-or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\
+Usage: ptype[/FLAGS] TYPE | EXPRESSION\n\
+Argument may be any type (for example a type name defined by typedef,\n\
+or \"struct STRUCT-TAG\" or \"class CLASS-NAME\" or \"union UNION-TAG\"\n\
+or \"enum ENUM-TAG\") or an expression.\n\
 The selected stack frame's lexical context is used to look up the name.\n\
 Contrary to \"whatis\", \"ptype\" always unrolls any typedefs.\n\
 \n\
@@ -614,10 +696,12 @@ Available FLAGS are:\n\
   /M    print methods defined in a class\n\
   /t    do not print typedefs defined in a class\n\
   /T    print typedefs defined in a class"));
+  set_cmd_completer (c, expression_completer);
 
-  add_com ("whatis", class_vars, whatis_command,
-          _("Print data type of expression EXP.\n\
+  c = add_com ("whatis", class_vars, whatis_command,
+              _("Print data type of expression EXP.\n\
 Only one level of typedefs is unrolled.  See also \"ptype\"."));
+  set_cmd_completer (c, expression_completer);
 
   add_prefix_cmd ("type", no_class, show_print_type,
                  _("Generic command for showing type-printing settings."),
This page took 0.044084 seconds and 4 git commands to generate.