WIP seemingly endless name analysis refactoring.
This commit is contained in:
parent
93c6a71185
commit
8b374e1066
@ -1,12 +1,20 @@
|
||||
use crate::ast::node::{
|
||||
CompilationUnit, ConcreteUseStatement, ConcreteUseStatementSuffix, Identifier, UseStatement,
|
||||
CompilationUnit, ConcreteUseStatement, ConcreteUseStatementSuffix, Function, FunctionBody,
|
||||
Identifier, Module, ModuleLevelDeclaration, Parameters, ReturnType, TypeUse, UseStatement,
|
||||
UseStatementIdentifier, UseStatementPrefix,
|
||||
};
|
||||
use crate::diagnostic::DmDiagnostic;
|
||||
use crate::name_analysis::symbol::function_symbol::FunctionSymbol;
|
||||
use crate::name_analysis::symbol::module_level_symbol::ModuleLevelSymbol;
|
||||
use crate::name_analysis::symbol::module_symbol::ModuleSymbol;
|
||||
use crate::name_analysis::symbol::parameter_symbol::ParameterSymbol;
|
||||
use crate::name_analysis::symbol::source_definition::SourceDefinition;
|
||||
use crate::name_analysis::symbol::use_symbol::ConcreteUseSymbol;
|
||||
use crate::name_analysis::symbol_table::SymbolTable;
|
||||
use crate::name_analysis::type_use_container::TypeUseContainer;
|
||||
use crate::name_analysis::util::handle_insert_error;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub fn na_p1_compilation_unit(
|
||||
file_name: &str,
|
||||
@ -30,6 +38,10 @@ pub fn na_p1_compilation_unit(
|
||||
na_p1_use_statement(use_statement, symbol_table, diagnostics);
|
||||
}
|
||||
|
||||
for module_level_declaration in compilation_unit.module_level_declarations_mut() {
|
||||
na_p1_module_level_declaration(module_level_declaration, symbol_table, diagnostics);
|
||||
}
|
||||
|
||||
symbol_table.pop_scope();
|
||||
}
|
||||
|
||||
@ -53,12 +65,12 @@ fn na_p1_concrete_use_statement(
|
||||
symbol_table: &mut SymbolTable,
|
||||
diagnostics: &mut Vec<DmDiagnostic>,
|
||||
) {
|
||||
let prefixes: Vec<String> = concrete_use_statement
|
||||
let prefixes: Vec<Rc<str>> = concrete_use_statement
|
||||
.prefixes()
|
||||
.map(UseStatementPrefix::identifier)
|
||||
.map(Identifier::name)
|
||||
.map(ToString::to_string)
|
||||
.collect::<Vec<_>>();
|
||||
.map(Rc::from)
|
||||
.collect();
|
||||
|
||||
match concrete_use_statement.suffix_mut() {
|
||||
ConcreteUseStatementSuffix::UseStatementIdentifier(use_statement_identifier) => {
|
||||
@ -83,16 +95,16 @@ fn na_p1_concrete_use_statement(
|
||||
}
|
||||
|
||||
fn handle_concrete_use_statement_identifier(
|
||||
prefixes: &Vec<String>,
|
||||
prefixes: &Vec<Rc<str>>,
|
||||
use_statement_identifier: &mut UseStatementIdentifier,
|
||||
symbol_table: &mut SymbolTable,
|
||||
diagnostics: &mut Vec<DmDiagnostic>,
|
||||
) {
|
||||
let mut fqn_parts: Vec<&str> = vec![];
|
||||
let mut fqn_parts: Vec<Rc<str>> = vec![];
|
||||
for prefix in prefixes {
|
||||
fqn_parts.push(prefix);
|
||||
fqn_parts.push(prefix.clone());
|
||||
}
|
||||
fqn_parts.push(use_statement_identifier.identifier().name());
|
||||
fqn_parts.push(Rc::from(use_statement_identifier.identifier().name()));
|
||||
|
||||
let to_insert = ConcreteUseSymbol::new(
|
||||
&fqn_parts,
|
||||
@ -111,9 +123,168 @@ fn handle_concrete_use_statement_identifier(
|
||||
use_statement_identifier.identifier().name(),
|
||||
use_statement_identifier.identifier().file_id(),
|
||||
use_statement_identifier.identifier().range(),
|
||||
"Use Symbol",
|
||||
diagnostics,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn na_p1_module_level_declaration(
|
||||
module_level_declaration: &mut ModuleLevelDeclaration,
|
||||
symbol_table: &mut SymbolTable,
|
||||
diagnostics: &mut Vec<DmDiagnostic>,
|
||||
) -> Option<ModuleLevelSymbol> {
|
||||
match module_level_declaration {
|
||||
ModuleLevelDeclaration::Module(module) => na_p1_module(module, symbol_table, diagnostics)
|
||||
.map(|module_symbol| ModuleLevelSymbol::Module(module_symbol)),
|
||||
ModuleLevelDeclaration::Interface(interface) => {
|
||||
todo!()
|
||||
}
|
||||
ModuleLevelDeclaration::Class(class) => {
|
||||
todo!()
|
||||
}
|
||||
ModuleLevelDeclaration::Function(function) => {
|
||||
na_p1_function(function, symbol_table, diagnostics)
|
||||
.map(|function_symbol| ModuleLevelSymbol::Function(function_symbol))
|
||||
}
|
||||
ModuleLevelDeclaration::PlatformFunction(platform_function) => {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn na_p1_module(
|
||||
module: &mut Module,
|
||||
symbol_table: &mut SymbolTable,
|
||||
diagnostics: &mut Vec<DmDiagnostic>,
|
||||
) -> Option<Rc<RefCell<ModuleSymbol>>> {
|
||||
symbol_table.push_fqn_part(module.identifier().name());
|
||||
symbol_table.push_scope(&format!("ModuleScope {}", module.identifier().name()));
|
||||
|
||||
let to_insert = ModuleSymbol::new(
|
||||
symbol_table.current_fqn_owned(),
|
||||
module.is_public(),
|
||||
Some(SourceDefinition::from_identifier(module.identifier())),
|
||||
);
|
||||
let module_symbol = match symbol_table.insert_module_symbol(to_insert) {
|
||||
Ok(module_symbol) => {
|
||||
for declaration in module.declarations_mut() {
|
||||
let declaration_result =
|
||||
na_p1_module_level_declaration(declaration, symbol_table, diagnostics);
|
||||
if let Some(module_level_symbol) = declaration_result {
|
||||
module_symbol
|
||||
.borrow_mut()
|
||||
.add_inner_symbol(module_level_symbol);
|
||||
}
|
||||
}
|
||||
Some(module_symbol)
|
||||
}
|
||||
Err(symbol_insert_error) => {
|
||||
handle_insert_error(
|
||||
symbol_insert_error,
|
||||
module.identifier().name(),
|
||||
module.identifier().file_id(),
|
||||
module.identifier().range(),
|
||||
diagnostics,
|
||||
);
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
symbol_table.pop_scope();
|
||||
symbol_table.pop_fqn_part();
|
||||
|
||||
module_symbol
|
||||
}
|
||||
|
||||
fn na_p1_function(
|
||||
function: &mut Function,
|
||||
symbol_table: &mut SymbolTable,
|
||||
diagnostics: &mut Vec<DmDiagnostic>,
|
||||
) -> Option<Rc<RefCell<FunctionSymbol>>> {
|
||||
symbol_table.push_scope(&format!("FunctionScope {}", function.identifier().name()));
|
||||
|
||||
let to_insert = FunctionSymbol::new(
|
||||
&symbol_table.resolve_fqn(Rc::from(function.identifier().name())),
|
||||
function.is_public(),
|
||||
false,
|
||||
Some(SourceDefinition::from_identifier(function.identifier())),
|
||||
);
|
||||
let function_symbol = match symbol_table.insert_function_symbol(to_insert) {
|
||||
Ok(function_symbol) => {
|
||||
{
|
||||
let mut as_ref_mut = function_symbol.borrow_mut();
|
||||
as_ref_mut.set_parameter_symbols(na_p1_parameters(
|
||||
function.parameters_mut(),
|
||||
symbol_table,
|
||||
diagnostics,
|
||||
));
|
||||
as_ref_mut.set_return_type(na_p1_return_type(
|
||||
function.return_type_mut(),
|
||||
symbol_table,
|
||||
diagnostics,
|
||||
))
|
||||
}
|
||||
Some(function_symbol)
|
||||
}
|
||||
Err(symbol_insert_error) => {
|
||||
handle_insert_error(
|
||||
symbol_insert_error,
|
||||
function.identifier().name(),
|
||||
function.identifier().file_id(),
|
||||
function.identifier().range(),
|
||||
diagnostics,
|
||||
);
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
symbol_table.pop_scope();
|
||||
|
||||
function_symbol
|
||||
}
|
||||
|
||||
fn na_p1_parameters(
|
||||
parameters: &mut Parameters,
|
||||
symbol_table: &mut SymbolTable,
|
||||
diagnostics: &mut Vec<DmDiagnostic>,
|
||||
) -> Vec<Rc<RefCell<ParameterSymbol>>> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn na_p1_return_type(
|
||||
return_type: &mut ReturnType,
|
||||
symbol_table: &mut SymbolTable,
|
||||
diagnostics: &mut Vec<DmDiagnostic>,
|
||||
) -> Rc<RefCell<TypeUseContainer>> {
|
||||
na_p1_type_use(return_type.type_use_mut(), symbol_table, diagnostics)
|
||||
}
|
||||
|
||||
fn na_p1_type_use(
|
||||
type_use: &mut TypeUse,
|
||||
symbol_table: &mut SymbolTable,
|
||||
diagnostics: &mut Vec<DmDiagnostic>,
|
||||
) -> Rc<RefCell<TypeUseContainer>> {
|
||||
match type_use {
|
||||
TypeUse::PrimitiveType(primitive_type) => {
|
||||
todo!()
|
||||
}
|
||||
TypeUse::InterfaceOrClassTypeUse(interface_or_class_type) => {
|
||||
todo!()
|
||||
}
|
||||
TypeUse::TupleTypeUse(tuple_type) => {
|
||||
todo!()
|
||||
}
|
||||
TypeUse::FunctionTypeUse(function_type) => {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn na_p1_function_body(
|
||||
function_body: &mut FunctionBody,
|
||||
symbol_table: &mut SymbolTable,
|
||||
diagnostics: &mut Vec<DmDiagnostic>,
|
||||
) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
@ -35,6 +35,7 @@ mod scope_table;
|
||||
mod second_pass;
|
||||
pub mod symbol;
|
||||
pub mod symbol_table;
|
||||
pub mod type_use_container;
|
||||
mod util;
|
||||
|
||||
pub fn analyze_names<'a, F: Files<'a, FileId = usize, Name = String>>(
|
||||
|
||||
@ -96,7 +96,6 @@ fn handle_concrete_use_statement_identifier(
|
||||
&fqn_parts.join("::"),
|
||||
use_statement_identifier.identifier().file_id(),
|
||||
use_statement_identifier.identifier().range(),
|
||||
"Usable Symbol",
|
||||
diagnostics,
|
||||
);
|
||||
}
|
||||
|
||||
@ -1,40 +1,40 @@
|
||||
use crate::name_analysis::symbol::class_member_symbol::ClassMemberSymbol;
|
||||
use crate::name_analysis::symbol::function_symbol::FunctionSymbol;
|
||||
use crate::name_analysis::symbol::source_definition::SourceDefinition;
|
||||
use crate::name_analysis::symbol::Symbol;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use crate::name_analysis::symbol::class_member_symbol::ClassMemberSymbol;
|
||||
use crate::name_analysis::symbol::function_symbol::FunctionSymbol;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ClassSymbol {
|
||||
declared_name: Rc<String>,
|
||||
fqn: Rc<String>,
|
||||
members: HashMap<Rc<String>, Rc<RefCell<ClassMemberSymbol>>>,
|
||||
functions: HashMap<Rc<String>, Rc<RefCell<FunctionSymbol>>>
|
||||
fqn_parts: Vec<Rc<str>>,
|
||||
members: HashMap<Rc<str>, Rc<RefCell<ClassMemberSymbol>>>,
|
||||
functions: HashMap<Rc<str>, Rc<RefCell<FunctionSymbol>>>,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
}
|
||||
|
||||
impl ClassSymbol {
|
||||
pub fn new(declared_name: &str, fqn: &str) -> Self {
|
||||
pub fn new(fqn_parts: &[Rc<str>], source_definition: Option<SourceDefinition>) -> Self {
|
||||
Self {
|
||||
declared_name: Rc::new(declared_name.to_string()),
|
||||
fqn: Rc::new(fqn.to_string()),
|
||||
fqn_parts: fqn_parts.to_vec(),
|
||||
members: HashMap::new(),
|
||||
functions: HashMap::new(),
|
||||
source_definition,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn declared_name(&self) -> &str {
|
||||
&self.declared_name
|
||||
self.fqn_parts.last().unwrap()
|
||||
}
|
||||
|
||||
pub fn declared_name_owned(&self) -> Rc<String> {
|
||||
self.declared_name.clone()
|
||||
pub fn declared_name_owned(&self) -> Rc<str> {
|
||||
self.fqn_parts.last().unwrap().clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fqn(&self) -> &str {
|
||||
&self.fqn
|
||||
}
|
||||
|
||||
pub fn fqn_owned(&self) -> Rc<String> {
|
||||
self.fqn.clone()
|
||||
impl Symbol for ClassSymbol {
|
||||
fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||
self.source_definition.as_ref()
|
||||
}
|
||||
}
|
||||
@ -1,30 +1,30 @@
|
||||
use crate::name_analysis::symbol::parameter_symbol::ParameterSymbol;
|
||||
use crate::name_analysis::symbol::source_definition::SourceDefinition;
|
||||
use crate::name_analysis::symbol::type_symbol::ConcreteTypeSymbol;
|
||||
use crate::name_analysis::symbol::Symbol;
|
||||
use crate::name_analysis::type_use_container::TypeUseContainer;
|
||||
use crate::name_analysis::util::join_fqn_parts;
|
||||
use std::cell::RefCell;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FunctionSymbol {
|
||||
fqn: String,
|
||||
declared_name: String,
|
||||
fqn_parts: Vec<Rc<str>>,
|
||||
is_public: bool,
|
||||
is_platform: bool,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
parameters: Vec<ParameterSymbol>,
|
||||
return_type: Option<ConcreteTypeSymbol>, // todo: can we use TypeSymbol?
|
||||
parameters: Vec<Rc<RefCell<ParameterSymbol>>>,
|
||||
return_type: Option<Rc<RefCell<TypeUseContainer>>>, // todo: can we use TypeSymbol?
|
||||
}
|
||||
|
||||
impl FunctionSymbol {
|
||||
pub fn without_parameters_or_return_type(
|
||||
fqn: &str,
|
||||
declared_name: &str,
|
||||
pub fn new(
|
||||
fqn_parts: &[Rc<str>],
|
||||
is_public: bool,
|
||||
is_platform: bool,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
) -> FunctionSymbol {
|
||||
FunctionSymbol {
|
||||
fqn: fqn.to_string(),
|
||||
declared_name: declared_name.to_string(),
|
||||
fqn_parts: fqn_parts.to_vec(),
|
||||
is_public,
|
||||
is_platform,
|
||||
source_definition,
|
||||
@ -33,36 +33,34 @@ impl FunctionSymbol {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_parameters(self, parameters: Vec<ParameterSymbol>) -> Self {
|
||||
pub fn with_parameters_and_return_type(
|
||||
fqn_parts: &[Rc<str>],
|
||||
is_public: bool,
|
||||
is_platform: bool,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
parameters: &[Rc<RefCell<ParameterSymbol>>],
|
||||
return_type: Option<Rc<RefCell<TypeUseContainer>>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
fqn: self.fqn,
|
||||
declared_name: self.declared_name,
|
||||
is_public: self.is_public,
|
||||
is_platform: self.is_platform,
|
||||
source_definition: self.source_definition,
|
||||
parameters,
|
||||
return_type: self.return_type,
|
||||
fqn_parts: fqn_parts.to_vec(),
|
||||
is_public,
|
||||
is_platform,
|
||||
source_definition,
|
||||
parameters: parameters.to_vec(),
|
||||
return_type,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_return_type(self, return_type: ConcreteTypeSymbol) -> Self {
|
||||
Self {
|
||||
fqn: self.fqn,
|
||||
declared_name: self.declared_name,
|
||||
is_public: self.is_public,
|
||||
is_platform: self.is_platform,
|
||||
source_definition: self.source_definition,
|
||||
parameters: self.parameters,
|
||||
return_type: Some(return_type),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fqn(&self) -> &str {
|
||||
&self.fqn
|
||||
pub fn fqn_parts_owned(&self) -> Vec<Rc<str>> {
|
||||
self.fqn_parts.to_vec()
|
||||
}
|
||||
|
||||
pub fn declared_name(&self) -> &str {
|
||||
&self.declared_name
|
||||
self.fqn_parts.last().unwrap().as_ref()
|
||||
}
|
||||
|
||||
pub fn declared_name_owned(&self) -> Rc<str> {
|
||||
self.fqn_parts.last().unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn is_public(&self) -> bool {
|
||||
@ -73,24 +71,30 @@ impl FunctionSymbol {
|
||||
self.is_platform
|
||||
}
|
||||
|
||||
pub fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||
self.source_definition.as_ref()
|
||||
}
|
||||
|
||||
pub fn parameters(&self) -> &[ParameterSymbol] {
|
||||
pub fn parameter_symbols(&self) -> &[Rc<RefCell<ParameterSymbol>>] {
|
||||
&self.parameters
|
||||
}
|
||||
|
||||
pub fn return_type(&self) -> Option<&ConcreteTypeSymbol> {
|
||||
self.return_type.as_ref()
|
||||
pub fn set_parameter_symbols(&mut self, parameter_symbols: Vec<Rc<RefCell<ParameterSymbol>>>) {
|
||||
self.parameters = parameter_symbols;
|
||||
}
|
||||
|
||||
pub fn set_return_type(&mut self, type_use_container: Rc<RefCell<TypeUseContainer>>) {
|
||||
self.return_type = Some(type_use_container);
|
||||
}
|
||||
}
|
||||
|
||||
impl Symbol for FunctionSymbol {
|
||||
fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||
self.source_definition.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for FunctionSymbol {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("FunctionSymbol")
|
||||
.field("fqn", &self.fqn)
|
||||
.field("declared_name", &self.declared_name)
|
||||
.field("fqn", &join_fqn_parts(&self.fqn_parts))
|
||||
.field("declared_name", &self.declared_name())
|
||||
.field("is_public", &self.is_public)
|
||||
.field("is_platform", &self.is_platform)
|
||||
.field("parameters", &self.parameters)
|
||||
|
||||
34
src/name_analysis/symbol/generic_type_symbol.rs
Normal file
34
src/name_analysis/symbol/generic_type_symbol.rs
Normal file
@ -0,0 +1,34 @@
|
||||
use crate::name_analysis::symbol::source_definition::SourceDefinition;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GenericTypeSymbol {
|
||||
declared_name: String,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
}
|
||||
|
||||
impl GenericTypeSymbol {
|
||||
pub fn new(declared_name: &str, source_definition: Option<SourceDefinition>) -> Self {
|
||||
GenericTypeSymbol {
|
||||
declared_name: declared_name.to_string(),
|
||||
source_definition,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn declared_name(&self) -> &str {
|
||||
&self.declared_name
|
||||
}
|
||||
|
||||
pub fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||
self.source_definition.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for GenericTypeSymbol {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("GenericTypeSymbol")
|
||||
.field("declared_name", &self.declared_name)
|
||||
.field("source_definition", &self.source_definition)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
@ -1,36 +1,37 @@
|
||||
use crate::name_analysis::symbol::function_symbol::FunctionSymbol;
|
||||
use crate::name_analysis::symbol::source_definition::SourceDefinition;
|
||||
use crate::name_analysis::symbol::Symbol;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use crate::name_analysis::symbol::function_symbol::FunctionSymbol;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct InterfaceSymbol {
|
||||
declared_name: Rc<String>,
|
||||
fqn: Rc<String>,
|
||||
functions: HashMap<String, FunctionSymbol>,
|
||||
fqn_parts: Vec<Rc<str>>,
|
||||
functions: HashMap<Rc<str>, Rc<RefCell<FunctionSymbol>>>,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
}
|
||||
|
||||
impl InterfaceSymbol {
|
||||
pub fn new(declared_name: &str, fqn: &str) -> Self {
|
||||
pub fn new(fqn_parts: &[Rc<str>], source_definition: Option<SourceDefinition>) -> Self {
|
||||
Self {
|
||||
declared_name: Rc::new(declared_name.to_string()),
|
||||
fqn: Rc::new(fqn.to_string()),
|
||||
fqn_parts: fqn_parts.to_vec(),
|
||||
functions: HashMap::new(),
|
||||
source_definition,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn declared_name(&self) -> &str {
|
||||
&self.declared_name
|
||||
self.fqn_parts.last().unwrap().as_ref()
|
||||
}
|
||||
|
||||
pub fn declared_name_owned(&self) -> Rc<String> {
|
||||
self.declared_name.clone()
|
||||
pub fn declared_name_owned(&self) -> Rc<str> {
|
||||
self.fqn_parts.last().unwrap().clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fqn(&self) -> &str {
|
||||
&self.fqn
|
||||
}
|
||||
|
||||
pub fn fqn_owned(&self) -> Rc<String> {
|
||||
self.fqn.clone()
|
||||
impl Symbol for InterfaceSymbol {
|
||||
fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||
self.source_definition.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,56 +1,21 @@
|
||||
pub mod class_member_symbol;
|
||||
pub mod class_symbol;
|
||||
pub mod function_symbol;
|
||||
pub mod generic_type_symbol;
|
||||
pub mod interface_symbol;
|
||||
pub mod module_level_symbol;
|
||||
pub mod module_symbol;
|
||||
pub mod parameter_symbol;
|
||||
pub mod primitive_type_symbol;
|
||||
pub mod source_definition;
|
||||
pub mod type_symbol;
|
||||
pub mod usable_symbol;
|
||||
pub mod use_symbol;
|
||||
pub mod variable_symbol;
|
||||
|
||||
use crate::name_analysis::symbol::type_symbol::ConcreteTypeSymbol;
|
||||
use crate::name_analysis::symbol::use_symbol::UseSymbol;
|
||||
use class_member_symbol::ClassMemberSymbol;
|
||||
use function_symbol::FunctionSymbol;
|
||||
use module_symbol::ModuleSymbol;
|
||||
use parameter_symbol::ParameterSymbol;
|
||||
use source_definition::SourceDefinition;
|
||||
use std::cell::RefCell;
|
||||
use std::fmt::{Debug, Display};
|
||||
use std::ops::Deref;
|
||||
use std::rc::Rc;
|
||||
use type_symbol::TypeSymbol;
|
||||
use variable_symbol::VariableSymbol;
|
||||
use crate::name_analysis::symbol::source_definition::SourceDefinition;
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Symbol {
|
||||
Use(UseSymbol),
|
||||
Module(ModuleSymbol),
|
||||
Type(TypeSymbol),
|
||||
ConcreteType(Rc<RefCell<ConcreteTypeSymbol>>),
|
||||
Function(FunctionSymbol),
|
||||
Parameter(ParameterSymbol),
|
||||
Variable(VariableSymbol),
|
||||
ClassMember(ClassMemberSymbol),
|
||||
}
|
||||
|
||||
impl Symbol {
|
||||
pub fn definition(&self) -> Option<SourceDefinition> {
|
||||
match self {
|
||||
Symbol::Use(use_symbol) => use_symbol.source_definition(),
|
||||
Symbol::Module(module) => module.source_definition().cloned(),
|
||||
Symbol::Type(type_symbol) => type_symbol.source_definition().cloned(),
|
||||
Symbol::ConcreteType(concrete_type_symbol) => {
|
||||
concrete_type_symbol.borrow().source_definition().cloned()
|
||||
}
|
||||
Symbol::Function(function_symbol) => function_symbol.source_definition().cloned(),
|
||||
Symbol::Parameter(parameter_symbol) => parameter_symbol.source_definition().cloned(),
|
||||
Symbol::Variable(variable_symbol) => variable_symbol.source_definition().cloned(),
|
||||
Symbol::ClassMember(class_member_symbol) => {
|
||||
class_member_symbol.source_definition().cloned()
|
||||
}
|
||||
}
|
||||
}
|
||||
pub trait Symbol: Debug {
|
||||
fn source_definition(&self) -> Option<&SourceDefinition>;
|
||||
}
|
||||
|
||||
30
src/name_analysis/symbol/module_level_symbol.rs
Normal file
30
src/name_analysis/symbol/module_level_symbol.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use crate::name_analysis::symbol::class_symbol::ClassSymbol;
|
||||
use crate::name_analysis::symbol::function_symbol::FunctionSymbol;
|
||||
use crate::name_analysis::symbol::interface_symbol::InterfaceSymbol;
|
||||
use crate::name_analysis::symbol::module_symbol::ModuleSymbol;
|
||||
use crate::name_analysis::symbol::Symbol;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ModuleLevelSymbol {
|
||||
Module(Rc<RefCell<ModuleSymbol>>),
|
||||
Interface(Rc<RefCell<InterfaceSymbol>>),
|
||||
Class(Rc<RefCell<ClassSymbol>>),
|
||||
Function(Rc<RefCell<FunctionSymbol>>),
|
||||
}
|
||||
|
||||
impl ModuleLevelSymbol {
|
||||
pub fn to_symbol(self) -> Rc<RefCell<dyn Symbol>> {
|
||||
match self {
|
||||
ModuleLevelSymbol::Module(module_symbol) => module_symbol as Rc<RefCell<dyn Symbol>>,
|
||||
ModuleLevelSymbol::Interface(interface_symbol) => {
|
||||
interface_symbol as Rc<RefCell<dyn Symbol>>
|
||||
}
|
||||
ModuleLevelSymbol::Class(class_symbol) => class_symbol as Rc<RefCell<dyn Symbol>>,
|
||||
ModuleLevelSymbol::Function(function_symbol) => {
|
||||
function_symbol as Rc<RefCell<dyn Symbol>>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,42 +1,54 @@
|
||||
use crate::name_analysis::symbol::module_level_symbol::ModuleLevelSymbol;
|
||||
use crate::name_analysis::symbol::source_definition::SourceDefinition;
|
||||
use crate::name_analysis::symbol::Symbol;
|
||||
use crate::name_analysis::util::join_fqn_parts;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ModuleSymbol {
|
||||
fqn: String,
|
||||
declared_name: String,
|
||||
fqn_parts: Vec<Rc<str>>,
|
||||
is_public: bool,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
inner_symbols: Vec<ModuleLevelSymbol>,
|
||||
}
|
||||
|
||||
impl ModuleSymbol {
|
||||
pub fn new(
|
||||
fqn: &str,
|
||||
declared_name: &str,
|
||||
fqn_parts: Vec<Rc<str>>,
|
||||
is_public: bool,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
) -> Self {
|
||||
Self {
|
||||
fqn: fqn.to_string(),
|
||||
declared_name: declared_name.to_string(),
|
||||
fqn_parts,
|
||||
is_public,
|
||||
source_definition,
|
||||
inner_symbols: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fqn(&self) -> &str {
|
||||
&self.fqn
|
||||
pub fn fqn_parts(&self) -> &[Rc<str>] {
|
||||
&self.fqn_parts
|
||||
}
|
||||
|
||||
pub fn declared_name(&self) -> &str {
|
||||
&self.declared_name
|
||||
self.fqn_parts.last().unwrap()
|
||||
}
|
||||
|
||||
pub fn declared_name_owned(&self) -> Rc<str> {
|
||||
self.fqn_parts.last().unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn is_public(&self) -> bool {
|
||||
self.is_public
|
||||
}
|
||||
|
||||
pub fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||
pub fn add_inner_symbol(&mut self, module_level_symbol: ModuleLevelSymbol) {
|
||||
self.inner_symbols.push(module_level_symbol);
|
||||
}
|
||||
}
|
||||
|
||||
impl Symbol for ModuleSymbol {
|
||||
fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||
self.source_definition.as_ref()
|
||||
}
|
||||
}
|
||||
@ -44,10 +56,10 @@ impl ModuleSymbol {
|
||||
impl Debug for ModuleSymbol {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("ModuleSymbol")
|
||||
.field("fqn", &self.fqn)
|
||||
.field("declared_name", &self.declared_name)
|
||||
.field("fqn", &join_fqn_parts(&self.fqn_parts))
|
||||
.field("is_public", &self.is_public)
|
||||
.field("source_definition", &self.source_definition)
|
||||
.field("inner_symbols", &self.inner_symbols)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,17 +1,23 @@
|
||||
use crate::name_analysis::symbol::source_definition::SourceDefinition;
|
||||
use crate::name_analysis::type_use_container::TypeUseContainer;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ParameterSymbol {
|
||||
declared_name: String,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
type_use_symbol: Option<TypeUseContainer>,
|
||||
}
|
||||
|
||||
impl ParameterSymbol {
|
||||
pub fn new(declared_name: &str, source_definition: Option<SourceDefinition>) -> Self {
|
||||
pub fn new(
|
||||
declared_name: &str,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
type_use_symbol: Option<TypeUseContainer>,
|
||||
) -> Self {
|
||||
ParameterSymbol {
|
||||
declared_name: declared_name.to_string(),
|
||||
source_definition,
|
||||
type_use_symbol,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
12
src/name_analysis/symbol/primitive_type_symbol.rs
Normal file
12
src/name_analysis/symbol/primitive_type_symbol.rs
Normal file
@ -0,0 +1,12 @@
|
||||
#[derive(Debug)]
|
||||
pub enum PrimitiveTypeSymbol {
|
||||
Byte,
|
||||
Char,
|
||||
Short,
|
||||
Int,
|
||||
Long,
|
||||
Float,
|
||||
Double,
|
||||
Boolean,
|
||||
String,
|
||||
}
|
||||
@ -1,121 +1,15 @@
|
||||
use crate::name_analysis::symbol::source_definition::SourceDefinition;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::cell::RefCell;
|
||||
use crate::name_analysis::symbol::class_symbol::ClassSymbol;
|
||||
use crate::name_analysis::symbol::generic_type_symbol::GenericTypeSymbol;
|
||||
use crate::name_analysis::symbol::interface_symbol::InterfaceSymbol;
|
||||
use crate::name_analysis::symbol::primitive_type_symbol::PrimitiveTypeSymbol;
|
||||
use std::fmt::Debug;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Debug)]
|
||||
pub enum TypeSymbol {
|
||||
Concrete(ConcreteTypeSymbol),
|
||||
Generic(GenericTypeSymbol),
|
||||
}
|
||||
|
||||
impl TypeSymbol {
|
||||
pub fn declared_name(&self) -> &str {
|
||||
match self {
|
||||
TypeSymbol::Concrete(t) => t.declared_name(),
|
||||
TypeSymbol::Generic(t) => t.declared_name(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||
match self {
|
||||
TypeSymbol::Concrete(t) => t.source_definition(),
|
||||
TypeSymbol::Generic(t) => t.source_definition(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ConcreteTypeSymbol {
|
||||
fqn: String,
|
||||
declared_name: String,
|
||||
is_public: bool,
|
||||
kind: ConcreteTypeSymbolKind,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
}
|
||||
|
||||
impl ConcreteTypeSymbol {
|
||||
pub fn new(
|
||||
fqn: &str,
|
||||
declared_name: &str,
|
||||
is_public: bool,
|
||||
kind: ConcreteTypeSymbolKind,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
) -> Self {
|
||||
Self {
|
||||
fqn: fqn.to_string(),
|
||||
declared_name: declared_name.to_string(),
|
||||
is_public,
|
||||
kind,
|
||||
source_definition
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fqn(&self) -> &str {
|
||||
&self.fqn
|
||||
}
|
||||
|
||||
pub fn is_public(&self) -> bool {
|
||||
self.is_public
|
||||
}
|
||||
|
||||
pub fn kind(&self) -> &ConcreteTypeSymbolKind {
|
||||
&self.kind
|
||||
}
|
||||
|
||||
pub fn declared_name(&self) -> &str {
|
||||
&self.declared_name
|
||||
}
|
||||
|
||||
pub fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||
self.source_definition.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for ConcreteTypeSymbol {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("TypeSymbol")
|
||||
.field("fqn", &self.fqn)
|
||||
.field("declared_name", &self.declared_name)
|
||||
.field("is_public", &self.is_public)
|
||||
.field("kind", &self.kind)
|
||||
.field("source_definition", &self.source_definition)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum ConcreteTypeSymbolKind {
|
||||
Interface,
|
||||
Class
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GenericTypeSymbol {
|
||||
declared_name: String,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
}
|
||||
|
||||
impl GenericTypeSymbol {
|
||||
pub fn new(declared_name: &str, source_definition: Option<SourceDefinition>) -> Self {
|
||||
GenericTypeSymbol {
|
||||
declared_name: declared_name.to_string(),
|
||||
source_definition,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn declared_name(&self) -> &str {
|
||||
&self.declared_name
|
||||
}
|
||||
|
||||
pub fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||
self.source_definition.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for GenericTypeSymbol {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("GenericTypeSymbol")
|
||||
.field("declared_name", &self.declared_name)
|
||||
.field("source_definition", &self.source_definition)
|
||||
.finish()
|
||||
}
|
||||
Primitive(PrimitiveTypeSymbol),
|
||||
Class(Rc<RefCell<ClassSymbol>>),
|
||||
Interface(Rc<RefCell<InterfaceSymbol>>),
|
||||
Generic(Rc<RefCell<GenericTypeSymbol>>),
|
||||
}
|
||||
|
||||
@ -1,12 +1,25 @@
|
||||
use crate::name_analysis::symbol::class_symbol::ClassSymbol;
|
||||
use crate::name_analysis::symbol::function_symbol::FunctionSymbol;
|
||||
use crate::name_analysis::symbol::interface_symbol::InterfaceSymbol;
|
||||
use crate::name_analysis::symbol::Symbol;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Debug)]
|
||||
pub enum UsableSymbol {
|
||||
Interface(Rc<RefCell<InterfaceSymbol>>),
|
||||
Class(Rc<RefCell<ClassSymbol>>),
|
||||
Function(Rc<RefCell<FunctionSymbol>>),
|
||||
}
|
||||
|
||||
impl UsableSymbol {
|
||||
pub fn to_symbol(self) -> Rc<RefCell<dyn Symbol>> {
|
||||
match self {
|
||||
UsableSymbol::Interface(interface_symbol) => {
|
||||
interface_symbol as Rc<RefCell<dyn Symbol>>
|
||||
}
|
||||
UsableSymbol::Class(class_symbol) => class_symbol as Rc<RefCell<dyn Symbol>>,
|
||||
UsableSymbol::Function(function_symbol) => function_symbol as Rc<RefCell<dyn Symbol>>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
use crate::name_analysis::symbol::source_definition::SourceDefinition;
|
||||
use crate::name_analysis::symbol::usable_symbol::UsableSymbol;
|
||||
use crate::name_analysis::symbol::Symbol;
|
||||
use crate::name_analysis::util::join_fqn_parts;
|
||||
use std::cell::RefCell;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug)]
|
||||
pub enum UseSymbol {
|
||||
Concrete(Rc<RefCell<ConcreteUseSymbol>>),
|
||||
Star(StarUseSymbol),
|
||||
@ -19,35 +21,31 @@ impl UseSymbol {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ConcreteUseSymbol {
|
||||
fqn_parts: Vec<String>,
|
||||
fqn_parts: Vec<Rc<str>>,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
resolved_symbol: Option<UsableSymbol>,
|
||||
}
|
||||
|
||||
impl ConcreteUseSymbol {
|
||||
pub fn new(
|
||||
fqn_parts: &[&str],
|
||||
source_definition: Option<SourceDefinition>,
|
||||
) -> Self {
|
||||
pub fn new(fqn_parts: &[Rc<str>], source_definition: Option<SourceDefinition>) -> Self {
|
||||
Self {
|
||||
fqn_parts: fqn_parts.iter().map(ToString::to_string).collect(),
|
||||
fqn_parts: fqn_parts.to_vec(),
|
||||
source_definition,
|
||||
resolved_symbol: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fqn_parts(&self) -> &[String] {
|
||||
self.fqn_parts.as_slice()
|
||||
pub fn fqn_parts(&self) -> &[Rc<str>] {
|
||||
&self.fqn_parts
|
||||
}
|
||||
|
||||
pub fn declared_name(&self) -> &str {
|
||||
self.fqn_parts.last().unwrap()
|
||||
}
|
||||
|
||||
pub fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||
self.source_definition.as_ref()
|
||||
pub fn declared_name_owned(&self) -> Rc<str> {
|
||||
self.fqn_parts.last().unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn resolved_symbol(&self) -> Option<&UsableSymbol> {
|
||||
@ -59,6 +57,12 @@ impl ConcreteUseSymbol {
|
||||
}
|
||||
}
|
||||
|
||||
impl Symbol for ConcreteUseSymbol {
|
||||
fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||
self.source_definition.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for ConcreteUseSymbol {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("ConcreteUseStatementSymbol")
|
||||
@ -70,23 +74,25 @@ impl Debug for ConcreteUseSymbol {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct StarUseSymbol {
|
||||
base_fqn: String,
|
||||
fqn_parts: Vec<Rc<str>>,
|
||||
source_definition: Option<SourceDefinition>,
|
||||
}
|
||||
|
||||
impl StarUseSymbol {
|
||||
pub fn new(base_fqn: &str, source_definition: Option<SourceDefinition>) -> Self {
|
||||
pub fn new(fqn_parts: &[Rc<str>], source_definition: Option<SourceDefinition>) -> Self {
|
||||
Self {
|
||||
base_fqn: base_fqn.to_string(),
|
||||
fqn_parts: fqn_parts.to_vec(),
|
||||
source_definition,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn base_fqn(&self) -> &str {
|
||||
&self.base_fqn
|
||||
pub fn fqn_parts(&self) -> &[Rc<str>] {
|
||||
&self.fqn_parts
|
||||
}
|
||||
}
|
||||
|
||||
pub fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||
impl Symbol for StarUseSymbol {
|
||||
fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||
self.source_definition.as_ref()
|
||||
}
|
||||
}
|
||||
@ -94,7 +100,7 @@ impl StarUseSymbol {
|
||||
impl Debug for StarUseSymbol {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("StarUseStatementSymbol")
|
||||
.field("base_fqn", &self.base_fqn)
|
||||
.field("fqn", &join_fqn_parts(&self.fqn_parts))
|
||||
.field("source_definition", &self.source_definition)
|
||||
.finish()
|
||||
}
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FqnContext {
|
||||
stack: Vec<String>,
|
||||
stack: Vec<Rc<str>>,
|
||||
}
|
||||
|
||||
impl FqnContext {
|
||||
@ -9,31 +11,18 @@ impl FqnContext {
|
||||
}
|
||||
|
||||
pub fn push(&mut self, name: &str) {
|
||||
self.stack.push(name.to_string());
|
||||
self.stack.push(Rc::from(name));
|
||||
}
|
||||
|
||||
pub fn pop(&mut self) {
|
||||
self.stack.pop();
|
||||
}
|
||||
|
||||
pub fn current(&self) -> String {
|
||||
let mut acc = String::new();
|
||||
for (i, name) in self.stack.iter().enumerate() {
|
||||
acc.push_str(name);
|
||||
if i != self.stack.len() - 1 {
|
||||
acc.push_str("::")
|
||||
}
|
||||
}
|
||||
acc
|
||||
pub fn current_fqn(&self) -> Vec<&str> {
|
||||
self.stack.iter().map(|part| part.as_ref()).collect()
|
||||
}
|
||||
|
||||
pub fn resolve(&self, name: &str) -> String {
|
||||
let mut acc = String::new();
|
||||
if !self.stack.is_empty() {
|
||||
acc.push_str(&self.current());
|
||||
acc.push_str("::");
|
||||
}
|
||||
acc.push_str(name);
|
||||
acc
|
||||
pub fn current_fqn_owned(&self) -> Vec<Rc<str>> {
|
||||
self.stack.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,20 +1,16 @@
|
||||
use crate::name_analysis::symbol::class_member_symbol::ClassMemberSymbol;
|
||||
use crate::name_analysis::symbol::function_symbol::FunctionSymbol;
|
||||
use crate::name_analysis::symbol::module_symbol::ModuleSymbol;
|
||||
use crate::name_analysis::symbol::parameter_symbol::ParameterSymbol;
|
||||
use crate::name_analysis::symbol::use_symbol::{ConcreteUseSymbol, StarUseSymbol, UseSymbol};
|
||||
use crate::name_analysis::symbol::variable_symbol::VariableSymbol;
|
||||
use crate::name_analysis::symbol::*;
|
||||
use crate::name_analysis::symbol::usable_symbol::UsableSymbol;
|
||||
use crate::name_analysis::symbol::use_symbol::{ConcreteUseSymbol, StarUseSymbol};
|
||||
use crate::name_analysis::symbol::Symbol;
|
||||
use crate::name_analysis::symbol_table::fqn_context::FqnContext;
|
||||
use crate::name_analysis::symbol_table::symbol_tree::SymbolTree;
|
||||
use crate::name_analysis::symbol_table::SymbolInsertError::SymbolAlreadyDefined;
|
||||
use crate::name_analysis::symbol_table::SymbolLookupError::NoDefinition;
|
||||
use scope::Scope;
|
||||
use std::cell::RefCell;
|
||||
use std::fmt::Display;
|
||||
use std::ops::Deref;
|
||||
use std::rc::Rc;
|
||||
use crate::name_analysis::symbol::usable_symbol::UsableSymbol;
|
||||
|
||||
pub(self) mod fqn_context;
|
||||
mod scope;
|
||||
@ -22,10 +18,9 @@ pub mod symbol_tree;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SymbolInsertError {
|
||||
SymbolAlreadyDefined(Symbol),
|
||||
SymbolAlreadyDefined(Rc<RefCell<dyn Symbol>>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SymbolLookupError {
|
||||
NoDefinition,
|
||||
}
|
||||
@ -85,8 +80,18 @@ impl SymbolTable {
|
||||
self.fqn_context.pop();
|
||||
}
|
||||
|
||||
pub fn symbol_tree(&self) -> &SymbolTree {
|
||||
&self.symbol_tree
|
||||
pub fn current_fqn(&self) -> Vec<&str> {
|
||||
self.fqn_context.current_fqn()
|
||||
}
|
||||
|
||||
pub fn current_fqn_owned(&self) -> Vec<Rc<str>> {
|
||||
self.fqn_context.current_fqn_owned()
|
||||
}
|
||||
|
||||
pub fn resolve_fqn(&self, part: Rc<str>) -> Vec<Rc<str>> {
|
||||
let mut parts = self.current_fqn_owned();
|
||||
parts.push(part);
|
||||
parts
|
||||
}
|
||||
|
||||
pub fn find_usable_symbol(&self, fqn_parts: &[&str]) -> Option<UsableSymbol> {
|
||||
@ -113,83 +118,20 @@ impl SymbolTable {
|
||||
&mut self.scopes[self.current_scope_id]
|
||||
}
|
||||
|
||||
fn find_current_scope_concrete_use_symbol(
|
||||
&self,
|
||||
declared_name: &str,
|
||||
) -> Option<Rc<RefCell<ConcreteUseSymbol>>> {
|
||||
self.current_scope()
|
||||
.concrete_use_symbols()
|
||||
.get(declared_name)
|
||||
.cloned()
|
||||
}
|
||||
|
||||
fn find_current_scope_module_symbol(&self, declared_name: &str) -> Option<&ModuleSymbol> {
|
||||
self.current_scope().module_symbols().get(declared_name)
|
||||
}
|
||||
|
||||
fn find_current_scope_parameter_symbol(&self, declared_name: &str) -> Option<&ParameterSymbol> {
|
||||
self.current_scope().parameter_symbols().get(declared_name)
|
||||
}
|
||||
|
||||
fn find_current_scope_variable_symbol(&self, declared_name: &str) -> Option<&VariableSymbol> {
|
||||
self.current_scope().variable_symbols().get(declared_name)
|
||||
}
|
||||
|
||||
fn find_current_scope_class_member_symbol(
|
||||
&self,
|
||||
declared_name: &str,
|
||||
) -> Option<&ClassMemberSymbol> {
|
||||
self.current_scope()
|
||||
.class_member_symbols()
|
||||
.get(declared_name)
|
||||
}
|
||||
|
||||
fn find_current_scope_variable_or_parameter_symbol(
|
||||
&self,
|
||||
declared_name: &str,
|
||||
) -> Option<Symbol> {
|
||||
self.find_current_scope_variable_symbol(declared_name)
|
||||
.map(|variable_symbol| Symbol::Variable(variable_symbol.clone()))
|
||||
.or_else(|| {
|
||||
self.find_current_scope_parameter_symbol(declared_name)
|
||||
.map(|parameter_symbol| Symbol::Parameter(parameter_symbol.clone()))
|
||||
})
|
||||
}
|
||||
|
||||
fn find_current_scope_usable_symbol(&self, declared_name: &str) -> Option<Symbol> {
|
||||
self.find_current_scope_concrete_use_symbol(declared_name)
|
||||
.map(|concrete_use_symbol| {
|
||||
Symbol::Use(UseSymbol::Concrete(concrete_use_symbol.clone()))
|
||||
})
|
||||
// .or_else(|| {
|
||||
// self.find_current_scope_type_symbol(declared_name)
|
||||
// .map(|type_symbol| Symbol::Type(type_symbol.clone()))
|
||||
// })
|
||||
.or_else(|| {
|
||||
self.find_current_scope_module_symbol(declared_name)
|
||||
.map(|module_symbol| Symbol::Module(module_symbol.clone()))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn insert_concrete_use_symbol(
|
||||
&mut self,
|
||||
concrete_use_symbol: ConcreteUseSymbol,
|
||||
) -> Result<Rc<RefCell<ConcreteUseSymbol>>, SymbolInsertError> {
|
||||
if let Some(defined_symbol) =
|
||||
self.find_current_scope_usable_symbol(concrete_use_symbol.declared_name())
|
||||
{
|
||||
Err(SymbolAlreadyDefined(defined_symbol))
|
||||
} else {
|
||||
let name = concrete_use_symbol.declared_name().to_string();
|
||||
self.current_scope_mut()
|
||||
.concrete_use_symbols_mut()
|
||||
.insert(name.clone(), Rc::new(RefCell::new(concrete_use_symbol)));
|
||||
Ok(self
|
||||
if let Some(defined_symbol) = self
|
||||
.current_scope()
|
||||
.concrete_use_symbols()
|
||||
.get(&name)
|
||||
.cloned()
|
||||
.unwrap())
|
||||
.find_module_level_symbol(concrete_use_symbol.declared_name())
|
||||
{
|
||||
Err(SymbolAlreadyDefined(defined_symbol.to_symbol()))
|
||||
} else {
|
||||
let inserted = self
|
||||
.current_scope_mut()
|
||||
.insert_concrete_use_symbol(concrete_use_symbol);
|
||||
Ok(inserted)
|
||||
}
|
||||
}
|
||||
|
||||
@ -206,142 +148,36 @@ impl SymbolTable {
|
||||
pub fn insert_module_symbol(
|
||||
&mut self,
|
||||
module_symbol: ModuleSymbol,
|
||||
) -> Result<(), SymbolInsertError> {
|
||||
if let Some(defined_symbol) =
|
||||
self.find_current_scope_usable_symbol(module_symbol.declared_name())
|
||||
) -> Result<Rc<RefCell<ModuleSymbol>>, SymbolInsertError> {
|
||||
if let Some(defined_symbol) = self
|
||||
.current_scope()
|
||||
.find_module_level_symbol(module_symbol.declared_name())
|
||||
{
|
||||
Err(SymbolAlreadyDefined(defined_symbol))
|
||||
Err(SymbolAlreadyDefined(defined_symbol.to_symbol()))
|
||||
} else {
|
||||
self.current_scope_mut()
|
||||
.module_symbols_mut()
|
||||
.insert(module_symbol.declared_name().to_string(), module_symbol);
|
||||
Ok(())
|
||||
let inserted = self.current_scope_mut().insert_module_symbol(module_symbol);
|
||||
self.symbol_tree.register_module(inserted.clone());
|
||||
Ok(inserted)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_function_symbol(
|
||||
&mut self,
|
||||
function_symbol: FunctionSymbol,
|
||||
) -> Result<(), SymbolInsertError> {
|
||||
if let Some(defined_symbol) =
|
||||
self.find_current_scope_usable_symbol(function_symbol.declared_name())
|
||||
) -> Result<Rc<RefCell<FunctionSymbol>>, SymbolInsertError> {
|
||||
if let Some(defined_symbol) = self
|
||||
.current_scope()
|
||||
.find_module_level_symbol(function_symbol.declared_name())
|
||||
{
|
||||
Err(SymbolAlreadyDefined(defined_symbol))
|
||||
Err(SymbolAlreadyDefined(defined_symbol.to_symbol()))
|
||||
} else {
|
||||
self.current_scope_mut()
|
||||
.function_symbols_mut()
|
||||
.insert(function_symbol.declared_name().to_string(), function_symbol);
|
||||
Ok(())
|
||||
let inserted = self
|
||||
.current_scope_mut()
|
||||
.insert_function_symbol(function_symbol);
|
||||
self.symbol_tree.register_function(inserted.clone());
|
||||
Ok(inserted)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_parameter_symbol(
|
||||
&mut self,
|
||||
parameter_symbol: ParameterSymbol,
|
||||
) -> Result<(), SymbolInsertError> {
|
||||
if let Some(defined_symbol) =
|
||||
self.find_current_scope_parameter_symbol(parameter_symbol.declared_name())
|
||||
{
|
||||
Err(SymbolAlreadyDefined(Symbol::Parameter(
|
||||
defined_symbol.clone(),
|
||||
)))
|
||||
} else {
|
||||
self.current_scope_mut().parameter_symbols_mut().insert(
|
||||
parameter_symbol.declared_name().to_string(),
|
||||
parameter_symbol,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_variable_symbol(
|
||||
&mut self,
|
||||
variable_symbol: VariableSymbol,
|
||||
) -> Result<(), SymbolInsertError> {
|
||||
if let Some(defined_symbol) =
|
||||
self.find_current_scope_variable_or_parameter_symbol(variable_symbol.declared_name())
|
||||
{
|
||||
Err(SymbolAlreadyDefined(defined_symbol))
|
||||
} else {
|
||||
self.current_scope_mut()
|
||||
.variable_symbols_mut()
|
||||
.insert(variable_symbol.declared_name().to_string(), variable_symbol);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_class_member_symbol(
|
||||
&mut self,
|
||||
class_member_symbol: ClassMemberSymbol,
|
||||
) -> Result<(), SymbolInsertError> {
|
||||
if let Some(defined_symbol) =
|
||||
self.find_current_scope_class_member_symbol(class_member_symbol.declared_name())
|
||||
{
|
||||
Err(SymbolAlreadyDefined(Symbol::ClassMember(
|
||||
defined_symbol.clone(),
|
||||
)))
|
||||
} else {
|
||||
self.current_scope_mut().class_member_symbols_mut().insert(
|
||||
class_member_symbol.declared_name().to_string(),
|
||||
class_member_symbol,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn lookup_addressable_in_scope_by_identifier(
|
||||
scope: &Scope,
|
||||
identifier: &str,
|
||||
) -> Option<Symbol> {
|
||||
scope
|
||||
.variable_symbols()
|
||||
.get(identifier)
|
||||
.map(|variable_symbol| Symbol::Variable(variable_symbol.clone()))
|
||||
.or_else(|| {
|
||||
scope
|
||||
.parameter_symbols()
|
||||
.get(identifier)
|
||||
.map(|parameter_symbol| Symbol::Parameter(parameter_symbol.clone()))
|
||||
})
|
||||
.or_else(|| {
|
||||
scope
|
||||
.class_member_symbols()
|
||||
.get(identifier)
|
||||
.map(|class_member_symbol| Symbol::ClassMember(class_member_symbol.clone()))
|
||||
})
|
||||
.or_else(|| {
|
||||
scope
|
||||
.function_symbols()
|
||||
.get(identifier)
|
||||
.map(|function_symbol| Symbol::Function(function_symbol.clone()))
|
||||
})
|
||||
// .or_else(|| {
|
||||
// scope
|
||||
// .type_symbols()
|
||||
// .get(identifier)
|
||||
// .map(|type_symbol| Symbol::Type(type_symbol.clone()))
|
||||
// })
|
||||
}
|
||||
|
||||
pub fn lookup_addressable_by_identifier(
|
||||
&self,
|
||||
identifier: &str,
|
||||
scope_id: usize,
|
||||
) -> Result<Symbol, SymbolLookupError> {
|
||||
let mut scope_opt = Some(&self.scopes[scope_id]);
|
||||
while let Some(scope) = scope_opt {
|
||||
if let Some(symbol) = Self::lookup_addressable_in_scope_by_identifier(scope, identifier)
|
||||
{
|
||||
return Ok(symbol);
|
||||
}
|
||||
scope_opt = if let Some(parent_id) = scope.parent() {
|
||||
Some(&self.scopes[parent_id])
|
||||
} else {
|
||||
None
|
||||
};
|
||||
}
|
||||
Err(NoDefinition)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for SymbolTable {
|
||||
|
||||
@ -2,6 +2,7 @@ use crate::name_analysis::symbol::class_member_symbol::ClassMemberSymbol;
|
||||
use crate::name_analysis::symbol::class_symbol::ClassSymbol;
|
||||
use crate::name_analysis::symbol::function_symbol::FunctionSymbol;
|
||||
use crate::name_analysis::symbol::interface_symbol::InterfaceSymbol;
|
||||
use crate::name_analysis::symbol::module_level_symbol::ModuleLevelSymbol;
|
||||
use crate::name_analysis::symbol::module_symbol::ModuleSymbol;
|
||||
use crate::name_analysis::symbol::parameter_symbol::ParameterSymbol;
|
||||
use crate::name_analysis::symbol::use_symbol::{ConcreteUseSymbol, StarUseSymbol};
|
||||
@ -16,18 +17,26 @@ pub struct Scope {
|
||||
parent: Option<usize>,
|
||||
id: usize,
|
||||
children: Vec<usize>,
|
||||
concrete_use_symbols: HashMap<String, Rc<RefCell<ConcreteUseSymbol>>>,
|
||||
star_use_symbols: HashMap<String, Rc<RefCell<StarUseSymbol>>>,
|
||||
module_symbols: HashMap<String, ModuleSymbol>,
|
||||
interface_symbols: HashMap<Rc<String>, Rc<RefCell<InterfaceSymbol>>>,
|
||||
class_symbols: HashMap<Rc<String>, Rc<RefCell<ClassSymbol>>>,
|
||||
function_symbols: HashMap<String, FunctionSymbol>,
|
||||
parameter_symbols: HashMap<String, ParameterSymbol>,
|
||||
variable_symbols: HashMap<String, VariableSymbol>,
|
||||
class_member_symbols: HashMap<String, ClassMemberSymbol>,
|
||||
concrete_use_symbols: HashMap<Rc<str>, Rc<RefCell<ConcreteUseSymbol>>>,
|
||||
star_use_symbols: HashMap<Vec<Rc<str>>, Rc<RefCell<StarUseSymbol>>>,
|
||||
module_symbols: HashMap<Rc<str>, Rc<RefCell<ModuleSymbol>>>,
|
||||
interface_symbols: HashMap<Rc<str>, Rc<RefCell<InterfaceSymbol>>>,
|
||||
class_symbols: HashMap<Rc<str>, Rc<RefCell<ClassSymbol>>>,
|
||||
function_symbols: HashMap<Rc<str>, Rc<RefCell<FunctionSymbol>>>,
|
||||
parameter_symbols: HashMap<Rc<str>, ParameterSymbol>,
|
||||
variable_symbols: HashMap<Rc<str>, VariableSymbol>,
|
||||
class_member_symbols: HashMap<Rc<str>, ClassMemberSymbol>,
|
||||
debug_name: String,
|
||||
}
|
||||
|
||||
macro_rules! insert_symbol {
|
||||
($table:expr, $symbol:expr, $key:expr) => {{
|
||||
let as_rc = Rc::new(RefCell::new($symbol));
|
||||
$table.insert($key, as_rc.clone());
|
||||
as_rc
|
||||
}};
|
||||
}
|
||||
|
||||
impl Scope {
|
||||
pub fn new(parent: Option<usize>, id: usize, debug_name: String) -> Self {
|
||||
Self {
|
||||
@ -63,81 +72,80 @@ impl Scope {
|
||||
self.children.clone()
|
||||
}
|
||||
|
||||
pub fn concrete_use_symbols(&self) -> &HashMap<String, Rc<RefCell<ConcreteUseSymbol>>> {
|
||||
&self.concrete_use_symbols
|
||||
}
|
||||
|
||||
pub fn concrete_use_symbols_mut(
|
||||
&mut self,
|
||||
) -> &mut HashMap<String, Rc<RefCell<ConcreteUseSymbol>>> {
|
||||
&mut self.concrete_use_symbols
|
||||
pub fn insert_concrete_use_symbol(&mut self, symbol: ConcreteUseSymbol) -> Rc<RefCell<ConcreteUseSymbol>> {
|
||||
let key = symbol.declared_name_owned();
|
||||
insert_symbol!(self.concrete_use_symbols, symbol, key)
|
||||
}
|
||||
|
||||
pub fn insert_star_use_symbol(&mut self, symbol: StarUseSymbol) -> Rc<RefCell<StarUseSymbol>> {
|
||||
let as_rc = Rc::new(RefCell::new(symbol));
|
||||
self.star_use_symbols.insert(as_rc.borrow().base_fqn().to_string(), as_rc.clone());
|
||||
as_rc.clone()
|
||||
let key = symbol.fqn_parts().to_vec();
|
||||
insert_symbol!(self.star_use_symbols, symbol, key)
|
||||
}
|
||||
|
||||
pub fn module_symbols(&self) -> &HashMap<String, ModuleSymbol> {
|
||||
&self.module_symbols
|
||||
pub fn insert_module_symbol(&mut self, symbol: ModuleSymbol) -> Rc<RefCell<ModuleSymbol>> {
|
||||
let key = symbol.declared_name_owned();
|
||||
insert_symbol!(self.module_symbols, symbol, key)
|
||||
}
|
||||
|
||||
pub fn module_symbols_mut(&mut self) -> &mut HashMap<String, ModuleSymbol> {
|
||||
&mut self.module_symbols
|
||||
pub fn get_module_symbol(&self, declared_name: &str) -> Option<&Rc<RefCell<ModuleSymbol>>> {
|
||||
self.module_symbols.get(declared_name)
|
||||
}
|
||||
|
||||
pub fn insert_interface_symbol(&mut self, symbol: InterfaceSymbol) {
|
||||
self.interface_symbols
|
||||
.insert(symbol.declared_name_owned(), Rc::new(RefCell::new(symbol)));
|
||||
pub fn insert_function_symbol(
|
||||
&mut self,
|
||||
symbol: FunctionSymbol,
|
||||
) -> Rc<RefCell<FunctionSymbol>> {
|
||||
let key = symbol.declared_name_owned();
|
||||
insert_symbol!(self.function_symbols, symbol, key)
|
||||
}
|
||||
|
||||
pub fn get_function_symbol(&self, declared_name: &str) -> Option<&Rc<RefCell<FunctionSymbol>>> {
|
||||
self.function_symbols.get(declared_name)
|
||||
}
|
||||
|
||||
pub fn insert_interface_symbol(
|
||||
&mut self,
|
||||
symbol: InterfaceSymbol,
|
||||
) -> Rc<RefCell<InterfaceSymbol>> {
|
||||
let key = symbol.declared_name_owned();
|
||||
insert_symbol!(self.interface_symbols, symbol, key)
|
||||
}
|
||||
|
||||
pub fn get_interface_symbol(
|
||||
&self,
|
||||
declared_name: Rc<String>,
|
||||
) -> Option<Rc<RefCell<InterfaceSymbol>>> {
|
||||
self.interface_symbols.get(&declared_name).cloned()
|
||||
declared_name: &str,
|
||||
) -> Option<&Rc<RefCell<InterfaceSymbol>>> {
|
||||
self.interface_symbols.get(declared_name)
|
||||
}
|
||||
|
||||
pub fn insert_class_symbol(&mut self, symbol: ClassSymbol) {
|
||||
pub fn insert_class_symbol(&mut self, symbol: ClassSymbol) -> Rc<RefCell<ClassSymbol>> {
|
||||
let key = symbol.declared_name_owned();
|
||||
insert_symbol!(self.class_symbols, symbol, key)
|
||||
}
|
||||
|
||||
pub fn get_class_symbol(&self, declared_name: &str) -> Option<&Rc<RefCell<ClassSymbol>>> {
|
||||
self.class_symbols.get(declared_name)
|
||||
}
|
||||
|
||||
pub fn find_module_level_symbol(&self, declared_name: &str) -> Option<ModuleLevelSymbol> {
|
||||
self.module_symbols
|
||||
.get(declared_name)
|
||||
.map(|module_symbol| ModuleLevelSymbol::Module(module_symbol.clone()))
|
||||
.or_else(|| {
|
||||
self.interface_symbols
|
||||
.get(declared_name)
|
||||
.map(|interface_symbol| ModuleLevelSymbol::Interface(interface_symbol.clone()))
|
||||
})
|
||||
.or_else(|| {
|
||||
self.class_symbols
|
||||
.insert(symbol.declared_name_owned(), Rc::new(RefCell::new(symbol)));
|
||||
}
|
||||
|
||||
pub fn get_class_symbol(&self, declared_name: Rc<String>) -> Option<Rc<RefCell<ClassSymbol>>> {
|
||||
self.class_symbols.get(&declared_name).cloned()
|
||||
}
|
||||
|
||||
pub fn function_symbols(&self) -> &HashMap<String, FunctionSymbol> {
|
||||
&self.function_symbols
|
||||
}
|
||||
|
||||
pub fn function_symbols_mut(&mut self) -> &mut HashMap<String, FunctionSymbol> {
|
||||
&mut self.function_symbols
|
||||
}
|
||||
|
||||
pub fn parameter_symbols(&self) -> &HashMap<String, ParameterSymbol> {
|
||||
&self.parameter_symbols
|
||||
}
|
||||
|
||||
pub fn parameter_symbols_mut(&mut self) -> &mut HashMap<String, ParameterSymbol> {
|
||||
&mut self.parameter_symbols
|
||||
}
|
||||
|
||||
pub fn variable_symbols(&self) -> &HashMap<String, VariableSymbol> {
|
||||
&self.variable_symbols
|
||||
}
|
||||
|
||||
pub fn variable_symbols_mut(&mut self) -> &mut HashMap<String, VariableSymbol> {
|
||||
&mut self.variable_symbols
|
||||
}
|
||||
|
||||
pub fn class_member_symbols(&self) -> &HashMap<String, ClassMemberSymbol> {
|
||||
&self.class_member_symbols
|
||||
}
|
||||
|
||||
pub fn class_member_symbols_mut(&mut self) -> &mut HashMap<String, ClassMemberSymbol> {
|
||||
&mut self.class_member_symbols
|
||||
.get(declared_name)
|
||||
.map(|class_symbol| ModuleLevelSymbol::Class(class_symbol.clone()))
|
||||
})
|
||||
.or_else(|| {
|
||||
self.function_symbols
|
||||
.get(declared_name)
|
||||
.map(|function_symbol| ModuleLevelSymbol::Function(function_symbol.clone()))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn debug_name(&self) -> &str {
|
||||
@ -164,14 +172,14 @@ impl Display for Scope {
|
||||
.unwrap_or_else(|| "None".to_string()),
|
||||
self.debug_name()
|
||||
)?;
|
||||
write_symbols!(f, self.concrete_use_symbols());
|
||||
todo!("self.star_use_symbols");
|
||||
write_symbols!(f, self.module_symbols());
|
||||
todo!("self.concrete_type_symbols");
|
||||
write_symbols!(f, self.function_symbols());
|
||||
write_symbols!(f, self.parameter_symbols());
|
||||
write_symbols!(f, self.variable_symbols());
|
||||
write_symbols!(f, self.class_member_symbols());
|
||||
write_symbols!(f, self.concrete_use_symbols);
|
||||
write_symbols!(f, self.star_use_symbols);
|
||||
write_symbols!(f, self.module_symbols);
|
||||
write_symbols!(f, self.interface_symbols);
|
||||
write_symbols!(f, self.function_symbols);
|
||||
write_symbols!(f, self.parameter_symbols);
|
||||
write_symbols!(f, self.variable_symbols);
|
||||
write_symbols!(f, self.class_member_symbols);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,16 +1,17 @@
|
||||
use std::cell::RefCell;
|
||||
use crate::name_analysis::symbol::class_symbol::ClassSymbol;
|
||||
use crate::name_analysis::symbol::function_symbol::FunctionSymbol;
|
||||
use crate::name_analysis::symbol::interface_symbol::InterfaceSymbol;
|
||||
use crate::name_analysis::symbol::module_symbol::ModuleSymbol;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SymbolTree {
|
||||
children: Box<HashMap<String, SymbolTree>>,
|
||||
classes: Box<HashMap<String, Rc<RefCell<ClassSymbol>>>>,
|
||||
interfaces: Box<HashMap<String, Rc<RefCell<InterfaceSymbol>>>>,
|
||||
functions: Box<HashMap<String, Rc<RefCell<FunctionSymbol>>>>,
|
||||
children: Box<HashMap<Rc<str>, SymbolTree>>,
|
||||
classes: Box<HashMap<Rc<str>, Rc<RefCell<ClassSymbol>>>>,
|
||||
interfaces: Box<HashMap<Rc<str>, Rc<RefCell<InterfaceSymbol>>>>,
|
||||
functions: Box<HashMap<Rc<str>, Rc<RefCell<FunctionSymbol>>>>,
|
||||
}
|
||||
|
||||
impl SymbolTree {
|
||||
@ -55,4 +56,49 @@ impl SymbolTree {
|
||||
.and_then(|child_tree| child_tree.find_function(&fqn_parts[1..])),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register_module(&mut self, module_symbol: Rc<RefCell<ModuleSymbol>>) {
|
||||
self.recurse_register_module(&module_symbol.borrow().fqn_parts());
|
||||
}
|
||||
|
||||
fn recurse_register_module(&mut self, fqn_parts: &[Rc<str>]) {
|
||||
if fqn_parts.len() == 0 {
|
||||
panic!("Unable to register module fqn with no parts.")
|
||||
}
|
||||
if fqn_parts.len() == 1 {
|
||||
self.children
|
||||
.insert(fqn_parts[0].clone(), SymbolTree::new());
|
||||
} else {
|
||||
if self.children.contains_key(fqn_parts[0].as_ref()) {
|
||||
let child = self.children.get_mut(fqn_parts[0].as_ref()).unwrap();
|
||||
child.recurse_register_module(&fqn_parts[1..]);
|
||||
} else {
|
||||
let mut child = SymbolTree::new();
|
||||
child.recurse_register_module(&fqn_parts[1..]);
|
||||
self.children.insert(fqn_parts[0].clone(), child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register_function(&mut self, function_symbol: Rc<RefCell<FunctionSymbol>>) {
|
||||
let fqn_parts = function_symbol.borrow().fqn_parts_owned();
|
||||
self.recurse_register_function(function_symbol, &fqn_parts);
|
||||
}
|
||||
|
||||
fn recurse_register_function(&mut self, function_symbol: Rc<RefCell<FunctionSymbol>>, fqn_parts: &[Rc<str>]) {
|
||||
if fqn_parts.len() == 0 {
|
||||
panic!("Unable to register function fqn with no parts.")
|
||||
}
|
||||
if fqn_parts.len() == 1 {
|
||||
self.functions.insert(fqn_parts[0].clone(), function_symbol);
|
||||
} else {
|
||||
if self.children.contains_key(fqn_parts[0].as_ref()) {
|
||||
let child = self.children.get_mut(fqn_parts[0].as_ref()).unwrap();
|
||||
child.recurse_register_function(function_symbol, &fqn_parts[1..]);
|
||||
} else {
|
||||
panic!("No such inner module registered: {}", fqn_parts[0])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
20
src/name_analysis/type_use_container.rs
Normal file
20
src/name_analysis/type_use_container.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use crate::name_analysis::symbol::type_symbol::TypeSymbol;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TypeUseContainer {
|
||||
resolved_type: Option<Rc<RefCell<TypeSymbol>>>
|
||||
}
|
||||
|
||||
impl TypeUseContainer {
|
||||
pub fn new() -> TypeUseContainer {
|
||||
Self {
|
||||
resolved_type: None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_resolved_type(&mut self, type_symbol: Rc<RefCell<TypeSymbol>>) {
|
||||
self.resolved_type = Some(type_symbol)
|
||||
}
|
||||
}
|
||||
@ -2,28 +2,28 @@ use crate::diagnostic::DmDiagnostic;
|
||||
use crate::name_analysis::symbol_table::{SymbolInsertError, SymbolLookupError};
|
||||
use codespan_reporting::diagnostic::{Diagnostic, Label};
|
||||
use std::range::Range;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub fn handle_insert_error(
|
||||
err: SymbolInsertError,
|
||||
error_symbol_name: &str,
|
||||
error_file_id: usize,
|
||||
error_range: Range<usize>,
|
||||
symbol_types: &str,
|
||||
diagnostics: &mut Vec<DmDiagnostic>,
|
||||
) {
|
||||
match err {
|
||||
SymbolInsertError::SymbolAlreadyDefined(s) => {
|
||||
let mut diagnostic = Diagnostic::error()
|
||||
.with_message(format!(
|
||||
"{} symbol '{}' already defined in the current scope.",
|
||||
symbol_types, error_symbol_name,
|
||||
"Symbol '{}' already defined in the current scope.",
|
||||
error_symbol_name,
|
||||
))
|
||||
.with_label(
|
||||
Label::primary(error_file_id, error_range)
|
||||
.with_message("Symbol duplicated here."),
|
||||
);
|
||||
|
||||
if let Some(source_definition) = s.definition() {
|
||||
if let Some(source_definition) = s.borrow().source_definition() {
|
||||
diagnostic = diagnostic.with_label(
|
||||
Label::secondary(source_definition.file_id(), source_definition.range())
|
||||
.with_message("Symbol defined here."),
|
||||
@ -40,16 +40,12 @@ pub fn handle_lookup_error(
|
||||
error_symbol_name: &str,
|
||||
error_file_id: usize,
|
||||
error_range: Range<usize>,
|
||||
symbol_types: &str,
|
||||
diagnostics: &mut Vec<DmDiagnostic>,
|
||||
) {
|
||||
match err {
|
||||
SymbolLookupError::NoDefinition => {
|
||||
let diagnostic = Diagnostic::error()
|
||||
.with_message(format!(
|
||||
"No such {} symbol '{}' in scope.",
|
||||
symbol_types, error_symbol_name,
|
||||
))
|
||||
.with_message(format!("No such symbol '{}' in scope.", error_symbol_name,))
|
||||
.with_label(
|
||||
Label::primary(error_file_id, error_range).with_message("Symbol used here."),
|
||||
);
|
||||
@ -57,3 +53,11 @@ pub fn handle_lookup_error(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_fqn(parts: &[&str]) -> String {
|
||||
parts.join("::")
|
||||
}
|
||||
|
||||
pub fn join_fqn_parts(parts: &[Rc<str>]) -> String {
|
||||
format_fqn(&parts.iter().map(|part| part.as_ref()).collect::<Vec<_>>())
|
||||
}
|
||||
|
||||
@ -1,27 +1,27 @@
|
||||
use crate::name_analysis::symbol::function_symbol::FunctionSymbol;
|
||||
use crate::name_analysis::symbol::parameter_symbol::ParameterSymbol;
|
||||
use crate::name_analysis::symbol_table::{SymbolInsertError, SymbolTable};
|
||||
use crate::name_analysis::type_use_container::TypeUseContainer;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub fn add_std_core_symbols(symbol_table: &mut SymbolTable) -> Result<(), SymbolInsertError> {
|
||||
symbol_table.insert_function_symbol(
|
||||
FunctionSymbol::without_parameters_or_return_type(
|
||||
"std::core::println",
|
||||
"println",
|
||||
symbol_table.set_current_fqn(&vec!["std", "core"]);
|
||||
|
||||
let mut println_msg_symbol_type_use = TypeUseContainer::new();
|
||||
println_msg_symbol_type_use.set_resolved_type(todo!());
|
||||
|
||||
let println_msg_symbol = ParameterSymbol::new("msg", None, Some(TypeUseContainer::new()));
|
||||
|
||||
let println_symbol = FunctionSymbol::with_parameters_and_return_type(
|
||||
&symbol_table.resolve_fqn(Rc::from("println")),
|
||||
true,
|
||||
true,
|
||||
None,
|
||||
)
|
||||
.with_parameters(vec![ParameterSymbol::new("msg", None)]),
|
||||
)?;
|
||||
symbol_table.insert_function_symbol(
|
||||
FunctionSymbol::without_parameters_or_return_type(
|
||||
"std::core::print",
|
||||
"print",
|
||||
true,
|
||||
true,
|
||||
&vec![Rc::new(RefCell::new(println_msg_symbol))],
|
||||
None,
|
||||
)
|
||||
.with_parameters(vec![ParameterSymbol::new("msg", None)]),
|
||||
)?;
|
||||
);
|
||||
symbol_table.insert_function_symbol(println_symbol)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user