Skip to content

Commit

Permalink
WIP: better generics
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickpeterse committed Sep 7, 2023
1 parent 97c7fb3 commit 0d4d064
Show file tree
Hide file tree
Showing 14 changed files with 1,621 additions and 159 deletions.
17 changes: 10 additions & 7 deletions compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use crate::config::{Config, SOURCE, SOURCE_EXT, TESTS};
use crate::hir;
use crate::linker::link;
use crate::llvm;
use crate::mir::passes as mir;
use crate::mir::printer::to_dot;
use crate::mir::{passes as mir, Mir};
use crate::mir::specialize::Specialize;
use crate::mir::Mir;
use crate::modules_parser::{ModulesParser, ParsedModule};
use crate::state::State;
use crate::type_check::define_types::{
Expand Down Expand Up @@ -131,12 +133,12 @@ impl Compiler {
}

let mut mir = Mir::new();
let state = &mut self.state;

mir::check_global_limits(&mut self.state)
.map_err(CompileError::Internal)?;
mir::check_global_limits(state).map_err(CompileError::Internal)?;

if mir::DefineConstants::run_all(&mut self.state, &mut mir, &modules)
&& mir::LowerToMir::run_all(&mut self.state, &mut mir, modules)
if mir::DefineConstants::run_all(state, &mut mir, &modules)
&& mir::LowerToMir::run_all(state, &mut mir, modules)
{
Ok(mir)
} else {
Expand Down Expand Up @@ -185,8 +187,9 @@ impl Compiler {
}

fn optimise_mir(&mut self, mir: &mut Mir) {
mir::ExpandDrop::run_all(&self.state.db, mir);
mir::ExpandReference::run_all(&self.state.db, mir);
Specialize::run_all(&mut self.state, mir);
mir::ExpandDrop::run_all(&self.state.db, mir); // TODO: remove in favour of specialization
mir::ExpandReference::run_all(&self.state.db, mir); // TODO: remove in favour of specialization
mir::clean_up_basic_blocks(mir);
}

Expand Down
8 changes: 4 additions & 4 deletions compiler/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ pub(crate) struct DefineVariant {
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct AssignInstanceLiteralField {
pub(crate) struct AssignClassLiteralField {
pub(crate) resolved_type: types::TypeRef,
pub(crate) field_id: Option<types::FieldId>,
pub(crate) field: Field,
Expand All @@ -349,7 +349,7 @@ pub(crate) struct ClassLiteral {
pub(crate) class_id: Option<types::ClassId>,
pub(crate) resolved_type: types::TypeRef,
pub(crate) class_name: Constant,
pub(crate) fields: Vec<AssignInstanceLiteralField>,
pub(crate) fields: Vec<AssignClassLiteralField>,
pub(crate) location: SourceLocation,
}

Expand Down Expand Up @@ -2794,7 +2794,7 @@ impl<'a> LowerToHir<'a> {
fields: node
.fields
.into_iter()
.map(|n| AssignInstanceLiteralField {
.map(|n| AssignClassLiteralField {
resolved_type: types::TypeRef::Unknown,
field_id: None,
field: self.field(n.field),
Expand Down Expand Up @@ -5923,7 +5923,7 @@ mod tests {
name: "A".to_string(),
location: cols(8, 8)
},
fields: vec![AssignInstanceLiteralField {
fields: vec![AssignClassLiteralField {
resolved_type: types::TypeRef::Unknown,
field_id: None,
field: Field {
Expand Down
15 changes: 8 additions & 7 deletions compiler/src/llvm/layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use inkwell::AddressSpace;
use std::cmp::max;
use std::collections::HashMap;
use types::{
ClassId, MethodId, MethodSource, BOOLEAN_ID, BYTE_ARRAY_ID, CALL_METHOD,
CHANNEL_ID, DROPPER_METHOD, FLOAT_ID, INT_ID, NIL_ID,
ClassId, MethodId, MethodSource, TraitId, BOOL_ID, BYTE_ARRAY_ID,
CALL_METHOD, CHANNEL_ID, DROPPER_METHOD, FLOAT_ID, INT_ID, NIL_ID,
};

/// The size of an object header.
Expand Down Expand Up @@ -180,12 +180,13 @@ impl<'ctx> Layouts<'ctx> {
//
// This information is defined first so we can update the `collision`
// flag when generating this information for method implementations.
for mir_trait in mir.traits.values() {
for method in mir_trait
.id
for idx in 0..db.number_of_traits() {
let id = TraitId(idx as _);

for method in id
.required_methods(db)
.into_iter()
.chain(mir_trait.id.default_methods(db))
.chain(id.default_methods(db))
{
let name = method.name(db);
let hash = method_hasher.hash(name);
Expand Down Expand Up @@ -249,7 +250,7 @@ impl<'ctx> Layouts<'ctx> {
header,
context.f64_type().into(),
),
BOOLEAN_ID | NIL_ID => {
BOOL_ID | NIL_ID => {
let typ = context.opaque_struct(&name);

typ.set_body(&[header.into()], false);
Expand Down
Loading

0 comments on commit 0d4d064

Please sign in to comment.