Skip to content

Commit 74465b7

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

File tree

12 files changed

+61
-54
lines changed

12 files changed

+61
-54
lines changed

src/codegen/mod.rs

+4-4
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

+2-2
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

+32-9
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,19 @@ 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+
/// Get a numeric representation of this id.
30+
pub fn as_usize(&self) -> usize {
31+
self.0
32+
}
33+
}
34+
2235
/// A key used to index a resolved type, so we only process it once.
2336
///
2437
/// This is almost always a USR string (an unique identifier generated by
@@ -50,6 +63,9 @@ pub struct BindgenContext<'ctx> {
5063
/// output.
5164
items: BTreeMap<ItemId, Item>,
5265

66+
/// The next item id to use during this bindings regeneration.
67+
next_item_id: ItemId,
68+
5369
/// Clang USR to type map. This is needed to be able to associate types with
5470
/// item ids during parsing.
5571
types: HashMap<TypeKey, ItemId>,
@@ -122,11 +138,12 @@ impl<'ctx> BindgenContext<'ctx> {
122138
parse_options)
123139
.expect("TranslationUnit::parse");
124140

125-
let root_module = Self::build_root_module();
141+
let root_module = Self::build_root_module(ItemId(0));
126142
let mut me = BindgenContext {
127143
items: Default::default(),
128144
types: Default::default(),
129145
modules: Default::default(),
146+
next_item_id: ItemId(1),
130147
root_module: root_module.id(),
131148
current_module: root_module.id(),
132149
currently_parsed_types: vec![],
@@ -422,9 +439,8 @@ impl<'ctx> BindgenContext<'ctx> {
422439
assert!(old_item.is_none(), "Inserted type twice?");
423440
}
424441

425-
fn build_root_module() -> Item {
442+
fn build_root_module(id: ItemId) -> Item {
426443
let module = Module::new(Some("root".into()));
427-
let id = ItemId::next();
428444
Item::new(id, None, None, id, ItemKind::Module(module))
429445
}
430446

@@ -702,6 +718,13 @@ impl<'ctx> BindgenContext<'ctx> {
702718
with_id
703719
}
704720

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+
705728
fn build_builtin_ty(&mut self,
706729
ty: &clang::Type,
707730
_declaration: Cursor)
@@ -747,7 +770,7 @@ impl<'ctx> BindgenContext<'ctx> {
747770
let is_const = ty.is_const();
748771
let layout = ty.fallible_layout().ok();
749772
let ty = Type::new(Some(spelling), layout, type_kind, is_const);
750-
let id = ItemId::next();
773+
let id = self.next_item_id();
751774
let item =
752775
Item::new(id, None, None, self.root_module, ItemKind::Type(ty));
753776
self.add_builtin_item(item);
@@ -841,11 +864,11 @@ impl<'ctx> BindgenContext<'ctx> {
841864
use clangll::*;
842865
assert!(cursor.kind() == CXCursor_Namespace, "Be a nice person");
843866
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+
}
848870

871+
let module_id = self.next_item_id();
849872
let module_name = self.translation_unit
850873
.tokens(&cursor)
851874
.and_then(|tokens| {

src/ir/enum_ty.rs

+2-2
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

+2-2
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

+10-23
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

+1-2
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

+2-2
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

+1-2
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

+2-2
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

+2-2
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

+1-2
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)