Compare commits

..

No commits in common. "d50cc24d0aa36e2d65b3dff746ffbb1f4bc67f48" and "11d368ab1a6f18233a28b0e38e750c254450f901" have entirely different histories.

10 changed files with 23 additions and 287 deletions

View File

@ -11,7 +11,7 @@ use crate::ast::helpers::{
resolve_ctor_name,
};
use crate::ast::statement::Statement;
use crate::ast::{FunctionReturnTypes, NodeId, NodesToSymbols, NodesToTypes, SymbolsToTypes};
use crate::ast::{NodeId, NodesToSymbols};
use crate::diagnostic::{Diagnostic, Diagnostics};
use crate::error_codes::{FIELD_MULTIPLE_INIT, FIELD_UNINIT};
use crate::ir::ir_class::{IrClass, IrField};
@ -187,7 +187,6 @@ impl Class {
(names_table, diagnostics)
}
#[deprecated]
pub fn check_names(&self, symbol_table: &SymbolTable) -> Vec<Diagnostic> {
let mut diagnostics: Vec<Diagnostic> = Vec::new();
@ -232,7 +231,6 @@ impl Class {
.collect()
}
#[deprecated]
pub fn analyze_local_names(&self, symbol_table: &mut SymbolTable) -> Vec<Diagnostic> {
let class_symbol = self.get_class_symbol_owned(symbol_table);
let mut diagnostics: Vec<Diagnostic> = Vec::new();
@ -246,7 +244,6 @@ impl Class {
diagnostics
}
#[deprecated]
pub fn gather_types(
&self,
symbol_table: &SymbolTable,
@ -475,7 +472,6 @@ impl Class {
Ok(())
}
#[deprecated]
pub fn type_check(
&mut self,
symbol_table: &SymbolTable,
@ -489,7 +485,6 @@ impl Class {
Ok(())
}
#[deprecated]
pub fn to_ir(
&self,
symbol_table: &SymbolTable,
@ -534,43 +529,4 @@ impl Class {
(ir_class, ir_functions)
}
pub fn lower_to_ir(
&self,
nodes_to_symbols: &NodesToSymbols,
symbols_to_types: &SymbolsToTypes,
nodes_to_types: &NodesToTypes,
function_return_types: &FunctionReturnTypes,
) -> (IrClass, Vec<IrFunction>) {
let mut ir_functions = Vec::new();
if let Some(constructor) = &self.constructor {
// todo
}
for function in &self.functions {
ir_functions.push(function.lower_to_ir_static(
nodes_to_symbols,
symbols_to_types,
nodes_to_types,
function_return_types,
));
}
let self_class_symbol = nodes_to_symbols
.get(&self.node_id)
.unwrap()
.unwrap_class_symbol();
let ir_class = IrClass::new(
self_class_symbol.declared_name_owned(),
fqn_parts_to_string(self_class_symbol.fqn_parts()).into(),
self.fields
.iter()
.map(|field| field.lower_to_ir_field(nodes_to_symbols, symbols_to_types))
.collect(),
);
(ir_class, ir_functions)
}
}

View File

@ -245,7 +245,7 @@ impl CompilationUnit {
diagnostics_result!(diagnostics)
}
pub fn lower_to_ir(
pub fn lower(
&self,
nodes_to_symbols: &NodesToSymbols,
symbols_to_types: &SymbolsToTypes,
@ -256,7 +256,7 @@ impl CompilationUnit {
let mut ir_functions = Vec::new();
for function in &self.functions {
ir_functions.push(function.lower_to_ir_static(
ir_functions.push(function.lower_static(
nodes_to_symbols,
symbols_to_types,
nodes_to_types,
@ -264,21 +264,9 @@ impl CompilationUnit {
));
}
for class in &self.classes {
let (ir_class, mut class_ir_functions) = class.lower_to_ir(
nodes_to_symbols,
symbols_to_types,
nodes_to_types,
function_return_types,
);
ir_classes.push(ir_class);
ir_functions.append(&mut class_ir_functions);
}
(ir_classes, ir_functions)
}
#[deprecated]
pub fn to_ir(
&self,
symbol_table: &SymbolTable,

View File

@ -1,3 +1,4 @@
use crate::ast::NodesToSymbols;
use crate::ast::field::Field;
use crate::ast::fqn_context::FqnContext;
use crate::ast::fqn_util::fqn_parts_to_string;
@ -7,7 +8,6 @@ use crate::ast::helpers::{
use crate::ast::ir_builder::IrBuilder;
use crate::ast::parameter::Parameter;
use crate::ast::statement::Statement;
use crate::ast::{NodeId, NodesToSymbols, NodesToTypes, SymbolsToTypes};
use crate::diagnostic::{Diagnostic, Diagnostics};
use crate::ir::ir_allocate::IrAllocate;
use crate::ir::ir_assign::IrAssign;
@ -34,7 +34,6 @@ use std::ops::Neg;
use std::rc::Rc;
pub struct Constructor {
node_id: NodeId,
is_public: bool,
ctor_keyword_source_range: SourceRange,
parameters: Vec<Parameter>,
@ -44,14 +43,12 @@ pub struct Constructor {
impl Constructor {
pub fn new(
node_id: NodeId,
is_public: bool,
ctor_keyword_source_range: SourceRange,
parameters: Vec<Parameter>,
statements: Vec<Statement>,
) -> Self {
Self {
node_id,
is_public,
ctor_keyword_source_range,
parameters,
@ -126,7 +123,6 @@ impl Constructor {
(names_table, diagnostics)
}
#[deprecated]
pub fn check_names(&self, symbol_table: &SymbolTable) -> Vec<Diagnostic> {
let mut diagnostics: Vec<Diagnostic> = Vec::new();
for parameter in &self.parameters {
@ -135,7 +131,6 @@ impl Constructor {
diagnostics
}
#[deprecated]
pub fn analyze_local_names(
&self,
symbol_table: &mut SymbolTable,
@ -147,14 +142,12 @@ impl Constructor {
.collect()
}
#[deprecated]
pub fn gather_types_into(&self, symbol_table: &SymbolTable, types_table: &mut TypesTable) {
for parameter in &self.parameters {
parameter.gather_types_into(symbol_table, types_table);
}
}
#[deprecated]
pub fn type_check(
&mut self,
symbol_table: &SymbolTable,
@ -187,7 +180,6 @@ impl Constructor {
}
}
#[deprecated]
pub fn to_ir(
&self,
class_symbol: &Rc<ClassSymbol>,
@ -323,139 +315,4 @@ impl Constructor {
entry_block.clone(),
)
}
pub fn lower_to_ir(
&self,
class_symbol: &Rc<ClassSymbol>,
fields: &[Field],
nodes_to_symbols: &NodesToSymbols,
symbols_to_types: &SymbolsToTypes,
nodes_to_types: &NodesToTypes,
) -> IrFunction {
let mut ir_builder = IrBuilder::new();
// gather ir_parameters
let mut ir_parameters = Vec::new();
let base_offset = (self.parameters.len() as isize).neg();
for (i, parameter) in self.parameters.iter().enumerate() {
let symbol = nodes_to_symbols.get(&parameter.node_id()).unwrap();
let parameter_type = symbols_to_types.get(symbol).unwrap();
let offset = base_offset + i as isize;
let ir_parameter = Rc::new(IrParameter::new(
symbol.declared_name(),
parameter_type.clone(),
offset,
));
// save in builder
ir_builder.push_parameter(symbol.unwrap_parameter_symbol(), ir_parameter.clone());
// push for saving to IrFunction
ir_parameters.push(ir_parameter);
}
// entry-block
let entry_block_id = ir_builder.new_block();
// PART 1: Make self object
let self_variable = Rc::new(RefCell::new(IrVariable::new_vr(
ir_builder.new_t_var().into(),
ir_builder.current_block().id(),
&TypeInfo::ParameterizedClass(class_symbol.clone(), Vec::new()), // todo: figure out the correct TypeInfo for this
)));
// save self variable in builder
ir_builder
.set_self_parameter_or_variable(IrParameterOrVariable::Variable(self_variable.clone()));
// allocate the self object
let ir_assign = IrAssign::new(
self_variable.clone(),
IrOperation::Allocate(IrAllocate::new(class_symbol.declared_name_owned())),
);
ir_builder
.current_block_mut()
.add_statement(IrStatement::Assign(ir_assign));
// PART 2: Initialize fields that are initialized OUTSIDE the constructor
for field in fields {
if let Some(initializer) = field.initializer() {
let symbol = nodes_to_symbols.get(&field.node_id()).unwrap();
let field_type = symbols_to_types.get(symbol).unwrap();
let field_symbol = symbol.unwrap_field_symbol();
// 1. Get a mut ref to the field
// mut t_var: Type = &mut self.x
let ir_get_field_ref_mut = IrGetFieldRefMut::new(
IrParameterOrVariable::Variable(self_variable.clone()),
field_symbol.field_index(),
);
let field_ref_mut_ir_variable = Rc::new(RefCell::new(IrVariable::new_vr(
ir_builder.new_t_var().into(),
ir_builder.current_block().id(),
field_type,
)));
let ir_assign = IrAssign::new(
field_ref_mut_ir_variable.clone(),
IrOperation::GetFieldRefMut(ir_get_field_ref_mut),
);
ir_builder
.current_block_mut()
.add_statement(IrStatement::Assign(ir_assign));
// save the mut ref for later uses if needed
ir_builder.field_mut_pointer_variables_mut().insert(
field.declared_name_owned(),
field_ref_mut_ir_variable.clone(),
);
// 2. Evaluate initializer and save result to mut field ref
let ir_expression = initializer.lower_to_ir_expression(
&mut ir_builder,
nodes_to_symbols,
symbols_to_types,
nodes_to_types,
);
let ir_set_field = IrSetField::new(&field_ref_mut_ir_variable, ir_expression);
ir_builder
.current_block_mut()
.add_statement(IrStatement::SetField(ir_set_field));
}
}
// PART 3. Constructor statements
for statement in &self.statements {
statement.lower(
&mut ir_builder,
nodes_to_symbols,
symbols_to_types,
nodes_to_types,
false,
);
}
// PART 4. Return finished self object
let ir_return_statement = IrStatement::Return(IrReturn::new(Some(IrExpression::Variable(
self_variable.clone(),
))));
ir_builder
.current_block_mut()
.add_statement(ir_return_statement);
// Finish up the builder and return IrFunction
ir_builder.finish_block();
let entry_block = ir_builder.get_block(entry_block_id);
let constructor_symbol = nodes_to_symbols
.get(&self.node_id)
.unwrap()
.unwrap_constructor_symbol();
IrFunction::new(
fqn_parts_to_string(constructor_symbol.fqn_parts()),
ir_parameters,
&TypeInfo::Class(class_symbol.clone()), // TODO
entry_block.clone(),
)
}
}

View File

@ -1,10 +1,9 @@
use crate::ast::NodesToSymbols;
use crate::ast::expression::Expression;
use crate::ast::helpers::insert_resolved_names_into;
use crate::ast::type_use::TypeUse;
use crate::ast::{NodeId, NodesToSymbols, SymbolsToTypes};
use crate::diagnostic::{Diagnostic, Diagnostics};
use crate::diagnostic_factories::field_has_no_type_or_init;
use crate::ir::ir_class::IrField;
use crate::source_range::SourceRange;
use crate::symbol::class_symbol::ClassSymbol;
use crate::symbol::field_symbol::FieldSymbol;
@ -13,7 +12,6 @@ use crate::types_table::TypesTable;
use std::rc::Rc;
pub struct Field {
node_id: NodeId,
declared_name: Rc<str>,
declared_name_source_range: SourceRange,
is_public: bool,
@ -25,7 +23,6 @@ pub struct Field {
impl Field {
pub fn new(
node_id: NodeId,
declared_name: &str,
declared_name_source_range: SourceRange,
is_public: bool,
@ -34,7 +31,6 @@ impl Field {
initializer: Option<Expression>,
) -> Self {
Self {
node_id,
declared_name: declared_name.into(),
declared_name_source_range,
is_public,
@ -45,10 +41,6 @@ impl Field {
}
}
pub fn node_id(&self) -> NodeId {
self.node_id
}
pub fn declared_name(&self) -> &str {
&self.declared_name
}
@ -223,18 +215,4 @@ impl Field {
None => Ok(()),
}
}
pub fn lower_to_ir_field(
&self,
nodes_to_symbols: &NodesToSymbols,
symbols_to_types: &SymbolsToTypes,
) -> IrField {
let symbol = nodes_to_symbols.get(&self.node_id).unwrap();
let field_type = symbols_to_types.get(symbol).unwrap();
IrField::new(
self.declared_name.clone(),
symbol.unwrap_field_symbol().field_index(), // todo: this needs to be stored NOT in the symbol
field_type.clone(),
)
}
}

View File

@ -18,7 +18,6 @@ use crate::source_range::SourceRange;
use crate::symbol::Symbol;
use crate::symbol::class_symbol::ClassSymbol;
use crate::symbol::function_symbol::FunctionSymbol;
use crate::symbol::parameter_symbol::ParameterSymbol;
use crate::symbol_table::SymbolTable;
use crate::type_info::TypeInfo;
use crate::types_table::TypesTable;
@ -34,8 +33,7 @@ pub struct Function {
parameters: Vec<Parameter>,
return_type: Option<TypeUse>,
statements: Vec<Statement>,
container_scope_id: Option<usize>,
function_scope_id: Option<usize>,
scope_id: Option<usize>,
}
impl Function {
@ -56,8 +54,7 @@ impl Function {
parameters,
return_type,
statements,
container_scope_id: None,
function_scope_id: None,
scope_id: None,
}
}
@ -74,10 +71,9 @@ impl Function {
}
pub fn init_scopes(&mut self, symbol_table: &mut SymbolTable, container_scope: usize) {
self.container_scope_id = Some(container_scope);
self.scope_id = Some(container_scope);
let function_scope =
symbol_table.push_function_scope(&format!("function_scope({})", self.declared_name));
self.function_scope_id = Some(function_scope);
for parameter in &mut self.parameters {
parameter.init_scopes(symbol_table, function_scope);
@ -106,17 +102,6 @@ impl Function {
let mut all_symbols: Vec<Symbol> = vec![];
let mut parameter_symbols = Vec::new();
if is_method {
let self_parameter_symbol = Rc::new(ParameterSymbol::new(
&Rc::from("self"),
None,
self.function_scope_id.unwrap(),
));
parameter_symbols.push(self_parameter_symbol.clone());
all_symbols.push(Symbol::Parameter(self_parameter_symbol))
}
collect_parameter_symbols_into(&self.parameters, &mut all_symbols, &mut parameter_symbols);
let function_symbol = Rc::new(FunctionSymbol::new(
@ -125,7 +110,7 @@ impl Function {
fqn_context.resolve(self.declared_name()),
false,
is_method,
self.container_scope_id.unwrap(),
self.scope_id.unwrap(),
parameter_symbols,
));
all_symbols.push(Symbol::Function(function_symbol.clone()));
@ -254,7 +239,7 @@ impl Function {
#[deprecated]
pub fn gather_types(&self, symbol_table: &SymbolTable, types_table: &mut TypesTable) {
let function_symbol = symbol_table
.get_function_symbol_owned(self.container_scope_id.unwrap(), self.declared_name())
.get_function_symbol_owned(self.scope_id.unwrap(), self.declared_name())
.unwrap();
// self type (the signature)
@ -351,7 +336,7 @@ impl Function {
) -> Result<(), Vec<Diagnostic>> {
let mut diagnostics = vec![];
let function_symbol = symbol_table
.get_function_symbol(self.container_scope_id.unwrap(), self.declared_name())
.get_function_symbol(self.scope_id.unwrap(), self.declared_name())
.unwrap();
// parameters
@ -423,7 +408,6 @@ impl Function {
}
}
#[deprecated]
pub fn to_ir(
&self,
symbol_table: &SymbolTable,
@ -432,7 +416,7 @@ impl Function {
) -> IrFunction {
let mut builder = IrBuilder::new();
let function_symbol = symbol_table
.get_function_symbol(self.container_scope_id.unwrap(), self.declared_name())
.get_function_symbol(self.scope_id.unwrap(), self.declared_name())
.unwrap();
// parameters
@ -457,7 +441,7 @@ impl Function {
)
}
pub fn lower_to_ir_static(
pub fn lower_static(
&self,
nodes_to_symbols: &NodesToSymbols,
symbols_to_types: &SymbolsToTypes,

View File

@ -52,7 +52,7 @@ impl Parameter {
pub fn make_symbol(&self) -> ParameterSymbol {
ParameterSymbol::new(
&self.declared_name,
Some(self.declared_name_source_range.clone()),
self.declared_name_source_range.clone(),
self.scope_id.unwrap(),
)
}

View File

@ -848,7 +848,6 @@ impl<'a> Parser<'a> {
if let Some(ctor_keyword) = ctor_keyword {
let constructor = Constructor::new(
self.next_node_id(),
is_public,
SourceRange::new(ctor_keyword.start(), ctor_keyword.end()),
parameters,
@ -900,7 +899,6 @@ impl<'a> Parser<'a> {
if let Some(identifier) = identifier {
let field = Field::new(
self.next_node_id(),
self.token_text(&identifier),
SourceRange::new(identifier.start(), identifier.end()),
is_public,

View File

@ -36,7 +36,7 @@ impl ExpressibleSymbol {
Some(function_symbol.declared_name_source_range())
}
ExpressibleSymbol::Parameter(parameter_symbol) => {
parameter_symbol.declared_name_source_range()
Some(parameter_symbol.declared_name_source_range())
}
ExpressibleSymbol::Variable(variable_symbol) => {
Some(variable_symbol.declared_name_source_range())

View File

@ -72,7 +72,9 @@ impl Symbol {
Some(constructor_symbol.declared_name_source_range())
}
Symbol::Function(function_symbol) => Some(function_symbol.declared_name_source_range()),
Symbol::Parameter(parameter_symbol) => parameter_symbol.declared_name_source_range(),
Symbol::Parameter(parameter_symbol) => {
Some(parameter_symbol.declared_name_source_range())
}
Symbol::Variable(variable_symbol) => Some(variable_symbol.declared_name_source_range()),
}
}
@ -117,32 +119,4 @@ impl Symbol {
_ => panic!(),
}
}
pub fn unwrap_class_symbol(&self) -> &Rc<ClassSymbol> {
match self {
Symbol::Class(class_symbol) => class_symbol,
_ => panic!(),
}
}
pub fn unwrap_field_symbol(&self) -> &Rc<FieldSymbol> {
match self {
Symbol::Field(field_symbol) => field_symbol,
_ => panic!(),
}
}
pub fn unwrap_parameter_symbol(&self) -> &Rc<ParameterSymbol> {
match self {
Symbol::Parameter(parameter_symbol) => parameter_symbol,
_ => panic!(),
}
}
pub fn unwrap_constructor_symbol(&self) -> &Rc<ConstructorSymbol> {
match self {
Symbol::Constructor(constructor_symbol) => constructor_symbol,
_ => panic!(),
}
}
}

View File

@ -1,17 +1,18 @@
use crate::source_range::SourceRange;
use crate::symbol::Symbol;
use std::hash::{Hash, Hasher};
use std::rc::Rc;
pub struct ParameterSymbol {
declared_name: Rc<str>,
declared_name_source_range: Option<SourceRange>,
declared_name_source_range: SourceRange,
scope_id: usize,
}
impl ParameterSymbol {
pub fn new(
declared_name: &Rc<str>,
declared_name_source_range: Option<SourceRange>,
declared_name_source_range: SourceRange,
scope_id: usize,
) -> Self {
Self {
@ -29,8 +30,8 @@ impl ParameterSymbol {
self.declared_name.clone()
}
pub fn declared_name_source_range(&self) -> Option<&SourceRange> {
self.declared_name_source_range.as_ref()
pub fn declared_name_source_range(&self) -> &SourceRange {
&self.declared_name_source_range
}
pub fn scope_id(&self) -> usize {