1use super::{
2 well_known::{SystemType, TypeName, WellKnown},
3 Constant, CustomAttribute, CustomAttributeCtor, DeclSecurity, Entry, EntryName, ImplMap,
4 MethodDefOrRef, TypeDefOrRef, WeakEntry,
5};
6use crate::dotnet::{
7 il::MethodBody,
8 md::streams::tables_stream::{
9 EventFlags, FieldFlags, GenericParamFlags, MethodFlags, MethodImplFlags, ParamFlags,
10 PropertyFlags, TypeAttributes,
11 },
12 signature::{FieldSig, MethodSig, PropertySig},
13};
14use strum::IntoStaticStr;
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, IntoStaticStr)]
18#[cfg_attr(feature = "reflect", derive(facet::Facet))]
19#[cfg_attr(feature = "reflect", repr(u8))]
20pub enum TypeKind {
21 #[strum(serialize = "class")]
22 Class,
23 #[strum(serialize = "struct")]
24 ValueType,
25 #[strum(serialize = "interface")]
26 Interface,
27 #[strum(serialize = "enum")]
28 Enum,
29 #[strum(serialize = "delegate")]
30 Delegate,
31}
32
33impl TypeKind {
34 pub fn keyword(self) -> &'static str {
36 self.into()
37 }
38}
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
41#[cfg_attr(feature = "reflect", derive(facet::Facet))]
42pub struct ClassLayout {
43 pub packing_size: u16,
44 pub class_size: u32,
45}
46
47#[derive(Debug, Clone, PartialEq, Eq)]
48#[cfg_attr(feature = "reflect", derive(facet::Facet))]
49pub struct InterfaceImpl {
50 pub interface: TypeDefOrRef,
51 pub custom_attributes: Vec<CustomAttribute>,
52}
53
54impl InterfaceImpl {
55 pub fn new(interface: TypeDefOrRef) -> Self {
56 Self {
57 interface,
58 custom_attributes: Vec::new(),
59 }
60 }
61}
62
63#[derive(Debug, Clone, PartialEq, Eq)]
65#[cfg_attr(feature = "reflect", derive(facet::Facet))]
66pub struct MethodImpl {
67 pub body: MethodDefOrRef,
68 pub declaration: MethodDefOrRef,
69}
70
71#[derive(Debug, Clone, Default)]
72#[cfg_attr(feature = "reflect", derive(facet::Facet))]
73pub struct TypeDef {
74 pub flags: TypeAttributes,
75 pub name: String,
76 pub namespace: String,
77 pub extends: Option<TypeDefOrRef>,
78 pub fields: Vec<Entry<Field>>,
79 pub methods: Vec<Entry<Method>>,
80 pub properties: Vec<Entry<Property>>,
81 pub events: Vec<Entry<Event>>,
82 pub nested_types: Vec<Entry<TypeDef>>,
83 pub generic_params: Vec<Entry<GenericParam>>,
84 pub interfaces: Vec<InterfaceImpl>,
85 pub method_impls: Vec<MethodImpl>,
86 pub class_layout: Option<ClassLayout>,
87 pub security: Vec<DeclSecurity>,
88 pub custom_attributes: Vec<CustomAttribute>,
89 pub declaring_type: WeakEntry<TypeDef>,
90}
91
92impl TypeDef {
93 pub fn new(
94 flags: TypeAttributes,
95 namespace: impl Into<String>,
96 name: impl Into<String>,
97 extends: Option<TypeDefOrRef>,
98 ) -> Self {
99 Self {
100 flags,
101 name: name.into(),
102 namespace: namespace.into(),
103 extends,
104 ..Default::default()
105 }
106 }
107
108 pub fn new_static_class(
110 namespace: impl Into<String>,
111 name: impl Into<String>,
112 object: TypeDefOrRef,
113 ) -> Self {
114 Self::new(
115 TypeAttributes::Public | TypeAttributes::Abstract | TypeAttributes::Sealed,
116 namespace,
117 name,
118 Some(object),
119 )
120 }
121
122 pub fn is_visible(&self) -> bool {
124 let visibility = self.flags & TypeAttributes::VisibilityMask;
125 (visibility == TypeAttributes::Public || visibility == TypeAttributes::NestedPublic)
126 && self
127 .declaring_type
128 .upgrade()
129 .is_none_or(|parent| parent.get().is_visible())
130 }
131
132 pub fn top_level_namespace(&self) -> String {
134 match self.declaring_type.upgrade() {
135 Some(parent) => parent.get().top_level_namespace(),
136 None => self.namespace.clone(),
137 }
138 }
139
140 pub fn full_name(&self) -> String {
141 if let Some(parent) = self.declaring_type.upgrade() {
142 format!("{}/{}", parent.get().full_name(), self.name)
143 } else if self.namespace.is_empty() {
144 self.name.clone()
145 } else {
146 format!("{}.{}", self.namespace, self.name)
147 }
148 }
149
150 pub fn full_name_is(&self, ty: impl TypeName) -> bool {
151 let (namespace, name) = ty.type_name();
152 self.namespace.as_str() == &*namespace && self.name.as_str() == &*name
153 }
154
155 pub fn extends_type(&self, ty: impl TypeName) -> bool {
157 self.extends.as_ref().is_some_and(|e| e.full_name_is(ty))
158 }
159
160 pub fn extends_system_type(&self, system_type: SystemType) -> bool {
161 self.extends_type(WellKnown::System(system_type))
162 }
163
164 pub fn kind(&self) -> TypeKind {
166 if self.is_enum() {
167 TypeKind::Enum
168 } else if self.is_interface() {
169 TypeKind::Interface
170 } else if self.is_delegate() {
171 TypeKind::Delegate
172 } else if self.is_value_type() {
173 TypeKind::ValueType
174 } else {
175 TypeKind::Class
176 }
177 }
178
179 pub fn is_interface(&self) -> bool {
180 self.flags & TypeAttributes::ClassSemanticsMask == TypeAttributes::Interface
181 }
182
183 pub fn is_enum(&self) -> bool {
184 self.extends_system_type(SystemType::Enum)
185 }
186
187 pub fn is_value_type(&self) -> bool {
188 self.is_enum()
189 || (self.extends_system_type(SystemType::ValueType)
190 && !self.full_name_is(SystemType::Enum))
191 }
192
193 pub fn is_delegate(&self) -> bool {
194 self.extends_system_type(SystemType::MulticastDelegate)
195 }
196
197 pub fn is_static(&self) -> bool {
198 !self.is_interface()
199 && self
200 .flags
201 .contains(TypeAttributes::Abstract | TypeAttributes::Sealed)
202 }
203
204 pub fn is_nested(&self) -> bool {
205 self.declaring_type.upgrade().is_some()
206 }
207
208 pub fn enum_underlying_field(&self) -> Option<Entry<Field>> {
210 self.fields
211 .iter()
212 .find(|f| {
213 let f = f.get();
214 !f.flags.contains(FieldFlags::Static) && f.flags.contains(FieldFlags::RTSpecialName)
215 })
216 .cloned()
217 }
218
219 pub fn find_method(&self, name: &str) -> Option<Entry<Method>> {
220 self.methods.iter().find(|m| m.get().name == name).cloned()
221 }
222
223 pub fn find_field(&self, name: &str) -> Option<Entry<Field>> {
224 self.fields.iter().find(|f| f.get().name == name).cloned()
225 }
226
227 pub fn add_field(this: &Entry<TypeDef>, field: Field) -> Entry<Field> {
228 this.add_field(field)
229 }
230
231 pub fn add_method(this: &Entry<TypeDef>, method: Method) -> Entry<Method> {
232 this.add_method(method)
233 }
234
235 pub fn add_nested_type(this: &Entry<TypeDef>, ty: TypeDef) -> Entry<TypeDef> {
236 this.add_nested_type(ty)
237 }
238
239 pub fn with_nested(this: &Entry<TypeDef>) -> Vec<Entry<TypeDef>> {
241 this.with_nested()
242 }
243}
244
245impl Entry<TypeDef> {
249 pub fn add_field(&self, mut field: Field) -> Entry<Field> {
250 field.declaring_type = self.downgrade();
251 let field = Entry::new(field);
252 self.get_mut().fields.push(field.clone());
253 field
254 }
255
256 pub fn add_method(&self, mut method: Method) -> Entry<Method> {
257 method.declaring_type = self.downgrade();
258 let method = Entry::new(method);
259 self.get_mut().methods.push(method.clone());
260 method
261 }
262
263 pub fn add_nested_type(&self, mut ty: TypeDef) -> Entry<TypeDef> {
264 ty.declaring_type = self.downgrade();
265 let ty = Entry::new(ty);
266 self.get_mut().nested_types.push(ty.clone());
267 ty
268 }
269
270 pub fn with_nested(&self) -> Vec<Entry<TypeDef>> {
272 let mut out = vec![self.clone()];
273 for nested in &self.get().nested_types {
274 out.extend(nested.with_nested());
275 }
276 out
277 }
278}
279
280impl EntryName for TypeDef {
281 fn entry_name(&self) -> String {
282 self.full_name()
283 }
284}
285
286#[derive(Debug, Clone, Default)]
287#[cfg_attr(feature = "reflect", derive(facet::Facet))]
288pub struct Field {
289 pub flags: FieldFlags,
290 pub name: String,
291 pub signature: FieldSig,
292 pub constant: Option<Constant>,
293 pub offset: Option<u32>,
295 pub marshal: Option<Vec<u8>>,
296 pub initial_value: Option<Vec<u8>>,
298 pub impl_map: Option<ImplMap>,
299 pub custom_attributes: Vec<CustomAttribute>,
300 pub declaring_type: WeakEntry<TypeDef>,
301}
302
303impl Field {
304 pub fn new(flags: FieldFlags, name: impl Into<String>, signature: FieldSig) -> Self {
305 Self {
306 flags,
307 name: name.into(),
308 signature,
309 ..Default::default()
310 }
311 }
312
313 pub fn is_static(&self) -> bool {
314 self.flags.contains(FieldFlags::Static)
315 }
316}
317
318impl EntryName for Field {
319 fn entry_name(&self) -> String {
320 self.name.clone()
321 }
322}
323
324#[derive(Debug, Clone, Default)]
325#[cfg_attr(feature = "reflect", derive(facet::Facet))]
326pub struct Method {
327 pub flags: MethodFlags,
328 pub impl_flags: MethodImplFlags,
329 pub name: String,
330 pub signature: MethodSig,
331 pub params: Vec<Entry<Param>>,
332 pub body: Option<MethodBody>,
333 pub generic_params: Vec<Entry<GenericParam>>,
334 pub impl_map: Option<ImplMap>,
335 pub security: Vec<DeclSecurity>,
336 pub custom_attributes: Vec<CustomAttribute>,
337 pub declaring_type: WeakEntry<TypeDef>,
338}
339
340impl Method {
341 pub fn new(flags: MethodFlags, name: impl Into<String>, signature: MethodSig) -> Self {
342 Self {
343 flags,
344 name: name.into(),
345 signature,
346 ..Default::default()
347 }
348 }
349
350 pub fn public_static(name: impl Into<String>, signature: MethodSig) -> Self {
352 Self::new(
353 MethodFlags::Public | MethodFlags::Static | MethodFlags::HideBySig,
354 name,
355 signature,
356 )
357 }
358
359 pub fn with_body(mut self, body: MethodBody) -> Self {
360 self.body = Some(body);
361 self
362 }
363
364 pub fn with_attribute(mut self, constructor: CustomAttributeCtor) -> Self {
366 self.custom_attributes
367 .push(CustomAttribute::without_arguments(constructor));
368 self
369 }
370
371 pub fn is_public(&self) -> bool {
372 self.flags & MethodFlags::MemberAccessMask == MethodFlags::Public
373 }
374
375 pub fn is_static(&self) -> bool {
376 self.flags.contains(MethodFlags::Static)
377 }
378
379 pub fn is_ctor(&self) -> bool {
380 self.flags.contains(MethodFlags::RTSpecialName) && self.name == ".ctor"
381 }
382
383 pub fn is_cctor(&self) -> bool {
384 self.flags.contains(MethodFlags::RTSpecialName) && self.name == ".cctor"
385 }
386
387 pub fn param(&self, sequence: u16) -> Option<Entry<Param>> {
389 self.params
390 .iter()
391 .find(|p| p.get().sequence == sequence)
392 .cloned()
393 }
394
395 pub fn param_name(&self, index: usize) -> String {
397 self.param(index as u16 + 1)
398 .map(|p| p.get().name.clone())
399 .unwrap_or_else(|| format!("arg{}", index))
400 }
401}
402
403impl EntryName for Method {
404 fn entry_name(&self) -> String {
405 match self.declaring_type.upgrade() {
406 Some(t) => format!("{}::{}", t.get().full_name(), self.name),
407 None => self.name.clone(),
408 }
409 }
410}
411
412#[derive(Debug, Clone, Default)]
413#[cfg_attr(feature = "reflect", derive(facet::Facet))]
414pub struct Param {
415 pub flags: ParamFlags,
416 pub sequence: u16,
417 pub name: String,
418 pub constant: Option<Constant>,
419 pub marshal: Option<Vec<u8>>,
420 pub custom_attributes: Vec<CustomAttribute>,
421}
422
423impl Param {
424 pub fn new(sequence: u16, name: impl Into<String>) -> Self {
425 Self {
426 sequence,
427 name: name.into(),
428 ..Default::default()
429 }
430 }
431}
432
433impl EntryName for Param {
434 fn entry_name(&self) -> String {
435 self.name.clone()
436 }
437}
438
439#[derive(Debug, Clone, Default)]
440#[cfg_attr(feature = "reflect", derive(facet::Facet))]
441pub struct Property {
442 pub flags: PropertyFlags,
443 pub name: String,
444 pub signature: PropertySig,
445 pub constant: Option<Constant>,
446 pub getter: Option<Entry<Method>>,
447 pub setter: Option<Entry<Method>>,
448 pub other_methods: Vec<Entry<Method>>,
449 pub custom_attributes: Vec<CustomAttribute>,
450}
451
452impl EntryName for Property {
453 fn entry_name(&self) -> String {
454 self.name.clone()
455 }
456}
457
458#[derive(Debug, Clone, Default)]
459#[cfg_attr(feature = "reflect", derive(facet::Facet))]
460pub struct Event {
461 pub flags: EventFlags,
462 pub name: String,
463 pub event_type: Option<TypeDefOrRef>,
464 pub add: Option<Entry<Method>>,
465 pub remove: Option<Entry<Method>>,
466 pub fire: Option<Entry<Method>>,
467 pub other_methods: Vec<Entry<Method>>,
468 pub custom_attributes: Vec<CustomAttribute>,
469}
470
471impl EntryName for Event {
472 fn entry_name(&self) -> String {
473 self.name.clone()
474 }
475}
476
477#[derive(Debug, Clone, PartialEq, Eq)]
478#[cfg_attr(feature = "reflect", derive(facet::Facet))]
479pub struct GenericParamConstraint {
480 pub constraint: TypeDefOrRef,
481 pub custom_attributes: Vec<CustomAttribute>,
482}
483
484#[derive(Debug, Clone, Default)]
485#[cfg_attr(feature = "reflect", derive(facet::Facet))]
486pub struct GenericParam {
487 pub number: u16,
488 pub flags: GenericParamFlags,
489 pub name: String,
490 pub constraints: Vec<GenericParamConstraint>,
491 pub custom_attributes: Vec<CustomAttribute>,
492}
493
494impl EntryName for GenericParam {
495 fn entry_name(&self) -> String {
496 self.name.clone()
497 }
498}