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
use ::core::fmt;
use ::proc_macro2::{Span, TokenStream};
use ::quote::{ToTokens, TokenStreamExt};
use ::syn::{
parenthesized,
parse,
parse2,
token::{Comma, Paren},
Error,
Ident,
LitInt,
};
pub enum Int {
I8,
I16,
I32,
I64,
I128,
Isize,
U8,
U16,
U32,
U64,
U128,
Usize,
}
impl Int {
pub fn as_str(&self) -> &'static str {
match self {
Int::I8 => "i8",
Int::I16 => "i16",
Int::I32 => "i32",
Int::I64 => "i64",
Int::I128 => "i128",
Int::Isize => "isize",
Int::U8 => "u8",
Int::U16 => "u16",
Int::U32 => "u32",
Int::U64 => "u64",
Int::U128 => "u128",
Int::Usize => "usize",
}
}
}
impl ToTokens for Int {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
tokens.append(Ident::new(self.as_str(), Span::call_site()));
}
}
impl fmt::Display for Int {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
pub enum BaseKind {
C,
Transparent,
Primitive(Int),
}
impl BaseKind {
pub fn as_str(&self) -> &'static str {
match self {
BaseKind::C => "C",
BaseKind::Transparent => "transparent",
BaseKind::Primitive(int) => int.as_str(),
}
}
}
impl ToTokens for BaseKind {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
tokens.append(Ident::new(self.as_str(), Span::call_site()));
}
}
impl fmt::Display for BaseKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
pub struct Base {
pub kind: BaseKind,
pub kind_token: Ident,
}
pub struct PrimitiveType {
pub int: Int,
pub int_token: Ident,
}
pub struct ModifierArg {
pub paren_token: Paren,
pub value: LitInt,
}
impl parse::Parse for ModifierArg {
fn parse(input: parse::ParseStream) -> parse::Result<Self> {
let value;
let paren_token = parenthesized!(value in input);
Ok(Self {
paren_token,
value: value.parse::<LitInt>()?,
})
}
}
pub enum ModifierKind {
Align(ModifierArg),
Packed(Option<ModifierArg>),
}
impl fmt::Display for ModifierKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Align(ModifierArg { value, .. }) => {
write!(f, "align({value})")
}
Self::Packed(None) => write!(f, "packed"),
Self::Packed(Some(ModifierArg { value, .. })) => {
write!(f, "packed({value})")
}
}
}
}
pub struct Modifier {
pub kind: ModifierKind,
pub kind_token: Ident,
}
pub struct Repr {
pub paren_token: Paren,
pub base: Option<Base>,
pub primitive_type: Option<PrimitiveType>,
pub modifier: Option<Modifier>,
}
impl Repr {
pub fn merge_attr(
this: &mut Option<Self>,
tokens: TokenStream,
) -> Result<(), Error> {
let other = parse2(tokens)?;
if let Some(this) = this.as_mut() {
this.merge(other)?;
} else {
*this = Some(other);
}
Ok(())
}
pub fn merge(&mut self, other: Self) -> Result<(), Error> {
if let Some(self_base) = &self.base {
if let Some(other_base) = other.base {
return Err(Error::new_spanned(
other_base.kind_token,
format!("base repr `{}` already specified", self_base.kind),
));
}
} else {
self.base = other.base;
}
if let Some(self_primitive_type) = &self.primitive_type {
if let Some(other_primitive_type) = other.primitive_type {
return Err(Error::new_spanned(
other_primitive_type.int_token,
format!(
"primitive type `{}` already specified",
self_primitive_type.int,
),
));
}
} else {
self.primitive_type = other.primitive_type;
}
if let Some(self_modifier) = &self.modifier {
if let Some(other_modifier) = other.modifier {
return Err(Error::new_spanned(
other_modifier.kind_token,
format!(
"repr modifier `{}` already specified",
self_modifier.kind,
),
));
}
} else {
self.modifier = other.modifier;
}
Ok(())
}
}
impl parse::Parse for Repr {
fn parse(input: parse::ParseStream) -> parse::Result<Self> {
let mut base = None;
let mut primitive_type = None;
let mut try_set_base = |kind, kind_token| {
let add_base = Base { kind, kind_token };
match (base.take(), add_base) {
(None, add_base) => base = Some(add_base),
(
Some(Base {
kind: BaseKind::C,
kind_token,
}),
Base {
kind: BaseKind::Primitive(int),
kind_token: int_token,
},
)
| (
Some(Base {
kind: BaseKind::Primitive(int),
kind_token: int_token,
}),
Base {
kind: BaseKind::C,
kind_token,
},
) => {
base = Some(Base {
kind: BaseKind::C,
kind_token,
});
primitive_type = Some(PrimitiveType { int, int_token });
}
(Some(base), add_base) => {
return Err(Error::new_spanned(
add_base.kind_token,
format!("base repr `{}` already specified", base.kind),
));
}
}
Ok(())
};
let mut modifier = None;
let mut try_set_modifier = |kind, kind_token| {
modifier.replace(Modifier { kind, kind_token }).map_or(
Ok(()),
|old| {
Err(Error::new_spanned(
old.kind_token,
"repr modifier already specified",
))
},
)
};
let args;
let paren_token = parenthesized!(args in input);
while !args.is_empty() {
let token = args.parse::<Ident>()?;
if token == "C" {
try_set_base(BaseKind::C, token)?;
} else if token == "transparent" {
try_set_base(BaseKind::Transparent, token)?;
} else if token == "i8" {
try_set_base(BaseKind::Primitive(Int::I8), token)?;
} else if token == "i16" {
try_set_base(BaseKind::Primitive(Int::I16), token)?;
} else if token == "i32" {
try_set_base(BaseKind::Primitive(Int::I32), token)?;
} else if token == "i64" {
try_set_base(BaseKind::Primitive(Int::I64), token)?;
} else if token == "i128" {
try_set_base(BaseKind::Primitive(Int::I128), token)?;
} else if token == "isize" {
try_set_base(BaseKind::Primitive(Int::Isize), token)?;
} else if token == "u8" {
try_set_base(BaseKind::Primitive(Int::U8), token)?;
} else if token == "u16" {
try_set_base(BaseKind::Primitive(Int::U16), token)?;
} else if token == "u32" {
try_set_base(BaseKind::Primitive(Int::U32), token)?;
} else if token == "u64" {
try_set_base(BaseKind::Primitive(Int::U64), token)?;
} else if token == "u128" {
try_set_base(BaseKind::Primitive(Int::U128), token)?;
} else if token == "usize" {
try_set_base(BaseKind::Primitive(Int::Usize), token)?;
} else if token == "align" {
try_set_modifier(
ModifierKind::Align(args.parse::<ModifierArg>()?),
token,
)?;
} else if token == "packed" {
try_set_modifier(
ModifierKind::Packed(args.parse::<ModifierArg>().ok()),
token,
)?;
} else {
return Err(Error::new_spanned(token, "invalid repr argument"));
}
if args.peek(Comma) {
args.parse::<Comma>()?;
}
}
Ok(Repr {
paren_token,
base,
primitive_type,
modifier,
})
}
}