Skip to content

ir: Make the id local to the context. #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use aster;

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

mod utils {
use aster;
use ir::context::BindgenContext;
use ir::item::{Item, ItemCanonicalPath, ItemId};
use ir::context::{BindgenContext, ItemId};
use ir::item::{Item, ItemCanonicalPath};
use ir::ty::TypeKind;
use std::mem;
use super::ItemToRustTy;
Expand Down
4 changes: 2 additions & 2 deletions src/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use parse::{ClangItemParser, ParseError};
use std::cell::Cell;
use std::cmp;
use super::annotations::Annotations;
use super::context::BindgenContext;
use super::item::{Item, ItemId};
use super::context::{BindgenContext, ItemId};
use super::item::Item;
use super::layout::Layout;
use super::ty::{RUST_DERIVE_IN_ARRAY_LIMIT, Type};
use super::type_collector::{ItemSet, TypeCollector};
Expand Down
41 changes: 32 additions & 9 deletions src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::collections::{HashMap, hash_map};
use std::collections::btree_map::{self, BTreeMap};
use std::fmt;
use super::int::IntKind;
use super::item::{Item, ItemCanonicalName, ItemId};
use super::item::{Item, ItemCanonicalName};
use super::item_kind::ItemKind;
use super::module::Module;
use super::ty::{FloatKind, Type, TypeKind};
Expand All @@ -19,6 +19,19 @@ use syntax::ast::Ident;
use syntax::codemap::{DUMMY_SP, Span};
use syntax::ext::base::ExtCtxt;

/// A single identifier for an item.
///
/// TODO: Build stronger abstractions on top of this, like TypeId(ItemId)?
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ItemId(usize);

impl ItemId {
/// Get a numeric representation of this id.
pub fn as_usize(&self) -> usize {
self.0
}
}

/// A key used to index a resolved type, so we only process it once.
///
/// This is almost always a USR string (an unique identifier generated by
Expand Down Expand Up @@ -50,6 +63,9 @@ pub struct BindgenContext<'ctx> {
/// output.
items: BTreeMap<ItemId, Item>,

/// The next item id to use during this bindings regeneration.
next_item_id: ItemId,

/// Clang USR to type map. This is needed to be able to associate types with
/// item ids during parsing.
types: HashMap<TypeKey, ItemId>,
Expand Down Expand Up @@ -122,11 +138,12 @@ impl<'ctx> BindgenContext<'ctx> {
parse_options)
.expect("TranslationUnit::parse");

let root_module = Self::build_root_module();
let root_module = Self::build_root_module(ItemId(0));
let mut me = BindgenContext {
items: Default::default(),
types: Default::default(),
modules: Default::default(),
next_item_id: ItemId(1),
root_module: root_module.id(),
current_module: root_module.id(),
currently_parsed_types: vec![],
Expand Down Expand Up @@ -422,9 +439,8 @@ impl<'ctx> BindgenContext<'ctx> {
assert!(old_item.is_none(), "Inserted type twice?");
}

fn build_root_module() -> Item {
fn build_root_module(id: ItemId) -> Item {
let module = Module::new(Some("root".into()));
let id = ItemId::next();
Item::new(id, None, None, id, ItemKind::Module(module))
}

Expand Down Expand Up @@ -702,6 +718,13 @@ impl<'ctx> BindgenContext<'ctx> {
with_id
}

/// Returns the next item id to be used for an item.
pub fn next_item_id(&mut self) -> ItemId {
let ret = self.next_item_id;
self.next_item_id = ItemId(self.next_item_id.0 + 1);
ret
}

fn build_builtin_ty(&mut self,
ty: &clang::Type,
_declaration: Cursor)
Expand Down Expand Up @@ -747,7 +770,7 @@ impl<'ctx> BindgenContext<'ctx> {
let is_const = ty.is_const();
let layout = ty.fallible_layout().ok();
let ty = Type::new(Some(spelling), layout, type_kind, is_const);
let id = ItemId::next();
let id = self.next_item_id();
let item =
Item::new(id, None, None, self.root_module, ItemKind::Type(ty));
self.add_builtin_item(item);
Expand Down Expand Up @@ -841,11 +864,11 @@ impl<'ctx> BindgenContext<'ctx> {
use clangll::*;
assert!(cursor.kind() == CXCursor_Namespace, "Be a nice person");
let cursor = cursor.canonical();
let module_id = match self.modules.get(&cursor) {
Some(id) => return *id,
None => ItemId::next(),
};
if let Some(id) = self.modules.get(&cursor) {
return *id;
}

let module_id = self.next_item_id();
let module_name = self.translation_unit
.tokens(&cursor)
.and_then(|tokens| {
Expand Down
4 changes: 2 additions & 2 deletions src/ir/enum_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use clang;
use parse::{ClangItemParser, ParseError};
use super::context::BindgenContext;
use super::item::{Item, ItemId};
use super::context::{BindgenContext, ItemId};
use super::item::Item;
use super::ty::TypeKind;

/// A C/C++ enumeration.
Expand Down
4 changes: 2 additions & 2 deletions src/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use clang;
use clangll::Enum_CXCallingConv;
use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};
use super::context::BindgenContext;
use super::item::{Item, ItemId};
use super::context::{BindgenContext, ItemId};
use super::item::Item;
use super::ty::TypeKind;
use super::type_collector::{ItemSet, TypeCollector};
use syntax::abi;
Expand Down
33 changes: 10 additions & 23 deletions src/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use clang;
use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};
use regex::Regex;
use std::cell::{Cell, RefCell};
use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
use super::annotations::Annotations;
use super::context::BindgenContext;
use super::context::{BindgenContext, ItemId};
use super::function::Function;
use super::item_kind::ItemKind;
use super::module::Module;
Expand Down Expand Up @@ -78,21 +77,6 @@ impl<'a, 'b> Iterator for ItemAncestorsIter<'a, 'b>
}
}

/// A single identifier for an item.
///
/// TODO: Build stronger abstractions on top of this, like TypeId(ItemId)?
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ItemId(usize);

impl ItemId {
/// Allocate the next `ItemId`.
pub fn next() -> Self {
static NEXT_ITEM_ID: AtomicUsize = ATOMIC_USIZE_INIT;
let next_id = NEXT_ITEM_ID.fetch_add(1, Ordering::Relaxed);
ItemId(next_id)
}
}

// Pure convenience
impl ItemCanonicalName for ItemId {
fn canonical_name(&self, ctx: &BindgenContext) -> String {
Expand Down Expand Up @@ -667,7 +651,7 @@ impl Item {
// Note that this `id_` prefix prevents (really unlikely) collisions
// between the global id and the local id of an item with the same
// parent.
format!("id_{}", self.id().0)
format!("id_{}", self.id().as_usize())
}

fn make_exposed_name(&self,
Expand Down Expand Up @@ -747,7 +731,7 @@ impl ClangItemParser for Item {
}

let ty = Type::new(None, None, kind, is_const);
let id = ItemId::next();
let id = ctx.next_item_id();
let module = ctx.root_module();
ctx.add_item(Item::new(id, None, None, module, ItemKind::Type(ty)),
None,
Expand Down Expand Up @@ -779,7 +763,7 @@ impl ClangItemParser for Item {
($what:ident) => {
match $what::parse(cursor, ctx) {
Ok(ParseResult::New(item, declaration)) => {
let id = ItemId::next();
let id = ctx.next_item_id();

ctx.add_item(Item::new(id, comment, annotations,
relevant_parent_id,
Expand Down Expand Up @@ -855,7 +839,8 @@ impl ClangItemParser for Item {
parent_id: Option<ItemId>,
ctx: &mut BindgenContext)
-> ItemId {
Self::from_ty_or_ref_with_id(ItemId::next(),
let id = ctx.next_item_id();
Self::from_ty_or_ref_with_id(id,
ty,
location,
parent_id,
Expand Down Expand Up @@ -925,7 +910,8 @@ impl ClangItemParser for Item {
parent_id: Option<ItemId>,
ctx: &mut BindgenContext)
-> Result<ItemId, ParseError> {
Self::from_ty_with_id(ItemId::next(), ty, location, parent_id, ctx)
let id = ctx.next_item_id();
Self::from_ty_with_id(id, ty, location, parent_id, ctx)
}

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

Expand Down
3 changes: 1 addition & 2 deletions src/ir/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use clang;
use parse::{ClangSubItemParser, ParseError, ParseResult};
use parse_one;
use super::context::BindgenContext;
use super::item::ItemId;
use super::context::{BindgenContext, ItemId};

/// A module, as in, a C++ namespace.
#[derive(Clone, Debug)]
Expand Down
4 changes: 2 additions & 2 deletions src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use clang::{self, Cursor};
use parse::{ClangItemParser, ParseError, ParseResult};
use super::comp::CompInfo;
use super::context::BindgenContext;
use super::context::{BindgenContext, ItemId};
use super::enum_ty::Enum;
use super::function::FunctionSig;
use super::int::IntKind;
use super::item::{Item, ItemId};
use super::item::Item;
use super::layout::Layout;
use super::type_collector::{ItemSet, TypeCollector};

Expand Down
3 changes: 1 addition & 2 deletions src/ir/type_collector.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Collecting type items.

use std::collections::BTreeSet;
use super::context::BindgenContext;
use super::item::ItemId;
use super::context::{BindgenContext, ItemId};

/// A set of items.
pub type ItemSet = BTreeSet<ItemId>;
Expand Down
4 changes: 2 additions & 2 deletions src/ir/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use cexpr;
use clang;
use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};
use std::num::Wrapping;
use super::context::BindgenContext;
use super::context::{BindgenContext, ItemId};
use super::function::cursor_mangling;
use super::int::IntKind;
use super::item::{Item, ItemId};
use super::item::Item;
use super::ty::TypeKind;

/// A `Var` is our intermediate representation of a variable.
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ mod codegen {
include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
}

use ir::context::BindgenContext;
use ir::item::{Item, ItemId};
use ir::context::{BindgenContext, ItemId};
use ir::item::Item;
use parse::{ClangItemParser, ParseError};
use regex_set::RegexSet;

Expand Down
3 changes: 1 addition & 2 deletions src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Common traits and types related to parsing our IR from Clang cursors.

use clang;
use ir::context::BindgenContext;
use ir::item::ItemId;
use ir::context::{BindgenContext, ItemId};
use ir::ty::TypeKind;

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