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/. */
6 use crate::backend::Literal;
7 use crate::interface::{Radix, Type};
10 fn render_literal(literal: &Literal) -> String {
11 fn typed_number(type_: &Type, num_str: String) -> String {
13 // special case Int32.
14 Type::Int32 => num_str,
15 // otherwise use constructor e.g. UInt8(x)
25 // XXX we should pass in the codetype itself.
29 super::SwiftCodeOracle.find(type_).type_label()
32 _ => panic!("Unexpected literal: {num_str} is not a number"),
37 Literal::Boolean(v) => format!("{v}"),
38 Literal::String(s) => format!("\"{s}\""),
39 Literal::Int(i, radix, type_) => typed_number(
42 Radix::Octal => format!("0o{i:o}"),
43 Radix::Decimal => format!("{i}"),
44 Radix::Hexadecimal => format!("{i:#x}"),
47 Literal::UInt(i, radix, type_) => typed_number(
50 Radix::Octal => format!("0o{i:o}"),
51 Radix::Decimal => format!("{i}"),
52 Radix::Hexadecimal => format!("{i:#x}"),
55 Literal::Float(string, type_) => typed_number(type_, string.clone()),
56 _ => unreachable!("Literal"),
60 macro_rules! impl_code_type_for_primitive {
61 ($T:ty, $class_name:literal) => {
66 impl CodeType for $T {
67 fn type_label(&self) -> String {
71 fn literal(&self, literal: &Literal) -> String {
72 render_literal(&literal)
79 impl_code_type_for_primitive!(BooleanCodeType, "Bool");
80 impl_code_type_for_primitive!(StringCodeType, "String");
81 impl_code_type_for_primitive!(BytesCodeType, "Data");
82 impl_code_type_for_primitive!(Int8CodeType, "Int8");
83 impl_code_type_for_primitive!(Int16CodeType, "Int16");
84 impl_code_type_for_primitive!(Int32CodeType, "Int32");
85 impl_code_type_for_primitive!(Int64CodeType, "Int64");
86 impl_code_type_for_primitive!(UInt8CodeType, "UInt8");
87 impl_code_type_for_primitive!(UInt16CodeType, "UInt16");
88 impl_code_type_for_primitive!(UInt32CodeType, "UInt32");
89 impl_code_type_for_primitive!(UInt64CodeType, "UInt64");
90 impl_code_type_for_primitive!(Float32CodeType, "Float");
91 impl_code_type_for_primitive!(Float64CodeType, "Double");