Skip to main content

ferro_babe/model/
constant.rs

1use rust_asm::constant_pool::CpInfo;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4pub struct ConstantPoolIndex(u16);
5
6impl ConstantPoolIndex {
7    pub(crate) const fn new(index: u16) -> Self {
8        Self(index)
9    }
10
11    #[must_use]
12    pub const fn get(self) -> u16 {
13        self.0
14    }
15}
16
17#[derive(Debug, Clone, Copy)]
18pub enum ConstantRef<'a> {
19    Unusable,
20    Utf8(&'a str),
21    Integer(i32),
22    Float(f32),
23    Long(i64),
24    Double(f64),
25    Class {
26        name: ConstantPoolIndex,
27    },
28    String {
29        value: ConstantPoolIndex,
30    },
31    FieldReference {
32        class: ConstantPoolIndex,
33        name_and_type: ConstantPoolIndex,
34    },
35    MethodReference {
36        class: ConstantPoolIndex,
37        name_and_type: ConstantPoolIndex,
38    },
39    InterfaceMethodReference {
40        class: ConstantPoolIndex,
41        name_and_type: ConstantPoolIndex,
42    },
43    NameAndType {
44        name: ConstantPoolIndex,
45        descriptor: ConstantPoolIndex,
46    },
47    MethodHandle {
48        reference_kind: u8,
49        reference: ConstantPoolIndex,
50    },
51    MethodType {
52        descriptor: ConstantPoolIndex,
53    },
54    Dynamic {
55        bootstrap_method: u16,
56        name_and_type: ConstantPoolIndex,
57    },
58    InvokeDynamic {
59        bootstrap_method: u16,
60        name_and_type: ConstantPoolIndex,
61    },
62    Module {
63        name: ConstantPoolIndex,
64    },
65    Package {
66        name: ConstantPoolIndex,
67    },
68}
69
70impl<'a> From<&'a CpInfo> for ConstantRef<'a> {
71    fn from(value: &'a CpInfo) -> Self {
72        match value {
73            CpInfo::Unusable => Self::Unusable,
74            CpInfo::Utf8(value) => Self::Utf8(value),
75            CpInfo::Integer(value) => Self::Integer(*value),
76            CpInfo::Float(value) => Self::Float(*value),
77            CpInfo::Long(value) => Self::Long(*value),
78            CpInfo::Double(value) => Self::Double(*value),
79            CpInfo::Class { name_index } => Self::Class {
80                name: ConstantPoolIndex::new(*name_index),
81            },
82            CpInfo::String { string_index } => Self::String {
83                value: ConstantPoolIndex::new(*string_index),
84            },
85            CpInfo::Fieldref {
86                class_index,
87                name_and_type_index,
88            } => Self::FieldReference {
89                class: ConstantPoolIndex::new(*class_index),
90                name_and_type: ConstantPoolIndex::new(*name_and_type_index),
91            },
92            CpInfo::Methodref {
93                class_index,
94                name_and_type_index,
95            } => Self::MethodReference {
96                class: ConstantPoolIndex::new(*class_index),
97                name_and_type: ConstantPoolIndex::new(*name_and_type_index),
98            },
99            CpInfo::InterfaceMethodref {
100                class_index,
101                name_and_type_index,
102            } => Self::InterfaceMethodReference {
103                class: ConstantPoolIndex::new(*class_index),
104                name_and_type: ConstantPoolIndex::new(*name_and_type_index),
105            },
106            CpInfo::NameAndType {
107                name_index,
108                descriptor_index,
109            } => Self::NameAndType {
110                name: ConstantPoolIndex::new(*name_index),
111                descriptor: ConstantPoolIndex::new(*descriptor_index),
112            },
113            CpInfo::MethodHandle {
114                reference_kind,
115                reference_index,
116            } => Self::MethodHandle {
117                reference_kind: *reference_kind,
118                reference: ConstantPoolIndex::new(*reference_index),
119            },
120            CpInfo::MethodType { descriptor_index } => Self::MethodType {
121                descriptor: ConstantPoolIndex::new(*descriptor_index),
122            },
123            CpInfo::Dynamic {
124                bootstrap_method_attr_index,
125                name_and_type_index,
126            } => Self::Dynamic {
127                bootstrap_method: *bootstrap_method_attr_index,
128                name_and_type: ConstantPoolIndex::new(*name_and_type_index),
129            },
130            CpInfo::InvokeDynamic {
131                bootstrap_method_attr_index,
132                name_and_type_index,
133            } => Self::InvokeDynamic {
134                bootstrap_method: *bootstrap_method_attr_index,
135                name_and_type: ConstantPoolIndex::new(*name_and_type_index),
136            },
137            CpInfo::Module { name_index } => Self::Module {
138                name: ConstantPoolIndex::new(*name_index),
139            },
140            CpInfo::Package { name_index } => Self::Package {
141                name: ConstantPoolIndex::new(*name_index),
142            },
143        }
144    }
145}