Skip to content

Commit 5449454

Browse files
committed
Add LiteralStructField
1 parent 89a9faa commit 5449454

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

src/bindgen/ir/constant.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ pub(crate) fn to_known_assoc_constant(associated_to: &Path, name: &str) -> Optio
7878
Some(format!("{}_{}", prefix, name))
7979
}
8080

81+
#[derive(Debug, Clone)]
82+
pub struct LiteralStructField {
83+
pub value: Literal,
84+
pub cfg: Option<Cfg>,
85+
}
86+
8187
#[derive(Debug, Clone)]
8288
pub enum Literal {
8389
Expr(String),
@@ -101,7 +107,7 @@ pub enum Literal {
101107
Struct {
102108
path: Path,
103109
export_name: String,
104-
fields: HashMap<String, Literal>,
110+
fields: HashMap<String, LiteralStructField>,
105111
},
106112
Cast {
107113
ty: Type,
@@ -135,7 +141,7 @@ impl Literal {
135141
self_ty.name().clone_into(export_name);
136142
}
137143
for ref mut expr in fields.values_mut() {
138-
expr.replace_self_with(self_ty);
144+
expr.value.replace_self_with(self_ty);
139145
}
140146
}
141147
Literal::Cast {
@@ -203,7 +209,7 @@ impl Literal {
203209
Literal::FieldAccess { ref base, .. } => base.visit(visitor),
204210
Literal::Struct { ref fields, .. } => {
205211
for (_name, field) in fields.iter() {
206-
if !field.visit(visitor) {
212+
if !field.value.visit(visitor) {
207213
return false;
208214
}
209215
}
@@ -250,7 +256,7 @@ impl Literal {
250256
} => {
251257
config.export.rename(export_name);
252258
for lit in fields.values_mut() {
253-
lit.rename_for_config(config);
259+
lit.value.rename_for_config(config);
254260
}
255261
}
256262
Literal::FieldAccess { ref mut base, .. } => {
@@ -388,12 +394,13 @@ impl Literal {
388394
} => name,
389395
_ => return Err(format!("Unsupported call expression. {:?}", *expr)),
390396
};
391-
let mut fields = HashMap::<String, Literal>::default();
397+
let mut fields = HashMap::<String, LiteralStructField>::default();
392398
for (index, arg) in args.iter().enumerate() {
393399
let ident =
394400
member_to_ident(&syn::Member::Unnamed(syn::Index::from(index))).to_string();
395401
let value = Literal::load(arg)?;
396-
fields.insert(ident, value);
402+
let field = LiteralStructField { value, cfg: None };
403+
fields.insert(ident, field);
397404
}
398405
Ok(Literal::Struct {
399406
path: Path::new(struct_name.clone()),
@@ -408,11 +415,13 @@ impl Literal {
408415
..
409416
}) => {
410417
let struct_name = path.segments[0].ident.unraw().to_string();
411-
let mut field_map = HashMap::<String, Literal>::default();
418+
let mut field_map = HashMap::<String, LiteralStructField>::default();
412419
for field in fields {
413420
let ident = member_to_ident(&field.member).to_string();
421+
let cfg = Cfg::load(&field.attrs);
414422
let value = Literal::load(&field.expr)?;
415-
field_map.insert(ident, value);
423+
let field = LiteralStructField { value, cfg };
424+
field_map.insert(ident, field);
416425
}
417426
Ok(Literal::Struct {
418427
path: Path::new(struct_name.clone()),
@@ -682,7 +691,7 @@ impl Constant {
682691
if !out.bindings().struct_is_transparent(path) {
683692
break;
684693
}
685-
value = fields.iter().next().unwrap().1
694+
value = &fields.iter().next().unwrap().1.value
686695
}
687696

688697
language_backend.write_documentation(out, self.documentation());

src/bindgen/language_backend/clike.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
918918
// TODO: Some C++ versions (c++20?) now support designated
919919
// initializers, consider generating them.
920920
write!(out, "/* .{} = */ ", ordered_key);
921-
self.write_literal(out, lit);
921+
self.write_literal(out, &lit.value);
922922
if i + 1 != ordered_fields.len() {
923923
write!(out, ",");
924924
if !is_constexpr {
@@ -937,7 +937,7 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
937937
} else {
938938
write!(out, ".{} = ", ordered_key);
939939
}
940-
self.write_literal(out, lit);
940+
self.write_literal(out, &lit.value);
941941
}
942942
}
943943
}

src/bindgen/language_backend/cython.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ impl LanguageBackend for CythonLanguageBackend<'_> {
399399
} else {
400400
is_first_field = false;
401401
}
402-
self.write_literal(out, lit);
402+
self.write_literal(out, &lit.value);
403403
}
404404
}
405405
write!(out, " }}");

0 commit comments

Comments
 (0)