Skip to content

Commit

Permalink
fix: generate struct in common crate rather than its own crate (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
Millione authored Nov 16, 2023
1 parent 73351f6 commit 0d34a55
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pilota-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pilota-build"
version = "0.9.8"
version = "0.9.9"
edition = "2021"
description = "Compile thrift and protobuf idl into rust code at compile-time."
documentation = "https://docs.rs/pilota-build"
Expand Down
48 changes: 37 additions & 11 deletions pilota-build/src/codegen/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,27 @@ where
.try_for_each_with(this, |this, (k, deps)| {
let name = this.cx().crate_name(k);
let deps = deps.iter().filter(|dep| dep.1 != ***k).collect_vec();
let (main_mod_path, re_pubs, deps) = match k {
DefLocation::Fixed(_, path) => (
Some(path.clone()),
deps.iter().map(|v| v.0).collect_vec(),
deps.iter()
.map(|dep| this.cx().crate_name(&dep.1))
.sorted()
.dedup()
.collect_vec(),
),
DefLocation::Dynamic => (None, vec![], vec![]),
};
this.create_crate(
&this.base_dir,
CrateInfo {
main_mod_path: match k {
DefLocation::Fixed(_, path) => Some(path.clone()),
DefLocation::Dynamic => None,
},
main_mod_path,
workspace_deps: workspace_deps.clone(),
name,
re_pubs: deps.iter().map(|v| v.0).collect_vec(),
re_pubs,
items: entry_map[*k].iter().map(|(k, _)| **k).collect_vec(),
deps: deps
.iter()
.map(|dep| this.cx().crate_name(&dep.1))
.sorted()
.dedup()
.collect_vec(),
deps,
user_gen: this.cx().plugin_gen.get(k).map(|v| v.value().clone()),
},
)
Expand Down Expand Up @@ -174,7 +178,29 @@ where
}
if !matches!(&*cx.item(def_id).unwrap(), rir::Item::Mod(_)) {
let file_id = cx.node(def_id).unwrap().file_id;

if cx.input_files().contains(&file_id) {
let type_graph = cx.type_graph();
let node = type_graph.node_map[&def_id];
for from in type_graph
.graph
.neighbors_directed(node, petgraph::Direction::Incoming)
{
let from_def_id = type_graph.id_map[&from];
let from_file_id = cx.node(from_def_id).unwrap().file_id;
if !cx.input_files().contains(&from_file_id)
|| map
.get(&from_def_id)
.map(|v| match v {
DefLocation::Fixed(_, _) => false,
DefLocation::Dynamic => true,
})
.unwrap_or_default()
{
map.insert(def_id, DefLocation::Dynamic);
return;
}
}
let file = cx.file(file_id).unwrap();
map.insert(
def_id,
Expand Down
22 changes: 22 additions & 0 deletions pilota-build/src/middle/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,29 @@ impl ContextBuilder {
}
if !matches!(&*cx.db.item(def_id).unwrap(), rir::Item::Mod(_)) {
let file_id = cx.db.node(def_id).unwrap().file_id;

if cx.db.input_files().contains(&file_id) {
let type_graph = cx.db.type_graph();
let node = type_graph.node_map[&def_id];
for from in type_graph
.graph
.neighbors_directed(node, petgraph::Direction::Incoming)
{
let from_def_id = type_graph.id_map[&from];
let from_file_id = cx.db.node(from_def_id).unwrap().file_id;
if !cx.db.input_files().contains(&from_file_id)
|| map
.get(&from_def_id)
.map(|v| match v {
DefLocation::Fixed(_, _) => false,
DefLocation::Dynamic => true,
})
.unwrap_or_default()
{
map.insert(def_id, DefLocation::Dynamic);
return;
}
}
let file = cx.db.file(file_id).unwrap();
map.insert(
def_id,
Expand Down
16 changes: 12 additions & 4 deletions pilota-build/src/middle/type_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ use crate::symbol::DefId;

#[derive(Debug)]
pub struct TypeGraph {
graph: Graph<DefId, ()>,
node_map: FxHashMap<DefId, NodeIndex>,
pub(crate) graph: Graph<DefId, ()>,
pub(crate) node_map: FxHashMap<DefId, NodeIndex>,
pub(crate) id_map: FxHashMap<NodeIndex, DefId>,
}

impl TypeGraph {
pub fn from_items(items: impl Iterator<Item = (DefId, Arc<Item>)> + Clone) -> Self {
let mut graph: Graph<DefId, ()> = Graph::new();
let mut node_map = FxHashMap::default();
let mut id_map = FxHashMap::default();
items.clone().for_each(|(def_id, _)| {
node_map.insert(def_id, graph.add_node(def_id));
let node_index = graph.add_node(def_id);
node_map.insert(def_id, node_index);
id_map.insert(node_index, def_id);
});

items.for_each(|(def_id, item)| {
Expand All @@ -46,7 +50,11 @@ impl TypeGraph {
_ => {}
};
});
Self { graph, node_map }
Self {
graph,
node_map,
id_map,
}
}

pub fn is_nested(&self, a: DefId, b: DefId) -> bool {
Expand Down

0 comments on commit 0d34a55

Please sign in to comment.