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
#![recursion_limit="128"]

extern crate regex;
extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;

use regex::Regex;
use proc_macro::TokenStream;
use syn::Body;
use syn::Ident;
use quote::Tokens;

enum UavcanType {
    PrimitiveType,
    DynamicArray,
    StaticArray,
    Struct,
}


#[proc_macro_derive(UavcanStruct, attributes(DSDLSignature, DataTypeSignature, UavcanCrateName))]
pub fn uavcan_sized(input: TokenStream) -> TokenStream {
    let s = input.to_string();
    let ast = syn::parse_macro_input(&s).unwrap();
    let gen = impl_uavcan_struct(&ast);
    gen.parse().unwrap()
}

fn impl_uavcan_struct(ast: &syn::DeriveInput) -> quote::Tokens {
    let name = &ast.ident;

    // first handle the attributes
    let mut dsdl_signature = quote!{0x00};
    let mut data_type_signature = quote!{0x00};
    let mut crate_name = quote!{uavcan};
    
    for attr in &ast.attrs {
        if let syn::MetaItem::NameValue(ref ident, ref lit) = attr.value {
            if ident == "DSDLSignature" {
                if let syn::Lit::Str(ref lit_str, _) = *lit {
                    let value = Ident::from(lit_str.clone()); // hack needed since only string literals is supported for attributes
                    dsdl_signature = quote!{#value};
                } else {
                    panic!("DSDLSignature must be on the form \"0x123456789abc\"");
                }
            } else if ident == "DataTypeSignature" {
                if let syn::Lit::Str(ref lit_str, _) = *lit {
                    let value = Ident::from(lit_str.clone()); // hack needed since only string literals is supported for attributes
                    data_type_signature = quote!{#value};
                } else {
                    panic!("Data type signature must be on the form \"0x123456789abc\"");
                }
            } else if ident == "UavcanCrateName" {
                if let syn::Lit::Str(ref lit_str, _) = *lit {
                    let value = Ident::from(lit_str.clone()); // hack needed since only string literals is supported for attributes
                    crate_name = quote!{#value};
                } else {
                    panic!("Crate name must be on the form \"uavcan_alternative\"");
                }
            }

        }
    }

    let mut bit_length_min = Tokens::new();
    let mut flattened_fields = Tokens::new();
    let mut serialize_body = Tokens::new();
    let mut deserialize_body = Tokens::new();
    
    match ast.body {
        Body::Enum(ref variants) => {
            // MIN and MAX bit length for enums is not implemented yet
            bit_length_min.append(quote!{0});

            flattened_fields.append(quote!{0});
            
            for variant in variants {
                if variant.data.fields().len() != 1 {
                    panic!("Enum variants must have exactly one field");
                } else if let Some(field) = variant.data.fields().first() {
                    let field_type = &field.ty;
                    
                    match classify_type(field_type) {
                        UavcanType::PrimitiveType | UavcanType::DynamicArray | UavcanType::StaticArray => flattened_fields.append(quote!{ + 1}),
                        UavcanType::Struct => flattened_fields.append(quote!{ + #field_type::FLATTENED_FIELDS_NUMBER}),
                    }
                }
            }

            serialize_body = quote!(unimplemented!("Serialization is not implemented for enum yet"));
            deserialize_body = quote!(unimplemented!("Serialization is not implemented for enum yet"));

        },
        Body::Struct(syn::VariantData::Struct(ref fields)) => {
            let mut field_index = Tokens::new();
            
            bit_length_min.append(quote!{0});
            flattened_fields.append(quote!{0});
            field_index.append(quote!{0});
            
            for (i, field) in fields.iter().enumerate() {
                let field_ident = &field.ident;
                let field_type = &field.ty;

                let last_field = if i == fields.len()-1 {
                    quote!{true}
                } else {
                    quote!{false}
                };
                
                
                match classify_type(field_type) {
                    UavcanType::PrimitiveType => bit_length_min.append(quote!{ + <#field_type as ::#crate_name::Serializable>::BIT_LENGTH_MIN}),
                    UavcanType::StaticArray => bit_length_min.append(quote!{ + <#field_type as ::#crate_name::Serializable>::BIT_LENGTH_MIN}),
                    UavcanType::DynamicArray => {
                        let array_type = array_from_dynamic(field_type);
                        bit_length_min.append(quote!{ + <::#crate_name::types::Dynamic<#array_type> as ::#crate_name::Serializable>::BIT_LENGTH_MIN});
                    },
                    UavcanType::Struct => bit_length_min.append(quote!{ + <#field_type as ::#crate_name::Serializable>::BIT_LENGTH_MIN}),
                }
                
                match classify_type(field_type) {
                    UavcanType::PrimitiveType => flattened_fields.append(quote!{ + 1}),
                    UavcanType::StaticArray => flattened_fields.append(quote!{ + <#field_type as ::#crate_name::Serializable>::FLATTENED_FIELDS_NUMBER}),
                    UavcanType::DynamicArray => {
                        let array_type = array_from_dynamic(field_type);
                        flattened_fields.append(quote!{ + <::#crate_name::types::Dynamic<#array_type> as ::#crate_name::Serializable>::FLATTENED_FIELDS_NUMBER});
                    },
                    UavcanType::Struct => flattened_fields.append(quote!{ + <#field_type as ::#crate_name::Serializable>::FLATTENED_FIELDS_NUMBER}),
                }
            

                if i != 0 { serialize_body.append(quote!{ else });}
                if i != 0 { deserialize_body.append(quote!{ else });}
                
                let field_length = match classify_type(field_type) {
                    UavcanType::PrimitiveType => quote!(1),
                    UavcanType::StaticArray => quote!(<#field_type as ::#crate_name::Serializable>::FLATTENED_FIELDS_NUMBER),
                    UavcanType::DynamicArray => {
                        let array_type = array_from_dynamic(field_type);
                        quote!{<::#crate_name::types::Dynamic<#array_type> as ::#crate_name::Serializable>::FLATTENED_FIELDS_NUMBER}
                    },
                    UavcanType::Struct => quote!{<#field_type as ::#crate_name::Serializable>::FLATTENED_FIELDS_NUMBER},
                };
                
                serialize_body.append(quote!{if *flattened_field >= (#field_index) && *flattened_field < (#field_index) + #field_length {
                    let mut current_field = *flattened_field - (#field_index);
                    if ::#crate_name::Serializable::serialize(&self.#field_ident, &mut current_field, bit, #last_field && last_field, buffer) == ::#crate_name::SerializationResult::Finished {
                        *flattened_field = (#field_index) + current_field;
                        *bit = 0;
                    } else {
                        *flattened_field = (#field_index) + current_field;
                        return ::#crate_name::SerializationResult::BufferFull;
                    }
                }});

                deserialize_body.append(quote!{if *flattened_field >= (#field_index) && *flattened_field < (#field_index) + #field_length {
                    let mut current_field = *flattened_field - (#field_index);
                    if ::#crate_name::Serializable::deserialize(&mut self.#field_ident, &mut current_field, bit, #last_field && last_field, buffer) == ::#crate_name::DeserializationResult::Finished {
                        *flattened_field = (#field_index) + current_field;
                        *bit = 0;
                    } else {
                        *flattened_field = (#field_index) + current_field;
                        return ::#crate_name::DeserializationResult::BufferInsufficient;
                    }
                }});
                
                field_index.append(quote!{ + #field_length});
            }
        },
        Body::Struct(syn::VariantData::Unit) => {
            bit_length_min = quote!(0);
            flattened_fields = quote!(0);
            serialize_body = quote!{
                assert_eq!(*flattened_fields, 0);
                *bit = 0;
                *flattened_fields = 1;
            };
            deserialize_body = quote!{
                assert_eq!(*flattened_fields, 0);
                *bit = 0;
                *flattened_fields = 1;
            };

        },
        _ => panic!("UavcanStruct is only derivable for enums and named structs"),
    };

    
    quote!{
        impl ::#crate_name::Struct for #name {
            const DSDL_SIGNATURE: u64 = #dsdl_signature;
            const DATA_TYPE_SIGNATURE: u64 = #data_type_signature;
        }

        impl ::#crate_name::Serializable for #name {
            const BIT_LENGTH_MIN: usize = #bit_length_min;
            const FLATTENED_FIELDS_NUMBER: usize = #flattened_fields;
            #[allow(unused_comparisons)]
            #[allow(unused_variables)]
            fn serialize(&self, flattened_field: &mut usize, bit: &mut usize, last_field: bool, buffer: &mut ::#crate_name::SerializationBuffer) -> ::#crate_name::SerializationResult {
                assert!(*flattened_field < Self::FLATTENED_FIELDS_NUMBER);
                while *flattened_field != Self::FLATTENED_FIELDS_NUMBER{
                    assert!(*flattened_field < Self::FLATTENED_FIELDS_NUMBER);
                    #serialize_body
                }
                ::#crate_name::SerializationResult::Finished
            }

            #[allow(unused_comparisons)]
            #[allow(unused_variables)]
            fn deserialize(&mut self, flattened_field: &mut usize, bit: &mut usize, last_field: bool, buffer: &mut ::#crate_name::DeserializationBuffer) -> ::#crate_name::DeserializationResult {
                assert!(*flattened_field < Self::FLATTENED_FIELDS_NUMBER);
                while *flattened_field != Self::FLATTENED_FIELDS_NUMBER{
                    assert!(*flattened_field < Self::FLATTENED_FIELDS_NUMBER);
                    #deserialize_body
                }
                ::#crate_name::DeserializationResult::Finished
            }


       }

    }
}

fn classify_type(ty: &syn::Ty) -> UavcanType {
    if is_primitive_type(ty) {
        UavcanType::PrimitiveType
    } else if is_dynamic_array(ty) {
        UavcanType::DynamicArray
    } else if is_static_array(ty) {
        UavcanType::StaticArray
    } else {
        UavcanType::Struct
    }
}

fn is_primitive_type(ty: &syn::Ty) -> bool {
    is_unsigned_primitive_type(ty) || is_signed_primitive_type(ty) || is_void_primitive_type(ty) || is_float_primitive_type(ty) || is_bool_primitive_type(ty)
}

fn is_bool_primitive_type(ty: &syn::Ty) -> bool {
    if let syn::Ty::Path(_, ref path) = *ty {
        let re = Regex::new(r"bool").unwrap();
        re.is_match(path.segments.as_slice().last().unwrap().ident.as_ref())
    } else {
        false
    }
}

fn is_unsigned_primitive_type(ty: &syn::Ty) -> bool {
    if let syn::Ty::Path(_, ref path) = *ty {
        let re = Regex::new(r"u([2-9]|[1-5][0-9]|6[0-4])").unwrap();
        re.is_match(path.segments.as_slice().last().unwrap().ident.as_ref())
    } else {
        false
    }
}

fn is_signed_primitive_type(ty: &syn::Ty) -> bool {
    if let syn::Ty::Path(_, ref path) = *ty {
        let re = Regex::new(r"i([2-9]|[1-5][0-9]|6[0-4])").unwrap();
        re.is_match(path.segments.as_slice().last().unwrap().ident.as_ref())
    } else {
        false
    }
}

fn is_float_primitive_type(ty: &syn::Ty) -> bool {
    if let syn::Ty::Path(_, ref path) = *ty {
        let re = Regex::new(r"f(16)|(32)|64").unwrap();
        re.is_match(path.segments.as_slice().last().unwrap().ident.as_ref())
    } else {
        false
    }
}

fn is_void_primitive_type(ty: &syn::Ty) -> bool {
    if let syn::Ty::Path(_, ref path) = *ty {
        let re = Regex::new(r"void([1-9]|[1-5][0-9]|6[0-4])").unwrap();
        re.is_match(path.segments.as_slice().last().unwrap().ident.as_ref())
    } else {
        false
    }
}

fn is_static_array(ty: &syn::Ty) -> bool {
    if let syn::Ty::Array(_, _) = *ty {
        true
    } else {
        false
    }
}

fn is_dynamic_array(type_name: &syn::Ty) -> bool {
    if let syn::Ty::Path(_, ref path) = *type_name {
        if path.segments.as_slice().last().unwrap().ident == syn::parse::ident("Dynamic").expect("") {
            return true;
        }
    }
    false
}

fn array_from_dynamic(type_name: &syn::Ty) -> Option<syn::Ty> {
    if let syn::Ty::Path(_, ref path) = *type_name {
        if path.segments.as_slice().last().unwrap().ident == syn::Ident::from("Dynamic") {
            if let syn::PathSegment{
                parameters: syn::PathParameters::AngleBracketed(syn::AngleBracketedParameterData{
                    ref types, ..
                }), ..
            } = *path.segments.as_slice().last().unwrap() {
                return Some(types[0].clone());
            }
        }
    }
    None
}