+/* Worker for is_dynamic_type. */
+
+static int
+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;
+ but it makes sense in this context, because the point is to determine
+ whether any part of the type needs to be resolved before it can
+ be exploited. */
+ if (TYPE_DATA_LOCATION (type) != NULL
+ && (TYPE_DATA_LOCATION_KIND (type) == PROP_LOCEXPR
+ || TYPE_DATA_LOCATION_KIND (type) == PROP_LOCLIST))
+ return 1;
+
+ if (TYPE_ASSOCIATED_PROP (type))
+ return 1;
+
+ if (TYPE_ALLOCATED_PROP (type))
+ return 1;
+
+ switch (TYPE_CODE (type))
+ {
+ case TYPE_CODE_RANGE:
+ {
+ /* A range type is obviously dynamic if it has at least one
+ dynamic bound. But also consider the range type to be
+ dynamic when its subtype is dynamic, even if the bounds
+ 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), 0));
+ }
+
+ case TYPE_CODE_ARRAY:
+ {
+ gdb_assert (TYPE_NFIELDS (type) == 1);
+
+ /* 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;
+ 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;
+ }
+ break;
+ }
+
+ return 0;
+}
+
+/* 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, 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
+ of that type. */
+
+static struct type *
+resolve_dynamic_range (struct type *dyn_range_type,
+ struct property_addr_info *addr_stack)
+{
+ CORE_ADDR value;
+ struct type *static_range_type, *static_target_type;
+ const struct dynamic_prop *prop;
+ struct dynamic_prop low_bound, high_bound;
+
+ gdb_assert (TYPE_CODE (dyn_range_type) == TYPE_CODE_RANGE);
+
+ prop = &TYPE_RANGE_DATA (dyn_range_type)->low;
+ if (dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
+ {
+ low_bound.kind = PROP_CONST;
+ low_bound.data.const_val = value;
+ }
+ else
+ {
+ low_bound.kind = PROP_UNDEFINED;
+ low_bound.data.const_val = 0;
+ }
+
+ prop = &TYPE_RANGE_DATA (dyn_range_type)->high;
+ if (dwarf2_evaluate_property (prop, NULL, addr_stack, &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
+ {
+ high_bound.kind = PROP_UNDEFINED;
+ high_bound.data.const_val = 0;
+ }
+
+ static_target_type
+ = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (dyn_range_type),
+ addr_stack, 0);
+ static_range_type = create_range_type (copy_type (dyn_range_type),
+ static_target_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.
+ ADDR_STACK is a stack of struct property_addr_info to be used
+ if needed during the dynamic resolution. */
+
+static struct type *
+resolve_dynamic_array (struct type *type,
+ struct property_addr_info *addr_stack)
+{
+ CORE_ADDR value;
+ struct type *elt_type;
+ struct type *range_type;
+ struct type *ary_dim;
+ struct dynamic_prop *prop;
+
+ gdb_assert (TYPE_CODE (type) == TYPE_CODE_ARRAY);
+
+ type = copy_type (type);
+
+ elt_type = type;
+ range_type = check_typedef (TYPE_INDEX_TYPE (elt_type));
+ range_type = resolve_dynamic_range (range_type, addr_stack);
+
+ /* Resolve allocated/associated here before creating a new array type, which
+ will update the length of the array accordingly. */
+ prop = TYPE_ALLOCATED_PROP (type);
+ if (prop != NULL && dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
+ {
+ TYPE_DYN_PROP_ADDR (prop) = value;
+ TYPE_DYN_PROP_KIND (prop) = PROP_CONST;
+ }
+ prop = TYPE_ASSOCIATED_PROP (type);
+ if (prop != NULL && dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
+ {
+ TYPE_DYN_PROP_ADDR (prop) = value;
+ TYPE_DYN_PROP_KIND (prop) = PROP_CONST;
+ }
+
+ 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 (ary_dim, addr_stack);
+ else
+ elt_type = TYPE_TARGET_TYPE (type);
+
+ return create_array_type_with_stride (type, elt_type, range_type,
+ TYPE_FIELD_BITSIZE (type, 0));
+}
+
+/* Resolve dynamic bounds of members of the union TYPE to static
+ bounds. ADDR_STACK is a stack of struct property_addr_info
+ to be used if needed during the dynamic resolution. */
+
+static struct type *
+resolve_dynamic_union (struct type *type,
+ struct property_addr_info *addr_stack)
+{
+ 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)
+ = (struct field *) 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_stack, 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. ADDR_STACK is a stack of struct property_addr_info to
+ be used if needed during the dynamic resolution. */
+
+static struct type *
+resolve_dynamic_struct (struct type *type,
+ struct property_addr_info *addr_stack)
+{
+ 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)
+ = (struct field *) 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;
+ struct property_addr_info pinfo;
+
+ if (field_is_static (&TYPE_FIELD (type, i)))
+ continue;
+
+ /* 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 (type, i) != FIELD_LOC_KIND_BITPOS)
+ error (_("Cannot determine struct field location"
+ " (invalid location kind)"));
+
+ pinfo.type = check_typedef (TYPE_FIELD_TYPE (type, i));
+ pinfo.valaddr = addr_stack->valaddr;
+ pinfo.addr
+ = (addr_stack->addr
+ + (TYPE_FIELD_BITPOS (resolved_type, i) / TARGET_CHAR_BIT));
+ pinfo.next = addr_stack;
+
+ TYPE_FIELD_TYPE (resolved_type, i)
+ = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
+ &pinfo, 0);
+ gdb_assert (TYPE_FIELD_LOC_KIND (resolved_type, i)
+ == FIELD_LOC_KIND_BITPOS);
+
+ 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;
+ }
+
+ /* The length of a type won't change for fortran, but it does for C and Ada.
+ For fortran the size of dynamic fields might change over time but not the
+ type length of the structure. If we adapt it, we run into problems
+ when calculating the element offset for arrays of structs. */
+ if (current_language->la_language != language_fortran)
+ 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;
+}
+
+/* Worker for resolved_dynamic_type. */
+
+static struct type *
+resolve_dynamic_type_internal (struct type *type,
+ 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, 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,
+ top_level);
+ }
+ else
+ {
+ /* Before trying to resolve TYPE, make sure it is not a stub. */
+ type = real_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;
+
+ case TYPE_CODE_RANGE:
+ resolved_type = resolve_dynamic_range (type, addr_stack);
+ break;
+
+ case TYPE_CODE_UNION:
+ resolved_type = resolve_dynamic_union (type, addr_stack);
+ break;
+
+ case TYPE_CODE_STRUCT:
+ resolved_type = resolve_dynamic_struct (type, addr_stack);
+ break;
+ }
+ }
+
+ /* Resolve data_location attribute. */
+ prop = TYPE_DATA_LOCATION (resolved_type);
+ if (prop != NULL
+ && dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
+ {
+ TYPE_DYN_PROP_ADDR (prop) = value;
+ TYPE_DYN_PROP_KIND (prop) = PROP_CONST;
+ }
+
+ return resolved_type;
+}
+
+/* See gdbtypes.h */
+
+struct type *
+resolve_dynamic_type (struct type *type, const gdb_byte *valaddr,
+ CORE_ADDR addr)
+{
+ struct property_addr_info pinfo
+ = {check_typedef (type), valaddr, addr, NULL};
+
+ return resolve_dynamic_type_internal (type, &pinfo, 1);
+}
+
+/* See gdbtypes.h */
+
+struct dynamic_prop *
+get_dyn_prop (enum dynamic_prop_node_kind prop_kind, const struct type *type)
+{
+ struct dynamic_prop_list *node = TYPE_DYN_PROP_LIST (type);
+
+ while (node != NULL)
+ {
+ if (node->prop_kind == prop_kind)
+ return &node->prop;
+ node = node->next;
+ }
+ return NULL;
+}
+
+/* See gdbtypes.h */
+
+void
+add_dyn_prop (enum dynamic_prop_node_kind prop_kind, struct dynamic_prop prop,
+ struct type *type, struct objfile *objfile)
+{
+ struct dynamic_prop_list *temp;
+
+ gdb_assert (TYPE_OBJFILE_OWNED (type));
+
+ temp = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop_list);
+ temp->prop_kind = prop_kind;
+ temp->prop = prop;
+ temp->next = TYPE_DYN_PROP_LIST (type);
+
+ TYPE_DYN_PROP_LIST (type) = temp;
+}
+
+/* Remove dynamic property from TYPE in case it exists. */
+
+void
+remove_dyn_prop (enum dynamic_prop_node_kind prop_kind,
+ struct type *type)
+{
+ struct dynamic_prop_list *prev_node, *curr_node;
+
+ curr_node = TYPE_DYN_PROP_LIST (type);
+ prev_node = NULL;
+
+ while (NULL != curr_node)
+ {
+ if (curr_node->prop_kind == prop_kind)
+ {
+ /* Update the linked list but don't free anything.
+ The property was allocated on objstack and it is not known
+ if we are on top of it. Nevertheless, everything is released
+ when the complete objstack is freed. */
+ if (NULL == prev_node)
+ TYPE_DYN_PROP_LIST (type) = curr_node->next;
+ else
+ prev_node->next = curr_node->next;
+
+ return;
+ }
+
+ prev_node = curr_node;
+ curr_node = curr_node->next;
+ }
+}
+