intermediary state, safe point

This commit is contained in:
2026-03-24 13:40:32 +00:00
parent c80260e780
commit f5d259ba2a
14 changed files with 702 additions and 480 deletions
@@ -1,11 +1,11 @@
use crate::building::linker::{LinkError, Linker};
use crate::building::output::{compile_project, CompileError};
use crate::building::plan::{BuildPlan, BuildTarget};
use crate::common::files::FileManager;
use crate::common::diagnostics::DiagnosticBundle;
use crate::common::files::FileManager;
use crate::deps::resolver::ResolvedGraph;
use std::collections::HashMap;
use prometeu_abi::virtual_machine::ProgramImage;
use std::collections::HashMap;
#[derive(Debug)]
pub enum BuildError {
@@ -150,11 +150,10 @@ pub fn build_from_graph(graph: &ResolvedGraph, target: BuildTarget) -> Result<Bu
mod tests {
use super::*;
use crate::deps::resolver::{ProjectKey, ResolvedGraph, ResolvedNode};
use crate::sources::discover;
use crate::manifest::{Manifest, ManifestKind};
use std::collections::BTreeMap;
use crate::sources::discover;
use prometeu_analysis::ids::ProjectId;
use std::collections::HashMap;
use std::collections::BTreeMap;
use std::fs;
use std::path::PathBuf;
use tempfile::tempdir;
+300 -256
View File
@@ -4,21 +4,20 @@ use crate::common::diagnostics::DiagnosticBundle;
use crate::common::files::FileManager;
use crate::common::spans::{FileId, Span};
use crate::deps::resolver::ProjectKey;
use prometeu_analysis::ids::ProjectId;
use crate::frontends::pbs::ast::ParsedAst;
use crate::frontends::pbs::collector::SymbolCollector;
use crate::frontends::pbs::lowering::Lowerer;
use crate::frontends::pbs::parser::Parser;
use crate::frontends::pbs::resolver::{ModuleProvider, Resolver};
use crate::frontends::pbs::symbols::{ModuleSymbols, Namespace, Symbol, SymbolKind, Visibility};
use crate::frontends::pbs::typecheck::TypeChecker;
use crate::frontends::pbs::types::PbsType;
use crate::frontends::pbs::{build_typed_module_symbols, SymbolCollector};
use crate::lowering::core_to_vm;
use crate::semantics::export_surface::ExportSurfaceKind;
use prometeu_bytecode::{ConstantPoolEntry, DebugInfo, FunctionMeta};
use prometeu_analysis::ids::ProjectId;
use prometeu_analysis::NameInterner;
use prometeu_bytecode::{ConstantPoolEntry, DebugInfo, FunctionMeta};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};
use crate::frontends::pbs::parser::Parser;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct ExportKey {
@@ -63,7 +62,7 @@ pub struct CompiledModule {
#[derive(Debug)]
pub enum CompileError {
Frontend(crate::common::diagnostics::DiagnosticBundle),
Frontend(DiagnosticBundle),
DuplicateExport {
symbol: String,
first_dep: String,
@@ -77,9 +76,15 @@ impl std::fmt::Display for CompileError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CompileError::Frontend(d) => write!(f, "Frontend error: {:?}", d),
CompileError::DuplicateExport { symbol, first_dep, second_dep } => {
write!(f, "duplicate export: symbol `{}`\n first defined in dependency `{}`\n again defined in dependency `{}`", symbol, first_dep, second_dep)
}
CompileError::DuplicateExport {
symbol,
first_dep,
second_dep,
} => write!(
f,
"duplicate export: symbol `{}`\n first defined in dependency `{}`\n again defined in dependency `{}`",
symbol, first_dep, second_dep
),
CompileError::Io(e) => write!(f, "IO error: {}", e),
CompileError::Internal(s) => write!(f, "Internal error: {}", s),
}
@@ -116,21 +121,22 @@ pub fn compile_project(
file_manager: &mut FileManager,
) -> Result<CompiledModule, CompileError> {
let mut interner = NameInterner::new();
// 1. Parse all files and group symbols by module
// 1) Parse all files; collect+merge symbols by module_path
let mut module_symbols_map: HashMap<String, ModuleSymbols> = HashMap::new();
let mut parsed_files: Vec<(String, ParsedAst)> = Vec::new(); // (module_path, ast)
let mut parsed_files: Vec<(String, ParsedAst)> = Vec::new();
for source_rel in &step.sources {
let source_abs = step.project_dir.join(source_rel);
let source_code = std::fs::read_to_string(&source_abs)?;
let file_id = file_manager.add(source_abs.clone(), source_code.clone());
let mut parser = Parser::new(&source_code, FileId(file_id as u32), &mut interner);
let parsed = parser.parse_file()?;
let mut collector = SymbolCollector::new(&interner);
let (ts, vs) = collector.collect(&parsed.arena, parsed.root)?;
let full_path = source_rel.to_string_lossy().replace('\\', "/");
let logical_module_path = if let Some(stripped) = full_path.strip_prefix("src/main/modules/") {
stripped
@@ -144,183 +150,207 @@ pub fn compile_project(
.parent()
.map(|p| p.to_string_lossy().replace('\\', "/"))
.unwrap_or_else(|| "".to_string());
let ms = module_symbols_map.entry(module_path.clone()).or_insert_with(ModuleSymbols::new);
// Merge symbols
for sym in ts.symbols.into_values() {
if let Some(existing) = ms.type_symbols.get(sym.name) {
return Err(DiagnosticBundle::error(
"E_RESOLVE_DUPLICATE_SYMBOL",
format!(
"Duplicate type symbol '{}' in module '{}'",
interner.resolve(existing.name),
module_path
),
existing.span.clone(),
).into());
let ms = module_symbols_map
.entry(module_path.clone())
.or_insert_with(ModuleSymbols::new);
// Merge Type symbols (no overload): reject duplicates
for list in ts.symbols.into_values() {
for sym in list {
if let Some(existing) = ms.type_symbols.get(sym.name) {
return Err(DiagnosticBundle::error(
"E_RESOLVE_DUPLICATE_SYMBOL",
format!(
"Duplicate type symbol '{}' in module '{}'",
interner.resolve(existing.name),
module_path
),
existing.span.clone(),
)
.into());
}
let _ = ms.type_symbols.insert(sym);
}
let _ = ms.type_symbols.insert(sym);
}
for sym in vs.symbols.into_values() {
if let Some(existing) = ms.value_symbols.get(sym.name) {
return Err(DiagnosticBundle::error(
"E_RESOLVE_DUPLICATE_SYMBOL",
format!(
"Duplicate value symbol '{}' in module '{}'",
interner.resolve(existing.name),
module_path
),
existing.span.clone(),
).into());
// Merge Value symbols: allow overloads only for Function kind
for list in vs.symbols.into_values() {
for sym in list {
if let Some(existing) = ms.value_symbols.get(sym.name) {
if !(existing.kind == SymbolKind::Function && sym.kind == SymbolKind::Function) {
return Err(DiagnosticBundle::error(
"E_RESOLVE_DUPLICATE_SYMBOL",
format!(
"Duplicate value symbol '{}' in module '{}'",
interner.resolve(existing.name),
module_path
),
existing.span.clone(),
)
.into());
}
}
let _ = ms.value_symbols.insert(sym);
}
let _ = ms.value_symbols.insert(sym);
}
parsed_files.push((module_path, parsed));
}
// 2. Synthesize ModuleSymbols for dependencies
// 2) Synthesize visible ModuleSymbols for dependencies (under synthetic module paths)
let mut all_visible_modules = module_symbols_map.clone();
for (alias, project_id) in &step.deps {
if let Some(compiled) = dep_modules.get(project_id) {
for (key, meta) in &compiled.exports {
// Support syntax: "alias/module" and "@alias:module"
let key_module_path = &key.module_path;
let synthetic_paths = [
format!("{}/{}", alias, key_module_path),
format!("@{}:{}", alias, key_module_path),
];
for synthetic_module_path in synthetic_paths {
let ms = all_visible_modules.entry(synthetic_module_path.clone()).or_insert_with(ModuleSymbols::new);
let sym = Symbol {
name: interner.intern(&key.symbol_name),
kind: match key.kind {
ExportSurfaceKind::Service => SymbolKind::Service,
ExportSurfaceKind::DeclareType => {
match &meta.ty {
Some(PbsType::Contract(_)) => SymbolKind::Contract,
Some(PbsType::ErrorType(_)) => SymbolKind::ErrorType,
_ => SymbolKind::Struct,
}
}
ExportSurfaceKind::Function => SymbolKind::Function,
let Some(compiled) = dep_modules.get(project_id) else { continue };
for (key, meta) in &compiled.exports {
let key_module_path = &key.module_path;
// Support both import styles:
// - "alias/module"
// - "@alias:module"
let synthetic_paths = [
format!("{}/{}", alias, key_module_path),
format!("@{}:{}", alias, key_module_path),
];
for synthetic_module_path in synthetic_paths {
let ms = all_visible_modules
.entry(synthetic_module_path.clone())
.or_insert_with(ModuleSymbols::new);
let sym = Symbol {
name: interner.intern(&key.symbol_name),
kind: match key.kind {
ExportSurfaceKind::Service => SymbolKind::Service,
ExportSurfaceKind::DeclareType => match &meta.ty {
Some(PbsType::Contract(_)) => SymbolKind::Contract,
Some(PbsType::ErrorType(_)) => SymbolKind::ErrorType,
_ => SymbolKind::Struct,
},
namespace: key.kind.namespace(),
visibility: Visibility::Pub,
ty: meta.ty.clone(),
is_host: meta.is_host,
span: Span::new(FileId::INVALID, 0, 0),
origin: Some(synthetic_module_path.clone()),
};
if sym.namespace == Namespace::Type {
if let Some(existing) = ms.type_symbols.get(sym.name) {
return Err(CompileError::DuplicateExport {
symbol: interner.resolve(sym.name).to_string(),
first_dep: existing.origin.clone().unwrap_or_else(|| "unknown".to_string()),
second_dep: sym.origin.unwrap_or_else(|| "unknown".to_string()),
});
}
let _ = ms.type_symbols.insert(sym.clone());
} else {
if let Some(existing) = ms.value_symbols.get(sym.name) {
return Err(CompileError::DuplicateExport {
symbol: interner.resolve(sym.name).to_string(),
first_dep: existing.origin.clone().unwrap_or_else(|| "unknown".to_string()),
second_dep: sym.origin.unwrap_or_else(|| "unknown".to_string()),
});
}
let _ = ms.value_symbols.insert(sym.clone());
ExportSurfaceKind::Function => SymbolKind::Function,
},
namespace: key.kind.namespace(),
visibility: Visibility::Pub,
ty: meta.ty.clone(),
is_host: meta.is_host,
span: Span::new(FileId::INVALID, 0, 0),
origin: Some(synthetic_module_path.clone()),
};
if sym.namespace == Namespace::Type {
if let Some(existing) = ms.type_symbols.get(sym.name) {
return Err(CompileError::DuplicateExport {
symbol: interner.resolve(sym.name).to_string(),
first_dep: existing.origin.clone().unwrap_or_else(|| "unknown".to_string()),
second_dep: sym.origin.clone().unwrap_or_else(|| "unknown".to_string()),
});
}
let _ = ms.type_symbols.insert(sym);
} else {
if let Some(existing) = ms.value_symbols.get(sym.name) {
if !(existing.kind == SymbolKind::Function && sym.kind == SymbolKind::Function) {
return Err(CompileError::DuplicateExport {
symbol: interner.resolve(sym.name).to_string(),
first_dep: existing.origin.clone().unwrap_or_else(|| "unknown".to_string()),
second_dep: sym.origin.clone().unwrap_or_else(|| "unknown".to_string()),
});
}
}
let _ = ms.value_symbols.insert(sym);
}
}
}
}
// 3. Resolve and TypeCheck each file
let module_provider = ProjectModuleProvider {
// 3) Resolve imports and type each file; keep imported_symbols per module_path for Lowerer
let module_provider = ProjectModuleProvider {
modules: all_visible_modules,
};
// We need to collect imported symbols for Lowerer
let mut file_imported_symbols: HashMap<String, ModuleSymbols> = HashMap::new(); // keyed by module_path
let mut file_imported_symbols: HashMap<String, ModuleSymbols> = HashMap::new();
// Ensure primitive names are interned early (resolver/typecheck may depend on NameIds)
{
let primitives = ["int", "bool", "float", "string", "bounded", "void", "error", "result"];
for p in primitives {
interner.intern(p);
}
}
for (module_path, parsed) in &parsed_files {
let ms = module_symbols_map.get(module_path).unwrap();
// Ensure primitive names are interned before creating resolver/bootstrap
{
let primitives = ["int", "bool", "float", "string", "bounded", "void"];
for p in primitives { interner.intern(p); }
}
let ms = module_symbols_map
.get(module_path)
.ok_or_else(|| CompileError::Internal(format!("Missing module_symbols for '{}'", module_path)))?;
let mut resolver = Resolver::new(ms, &module_provider, &interner);
resolver.bootstrap_types(&interner);
resolver.resolve(&parsed.arena, parsed.root)?;
// Capture imported symbols
file_imported_symbols.insert(module_path.clone(), resolver.imported_symbols.clone());
// TypeChecker also needs &mut ModuleSymbols
let mut ms_mut = module_symbols_map.get_mut(module_path).unwrap();
let imported = file_imported_symbols.get(module_path).unwrap();
let mut typechecker = TypeChecker::new(&mut ms_mut, imported, &module_provider, &interner);
typechecker.check(&parsed.arena, parsed.root)?;
// Unified typed-symbol builder: typecheck the already-collected module symbols in place
let imported = file_imported_symbols
.get(module_path)
.ok_or_else(|| CompileError::Internal(format!("Missing imported_symbols for '{}'", module_path)))?;
let ms_mut = module_symbols_map
.get_mut(module_path)
.ok_or_else(|| CompileError::Internal(format!("Missing module_symbols (mut) for '{}'", module_path)))?;
build_typed_module_symbols(parsed, ms_mut, imported, &mut interner)?;
}
// 4. Lower ALL modules to VM and emit a single combined bytecode image for this project
// Rationale: services and functions can live in multiple modules; exports must refer to
// correct function indices within this CompiledModule. We aggregate all VM functions
// into a single ir_vm::Module and assemble once using the public API `emit_fragments`.
// Combined VM module (we will merge const pools and remap ConstIds accordingly)
// 4) Lower ALL sources into a single combined VM module (so exports func_idx match final image)
let mut combined_vm = crate::ir_vm::Module::new(step.project_key.name.clone());
combined_vm.const_pool = crate::ir_core::ConstPool::new();
// Track origin module_path for each function we append to combined_vm
// Origin module_path per appended function
let mut combined_func_origins: Vec<String> = Vec::new();
// Helper to insert a constant value into combined pool and return its new id
let mut insert_const = |pool: &mut crate::ir_core::ConstPool, val: &crate::ir_core::ConstantValue| -> crate::ir_vm::types::ConstId {
let new_id = pool.insert(val.clone());
crate::ir_vm::types::ConstId(new_id.0)
};
let insert_const =
|pool: &mut crate::ir_core::ConstPool, val: &crate::ir_core::ConstantValue| -> crate::ir_vm::types::ConstId {
let new_id = pool.insert(val.clone());
crate::ir_vm::types::ConstId(new_id.0)
};
// Accumulate VM functions from each source file, remapping ConstIds as we go
for (module_path, parsed) in &parsed_files {
let ms = module_symbols_map.get(module_path).unwrap();
let imported = file_imported_symbols.get(module_path).unwrap();
let lowerer = Lowerer::new(&parsed.arena, ms, imported, &interner);
let lowerer = Lowerer::new(&parsed.arena, ms, imported, &module_provider, &interner);
let program = lowerer.lower_file(parsed.root, module_path)?;
let vm_module = core_to_vm::lower_program(&program)
.map_err(|e| CompileError::Internal(format!("Lowering error ({}): {}", module_path, e)))?;
// Build remap for this module's const ids
// Remap this module's const pool into the combined pool
let mut const_map: Vec<crate::ir_vm::types::ConstId> = Vec::with_capacity(vm_module.const_pool.constants.len());
for c in &vm_module.const_pool.constants {
let new_id = insert_const(&mut combined_vm.const_pool, c);
const_map.push(new_id);
const_map.push(insert_const(&mut combined_vm.const_pool, c));
}
// Clone functions and remap any PushConst const ids
// Append functions; remap PushConst ids safely
for mut f in vm_module.functions.into_iter() {
for instr in &mut f.body {
if let crate::ir_vm::instr::InstrKind::PushConst(old_id) = instr.kind {
// safest: clone the kind and rewrite if needed
let kind_clone = instr.kind.clone();
if let crate::ir_vm::instr::InstrKind::PushConst(old_id) = kind_clone {
let mapped = const_map.get(old_id.0 as usize).cloned().unwrap_or(old_id);
instr.kind = crate::ir_vm::instr::InstrKind::PushConst(mapped);
}
}
combined_func_origins.push(module_path.clone());
combined_vm.functions.push(f);
}
}
// Assemble once for the whole project using the public API
let fragments = emit_fragments(&combined_vm)
.map_err(|e| CompileError::Internal(format!("Emission error: {}", e)))?;
// Post-fix FunctionMeta slots from VM IR (some emitters may default to 0)
// Ensure function metas reflect final slots info
let mut fixed_function_metas = fragments.functions.clone();
for (i, fm) in fixed_function_metas.iter_mut().enumerate() {
if let Some(vm_func) = combined_vm.functions.get(i) {
@@ -330,87 +360,93 @@ pub fn compile_project(
}
}
// Note: Entry point validation for the root project is now performed at the orchestrator level,
// after compilation and before linking, using enriched debug info. We skip it here to avoid
// double validation and mismatches with name annotations.
// 5. Collect exports
// 5) Collect exports
let mut exports = BTreeMap::new();
for (module_path, ms) in &module_symbols_map {
for sym in ms.type_symbols.symbols.values() {
if sym.visibility == Visibility::Pub {
if let Some(surface_kind) = ExportSurfaceKind::from_symbol_kind(sym.kind) {
exports.insert(ExportKey {
// Type exports (simple name)
for list in ms.type_symbols.symbols.values() {
for sym in list {
if sym.visibility != Visibility::Pub {
continue;
}
let Some(surface_kind) = ExportSurfaceKind::from_symbol_kind(sym.kind) else {
continue;
};
exports.insert(
ExportKey {
module_path: module_path.clone(),
symbol_name: interner.resolve(sym.name).to_string(),
kind: surface_kind,
}, ExportMetadata {
},
ExportMetadata {
func_idx: None,
is_host: sym.is_host,
ty: sym.ty.clone(),
});
}
}
}
// Build a set of public function names declared in this module (value namespace)
let mut pub_fn_names: std::collections::HashSet<String> = std::collections::HashSet::new();
for sym in ms.value_symbols.symbols.values() {
if sym.visibility == Visibility::Pub {
if let Some(surface_kind) = ExportSurfaceKind::from_symbol_kind(sym.kind) {
if matches!(surface_kind, ExportSurfaceKind::Function) {
pub_fn_names.insert(interner.resolve(sym.name).to_string());
}
}
},
);
}
}
for sym in ms.value_symbols.symbols.values() {
if sym.visibility == Visibility::Pub {
if let Some(surface_kind) = ExportSurfaceKind::from_symbol_kind(sym.kind) {
// Encontrar TODAS as funções no módulo VM combinado originadas deste module_path
// cujo nome seja igual ao do símbolo público; para cada uma, exportar
// tanto o nome simples quanto o alias com aridade.
let name_simple = interner.resolve(sym.name).to_string();
let mut any_found = false;
for (i, f) in combined_vm.functions.iter().enumerate() {
if combined_func_origins.get(i).map(|s| s.as_str()) != Some(module_path.as_str()) { continue; }
if f.name != name_simple { continue; }
any_found = true;
let meta = ExportMetadata {
// Value exports: only by canonical signature "name#sigN"
for list in ms.value_symbols.symbols.values() {
for sym in list {
if sym.visibility != Visibility::Pub {
continue;
}
let Some(surface_kind) = ExportSurfaceKind::from_symbol_kind(sym.kind) else {
continue;
};
let name_simple = interner.resolve(sym.name).to_string();
// For service methods, VM function name is "Service.method"
let expected_vm_name = if let Some(origin) = &sym.origin {
if let Some(svc) = origin.strip_prefix("svc:") {
format!("{}.{}", svc, name_simple)
} else {
name_simple.clone()
}
} else {
name_simple.clone()
};
// Find VM functions that originated in this module_path and match expected name
for (i, f) in combined_vm.functions.iter().enumerate() {
if combined_func_origins.get(i).map(|s| s.as_str()) != Some(module_path.as_str()) {
continue;
}
if f.name != expected_vm_name {
continue;
}
let sig_name = format!("{}#sig{}", name_simple, f.sig.0);
let ty = sym.ty.clone().ok_or_else(|| {
CompileError::Internal(format!(
"Missing type for exported symbol '{}' in module '{}'",
name_simple, module_path
))
})?;
exports.insert(
ExportKey {
module_path: module_path.clone(),
symbol_name: sig_name,
kind: surface_kind,
},
ExportMetadata {
func_idx: Some(i as u32),
is_host: sym.is_host,
ty: sym.ty.clone(),
};
// Simple name
exports.insert(ExportKey {
module_path: module_path.clone(),
symbol_name: name_simple.clone(),
kind: surface_kind,
}, meta.clone());
// name/arity
let arity = f.params.len();
let export_name_arity = format!("{}/{}", name_simple, arity);
exports.insert(ExportKey {
module_path: module_path.clone(),
symbol_name: export_name_arity,
kind: surface_kind,
}, meta);
}
// Caso nada tenha sido encontrado no VM (ex.: métodos ainda não materializados),
// publique ao menos o nome simples sem func_idx (mantém compatibilidade de surface)
if !any_found {
exports.insert(ExportKey {
module_path: module_path.clone(),
symbol_name: name_simple.clone(),
kind: surface_kind,
}, ExportMetadata { func_idx: None, is_host: sym.is_host, ty: sym.ty.clone() });
}
ty: Some(ty),
},
);
}
}
}
}
// 6. Collect symbols
// 6) Collect symbols for analysis (LSP, etc.)
let project_symbols = crate::common::symbols::collect_symbols(
&step.project_key.name,
&module_symbols_map,
@@ -418,52 +454,57 @@ pub fn compile_project(
&interner,
);
// 6.b) Enriquecer debug_info com metadados de função (offset/len) para análise externa
let mut dbg = fragments.debug_info.clone().unwrap_or_default();
// Adiciona pares (func_idx, (code_offset, code_len)) ao campo function_names como anotações extras
// Sem quebrar o formato, usamos o name como "name@offset+len" para tooling/analysis.json
let mut enriched_function_names = Vec::new();
for (i, (fid, name)) in fragments
.debug_info
.as_ref()
.map(|d| d.function_names.clone())
.unwrap_or_default()
.into_iter()
.enumerate()
{
let meta = &fixed_function_metas[i];
let annotated = format!("{}@{}+{}", name, meta.code_offset, meta.code_len);
enriched_function_names.push((fid, annotated));
}
if !enriched_function_names.is_empty() {
dbg.function_names = enriched_function_names;
}
// 7. Collect imports from unresolved labels
let mut imports = Vec::new();
for (label, pcs) in fragments.unresolved_labels {
if label.starts_with('@') {
// Format: @dep_alias::module_path:symbol_name
let parts: Vec<&str> = label[1..].splitn(2, "::").collect();
if parts.len() == 2 {
let dep_alias = parts[0].to_string();
let rest = parts[1];
let sub_parts: Vec<&str> = rest.rsplitn(2, ':').collect();
if sub_parts.len() == 2 {
let symbol_name = sub_parts[0].to_string();
let module_path = sub_parts[1].to_string();
imports.push(ImportMetadata {
key: ImportKey {
dep_alias,
module_path,
symbol_name,
},
relocation_pcs: pcs,
});
}
// 6.b) Enrich debug_info (only if present). Avoid requiring Default on DebugInfo.
let mut debug_info = fragments.debug_info.clone();
if let Some(dbg) = debug_info.as_mut() {
// annotate function names with "@offset+len"
// NOTE: assumes dbg.function_names aligns with functions order.
let mut enriched = Vec::new();
for (i, (fid, name)) in dbg.function_names.clone().into_iter().enumerate() {
if let Some(meta) = fixed_function_metas.get(i) {
enriched.push((fid, format!("{}@{}+{}", name, meta.code_offset, meta.code_len)));
} else {
enriched.push((fid, name));
}
}
if !enriched.is_empty() {
dbg.function_names = enriched;
}
}
// 7) Collect imports from unresolved labels
let mut imports = Vec::new();
for (label, pcs) in fragments.unresolved_labels {
if !label.starts_with('@') {
continue;
}
// Format: @dep_alias::module_path:symbol_name
let parts: Vec<&str> = label[1..].splitn(2, "::").collect();
if parts.len() != 2 {
continue;
}
let dep_alias = parts[0].to_string();
let rest = parts[1];
// Split from the right once: "...:<symbol_name>"
let sub_parts: Vec<&str> = rest.rsplitn(2, ':').collect();
if sub_parts.len() != 2 {
continue;
}
let symbol_name = sub_parts[0].to_string();
let module_path = sub_parts[1].to_string();
imports.push(ImportMetadata {
key: ImportKey {
dep_alias,
module_path,
symbol_name,
},
relocation_pcs: pcs,
});
}
Ok(CompiledModule {
@@ -475,7 +516,7 @@ pub fn compile_project(
const_pool: fragments.const_pool,
code: fragments.code,
function_metas: fixed_function_metas,
debug_info: Some(dbg),
debug_info,
symbols: project_symbols,
})
}
@@ -491,25 +532,31 @@ mod tests {
fn test_compile_root_only_project() {
let dir = tempdir().unwrap();
let project_dir = dir.path().to_path_buf();
fs::create_dir_all(project_dir.join("src/main/modules")).unwrap();
// NOTE: ajuste de sintaxe: seu PBS entrypoint atual é `fn frame(): void` dentro de main.pbs
// e "mod fn frame" pode não ser válido. Mantive o essencial.
let main_code = r#"
pub declare struct Vec2(x: int, y: int)
fn add(a: int, b: int): int {
return a + b;
}
mod fn frame(): void {
fn frame(): void {
let x = add(1, 2);
}
"#;
fs::write(project_dir.join("src/main/modules/main.pbs"), main_code).unwrap();
let project_key = ProjectKey { name: "root".to_string(), version: "0.1.0".to_string() };
let project_key = ProjectKey {
name: "root".to_string(),
version: "0.1.0".to_string(),
};
let project_id = ProjectId(0);
let step = BuildStep {
project_id,
project_key: project_key.clone(),
@@ -518,13 +565,14 @@ mod tests {
sources: vec![PathBuf::from("src/main/modules/main.pbs")],
deps: BTreeMap::new(),
};
let mut file_manager = FileManager::new();
let compiled = compile_project(step, &HashMap::new(), &mut file_manager).expect("Failed to compile project");
let compiled =
compile_project(step, &HashMap::new(), &mut file_manager).expect("Failed to compile project");
assert_eq!(compiled.project_id, project_id);
assert_eq!(compiled.target, BuildTarget::Main);
// Vec2 should be exported
let vec2_key = ExportKey {
module_path: "".to_string(),
@@ -532,9 +580,5 @@ mod tests {
kind: ExportSurfaceKind::DeclareType,
};
assert!(compiled.exports.contains_key(&vec2_key));
// frame is NOT exported (top-level fn cannot be pub in v0)
// Wait, I put "pub fn frame" in the test code. SymbolCollector should have ignored pub.
// Actually, SymbolCollector might error on pub fn.
}
}