use std::process;
use proc_macro2::Span;
+use quote::format_ident;
+use syn::parse_quote;
mod types;
mod blocks;
writeln!(w, "/// Serialize the {} object into a byte array which can be read by {}_read", for_obj, for_obj).unwrap();
writeln!(w, "pub extern \"C\" fn {}_write(obj: &{}) -> crate::c_types::derived::CVec_u8Z {{", for_obj, full_obj_path).unwrap();
- let ref_type = syn::Type::Reference(syn::TypeReference {
- and_token: syn::Token!(&)(Span::call_site()), lifetime: None, mutability: None,
- elem: Box::new(for_ty.clone()) });
- assert!(!types.write_from_c_conversion_new_var(w, &syn::Ident::new("obj", Span::call_site()), &ref_type, Some(generics)));
+ let ref_type: syn::Type = syn::parse_quote!(&#for_ty);
+ assert!(!types.write_from_c_conversion_new_var(w, &format_ident!("obj"), &ref_type, Some(generics)));
write!(w, "\tcrate::c_types::serialize_obj(").unwrap();
types.write_from_c_conversion_prefix(w, &ref_type, Some(generics));
},
"lightning::util::ser::Readable"|"lightning::util::ser::ReadableArgs" => {
// Create the Result<Object, DecodeError> syn::Type
- let mut err_segs = syn::punctuated::Punctuated::new();
- err_segs.push(syn::PathSegment { ident: syn::Ident::new("ln", Span::call_site()), arguments: syn::PathArguments::None });
- err_segs.push(syn::PathSegment { ident: syn::Ident::new("msgs", Span::call_site()), arguments: syn::PathArguments::None });
- err_segs.push(syn::PathSegment { ident: syn::Ident::new("DecodeError", Span::call_site()), arguments: syn::PathArguments::None });
- let mut args = syn::punctuated::Punctuated::new();
- args.push(syn::GenericArgument::Type(for_ty.clone()));
- args.push(syn::GenericArgument::Type(syn::Type::Path(syn::TypePath {
- qself: None, path: syn::Path {
- leading_colon: Some(syn::Token![::](Span::call_site())), segments: err_segs,
- }
- })));
- let mut res_segs = syn::punctuated::Punctuated::new();
- res_segs.push(syn::PathSegment {
- ident: syn::Ident::new("Result", Span::call_site()),
- arguments: syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
- colon2_token: None, lt_token: syn::Token![<](Span::call_site()), args, gt_token: syn::Token![>](Span::call_site()),
- })
- });
- let res_ty = syn::Type::Path(syn::TypePath { qself: None, path: syn::Path {
- leading_colon: None, segments: res_segs } });
+ let res_ty: syn::Type = parse_quote!(Result<#for_ty, ::ln::msgs::DecodeError>);
writeln!(w, "#[no_mangle]").unwrap();
writeln!(w, "/// Read a {} from a byte array, created by {}_write", for_obj, for_obj).unwrap();
if let syn::GenericArgument::Type(args_ty) = args.args.iter().next().unwrap() {
types.write_c_type(w, args_ty, Some(generics), false);
- assert!(!types.write_from_c_conversion_new_var(&mut arg_conv, &syn::Ident::new("arg", Span::call_site()), &args_ty, Some(generics)));
+ assert!(!types.write_from_c_conversion_new_var(&mut arg_conv, &format_ident!("arg"), &args_ty, Some(generics)));
write!(&mut arg_conv, "\tlet arg_conv = ").unwrap();
types.write_from_c_conversion_prefix(&mut arg_conv, &args_ty, Some(generics));
writeln!(w, "\tlet res = crate::c_types::deserialize_obj(ser);").unwrap();
}
write!(w, "\t").unwrap();
- if types.write_to_c_conversion_new_var(w, &syn::Ident::new("res", Span::call_site()), &res_ty, Some(generics), false) {
+ if types.write_to_c_conversion_new_var(w, &format_ident!("res"), &res_ty, Some(generics), false) {
write!(w, "\n\t").unwrap();
}
types.write_to_c_conversion_inline_prefix(w, &res_ty, Some(generics), false);
write!(w, "#[no_mangle]\npub extern \"C\" fn {}_get_{}(this_ptr: &{}) -> ", struct_name, ident, struct_name).unwrap();
types.write_c_type(w, &ref_type, Some(&gen_types), true);
write!(w, " {{\n\tlet mut inner_val = &mut unsafe {{ &mut *this_ptr.inner }}.{};\n\t", ident).unwrap();
- let local_var = types.write_to_c_conversion_new_var(w, &syn::Ident::new("inner_val", Span::call_site()), &ref_type, Some(&gen_types), true);
+ let local_var = types.write_to_c_conversion_new_var(w, &format_ident!("inner_val"), &ref_type, Some(&gen_types), true);
if local_var { write!(w, "\n\t").unwrap(); }
types.write_to_c_conversion_inline_prefix(w, &ref_type, Some(&gen_types), true);
- if local_var {
- write!(w, "inner_val").unwrap();
- } else {
- write!(w, "(*inner_val)").unwrap();
- }
+ write!(w, "inner_val").unwrap();
types.write_to_c_conversion_inline_suffix(w, &ref_type, Some(&gen_types), true);
writeln!(w, "\n}}").unwrap();
}
write!(w, "#[no_mangle]\npub extern \"C\" fn {}_set_{}(this_ptr: &mut {}, mut val: ", struct_name, ident, struct_name).unwrap();
types.write_c_type(w, &field.ty, Some(&gen_types), false);
write!(w, ") {{\n\t").unwrap();
- let local_var = types.write_from_c_conversion_new_var(w, &syn::Ident::new("val", Span::call_site()), &field.ty, Some(&gen_types));
+ let local_var = types.write_from_c_conversion_new_var(w, &format_ident!("val"), &field.ty, Some(&gen_types));
if local_var { write!(w, "\n\t").unwrap(); }
write!(w, "unsafe {{ &mut *this_ptr.inner }}.{} = ", ident).unwrap();
types.write_from_c_conversion_prefix(w, &field.ty, Some(&gen_types));
}
write!(w, ") -> {} {{\n\t", struct_name).unwrap();
for field in fields.named.iter() {
- let field_name = format!("{}_arg", field.ident.as_ref().unwrap());
- if types.write_from_c_conversion_new_var(w, &syn::Ident::new(&field_name, Span::call_site()), &field.ty, Some(&gen_types)) {
+ let field_ident = format_ident!("{}_arg", field.ident.as_ref().unwrap());
+ if types.write_from_c_conversion_new_var(w, &field_ident, &field.ty, Some(&gen_types)) {
write!(w, "\n\t").unwrap();
}
}
if $ref {
write!(w, "let mut {}_nonref = (*{}).clone();\n\t\t\t\t", $field_ident, $field_ident).unwrap();
if new_var {
- let nonref_ident = syn::Ident::new(&format!("{}_nonref", $field_ident), Span::call_site());
+ let nonref_ident = format_ident!("{}_nonref", $field_ident);
if $to_c {
types.write_to_c_conversion_new_var(w, &nonref_ident, &$field.ty, None, false);
} else {
} else if let syn::Fields::Unnamed(fields) = &var.fields {
write!(w, " {{\n\t\t\t\t").unwrap();
for (idx, field) in fields.unnamed.iter().enumerate() {
- handle_field_a!(field, &syn::Ident::new(&(('a' as u8 + idx as u8) as char).to_string(), Span::call_site()));
+ handle_field_a!(field, &format_ident!("{}", ('a' as u8 + idx as u8) as char));
}
} else { write!(w, " ").unwrap(); }
write!(w, " (").unwrap();
for (idx, field) in fields.unnamed.iter().enumerate() {
write!(w, "\n\t\t\t\t\t").unwrap();
- handle_field_b!(field, &syn::Ident::new(&(('a' as u8 + idx as u8) as char).to_string(), Span::call_site()));
+ handle_field_b!(field, &format_ident!("{}", ('a' as u8 + idx as u8) as char));
}
writeln!(w, "\n\t\t\t\t)").unwrap();
write!(w, "\t\t\t}}").unwrap();
if process_alias {
match &*t.ty {
syn::Type::Path(p) => {
+ let t_ident = &t.ident;
+
// If its a path with no generics, assume we don't map the aliased type and map it opaque
- let mut segments = syn::punctuated::Punctuated::new();
- segments.push(syn::PathSegment {
- ident: t.ident.clone(),
- arguments: syn::PathArguments::None,
- });
- let path_obj = syn::Path { leading_colon: None, segments };
+ let path_obj = parse_quote!(#t_ident);
let args_obj = p.path.segments.last().unwrap().arguments.clone();
match crate_types.reverse_alias_map.entry(import_resolver.maybe_resolve_path(&p.path, None).unwrap()) {
hash_map::Entry::Occupied(mut e) => { e.get_mut().push((path_obj, args_obj)); },
hash_map::Entry::Vacant(e) => { e.insert(vec![(path_obj, args_obj)]); },
}
- crate_types.opaques.insert(type_path.clone(), &t.ident);
+ crate_types.opaques.insert(type_path, t_ident);
},
_ => {
crate_types.type_aliases.insert(type_path, import_resolver.resolve_imported_refs((*t.ty).clone()));