Skip to content

Commit

Permalink
Auto merge of gtk-rs#126 - gkoz:imports, r=gkoz
Browse files Browse the repository at this point in the history
Add struct Imports to track the list of imported types and modules

None
  • Loading branch information
homu committed Sep 27, 2015
2 parents 25280e4 + 6256d94 commit cda7adf
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 31 deletions.
14 changes: 7 additions & 7 deletions src/analysis/functions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;
use std::collections::HashSet;
use std::vec::Vec;

use analysis::imports::Imports;
use analysis::needed_upcast::needed_upcast;
use analysis::out_parameters;
use analysis::return_value;
Expand All @@ -28,19 +28,19 @@ pub struct Info {
}

pub fn analyze(env: &Env, klass: &library::Class, class_tid: library::TypeId,
non_nullable_overrides: &[String], used_types: &mut HashSet<String>) -> Vec<Info> {
non_nullable_overrides: &[String], imports: &mut Imports) -> Vec<Info> {
let mut funcs = Vec::new();

for func in &klass.functions {
let info = analyze_function(env, func, class_tid, non_nullable_overrides, used_types);
let info = analyze_function(env, func, class_tid, non_nullable_overrides, imports);
funcs.push(info);
}

funcs
}

fn analyze_function(env: &Env, func: &library::Function, class_tid: library::TypeId,
non_nullable_overrides: &[String], all_used_types: &mut HashSet<String>) -> Info {
non_nullable_overrides: &[String], imports: &mut Imports) -> Info {
let mut commented = false;
let mut upcasts: Upcasts = Default::default();
let mut used_types: Vec<String> = Vec::with_capacity(4);
Expand Down Expand Up @@ -77,16 +77,16 @@ fn analyze_function(env: &Env, func: &library::Function, class_tid: library::Typ
warn!("Function {} has unsupported outs", func.c_identifier.as_ref().unwrap_or(&func.name));
commented = true;
} else if !outs.is_empty() {
all_used_types.insert("std::mem".into());
imports.add("std::mem".into(), func.version);
}

if !commented {
for s in used_types {
if let Some(i) = s.find("::") {
all_used_types.insert(s[..i].into());
imports.add(s[..i].into(), func.version);
}
else {
all_used_types.insert(s);
imports.add(s, func.version);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/analysis/implements.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::collections::HashSet;
use std::vec::Vec;

use analysis::rust_type::used_rust_type;
use env::Env;
use super::general::StatusedTypeId;
use super::imports::Imports;
use library;

pub fn analyze(env: &Env, type_: &library::Class, used_types: &mut HashSet<String>)
pub fn analyze(env: &Env, type_: &library::Class, imports: &mut Imports)
-> Vec<StatusedTypeId> {
let mut implements = Vec::new();

Expand All @@ -23,7 +23,7 @@ pub fn analyze(env: &Env, type_: &library::Class, used_types: &mut HashSet<Strin
status: status,
});
let _ = used_rust_type(env, interface_tid)
.map(|s| used_types.insert(s));
.map(|s| imports.add(s, None));
}
implements
}
29 changes: 29 additions & 0 deletions src/analysis/imports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::collections::btree_map::{BTreeMap, Iter};

use version::Version;

#[derive(Clone, Debug, Default)]
pub struct Imports {
map: BTreeMap<String, Option<Version>>,
}

impl Imports {
pub fn new() -> Imports {
Imports { map: BTreeMap::new() }
}

pub fn add(&mut self, name: String, version: Option<Version>) {
let entry = self.map.entry(name).or_insert(version);
if version < *entry {
*entry = version;
}
}

pub fn remove(&mut self, name: &str) {
self.map.remove(name);
}

pub fn iter(&self) -> Iter<String, Option<Version>> {
self.map.iter()
}
}
1 change: 1 addition & 0 deletions src/analysis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod c_type;
pub mod functions;
pub mod general;
pub mod implements;
pub mod imports;
pub mod needed_upcast;
pub mod object;
pub mod out_parameters;
Expand Down
19 changes: 9 additions & 10 deletions src/analysis/object.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::collections::HashSet;

use env::Env;
use gobjects::GObject;
use library;
use nameutil::*;
use super::*;
use super::imports::Imports;
use super::type_kind::TypeKind;
use traits::*;
use version::Version;
Expand All @@ -23,7 +22,7 @@ pub struct Info {
pub has_constructors: bool,
pub has_methods: bool,
pub has_functions: bool,
pub used_types: HashSet<String>,
pub imports: Imports,
pub version: Option<Version>,
}

Expand Down Expand Up @@ -56,19 +55,19 @@ impl Info {
}

pub fn new(env: &Env, obj: &GObject) -> Info {
let mut used_types: HashSet<String> = HashSet::with_capacity(20);
let mut imports = Imports::new();
let full_name = obj.name.clone();

let class_tid = env.library.find_type_unwrapped(0, &full_name, "Class");

let type_ = env.type_(class_tid);
let kind = TypeKind::of(&env.library, class_tid);

let name = split_namespace_name(&full_name).1.into();
let name: String = split_namespace_name(&full_name).1.into();

let klass = type_.to_ref();
let (parents, has_ignored_parents) = parents::analyze(env, klass, &mut used_types);
let implements = implements::analyze(env, klass, &mut used_types);
let (parents, has_ignored_parents) = parents::analyze(env, klass, &mut imports);
let implements = implements::analyze(env, klass, &mut imports);

let mut has_children = false;

Expand All @@ -84,12 +83,12 @@ pub fn new(env: &Env, obj: &GObject) -> Info {
}

let functions =
functions::analyze(env, klass, class_tid, &obj.non_nullable_overrides, &mut used_types);
functions::analyze(env, klass, class_tid, &obj.non_nullable_overrides, &mut imports);

let version = functions.iter().filter_map(|f| f.version).min();

//don't `use` yourself
used_types.remove(&name);
imports.remove(&name);

let mut info = Info {
full_name: full_name,
Expand All @@ -101,7 +100,7 @@ pub fn new(env: &Env, obj: &GObject) -> Info {
has_children: has_children,
has_ignored_parents: has_ignored_parents,
functions: functions,
used_types: used_types,
imports: imports,
version: version,
.. Default::default()
};
Expand Down
6 changes: 3 additions & 3 deletions src/analysis/parents.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::collections::HashSet;
use std::vec::Vec;

use analysis::rust_type::used_rust_type;
use env::Env;
use super::general::StatusedTypeId;
use super::imports::Imports;
use library::Class;
use traits::*;

pub fn analyze(env: &Env, type_: &Class, used_types: &mut HashSet<String>)
pub fn analyze(env: &Env, type_: &Class, imports: &mut Imports)
-> (Vec<StatusedTypeId>, bool) {
let mut parents = Vec::new();
let mut has_ignored_parents = false;
Expand All @@ -24,7 +24,7 @@ pub fn analyze(env: &Env, type_: &Class, used_types: &mut HashSet<String>)
name: parent_type.name.clone(),
status: status,
});
used_rust_type(env, parent_tid).ok().map(|s| used_types.insert(s));
used_rust_type(env, parent_tid).ok().map(|s| imports.add(s, None));

if status.ignored() { has_ignored_parents = true; }

Expand Down
12 changes: 5 additions & 7 deletions src/codegen/general.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashSet;
use std::fmt::Display;
use std::io::{Result, Write};

use analysis::general::StatusedTypeId;
use analysis::imports::Imports;
use config::Config;
use git::repo_hash;
use gir_version::VERSION;
Expand All @@ -17,7 +17,8 @@ pub fn start_comments<W: Write>(w: &mut W, conf: &Config) -> Result<()>{
Ok(())
}

pub fn uses<W: Write>(w: &mut W, used_types: &HashSet<String>) -> Result<()>{
pub fn uses<W: Write>(w: &mut W, imports: &Imports, library_name: &str, min_cfg_version: Version)
-> Result<()>{
let v = vec![
"",
"use glib::translate::*;",
Expand All @@ -28,11 +29,8 @@ pub fn uses<W: Write>(w: &mut W, used_types: &HashSet<String>) -> Result<()>{
];
try!(write_vec(w, &v));

let mut used_types: Vec<String> = used_types.iter()
.map(|s| s.clone()).collect();
used_types.sort_by(|a, b| a.cmp(b));

for name in used_types {
for (name, version) in imports.iter() {
try!(version_condition(w, library_name, min_cfg_version, version.clone(), false, 0));
try!(writeln!(w, "use {};", name));
}

Expand Down
2 changes: 1 addition & 1 deletion src/codegen/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn generate<W: Write>(w: &mut W, env: &Env, analysis: &analysis::object::Inf
let type_ = analysis.type_(&env.library);

try!(general::start_comments(w, &env.config));
try!(general::uses(w, &analysis.used_types));
try!(general::uses(w, &analysis.imports, &env.config.library_name, env.config.min_cfg_version));
try!(general::objects_child_type(w, &analysis.name, &type_.c_type));
try!(general::impl_parents(w, &analysis.name, &analysis.parents));
try!(general::impl_interfaces(w, &analysis.name, &analysis.implements));
Expand Down

0 comments on commit cda7adf

Please sign in to comment.