mirror of
https://github.com/edg-l/edlang.git
synced 2024-11-23 00:18:24 +00:00
handle unit return
This commit is contained in:
parent
497072760d
commit
db6b6e980b
|
@ -136,19 +136,26 @@ fn compile_module(ctx: &ModuleCompileCtx, module: &ir::ModuleBody) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile_fn_signature(ctx: &ModuleCompileCtx, body: &ir::Body) {
|
fn compile_fn_signature(ctx: &ModuleCompileCtx, body: &ir::Body) {
|
||||||
let (args, ret_type) = { (body.get_args(), body.ret_type.clone().unwrap()) };
|
let name = ctx.ctx.symbols.get(&body.def_id).unwrap();
|
||||||
|
info!("compiling fn sig: {}", name);
|
||||||
|
|
||||||
|
let (args, ret_type) = { (body.get_args(), body.ret_type.clone()) };
|
||||||
|
|
||||||
let args: Vec<BasicMetadataTypeEnum> = args
|
let args: Vec<BasicMetadataTypeEnum> = args
|
||||||
.iter()
|
.iter()
|
||||||
.map(|x| compile_basic_type(ctx, &x.ty).into())
|
.map(|x| compile_basic_type(ctx, &x.ty).into())
|
||||||
.collect();
|
.collect();
|
||||||
let ret_type = compile_basic_type(ctx, &ret_type);
|
// let ret_type = compile_basic_type(ctx, &ret_type);
|
||||||
let name = ctx.ctx.symbols.get(&body.def_id).unwrap();
|
|
||||||
info!("compiling fn sig: {}", name);
|
let fn_type = if let ir::TypeKind::Unit = ret_type.kind {
|
||||||
|
ctx.ctx.context.void_type().fn_type(&args, false)
|
||||||
|
} else {
|
||||||
|
compile_basic_type(ctx, &ret_type).fn_type(&args, false)
|
||||||
|
};
|
||||||
|
|
||||||
ctx.module.add_function(
|
ctx.module.add_function(
|
||||||
name,
|
name,
|
||||||
ret_type.fn_type(&args, false),
|
fn_type,
|
||||||
Some(if body.is_extern {
|
Some(if body.is_extern {
|
||||||
inkwell::module::Linkage::AvailableExternally
|
inkwell::module::Linkage::AvailableExternally
|
||||||
} else if body.is_pub {
|
} else if body.is_pub {
|
||||||
|
@ -381,7 +388,7 @@ fn compile_type<'a>(
|
||||||
.functions
|
.functions
|
||||||
.get(def_id)
|
.get(def_id)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
(fn_body.get_args(), fn_body.ret_type.clone().unwrap())
|
(fn_body.get_args(), fn_body.ret_type.clone())
|
||||||
};
|
};
|
||||||
|
|
||||||
let args: Vec<BasicMetadataTypeEnum> = args
|
let args: Vec<BasicMetadataTypeEnum> = args
|
||||||
|
|
|
@ -36,7 +36,7 @@ pub struct Body {
|
||||||
pub def_id: DefId,
|
pub def_id: DefId,
|
||||||
pub is_pub: bool,
|
pub is_pub: bool,
|
||||||
pub is_extern: bool,
|
pub is_extern: bool,
|
||||||
pub ret_type: Option<TypeInfo>,
|
pub ret_type: TypeInfo,
|
||||||
pub locals: SmallVec<[Local; 4]>,
|
pub locals: SmallVec<[Local; 4]>,
|
||||||
pub blocks: SmallVec<[BasicBlock; 8]>,
|
pub blocks: SmallVec<[BasicBlock; 8]>,
|
||||||
pub fn_span: Span,
|
pub fn_span: Span,
|
||||||
|
|
|
@ -122,7 +122,14 @@ fn lower_function(
|
||||||
|
|
||||||
let body = ir::Body {
|
let body = ir::Body {
|
||||||
def_id,
|
def_id,
|
||||||
ret_type: func.return_type.as_ref().map(|x| lower_type(&mut ctx, x)),
|
ret_type: func
|
||||||
|
.return_type
|
||||||
|
.as_ref()
|
||||||
|
.map(|x| lower_type(&mut ctx, x))
|
||||||
|
.unwrap_or_else(|| TypeInfo {
|
||||||
|
span: None,
|
||||||
|
kind: ir::TypeKind::Unit,
|
||||||
|
}),
|
||||||
locals: Default::default(),
|
locals: Default::default(),
|
||||||
blocks: Default::default(),
|
blocks: Default::default(),
|
||||||
fn_span: func.span,
|
fn_span: func.span,
|
||||||
|
@ -543,7 +550,7 @@ fn lower_value(
|
||||||
fn lower_return(builder: &mut BodyBuilder, info: &ast::ReturnStmt) {
|
fn lower_return(builder: &mut BodyBuilder, info: &ast::ReturnStmt) {
|
||||||
let ret_type = builder.body.ret_type.clone();
|
let ret_type = builder.body.ret_type.clone();
|
||||||
if let Some(value) = &info.value {
|
if let Some(value) = &info.value {
|
||||||
let rvalue = lower_expr(builder, value, ret_type.as_ref());
|
let rvalue = lower_expr(builder, value, Some(&ret_type));
|
||||||
let ret_local = builder.ret_local.unwrap();
|
let ret_local = builder.ret_local.unwrap();
|
||||||
|
|
||||||
builder.statements.push(Statement {
|
builder.statements.push(Statement {
|
||||||
|
|
|
@ -5,4 +5,10 @@ mod Main {
|
||||||
x = 4;
|
x = 4;
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn a() {
|
||||||
|
let mut x: i32 = 2;
|
||||||
|
x = 4;
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue