#include "gdbsupport/unordered_set.h"
#include "dwarf2/die.h"
-struct field_info;
-
/* Type used for delaying computation of method physnames.
See comments for compute_delayed_physnames. */
struct delayed_method_info
.debug_str_offsets section (8 or 4, depending on the address size). */
std::optional<ULONGEST> str_offsets_base;
- /* We may encounter a DIE where a property refers to a field in some
- outer type. For example, in Ada, an array length may depend on a
- field in some outer record. In this case, we need to be able to
- stash a pointer to the 'struct field' into the appropriate
- dynamic property baton. However, the fields aren't created until
- the type has been fully processed, so we need a temporary
- back-link to this object. */
- struct field_info *field_info = nullptr;
-
/* Mark used when releasing cached dies. */
bool m_mark : 1;
we're reading. */
std::vector<variant_part_builder> variant_parts;
- /* All the field batons that must be updated. This is only used
- when a type's property depends on a field of this structure; for
- example in Ada when an array's length may come from a field of an
- enclosing record. */
- gdb::unordered_map<dwarf2_property_baton *, sect_offset> field_batons;
-
/* Return the total number of fields (including baseclasses). */
int nfields () const
{
}
}
+/* A helper that computes the location of a field. The CU and the
+ DW_TAG_member DIE are passed in. The results are stored in
+ *FP. */
+
+static void
+compute_field_location (dwarf2_cu *cu, die_info *die, field *fp)
+{
+ /* Get type of field. */
+ fp->set_type (die_type (die, cu));
+
+ fp->set_loc_bitpos (0);
+
+ /* Get bit size of field (zero if none). */
+ attribute *attr = dwarf2_attr (die, DW_AT_bit_size, cu);
+ if (attr != nullptr)
+ fp->set_bitsize (attr->unsigned_constant ().value_or (0));
+ else
+ fp->set_bitsize (0);
+
+ /* Get bit offset of field. */
+ handle_member_location (die, cu, fp);
+}
+
/* Add an aggregate field to the field list. */
static void
}
/* Data member other than a C++ static data member. */
- /* Get type of field. */
- fp->set_type (die_type (die, cu));
-
- fp->set_loc_bitpos (0);
-
- /* Get bit size of field (zero if none). */
- attr = dwarf2_attr (die, DW_AT_bit_size, cu);
- if (attr != nullptr)
- fp->set_bitsize (attr->unsigned_constant ().value_or (0));
- else
- fp->set_bitsize (0);
-
- /* Get bit offset of field. */
- handle_member_location (die, cu, fp);
+ compute_field_location (cu, die, fp);
/* Get name of field. */
fieldname = dwarf2_name (die, cu);
update_field_batons. If OFFSET is not found, NULL is returned. */
static dwarf2_property_baton *
-find_field_create_baton (dwarf2_cu *cu, sect_offset offset)
+find_field_create_baton (dwarf2_cu *cu, die_info *die)
{
- field_info *fi = cu->field_info;
- /* Defensive programming in case we see unusual DWARF. */
- if (fi == nullptr)
- return nullptr;
- for (const auto &fld : fi->fields)
- if (fld.offset == offset)
- {
- dwarf2_property_baton *result
- = XOBNEW (&cu->per_objfile->objfile->objfile_obstack,
- struct dwarf2_property_baton);
- fi->field_batons[result] = offset;
- return result;
- }
- return nullptr;
-}
-
-/* Update all the stored field property batons. FI is the field info
- for the structure being created. TYPE is the corresponding struct
- type with its fields already filled in. This fills in the correct
- field for each baton that was stored while processing this
- type. */
-
-static void
-update_field_batons (field_info *fi, struct type *type)
-{
- int n_bases = fi->baseclasses.size ();
- for (auto &[baton, offset] : fi->field_batons)
- {
- for (int i = 0; i < fi->fields.size (); ++i)
- {
- if (fi->fields[i].offset == offset)
- {
- baton->field = &type->field (n_bases + i);
- break;
- }
- }
- }
+ dwarf2_property_baton *result
+ = XOBNEW (&cu->per_objfile->objfile->objfile_obstack,
+ struct dwarf2_property_baton);
+ memset (&result->field, 0, sizeof (result->field));
+ compute_field_location (cu, die, &result->field);
+ return result;
}
/* Finish creating a structure or union type, including filling in its
struct field_info fi;
std::vector<struct symbol *> template_args;
- scoped_restore save_field_info
- = make_scoped_restore (&cu->field_info, &fi);
-
for (die_info *child_die : die->children ())
handle_struct_member_die (child_die, type, &fi, &template_args, cu);
/* Attach fields and member functions to the type. */
if (fi.nfields () > 0)
- {
- dwarf2_attach_fields_to_type (&fi, type, cu);
- update_field_batons (&fi, type);
- }
-
+ dwarf2_attach_fields_to_type (&fi, type, cu);
if (!fi.fnfieldlists.empty ())
{
dwarf2_attach_fn_fields_to_type (&fi, type, cu);
case DW_AT_data_member_location:
case DW_AT_data_bit_offset:
{
- baton = find_field_create_baton (cu, target_die->sect_off);
+ baton = find_field_create_baton (cu, target_die);
if (baton == nullptr)
return 0;
baton->property_type = read_type_die (target_die->parent,
target_cu);
- baton->field = nullptr;
prop->set_field (baton);
break;
}
--- /dev/null
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2025 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* The data used for the structure. */
+
+unsigned char our_data[] = { 3, 7, 11, 13 };
+
+/* Dummy main function. */
+
+int
+main()
+{
+ asm ("main_label: .globl main_label");
+ return 0;
+}
--- /dev/null
+# Copyright 2025 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Test handling of an array type whose bound comes from the field of a
+# structure.
+
+load_lib dwarf.exp
+
+# This test can only be run on targets which support DWARF-2 and use gas.
+require dwarf2_support
+
+standard_testfile .c -debug.S
+
+# Set up the DWARF for the test.
+
+set asm_file [standard_output_file $srcfile2]
+Dwarf::assemble $asm_file {
+ global srcdir subdir srcfile
+
+ cu {} {
+ DW_TAG_compile_unit {
+ {DW_AT_language @DW_LANG_Ada95}
+ {DW_AT_name $srcfile}
+ } {
+ declare_labels byte array disc struct
+
+ byte: DW_TAG_base_type {
+ {DW_AT_byte_size 1 DW_FORM_sdata}
+ {DW_AT_encoding @DW_ATE_unsigned}
+ {DW_AT_name byte}
+ }
+
+ array: DW_TAG_array_type {
+ {DW_AT_name array_type}
+ {DW_AT_type :$byte}
+ } {
+ DW_TAG_subrange_type {
+ {DW_AT_type :$byte}
+ {DW_AT_upper_bound :$disc}
+ }
+ }
+
+ struct: DW_TAG_structure_type {
+ {DW_AT_name discriminated}
+ {DW_AT_byte_size 4 DW_FORM_sdata}
+ } {
+ disc: DW_TAG_member {
+ {DW_AT_name disc}
+ {DW_AT_type :$byte}
+ {DW_AT_data_member_location 0 DW_FORM_sdata}
+ }
+ DW_TAG_member {
+ {DW_AT_name nums}
+ {DW_AT_type :$array}
+ {DW_AT_data_member_location 1 DW_FORM_sdata}
+ }
+ }
+
+ DW_TAG_variable {
+ {DW_AT_name "value"}
+ {DW_AT_type :$struct}
+ {DW_AT_external 1 DW_FORM_flag}
+ {DW_AT_location {DW_OP_addr [gdb_target_symbol "our_data"]}
+ SPECIAL_expr}
+ }
+ }
+ }
+}
+
+if { [prepare_for_testing "failed to prepare" ${testfile} \
+ [list $srcfile $asm_file] {nodebug}] } {
+ return -1
+}
+
+gdb_test_no_output "set language ada"
+gdb_test "print value" \
+ [string_to_regexp " = (disc => 3, nums => (7, 11, 13))"]