Skip to content

Commit

Permalink
Avoid generating invalid Debug traits (#1473)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Jan 28, 2022
1 parent a8b3a8b commit 4bd7251
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 1,357 deletions.
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/structs.rs
Expand Up @@ -194,7 +194,7 @@ fn gen_compare_traits(def: &TypeDef, name: &TokenStream, cfg: &Cfg, gen: &Gen) -
}

fn gen_debug(def: &TypeDef, ident: &TokenStream, cfg: &Cfg, gen: &Gen) -> TokenStream {
if gen.sys || def.is_union() || def.has_explicit() || def.is_packed() {
if gen.sys || def.has_union() || def.has_pack() {
quote! {}
} else {
let name = ident.as_str();
Expand Down
14 changes: 11 additions & 3 deletions crates/libs/metadata/src/element_type.rs
Expand Up @@ -199,10 +199,18 @@ impl ElementType {
}
}

pub fn has_explicit(&self) -> bool {
pub fn has_union(&self) -> bool {
match self {
Self::TypeDef(t) => t.has_explicit(),
Self::Array((kind, _)) => kind.has_explicit(),
Self::TypeDef(t) => t.has_union(),
Self::Array((kind, _)) => kind.has_union(),
_ => false,
}
}

pub fn has_pack(&self) -> bool {
match self {
Self::TypeDef(t) => t.has_pack(),
Self::Array((kind, _)) => kind.has_pack(),
_ => false,
}
}
Expand Down
16 changes: 4 additions & 12 deletions crates/libs/metadata/src/signature.rs
Expand Up @@ -18,20 +18,12 @@ impl Signature {
self.pointers == 0 && self.kind.is_udt()
}

pub fn has_explicit(&self) -> bool {
self.pointers == 0 && self.kind.has_explicit()
pub fn has_union(&self) -> bool {
self.pointers == 0 && self.kind.has_union()
}

pub fn is_packed(&self) -> bool {
if self.pointers > 0 {
return false;
}

match &self.kind {
ElementType::TypeDef(def) => def.is_packed(),
ElementType::Array((signature, _)) => signature.is_packed(),
_ => false,
}
pub fn has_pack(&self) -> bool {
self.pointers == 0 && self.kind.has_pack()
}

pub fn is_callback(&self) -> bool {
Expand Down
74 changes: 55 additions & 19 deletions crates/libs/metadata/src/tables/type_def.rs
Expand Up @@ -141,18 +141,6 @@ impl TypeDef {
result
}

pub fn is_packed(&self) -> bool {
if self.kind() != TypeKind::Struct {
return false;
}

if self.class_layout().is_some() {
return true;
}

self.fields().any(|field| field.signature(Some(self)).is_packed())
}

pub fn size(&self) -> usize {
if self.kind() == TypeKind::Struct {
self.fields().fold(0, |sum, field| sum + field.signature(Some(self)).size())
Expand Down Expand Up @@ -237,16 +225,64 @@ impl TypeDef {
self.row.u32(0) & 0b1_0000 != 0
}

pub fn has_explicit(&self) -> bool {
if self.kind() != TypeKind::Struct {
return false;
pub fn has_union(&self) -> bool {
fn has_union(def: &TypeDef) -> bool {
if def.kind() != TypeKind::Struct {
return false;
}

if def.is_union() {
true
} else {
def.fields().any(|f| f.signature(Some(def)).has_union())
}
}

if self.is_union() {
true
} else {
self.fields().any(|f| f.signature(Some(self)).has_explicit())
if has_union(self) {
return true;
}

if let Some(entry) = TypeReader::get().get_type_entry(self.type_name()) {
for def in entry {
if let ElementType::TypeDef(def) = def {
if has_union(def) {
return true;
}
}
}
}

false
}

pub fn has_pack(&self) -> bool {
fn has_pack(def: &TypeDef) -> bool {
if def.kind() != TypeKind::Struct {
return false;
}

if def.class_layout().is_some() {
true
} else {
def.fields().any(|f| f.signature(Some(def)).has_pack())
}
}

if has_pack(self) {
return true;
}

if let Some(entry) = TypeReader::get().get_type_entry(self.type_name()) {
for def in entry {
if let ElementType::TypeDef(def) = def {
if has_pack(def) {
return true;
}
}
}
}

false
}

pub fn type_signature(&self) -> String {
Expand Down

0 comments on commit 4bd7251

Please sign in to comment.