diff options
author | gnzlbg <gonzalobg88@gmail.com> | 2018-08-14 19:16:05 +0200 |
---|---|---|
committer | gnzlbg <gonzalobg88@gmail.com> | 2018-08-14 19:16:05 +0200 |
commit | 122198bfbe30afd00cab87362c0026ded4c9743b (patch) | |
tree | a53cbc062daa7a85e3c9091fd121d5b22aed65f5 | |
parent | e9709955e168098b6d9b327081eda686bb6ee098 (diff) |
map vector types to arrays and pass them by value
-rw-r--r-- | src/codegen/impl_debug.rs | 9 | ||||
-rw-r--r-- | src/codegen/impl_partialeq.rs | 7 | ||||
-rw-r--r-- | src/codegen/mod.rs | 7 | ||||
-rw-r--r-- | src/ir/analysis/derive_copy.rs | 1 | ||||
-rw-r--r-- | src/ir/analysis/derive_debug.rs | 1 | ||||
-rw-r--r-- | src/ir/analysis/derive_default.rs | 1 | ||||
-rw-r--r-- | src/ir/analysis/derive_hash.rs | 12 | ||||
-rw-r--r-- | src/ir/analysis/derive_partialeq_or_partialord.rs | 10 | ||||
-rw-r--r-- | src/ir/analysis/has_float.rs | 8 | ||||
-rw-r--r-- | src/ir/analysis/has_type_param_in_array.rs | 1 | ||||
-rw-r--r-- | src/ir/analysis/sizedness.rs | 4 | ||||
-rw-r--r-- | src/ir/ty.rs | 22 | ||||
-rw-r--r-- | tests/expectations/tests/vector.rs | 2 |
13 files changed, 77 insertions, 8 deletions
diff --git a/src/codegen/impl_debug.rs b/src/codegen/impl_debug.rs index ab934ed6..9e3e3425 100644 --- a/src/codegen/impl_debug.rs +++ b/src/codegen/impl_debug.rs @@ -197,6 +197,15 @@ impl<'a> ImplDebug<'a> for Item { )) } } + TypeKind::Vector(_, len, _) => { + let self_ids = 0..len; + Some(( + format!("{}({{}})", name), + vec![quote! { + #(format!("{:?}", self.#self_ids)),* + }] + )) + } TypeKind::ResolvedTypeRef(t) | TypeKind::TemplateAlias(t, _) | diff --git a/src/codegen/impl_partialeq.rs b/src/codegen/impl_partialeq.rs index a05c65e7..936cc05d 100644 --- a/src/codegen/impl_partialeq.rs +++ b/src/codegen/impl_partialeq.rs @@ -115,6 +115,13 @@ fn gen_field(ctx: &BindgenContext, ty_item: &Item, name: &str) -> quote::Tokens &self. #name_ident [..] == &other. #name_ident [..] } }, + TypeKind::Vector(_, len, _) => { + let self_ids = 0..len; + let other_ids = 0..len; + quote! { + #(self.#self_ids == other.#other_ids &&)* true + } + }, TypeKind::ResolvedTypeRef(t) | TypeKind::TemplateAlias(t, _) | diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 8c113f5a..acf45841 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -595,6 +595,7 @@ impl CodeGenerator for Type { TypeKind::Float(..) | TypeKind::Complex(..) | TypeKind::Array(..) | + TypeKind::Vector(..) | TypeKind::Pointer(..) | TypeKind::BlockPointer | TypeKind::Reference(..) | @@ -3055,6 +3056,12 @@ impl TryToRustTy for Type { [ #ty ; #len ] }) } + TypeKind::Vector(item, len, _) => { + let ty = item.try_to_rust_ty(ctx, &())?; + Ok(quote! { + [ #ty ; #len ] + }) + } TypeKind::Enum(..) => { let path = item.namespace_aware_canonical_path(ctx); let path = Term::new(&path.join("::"), Span::call_site()); diff --git a/src/ir/analysis/derive_copy.rs b/src/ir/analysis/derive_copy.rs index 1e8dc819..abb25174 100644 --- a/src/ir/analysis/derive_copy.rs +++ b/src/ir/analysis/derive_copy.rs @@ -167,6 +167,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveCopy<'ctx> { TypeKind::NullPtr | TypeKind::Int(..) | TypeKind::Float(..) | + TypeKind::Vector(..) | TypeKind::Complex(..) | TypeKind::Function(..) | TypeKind::Enum(..) | diff --git a/src/ir/analysis/derive_debug.rs b/src/ir/analysis/derive_debug.rs index b191d37d..b0d70f15 100644 --- a/src/ir/analysis/derive_debug.rs +++ b/src/ir/analysis/derive_debug.rs @@ -182,6 +182,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveDebug<'ctx> { TypeKind::Function(..) | TypeKind::Enum(..) | TypeKind::Reference(..) | + TypeKind::Vector(..) | TypeKind::BlockPointer | TypeKind::TypeParam | TypeKind::UnresolvedTypeRef(..) | diff --git a/src/ir/analysis/derive_default.rs b/src/ir/analysis/derive_default.rs index e319166d..a43d7737 100644 --- a/src/ir/analysis/derive_default.rs +++ b/src/ir/analysis/derive_default.rs @@ -204,6 +204,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveDefault<'ctx> { TypeKind::Function(..) | TypeKind::Int(..) | TypeKind::Float(..) | + TypeKind::Vector(..) | TypeKind::Complex(..) => { trace!(" simple type that can always derive Default"); ConstrainResult::Same diff --git a/src/ir/analysis/derive_hash.rs b/src/ir/analysis/derive_hash.rs index c23a891e..df160c44 100644 --- a/src/ir/analysis/derive_hash.rs +++ b/src/ir/analysis/derive_hash.rs @@ -202,6 +202,18 @@ impl<'ctx> MonotoneFramework for CannotDeriveHash<'ctx> { self.insert(id) } } + TypeKind::Vector(t, len, _) => { + if self.cannot_derive_hash.contains(&t.into()) { + trace!( + " vectors of T for which we cannot derive Hash \ + also cannot derive Hash" + ); + return self.insert(id); + } + assert!(len != 0); + trace!(" vector can derive Hash"); + ConstrainResult::Same + } TypeKind::Pointer(inner) => { let inner_type = diff --git a/src/ir/analysis/derive_partialeq_or_partialord.rs b/src/ir/analysis/derive_partialeq_or_partialord.rs index cebdceef..d78da1e2 100644 --- a/src/ir/analysis/derive_partialeq_or_partialord.rs +++ b/src/ir/analysis/derive_partialeq_or_partialord.rs @@ -148,7 +148,7 @@ impl<'ctx> CannotDerivePartialEqOrPartialOrd<'ctx> { } match *ty.kind() { - // Handle the simple cases. These can derive partialeq without further + // Handle the simple cases. These can derive partialeq/partialord without further // information. TypeKind::Void | TypeKind::NullPtr | @@ -199,6 +199,14 @@ impl<'ctx> CannotDerivePartialEqOrPartialOrd<'ctx> { return CanDerive::ArrayTooLarge; } } + TypeKind::Vector(..) => { + // FIXME: vectors always can derive PartialEq, but they can + // never derive PartialOrd. + trace!( + " vectors cannot derive `PartialEq`/`PartialOrd`" + ); + return CanDerive::No; + } TypeKind::Pointer(inner) => { let inner_type = diff --git a/src/ir/analysis/has_float.rs b/src/ir/analysis/has_float.rs index 193862c0..f55fce37 100644 --- a/src/ir/analysis/has_float.rs +++ b/src/ir/analysis/has_float.rs @@ -148,6 +148,14 @@ impl<'ctx> MonotoneFramework for HasFloat<'ctx> { trace!(" Array with type T that do not have float also do not have float"); ConstrainResult::Same } + TypeKind::Vector(t, _, _) => { + if self.has_float.contains(&t.into()) { + trace!(" Vector with type T that has float also has float"); + return self.insert(id) + } + trace!(" Vector with type T that do not have float also do not have float"); + ConstrainResult::Same + } TypeKind::ResolvedTypeRef(t) | TypeKind::TemplateAlias(t, _) | diff --git a/src/ir/analysis/has_type_param_in_array.rs b/src/ir/analysis/has_type_param_in_array.rs index aa8d34be..b7afe043 100644 --- a/src/ir/analysis/has_type_param_in_array.rs +++ b/src/ir/analysis/has_type_param_in_array.rs @@ -130,6 +130,7 @@ impl<'ctx> MonotoneFramework for HasTypeParameterInArray<'ctx> { TypeKind::NullPtr | TypeKind::Int(..) | TypeKind::Float(..) | + TypeKind::Vector(..) | TypeKind::Complex(..) | TypeKind::Function(..) | TypeKind::Enum(..) | diff --git a/src/ir/analysis/sizedness.rs b/src/ir/analysis/sizedness.rs index e82c1798..e49b2570 100644 --- a/src/ir/analysis/sizedness.rs +++ b/src/ir/analysis/sizedness.rs @@ -295,6 +295,10 @@ impl<'ctx> MonotoneFramework for SizednessAnalysis<'ctx> { trace!(" arrays of > 0 elements are not zero-sized"); self.insert(id, SizednessResult::NonZeroSized) } + TypeKind::Vector(..) => { + trace!(" vectors are not zero-sized"); + self.insert(id, SizednessResult::NonZeroSized) + } TypeKind::Comp(ref info) => { trace!(" comp considers its own fields and bases"); diff --git a/src/ir/ty.rs b/src/ir/ty.rs index b805dd62..22cdc248 100644 --- a/src/ir/ty.rs +++ b/src/ir/ty.rs @@ -320,6 +320,7 @@ impl Type { match self.kind { TypeKind::TypeParam | TypeKind::Array(..) | + TypeKind::Vector(..) | TypeKind::Comp(..) | TypeKind::Opaque | TypeKind::Int(..) | @@ -472,6 +473,7 @@ impl TypeKind { TypeKind::Alias(..) => "Alias", TypeKind::TemplateAlias(..) => "TemplateAlias", TypeKind::Array(..) => "Array", + TypeKind::Vector(..) => "Vector", TypeKind::Function(..) => "Function", TypeKind::Enum(..) => "Enum", TypeKind::Pointer(..) => "Pointer", @@ -565,6 +567,7 @@ impl TemplateParameters for TypeKind { TypeKind::Float(_) | TypeKind::Complex(_) | TypeKind::Array(..) | + TypeKind::Vector(..) | TypeKind::Function(_) | TypeKind::Enum(_) | TypeKind::Pointer(_) | @@ -627,6 +630,9 @@ pub enum TypeKind { /// template parameters. TemplateAlias(TypeId, Vec<TypeId>), + /// A packed vector type: element type, number of elements, vector alignment. + Vector(TypeId, usize, usize), + /// An array of a type and a length. Array(TypeId, usize), @@ -1148,12 +1154,15 @@ impl Type { TypeKind::Comp(complex) } - // FIXME: We stub vectors as arrays since in 99% of the cases the - // layout is going to be correct, and there's no way we can generate - // vector types properly in Rust for now. - // - // That being said, that should be fixed eventually. - CXType_Vector | + CXType_Vector => { + let inner = Item::from_ty( + ty.elem_type().as_ref().unwrap(), + location, + None, + ctx, + ).expect("Not able to resolve vector element?"); + TypeKind::Vector(inner, ty.num_elements().unwrap(), ty.align()) + } CXType_ConstantArray => { let inner = Item::from_ty( ty.elem_type().as_ref().unwrap(), @@ -1214,6 +1223,7 @@ impl Trace for Type { TypeKind::Pointer(inner) | TypeKind::Reference(inner) | TypeKind::Array(inner, _) | + TypeKind::Vector(inner, _, _) | TypeKind::Alias(inner) | TypeKind::ResolvedTypeRef(inner) => { tracer.visit_kind(inner.into(), EdgeKind::TypeReference); diff --git a/tests/expectations/tests/vector.rs b/tests/expectations/tests/vector.rs index a70ded7a..86a9d737 100644 --- a/tests/expectations/tests/vector.rs +++ b/tests/expectations/tests/vector.rs @@ -40,5 +40,5 @@ pub type __m128d = [f64; 2usize]; pub type __m128i = [::std::os::raw::c_longlong; 2usize]; extern "C" { #[link_name = "\u{1}_Z3fooDv2_xDv2_d"] - pub fn foo(arg1: *mut ::std::os::raw::c_longlong, arg2: *mut f64) -> __m128; + pub fn foo(arg1: __m128i, arg2: __m128d) -> __m128; } |