| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use super::CodeType; |
| 6 |
use crate::{ |
| 7 |
backend::Literal, |
| 8 |
bail, |
| 9 |
interface::{ComponentInterface, Radix, Type}, |
| 10 |
Result, |
| 11 |
}; |
| 12 |
|
| 13 |
fn render_literal(literal: &Literal, _ci: &ComponentInterface) -> Result<String> { |
| 14 |
fn typed_number(type_: &Type, num_str: String) -> Result<String> { |
| 15 |
let unwrapped_type = match type_ { |
| 16 |
Type::Optional { inner_type } => inner_type, |
| 17 |
t => t, |
| 18 |
}; |
| 19 |
Ok(match unwrapped_type { |
| 20 |
// Bytes, Shorts and Ints can all be inferred from the type. |
| 21 |
Type::Int8 | Type::Int16 | Type::Int32 => num_str, |
| 22 |
Type::Int64 => format!("{num_str}L"), |
| 23 |
|
| 24 |
Type::UInt8 | Type::UInt16 | Type::UInt32 => format!("{num_str}u"), |
| 25 |
Type::UInt64 => format!("{num_str}uL"), |
| 26 |
|
| 27 |
Type::Float32 => format!("{num_str}f"), |
| 28 |
Type::Float64 => num_str, |
| 29 |
_ => bail!("Unexpected literal: {num_str} for type: {type_:?}"), |
| 30 |
}) |
| 31 |
} |
| 32 |
|
| 33 |
match literal { |
| 34 |
Literal::Boolean(v) => Ok(format!("{v}")), |
| 35 |
Literal::String(s) => Ok(format!("\"{s}\"")), |
| 36 |
Literal::Int(i, radix, type_) => typed_number( |
| 37 |
type_, |
| 38 |
match radix { |
| 39 |
Radix::Octal => format!("{i:#x}"), |
| 40 |
Radix::Decimal => format!("{i}"), |
| 41 |
Radix::Hexadecimal => format!("{i:#x}"), |
| 42 |
}, |
| 43 |
), |
| 44 |
Literal::UInt(i, radix, type_) => typed_number( |
| 45 |
type_, |
| 46 |
match radix { |
| 47 |
Radix::Octal => format!("{i:#x}"), |
| 48 |
Radix::Decimal => format!("{i}"), |
| 49 |
Radix::Hexadecimal => format!("{i:#x}"), |
| 50 |
}, |
| 51 |
), |
| 52 |
Literal::Float(string, type_) => typed_number(type_, string.clone()), |
| 53 |
|
| 54 |
_ => bail!("Invalid literal {literal:?}"), |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
macro_rules! impl_code_type_for_primitive { |
| 59 |
($T:ident, $class_name:literal) => { |
| 60 |
#[derive(Debug)] |
| 61 |
pub struct $T; |
| 62 |
|
| 63 |
impl CodeType for $T { |
| 64 |
fn type_label(&self, _ci: &ComponentInterface) -> String { |
| 65 |
format!("kotlin.{}", $class_name) |
| 66 |
} |
| 67 |
|
| 68 |
fn canonical_name(&self) -> String { |
| 69 |
$class_name.into() |
| 70 |
} |
| 71 |
|
| 72 |
fn literal(&self, literal: &Literal, ci: &ComponentInterface) -> Result<String> { |
| 73 |
render_literal(&literal, ci) |
| 74 |
} |
| 75 |
} |
| 76 |
}; |
| 77 |
} |
| 78 |
|
| 79 |
impl_code_type_for_primitive!(BooleanCodeType, "Boolean"); |
| 80 |
impl_code_type_for_primitive!(StringCodeType, "String"); |
| 81 |
impl_code_type_for_primitive!(BytesCodeType, "ByteArray"); |
| 82 |
impl_code_type_for_primitive!(Int8CodeType, "Byte"); |
| 83 |
impl_code_type_for_primitive!(Int16CodeType, "Short"); |
| 84 |
impl_code_type_for_primitive!(Int32CodeType, "Int"); |
| 85 |
impl_code_type_for_primitive!(Int64CodeType, "Long"); |
| 86 |
impl_code_type_for_primitive!(UInt8CodeType, "UByte"); |
| 87 |
impl_code_type_for_primitive!(UInt16CodeType, "UShort"); |
| 88 |
impl_code_type_for_primitive!(UInt32CodeType, "UInt"); |
| 89 |
impl_code_type_for_primitive!(UInt64CodeType, "ULong"); |
| 90 |
impl_code_type_for_primitive!(Float32CodeType, "Float"); |
| 91 |
impl_code_type_for_primitive!(Float64CodeType, "Double"); |