feat(mir): Phase 136 Step 7/7 - CompilationContext extraction

**Step 7/7 Complete**: Extract compilation-related fields into CompilationContext

**抽出したフィールド** (15個):
- compilation_context: Box compilation context
- current_static_box: Current static box name
- user_defined_boxes: User-defined box registry
- reserved_value_ids: Reserved ValueIds for PHI
- fn_body_ast: Function body AST for capture analysis
- weak_fields_by_box: Weak field registry
- property_getters_by_box: Property getter registry
- field_origin_class: Field origin tracking
- field_origin_by_box: Class-level field origin
- static_method_index: Static method index
- method_tail_index: Method tail index (fast lookup)
- method_tail_index_source_len: Source size snapshot
- type_registry: Type registry (TypeRegistryBox)
- current_slot_registry: Function scope SlotRegistry
- plugin_method_sigs: Plugin method signatures

**新規ファイル**:
- src/mir/builder/compilation_context.rs (435 lines)
  - CompilationContext struct with 15 fields
  - Helper methods for all compilation operations
  - Comprehensive tests (13 test cases)

**MirBuilder 統合**:
- Added comp_ctx: CompilationContext field
- Marked 15 legacy fields as #[deprecated]
- CompilationContext::with_plugin_sigs() initialization

**テスト結果**:
-  cargo build --release (469 warnings - deprecated)
-  cargo test --release --lib (1029/1033 PASS)
-  phase135_trim_mir_verify.sh (PASS)

**影響範囲**:
- 12 files use comp_ctx fields (87 total usages)
- 36 deprecated fields in builder.rs (ready for Phase 2 cleanup)

**Phase 136 Status**:  7/7 Context Boxes Complete!
- TypeContext, CoreContext, ScopeContext (Steps 1-3)
- BindingContext, VariableContext, MetadataContext (Steps 4-6)
- CompilationContext (Step 7) - NOW COMPLETE

**Next Phase**: Legacy field deletion (Phase 2)
- Remove #[deprecated] fields from builder.rs
- Migrate all code to ctx accessors
- Reduce deprecation warnings from 469 → 0

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-15 22:12:33 +09:00
parent 903ab8ef4c
commit ceb7baffb5
3 changed files with 559 additions and 23 deletions

View File

@ -205,15 +205,61 @@ builder.rs の 1219 行を責任ごとに Context Box に分割し、保守性
- builder 内で current_span, source_file, hint_sink, current_region_stack を使用中のコードを metadata_ctx 経由に移行
- 段階的移行により破壊的変更なし
**コミット**: [今回のコミット]
**コミット**: 903ab8ef
## 残りの Context (1/7)
## ✅ 全 Context 完了! (7/7)
### 6. CompilationContext (計画中)
### CompilationContext (Step 7) - 完了
**実装日**: 2025-12-15
**抽出したフィールド** (15個):
- `compilation_context: Option<BoxCompilationContext>` - Box コンパイルコンテキスト
- `current_static_box: Option<String>` - 現在の static box 名
- `user_defined_boxes: HashSet<String>` - ユーザー定義 Box 名
- `reserved_value_ids: HashSet<ValueId>` - 予約済み ValueId
- `reserved_value_ids: HashSet<ValueId>` - 予約済み ValueId (PHI 用)
- `fn_body_ast: Option<Vec<ASTNode>>` - 関数本体 AST (キャプチャ分析用)
- `weak_fields_by_box: HashMap<String, HashSet<String>>` - Weak フィールドレジストリ
- `property_getters_by_box: HashMap<String, HashMap<String, PropertyKind>>` - Property getter レジストリ
- `field_origin_class: HashMap<(ValueId, String), String>` - フィールド origin 追跡
- `field_origin_by_box: HashMap<(String, String), String>` - クラスレベル origin
- `static_method_index: HashMap<String, Vec<(String, usize)>>` - Static method インデックス
- `method_tail_index: HashMap<String, Vec<String>>` - Method tail インデックス (高速検索)
- `method_tail_index_source_len: usize` - Source サイズスナップショット
- `type_registry: TypeRegistry` - 型情報管理の一元化 (TypeRegistryBox)
- `current_slot_registry: Option<FunctionSlotRegistry>` - 関数スコープ SlotRegistry
- `plugin_method_sigs: HashMap<(String, String), MirType>` - Plugin method シグネチャ
**ファイル**:
- `/home/tomoaki/git/hakorune-selfhost/src/mir/builder/compilation_context.rs` (新規作成, 405 行)
**統合方法**:
- `MirBuilder``comp_ctx: CompilationContext` フィールドを追加
- 既存フィールドは `#[deprecated]` でマーク(後方互換性維持)
- `CompilationContext::with_plugin_sigs()` で plugin_method_sigs を初期化
- 全 15 フィールドが comp_ctx に統合され、SSOT 化完了
**特徴**:
- **Box コンパイル**: BoxCompilationContext で static box コンパイル分離
- **PHI 予約**: reserved_value_ids で LoopHeaderPhiBuilder の ValueId 衝突を防止
- **キャプチャ分析**: fn_body_ast を FunctionScopeCaptureAnalyzer で使用
- **Method 解決**: static_method_index + method_tail_index で高速検索
- **Weak フィールド**: weak_fields_by_box で weak 参照管理
- **Property**: property_getters_by_box で computed/once/birth_once 管理
- **Origin 追跡**: field_origin_class + field_origin_by_box で型推論支援
- **型情報**: type_registry で型情報一元管理 (NYASH_USE_TYPE_REGISTRY=1)
- **Slot レジストリ**: current_slot_registry で関数スコープ観測
**テスト結果**:
-`cargo build --release` 成功 (469 warnings - deprecated フィールド使用)
-`cargo test --release --lib` - 1029/1033 PASS (4 tests 失敗は既存問題)
-`phase135_trim_mir_verify.sh` - PASS
**影響範囲**:
- builder 内の compilation 関連フィールドを使用中のコードは全て comp_ctx 経由に移行可能
- 段階的移行により破壊的変更なし
**コミット**: [今回のコミット]
## 設計原則
@ -222,11 +268,24 @@ builder.rs の 1219 行を責任ごとに Context Box に分割し、保守性
3. **Box-First** - 各 Context は独立した struct として配置
4. **テスト駆動** - 各段階で全テストが PASS することを確認
## 次のステップ
## 次のステップ: Legacy フィールド削除
**優先順位 7**: CompilationContext 抽出 (最終ステップ)
- Box コンパイルコンテキストの集約
- 静的 Box 管理・予約済み ValueId の統一管理
Phase 136 follow-up の全 7 Context が完了しました!次は **Phase 2: レガシーフィールド削除** です。
**Phase 2 タスク**:
1. builder.rs から `#[deprecated]` フィールドを削除
2. sync helper メソッドを削除 (`sync_*_to_legacy`, `sync_legacy_to_*`)
3. 全ファイルを ctx 経由に移行 (段階的、Context ごと)
- `rg "self\.value_types" src/mir/builder/``self.type_ctx.value_types`
- `rg "self\.value_gen" src/mir/builder/``self.core_ctx.value_gen`
- 等々、全フィールド
4. テスト実行(各 Context 削除後)
5. コミット1-2 回に分割可能)
**期待効果**:
- Deprecation warnings が 469 → 0 に削減
- builder.rs の行数削減1200行 → 800行程度を期待
- Context Box 化の完全完了!
## 参考資料