This commit is contained in:
2026-03-24 13:40:27 +00:00
parent aea8f3bc08
commit adb4dad14b
16 changed files with 414 additions and 670 deletions
@@ -1,13 +1,11 @@
use crate::common::diagnostics::{Diagnostic, Severity};
use crate::common::spans::Span;
use crate::frontends::pbs::ast::{AstArena, NodeId};
use prometeu_analysis::NameId;
use crate::frontends::pbs::ast::AstArena;
use prometeu_analysis::NodeId;
use prometeu_analysis::{NameId, SymbolId, ModuleId};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct SymbolId(pub u32);
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum SymbolKind {
Type,
@@ -32,7 +30,7 @@ pub struct Symbol {
pub name: NameId,
pub kind: SymbolKind,
pub exported: bool,
pub module: u32,
pub module: ModuleId,
pub decl_span: Span,
}
@@ -43,7 +41,7 @@ pub struct SymbolArena {
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct DefKey {
pub module: u32,
pub module: ModuleId,
pub name: NameId,
pub namespace: Namespace,
}
@@ -107,7 +105,7 @@ impl DefIndex {
}
/// Lookup by name/namespace ignoring module. Returns the first match with its module id.
pub fn get_by_name_any_module(&self, name: NameId, namespace: Namespace) -> Option<(u32, SymbolId)> {
pub fn get_by_name_any_module(&self, name: NameId, namespace: Namespace) -> Option<(ModuleId, SymbolId)> {
for (k, v) in &self.symbols {
if k.name == name && k.namespace == namespace {
return Some((k.module, *v));
@@ -173,7 +171,7 @@ impl NodeToSymbol {
mod tests {
use super::*;
fn sample_symbol(name: NameId, module: u32) -> Symbol {
fn sample_symbol(name: NameId, module: ModuleId) -> Symbol {
Symbol {
name,
kind: SymbolKind::Function,
@@ -186,8 +184,8 @@ mod tests {
#[test]
fn insert_returns_incremental_ids() {
let mut arena = SymbolArena::new();
let id0 = arena.insert(sample_symbol(NameId(0), 0));
let id1 = arena.insert(sample_symbol(NameId(1), 0));
let id0 = arena.insert(sample_symbol(NameId(0), ModuleId(0)));
let id1 = arena.insert(sample_symbol(NameId(1), ModuleId(0)));
assert_eq!(id0, SymbolId(0));
assert_eq!(id1, SymbolId(1));
@@ -196,7 +194,7 @@ mod tests {
#[test]
fn get_returns_correct_symbol() {
let mut arena = SymbolArena::new();
let symbol = sample_symbol(NameId(7), 3);
let symbol = sample_symbol(NameId(7), ModuleId(3));
let id = arena.insert(symbol.clone());
assert_eq!(arena.get(id).name, symbol.name);
@@ -209,7 +207,7 @@ mod tests {
fn def_index_duplicate_in_same_namespace_errors() {
let mut index = DefIndex::new();
let key = DefKey {
module: 1,
module: ModuleId(1),
name: NameId(10),
namespace: Namespace::Type,
};
@@ -225,12 +223,12 @@ mod tests {
let mut index = DefIndex::new();
let name = NameId(11);
let type_key = DefKey {
module: 2,
module: ModuleId(2),
name,
namespace: Namespace::Type,
};
let value_key = DefKey {
module: 2,
module: ModuleId(2),
name,
namespace: Namespace::Value,
};
@@ -1,10 +1,8 @@
use crate::analysis::symbols::{SymbolArena, SymbolId};
use crate::frontends::pbs::ast::NodeId;
use prometeu_analysis::interner::{NameId, NameInterner};
use crate::analysis::symbols::{SymbolArena};
use prometeu_analysis::{NameId, NameInterner, TypeId, SymbolId, NodeId, ModuleId};
use serde::{Deserialize, Serialize};
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct TypeId(pub u32);
// Use canonical TypeId from prometeu-analysis
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TypeKind {
@@ -212,7 +210,7 @@ mod tests {
name: my_struct_name,
kind: SymbolKind::Struct,
exported: false,
module: 0,
module: ModuleId(0),
decl_span: Span::new(0, 0, 0),
});
@@ -1,9 +1,8 @@
use crate::common::spans::Span;
use prometeu_analysis::NameId;
use prometeu_analysis::{NameId, NodeId};
use serde::{Deserialize, Serialize};
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct NodeId(pub u32);
// Use canonical NodeId from prometeu-analysis
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct AstArena {
@@ -2,7 +2,7 @@ use crate::common::diagnostics::{Diagnostic, DiagnosticBundle, Severity};
use crate::frontends::pbs::ast::*;
use crate::frontends::pbs::symbols::*;
use crate::semantics::export_surface::ExportSurfaceKind;
use prometeu_analysis::NameInterner;
use prometeu_analysis::{NameInterner, NodeId};
pub struct SymbolCollector<'a> {
interner: &'a NameInterner,
@@ -7,7 +7,7 @@ use crate::frontends::pbs::types::PbsType;
use crate::ir_core;
use crate::ir_core::ids::{FieldId, FunctionId, TypeId, ValueId};
use crate::ir_core::{Block, Function, Instr, InstrKind, Module, Param, Program, Terminator, Type};
use prometeu_analysis::NameInterner;
use prometeu_analysis::{NameInterner, NodeId};
use std::collections::HashMap;
#[derive(Clone)]
@@ -3,7 +3,7 @@ use crate::common::spans::Span;
use crate::frontends::pbs::ast::*;
use crate::frontends::pbs::lexer::Lexer;
use crate::frontends::pbs::token::{Token, TokenKind};
use prometeu_analysis::{NameId, NameInterner};
use prometeu_analysis::{NameId, NameInterner, NodeId};
pub struct Parser<'a> {
tokens: Vec<Token>,
@@ -1,11 +1,11 @@
use crate::common::diagnostics::{Diagnostic, Severity};
use crate::frontends::pbs::ast::{AstArena, NodeKind, NodeId};
use crate::frontends::pbs::ast::{AstArena, NodeKind};
use crate::analysis::symbols::{Symbol, SymbolArena, SymbolKind, DefIndex, DefKey, Namespace, NodeToSymbol, RefIndex};
use prometeu_analysis::NameInterner;
use prometeu_analysis::{NameInterner, NodeId, ModuleId};
pub fn build_def_index(
arena: &AstArena,
module: u32,
module: ModuleId,
_interner: &NameInterner,
imports: Option<(&SymbolArena, &DefIndex)>,
) -> (SymbolArena, DefIndex, RefIndex, NodeToSymbol, Vec<Diagnostic>) {
@@ -79,7 +79,7 @@ pub fn build_def_index(
fn walk_node(
node_id: NodeId,
arena: &AstArena,
module: u32,
module: ModuleId,
index: &DefIndex,
imports: Option<(&SymbolArena, &DefIndex)>,
ref_index: &mut RefIndex,
@@ -134,7 +134,7 @@ fn walk_node(
diagnostics.push(Diagnostic {
severity: Severity::Error,
code: "E_RESOLVE_VISIBILITY".to_string(),
message: format!("Symbol is not exported from module {}", symbol.module),
message: format!("Symbol is not exported from module {:?}", symbol.module),
span,
related: Vec::new(),
});
@@ -278,18 +278,18 @@ mod tests {
arena.roots.push(file_id);
let (symbols, index, _ref_index, _node_to_symbol, diagnostics) = build_def_index(&arena, 1, &interner, None);
let (symbols, index, _ref_index, _node_to_symbol, diagnostics) = build_def_index(&arena, ModuleId(1), &interner, None);
assert!(diagnostics.is_empty());
assert_eq!(symbols.symbols.len(), 2);
let fn_sym_id = index.get(DefKey { module: 1, name: fn_name, namespace: Namespace::Value }).unwrap();
let fn_sym_id = index.get(DefKey { module: ModuleId(1), name: fn_name, namespace: Namespace::Value }).unwrap();
let fn_sym = symbols.get(fn_sym_id);
assert_eq!(fn_sym.name, fn_name);
assert_eq!(fn_sym.kind, SymbolKind::Function);
assert!(fn_sym.exported);
let type_sym_id = index.get(DefKey { module: 1, name: type_name, namespace: Namespace::Type }).unwrap();
let type_sym_id = index.get(DefKey { module: ModuleId(1), name: type_name, namespace: Namespace::Type }).unwrap();
let type_sym = symbols.get(type_sym_id);
assert_eq!(type_sym.name, type_name);
assert_eq!(type_sym.kind, SymbolKind::Struct);
@@ -330,7 +330,7 @@ mod tests {
arena.roots.push(file_id);
let (_symbols, _index, _ref_index, _node_to_symbol, diagnostics) = build_def_index(&arena, 1, &interner, None);
let (_symbols, _index, _ref_index, _node_to_symbol, diagnostics) = build_def_index(&arena, ModuleId(1), &interner, None);
assert_eq!(diagnostics.len(), 1);
assert_eq!(diagnostics[0].code, "E_RESOLVE_DUPLICATE_SYMBOL");
@@ -360,7 +360,7 @@ mod tests {
arena.roots.push(file_id);
let (symbols, _index, _ref_index, node_to_symbol, _diagnostics) = build_def_index(&arena, 1, &interner, None);
let (symbols, _index, _ref_index, node_to_symbol, _diagnostics) = build_def_index(&arena, ModuleId(1), &interner, None);
let symbol_id = node_to_symbol.get(decl_id).expect("Node should be bound to a symbol");
let symbol = symbols.get(symbol_id);
@@ -393,7 +393,7 @@ mod tests {
arena.roots.push(file_id);
let (_symbols, _index, _ref_index, _node_to_symbol, diagnostics) = build_def_index(&arena, 1, &interner, None);
let (_symbols, _index, _ref_index, _node_to_symbol, diagnostics) = build_def_index(&arena, ModuleId(1), &interner, None);
assert_eq!(diagnostics.len(), 1);
assert_eq!(diagnostics[0].code, "E_RESOLVE_UNDEFINED_IDENTIFIER");
@@ -439,11 +439,11 @@ mod tests {
arena.roots.push(file_id);
let (_symbols, index, ref_index, node_to_symbol, diagnostics) = build_def_index(&arena, 1, &interner, None);
let (_symbols, index, ref_index, node_to_symbol, diagnostics) = build_def_index(&arena, ModuleId(1), &interner, None);
assert!(diagnostics.is_empty(), "Diagnostics should be empty: {:?}", diagnostics);
let target_sym_id = index.get(DefKey { module: 1, name: target_name, namespace: Namespace::Value }).expect("target should be in index");
let target_sym_id = index.get(DefKey { module: ModuleId(1), name: target_name, namespace: Namespace::Value }).expect("target should be in index");
let refs = ref_index.refs_of(target_sym_id);
assert_eq!(refs.len(), 1);
assert_eq!(refs[0], Span::new(0, 50, 56));
@@ -474,7 +474,7 @@ mod tests {
}), Span::new(0, 0, 100));
arena1.roots.push(file1);
let (symbols1, index1, _, _, _) = build_def_index(&arena1, 1, &interner, None);
let (symbols1, index1, _, _, _) = build_def_index(&arena1, ModuleId(1), &interner, None);
// Módulo 2: tenta usar função privada do Módulo 1
let mut arena2 = AstArena::default();
@@ -496,7 +496,7 @@ mod tests {
arena2.roots.push(file2);
let (_symbols2, _index2, _ref_index2, _node_to_symbol2, diagnostics) =
build_def_index(&arena2, 2, &interner, Some((&symbols1, &index1)));
build_def_index(&arena2, ModuleId(2), &interner, Some((&symbols1, &index1)));
assert_eq!(diagnostics.len(), 1);
assert_eq!(diagnostics[0].code, "E_RESOLVE_VISIBILITY");
@@ -541,8 +541,8 @@ mod tests {
arena.roots.push(file_id);
let run1 = build_def_index(&arena, 1, &interner, None);
let run2 = build_def_index(&arena, 1, &interner, None);
let run1 = build_def_index(&arena, ModuleId(1), &interner, None);
let run2 = build_def_index(&arena, ModuleId(1), &interner, None);
// runX is (SymbolArena, DefIndex, RefIndex, NodeToSymbol, Vec<Diagnostic>)
@@ -1,10 +1,10 @@
use crate::analysis::symbols::{SymbolArena, SymbolId, NodeToSymbol};
use crate::analysis::types::{TypeArena, TypeFacts, TypeId, TypeKind};
use crate::analysis::symbols::{SymbolArena, NodeToSymbol};
use crate::analysis::types::{TypeArena, TypeFacts, TypeKind};
use crate::common::diagnostics::{Diagnostic, DiagnosticBundle, Severity};
use crate::common::spans::Span;
use crate::frontends::pbs::ast::*;
use crate::frontends::pbs::symbols::*;
use prometeu_analysis::{NameId, NameInterner};
use prometeu_analysis::{NameId, NameInterner, SymbolId, ModuleId, TypeId, NodeId};
use std::collections::HashMap;
pub trait ModuleProvider {
@@ -80,7 +80,7 @@ impl<'a> Resolver<'a> {
SymbolKind::Local => crate::analysis::symbols::SymbolKind::Local,
},
exported: sym.visibility == Visibility::Pub,
module: 0,
module: ModuleId(0),
decl_span: sym.span,
});
}
@@ -96,7 +96,7 @@ impl<'a> Resolver<'a> {
SymbolKind::Local => crate::analysis::symbols::SymbolKind::Local,
},
exported: sym.visibility == Visibility::Pub,
module: 0,
module: ModuleId(0),
decl_span: sym.span,
});
}
@@ -121,7 +121,7 @@ impl<'a> Resolver<'a> {
SymbolKind::Local => crate::analysis::symbols::SymbolKind::Local,
},
exported: sym.visibility == Visibility::Pub,
module: 0, // Should be target module
module: ModuleId(0), // Should be target module
decl_span: sym.span,
});
}
@@ -137,7 +137,7 @@ impl<'a> Resolver<'a> {
SymbolKind::Local => crate::analysis::symbols::SymbolKind::Local,
},
exported: sym.visibility == Visibility::Pub,
module: 0, // Should be target module
module: ModuleId(0), // Should be target module
decl_span: sym.span,
});
}
@@ -838,7 +838,7 @@ impl<'a> Resolver<'a> {
_ => crate::analysis::symbols::SymbolKind::Value,
},
exported: false,
module: 0, // TODO
module: ModuleId(0), // TODO: set actual module id when available
decl_span: span,
});
@@ -5,7 +5,7 @@ use crate::frontends::pbs::contracts::ContractRegistry;
use crate::frontends::pbs::resolver::ModuleProvider;
use crate::frontends::pbs::symbols::*;
use crate::frontends::pbs::types::PbsType;
use prometeu_analysis::{NameId, NameInterner};
use prometeu_analysis::{NameId, NameInterner, NodeId};
use std::collections::HashMap;
pub struct TypeChecker<'a> {