Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
Move function linking to its own pass (#150)
Browse files Browse the repository at this point in the history
* Specify that function is from trait

* Move linking to its own pass

* Fix llvm version in worlkflow
  • Loading branch information
gavrilikhin-d authored May 3, 2024
1 parent e2b0344 commit 88c0ef1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ jobs:
toolchain: nightly
default: true
- name: Install LLVM and Clang
uses: KyleMayes/install-llvm-action@v1
uses: KyleMayes/install-llvm-action@v2
with:
version: "18.0"
version: "18.1.3"
- name: Build compiler
run: cargo build --verbose
- name: Run tests
Expand Down
9 changes: 8 additions & 1 deletion src/ir/to_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,15 @@ impl<'llvm, 'm> ToIR<'llvm, FunctionContext<'llvm, 'm, '_>> for Call {
debug_assert!(
self.function.read().unwrap().mangled_name.is_some()
|| self.function.read().unwrap().is_definition(),
"Generic function {} has no definition, inside this call: {}",
"Generic function {} {} has no definition, inside this call: {}",
self.function.read().unwrap(),
self.function
.read()
.unwrap()
.tr
.as_ref()
.map(|tr| format!("from trait {}", tr.name()))
.unwrap_or_default(),
self
);
self.function.read().unwrap().to_ir(context)
Expand Down
37 changes: 37 additions & 0 deletions src/semantics/link_impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use derive_visitor::VisitorMut;
use log::debug;

use crate::hir::{Call, Generic, Type};

use super::Context;

#[derive(VisitorMut)]
#[visitor(Call(exit))]
pub struct TraitFunctionsLinker<'ctx, C: Context> {
context: &'ctx mut C,
}

impl<'ctx, C: Context> TraitFunctionsLinker<'ctx, C> {
pub fn new(context: &'ctx mut C) -> Self {
Self { context }
}

fn exit_call(&mut self, call: &mut Call) {
let f = call.function.read().unwrap();
// FIXME: definition may be overrided
if f.is_generic() || !f.is_from_trait() || f.is_definition() {
return;
}

debug!(target: "linking-trait-fn-from", "{f}");
// Unknown type here is ok, because we don't have selfs any more
let real_impl = self
.context
.find_implementation(&f, &Type::Unknown)
.unwrap();
drop(f);

call.function = real_impl;
debug!(target: "linking-trait-fn-to", "{}", call.function);
}
}
3 changes: 3 additions & 0 deletions src/semantics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ pub use unnamed::*;

mod replace_self;
pub use replace_self::*;

mod link_impls;
pub use link_impls::*;
5 changes: 4 additions & 1 deletion src/semantics/to_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use crate::hir::{
use crate::mutability::{Mutability, Mutable};
use crate::named::Named;
use crate::semantics::clone::Clonner;
use crate::semantics::{InsertDestructors, ParameterNamer, TemporariesInserter};
use crate::semantics::{
InsertDestructors, ParameterNamer, TemporariesInserter, TraitFunctionsLinker,
};
use crate::syntax::{Identifier, Keyword, Ranged};
use crate::{AddSourceLocation, ErrVec, SourceLocation, WithSourceLocation};

Expand Down Expand Up @@ -975,6 +977,7 @@ impl ToHIR for ast::Module {
debug!(target: &format!("{name}-hir"), "\n{:#}", module);
trace!(target: "steps", "Running passes on `{}`", module.source_file.path().display());
module.drive_mut(&mut ParameterNamer::new());
module.drive_mut(&mut TraitFunctionsLinker::new(context));
module.drive_mut(&mut Clonner::new(context));
module.drive_mut(&mut TemporariesInserter::new());
module.insert_destructors(context);
Expand Down

0 comments on commit 88c0ef1

Please sign in to comment.