Skip to main content

ferro_jtype/types/
integral.rs

1use crate::PrimitiveType;
2
3/// The source-level primitive types still compatible with an integral JVM value.
4///
5/// The JVM verifier uses one integral category for `boolean`, `byte`, `char`,
6/// `short`, and `int`. This set preserves the narrower information supplied by
7/// descriptors and instructions without inventing a single source type when
8/// the bytecode cannot prove one.
9/// Integer constants retain only types whose value ranges contain the constant;
10/// zero and one remain ambiguous between boolean and numeric uses.
11#[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    /// The exact `boolean` type.
22    pub const BOOLEAN: Self = Self(Self::BOOLEAN_BIT);
23    /// The exact `byte` type.
24    pub const BYTE: Self = Self(Self::BYTE_BIT);
25    /// The exact `char` type.
26    pub const CHAR: Self = Self(Self::CHAR_BIT);
27    /// The exact `short` type.
28    pub const SHORT: Self = Self(Self::SHORT_BIT);
29    /// The exact `int` type.
30    pub const INT: Self = Self(Self::INT_BIT);
31    /// Every source-level type represented by the JVM integral verification category.
32    pub const ALL: Self =
33        Self(Self::BOOLEAN_BIT | Self::BYTE_BIT | Self::CHAR_BIT | Self::SHORT_BIT | Self::INT_BIT);
34
35    /// Returns whether `primitive` remains a candidate.
36    #[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    /// Returns the exact primitive type when this set has one candidate.
50    #[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    /// Returns whether this set has exactly one candidate.
63    #[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}