refactor(joinir): Unify Pattern 2 with PatternPipelineContext
Phase 179-B Task 5: Refactor Pattern 2 to use PatternPipelineContext for unified preprocessing. Changes: - Use build_pattern_context() for initial loop variable extraction and scope construction - Extract loop_var_name, loop_var_id, carrier_info, and scope from context - Keep Trim pattern logic inline (complex, needs dedicated module in future Phase 180+) - Reduce line count: 517 → 509 lines (1.5% reduction) Note: Pattern 2 has significant complexity (Trim pattern, carrier filtering, break condition processing) that cannot be easily unified without breaking the "analyzer-only" design constraint of PatternPipelineContext. The minimal refactoring maintains compatibility while establishing the pipeline pattern. Benefits: - Consistent entry point with Pattern 1/3 - Establishes pattern for future Trim module extraction - Maintains all existing functionality and test compatibility 🤖 Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@ -75,17 +75,19 @@ pub fn lower(
|
||||
}
|
||||
|
||||
impl MirBuilder {
|
||||
/// Phase 188-Impl-2: Pattern 2 (Loop with Conditional Break) minimal lowerer
|
||||
/// Phase 179-B: Pattern 2 (Loop with Conditional Break) minimal lowerer
|
||||
///
|
||||
/// Similar to Pattern 1, but handles loops with break statements.
|
||||
/// **Refactored**: Now uses PatternPipelineContext for unified preprocessing
|
||||
///
|
||||
/// # Steps
|
||||
/// 1. Extract loop variable from condition
|
||||
/// 2. Generate JoinIR using loop_with_break_minimal
|
||||
/// 3. Convert JoinModule → MirModule
|
||||
/// 4. Create JoinInlineBoundary for input mapping
|
||||
/// 5. Merge MIR blocks into current_function
|
||||
/// 6. Return Void (loop doesn't produce values)
|
||||
/// # Pipeline (Phase 179-B)
|
||||
/// 1. Build preprocessing context → PatternPipelineContext
|
||||
/// 2. Pattern 2-specific processing (Trim, carrier updates, break condition)
|
||||
/// 3. Call JoinIR lowerer → JoinModule
|
||||
/// 4. Create boundary from context → JoinInlineBoundary
|
||||
/// 5. Merge MIR blocks → JoinIRConversionPipeline
|
||||
///
|
||||
/// Note: Pattern 2 has complex Trim pattern logic that remains inline
|
||||
/// for now. Future Phase 180+ will move Trim logic to dedicated module.
|
||||
pub(in crate::mir::builder) fn cf_loop_pattern2_with_break(
|
||||
&mut self,
|
||||
condition: &ASTNode,
|
||||
@ -94,29 +96,31 @@ impl MirBuilder {
|
||||
debug: bool,
|
||||
) -> Result<Option<ValueId>, String> {
|
||||
use crate::mir::join_ir::lowering::loop_with_break_minimal::lower_loop_with_break_minimal;
|
||||
use crate::mir::BasicBlockId;
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
// Phase 195: Use unified trace
|
||||
trace::trace().debug("pattern2", "Calling Pattern 2 minimal lowerer");
|
||||
|
||||
// Phase 33-22: Use CommonPatternInitializer for loop variable extraction
|
||||
// Phase 171-C-4: carrier_info is now mutable for promotion merging
|
||||
use super::common_init::CommonPatternInitializer;
|
||||
let (loop_var_name, loop_var_id, mut carrier_info) =
|
||||
CommonPatternInitializer::initialize_pattern(
|
||||
self,
|
||||
condition,
|
||||
&self.variable_map,
|
||||
None, // Pattern 2 handles break-triggered vars via condition_bindings
|
||||
)?;
|
||||
// Phase 179-B: Use PatternPipelineContext for unified preprocessing
|
||||
// Note: Pattern 2 still needs inline processing for Trim/carrier logic
|
||||
use super::pattern_pipeline::{build_pattern_context, PatternVariant};
|
||||
let ctx = build_pattern_context(
|
||||
self,
|
||||
condition,
|
||||
_body,
|
||||
PatternVariant::Pattern2,
|
||||
)?;
|
||||
|
||||
// Extract from context
|
||||
let loop_var_name = ctx.loop_var_name.clone();
|
||||
let loop_var_id = ctx.loop_var_id;
|
||||
let mut carrier_info = ctx.carrier_info.clone();
|
||||
let scope = ctx.loop_scope.clone();
|
||||
|
||||
eprintln!(
|
||||
"[pattern2/init] CommonPatternInitializer returned: loop_var='{}', loop_var_id={:?}, carriers={}",
|
||||
"[pattern2/init] PatternPipelineContext: loop_var='{}', loop_var_id={:?}, carriers={}",
|
||||
loop_var_name, loop_var_id, carrier_info.carriers.len()
|
||||
);
|
||||
|
||||
|
||||
// Phase 195: Use unified trace
|
||||
trace::trace().varmap("pattern2_start", &self.variable_map);
|
||||
|
||||
@ -150,18 +154,6 @@ impl MirBuilder {
|
||||
eprintln!(" No condition-only variables");
|
||||
}
|
||||
|
||||
// Phase 171-172: Use LoopScopeShapeBuilder for unified initialization (Issue 4)
|
||||
// Extract body_locals from loop body AST for proper variable classification (Trim support)
|
||||
use super::loop_scope_shape_builder::LoopScopeShapeBuilder;
|
||||
let scope = LoopScopeShapeBuilder::with_body_locals(
|
||||
BasicBlockId(0),
|
||||
BasicBlockId(0),
|
||||
BasicBlockId(0),
|
||||
BasicBlockId(0),
|
||||
BTreeSet::new(),
|
||||
_body,
|
||||
);
|
||||
|
||||
// Phase 170-B: Extract break condition from loop body
|
||||
use super::ast_feature_extractor;
|
||||
let break_condition_raw = ast_feature_extractor::extract_break_condition(_body)
|
||||
|
||||
Reference in New Issue
Block a user