use crate::adapter::cst::MaterialiseError;
use crate::adapter::kotlin::{
kotlin_type_name, kotlin_value_type, property_name, type_tag, unsigned_holder,
};
use crate::default_common::{discriminator_default_value, resolve_alias, selected_case};
use crate::encode::EncodeError;
use crate::fixed_literal::{fixed_text, zero_decimal};
use zerodds_ast::{
Ast, ConstExpr, Literal as AstLiteral, MemberNode, PrimitiveKind, TypeKind, TypeNode,
};
use zerodds_ast_core::Id;
use zerodds_lang_cst::common::{Doc, Ident, Literal};
use zerodds_lang_cst::kotlin;
#[must_use]
pub fn defaulter_name(ast: &Ast, id: Id<TypeNode>) -> String {
match ast.type_(id).map(|node| node.kind.clone()) {
Ok(TypeKind::Primitive(kind)) => {
format!("zd_def_p_{}", kind.as_str().replace(' ', "_"))
}
_ => format!("zd_def_{}", type_tag(ast, id, 0)),
}
}
#[must_use]
pub fn defaulter_from_decoder(decoder: &str) -> String {
match decoder.strip_prefix("zd_dec_") {
Some(rest) => format!("zd_def_{rest}"),
None => format!("zd_def_{decoder}"),
}
}
pub fn default_into(ast: &Ast, id: Id<TypeNode>, place: kotlin::Expr) -> kotlin::Stmt {
kotlin::Stmt::Expr(kotlin::Expr::Binary {
op: "=",
lhs: Box::new(place),
rhs: Box::new(call(&defaulter_name(ast, id), Vec::new())),
})
}
pub fn member_default(
ast: &Ast,
node: &MemberNode,
type_: Id<TypeNode>,
place: kotlin::Expr,
) -> Result<Vec<kotlin::Stmt>, EncodeError> {
let Some(expr) = node.facets.xtypes.as_ref().and_then(|x| x.default_value) else {
return Ok(vec![default_into(ast, type_, place)]);
};
let value = explicit(ast, expr, type_, &node.name)?;
Ok(vec![assign(place, value)])
}
pub fn kotlin_defaulters_called_by(
ast: &Ast,
decls: &[kotlin::Decl],
) -> Result<Vec<kotlin::Decl>, EncodeError> {
let index: Vec<(String, Id<TypeNode>)> = ast
.types()
.map(|(id, _)| (defaulter_name(ast, id), id))
.collect();
let mut called: Vec<String> = Vec::new();
for decl in decls {
calls_in_decl(decl, &mut called);
}
let mut wanted: Vec<Id<TypeNode>> = Vec::new();
let mut have: Vec<String> = Vec::new();
for _ in 0..=index.len() {
let mut grew = false;
for name in &called {
if have.contains(name) {
continue;
}
have.push(name.clone());
if let Some(id) = index.iter().find(|(n, _)| n == name).map(|(_, id)| *id) {
wanted.push(id);
grew = true;
}
}
let produced = kotlin_defaulters_over(ast, &wanted)?;
if !grew {
return Ok(produced);
}
for decl in &produced {
calls_in_decl(decl, &mut called);
}
}
kotlin_defaulters_over(ast, &wanted)
}
pub fn kotlin_defaulters_over(
ast: &Ast,
types: &[Id<TypeNode>],
) -> Result<Vec<kotlin::Decl>, EncodeError> {
let mut out = Vec::new();
let mut emitted: Vec<String> = Vec::new();
for id in types.iter().copied() {
let name = defaulter_name(ast, id);
if emitted.contains(&name) {
continue;
}
let Some(body) = body_of(ast, id, &name)? else {
continue;
};
emitted.push(name.clone());
out.push(kotlin::Decl::Func(kotlin::FuncDecl {
doc: Doc::line(
"Table 9 of DDS-XTypes 1.3: what this type is where the stream did not \
carry it.",
),
name: Ident::new(&name),
params: Vec::new(),
returns: value_type(ast, id),
body,
}));
}
Ok(out)
}
fn value_type(ast: &Ast, id: Id<TypeNode>) -> kotlin::Type {
kotlin_value_type(ast, id, "default")
.unwrap_or_else(|_| kotlin::Type::named(&format!("UnresolvedType{}", id.slot())))
}
#[allow(clippy::too_many_lines)]
fn body_of(
ast: &Ast,
id: Id<TypeNode>,
own_name: &str,
) -> Result<Option<Vec<kotlin::Stmt>>, EncodeError> {
let refuse = |kind: &'static str| EncodeError::NoWriter {
member: own_name.to_string(),
kind,
};
let Ok(node) = ast.type_(id) else {
return Err(refuse("a type the model does not hold"));
};
let statements = match &node.kind {
TypeKind::Primitive(kind) => vec![give_back(primitive_default(*kind))],
TypeKind::String { .. } => vec![give_back(text(""))],
TypeKind::Fixed { scale, .. } => vec![give_back(text(&zero_decimal(*scale)))],
TypeKind::Alias { aliased } => {
let Some(target) = aliased.resolved() else {
return Err(refuse("typedef with an unresolved target"));
};
vec![give_back(call(&defaulter_name(ast, target), Vec::new()))]
}
TypeKind::Enumeration { .. } => {
let Some(enumerator) = crate::emit::default_enumerator_name(ast, id) else {
return Err(refuse(
"enumeration with no enumerator to take as its default",
));
};
vec![give_back(enumerator_constant(ast, id, &enumerator))]
}
TypeKind::Bitmask { .. } => {
let holder = kotlin::Type::Primitive(unsigned_holder(ast.effective_bit_bound(id)));
vec![give_back(zero_of(&holder))]
}
TypeKind::Bitset { .. } => {
let mut statements = vec![new_local(value_type(ast, id))];
for field in &ast.all_bitfields_of(id) {
let Ok(field_node) = ast.bitfield(*field) else {
return Err(refuse("a bitfield the model does not hold"));
};
if let Some(field_name) = field_node.name.as_ref() {
statements.push(assign(
member("v", &property_name(field_name)),
zero_of(&kotlin::Type::Primitive(unsigned_holder(field_node.bits))),
));
}
}
statements.push(give_back(name("v")));
statements
}
TypeKind::Sequence { .. } | TypeKind::Map { .. } => {
let kotlin::Type::Array(element) = value_type(ast, id) else {
return Err(refuse("collection whose Kotlin form is not an array"));
};
let init = match &node.kind {
TypeKind::Sequence { element, .. } => element
.resolved()
.and_then(|element| alias_slot_start(ast, element)),
_ => None,
};
vec![give_back(kotlin::Expr::NewArray {
element,
length: Box::new(kotlin::Expr::int(0)),
init: init.map(Box::new),
})]
}
TypeKind::Array { element, dims } => {
let Some(element) = element.resolved() else {
return Err(refuse("array with an unresolved element"));
};
let mut lengths = Vec::new();
for dim in dims {
lengths.push(
ast.eval_integral(*dim)
.map_err(|_| refuse("array whose length does not evaluate"))?,
);
}
let element_default = element_default_of(ast, element, own_name)?;
let mut expr = element_default;
let mut type_ = value_type(ast, element);
for length in lengths.iter().rev() {
let count = usize::try_from(*length)
.map_err(|_| refuse("array whose length does not fit"))?;
expr = kotlin::Expr::ArrayOf {
element: Box::new(type_.clone()),
elements: vec![expr; count],
};
type_ = kotlin::Type::array(type_);
}
vec![give_back(expr)]
}
TypeKind::Structure { .. } | TypeKind::Valuetype { .. } => {
let mut statements = vec![new_local(value_type(ast, id))];
if let Some(base) = crate::adapter::c::struct_base(ast, id) {
statements.push(default_into(
ast,
base,
member("v", &property_name("parent")),
));
}
for member_id in ast.members_of(id) {
let Ok(member_node) = ast.member(*member_id) else {
return Err(EncodeError::NoWriter {
member: crate::adapter::cst::handle_of(*member_id),
kind: "a member the model does not hold",
});
};
let field = property_name(&member_node.name);
if member_node.facets.is_optional() {
statements.push(assign(
member("v", &field),
kotlin::Expr::Literal(Literal::Null),
));
continue;
}
let Some(member_type) = member_node.type_.resolved() else {
return Err(EncodeError::NoWriter {
member: member_node.name.clone(),
kind: "member with an unresolved type",
});
};
statements.extend(member_default(
ast,
member_node,
member_type,
member("v", &field),
)?);
}
statements.push(give_back(name("v")));
statements
}
TypeKind::Union { discriminator, .. } => {
let Some(discriminator) = discriminator.resolved() else {
return Err(refuse("union with an unresolved discriminator"));
};
let mut statements = vec![
new_local(value_type(ast, id)),
default_into(ast, discriminator, member("v", "_d")),
];
if let Some((member_type, field)) = selected_by_default(ast, id, discriminator)? {
statements.push(default_into(ast, member_type, member("v", &field)));
}
statements.push(give_back(name("v")));
statements
}
TypeKind::Native | TypeKind::ObjectRef { .. } | TypeKind::AnnotationType { .. } => {
return Ok(None);
}
TypeKind::ValueBox { .. } => {
return Err(refuse(
"boxed value, whose presence flag has no settled wire form",
));
}
TypeKind::Any => {
return Err(refuse(
"`any`, which DDS-XTypes has no encoding for; only CORBA CDR writes one, \
as a TypeCode ahead of the value",
));
}
TypeKind::ValueBase => {
return Err(refuse(
"`ValueBase`, which DDS-XTypes has no encoding for; only CORBA CDR writes \
one, as a chunked value with its repository id",
));
}
};
Ok(Some(statements))
}
fn element_default_of(
ast: &Ast,
element: Id<TypeNode>,
own_name: &str,
) -> Result<kotlin::Expr, EncodeError> {
let refuse = |kind: &'static str| EncodeError::NoWriter {
member: own_name.to_string(),
kind,
};
let Ok(node) = ast.type_(element) else {
return Err(refuse("array element with an unresolved type"));
};
Ok(match &node.kind {
TypeKind::Primitive(kind) => primitive_default(*kind),
TypeKind::String { .. } => text(""),
TypeKind::Fixed { scale, .. } => text(&zero_decimal(*scale)),
_ => call(&defaulter_name(ast, element), Vec::new()),
})
}
fn primitive_default(kind: PrimitiveKind) -> kotlin::Expr {
match kind {
PrimitiveKind::Boolean => kotlin::Expr::Literal(Literal::Bool(false)),
PrimitiveKind::Char => kotlin::Expr::Literal(Literal::Char('\u{0}')),
PrimitiveKind::Float32 => kotlin::Expr::Literal(Literal::Float("0.0f".to_string())),
PrimitiveKind::Float64 => kotlin::Expr::Literal(Literal::Float("0.0".to_string())),
PrimitiveKind::Float128 => call_on(name("Float128"), "zero", Vec::new()),
PrimitiveKind::Int64 | PrimitiveKind::UInt32 | PrimitiveKind::UInt64 => {
kotlin::Expr::Literal(Literal::Int("0L".to_string()))
}
PrimitiveKind::Octet
| PrimitiveKind::UInt8
| PrimitiveKind::UInt16
| PrimitiveKind::WChar
| PrimitiveKind::Int8
| PrimitiveKind::Int16
| PrimitiveKind::Int32 => kotlin::Expr::int(0),
}
}
fn zero_of(holder: &kotlin::Type) -> kotlin::Expr {
if *holder == kotlin::Type::Primitive(kotlin::Primitive::Long) {
return kotlin::Expr::Literal(Literal::Int("0L".to_string()));
}
kotlin::Expr::int(0)
}
pub(crate) fn enumerator_constant(ast: &Ast, id: Id<TypeNode>, enumerator: &str) -> kotlin::Expr {
kotlin::Expr::field(name(&kotlin_type_name(ast, id)), &property_name(enumerator))
}
pub(crate) fn alias_slot_start(ast: &Ast, element: Id<TypeNode>) -> Option<kotlin::Expr> {
match ast.type_(element).map(|node| &node.kind) {
Ok(TypeKind::Alias { aliased }) => aliased
.resolved()
.and_then(|target| alias_zero(ast, target)),
_ => None,
}
}
fn alias_zero(ast: &Ast, target: Id<TypeNode>) -> Option<kotlin::Expr> {
let resolved = resolve_alias(ast, target);
match ast.type_(resolved).map(|node| &node.kind) {
Ok(TypeKind::Primitive(kind)) => Some(primitive_default(*kind)),
Ok(TypeKind::Enumeration { .. }) => crate::emit::default_enumerator_name(ast, resolved)
.map(|enumerator| enumerator_constant(ast, resolved, &enumerator)),
Ok(TypeKind::Bitmask { .. }) => Some(zero_of(&kotlin::Type::Primitive(unsigned_holder(
ast.effective_bit_bound(resolved),
)))),
_ => None,
}
}
fn selected_by_default(
ast: &Ast,
id: Id<TypeNode>,
discriminator: Id<TypeNode>,
) -> Result<Option<(Id<TypeNode>, String)>, EncodeError> {
let Some(selector) = discriminator_default_value(ast, discriminator) else {
return Ok(None);
};
Ok(selected_case(ast, id, selector)?
.map(|(member, member_type)| (member_type, property_name(&member.name))))
}
#[allow(clippy::too_many_lines)]
fn explicit(
ast: &Ast,
expr: Id<zerodds_ast::ConstExprNode>,
type_: Id<TypeNode>,
member_name: &str,
) -> Result<kotlin::Expr, EncodeError> {
let refuse = |kind: &'static str| EncodeError::NoWriter {
member: member_name.to_string(),
kind,
};
let node = ast
.expr(expr)
.map_err(|_| refuse("a `@default` this frontend cannot follow"))?;
let resolved = resolve_alias(ast, type_);
let kind = ast.type_(resolved).map(|node| node.kind.clone());
match &node.expr {
ConstExpr::Literal(AstLiteral::Boolean(flag)) => {
Ok(kotlin::Expr::Literal(Literal::Bool(*flag)))
}
ConstExpr::Literal(AstLiteral::Char(ch)) => {
point_literal(u32::from(*ch), &kind, member_name)
}
ConstExpr::Literal(AstLiteral::WChar(code)) => point_literal(*code, &kind, member_name),
ConstExpr::Literal(AstLiteral::String(value)) => Ok(text(value)),
ConstExpr::Literal(AstLiteral::WString(codes)) => {
let mut out = String::new();
for code in codes {
let Some(ch) = char::from_u32(*code) else {
return Err(refuse("a `@default` wide string that is not code points"));
};
out.push(ch);
}
Ok(text(&out))
}
ConstExpr::Literal(AstLiteral::Float { mantissa, exponent }) => {
let suffix = match kind {
Ok(TypeKind::Primitive(PrimitiveKind::Float32)) => "f",
_ => "",
};
let exponent = exponent
.as_ref()
.map_or(String::new(), |text| format!("e{text}"));
Ok(kotlin::Expr::Literal(Literal::Float(format!(
"{mantissa}{exponent}{suffix}"
))))
}
ConstExpr::Literal(AstLiteral::Fixed { digits, scale }) => {
Ok(text(&fixed_text(digits, *scale)))
}
ConstExpr::Literal(AstLiteral::Enumerator(reference)) => {
let target = reference
.resolved()
.ok_or_else(|| refuse("a `@default` enumerator that is unresolved"))?;
let enumerator = ast
.enumerator(target)
.map_err(|_| refuse("a `@default` enumerator this frontend cannot follow"))?;
Ok(enumerator_constant(ast, resolved, &enumerator.name))
}
_ => {
let value = ast
.eval_integral(expr)
.map_err(|_| refuse("a `@default` this frontend cannot evaluate"))?;
match kind {
Ok(TypeKind::Primitive(PrimitiveKind::Char | PrimitiveKind::WChar)) => {
let code = u32::try_from(value)
.map_err(|_| refuse("a `@default` outside the range of a code point"))?;
point_literal(code, &kind, member_name)
}
Ok(TypeKind::Primitive(
PrimitiveKind::Int64 | PrimitiveKind::UInt32 | PrimitiveKind::UInt64,
)) => Ok(wide(value)),
_ => Ok(kotlin::Expr::int(value)),
}
}
}
}
fn wide(value: i128) -> kotlin::Expr {
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
let pattern = if value > i128::from(i64::MAX) {
value as u64 as i64
} else {
value as i64
};
kotlin::Expr::Literal(Literal::Int(zerodds_lang_render::kotlin::long_text(
pattern,
)))
}
fn point_literal<E>(
code: u32,
kind: &Result<TypeKind, E>,
member_name: &str,
) -> Result<kotlin::Expr, EncodeError> {
if matches!(kind, Ok(TypeKind::Primitive(PrimitiveKind::Char))) {
let ch = char::from_u32(code).ok_or_else(|| EncodeError::NoWriter {
member: member_name.to_string(),
kind: "a `@default` character that is not a Unicode scalar value",
})?;
return Ok(kotlin::Expr::Literal(Literal::Char(ch)));
}
Ok(kotlin::Expr::int(i128::from(code)))
}
fn calls_in_decl(decl: &kotlin::Decl, out: &mut Vec<String>) {
match decl {
kotlin::Decl::Func(func) => calls_in_block(&func.body, out),
kotlin::Decl::Const(const_) => calls_in_expr(&const_.value, out),
kotlin::Decl::Class(_)
| kotlin::Decl::Object(_)
| kotlin::Decl::Alias(_)
| kotlin::Decl::Interface(_)
| kotlin::Decl::Comment(_)
| kotlin::Decl::Verbatim(_) => {}
}
}
fn calls_in_block(body: &[kotlin::Stmt], out: &mut Vec<String>) {
for statement in body {
calls_in_statement(statement, out);
}
}
fn calls_in_statement(statement: &kotlin::Stmt, out: &mut Vec<String>) {
match statement {
kotlin::Stmt::Declare { value, .. } => {
if let Some(value) = value {
calls_in_expr(value, out);
}
}
kotlin::Stmt::Expr(expr) | kotlin::Stmt::Throw(expr) => calls_in_expr(expr, out),
kotlin::Stmt::Return(value) => {
if let Some(value) = value {
calls_in_expr(value, out);
}
}
kotlin::Stmt::If {
condition,
then,
otherwise,
} => {
calls_in_expr(condition, out);
calls_in_block(&then.statements, out);
if let Some(otherwise) = otherwise {
calls_in_block(&otherwise.statements, out);
}
}
kotlin::Stmt::CountUp { bound, body, .. } => {
calls_in_expr(bound, out);
calls_in_block(&body.statements, out);
}
kotlin::Stmt::While { condition, body } => {
calls_in_expr(condition, out);
calls_in_block(&body.statements, out);
}
kotlin::Stmt::Break | kotlin::Stmt::Comment(_) => {}
}
}
#[allow(clippy::too_many_lines)]
fn calls_in_expr(expr: &kotlin::Expr, out: &mut Vec<String>) {
match expr {
kotlin::Expr::Call {
receiver,
name,
args,
} => {
if receiver.is_none() {
let name = name.as_str();
if name.starts_with("zd_def_") && !out.iter().any(|seen| seen == name) {
out.push(name.to_string());
}
}
if let Some(receiver) = receiver {
calls_in_expr(receiver, out);
}
for arg in args {
calls_in_expr(arg, out);
}
}
kotlin::Expr::Field { value, .. } | kotlin::Expr::Paren(value) => calls_in_expr(value, out),
kotlin::Expr::As { value, .. } => calls_in_expr(value, out),
kotlin::Expr::Index { value, index } => {
calls_in_expr(value, out);
calls_in_expr(index, out);
}
kotlin::Expr::New { args, .. } => {
for arg in args {
calls_in_expr(arg, out);
}
}
kotlin::Expr::NewArray { length, init, .. } => {
calls_in_expr(length, out);
if let Some(init) = init {
calls_in_expr(init, out);
}
}
kotlin::Expr::ArrayOf { elements, .. } => {
for element in elements {
calls_in_expr(element, out);
}
}
kotlin::Expr::Binary { lhs, rhs, .. } => {
calls_in_expr(lhs, out);
calls_in_expr(rhs, out);
}
kotlin::Expr::If {
condition,
then,
otherwise,
} => {
calls_in_expr(condition, out);
calls_in_expr(then, out);
calls_in_expr(otherwise, out);
}
kotlin::Expr::Not(value) | kotlin::Expr::NotNull(value) => calls_in_expr(value, out),
kotlin::Expr::Literal(_) | kotlin::Expr::Path(_) => {}
}
}
fn name(text: &str) -> kotlin::Expr {
kotlin::Expr::name(text)
}
fn text(value: &str) -> kotlin::Expr {
kotlin::Expr::Literal(Literal::Str(value.to_string()))
}
fn call(callee: &str, args: Vec<kotlin::Expr>) -> kotlin::Expr {
kotlin::Expr::call(callee, args)
}
fn call_on(receiver: kotlin::Expr, name: &str, args: Vec<kotlin::Expr>) -> kotlin::Expr {
kotlin::Expr::method_call(receiver, name, args)
}
fn member(base: &str, field: &str) -> kotlin::Expr {
kotlin::Expr::field(name(base), field)
}
fn assign(place: kotlin::Expr, value: kotlin::Expr) -> kotlin::Stmt {
kotlin::Stmt::Expr(kotlin::Expr::Binary {
op: "=",
lhs: Box::new(place),
rhs: Box::new(value),
})
}
fn give_back(value: kotlin::Expr) -> kotlin::Stmt {
kotlin::Stmt::Return(Some(value))
}
fn new_local(type_: kotlin::Type) -> kotlin::Stmt {
kotlin::Stmt::Declare {
mutable: false,
type_: type_.clone(),
name: Ident::new("v"),
value: Some(kotlin::Expr::New {
type_: Box::new(type_),
args: Vec::new(),
}),
}
}
const _: Option<MaterialiseError> = None;