Unverified Commit 1ff12504 authored by Robin Freyler's avatar Robin Freyler
Browse files

[lang] Move code gen into its own module

This will later be useful for specialized test and doc code generation.
parent 6a848890
......@@ -14,6 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with pDSL. If not, see <http://www.gnu.org/licenses/>.
//! Code generation for normal Wasm smart contract builds.
//!
//! Generated code that conflicts with specialized `test` or `doc`
//! code needs to be guarded by `#[cfg(..)]`.
use crate::{
ast,
hir,
......@@ -21,7 +26,7 @@ use crate::{
use proc_macro2::{
Ident,
Span,
TokenStream,
TokenStream as TokenStream2,
};
use quote::{
quote,
......@@ -33,18 +38,16 @@ use syn::{
Token,
};
pub fn codegen(contract: &hir::Contract) -> proc_macro2::TokenStream {
let mut tokens = quote! {};
codegen_for_state(&mut tokens, contract);
codegen_for_messages(&mut tokens, contract);
codegen_for_message_impls(&mut tokens, contract);
codegen_for_method_impls(&mut tokens, contract);
codegen_for_instantiate(&mut tokens, contract);
codegen_for_entry_points(&mut tokens);
tokens
pub fn generate_code(tokens: &mut TokenStream2, contract: &hir::Contract) {
codegen_for_state(tokens, contract);
codegen_for_messages(tokens, contract);
codegen_for_message_impls(tokens, contract);
codegen_for_method_impls(tokens, contract);
codegen_for_instantiate(tokens, contract);
codegen_for_entry_points(tokens);
}
fn codegen_for_entry_points(tokens: &mut TokenStream) {
fn codegen_for_entry_points(tokens: &mut TokenStream2) {
tokens.extend(quote! {
#[no_mangle]
fn deploy() {
......@@ -58,7 +61,7 @@ fn codegen_for_entry_points(tokens: &mut TokenStream) {
})
}
fn codegen_for_instantiate(tokens: &mut TokenStream, contract: &hir::Contract) {
fn codegen_for_instantiate(tokens: &mut TokenStream2, contract: &hir::Contract) {
let state_name = &contract.name;
let deploy_handler_toks = {
......@@ -180,7 +183,7 @@ fn codegen_for_instantiate(tokens: &mut TokenStream, contract: &hir::Contract) {
})
}
fn codegen_for_message_impls(tokens: &mut TokenStream, contract: &hir::Contract) {
fn codegen_for_message_impls(tokens: &mut TokenStream2, contract: &hir::Contract) {
let state_name = &contract.name;
let message_impls = {
let mut content = quote! {};
......@@ -229,7 +232,7 @@ fn codegen_for_message_impls(tokens: &mut TokenStream, contract: &hir::Contract)
});
}
fn codegen_for_method_impls(tokens: &mut TokenStream, contract: &hir::Contract) {
fn codegen_for_method_impls(tokens: &mut TokenStream2, contract: &hir::Contract) {
let state_name = &contract.name;
let methods_impls = {
let mut content = quote! {};
......@@ -289,7 +292,7 @@ impl CustomArgCaptured {
}
}
fn codegen_for_state(tokens: &mut TokenStream, contract: &hir::Contract) {
fn codegen_for_state(tokens: &mut TokenStream2, contract: &hir::Contract) {
let state_attrs_toks = {
let mut content = quote! {};
for attr in &contract.state.attrs {
......@@ -308,7 +311,7 @@ fn codegen_for_state(tokens: &mut TokenStream, contract: &hir::Contract) {
});
}
fn codegen_for_messages(tokens: &mut TokenStream, contract: &hir::Contract) {
fn codegen_for_messages(tokens: &mut TokenStream2, contract: &hir::Contract) {
let messages_content = {
let mut content = quote! {};
for message in contract.messages.iter() {
......
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of pDSL.
//
// pDSL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// pDSL is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with pDSL. If not, see <http://www.gnu.org/licenses/>.
//! Code generation for documentation generation of Wasm smart contracts.
//!
//! We use the special `#[cfg(rustdoc)]` that is set when `rustdoc` is
//! compiling a crate in order to generate special code that is only used
//! for documentation purposes.
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of pDSL.
//
// pDSL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// pDSL is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with pDSL. If not, see <http://www.gnu.org/licenses/>.
mod build;
use crate::hir;
use proc_macro2::TokenStream as TokenStream2;
use quote::{
quote,
};
/// Generates code for the given contract.
///
/// # Note
///
/// This generates code for normal Wasm smart contract builds as
/// well as code for specialized `test` and `doc` targets.
pub fn generate_code(contract: &hir::Contract) -> TokenStream2 {
let mut tokens = quote! {};
build::generate_code(&mut tokens, contract);
tokens
}
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of pDSL.
//
// pDSL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// pDSL is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with pDSL. If not, see <http://www.gnu.org/licenses/>.
//! Code generation for test generation of Wasm smart contracts.
//!
//! Test code is generated under the `#[cfg(test)]` compile flag.
......@@ -64,7 +64,7 @@ pub(crate) fn contract_gen_impl2(
let ast_contract = parser::parse_contract(input.clone())?;
let hir_contract = hir::Contract::from_ast(&ast_contract)?;
generate_api_description(&hir_contract)?;
let tokens = gen::codegen(&hir_contract);
let tokens = gen::generate_code(&hir_contract);
Ok(tokens)
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment