Fix typo in gdb.python/py-objfile.exp
[deliverable/binutils-gdb.git] / gdb / gdbtypes.c
index 4aec5ec526c37a21cdb34ba5e743e2f0f8165dab..e6a95474a5a2793ca4756f2fd2fb42c7c871e46a 100644 (file)
@@ -252,6 +252,17 @@ get_target_type (struct type *type)
   return type;
 }
 
+/* See gdbtypes.h.  */
+
+unsigned int
+type_length_units (struct type *type)
+{
+  struct gdbarch *arch = get_type_arch (type);
+  int unit_size = gdbarch_addressable_memory_unit_size (arch);
+
+  return TYPE_LENGTH (type) / unit_size;
+}
+
 /* Alloc a new type instance structure, fill it with some defaults,
    and point it at OLDTYPE.  Allocate the new type instance from the
    same place as OLDTYPE.  */
@@ -898,7 +909,7 @@ has_static_range (const struct range_bounds *bounds)
 int
 get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
 {
-  CHECK_TYPEDEF (type);
+  type = check_typedef (type);
   switch (TYPE_CODE (type))
     {
     case TYPE_CODE_RANGE:
@@ -1004,6 +1015,45 @@ get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
   return 1;
 }
 
+/* Assuming that TYPE is a discrete type and VAL is a valid integer
+   representation of a value of this type, save the corresponding
+   position number in POS.
+
+   Its differs from VAL only in the case of enumeration types.  In
+   this case, the position number of the value of the first listed
+   enumeration literal is zero; the position number of the value of
+   each subsequent enumeration literal is one more than that of its
+   predecessor in the list.
+
+   Return 1 if the operation was successful.  Return zero otherwise,
+   in which case the value of POS is unmodified.
+*/
+
+int
+discrete_position (struct type *type, LONGEST val, LONGEST *pos)
+{
+  if (TYPE_CODE (type) == TYPE_CODE_ENUM)
+    {
+      int i;
+
+      for (i = 0; i < TYPE_NFIELDS (type); i += 1)
+        {
+          if (val == TYPE_FIELD_ENUMVAL (type, i))
+           {
+             *pos = i;
+             return 1;
+           }
+        }
+      /* Invalid enumeration value.  */
+      return 0;
+    }
+  else
+    {
+      *pos = val;
+      return 1;
+    }
+}
+
 /* Create an array type using either a blank type supplied in
    RESULT_TYPE, or creating a new type, inheriting the objfile from
    RANGE_TYPE.
@@ -1035,7 +1085,7 @@ create_array_type_with_stride (struct type *result_type,
 
       if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
        low_bound = high_bound = 0;
-      CHECK_TYPEDEF (element_type);
+      element_type = check_typedef (element_type);
       /* Be careful when setting the array length.  Ada arrays can be
         empty arrays with the high_bound being smaller than the low_bound.
         In such cases, the array length should be zero.  */
@@ -1344,7 +1394,7 @@ type_name_no_tag_or_error (struct type *type)
   const char *name;
   struct objfile *objfile;
 
-  CHECK_TYPEDEF (type);
+  type = check_typedef (type);
 
   name = type_name_no_tag (type);
   if (name != NULL)
@@ -1523,7 +1573,7 @@ lookup_struct_elt_type (struct type *type, const char *name, int noerr)
 
   for (;;)
     {
-      CHECK_TYPEDEF (type);
+      type = check_typedef (type);
       if (TYPE_CODE (type) != TYPE_CODE_PTR
          && TYPE_CODE (type) != TYPE_CODE_REF)
        break;
@@ -1600,7 +1650,7 @@ get_unsigned_type_max (struct type *type, ULONGEST *max)
 {
   unsigned int n;
 
-  CHECK_TYPEDEF (type);
+  type = check_typedef (type);
   gdb_assert (TYPE_CODE (type) == TYPE_CODE_INT && TYPE_UNSIGNED (type));
   gdb_assert (TYPE_LENGTH (type) <= sizeof (ULONGEST));
 
@@ -1617,7 +1667,7 @@ get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
 {
   unsigned int n;
 
-  CHECK_TYPEDEF (type);
+  type = check_typedef (type);
   gdb_assert (TYPE_CODE (type) == TYPE_CODE_INT && !TYPE_UNSIGNED (type));
   gdb_assert (TYPE_LENGTH (type) <= sizeof (LONGEST));
 
@@ -1636,7 +1686,7 @@ get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
 int
 internal_type_vptr_fieldno (struct type *type)
 {
-  CHECK_TYPEDEF (type);
+  type = check_typedef (type);
   gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
              || TYPE_CODE (type) == TYPE_CODE_UNION);
   if (!HAVE_CPLUS_STRUCT (type))
@@ -1649,7 +1699,7 @@ internal_type_vptr_fieldno (struct type *type)
 void
 set_type_vptr_fieldno (struct type *type, int fieldno)
 {
-  CHECK_TYPEDEF (type);
+  type = check_typedef (type);
   gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
              || TYPE_CODE (type) == TYPE_CODE_UNION);
   if (!HAVE_CPLUS_STRUCT (type))
@@ -1663,7 +1713,7 @@ set_type_vptr_fieldno (struct type *type, int fieldno)
 struct type *
 internal_type_vptr_basetype (struct type *type)
 {
-  CHECK_TYPEDEF (type);
+  type = check_typedef (type);
   gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
              || TYPE_CODE (type) == TYPE_CODE_UNION);
   gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_CPLUS_STUFF);
@@ -1675,7 +1725,7 @@ internal_type_vptr_basetype (struct type *type)
 void
 set_type_vptr_basetype (struct type *type, struct type *basetype)
 {
-  CHECK_TYPEDEF (type);
+  type = check_typedef (type);
   gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
              || TYPE_CODE (type) == TYPE_CODE_UNION);
   if (!HAVE_CPLUS_STRUCT (type))
@@ -1698,7 +1748,7 @@ set_type_vptr_basetype (struct type *type, struct type *basetype)
 int
 get_vptr_fieldno (struct type *type, struct type **basetypep)
 {
-  CHECK_TYPEDEF (type);
+  type = check_typedef (type);
 
   if (TYPE_VPTR_FIELDNO (type) < 0)
     {
@@ -1748,10 +1798,14 @@ stub_noname_complaint (void)
 /* Worker for is_dynamic_type.  */
 
 static int
-is_dynamic_type_internal (struct type *type)
+is_dynamic_type_internal (struct type *type, int top_level)
 {
   type = check_typedef (type);
 
+  /* 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));
+
   /* Types that have a dynamic TYPE_DATA_LOCATION are considered
      dynamic, even if the type itself is statically defined.
      From a user's point of view, this may appear counter-intuitive;
@@ -1773,7 +1827,7 @@ is_dynamic_type_internal (struct type *type)
           of the range type are static.  It allows us to assume that
           the subtype of a static range type is also static.  */
        return (!has_static_range (TYPE_RANGE_DATA (type))
-               || is_dynamic_type_internal (TYPE_TARGET_TYPE (type)));
+               || is_dynamic_type_internal (TYPE_TARGET_TYPE (type), 0));
       }
 
     case TYPE_CODE_ARRAY:
@@ -1782,9 +1836,9 @@ is_dynamic_type_internal (struct type *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)))
+       if (is_dynamic_type_internal (TYPE_INDEX_TYPE (type), 0))
          return 1;
-       return is_dynamic_type_internal (TYPE_TARGET_TYPE (type));
+       return is_dynamic_type_internal (TYPE_TARGET_TYPE (type), 0);
       }
 
     case TYPE_CODE_STRUCT:
@@ -1794,7 +1848,7 @@ is_dynamic_type_internal (struct type *type)
 
        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)))
+             && is_dynamic_type_internal (TYPE_FIELD_TYPE (type, i), 0))
            return 1;
       }
       break;
@@ -1808,11 +1862,11 @@ is_dynamic_type_internal (struct type *type)
 int
 is_dynamic_type (struct type *type)
 {
-  return is_dynamic_type_internal (type);
+  return is_dynamic_type_internal (type, 1);
 }
 
 static struct type *resolve_dynamic_type_internal
-  (struct type *type, struct property_addr_info *addr_stack);
+  (struct type *type, struct property_addr_info *addr_stack, int top_level);
 
 /* Given a dynamic range type (dyn_range_type) and a stack of
    struct property_addr_info elements, return a static version
@@ -1860,7 +1914,7 @@ resolve_dynamic_range (struct type *dyn_range_type,
 
   static_target_type
     = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (dyn_range_type),
-                                    addr_stack);
+                                    addr_stack, 0);
   static_range_type = create_range_type (copy_type (dyn_range_type),
                                         static_target_type,
                                         &low_bound, &high_bound);
@@ -1890,13 +1944,13 @@ resolve_dynamic_array (struct type *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_array (TYPE_TARGET_TYPE (type), addr_stack);
+    elt_type = resolve_dynamic_array (ary_dim, addr_stack);
   else
     elt_type = TYPE_TARGET_TYPE (type);
 
-  return create_array_type (copy_type (type),
-                           elt_type,
-                           range_type);
+  return create_array_type_with_stride (copy_type (type),
+                                       elt_type, range_type,
+                                       TYPE_FIELD_BITSIZE (type, 0));
 }
 
 /* Resolve dynamic bounds of members of the union TYPE to static
@@ -1928,7 +1982,7 @@ resolve_dynamic_union (struct type *type,
        continue;
 
       t = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
-                                        addr_stack);
+                                        addr_stack, 0);
       TYPE_FIELD_TYPE (resolved_type, i) = t;
       if (TYPE_LENGTH (t) > max_len)
        max_len = TYPE_LENGTH (t);
@@ -1980,12 +2034,13 @@ resolve_dynamic_struct (struct type *type,
                 " (invalid location kind)"));
 
       pinfo.type = check_typedef (TYPE_FIELD_TYPE (type, i));
+      pinfo.valaddr = addr_stack->valaddr;
       pinfo.addr = addr_stack->addr;
       pinfo.next = addr_stack;
 
       TYPE_FIELD_TYPE (resolved_type, i)
        = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
-                                        &pinfo);
+                                        &pinfo, 0);
       gdb_assert (TYPE_FIELD_LOC_KIND (resolved_type, i)
                  == FIELD_LOC_KIND_BITPOS);
 
@@ -2009,6 +2064,10 @@ resolve_dynamic_struct (struct type *type,
   TYPE_LENGTH (resolved_type)
     = (resolved_type_bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
 
+  /* The Ada language uses this field as a cache for static fixed types: reset
+     it as RESOLVED_TYPE must have its own static fixed type.  */
+  TYPE_TARGET_TYPE (resolved_type) = NULL;
+
   return resolved_type;
 }
 
@@ -2016,21 +2075,23 @@ resolve_dynamic_struct (struct type *type,
 
 static struct type *
 resolve_dynamic_type_internal (struct type *type,
-                              struct property_addr_info *addr_stack)
+                              struct property_addr_info *addr_stack,
+                              int top_level)
 {
   struct type *real_type = check_typedef (type);
   struct type *resolved_type = type;
   struct dynamic_prop *prop;
   CORE_ADDR value;
 
-  if (!is_dynamic_type_internal (real_type))
+  if (!is_dynamic_type_internal (real_type, top_level))
     return type;
 
   if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
     {
       resolved_type = copy_type (type);
       TYPE_TARGET_TYPE (resolved_type)
-       = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type), addr_stack);
+       = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type), addr_stack,
+                                        top_level);
     }
   else 
     {
@@ -2039,6 +2100,25 @@ resolve_dynamic_type_internal (struct type *type,
 
       switch (TYPE_CODE (type))
        {
+       case TYPE_CODE_REF:
+         {
+           struct property_addr_info pinfo;
+
+           pinfo.type = check_typedef (TYPE_TARGET_TYPE (type));
+           pinfo.valaddr = NULL;
+           if (addr_stack->valaddr != NULL)
+             pinfo.addr = extract_typed_address (addr_stack->valaddr, type);
+           else
+             pinfo.addr = read_memory_typed_address (addr_stack->addr, type);
+           pinfo.next = addr_stack;
+
+           resolved_type = copy_type (type);
+           TYPE_TARGET_TYPE (resolved_type)
+             = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type),
+                                              &pinfo, top_level);
+           break;
+         }
+
        case TYPE_CODE_ARRAY:
          resolved_type = resolve_dynamic_array (type, addr_stack);
          break;
@@ -2071,11 +2151,13 @@ resolve_dynamic_type_internal (struct type *type,
 /* See gdbtypes.h  */
 
 struct type *
-resolve_dynamic_type (struct type *type, CORE_ADDR addr)
+resolve_dynamic_type (struct type *type, const gdb_byte *valaddr,
+                     CORE_ADDR addr)
 {
-  struct property_addr_info pinfo = {check_typedef (type), addr, NULL};
+  struct property_addr_info pinfo
+    = {check_typedef (type), valaddr, addr, NULL};
 
-  return resolve_dynamic_type_internal (type, &pinfo);
+  return resolve_dynamic_type_internal (type, &pinfo, 1);
 }
 
 /* See gdbtypes.h  */
@@ -2608,7 +2690,7 @@ can_dereference (struct type *t)
 {
   /* FIXME: Should we return true for references as well as
      pointers?  */
-  CHECK_TYPEDEF (t);
+  t = check_typedef (t);
   return
     (t != NULL
      && TYPE_CODE (t) == TYPE_CODE_PTR
@@ -2618,7 +2700,7 @@ can_dereference (struct type *t)
 int
 is_integral_type (struct type *t)
 {
-  CHECK_TYPEDEF (t);
+  t = check_typedef (t);
   return
     ((t != NULL)
      && ((TYPE_CODE (t) == TYPE_CODE_INT)
@@ -2634,7 +2716,7 @@ is_integral_type (struct type *t)
 static int
 is_scalar_type (struct type *type)
 {
-  CHECK_TYPEDEF (type);
+  type = check_typedef (type);
 
   switch (TYPE_CODE (type))
     {
@@ -2656,7 +2738,7 @@ is_scalar_type (struct type *type)
 int
 is_scalar_type_recursive (struct type *t)
 {
-  CHECK_TYPEDEF (t);
+  t = check_typedef (t);
 
   if (is_scalar_type (t))
     return 1;
@@ -2738,8 +2820,8 @@ distance_to_ancestor (struct type *base, struct type *dclass, int is_public)
   int i;
   int d;
 
-  CHECK_TYPEDEF (base);
-  CHECK_TYPEDEF (dclass);
+  base = check_typedef (base);
+  dclass = check_typedef (dclass);
 
   if (class_types_same_p (base, dclass))
     return 0;
@@ -2788,8 +2870,8 @@ is_unique_ancestor_worker (struct type *base, struct type *dclass,
 {
   int i, count = 0;
 
-  CHECK_TYPEDEF (base);
-  CHECK_TYPEDEF (dclass);
+  base = check_typedef (base);
+  dclass = check_typedef (dclass);
 
   for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
     {
@@ -3108,8 +3190,8 @@ static int
 check_types_equal (struct type *type1, struct type *type2,
                   VEC (type_equality_entry_d) **worklist)
 {
-  CHECK_TYPEDEF (type1);
-  CHECK_TYPEDEF (type2);
+  type1 = check_typedef (type1);
+  type2 = check_typedef (type2);
 
   if (type1 == type2)
     return 1;
@@ -3867,7 +3949,13 @@ print_gnat_stuff (struct type *type, int spaces)
 {
   struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
 
-  recursive_dump_type (descriptive_type, spaces + 2);
+  if (descriptive_type == NULL)
+    printfi_filtered (spaces + 2, "no descriptive type\n");
+  else
+    {
+      printfi_filtered (spaces + 2, "descriptive type\n");
+      recursive_dump_type (descriptive_type, spaces + 4);
+    }
 }
 
 static struct obstack dont_print_type_obstack;
This page took 0.029729 seconds and 4 git commands to generate.