Skip to content

Commit 3495d03

Browse files
committed
Make methods/constructors/destructors use FunctionId
And also allow ID comparison across ID types, as this makes implementing the above much easier.
1 parent 3ef31e8 commit 3495d03

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed

src/codegen/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ impl CodeGenerator for Module {
379379
}
380380
}
381381

382-
if item.id() == ctx.root_module().into() {
382+
if item.id() == ctx.root_module() {
383383
if result.saw_bindgen_union {
384384
utils::prepend_union_types(ctx, &mut *result);
385385
}

src/ir/analysis/template_params.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ impl<'ctx> UsedTemplateParameters<'ctx> {
277277

278278
let params = decl.self_template_params(self.ctx).unwrap_or(vec![]);
279279

280-
debug_assert!(this_id != instantiation.template_definition().into());
280+
debug_assert!(this_id != instantiation.template_definition());
281281
let used_by_def = self.used
282282
.get(&instantiation.template_definition().into())
283283
.expect("Should have a used entry for instantiation's template definition")

src/ir/comp.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::analysis::HasVtable;
44
use super::annotations::Annotations;
5-
use super::context::{BindgenContext, ItemId, TypeId, VarId};
5+
use super::context::{BindgenContext, FunctionId, ItemId, TypeId, VarId};
66
use super::dot::DotAttributes;
77
use super::item::{IsOpaque, Item};
88
use super::layout::Layout;
@@ -53,13 +53,13 @@ pub struct Method {
5353
/// item, but a `Function` one.
5454
///
5555
/// This is tricky and probably this field should be renamed.
56-
signature: ItemId,
56+
signature: FunctionId,
5757
is_const: bool,
5858
}
5959

6060
impl Method {
6161
/// Construct a new `Method`.
62-
pub fn new(kind: MethodKind, signature: ItemId, is_const: bool) -> Self {
62+
pub fn new(kind: MethodKind, signature: FunctionId, is_const: bool) -> Self {
6363
Method {
6464
kind: kind,
6565
signature: signature,
@@ -94,8 +94,8 @@ impl Method {
9494
self.kind == MethodKind::Static
9595
}
9696

97-
/// Get the `ItemId` for the `Function` signature for this method.
98-
pub fn signature(&self) -> ItemId {
97+
/// Get the id for the `Function` signature for this method.
98+
pub fn signature(&self) -> FunctionId {
9999
self.signature
100100
}
101101

@@ -831,11 +831,11 @@ pub struct CompInfo {
831831
methods: Vec<Method>,
832832

833833
/// The different constructors this struct or class contains.
834-
constructors: Vec<ItemId>,
834+
constructors: Vec<FunctionId>,
835835

836836
/// The destructor of this type. The bool represents whether this destructor
837837
/// is virtual.
838-
destructor: Option<(bool, ItemId)>,
838+
destructor: Option<(bool, FunctionId)>,
839839

840840
/// Vector of classes this one inherits from.
841841
base_members: Vec<Base>,
@@ -984,12 +984,12 @@ impl CompInfo {
984984
}
985985

986986
/// Get this type's set of constructors.
987-
pub fn constructors(&self) -> &[ItemId] {
987+
pub fn constructors(&self) -> &[FunctionId] {
988988
&self.constructors
989989
}
990990

991991
/// Get this type's destructor.
992-
pub fn destructor(&self) -> Option<(bool, ItemId)> {
992+
pub fn destructor(&self) -> Option<(bool, FunctionId)> {
993993
self.destructor
994994
}
995995

@@ -1233,6 +1233,8 @@ impl CompInfo {
12331233
_ => return CXChildVisit_Continue,
12341234
};
12351235

1236+
let signature = signature.expect_function_id(ctx);
1237+
12361238
match cur.kind() {
12371239
CXCursor_Constructor => {
12381240
ci.constructors.push(signature);
@@ -1497,14 +1499,14 @@ impl Trace for CompInfo {
14971499

14981500
for method in self.methods() {
14991501
if method.is_destructor() {
1500-
tracer.visit_kind(method.signature, EdgeKind::Destructor);
1502+
tracer.visit_kind(method.signature.into(), EdgeKind::Destructor);
15011503
} else {
1502-
tracer.visit_kind(method.signature, EdgeKind::Method);
1504+
tracer.visit_kind(method.signature.into(), EdgeKind::Method);
15031505
}
15041506
}
15051507

1506-
for &ctor in self.constructors() {
1507-
tracer.visit_kind(ctor, EdgeKind::Constructor);
1508+
for ctor in self.constructors() {
1509+
tracer.visit_kind(ctor.into(), EdgeKind::Constructor);
15081510
}
15091511

15101512
// Base members and fields are not generated for opaque types (but all

src/ir/context.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use std::iter::IntoIterator;
3131
use std::mem;
3232

3333
/// An identifier for some kind of IR item.
34-
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
34+
#[derive(Debug, Copy, Clone, Eq, PartialOrd, Ord, Hash)]
3535
pub struct ItemId(usize);
3636

3737
macro_rules! item_id_newtype {
@@ -47,7 +47,7 @@ macro_rules! item_id_newtype {
4747
unchecked = $unchecked:ident;
4848
) => {
4949
$( #[$attr] )*
50-
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
50+
#[derive(Debug, Copy, Clone, Eq, PartialOrd, Ord, Hash)]
5151
pub struct $name(ItemId);
5252

5353
impl $name {
@@ -58,6 +58,16 @@ macro_rules! item_id_newtype {
5858
}
5959
}
6060

61+
impl<T> ::std::cmp::PartialEq<T> for $name
62+
where
63+
T: Copy + Into<ItemId>
64+
{
65+
fn eq(&self, rhs: &T) -> bool {
66+
let rhs: ItemId = (*rhs).into();
67+
self.0 == rhs
68+
}
69+
}
70+
6171
impl From<$name> for ItemId {
6272
fn from(id: $name) -> ItemId {
6373
id.0
@@ -186,6 +196,16 @@ impl ItemId {
186196
}
187197
}
188198

199+
impl<T> ::std::cmp::PartialEq<T> for ItemId
200+
where
201+
T: Copy + Into<ItemId>
202+
{
203+
fn eq(&self, rhs: &T) -> bool {
204+
let rhs: ItemId = (*rhs).into();
205+
self.0 == rhs.0
206+
}
207+
}
208+
189209
impl<T> CanDeriveDebug for T
190210
where
191211
T: Copy + Into<ItemId>
@@ -651,7 +671,7 @@ impl BindgenContext {
651671
let is_template_instantiation = is_type &&
652672
item.expect_type().is_template_instantiation();
653673

654-
if item.id() != self.root_module.into() {
674+
if item.id() != self.root_module {
655675
self.add_item_to_module(&item);
656676
}
657677

@@ -715,7 +735,7 @@ impl BindgenContext {
715735
/// codegen'd, even if its parent is not whitelisted. See issue #769 for
716736
/// details.
717737
fn add_item_to_module(&mut self, item: &Item) {
718-
assert!(item.id() != self.root_module.into());
738+
assert!(item.id() != self.root_module);
719739
assert!(!self.items.contains_key(&item.id()));
720740

721741
if let Some(parent) = self.items.get_mut(&item.parent_id()) {
@@ -1191,7 +1211,7 @@ impl BindgenContext {
11911211
assert!(self.current_module == self.root_module);
11921212

11931213
for (&id, _item) in self.items() {
1194-
if id == self.root_module.into() {
1214+
if id == self.root_module {
11951215
continue;
11961216
}
11971217

src/ir/item.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ impl Item {
577577
// FIXME: Workaround for some types falling behind when parsing weird
578578
// stl classes, for example.
579579
if ctx.options().enable_cxx_namespaces && self.kind().is_module() &&
580-
self.id() != ctx.root_module().into()
580+
self.id() != ctx.root_module()
581581
{
582582
return false;
583583
}
@@ -589,7 +589,7 @@ impl Item {
589589
None => return false,
590590
};
591591

592-
if parent_item.id() == ctx.root_module().into() {
592+
if parent_item.id() == ctx.root_module() {
593593
return true;
594594
} else if ctx.options().enable_cxx_namespaces ||
595595
!parent_item.kind().is_module()
@@ -834,7 +834,7 @@ impl Item {
834834
let mut names: Vec<_> = target
835835
.parent_id()
836836
.ancestors(ctx)
837-
.filter(|id| *id != ctx.root_module().into())
837+
.filter(|id| *id != ctx.root_module())
838838
.take_while(|id| {
839839
// Stop iterating ancestors once we reach a non-inline namespace
840840
// when opt.within_namespaces is set.

0 commit comments

Comments
 (0)