}
}
- /// Attempt to resolve an Ident as a generic parameter and return the full path.
- pub fn maybe_resolve_ident<'b>(&'b self, ident: &syn::Ident) -> Option<&'b String> {
- if let Some(ty) = &self.self_ty {
- if format!("{}", ident) == "Self" {
- return Some(&ty);
- }
- }
- if let Some(res) = self.typed_generics.get(ident) {
- return Some(res);
- }
- if let Some(parent) = self.parent {
- parent.maybe_resolve_ident(ident)
- } else {
- None
- }
- }
-
/// Attempt to resolve a Path as a generic parameter and return the full path. as both a string
/// and syn::Path.
pub fn maybe_resolve_path<'b>(&'b self, path: &syn::Path) -> Option<&'b String> {
if let syn::Type::Reference(r_arg) = t {
assert!(!is_ref); // We don't currently support outer reference types for non-primitive inners
- if !self.write_c_type_intern(w, &*r_arg.elem, generics, false, false, false) { return false; }
+ if !self.write_c_type_intern(w, &*r_arg.elem, generics, false, false, false, false) { return false; }
// While write_c_type_intern, above is correct, we don't want to blindly convert a
// reference to something stupid, so check that the container is either opaque or a
} else {
assert!(!is_ref); // We don't currently support outer reference types for non-primitive inners
}
- if !self.write_c_type_intern(w, t, generics, false, false, false) { return false; }
+ if !self.write_c_type_intern(w, t, generics, false, false, false, false) { return false; }
} else {
assert!(!is_ref); // We don't currently support outer reference types for non-primitive inners
- if !self.write_c_type_intern(w, t, generics, false, false, false) { return false; }
+ if !self.write_c_type_intern(w, t, generics, false, false, false, false) { return false; }
}
}
true
if self.is_transparent_container(ident, is_ref, args.iter().map(|a| *a), generics) {
if !in_type {
if self.c_type_has_inner_from_path(&subtype) {
- if !self.write_c_path_intern(w, &$p_arg.path, generics, is_ref, is_mut, ptr_for_ref) { return false; }
+ if !self.write_c_path_intern(w, &$p_arg.path, generics, is_ref, is_mut, ptr_for_ref, false) { return false; }
} else {
if let Some(arr_ty) = self.is_real_type_array(&subtype) {
- if !self.write_c_type_intern(w, &arr_ty, generics, false, true, false) { return false; }
+ if !self.write_c_type_intern(w, &arr_ty, generics, false, true, false, false) { return false; }
} else {
// Option<T> needs to be converted to a *mut T, ie mut ptr-for-ref
- if !self.write_c_path_intern(w, &$p_arg.path, generics, true, true, true) { return false; }
+ if !self.write_c_path_intern(w, &$p_arg.path, generics, true, true, true, false) { return false; }
}
}
} else {
// *** C Type Equivalent Printing ***
// **********************************
- fn write_c_path_intern<W: std::io::Write>(&self, w: &mut W, path: &syn::Path, generics: Option<&GenericTypes>, is_ref: bool, is_mut: bool, ptr_for_ref: bool) -> bool {
+ fn write_c_path_intern<W: std::io::Write>(&self, w: &mut W, path: &syn::Path, generics: Option<&GenericTypes>, is_ref: bool, is_mut: bool, ptr_for_ref: bool, with_ref_lifetime: bool) -> bool {
let full_path = match self.maybe_resolve_path(&path, generics) {
Some(path) => path, None => return false };
if let Some(c_type) = self.c_type_from_path(&full_path, is_ref, ptr_for_ref) {
if is_ref && ptr_for_ref {
write!(w, "*{} crate::{}", if is_mut { "mut" } else { "const" }, full_path).unwrap();
} else if is_ref {
+ if with_ref_lifetime { unimplemented!(); }
write!(w, "&{}crate::{}", if is_mut { "mut " } else { "" }, full_path).unwrap();
} else {
write!(w, "crate::{}", full_path).unwrap();
// the actual object itself (for opaque types we'll set the pointer to the actual
// type and note that its a reference).
write!(w, "crate::{}", full_path).unwrap();
+ } else if is_ref && with_ref_lifetime {
+ assert!(!is_mut);
+ // If we're concretizing something with a lifetime parameter, we have to pick a
+ // lifetime, of which the only real available choice is `static`, obviously.
+ write!(w, "&'static ").unwrap();
+ self.write_rust_path(w, generics, path);
} else if is_ref {
write!(w, "&{}crate::{}", if is_mut { "mut " } else { "" }, full_path).unwrap();
} else {
false
}
}
- fn write_c_type_intern<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>, is_ref: bool, is_mut: bool, ptr_for_ref: bool) -> bool {
+ fn write_c_type_intern<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>, is_ref: bool, is_mut: bool, ptr_for_ref: bool, with_ref_lifetime: bool) -> bool {
match generics.resolve_type(t) {
syn::Type::Path(p) => {
if p.qself.is_some() {
return self.write_c_mangled_container_path(w, Self::path_to_generic_args(&p.path), generics, &full_path, is_ref, is_mut, ptr_for_ref);
}
if let Some(aliased_type) = self.crate_types.type_aliases.get(&full_path).cloned() {
- return self.write_c_type_intern(w, &aliased_type, None, is_ref, is_mut, ptr_for_ref);
+ return self.write_c_type_intern(w, &aliased_type, None, is_ref, is_mut, ptr_for_ref, with_ref_lifetime);
}
}
- self.write_c_path_intern(w, &p.path, generics, is_ref, is_mut, ptr_for_ref)
+ self.write_c_path_intern(w, &p.path, generics, is_ref, is_mut, ptr_for_ref, with_ref_lifetime)
},
syn::Type::Reference(r) => {
- self.write_c_type_intern(w, &*r.elem, generics, true, r.mutability.is_some(), ptr_for_ref)
+ self.write_c_type_intern(w, &*r.elem, generics, true, r.mutability.is_some(), ptr_for_ref, with_ref_lifetime)
},
syn::Type::Array(a) => {
if is_ref && is_mut {
write!(w, "*mut [").unwrap();
- if !self.write_c_type_intern(w, &a.elem, generics, false, false, ptr_for_ref) { return false; }
+ if !self.write_c_type_intern(w, &a.elem, generics, false, false, ptr_for_ref, with_ref_lifetime) { return false; }
} else if is_ref {
write!(w, "*const [").unwrap();
- if !self.write_c_type_intern(w, &a.elem, generics, false, false, ptr_for_ref) { return false; }
+ if !self.write_c_type_intern(w, &a.elem, generics, false, false, ptr_for_ref, with_ref_lifetime) { return false; }
} else {
let mut typecheck = Vec::new();
- if !self.write_c_type_intern(&mut typecheck, &a.elem, generics, false, false, ptr_for_ref) { return false; }
+ if !self.write_c_type_intern(&mut typecheck, &a.elem, generics, false, false, ptr_for_ref, with_ref_lifetime) { return false; }
if typecheck[..] != ['u' as u8, '8' as u8] { return false; }
}
if let syn::Expr::Lit(l) = &a.len {
args.push(syn::GenericArgument::Type((*s.elem).clone()));
let mut segments = syn::punctuated::Punctuated::new();
segments.push(parse_quote!(Vec<#args>));
- self.write_c_type_intern(w, &syn::Type::Path(syn::TypePath { qself: None, path: syn::Path { leading_colon: None, segments } }), generics, false, is_mut, ptr_for_ref)
+ self.write_c_type_intern(w, &syn::Type::Path(syn::TypePath { qself: None, path: syn::Path { leading_colon: None, segments } }), generics, false, is_mut, ptr_for_ref, with_ref_lifetime)
} else { false }
},
syn::Type::Tuple(t) => {
}
}
pub fn write_c_type<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>, ptr_for_ref: bool) {
- assert!(self.write_c_type_intern(w, t, generics, false, false, ptr_for_ref));
+ assert!(self.write_c_type_intern(w, t, generics, false, false, ptr_for_ref, false));
+ }
+ pub fn write_c_type_in_generic_param<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, generics: Option<&GenericTypes>, ptr_for_ref: bool) {
+ assert!(self.write_c_type_intern(w, t, generics, false, false, ptr_for_ref, true));
}
pub fn understood_c_path(&self, p: &syn::Path) -> bool {
if p.leading_colon.is_some() { return false; }
- self.write_c_path_intern(&mut std::io::sink(), p, None, false, false, false)
+ self.write_c_path_intern(&mut std::io::sink(), p, None, false, false, false, false)
}
pub fn understood_c_type(&self, t: &syn::Type, generics: Option<&GenericTypes>) -> bool {
- self.write_c_type_intern(&mut std::io::sink(), t, generics, false, false, false)
+ self.write_c_type_intern(&mut std::io::sink(), t, generics, false, false, false, false)
}
}