ink_codegen/generator/
macros.rs

1// Copyright (C) Use Ink (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/// Returns the "default" ABI for calls.
16///
17/// # Note
18///
19/// The "default" ABI for calls is "ink", unless the ABI is set to "sol"
20/// in the ink! project's manifest file (i.e. `Cargo.toml`).
21macro_rules! default_abi {
22    () => {{
23        if cfg!(ink_abi = "sol") {
24            quote!(::ink::abi::Sol)
25        } else {
26            quote!(::ink::abi::Ink)
27        }
28    }};
29}
30
31/// Calls the given callback function once for each enabled ABI.
32///
33/// # Note
34///
35/// The ABI is passed to the callback function as an argument.
36/// The argument value can be either as an `ink_primitives::abi::Abi` variant,
37/// or tokens for `::ink::abi::Ink` or `::ink::abi::Sol`.
38macro_rules! for_each_abi {
39    ($callback: expr, $ink_abi: expr, $sol_abi: expr) => {{
40        #[cfg(not(ink_abi = "sol"))]
41        $callback($ink_abi);
42
43        #[cfg(any(ink_abi = "sol", ink_abi = "all"))]
44        $callback($sol_abi);
45    }};
46    (@tokens $callback: expr) => {
47        for_each_abi!($callback, quote!(::ink::abi::Ink), quote!(::ink::abi::Sol))
48    };
49    (@type $callback: expr) => {
50        for_each_abi!(
51            $callback,
52            ink_primitives::abi::Abi::Ink,
53            ink_primitives::abi::Abi::Sol
54        )
55    };
56}
57
58/// Generates code for all enabled ABIs by calling the given generator function for each
59/// enabled ABI, and returns a `TokenStream` combining all generated ABI specific code.
60/// with the ABI as an argument.
61///
62/// # Note
63///
64/// The ABI is passed to the generator function as an argument.
65/// The argument value can be either as an `ink_primitives::abi::Abi` variant,
66/// or tokens for `::ink::abi::Ink` or `::ink::abi::Sol`.
67macro_rules! generate_abi_impls {
68    ($generator: expr, $ink_abi: expr, $sol_abi: expr) => {{
69        let mut abi_impls = Vec::new();
70        for_each_abi!(@type |abi| {
71            match abi {
72                ink_primitives::abi::Abi::Ink => {
73                    abi_impls.push($generator($ink_abi));
74                },
75                ink_primitives::abi::Abi::Sol => {
76                    abi_impls.push($generator($sol_abi));
77                },
78            }
79        });
80        quote! {
81            #( #abi_impls )*
82        }
83    }};
84    (@tokens $callback: expr) => {
85        generate_abi_impls!(
86            $callback,
87            quote!(::ink::abi::Ink),
88            quote!(::ink::abi::Sol)
89        )
90    };
91    (@type $callback: expr) => {
92        generate_abi_impls!(
93            $callback,
94            ink_primitives::abi::Abi::Ink,
95            ink_primitives::abi::Abi::Sol
96        )
97    };
98}