-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:paritytech/ink into robin-base-ke…
…y-on-u8x32 # Conflicts: # .config/cargo_spellcheck.dic
- Loading branch information
Showing
406 changed files
with
11,734 additions
and
4,716 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2018-2021 Parity Technologies (UK) Ltd. | ||
// | ||
// 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 impl_serde::serialize as serde_hex; | ||
use quote::format_ident; | ||
|
||
/// Errors which may occur when forwarding a call is not allowed. | ||
/// | ||
/// We insert markers for these errors in the generated contract code. | ||
/// This is necessary since we can't check these errors at compile time | ||
/// of the contract. | ||
/// `cargo-contract` checks the contract code for these error markers | ||
/// when building a contract and fails if it finds markers. | ||
#[derive(scale::Encode, scale::Decode)] | ||
pub enum EnforcedErrors { | ||
/// The below error represents calling a `&mut self` message in a context that | ||
/// only allows for `&self` messages. This may happen under certain circumstances | ||
/// when ink! trait implementations are involved with long-hand calling notation. | ||
#[codec(index = 1)] | ||
CannotCallTraitMessage { | ||
/// The trait that defines the called message. | ||
trait_ident: String, | ||
/// The name of the called message. | ||
message_ident: String, | ||
/// The selector of the called message. | ||
message_selector: [u8; 4], | ||
/// Is `true` if the `self` receiver of the ink! message is `&mut self`. | ||
message_is_mut: bool, | ||
}, | ||
} | ||
|
||
impl EnforcedErrors { | ||
/// Create the identifier of an enforced ink! compilation error. | ||
fn into_ident(self) -> syn::Ident { | ||
format_ident!( | ||
"__ink_enforce_error_{}", | ||
serde_hex::to_hex(&scale::Encode::encode(&self), false) | ||
) | ||
} | ||
|
||
/// Creates an enforced linker error to signal that an invalid | ||
/// implementation of an ink! trait message has been called. | ||
pub fn cannot_call_trait_message( | ||
trait_ident: &syn::Ident, | ||
message_ident: &syn::Ident, | ||
message_selector: ir::Selector, | ||
message_is_mut: bool, | ||
) -> syn::Ident { | ||
Self::CannotCallTraitMessage { | ||
trait_ident: trait_ident.to_string(), | ||
message_ident: message_ident.to_string(), | ||
message_selector: message_selector.to_bytes(), | ||
message_is_mut, | ||
} | ||
.into_ident() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2018-2021 Parity Technologies (UK) Ltd. | ||
// | ||
// 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 heck::CamelCase; | ||
use proc_macro2::{ | ||
Span, | ||
TokenStream as TokenStream2, | ||
}; | ||
use quote::{ | ||
format_ident, | ||
quote, | ||
quote_spanned, | ||
}; | ||
|
||
/// Returns the associated output type for an ink! trait message. | ||
pub fn output_ident(message_name: &syn::Ident) -> syn::Ident { | ||
format_ident!("{}Output", message_name.to_string().to_camel_case()) | ||
} | ||
|
||
/// Returns the sequence of artificial input parameter bindings for the message. | ||
/// | ||
/// # Note | ||
/// | ||
/// This returns `__ink_binding_N` for every message input where `N` is the number | ||
/// of the input from first to last. | ||
pub fn input_bindings(inputs: ir::InputsIter) -> Vec<syn::Ident> { | ||
inputs | ||
.enumerate() | ||
.map(|(n, _)| format_ident!("__ink_binding_{}", n)) | ||
.collect::<Vec<_>>() | ||
} | ||
|
||
/// Returns the sequence of input types for the message. | ||
pub fn input_types(inputs: ir::InputsIter) -> Vec<&syn::Type> { | ||
inputs.map(|pat_type| &*pat_type.ty).collect::<Vec<_>>() | ||
} | ||
|
||
/// Returns a tuple type representing the types yielded by the input types. | ||
pub fn input_types_tuple(inputs: ir::InputsIter) -> TokenStream2 { | ||
let input_types = input_types(inputs); | ||
if input_types.len() != 1 { | ||
// Pack all types into a tuple if they are not exactly 1. | ||
// This results in `()` for zero input types. | ||
quote! { ( #( #input_types ),* ) } | ||
} else { | ||
// Return the single type without turning it into a tuple. | ||
quote! { #( #input_types )* } | ||
} | ||
} | ||
|
||
/// Returns a tuple expression representing the bindings yielded by the inputs. | ||
pub fn input_bindings_tuple(inputs: ir::InputsIter) -> TokenStream2 { | ||
let input_bindings = input_bindings(inputs); | ||
match input_bindings.len() { | ||
0 => quote! { _ }, | ||
1 => quote! { #( #input_bindings ),* }, | ||
_ => quote! { ( #( #input_bindings ),* ) }, | ||
} | ||
} | ||
|
||
/// Builds up the `ink_env::call::utils::ArgumentList` type structure for the given types. | ||
pub fn generate_argument_list<'b, Args>(args: Args) -> TokenStream2 | ||
where | ||
Args: IntoIterator<Item = &'b syn::Type>, | ||
<Args as IntoIterator>::IntoIter: Iterator, | ||
{ | ||
use syn::spanned::Spanned as _; | ||
args.into_iter().fold( | ||
quote! { ::ink_env::call::utils::EmptyArgumentList }, | ||
|rest, arg| { | ||
let span = arg.span(); | ||
quote_spanned!(span=> | ||
::ink_env::call::utils::ArgumentList<::ink_env::call::utils::Argument<#arg>, #rest> | ||
) | ||
} | ||
) | ||
} | ||
|
||
/// Generates code to uniquely identify a trait by its unique ID given only its identifier. | ||
/// | ||
/// # Note | ||
/// | ||
/// As with all Rust macros identifiers can shadow each other so the given identifier | ||
/// needs to be valid for the scope in which the returned code is generated. | ||
pub fn generate_reference_to_trait_info( | ||
span: Span, | ||
trait_path: &syn::Path, | ||
) -> TokenStream2 { | ||
quote_spanned!(span=> | ||
<::ink_lang::reflect::TraitDefinitionRegistry<Environment> | ||
as #trait_path>::__ink_TraitInfo | ||
) | ||
} |
Oops, something went wrong.