From 1b4de917df2a89c977273d4cd3955ea164c9c03e Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sun, 11 Aug 2024 00:46:07 +0900 Subject: [PATCH] Add LiteralStructField --- src/bindgen/ir/constant.rs | 27 +++++++++++++++++--------- src/bindgen/language_backend/clike.rs | 4 ++-- src/bindgen/language_backend/cython.rs | 2 +- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/bindgen/ir/constant.rs b/src/bindgen/ir/constant.rs index ca6499d8..5f9493d8 100644 --- a/src/bindgen/ir/constant.rs +++ b/src/bindgen/ir/constant.rs @@ -78,6 +78,12 @@ pub(crate) fn to_known_assoc_constant(associated_to: &Path, name: &str) -> Optio Some(format!("{}_{}", prefix, name)) } +#[derive(Debug, Clone)] +pub struct LiteralStructField { + pub value: Literal, + pub cfg: Option, +} + #[derive(Debug, Clone)] pub enum Literal { Expr(String), @@ -101,7 +107,7 @@ pub enum Literal { Struct { path: Path, export_name: String, - fields: HashMap, + fields: HashMap, }, Cast { ty: Type, @@ -135,7 +141,7 @@ impl Literal { self_ty.name().clone_into(export_name); } for ref mut expr in fields.values_mut() { - expr.replace_self_with(self_ty); + expr.value.replace_self_with(self_ty); } } Literal::Cast { @@ -203,7 +209,7 @@ impl Literal { Literal::FieldAccess { ref base, .. } => base.visit(visitor), Literal::Struct { ref fields, .. } => { for (_name, field) in fields.iter() { - if !field.visit(visitor) { + if !field.value.visit(visitor) { return false; } } @@ -250,7 +256,7 @@ impl Literal { } => { config.export.rename(export_name); for lit in fields.values_mut() { - lit.rename_for_config(config); + lit.value.rename_for_config(config); } } Literal::FieldAccess { ref mut base, .. } => { @@ -388,12 +394,13 @@ impl Literal { } => name, _ => return Err(format!("Unsupported call expression. {:?}", *expr)), }; - let mut fields = HashMap::::default(); + let mut fields = HashMap::::default(); for (index, arg) in args.iter().enumerate() { let ident = member_to_ident(&syn::Member::Unnamed(syn::Index::from(index))).to_string(); let value = Literal::load(arg)?; - fields.insert(ident, value); + let field = LiteralStructField { value, cfg: None }; + fields.insert(ident, field); } Ok(Literal::Struct { path: Path::new(struct_name.clone()), @@ -408,11 +415,13 @@ impl Literal { .. }) => { let struct_name = path.segments[0].ident.unraw().to_string(); - let mut field_map = HashMap::::default(); + let mut field_map = HashMap::::default(); for field in fields { let ident = member_to_ident(&field.member).to_string(); + let cfg = Cfg::load(&field.attrs); let value = Literal::load(&field.expr)?; - field_map.insert(ident, value); + let field = LiteralStructField { value, cfg }; + field_map.insert(ident, field); } Ok(Literal::Struct { path: Path::new(struct_name.clone()), @@ -682,7 +691,7 @@ impl Constant { if !out.bindings().struct_is_transparent(path) { break; } - value = fields.iter().next().unwrap().1 + value = &fields.iter().next().unwrap().1.value } language_backend.write_documentation(out, self.documentation()); diff --git a/src/bindgen/language_backend/clike.rs b/src/bindgen/language_backend/clike.rs index b41a3c46..3830b02a 100644 --- a/src/bindgen/language_backend/clike.rs +++ b/src/bindgen/language_backend/clike.rs @@ -918,7 +918,7 @@ impl LanguageBackend for CLikeLanguageBackend<'_> { // TODO: Some C++ versions (c++20?) now support designated // initializers, consider generating them. write!(out, "/* .{} = */ ", ordered_key); - self.write_literal(out, lit); + self.write_literal(out, &lit.value); if i + 1 != ordered_fields.len() { write!(out, ","); if !is_constexpr { @@ -937,7 +937,7 @@ impl LanguageBackend for CLikeLanguageBackend<'_> { } else { write!(out, ".{} = ", ordered_key); } - self.write_literal(out, lit); + self.write_literal(out, &lit.value); } } } diff --git a/src/bindgen/language_backend/cython.rs b/src/bindgen/language_backend/cython.rs index 8497f3cd..0327ffd0 100644 --- a/src/bindgen/language_backend/cython.rs +++ b/src/bindgen/language_backend/cython.rs @@ -399,7 +399,7 @@ impl LanguageBackend for CythonLanguageBackend<'_> { } else { is_first_field = false; } - self.write_literal(out, lit); + self.write_literal(out, &lit.value); } } write!(out, " }}");