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

Commit

Permalink
Move linking to its own pass
Browse files Browse the repository at this point in the history
  • Loading branch information
gavrilikhin-d committed May 2, 2024
1 parent c84b9d4 commit aa13d1f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
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 aa13d1f

Please sign in to comment.