Skip to content

Commit b59ffb8

Browse files
committed
Add HasVtable::has_vtable_ptr for querying if a type has its own vtable pointer
1 parent 500593b commit b59ffb8

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

src/ir/analysis/has_vtable.rs

+4
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,8 @@ impl<'ctx> From<HasVtableAnalysis<'ctx>> for HashMap<ItemId, HasVtableResult> {
242242
pub trait HasVtable {
243243
/// Return `true` if this thing has vtable, `false` otherwise.
244244
fn has_vtable(&self, ctx: &BindgenContext) -> bool;
245+
246+
/// Return `true` if this thing has an actual vtable pointer in itself, as
247+
/// opposed to transitively in a base member.
248+
fn has_vtable_ptr(&self, ctx: &BindgenContext) -> bool;
245249
}

src/ir/comp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ impl CompInfo {
10121012

10131013
/// Is this compound type unsized?
10141014
pub fn is_unsized(&self, ctx: &BindgenContext, id: TypeId) -> bool {
1015-
!ctx.lookup_has_vtable(id) && self.fields().is_empty() &&
1015+
!id.has_vtable(ctx) && self.fields().is_empty() &&
10161016
self.base_members.iter().all(|base| {
10171017
ctx.resolve_type(base.ty).canonical_type(ctx).is_unsized(
10181018
ctx,

src/ir/context.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1267,15 +1267,20 @@ impl BindgenContext {
12671267
}
12681268

12691269
/// Look up whether the item with `id` has vtable or not.
1270-
pub fn lookup_has_vtable(&self, id: TypeId) -> bool {
1270+
pub fn lookup_has_vtable(&self, id: TypeId) -> HasVtableResult {
12711271
assert!(
12721272
self.in_codegen_phase(),
12731273
"We only compute vtables when we enter codegen"
12741274
);
12751275

12761276
// Look up the computed value for whether the item with `id` has a
12771277
// vtable or not.
1278-
self.have_vtable.as_ref().unwrap().contains_key(&id.into())
1278+
self.have_vtable
1279+
.as_ref()
1280+
.unwrap()
1281+
.get(&id.into())
1282+
.cloned()
1283+
.unwrap_or(HasVtableResult::No)
12791284
}
12801285

12811286
/// Compute whether the type has a destructor.

src/ir/item.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Bindgen's core intermediate representation type.
22
3-
use super::analysis::HasVtable;
3+
use super::analysis::{HasVtable, HasVtableResult};
44
use super::annotations::Annotations;
55
use super::comment;
66
use super::comp::MethodKind;
@@ -1001,15 +1001,29 @@ where
10011001
fn has_vtable(&self, ctx: &BindgenContext) -> bool {
10021002
let id: ItemId = (*self).into();
10031003
id.as_type_id(ctx)
1004-
.map_or(false, |id| ctx.lookup_has_vtable(id))
1004+
.map_or(false, |id| match ctx.lookup_has_vtable(id) {
1005+
HasVtableResult::No => false,
1006+
_ => true,
1007+
})
1008+
}
1009+
1010+
fn has_vtable_ptr(&self, ctx: &BindgenContext) -> bool {
1011+
let id: ItemId = (*self).into();
1012+
id.as_type_id(ctx)
1013+
.map_or(false, |id| match ctx.lookup_has_vtable(id) {
1014+
HasVtableResult::SelfHasVtable => true,
1015+
_ => false,
1016+
})
10051017
}
10061018
}
10071019

10081020
impl HasVtable for Item {
10091021
fn has_vtable(&self, ctx: &BindgenContext) -> bool {
1010-
self.id()
1011-
.as_type_id(ctx)
1012-
.map_or(false, |id| ctx.lookup_has_vtable(id))
1022+
self.id().has_vtable(ctx)
1023+
}
1024+
1025+
fn has_vtable_ptr(&self, ctx: &BindgenContext) -> bool {
1026+
self.id().has_vtable_ptr(ctx)
10131027
}
10141028
}
10151029

0 commit comments

Comments
 (0)