Phase 25.1 完了成果: - ✅ LoopForm v2 テスト・ドキュメント・コメント完備 - 4ケース(A/B/C/D)完全テストカバレッジ - 最小再現ケース作成(SSAバグ調査用) - SSOT文書作成(loopform_ssot.md) - 全ソースに [LoopForm] コメントタグ追加 - ✅ Stage-1 CLI デバッグ環境構築 - stage1_cli.hako 実装 - stage1_bridge.rs ブリッジ実装 - デバッグツール作成(stage1_debug.sh/stage1_minimal.sh) - アーキテクチャ改善提案文書 - ✅ 環境変数削減計画策定 - 25変数の完全調査・分類 - 6段階削減ロードマップ(25→5、80%削減) - 即時削除可能変数特定(NYASH_CONFIG/NYASH_DEBUG) Phase 26-D からの累積変更: - PHI実装改善(ExitPhiBuilder/HeaderPhiBuilder等) - MIRビルダーリファクタリング - 型伝播・最適化パス改善 - その他約300ファイルの累積変更 🎯 技術的成果: - SSAバグ根本原因特定(条件分岐内loop変数変更) - Region+next_iパターン適用完了(UsingCollectorBox等) - LoopFormパターン文書化・テスト化完了 - セルフホスティング基盤強化 Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: ChatGPT <noreply@openai.com> Co-Authored-By: Task Assistant <task@anthropic.com>
158 lines
4.4 KiB
Rust
158 lines
4.4 KiB
Rust
#![allow(dead_code)]
|
|
|
|
/*!
|
|
* Function Lowering Utilities
|
|
*
|
|
* Helpers for lowering box methods and static methods to MIR functions
|
|
* Manages the complex state transitions during function lowering
|
|
*/
|
|
|
|
use super::special_handlers::contains_value_return;
|
|
use crate::ast::ASTNode;
|
|
use crate::mir::{Effect, EffectMask, FunctionSignature, MirType};
|
|
|
|
/// 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"
|
|
)
|
|
}
|