aboutsummaryrefslogtreecommitdiff
path: root/macros/src/codegen/shared_resources_struct.rs
blob: 61226517954e8da374d28c588afeace30bc748bf (plain) (blame)
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
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use rtic_syntax::{ast::App, Context};

use crate::codegen::util;

/// Generate shared resources structs
pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2, TokenStream2) {
    let mut lt = None;

    let resources = match ctxt {
        Context::Init => unreachable!("Tried to generate shared resources struct for init"),
        Context::Idle => &app.idle.as_ref().unwrap().args.shared_resources,
        Context::HardwareTask(name) => &app.hardware_tasks[name].args.shared_resources,
        Context::SoftwareTask(name) => &app.software_tasks[name].args.shared_resources,
    };

    let mut fields = vec![];
    let mut values = vec![];
    let mut has_cfgs = false;

    for (name, access) in resources {
        let res = app.shared_resources.get(name).expect("UNREACHABLE");

        let cfgs = &res.cfgs;
        has_cfgs |= !cfgs.is_empty();

        // access hold if the resource is [x] (exclusive) or [&x] (shared)
        let mut_ = if access.is_exclusive() {
            Some(quote!(mut))
        } else {
            None
        };
        let ty = &res.ty;
        let mangled_name = util::static_shared_resource_ident(&name);
        let shared_name = util::need_to_lock_ident(name);

        if !res.properties.lock_free {
            if access.is_shared() {
                lt = Some(quote!('a));

                fields.push(quote!(
                    #(#cfgs)*
                    pub #name: &'a #ty
                ));
            } else {
                // Resource proxy
                lt = Some(quote!('a));

                fields.push(quote!(
                    #(#cfgs)*
                    pub #name: shared_resources::#shared_name<'a>
                ));

                values.push(quote!(
                    #(#cfgs)*
                    #name: shared_resources::#shared_name::new(priority)

                ));

                // continue as the value has been filled,
                continue;
            }
        } else {
            let lt = if ctxt.runs_once() {
                quote!('static)
            } else {
                lt = Some(quote!('a));
                quote!('a)
            };

            fields.push(quote!(
                #(#cfgs)*
                pub #name: &#lt #mut_ #ty
            ));
        }

        let expr = if access.is_exclusive() {
            quote!(&mut *(&mut *#mangled_name.get_mut()).as_mut_ptr())
        } else {
            quote!(&*(&*#mangled_name.get()).as_ptr())
        };

        values.push(quote!(
            #(#cfgs)*
            #name: #expr
        ));
    }

    if lt.is_some() {
        *needs_lt = true;

        // The struct could end up empty due to `cfg`s leading to an error due to `'a` being unused
        if has_cfgs {
            fields.push(quote!(
                #[doc(hidden)]
                pub __marker__: core::marker::PhantomData<&'a ()>
            ));

            values.push(quote!(__marker__: core::marker::PhantomData))
        }
    }

    let doc = format!("Shared resources `{}` has access to", ctxt.ident(app));
    let ident = util::shared_resources_ident(ctxt, app);
    let item = quote!(
        #[allow(non_snake_case)]
        #[allow(non_camel_case_types)]
        #[doc = #doc]
        pub struct #ident<#lt> {
            #(#fields,)*
        }
    );

    let arg = if ctxt.is_init() {
        None
    } else {
        Some(quote!(priority: &#lt rtic::export::Priority))
    };
    let constructor = quote!(
        impl<#lt> #ident<#lt> {
            #[inline(always)]
            pub unsafe fn new(#arg) -> Self {
                #ident {
                    #(#values,)*
                }
            }
        }
    );

    (item, constructor)
}
'>Little more clarify readmeGravatar Jarred Sumner 1-2/+4 2021-09-17Begin to add integration testsGravatar Jarred Sumner 21-1/+534 2021-09-17Fix bugs with ESM -> CJS when not bundledGravatar Jarred Sumner 7-147/+334 2021-09-16Do not attempt to HMR export {value} from, just assume it will be HMR'd (or not)Gravatar Jarred Sumner 1-10/+38 2021-09-16Update options.zigGravatar Jarred Sumner 1-1/+1 2021-09-16Fix export * as fromGravatar Jarred Sumner 1-1/+2 2021-09-16Add flag to dev server to disable HMRGravatar Jarred Sumner 5-1885/+2044 2021-09-16Help output should print command namesGravatar Jarred Sumner 1-5/+13 2021-09-16Fix symbol name for cjs2esmGravatar Jarred Sumner 1-0/+1 2021-09-16When port is in use, auto-increment port number up to 10 times and then bail ↵Gravatar Jarred Sumner 1-7/+43 if all 10 are in use 2021-09-16Always bold ^Gravatar Jarred Sumner 1-0/+2 2021-09-16Delete some dead codeGravatar Jarred Sumner 2-538/+0 2021-09-16Print absolute paths in log errors so that ctrl+click to open file in editor ↵Gravatar Jarred Sumner 1-1/+1 works (depending on terminal) 2021-09-16Colorize build/resolve errors and add a ^Gravatar Jarred Sumner 7-127/+210 2021-09-16Commit build idGravatar Jarred Sumner 3-3/+3 2021-09-16Automatically rewrite TS import paths from .jsx? -> .tsx? when .jsx? is not ↵bun-v0.0.16Gravatar Jarred Sumner 1-8/+10 found This was already partially implemented but it was returning filenames instead of absolute paths. This matches the behavior from https://github.com/microsoft/TypeScript/issues/4595. 2021-09-15write the versionGravatar Jarred Sumner 1-1/+1 2021-09-15Switch to 0.0.x instead of 0.0.0-x to fix the npm install issueGravatar Jarred Sumner 6-9/+11 2021-09-15Bump versionsbun-v0.0.15bun-v0.0.0-15Gravatar Jarred Sumner 2-2/+2 2021-09-15Bumpbun-v0.0.0-14Gravatar Jarred Sumner 1-1/+1 2021-09-15Fix require bug in runtime.jsGravatar Jarred Sumner 2-31/+10 2021-09-15Fix CJS symbol namesGravatar Jarred Sumner 1-11/+37 2021-09-15Remove most usages of anytype in js_ast.zigGravatar Jarred Sumner 4-72/+74 2021-09-15Fix crash in `bun bun`Gravatar Jarred Sumner 1-24/+26 2021-09-15woopsGravatar Jarred Sumner 4-6/+6 2021-09-15Bumpbun-v0.0.0-13Gravatar Jarred Sumner 5-10/+12