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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
use std::collections::HashSet;
// use indexmap::map::Entry;
use proc_macro2::TokenStream as TokenStream2;
use syn::{
parse::{self, ParseStream, Parser},
spanned::Spanned,
Expr, ExprArray, Fields, ForeignItem, Ident, Item, LitBool, Path, Token, Visibility,
};
use super::Input;
use crate::syntax::{
ast::{
App, AppArgs, Dispatcher, Dispatchers, HardwareTask, Idle, IdleArgs, Init, InitArgs,
LocalResource, SharedResource, SoftwareTask,
},
parse::{self as syntax_parse, util},
Either, Map, Set,
};
impl AppArgs {
pub(crate) fn parse(tokens: TokenStream2) -> parse::Result<Self> {
(|input: ParseStream<'_>| -> parse::Result<Self> {
let mut custom = Set::new();
let mut device = None;
let mut peripherals = true;
let mut dispatchers = Dispatchers::new();
loop {
if input.is_empty() {
break;
}
// #ident = ..
let ident: Ident = input.parse()?;
let _eq_token: Token![=] = input.parse()?;
if custom.contains(&ident) {
return Err(parse::Error::new(
ident.span(),
"argument appears more than once",
));
}
custom.insert(ident.clone());
let ks = ident.to_string();
match &*ks {
"device" => {
if let Ok(p) = input.parse::<Path>() {
device = Some(p);
} else {
return Err(parse::Error::new(
ident.span(),
"unexpected argument value; this should be a path",
));
}
}
"peripherals" => {
if let Ok(p) = input.parse::<LitBool>() {
peripherals = p.value;
} else {
return Err(parse::Error::new(
ident.span(),
"unexpected argument value; this should be a boolean",
));
}
}
"dispatchers" => {
if let Ok(p) = input.parse::<ExprArray>() {
for e in p.elems {
match e {
Expr::Path(ep) => {
let path = ep.path;
let ident = if path.leading_colon.is_some()
|| path.segments.len() != 1
{
return Err(parse::Error::new(
path.span(),
"interrupt must be an identifier, not a path",
));
} else {
path.segments[0].ident.clone()
};
let span = ident.span();
if dispatchers.contains_key(&ident) {
return Err(parse::Error::new(
span,
"this extern interrupt is listed more than once",
));
} else {
dispatchers
.insert(ident, Dispatcher { attrs: ep.attrs });
}
}
_ => {
return Err(parse::Error::new(
e.span(),
"interrupt must be an identifier",
));
}
}
}
} else {
return Err(parse::Error::new(
ident.span(),
// increasing the length of the error message will break rustfmt
"unexpected argument value; expected an array",
));
}
}
_ => {
return Err(parse::Error::new(ident.span(), "unexpected argument"));
}
}
if input.is_empty() {
break;
}
// ,
let _: Token![,] = input.parse()?;
}
let device = if let Some(device) = device {
device
} else {
return Err(parse::Error::new(input.span(), "missing `device = ...`"));
};
Ok(AppArgs {
device,
peripherals,
dispatchers,
})
})
.parse2(tokens)
}
}
impl App {
pub(crate) fn parse(args: AppArgs, input: Input) -> parse::Result<Self> {
let mut init = None;
let mut idle = None;
let mut shared_resources_ident = None;
let mut shared_resources = Map::new();
let mut local_resources_ident = None;
let mut local_resources = Map::new();
let mut hardware_tasks = Map::new();
let mut software_tasks = Map::new();
let mut user_imports = vec![];
let mut user_code = vec![];
let mut seen_idents = HashSet::<Ident>::new();
let mut bindings = HashSet::<Ident>::new();
let mut check_binding = |ident: &Ident| {
if bindings.contains(ident) {
return Err(parse::Error::new(
ident.span(),
"this interrupt is already bound",
));
} else {
bindings.insert(ident.clone());
}
Ok(())
};
let mut check_ident = |ident: &Ident| {
if seen_idents.contains(ident) {
return Err(parse::Error::new(
ident.span(),
"this identifier has already been used",
));
} else {
seen_idents.insert(ident.clone());
}
Ok(())
};
for mut item in input.items {
match item {
Item::Fn(mut item) => {
let span = item.sig.ident.span();
if let Some(pos) = item
.attrs
.iter()
.position(|attr| util::attr_eq(attr, "init"))
{
let args = InitArgs::parse(item.attrs.remove(pos).tokens)?;
// If an init function already exists, error
if init.is_some() {
return Err(parse::Error::new(
span,
"`#[init]` function must appear at most once",
));
}
check_ident(&item.sig.ident)?;
init = Some(Init::parse(args, item)?);
} else if let Some(pos) = item
.attrs
.iter()
.position(|attr| util::attr_eq(attr, "idle"))
{
let args = IdleArgs::parse(item.attrs.remove(pos).tokens)?;
// If an idle function already exists, error
if idle.is_some() {
return Err(parse::Error::new(
span,
"`#[idle]` function must appear at most once",
));
}
check_ident(&item.sig.ident)?;
idle = Some(Idle::parse(args, item)?);
} else if let Some(pos) = item
.attrs
.iter()
.position(|attr| util::attr_eq(attr, "task"))
{
if hardware_tasks.contains_key(&item.sig.ident)
|| software_tasks.contains_key(&item.sig.ident)
{
return Err(parse::Error::new(
span,
"this task is defined multiple times",
));
}
match syntax_parse::task_args(item.attrs.remove(pos).tokens)? {
Either::Left(args) => {
check_binding(&args.binds)?;
check_ident(&item.sig.ident)?;
hardware_tasks.insert(
item.sig.ident.clone(),
HardwareTask::parse(args, item)?,
);
}
Either::Right(args) => {
check_ident(&item.sig.ident)?;
software_tasks.insert(
item.sig.ident.clone(),
SoftwareTask::parse(args, item)?,
);
}
}
} else {
// Forward normal functions
user_code.push(Item::Fn(item.clone()));
}
}
Item::Struct(ref mut struct_item) => {
// Match structures with the attribute #[shared], name of structure is not
// important
if let Some(_pos) = struct_item
.attrs
.iter()
.position(|attr| util::attr_eq(attr, "shared"))
{
let span = struct_item.ident.span();
shared_resources_ident = Some(struct_item.ident.clone());
if !shared_resources.is_empty() {
return Err(parse::Error::new(
span,
"`#[shared]` struct must appear at most once",
));
}
if struct_item.vis != Visibility::Inherited {
return Err(parse::Error::new(
struct_item.span(),
"this item must have inherited / private visibility",
));
}
if let Fields::Named(fields) = &mut struct_item.fields {
for field in &mut fields.named {
let ident = field.ident.as_ref().expect("UNREACHABLE");
if shared_resources.contains_key(ident) {
return Err(parse::Error::new(
ident.span(),
"this resource is listed more than once",
));
}
shared_resources.insert(
ident.clone(),
SharedResource::parse(field, ident.span())?,
);
}
} else {
return Err(parse::Error::new(
struct_item.span(),
"this `struct` must have named fields",
));
}
} else if let Some(_pos) = struct_item
.attrs
.iter()
.position(|attr| util::attr_eq(attr, "local"))
{
let span = struct_item.ident.span();
local_resources_ident = Some(struct_item.ident.clone());
if !local_resources.is_empty() {
return Err(parse::Error::new(
span,
"`#[local]` struct must appear at most once",
));
}
if struct_item.vis != Visibility::Inherited {
return Err(parse::Error::new(
struct_item.span(),
"this item must have inherited / private visibility",
));
}
if let Fields::Named(fields) = &mut struct_item.fields {
for field in &mut fields.named {
let ident = field.ident.as_ref().expect("UNREACHABLE");
if local_resources.contains_key(ident) {
return Err(parse::Error::new(
ident.span(),
"this resource is listed more than once",
));
}
local_resources.insert(
ident.clone(),
LocalResource::parse(field, ident.span())?,
);
}
} else {
return Err(parse::Error::new(
struct_item.span(),
"this `struct` must have named fields",
));
}
} else {
// Structure without the #[resources] attribute should just be passed along
user_code.push(item.clone());
}
}
Item::ForeignMod(mod_) => {
if !util::abi_is_rust(&mod_.abi) {
return Err(parse::Error::new(
mod_.abi.extern_token.span(),
"this `extern` block must use the \"Rust\" ABI",
));
}
for item in mod_.items {
if let ForeignItem::Fn(mut item) = item {
let span = item.sig.ident.span();
if let Some(pos) = item
.attrs
.iter()
.position(|attr| util::attr_eq(attr, "task"))
{
if hardware_tasks.contains_key(&item.sig.ident)
|| software_tasks.contains_key(&item.sig.ident)
{
return Err(parse::Error::new(
span,
"this task is defined multiple times",
));
}
if item.attrs.len() != 1 {
return Err(parse::Error::new(
span,
"`extern` task required `#[task(..)]` attribute",
));
}
match syntax_parse::task_args(item.attrs.remove(pos).tokens)? {
Either::Left(args) => {
check_binding(&args.binds)?;
check_ident(&item.sig.ident)?;
hardware_tasks.insert(
item.sig.ident.clone(),
HardwareTask::parse_foreign(args, item)?,
);
}
Either::Right(args) => {
check_ident(&item.sig.ident)?;
software_tasks.insert(
item.sig.ident.clone(),
SoftwareTask::parse_foreign(args, item)?,
);
}
}
} else {
return Err(parse::Error::new(
span,
"`extern` task required `#[task(..)]` attribute",
));
}
} else {
return Err(parse::Error::new(
item.span(),
"this item must live outside the `#[app]` module",
));
}
}
}
Item::Use(itemuse_) => {
// Store the user provided use-statements
user_imports.push(itemuse_.clone());
}
_ => {
// Anything else within the module should not make any difference
user_code.push(item.clone());
}
}
}
let shared_resources_ident =
shared_resources_ident.expect("No `#[shared]` resource struct defined");
let local_resources_ident =
local_resources_ident.expect("No `#[local]` resource struct defined");
let init = init.expect("No `#[init]` function defined");
if shared_resources_ident != init.user_shared_struct {
return Err(parse::Error::new(
init.user_shared_struct.span(),
format!(
"This name and the one defined on `#[shared]` are not the same. Should this be `{shared_resources_ident}`?"
),
));
}
if local_resources_ident != init.user_local_struct {
return Err(parse::Error::new(
init.user_local_struct.span(),
format!(
"This name and the one defined on `#[local]` are not the same. Should this be `{local_resources_ident}`?"
),
));
}
Ok(App {
args,
name: input.ident,
init,
idle,
shared_resources,
local_resources,
user_imports,
user_code,
hardware_tasks,
software_tasks,
})
}
}
|