1818#include " typelib/TypeDatabase.h"
1919#include " typelib/TypeInterface.h"
2020
21+ #include " llvm/ADT/StringExtras.h"
22+ #include " llvm/Support/MD5.h"
23+
2124#include < cassert>
2225#include < cstddef>
2326#include < cstdint>
@@ -136,6 +139,34 @@ dimeta::ArraySize vector_num_elements(const Type& type) {
136139 });
137140}
138141
142+ std::string get_anon_struct_identifier (const dimeta::QualifiedCompound& compound) {
143+ llvm::MD5 compound_hash;
144+ if (compound.type .members .empty ()) {
145+ LOG_WARNING (" Anonymous struct has no members" )
146+ }
147+ for (const auto & [member, offset, size] :
148+ llvm::zip (compound.type .members , compound.type .offsets , compound.type .sizes )) {
149+ compound_hash.update (member->name );
150+ compound_hash.update (offset);
151+ compound_hash.update (size);
152+ compound_hash.update (std::visit (overload{[&](const dimeta::QualifiedFundamental& member_fundamental) {
153+ return std::to_string (
154+ static_cast <int >(member_fundamental.type .encoding )) +
155+ std::to_string (static_cast <int >(member_fundamental.type .extent ));
156+ },
157+ [&](const dimeta::QualifiedCompound& member_compound) {
158+ return get_anon_struct_identifier (member_compound);
159+ }},
160+ member->member ));
161+ compound_hash.update (" \0 " );
162+ }
163+ compound_hash.update (compound.type .extent );
164+ compound_hash.update (" \0 " );
165+ llvm::MD5::MD5Result hash_result;
166+ compound_hash.final (hash_result);
167+ return " anonymous_compound_" + std::string (hash_result.digest ().str ());
168+ }
169+
139170template <typename Type>
140171dimeta::ArraySize array_size (const Type& type) {
141172 return detail::apply_function (type, [](const auto & t) -> dimeta::Extent {
@@ -158,6 +189,10 @@ std::string name_or_typedef_of(const Type& type) {
158189 const bool no_name = qual_type.type .name .empty ();
159190 if constexpr (std::is_same_v<Type, typename dimeta::QualifiedCompound>) {
160191 const bool no_identifier = qual_type.type .identifier .empty ();
192+ const bool no_typedef = qual_type.typedef_name .empty ();
193+ if (no_identifier && no_name && no_typedef) {
194+ return get_anon_struct_identifier (qual_type);
195+ }
161196 if (no_identifier && no_name) {
162197 return qual_type.typedef_name ;
163198 }
0 commit comments