-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
ScopeGraphFormatter
to dump dot representation of `ScopeG…
…raph`
- Loading branch information
Showing
11 changed files
with
498 additions
and
48 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,10 +1,41 @@ | ||
use fe_driver2::DriverDataBase; | ||
|
||
use clap::Parser; | ||
use hir::hir_def::TopLevelMod; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(author, version, about, long_about = None)] | ||
struct Args { | ||
/// The file to compile. | ||
#[arg()] | ||
file_path: String, | ||
|
||
/// Dump a graphviz dot file of the scope graph for the given file. | ||
#[arg(long = "dump-scope-graph", default_value_t = false)] | ||
dump_scope_graph: bool, | ||
} | ||
|
||
pub fn main() { | ||
let arg = std::env::args().nth(1).unwrap(); | ||
let args = Args::parse(); | ||
let path = std::path::Path::new(&args.file_path); | ||
if !path.exists() { | ||
eprintln!("file '{}' does not exist", args.file_path); | ||
std::process::exit(2); | ||
} | ||
let source = std::fs::read_to_string(&args.file_path).unwrap(); | ||
|
||
let mut db = DriverDataBase::default(); | ||
let source = std::fs::read_to_string(&arg).unwrap(); | ||
db.run_on_file(std::path::Path::new(&arg), &source); | ||
let top_mod = db.top_mod_from_file(path, &source); | ||
db.run_on_top_mod(top_mod); | ||
db.emit_diags(); | ||
|
||
if args.dump_scope_graph { | ||
println!("{}", dump_scope_graph(&db, top_mod)); | ||
} | ||
} | ||
|
||
fn dump_scope_graph(db: &DriverDataBase, top_mod: TopLevelMod) -> String { | ||
let mut s = vec![]; | ||
top_mod.scope_graph(db).write_as_dot(db, &mut s).unwrap(); | ||
String::from_utf8(s).unwrap() | ||
} |
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
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
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,172 @@ | ||
use std::{ | ||
collections::{hash_map::Entry, VecDeque}, | ||
io, | ||
}; | ||
|
||
use cranelift_entity::{entity_impl, PrimaryMap}; | ||
use dot2::label::Text; | ||
use rustc_hash::{FxHashMap, FxHashSet}; | ||
|
||
use crate::HirDb; | ||
|
||
use super::scope_graph::{EdgeKind, ScopeGraph, ScopeId}; | ||
|
||
type NodeId = usize; | ||
|
||
pub(super) struct ScopeGraphFormatter<'db> { | ||
db: &'db dyn HirDb, | ||
edges: PrimaryMap<EdgeId, Edge>, | ||
nodes: Vec<ScopeId>, | ||
} | ||
|
||
impl<'db> ScopeGraphFormatter<'db> { | ||
fn build_formatter(&mut self, s_graph: &ScopeGraph) { | ||
let mut visited = FxHashSet::default(); | ||
let mut nodes_map = FxHashMap::default(); | ||
|
||
let mut worklist = VecDeque::new(); | ||
let root = s_graph.top_mod.scope(); | ||
worklist.push_back(root); | ||
while let Some(scope) = worklist.pop_front() { | ||
if !visited.insert(scope) { | ||
continue; | ||
} | ||
let source = self.node_id(scope, &mut nodes_map); | ||
|
||
for edge in s_graph.edges(scope) { | ||
let target = self.node_id(edge.dest, &mut nodes_map); | ||
|
||
self.edges.push(Edge { | ||
kind: edge.kind, | ||
target, | ||
source, | ||
}); | ||
|
||
if !visited.contains(&edge.dest) { | ||
worklist.push_back(edge.dest); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn node_id(&mut self, scope: ScopeId, nodes_map: &mut FxHashMap<ScopeId, usize>) -> NodeId { | ||
match nodes_map.entry(scope) { | ||
Entry::Occupied(entry) => *entry.get(), | ||
Entry::Vacant(entry) => { | ||
let id = self.nodes.len(); | ||
self.nodes.push(scope); | ||
entry.insert(id); | ||
id | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<'db> ScopeGraphFormatter<'db> { | ||
pub(super) fn new(db: &'db dyn HirDb, s_graph: &ScopeGraph) -> Self { | ||
let nodes = Vec::new(); | ||
let edges = PrimaryMap::new(); | ||
let mut formatter = Self { db, edges, nodes }; | ||
|
||
formatter.build_formatter(s_graph); | ||
formatter | ||
} | ||
|
||
pub(super) fn render(&self, w: &mut impl io::Write) -> io::Result<()> { | ||
dot2::render(self, w).map_err(|err| match err { | ||
dot2::Error::Io(err) => err, | ||
dot2::Error::InvalidId => unreachable!(), | ||
}) | ||
} | ||
} | ||
|
||
impl<'db, 'a> dot2::Labeller<'a> for ScopeGraphFormatter<'db> { | ||
type Node = NodeId; | ||
type Edge = EdgeId; | ||
type Subgraph = (); | ||
|
||
fn graph_id(&'a self) -> dot2::Result<dot2::Id<'a>> { | ||
dot2::Id::new("example1") | ||
} | ||
|
||
fn node_id(&'a self, n: &Self::Node) -> dot2::Result<dot2::Id<'a>> { | ||
dot2::Id::new(format!("N{n}")) | ||
} | ||
|
||
fn node_label<'b>(&'a self, node: &Self::Node) -> dot2::Result<Text<'db>> { | ||
let label = match &self.nodes[*node] { | ||
ScopeId::Item(item) => format!( | ||
"{} {}", | ||
item.kind_name(), | ||
item.name(self.db).map_or("", |name| name.data(self.db)) | ||
), | ||
|
||
ScopeId::Block(body, expr) => { | ||
let idx = body.block_order(self.db)[expr]; | ||
format!("{{block{}}}", idx) | ||
} | ||
|
||
scope => scope | ||
.name(self.db) | ||
.map_or(String::new(), |name| name.data(self.db).to_string()), | ||
}; | ||
Ok(Text::LabelStr(label.into())) | ||
} | ||
|
||
fn edge_label(&'a self, e: &Self::Edge) -> Text<'a> { | ||
let edge = &self.edges[*e]; | ||
|
||
let label = match edge.kind { | ||
EdgeKind::Lex(_) => "lex", | ||
EdgeKind::Mod(_) => "mod", | ||
EdgeKind::Type(_) => "type", | ||
EdgeKind::Trait(_) => "trait", | ||
EdgeKind::GenericParam(_) => "generic_param", | ||
EdgeKind::Value(_) => "value", | ||
EdgeKind::Field(_) => "field", | ||
EdgeKind::Variant(_) => "variant", | ||
EdgeKind::Super(_) => "super", | ||
EdgeKind::Ingot(_) => "ingot", | ||
EdgeKind::Self_(_) => "self", | ||
EdgeKind::SelfTy(_) => "self_ty", | ||
EdgeKind::Anon(_) => "anon", | ||
}; | ||
Text::LabelStr(label.into()) | ||
} | ||
|
||
fn node_shape(&self, _n: &Self::Node) -> Option<Text<'db>> { | ||
Some(Text::LabelStr("box".into())) | ||
} | ||
} | ||
|
||
impl<'db, 'a> dot2::GraphWalk<'a> for ScopeGraphFormatter<'db> { | ||
type Node = NodeId; | ||
type Edge = EdgeId; | ||
type Subgraph = (); | ||
|
||
fn nodes(&'a self) -> dot2::Nodes<'a, Self::Node> { | ||
(0..self.nodes.len()).collect() | ||
} | ||
|
||
fn edges(&'a self) -> dot2::Edges<'a, Self::Edge> { | ||
self.edges.keys().collect() | ||
} | ||
|
||
fn source(&self, e: &Self::Edge) -> Self::Node { | ||
self.edges[*e].source | ||
} | ||
|
||
fn target(&self, e: &Self::Edge) -> Self::Node { | ||
self.edges[*e].target | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub(super) struct EdgeId(u32); | ||
entity_impl!(EdgeId); | ||
|
||
struct Edge { | ||
kind: EdgeKind, | ||
target: NodeId, | ||
source: NodeId, | ||
} |
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