use std::fs::File;
use std::io::Write;
-use proc_macro2::{TokenTree, Span};
+
+use proc_macro2::TokenTree;
+use quote::format_ident;
use crate::types::*;
/// mut ret = " assuming the next print will be the unmapped Rust function to call followed by the
/// parameters we mapped to/from C here.
pub fn write_method_var_decl_body<W: std::io::Write>(w: &mut W, sig: &syn::Signature, extra_indent: &str, types: &TypeResolver, generics: Option<&GenericTypes>, to_c: bool) {
- let mut num_unused = 0;
+ let mut num_unused = 0u32;
for inp in sig.inputs.iter() {
match inp {
syn::FnArg::Receiver(_) => {},
},
syn::Pat::Wild(w) => {
if !w.attrs.is_empty() { unimplemented!(); }
- write_new_var!(syn::Ident::new(&format!("unused_{}", num_unused), Span::call_site()), *arg.ty);
+ write_new_var!(format_ident!("unused_{}", num_unused), *arg.ty);
num_unused += 1;
},
_ => unimplemented!(),
// If we're returning "Self" (and not "Self::X"), just do it manually
write!(w, "{} {{ inner: Box::into_raw(Box::new(ret)), is_owned: true }}", this_type).unwrap();
} else if to_c {
- let new_var = types.write_from_c_conversion_new_var(w, &syn::Ident::new("ret", Span::call_site()), rtype, generics);
+ let new_var = types.write_from_c_conversion_new_var(w, &format_ident!("ret"), rtype, generics);
if new_var {
write!(w, "\n\t{}", extra_indent).unwrap();
}
types.write_from_c_conversion_suffix(w, &*rtype, generics);
} else {
let ret_returned = if let syn::Type::Reference(_) = &**rtype { true } else { false };
- let new_var = types.write_to_c_conversion_new_var(w, &syn::Ident::new("ret", Span::call_site()), &rtype, generics, true);
+ let new_var = types.write_to_c_conversion_new_var(w, &format_ident!("ret"), &rtype, generics, true);
if new_var {
write!(w, "\n\t{}", extra_indent).unwrap();
}
use std::process;
use proc_macro2::Span;
+use quote::format_ident;
use syn::parse_quote;
mod types;
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 = syn::parse_quote!(&#for_ty);
- assert!(!types.write_from_c_conversion_new_var(w, &syn::Ident::new("obj", Span::call_site()), &ref_type, Some(generics)));
+ 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));
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, "#[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();
use crate::blocks::*;
use proc_macro2::{TokenTree, Span};
+use quote::format_ident;
use syn::parse_quote;
// The following utils are used purely to build our known types maps - they break down all the
($ident: expr, $path_suffix: expr) => {
if partial_path == "" && !dependencies.contains(&$ident) {
new_path = format!("{}::{}{}", crate_name, $ident, $path_suffix);
- path.push(syn::PathSegment { ident: syn::Ident::new(crate_name, Span::call_site()), arguments: syn::PathArguments::None });
+ path.push(syn::PathSegment { ident: format_ident!("{}", crate_name), arguments: syn::PathArguments::None });
} else {
new_path = format!("{}{}{}", partial_path, $ident, $path_suffix);
}
}
fn insert_primitive(imports: &mut HashMap<syn::Ident, (String, syn::Path)>, id: &str) {
- let ident = syn::Ident::new(id, Span::call_site());
+ let ident = format_ident!("{}", id);
let mut path = syn::punctuated::Punctuated::new();
path.push(syn::PathSegment { ident: ident.clone(), arguments: syn::PathArguments::None });
imports.insert(ident, (id.to_owned(), syn::Path { leading_colon: None, segments: path }));
write!(w, "{} {{ ", pfx).unwrap();
let new_var_name = format!("{}_{}", ident, idx);
- let new_var = self.write_conversion_new_var_intern(w, &syn::Ident::new(&new_var_name, Span::call_site()),
+ let new_var = self.write_conversion_new_var_intern(w, &format_ident!("{}", new_var_name),
&var_access, conv_ty, generics, contains_slice || (is_ref && ty_has_inner), ptr_for_ref, to_c, path_lookup, container_lookup, var_prefix, var_suffix);
if new_var { write!(w, " ").unwrap(); }
for (idx, elem) in t.elems.iter().enumerate() {
if let syn::Type::Path(p) = elem {
let v_name = format!("orig_{}_{}", ident, idx);
- let tuple_elem_ident = syn::Ident::new(&v_name, Span::call_site());
+ let tuple_elem_ident = format_ident!("{}", &v_name);
if self.write_conversion_new_var_intern(w, &tuple_elem_ident, &v_name, elem, generics,
false, ptr_for_ref, to_c,
path_lookup, container_lookup, var_prefix, var_suffix) {