joinir: clean pattern visibility and refactor pattern2 pipeline

This commit is contained in:
nyash-codex
2025-12-11 19:11:26 +09:00
parent 463a6240fb
commit 59a985b7fa
16 changed files with 1141 additions and 812 deletions

View File

@ -30,7 +30,7 @@ use crate::mir::loop_pattern_detection::{LoopFeatures, LoopPatternKind};
use super::ast_feature_extractor as ast_features;
/// Context passed to pattern detect/lower functions
pub struct LoopPatternContext<'a> {
pub(crate) struct LoopPatternContext<'a> {
/// Loop condition AST node
pub condition: &'a ASTNode,
@ -69,7 +69,7 @@ impl<'a> LoopPatternContext<'a> {
/// Phase 194+: Automatically detects continue/break statements in body
/// Phase 192: Extract features and classify pattern from AST
/// Phase 193: Feature extraction delegated to ast_feature_extractor module
pub fn new(
pub(crate) fn new(
condition: &'a ASTNode,
body: &'a [ASTNode],
func_name: &'a str,
@ -99,7 +99,7 @@ impl<'a> LoopPatternContext<'a> {
}
/// Phase 200-C: Create context with fn_body for capture analysis
pub fn with_fn_body(
pub(crate) fn with_fn_body(
condition: &'a ASTNode,
body: &'a [ASTNode],
func_name: &'a str,
@ -117,19 +117,19 @@ impl<'a> LoopPatternContext<'a> {
/// Entry in the loop pattern router table.
/// Each pattern registers a detect function and a lower function.
pub struct LoopPatternEntry {
pub(crate) struct LoopPatternEntry {
/// Human-readable pattern name for debugging
pub name: &'static str,
pub(crate) name: &'static str,
/// Priority (lower = tried first). Pattern1=10, Pattern2=20, Pattern3=30
#[allow(dead_code)]
pub priority: u8,
pub(crate) priority: u8,
/// Detection function: returns true if this pattern matches
pub detect: fn(&MirBuilder, &LoopPatternContext) -> bool,
pub(crate) detect: fn(&MirBuilder, &LoopPatternContext) -> bool,
/// Lowering function: performs the actual JoinIR generation
pub lower: fn(&mut MirBuilder, &LoopPatternContext) -> Result<Option<ValueId>, String>,
pub(crate) lower: fn(&mut MirBuilder, &LoopPatternContext) -> Result<Option<ValueId>, String>,
}
/// Static table of all registered loop patterns.
@ -156,16 +156,16 @@ pub struct LoopPatternEntry {
/// - Structure: has_break && !has_continue
///
/// Note: func_name is now only used for debug logging, not pattern detection
pub static LOOP_PATTERNS: &[LoopPatternEntry] = &[
pub(crate) static LOOP_PATTERNS: &[LoopPatternEntry] = &[
LoopPatternEntry {
name: "Pattern4_WithContinue",
priority: 5, // Highest priority - continue is most specific
priority: 5, // Highest priority - continue is most specific
detect: super::pattern4_with_continue::can_lower,
lower: super::pattern4_with_continue::lower,
},
LoopPatternEntry {
name: "Pattern3_WithIfPhi",
priority: 30, // NOTE: Pattern 3 must be checked BEFORE Pattern 1 (both use "main")
priority: 30, // NOTE: Pattern 3 must be checked BEFORE Pattern 1 (both use "main")
detect: super::pattern3_with_if_phi::can_lower,
lower: super::pattern3_with_if_phi::lower,
},
@ -195,7 +195,7 @@ pub static LOOP_PATTERNS: &[LoopPatternEntry] = &[
/// - Pattern detection: `ctx.pattern_kind` (from `loop_pattern_detection::classify`)
/// - No redundant pattern detection in detect functions
/// - All patterns use structure-based classification
pub fn route_loop_pattern(
pub(crate) fn route_loop_pattern(
builder: &mut MirBuilder,
ctx: &LoopPatternContext,
) -> Result<Option<ValueId>, String> {
@ -217,7 +217,13 @@ pub fn route_loop_pattern(
// No pattern matched - return None (caller will handle error)
// Phase 187-2: Legacy LoopBuilder removed, all loops must use JoinIR
if ctx.debug {
trace::trace().debug("route", &format!("No pattern matched for function '{}' (pattern_kind={:?})", ctx.func_name, ctx.pattern_kind));
trace::trace().debug(
"route",
&format!(
"No pattern matched for function '{}' (pattern_kind={:?})",
ctx.func_name, ctx.pattern_kind
),
);
}
Ok(None)
}