+/* Resolve an array or string type with dynamic properties, return a new
+ type with the dynamic properties resolved to actual values. The
+ ADDR_STACK represents the location of the object being resolved. */
+
+static struct type *
+resolve_dynamic_array_or_string (struct type *type,
+ struct property_addr_info *addr_stack)
+{
+ CORE_ADDR value;
+ int rank = 0;
+
+ /* For dynamic type resolution strings can be treated like arrays of
+ characters. */
+ gdb_assert (type->code () == TYPE_CODE_ARRAY
+ || type->code () == TYPE_CODE_STRING);
+
+ type = copy_type (type);
+
+ /* Resolve the rank property to get rank value. */
+ struct dynamic_prop *prop = TYPE_RANK_PROP (type);
+ if (dwarf2_evaluate_property (prop, nullptr, addr_stack, &value))
+ {
+ prop->set_const_val (value);
+ rank = value;
+
+ if (rank == 0)
+ {
+ /* Rank is zero, if a variable is passed as an argument to a
+ function. In this case the resolved type should not be an
+ array, but should instead be that of an array element. */
+ struct type *dynamic_array_type = type;
+ type = copy_type (TYPE_TARGET_TYPE (dynamic_array_type));
+ struct dynamic_prop_list *prop_list
+ = TYPE_MAIN_TYPE (dynamic_array_type)->dyn_prop_list;
+ if (prop_list != nullptr)
+ {
+ struct obstack *obstack
+ = &type->objfile_owner ()->objfile_obstack;
+ TYPE_MAIN_TYPE (type)->dyn_prop_list
+ = copy_dynamic_prop_list (obstack, prop_list);
+ }
+ return type;
+ }
+ else if (type->code () == TYPE_CODE_STRING && rank != 1)
+ {
+ /* What would this even mean? A string with a dynamic rank
+ greater than 1. */
+ error (_("unable to handle string with dynamic rank greater than 1"));
+ }
+ else if (rank > 1)
+ {
+ /* Arrays with dynamic rank are initially just an array type
+ with a target type that is the array element.
+
+ However, now we know the rank of the array we need to build
+ the array of arrays structure that GDB expects, that is we
+ need an array type that has a target which is an array type,
+ and so on, until eventually, we have the element type at the
+ end of the chain. Create all the additional array types here
+ by copying the top level array type. */
+ struct type *element_type = TYPE_TARGET_TYPE (type);
+ struct type *rank_type = type;
+ for (int i = 1; i < rank; i++)
+ {
+ TYPE_TARGET_TYPE (rank_type) = copy_type (rank_type);
+ rank_type = TYPE_TARGET_TYPE (rank_type);
+ }
+ TYPE_TARGET_TYPE (rank_type) = element_type;
+ }
+ }
+ else
+ {
+ rank = 1;
+
+ for (struct type *tmp_type = check_typedef (TYPE_TARGET_TYPE (type));
+ tmp_type->code () == TYPE_CODE_ARRAY;
+ tmp_type = check_typedef (TYPE_TARGET_TYPE (tmp_type)))
+ ++rank;
+ }
+
+ /* The rank that we calculated above is actually a count of the number of
+ ranks. However, when we resolve the type of each individual array
+ rank we should actually use a rank "offset", e.g. an array with a rank
+ count of 1 (calculated above) will use the rank offset 0 in order to
+ resolve the details of the first array dimension. As a result, we
+ reduce the rank by 1 here. */
+ --rank;
+
+ return resolve_dynamic_array_or_string_1 (type, addr_stack, rank, true);
+}
+