Fix xmethod Python so that it works with Python3.
[deliverable/binutils-gdb.git] / gdb / gdbtypes.c
index 9456059fae5da6a41762f4a652fb1cbf36f07e67..3694597f352d10c7f3877a245672a1ca86875dac 100644 (file)
@@ -20,7 +20,6 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
-#include <string.h>
 #include "bfd.h"
 #include "symtab.h"
 #include "symfile.h"
 #include "complaints.h"
 #include "gdbcmd.h"
 #include "cp-abi.h"
-#include "gdb_assert.h"
 #include "hashtab.h"
 #include "exceptions.h"
 #include "cp-support.h"
 #include "bcache.h"
 #include "dwarf2loc.h"
+#include "gdbcore.h"
 
 /* Initialize BADNESS constants.  */
 
@@ -1610,68 +1609,77 @@ stub_noname_complaint (void)
   complaint (&symfile_complaints, _("stub type has NULL name"));
 }
 
-/* See gdbtypes.h.  */
+/* Worker for is_dynamic_type.  */
 
-int
-is_dynamic_type (struct type *type)
+static int
+is_dynamic_type_internal (struct type *type, int top_level)
 {
   type = check_typedef (type);
 
-  if (TYPE_CODE (type) == TYPE_CODE_REF)
+  /* We only want to recognize references at the outermost level.  */
+  if (top_level && TYPE_CODE (type) == TYPE_CODE_REF)
     type = check_typedef (TYPE_TARGET_TYPE (type));
 
   switch (TYPE_CODE (type))
     {
+    case TYPE_CODE_RANGE:
+      return !has_static_range (TYPE_RANGE_DATA (type));
+
     case TYPE_CODE_ARRAY:
       {
-       const struct type *range_type;
-
        gdb_assert (TYPE_NFIELDS (type) == 1);
-       range_type = TYPE_INDEX_TYPE (type);
-       if (!has_static_range (TYPE_RANGE_DATA (range_type)))
+
+       /* The array is dynamic if either the bounds are dynamic,
+          or the elements it contains have a dynamic contents.  */
+       if (is_dynamic_type_internal (TYPE_INDEX_TYPE (type), 0))
          return 1;
-       else
-         return is_dynamic_type (TYPE_TARGET_TYPE (type));
-       break;
+       return is_dynamic_type_internal (TYPE_TARGET_TYPE (type), 0);
+      }
+
+    case TYPE_CODE_STRUCT:
+    case TYPE_CODE_UNION:
+      {
+       int i;
+
+       for (i = 0; i < TYPE_NFIELDS (type); ++i)
+         if (!field_is_static (&TYPE_FIELD (type, i))
+             && is_dynamic_type_internal (TYPE_FIELD_TYPE (type, i), 0))
+           return 1;
       }
-    default:
-      return 0;
       break;
     }
+
+  return 0;
 }
 
-/* Resolves dynamic bound values of an array type TYPE to static ones.
-   ADDRESS might be needed to resolve the subrange bounds, it is the location
-   of the associated array.  */
+/* See gdbtypes.h.  */
+
+int
+is_dynamic_type (struct type *type)
+{
+  return is_dynamic_type_internal (type, 1);
+}
+
+static struct type *resolve_dynamic_type_internal (struct type *type,
+                                                  CORE_ADDR addr,
+                                                  int top_level);
+
+/* Given a dynamic range type (dyn_range_type), return a static version
+   of that type.  */
 
 static struct type *
-resolve_dynamic_bounds (struct type *type, CORE_ADDR addr)
+resolve_dynamic_range (struct type *dyn_range_type)
 {
   CORE_ADDR value;
-  struct type *elt_type;
-  struct type *range_type;
-  struct type *ary_dim;
+  struct type *static_range_type;
   const struct dynamic_prop *prop;
   const struct dwarf2_locexpr_baton *baton;
   struct dynamic_prop low_bound, high_bound;
 
-  if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
-    {
-      struct type *copy = copy_type (type);
-
-      TYPE_TARGET_TYPE (copy)
-       = resolve_dynamic_bounds (TYPE_TARGET_TYPE (type), addr);
-
-      return copy;
-    }
-
-  gdb_assert (TYPE_CODE (type) == TYPE_CODE_ARRAY);
-
-  elt_type = type;
-  range_type = check_typedef (TYPE_INDEX_TYPE (elt_type));
+  gdb_assert (TYPE_CODE (dyn_range_type) == TYPE_CODE_RANGE);
 
-  prop = &TYPE_RANGE_DATA (range_type)->low;
-  if (dwarf2_evaluate_property (prop, addr, &value))
+  prop = &TYPE_RANGE_DATA (dyn_range_type)->low;
+  if (dwarf2_evaluate_property (prop, &value))
     {
       low_bound.kind = PROP_CONST;
       low_bound.data.const_val = value;
@@ -1682,11 +1690,15 @@ resolve_dynamic_bounds (struct type *type, CORE_ADDR addr)
       low_bound.data.const_val = 0;
     }
 
-  prop = &TYPE_RANGE_DATA (range_type)->high;
-  if (dwarf2_evaluate_property (prop, addr, &value))
+  prop = &TYPE_RANGE_DATA (dyn_range_type)->high;
+  if (dwarf2_evaluate_property (prop, &value))
     {
       high_bound.kind = PROP_CONST;
       high_bound.data.const_val = value;
+
+      if (TYPE_RANGE_DATA (dyn_range_type)->flag_upper_bound_is_count)
+       high_bound.data.const_val
+         = low_bound.data.const_val + high_bound.data.const_val - 1;
     }
   else
     {
@@ -1694,37 +1706,204 @@ resolve_dynamic_bounds (struct type *type, CORE_ADDR addr)
       high_bound.data.const_val = 0;
     }
 
+  static_range_type = create_range_type (copy_type (dyn_range_type),
+                                        TYPE_TARGET_TYPE (dyn_range_type),
+                                        &low_bound, &high_bound);
+  TYPE_RANGE_DATA (static_range_type)->flag_bound_evaluated = 1;
+  return static_range_type;
+}
+
+/* Resolves dynamic bound values of an array type TYPE to static ones.
+   ADDRESS might be needed to resolve the subrange bounds, it is the location
+   of the associated array.  */
+
+static struct type *
+resolve_dynamic_array (struct type *type)
+{
+  CORE_ADDR value;
+  struct type *elt_type;
+  struct type *range_type;
+  struct type *ary_dim;
+
+  gdb_assert (TYPE_CODE (type) == TYPE_CODE_ARRAY);
+
+  elt_type = type;
+  range_type = check_typedef (TYPE_INDEX_TYPE (elt_type));
+  range_type = resolve_dynamic_range (range_type);
+
   ary_dim = check_typedef (TYPE_TARGET_TYPE (elt_type));
 
   if (ary_dim != NULL && TYPE_CODE (ary_dim) == TYPE_CODE_ARRAY)
-    elt_type = resolve_dynamic_bounds (TYPE_TARGET_TYPE (type), addr);
+    elt_type = resolve_dynamic_array (TYPE_TARGET_TYPE (type));
   else
     elt_type = TYPE_TARGET_TYPE (type);
 
-  range_type = create_range_type (NULL,
-                                 TYPE_TARGET_TYPE (range_type),
-                                 &low_bound, &high_bound);
   return create_array_type (copy_type (type),
                            elt_type,
                            range_type);
 }
 
-/* See gdbtypes.h  */
+/* Resolve dynamic bounds of members of the union TYPE to static
+   bounds.  */
 
-struct type *
-resolve_dynamic_type (struct type *type, CORE_ADDR addr)
+static struct type *
+resolve_dynamic_union (struct type *type, CORE_ADDR addr)
 {
-  struct type *real_type = check_typedef (type);
   struct type *resolved_type;
+  int i;
+  unsigned int max_len = 0;
+
+  gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
+
+  resolved_type = copy_type (type);
+  TYPE_FIELDS (resolved_type)
+    = TYPE_ALLOC (resolved_type,
+                 TYPE_NFIELDS (resolved_type) * sizeof (struct field));
+  memcpy (TYPE_FIELDS (resolved_type),
+         TYPE_FIELDS (type),
+         TYPE_NFIELDS (resolved_type) * sizeof (struct field));
+  for (i = 0; i < TYPE_NFIELDS (resolved_type); ++i)
+    {
+      struct type *t;
+
+      if (field_is_static (&TYPE_FIELD (type, i)))
+       continue;
+
+      t = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
+                                        addr, 0);
+      TYPE_FIELD_TYPE (resolved_type, i) = t;
+      if (TYPE_LENGTH (t) > max_len)
+       max_len = TYPE_LENGTH (t);
+    }
+
+  TYPE_LENGTH (resolved_type) = max_len;
+  return resolved_type;
+}
+
+/* Resolve dynamic bounds of members of the struct TYPE to static
+   bounds.  */
+
+static struct type *
+resolve_dynamic_struct (struct type *type, CORE_ADDR addr)
+{
+  struct type *resolved_type;
+  int i;
+  unsigned resolved_type_bit_length = 0;
+
+  gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT);
+  gdb_assert (TYPE_NFIELDS (type) > 0);
+
+  resolved_type = copy_type (type);
+  TYPE_FIELDS (resolved_type)
+    = TYPE_ALLOC (resolved_type,
+                 TYPE_NFIELDS (resolved_type) * sizeof (struct field));
+  memcpy (TYPE_FIELDS (resolved_type),
+         TYPE_FIELDS (type),
+         TYPE_NFIELDS (resolved_type) * sizeof (struct field));
+  for (i = 0; i < TYPE_NFIELDS (resolved_type); ++i)
+    {
+      unsigned new_bit_length;
+
+      if (field_is_static (&TYPE_FIELD (type, i)))
+       continue;
+
+      TYPE_FIELD_TYPE (resolved_type, i)
+       = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
+                                        addr, 0);
+
+      /* As we know this field is not a static field, the field's
+        field_loc_kind should be FIELD_LOC_KIND_BITPOS.  Verify
+        this is the case, but only trigger a simple error rather
+        than an internal error if that fails.  While failing
+        that verification indicates a bug in our code, the error
+        is not severe enough to suggest to the user he stops
+        his debugging session because of it.  */
+      if (TYPE_FIELD_LOC_KIND (resolved_type, i) != FIELD_LOC_KIND_BITPOS)
+       error (_("Cannot determine struct field location"
+                " (invalid location kind)"));
+      new_bit_length = TYPE_FIELD_BITPOS (resolved_type, i);
+      if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0)
+       new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i);
+      else
+       new_bit_length += (TYPE_LENGTH (TYPE_FIELD_TYPE (resolved_type, i))
+                          * TARGET_CHAR_BIT);
+
+      /* Normally, we would use the position and size of the last field
+        to determine the size of the enclosing structure.  But GCC seems
+        to be encoding the position of some fields incorrectly when
+        the struct contains a dynamic field that is not placed last.
+        So we compute the struct size based on the field that has
+        the highest position + size - probably the best we can do.  */
+      if (new_bit_length > resolved_type_bit_length)
+       resolved_type_bit_length = new_bit_length;
+    }
+
+  TYPE_LENGTH (resolved_type)
+    = (resolved_type_bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
+
+  return resolved_type;
+}
 
-  if (!is_dynamic_type (real_type))
+/* Worker for resolved_dynamic_type.  */
+
+static struct type *
+resolve_dynamic_type_internal (struct type *type, CORE_ADDR addr,
+                              int top_level)
+{
+  struct type *real_type = check_typedef (type);
+  struct type *resolved_type = type;
+
+  if (!is_dynamic_type_internal (real_type, top_level))
     return type;
 
-  resolved_type = resolve_dynamic_bounds (type, addr);
+  switch (TYPE_CODE (type))
+    {
+      case TYPE_CODE_TYPEDEF:
+       resolved_type = copy_type (type);
+       TYPE_TARGET_TYPE (resolved_type)
+         = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type), addr,
+                                          top_level);
+       break;
+
+      case TYPE_CODE_REF:
+       {
+         CORE_ADDR target_addr = read_memory_typed_address (addr, type);
+
+         resolved_type = copy_type (type);
+         TYPE_TARGET_TYPE (resolved_type)
+           = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type),
+                                            target_addr, top_level);
+         break;
+       }
+
+      case TYPE_CODE_ARRAY:
+       resolved_type = resolve_dynamic_array (type);
+       break;
+
+      case TYPE_CODE_RANGE:
+       resolved_type = resolve_dynamic_range (type);
+       break;
+
+    case TYPE_CODE_UNION:
+      resolved_type = resolve_dynamic_union (type, addr);
+      break;
+
+    case TYPE_CODE_STRUCT:
+      resolved_type = resolve_dynamic_struct (type, addr);
+      break;
+    }
 
   return resolved_type;
 }
 
+/* See gdbtypes.h  */
+
+struct type *
+resolve_dynamic_type (struct type *type, CORE_ADDR addr)
+{
+  return resolve_dynamic_type_internal (type, addr, 1);
+}
+
 /* Find the real type of TYPE.  This function returns the real type,
    after removing all layers of typedefs, and completing opaque or stub
    types.  Completion changes the TYPE argument, but stripping of
@@ -2545,7 +2724,7 @@ rank_function (struct type **parms, int nparms,
 
   bv = xmalloc (sizeof (struct badness_vector));
   bv->length = nargs + 1;      /* add 1 for the length-match rank.  */
-  bv->rank = xmalloc ((nargs + 1) * sizeof (int));
+  bv->rank = XNEWVEC (struct rank, nargs + 1);
 
   /* First compare the lengths of the supplied lists.
      If there is a mismatch, set it to a high value.  */
@@ -3070,6 +3249,8 @@ rank_one_type (struct type *parm, struct type *arg, struct value *value)
        case TYPE_CODE_CHAR:
        case TYPE_CODE_RANGE:
        case TYPE_CODE_BOOL:
+         if (TYPE_DECLARED_CLASS (arg))
+           return INCOMPATIBLE_TYPE_BADNESS;
          return INTEGER_PROMOTION_BADNESS;
        case TYPE_CODE_FLT:
          return INT_FLOAT_CONVERSION_BADNESS;
@@ -3087,6 +3268,8 @@ rank_one_type (struct type *parm, struct type *arg, struct value *value)
        case TYPE_CODE_RANGE:
        case TYPE_CODE_BOOL:
        case TYPE_CODE_ENUM:
+         if (TYPE_DECLARED_CLASS (parm) || TYPE_DECLARED_CLASS (arg))
+           return INCOMPATIBLE_TYPE_BADNESS;
          return INTEGER_CONVERSION_BADNESS;
        case TYPE_CODE_FLT:
          return INT_FLOAT_CONVERSION_BADNESS;
@@ -3100,6 +3283,8 @@ rank_one_type (struct type *parm, struct type *arg, struct value *value)
        case TYPE_CODE_RANGE:
        case TYPE_CODE_BOOL:
        case TYPE_CODE_ENUM:
+         if (TYPE_DECLARED_CLASS (arg))
+           return INCOMPATIBLE_TYPE_BADNESS;
          return INTEGER_CONVERSION_BADNESS;
        case TYPE_CODE_FLT:
          return INT_FLOAT_CONVERSION_BADNESS;
@@ -4344,6 +4529,10 @@ gdbtypes_post_init (struct gdbarch *gdbarch)
     = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
                 "<internal function>");
 
+  /* This type represents an xmethod.  */
+  builtin_type->xmethod
+    = arch_type (gdbarch, TYPE_CODE_XMETHOD, 0, "<xmethod>");
+
   return builtin_type;
 }
 
This page took 0.028457 seconds and 4 git commands to generate.