-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!:
Namer
optionally appends node index to mangled names.
- Loading branch information
Showing
7 changed files
with
165 additions
and
125 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use hugr::{ops::OpType, HugrView}; | ||
use inkwell::values::BasicValueEnum; | ||
|
||
use crate::fat::FatNode; | ||
|
||
use super::func::RowPromise; | ||
|
||
/// A type used whenever emission is delegated to a function, for example in | ||
/// [crate::emit::EmitOp]. | ||
pub struct EmitOpArgs<'c, OT, H> { | ||
/// This [HugrView] and [Node] we are emitting | ||
pub node: FatNode<'c, OT, H>, | ||
/// The values that should be used for all Value input ports of the node | ||
pub inputs: Vec<BasicValueEnum<'c>>, | ||
/// The results of the node should be put here | ||
pub outputs: RowPromise<'c>, | ||
} | ||
|
||
impl<'c, OT, H> EmitOpArgs<'c, OT, H> { | ||
/// Get the internal [FatNode] | ||
pub fn node(&self) -> FatNode<'c, OT, H> { | ||
self.node.clone() | ||
} | ||
} | ||
|
||
impl<'c, H: HugrView> EmitOpArgs<'c, OpType, H> { | ||
/// Attempt to specialise the internal [FatNode]. | ||
pub fn try_into_ot<OT: 'c>(self) -> Result<EmitOpArgs<'c, OT, H>, Self> | ||
where | ||
&'c OpType: TryInto<&'c OT>, | ||
{ | ||
let EmitOpArgs { | ||
node, | ||
inputs, | ||
outputs, | ||
} = self; | ||
match node.try_into_ot() { | ||
Some(new_node) => Ok(EmitOpArgs { | ||
node: new_node, | ||
inputs, | ||
outputs, | ||
}), | ||
None => Err(EmitOpArgs { | ||
node, | ||
inputs, | ||
outputs, | ||
}), | ||
} | ||
} | ||
|
||
/// Specialise the internal [FatNode]. | ||
/// | ||
/// Panics if `OT` is not the `get_optype` of the internal [Node]. | ||
pub fn into_ot<OTInto: PartialEq + 'c>(self, ot: &OTInto) -> EmitOpArgs<'c, OTInto, H> | ||
where | ||
for<'a> &'a OpType: TryInto<&'a OTInto>, | ||
{ | ||
let EmitOpArgs { | ||
node, | ||
inputs, | ||
outputs, | ||
} = self; | ||
EmitOpArgs { | ||
node: node.into_ot(ot), | ||
inputs, | ||
outputs, | ||
} | ||
} | ||
} |
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,54 @@ | ||
use hugr::{Node, NodeIndex as _}; | ||
|
||
/// A type with features for mangling the naming of symbols. | ||
#[derive(Clone)] | ||
pub struct Namer { | ||
prefix: String, | ||
postfix_node: bool, | ||
} | ||
|
||
impl Namer { | ||
/// The [Default] impl of `Namer` uses this as a prefix. | ||
pub const DEFAULT_PREFIX: &'static str = "_hl."; | ||
|
||
/// Create a new `Namer` that for each symbol: | ||
/// * prefixes `prefix` | ||
/// * if post_fix_node is true, postfixes ".{node.index()}" | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use hugr_llvm::emit::Namer; | ||
/// use hugr::Node; | ||
/// let node = Node::from(portgraph::NodeIndex::new(7)); | ||
/// let namer = Namer::default(); | ||
/// assert_eq!(namer.name_func("name", node), "_hl.name.7"); | ||
/// | ||
/// let namer = Namer::new("prefix.", false); | ||
/// assert_eq!(namer.name_func("name", node), "prefix.name") | ||
/// ``` | ||
pub fn new(prefix: impl Into<String>, postfix_node: bool) -> Self { | ||
Self { | ||
prefix: prefix.into(), | ||
postfix_node, | ||
} | ||
} | ||
|
||
/// Mangle the the name of a [FuncDefn] or [FuncDecl]. | ||
pub fn name_func(&self, name: impl AsRef<str>, node: Node) -> String { | ||
let prefix = &self.prefix; | ||
let name = name.as_ref(); | ||
let postfix = if self.postfix_node { | ||
format!(".{}", node.index()) | ||
} else { | ||
String::new() | ||
}; | ||
format!("{prefix}{name}{postfix}") | ||
} | ||
} | ||
|
||
impl Default for Namer { | ||
fn default() -> Self { | ||
Self::new(Self::DEFAULT_PREFIX, true) | ||
} | ||
} |
Oops, something went wrong.