|
| 1 | +//! Determining which types have destructors |
| 2 | +
|
| 3 | +use super::{ConstrainResult, MonotoneFramework, generate_dependencies}; |
| 4 | +use ir::context::{BindgenContext, ItemId}; |
| 5 | +use ir::traversal::EdgeKind; |
| 6 | +use ir::comp::{CompKind, Field, FieldMethods}; |
| 7 | +use ir::ty::TypeKind; |
| 8 | +use std::collections::HashMap; |
| 9 | +use std::collections::HashSet; |
| 10 | + |
| 11 | +/// An analysis that finds for each IR item whether it has a destructor or not |
| 12 | +/// |
| 13 | +/// We use the monotone function `has destructor`, defined as follows: |
| 14 | +/// |
| 15 | +/// * If T is a type alias, a templated alias, or an indirection to another type, |
| 16 | +/// T has a destructor if the type T refers to has a destructor. |
| 17 | +/// * If T is a compound type, T has a destructor if we saw a destructor when parsing it, |
| 18 | +/// or if it's a struct, T has a destructor if any of its base members has a destructor, |
| 19 | +/// or if any of its fields have a destructor. |
| 20 | +/// * If T is an instantiation of an abstract template definition, T has |
| 21 | +/// a destructor if its template definition has a destructor, |
| 22 | +/// or if any of the template arguments has a destructor. |
| 23 | +/// * If T is the type of a field, that field has a destructor if it's not a bitfield, |
| 24 | +/// and if T has a destructor. |
| 25 | +#[derive(Debug, Clone)] |
| 26 | +pub struct HasDestructorAnalysis<'ctx, 'gen> |
| 27 | +where |
| 28 | + 'gen: 'ctx, |
| 29 | +{ |
| 30 | + ctx: &'ctx BindgenContext<'gen>, |
| 31 | + |
| 32 | + // The incremental result of this analysis's computation. Everything in this |
| 33 | + // set definitely has a destructor. |
| 34 | + have_destructor: HashSet<ItemId>, |
| 35 | + |
| 36 | + // Dependencies saying that if a key ItemId has been inserted into the |
| 37 | + // `have_destructor` set, then each of the ids in Vec<ItemId> need to be |
| 38 | + // considered again. |
| 39 | + // |
| 40 | + // This is a subset of the natural IR graph with reversed edges, where we |
| 41 | + // only include the edges from the IR graph that can affect whether a type |
| 42 | + // has a destructor or not. |
| 43 | + dependencies: HashMap<ItemId, Vec<ItemId>>, |
| 44 | +} |
| 45 | + |
| 46 | +impl<'ctx, 'gen> HasDestructorAnalysis<'ctx, 'gen> { |
| 47 | + fn consider_edge(kind: EdgeKind) -> bool { |
| 48 | + match kind { |
| 49 | + // These are the only edges that can affect whether a type has a |
| 50 | + // destructor or not. |
| 51 | + EdgeKind::TypeReference | |
| 52 | + EdgeKind::BaseMember | |
| 53 | + EdgeKind::Field | |
| 54 | + EdgeKind::TemplateArgument | |
| 55 | + EdgeKind::TemplateDeclaration => true, |
| 56 | + _ => false, |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + fn insert(&mut self, id: ItemId) -> ConstrainResult { |
| 61 | + let was_not_already_in_set = self.have_destructor.insert(id); |
| 62 | + assert!( |
| 63 | + was_not_already_in_set, |
| 64 | + "We shouldn't try and insert {:?} twice because if it was \ |
| 65 | + already in the set, `constrain` should have exited early.", |
| 66 | + id |
| 67 | + ); |
| 68 | + ConstrainResult::Changed |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl<'ctx, 'gen> MonotoneFramework for HasDestructorAnalysis<'ctx, 'gen> { |
| 73 | + type Node = ItemId; |
| 74 | + type Extra = &'ctx BindgenContext<'gen>; |
| 75 | + type Output = HashSet<ItemId>; |
| 76 | + |
| 77 | + fn new(ctx: &'ctx BindgenContext<'gen>) -> Self { |
| 78 | + let have_destructor = HashSet::new(); |
| 79 | + let dependencies = generate_dependencies(ctx, Self::consider_edge); |
| 80 | + |
| 81 | + HasDestructorAnalysis { |
| 82 | + ctx, |
| 83 | + have_destructor, |
| 84 | + dependencies, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + fn initial_worklist(&self) -> Vec<ItemId> { |
| 89 | + self.ctx.whitelisted_items().iter().cloned().collect() |
| 90 | + } |
| 91 | + |
| 92 | + fn constrain(&mut self, id: ItemId) -> ConstrainResult { |
| 93 | + if self.have_destructor.contains(&id) { |
| 94 | + // We've already computed that this type has a destructor and that can't |
| 95 | + // change. |
| 96 | + return ConstrainResult::Same; |
| 97 | + } |
| 98 | + |
| 99 | + let item = self.ctx.resolve_item(id); |
| 100 | + let ty = match item.as_type() { |
| 101 | + None => return ConstrainResult::Same, |
| 102 | + Some(ty) => ty, |
| 103 | + }; |
| 104 | + |
| 105 | + match *ty.kind() { |
| 106 | + TypeKind::TemplateAlias(t, _) | |
| 107 | + TypeKind::Alias(t) | |
| 108 | + TypeKind::ResolvedTypeRef(t) => { |
| 109 | + if self.have_destructor.contains(&t) { |
| 110 | + self.insert(id) |
| 111 | + } else { |
| 112 | + ConstrainResult::Same |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + TypeKind::Comp(ref info) => { |
| 117 | + if info.has_own_destructor() { |
| 118 | + return self.insert(id); |
| 119 | + } |
| 120 | + |
| 121 | + match info.kind() { |
| 122 | + CompKind::Union => ConstrainResult::Same, |
| 123 | + CompKind::Struct => { |
| 124 | + let base_or_field_destructor = |
| 125 | + info.base_members().iter().any(|base| { |
| 126 | + self.have_destructor.contains(&base.ty) |
| 127 | + }) || |
| 128 | + info.fields().iter().any(|field| { |
| 129 | + match *field { |
| 130 | + Field::DataMember(ref data) => |
| 131 | + self.have_destructor.contains(&data.ty()), |
| 132 | + Field::Bitfields(_) => false |
| 133 | + } |
| 134 | + }); |
| 135 | + if base_or_field_destructor { |
| 136 | + self.insert(id) |
| 137 | + } else { |
| 138 | + ConstrainResult::Same |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + TypeKind::TemplateInstantiation(ref inst) => { |
| 145 | + let definition_or_arg_destructor = |
| 146 | + self.have_destructor.contains(&inst.template_definition()) |
| 147 | + || |
| 148 | + inst.template_arguments().iter().any(|arg| { |
| 149 | + self.have_destructor.contains(arg) |
| 150 | + }); |
| 151 | + if definition_or_arg_destructor { |
| 152 | + self.insert(id) |
| 153 | + } else { |
| 154 | + ConstrainResult::Same |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + _ => ConstrainResult::Same, |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + fn each_depending_on<F>(&self, id: ItemId, mut f: F) |
| 163 | + where |
| 164 | + F: FnMut(ItemId), |
| 165 | + { |
| 166 | + if let Some(edges) = self.dependencies.get(&id) { |
| 167 | + for item in edges { |
| 168 | + trace!("enqueue {:?} into worklist", item); |
| 169 | + f(*item); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +impl<'ctx, 'gen> From<HasDestructorAnalysis<'ctx, 'gen>> for HashSet<ItemId> { |
| 176 | + fn from(analysis: HasDestructorAnalysis<'ctx, 'gen>) -> Self { |
| 177 | + analysis.have_destructor |
| 178 | + } |
| 179 | +} |
0 commit comments