2025-12-15 19:59:55 +09:00
|
|
|
# Phase 136 Follow-up: Builder Context Box化 進捗
|
|
|
|
|
|
|
|
|
|
## 概要
|
|
|
|
|
|
|
|
|
|
builder.rs の 1219 行を責任ごとに Context Box に分割し、保守性・テスト容易性を向上させる段階的リファクタリング。
|
|
|
|
|
|
2025-12-15 20:10:36 +09:00
|
|
|
## 完了した Context (2/7)
|
2025-12-15 19:59:55 +09:00
|
|
|
|
|
|
|
|
### ✅ TypeContext (Step 1) - 完了
|
|
|
|
|
|
|
|
|
|
**実装日**: 2025-12-15
|
|
|
|
|
|
|
|
|
|
**抽出したフィールド** (3個):
|
|
|
|
|
- `value_types: BTreeMap<ValueId, MirType>` - 型注釈マップ
|
|
|
|
|
- `value_kinds: HashMap<ValueId, MirValueKind>` - 型種別マップ (Phase 26-A)
|
|
|
|
|
- `value_origin_newbox: BTreeMap<ValueId, String>` - Box クラス名由来追跡
|
|
|
|
|
|
|
|
|
|
**ファイル**:
|
|
|
|
|
- `/home/tomoaki/git/hakorune-selfhost/src/mir/builder/type_context.rs` (新規作成)
|
|
|
|
|
|
|
|
|
|
**統合方法**:
|
|
|
|
|
- `MirBuilder` に `type_ctx: TypeContext` フィールドを追加
|
|
|
|
|
- 既存フィールドは `#[deprecated]` でマーク(後方互換性維持)
|
|
|
|
|
- 同期ヘルパー (`sync_type_ctx_to_legacy()`, `sync_legacy_to_type_ctx()`) を実装
|
|
|
|
|
|
|
|
|
|
**テスト結果**:
|
|
|
|
|
- ✅ `cargo build --release` 成功 (警告のみ)
|
|
|
|
|
- ✅ `cargo test --release --lib` - 997/997 PASS
|
|
|
|
|
- ✅ `phase135_trim_mir_verify.sh` - PASS
|
|
|
|
|
- ✅ `phase132_exit_phi_parity.sh` - 3/3 PASS
|
|
|
|
|
|
|
|
|
|
**影響範囲**:
|
|
|
|
|
- 16 ファイルで 113 箇所が deprecated フィールドを使用中
|
|
|
|
|
- 段階的移行により破壊的変更なし
|
|
|
|
|
|
2025-12-15 20:10:36 +09:00
|
|
|
**コミット**: 076f193f
|
|
|
|
|
|
|
|
|
|
### ✅ CoreContext (Step 2) - 完了
|
|
|
|
|
|
|
|
|
|
**実装日**: 2025-12-15
|
|
|
|
|
|
|
|
|
|
**抽出したフィールド** (5個):
|
|
|
|
|
- `value_gen: ValueIdGenerator` - SSA 値 ID 生成器
|
|
|
|
|
- `block_gen: BasicBlockIdGenerator` - 基本ブロック ID 生成器
|
|
|
|
|
- `next_binding_id: u32` - BindingId 割り当てカウンタ (Phase 74)
|
|
|
|
|
- `temp_slot_counter: u32` - 一時ピンスロットカウンタ
|
|
|
|
|
- `debug_join_counter: u32` - デバッグスコープ join ID カウンタ
|
|
|
|
|
|
|
|
|
|
**ファイル**:
|
|
|
|
|
- `/home/tomoaki/git/hakorune-selfhost/src/mir/builder/core_context.rs` (新規作成)
|
|
|
|
|
|
|
|
|
|
**統合方法**:
|
|
|
|
|
- `MirBuilder` に `core_ctx: CoreContext` フィールドを追加
|
|
|
|
|
- 既存フィールドは `#[deprecated]` でマーク(後方互換性維持)
|
|
|
|
|
- ID 割り当てメソッド (`next_value_id()`, `allocate_binding_id()`, `debug_next_join_id()`) が core_ctx を SSOT として使用し、legacy フィールドを同期
|
|
|
|
|
- 新規ヘルパー `next_block_id()` を追加し、30 箇所の `block_gen.next()` 呼び出しを置換
|
|
|
|
|
|
|
|
|
|
**テスト結果**:
|
|
|
|
|
- ✅ `cargo build --release` 成功 (警告のみ、193 warnings)
|
|
|
|
|
- ✅ `cargo test --release --lib` - 1004/1004 PASS (7 tests 追加)
|
|
|
|
|
- ✅ `phase135_trim_mir_verify.sh` - PASS
|
|
|
|
|
- ✅ `phase132_exit_phi_parity.sh` - 3/3 PASS
|
2025-12-15 19:59:55 +09:00
|
|
|
|
2025-12-15 20:10:36 +09:00
|
|
|
**影響範囲**:
|
|
|
|
|
- builder 内 30+ ファイルで `block_gen.next()` を `next_block_id()` に自動置換
|
|
|
|
|
- 段階的移行により破壊的変更なし
|
|
|
|
|
|
|
|
|
|
**コミット**: (次回コミット時に記載)
|
2025-12-15 19:59:55 +09:00
|
|
|
|
2025-12-15 20:10:36 +09:00
|
|
|
## 残りの Context (5/7)
|
2025-12-15 19:59:55 +09:00
|
|
|
|
|
|
|
|
### 3. ScopeContext (計画中)
|
|
|
|
|
- `lexical_scope_stack: Vec<LexicalScopeFrame>`
|
|
|
|
|
- `scope_stack: Vec<...>` (既存の control flow スタック)
|
|
|
|
|
- `current_function: Option<MirFunction>`
|
|
|
|
|
|
|
|
|
|
### 4. BindingContext (計画中)
|
|
|
|
|
- `bindings_stack: ...`
|
|
|
|
|
- `global_bindings: ...`
|
|
|
|
|
- `next_binding_id: u32`
|
|
|
|
|
- `binding_map: BTreeMap<String, BindingId>`
|
|
|
|
|
|
|
|
|
|
### 5. ControlFlowContext (計画中)
|
|
|
|
|
- `current_basic_block: Option<BasicBlockId>`
|
|
|
|
|
- `pending_phis: Vec<...>`
|
|
|
|
|
- `loop_header_stack: Vec<BasicBlockId>`
|
|
|
|
|
- `loop_exit_stack: Vec<BasicBlockId>`
|
|
|
|
|
- `if_merge_stack: Vec<BasicBlockId>`
|
|
|
|
|
|
|
|
|
|
### 6. MetadataContext (計画中)
|
|
|
|
|
- `metadata: ...`
|
|
|
|
|
- `loc_gen: ...`
|
|
|
|
|
- `source_map: ...`
|
|
|
|
|
- `hint_sink: HintSink`
|
|
|
|
|
|
|
|
|
|
### 7. ResourceContext (計画中)
|
|
|
|
|
- `handle_registry: ...` (将来追加予定)
|
|
|
|
|
- その他リソース管理関連
|
|
|
|
|
|
|
|
|
|
## 設計原則
|
|
|
|
|
|
|
|
|
|
1. **段階的移行** - 全フィールドを一度に移行せず、1-2 Context ずつ
|
|
|
|
|
2. **後方互換性** - 既存の public API は維持(内部で Context 経由に変更)
|
|
|
|
|
3. **Box-First** - 各 Context は独立した struct として配置
|
|
|
|
|
4. **テスト駆動** - 各段階で全テストが PASS することを確認
|
|
|
|
|
|
|
|
|
|
## 次のステップ
|
|
|
|
|
|
2025-12-15 20:10:36 +09:00
|
|
|
**優先順位 3**: ScopeContext 抽出
|
|
|
|
|
- スコープスタック管理の集約
|
|
|
|
|
- 制御フロースタック (loop/if/try) の統合
|
|
|
|
|
- 関数コンテキスト管理の整理
|
2025-12-15 19:59:55 +09:00
|
|
|
|
|
|
|
|
## 参考資料
|
|
|
|
|
|
|
|
|
|
- [Phase 136 分析ドキュメント](./phase-136-builder-analysis.md) (前提分析)
|
|
|
|
|
- [Builder.rs](../../../../src/mir/builder.rs) (対象ファイル)
|