diff options
author | Nick Fitzgerald <fitzgen@gmail.com> | 2017-09-29 17:03:44 -0700 |
---|---|---|
committer | Nick Fitzgerald <fitzgen@gmail.com> | 2017-10-02 11:33:26 -0700 |
commit | 3e869f17fb00ef85025450c0017de5ea7e60e188 (patch) | |
tree | 40c79058d5391e7a6dd04bae6bfcdbdbfdf5ed84 | |
parent | 95d97416475666fa8975289deb3b90eb1724cc03 (diff) |
Make a bunch more methods take generic ids
-rw-r--r-- | src/codegen/mod.rs | 12 | ||||
-rw-r--r-- | src/ir/comp.rs | 12 | ||||
-rw-r--r-- | src/ir/context.rs | 13 | ||||
-rw-r--r-- | src/ir/item.rs | 39 | ||||
-rw-r--r-- | src/ir/ty.rs | 5 |
5 files changed, 51 insertions, 30 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 22abad10..acc87692 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -6,6 +6,7 @@ pub mod struct_layout; use self::helpers::attributes; use self::struct_layout::StructLayoutTracker; +use ir::analysis::HasVtable; use ir::annotations::FieldAccessorKind; use ir::comment; use ir::comp::{Base, Bitfield, BitfieldUnit, CompInfo, CompKind, Field, @@ -1546,7 +1547,7 @@ impl CodeGenerator for CompInfo { // NB: We won't include unsized types in our base chain because they // would contribute to our size given the dummy field we insert for // unsized types. - if base_ty.is_unsized(ctx, &base.ty) { + if base_ty.is_unsized(ctx, base.ty) { continue; } @@ -1625,7 +1626,7 @@ impl CodeGenerator for CompInfo { warn!("Opaque type without layout! Expect dragons!"); } } - } else if !is_union && !self.is_unsized(ctx, &item.id()) { + } else if !is_union && !self.is_unsized(ctx, item.id()) { if let Some(padding_field) = layout.and_then(|layout| struct_layout.pad_struct(layout)) { @@ -1649,7 +1650,7 @@ impl CodeGenerator for CompInfo { // // NOTE: This check is conveniently here to avoid the dummy fields we // may add for unused template parameters. - if self.is_unsized(ctx, &item.id()) { + if self.is_unsized(ctx, item.id()) { let has_address = if is_opaque { // Generate the address field if it's an opaque type and // couldn't determine the layout of the blob. @@ -1758,9 +1759,8 @@ impl CodeGenerator for CompInfo { // FIXME when [issue #465](https://github.com/rust-lang-nursery/rust-bindgen/issues/465) ready let too_many_base_vtables = self.base_members() .iter() - .filter(|base| ctx.lookup_item_id_has_vtable(&base.ty)) - .count() > - 1; + .filter(|base| base.ty.has_vtable(ctx)) + .count() > 1; let should_skip_field_offset_checks = is_opaque || too_many_base_vtables; diff --git a/src/ir/comp.rs b/src/ir/comp.rs index 7d3da4c5..5cbcaf09 100644 --- a/src/ir/comp.rs +++ b/src/ir/comp.rs @@ -1,5 +1,6 @@ //! Compound types (unions and structs) in our intermediate representation. +use super::analysis::HasVtable; use super::annotations::Annotations; use super::context::{BindgenContext, ItemId, TypeId}; use super::dot::DotAttributes; @@ -910,12 +911,12 @@ impl CompInfo { } /// Is this compound type unsized? - pub fn is_unsized(&self, ctx: &BindgenContext, itemid: &ItemId) -> bool { - !ctx.lookup_item_id_has_vtable(itemid) && self.fields().is_empty() && + pub fn is_unsized<Id: Into<ItemId>>(&self, ctx: &BindgenContext, id: Id) -> bool { + !ctx.lookup_item_id_has_vtable(id.into()) && self.fields().is_empty() && self.base_members.iter().all(|base| { ctx.resolve_type(base.ty.as_type_id_unchecked()).canonical_type(ctx).is_unsized( ctx, - &base.ty, + base.ty, ) }) } @@ -1354,8 +1355,7 @@ impl CompInfo { ctx: &BindgenContext, item: &Item, ) -> bool { - ctx.lookup_item_id_has_vtable(&item.id()) && - !self.base_members.iter().any(|base| { + item.has_vtable(ctx) && !self.base_members.iter().any(|base| { // NB: Ideally, we could rely in all these types being `comp`, and // life would be beautiful. // @@ -1365,7 +1365,7 @@ impl CompInfo { ctx.resolve_type(base.ty.as_type_id_unchecked()) .canonical_type(ctx) .as_comp() - .map_or(false, |_| ctx.lookup_item_id_has_vtable(&base.ty)) + .map_or(false, |_| base.ty.has_vtable(ctx)) }) } diff --git a/src/ir/context.rs b/src/ir/context.rs index 2aed6872..5c3530a6 100644 --- a/src/ir/context.rs +++ b/src/ir/context.rs @@ -108,7 +108,10 @@ where } } -impl CanDeriveHash for ItemId { +impl<T> CanDeriveHash for T +where + T: Copy + Into<ItemId> +{ fn can_derive_hash(&self, ctx: &BindgenContext) -> bool { ctx.options().derive_hash && ctx.lookup_item_id_can_derive_hash(*self) } @@ -1122,7 +1125,7 @@ impl BindgenContext { } /// Look up whether the item with `id` has vtable or not. - pub fn lookup_item_id_has_vtable(&self, id: &ItemId) -> bool { + pub fn lookup_item_id_has_vtable<Id: Into<ItemId>>(&self, id: Id) -> bool { assert!( self.in_codegen_phase(), "We only compute vtables when we enter codegen" @@ -1130,7 +1133,7 @@ impl BindgenContext { // Look up the computed value for whether the item with `id` has a // vtable or not. - self.have_vtable.as_ref().unwrap().contains(id) + self.have_vtable.as_ref().unwrap().contains(&id.into()) } /// Compute whether the type has a destructor. @@ -1282,8 +1285,8 @@ impl BindgenContext { /// Resolve the given `ItemId` into an `Item`, or `None` if no such item /// exists. - pub fn resolve_item_fallible(&self, item_id: ItemId) -> Option<&Item> { - self.items.get(&item_id) + pub fn resolve_item_fallible<Id: Into<ItemId>>(&self, id: Id) -> Option<&Item> { + self.items.get(&id.into()) } /// Resolve the given `ItemId` into an `Item`. diff --git a/src/ir/item.rs b/src/ir/item.rs index 8a386753..27c08fa0 100644 --- a/src/ir/item.rs +++ b/src/ir/item.rs @@ -128,9 +128,9 @@ pub struct ItemAncestorsIter<'a> { } impl<'a> ItemAncestorsIter<'a> { - fn new(ctx: &'a BindgenContext, item: ItemId) -> Self { + fn new<Id: Into<ItemId>>(ctx: &'a BindgenContext, id: Id) -> Self { ItemAncestorsIter { - item: item, + item: id.into(), ctx: ctx, seen: DebugOnlyItemSet::new(), } @@ -199,8 +199,10 @@ impl AsTemplateParam for ItemKind { } } -// Pure convenience -impl ItemCanonicalName for ItemId { +impl<T> ItemCanonicalName for T +where + T: Copy + Into<ItemId> +{ fn canonical_name(&self, ctx: &BindgenContext) -> String { debug_assert!( ctx.in_codegen_phase(), @@ -210,7 +212,10 @@ impl ItemCanonicalName for ItemId { } } -impl ItemCanonicalPath for ItemId { +impl<T> ItemCanonicalPath for T + where + T: Copy + Into<ItemId> +{ fn namespace_aware_canonical_path( &self, ctx: &BindgenContext, @@ -231,7 +236,10 @@ impl ItemCanonicalPath for ItemId { } } -impl ItemAncestors for ItemId { +impl<T> ItemAncestors for T +where + T: Copy + Into<ItemId> +{ fn ancestors<'a>( &self, ctx: &'a BindgenContext, @@ -249,7 +257,10 @@ impl ItemAncestors for Item { } } -impl Trace for ItemId { +impl<Id> Trace for Id +where + Id: Copy + Into<ItemId> +{ type Extra = (); fn trace<T>(&self, ctx: &BindgenContext, tracer: &mut T, extra: &()) @@ -983,15 +994,18 @@ impl IsOpaque for Item { } } -impl HasVtable for ItemId { +impl<T> HasVtable for T +where + T: Copy + Into<ItemId> +{ fn has_vtable(&self, ctx: &BindgenContext) -> bool { - ctx.lookup_item_id_has_vtable(self) + ctx.lookup_item_id_has_vtable(*self) } } impl HasVtable for Item { fn has_vtable(&self, ctx: &BindgenContext) -> bool { - ctx.lookup_item_id_has_vtable(&self.id()) + ctx.lookup_item_id_has_vtable(self.id()) } } @@ -1065,7 +1079,10 @@ impl DotAttributes for Item { } } -impl TemplateParameters for ItemId { +impl<T> TemplateParameters for T +where + T: Copy + Into<ItemId> +{ fn self_template_params( &self, ctx: &BindgenContext, diff --git a/src/ir/ty.rs b/src/ir/ty.rs index 1bb2cce7..d119e9e4 100644 --- a/src/ir/ty.rs +++ b/src/ir/ty.rs @@ -693,12 +693,13 @@ impl Type { /// derive whether we should generate a dummy `_address` field for structs, /// to comply to the C and C++ layouts, that specify that every type needs /// to be addressable. - pub fn is_unsized(&self, ctx: &BindgenContext, itemid: &ItemId) -> bool { + pub fn is_unsized<Id: Into<ItemId>>(&self, ctx: &BindgenContext, id: Id) -> bool { debug_assert!(ctx.in_codegen_phase(), "Not yet"); + let id = id.into(); match self.kind { TypeKind::Void => true, - TypeKind::Comp(ref ci) => ci.is_unsized(ctx, itemid), + TypeKind::Comp(ref ci) => ci.is_unsized(ctx, id), TypeKind::Opaque => self.layout.map_or(true, |l| l.size == 0), TypeKind::Array(inner, size) => { size == 0 || ctx.resolve_type(inner).is_unsized(ctx, &inner.into()) |