-
Notifications
You must be signed in to change notification settings - Fork 717
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 #919 - photoszzt:has_float, r=fitzgen
- Loading branch information
Showing
200 changed files
with
803 additions
and
414 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,239 @@ | ||
//! Determining which types has float. | ||
use super::{ConstrainResult, MonotoneFramework, generate_dependencies}; | ||
use std::collections::HashSet; | ||
use std::collections::HashMap; | ||
use ir::context::{BindgenContext, ItemId}; | ||
use ir::traversal::EdgeKind; | ||
use ir::ty::TypeKind; | ||
use ir::comp::Field; | ||
use ir::comp::FieldMethods; | ||
|
||
/// An analysis that finds for each IR item whether it has float or not. | ||
/// | ||
/// We use the monotone constraint function `has_float`, | ||
/// defined as follows: | ||
/// | ||
/// * If T is float or complex float, T trivially has. | ||
/// * If T is a type alias, a templated alias or an indirection to another type, | ||
/// it has float if the type T refers to has. | ||
/// * If T is a compound type, it has float if any of base memter or field | ||
/// has. | ||
/// * If T is an instantiation of an abstract template definition, T has | ||
/// float if any of the template arguments or template definition | ||
/// has. | ||
#[derive(Debug, Clone)] | ||
pub struct HasFloat<'ctx, 'gen> | ||
where 'gen: 'ctx | ||
{ | ||
ctx: &'ctx BindgenContext<'gen>, | ||
|
||
// The incremental result of this analysis's computation. Everything in this | ||
// set has float. | ||
has_float: HashSet<ItemId>, | ||
|
||
// Dependencies saying that if a key ItemId has been inserted into the | ||
// `has_float` 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 float or not. | ||
dependencies: HashMap<ItemId, Vec<ItemId>>, | ||
} | ||
|
||
impl<'ctx, 'gen> HasFloat<'ctx, 'gen> { | ||
fn consider_edge(kind: EdgeKind) -> bool { | ||
match kind { | ||
EdgeKind::BaseMember | | ||
EdgeKind::Field | | ||
EdgeKind::TypeReference | | ||
EdgeKind::VarType | | ||
EdgeKind::TemplateArgument | | ||
EdgeKind::TemplateDeclaration | | ||
EdgeKind::TemplateParameterDefinition => true, | ||
|
||
EdgeKind::Constructor | | ||
EdgeKind::Destructor | | ||
EdgeKind::FunctionReturn | | ||
EdgeKind::FunctionParameter | | ||
EdgeKind::InnerType | | ||
EdgeKind::InnerVar | | ||
EdgeKind::Method => false, | ||
EdgeKind::Generic => false, | ||
} | ||
} | ||
|
||
fn insert(&mut self, id: ItemId) -> ConstrainResult { | ||
trace!("inserting {:?} into the has_float set", id); | ||
|
||
let was_not_already_in_set = self.has_float.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 HasFloat<'ctx, 'gen> { | ||
type Node = ItemId; | ||
type Extra = &'ctx BindgenContext<'gen>; | ||
type Output = HashSet<ItemId>; | ||
|
||
fn new(ctx: &'ctx BindgenContext<'gen>) -> HasFloat<'ctx, 'gen> { | ||
let has_float = HashSet::new(); | ||
let dependencies = generate_dependencies(ctx, Self::consider_edge); | ||
|
||
HasFloat { | ||
ctx, | ||
has_float, | ||
dependencies, | ||
} | ||
} | ||
|
||
fn initial_worklist(&self) -> Vec<ItemId> { | ||
self.ctx.whitelisted_items().iter().cloned().collect() | ||
} | ||
|
||
fn constrain(&mut self, id: ItemId) -> ConstrainResult { | ||
trace!("constrain: {:?}", id); | ||
|
||
if self.has_float.contains(&id) { | ||
trace!(" already know it do not have float"); | ||
return ConstrainResult::Same; | ||
} | ||
|
||
let item = self.ctx.resolve_item(id); | ||
let ty = match item.as_type() { | ||
Some(ty) => ty, | ||
None => { | ||
trace!(" not a type; ignoring"); | ||
return ConstrainResult::Same; | ||
} | ||
}; | ||
|
||
match *ty.kind() { | ||
TypeKind::Void | | ||
TypeKind::NullPtr | | ||
TypeKind::Int(..) | | ||
TypeKind::Function(..) | | ||
TypeKind::Enum(..) | | ||
TypeKind::Reference(..) | | ||
TypeKind::BlockPointer | | ||
TypeKind::TypeParam | | ||
TypeKind::Opaque | | ||
TypeKind::Pointer(..) | | ||
TypeKind::UnresolvedTypeRef(..) | | ||
TypeKind::ObjCInterface(..) | | ||
TypeKind::ObjCId | | ||
TypeKind::ObjCSel => { | ||
trace!(" simple type that do not have float"); | ||
ConstrainResult::Same | ||
} | ||
|
||
TypeKind::Float(..) | | ||
TypeKind::Complex(..) => { | ||
trace!(" float type has float"); | ||
self.insert(id) | ||
} | ||
|
||
TypeKind::Array(t, _) => { | ||
if self.has_float.contains(&t) { | ||
trace!(" Array with type T that has float also has float"); | ||
return self.insert(id) | ||
} | ||
trace!(" Array with type T that do not have float also do not have float"); | ||
ConstrainResult::Same | ||
} | ||
|
||
TypeKind::ResolvedTypeRef(t) | | ||
TypeKind::TemplateAlias(t, _) | | ||
TypeKind::Alias(t) => { | ||
if self.has_float.contains(&t) { | ||
trace!(" aliases and type refs to T which have float \ | ||
also have float"); | ||
self.insert(id) | ||
} else { | ||
trace!(" aliases and type refs to T which do not have float \ | ||
also do not have floaarrayt"); | ||
ConstrainResult::Same | ||
} | ||
} | ||
|
||
TypeKind::Comp(ref info) => { | ||
let bases_have = info.base_members() | ||
.iter() | ||
.any(|base| self.has_float.contains(&base.ty)); | ||
if bases_have { | ||
trace!(" bases have float, so we also have"); | ||
return self.insert(id); | ||
} | ||
let fields_have = info.fields() | ||
.iter() | ||
.any(|f| { | ||
match *f { | ||
Field::DataMember(ref data) => { | ||
self.has_float.contains(&data.ty()) | ||
} | ||
Field::Bitfields(ref bfu) => { | ||
bfu.bitfields() | ||
.iter().any(|b| { | ||
self.has_float.contains(&b.ty()) | ||
}) | ||
}, | ||
} | ||
}); | ||
if fields_have { | ||
trace!(" fields have float, so we also have"); | ||
return self.insert(id); | ||
} | ||
|
||
trace!(" comp doesn't have float"); | ||
ConstrainResult::Same | ||
} | ||
|
||
TypeKind::TemplateInstantiation(ref template) => { | ||
let args_have = template.template_arguments() | ||
.iter() | ||
.any(|arg| self.has_float.contains(&arg)); | ||
if args_have { | ||
trace!(" template args have float, so \ | ||
insantiation also has float"); | ||
return self.insert(id); | ||
} | ||
|
||
let def_has = self.has_float | ||
.contains(&template.template_definition()); | ||
if def_has { | ||
trace!(" template definition has float, so \ | ||
insantiation also has"); | ||
return self.insert(id); | ||
} | ||
|
||
trace!(" template instantiation do not have float"); | ||
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<HasFloat<'ctx, 'gen>> for HashSet<ItemId> { | ||
fn from(analysis: HasFloat<'ctx, 'gen>) -> Self { | ||
analysis.has_float | ||
} | ||
} |
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
Oops, something went wrong.