add possbility to be applied to non-pub functions

This commit is contained in:
vms 2020-07-19 19:14:38 +03:00
parent be47b96ce8
commit 79a5896015
6 changed files with 23 additions and 26 deletions

View File

@ -24,7 +24,7 @@ fluence-sdk-main = { path = "crates/main", version = "=0.2.0" }
# Print some internal logs by log_utf8_string
debug = ["fluence-sdk-main/debug"]
# Print some internal logs by log_utf8_string
# Enable logger (this will cause log_utf8_string to appear in imports)
logger = ["fluence-sdk-main/logger"]
[workspace]

View File

@ -28,5 +28,5 @@ lazy_static = "1.4.0" # used in doc test
# Print some internal logs by log_utf8_string
debug = []
# Enable logger, this will cause log_utf8_string to appear in imports
# Enable logger (this will cause log_utf8_string to appear in imports)
logger = []

View File

@ -19,8 +19,11 @@ use crate::parsed_type::ParsedType;
use serde::Serialize;
use serde::Deserialize;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Serialize, Deserialize)]
pub struct AstFunctionSignature {
// Option is needed only for skipping serialization/deserialization of syn::ItemFn
#[serde(skip)]
pub visibility: Option<syn::Visibility>,
pub name: String,
pub input_types: Vec<ParsedType>,
// fce supports only one return value now,
@ -45,7 +48,7 @@ pub struct AstRecordItem {
pub original: Option<syn::ItemStruct>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Serialize, Deserialize)]
pub struct AstExternFnItem {
pub link_name: Option<String>,
// only imports are possible here

View File

@ -22,24 +22,24 @@ use syn::Result;
impl ParseMacroInput for syn::ItemFn {
fn parse_macro_input(self) -> Result<FCEAst> {
parse_function(self.sig.clone(), self.vis.clone()).map(|f| {
try_to_ast_signature(self.sig.clone(), self.vis.clone()).map(|signature| {
FCEAst::Function(AstFunctionItem {
signature: f,
signature,
original: Some(self),
})
})
}
}
pub(super) fn parse_function(
function_sig: syn::Signature,
function_vis: syn::Visibility,
pub(super) fn try_to_ast_signature(
signature: syn::Signature,
visibility: syn::Visibility,
) -> Result<fce_ast_types::AstFunctionSignature> {
use crate::parsed_type::ParsedType;
check_func(&function_sig, function_vis)?;
check_function(&signature)?;
let syn::Signature { inputs, output, .. } = function_sig;
let syn::Signature { inputs, output, .. } = signature;
let input_types = inputs
.iter()
@ -49,7 +49,8 @@ pub(super) fn parse_function(
let output_type = ParsedType::from_return_type(&output)?;
let ast_function_item = fce_ast_types::AstFunctionSignature {
name: function_sig.ident.to_string(),
visibility: Some(visibility),
name: signature.ident.to_string(),
input_types,
output_type,
};
@ -58,7 +59,7 @@ pub(super) fn parse_function(
}
/// Check whether the #[fce] macro could be applied to a function.
fn check_func(function_sig: &syn::Signature, function_vis: syn::Visibility) -> Result<()> {
fn check_function(signature: &syn::Signature) -> Result<()> {
use syn::Error;
use syn::spanned::Spanned;
@ -69,7 +70,7 @@ fn check_func(function_sig: &syn::Signature, function_vis: syn::Visibility) -> R
variadic,
generics,
..
} = function_sig;
} = signature;
if let Some(constness) = constness {
return Err(Error::new(
@ -91,7 +92,7 @@ fn check_func(function_sig: &syn::Signature, function_vis: syn::Visibility) -> R
}
if !generics.params.is_empty() || generics.where_clause.is_some() {
return Err(Error::new(
function_sig.span(),
signature.span(),
"FCE export function shouldn't use template parameters",
));
}
@ -103,12 +104,5 @@ fn check_func(function_sig: &syn::Signature, function_vis: syn::Visibility) -> R
}
// TODO: check for a lifetime
match function_vis {
syn::Visibility::Public(_) => Ok(()),
_ => Err(Error::new(
variadic.span(),
"FCE export function should be public",
)),
}
Ok(())
}

View File

@ -117,7 +117,7 @@ fn parse_raw_foreign_item(raw_item: syn::ForeignItem) -> Result<fce_ast_types::A
None => None,
};
let function = super::item_fn::parse_function(function_item.sig, function_item.vis)?;
let function = super::item_fn::try_to_ast_signature(function_item.sig, function_item.vis)?;
let ast_extern_fn_item = fce_ast_types::AstExternFnItem {
link_name,
signature: function,

View File

@ -79,7 +79,7 @@ fn generate_extern_section_items(extern_item: &fce_ast_types::AstExternModItem)
let func = quote! {
#[link_name = #link_name]
pub fn #import_name(#(#raw_arg_names: #raw_arg_types),*) #fn_return_type;
fn #import_name(#(#raw_arg_names: #raw_arg_types),*) #fn_return_type;
};
token_stream.extend(func);
@ -98,7 +98,7 @@ fn generate_wrapper_functions(extern_item: &fce_ast_types::AstExternModItem) ->
for import in &extern_item.imports {
let signature = &import.signature;
let visibility = new_ident!("pub");
let visibility = &signature.visibility;
let func_name = new_ident!(&signature.name);
let return_type = signature.output_type.generate_wrapper_return_type();