feat: 大規模リファクタリング - SRP原則に基づくモジュール分割

## MIR builder_calls.rs リファクタリング
- 879行 → 629行 (28%削減) + 7専門モジュール
- calls/ ディレクトリに機能別分割:
  - call_target.rs: CallTarget型定義
  - method_resolution.rs: メソッド解決ロジック
  - extern_calls.rs: 外部呼び出し処理
  - special_handlers.rs: 特殊ハンドラー
  - function_lowering.rs: 関数変換ユーティリティ
  - call_unified.rs: 統一Call実装
  - mod.rs: モジュール統合

## Parser statements.rs リファクタリング
- 723行 → 8専門モジュール
- statements/ ディレクトリに機能別分割:
  - control_flow.rs: if/loop/break/continue/return
  - declarations.rs: 宣言系ディスパッチャー
  - exceptions.rs: try/throw/catch/cleanup
  - helpers.rs: ヘルパー関数
  - io_async.rs: print/nowait
  - modules.rs: import/using/from
  - variables.rs: local/outbox/assignments
  - mod.rs: 統合モジュール

## 効果
 単一責任原則(SRP)の達成
 保守性・再利用性の向上
 ChatGPT5 Pro設計の型安全Call解決システム実装
 スモークテスト通過確認済み

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Selfhosting Dev
2025-09-25 09:01:55 +09:00
parent 26d181cac3
commit 2f306dd6a5
23 changed files with 1819 additions and 1616 deletions

View File

@ -0,0 +1,150 @@
/*!
* Function Lowering Utilities
*
* Helpers for lowering box methods and static methods to MIR functions
* Manages the complex state transitions during function lowering
*/
use crate::ast::ASTNode;
use crate::mir::{Effect, EffectMask, FunctionSignature, MirType};
use super::special_handlers::contains_value_return;
/// Prepare function signature for a box method
/// Includes 'me' parameter as first parameter
pub fn prepare_method_signature(
func_name: String,
box_name: &str,
params: &[String],
body: &[ASTNode],
) -> FunctionSignature {
let mut param_types = Vec::new();
// First parameter is always 'me' (the box instance)
param_types.push(MirType::Box(box_name.to_string()));
// Additional parameters (type unknown initially)
for _ in params {
param_types.push(MirType::Unknown);
}
// Determine return type based on body analysis
let returns_value = contains_value_return(body);
let ret_ty = if returns_value {
MirType::Unknown // Will be inferred later
} else {
MirType::Void
};
FunctionSignature {
name: func_name,
params: param_types,
return_type: ret_ty,
effects: EffectMask::READ.add(Effect::ReadHeap),
}
}
/// Prepare function signature for a static method
/// No 'me' parameter needed
pub fn prepare_static_method_signature(
func_name: String,
params: &[String],
body: &[ASTNode],
) -> FunctionSignature {
let mut param_types = Vec::new();
// Parameters (type unknown initially)
for _ in params {
param_types.push(MirType::Unknown);
}
// Determine return type based on body analysis
let returns_value = contains_value_return(body);
let ret_ty = if returns_value {
MirType::Unknown // Will be inferred later
} else {
MirType::Void
};
FunctionSignature {
name: func_name,
params: param_types,
return_type: ret_ty,
effects: EffectMask::READ.add(Effect::ReadHeap),
}
}
/// Generate canonical method name for MIR function
/// E.g., "StringBox.upper/0" for StringBox's upper method with 0 args
pub fn generate_method_function_name(
box_name: &str,
method_name: &str,
arity: usize,
) -> String {
format!("{}.{}/{}", box_name, method_name, arity)
}
/// Generate canonical static method name for MIR function
/// E.g., "Main.main/0" for static box Main's main method
pub fn generate_static_method_function_name(
static_box_name: &str,
method_name: &str,
arity: usize,
) -> String {
format!("{}.{}/{}", static_box_name, method_name, arity)
}
/// Check if a function needs termination with void return
pub fn needs_void_termination(returns_value: bool, is_terminated: bool) -> bool {
!returns_value && !is_terminated
}
/// Create parameter mapping for method lowering
/// Returns (parameter_names, includes_me)
pub fn create_method_parameter_mapping(
box_name: &str,
params: &[String],
) -> (Vec<(String, MirType)>, bool) {
let mut param_mapping = Vec::new();
// Add 'me' parameter
param_mapping.push(("me".to_string(), MirType::Box(box_name.to_string())));
// Add regular parameters
for p in params {
param_mapping.push((p.clone(), MirType::Unknown));
}
(param_mapping, true)
}
/// Create parameter mapping for static method lowering
pub fn create_static_parameter_mapping(
params: &[String],
) -> Vec<(String, MirType)> {
params.iter()
.map(|p| (p.clone(), MirType::Unknown))
.collect()
}
/// Wrap statements in a Program node for consistent processing
pub fn wrap_in_program(statements: Vec<ASTNode>) -> ASTNode {
ASTNode::Program {
statements,
span: crate::ast::Span::unknown(),
}
}
/// Check if method name suggests it returns a value
pub fn method_likely_returns_value(method_name: &str) -> bool {
// Heuristic: methods that likely return values
method_name.starts_with("get") ||
method_name.starts_with("is") ||
method_name.starts_with("has") ||
method_name.starts_with("to") ||
matches!(method_name,
"length" | "size" | "count" |
"upper" | "lower" | "trim" |
"add" | "sub" | "mul" | "div" |
"min" | "max" | "abs"
)
}