001package gudusoft.gsqlparser.ir.builder;
002
003import gudusoft.gsqlparser.TStatementList;
004import gudusoft.gsqlparser.analyzer.v2.AnalyzerV2Config;
005import gudusoft.gsqlparser.analyzer.v2.IRProgram;
006import gudusoft.gsqlparser.ir.bound.BoundProgram;
007import gudusoft.gsqlparser.ir.logical.LogicalProgram;
008
009/**
010 * Orchestrates the three-phase IR translation pipeline.
011 * <p>
012 * Phase 1: AST → Bound IR (name resolution, scope binding)
013 * Phase 2: Bound IR → Logical IR (relational + procedural plans)
014 * Phase 3: Logical IR → Flow IR (CFG/DFG/CallGraph/Provenance)
015 * <p>
016 * In Phase A, only Phase 1 is active.
017 */
018public class IRTranslator {
019
020    private final AnalyzerV2Config config;
021    private final IBoundIRBuilder boundBuilder;
022    private final ILogicalIRBuilder logicalBuilder;
023    private final IFlowPlanner flowPlanner;
024
025    /**
026     * Full pipeline constructor (all three phases).
027     */
028    public IRTranslator(AnalyzerV2Config config,
029                        IBoundIRBuilder boundBuilder,
030                        ILogicalIRBuilder logicalBuilder,
031                        IFlowPlanner flowPlanner) {
032        this.config = config;
033        this.boundBuilder = boundBuilder;
034        this.logicalBuilder = logicalBuilder;
035        this.flowPlanner = flowPlanner;
036    }
037
038    /**
039     * Phase A constructor (bound IR only).
040     */
041    public IRTranslator(AnalyzerV2Config config, IBoundIRBuilder boundBuilder) {
042        this(config, boundBuilder, null, null);
043    }
044
045    /**
046     * Translates AST statements through all available IR phases.
047     */
048    public IRProgram translate(TStatementList stmts) {
049        // Phase 1: Binding
050        BoundProgram bound = boundBuilder.build(stmts, config);
051
052        // Phase 2: Logical IR — slice 174 (S13). Runs only when enabled by
053        // config AND a builder is supplied; otherwise bound-only behaviour
054        // is unchanged (logical stays null).
055        LogicalProgram logical = null;
056        if (config != null && config.enableLogicalPhase && logicalBuilder != null) {
057            logical = logicalBuilder.build(bound, config);
058        }
059
060        // Phase 3: Flow IR (when available)
061        // FlowBundle flow = null;
062        // if (flowPlanner != null && logical != null) {
063        //     flow = flowPlanner.build(logical, config);
064        // }
065
066        return new IRProgram(bound, logical);
067    }
068}