Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uses Hindley-Milner base for type inference. #271

Merged
merged 35 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/base_db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[derive(Default)]
#[salsa::db(hir::Jar)]
#[salsa::db(hir::Jar, hir_ty::Jar)]
pub struct SalsaDatabase {
storage: salsa::Storage<Self>,
}
Expand Down
17 changes: 9 additions & 8 deletions crates/codegen_llvm/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ impl<'a, 'ctx> BodyCodegen<'a, 'ctx> {
// register alloc locals.
for (idx, local) in self.body.locals.iter() {
let ty: BasicTypeEnum = match local.ty {
hir_ty::ResolvedType::Unit => self.codegen.unit_type().into(),
hir_ty::ResolvedType::Integer => self.codegen.integer_type().into(),
hir_ty::ResolvedType::String => self.codegen.string_type().into(),
hir_ty::ResolvedType::Bool => self.codegen.bool_type().into(),
hir_ty::ResolvedType::Char => todo!(),
hir_ty::ResolvedType::Never => todo!(),
hir_ty::ResolvedType::Function(_) => todo!(),
hir_ty::ResolvedType::Unknown => unreachable!(),
hir_ty::Monotype::Unit => self.codegen.unit_type().into(),
hir_ty::Monotype::Integer => self.codegen.integer_type().into(),
hir_ty::Monotype::String => self.codegen.string_type().into(),
hir_ty::Monotype::Bool => self.codegen.bool_type().into(),
hir_ty::Monotype::Char => todo!(),
hir_ty::Monotype::Never => todo!(),
hir_ty::Monotype::Function(_) => todo!(),
hir_ty::Monotype::Variable(_) => unreachable!(""),
hir_ty::Monotype::Unknown => unreachable!(),
};

let local_ptr = self
Expand Down
20 changes: 10 additions & 10 deletions crates/codegen_llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,16 @@ impl<'a, 'ctx> Codegen<'a, 'ctx> {
body.params
.iter()
.map(|(_, param)| match param.ty {
hir_ty::ResolvedType::Integer => {
hir_ty::Monotype::Integer => {
BasicMetadataTypeEnum::IntType(self.context.i64_type())
}
hir_ty::ResolvedType::String => self
hir_ty::Monotype::String => self
.context
.i8_type()
.vec_type(1)
.ptr_type(AddressSpace::default())
.into(),
hir_ty::ResolvedType::Bool => self.context.bool_type().into(),
hir_ty::Monotype::Bool => self.context.bool_type().into(),
_ => unimplemented!(),
})
.collect::<Vec<_>>()
Expand Down Expand Up @@ -197,22 +197,22 @@ impl<'a, 'ctx> Codegen<'a, 'ctx> {
fn gen_function_signatures(&mut self, db: &dyn hir::HirMasterDatabase) {
for (idx, body) in self.mir_result.ref_bodies() {
let params = self.body_to_params(body);
let return_type = body.locals[body.return_local].ty;
let return_type = body.locals[body.return_local].ty.clone();

let fn_ty: FunctionType<'ctx> = match return_type {
hir_ty::ResolvedType::Unit => {
hir_ty::Monotype::Unit => {
let ty = self.context.struct_type(&[], false);
ty.fn_type(&params, false)
}
hir_ty::ResolvedType::Integer => {
hir_ty::Monotype::Integer => {
let ty = self.context.i64_type();
ty.fn_type(&params, false)
}
hir_ty::ResolvedType::String => {
hir_ty::Monotype::String => {
let ty = self.string_type();
ty.fn_type(&params, false)
}
hir_ty::ResolvedType::Bool => {
hir_ty::Monotype::Bool => {
let ty = self.context.bool_type();
ty.fn_type(&params, false)
}
Expand Down Expand Up @@ -256,12 +256,12 @@ mod tests {
fn lower(
fixture: &str,
) -> (
hir::TestingDatabase,
hir_ty::TestingDatabase,
hir::Pods,
hir_ty::TyLowerResult,
mir::LowerResult,
) {
let db = hir::TestingDatabase::default();
let db = hir_ty::TestingDatabase::default();
let mut source_db = FixtureDatabase::new(&db, fixture);

let pods = hir::parse_pods(&db, "/main.nail", &mut source_db);
Expand Down
4 changes: 3 additions & 1 deletion crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub use db::{HirMasterDatabase, Jar};
pub use input::{FixtureDatabase, NailFile, SourceDatabase, SourceDatabaseTrait};
pub use item::{Function, Item, Module, ModuleKind, Param, Type, UseItem};
use name_resolver::resolve_symbols;
pub use name_resolver::{ResolutionMap, ResolutionStatus};
pub use name_resolver::{ModuleScopeOrigin, ResolutionMap, ResolutionStatus};
pub use testing::TestingDatabase;

/// ビルド対象全体を表します。
Expand Down Expand Up @@ -90,6 +90,8 @@ impl Pod {
}

/// ファイルの登録順の昇順でHIR構築結果を返します。
///
/// ルートファイルは含まれません。
pub fn get_hir_files_order_registration_asc(&self) -> Vec<(NailFile, &HirFile)> {
let mut lower_results = vec![];
for file in &self.registration_order {
Expand Down
Loading