1
2
3
4
5
6
7
8
9
10
11
12
13
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// 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 super::parse::runtime_types::RuntimeType;
use crate::{
	construct_runtime::{
		check_pallet_number, decl_all_pallets, decl_integrity_test, decl_pallet_runtime_setup,
		decl_static_assertions, expand,
	},
	runtime::{
		parse::{
			AllPalletsDeclaration, ExplicitAllPalletsDeclaration, ImplicitAllPalletsDeclaration,
		},
		Def,
	},
};
use cfg_expr::Predicate;
use frame_support_procedural_tools::{
	generate_access_from_frame_or_crate, generate_crate_access, generate_hidden_includes,
};
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use std::collections::HashSet;
use syn::{Ident, Result};

/// The fixed name of the system pallet.
const SYSTEM_PALLET_NAME: &str = "System";

pub fn expand(def: Def, legacy_ordering: bool) -> TokenStream2 {
	let input = def.input;

	let (check_pallet_number_res, res) = match def.pallets {
		AllPalletsDeclaration::Implicit(ref decl) => (
			check_pallet_number(input.clone(), decl.pallet_count),
			construct_runtime_implicit_to_explicit(input.into(), decl.clone(), legacy_ordering),
		),
		AllPalletsDeclaration::Explicit(ref decl) => (
			check_pallet_number(input, decl.pallets.len()),
			construct_runtime_final_expansion(
				def.runtime_struct.ident.clone(),
				decl.clone(),
				def.runtime_types.clone(),
				legacy_ordering,
			),
		),
	};

	let res = res.unwrap_or_else(|e| e.to_compile_error());

	// We want to provide better error messages to the user and thus, handle the error here
	// separately. If there is an error, we print the error and still generate all of the code to
	// get in overall less errors for the user.
	let res = if let Err(error) = check_pallet_number_res {
		let error = error.to_compile_error();

		quote! {
			#error

			#res
		}
	} else {
		res
	};

	let res = expander::Expander::new("construct_runtime")
		.dry(std::env::var("EXPAND_MACROS").is_err())
		.verbose(true)
		.write_to_out_dir(res)
		.expect("Does not fail because of IO in OUT_DIR; qed");

	res.into()
}

fn construct_runtime_implicit_to_explicit(
	input: TokenStream2,
	definition: ImplicitAllPalletsDeclaration,
	legacy_ordering: bool,
) -> Result<TokenStream2> {
	let frame_support = generate_access_from_frame_or_crate("frame-support")?;
	let attr = if legacy_ordering { quote!((legacy_ordering)) } else { quote!() };
	let mut expansion = quote::quote!(
		#[#frame_support::runtime #attr]
		#input
	);
	for pallet in definition.pallet_decls.iter() {
		let pallet_path = &pallet.path;
		let pallet_name = &pallet.name;
		let runtime_param = &pallet.runtime_param;
		let pallet_segment_and_instance = match (&pallet.pallet_segment, &pallet.instance) {
			(Some(segment), Some(instance)) => quote::quote!(::#segment<#runtime_param, #instance>),
			(Some(segment), None) => quote::quote!(::#segment<#runtime_param>),
			(None, Some(instance)) => quote::quote!(<#instance>),
			(None, None) => quote::quote!(),
		};
		expansion = quote::quote!(
			#frame_support::__private::tt_call! {
				macro = [{ #pallet_path::tt_default_parts_v2 }]
				your_tt_return = [{ #frame_support::__private::tt_return }]
				~~> #frame_support::match_and_insert! {
					target = [{ #expansion }]
					pattern = [{ #pallet_name = #pallet_path #pallet_segment_and_instance }]
				}
			}
		);
	}

	Ok(expansion)
}

fn construct_runtime_final_expansion(
	name: Ident,
	definition: ExplicitAllPalletsDeclaration,
	runtime_types: Vec<RuntimeType>,
	legacy_ordering: bool,
) -> Result<TokenStream2> {
	let ExplicitAllPalletsDeclaration { mut pallets, name: pallets_name } = definition;

	if !legacy_ordering {
		// Ensure that order of hooks is based on the pallet index
		pallets.sort_by_key(|p| p.index);
	}

	let system_pallet =
		pallets.iter().find(|decl| decl.name == SYSTEM_PALLET_NAME).ok_or_else(|| {
			syn::Error::new(
				pallets_name.span(),
				"`System` pallet declaration is missing. \
			 Please add this line: `pub type System = frame_system;`",
			)
		})?;
	if !system_pallet.cfg_pattern.is_empty() {
		return Err(syn::Error::new(
			system_pallet.name.span(),
			"`System` pallet declaration is feature gated, please remove any `#[cfg]` attributes",
		))
	}

	let features = pallets
		.iter()
		.filter_map(|decl| {
			(!decl.cfg_pattern.is_empty()).then(|| {
				decl.cfg_pattern.iter().flat_map(|attr| {
					attr.predicates().filter_map(|pred| match pred {
						Predicate::Feature(feat) => Some(feat),
						Predicate::Test => Some("test"),
						_ => None,
					})
				})
			})
		})
		.flatten()
		.collect::<HashSet<_>>();

	let hidden_crate_name = "construct_runtime";
	let scrate = generate_crate_access(hidden_crate_name, "frame-support");
	let scrate_decl = generate_hidden_includes(hidden_crate_name, "frame-support");

	let frame_system = generate_access_from_frame_or_crate("frame-system")?;
	let block = quote!(<#name as #frame_system::Config>::Block);
	let unchecked_extrinsic = quote!(<#block as #scrate::sp_runtime::traits::Block>::Extrinsic);

	let mut dispatch = None;
	let mut outer_event = None;
	let mut outer_error = None;
	let mut outer_origin = None;
	let mut freeze_reason = None;
	let mut hold_reason = None;
	let mut slash_reason = None;
	let mut lock_id = None;
	let mut task = None;

	for runtime_type in runtime_types.iter() {
		match runtime_type {
			RuntimeType::RuntimeCall(_) => {
				dispatch =
					Some(expand::expand_outer_dispatch(&name, system_pallet, &pallets, &scrate));
			},
			RuntimeType::RuntimeEvent(_) => {
				outer_event = Some(expand::expand_outer_enum(
					&name,
					&pallets,
					&scrate,
					expand::OuterEnumType::Event,
				)?);
			},
			RuntimeType::RuntimeError(_) => {
				outer_error = Some(expand::expand_outer_enum(
					&name,
					&pallets,
					&scrate,
					expand::OuterEnumType::Error,
				)?);
			},
			RuntimeType::RuntimeOrigin(_) => {
				outer_origin =
					Some(expand::expand_outer_origin(&name, system_pallet, &pallets, &scrate)?);
			},
			RuntimeType::RuntimeFreezeReason(_) => {
				freeze_reason = Some(expand::expand_outer_freeze_reason(&pallets, &scrate));
			},
			RuntimeType::RuntimeHoldReason(_) => {
				hold_reason = Some(expand::expand_outer_hold_reason(&pallets, &scrate));
			},
			RuntimeType::RuntimeSlashReason(_) => {
				slash_reason = Some(expand::expand_outer_slash_reason(&pallets, &scrate));
			},
			RuntimeType::RuntimeLockId(_) => {
				lock_id = Some(expand::expand_outer_lock_id(&pallets, &scrate));
			},
			RuntimeType::RuntimeTask(_) => {
				task = Some(expand::expand_outer_task(&name, &pallets, &scrate));
			},
		}
	}

	let all_pallets = decl_all_pallets(&name, pallets.iter(), &features);
	let pallet_to_index = decl_pallet_runtime_setup(&name, &pallets, &scrate);

	let metadata = expand::expand_runtime_metadata(
		&name,
		&pallets,
		&scrate,
		&unchecked_extrinsic,
		&system_pallet.path,
	);
	let outer_config = expand::expand_outer_config(&name, &pallets, &scrate);
	let inherent =
		expand::expand_outer_inherent(&name, &block, &unchecked_extrinsic, &pallets, &scrate);
	let validate_unsigned = expand::expand_outer_validate_unsigned(&name, &pallets, &scrate);
	let integrity_test = decl_integrity_test(&scrate);
	let static_assertions = decl_static_assertions(&name, &pallets, &scrate);

	let res = quote!(
		#scrate_decl

		// Prevent UncheckedExtrinsic to print unused warning.
		const _: () = {
			#[allow(unused)]
			type __HiddenUseOfUncheckedExtrinsic = #unchecked_extrinsic;
		};

		#[derive(
			Clone, Copy, PartialEq, Eq, #scrate::sp_runtime::RuntimeDebug,
			#scrate::__private::scale_info::TypeInfo
		)]
		pub struct #name;
		impl #scrate::sp_runtime::traits::GetRuntimeBlockType for #name {
			type RuntimeBlock = #block;
		}

		// Each runtime must expose the `runtime_metadata()` to fetch the runtime API metadata.
		// The function is implemented by calling `impl_runtime_apis!`.
		//
		// However, the `runtime` may be used without calling `impl_runtime_apis!`.
		// Rely on the `Deref` trait to differentiate between a runtime that implements
		// APIs (by macro impl_runtime_apis!) and a runtime that is simply created (by macro runtime).
		//
		// Both `InternalConstructRuntime` and `InternalImplRuntimeApis` expose a `runtime_metadata()` function.
		// `InternalConstructRuntime` is implemented by the `runtime` for Runtime references (`& Runtime`),
		// while `InternalImplRuntimeApis` is implemented by the `impl_runtime_apis!` for Runtime (`Runtime`).
		//
		// Therefore, the `Deref` trait will resolve the `runtime_metadata` from `impl_runtime_apis!`
		// when both macros are called; and will resolve an empty `runtime_metadata` when only the `runtime`
		// is used.

		#[doc(hidden)]
		trait InternalConstructRuntime {
			#[inline(always)]
			fn runtime_metadata(&self) -> #scrate::__private::Vec<#scrate::__private::metadata_ir::RuntimeApiMetadataIR> {
				Default::default()
			}
		}
		#[doc(hidden)]
		impl InternalConstructRuntime for &#name {}

		#outer_event

		#outer_error

		#outer_origin

		#all_pallets

		#pallet_to_index

		#dispatch

		#task

		#metadata

		#outer_config

		#inherent

		#validate_unsigned

		#freeze_reason

		#hold_reason

		#lock_id

		#slash_reason

		#integrity_test

		#static_assertions
	);

	Ok(res)
}