ferro_jtype/types/
integral.rs1use crate::PrimitiveType;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
12pub struct IntegralTypeSet(u8);
13
14impl IntegralTypeSet {
15 const BOOLEAN_BIT: u8 = 1 << 0;
16 const BYTE_BIT: u8 = 1 << 1;
17 const CHAR_BIT: u8 = 1 << 2;
18 const SHORT_BIT: u8 = 1 << 3;
19 const INT_BIT: u8 = 1 << 4;
20
21 pub const BOOLEAN: Self = Self(Self::BOOLEAN_BIT);
23 pub const BYTE: Self = Self(Self::BYTE_BIT);
25 pub const CHAR: Self = Self(Self::CHAR_BIT);
27 pub const SHORT: Self = Self(Self::SHORT_BIT);
29 pub const INT: Self = Self(Self::INT_BIT);
31 pub const ALL: Self =
33 Self(Self::BOOLEAN_BIT | Self::BYTE_BIT | Self::CHAR_BIT | Self::SHORT_BIT | Self::INT_BIT);
34
35 #[must_use]
37 pub const fn contains(self, primitive: PrimitiveType) -> bool {
38 let bit = match primitive {
39 PrimitiveType::Boolean => Self::BOOLEAN_BIT,
40 PrimitiveType::Byte => Self::BYTE_BIT,
41 PrimitiveType::Char => Self::CHAR_BIT,
42 PrimitiveType::Short => Self::SHORT_BIT,
43 PrimitiveType::Int => Self::INT_BIT,
44 PrimitiveType::Float | PrimitiveType::Long | PrimitiveType::Double => return false,
45 };
46 self.0 & bit != 0
47 }
48
49 #[must_use]
51 pub const fn exact_type(self) -> Option<PrimitiveType> {
52 match self.0 {
53 Self::BOOLEAN_BIT => Some(PrimitiveType::Boolean),
54 Self::BYTE_BIT => Some(PrimitiveType::Byte),
55 Self::CHAR_BIT => Some(PrimitiveType::Char),
56 Self::SHORT_BIT => Some(PrimitiveType::Short),
57 Self::INT_BIT => Some(PrimitiveType::Int),
58 _ => None,
59 }
60 }
61
62 #[must_use]
64 pub const fn is_exact(self) -> bool {
65 self.exact_type().is_some()
66 }
67
68 pub(crate) const fn from_primitive(primitive: PrimitiveType) -> Option<Self> {
69 match primitive {
70 PrimitiveType::Boolean => Some(Self::BOOLEAN),
71 PrimitiveType::Byte => Some(Self::BYTE),
72 PrimitiveType::Char => Some(Self::CHAR),
73 PrimitiveType::Short => Some(Self::SHORT),
74 PrimitiveType::Int => Some(Self::INT),
75 PrimitiveType::Float | PrimitiveType::Long | PrimitiveType::Double => None,
76 }
77 }
78
79 pub(crate) const fn union(self, other: Self) -> Self {
80 Self(self.0 | other.0)
81 }
82
83 pub(crate) fn for_constant(value: i32) -> Self {
84 let mut types = Self::INT;
85 if matches!(value, 0 | 1) {
86 types = types.union(Self::BOOLEAN);
87 }
88 if i8::try_from(value).is_ok() {
89 types = types.union(Self::BYTE);
90 }
91 if i16::try_from(value).is_ok() {
92 types = types.union(Self::SHORT);
93 }
94 if u16::try_from(value).is_ok() {
95 types = types.union(Self::CHAR);
96 }
97 types
98 }
99}