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>
@@ -108,6 +111,34 @@ dimeta::ArraySize vector_num_elements(const Type& type) {
108111 });
109112}
110113
114+ std::string get_anon_struct_identifier (const dimeta::QualifiedCompound& compound) {
115+ llvm::MD5 compound_hash;
116+ if (compound.type .members .empty ()) {
117+ LOG_WARNING (" Anonymous struct has no members" )
118+ }
119+ for (const auto & [member, offset, size] :
120+ llvm::zip (compound.type .members , compound.type .offsets , compound.type .sizes )) {
121+ compound_hash.update (member->name );
122+ compound_hash.update (offset);
123+ compound_hash.update (size);
124+ compound_hash.update (std::visit (overload{[&](const dimeta::QualifiedFundamental& member_fundamental) {
125+ return std::to_string (
126+ static_cast <int >(member_fundamental.type .encoding )) +
127+ std::to_string (static_cast <int >(member_fundamental.type .extent ));
128+ },
129+ [&](const dimeta::QualifiedCompound& member_compound) {
130+ return get_anon_struct_identifier (member_compound);
131+ }},
132+ member->member ));
133+ compound_hash.update (" \0 " );
134+ }
135+ compound_hash.update (compound.type .extent );
136+ compound_hash.update (" \0 " );
137+ llvm::MD5::MD5Result hash_result;
138+ compound_hash.final (hash_result);
139+ return " anonymous_compound_" + std::string (hash_result.digest ().str ());
140+ }
141+
111142template <typename Type>
112143dimeta::ArraySize array_size (const Type& type) {
113144 return detail::apply_func (type, [](const auto & t) -> dimeta::Extent {
@@ -130,6 +161,10 @@ std::string name_or_typedef_of(const Type& type) {
130161 const bool no_name = qual_type.type .name .empty ();
131162 if constexpr (std::is_same_v<Type, typename dimeta::QualifiedCompound>) {
132163 const bool no_identifier = qual_type.type .identifier .empty ();
164+ const bool no_typedef = qual_type.typedef_name .empty ();
165+ if (no_identifier && no_name && no_typedef) {
166+ return get_anon_struct_identifier (qual_type);
167+ }
133168 if (no_identifier && no_name) {
134169 return qual_type.typedef_name ;
135170 }
0 commit comments