Span trace utilities and runner source hint

This commit is contained in:
nyash-codex
2025-11-24 14:17:02 +09:00
parent 3154903121
commit 466e636af6
106 changed files with 4597 additions and 958 deletions

View File

@ -9,12 +9,12 @@ use super::{
BasicBlock, BasicBlockId, BasicBlockIdGenerator, CompareOp, ConstValue, Effect, EffectMask,
FunctionSignature, MirFunction, MirInstruction, MirModule, MirType, ValueId, ValueIdGenerator,
};
use crate::ast::{ASTNode, LiteralValue};
use crate::ast::{ASTNode, LiteralValue, Span};
use crate::mir::builder::builder_calls::CallTarget;
use crate::mir::region::function_slot_registry::FunctionSlotRegistry;
use crate::mir::region::RegionId;
use std::collections::{BTreeMap, HashMap};
use std::collections::HashSet;
use std::collections::{BTreeMap, HashMap};
mod builder_calls;
mod call_resolution; // ChatGPT5 Pro: Type-safe call resolution utilities
mod calls; // Call system modules (refactored from builder_calls)
@ -219,6 +219,12 @@ pub struct MirBuilder {
/// Tracks the depth of build_expression calls to detect infinite loops
pub(super) recursion_depth: usize,
/// Current AST span being lowered (used to annotate MIR instructions)
pub(super) current_span: Span,
/// Optional source file hint for metadata/spans
pub(super) source_file: Option<String>,
/// Root lowering mode: how to treat top-level Program
/// - None: not decided yet (lower_root not called)
/// - Some(true): App mode (static box Main.main is entry)
@ -243,7 +249,7 @@ impl MirBuilder {
current_block: None,
value_gen: ValueIdGenerator::new(),
block_gen: BasicBlockIdGenerator::new(),
compilation_context: None, // 箱理論: デフォルトは従来モード
compilation_context: None, // 箱理論: デフォルトは従来モード
variable_map: BTreeMap::new(), // Phase 25.1: 決定性確保
pending_phis: Vec::new(),
value_origin_newbox: BTreeMap::new(), // Phase 25.1: 決定性確保
@ -253,7 +259,7 @@ impl MirBuilder {
field_origin_class: HashMap::new(),
field_origin_by_box: HashMap::new(),
value_types: BTreeMap::new(), // Phase 25.1: 決定性確保
value_kinds: HashMap::new(), // Phase 26-A: ValueId型安全化
value_kinds: HashMap::new(), // Phase 26-A: ValueId型安全化
current_slot_registry: None,
type_registry: type_registry::TypeRegistry::new(),
plugin_method_sigs,
@ -290,6 +296,8 @@ impl MirBuilder {
in_unified_boxcall_fallback: false,
recursion_depth: 0,
current_span: Span::unknown(),
source_file: None,
root_is_app_mode: None,
static_box_singletons: HashMap::new(), // Phase 21.7: methodization support
}
@ -347,6 +355,34 @@ impl MirBuilder {
self.debug_scope_stack.last().cloned()
}
/// Hint for downstream metadata: set the logical source file name/path for the next build.
pub fn set_source_file_hint<S: Into<String>>(&mut self, source: S) {
self.source_file = Some(source.into());
}
/// Clear the source file hint (used when reusing the builder across modules).
pub fn clear_source_file_hint(&mut self) {
self.source_file = None;
}
/// Resolve current source file hint (builder field or env fallback).
fn current_source_file(&self) -> Option<String> {
self.source_file
.clone()
.or_else(|| std::env::var("NYASH_SOURCE_FILE_HINT").ok())
}
/// Create a new MirFunction with source metadata applied.
fn new_function_with_metadata(
&self,
signature: FunctionSignature,
entry_block: BasicBlockId,
) -> MirFunction {
let mut f = MirFunction::new(signature, entry_block);
f.metadata.source_file = self.current_source_file();
f
}
// ----------------------
// Compile trace helpers (dev only; env-gated)
// ----------------------
@ -739,7 +775,7 @@ impl MirBuilder {
}
);
}
block.add_instruction(instruction);
block.add_instruction_with_span(instruction, self.current_span);
// Drop the mutable borrow of `block` before updating other blocks
}
// Update predecessor sets for branch/jump immediately so that