Skip to content

Implement Default trait #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use aster;
use ir::annotations::FieldAccessorKind;
use ir::comp::{Base, CompInfo, CompKind, Field, Method, MethodKind};
use ir::context::{BindgenContext, ItemId};
use ir::derive::{CanDeriveCopy, CanDeriveDebug};
use ir::derive::{CanDeriveCopy, CanDeriveDebug, CanDeriveDefault};
use ir::enum_ty::{Enum, EnumVariant, EnumVariantValue};
use ir::function::{Function, FunctionSig};
use ir::int::IntKind;
Expand Down Expand Up @@ -688,10 +688,16 @@ impl<'a> CodeGenerator for Vtable<'a> {
assert_eq!(item.id(), self.item_id);
// For now, generate an empty struct, later we should generate function
// pointers and whatnot.
let mut attributes = vec![attributes::repr("C")];

if ctx.options().derive_default {
attributes.push(attributes::derives(&["Default"]))
}

let vtable = aster::AstBuilder::new()
.item()
.pub_()
.with_attr(attributes::repr("C"))
.with_attrs(attributes)
.struct_(self.canonical_name(ctx))
.build();
result.push(vtable);
Expand Down Expand Up @@ -879,6 +885,7 @@ impl CodeGenerator for CompInfo {

let mut attributes = vec![];
let mut needs_clone_impl = false;
let mut needs_default_impl = false;
if ctx.options().generate_comments {
if let Some(comment) = item.comment() {
attributes.push(attributes::doc(comment));
Expand All @@ -896,6 +903,12 @@ impl CodeGenerator for CompInfo {
derives.push("Debug");
}

if item.can_derive_default(ctx, ()) {
derives.push("Default");
} else {
needs_default_impl = ctx.options().derive_default;
}

if item.can_derive_copy(ctx, ()) &&
!item.annotations().disallow_copy() {
derives.push("Copy");
Expand Down Expand Up @@ -1440,8 +1453,14 @@ impl CodeGenerator for CompInfo {

// NB: We can't use to_rust_ty here since for opaque types this tries to
// use the specialization knowledge to generate a blob field.
let ty_for_impl =
aster::AstBuilder::new().ty().path().id(&canonical_name).build();
let ty_for_impl = aster::AstBuilder::new()
.ty()
.path()
.segment(&canonical_name)
.with_generics(generics.clone())
.build()
.build();

if needs_clone_impl {
let impl_ = quote_item!(ctx.ext_cx(),
impl X {
Expand All @@ -1467,6 +1486,32 @@ impl CodeGenerator for CompInfo {
result.push(clone_impl);
}

if needs_default_impl {
let prefix = ctx.trait_prefix();
let impl_ = quote_item!(ctx.ext_cx(),
impl X {
fn default() -> Self { unsafe { ::$prefix::mem::zeroed() } }
}
);

let impl_ = match impl_.unwrap().node {
ast::ItemKind::Impl(_, _, _, _, _, ref items) => items.clone(),
_ => unreachable!(),
};

let default_impl = aster::AstBuilder::new()
.item()
.impl_()
.trait_()
.id("Default")
.build()
.with_generics(generics.clone())
.with_items(impl_)
.build_ty(ty_for_impl.clone());

result.push(default_impl);
}

if !methods.is_empty() {
let methods = aster::AstBuilder::new()
.item()
Expand Down Expand Up @@ -2582,6 +2627,7 @@ mod utils {

let incomplete_array_decl = quote_item!(ctx.ext_cx(),
#[repr(C)]
#[derive(Default)]
pub struct __IncompleteArrayField<T>(
::$prefix::marker::PhantomData<T>);
)
Expand Down
61 changes: 60 additions & 1 deletion src/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::annotations::Annotations;
use super::context::{BindgenContext, ItemId};
use super::derive::{CanDeriveCopy, CanDeriveDebug};
use super::derive::{CanDeriveCopy, CanDeriveDebug, CanDeriveDefault};
use super::item::Item;
use super::layout::Layout;
use super::ty::Type;
Expand Down Expand Up @@ -171,6 +171,14 @@ impl CanDeriveDebug for Field {
}
}

impl CanDeriveDefault for Field {
type Extra = ();

fn can_derive_default(&self, ctx: &BindgenContext, _: ()) -> bool {
self.ty.can_derive_default(ctx, ())
}
}

impl<'a> CanDeriveCopy<'a> for Field {
type Extra = ();

Expand Down Expand Up @@ -296,6 +304,10 @@ pub struct CompInfo {
/// around the template arguments.
detect_derive_debug_cycle: Cell<bool>,

/// Used to detect if we've run in a can_derive_default cycle while cycling
/// around the template arguments.
detect_derive_default_cycle: Cell<bool>,

/// Used to detect if we've run in a has_destructor cycle while cycling
/// around the template arguments.
detect_has_destructor_cycle: Cell<bool>,
Expand Down Expand Up @@ -326,6 +338,7 @@ impl CompInfo {
is_anonymous: false,
found_unknown_attr: false,
detect_derive_debug_cycle: Cell::new(false),
detect_derive_default_cycle: Cell::new(false),
detect_has_destructor_cycle: Cell::new(false),
is_forward_declaration: false,
}
Expand Down Expand Up @@ -952,6 +965,52 @@ impl CanDeriveDebug for CompInfo {
}
}

impl CanDeriveDefault for CompInfo {
type Extra = Option<Layout>;

fn can_derive_default(&self,
ctx: &BindgenContext,
layout: Option<Layout>)
-> bool {
// We can reach here recursively via template parameters of a member,
// for example.
if self.detect_derive_default_cycle.get() {
warn!("Derive default cycle detected!");
return true;
}

if self.kind == CompKind::Union {
if ctx.options().unstable_rust {
return false;
}

return layout.unwrap_or_else(Layout::zero)
.opaque()
.can_derive_debug(ctx, ());
}

self.detect_derive_default_cycle.set(true);

let can_derive_default = !self.has_vtable(ctx) &&
!self.needs_explicit_vtable(ctx) &&
self.base_members
.iter()
.all(|base| base.ty.can_derive_default(ctx, ())) &&
self.template_args
.iter()
.all(|id| id.can_derive_default(ctx, ())) &&
self.fields
.iter()
.all(|f| f.can_derive_default(ctx, ())) &&
self.ref_template
.map_or(true, |id| id.can_derive_default(ctx, ()));

self.detect_derive_default_cycle.set(false);

can_derive_default
}
}

impl<'a> CanDeriveCopy<'a> for CompInfo {
type Extra = (&'a Item, Option<Layout>);

Expand Down
10 changes: 9 additions & 1 deletion src/ir/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Common context that is passed around during parsing and codegen.

use super::derive::{CanDeriveCopy, CanDeriveDebug};
use super::derive::{CanDeriveCopy, CanDeriveDebug, CanDeriveDefault};
use super::int::IntKind;
use super::item::{Item, ItemCanonicalPath};
use super::item_kind::ItemKind;
Expand Down Expand Up @@ -42,6 +42,14 @@ impl CanDeriveDebug for ItemId {
}
}

impl CanDeriveDefault for ItemId {
type Extra = ();

fn can_derive_default(&self, ctx: &BindgenContext, _: ()) -> bool {
ctx.resolve_item(*self).can_derive_default(ctx, ())
}
}

impl<'a> CanDeriveCopy<'a> for ItemId {
type Extra = ();

Expand Down
21 changes: 21 additions & 0 deletions src/ir/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,24 @@ pub trait CanDeriveCopy<'a> {
extra: Self::Extra)
-> bool;
}

/// A trait that encapsulates the logic for whether or not we can derive `Default`
/// for a given thing.
///
/// This should ideally be a no-op that just returns `true`, but instead needs
/// to be a recursive method that checks whether all the proper members can
/// derive default or not, because of the limit rust has on 32 items as max in the
/// array.
pub trait CanDeriveDefault {
/// Implementations can define this type to get access to any extra
/// information required to determine whether they can derive `Default`. If
/// extra information is unneeded, then this should simply be the unit type.
type Extra;

/// Return `true` if `Default` can be derived for this thing, `false`
/// otherwise.
fn can_derive_default(&self,
ctx: &BindgenContext,
extra: Self::Extra)
-> bool;
}
22 changes: 21 additions & 1 deletion src/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::annotations::Annotations;
use super::context::{BindgenContext, ItemId};
use super::derive::{CanDeriveCopy, CanDeriveDebug};
use super::derive::{CanDeriveCopy, CanDeriveDebug, CanDeriveDefault};
use super::function::Function;
use super::item_kind::ItemKind;
use super::module::Module;
Expand Down Expand Up @@ -235,6 +235,26 @@ impl CanDeriveDebug for Item {
}
}

impl CanDeriveDefault for Item {
type Extra = ();

fn can_derive_default(&self, ctx: &BindgenContext, _: ()) -> bool {
ctx.options().derive_default &&
match self.kind {
ItemKind::Type(ref ty) => {
if self.is_opaque(ctx) {
ty.layout(ctx)
.map_or(false,
|l| l.opaque().can_derive_default(ctx, ()))
} else {
ty.can_derive_default(ctx, ())
}
}
_ => false,
}
}
}

impl<'a> CanDeriveCopy<'a> for Item {
type Extra = ();

Expand Down
11 changes: 10 additions & 1 deletion src/ir/layout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Intermediate representation for the physical layout of some type.

use super::context::BindgenContext;
use super::derive::{CanDeriveCopy, CanDeriveDebug};
use super::derive::{CanDeriveCopy, CanDeriveDebug, CanDeriveDefault};
use super::ty::RUST_DERIVE_IN_ARRAY_LIMIT;
use std::cmp;

Expand Down Expand Up @@ -79,6 +79,15 @@ impl CanDeriveDebug for Opaque {
}
}

impl CanDeriveDefault for Opaque {
type Extra = ();

fn can_derive_default(&self, _: &BindgenContext, _: ()) -> bool {
self.array_size()
.map_or(false, |size| size <= RUST_DERIVE_IN_ARRAY_LIMIT)
}
}

impl<'a> CanDeriveCopy<'a> for Opaque {
type Extra = ();

Expand Down
35 changes: 34 additions & 1 deletion src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::comp::CompInfo;
use super::context::{BindgenContext, ItemId};
use super::derive::{CanDeriveCopy, CanDeriveDebug};
use super::derive::{CanDeriveCopy, CanDeriveDebug, CanDeriveDefault};
use super::enum_ty::Enum;
use super::function::FunctionSig;
use super::int::IntKind;
Expand Down Expand Up @@ -412,6 +412,39 @@ impl CanDeriveDebug for Type {
}
}

impl CanDeriveDefault for Type {
type Extra = ();

fn can_derive_default(&self, ctx: &BindgenContext, _: ()) -> bool {
match self.kind {
TypeKind::Array(t, len) => {
len <= RUST_DERIVE_IN_ARRAY_LIMIT &&
t.can_derive_default(ctx, ())
}
TypeKind::ResolvedTypeRef(t) |
TypeKind::TemplateAlias(t, _) |
TypeKind::Alias(t) => t.can_derive_default(ctx, ()),
TypeKind::Comp(ref info) => {
info.can_derive_default(ctx, self.layout(ctx))
}
TypeKind::Void |
TypeKind::Named |
TypeKind::TemplateRef(..) |
TypeKind::Reference(..) |
TypeKind::NullPtr |
TypeKind::Pointer(..) |
TypeKind::BlockPointer |
TypeKind::ObjCInterface(..) |
TypeKind::Enum(..) => false,
TypeKind::Function(..) |
TypeKind::Int(..) |
TypeKind::Float(..) |
TypeKind::Complex(..) => true,
TypeKind::UnresolvedTypeRef(..) => unreachable!(),
}
}
}

impl<'a> CanDeriveCopy<'a> for Type {
type Extra = &'a Item;

Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ impl Builder {
self
}

/// Set whether `Default` should be derived by default.
pub fn derive_default(mut self, doit: bool) -> Self {
self.options.derive_default = doit;
self
}

/// Emit Clang AST.
pub fn emit_clang_ast(mut self) -> Builder {
self.options.emit_ast = true;
Expand Down Expand Up @@ -496,6 +502,10 @@ pub struct BindgenOptions {
/// and types.
pub derive_debug: bool,

/// True if we shold derive Default trait implementations for C/C++ structures
/// and types.
pub derive_default: bool,

/// True if we can use unstable Rust code in the bindings, false if we
/// cannot.
pub unstable_rust: bool,
Expand Down Expand Up @@ -581,6 +591,7 @@ impl Default for BindgenOptions {
emit_ast: false,
emit_ir: false,
derive_debug: true,
derive_default: false,
enable_cxx_namespaces: false,
disable_name_namespacing: false,
unstable_rust: true,
Expand Down
Loading