Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cfg): add type alias for Graph #6322

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/oxc_cfg/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct ErrorHarness(ErrorEdgeKind, BasicBlockId);

#[derive(Debug, Default)]
pub struct ControlFlowGraphBuilder<'a> {
pub graph: Graph<usize, EdgeType>,
pub graph: Graph,
pub basic_blocks: Vec<BasicBlock>,
pub current_node_ix: BasicBlockId,
ctx_stack: Vec<Ctx<'a>>,
Expand Down
13 changes: 6 additions & 7 deletions crates/oxc_cfg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod visit;
use itertools::Itertools;
use petgraph::{
visit::{Control, DfsEvent, EdgeRef},
Direction, Graph,
Direction,
};

pub mod graph {
Expand All @@ -23,6 +23,8 @@ pub use builder::{ControlFlowGraphBuilder, CtxCursor, CtxFlags};
pub use dot::DisplayDot;
use visit::set_depth_first_search;

pub type Graph = petgraph::graph::DiGraph<usize, EdgeType>;

#[derive(Debug, Clone)]
pub enum EdgeType {
/// Conditional jumps
Expand Down Expand Up @@ -62,12 +64,12 @@ pub enum EvalConstConditionResult {

#[derive(Debug)]
pub struct ControlFlowGraph {
pub graph: Graph<usize, EdgeType>,
pub graph: Graph,
pub basic_blocks: Vec<BasicBlock>,
}

impl ControlFlowGraph {
pub fn graph(&self) -> &Graph<usize, EdgeType> {
pub fn graph(&self) -> &Graph {
&self.graph
}

Expand Down Expand Up @@ -131,10 +133,7 @@ impl ControlFlowGraph {
where
F: Fn(&Instruction) -> EvalConstConditionResult,
{
fn get_jump_target(
graph: &Graph<usize, EdgeType>,
node: BasicBlockId,
) -> Option<BasicBlockId> {
fn get_jump_target(graph: &Graph, node: BasicBlockId) -> Option<BasicBlockId> {
graph
.edges_directed(node, Direction::Outgoing)
.find_or_first(|e| matches!(e.weight(), EdgeType::Jump))
Expand Down