Add AST -> MIR processing for associated functions
This commit is contained in:
		
							parent
							
								
									09f1784810
								
							
						
					
					
						commit
						ab94bd7df0
					
				| @ -1,7 +1,7 @@ | |||||||
| use std::path::PathBuf; | use std::path::PathBuf; | ||||||
| 
 | 
 | ||||||
| use crate::{ | use crate::{ | ||||||
|     ast::{self}, |     ast::{self, FunctionCallExpression}, | ||||||
|     mir::{ |     mir::{ | ||||||
|         self, CustomTypeKey, ModuleMap, NamedVariableRef, ReturnKind, SourceModuleId, StmtKind, StructField, |         self, CustomTypeKey, ModuleMap, NamedVariableRef, ReturnKind, SourceModuleId, StmtKind, StructField, | ||||||
|         StructType, WhileStatement, |         StructType, WhileStatement, | ||||||
| @ -21,6 +21,7 @@ impl mir::Context { | |||||||
| impl ast::Module { | impl ast::Module { | ||||||
|     pub fn process(self, module_id: SourceModuleId) -> mir::Module { |     pub fn process(self, module_id: SourceModuleId) -> mir::Module { | ||||||
|         let mut imports = Vec::new(); |         let mut imports = Vec::new(); | ||||||
|  |         let mut associated_functions = Vec::new(); | ||||||
|         let mut functions = Vec::new(); |         let mut functions = Vec::new(); | ||||||
|         let mut typedefs = Vec::new(); |         let mut typedefs = Vec::new(); | ||||||
|         let mut binops = Vec::new(); |         let mut binops = Vec::new(); | ||||||
| @ -31,44 +32,7 @@ impl ast::Module { | |||||||
|                 Import(import) => { |                 Import(import) => { | ||||||
|                     imports.push(mir::Import(import.0.clone(), import.1.as_meta(module_id))); |                     imports.push(mir::Import(import.0.clone(), import.1.as_meta(module_id))); | ||||||
|                 } |                 } | ||||||
|                 FunctionDefinition(ast::FunctionDefinition(signature, is_pub, block, range)) => { |                 FunctionDefinition(function_def) => functions.push(function_def.into_mir(module_id)), | ||||||
|                     let mut params = Vec::new(); |  | ||||||
|                     match &signature.self_kind { |  | ||||||
|                         ast::SelfKind::Borrow(type_kind) => params.push(( |  | ||||||
|                             "self".to_owned(), |  | ||||||
|                             mir::TypeKind::Borrow(Box::new(type_kind.into_mir(module_id)), false), |  | ||||||
|                         )), |  | ||||||
|                         ast::SelfKind::MutBorrow(type_kind) => params.push(( |  | ||||||
|                             "self".to_owned(), |  | ||||||
|                             mir::TypeKind::Borrow(Box::new(type_kind.into_mir(module_id)), true), |  | ||||||
|                         )), |  | ||||||
|                         ast::SelfKind::None => {} |  | ||||||
|                     } |  | ||||||
| 
 |  | ||||||
|                     params.extend( |  | ||||||
|                         signature |  | ||||||
|                             .params |  | ||||||
|                             .iter() |  | ||||||
|                             .cloned() |  | ||||||
|                             .map(|p| (p.0, p.1 .0.into_mir(module_id))), |  | ||||||
|                     ); |  | ||||||
|                     let def = mir::FunctionDefinition { |  | ||||||
|                         name: signature.name.clone(), |  | ||||||
|                         is_pub: *is_pub, |  | ||||||
|                         is_imported: false, |  | ||||||
|                         return_type: signature |  | ||||||
|                             .return_type |  | ||||||
|                             .clone() |  | ||||||
|                             .map(|r| r.0.into_mir(module_id)) |  | ||||||
|                             .unwrap_or(mir::TypeKind::Void), |  | ||||||
|                         parameters: params, |  | ||||||
|                         kind: mir::FunctionDefinitionKind::Local( |  | ||||||
|                             block.into_mir(module_id), |  | ||||||
|                             (*range).as_meta(module_id), |  | ||||||
|                         ), |  | ||||||
|                     }; |  | ||||||
|                     functions.push(def); |  | ||||||
|                 } |  | ||||||
|                 ExternFunction(signature) => { |                 ExternFunction(signature) => { | ||||||
|                     let def = mir::FunctionDefinition { |                     let def = mir::FunctionDefinition { | ||||||
|                         name: signature.name.clone(), |                         name: signature.name.clone(), | ||||||
| @ -135,7 +99,11 @@ impl ast::Module { | |||||||
|                         exported: false, |                         exported: false, | ||||||
|                     }); |                     }); | ||||||
|                 } |                 } | ||||||
|                 AssociatedFunction(_, function_definition) => todo!(), |                 AssociatedFunction(ty, function_definition) => { | ||||||
|  |                     for function_def in function_definition { | ||||||
|  |                         associated_functions.push((ty.0.into_mir(module_id), function_def.into_mir(module_id))); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| @ -144,6 +112,7 @@ impl ast::Module { | |||||||
|             module_id: module_id, |             module_id: module_id, | ||||||
|             binop_defs: binops, |             binop_defs: binops, | ||||||
|             imports, |             imports, | ||||||
|  |             associated_functions, | ||||||
|             functions, |             functions, | ||||||
|             path: self.path.clone(), |             path: self.path.clone(), | ||||||
|             is_main: self.is_main, |             is_main: self.is_main, | ||||||
| @ -153,6 +122,45 @@ impl ast::Module { | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | impl ast::FunctionDefinition { | ||||||
|  |     pub fn into_mir(&self, module_id: SourceModuleId) -> mir::FunctionDefinition { | ||||||
|  |         let &ast::FunctionDefinition(signature, is_pub, block, range) = &self; | ||||||
|  | 
 | ||||||
|  |         let mut params = Vec::new(); | ||||||
|  |         match &signature.self_kind { | ||||||
|  |             ast::SelfKind::Borrow(type_kind) => params.push(( | ||||||
|  |                 "self".to_owned(), | ||||||
|  |                 mir::TypeKind::Borrow(Box::new(type_kind.into_mir(module_id)), false), | ||||||
|  |             )), | ||||||
|  |             ast::SelfKind::MutBorrow(type_kind) => params.push(( | ||||||
|  |                 "self".to_owned(), | ||||||
|  |                 mir::TypeKind::Borrow(Box::new(type_kind.into_mir(module_id)), true), | ||||||
|  |             )), | ||||||
|  |             ast::SelfKind::None => {} | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         params.extend( | ||||||
|  |             signature | ||||||
|  |                 .params | ||||||
|  |                 .iter() | ||||||
|  |                 .cloned() | ||||||
|  |                 .map(|p| (p.0, p.1 .0.into_mir(module_id))), | ||||||
|  |         ); | ||||||
|  |         mir::FunctionDefinition { | ||||||
|  |             name: signature.name.clone(), | ||||||
|  |             is_pub: *is_pub, | ||||||
|  |             is_imported: false, | ||||||
|  |             return_type: signature | ||||||
|  |                 .return_type | ||||||
|  |                 .clone() | ||||||
|  |                 .map(|r| r.0.into_mir(module_id)) | ||||||
|  |                 .unwrap_or(mir::TypeKind::Void), | ||||||
|  |             parameters: params, | ||||||
|  |             kind: mir::FunctionDefinitionKind::Local(block.into_mir(module_id), (range).as_meta(module_id)), | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
| impl ast::Block { | impl ast::Block { | ||||||
|     pub fn into_mir(&self, module_id: SourceModuleId) -> mir::Block { |     pub fn into_mir(&self, module_id: SourceModuleId) -> mir::Block { | ||||||
|         let mut mir_statements = Vec::new(); |         let mut mir_statements = Vec::new(); | ||||||
| @ -404,7 +412,15 @@ impl ast::Expression { | |||||||
|                     .map(|e| e.process(module_id)) |                     .map(|e| e.process(module_id)) | ||||||
|                     .collect(), |                     .collect(), | ||||||
|             ), |             ), | ||||||
|             ast::ExpressionKind::AssociatedFunctionCall(_, function_call_expression) => todo!(), |             ast::ExpressionKind::AssociatedFunctionCall(ty, fn_call_expr) => mir::ExprKind::AssociatedFunctionCall( | ||||||
|  |                 ty.0.into_mir(module_id), | ||||||
|  |                 mir::FunctionCall { | ||||||
|  |                     name: fn_call_expr.0.clone(), | ||||||
|  |                     return_type: mir::TypeKind::Vague(mir::VagueType::Unknown), | ||||||
|  |                     parameters: fn_call_expr.1.iter().map(|e| e.process(module_id)).collect(), | ||||||
|  |                     meta: fn_call_expr.2.as_meta(module_id), | ||||||
|  |                 }, | ||||||
|  |             ), | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|         mir::Expression(kind, self.1.as_meta(module_id)) |         mir::Expression(kind, self.1.as_meta(module_id)) | ||||||
|  | |||||||
| @ -5,10 +5,12 @@ use reid_lib::{ | |||||||
|     Block, |     Block, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| use crate::mir::{ | use mir::{ | ||||||
|     self, CustomTypeKey, FunctionCall, FunctionDefinitionKind, IfExpression, SourceModuleId, TypeKind, WhileStatement, |     CustomTypeKey, FunctionCall, FunctionDefinitionKind, IfExpression, SourceModuleId, TypeKind, WhileStatement, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | use crate::mir; | ||||||
|  | 
 | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub struct Allocator { | pub struct Allocator { | ||||||
|     allocations: Vec<Allocation>, |     allocations: Vec<Allocation>, | ||||||
| @ -100,7 +102,7 @@ impl mir::Statement { | |||||||
|         let mut allocated = Vec::new(); |         let mut allocated = Vec::new(); | ||||||
| 
 | 
 | ||||||
|         match &self.0 { |         match &self.0 { | ||||||
|             crate::mir::StmtKind::Let(named_variable_ref, _, expression) => { |             mir::StmtKind::Let(named_variable_ref, _, expression) => { | ||||||
|                 allocated.extend(expression.allocate(scope)); |                 allocated.extend(expression.allocate(scope)); | ||||||
|                 let allocation = scope |                 let allocation = scope | ||||||
|                     .block |                     .block | ||||||
| @ -115,15 +117,15 @@ impl mir::Statement { | |||||||
|                     allocation, |                     allocation, | ||||||
|                 )); |                 )); | ||||||
|             } |             } | ||||||
|             crate::mir::StmtKind::Set(lhs, rhs) => { |             mir::StmtKind::Set(lhs, rhs) => { | ||||||
|                 allocated.extend(lhs.allocate(scope)); |                 allocated.extend(lhs.allocate(scope)); | ||||||
|                 allocated.extend(rhs.allocate(scope)); |                 allocated.extend(rhs.allocate(scope)); | ||||||
|             } |             } | ||||||
|             crate::mir::StmtKind::Import(_) => {} |             mir::StmtKind::Import(_) => {} | ||||||
|             crate::mir::StmtKind::Expression(expression) => { |             mir::StmtKind::Expression(expression) => { | ||||||
|                 allocated.extend(expression.allocate(scope)); |                 allocated.extend(expression.allocate(scope)); | ||||||
|             } |             } | ||||||
|             crate::mir::StmtKind::While(WhileStatement { condition, block, .. }) => { |             mir::StmtKind::While(WhileStatement { condition, block, .. }) => { | ||||||
|                 allocated.extend(condition.allocate(scope)); |                 allocated.extend(condition.allocate(scope)); | ||||||
|                 allocated.extend(block.allocate(scope)); |                 allocated.extend(block.allocate(scope)); | ||||||
|             } |             } | ||||||
| @ -138,49 +140,50 @@ impl mir::Expression { | |||||||
|         let mut allocated = Vec::new(); |         let mut allocated = Vec::new(); | ||||||
| 
 | 
 | ||||||
|         match &self.0 { |         match &self.0 { | ||||||
|             crate::mir::ExprKind::Variable(_) => {} |             mir::ExprKind::Variable(_) => {} | ||||||
|             crate::mir::ExprKind::Indexed(expr, _, idx) => { |             mir::ExprKind::Indexed(expr, _, idx) => { | ||||||
|                 allocated.extend(expr.allocate(scope)); |                 allocated.extend(expr.allocate(scope)); | ||||||
|                 allocated.extend(idx.allocate(scope)); |                 allocated.extend(idx.allocate(scope)); | ||||||
|             } |             } | ||||||
|             crate::mir::ExprKind::Accessed(expression, _, _) => { |             mir::ExprKind::Accessed(expression, _, _) => { | ||||||
|                 allocated.extend(expression.allocate(scope)); |                 allocated.extend(expression.allocate(scope)); | ||||||
|             } |             } | ||||||
|             crate::mir::ExprKind::Array(expressions) => { |             mir::ExprKind::Array(expressions) => { | ||||||
|                 for expression in expressions { |                 for expression in expressions { | ||||||
|                     allocated.extend(expression.allocate(scope)); |                     allocated.extend(expression.allocate(scope)); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             crate::mir::ExprKind::Struct(_, items) => { |             mir::ExprKind::Struct(_, items) => { | ||||||
|                 for (_, expression) in items { |                 for (_, expression) in items { | ||||||
|                     allocated.extend(expression.allocate(scope)); |                     allocated.extend(expression.allocate(scope)); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             crate::mir::ExprKind::Literal(_) => {} |             mir::ExprKind::Literal(_) => {} | ||||||
|             crate::mir::ExprKind::BinOp(_, lhs, rhs, _) => { |             mir::ExprKind::BinOp(_, lhs, rhs, _) => { | ||||||
|                 allocated.extend(lhs.allocate(scope)); |                 allocated.extend(lhs.allocate(scope)); | ||||||
|                 allocated.extend(rhs.allocate(scope)); |                 allocated.extend(rhs.allocate(scope)); | ||||||
|             } |             } | ||||||
|             crate::mir::ExprKind::FunctionCall(FunctionCall { parameters, .. }) => { |             mir::ExprKind::FunctionCall(FunctionCall { parameters, .. }) => { | ||||||
|                 for param in parameters { |                 for param in parameters { | ||||||
|                     allocated.extend(param.allocate(scope)); |                     allocated.extend(param.allocate(scope)); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             crate::mir::ExprKind::If(IfExpression(cond, then_ex, else_ex)) => { |             mir::ExprKind::If(IfExpression(cond, then_ex, else_ex)) => { | ||||||
|                 allocated.extend(cond.allocate(scope)); |                 allocated.extend(cond.allocate(scope)); | ||||||
|                 allocated.extend(then_ex.allocate(scope)); |                 allocated.extend(then_ex.allocate(scope)); | ||||||
|                 if let Some(else_ex) = else_ex.as_ref() { |                 if let Some(else_ex) = else_ex.as_ref() { | ||||||
|                     allocated.extend(else_ex.allocate(scope)); |                     allocated.extend(else_ex.allocate(scope)); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             crate::mir::ExprKind::Block(block) => { |             mir::ExprKind::Block(block) => { | ||||||
|                 allocated.extend(block.allocate(scope)); |                 allocated.extend(block.allocate(scope)); | ||||||
|             } |             } | ||||||
|             crate::mir::ExprKind::Borrow(_, _) => {} |             mir::ExprKind::Borrow(_, _) => {} | ||||||
|             crate::mir::ExprKind::Deref(_) => {} |             mir::ExprKind::Deref(_) => {} | ||||||
|             crate::mir::ExprKind::CastTo(expression, _) => { |             mir::ExprKind::CastTo(expression, _) => { | ||||||
|                 allocated.extend(expression.allocate(scope)); |                 allocated.extend(expression.allocate(scope)); | ||||||
|             } |             } | ||||||
|  |             mir::ExprKind::AssociatedFunctionCall(type_kind, function_call) => todo!(), | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         allocated |         allocated | ||||||
|  | |||||||
| @ -1217,6 +1217,7 @@ impl mir::Expression { | |||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |             mir::ExprKind::AssociatedFunctionCall(..) => todo!(), | ||||||
|         }; |         }; | ||||||
|         if let Some(value) = &value { |         if let Some(value) = &value { | ||||||
|             value.instr().maybe_location(&mut scope.block, location); |             value.instr().maybe_location(&mut scope.block, location); | ||||||
|  | |||||||
| @ -272,6 +272,11 @@ impl Display for ExprKind { | |||||||
|             ExprKind::Borrow(var_ref, true) => write!(f, "&mut {}", var_ref), |             ExprKind::Borrow(var_ref, true) => write!(f, "&mut {}", var_ref), | ||||||
|             ExprKind::Deref(var_ref) => write!(f, "*{}", var_ref), |             ExprKind::Deref(var_ref) => write!(f, "*{}", var_ref), | ||||||
|             ExprKind::CastTo(expression, type_kind) => write!(f, "{} as {}", expression, type_kind), |             ExprKind::CastTo(expression, type_kind) => write!(f, "{} as {}", expression, type_kind), | ||||||
|  |             ExprKind::AssociatedFunctionCall(type_kind, function_call) => { | ||||||
|  |                 Display::fmt(type_kind, f)?; | ||||||
|  |                 write!(f, "::")?; | ||||||
|  |                 Display::fmt(function_call, f) | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -443,6 +443,7 @@ impl Expression { | |||||||
|                 }, |                 }, | ||||||
|                 Err(_) => Ok((ReturnKind::Soft, type_kind.clone())), |                 Err(_) => Ok((ReturnKind::Soft, type_kind.clone())), | ||||||
|             }, |             }, | ||||||
|  |             AssociatedFunctionCall(type_kind, function_call) => todo!(), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -461,6 +462,7 @@ impl Expression { | |||||||
|             ExprKind::FunctionCall(_) => None, |             ExprKind::FunctionCall(_) => None, | ||||||
|             ExprKind::If(_) => None, |             ExprKind::If(_) => None, | ||||||
|             ExprKind::CastTo(expression, _) => expression.backing_var(), |             ExprKind::CastTo(expression, _) => expression.backing_var(), | ||||||
|  |             ExprKind::AssociatedFunctionCall(type_kind, function_call) => None, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -499,6 +501,7 @@ impl Expression { | |||||||
|             ExprKind::Borrow(_, _) => None, |             ExprKind::Borrow(_, _) => None, | ||||||
|             ExprKind::Deref(_) => None, |             ExprKind::Deref(_) => None, | ||||||
|             ExprKind::CastTo(expression, _) => expression.num_value()?, |             ExprKind::CastTo(expression, _) => expression.num_value()?, | ||||||
|  |             ExprKind::AssociatedFunctionCall(..) => None, | ||||||
|         }) |         }) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -258,6 +258,7 @@ pub enum ExprKind { | |||||||
|     Literal(Literal), |     Literal(Literal), | ||||||
|     BinOp(BinaryOperator, Box<Expression>, Box<Expression>, TypeKind), |     BinOp(BinaryOperator, Box<Expression>, Box<Expression>, TypeKind), | ||||||
|     FunctionCall(FunctionCall), |     FunctionCall(FunctionCall), | ||||||
|  |     AssociatedFunctionCall(TypeKind, FunctionCall), | ||||||
|     If(IfExpression), |     If(IfExpression), | ||||||
|     Block(Block), |     Block(Block), | ||||||
|     Borrow(NamedVariableRef, bool), |     Borrow(NamedVariableRef, bool), | ||||||
| @ -399,6 +400,7 @@ pub struct Module { | |||||||
|     pub name: String, |     pub name: String, | ||||||
|     pub module_id: SourceModuleId, |     pub module_id: SourceModuleId, | ||||||
|     pub imports: Vec<Import>, |     pub imports: Vec<Import>, | ||||||
|  |     pub associated_functions: Vec<(TypeKind, FunctionDefinition)>, | ||||||
|     pub functions: Vec<FunctionDefinition>, |     pub functions: Vec<FunctionDefinition>, | ||||||
|     pub typedefs: Vec<TypeDefinition>, |     pub typedefs: Vec<TypeDefinition>, | ||||||
|     pub binop_defs: Vec<BinopDefinition>, |     pub binop_defs: Vec<BinopDefinition>, | ||||||
|  | |||||||
| @ -724,6 +724,7 @@ impl Expression { | |||||||
|                 let expr = expression.typecheck(state, typerefs, HintKind::Default)?; |                 let expr = expression.typecheck(state, typerefs, HintKind::Default)?; | ||||||
|                 expr.resolve_ref(typerefs).cast_into(type_kind) |                 expr.resolve_ref(typerefs).cast_into(type_kind) | ||||||
|             } |             } | ||||||
|  |             ExprKind::AssociatedFunctionCall(type_kind, function_call) => todo!(), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -566,6 +566,7 @@ impl Expression { | |||||||
|                 expression.infer_types(state, type_refs)?; |                 expression.infer_types(state, type_refs)?; | ||||||
|                 Ok(type_refs.from_type(type_kind).unwrap()) |                 Ok(type_refs.from_type(type_kind).unwrap()) | ||||||
|             } |             } | ||||||
|  |             ExprKind::AssociatedFunctionCall(type_kind, function_call) => todo!(), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user