let mut module_iter = module.rsplitn(2, "::");
module_iter.next().unwrap();
let module = module_iter.next().unwrap();
- let imports = ImportResolver::new(module.splitn(2, "::").next().unwrap(), &$crate_types.lib_ast.dependencies,
+ let imports = ImportResolver::new(module.splitn(2, "::").next().unwrap(), &$crate_types.lib_ast,
module, &$crate_types.lib_ast.modules.get(module).unwrap().items);
TypeResolver::new(module, imports, $crate_types)
} }
// the Rust type and a flag to indicate whether deallocation needs to
// happen) as well as provide an Option<>al function pointer which is
// called when the trait method is called which allows updating on the fly.
- write!(w, "\tpub {}: ", m.sig.ident).unwrap();
- generated_fields.push((format!("{}", m.sig.ident), None, None));
+ write!(w, "\tpub {}: core::cell::UnsafeCell<", m.sig.ident).unwrap();
+ generated_fields.push((format!("{}", m.sig.ident), Some(("Clone::clone(unsafe { &*core::cell::UnsafeCell::get(".to_owned(), ")}).into()")), None));
types.write_c_type(w, &*r.elem, Some(&meth_gen_types), false);
- writeln!(w, ",").unwrap();
+ writeln!(w, ">,").unwrap();
writeln!(w, "\t/// Fill in the {} field as a reference to it will be given to Rust after this returns", m.sig.ident).unwrap();
writeln!(w, "\t/// Note that this takes a pointer to this object, not the this_ptr like other methods do").unwrap();
writeln!(w, "\t/// This function pointer may be NULL if {} is filled in when this object is created and never needs updating.", m.sig.ident).unwrap();
let is_clonable = types.is_clonable(s);
writeln!(w, "\tpub {}: crate::{},", i, s).unwrap();
(format!("{}", i), if !is_clonable {
- Some(format!("crate::{}_clone_fields", s))
+ Some((format!("crate::{}_clone_fields(", s), ")"))
} else { None }, None)
});
}
writeln!(w, "\t\t\t(f)(&self{});", $impl_accessor).unwrap();
write!(w, "\t\t}}\n\t\t").unwrap();
$type_resolver.write_from_c_conversion_to_ref_prefix(w, &*r.elem, Some(&meth_gen_types));
- write!(w, "self{}.{}", $impl_accessor, m.sig.ident).unwrap();
+ write!(w, "unsafe {{ &*self{}.{}.get() }}", $impl_accessor, m.sig.ident).unwrap();
$type_resolver.write_from_c_conversion_to_ref_suffix(w, &*r.elem, Some(&meth_gen_types));
writeln!(w, "\n\t}}").unwrap();
continue;
writeln!(w, "\t{} {{", trait_name).unwrap();
writeln!(w, "\t\tthis_arg: orig.this_arg,").unwrap();
for (field, clone_fn, _) in generated_fields.iter() {
- if let Some(f) = clone_fn {
+ if let Some((pfx, sfx)) = clone_fn {
// If the field isn't clonable, blindly assume its a trait and hope for the best.
- writeln!(w, "\t\t{}: {}(&orig.{}),", field, f, field).unwrap();
+ writeln!(w, "\t\t{}: {}&orig.{}{},", field, pfx, field, sfx).unwrap();
} else {
writeln!(w, "\t\t{}: Clone::clone(&orig.{}),", field, field).unwrap();
}
(s, i, generic_args) => {
if let Some(supertrait) = types.crate_types.traits.get(s) {
let resolver = get_module_type_resolver!(s, types.crate_libs, types.crate_types);
-
- // Blindly assume that the same imports where `supertrait` is defined are also
- // imported here. This will almost certainly break at some point, but it should be
- // a compilation failure when it does so.
- write!(w, "impl").unwrap();
- maybe_write_lifetime_generics(w, &supertrait.generics, types);
- write!(w, " {}", s).unwrap();
- maybe_write_generics(w, &supertrait.generics, generic_args, types, false);
- writeln!(w, " for {} {{", trait_name).unwrap();
-
- impl_trait_for_c!(supertrait, format!(".{}", i), &resolver, generic_args);
- writeln!(w, "}}").unwrap();
+ macro_rules! impl_supertrait {
+ ($s: expr, $supertrait: expr, $i: expr, $generic_args: expr) => {
+ let resolver = get_module_type_resolver!($s, types.crate_libs, types.crate_types);
+
+ // Blindly assume that the same imports where `supertrait` is defined are also
+ // imported here. This will almost certainly break at some point, but it should be
+ // a compilation failure when it does so.
+ write!(w, "impl").unwrap();
+ maybe_write_lifetime_generics(w, &$supertrait.generics, types);
+ write!(w, " {}", $s).unwrap();
+ maybe_write_generics(w, &$supertrait.generics, $generic_args, types, false);
+ writeln!(w, " for {} {{", trait_name).unwrap();
+
+ impl_trait_for_c!($supertrait, format!(".{}", $i), &resolver, $generic_args);
+ writeln!(w, "}}").unwrap();
+ }
+ }
+ impl_supertrait!(s, supertrait, i, generic_args);
+ walk_supertraits!(supertrait, Some(&resolver), (
+ (s, supertrait_i, generic_args) => {
+ if let Some(supertrait) = types.crate_types.traits.get(s) {
+ impl_supertrait!(s, supertrait, format!("{}.{}", i, supertrait_i), generic_args);
+ }
+ }
+ ) );
} else {
do_write_impl_trait(w, s, i, &trait_name);
}
writeln!(w, "#[allow(unused)]").unwrap();
writeln!(w, "/// Used only if an object of this type is returned as a trait impl by a method").unwrap();
writeln!(w, "pub(crate) extern \"C\" fn {}_free_void(this_ptr: *mut c_void) {{", struct_name).unwrap();
- writeln!(w, "\tunsafe {{ let _ = Box::from_raw(this_ptr as *mut native{}); }}\n}}", struct_name).unwrap();
+ writeln!(w, "\tlet _ = unsafe {{ Box::from_raw(this_ptr as *mut native{}) }};\n}}", struct_name).unwrap();
writeln!(w, "#[allow(unused)]").unwrap();
writeln!(w, "impl {} {{", struct_name).unwrap();
writeln!(w, "\tpub(crate) fn get_native_ref(&self) -> &'static native{} {{", struct_name).unwrap();
write!(w, "\t}}").unwrap();
},
syn::Fields::Unnamed(fields) => {
- assert!(s.generics.lt_token.is_none());
+ assert!(!s.generics.params.iter()
+ .any(|gen| if let syn::GenericParam::Lifetime(_) = gen { false } else { true }));
writeln!(w, "{} (", types.maybe_resolve_ident(&s.ident).unwrap()).unwrap();
for (idx, field) in fields.unnamed.iter().enumerate() {
write!(w, "\t\t").unwrap();
if let syn::Type::Reference(r) = &**rtype {
write!(w, "\n\t\t{}{}: ", $indent, $m.sig.ident).unwrap();
types.write_empty_rust_val(Some(&gen_types), w, &*r.elem);
- writeln!(w, ",\n{}\t\tset_{}: Some({}_{}_set_{}),", $indent, $m.sig.ident, ident, $trait.ident, $m.sig.ident).unwrap();
+ writeln!(w, ".into(),\n{}\t\tset_{}: Some({}_{}_set_{}),", $indent, $m.sig.ident, ident, $trait.ident, $m.sig.ident).unwrap();
printed = true;
}
}
("core::fmt::Debug", _, _) => {},
(s, t, _) => {
if let Some(supertrait_obj) = types.crate_types.traits.get(s) {
- writeln!(w, "\t\t{}: crate::{} {{", t, s).unwrap();
- writeln!(w, "\t\t\tthis_arg: unsafe {{ ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void }},").unwrap();
- writeln!(w, "\t\t\tfree: None,").unwrap();
- for item in supertrait_obj.items.iter() {
- match item {
- syn::TraitItem::Method(m) => {
- write_meth!(m, supertrait_obj, "\t");
+ macro_rules! write_impl_fields {
+ ($s: expr, $supertrait_obj: expr, $t: expr, $pfx: expr, $resolver: expr) => {
+ writeln!(w, "{}\t{}: crate::{} {{", $pfx, $t, $s).unwrap();
+ writeln!(w, "{}\t\tthis_arg: unsafe {{ ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void }},", $pfx).unwrap();
+ writeln!(w, "{}\t\tfree: None,", $pfx).unwrap();
+ for item in $supertrait_obj.items.iter() {
+ match item {
+ syn::TraitItem::Method(m) => {
+ write_meth!(m, $supertrait_obj, $pfx);
+ },
+ _ => {},
+ }
+ }
+ walk_supertraits!($supertrait_obj, Some(&$resolver), (
+ ("Clone", _, _) => {
+ writeln!(w, "{}\tcloned: Some({}_{}_cloned),", $pfx, $supertrait_obj.ident, ident).unwrap();
},
- _ => {},
+ (_, _, _) => {}
+ ) );
}
}
+ write_impl_fields!(s, supertrait_obj, t, "\t", types);
+
+ let resolver = get_module_type_resolver!(s, types.crate_libs, types.crate_types);
+ walk_supertraits!(supertrait_obj, Some(&resolver), (
+ (s, t, _) => {
+ if let Some(supertrait_obj) = types.crate_types.traits.get(s) {
+ write_impl_fields!(s, supertrait_obj, t, "\t\t", resolver);
+ write!(w, "\t\t\t}},\n").unwrap();
+ }
+ }
+ ) );
write!(w, "\t\t}},\n").unwrap();
} else {
write_trait_impl_field_assign(w, s, ident);
writeln!(w, "\t// This is a bit race-y in the general case, but for our specific use-cases today, we're safe").unwrap();
writeln!(w, "\t// Specifically, we must ensure that the first time we're called it can never be in parallel").unwrap();
write!(w, "\tif ").unwrap();
- $types.write_empty_rust_val_check(Some(&meth_gen_types), w, &*r.elem, &format!("trait_self_arg.{}", $m.sig.ident));
+ $types.write_empty_rust_val_check(Some(&meth_gen_types), w, &*r.elem, &format!("unsafe {{ &*trait_self_arg.{}.get() }}", $m.sig.ident));
writeln!(w, " {{").unwrap();
- writeln!(w, "\t\tunsafe {{ &mut *(trait_self_arg as *const {} as *mut {}) }}.{} = {}_{}_{}(trait_self_arg.this_arg);", $trait.ident, $trait.ident, $m.sig.ident, ident, $trait.ident, $m.sig.ident).unwrap();
+ writeln!(w, "\t\t*unsafe {{ &mut *(&*(trait_self_arg as *const {})).{}.get() }} = {}_{}_{}(trait_self_arg.this_arg).into();", $trait.ident, $m.sig.ident, ident, $trait.ident, $m.sig.ident).unwrap();
writeln!(w, "\t}}").unwrap();
writeln!(w, "}}").unwrap();
}
writeln!(w, " {{ true }} else {{ false }}\n}}").unwrap();
} else if path_matches_nongeneric(&trait_path.1, &["core", "hash", "Hash"]) {
- writeln!(w, "/// Checks if two {}s contain equal inner contents.", ident).unwrap();
+ writeln!(w, "/// Generates a non-cryptographic 64-bit hash of the {}.", ident).unwrap();
write!(w, "#[no_mangle]\npub extern \"C\" fn {}_hash(o: &{}) -> u64 {{\n", ident, ident).unwrap();
if types.c_type_has_inner_from_path(&resolved_path) {
write!(w, "\tif o.inner.is_null() {{ return 0; }}\n").unwrap();
ExportStatus::NoExport|ExportStatus::TestOnly => continue,
ExportStatus::NotImplementable => panic!("(C-not implementable) must only appear on traits"),
}
+ if m.sig.asyncness.is_some() { continue; }
let mut meth_gen_types = gen_types.push_ctx();
assert!(meth_gen_types.learn_generics(&m.sig.generics, types));
if m.defaultness.is_some() { unimplemented!(); }
let alias_resolver_override;
let alias_resolver = if alias_module != types.module_path {
- alias_resolver_override = ImportResolver::new(types.types.crate_name, &types.crate_types.lib_ast.dependencies,
+ alias_resolver_override = ImportResolver::new(types.types.crate_name, &types.crate_types.lib_ast,
alias_module, &types.crate_types.lib_ast.modules.get(alias_module).unwrap().items);
&alias_resolver_override
} else { &types.types };
write!(w, ")").unwrap();
}
}
- if var.discriminant.is_some() { unimplemented!(); }
write!(&mut constr, ") -> {} {{\n\t{}::{}", e.ident, e.ident, var.ident).unwrap();
if let syn::Fields::Named(fields) = &var.fields {
writeln!(&mut constr, " {{").unwrap();
};
if $ref || new_var {
if $ref {
- write!(w, "let mut {}_nonref = (*{}).clone();\n\t\t\t\t", $field_ident, $field_ident).unwrap();
+ write!(w, "let mut {}_nonref = Clone::clone({});\n\t\t\t\t", $field_ident, $field_ident).unwrap();
if new_var {
let nonref_ident = format_ident!("{}_nonref", $field_ident);
if $to_c {
use_items.push(item);
}
}
- let import_resolver = ImportResolver::from_borrowed_items(mod_path.splitn(2, "::").next().unwrap(), &libast.dependencies, mod_path, &use_items);
+ let import_resolver = ImportResolver::from_borrowed_items(mod_path.splitn(2, "::").next().unwrap(), libast, mod_path, &use_items);
let mut types = TypeResolver::new(mod_path, import_resolver, crate_types);
writeln!(w, "mod {} {{\n{}", module.ident, DEFAULT_IMPORTS).unwrap();
eprintln!("Converting {} entries...", module);
- let import_resolver = ImportResolver::new(orig_crate, &libast.dependencies, module, items);
+ let import_resolver = ImportResolver::new(orig_crate, libast, module, items);
let mut type_resolver = TypeResolver::new(module, import_resolver, crate_types);
for item in items.iter() {
let ASTModule { ref attrs, ref items, .. } = astmod;
assert_eq!(export_status(&attrs), ExportStatus::Export);
- let import_resolver = ImportResolver::new(orig_crate, &ast_storage.dependencies, module, items);
+ let import_resolver = ImportResolver::new(orig_crate, ast_storage, module, items);
let mut types = TypeResolver::new(module, import_resolver, crate_types);
for item in items.iter() {
}
fn walk_private_mod<'a>(ast_storage: &'a FullLibraryAST, orig_crate: &str, module: String, items: &'a syn::ItemMod, crate_types: &mut CrateTypes<'a>) {
- let import_resolver = ImportResolver::new(orig_crate, &ast_storage.dependencies, &module, &items.content.as_ref().unwrap().1);
+ let import_resolver = ImportResolver::new(orig_crate, ast_storage, &module, &items.content.as_ref().unwrap().1);
for item in items.content.as_ref().unwrap().1.iter() {
match item {
syn::Item::Mod(m) => walk_private_mod(ast_storage, orig_crate, format!("{}::{}", module, m.ident), m, crate_types),
let ASTModule { ref attrs, ref items, submods: _ } = astmod;
assert_eq!(export_status(&attrs), ExportStatus::Export);
let orig_crate = module.splitn(2, "::").next().unwrap();
- let import_resolver = ImportResolver::new(orig_crate, &ast_storage.dependencies, module, items);
+ let import_resolver = ImportResolver::new(orig_crate, ast_storage, module, items);
for item in items.iter() {
match item {