1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright 2020 Fluence Labs Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use super::ParsedType;
use crate::new_ident;

use quote::quote;

/// This trait could be used to generate various parts needed to construct epilog of an wrapper of
/// import function.
pub(crate) trait ForeignModEpilogGlueCodeGenerator {
    fn generate_wrapper_return_type(&self) -> proc_macro2::TokenStream;

    fn generate_wrapper_epilog(&self) -> proc_macro2::TokenStream;
}

impl ForeignModEpilogGlueCodeGenerator for Option<ParsedType> {
    fn generate_wrapper_return_type(&self) -> proc_macro2::TokenStream {
        use quote::ToTokens;

        match self {
            Some(ty) => {
                let ty = ty.to_token_stream();
                quote! { -> #ty }
            }
            None => quote!(),
        }
    }

    fn generate_wrapper_epilog(&self) -> proc_macro2::TokenStream {
        match self {
            None => quote!(),
            Some(ParsedType::Boolean) => quote! {
                return result != 0;
            },
            Some(ty) if !ty.is_complex_type() => quote! {
                return result as _;
            },
            Some(ParsedType::Utf8String) => quote! {
                String::from_raw_parts(
                    fluence::internal::get_result_ptr() as _,
                    fluence::internal::get_result_size() as _,
                    fluence::internal::get_result_size() as _
                )
            },
            Some(ParsedType::Vector(ty)) => {
                let generated_deserializer_name = String::from("__fce_generated_vec_deserializer");
                let generated_deserializer_ident = new_ident!(generated_deserializer_name);
                let vector_deserializer = super::vector_utils::generate_vector_deserializer(
                    ty,
                    &generated_deserializer_name,
                );

                quote! {
                    #vector_deserializer
                    #generated_deserializer_ident(
                        fluence::internal::get_result_ptr() as _,
                        fluence::internal::get_result_size() as _,
                    )
                }
            }
            Some(ParsedType::Record(record_name)) => {
                let record_ident = new_ident!(record_name);

                quote! {
                    #record_ident::__fce_generated_deserialize(fluence::internal::get_result_ptr() as _)
                }
            }
            _ => panic!(
                "perhaps new type's been added to ParsedType, and this match became incomplete"
            ),
        }
    }
}