Skip to content

Commit

Permalink
Fixed display for template arg
Browse files Browse the repository at this point in the history
  • Loading branch information
IBims1NicerTobi committed Jan 14, 2025
1 parent 5661676 commit 5ac655f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
21 changes: 10 additions & 11 deletions src/to_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ pub struct WrittenTypeDisplay<
template_names: &'a TemplateVec,
}

impl<'a, TypVec: Index<TypeUUID, Output = StructType>, TemplateVec: TemplateNameGetter> Display
for WrittenTypeDisplay<'a, TypVec, TemplateVec>
impl<TypVec: Index<TypeUUID, Output = StructType>, TemplateVec: TemplateNameGetter> Display
for WrittenTypeDisplay<'_, TypVec, TemplateVec>
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self.inner {
Expand Down Expand Up @@ -313,7 +313,7 @@ where
{
assert!(given_template_args.len() == target_link_info.template_parameters.len());
let object_full_name = target_link_info.get_full_name();
if given_template_args.len() == 0 {
if given_template_args.is_empty() {
return format!("{object_full_name} #()");
}

Expand All @@ -323,25 +323,24 @@ where
write!(result, " {}: ", arg_in_target.name).unwrap();
match arg {
ConcreteTemplateArg::Type(concrete_type, how_do_we_know_the_template_arg) => {
write!(
writeln!(
result,
"type {} /* {} */,\n",
"type {} /* {} */,",
concrete_type.display(linker_types),
how_do_we_know_the_template_arg.to_str()
how_do_we_know_the_template_arg
)
.unwrap();
}
ConcreteTemplateArg::Value(value, how_do_we_know_the_template_arg) => {
write!(
writeln!(
result,
"{} /* {} */,\n",
value.to_string(),
how_do_we_know_the_template_arg.to_str()
"{} /* {} */,",
value, how_do_we_know_the_template_arg
)
.unwrap();
}
ConcreteTemplateArg::NotProvided => {
write!(result, "/* Could not infer */\n").unwrap();
writeln!(result, "/* Could not infer */").unwrap();
}
}
}
Expand Down
50 changes: 27 additions & 23 deletions src/typing/template.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::fmt::Display;
use crate::{prelude::*, value::Value};

use super::{abstract_type::AbstractType, concrete_type::ConcreteType};
use crate::flattening::WrittenType;

/// References any [crate::flattening::Module], [crate::flattening::StructType], or [crate::flattening::NamedConstant],
/// and includes any template arguments.
///
/// References any [crate::flattening::Module], [crate::flattening::StructType], or [crate::flattening::NamedConstant],
/// and includes any template arguments.
///
/// As an example, this is the struct in charge of representing:
/// ```sus
/// FIFO #(DEPTH : 32, T : type int)
Expand All @@ -30,10 +30,10 @@ impl<ID> GlobalReference<ID> {
}

/// The template parameters of an object ([crate::flattening::Module], [crate::flattening::StructType], or [crate::flattening::NamedConstant])
///
///
/// See [crate::linker::LinkInfo]
///
/// Not to be confused with [TemplateArg], which is the argument passed to this parameter.
///
/// Not to be confused with [TemplateArg], which is the argument passed to this parameter.
#[derive(Debug)]
pub struct Parameter {
pub name: String,
Expand Down Expand Up @@ -77,12 +77,12 @@ impl ParameterKind {
}
}

/// An argument passed to a template parameter.
///
/// An argument passed to a template parameter.
///
/// See [GlobalReference]
///
/// Not to be confused with [Parameter], which it is passed into.
///
///
/// Not to be confused with [Parameter], which it is passed into.
///
/// When instantiated, this becomes a [ConcreteTemplateArg]
#[derive(Debug)]
pub struct TemplateArg {
Expand Down Expand Up @@ -121,19 +121,19 @@ pub enum HowDoWeKnowTheTemplateArg {
Inferred,
}

impl HowDoWeKnowTheTemplateArg {
pub fn to_str(&self) -> &'static str {
match self {
impl Display for HowDoWeKnowTheTemplateArg {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
HowDoWeKnowTheTemplateArg::Given => "given",
HowDoWeKnowTheTemplateArg::Inferred => "inferred",
}
})
}
}

/// Represents the value we're passing into a template argument.
///
/// Represents the value we're passing into a template argument.
///
/// It is the instantiated variant of [TemplateArg]
///
///
/// And it is passed to a [crate::flattening::Module], [crate::flattening::StructType], or [crate::flattening::NamedConstant]'s [Parameter]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ConcreteTemplateArg {
Expand All @@ -147,20 +147,24 @@ pub enum ConcreteTemplateArg {
impl ConcreteTemplateArg {
#[track_caller]
pub fn unwrap_type(&self) -> &ConcreteType {
let Self::Type(t, _) = self else {unreachable!()};
let Self::Type(t, _) = self else {
unreachable!()
};
t
}
#[track_caller]
pub fn unwrap_value(&self) -> &Value {
let Self::Value(v, _) = self else {unreachable!()};
let Self::Value(v, _) = self else {
unreachable!()
};
v
}
}

/// See [TemplateArg]
pub type TemplateArgs = FlatAlloc<Option<TemplateArg>, TemplateIDMarker>;
/// Applies to both Template Type args and Template Value args.
///
/// Applies to both Template Type args and Template Value args.
///
/// For Types this is the Type, for Values this is unified with the parameter declaration type
pub type TemplateAbstractTypes = FlatAlloc<AbstractType, TemplateIDMarker>;
/// See [Parameter]
Expand Down

0 comments on commit 5ac655f

Please sign in to comment.