contract.rs 4.2 KB
Newer Older
1 2
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
//
3 4 5
// 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
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9 10 11 12 13
// 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.
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

use derive_more::From;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;

pub use crate::{
    codegen::{
        abi::GenerateAbi,
        cross_calling::{
            CrossCalling,
            CrossCallingConflictCfg,
        },
        dispatch::Dispatch,
        env_types::EnvTypes,
        events::{
            EventHelpers,
            EventImports,
            EventStructs,
        },
        storage::Storage,
        testable::TestWrapper,
        GenerateCode,
        GenerateCodeUsing,
    },
    ir::Contract,
};

/// Generates code for the entirety of the ink! contract.
#[derive(From)]
pub struct ContractModule<'a> {
    /// The contract to generate code for.
    contract: &'a Contract,
}

impl<'a> GenerateCodeUsing for ContractModule<'a> {
    fn contract(&self) -> &Contract {
        self.contract
    }
}

impl GenerateCode for ContractModule<'_> {
    /// Generates ink! contract code.
    fn generate_code(&self) -> TokenStream2 {
        let ident = &self.contract.ident;
        let storage_ident = &self.contract.storage.ident;

        let conflic_depedency_cfg = self.generate_code_using::<CrossCallingConflictCfg>();
        let env_types = self.generate_code_using::<EnvTypes>();
        let storage = self.generate_code_using::<Storage>();
        let dispatch = self.generate_code_using::<Dispatch>();
        let generate_abi = self.generate_code_using::<GenerateAbi>();
        let event_helpers = self.generate_code_using::<EventHelpers>();
        let event_structs = self.generate_code_using::<EventStructs>();
        let event_imports = self.generate_code_using::<EventImports>();
        let test_wrapper = self.generate_code_using::<TestWrapper>();
        let cross_calling = self.generate_code_using::<CrossCalling>();
        let non_ink_items = &self.contract.non_ink_items;

72 73 74 75 76 77 78 79 80
        let test_event_alias = if !self.contract.events.is_empty() {
            quote! {
                #[cfg(all(test, feature = "test-env"))]
                pub type Event = self::__ink_private::Event;
            }
        } else {
            quote! {}
        };

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        quote! {
            mod #ident {
                #env_types

                // Private struct and other type definitions.
                mod __ink_private {
                    use super::*;
                    #event_imports

                    #storage
                    #event_helpers
                    #dispatch
                    #generate_abi
                    #test_wrapper
                    #cross_calling
                }

                #[cfg(all(test, feature = "test-env"))]
99
                pub type #storage_ident = self::__ink_private::TestableStorage;
100 101 102

                #[cfg(not(all(test, feature = "test-env")))]
                #conflic_depedency_cfg
103
                pub type #storage_ident = self::__ink_private::Storage;
104 105 106 107

                #[cfg(feature = "ink-as-dependency")]
                pub type #storage_ident = self::__ink_private::StorageAsDependency;

108 109
                #test_event_alias

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
                #event_structs

                #(
                    #non_ink_items
                )*
            }

            // Only re-export if we want to generate the ABI.
            // We should rethink this approach, it isn't a good
            // idea to generate code outside of the scope of the
            // given ink! module.
            #[cfg(feature = "ink-generate-abi")]
            #conflic_depedency_cfg
            pub use crate::#ident::#storage_ident;
        }
    }
}

impl GenerateCode for Contract {
    fn generate_code(&self) -> TokenStream2 {
        ContractModule::from(self).generate_code()
    }
}