-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #932 - bd339:has_destructor_fp, r=fitzgen
Rewrite `has destructor` analysis as a fixed-point analysis Fixes #927 . Note that this creates a dependency between the "cannot derive copy" and "has destructor" analysis, i.e. the "has destructor" analysis must run before the "cannot derive copy" analysis, because "cannot derive copy" needs the results of "has destructor".
- Loading branch information
Showing
7 changed files
with
213 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
//! Determining which types have destructors | ||
use super::{ConstrainResult, MonotoneFramework, generate_dependencies}; | ||
use ir::context::{BindgenContext, ItemId}; | ||
use ir::traversal::EdgeKind; | ||
use ir::comp::{CompKind, Field, FieldMethods}; | ||
use ir::ty::TypeKind; | ||
use std::collections::HashMap; | ||
use std::collections::HashSet; | ||
|
||
/// An analysis that finds for each IR item whether it has a destructor or not | ||
/// | ||
/// We use the monotone function `has destructor`, defined as follows: | ||
/// | ||
/// * If T is a type alias, a templated alias, or an indirection to another type, | ||
/// T has a destructor if the type T refers to has a destructor. | ||
/// * If T is a compound type, T has a destructor if we saw a destructor when parsing it, | ||
/// or if it's a struct, T has a destructor if any of its base members has a destructor, | ||
/// or if any of its fields have a destructor. | ||
/// * If T is an instantiation of an abstract template definition, T has | ||
/// a destructor if its template definition has a destructor, | ||
/// or if any of the template arguments has a destructor. | ||
/// * If T is the type of a field, that field has a destructor if it's not a bitfield, | ||
/// and if T has a destructor. | ||
#[derive(Debug, Clone)] | ||
pub struct HasDestructorAnalysis<'ctx, 'gen> | ||
where | ||
'gen: 'ctx, | ||
{ | ||
ctx: &'ctx BindgenContext<'gen>, | ||
|
||
// The incremental result of this analysis's computation. Everything in this | ||
// set definitely has a destructor. | ||
have_destructor: HashSet<ItemId>, | ||
|
||
// Dependencies saying that if a key ItemId has been inserted into the | ||
// `have_destructor` set, then each of the ids in Vec<ItemId> need to be | ||
// considered again. | ||
// | ||
// This is a subset of the natural IR graph with reversed edges, where we | ||
// only include the edges from the IR graph that can affect whether a type | ||
// has a destructor or not. | ||
dependencies: HashMap<ItemId, Vec<ItemId>>, | ||
} | ||
|
||
impl<'ctx, 'gen> HasDestructorAnalysis<'ctx, 'gen> { | ||
fn consider_edge(kind: EdgeKind) -> bool { | ||
match kind { | ||
// These are the only edges that can affect whether a type has a | ||
// destructor or not. | ||
EdgeKind::TypeReference | | ||
EdgeKind::BaseMember | | ||
EdgeKind::Field | | ||
EdgeKind::TemplateArgument | | ||
EdgeKind::TemplateDeclaration => true, | ||
_ => false, | ||
} | ||
} | ||
|
||
fn insert(&mut self, id: ItemId) -> ConstrainResult { | ||
let was_not_already_in_set = self.have_destructor.insert(id); | ||
assert!( | ||
was_not_already_in_set, | ||
"We shouldn't try and insert {:?} twice because if it was \ | ||
already in the set, `constrain` should have exited early.", | ||
id | ||
); | ||
ConstrainResult::Changed | ||
} | ||
} | ||
|
||
impl<'ctx, 'gen> MonotoneFramework for HasDestructorAnalysis<'ctx, 'gen> { | ||
type Node = ItemId; | ||
type Extra = &'ctx BindgenContext<'gen>; | ||
type Output = HashSet<ItemId>; | ||
|
||
fn new(ctx: &'ctx BindgenContext<'gen>) -> Self { | ||
let have_destructor = HashSet::new(); | ||
let dependencies = generate_dependencies(ctx, Self::consider_edge); | ||
|
||
HasDestructorAnalysis { | ||
ctx, | ||
have_destructor, | ||
dependencies, | ||
} | ||
} | ||
|
||
fn initial_worklist(&self) -> Vec<ItemId> { | ||
self.ctx.whitelisted_items().iter().cloned().collect() | ||
} | ||
|
||
fn constrain(&mut self, id: ItemId) -> ConstrainResult { | ||
if self.have_destructor.contains(&id) { | ||
// We've already computed that this type has a destructor and that can't | ||
// change. | ||
return ConstrainResult::Same; | ||
} | ||
|
||
let item = self.ctx.resolve_item(id); | ||
let ty = match item.as_type() { | ||
None => return ConstrainResult::Same, | ||
Some(ty) => ty, | ||
}; | ||
|
||
match *ty.kind() { | ||
TypeKind::TemplateAlias(t, _) | | ||
TypeKind::Alias(t) | | ||
TypeKind::ResolvedTypeRef(t) => { | ||
if self.have_destructor.contains(&t) { | ||
self.insert(id) | ||
} else { | ||
ConstrainResult::Same | ||
} | ||
} | ||
|
||
TypeKind::Comp(ref info) => { | ||
if info.has_own_destructor() { | ||
return self.insert(id); | ||
} | ||
|
||
match info.kind() { | ||
CompKind::Union => ConstrainResult::Same, | ||
CompKind::Struct => { | ||
let base_or_field_destructor = | ||
info.base_members().iter().any(|base| { | ||
self.have_destructor.contains(&base.ty) | ||
}) || | ||
info.fields().iter().any(|field| { | ||
match *field { | ||
Field::DataMember(ref data) => | ||
self.have_destructor.contains(&data.ty()), | ||
Field::Bitfields(_) => false | ||
} | ||
}); | ||
if base_or_field_destructor { | ||
self.insert(id) | ||
} else { | ||
ConstrainResult::Same | ||
} | ||
} | ||
} | ||
} | ||
|
||
TypeKind::TemplateInstantiation(ref inst) => { | ||
let definition_or_arg_destructor = | ||
self.have_destructor.contains(&inst.template_definition()) | ||
|| | ||
inst.template_arguments().iter().any(|arg| { | ||
self.have_destructor.contains(arg) | ||
}); | ||
if definition_or_arg_destructor { | ||
self.insert(id) | ||
} else { | ||
ConstrainResult::Same | ||
} | ||
} | ||
|
||
_ => ConstrainResult::Same, | ||
} | ||
} | ||
|
||
fn each_depending_on<F>(&self, id: ItemId, mut f: F) | ||
where | ||
F: FnMut(ItemId), | ||
{ | ||
if let Some(edges) = self.dependencies.get(&id) { | ||
for item in edges { | ||
trace!("enqueue {:?} into worklist", item); | ||
f(*item); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<'ctx, 'gen> From<HasDestructorAnalysis<'ctx, 'gen>> for HashSet<ItemId> { | ||
fn from(analysis: HasDestructorAnalysis<'ctx, 'gen>) -> Self { | ||
analysis.have_destructor | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters