This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.rs
385 lines (313 loc) · 9.26 KB
/
app.rs
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use syn::{
Error, Item, ItemMod, ItemStruct,
ItemType, ItemUse, Result,
};
use crate::{schema, Struct, Function, Schema};
use super::{r#struct, function};
use r#struct::has_storage_attr;
pub struct App {
name: Ident,
functions: Vec<Function>,
structs: Vec<Struct>,
imports: Vec<ItemUse>,
aliases: Vec<ItemType>,
}
impl App {
pub fn name(&self) -> &Ident {
&self.name
}
pub fn functions(&self) -> &[Function] {
&self.functions
}
pub fn structs(&self) -> &[Struct] {
&self.structs
}
pub fn imports(&self) -> &[ItemUse] {
&self.imports
}
pub fn aliases(&self) -> &[ItemType] {
&self.aliases
}
}
pub fn expand(_args: TokenStream, input: TokenStream) -> Result<(Schema, TokenStream)> {
let module = syn::parse2(input)?;
let app = parse_app(module)?;
let imports = app.imports();
let aliases = app.aliases();
let structs = expand_structs(&app)?;
let functions = expand_functions(&app)?;
let alloc_func = alloc_func_ast();
let schema = schema::app_schema(&app);
let ast = quote! {
// #(#imports)*
// #(#aliases)*
#alloc_func
#structs
#functions
};
Ok((schema,ast))
}
pub fn parse_app(mut raw_app: ItemMod) -> Result<App> {
let name = raw_app.ident.clone();
let mut functions = Vec::new();
let mut structs = Vec::new();
let mut imports = Vec::new();
let mut aliases = Vec::new();
let (_, content) = raw_app.content.take().unwrap();
for item in content {
// TODO: Is is possible to extract the `item` real `Span`?
let span = Span::call_site();
match item {
Item::Fn(item) => {
let func = Function::new(item);
functions.push(func);
}
Item::Struct(item) => {
let strukt = Struct::new(item);
structs.push(strukt);
}
Item::Use(item) => imports.push(item),
Item::Type(item) => aliases.push(item),
Item::Const(..) => {
let msg = "declaring `const` inside `#[app]` is not supported.";
return Err(Error::new(span, msg));
}
Item::Enum(..) => {
let msg = "declaring `enum` inside `#[app]` is not supported.";
return Err(Error::new(span, msg));
}
Item::ExternCrate(..) => {
let msg = "using `extern crate` inside `#[app]` is not supported.";
return Err(Error::new(span, msg));
}
Item::ForeignMod(..) => {
let msg =
"using foreign items such as `extern \"C\"` inside `#[app]` is not supported.";
return Err(Error::new(span, msg));
}
Item::Impl(..) => {
let msg = "using `impl` inside `#[app]` is not supported.";
return Err(Error::new(span, msg));
}
Item::Macro(..) => {
let msg = "declaring `macro_rules!` inside `#[app]` is not supported.";
return Err(Error::new(span, msg));
}
Item::Macro2(..) => {
let msg = "declaring `macro` inside `#[app]` is not supported.";
return Err(Error::new(span, msg));
}
Item::Mod(..) => {
let msg = "declaring new modules inside `#[app]` is not supported.";
return Err(Error::new(span, msg));
}
Item::Static(..) => {
let msg = "declaring new `static` items inside `#[app]` is not supported.";
return Err(Error::new(span, msg));
}
Item::Trait(..) => {
let msg = "declaring new traits inside `#[app]` is not supported.";
return Err(Error::new(span, msg));
}
Item::TraitAlias(..) => {
let msg = "using trait aliases inside `#[app]` is not supported.";
return Err(Error::new(span, msg));
}
Item::Union(..) => {
let msg = "declaring `union` inside `#[app]` is not supported.";
return Err(Error::new(span, msg));
}
Item::Verbatim(item) => {
let msg = format!("invalid Rust code: {}", item);
return Err(Error::new(span, msg));
}
Item::__Nonexhaustive => unreachable!(),
}
}
let app = App {
name,
functions,
structs,
imports,
aliases,
};
Ok(app)
}
fn expand_structs(app: &App) -> Result<TokenStream> {
let mut structs = Vec::new();
validate_structs(app)?;
for strukt in app.structs() {
let strukt = r#struct::expand(strukt)?;
structs.push(strukt);
}
let ast = quote! {
#(#structs)*
};
Ok(ast)
}
fn validate_structs(app: &App) -> Result<()> {
let mut seen_storage = false;
for strukt in app.structs() {
match strukt.attrs() {
Ok(attrs) => {
if has_storage_attr(attrs) {
if seen_storage {
let msg = format!("an App can have only a single `#[storage]`");
let span = Span::call_site();
return Err(Error::new(span, msg));
}
seen_storage = true;
}
}
Err(err) => return Err(err.clone())
}
}
Ok(())
}
fn expand_functions(app: &App) -> Result<TokenStream> {
let mut funcs = Vec::new();
for func in app.functions() {
let func = function::expand(func)?;
funcs.push(func);
}
let ast = quote! {
#(#funcs)*
};
Ok(ast)
}
fn alloc_func_ast() -> TokenStream {
quote! {
// injects the `#[global_allocator]`
extern crate svm_sdk;
#[no_mangle]
pub extern "C" fn svm_alloc(size: u32) -> u32 {
let ptr = svm_sdk::alloc(size as usize);
ptr.offset() as u32
}
}
}
#[cfg(test)]
mod test {
use super::*;
use syn::parse_quote;
macro_rules! assert_err {
($expected:expr, $($tt:tt)*) => {{
let raw_app: ItemMod = parse_quote!( $($tt)* );
let res = parse_app(raw_app);
// we can't use `unwrap_err()` since `App`
// doesn't implement `std::fmt::Debug`
let actual = res.err().unwrap();
assert_eq!($expected, actual.to_string());
}};
}
#[test]
fn app_empty() {
let raw_app: ItemMod = parse_quote! {
#[app]
mod my_app {}
};
let res = parse_app(raw_app);
assert!(res.is_ok());
}
#[test]
fn app_declaring_const_not_allowed() {
let err = "declaring `const` inside `#[app]` is not supported.";
assert_err!(
err,
#[app]
mod my_app {
const N: u32 = 10;
}
);
}
#[test]
fn app_declaring_static_not_allowed() {
let err = "declaring new `static` items inside `#[app]` is not supported.";
assert_err!(
err,
#[app]
mod my_app {
static N: u32 = 10;
}
);
}
#[test]
fn app_declaring_enum_not_allowed() {
let err = "declaring `enum` inside `#[app]` is not supported.";
assert_err!(
err,
#[app]
mod my_app {
enum MyEum {}
}
);
}
#[test]
fn app_using_extern_crate_not_allowed() {
let err = "using `extern crate` inside `#[app]` is not supported.";
assert_err!(
err,
#[app]
mod my_app {
extern crate alloc;
}
);
}
#[test]
fn app_using_ffi_not_allowed() {
let err = "using foreign items such as `extern \"C\"` inside `#[app]` is not supported.";
assert_err!(
err,
#[app]
mod my_app {
extern "C" {}
}
);
}
#[test]
fn app_using_impl_not_allowed() {
let err = "using `impl` inside `#[app]` is not supported.";
assert_err!(
err,
#[app]
mod my_app {
struct S;
impl S {}
}
);
}
#[test]
fn app_using_macro_rules_not_allowed() {
let err = "declaring `macro_rules!` inside `#[app]` is not supported.";
assert_err!(
err,
#[app]
mod my_app {
macro_rules! print {}
}
);
}
#[test]
fn app_declaring_traits_not_allowed() {
let err = "declaring new traits inside `#[app]` is not supported.";
assert_err!(err,
#[app]
mod my_app {
trait Print {}
}
);
}
#[test]
fn app_declaring_union_not_allowed() {
let err = "declaring `union` inside `#[app]` is not supported.";
assert_err!(
err,
#[app]
mod my_app {
union U {}
}
);
}
}