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
/*
 * 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.
 */

#[macro_export]
/// Crates new syn::Ident with the given string and new call span
macro_rules! new_ident {
    ($string: expr) => {
        syn::Ident::new(&$string, proc_macro2::Span::call_site());
    };
}

#[macro_export]
macro_rules! prepare_global_data {
    ($fce_type: ident, $self: ident, $name: expr, $data: ident, $data_size: ident, $global_static_name: ident, $section_name: ident) => {
        // TODO: change serialization protocol
        let fce_type = fce_ast_types::FCEAst::$fce_type($self.clone());
        let $data = serde_json::to_vec(&fce_type).unwrap();
        let $data_size = $data.len();
        let $data = syn::LitByteStr::new(&$data, proc_macro2::Span::call_site());

        let $global_static_name = crate::new_ident!(format!(
            "{}{}",
            crate::token_stream_generator::GENERATED_GLOBAL_PREFIX,
            $name.replace(".", "_"),
        ));
        let $section_name = format!(
            "{}{}",
            crate::token_stream_generator::GENERATED_SECTION_PREFIX,
            $name.replace(".", "_"),
        );
    };
}

/// Calculate record size in an internal serialized view.
pub fn get_record_size<'a>(
    fields: impl Iterator<Item = &'a crate::parsed_type::ParsedType>,
) -> usize {
    use crate::parsed_type::ParsedType;

    let mut size = 0;

    for field in fields {
        let params_count = match field {
            ParsedType::Vector(_) | ParsedType::Utf8String => 2,
            _ => 1,
        };

        // internally record is being serialized to a vector of u64.
        size += std::mem::size_of::<u64>() * params_count;
    }

    size
}