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
#![doc(
html_logo_url = "https://github.com/vertexclique/cuneiform/raw/master/img/cuneiform-logo.png"
)]
#![warn(missing_docs)]
#![warn(missing_debug_implementations)]
#![forbid(unsafe_code)]
extern crate proc_macro;
use self::proc_macro::TokenStream;
use heapless::consts::*;
use heapless::String;
use quote::*;
use syn::parse::{Parse, ParseStream, Result};
use syn::{parse_macro_input, DeriveInput, Index, LitBool, LitInt, LitStr, Token};
mod detection;
mod slabs;
pub(crate) struct CuneiformArgs {
pub(crate) hermetic: bool,
pub(crate) slab: String<U64>,
pub(crate) force: isize,
}
impl CuneiformArgs {
fn new() -> Self {
CuneiformArgs {
hermetic: true,
slab: String::from(""),
force: !0,
}
}
fn with_hermetic(&mut self, hermetic: bool) {
self.hermetic = hermetic;
}
fn with_slab(&mut self, slab: String<U64>) {
self.slab = slab;
}
fn with_force(&mut self, force: isize) {
self.force = force;
}
}
mod cunei_keywords {
syn::custom_keyword!(hermetic);
syn::custom_keyword!(slab);
syn::custom_keyword!(force);
}
impl Parse for CuneiformArgs {
fn parse(input: ParseStream) -> Result<Self> {
let mut cargs = CuneiformArgs::new();
if input.parse::<cunei_keywords::hermetic>().is_ok() {
input.parse::<Token![=]>()?;
let hermetic: LitBool = input.parse()?;
cargs.with_hermetic(hermetic.value);
}
if input.parse::<cunei_keywords::slab>().is_ok() {
input.parse::<Token![=]>()?;
let slab: LitStr = input.parse()?;
cargs.with_slab(String::from(slab.value().as_str()));
}
if input.parse::<cunei_keywords::force>().is_ok() {
input.parse::<Token![=]>()?;
let force_lit: LitInt = input.parse()?;
let force = force_lit.base10_parse::<isize>()?;
cargs.with_force(force);
}
Ok(cargs)
}
}
#[proc_macro_attribute]
pub fn cuneiform(args: TokenStream, input: TokenStream) -> TokenStream {
let pargs = parse_macro_input!(args as CuneiformArgs);
let input = parse_macro_input!(input as DeriveInput);
let frep = crate::slabs::fetch(pargs);
let frep = Index::from(frep as usize);
TokenStream::from(quote! {
#[repr(align(#frep))]
#input
})
}