refactor(mir): Extract VariableContext from MirBuilder (Phase 136 follow-up 5/7)
## Summary Extracted variable mapping management into dedicated VariableContext struct, completing step 5 of 7 in the Context Box refactoring plan. ## Changes - NEW: src/mir/builder/variable_context.rs (VariableContext struct + helpers) - Modified: src/mir/builder.rs (added variable_ctx field + sync helpers) - Updated: phase-136-context-box-progress.md (5/7 progress) ## Extracted Fields - variable_map: BTreeMap<String, ValueId> → variable_ctx.variable_map ## Key Features - snapshot/restore for if/loop pattern state management - JoinIR integration: CarrierInfo::from_variable_map() - ExitLine contract enforcement (carriers in variable_map) - NYASH_TRACE_VARMAP debug visualization support ## Design Clarity - BindingContext: String → BindingId (binding identity, survives SSA renaming) - VariableContext: String → ValueId (current SSA values, block-local) - Both contexts work together for complete variable management ## Tests - cargo test --release --lib: 1014/1018 passed (4 pre-existing failures) - phase135_trim_mir_verify.sh: PASS - Backward compatibility: 100% maintained (deprecated fields synced) ## Progress Phase 136 Context Extraction: 5/7 complete (71%) - ✅ Step 1: TypeContext - ✅ Step 2: CoreContext - ✅ Step 3: ScopeContext - ✅ Step 4: BindingContext - ✅ Step 5: VariableContext (this commit) - ⏳ Step 6: MetadataContext - ⏳ Step 7: CompilationContext 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@ -4,7 +4,7 @@
|
||||
|
||||
builder.rs の 1219 行を責任ごとに Context Box に分割し、保守性・テスト容易性を向上させる段階的リファクタリング。
|
||||
|
||||
## 完了した Context (3/7)
|
||||
## 完了した Context (5/7)
|
||||
|
||||
### ✅ TypeContext (Step 1) - 完了
|
||||
|
||||
@ -103,13 +103,72 @@ builder.rs の 1219 行を責任ごとに Context Box に分割し、保守性
|
||||
- `calls/lowering.rs` - 関数 lowering の文脈管理を scope_ctx 同期
|
||||
- 段階的移行により破壊的変更なし
|
||||
|
||||
**コミット**: 3127ebb7
|
||||
|
||||
### ✅ BindingContext (Step 4) - 完了
|
||||
|
||||
**実装日**: 2025-12-15
|
||||
|
||||
**抽出したフィールド** (1個):
|
||||
- `binding_map: BTreeMap<String, BindingId>` - 変数名 → BindingId マッピング (Phase 74)
|
||||
|
||||
**ファイル**:
|
||||
- `/home/tomoaki/git/hakorune-selfhost/src/mir/builder/binding_context.rs` (新規作成)
|
||||
|
||||
**統合方法**:
|
||||
- `MirBuilder` に `binding_ctx: BindingContext` フィールドを追加
|
||||
- 既存フィールドは `#[deprecated]` でマーク(後方互換性維持)
|
||||
- 同期ヘルパー (`sync_binding_ctx_to_legacy()`, `sync_legacy_to_binding_ctx()`) を実装
|
||||
- BindingId は CoreContext 経由で割り当て (`allocate_binding_id()`)
|
||||
|
||||
**テスト結果**:
|
||||
- ✅ `cargo build --release` 成功 (302 warnings - deprecated フィールド使用)
|
||||
- ✅ `cargo test --release --lib` - 1010/1014 PASS (4 tests 失敗は既存問題)
|
||||
- ✅ `phase135_trim_mir_verify.sh` - PASS
|
||||
|
||||
**影響範囲**:
|
||||
- `vars/lexical_scope.rs` - binding_ctx.binding_map 使用に更新(スコープ復元処理)
|
||||
- `vars/assignment_resolver.rs` - binding_ctx.contains() 使用に更新
|
||||
- 段階的移行により破壊的変更なし
|
||||
|
||||
**コミット**: 1adf57ec
|
||||
|
||||
### ✅ VariableContext (Step 5) - 完了
|
||||
|
||||
**実装日**: 2025-12-15
|
||||
|
||||
**抽出したフィールド** (1個):
|
||||
- `variable_map: BTreeMap<String, ValueId>` - 変数名 → ValueId マッピング (SSA 変換)
|
||||
|
||||
**ファイル**:
|
||||
- `/home/tomoaki/git/hakorune-selfhost/src/mir/builder/variable_context.rs` (新規作成)
|
||||
|
||||
**統合方法**:
|
||||
- `MirBuilder` に `variable_ctx: VariableContext` フィールドを追加
|
||||
- 既存フィールドは `#[deprecated]` でマーク(後方互換性維持)
|
||||
- 同期ヘルパー (`sync_variable_ctx_to_legacy()`, `sync_legacy_to_variable_ctx()`) を実装
|
||||
- JoinIR 統合: `CarrierInfo::from_variable_map(&variable_map)` で carrier 追跡
|
||||
- NYASH_TRACE_VARMAP デバッグサポート (variable_map 可視化)
|
||||
|
||||
**特徴**:
|
||||
- **BindingContext との違い**: BindingContext は BindingId (バインディング識別子), VariableContext は ValueId (SSA 値)
|
||||
- **JoinIR 連携**: Pattern 2/3/4 のループで carrier variable 追跡に使用
|
||||
- **PHI 生成**: if/loop の variable_map 変化から PHI ノードを生成
|
||||
- **Snapshot/Restore**: if 文・ループで variable_map のスナップショット/復元パターンを使用
|
||||
|
||||
**テスト結果**:
|
||||
- ✅ `cargo build --release` 成功 (367 warnings - deprecated フィールド使用)
|
||||
- ✅ `cargo test --release --lib` - 1014/1018 PASS (4 tests 失敗は既存問題)
|
||||
- ✅ `phase135_trim_mir_verify.sh` - PASS
|
||||
|
||||
**影響範囲**:
|
||||
- builder 内 17 ファイルで variable_map を使用中 (phi.rs, stmts.rs, if_form.rs, decls.rs 等)
|
||||
- JoinIR lowering で `CarrierInfo::from_variable_map()` を使用
|
||||
- 段階的移行により破壊的変更なし
|
||||
|
||||
**コミット**: [今回のコミット]
|
||||
|
||||
## 残りの Context (4/7)
|
||||
|
||||
### 4. BindingContext (計画中)
|
||||
- `binding_map: BTreeMap<String, BindingId>` - 変数名 → BindingId マッピング
|
||||
- `variable_map: BTreeMap<String, ValueId>` - 変数名 → ValueId マッピング (SSA)
|
||||
## 残りの Context (2/7)
|
||||
|
||||
### 5. MetadataContext (計画中)
|
||||
- `hint_sink: HintSink` - ヒントシンク (zero-cost guidance)
|
||||
@ -135,10 +194,9 @@ builder.rs の 1219 行を責任ごとに Context Box に分割し、保守性
|
||||
|
||||
## 次のステップ
|
||||
|
||||
**優先順位 4**: BindingContext 抽出
|
||||
- 変数バインディング管理の集約
|
||||
- variable_map と binding_map の統一管理
|
||||
- SSA 変換との連携強化
|
||||
**優先順位 6**: MetadataContext 抽出
|
||||
- ヒントシンク・スパン・リージョン管理の集約
|
||||
- コンパイル時メタデータの統一管理
|
||||
|
||||
## 参考資料
|
||||
|
||||
|
||||
Reference in New Issue
Block a user