]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Support printing impl generics without an immediate bound
authorMatt Corallo <git@bluematt.me>
Sun, 31 Oct 2021 18:06:42 +0000 (18:06 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 2 Nov 2021 16:54:45 +0000 (16:54 +0000)
(e.g. if you have `fn <A> ... where A: Trait` we'd not have printed
`<A>`)

This further supports printing impl generics with a reference to a
struct (not a trait), which we concretize with a regular reference.

c-bindings-gen/src/blocks.rs
c-bindings-gen/src/types.rs

index b214445584b6f6439413f8e5c7e277b5d4af6ad1..e48a98a5c5a14787f489096eabd8d283267d33ec 100644 (file)
@@ -733,17 +733,9 @@ pub fn maybe_write_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generi
                for (idx, generic) in generics.params.iter().enumerate() {
                        match generic {
                                syn::GenericParam::Type(type_param) => {
-                                       let mut printed_param = false;
-                                       for bound in type_param.bounds.iter() {
-                                               if let syn::TypeParamBound::Trait(trait_bound) = bound {
-                                                       assert_simple_bound(&trait_bound);
-                                                       write!(w, "{}crate::{}", if idx != 0 { ", " } else { "" }, gen_types.maybe_resolve_ident(&type_param.ident).unwrap()).unwrap();
-                                                       if printed_param {
-                                                               unimplemented!("Can't print generic params that have multiple non-lifetime bounds");
-                                                       }
-                                                       printed_param = true;
-                                               }
-                                       }
+                                       write!(w, "{}", if idx != 0 { ", " } else { "" }).unwrap();
+                                       let type_ident = &type_param.ident;
+                                       types.write_c_type_in_generic_param(w, &syn::parse_quote!(#type_ident), Some(&gen_types), false);
                                },
                                syn::GenericParam::Lifetime(lt) => {
                                        if concrete_lifetimes {
index 7f53019785a971af285dc3a4dd1127e9d8e08992..224b3ecfcb8caa3905ce45823aaf27abf7b3ff2f 100644 (file)
@@ -313,23 +313,6 @@ impl<'a, 'p: 'a> GenericTypes<'a, 'p> {
                }
        }
 
-       /// 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> {
@@ -2343,7 +2326,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        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
@@ -2361,10 +2344,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                } 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
@@ -2463,13 +2446,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                                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 {
@@ -2586,7 +2569,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
        // *** 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) {
@@ -2596,6 +2579,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        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();
@@ -2608,6 +2592,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                // 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 {
@@ -2618,7 +2608,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        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() {
@@ -2629,24 +2619,24 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                                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 {
@@ -2704,7 +2694,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                        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) => {
@@ -2719,13 +2709,16 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                }
        }
        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)
        }
 }