Debian Codegraph @

third_party/rust/diplomat_core/src/hir/primitives.rs

raw
1 //! Primitives types that can cross the FFI boundary.
2 use crate::ast;
3
4 /// 8, 16, 32, and 64-bit signed and unsigned integers.
5 #[derive(Copy, Clone, Debug)]
6 #[allow(clippy::exhaustive_enums)] // there are only these
7 pub enum IntType {
8 I8,
9 I16,
10 I32,
11 I64,
12 U8,
13 U16,
14 U32,
15 U64,
16 }
17
18 /// Platform-dependent signed and unsigned size types.
19 #[derive(Copy, Clone, Debug)]
20 #[allow(clippy::exhaustive_enums)] // there are only these
21 pub enum IntSizeType {
22 Isize,
23 Usize,
24 }
25
26 /// 128-bit signed and unsigned integers.
27 #[derive(Copy, Clone, Debug)]
28 #[allow(clippy::exhaustive_enums)] // there are only these
29 pub enum Int128Type {
30 I128,
31 U128,
32 }
33
34 /// 32 and 64-bit floating point numbers.
35 #[derive(Copy, Clone, Debug)]
36 #[allow(clippy::exhaustive_enums)] // there are only these
37 pub enum FloatType {
38 F32,
39 F64,
40 }
41
42 /// All primitive types.
43 #[derive(Copy, Clone, Debug)]
44 #[allow(clippy::exhaustive_enums)] // there are only these
45 pub enum PrimitiveType {
46 Bool,
47 Char,
48 /// a primitive byte that is not meant to be interpreted numerically
49 /// in languages that don't have fine-grained integer types
50 Byte,
51 Int(IntType),
52 IntSize(IntSizeType),
53 Int128(Int128Type),
54 Float(FloatType),
55 }
56
57 impl IntType {
58 /// Returns the string representation of `self`.
59 pub fn as_str(&self) -> &'static str {
60 match self {
61 IntType::I8 => "i8",
62 IntType::I16 => "i16",
63 IntType::I32 => "i32",
64 IntType::I64 => "i64",
65 IntType::U8 => "u8",
66 IntType::U16 => "u16",
67 IntType::U32 => "u32",
68 IntType::U64 => "u64",
69 }
70 }
71 }
72
73 impl IntSizeType {
74 /// Returns the string representation of `self`.
75 pub fn as_str(&self) -> &'static str {
76 match self {
77 IntSizeType::Isize => "isize",
78 IntSizeType::Usize => "usize",
79 }
80 }
81 }
82
83 impl Int128Type {
84 /// Returns the string representation of `self`.
85 pub fn as_str(&self) -> &'static str {
86 match self {
87 Int128Type::I128 => "i128",
88 Int128Type::U128 => "u128",
89 }
90 }
91 }
92
93 impl FloatType {
94 /// Returns the string representation of `self`.
95 pub fn as_str(&self) -> &'static str {
96 match self {
97 FloatType::F32 => "f32",
98 FloatType::F64 => "f64",
99 }
100 }
101 }
102
103 impl PrimitiveType {
104 pub(super) fn from_ast(prim: ast::PrimitiveType) -> Self {
105 match prim {
106 ast::PrimitiveType::i8 => PrimitiveType::Int(IntType::I8),
107 ast::PrimitiveType::u8 => PrimitiveType::Int(IntType::U8),
108 ast::PrimitiveType::i16 => PrimitiveType::Int(IntType::I16),
109 ast::PrimitiveType::u16 => PrimitiveType::Int(IntType::U16),
110 ast::PrimitiveType::i32 => PrimitiveType::Int(IntType::I32),
111 ast::PrimitiveType::u32 => PrimitiveType::Int(IntType::U32),
112 ast::PrimitiveType::i64 => PrimitiveType::Int(IntType::I64),
113 ast::PrimitiveType::u64 => PrimitiveType::Int(IntType::U64),
114 ast::PrimitiveType::isize => PrimitiveType::IntSize(IntSizeType::Isize),
115 ast::PrimitiveType::usize => PrimitiveType::IntSize(IntSizeType::Usize),
116 ast::PrimitiveType::i128 => PrimitiveType::Int128(Int128Type::I128),
117 ast::PrimitiveType::u128 => PrimitiveType::Int128(Int128Type::U128),
118 ast::PrimitiveType::f32 => PrimitiveType::Float(FloatType::F32),
119 ast::PrimitiveType::f64 => PrimitiveType::Float(FloatType::F64),
120 ast::PrimitiveType::bool => PrimitiveType::Bool,
121 ast::PrimitiveType::char => PrimitiveType::Char,
122 ast::PrimitiveType::byte => PrimitiveType::Byte,
123 }
124 }
125
126 /// Returns the string representation of `self`.
127 pub fn as_str(&self) -> &'static str {
128 match self {
129 PrimitiveType::Bool => "bool",
130 PrimitiveType::Char => "char",
131 PrimitiveType::Byte => "byte",
132 PrimitiveType::Int(ty) => ty.as_str(),
133 PrimitiveType::IntSize(ty) => ty.as_str(),
134 PrimitiveType::Int128(ty) => ty.as_str(),
135 PrimitiveType::Float(ty) => ty.as_str(),
136 }
137 }
138 }