@@ -10,7 +10,7 @@ use std::collections::{HashMap, hash_map};
10
10
use std:: collections:: btree_map:: { self , BTreeMap } ;
11
11
use std:: fmt;
12
12
use super :: int:: IntKind ;
13
- use super :: item:: { Item , ItemCanonicalName , ItemId } ;
13
+ use super :: item:: { Item , ItemCanonicalName } ;
14
14
use super :: item_kind:: ItemKind ;
15
15
use super :: module:: Module ;
16
16
use super :: ty:: { FloatKind , Type , TypeKind } ;
@@ -19,6 +19,19 @@ use syntax::ast::Ident;
19
19
use syntax:: codemap:: { DUMMY_SP , Span } ;
20
20
use syntax:: ext:: base:: ExtCtxt ;
21
21
22
+ /// A single identifier for an item.
23
+ ///
24
+ /// TODO: Build stronger abstractions on top of this, like TypeId(ItemId)?
25
+ #[ derive( Debug , Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
26
+ pub struct ItemId ( usize ) ;
27
+
28
+ impl ItemId {
29
+ /// Get a numeric representation of this id.
30
+ pub fn as_usize ( & self ) -> usize {
31
+ self . 0
32
+ }
33
+ }
34
+
22
35
/// A key used to index a resolved type, so we only process it once.
23
36
///
24
37
/// This is almost always a USR string (an unique identifier generated by
@@ -50,6 +63,9 @@ pub struct BindgenContext<'ctx> {
50
63
/// output.
51
64
items : BTreeMap < ItemId , Item > ,
52
65
66
+ /// The next item id to use during this bindings regeneration.
67
+ next_item_id : ItemId ,
68
+
53
69
/// Clang USR to type map. This is needed to be able to associate types with
54
70
/// item ids during parsing.
55
71
types : HashMap < TypeKey , ItemId > ,
@@ -122,11 +138,12 @@ impl<'ctx> BindgenContext<'ctx> {
122
138
parse_options)
123
139
. expect ( "TranslationUnit::parse" ) ;
124
140
125
- let root_module = Self :: build_root_module ( ) ;
141
+ let root_module = Self :: build_root_module ( ItemId ( 0 ) ) ;
126
142
let mut me = BindgenContext {
127
143
items : Default :: default ( ) ,
128
144
types : Default :: default ( ) ,
129
145
modules : Default :: default ( ) ,
146
+ next_item_id : ItemId ( 1 ) ,
130
147
root_module : root_module. id ( ) ,
131
148
current_module : root_module. id ( ) ,
132
149
currently_parsed_types : vec ! [ ] ,
@@ -422,9 +439,8 @@ impl<'ctx> BindgenContext<'ctx> {
422
439
assert ! ( old_item. is_none( ) , "Inserted type twice?" ) ;
423
440
}
424
441
425
- fn build_root_module ( ) -> Item {
442
+ fn build_root_module ( id : ItemId ) -> Item {
426
443
let module = Module :: new ( Some ( "root" . into ( ) ) ) ;
427
- let id = ItemId :: next ( ) ;
428
444
Item :: new ( id, None , None , id, ItemKind :: Module ( module) )
429
445
}
430
446
@@ -702,6 +718,13 @@ impl<'ctx> BindgenContext<'ctx> {
702
718
with_id
703
719
}
704
720
721
+ /// Returns the next item id to be used for an item.
722
+ pub fn next_item_id ( & mut self ) -> ItemId {
723
+ let ret = self . next_item_id ;
724
+ self . next_item_id = ItemId ( self . next_item_id . 0 + 1 ) ;
725
+ ret
726
+ }
727
+
705
728
fn build_builtin_ty ( & mut self ,
706
729
ty : & clang:: Type ,
707
730
_declaration : Cursor )
@@ -747,7 +770,7 @@ impl<'ctx> BindgenContext<'ctx> {
747
770
let is_const = ty. is_const ( ) ;
748
771
let layout = ty. fallible_layout ( ) . ok ( ) ;
749
772
let ty = Type :: new ( Some ( spelling) , layout, type_kind, is_const) ;
750
- let id = ItemId :: next ( ) ;
773
+ let id = self . next_item_id ( ) ;
751
774
let item =
752
775
Item :: new ( id, None , None , self . root_module , ItemKind :: Type ( ty) ) ;
753
776
self . add_builtin_item ( item) ;
@@ -841,11 +864,11 @@ impl<'ctx> BindgenContext<'ctx> {
841
864
use clangll:: * ;
842
865
assert ! ( cursor. kind( ) == CXCursor_Namespace , "Be a nice person" ) ;
843
866
let cursor = cursor. canonical ( ) ;
844
- let module_id = match self . modules . get ( & cursor) {
845
- Some ( id) => return * id,
846
- None => ItemId :: next ( ) ,
847
- } ;
867
+ if let Some ( id) = self . modules . get ( & cursor) {
868
+ return * id;
869
+ }
848
870
871
+ let module_id = self . next_item_id ( ) ;
849
872
let module_name = self . translation_unit
850
873
. tokens ( & cursor)
851
874
. and_then ( |tokens| {
0 commit comments