This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
168c553
commit ac3f009
Showing
31 changed files
with
624 additions
and
444 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,82 @@ | ||
use derive_visitor::VisitorMut; | ||
|
||
use crate::{ | ||
hir::{Call, Expression, Typed}, | ||
hir::{ | ||
Assignment, Call, Expression, ImplicitConversion, ImplicitConversionKind, Initializer, | ||
Return, Typed, VariableData, | ||
}, | ||
syntax::Ranged, | ||
}; | ||
|
||
use super::Context; | ||
|
||
pub trait CloneIfNeeded: Sized { | ||
fn clone_if_needed_inplace(&mut self, context: &mut impl Context); | ||
#[derive(VisitorMut)] | ||
#[visitor( | ||
Assignment(exit), | ||
Return(exit), | ||
Initializer(exit), | ||
Call(exit), | ||
VariableData(exit) | ||
)] | ||
pub struct Clonner<'ctx> { | ||
context: &'ctx mut dyn Context, | ||
} | ||
|
||
fn clone_if_needed(mut self, context: &mut impl Context) -> Self { | ||
self.clone_if_needed_inplace(context); | ||
self | ||
impl<'ctx> Clonner<'ctx> { | ||
pub fn new(context: &'ctx mut dyn Context) -> Self { | ||
Self { context } | ||
} | ||
} | ||
|
||
impl CloneIfNeeded for Expression { | ||
fn clone_if_needed_inplace(&mut self, context: &mut impl Context) { | ||
fn clone_expr(&mut self, expr: &mut Expression) -> Option<()> { | ||
if !matches!( | ||
self, | ||
Expression::ImplicitConversion(_) | ||
| Expression::VariableReference(_) | ||
expr, | ||
Expression::VariableReference(_) | ||
| Expression::MemberReference(_) | ||
) || self.ty().is_any_reference() | ||
{ | ||
return; | ||
| Expression::ImplicitConversion(ImplicitConversion { | ||
kind: ImplicitConversionKind::Dereference, | ||
.. | ||
}) | ||
) { | ||
return None; | ||
} | ||
|
||
if let Some(clone) = context.clone_for(self.ty()) { | ||
let mut expr: Expression = Call { | ||
range: self.range(), | ||
function: clone, | ||
generic: None, | ||
args: vec![], | ||
} | ||
.into(); | ||
std::mem::swap(&mut expr, self); | ||
match self { | ||
Expression::Call(call) => { | ||
call.args.push(expr); | ||
} | ||
_ => unreachable!("We've just replaced self with call"), | ||
let clone = self.context.clone_for(expr.ty())?; | ||
let mut expr_new: Expression = Call { | ||
range: expr.range(), | ||
function: clone, | ||
generic: None, | ||
args: vec![], | ||
} | ||
.into(); | ||
std::mem::swap(&mut expr_new, expr); | ||
match expr { | ||
Expression::Call(call) => { | ||
call.args.push(expr_new); | ||
} | ||
_ => unreachable!("We've just replaced self with call"), | ||
} | ||
Some(()) | ||
} | ||
|
||
fn exit_variable_data(&mut self, var: &mut VariableData) { | ||
var.initializer.as_mut().map(|expr| self.clone_expr(expr)); | ||
} | ||
|
||
fn exit_assignment(&mut self, assignment: &mut Assignment) { | ||
self.clone_expr(&mut assignment.value); | ||
} | ||
|
||
fn exit_return(&mut self, ret: &mut Return) { | ||
ret.value_mut().map(|expr| self.clone_expr(expr)); | ||
} | ||
|
||
fn exit_initializer(&mut self, init: &mut Initializer) { | ||
self.clone_expr(&mut init.value); | ||
} | ||
|
||
fn exit_call(&mut self, call: &mut Call) { | ||
for arg in &mut call.args { | ||
self.clone_expr(arg); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.