@@ -27,7 +27,6 @@ use parse::ClangItemParser;
27
27
use proc_macro2:: { Ident , Span } ;
28
28
use std:: borrow:: Cow ;
29
29
use std:: cell:: Cell ;
30
- use std:: collections:: btree_map:: { self , BTreeMap } ;
31
30
use std:: iter:: IntoIterator ;
32
31
use std:: mem;
33
32
use std:: collections:: HashMap as StdHashMap ;
@@ -302,14 +301,8 @@ enum TypeKey {
302
301
/// A context used during parsing and generation of structs.
303
302
#[ derive( Debug ) ]
304
303
pub struct BindgenContext {
305
- /// The map of all the items parsed so far.
306
- ///
307
- /// It's a BTreeMap because we want the keys to be sorted to have consistent
308
- /// output.
309
- items : BTreeMap < ItemId , Item > ,
310
-
311
- /// The next item id to use during this bindings regeneration.
312
- next_item_id : ItemId ,
304
+ /// The map of all the items parsed so far, keyed off ItemId.
305
+ items : Vec < Option < Item > > ,
313
306
314
307
/// Clang USR to type map. This is needed to be able to associate types with
315
308
/// item ids during parsing.
@@ -597,12 +590,11 @@ If you encounter an error missing from this list, please file an issue or a PR!"
597
590
let root_module = Self :: build_root_module ( ItemId ( 0 ) ) ;
598
591
let root_module_id = root_module. id ( ) . as_module_id_unchecked ( ) ;
599
592
600
- let mut me = BindgenContext {
601
- items : Default :: default ( ) ,
593
+ BindgenContext {
594
+ items : vec ! [ Some ( root_module ) ] ,
602
595
types : Default :: default ( ) ,
603
596
type_params : Default :: default ( ) ,
604
597
modules : Default :: default ( ) ,
605
- next_item_id : ItemId ( 1 ) ,
606
598
root_module : root_module_id,
607
599
current_module : root_module_id,
608
600
semantic_parents : Default :: default ( ) ,
@@ -631,11 +623,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
631
623
have_destructor : None ,
632
624
has_type_param_in_array : None ,
633
625
has_float : None ,
634
- } ;
635
-
636
- me. add_item ( root_module, None , None ) ;
637
-
638
- me
626
+ }
639
627
}
640
628
641
629
/// Creates a timer for the current bindgen phase. If time_phases is `true`,
@@ -718,7 +706,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
718
706
self . need_bitfield_allocation . push ( id) ;
719
707
}
720
708
721
- let old_item = self . items . insert ( id , item) ;
709
+ let old_item = mem :: replace ( & mut self . items [ id . 0 ] , Some ( item) ) ;
722
710
assert ! (
723
711
old_item. is_none( ) ,
724
712
"should not have already associated an item with the given id"
@@ -746,7 +734,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
746
734
debug ! (
747
735
"Invalid declaration {:?} found for type {:?}" ,
748
736
declaration,
749
- self . items . get ( & id) . unwrap( ) . kind( ) . expect_type( )
737
+ self . resolve_item_fallible ( id) . unwrap( ) . kind( ) . expect_type( )
750
738
) ;
751
739
return ;
752
740
}
@@ -775,9 +763,9 @@ If you encounter an error missing from this list, please file an issue or a PR!"
775
763
/// details.
776
764
fn add_item_to_module ( & mut self , item : & Item ) {
777
765
assert ! ( item. id( ) != self . root_module) ;
778
- assert ! ( ! self . items . contains_key ( & item. id( ) ) ) ;
766
+ assert ! ( self . resolve_item_fallible ( item. id( ) ) . is_none ( ) ) ;
779
767
780
- if let Some ( parent) = self . items . get_mut ( & item. parent_id ( ) ) {
768
+ if let Some ( ref mut parent) = self . items [ item. parent_id ( ) . 0 ] {
781
769
if let Some ( module) = parent. as_module_mut ( ) {
782
770
debug ! (
783
771
"add_item_to_module: adding {:?} as child of parent module {:?}" ,
@@ -796,8 +784,8 @@ If you encounter an error missing from this list, please file an issue or a PR!"
796
784
self . current_module
797
785
) ;
798
786
799
- self . items
800
- . get_mut ( & self . current_module . into ( ) )
787
+ self . items [ ( self . current_module . 0 ) . 0 ]
788
+ . as_mut ( )
801
789
. expect ( "Should always have an item for self.current_module" )
802
790
. as_module_mut ( )
803
791
. expect ( "self.current_module should always be a module" )
@@ -825,7 +813,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
825
813
self . add_item_to_module ( & item) ;
826
814
827
815
let id = item. id ( ) ;
828
- let old_item = self . items . insert ( id , item) ;
816
+ let old_item = mem :: replace ( & mut self . items [ id . 0 ] , Some ( item) ) ;
829
817
assert ! (
830
818
old_item. is_none( ) ,
831
819
"should not have already associated an item with the given id"
@@ -941,8 +929,14 @@ If you encounter an error missing from this list, please file an issue or a PR!"
941
929
}
942
930
943
931
/// Iterate over all items that have been defined.
944
- pub fn items < ' a > ( & ' a self ) -> btree_map:: Iter < ' a , ItemId , Item > {
945
- self . items . iter ( )
932
+ pub fn items ( & self ) -> impl Iterator < Item = ( ItemId , & Item ) > {
933
+ self . items
934
+ . iter ( )
935
+ . enumerate ( )
936
+ . filter_map ( |( index, item) | {
937
+ let item = item. as_ref ( ) ?;
938
+ Some ( ( ItemId ( index) , item) )
939
+ } )
946
940
}
947
941
948
942
/// Have we collected all unresolved type references yet?
@@ -957,7 +951,8 @@ If you encounter an error missing from this list, please file an issue or a PR!"
957
951
debug_assert ! ( !self . collected_typerefs) ;
958
952
self . collected_typerefs = true ;
959
953
let mut typerefs = vec ! [ ] ;
960
- for ( id, ref mut item) in & mut self . items {
954
+
955
+ for ( id, item) in self . items ( ) {
961
956
let kind = item. kind ( ) ;
962
957
let ty = match kind. as_type ( ) {
963
958
Some ( ty) => ty,
@@ -966,7 +961,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
966
961
967
962
match * ty. kind ( ) {
968
963
TypeKind :: UnresolvedTypeRef ( ref ty, loc, parent_id) => {
969
- typerefs. push ( ( * id, ty. clone ( ) , loc, parent_id) ) ;
964
+ typerefs. push ( ( id, ty. clone ( ) , loc, parent_id) ) ;
970
965
}
971
966
_ => { }
972
967
} ;
@@ -987,7 +982,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
987
982
Item :: new_opaque_type ( self . next_item_id ( ) , & ty, self )
988
983
} ) ;
989
984
990
- let item = self . items . get_mut ( & id ) . unwrap ( ) ;
985
+ let item = self . items [ id . 0 ] . as_mut ( ) . unwrap ( ) ;
991
986
* item. kind_mut ( ) . as_type_mut ( ) . unwrap ( ) . kind_mut ( ) =
992
987
TypeKind :: ResolvedTypeRef ( resolved) ;
993
988
resolved
@@ -1018,11 +1013,11 @@ If you encounter an error missing from this list, please file an issue or a PR!"
1018
1013
where
1019
1014
F : ( FnOnce ( & BindgenContext , & mut Item ) -> T )
1020
1015
{
1021
- let mut item = self . items . remove ( & id ) . unwrap ( ) ;
1016
+ let mut item = self . items [ id . 0 ] . take ( ) . unwrap ( ) ;
1022
1017
1023
1018
let result = f ( self , & mut item) ;
1024
1019
1025
- let existing = self . items . insert ( id , item) ;
1020
+ let existing = mem :: replace ( & mut self . items [ id . 0 ] , Some ( item) ) ;
1026
1021
assert ! ( existing. is_none( ) ) ;
1027
1022
1028
1023
result
@@ -1051,15 +1046,13 @@ If you encounter an error missing from this list, please file an issue or a PR!"
1051
1046
fn deanonymize_fields ( & mut self ) {
1052
1047
let _t = self . timer ( "deanonymize_fields" ) ;
1053
1048
1054
- let comp_item_ids: Vec < ItemId > = self . items
1055
- . iter ( )
1049
+ let comp_item_ids: Vec < ItemId > = self . items ( )
1056
1050
. filter_map ( |( id, item) | {
1057
1051
if item. kind ( ) . as_type ( ) ?. is_comp ( ) {
1058
1052
return Some ( id) ;
1059
1053
}
1060
1054
None
1061
1055
} )
1062
- . cloned ( )
1063
1056
. collect ( ) ;
1064
1057
1065
1058
for id in comp_item_ids {
@@ -1090,7 +1083,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
1090
1083
1091
1084
let mut replacements = vec ! [ ] ;
1092
1085
1093
- for ( id, item) in self . items . iter ( ) {
1086
+ for ( id, item) in self . items ( ) {
1094
1087
if item. annotations ( ) . use_instead_of ( ) . is_some ( ) {
1095
1088
continue ;
1096
1089
}
@@ -1114,10 +1107,10 @@ If you encounter an error missing from this list, please file an issue or a PR!"
1114
1107
let replacement = self . replacements . get ( & path[ 1 ..] ) ;
1115
1108
1116
1109
if let Some ( replacement) = replacement {
1117
- if replacement != id {
1110
+ if * replacement != id {
1118
1111
// We set this just after parsing the annotation. It's
1119
1112
// very unlikely, but this can happen.
1120
- if self . items . get ( replacement) . is_some ( ) {
1113
+ if self . resolve_item_fallible ( * replacement) . is_some ( ) {
1121
1114
replacements. push ( ( id. expect_type_id ( self ) , replacement. expect_type_id ( self ) ) ) ;
1122
1115
}
1123
1116
}
@@ -1126,9 +1119,9 @@ If you encounter an error missing from this list, please file an issue or a PR!"
1126
1119
1127
1120
for ( id, replacement_id) in replacements {
1128
1121
debug ! ( "Replacing {:?} with {:?}" , id, replacement_id) ;
1129
-
1130
1122
let new_parent = {
1131
- let item = self . items . get_mut ( & id. into ( ) ) . unwrap ( ) ;
1123
+ let item_id: ItemId = id. into ( ) ;
1124
+ let item = self . items [ item_id. 0 ] . as_mut ( ) . unwrap ( ) ;
1132
1125
* item. kind_mut ( ) . as_type_mut ( ) . unwrap ( ) . kind_mut ( ) =
1133
1126
TypeKind :: ResolvedTypeRef ( replacement_id) ;
1134
1127
item. parent_id ( )
@@ -1146,8 +1139,9 @@ If you encounter an error missing from this list, please file an issue or a PR!"
1146
1139
continue ;
1147
1140
}
1148
1141
1149
- self . items
1150
- . get_mut ( & replacement_id. into ( ) )
1142
+ let replacement_item_id: ItemId = replacement_id. into ( ) ;
1143
+ self . items [ replacement_item_id. 0 ]
1144
+ . as_mut ( )
1151
1145
. unwrap ( )
1152
1146
. set_parent_for_replacement ( new_parent) ;
1153
1147
@@ -1183,16 +1177,16 @@ If you encounter an error missing from this list, please file an issue or a PR!"
1183
1177
continue ;
1184
1178
}
1185
1179
1186
- self . items
1187
- . get_mut ( & old_module )
1180
+ self . items [ old_module . 0 ]
1181
+ . as_mut ( )
1188
1182
. unwrap ( )
1189
1183
. as_module_mut ( )
1190
1184
. unwrap ( )
1191
1185
. children_mut ( )
1192
1186
. remove ( & replacement_id. into ( ) ) ;
1193
1187
1194
- self . items
1195
- . get_mut ( & new_module )
1188
+ self . items [ new_module . 0 ]
1189
+ . as_mut ( )
1196
1190
. unwrap ( )
1197
1191
. as_module_mut ( )
1198
1192
. unwrap ( )
@@ -1260,7 +1254,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
1260
1254
assert ! ( self . in_codegen_phase( ) ) ;
1261
1255
assert ! ( self . current_module == self . root_module) ;
1262
1256
1263
- let roots = self . items ( ) . map ( |( & id, _) | id) ;
1257
+ let roots = self . items ( ) . map ( |( id, _) | id) ;
1264
1258
traversal:: AssertNoDanglingItemsTraversal :: new (
1265
1259
self ,
1266
1260
roots,
@@ -1276,7 +1270,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
1276
1270
assert ! ( self . in_codegen_phase( ) ) ;
1277
1271
assert ! ( self . current_module == self . root_module) ;
1278
1272
1279
- for ( & id, _item) in self . items ( ) {
1273
+ for ( id, _item) in self . items ( ) {
1280
1274
if id == self . root_module {
1281
1275
continue ;
1282
1276
}
@@ -1467,7 +1461,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
1467
1461
debug_assert ! ( item. kind( ) . is_type( ) ) ;
1468
1462
self . add_item_to_module ( & item) ;
1469
1463
let id = item. id ( ) ;
1470
- let old_item = self . items . insert ( id , item) ;
1464
+ let old_item = mem :: replace ( & mut self . items [ id . 0 ] , Some ( item) ) ;
1471
1465
assert ! ( old_item. is_none( ) , "Inserted type twice?" ) ;
1472
1466
}
1473
1467
@@ -1502,21 +1496,21 @@ If you encounter an error missing from this list, please file an issue or a PR!"
1502
1496
///
1503
1497
/// Panics if the id resolves to an item that is not a type.
1504
1498
pub fn safe_resolve_type ( & self , type_id : TypeId ) -> Option < & Type > {
1505
- self . items . get ( & type_id. into ( ) ) . map ( |t| t. kind ( ) . expect_type ( ) )
1499
+ self . resolve_item_fallible ( type_id) . map ( |t| t. kind ( ) . expect_type ( ) )
1506
1500
}
1507
1501
1508
1502
/// Resolve the given `ItemId` into an `Item`, or `None` if no such item
1509
1503
/// exists.
1510
1504
pub fn resolve_item_fallible < Id : Into < ItemId > > ( & self , id : Id ) -> Option < & Item > {
1511
- self . items . get ( & id. into ( ) )
1505
+ self . items . get ( id. into ( ) . 0 ) ? . as_ref ( )
1512
1506
}
1513
1507
1514
1508
/// Resolve the given `ItemId` into an `Item`.
1515
1509
///
1516
1510
/// Panics if the given id does not resolve to any item.
1517
1511
pub fn resolve_item < Id : Into < ItemId > > ( & self , item_id : Id ) -> & Item {
1518
1512
let item_id = item_id. into ( ) ;
1519
- match self . items . get ( & item_id) {
1513
+ match self . resolve_item_fallible ( item_id) {
1520
1514
Some ( item) => item,
1521
1515
None => panic ! ( "Not an item: {:?}" , item_id) ,
1522
1516
}
@@ -1782,8 +1776,8 @@ If you encounter an error missing from this list, please file an issue or a PR!"
1782
1776
sub_item
1783
1777
) ;
1784
1778
self . add_item_to_module ( & sub_item) ;
1785
- debug_assert ! ( sub_id == sub_item. id( ) ) ;
1786
- self . items . insert ( sub_id, sub_item) ;
1779
+ debug_assert_eq ! ( sub_id, sub_item. id( ) ) ;
1780
+ self . items [ sub_id. 0 ] = Some ( sub_item) ;
1787
1781
args. push ( sub_id. as_type_id_unchecked ( ) ) ;
1788
1782
}
1789
1783
}
@@ -1842,8 +1836,8 @@ If you encounter an error missing from this list, please file an issue or a PR!"
1842
1836
// Bypass all the validations in add_item explicitly.
1843
1837
debug ! ( "instantiate_template: inserting item: {:?}" , item) ;
1844
1838
self . add_item_to_module ( & item) ;
1845
- debug_assert ! ( with_id == item. id( ) ) ;
1846
- self . items . insert ( with_id, item) ;
1839
+ debug_assert_eq ! ( with_id, item. id( ) ) ;
1840
+ self . items [ with_id. 0 ] = Some ( item) ;
1847
1841
Some ( with_id. as_type_id_unchecked ( ) )
1848
1842
}
1849
1843
@@ -1999,8 +1993,8 @@ If you encounter an error missing from this list, please file an issue or a PR!"
1999
1993
2000
1994
/// Returns the next item id to be used for an item.
2001
1995
pub fn next_item_id ( & mut self ) -> ItemId {
2002
- let ret = self . next_item_id ;
2003
- self . next_item_id = ItemId ( self . next_item_id . 0 + 1 ) ;
1996
+ let ret = ItemId ( self . items . len ( ) ) ;
1997
+ self . items . push ( None ) ;
2004
1998
ret
2005
1999
}
2006
2000
@@ -2349,7 +2343,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
2349
2343
}
2350
2344
}
2351
2345
} )
2352
- . map ( |( & id, _) | id)
2346
+ . map ( |( id, _) | id)
2353
2347
. collect :: < Vec < _ > > ( ) ;
2354
2348
2355
2349
// The reversal preserves the expected ordering of traversal,
0 commit comments