Skip to content

Commit

Permalink
replace is_first_field to enumerate
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone authored and emilio committed Oct 28, 2024
1 parent d8432db commit 54a1098
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/bindgen/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl Literal {
}
}

fn can_be_constexpr(&self) -> bool {
pub(crate) fn can_be_constexpr(&self) -> bool {
!self.has_pointer_casts()
}

Expand Down
31 changes: 22 additions & 9 deletions src/bindgen/language_backend/clike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,30 +893,43 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
fields,
path,
} => {
let allow_constexpr = self.config.constant.allow_constexpr && l.can_be_constexpr();
let is_constexpr = self.config.language == Language::Cxx
&& (self.config.constant.allow_static_const || allow_constexpr);
if self.config.language == Language::C {
write!(out, "({})", export_name);
} else {
write!(out, "{}", export_name);
}

write!(out, "{{ ");
let mut is_first_field = true;
// In C++, same order as defined is required.
let ordered_fields = out.bindings().struct_field_names(path);
for ordered_key in ordered_fields.iter() {
for (i, ordered_key) in ordered_fields.iter().enumerate() {
if let Some(lit) = fields.get(ordered_key) {
if !is_first_field {
write!(out, ", ");
}
is_first_field = false;
if self.config.language == Language::Cxx {
if is_constexpr {
if i > 0 {
write!(out, ", ");
}

// TODO: Some C++ versions (c++20?) now support designated
// initializers, consider generating them.
write!(out, "/* .{} = */ ", ordered_key);
self.write_literal(out, lit);
} else {
write!(out, ".{} = ", ordered_key);
if i > 0 {
write!(out, ", ");
}

if self.config.language == Language::Cxx {
// TODO: Some C++ versions (c++20?) now support designated
// initializers, consider generating them.
write!(out, "/* .{} = */ ", ordered_key);
} else {
write!(out, ".{} = ", ordered_key);
}
self.write_literal(out, lit);
}
self.write_literal(out, lit);
}
}
write!(out, " }}");
Expand Down

0 comments on commit 54a1098

Please sign in to comment.