Skip to content

Commit

Permalink
introduce Opaque and out-pointers in IR
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic committed Sep 11, 2022
1 parent a669b1d commit 6bf3c1f
Show file tree
Hide file tree
Showing 16 changed files with 763 additions and 495 deletions.
3 changes: 2 additions & 1 deletion crypto/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ ffi::ffi_item! {
IntoSchema,
FfiType,
)]
#[repr(transparent)]
// TODO: Make transparent
//#[repr(transparent)]
#[display(fmt = "{}", "hex::encode(_0)")]
#[debug(fmt = "{{ Hash({}) }}", "hex::encode(_0)")]
pub struct Hash([u8; Self::LENGTH]);
Expand Down
322 changes: 162 additions & 160 deletions ffi/derive/src/convert.rs

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions ffi/derive/src/ffi_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ fn gen_signature(ffi_fn_name: &Ident, fn_descriptor: &FnDescriptor) -> TokenStre

fn gen_input_arg(arg: &Arg) -> TokenStream {
let arg_name = arg.name();
let arg_type = arg.ffi_type_resolved();
let arg_type = arg.ffi_type_resolved(true);

quote! { #arg_name: #arg_type }
}

fn gen_out_ptr_arg(arg: &Arg) -> TokenStream {
let arg_name = arg.name();
let arg_type = arg.ffi_type_resolved();
let arg_type = arg.src_type_resolved(true);

quote! { #arg_name: <#arg_type as iroha_ffi::Output>::OutPtr }
quote! { #arg_name: <#arg_type as iroha_ffi::FfiOutPtr<'__iroha_ffi_itm>>::OutPtr }
}

fn gen_body(fn_descriptor: &FnDescriptor) -> syn::Block {
Expand Down Expand Up @@ -189,11 +189,12 @@ fn gen_method_call_stmt(fn_descriptor: &FnDescriptor) -> TokenStream {
fn gen_output_assignment_stmts(fn_descriptor: &FnDescriptor) -> TokenStream {
match &fn_descriptor.output_arg {
Some(output_arg) => {
let (arg_name, arg_type) = (output_arg.name(), output_arg.ffi_type_resolved());
let (arg_name, arg_type) = (output_arg.name(), output_arg.src_type_resolved(true));
let output_arg_conversion = crate::util::gen_arg_src_to_ffi(output_arg, true);

quote! {
#output_arg_conversion
iroha_ffi::OutPtrOf::<#arg_type>::write(__out_ptr, #arg_name)?;
<<#arg_type as iroha_ffi::FfiOutPtr>::OutPtr as iroha_ffi::OutPtrOf<_>>::write(__out_ptr, #arg_name)?;
}
}
None => quote! {},
Expand Down
27 changes: 12 additions & 15 deletions ffi/derive/src/impl_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,30 @@ impl Arg {
pub fn src_type(&self) -> &Type {
&self.type_
}
pub fn src_type_resolved(&self) -> Type {
resolve_src_type(self.self_ty.as_ref(), self.type_.clone())
pub fn src_type_resolved(&self, inject_lifetime: bool) -> Type {
resolve_type(self.self_ty.as_ref(), self.type_.clone(), inject_lifetime)
}
pub fn ffi_type_resolved(&self) -> Type {
pub fn ffi_type_resolved(&self, inject_lifetime: bool) -> Type {
let lifetime = quote!('__iroha_ffi_itm);
let mut arg_type = self.type_.clone();

ImplTraitResolver.visit_type_mut(&mut arg_type);
if let Some(self_ty) = self.self_ty.as_ref() {
SelfResolver::new(self_ty).visit_type_mut(&mut arg_type);
}

if let Some(result_type) = unwrap_result_type(&arg_type) {
arg_type = result_type.clone();
}

LifetimeResolver.visit_type_mut(&mut arg_type);
let arg_type = resolve_type(self.self_ty.as_ref(), self.type_.clone(), inject_lifetime);
parse_quote! {<#arg_type as iroha_ffi::FfiType<#lifetime>>::ReprC}
}
}

fn resolve_src_type(self_type: Option<&syn::Path>, mut arg_type: Type) -> Type {
fn resolve_type(self_type: Option<&syn::Path>, mut arg_type: Type, inject_lifetime: bool) -> Type {
ImplTraitResolver.visit_type_mut(&mut arg_type);

if let Some(self_ty) = self_type {
SelfResolver::new(self_ty).visit_type_mut(&mut arg_type);
}
if let Some(result_type) = unwrap_result_type(&arg_type) {
arg_type = result_type.clone();
}

if inject_lifetime {
LifetimeResolver.visit_type_mut(&mut arg_type);
}

arg_type
}
Expand Down
6 changes: 3 additions & 3 deletions ffi/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ pub fn ffi_type_derive(input: TokenStream) -> TokenStream {
pub fn ffi_export(attr: TokenStream, item: TokenStream) -> TokenStream {
match parse_macro_input!(item) {
Item::Impl(item) => {
let impl_descriptor = ImplDescriptor::from_impl(&item);
let ffi_fns = impl_descriptor.fns.iter().map(ffi_fn::gen_definition);

if !attr.is_empty() {
abort!(item, "Unknown tokens in the attribute");
}

let impl_descriptor = ImplDescriptor::from_impl(&item);
let ffi_fns = impl_descriptor.fns.iter().map(ffi_fn::gen_definition);

quote! {
#item
#(#ffi_fns)*
Expand Down
2 changes: 1 addition & 1 deletion ffi/derive/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn gen_derived_methods(item: &syn::ItemStruct) -> Vec<FnDescriptor> {
}

pub fn gen_arg_ffi_to_src(arg: &Arg, is_output: bool) -> TokenStream {
let (arg_name, src_type) = (arg.name(), arg.src_type_resolved());
let (arg_name, src_type) = (arg.name(), arg.src_type_resolved(false));
let store_name = gen_store_name(arg_name);

if is_output {
Expand Down
4 changes: 2 additions & 2 deletions ffi/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ macro_rules! def_ffi_fn {
handle_id: $crate::handle::Id,
left_handle_ptr: *const core::ffi::c_void,
right_handle_ptr: *const core::ffi::c_void,
output_ptr: <<u8 as $crate::FfiType>::ReprC as $crate::Output>::OutPtr,
output_ptr: <u8 as $crate::FfiOutPtr>::OutPtr,
) -> $crate::FfiReturn {
$crate::def_ffi_fn!(@catch_unwind {
use core::borrow::Borrow;
Expand Down Expand Up @@ -152,7 +152,7 @@ macro_rules! def_ffi_fn {
handle_id: $crate::handle::Id,
left_handle_ptr: *const core::ffi::c_void,
right_handle_ptr: *const core::ffi::c_void,
output_ptr: <<core::cmp::Ordering as $crate::FfiType>::ReprC as $crate::Output>::OutPtr,
output_ptr: <i8 as $crate::FfiOutPtr>::OutPtr,
) -> $crate::FfiReturn {
$crate::def_ffi_fn!(@catch_unwind {
use core::borrow::Borrow;
Expand Down
Loading

0 comments on commit 6bf3c1f

Please sign in to comment.