]> git.bitcoin.ninja Git - rust-lightning/commitdiff
[bindings] Handle unnamed enum variants
authorMatt Corallo <git@bluematt.me>
Fri, 19 Feb 2021 18:54:45 +0000 (13:54 -0500)
committerMatt Corallo <git@bluematt.me>
Fri, 19 Feb 2021 19:02:46 +0000 (14:02 -0500)
c-bindings-gen/src/main.rs
c-bindings-gen/src/types.rs

index e919dc758283d8f7bacd78ba678b398e4d1f903c..a40875d95cf83209bb7ec2008b76c9b54012f853 100644 (file)
@@ -1076,6 +1076,17 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type
                                writeln!(w, ",").unwrap();
                        }
                        write!(w, "\t}}").unwrap();
+               } else if let syn::Fields::Unnamed(fields) = &var.fields {
+                       needs_free = true;
+                       write!(w, "(").unwrap();
+                       for (idx, field) in fields.unnamed.iter().enumerate() {
+                               if export_status(&field.attrs) == ExportStatus::TestOnly { continue; }
+                               types.write_c_type(w, &field.ty, None, false);
+                               if idx != fields.unnamed.len() - 1 {
+                                       write!(w, ",").unwrap();
+                               }
+                       }
+                       write!(w, ")").unwrap();
                }
                if var.discriminant.is_some() { unimplemented!(); }
                writeln!(w, ",").unwrap();
@@ -1094,28 +1105,35 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type
                                                write!(w, "{}{}, ", if $ref { "ref " } else { "mut " }, field.ident.as_ref().unwrap()).unwrap();
                                        }
                                        write!(w, "}} ").unwrap();
+                               } else if let syn::Fields::Unnamed(fields) = &var.fields {
+                                       write!(w, "(").unwrap();
+                                       for (idx, field) in fields.unnamed.iter().enumerate() {
+                                               if export_status(&field.attrs) == ExportStatus::TestOnly { continue; }
+                                               write!(w, "{}{}, ", if $ref { "ref " } else { "mut " }, ('a' as u8 + idx as u8) as char).unwrap();
+                                       }
+                                       write!(w, ") ").unwrap();
                                }
                                write!(w, "=>").unwrap();
-                               if let syn::Fields::Named(fields) = &var.fields {
-                                       write!(w, " {{\n\t\t\t\t").unwrap();
-                                       for field in fields.named.iter() {
-                                               if export_status(&field.attrs) == ExportStatus::TestOnly { continue; }
+
+                               macro_rules! handle_field_a {
+                                       ($field: expr, $field_ident: expr) => { {
+                                               if export_status(&$field.attrs) == ExportStatus::TestOnly { continue; }
                                                let mut sink = ::std::io::sink();
                                                let mut out: &mut dyn std::io::Write = if $ref { &mut sink } else { w };
                                                let new_var = if $to_c {
-                                                       types.write_to_c_conversion_new_var(&mut out, field.ident.as_ref().unwrap(), &field.ty, None, false)
+                                                       types.write_to_c_conversion_new_var(&mut out, $field_ident, &$field.ty, None, false)
                                                } else {
-                                                       types.write_from_c_conversion_new_var(&mut out, field.ident.as_ref().unwrap(), &field.ty, None)
+                                                       types.write_from_c_conversion_new_var(&mut out, $field_ident, &$field.ty, None)
                                                };
                                                if $ref || new_var {
                                                        if $ref {
-                                                               write!(w, "let mut {}_nonref = (*{}).clone();\n\t\t\t\t", field.ident.as_ref().unwrap(), field.ident.as_ref().unwrap()).unwrap();
+                                                               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.as_ref().unwrap()), Span::call_site());
+                                                                       let nonref_ident = syn::Ident::new(&format!("{}_nonref", $field_ident), Span::call_site());
                                                                        if $to_c {
-                                                                               types.write_to_c_conversion_new_var(w, &nonref_ident, &field.ty, None, false);
+                                                                               types.write_to_c_conversion_new_var(w, &nonref_ident, &$field.ty, None, false);
                                                                        } else {
-                                                                               types.write_from_c_conversion_new_var(w, &nonref_ident, &field.ty, None);
+                                                                               types.write_from_c_conversion_new_var(w, &nonref_ident, &$field.ty, None);
                                                                        }
                                                                        write!(w, "\n\t\t\t\t").unwrap();
                                                                }
@@ -1123,31 +1141,58 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type
                                                                write!(w, "\n\t\t\t\t").unwrap();
                                                        }
                                                }
+                                       } }
+                               }
+                               if let syn::Fields::Named(fields) = &var.fields {
+                                       write!(w, " {{\n\t\t\t\t").unwrap();
+                                       for field in fields.named.iter() {
+                                               handle_field_a!(field, field.ident.as_ref().unwrap());
+                                       }
+                               } 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()));
                                        }
                                } else { write!(w, " ").unwrap(); }
+
                                write!(w, "{}{}::{}", if $to_c { "" } else { "native" }, e.ident, var.ident).unwrap();
-                               if let syn::Fields::Named(fields) = &var.fields {
-                                       write!(w, " {{").unwrap();
-                                       for field in fields.named.iter() {
-                                               if export_status(&field.attrs) == ExportStatus::TestOnly { continue; }
-                                               write!(w, "\n\t\t\t\t\t{}: ", field.ident.as_ref().unwrap()).unwrap();
+
+                               macro_rules! handle_field_b {
+                                       ($field: expr, $field_ident: expr) => { {
+                                               if export_status(&$field.attrs) == ExportStatus::TestOnly { continue; }
                                                if $to_c {
-                                                       types.write_to_c_conversion_inline_prefix(w, &field.ty, None, false);
+                                                       types.write_to_c_conversion_inline_prefix(w, &$field.ty, None, false);
                                                } else {
-                                                       types.write_from_c_conversion_prefix(w, &field.ty, None);
+                                                       types.write_from_c_conversion_prefix(w, &$field.ty, None);
                                                }
-                                               write!(w, "{}{}",
-                                                       field.ident.as_ref().unwrap(),
+                                               write!(w, "{}{}", $field_ident,
                                                        if $ref { "_nonref" } else { "" }).unwrap();
                                                if $to_c {
-                                                       types.write_to_c_conversion_inline_suffix(w, &field.ty, None, false);
+                                                       types.write_to_c_conversion_inline_suffix(w, &$field.ty, None, false);
                                                } else {
-                                                       types.write_from_c_conversion_suffix(w, &field.ty, None);
+                                                       types.write_from_c_conversion_suffix(w, &$field.ty, None);
                                                }
                                                write!(w, ",").unwrap();
+                                       } }
+                               }
+
+                               if let syn::Fields::Named(fields) = &var.fields {
+                                       write!(w, " {{").unwrap();
+                                       for field in fields.named.iter() {
+                                               if export_status(&field.attrs) == ExportStatus::TestOnly { continue; }
+                                               write!(w, "\n\t\t\t\t\t{}: ", field.ident.as_ref().unwrap()).unwrap();
+                                               handle_field_b!(field, field.ident.as_ref().unwrap());
                                        }
                                        writeln!(w, "\n\t\t\t\t}}").unwrap();
                                        write!(w, "\t\t\t}}").unwrap();
+                               } else if let syn::Fields::Unnamed(fields) = &var.fields {
+                                       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()));
+                                       }
+                                       writeln!(w, "\n\t\t\t\t)").unwrap();
+                                       write!(w, "\t\t\t}}").unwrap();
                                }
                                writeln!(w, ",").unwrap();
                        }
index 0c570bff7d5f5f73564d96c1e70374c263884af6..e7244039f4bcc5a32ab230b9f0a22640c56f28fd 100644 (file)
@@ -114,16 +114,20 @@ pub fn assert_simple_bound(bound: &syn::TraitBound) {
 /// type), otherwise it is mapped into a transparent, C-compatible version of itself.
 pub fn is_enum_opaque(e: &syn::ItemEnum) -> bool {
        for var in e.variants.iter() {
-               if let syn::Fields::Unit = var.fields {
-               } else if let syn::Fields::Named(fields) = &var.fields {
+               if let syn::Fields::Named(fields) = &var.fields {
                        for field in fields.named.iter() {
                                match export_status(&field.attrs) {
                                        ExportStatus::Export|ExportStatus::TestOnly => {},
                                        ExportStatus::NoExport => return true,
                                }
                        }
-               } else {
-                       return true;
+               } else if let syn::Fields::Unnamed(fields) = &var.fields {
+                       for field in fields.unnamed.iter() {
+                               match export_status(&field.attrs) {
+                                       ExportStatus::Export|ExportStatus::TestOnly => {},
+                                       ExportStatus::NoExport => return true,
+                               }
+                       }
                }
        }
        false