use super::{CodecTarget, Constant, Handle, Handoff, Routine, Scalar};
use crate::adapter::nim::{
field_name, module_prefix, nim_bare_name, nim_enumerator_constant, nim_map_entry_name,
nim_value_type,
};
use crate::encode::EncodeError;
use zerodds_ast::{Ast, CharWidth, MemberNode, TypeKind, TypeNode};
use zerodds_ast_core::Id;
use zerodds_lang_cst::common::{Doc, Ident, Literal};
use zerodds_lang_cst::nim;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Nim;
const MAX_TYPE_DEPTH: usize = 64;
const RUNTIME: &str = "zdwire";
const SIGNED_CHAR: &str = "signed char";
const NO_FORM: &str = "ZdNoNimForm";
#[derive(Debug, Clone)]
pub enum NimExpr {
Plain(nim::Expr),
OptionalSlot(nim::Expr),
ExternalSlot(nim::Expr),
OptionalExternalSlot(nim::Expr),
CaseSlot(nim::Expr),
CaseExternalSlot(nim::Expr),
}
fn value_of(expr: NimExpr) -> nim::Expr {
match expr {
NimExpr::Plain(e) => e,
NimExpr::OptionalSlot(e) | NimExpr::CaseSlot(e) => nim::Expr::dot(e, "get"),
NimExpr::ExternalSlot(e) => nim::Expr::Deref(Box::new(e)),
NimExpr::OptionalExternalSlot(e) | NimExpr::CaseExternalSlot(e) => {
nim::Expr::Deref(Box::new(nim::Expr::dot(e, "get")))
}
}
}
fn plain(e: nim::Expr) -> NimExpr {
NimExpr::Plain(e)
}
impl Nim {
fn runtime_name(name: &str) -> nim::Expr {
nim::Expr::dot(nim::Expr::name(RUNTIME), name)
}
fn camel(method: &str) -> String {
let mut out = String::new();
for (index, segment) in method.split('_').enumerate() {
if index == 0 {
out.push_str(segment);
continue;
}
let mut chars = segment.chars();
if let Some(first) = chars.next() {
out.extend(first.to_uppercase());
out.push_str(chars.as_str());
}
}
out
}
fn text(value: &str) -> NimExpr {
plain(nim::Expr::Literal(Literal::Str(value.to_string())))
}
fn runtime_constant(name: &str) -> NimExpr {
plain(Self::runtime_name(name))
}
fn into_slot(place: NimExpr, value: nim::Expr) -> nim::Stmt {
let wrap_some = |inner: nim::Expr| nim::Expr::call(nim::Expr::name("some"), vec![inner]);
let wrap_ref = |inner: nim::Expr| nim::Expr::call(Self::runtime_name("zdRef"), vec![inner]);
match place {
NimExpr::Plain(field) => nim::Stmt::Assign {
place: field,
value,
},
NimExpr::OptionalSlot(field) | NimExpr::CaseSlot(field) => nim::Stmt::Assign {
place: field,
value: wrap_some(value),
},
NimExpr::ExternalSlot(field) => nim::Stmt::Assign {
place: field,
value: wrap_ref(value),
},
NimExpr::OptionalExternalSlot(field) | NimExpr::CaseExternalSlot(field) => {
nim::Stmt::Assign {
place: field,
value: wrap_some(wrap_ref(value)),
}
}
}
}
fn op(op: &'static str) -> &'static str {
match op {
"&&" | "&" => "and",
"||" | "|" => "or",
"^" => "xor",
"<<" => "shl",
">>" => "shr",
_ => op,
}
}
}
impl CodecTarget for Nim {
type Expr = NimExpr;
type Stmt = nim::Stmt;
type Block = Vec<nim::Stmt>;
type Type = nim::Type;
type Decl = nim::ProcDecl;
fn scalar(kind: Scalar) -> Self::Type {
match kind {
Scalar::Bool => nim::Type::Primitive(nim::Primitive::Bool),
Scalar::Char | Scalar::UInt8 => nim::Type::Primitive(nim::Primitive::UInt8),
Scalar::Int8 => nim::Type::Primitive(nim::Primitive::Int8),
Scalar::Int16 => nim::Type::Primitive(nim::Primitive::Int16),
Scalar::UInt16 => nim::Type::Primitive(nim::Primitive::UInt16),
Scalar::Int32 => nim::Type::Primitive(nim::Primitive::Int32),
Scalar::UInt32 | Scalar::Index => nim::Type::Primitive(nim::Primitive::UInt32),
Scalar::Int64 => nim::Type::Primitive(nim::Primitive::Int64),
Scalar::UInt64 => nim::Type::Primitive(nim::Primitive::UInt64),
Scalar::Float => nim::Type::Primitive(nim::Primitive::Float32),
Scalar::Double => nim::Type::Primitive(nim::Primitive::Float64),
Scalar::LongDouble => nim::Type::Primitive(nim::Primitive::String),
Scalar::Size => nim::Type::named("int"),
}
}
fn named(name: &str) -> Self::Type {
nim::Type::named(name)
}
fn nothing() -> Self::Type {
nim::Type::named("void")
}
fn handle(kind: Handle) -> Self::Type {
nim::Type::named(match kind {
Handle::DHeader => "zdwire.DHeader",
Handle::EmHeader => "zdwire.EmHeader",
Handle::Pid => "zdwire.Pid",
Handle::TryConstruct => "zdwire.TryConstruct",
Handle::ReaderMark => "zdwire.ReaderMark",
Handle::PidRead => "zdwire.PidRead",
Handle::EmHeaderRead => "zdwire.EmHeaderRead",
})
}
fn code_point(value: Self::Expr, _narrow: bool) -> Self::Expr {
value
}
fn constant(which: Constant) -> Self::Expr {
match which {
Constant::WcharWidth2 => plain(nim::Expr::int(2)),
Constant::WcharWidth4 => plain(nim::Expr::int(4)),
Constant::WcharFixed => Self::text("fixed"),
Constant::WcharGiopLengthPrefixed => Self::text("giop-length-prefixed"),
Constant::WstringUnitsStream => Self::text("stream"),
Constant::WstringUnitsBigEndianAlways => Self::text("big-endian-always"),
Constant::WstringUnitsByteOrderMark => Self::text("byte-order-mark"),
Constant::WstringUnitsHostUnswapped => Self::text("host-unswapped"),
Constant::WstringSpecification => Self::text("specification"),
Constant::WstringCharacterCount => Self::text("character-count"),
Constant::WstringCharacterCountTerminated => Self::text("character-count-terminated"),
Constant::WstringSpecificationWideUnits => Self::text("specification-wide-units"),
Constant::WstringRosTypeSupport => Self::text("ros-type-support"),
Constant::WstringOctetsTerminated2 => Self::text("octets/terminated/2"),
Constant::WstringOctetsTerminated4 => Self::text("octets/terminated/4"),
Constant::WstringCharactersTerminated2 => Self::text("characters/terminated/2"),
Constant::FixedDeclared => Self::text("declared"),
Constant::FixedValuePrecision => Self::text("value-precision"),
Constant::FixedValueStringDigits => Self::text("value-string-digits"),
Constant::ParameterMember => Self::runtime_constant("ParameterMember"),
Constant::ParameterOptional => Self::runtime_constant("ParameterOptional"),
Constant::TryConstructDiscard => Self::runtime_constant("TryConstructDiscard"),
Constant::TryConstructTrim => Self::runtime_constant("TryConstructTrim"),
Constant::TryConstructUseDefault => Self::runtime_constant("TryConstructUseDefault"),
Constant::StatusOk => Self::runtime_constant("StatusOk"),
Constant::StatusDiscarded => Self::runtime_constant("StatusDiscarded"),
Constant::StatusImpossibleLength => Self::runtime_constant("StatusImpossibleLength"),
Constant::StatusOutOfRange => Self::runtime_constant("StatusOutOfRange"),
Constant::StatusTruncated => Self::runtime_constant("StatusTruncated"),
}
}
fn unsigned_holder(bits: u16) -> Self::Type {
nim::Type::Primitive(crate::adapter::nim::nim_holder(bits))
}
fn value_type(ast: &Ast, id: Id<TypeNode>) -> Self::Type {
#[allow(clippy::manual_unwrap_or_default, clippy::manual_unwrap_or)]
match nim_value_type(ast, id) {
Ok(type_) => type_,
Err(_) => nim::Type::named(NO_FORM),
}
}
fn type_name(ast: &Ast, id: Id<TypeNode>) -> String {
type_tag(ast, id, 0)
}
fn map_entry_name(ast: &Ast, id: Id<TypeNode>) -> String {
nim_map_entry_name(ast, id).unwrap_or_else(|_| format!("ZdUnresolvedEntry{}", id.slot()))
}
fn module_prefix(ast: &Ast, id: Id<TypeNode>) -> String {
module_prefix(ast, id)
}
fn escape(name: &str) -> String {
field_name(name)
}
fn inherited_path(depth: usize) -> Option<String> {
Some(format!("{}.", field_name("parent")).repeat(depth))
}
fn name(text: &str) -> Self::Expr {
plain(nim::Expr::name(text))
}
fn int(value: i128) -> Self::Expr {
plain(nim::Expr::int(value))
}
fn literal(value: Literal) -> Self::Expr {
let Literal::Int(text) = &value else {
return plain(nim::Expr::Literal(value));
};
match text.as_str() {
"true" => plain(nim::Expr::Literal(Literal::Bool(true))),
"false" => plain(nim::Expr::Literal(Literal::Bool(false))),
_ => plain(nim::Expr::Literal(value)),
}
}
fn wide_int(value: i128) -> Self::Expr {
if value > i128::from(i64::MAX) {
return plain(nim::Expr::Literal(Literal::Int(format!("{value}'u64"))));
}
if value > i128::from(i32::MAX) || value < i128::from(i32::MIN) {
return plain(nim::Expr::Literal(Literal::Int(format!("{value}'i64"))));
}
plain(nim::Expr::int(value))
}
fn unsigned_int(value: i128) -> Self::Expr {
Self::wide_int(value)
}
fn wide_unsigned(value: u64) -> Self::Expr {
plain(nim::Expr::Literal(Literal::Int(format!("{value}'u64"))))
}
fn wide_signed(value: i128) -> Self::Expr {
plain(nim::Expr::Literal(Literal::Int(format!("{value}'i64"))))
}
fn member(base: &str, field: &str) -> Self::Expr {
let spelled = if field == "_d" {
crate::adapter::nim::DISCRIMINATOR
} else {
field
};
plain(nim::Expr::dot(nim::Expr::name(base), spelled))
}
fn whole(base: &str) -> Self::Expr {
plain(nim::Expr::name(base))
}
fn field_of(value: Self::Expr, field: &str) -> Self::Expr {
plain(nim::Expr::dot(value_of(value), field))
}
fn header_field(value: Self::Expr, field: &str) -> Self::Expr {
let spelled = if field == "end" {
"endOfList".to_string()
} else {
Self::camel(field)
};
plain(nim::Expr::dot(value_of(value), &spelled))
}
fn index(value: Self::Expr, at: Self::Expr) -> Self::Expr {
plain(nim::Expr::Index {
value: Box::new(value_of(value)),
index: Box::new(nim::Expr::Convert {
type_: Box::new(nim::Type::named("int")),
value: Box::new(value_of(at)),
}),
})
}
fn count_of(base: &str, fixed: Option<u64>) -> Self::Expr {
match fixed {
Some(bound) => plain(nim::Expr::int(i128::from(bound))),
None => plain(nim::Expr::Convert {
type_: Box::new(nim::Type::Primitive(nim::Primitive::UInt32)),
value: Box::new(nim::Expr::dot(nim::Expr::name(base), "len")),
}),
}
}
fn elements(base: &str) -> Self::Expr {
plain(nim::Expr::name(base))
}
fn case_member(base: &str, field: &str) -> Self::Expr {
NimExpr::CaseSlot(value_of(Self::member(base, field)))
}
fn entry_half(entry: Self::Expr, key: bool) -> Self::Expr {
plain(nim::Expr::dot(
value_of(entry),
if key { "key" } else { "value" },
))
}
fn absent() -> Self::Expr {
plain(nim::Expr::Literal(Literal::Null))
}
fn materialised(_value: Self::Expr) -> Option<Self::Expr> {
None
}
fn optional_value(_ast: &Ast, _id: Id<TypeNode>, base: &str, field: &str) -> Self::Expr {
plain(nim::Expr::dot(value_of(Self::member(base, field)), "get"))
}
fn present(base: &str, field: &str) -> Self::Expr {
plain(nim::Expr::dot(
value_of(Self::member(base, field)),
"isSome",
))
}
fn set_present(base: &str, field: &str, present: bool) -> Vec<Self::Stmt> {
if present {
return Vec::new();
}
vec![nim::Stmt::Expr(nim::Expr::call(
nim::Expr::name("reset"),
vec![value_of(Self::member(base, field))],
))]
}
fn clear_union_cases(base: &str, fields: &[String]) -> Vec<Self::Stmt> {
fields
.iter()
.flat_map(|field| Self::set_present(base, field, false))
.collect()
}
fn optional_destination(base: &str, field: &str) -> (Vec<Self::Stmt>, Self::Expr) {
(
Vec::new(),
NimExpr::OptionalSlot(value_of(Self::member(base, field))),
)
}
fn holds_a_cycle_externally() -> bool {
true
}
fn external_value(
_ast: &Ast,
_id: Id<TypeNode>,
base: &str,
field: &str,
optional: bool,
) -> Self::Expr {
let member = value_of(Self::member(base, field));
if optional {
return plain(nim::Expr::Deref(Box::new(nim::Expr::dot(member, "get"))));
}
plain(nim::Expr::Deref(Box::new(member)))
}
fn external_destination(
base: &str,
field: &str,
optional: bool,
) -> (Vec<Self::Stmt>, Self::Expr) {
let member = value_of(Self::member(base, field));
let slot = if optional {
NimExpr::OptionalExternalSlot(member)
} else {
NimExpr::ExternalSlot(member)
};
(Vec::new(), slot)
}
fn external_present(base: &str, field: &str) -> Self::Expr {
Self::present(base, field)
}
fn external_set_present(base: &str, field: &str, present: bool) -> Vec<Self::Stmt> {
Self::set_present(base, field, present)
}
fn external_case_value(_ast: &Ast, _id: Id<TypeNode>, base: &str, field: &str) -> Self::Expr {
plain(nim::Expr::Deref(Box::new(nim::Expr::dot(
value_of(Self::member(base, field)),
"get",
))))
}
fn external_case_destination(base: &str, field: &str) -> (Vec<Self::Stmt>, Self::Expr) {
(
Vec::new(),
NimExpr::CaseExternalSlot(value_of(Self::member(base, field))),
)
}
fn binary(op: &'static str, lhs: Self::Expr, rhs: Self::Expr) -> Self::Expr {
plain(nim::Expr::Binary {
op: Self::op(op),
lhs: Box::new(value_of(lhs)),
rhs: Box::new(value_of(rhs)),
})
}
fn unary(op: &'static str, value: Self::Expr) -> Self::Expr {
if op == "*" {
return plain(nim::Expr::Deref(Box::new(value_of(value))));
}
plain(nim::Expr::Unary {
op: match op {
"!" | "~" => "not",
other => other,
},
value: Box::new(value_of(value)),
})
}
fn paren(value: Self::Expr) -> Self::Expr {
plain(nim::Expr::Paren(Box::new(value_of(value))))
}
fn cast(type_: Self::Type, value: Self::Expr) -> Self::Expr {
if type_ == nim::Type::named(SIGNED_CHAR) {
return plain(nim::Expr::Cast {
type_: Box::new(nim::Type::Primitive(nim::Primitive::Int8)),
value: Box::new(value_of(value)),
});
}
if type_ == nim::Type::Primitive(nim::Primitive::Bool) {
if let NimExpr::Plain(nim::Expr::Literal(Literal::Int(text))) = &value {
return plain(nim::Expr::Literal(Literal::Bool(text != "0")));
}
}
if type_ == nim::Type::Primitive(nim::Primitive::Int64) {
return plain(nim::Expr::call(
Self::runtime_name("zdI64"),
vec![value_of(value)],
));
}
plain(nim::Expr::Convert {
type_: Box::new(type_),
value: Box::new(value_of(value)),
})
}
fn handoff(value: Self::Expr, _form: Handoff) -> Self::Expr {
value
}
fn call(callee: &str, args: Vec<Self::Expr>) -> Self::Expr {
let arguments: Vec<nim::Expr> = args.into_iter().map(value_of).collect();
if callee == "strcmp" {
return plain(nim::Expr::call(Self::runtime_name("strcmp"), arguments));
}
plain(nim::Expr::call(nim::Expr::name(callee), arguments))
}
fn runtime(receiver: &str, method: &str, args: Vec<Self::Expr>) -> Self::Expr {
if method == "read_pid" && args.is_empty() {
return Self::runtime(
receiver,
"read_pid_as",
vec![Self::constant(Constant::ParameterMember)],
);
}
plain(nim::Expr::call(
nim::Expr::dot(nim::Expr::name(receiver), &Self::camel(method)),
args.into_iter().map(value_of).collect(),
))
}
fn runtime_static(method: &str, args: Vec<Self::Expr>) -> Self::Expr {
if method == "pid_is_user_member" {
let mut arguments = args.into_iter().map(value_of);
if let Some(header) = arguments.next() {
return plain(nim::Expr::call(
nim::Expr::dot(header, "isUserMember"),
Vec::new(),
));
}
return plain(nim::Expr::call(
Self::runtime_name("isUserMember"),
Vec::new(),
));
}
plain(nim::Expr::call(
Self::runtime_name(&Self::camel(method)),
args.into_iter().map(value_of).collect(),
))
}
fn carries_hdds_axes() -> bool {
true
}
fn declare(type_: Self::Type, name: &str, value: Option<Self::Expr>) -> Self::Stmt {
nim::Stmt::Var {
name: Ident::new(name),
type_: Some(type_),
value: value.map(value_of),
}
}
fn perform(value: Self::Expr) -> Self::Stmt {
if let NimExpr::Plain(nim::Expr::Binary { op: "=", lhs, rhs }) = value {
return nim::Stmt::Assign {
place: *lhs,
value: *rhs,
};
}
nim::Stmt::Expr(value_of(value))
}
fn give_back(value: Option<Self::Expr>) -> Self::Stmt {
nim::Stmt::Return(Some(match value {
Some(value) => value_of(value),
None => nim::Expr::name("v"),
}))
}
fn when(
condition: Self::Expr,
then: Self::Block,
otherwise: Option<Self::Block>,
) -> Self::Stmt {
nim::Stmt::If {
condition: value_of(condition),
then,
otherwise: otherwise.filter(|body| !body.is_empty()),
}
}
fn count_up(counter: &str, bound: Self::Expr, body: Self::Block) -> Self::Stmt {
nim::Stmt::For {
counter: Ident::new(counter),
bound: Box::new(value_of(bound)),
body,
}
}
fn until(condition: Self::Expr, body: Self::Block) -> Self::Stmt {
nim::Stmt::While {
condition: value_of(condition),
body,
}
}
fn leave() -> Self::Stmt {
nim::Stmt::Break
}
fn note(doc: Doc) -> Self::Stmt {
nim::Stmt::Comment(doc)
}
fn drop_value(value: Self::Expr) -> Self::Stmt {
nim::Stmt::Discard(value_of(value))
}
fn mark_used(name: &str) -> Vec<Self::Stmt> {
vec![nim::Stmt::Discard(nim::Expr::name(name))]
}
fn read_call(
callee: &str,
place: Self::Expr,
_form: Handoff,
behaviour: Self::Expr,
) -> Self::Stmt {
let decoded = nim::Expr::call(
nim::Expr::name(callee),
vec![nim::Expr::name("r"), value_of(behaviour)],
);
Self::into_slot(place, decoded)
}
fn read_forward(callee: &str) -> Self::Stmt {
Self::read_call(
callee,
Self::whole("v"),
Handoff::Value,
Self::name("zd_tc"),
)
}
fn store(place: Self::Expr, _type_: Self::Type, value: Self::Expr) -> Self::Stmt {
Self::into_slot(place, value_of(value))
}
fn block(statements: Vec<Self::Stmt>) -> Self::Block {
statements
}
fn into_statements(block: Self::Block) -> Vec<Self::Stmt> {
block
}
fn make_room(
base: &str,
element: Self::Type,
count: Self::Expr,
_assigned_whole: bool,
fill: Vec<Self::Stmt>,
) -> Vec<Self::Stmt> {
let mut out = vec![nim::Stmt::Assign {
place: nim::Expr::name(base),
value: nim::Expr::NewSeq {
element: Box::new(element),
length: Box::new(nim::Expr::Convert {
type_: Box::new(nim::Type::named("int")),
value: Box::new(value_of(count)),
}),
},
}];
out.extend(fill);
out
}
fn keep_first(base: &str, count: Self::Expr) -> Vec<Self::Stmt> {
vec![nim::Stmt::Expr(nim::Expr::call(
nim::Expr::dot(nim::Expr::name(base), "setLen"),
vec![nim::Expr::Convert {
type_: Box::new(nim::Type::named("int")),
value: Box::new(value_of(count)),
}],
))]
}
fn sequence_element(
base: &str,
index: Self::Expr,
_element: Self::Type,
) -> (Vec<Self::Stmt>, Self::Expr, Vec<Self::Stmt>) {
(
Vec::new(),
Self::index(Self::elements(base), index),
Vec::new(),
)
}
fn routine(spec: Routine<Self>) -> Option<Self::Decl> {
let mut params = vec![nim::Param {
name: Ident::new(if spec.writing { "w" } else { "r" }),
type_: nim::Type::named(if spec.writing {
"zdwire.Writer"
} else {
"zdwire.Reader"
}),
}];
if spec.writing {
params.push(nim::Param {
name: Ident::new("v"),
type_: spec.sample.clone(),
});
}
for (type_, name) in spec.extra {
params.push(nim::Param {
name: Ident::new(&name),
type_,
});
}
let (returns, body) = if spec.writing {
(None, spec.body)
} else {
let body = spec.body.map(|body| {
let defaulter = crate::default_nim::defaulter_from_decoder(&spec.name);
let mut statements = vec![nim::Stmt::Var {
name: Ident::new("v"),
type_: None,
value: Some(nim::Expr::call(nim::Expr::name(&defaulter), Vec::new())),
}];
statements.extend(body);
statements.push(nim::Stmt::Return(Some(nim::Expr::name("v"))));
statements
});
(Some(spec.sample), body)
};
Some(nim::ProcDecl {
doc: spec.doc,
name: Ident::new(&spec.name),
exported: true,
params,
returns,
body,
})
}
fn default_into(
ast: &Ast,
id: Id<TypeNode>,
place: Self::Expr,
) -> Result<Self::Stmt, EncodeError> {
let defaulted = nim::Expr::call(
nim::Expr::name(&crate::default_nim::defaulter_name(ast, id)),
Vec::new(),
);
Ok(Self::into_slot(place, defaulted))
}
fn member_default(
ast: &Ast,
node: &MemberNode,
type_: Id<TypeNode>,
place: Self::Expr,
) -> Result<Vec<Self::Stmt>, EncodeError> {
crate::default_nim::member_default(ast, node, type_, value_of(place))
}
fn enumerator_constant(ast: &Ast, id: Id<TypeNode>, enumerator: &str) -> Self::Expr {
plain(nim::Expr::name(&nim_enumerator_constant(
ast, id, enumerator,
)))
}
}
fn bound_tag(ast: &Ast, bound: &zerodds_ast::Bound) -> String {
match bound.expr() {
None => String::new(),
Some(expr) => match ast.eval_integral(expr) {
Ok(limit) => format!("_b{limit}"),
Err(_) => "_bx".to_string(),
},
}
}
#[must_use]
pub fn type_tag(ast: &Ast, id: Id<TypeNode>, depth: usize) -> String {
if depth > MAX_TYPE_DEPTH {
return "unresolved".to_string();
}
let node = match ast.type_(id) {
Ok(node) => node,
Err(_) => return "unresolved".to_string(),
};
match &node.kind {
TypeKind::Primitive(kind) => kind.as_str().replace(' ', "_"),
TypeKind::String {
width: CharWidth::Char,
bound,
} => format!("string{}", bound_tag(ast, bound)),
TypeKind::String {
width: CharWidth::WChar,
bound,
} => format!("wstring{}", bound_tag(ast, bound)),
TypeKind::Fixed { digits, scale } => format!("fixed_{digits}_{scale}"),
TypeKind::Sequence { element, bound } => match element.resolved() {
Some(element) => format!(
"seq{}_{}",
bound_tag(ast, bound),
type_tag(ast, element, depth + 1)
),
None => format!("seq_unresolved{}", bound_tag(ast, bound)),
},
TypeKind::Array { element, dims } => {
let mut name = match element.resolved() {
Some(element) => format!("arr_{}", type_tag(ast, element, depth + 1)),
None => "arr_unresolved".to_string(),
};
for dim in dims {
match ast.eval_integral(*dim) {
Ok(length) => name.push_str(&format!("_{length}")),
Err(_) => name.push_str("_x"),
}
}
name
}
TypeKind::Map { key, value, bound } => {
let half = |half: Option<Id<TypeNode>>| match half {
Some(target) => type_tag(ast, target, depth + 1),
None => "unresolved".to_string(),
};
format!(
"map{}_{}_{}",
bound_tag(ast, bound),
half(key.resolved()),
half(value.resolved())
)
}
TypeKind::Alias { aliased } => match aliased.resolved() {
Some(target) => type_tag(ast, target, depth + 1),
None => "unresolved".to_string(),
},
TypeKind::ValueBox { boxed } => match boxed.resolved() {
Some(target) => format!("box_{}", type_tag(ast, target, depth + 1)),
None => "box_unresolved".to_string(),
},
TypeKind::Structure { .. }
| TypeKind::Union { .. }
| TypeKind::Enumeration { .. }
| TypeKind::Bitmask { .. }
| TypeKind::Bitset { .. }
| TypeKind::Valuetype { .. }
| TypeKind::Native => nim_bare_name(ast, id),
TypeKind::Any => "any".to_string(),
TypeKind::ValueBase => "valuebase".to_string(),
TypeKind::ObjectRef { .. } => "objectref".to_string(),
TypeKind::AnnotationType { .. } => "annotation".to_string(),
}
}