|
| 1 | +//! Determining which types cannot be passed by value. |
| 2 | +
|
| 3 | +use super::{generate_dependencies, ConstrainResult, MonotoneFramework}; |
| 4 | +use crate::ir::comp::Field; |
| 5 | +use crate::ir::comp::FieldMethods; |
| 6 | +use crate::ir::context::{BindgenContext, ItemId}; |
| 7 | +use crate::ir::traversal::EdgeKind; |
| 8 | +use crate::ir::ty::TypeKind; |
| 9 | +use crate::{HashMap, HashSet}; |
| 10 | + |
| 11 | +/// An analysis that finds each IR item which is a type which cannot be passed by value in Rust. |
| 12 | +/// |
| 13 | +/// This is defined as follows: |
| 14 | +/// |
| 15 | +/// * If T is float or complex float with size not 32 or 64 bit (twice that for complex), it cannot |
| 16 | +/// be passed by value. |
| 17 | +/// * If T is a type alias, a templated alias or an indirection to another type, |
| 18 | +/// it matches if the type T refers to does |
| 19 | +/// * If T is a compound type, it matches if any of base memter or field does. |
| 20 | +/// * If T is an instantiation of an abstract template definition, T matches if any of the template |
| 21 | +/// arguments or template definition do. |
| 22 | +/// |
| 23 | +/// A more advanced implementation might be aware of which registers arguments will actually end up |
| 24 | +/// in and permit some signatures this rejects on a platform-specific basis. |
| 25 | +#[derive(Debug, Clone)] |
| 26 | +pub struct NeverByValue<'ctx> { |
| 27 | + ctx: &'ctx BindgenContext, |
| 28 | + |
| 29 | + // The incremental result of this analysis's computation. Everything in this |
| 30 | + // set has float. |
| 31 | + never_by_value: HashSet<ItemId>, |
| 32 | + |
| 33 | + // Dependencies saying that if a key ItemId has been inserted into the |
| 34 | + // `never_by_value` set, then each of the ids in Vec<ItemId> need to be |
| 35 | + // considered again. |
| 36 | + // |
| 37 | + // This is a subset of the natural IR graph with reversed edges, where we |
| 38 | + // only include the edges from the IR graph that can affect whether a type |
| 39 | + // has float or not. |
| 40 | + dependencies: HashMap<ItemId, Vec<ItemId>>, |
| 41 | +} |
| 42 | + |
| 43 | +impl<'ctx> NeverByValue<'ctx> { |
| 44 | + fn consider_edge(kind: EdgeKind) -> bool { |
| 45 | + match kind { |
| 46 | + EdgeKind::BaseMember | |
| 47 | + EdgeKind::Field | |
| 48 | + EdgeKind::TypeReference | |
| 49 | + EdgeKind::VarType | |
| 50 | + EdgeKind::TemplateArgument | |
| 51 | + EdgeKind::TemplateDeclaration | |
| 52 | + EdgeKind::TemplateParameterDefinition => true, |
| 53 | + |
| 54 | + EdgeKind::Constructor | |
| 55 | + EdgeKind::Destructor | |
| 56 | + EdgeKind::FunctionReturn | |
| 57 | + EdgeKind::FunctionParameter | |
| 58 | + EdgeKind::InnerType | |
| 59 | + EdgeKind::InnerVar | |
| 60 | + EdgeKind::Method => false, |
| 61 | + EdgeKind::Generic => false, |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + fn insert<Id: Into<ItemId>>(&mut self, id: Id) -> ConstrainResult { |
| 66 | + let id = id.into(); |
| 67 | + info!("inserting {id:?} into the never_by_value set"); |
| 68 | + |
| 69 | + let was_not_already_in_set = self.never_by_value.insert(id); |
| 70 | + assert!( |
| 71 | + was_not_already_in_set, |
| 72 | + "We shouldn't try and insert {:?} twice because if it was \ |
| 73 | + already in the set, `constrain` should have exited early.", |
| 74 | + id |
| 75 | + ); |
| 76 | + |
| 77 | + ConstrainResult::Changed |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl<'ctx> MonotoneFramework for NeverByValue<'ctx> { |
| 82 | + type Node = ItemId; |
| 83 | + type Extra = &'ctx BindgenContext; |
| 84 | + type Output = HashSet<ItemId>; |
| 85 | + |
| 86 | + fn new(ctx: &'ctx BindgenContext) -> Self { |
| 87 | + let never_by_value = HashSet::default(); |
| 88 | + let dependencies = generate_dependencies(ctx, Self::consider_edge); |
| 89 | + |
| 90 | + NeverByValue { |
| 91 | + ctx, |
| 92 | + never_by_value, |
| 93 | + dependencies, |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + fn initial_worklist(&self) -> Vec<ItemId> { |
| 98 | + self.ctx.allowlisted_items().iter().cloned().collect() |
| 99 | + } |
| 100 | + |
| 101 | + fn constrain(&mut self, id: ItemId) -> ConstrainResult { |
| 102 | + info!("constrain: {id:?}"); |
| 103 | + |
| 104 | + if self.never_by_value.contains(&id) { |
| 105 | + info!(" already in set"); |
| 106 | + return ConstrainResult::Same; |
| 107 | + } |
| 108 | + |
| 109 | + let item = self.ctx.resolve_item(id); |
| 110 | + if let Some(ty) = item.kind().as_type() { |
| 111 | + match *ty.kind() { |
| 112 | + TypeKind::Void | |
| 113 | + TypeKind::NullPtr | |
| 114 | + TypeKind::Int(..) | |
| 115 | + TypeKind::Function(..) | |
| 116 | + TypeKind::Enum(..) | |
| 117 | + TypeKind::Reference(..) | |
| 118 | + TypeKind::TypeParam | |
| 119 | + TypeKind::Opaque | |
| 120 | + TypeKind::Pointer(..) | |
| 121 | + TypeKind::UnresolvedTypeRef(..) | |
| 122 | + TypeKind::ObjCInterface(..) | |
| 123 | + TypeKind::ObjCId | |
| 124 | + TypeKind::ObjCSel | |
| 125 | + TypeKind::BlockPointer(_) => { |
| 126 | + info!(" simple type that is not float"); |
| 127 | + ConstrainResult::Same |
| 128 | + } |
| 129 | + |
| 130 | + TypeKind::Float(..) | TypeKind::Complex(..) => { |
| 131 | + let size = ty.layout(self.ctx).expect("float with unknown layout").size; |
| 132 | + match (ty.kind(), size) { |
| 133 | + (TypeKind::Float(..), 4 | 8) | (TypeKind::Complex(..), 8 | 16) => { |
| 134 | + info!(" skipped f32 or f64"); |
| 135 | + ConstrainResult::Same |
| 136 | + } |
| 137 | + _ => { |
| 138 | + info!(" extended float size {size}"); |
| 139 | + self.insert(id) |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + TypeKind::Alias(t) | |
| 145 | + TypeKind::Array(t, _) | |
| 146 | + TypeKind::ResolvedTypeRef(t) | |
| 147 | + TypeKind::TemplateAlias(t, _) | |
| 148 | + TypeKind::Vector(t, _) => { |
| 149 | + if self.never_by_value.contains(&t.into()) { |
| 150 | + info!(" contains/aliases matching type, so matches"); |
| 151 | + self.insert(id) |
| 152 | + } else { |
| 153 | + info!(" does not contain/alias matching type"); |
| 154 | + ConstrainResult::Same |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + TypeKind::Comp(ref info) => { |
| 159 | + let bases_have = info |
| 160 | + .base_members() |
| 161 | + .iter() |
| 162 | + .any(|base| self.never_by_value.contains(&base.ty.into())); |
| 163 | + if bases_have { |
| 164 | + info!(" bases have float, so we also have"); |
| 165 | + return self.insert(id); |
| 166 | + } |
| 167 | + let fields_have = info.fields().iter().any(|f| match *f { |
| 168 | + Field::DataMember(ref data) => { |
| 169 | + self.never_by_value.contains(&data.ty().into()) |
| 170 | + } |
| 171 | + Field::Bitfields(ref bfu) => bfu |
| 172 | + .bitfields() |
| 173 | + .iter() |
| 174 | + .any(|b| self.never_by_value.contains(&b.ty().into())), |
| 175 | + }); |
| 176 | + if fields_have { |
| 177 | + info!(" fields have float, so we also have"); |
| 178 | + return self.insert(id); |
| 179 | + } |
| 180 | + |
| 181 | + info!(" comp doesn't have float"); |
| 182 | + ConstrainResult::Same |
| 183 | + } |
| 184 | + |
| 185 | + TypeKind::TemplateInstantiation(ref template) => { |
| 186 | + let args_have = template |
| 187 | + .template_arguments() |
| 188 | + .iter() |
| 189 | + .any(|arg| self.never_by_value.contains(&arg.into())); |
| 190 | + if args_have { |
| 191 | + info!(" template args match, so instantiation also matches"); |
| 192 | + return self.insert(id); |
| 193 | + } |
| 194 | + |
| 195 | + let def_has = self |
| 196 | + .never_by_value |
| 197 | + .contains(&template.template_definition().into()); |
| 198 | + if def_has { |
| 199 | + info!(" template definition has float, so instantiation also has"); |
| 200 | + return self.insert(id); |
| 201 | + } |
| 202 | + |
| 203 | + info!(" template instantiation does not match"); |
| 204 | + ConstrainResult::Same |
| 205 | + } |
| 206 | + } |
| 207 | + } else { |
| 208 | + info!(" not a type; skipped"); |
| 209 | + ConstrainResult::Same |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + fn each_depending_on<F>(&self, id: ItemId, mut f: F) |
| 214 | + where |
| 215 | + F: FnMut(ItemId), |
| 216 | + { |
| 217 | + if let Some(edges) = self.dependencies.get(&id) { |
| 218 | + for item in edges { |
| 219 | + info!("enqueue {item:?} into worklist"); |
| 220 | + f(*item); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +impl<'ctx> From<NeverByValue<'ctx>> for HashSet<ItemId> { |
| 227 | + fn from(analysis: NeverByValue<'ctx>) -> Self { |
| 228 | + analysis.never_by_value |
| 229 | + } |
| 230 | +} |
0 commit comments