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

## Summary
Extract scope and control flow management into ScopeContext for better organization.

## Changes
- **New file**: src/mir/builder/scope_context.rs (264 lines)
  - Lexical scope stack management
  - Control flow stacks (loop/if)
  - Function context tracking
  - Debug scope helpers
- **Updated**: src/mir/builder.rs
  - Add scope_ctx field
  - Mark legacy fields as deprecated
  - Add sync helpers (sync_scope_ctx_to_legacy, sync_legacy_to_scope_ctx)
- **Updated**: src/mir/builder/vars/lexical_scope.rs
  - Use scope_ctx as SSOT
  - Sync to legacy fields for backward compat
- **Updated**: src/mir/builder/lifecycle.rs
  - Sync current_function via scope_ctx
- **Updated**: src/mir/builder/calls/lowering.rs
  - Sync function context in lowering flow

## Extracted Fields (7)
1. lexical_scope_stack - Block-scoped locals
2. loop_header_stack - Loop headers for break/continue
3. loop_exit_stack - Loop exit blocks
4. if_merge_stack - If merge blocks
5. current_function - Currently building function
6. function_param_names - Function parameters (for LoopForm)
7. debug_scope_stack - Debug region identifiers

## Test Results
-  cargo build --release (291 warnings - deprecated usage)
-  cargo test --release --lib - 1005/1009 PASS
-  phase135_trim_mir_verify.sh - PASS
- ⚠️ phase132_exit_phi_parity.sh - Error (pre-existing issue)

## Progress
Phase 136: 3/7 Contexts complete (TypeContext, CoreContext, ScopeContext)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-15 20:28:21 +09:00
parent 89edf11699
commit 3127ebb73d
6 changed files with 463 additions and 57 deletions

View File

@ -4,7 +4,7 @@
builder.rs の 1219 行を責任ごとに Context Box に分割し、保守性・テスト容易性を向上させる段階的リファクタリング。
## 完了した Context (2/7)
## 完了した Context (3/7)
### ✅ TypeContext (Step 1) - 完了
@ -65,37 +65,66 @@ builder.rs の 1219 行を責任ごとに Context Box に分割し、保守性
- builder 内 30+ ファイルで `block_gen.next()``next_block_id()` に自動置換
- 段階的移行により破壊的変更なし
**コミット**: 81d79161
**コミット**: 81d79161, 89edf116
## 残りの Context (5/7)
### ✅ ScopeContext (Step 3) - 完了
### 3. ScopeContext (計画中)
- `lexical_scope_stack: Vec<LexicalScopeFrame>`
- `scope_stack: Vec<...>` (既存の control flow スタック)
- `current_function: Option<MirFunction>`
**実装日**: 2025-12-15
**抽出したフィールド** (7個):
- `lexical_scope_stack: Vec<LexicalScopeFrame>` - Block-scoped local 変数スコープ
- `loop_header_stack: Vec<BasicBlockId>` - ループヘッダースタック (break/continue 用)
- `loop_exit_stack: Vec<BasicBlockId>` - ループ出口スタック
- `if_merge_stack: Vec<BasicBlockId>` - If マージブロックスタック
- `current_function: Option<MirFunction>` - 現在ビルド中の関数
- `function_param_names: HashSet<String>` - 関数パラメータ名 (LoopForm PHI 用)
- `debug_scope_stack: Vec<String>` - デバッグリージョン識別子スタック
**ファイル**:
- `/home/tomoaki/git/hakorune-selfhost/src/mir/builder/scope_context.rs` (新規作成)
**統合方法**:
- `MirBuilder``scope_ctx: ScopeContext` フィールドを追加
- 既存フィールドは `#[deprecated]` でマーク(後方互換性維持)
- Lexical scope ヘルパー (`push_lexical_scope()`, `pop_lexical_scope()`, `declare_local_in_current_scope()`) が scope_ctx を SSOT として使用
- Control flow stack ヘルパー (`push_if_merge()`, `pop_if_merge()`) が両方を同期
- Debug scope ヘルパー (`debug_push_region()`, `debug_pop_region()`, `debug_current_region_id()`) を更新
- Function context は設定/復元時に両方を同期 (lifecycle.rs, calls/lowering.rs)
**テスト結果**:
-`cargo build --release` 成功 (291 warnings - deprecated フィールド使用)
-`cargo test --release --lib` - 1005/1009 PASS (4 tests 失敗は既存問題)
-`phase135_trim_mir_verify.sh` - PASS
- ⚠️ `phase132_exit_phi_parity.sh` - エラー (既存問題、ScopeContext 変更とは無関係)
**影響範囲**:
- `vars/lexical_scope.rs` - scope_ctx 使用に更新
- `lifecycle.rs` - current_function 設定/復元を scope_ctx 同期
- `calls/lowering.rs` - 関数 lowering の文脈管理を scope_ctx 同期
- 段階的移行により破壊的変更なし
**コミット**: [今回のコミット]
## 残りの Context (4/7)
### 4. BindingContext (計画中)
- `bindings_stack: ...`
- `global_bindings: ...`
- `next_binding_id: u32`
- `binding_map: BTreeMap<String, BindingId>`
- `binding_map: BTreeMap<String, BindingId>` - 変数名 → BindingId マッピング
- `variable_map: BTreeMap<String, ValueId>` - 変数名 → ValueId マッピング (SSA)
### 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>`
### 5. MetadataContext (計画中)
- `hint_sink: HintSink` - ヒントシンク (zero-cost guidance)
- `current_span: Span` - 現在の AST スパン
- `source_file: Option<String>` - ソースファイルヒント
- `current_region_stack: Vec<RegionId>` - Region 観測スタック
### 6. MetadataContext (計画中)
- `metadata: ...`
- `loc_gen: ...`
- `source_map: ...`
- `hint_sink: HintSink`
### 6. CompilationContext (計画中)
- `compilation_context: Option<BoxCompilationContext>` - Box コンパイルコンテキスト
- `current_static_box: Option<String>` - 現在の static box 名
- `user_defined_boxes: HashSet<String>` - ユーザー定義 Box 名
- `reserved_value_ids: HashSet<ValueId>` - 予約済み ValueId
### 7. ResourceContext (計画中)
- `handle_registry: ...` (将来追加予定)
- その他リソース管理関連
### 7. ResourceContext (将来予定)
- リソース管理関連フィールド (将来追加)
## 設計原則
@ -106,10 +135,10 @@ builder.rs の 1219 行を責任ごとに Context Box に分割し、保守性
## 次のステップ
**優先順位 3**: ScopeContext 抽出
- スコープスタック管理の集約
- 制御フロースタック (loop/if/try) の統合
- 関数コンテキスト管理の整理
**優先順位 4**: BindingContext 抽出
- 変数バインディング管理の集約
- variable_map と binding_map の統一管理
- SSA 変換との連携強化
## 参考資料