Commit 35c3103e authored by Ryan Hunt's avatar Ryan Hunt
Browse files

Simplify an Option<*T> to be *T

parent 35cba367
struct Opaque();
#[repr(C)]
struct Foo {
x: Option<&Opaque>,
y: Option<&mut Opaque>,
z: Option<fn () -> ()>,
}
#[repr(C)]
union Bar {
x: Option<&Opaque>,
y: Option<&mut Opaque>,
z: Option<fn () -> ()>,
}
#[no_mangle]
extern "C" fn root(a: Option<&Opaque>,
b: Option<&mut Opaque>,
c: Foo,
d: Bar)
{ }
......@@ -49,6 +49,13 @@ impl Function {
})
}
pub fn simplify_option_to_ptr(&mut self) {
self.ret.simplify_option_to_ptr();
for &mut (_, ref mut ty) in &mut self.args {
ty.simplify_option_to_ptr();
}
}
pub fn add_dependencies(&self, library: &Library, out: &mut Dependencies) {
self.ret.add_dependencies(library, out);
for &(_, ref ty) in &self.args {
......
......@@ -48,6 +48,10 @@ impl Static {
documentation: Documentation::load(attrs),
})
}
pub fn simplify_option_to_ptr(&mut self) {
self.ty.simplify_option_to_ptr();
}
}
impl Item for Static {
......
......@@ -75,6 +75,12 @@ impl Struct {
})
}
pub fn simplify_option_to_ptr(&mut self) {
for &mut (_, ref mut ty, _) in &mut self.fields {
ty.simplify_option_to_ptr();
}
}
pub fn is_generic(&self) -> bool {
self.generic_params.len() > 0
}
......
......@@ -274,6 +274,31 @@ impl Type {
}
}
pub fn is_repr_ptr(&self) -> bool {
match self {
&Type::Ptr(..) => true,
&Type::ConstPtr(..) => true,
&Type::FuncPtr(..) => true,
_ => false,
}
}
pub fn simplify_option_to_ptr(&mut self) {
let mut simplified = None;
if let &mut Type::Path(ref mut path) = self {
if path.name == "Option" &&
path.generics.len() == 1 &&
path.generics[0].is_repr_ptr() {
simplified = Some(path.generics.pop().unwrap());
}
}
if let Some(ty) = simplified {
*self = ty;
}
}
pub fn get_root_path(&self) -> Option<Path> {
let mut current = self;
loop {
......
......@@ -42,6 +42,10 @@ impl Typedef {
}
}
pub fn simplify_option_to_ptr(&mut self) {
self.aliased.simplify_option_to_ptr();
}
pub fn transfer_annotations(&mut self, out: &mut HashMap<Path, AnnotationSet>) {
if self.annotations.is_empty() {
return;
......
......@@ -76,6 +76,12 @@ impl Union {
})
}
pub fn simplify_option_to_ptr(&mut self) {
for &mut (_, ref mut ty, _) in &mut self.fields {
ty.simplify_option_to_ptr();
}
}
pub fn is_generic(&self) -> bool {
self.generic_params.len() > 0
}
......
......@@ -57,6 +57,7 @@ impl Library {
self.transfer_annotations();
self.rename_items();
self.specialize_items();
self.simplify_option_to_ptr();
self.instantiate_monomorphs();
let mut dependencies = Dependencies::new();
......@@ -261,6 +262,24 @@ impl Library {
self.specializations.clear();
}
fn simplify_option_to_ptr(&mut self) {
self.structs.for_all_items_mut(|x| {
x.simplify_option_to_ptr();
});
self.unions.for_all_items_mut(|x| {
x.simplify_option_to_ptr();
});
self.globals.for_all_items_mut(|x| {
x.simplify_option_to_ptr();
});
self.typedefs.for_all_items_mut(|x| {
x.simplify_option_to_ptr();
});
for x in &mut self.functions {
x.simplify_option_to_ptr();
};
}
fn instantiate_monomorphs(&mut self) {
assert!(self.specializations.len() == 0);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment