Code gen thunks for top-level module decls

This commit is contained in:
Richard Feldman 2020-04-30 20:33:02 -04:00
parent 072cf4e844
commit 5c31369072
6 changed files with 137 additions and 68 deletions

View file

@ -16,7 +16,7 @@ use roc_gen::llvm::build::{
use roc_gen::llvm::convert::basic_type_from_layout;
use roc_load::file::{LoadedModule, LoadingProblem};
use roc_module::symbol::Symbol;
use roc_mono::expr::{Expr, Procs};
use roc_mono::expr::{Expr, PartialProc, Procs};
use roc_mono::layout::Layout;
use std::time::SystemTime;
@ -275,17 +275,22 @@ fn gen(
}
}
let mut decls_by_id = loaded.declarations_by_id;
let home_decls = decls_by_id
.remove(&loaded.module_id)
.expect("Root module ID not found in loaded declarations_by_id");
// We use a loop label here so we can break all the way out of a nested
// loop inside DeclareRec if we find the expr there.
//
// https://doc.rust-lang.org/1.30.0/book/first-edition/loops.html#loop-labels
'find_expr: for decl in loaded.declarations {
'find_expr: for decl in home_decls.iter() {
use roc_can::def::Declaration::*;
match decl {
Declare(def) => {
if def.pattern_vars.contains_key(&main_symbol) {
main_expr = Some(def.loc_expr);
main_expr = Some(def.loc_expr.clone());
break 'find_expr;
}
@ -294,7 +299,7 @@ fn gen(
DeclareRec(defs) => {
for def in defs {
if def.pattern_vars.contains_key(&main_symbol) {
main_expr = Some(def.loc_expr);
main_expr = Some(def.loc_expr.clone());
break 'find_expr;
}
@ -347,9 +352,52 @@ fn gen(
ptr_bytes,
};
let mut procs = Procs::default();
// TODO remove this
decls_by_id.insert(loaded.module_id, home_decls);
// Populate Procs from decls
for (_, mut decls) in decls_by_id.drain() {
for decl in decls.drain(..) {
use roc_can::def::Declaration::*;
use roc_can::expr::Expr::*;
use roc_can::pattern::Pattern::*;
match decl {
Declare(def) => match def.loc_pattern.value {
Identifier(symbol) => {
let proc = match def.loc_expr.value {
Closure(_, _, _, _, _) => {
todo!("TODO register PartialProc for Closure");
}
body => PartialProc {
annotation: def.expr_var,
// This is a 0-arity thunk, so it has no arguments.
patterns: bumpalo::collections::Vec::new_in(arena),
body,
},
};
procs.user_defined.insert(symbol, proc);
procs.module_decls.insert(symbol);
}
other => {
todo!("TODO gracefully handle Declare({:?})", other);
}
},
DeclareRec(_defs) => {
todo!("TODO support DeclareRec");
}
InvalidCycle(_loc_idents, _regions) => {
todo!("TODO handle InvalidCycle");
}
}
}
}
let mut ident_ids = env.interns.all_ident_ids.remove(&home).unwrap();
// Populate Procs and get the low-level Expr from the canonical Expr
// Populate Procs further and get the low-level Expr from the canonical Expr
let mut mono_problems = std::vec::Vec::new();
let main_body = Expr::new(
&arena,