1use rust_asm::constant_pool::CpInfo;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4pub struct ConstantPoolIndex(u16);
10
11impl ConstantPoolIndex {
12 pub(crate) const fn new(index: u16) -> Self {
13 Self(index)
14 }
15
16 #[must_use]
18 pub const fn get(self) -> u16 {
19 self.0
20 }
21}
22
23#[derive(Debug, Clone, Copy)]
24pub enum ConstantRef<'a> {
29 Unusable,
31 Utf8(&'a str),
33 Integer(i32),
35 Float(f32),
37 Long(i64),
39 Double(f64),
41 Class {
43 name: ConstantPoolIndex,
45 },
46 String {
48 value: ConstantPoolIndex,
50 },
51 FieldReference {
53 class: ConstantPoolIndex,
55 name_and_type: ConstantPoolIndex,
57 },
58 MethodReference {
60 class: ConstantPoolIndex,
62 name_and_type: ConstantPoolIndex,
64 },
65 InterfaceMethodReference {
67 class: ConstantPoolIndex,
69 name_and_type: ConstantPoolIndex,
71 },
72 NameAndType {
74 name: ConstantPoolIndex,
76 descriptor: ConstantPoolIndex,
78 },
79 MethodHandle {
81 reference_kind: u8,
83 reference: ConstantPoolIndex,
85 },
86 MethodType {
88 descriptor: ConstantPoolIndex,
90 },
91 Dynamic {
93 bootstrap_method: u16,
95 name_and_type: ConstantPoolIndex,
97 },
98 InvokeDynamic {
100 bootstrap_method: u16,
102 name_and_type: ConstantPoolIndex,
104 },
105 Module {
107 name: ConstantPoolIndex,
109 },
110 Package {
112 name: ConstantPoolIndex,
114 },
115}
116
117impl<'a> From<&'a CpInfo> for ConstantRef<'a> {
118 fn from(value: &'a CpInfo) -> Self {
119 match value {
120 CpInfo::Unusable => Self::Unusable,
121 CpInfo::Utf8(value) => Self::Utf8(value),
122 CpInfo::Integer(value) => Self::Integer(*value),
123 CpInfo::Float(value) => Self::Float(*value),
124 CpInfo::Long(value) => Self::Long(*value),
125 CpInfo::Double(value) => Self::Double(*value),
126 CpInfo::Class { name_index } => Self::Class {
127 name: ConstantPoolIndex::new(*name_index),
128 },
129 CpInfo::String { string_index } => Self::String {
130 value: ConstantPoolIndex::new(*string_index),
131 },
132 CpInfo::Fieldref {
133 class_index,
134 name_and_type_index,
135 } => Self::FieldReference {
136 class: ConstantPoolIndex::new(*class_index),
137 name_and_type: ConstantPoolIndex::new(*name_and_type_index),
138 },
139 CpInfo::Methodref {
140 class_index,
141 name_and_type_index,
142 } => Self::MethodReference {
143 class: ConstantPoolIndex::new(*class_index),
144 name_and_type: ConstantPoolIndex::new(*name_and_type_index),
145 },
146 CpInfo::InterfaceMethodref {
147 class_index,
148 name_and_type_index,
149 } => Self::InterfaceMethodReference {
150 class: ConstantPoolIndex::new(*class_index),
151 name_and_type: ConstantPoolIndex::new(*name_and_type_index),
152 },
153 CpInfo::NameAndType {
154 name_index,
155 descriptor_index,
156 } => Self::NameAndType {
157 name: ConstantPoolIndex::new(*name_index),
158 descriptor: ConstantPoolIndex::new(*descriptor_index),
159 },
160 CpInfo::MethodHandle {
161 reference_kind,
162 reference_index,
163 } => Self::MethodHandle {
164 reference_kind: *reference_kind,
165 reference: ConstantPoolIndex::new(*reference_index),
166 },
167 CpInfo::MethodType { descriptor_index } => Self::MethodType {
168 descriptor: ConstantPoolIndex::new(*descriptor_index),
169 },
170 CpInfo::Dynamic {
171 bootstrap_method_attr_index,
172 name_and_type_index,
173 } => Self::Dynamic {
174 bootstrap_method: *bootstrap_method_attr_index,
175 name_and_type: ConstantPoolIndex::new(*name_and_type_index),
176 },
177 CpInfo::InvokeDynamic {
178 bootstrap_method_attr_index,
179 name_and_type_index,
180 } => Self::InvokeDynamic {
181 bootstrap_method: *bootstrap_method_attr_index,
182 name_and_type: ConstantPoolIndex::new(*name_and_type_index),
183 },
184 CpInfo::Module { name_index } => Self::Module {
185 name: ConstantPoolIndex::new(*name_index),
186 },
187 CpInfo::Package { name_index } => Self::Package {
188 name: ConstantPoolIndex::new(*name_index),
189 },
190 }
191 }
192}