Skip to content

Commit fcbe260

Browse files
author
bors-servo
authored
Auto merge of #1021 - pepyakin:deanonymize-fields, r=fitzgen
Deanonymize fields r? @fitzgen
2 parents d386b5e + a8663ae commit fcbe260

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

src/codegen/mod.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -865,24 +865,6 @@ impl CodeGenerator for TemplateInstantiation {
865865
}
866866
}
867867

868-
/// Generates an infinite number of anonymous field names.
869-
struct AnonFieldNames(usize);
870-
871-
impl Default for AnonFieldNames {
872-
fn default() -> AnonFieldNames {
873-
AnonFieldNames(0)
874-
}
875-
}
876-
877-
impl Iterator for AnonFieldNames {
878-
type Item = String;
879-
880-
fn next(&mut self) -> Option<String> {
881-
self.0 += 1;
882-
Some(format!("__bindgen_anon_{}", self.0))
883-
}
884-
}
885-
886868
/// Trait for implementing the code generation of a struct or union field.
887869
trait FieldCodegen<'a> {
888870
type Extra;
@@ -894,7 +876,6 @@ trait FieldCodegen<'a> {
894876
codegen_depth: usize,
895877
accessor_kind: FieldAccessorKind,
896878
parent: &CompInfo,
897-
anon_field_names: &mut AnonFieldNames,
898879
result: &mut CodegenResult,
899880
struct_layout: &mut StructLayoutTracker,
900881
fields: &mut F,
@@ -915,7 +896,6 @@ impl<'a> FieldCodegen<'a> for Field {
915896
codegen_depth: usize,
916897
accessor_kind: FieldAccessorKind,
917898
parent: &CompInfo,
918-
anon_field_names: &mut AnonFieldNames,
919899
result: &mut CodegenResult,
920900
struct_layout: &mut StructLayoutTracker,
921901
fields: &mut F,
@@ -933,7 +913,6 @@ impl<'a> FieldCodegen<'a> for Field {
933913
codegen_depth,
934914
accessor_kind,
935915
parent,
936-
anon_field_names,
937916
result,
938917
struct_layout,
939918
fields,
@@ -948,7 +927,6 @@ impl<'a> FieldCodegen<'a> for Field {
948927
codegen_depth,
949928
accessor_kind,
950929
parent,
951-
anon_field_names,
952930
result,
953931
struct_layout,
954932
fields,
@@ -970,7 +948,6 @@ impl<'a> FieldCodegen<'a> for FieldData {
970948
codegen_depth: usize,
971949
accessor_kind: FieldAccessorKind,
972950
parent: &CompInfo,
973-
anon_field_names: &mut AnonFieldNames,
974951
result: &mut CodegenResult,
975952
struct_layout: &mut StructLayoutTracker,
976953
fields: &mut F,
@@ -1030,7 +1007,7 @@ impl<'a> FieldCodegen<'a> for FieldData {
10301007
let field_name =
10311008
self.name()
10321009
.map(|name| ctx.rust_mangle(name).into_owned())
1033-
.unwrap_or_else(|| anon_field_names.next().unwrap());
1010+
.expect("Each field should have a name in codegen!");
10341011
let field_ident = ctx.rust_ident_raw(field_name.as_str());
10351012

10361013
if !parent.is_union() {
@@ -1164,7 +1141,6 @@ impl<'a> FieldCodegen<'a> for BitfieldUnit {
11641141
codegen_depth: usize,
11651142
accessor_kind: FieldAccessorKind,
11661143
parent: &CompInfo,
1167-
anon_field_names: &mut AnonFieldNames,
11681144
result: &mut CodegenResult,
11691145
struct_layout: &mut StructLayoutTracker,
11701146
fields: &mut F,
@@ -1213,7 +1189,6 @@ impl<'a> FieldCodegen<'a> for BitfieldUnit {
12131189
codegen_depth,
12141190
accessor_kind,
12151191
parent,
1216-
anon_field_names,
12171192
result,
12181193
struct_layout,
12191194
fields,
@@ -1321,7 +1296,6 @@ impl<'a> FieldCodegen<'a> for Bitfield {
13211296
_codegen_depth: usize,
13221297
_accessor_kind: FieldAccessorKind,
13231298
parent: &CompInfo,
1324-
_anon_field_names: &mut AnonFieldNames,
13251299
_result: &mut CodegenResult,
13261300
_struct_layout: &mut StructLayoutTracker,
13271301
_fields: &mut F,
@@ -1595,7 +1569,6 @@ impl CodeGenerator for CompInfo {
15951569

15961570
let mut methods = vec![];
15971571
if !is_opaque {
1598-
let mut anon_field_names = AnonFieldNames::default();
15991572
let codegen_depth = item.codegen_depth(ctx);
16001573
let fields_should_be_private =
16011574
item.annotations().private_fields().unwrap_or(false);
@@ -1609,7 +1582,6 @@ impl CodeGenerator for CompInfo {
16091582
codegen_depth,
16101583
struct_accessor_kind,
16111584
self,
1612-
&mut anon_field_names,
16131585
result,
16141586
&mut struct_layout,
16151587
&mut fields,

src/ir/comp.rs

+32
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,33 @@ impl CompFields {
666666
CompFields::AfterComputingBitfieldUnits(fields_and_units),
667667
);
668668
}
669+
670+
fn deanonymize_fields(&mut self) {
671+
let fields = match *self {
672+
CompFields::AfterComputingBitfieldUnits(ref mut fields) => {
673+
fields
674+
}
675+
CompFields::BeforeComputingBitfieldUnits(_) => {
676+
panic!("Not yet computed bitfield units.");
677+
}
678+
};
679+
680+
let mut anon_field_counter = 0;
681+
for field in fields.iter_mut() {
682+
let field_data = match *field {
683+
Field::DataMember(ref mut fd) => fd,
684+
Field::Bitfields(_) => continue,
685+
};
686+
687+
if let Some(_) = field_data.name {
688+
continue;
689+
}
690+
691+
anon_field_counter += 1;
692+
let name = format!("__bindgen_anon_{}", anon_field_counter);
693+
field_data.name = Some(name);
694+
}
695+
}
669696
}
670697

671698
impl Trace for CompFields {
@@ -1351,6 +1378,11 @@ impl CompInfo {
13511378
self.fields.compute_bitfield_units(ctx);
13521379
}
13531380

1381+
/// Assign for each anonymous field a generated name.
1382+
pub fn deanonymize_fields(&mut self) {
1383+
self.fields.deanonymize_fields();
1384+
}
1385+
13541386
/// Returns whether the current union can be represented as a Rust `union`
13551387
///
13561388
/// Requirements:

src/ir/context.rs

+14
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,18 @@ impl BindgenContext {
799799
}
800800
}
801801

802+
/// Assign a new generated name for each anonymous field.
803+
fn deanonymize_fields(&mut self) {
804+
let _t = self.timer("deanonymize_fields");
805+
let comp_types = self.items
806+
.values_mut()
807+
.filter_map(|item| item.kind_mut().as_type_mut())
808+
.filter_map(Type::as_comp_mut);
809+
for comp_info in comp_types {
810+
comp_info.deanonymize_fields();
811+
}
812+
}
813+
802814
/// Iterate over all items and replace any item that has been named in a
803815
/// `replaces="SomeType"` annotation with the replacement type.
804816
fn process_replacements(&mut self) {
@@ -940,6 +952,8 @@ impl BindgenContext {
940952
self.compute_bitfield_units();
941953
self.process_replacements();
942954
}
955+
956+
self.deanonymize_fields();
943957

944958
// And assert once again, because resolving type refs and processing
945959
// replacements both mutate the IR graph.

0 commit comments

Comments
 (0)