Skip to content

Commit 45cae56

Browse files
committed
ir: Make the id local to the context.
1 parent 53cd0e4 commit 45cae56

File tree

12 files changed

+59
-54
lines changed

12 files changed

+59
-54
lines changed

src/codegen/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use aster;
55

66
use ir::annotations::FieldAccessorKind;
77
use ir::comp::{CompInfo, CompKind, Field, Method};
8-
use ir::context::BindgenContext;
8+
use ir::context::{BindgenContext, ItemId};
99
use ir::enum_ty::{Enum, EnumVariant, EnumVariantValue};
1010
use ir::function::{Function, FunctionSig};
1111
use ir::int::IntKind;
12-
use ir::item::{Item, ItemCanonicalName, ItemCanonicalPath, ItemId};
12+
use ir::item::{Item, ItemCanonicalName, ItemCanonicalPath};
1313
use ir::item_kind::ItemKind;
1414
use ir::layout::Layout;
1515
use ir::module::Module;
@@ -1919,8 +1919,8 @@ pub fn codegen(context: &mut BindgenContext) -> Vec<P<ast::Item>> {
19191919

19201920
mod utils {
19211921
use aster;
1922-
use ir::context::BindgenContext;
1923-
use ir::item::{Item, ItemCanonicalPath, ItemId};
1922+
use ir::context::{BindgenContext, ItemId};
1923+
use ir::item::{Item, ItemCanonicalPath};
19241924
use ir::ty::TypeKind;
19251925
use std::mem;
19261926
use super::ItemToRustTy;

src/ir/comp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use parse::{ClangItemParser, ParseError};
55
use std::cell::Cell;
66
use std::cmp;
77
use super::annotations::Annotations;
8-
use super::context::BindgenContext;
9-
use super::item::{Item, ItemId};
8+
use super::context::{BindgenContext, ItemId};
9+
use super::item::Item;
1010
use super::layout::Layout;
1111
use super::ty::{RUST_DERIVE_IN_ARRAY_LIMIT, Type};
1212
use super::type_collector::{ItemSet, TypeCollector};

src/ir/context.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::collections::{HashMap, hash_map};
1010
use std::collections::btree_map::{self, BTreeMap};
1111
use std::fmt;
1212
use super::int::IntKind;
13-
use super::item::{Item, ItemCanonicalName, ItemId};
13+
use super::item::{Item, ItemCanonicalName};
1414
use super::item_kind::ItemKind;
1515
use super::module::Module;
1616
use super::ty::{FloatKind, Type, TypeKind};
@@ -19,6 +19,18 @@ use syntax::ast::Ident;
1919
use syntax::codemap::{DUMMY_SP, Span};
2020
use syntax::ext::base::ExtCtxt;
2121

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+
pub fn as_usize(&self) -> usize {
30+
self.0
31+
}
32+
}
33+
2234
/// A key used to index a resolved type, so we only process it once.
2335
///
2436
/// This is almost always a USR string (an unique identifier generated by
@@ -50,6 +62,9 @@ pub struct BindgenContext<'ctx> {
5062
/// output.
5163
items: BTreeMap<ItemId, Item>,
5264

65+
/// The next item id to use during this bindings regeneration.
66+
next_item_id: ItemId,
67+
5368
/// Clang USR to type map. This is needed to be able to associate types with
5469
/// item ids during parsing.
5570
types: HashMap<TypeKey, ItemId>,
@@ -122,11 +137,12 @@ impl<'ctx> BindgenContext<'ctx> {
122137
parse_options)
123138
.expect("TranslationUnit::parse");
124139

125-
let root_module = Self::build_root_module();
140+
let root_module = Self::build_root_module(ItemId(0));
126141
let mut me = BindgenContext {
127142
items: Default::default(),
128143
types: Default::default(),
129144
modules: Default::default(),
145+
next_item_id: ItemId(1),
130146
root_module: root_module.id(),
131147
current_module: root_module.id(),
132148
currently_parsed_types: vec![],
@@ -422,9 +438,8 @@ impl<'ctx> BindgenContext<'ctx> {
422438
assert!(old_item.is_none(), "Inserted type twice?");
423439
}
424440

425-
fn build_root_module() -> Item {
441+
fn build_root_module(id: ItemId) -> Item {
426442
let module = Module::new(Some("root".into()));
427-
let id = ItemId::next();
428443
Item::new(id, None, None, id, ItemKind::Module(module))
429444
}
430445

@@ -702,6 +717,12 @@ impl<'ctx> BindgenContext<'ctx> {
702717
with_id
703718
}
704719

720+
pub fn next_item_id(&mut self) -> ItemId {
721+
let ret = self.next_item_id;
722+
self.next_item_id = ItemId(self.next_item_id.0 + 1);
723+
ret
724+
}
725+
705726
fn build_builtin_ty(&mut self,
706727
ty: &clang::Type,
707728
_declaration: Cursor)
@@ -747,7 +768,7 @@ impl<'ctx> BindgenContext<'ctx> {
747768
let is_const = ty.is_const();
748769
let layout = ty.fallible_layout().ok();
749770
let ty = Type::new(Some(spelling), layout, type_kind, is_const);
750-
let id = ItemId::next();
771+
let id = self.next_item_id();
751772
let item =
752773
Item::new(id, None, None, self.root_module, ItemKind::Type(ty));
753774
self.add_builtin_item(item);
@@ -841,11 +862,11 @@ impl<'ctx> BindgenContext<'ctx> {
841862
use clangll::*;
842863
assert!(cursor.kind() == CXCursor_Namespace, "Be a nice person");
843864
let cursor = cursor.canonical();
844-
let module_id = match self.modules.get(&cursor) {
845-
Some(id) => return *id,
846-
None => ItemId::next(),
847-
};
865+
if let Some(id) = self.modules.get(&cursor) {
866+
return *id;
867+
}
848868

869+
let module_id = self.next_item_id();
849870
let module_name = self.translation_unit
850871
.tokens(&cursor)
851872
.and_then(|tokens| {

src/ir/enum_ty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use clang;
44
use parse::{ClangItemParser, ParseError};
5-
use super::context::BindgenContext;
6-
use super::item::{Item, ItemId};
5+
use super::context::{BindgenContext, ItemId};
6+
use super::item::Item;
77
use super::ty::TypeKind;
88

99
/// A C/C++ enumeration.

src/ir/function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use clang;
44
use clangll::Enum_CXCallingConv;
55
use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};
6-
use super::context::BindgenContext;
7-
use super::item::{Item, ItemId};
6+
use super::context::{BindgenContext, ItemId};
7+
use super::item::Item;
88
use super::ty::TypeKind;
99
use super::type_collector::{ItemSet, TypeCollector};
1010
use syntax::abi;

src/ir/item.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use clang;
44
use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};
55
use regex::Regex;
66
use std::cell::{Cell, RefCell};
7-
use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
87
use super::annotations::Annotations;
9-
use super::context::BindgenContext;
8+
use super::context::{BindgenContext, ItemId};
109
use super::function::Function;
1110
use super::item_kind::ItemKind;
1211
use super::module::Module;
@@ -78,21 +77,6 @@ impl<'a, 'b> Iterator for ItemAncestorsIter<'a, 'b>
7877
}
7978
}
8079

81-
/// A single identifier for an item.
82-
///
83-
/// TODO: Build stronger abstractions on top of this, like TypeId(ItemId)?
84-
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
85-
pub struct ItemId(usize);
86-
87-
impl ItemId {
88-
/// Allocate the next `ItemId`.
89-
pub fn next() -> Self {
90-
static NEXT_ITEM_ID: AtomicUsize = ATOMIC_USIZE_INIT;
91-
let next_id = NEXT_ITEM_ID.fetch_add(1, Ordering::Relaxed);
92-
ItemId(next_id)
93-
}
94-
}
95-
9680
// Pure convenience
9781
impl ItemCanonicalName for ItemId {
9882
fn canonical_name(&self, ctx: &BindgenContext) -> String {
@@ -667,7 +651,7 @@ impl Item {
667651
// Note that this `id_` prefix prevents (really unlikely) collisions
668652
// between the global id and the local id of an item with the same
669653
// parent.
670-
format!("id_{}", self.id().0)
654+
format!("id_{}", self.id().as_usize())
671655
}
672656

673657
fn make_exposed_name(&self,
@@ -747,7 +731,7 @@ impl ClangItemParser for Item {
747731
}
748732

749733
let ty = Type::new(None, None, kind, is_const);
750-
let id = ItemId::next();
734+
let id = ctx.next_item_id();
751735
let module = ctx.root_module();
752736
ctx.add_item(Item::new(id, None, None, module, ItemKind::Type(ty)),
753737
None,
@@ -779,7 +763,7 @@ impl ClangItemParser for Item {
779763
($what:ident) => {
780764
match $what::parse(cursor, ctx) {
781765
Ok(ParseResult::New(item, declaration)) => {
782-
let id = ItemId::next();
766+
let id = ctx.next_item_id();
783767

784768
ctx.add_item(Item::new(id, comment, annotations,
785769
relevant_parent_id,
@@ -855,7 +839,8 @@ impl ClangItemParser for Item {
855839
parent_id: Option<ItemId>,
856840
ctx: &mut BindgenContext)
857841
-> ItemId {
858-
Self::from_ty_or_ref_with_id(ItemId::next(),
842+
let id = ctx.next_item_id();
843+
Self::from_ty_or_ref_with_id(id,
859844
ty,
860845
location,
861846
parent_id,
@@ -925,7 +910,8 @@ impl ClangItemParser for Item {
925910
parent_id: Option<ItemId>,
926911
ctx: &mut BindgenContext)
927912
-> Result<ItemId, ParseError> {
928-
Self::from_ty_with_id(ItemId::next(), ty, location, parent_id, ctx)
913+
let id = ctx.next_item_id();
914+
Self::from_ty_with_id(id, ty, location, parent_id, ctx)
929915
}
930916

931917
/// This is one of the trickiest methods you'll find (probably along with
@@ -1109,7 +1095,8 @@ impl ClangItemParser for Item {
11091095
-> ItemId
11101096
where S: Into<String>,
11111097
{
1112-
Self::named_type_with_id(ItemId::next(), name, default, parent_id, ctx)
1098+
let id = ctx.next_item_id();
1099+
Self::named_type_with_id(id, name, default, parent_id, ctx)
11131100
}
11141101
}
11151102

src/ir/module.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
use clang;
44
use parse::{ClangSubItemParser, ParseError, ParseResult};
55
use parse_one;
6-
use super::context::BindgenContext;
7-
use super::item::ItemId;
6+
use super::context::{BindgenContext, ItemId};
87

98
/// A module, as in, a C++ namespace.
109
#[derive(Clone, Debug)]

src/ir/ty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
use clang::{self, Cursor};
44
use parse::{ClangItemParser, ParseError, ParseResult};
55
use super::comp::CompInfo;
6-
use super::context::BindgenContext;
6+
use super::context::{BindgenContext, ItemId};
77
use super::enum_ty::Enum;
88
use super::function::FunctionSig;
99
use super::int::IntKind;
10-
use super::item::{Item, ItemId};
10+
use super::item::Item;
1111
use super::layout::Layout;
1212
use super::type_collector::{ItemSet, TypeCollector};
1313

src/ir/type_collector.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Collecting type items.
22
33
use std::collections::BTreeSet;
4-
use super::context::BindgenContext;
5-
use super::item::ItemId;
4+
use super::context::{BindgenContext, ItemId};
65

76
/// A set of items.
87
pub type ItemSet = BTreeSet<ItemId>;

src/ir/var.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use cexpr;
44
use clang;
55
use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};
66
use std::num::Wrapping;
7-
use super::context::BindgenContext;
7+
use super::context::{BindgenContext, ItemId};
88
use super::function::cursor_mangling;
99
use super::int::IntKind;
10-
use super::item::{Item, ItemId};
10+
use super::item::Item;
1111
use super::ty::TypeKind;
1212

1313
/// A `Var` is our intermediate representation of a variable.

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ mod codegen {
7878
include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
7979
}
8080

81-
use ir::context::BindgenContext;
82-
use ir::item::{Item, ItemId};
81+
use ir::context::{BindgenContext, ItemId};
82+
use ir::item::Item;
8383
use parse::{ClangItemParser, ParseError};
8484
use regex_set::RegexSet;
8585

src/parse.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Common traits and types related to parsing our IR from Clang cursors.
22
33
use clang;
4-
use ir::context::BindgenContext;
5-
use ir::item::ItemId;
4+
use ir::context::{BindgenContext, ItemId};
65
use ir::ty::TypeKind;
76

87
/// Not so much an error in the traditional sense, but a control flow message

0 commit comments

Comments
 (0)