-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
3 changed files
with
117 additions
and
108 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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
pub mod basic; | ||
|
||
use std::any::{Any, TypeId}; | ||
|
||
use smallvec::SmallVec; | ||
|
||
use crate::Value; | ||
|
||
pub trait Inst: Any { | ||
fn visit_values(&self, f: &mut dyn FnMut(Value)); | ||
fn visit_values_mut(&mut self, f: &mut dyn FnMut(&mut Value)); | ||
fn has_side_effect(&self) -> bool; | ||
fn as_text(&self) -> &'static str; | ||
} | ||
|
||
/// This trait works as a "proof" that a specific ISA contains `I`, | ||
/// and then allows a construction and reflection of type `I` in that specific ISA context. | ||
pub trait HasInst<I: Inst> { | ||
fn is(&self, inst: &dyn Inst) -> bool { | ||
inst.type_id() == TypeId::of::<I>() | ||
} | ||
} | ||
|
||
pub(crate) trait ValueVisitable { | ||
fn visit_with(&self, f: &mut dyn FnMut(Value)); | ||
fn visit_mut_with(&mut self, f: &mut dyn FnMut(&mut Value)); | ||
} | ||
|
||
impl ValueVisitable for Value { | ||
fn visit_with(&self, f: &mut dyn FnMut(Value)) { | ||
f(*self) | ||
} | ||
|
||
fn visit_mut_with(&mut self, f: &mut dyn FnMut(&mut Value)) { | ||
f(self) | ||
} | ||
} | ||
|
||
impl<V> ValueVisitable for Option<V> | ||
where | ||
V: ValueVisitable, | ||
{ | ||
fn visit_with(&self, f: &mut dyn FnMut(Value)) { | ||
if let Some(value) = self { | ||
value.visit_with(f) | ||
} | ||
} | ||
|
||
fn visit_mut_with(&mut self, f: &mut dyn FnMut(&mut Value)) { | ||
if let Some(value) = self.as_mut() { | ||
value.visit_mut_with(f) | ||
} | ||
} | ||
} | ||
|
||
impl<V, T> ValueVisitable for (V, T) | ||
where | ||
V: ValueVisitable, | ||
{ | ||
fn visit_with(&self, f: &mut dyn FnMut(Value)) { | ||
self.0.visit_with(f) | ||
} | ||
|
||
fn visit_mut_with(&mut self, f: &mut dyn FnMut(&mut Value)) { | ||
self.0.visit_mut_with(f) | ||
} | ||
} | ||
|
||
impl<V> ValueVisitable for Vec<V> | ||
where | ||
V: ValueVisitable, | ||
{ | ||
fn visit_with(&self, f: &mut dyn FnMut(Value)) { | ||
self.iter().for_each(|v| v.visit_with(f)) | ||
} | ||
|
||
fn visit_mut_with(&mut self, f: &mut dyn FnMut(&mut Value)) { | ||
self.iter_mut().for_each(|v| v.visit_mut_with(f)) | ||
} | ||
} | ||
|
||
impl<V> ValueVisitable for [V] | ||
where | ||
V: ValueVisitable, | ||
{ | ||
fn visit_with(&self, f: &mut dyn FnMut(Value)) { | ||
self.iter().for_each(|v| v.visit_with(f)) | ||
} | ||
|
||
fn visit_mut_with(&mut self, f: &mut dyn FnMut(&mut Value)) { | ||
self.iter_mut().for_each(|v| v.visit_mut_with(f)) | ||
} | ||
} | ||
|
||
impl<V, const N: usize> ValueVisitable for SmallVec<[V; N]> | ||
where | ||
V: ValueVisitable, | ||
[V; N]: smallvec::Array<Item = V>, | ||
{ | ||
fn visit_with(&self, f: &mut dyn FnMut(Value)) { | ||
self.iter().for_each(|v| v.visit_with(f)) | ||
} | ||
|
||
fn visit_mut_with(&mut self, f: &mut dyn FnMut(&mut Value)) { | ||
self.iter_mut().for_each(|v| v.visit_mut_with(f)) | ||
} | ||
} |
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