From 8969186467505297df889fc0aa94d72c89c687e9 Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Fri, 3 Oct 2025 10:43:20 -0500 Subject: [PATCH] Adding more name analysis gather. --- src/bin/dmc/name_analysis.rs | 17 ++--- src/name_analysis/gather.rs | 123 +++++++++++++++++++++++++++++++++-- src/name_analysis/mod.rs | 6 +- src/parser/ast.yaml | 3 + 4 files changed, 131 insertions(+), 18 deletions(-) diff --git a/src/bin/dmc/name_analysis.rs b/src/bin/dmc/name_analysis.rs index 3bbd993..b4c4e31 100644 --- a/src/bin/dmc/name_analysis.rs +++ b/src/bin/dmc/name_analysis.rs @@ -5,14 +5,14 @@ use deimos::ast::build::build_ast; use deimos::name_analysis::analyze_names; use deimos::name_analysis::symbol_table::SymbolTable; use deimos::parser::{DeimosParser, Rule}; +use deimos::std_core::add_std_core_symbols; use pest::Parser; use std::path::PathBuf; -use deimos::std_core::add_std_core_symbols; pub fn name_analysis(paths: &Vec) -> Result<(), Box> { let mut compilation_units = vec![]; - let mut files = SimpleFiles::new(); - + let mut files: SimpleFiles = SimpleFiles::new(); + for path in paths { let src = std::fs::read_to_string(path).unwrap(); let parse_result = DeimosParser::parse(Rule::CompilationUnit, &src); @@ -30,14 +30,11 @@ pub fn name_analysis(paths: &Vec) -> Result<(), Box) -> Result<(), Box) { +fn gather_concrete_use_symbol( + base_fqn: &str, + identifier: &Identifier, + symbol_table: &mut SymbolTable, + diagnostics: &mut Vec, +) { let symbol = ConcreteUseSymbol::new( base_fqn, identifier.name(), @@ -106,6 +116,76 @@ fn gather_use_statement( } } +fn gather_function( + function: &Function, + symbol_table: &mut SymbolTable, + diagnostics: &mut Vec, +) { + let function_symbol = FunctionSymbol::without_parameters_or_return_type( + "", + function.identifier().name(), + function.is_public(), + false, + Some(SourceDefinition::from_identifier(function.identifier())), + ); + if let Err(insert_error) = symbol_table.insert_function_symbol(function_symbol) { + handle_insert_error( + insert_error, + function.identifier().name(), + function.identifier().file_id(), + function.identifier().range(), + "Function", + diagnostics, + ); + } + gather_node( + AstNodeRef::FunctionBody(function.function_body()), + symbol_table, + diagnostics, + ); +} + +fn gather_function_block_body( + function_block_body: &FunctionBlockBody, + symbol_table: &mut SymbolTable, + diagnostics: &mut Vec, +) { + symbol_table.push_scope("FunctionBlockBody"); + gather_node_children(function_block_body, symbol_table, diagnostics); + symbol_table.pop_scope(); +} + +fn gather_variable_declaration( + variable_declaration: &VariableDeclaration, + symbol_table: &mut SymbolTable, + diagnostics: &mut Vec, +) { + let variable_symbol = VariableSymbol::new( + variable_declaration.identifier().name(), + variable_declaration.is_mut(), + Some(SourceDefinition::from_identifier( + variable_declaration.identifier(), + )), + ); + if let Err(insert_error) = symbol_table.insert_variable_symbol(variable_symbol) { + handle_insert_error( + insert_error, + variable_declaration.identifier().name(), + variable_declaration.identifier().file_id(), + variable_declaration.identifier().range(), + "Variable", + diagnostics, + ); + } + if let Some(expression) = variable_declaration.expression() { + gather_node( + AstNodeRef::Expression(expression), + symbol_table, + diagnostics, + ); + } +} + fn gather_node_children( node: &impl AstNode, symbol_table: &mut SymbolTable, @@ -151,27 +231,53 @@ fn gather_node( AstNodeRef::UseStatementPrefix(_) => {} AstNodeRef::UseStatementSuffix(_) => {} AstNodeRef::UseList(_) => {} - AstNodeRef::ModuleLevelDeclaration(_) => {} + AstNodeRef::ModuleLevelDeclaration(module_level_declaration) => { + gather_node_children(module_level_declaration, symbol_table, diagnostics); + } AstNodeRef::InterfaceLevelDeclaration(_) => {} AstNodeRef::ClassLevelDeclaration(_) => {} AstNodeRef::Module(_) => {} AstNodeRef::CompanionModule(_) => {} AstNodeRef::Interface(_) => {} AstNodeRef::Class(_) => {} - AstNodeRef::Function(_) => {} + AstNodeRef::Function(function) => { + gather_function(function, symbol_table, diagnostics); + } AstNodeRef::OperatorFunction(_) => {} AstNodeRef::PlatformFunction(_) => {} AstNodeRef::InterfaceFunction(_) => {} AstNodeRef::InterfaceDefaultFunction(_) => {} AstNodeRef::InterfaceOperatorFunction(_) => {} AstNodeRef::InterfaceDefaultOperatorFunction(_) => {} - AstNodeRef::FunctionBody(_) => {} + AstNodeRef::FunctionBody(function_body) => match function_body { + FunctionBody::FunctionAliasBody(alias_body) => { + gather_node(alias_body.as_node_ref(), symbol_table, diagnostics); + } + FunctionBody::FunctionEqualsBody(equals_body) => { + gather_node(equals_body.as_node_ref(), symbol_table, diagnostics); + } + FunctionBody::FunctionBlockBody(block_body) => { + gather_node(block_body.as_node_ref(), symbol_table, diagnostics); + } + }, AstNodeRef::FunctionEqualsBody(_) => {} AstNodeRef::FunctionAliasBody(_) => {} - AstNodeRef::FunctionBlockBody(_) => {} + AstNodeRef::FunctionBlockBody(block_body) => { + gather_function_block_body(block_body, symbol_table, diagnostics); + } AstNodeRef::ClassConstructor(_) => {} AstNodeRef::Member(_) => {} - AstNodeRef::Statement(_) => {} + AstNodeRef::Statement(statement) => match statement { + Statement::VariableDeclaration(variable_declaration) => { + gather_variable_declaration(variable_declaration, symbol_table, diagnostics); + } + Statement::AssignmentStatement(_) => {} + Statement::ExpressionStatement(_) => {} + Statement::UseStatement(_) => {} + Statement::IfStatement(_) => {} + Statement::WhileStatement(_) => {} + Statement::ForStatement(_) => {} + }, AstNodeRef::VariableDeclaration(_) => {} AstNodeRef::AssignmentStatement(_) => {} AstNodeRef::ExpressionStatement(_) => {} @@ -224,9 +330,12 @@ fn gather_node( pub fn gather_compilation_unit( compilation_unit: &mut CompilationUnit, + file_name: &str, symbol_table: &mut SymbolTable, identifier_scope_ids: &mut HashMap, diagnostics: &mut Vec, ) { + symbol_table.push_scope(&format!("FileScope {}", file_name)); gather_node(compilation_unit.as_node_ref(), symbol_table, diagnostics); + symbol_table.pop_scope(); } diff --git a/src/name_analysis/mod.rs b/src/name_analysis/mod.rs index 3c89e3c..3a951a1 100644 --- a/src/name_analysis/mod.rs +++ b/src/name_analysis/mod.rs @@ -24,6 +24,7 @@ use crate::diagnostic::DmDiagnostic; use crate::name_analysis::gather::gather_compilation_unit; // use crate::name_analysis::resolve::resolve_compilation_unit; use crate::name_analysis::symbol_table::SymbolTable; +use codespan_reporting::files::Files; use std::collections::HashMap; mod fqn_context; @@ -32,8 +33,9 @@ mod gather; pub mod symbol; pub mod symbol_table; -pub fn analyze_names( +pub fn analyze_names<'a, F: Files<'a, FileId = usize, Name = String>>( compilation_units: &mut [Box], + files: &'a F, symbol_table: &mut SymbolTable, ) -> Vec { let mut diagnostics = vec![]; @@ -41,8 +43,10 @@ pub fn analyze_names( // gather symbols for compilation_unit in compilation_units.iter_mut() { + let file_name = files.name(compilation_unit.file_id()).unwrap(); gather_compilation_unit( compilation_unit, + &file_name, symbol_table, &mut identifier_scope_ids, &mut diagnostics, diff --git a/src/parser/ast.yaml b/src/parser/ast.yaml index d4c4db2..202edcf 100644 --- a/src/parser/ast.yaml +++ b/src/parser/ast.yaml @@ -193,6 +193,9 @@ CompilationUnit: - eoi: skip: rule: EOI + - file_id: + special: + kind: file_id ParentMod: struct: children: