1use crate::{
19 deprecation::extract_or_return_allow_attrs,
20 pallet::{
21 expand::warnings::{weight_constant_warning, weight_witness_warning},
22 parse::{
23 call::{CallVariantDef, CallWeightDef},
24 helper::CallReturnType,
25 },
26 Def,
27 },
28 COUNTER,
29};
30use proc_macro2::TokenStream as TokenStream2;
31use proc_macro_warning::Warning;
32use quote::{quote, ToTokens};
33use syn::spanned::Spanned;
34
35fn expand_weight(
37 prefix: &str,
38 frame_support: &syn::Path,
39 dev_mode: bool,
40 weight_warnings: &mut Vec<Warning>,
41 method: &CallVariantDef,
42 weight: &CallWeightDef,
43) -> TokenStream2 {
44 match weight {
45 CallWeightDef::DevModeDefault => quote::quote!(
46 #frame_support::pallet_prelude::Weight::zero()
47 ),
48 CallWeightDef::Immediate(e) => {
49 weight_constant_warning(e, dev_mode, weight_warnings);
50 weight_witness_warning(method, dev_mode, weight_warnings);
51
52 e.into_token_stream()
53 },
54 CallWeightDef::Inherited(t) => {
55 let n = &syn::Ident::new(&format!("{}{}", prefix, method.name), method.name.span());
57 quote!({ < #t > :: #n () })
58 },
59 }
60}
61
62pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
66 let (span, where_clause, methods, docs) = match def.call.as_ref() {
67 Some(call) => {
68 let span = call.attr_span;
69 let where_clause = call.where_clause.clone();
70 let methods = call.methods.clone();
71 let docs = call.docs.clone();
72
73 (span, where_clause, methods, docs)
74 },
75 None => (def.item.span(), def.config.where_clause.clone(), Vec::new(), Vec::new()),
76 };
77 let frame_support = &def.frame_support;
78 let frame_system = &def.frame_system;
79 let type_impl_gen = &def.type_impl_generics(span);
80 let type_decl_bounded_gen = &def.type_decl_bounded_generics(span);
81 let type_use_gen = &def.type_use_generics(span);
82 let call_ident = syn::Ident::new("Call", span);
83 let pallet_ident = &def.pallet_struct.pallet;
84
85 let fn_name = methods.iter().map(|method| &method.name).collect::<Vec<_>>();
86 let call_index = methods.iter().map(|method| method.call_index).collect::<Vec<_>>();
87 let new_call_variant_fn_name = fn_name
88 .iter()
89 .map(|fn_name| quote::format_ident!("new_call_variant_{}", fn_name))
90 .collect::<Vec<_>>();
91
92 let new_call_variant_doc = fn_name
93 .iter()
94 .map(|fn_name| format!("Create a call with the variant `{}`.", fn_name))
95 .collect::<Vec<_>>();
96
97 let mut call_index_warnings = Vec::new();
98 for method in &methods {
100 if method.explicit_call_index || def.dev_mode {
101 continue
102 }
103
104 let warning = Warning::new_deprecated("ImplicitCallIndex")
105 .index(call_index_warnings.len())
106 .old("use implicit call indices")
107 .new("ensure that all calls have a `pallet::call_index` attribute or put the pallet into `dev` mode")
108 .help_links(&[
109 "https://github.com/paritytech/substrate/pull/12891",
110 "https://github.com/paritytech/substrate/pull/11381"
111 ])
112 .span(method.name.span())
113 .build_or_panic();
114 call_index_warnings.push(warning);
115 }
116
117 let mut fn_weight = Vec::<TokenStream2>::new();
118 let mut weight_warnings = Vec::new();
119 for method in &methods {
120 let w = expand_weight(
121 "",
122 frame_support,
123 def.dev_mode,
124 &mut weight_warnings,
125 method,
126 &method.weight,
127 );
128 fn_weight.push(w);
129 }
130 debug_assert_eq!(fn_weight.len(), methods.len());
131
132 let fn_doc = methods.iter().map(|method| &method.docs).collect::<Vec<_>>();
133
134 let args_name = methods
135 .iter()
136 .map(|method| method.args.iter().map(|(_, name, _)| name.clone()).collect::<Vec<_>>())
137 .collect::<Vec<_>>();
138
139 let args_name_stripped = methods
140 .iter()
141 .map(|method| {
142 method
143 .args
144 .iter()
145 .map(|(_, name, _)| {
146 syn::Ident::new(name.to_string().trim_start_matches('_'), name.span())
147 })
148 .collect::<Vec<_>>()
149 })
150 .collect::<Vec<_>>();
151
152 let make_args_name_pattern = |ref_tok| {
153 args_name
154 .iter()
155 .zip(args_name_stripped.iter())
156 .map(|(args_name, args_name_stripped)| {
157 args_name
158 .iter()
159 .zip(args_name_stripped)
160 .map(|(args_name, args_name_stripped)| {
161 if args_name == args_name_stripped {
162 quote::quote!( #ref_tok #args_name )
163 } else {
164 quote::quote!( #args_name_stripped: #ref_tok #args_name )
165 }
166 })
167 .collect::<Vec<_>>()
168 })
169 .collect::<Vec<_>>()
170 };
171
172 let args_name_pattern = make_args_name_pattern(None);
173 let args_name_pattern_ref = make_args_name_pattern(Some(quote::quote!(ref)));
174
175 let args_type = methods
176 .iter()
177 .map(|method| method.args.iter().map(|(_, _, type_)| type_.clone()).collect::<Vec<_>>())
178 .collect::<Vec<_>>();
179
180 let args_compact_attr = methods.iter().map(|method| {
181 method
182 .args
183 .iter()
184 .map(|(is_compact, _, type_)| {
185 if *is_compact {
186 quote::quote_spanned!(type_.span() => #[codec(compact)] )
187 } else {
188 quote::quote!()
189 }
190 })
191 .collect::<Vec<_>>()
192 });
193
194 let default_docs =
195 [syn::parse_quote!(r"Contains a variant per dispatchable extrinsic that this pallet has.")];
196 let docs = if docs.is_empty() { &default_docs[..] } else { &docs[..] };
197
198 let maybe_compile_error = if def.call.is_none() {
199 quote::quote! {
200 compile_error!(concat!(
201 "`",
202 stringify!($pallet_name),
203 "` does not have #[pallet::call] defined, perhaps you should remove `Call` from \
204 construct_runtime?",
205 ));
206 }
207 } else {
208 proc_macro2::TokenStream::new()
209 };
210
211 let count = COUNTER.with(|counter| counter.borrow_mut().inc());
212 let macro_ident = syn::Ident::new(&format!("__is_call_part_defined_{}", count), span);
213
214 let capture_docs = if cfg!(feature = "no-metadata-docs") { "never" } else { "always" };
215
216 if let Some(call) = def.call.as_ref() {
218 let item_impl =
219 &mut def.item.content.as_mut().expect("Checked by def parser").1[call.index];
220 let syn::Item::Impl(item_impl) = item_impl else {
221 unreachable!("Checked by def parser");
222 };
223
224 item_impl.items.iter_mut().enumerate().for_each(|(i, item)| {
225 if let syn::ImplItem::Fn(method) = item {
226 let return_type =
227 &call.methods.get(i).expect("def should be consistent with item").return_type;
228
229 let (ok_type, err_type) = match return_type {
230 CallReturnType::DispatchResult => (
231 quote::quote!(()),
232 quote::quote!(#frame_support::pallet_prelude::DispatchError),
233 ),
234 CallReturnType::DispatchResultWithPostInfo => (
235 quote::quote!(#frame_support::dispatch::PostDispatchInfo),
236 quote::quote!(#frame_support::dispatch::DispatchErrorWithPostInfo),
237 ),
238 };
239
240 let block = &method.block;
241 method.block = syn::parse_quote! {{
242 #frame_support::storage::with_storage_layer::<#ok_type, #err_type, _>(
245 || #block
246 )
247 }};
248 }
249 });
250 }
251
252 let maybe_allow_attrs = methods
254 .iter()
255 .map(|method| {
256 let attrs = extract_or_return_allow_attrs(&method.attrs);
257 quote::quote! {
258 #(#attrs)*
259 }
260 })
261 .collect::<Vec<_>>();
262
263 let cfg_attrs = methods
264 .iter()
265 .map(|method| {
266 let attrs =
267 method.cfg_attrs.iter().map(|attr| attr.to_token_stream()).collect::<Vec<_>>();
268 quote::quote!( #( #attrs )* )
269 })
270 .collect::<Vec<_>>();
271
272 let feeless_checks = methods.iter().map(|method| &method.feeless_check).collect::<Vec<_>>();
273 let feeless_check =
274 feeless_checks.iter().zip(args_name.iter()).map(|(feeless_check, arg_name)| {
275 if let Some(check) = feeless_check {
276 quote::quote_spanned!(span => #check)
277 } else {
278 quote::quote_spanned!(span => |_origin, #( #arg_name, )*| { false })
279 }
280 });
281
282 let deprecation = match crate::deprecation::get_deprecation_enum(
283 "e::quote! {#frame_support},
284 methods.iter().map(|item| (item.call_index as u8, item.attrs.as_ref())),
285 ) {
286 Ok(deprecation) => deprecation,
287 Err(e) => return e.into_compile_error(),
288 };
289
290 let (authorize_fn_pallet_impl, authorize_impl) = methods
295 .iter()
296 .zip(args_name.iter())
297 .zip(args_type.iter())
298 .zip(cfg_attrs.iter())
299 .map(|(((method, arg_name), arg_type), cfg_attr)| {
300 if let Some(authorize_def) = &method.authorize {
301 let authorize_fn = &authorize_def.expr;
302 let attr_fn_getter = syn::Ident::new(
303 &format!("__macro_inner_authorize_call_for_{}", method.name),
304 authorize_fn.span(),
305 );
306 let source = syn::Ident::new("source", span);
307
308 let authorize_fn_pallet_impl = quote::quote_spanned!(authorize_fn.span() =>
309 #cfg_attr
316 impl<#type_impl_gen> Pallet<#type_use_gen> #where_clause {
317 #[doc(hidden)]
318 fn #attr_fn_getter() -> impl Fn(
319 #frame_support::pallet_prelude::TransactionSource,
320 #( &#arg_type ),*
321 ) -> #frame_support::pallet_prelude::TransactionValidityWithRefund {
322 #authorize_fn
323 }
324 }
325 );
326
327 let authorize_impl = quote::quote!(
329 {
330 let authorize_fn = Pallet::<#type_use_gen>::#attr_fn_getter();
331 let res = authorize_fn(#source, #( #arg_name, )*);
332
333 Some(res)
334 }
335 );
336
337 (authorize_fn_pallet_impl, authorize_impl)
338 } else {
339 (Default::default(), quote::quote!(None))
340 }
341 })
342 .unzip::<_, _, Vec<TokenStream2>, Vec<TokenStream2>>();
343
344 let mut authorize_fn_weight = Vec::<TokenStream2>::new();
346 for method in &methods {
347 let w = match &method.authorize {
348 Some(authorize_def) => expand_weight(
349 "authorize_",
350 frame_support,
351 def.dev_mode,
352 &mut weight_warnings,
353 method,
354 &authorize_def.weight,
355 ),
356 None => quote::quote!(#frame_support::pallet_prelude::Weight::zero()),
358 };
359 authorize_fn_weight.push(w);
360 }
361 assert_eq!(authorize_fn_weight.len(), methods.len());
362
363 quote::quote_spanned!(span =>
364 #[doc(hidden)]
365 mod warnings {
366 #(
367 #call_index_warnings
368 )*
369 #(
370 #weight_warnings
371 )*
372 }
373
374 #[allow(unused_imports)]
375 #[doc(hidden)]
376 pub mod __substrate_call_check {
377 #[macro_export]
378 #[doc(hidden)]
379 macro_rules! #macro_ident {
380 ($pallet_name:ident) => {
381 #maybe_compile_error
382 };
383 }
384
385 #[doc(hidden)]
386 pub use #macro_ident as is_call_part_defined;
387 }
388
389 #( #[doc = #docs] )*
390 #[derive(
391 #frame_support::RuntimeDebugNoBound,
392 #frame_support::CloneNoBound,
393 #frame_support::EqNoBound,
394 #frame_support::PartialEqNoBound,
395 #frame_support::__private::codec::Encode,
396 #frame_support::__private::codec::Decode,
397 #frame_support::__private::codec::DecodeWithMemTracking,
398 #frame_support::__private::scale_info::TypeInfo,
399 )]
400 #[codec(encode_bound())]
401 #[codec(decode_bound())]
402 #[scale_info(skip_type_params(#type_use_gen), capture_docs = #capture_docs)]
403 #[allow(non_camel_case_types)]
404 pub enum #call_ident<#type_decl_bounded_gen> #where_clause {
405 #[doc(hidden)]
406 #[codec(skip)]
407 __Ignore(
408 ::core::marker::PhantomData<(#type_use_gen,)>,
409 #frame_support::Never,
410 ),
411 #(
412 #cfg_attrs
413 #( #[doc = #fn_doc] )*
414 #[codec(index = #call_index)]
415 #fn_name {
416 #(
417 #[allow(missing_docs)]
418 #args_compact_attr #args_name_stripped: #args_type
419 ),*
420 },
421 )*
422 }
423
424 impl<#type_impl_gen> #call_ident<#type_use_gen> #where_clause {
425 #(
426 #cfg_attrs
427 #[doc = #new_call_variant_doc]
428 pub fn #new_call_variant_fn_name(
429 #( #args_name_stripped: #args_type ),*
430 ) -> Self {
431 Self::#fn_name {
432 #( #args_name_stripped ),*
433 }
434 }
435 )*
436 }
437
438 impl<#type_impl_gen> #frame_support::dispatch::GetDispatchInfo
439 for #call_ident<#type_use_gen>
440 #where_clause
441 {
442 fn get_dispatch_info(&self) -> #frame_support::dispatch::DispatchInfo {
443 match *self {
444 #(
445 #cfg_attrs
446 Self::#fn_name { #( #args_name_pattern_ref, )* } => {
447 let __pallet_base_weight = #fn_weight;
448
449 let __pallet_weight = <
450 dyn #frame_support::dispatch::WeighData<( #( & #args_type, )* )>
451 >::weigh_data(&__pallet_base_weight, ( #( #args_name, )* ));
452
453 let __pallet_class = <
454 dyn #frame_support::dispatch::ClassifyDispatch<
455 ( #( & #args_type, )* )
456 >
457 >::classify_dispatch(&__pallet_base_weight, ( #( #args_name, )* ));
458
459 let __pallet_pays_fee = <
460 dyn #frame_support::dispatch::PaysFee<( #( & #args_type, )* )>
461 >::pays_fee(&__pallet_base_weight, ( #( #args_name, )* ));
462
463 #frame_support::dispatch::DispatchInfo {
464 call_weight: __pallet_weight,
465 extension_weight: Default::default(),
466 class: __pallet_class,
467 pays_fee: __pallet_pays_fee,
468 }
469 },
470 )*
471 Self::__Ignore(_, _) => unreachable!("__Ignore cannot be used"),
472 }
473 }
474 }
475
476 impl<#type_impl_gen> #frame_support::dispatch::CheckIfFeeless for #call_ident<#type_use_gen>
477 #where_clause
478 {
479 type Origin = #frame_system::pallet_prelude::OriginFor<T>;
480 #[allow(unused_variables)]
481 fn is_feeless(&self, origin: &Self::Origin) -> bool {
482 match *self {
483 #(
484 #cfg_attrs
485 Self::#fn_name { #( #args_name_pattern_ref, )* } => {
486 let feeless_check = #feeless_check;
487 feeless_check(origin, #( #args_name, )*)
488 },
489 )*
490 Self::__Ignore(_, _) => unreachable!("__Ignore cannot be used"),
491 }
492 }
493 }
494
495 impl<#type_impl_gen> #frame_support::traits::GetCallName for #call_ident<#type_use_gen>
496 #where_clause
497 {
498 fn get_call_name(&self) -> &'static str {
499 match *self {
500 #( #cfg_attrs Self::#fn_name { .. } => stringify!(#fn_name), )*
501 Self::__Ignore(_, _) => unreachable!("__PhantomItem cannot be used."),
502 }
503 }
504
505 fn get_call_names() -> &'static [&'static str] {
506 &[ #( #cfg_attrs stringify!(#fn_name), )* ]
507 }
508 }
509
510 impl<#type_impl_gen> #frame_support::traits::GetCallIndex for #call_ident<#type_use_gen>
511 #where_clause
512 {
513 fn get_call_index(&self) -> u8 {
514 match *self {
515 #( #cfg_attrs Self::#fn_name { .. } => #call_index, )*
516 Self::__Ignore(_, _) => unreachable!("__PhantomItem cannot be used."),
517 }
518 }
519
520 fn get_call_indices() -> &'static [u8] {
521 &[ #( #cfg_attrs #call_index, )* ]
522 }
523 }
524
525 impl<#type_impl_gen> #frame_support::traits::UnfilteredDispatchable
526 for #call_ident<#type_use_gen>
527 #where_clause
528 {
529 type RuntimeOrigin = #frame_system::pallet_prelude::OriginFor<T>;
530 fn dispatch_bypass_filter(
531 self,
532 origin: Self::RuntimeOrigin
533 ) -> #frame_support::dispatch::DispatchResultWithPostInfo {
534 #frame_support::dispatch_context::run_in_context(|| {
535 match self {
536 #(
537 #cfg_attrs
538 Self::#fn_name { #( #args_name_pattern, )* } => {
539 #frame_support::__private::sp_tracing::enter_span!(
540 #frame_support::__private::sp_tracing::trace_span!(stringify!(#fn_name))
541 );
542 #maybe_allow_attrs
543 #[allow(clippy::useless_conversion)]
544 <#pallet_ident<#type_use_gen>>::#fn_name(origin, #( #args_name, )* )
545 .map(Into::into).map_err(Into::into)
546 },
547 )*
548 Self::__Ignore(_, _) => {
549 let _ = origin; unreachable!("__PhantomItem cannot be used.");
551 },
552 }
553 })
554 }
555 }
556
557 impl<#type_impl_gen> #frame_support::dispatch::Callable<T> for #pallet_ident<#type_use_gen>
558 #where_clause
559 {
560 type RuntimeCall = #call_ident<#type_use_gen>;
561 }
562
563 impl<#type_impl_gen> #pallet_ident<#type_use_gen> #where_clause {
564 #[allow(dead_code)]
565 #[doc(hidden)]
566 pub fn call_functions() -> #frame_support::__private::metadata_ir::PalletCallMetadataIR {
567 #frame_support::__private::metadata_ir::PalletCallMetadataIR {
568 ty: #frame_support::__private::scale_info::meta_type::<#call_ident<#type_use_gen>>(),
569 deprecation_info: #deprecation,
570 }
571 }
572 }
573
574 #( #authorize_fn_pallet_impl )*
575
576 impl<#type_impl_gen> #frame_support::traits::Authorize for #call_ident<#type_use_gen>
577 #where_clause
578 {
579 fn authorize(&self, source: #frame_support::pallet_prelude::TransactionSource) -> ::core::option::Option<::core::result::Result<
580 (
581 #frame_support::pallet_prelude::ValidTransaction,
582 #frame_support::pallet_prelude::Weight,
583 ),
584 #frame_support::pallet_prelude::TransactionValidityError
585 >>
586 {
587 match *self {
588 #(
589 #cfg_attrs
590 Self::#fn_name { #( #args_name_pattern_ref, )* } => {
591 #authorize_impl
592 },
593 )*
594 Self::__Ignore(_, _) => {
595 let _ = source;
596 unreachable!("__Ignore cannot be used")
597 },
598 }
599 }
600
601 fn weight_of_authorize(&self) -> #frame_support::pallet_prelude::Weight {
602 match *self {
603 #(
604 #cfg_attrs
605 Self::#fn_name { #( #args_name_pattern_ref, )* } => {
606 #authorize_fn_weight
607 },
608 )*
609 Self::__Ignore(_, _) => unreachable!("__Ignore cannot be used"),
610 }
611 }
612 }
613 )
614}