Add fqn_context.
This commit is contained in:
parent
5be778ba80
commit
0e24ce1784
@ -1,4 +1,5 @@
|
|||||||
use crate::ast::expression::Expression;
|
use crate::ast::expression::Expression;
|
||||||
|
use crate::ast::fqn_util::fqn_parts_to_string;
|
||||||
use crate::ast::ir_builder::IrBuilder;
|
use crate::ast::ir_builder::IrBuilder;
|
||||||
use crate::diagnostic::Diagnostic;
|
use crate::diagnostic::Diagnostic;
|
||||||
use crate::ir::ir_call::IrCall;
|
use crate::ir::ir_call::IrCall;
|
||||||
@ -186,7 +187,7 @@ impl Call {
|
|||||||
let callable_symbol = self.get_callee_symbol();
|
let callable_symbol = self.get_callee_symbol();
|
||||||
match callable_symbol {
|
match callable_symbol {
|
||||||
CallableSymbol::Function(function_symbol) => IrCall::new(
|
CallableSymbol::Function(function_symbol) => IrCall::new(
|
||||||
function_symbol.borrow().declared_name_owned(),
|
fqn_parts_to_string(function_symbol.borrow().fqn_parts()),
|
||||||
arguments,
|
arguments,
|
||||||
function_symbol.borrow().is_extern(),
|
function_symbol.borrow().is_extern(),
|
||||||
),
|
),
|
||||||
@ -197,7 +198,7 @@ impl Call {
|
|||||||
.cloned()
|
.cloned()
|
||||||
.expect("Default constructors not supported yet.");
|
.expect("Default constructors not supported yet.");
|
||||||
IrCall::new(
|
IrCall::new(
|
||||||
constructor_symbol.borrow().declared_name_owned(),
|
fqn_parts_to_string(constructor_symbol.borrow().fqn_parts()),
|
||||||
arguments,
|
arguments,
|
||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
use crate::ast::constructor::Constructor;
|
use crate::ast::constructor::Constructor;
|
||||||
use crate::ast::field::Field;
|
use crate::ast::field::Field;
|
||||||
|
use crate::ast::fqn_context::FqnContext;
|
||||||
use crate::ast::function::Function;
|
use crate::ast::function::Function;
|
||||||
use crate::diagnostic::{Diagnostic, SecondaryLabel};
|
use crate::diagnostic::{Diagnostic, SecondaryLabel};
|
||||||
use crate::ir::ir_function::IrFunction;
|
use crate::ir::ir_function::IrFunction;
|
||||||
@ -40,7 +41,11 @@ impl Class {
|
|||||||
pub fn gather_declared_names(
|
pub fn gather_declared_names(
|
||||||
&mut self,
|
&mut self,
|
||||||
symbol_table: &mut SymbolTable,
|
symbol_table: &mut SymbolTable,
|
||||||
|
fqn_context: &mut FqnContext,
|
||||||
) -> Result<(), Vec<Diagnostic>> {
|
) -> Result<(), Vec<Diagnostic>> {
|
||||||
|
// 0. Push class name on fqn
|
||||||
|
fqn_context.push(self.declared_name.clone());
|
||||||
|
|
||||||
// 1. insert class symbol
|
// 1. insert class symbol
|
||||||
let to_insert = ClassSymbol::new(
|
let to_insert = ClassSymbol::new(
|
||||||
&self.declared_name,
|
&self.declared_name,
|
||||||
@ -94,7 +99,8 @@ impl Class {
|
|||||||
|
|
||||||
// 4. gather constructor
|
// 4. gather constructor
|
||||||
if let Some(constructor) = &mut self.constructor {
|
if let Some(constructor) = &mut self.constructor {
|
||||||
let constructor_symbol = constructor.gather_declared_names(symbol_table)?;
|
let constructor_symbol =
|
||||||
|
constructor.gather_declared_names(symbol_table, fqn_context)?;
|
||||||
self.class_symbol
|
self.class_symbol
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -108,7 +114,11 @@ impl Class {
|
|||||||
.functions
|
.functions
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.map(|function| {
|
.map(|function| {
|
||||||
function.gather_declared_names(symbol_table, self.class_symbol.as_ref())
|
function.gather_declared_names(
|
||||||
|
symbol_table,
|
||||||
|
fqn_context,
|
||||||
|
self.class_symbol.as_ref(),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.filter_map(Result::err)
|
.filter_map(Result::err)
|
||||||
.flatten()
|
.flatten()
|
||||||
@ -121,6 +131,9 @@ impl Class {
|
|||||||
// 6. pop scope
|
// 6. pop scope
|
||||||
symbol_table.pop_scope();
|
symbol_table.pop_scope();
|
||||||
|
|
||||||
|
// 7. pop fqn part
|
||||||
|
fqn_context.pop();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
use crate::ast::class::Class;
|
use crate::ast::class::Class;
|
||||||
use crate::ast::extern_function::ExternFunction;
|
use crate::ast::extern_function::ExternFunction;
|
||||||
|
use crate::ast::fqn_context::FqnContext;
|
||||||
use crate::ast::function::Function;
|
use crate::ast::function::Function;
|
||||||
use crate::diagnostic::Diagnostic;
|
use crate::diagnostic::Diagnostic;
|
||||||
use crate::ir::ir_function::IrFunction;
|
use crate::ir::ir_function::IrFunction;
|
||||||
@ -42,24 +43,26 @@ impl CompilationUnit {
|
|||||||
) -> Result<(), Vec<Diagnostic>> {
|
) -> Result<(), Vec<Diagnostic>> {
|
||||||
symbol_table.push_module_scope("compilation_unit_scope");
|
symbol_table.push_module_scope("compilation_unit_scope");
|
||||||
|
|
||||||
|
let mut fqn_context = FqnContext::new(); // in the future, we'll push the pkg/ns on here
|
||||||
let mut diagnostics: Vec<Diagnostic> = vec![];
|
let mut diagnostics: Vec<Diagnostic> = vec![];
|
||||||
|
|
||||||
self.functions
|
self.functions
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.map(|f| f.gather_declared_names(symbol_table, None))
|
.map(|f| f.gather_declared_names(symbol_table, &fqn_context, None))
|
||||||
.filter_map(Result::err)
|
.filter_map(Result::err)
|
||||||
.flatten()
|
.flatten()
|
||||||
.for_each(|diagnostic| diagnostics.push(diagnostic));
|
.for_each(|diagnostic| diagnostics.push(diagnostic));
|
||||||
|
|
||||||
self.extern_functions
|
self.extern_functions
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.map(|f| f.gather_declared_names(symbol_table))
|
.map(|f| f.gather_declared_names(symbol_table, &fqn_context))
|
||||||
.filter_map(Result::err)
|
.filter_map(Result::err)
|
||||||
.flatten()
|
.flatten()
|
||||||
.for_each(|diagnostic| diagnostics.push(diagnostic));
|
.for_each(|diagnostic| diagnostics.push(diagnostic));
|
||||||
|
|
||||||
self.classes
|
self.classes
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.map(|c| c.gather_declared_names(symbol_table))
|
.map(|c| c.gather_declared_names(symbol_table, &mut fqn_context))
|
||||||
.filter_map(Result::err)
|
.filter_map(Result::err)
|
||||||
.flatten()
|
.flatten()
|
||||||
.for_each(|diagnostic| diagnostics.push(diagnostic));
|
.for_each(|diagnostic| diagnostics.push(diagnostic));
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
use crate::ast::field::Field;
|
use crate::ast::field::Field;
|
||||||
|
use crate::ast::fqn_context::FqnContext;
|
||||||
|
use crate::ast::fqn_util::fqn_parts_to_string;
|
||||||
use crate::ast::ir_builder::IrBuilder;
|
use crate::ast::ir_builder::IrBuilder;
|
||||||
use crate::ast::parameter::Parameter;
|
use crate::ast::parameter::Parameter;
|
||||||
use crate::ast::statement::Statement;
|
use crate::ast::statement::Statement;
|
||||||
@ -55,9 +57,14 @@ impl Constructor {
|
|||||||
pub fn gather_declared_names(
|
pub fn gather_declared_names(
|
||||||
&mut self,
|
&mut self,
|
||||||
symbol_table: &mut SymbolTable,
|
symbol_table: &mut SymbolTable,
|
||||||
|
fqn_context: &FqnContext,
|
||||||
) -> Result<Rc<RefCell<ConstructorSymbol>>, Vec<Diagnostic>> {
|
) -> Result<Rc<RefCell<ConstructorSymbol>>, Vec<Diagnostic>> {
|
||||||
// insert constructor symbol
|
// insert constructor symbol
|
||||||
let to_insert = ConstructorSymbol::new(self.ctor_keyword_source_range.clone(), false);
|
let to_insert = ConstructorSymbol::new(
|
||||||
|
self.ctor_keyword_source_range.clone(),
|
||||||
|
fqn_context.resolve("ctor"), // ctor is a keyword at the language level, should not be callable via normal means
|
||||||
|
false,
|
||||||
|
);
|
||||||
let constructor_symbol =
|
let constructor_symbol =
|
||||||
symbol_table
|
symbol_table
|
||||||
.insert_constructor_symbol(to_insert)
|
.insert_constructor_symbol(to_insert)
|
||||||
@ -277,8 +284,15 @@ impl Constructor {
|
|||||||
let entry_block = ir_builder.get_block(entry_block_id);
|
let entry_block = ir_builder.get_block(entry_block_id);
|
||||||
|
|
||||||
IrFunction::new(
|
IrFunction::new(
|
||||||
format!("{}::ctor", class_symbol.borrow().declared_name()).into(), // fake function symbol
|
fqn_parts_to_string(
|
||||||
&ir_parameters, // make params
|
class_symbol
|
||||||
|
.borrow()
|
||||||
|
.constructor_symbol()
|
||||||
|
.unwrap()
|
||||||
|
.borrow()
|
||||||
|
.fqn_parts(),
|
||||||
|
), // fake function symbol
|
||||||
|
&ir_parameters, // make params
|
||||||
&TypeInfo::ClassInstance(class_symbol.clone()),
|
&TypeInfo::ClassInstance(class_symbol.clone()),
|
||||||
entry_block.clone(),
|
entry_block.clone(),
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
use crate::ast::fqn_context::FqnContext;
|
||||||
use crate::ast::parameter::Parameter;
|
use crate::ast::parameter::Parameter;
|
||||||
use crate::ast::type_use::TypeUse;
|
use crate::ast::type_use::TypeUse;
|
||||||
use crate::diagnostic::Diagnostic;
|
use crate::diagnostic::Diagnostic;
|
||||||
@ -39,12 +40,14 @@ impl ExternFunction {
|
|||||||
pub fn gather_declared_names(
|
pub fn gather_declared_names(
|
||||||
&mut self,
|
&mut self,
|
||||||
symbol_table: &mut SymbolTable,
|
symbol_table: &mut SymbolTable,
|
||||||
|
fqn_context: &FqnContext,
|
||||||
) -> Result<(), Vec<Diagnostic>> {
|
) -> Result<(), Vec<Diagnostic>> {
|
||||||
let mut diagnostics = vec![];
|
let mut diagnostics = vec![];
|
||||||
|
|
||||||
let insert_result = symbol_table.insert_function_symbol(FunctionSymbol::new(
|
let insert_result = symbol_table.insert_function_symbol(FunctionSymbol::new(
|
||||||
&self.declared_name,
|
&self.declared_name,
|
||||||
self.declared_name_source_range.clone(),
|
self.declared_name_source_range.clone(),
|
||||||
|
fqn_context.resolve(self.declared_name()),
|
||||||
true,
|
true,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|||||||
25
dmc-lib/src/ast/fqn_context.rs
Normal file
25
dmc-lib/src/ast/fqn_context.rs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
pub struct FqnContext {
|
||||||
|
parts: Vec<Rc<str>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FqnContext {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self { parts: vec![] }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push(&mut self, part: Rc<str>) {
|
||||||
|
self.parts.push(part);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pop(&mut self) {
|
||||||
|
self.parts.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resolve(&self, name: &str) -> Vec<Rc<str>> {
|
||||||
|
let mut result = self.parts.clone();
|
||||||
|
result.push(name.into());
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
6
dmc-lib/src/ast/fqn_util.rs
Normal file
6
dmc-lib/src/ast/fqn_util.rs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn fqn_parts_to_string(parts: &[Rc<str>]) -> Rc<str> {
|
||||||
|
parts.join("::").into()
|
||||||
|
}
|
||||||
@ -1,3 +1,5 @@
|
|||||||
|
use crate::ast::fqn_context::FqnContext;
|
||||||
|
use crate::ast::fqn_util::fqn_parts_to_string;
|
||||||
use crate::ast::ir_builder::IrBuilder;
|
use crate::ast::ir_builder::IrBuilder;
|
||||||
use crate::ast::parameter::Parameter;
|
use crate::ast::parameter::Parameter;
|
||||||
use crate::ast::statement::Statement;
|
use crate::ast::statement::Statement;
|
||||||
@ -7,9 +9,9 @@ use crate::ir::ir_function::IrFunction;
|
|||||||
use crate::ir::ir_parameter::IrParameter;
|
use crate::ir::ir_parameter::IrParameter;
|
||||||
use crate::ir::ir_parameter_or_variable::IrParameterOrVariable;
|
use crate::ir::ir_parameter_or_variable::IrParameterOrVariable;
|
||||||
use crate::source_range::SourceRange;
|
use crate::source_range::SourceRange;
|
||||||
|
use crate::symbol::Symbol;
|
||||||
use crate::symbol::class_symbol::ClassSymbol;
|
use crate::symbol::class_symbol::ClassSymbol;
|
||||||
use crate::symbol::function_symbol::FunctionSymbol;
|
use crate::symbol::function_symbol::FunctionSymbol;
|
||||||
use crate::symbol::Symbol;
|
|
||||||
use crate::symbol_table::{SymbolInsertError, SymbolTable};
|
use crate::symbol_table::{SymbolInsertError, SymbolTable};
|
||||||
use crate::type_info::TypeInfo;
|
use crate::type_info::TypeInfo;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
@ -57,6 +59,7 @@ impl Function {
|
|||||||
pub fn gather_declared_names(
|
pub fn gather_declared_names(
|
||||||
&mut self,
|
&mut self,
|
||||||
symbol_table: &mut SymbolTable,
|
symbol_table: &mut SymbolTable,
|
||||||
|
fqn_context: &FqnContext,
|
||||||
class_context: Option<&Rc<RefCell<ClassSymbol>>>,
|
class_context: Option<&Rc<RefCell<ClassSymbol>>>,
|
||||||
) -> Result<(), Vec<Diagnostic>> {
|
) -> Result<(), Vec<Diagnostic>> {
|
||||||
let mut diagnostics = vec![];
|
let mut diagnostics = vec![];
|
||||||
@ -69,6 +72,7 @@ impl Function {
|
|||||||
let insert_result = symbol_table.insert_function_symbol(FunctionSymbol::new(
|
let insert_result = symbol_table.insert_function_symbol(FunctionSymbol::new(
|
||||||
self.declared_name(),
|
self.declared_name(),
|
||||||
self.declared_name_source_range.clone(),
|
self.declared_name_source_range.clone(),
|
||||||
|
fqn_context.resolve(self.declared_name()),
|
||||||
false,
|
false,
|
||||||
));
|
));
|
||||||
|
|
||||||
@ -304,11 +308,7 @@ impl Function {
|
|||||||
|
|
||||||
let entry_block = builder.get_block(entry_block_id).clone();
|
let entry_block = builder.get_block(entry_block_id).clone();
|
||||||
IrFunction::new(
|
IrFunction::new(
|
||||||
self.function_symbol
|
fqn_parts_to_string(self.function_symbol.as_ref().unwrap().borrow().fqn_parts()),
|
||||||
.as_ref()
|
|
||||||
.unwrap()
|
|
||||||
.borrow()
|
|
||||||
.declared_name_owned(), // ok for now... but we need to start using the fqn
|
|
||||||
builder.parameters(),
|
builder.parameters(),
|
||||||
return_type_info,
|
return_type_info,
|
||||||
entry_block,
|
entry_block,
|
||||||
|
|||||||
@ -9,6 +9,8 @@ pub mod expression_statement;
|
|||||||
pub mod extern_function;
|
pub mod extern_function;
|
||||||
pub mod field;
|
pub mod field;
|
||||||
pub mod fqn;
|
pub mod fqn;
|
||||||
|
pub mod fqn_context;
|
||||||
|
mod fqn_util;
|
||||||
pub mod function;
|
pub mod function;
|
||||||
pub mod identifier;
|
pub mod identifier;
|
||||||
pub mod integer_literal;
|
pub mod integer_literal;
|
||||||
|
|||||||
@ -6,19 +6,29 @@ use std::rc::Rc;
|
|||||||
|
|
||||||
pub struct ConstructorSymbol {
|
pub struct ConstructorSymbol {
|
||||||
ctor_keyword_source_range: SourceRange,
|
ctor_keyword_source_range: SourceRange,
|
||||||
|
fqn_parts: Vec<Rc<str>>,
|
||||||
is_extern: bool,
|
is_extern: bool,
|
||||||
parameters: Option<Vec<Rc<RefCell<ParameterSymbol>>>>,
|
parameters: Option<Vec<Rc<RefCell<ParameterSymbol>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConstructorSymbol {
|
impl ConstructorSymbol {
|
||||||
pub fn new(ctor_keyword_source_range: SourceRange, is_extern: bool) -> Self {
|
pub fn new(
|
||||||
|
ctor_keyword_source_range: SourceRange,
|
||||||
|
fqn_parts: Vec<Rc<str>>,
|
||||||
|
is_extern: bool,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
ctor_keyword_source_range,
|
ctor_keyword_source_range,
|
||||||
|
fqn_parts,
|
||||||
is_extern,
|
is_extern,
|
||||||
parameters: None,
|
parameters: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn fqn_parts(&self) -> &[Rc<str>] {
|
||||||
|
&self.fqn_parts
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_parameters(&mut self, parameters: Vec<Rc<RefCell<ParameterSymbol>>>) {
|
pub fn set_parameters(&mut self, parameters: Vec<Rc<RefCell<ParameterSymbol>>>) {
|
||||||
self.parameters = Some(parameters);
|
self.parameters = Some(parameters);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,7 @@ use std::rc::Rc;
|
|||||||
pub struct FunctionSymbol {
|
pub struct FunctionSymbol {
|
||||||
declared_name: Rc<str>,
|
declared_name: Rc<str>,
|
||||||
declared_name_source_range: SourceRange,
|
declared_name_source_range: SourceRange,
|
||||||
|
fqn_parts: Vec<Rc<str>>,
|
||||||
is_extern: bool,
|
is_extern: bool,
|
||||||
parameters: Option<Vec<Rc<RefCell<ParameterSymbol>>>>,
|
parameters: Option<Vec<Rc<RefCell<ParameterSymbol>>>>,
|
||||||
return_type: Option<TypeInfo>,
|
return_type: Option<TypeInfo>,
|
||||||
@ -17,17 +18,23 @@ impl FunctionSymbol {
|
|||||||
pub fn new(
|
pub fn new(
|
||||||
declared_name: &str,
|
declared_name: &str,
|
||||||
declared_name_source_range: SourceRange,
|
declared_name_source_range: SourceRange,
|
||||||
|
fqn_parts: Vec<Rc<str>>,
|
||||||
is_extern: bool,
|
is_extern: bool,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
declared_name: declared_name.into(),
|
declared_name: declared_name.into(),
|
||||||
declared_name_source_range,
|
declared_name_source_range,
|
||||||
|
fqn_parts,
|
||||||
is_extern,
|
is_extern,
|
||||||
parameters: None,
|
parameters: None,
|
||||||
return_type: None,
|
return_type: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn fqn_parts(&self) -> &[Rc<str>] {
|
||||||
|
&self.fqn_parts
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_parameters(&mut self, parameters: Vec<Rc<RefCell<ParameterSymbol>>>) {
|
pub fn set_parameters(&mut self, parameters: Vec<Rc<RefCell<ParameterSymbol>>>) {
|
||||||
self.parameters = Some(parameters);
|
self.parameters = Some(parameters);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user