Skip to content

Commit

Permalink
Add LiteralStructField
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Aug 10, 2024
1 parent 5ffd0bb commit b080226
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
27 changes: 18 additions & 9 deletions src/bindgen/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cfg>,
}

#[derive(Debug, Clone)]
pub enum Literal {
Expr(String),
Expand All @@ -101,7 +107,7 @@ pub enum Literal {
Struct {
path: Path,
export_name: String,
fields: HashMap<String, Literal>,
fields: HashMap<String, LiteralStructField>,
},
Cast {
ty: Type,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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, .. } => {
Expand Down Expand Up @@ -388,12 +394,13 @@ impl Literal {
} => name,
_ => return Err(format!("Unsupported call expression. {:?}", *expr)),
};
let mut fields = HashMap::<String, Literal>::default();
let mut fields = HashMap::<String, LiteralStructField>::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()),
Expand All @@ -408,11 +415,13 @@ impl Literal {
..
}) => {
let struct_name = path.segments[0].ident.unraw().to_string();
let mut field_map = HashMap::<String, Literal>::default();
let mut field_map = HashMap::<String, LiteralStructField>::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()),
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions src/bindgen/language_backend/clike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,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, ", ");
}
Expand All @@ -949,7 +949,7 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
} else {
write!(out, ".{} = ", ordered_key);
}
self.write_literal(out, lit);
self.write_literal(out, &lit.value);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/language_backend/cython.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ impl LanguageBackend for CythonLanguageBackend<'_> {
} else {
is_first_field = false;
}
self.write_literal(out, lit);
self.write_literal(out, &lit.value);
}
}
write!(out, " }}");
Expand Down

0 comments on commit b080226

Please sign in to comment.