dotscope 0.6.0

A high-performance, cross-platform framework for analyzing and reverse engineering .NET PE executables
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
//! SSA type system for .NET CIL types.
//!
//! This module provides a type representation for SSA variables that captures
//! the essential type information needed for analysis and optimization without
//! requiring full metadata resolution.
//!
//! # Design Rationale
//!
//! The `SsaType` enum is designed to be:
//! - **Fast to compare**: Enum variants are faster than token resolution
//! - **Self-contained**: No metadata context needed for basic operations
//! - **Analysis-friendly**: Includes special types like `Unknown` and `Null`
//!
//! # Type Categories
//!
//! - **Primitives**: Fixed-size numeric and boolean types
//! - **References**: Object references, arrays, and pointers
//! - **Special**: Generic parameters and analysis-only types

use std::fmt;

use crate::metadata::{
    cilobject::CilObject,
    method::Method,
    signatures::{
        CustomModifiers, SignatureArray, SignatureLocalVariable, SignatureMethod,
        SignatureParameter, SignaturePointer, SignatureSzArray, TypeSignature,
    },
    tables::{MemberRefSignature, StandAloneSigRaw, StandAloneSignature},
    token::Token,
    typesystem::{ArrayDimensions, CilFlavor},
};

/// Reference to a type in metadata.
///
/// This is a lightweight handle that can be resolved to full type information
/// when needed. Used for class types, value types, and generic instantiations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TypeRef(pub Token);

impl TypeRef {
    /// Creates a new type reference from a metadata token.
    #[must_use]
    pub const fn new(token: Token) -> Self {
        Self(token)
    }

    /// Returns the underlying metadata token.
    #[must_use]
    pub const fn token(&self) -> Token {
        self.0
    }
}

impl fmt::Display for TypeRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "TypeRef({})", self.0)
    }
}

/// Reference to a method in metadata.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MethodRef(pub Token);

impl MethodRef {
    /// Creates a new method reference from a metadata token.
    #[must_use]
    pub const fn new(token: Token) -> Self {
        Self(token)
    }

    /// Returns the underlying metadata token.
    #[must_use]
    pub const fn token(&self) -> Token {
        self.0
    }
}

impl fmt::Display for MethodRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "MethodRef({})", self.0)
    }
}

/// Reference to a field in metadata.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FieldRef(pub Token);

impl FieldRef {
    /// Creates a new field reference from a metadata token.
    #[must_use]
    pub const fn new(token: Token) -> Self {
        Self(token)
    }

    /// Returns the underlying metadata token.
    #[must_use]
    pub const fn token(&self) -> Token {
        self.0
    }
}

impl fmt::Display for FieldRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "FieldRef({})", self.0)
    }
}

/// Reference to a standalone signature in metadata.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SigRef(pub Token);

impl SigRef {
    /// Creates a new signature reference from a metadata token.
    #[must_use]
    pub const fn new(token: Token) -> Self {
        Self(token)
    }

    /// Returns the underlying metadata token.
    #[must_use]
    pub const fn token(&self) -> Token {
        self.0
    }
}

impl fmt::Display for SigRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SigRef({})", self.0)
    }
}

/// SSA type representation for CIL types.
///
/// This enum provides a simplified view of .NET types suitable for SSA analysis.
/// It captures the essential type information without requiring full metadata
/// resolution for common operations.
///
/// # Examples
///
/// ```rust,no_run
/// use dotscope::analysis::SsaType;
///
/// let int_type = SsaType::I32;
/// let string_type = SsaType::String;
/// let array_type = SsaType::Array(Box::new(SsaType::I32), 1);
///
/// assert!(int_type.is_primitive());
/// assert!(string_type.is_reference());
/// assert!(array_type.is_array());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub enum SsaType {
    // ========== Primitives ==========
    /// No return value (void).
    Void,

    /// Boolean type (System.Boolean).
    Bool,

    /// Signed 8-bit integer (System.SByte).
    I8,

    /// Unsigned 8-bit integer (System.Byte).
    U8,

    /// Signed 16-bit integer (System.Int16).
    I16,

    /// Unsigned 16-bit integer (System.UInt16).
    U16,

    /// Signed 32-bit integer (System.Int32).
    I32,

    /// Unsigned 32-bit integer (System.UInt32).
    U32,

    /// Signed 64-bit integer (System.Int64).
    I64,

    /// Unsigned 64-bit integer (System.UInt64).
    U64,

    /// Native-sized signed integer (System.IntPtr).
    NativeInt,

    /// Native-sized unsigned integer (System.UIntPtr).
    NativeUInt,

    /// 32-bit floating point (System.Single).
    F32,

    /// 64-bit floating point (System.Double).
    F64,

    /// Unicode character (System.Char).
    Char,

    // ========== Reference Types ==========
    /// System.Object reference.
    Object,

    /// System.String reference.
    String,

    /// Reference to a specific class type.
    Class(TypeRef),

    /// Value type (struct) - stored inline, not by reference.
    ValueType(TypeRef),

    /// Single-dimensional or multi-dimensional array.
    ///
    /// The `u32` is the rank (number of dimensions). Rank 1 is a vector (SZ array).
    Array(Box<SsaType>, u32),

    /// Unmanaged pointer to a type.
    Pointer(Box<SsaType>),

    /// Managed reference (byref) to a type.
    ByRef(Box<SsaType>),

    // ========== Special Types ==========
    /// Typed reference (System.TypedReference).
    TypedReference,

    /// Generic type parameter (e.g., `!0`, `!1`).
    ///
    /// The `u32` is the parameter index.
    GenericParam(u32),

    /// Generic method parameter (e.g., `!!0`, `!!1`).
    ///
    /// The `u32` is the parameter index.
    MethodGenericParam(u32),

    /// Function pointer type.
    FnPtr(Box<FnPtrSig>),

    // ========== Runtime Handle Types ==========
    /// Runtime type handle (System.RuntimeTypeHandle).
    ///
    /// Result of `ldtoken` on a type. Used with `Type.GetTypeFromHandle()`.
    RuntimeTypeHandle,

    /// Runtime method handle (System.RuntimeMethodHandle).
    ///
    /// Result of `ldtoken` on a method. Used with `MethodBase.GetMethodFromHandle()`.
    RuntimeMethodHandle,

    /// Runtime field handle (System.RuntimeFieldHandle).
    ///
    /// Result of `ldtoken` on a field. Used with `FieldInfo.GetFieldFromHandle()`.
    RuntimeFieldHandle,

    // ========== Analysis Types ==========
    /// Known null constant (more precise than Object).
    Null,

    /// Type not yet inferred or unknown.
    ///
    /// This is used during type inference before a type is determined.
    #[default]
    Unknown,

    /// Type that varies depending on control flow (for incomplete inference).
    Varying,
}

/// Function pointer signature.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FnPtrSig {
    /// Return type.
    pub ret: SsaType,
    /// Parameter types.
    pub params: Vec<SsaType>,
    /// Calling convention flags.
    pub call_conv: u8,
}

/// Classification of types by their storage requirements.
///
/// Used for local variable coalescing to determine which types can share
/// the same storage slot. Types in the same class can be coalesced.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum TypeClass {
    /// 32-bit integer types (I32, U32, Bool, Char, I16, U16, I8, U8).
    ///
    /// All these types are stored as 4 bytes on the CLR evaluation stack.
    Int32,
    /// 64-bit integer types (I64, U64).
    Int64,
    /// 32-bit floating point (F32).
    Float32,
    /// 64-bit floating point (F64).
    Float64,
    /// Reference types (Object, String, Class, Array, etc.).
    ///
    /// All reference types are pointer-sized.
    Reference,
    /// Native-sized integers (IntPtr, UIntPtr).
    ///
    /// Platform-dependent size (4 or 8 bytes).
    NativeInt,
    /// Other/unknown types that cannot be coalesced.
    Other,
}

impl SsaType {
    /// Returns `true` if this is a primitive numeric or boolean type.
    #[must_use]
    pub const fn is_primitive(&self) -> bool {
        matches!(
            self,
            Self::Bool
                | Self::I8
                | Self::U8
                | Self::I16
                | Self::U16
                | Self::I32
                | Self::U32
                | Self::I64
                | Self::U64
                | Self::NativeInt
                | Self::NativeUInt
                | Self::F32
                | Self::F64
                | Self::Char
        )
    }

    /// Returns `true` if this is an integer type (signed or unsigned).
    #[must_use]
    pub const fn is_integer(&self) -> bool {
        matches!(
            self,
            Self::I8
                | Self::U8
                | Self::I16
                | Self::U16
                | Self::I32
                | Self::U32
                | Self::I64
                | Self::U64
                | Self::NativeInt
                | Self::NativeUInt
        )
    }

    /// Returns `true` if this is a floating-point type.
    #[must_use]
    pub const fn is_float(&self) -> bool {
        matches!(self, Self::F32 | Self::F64)
    }

    /// Returns `true` if this is a reference type (can be null).
    #[must_use]
    pub fn is_reference(&self) -> bool {
        matches!(
            self,
            Self::Object | Self::String | Self::Class(_) | Self::Array(_, _) | Self::Null
        )
    }

    /// Returns `true` if this is a value type (struct).
    #[must_use]
    pub const fn is_value_type(&self) -> bool {
        matches!(self, Self::ValueType(_)) || self.is_primitive()
    }

    /// Returns `true` if this is an array type.
    #[must_use]
    pub const fn is_array(&self) -> bool {
        matches!(self, Self::Array(_, _))
    }

    /// Returns `true` if this is a pointer type (managed or unmanaged).
    #[must_use]
    pub const fn is_pointer(&self) -> bool {
        matches!(self, Self::Pointer(_) | Self::ByRef(_))
    }

    /// Returns `true` if this is the void type.
    #[must_use]
    pub const fn is_void(&self) -> bool {
        matches!(self, Self::Void)
    }

    /// Returns `true` if this type is unknown or not yet inferred.
    #[must_use]
    pub const fn is_unknown(&self) -> bool {
        matches!(self, Self::Unknown | Self::Varying)
    }

    /// Returns `true` if this is the null type.
    #[must_use]
    pub const fn is_null(&self) -> bool {
        matches!(self, Self::Null)
    }

    /// Returns `true` if this is a generic parameter.
    #[must_use]
    pub const fn is_generic_param(&self) -> bool {
        matches!(self, Self::GenericParam(_) | Self::MethodGenericParam(_))
    }

    /// Returns the element type if this is an array.
    #[must_use]
    pub fn array_element_type(&self) -> Option<&SsaType> {
        match self {
            Self::Array(elem, _) => Some(elem),
            _ => None,
        }
    }

    /// Returns the array rank (number of dimensions) if this is an array.
    #[must_use]
    pub const fn array_rank(&self) -> Option<u32> {
        match self {
            Self::Array(_, rank) => Some(*rank),
            _ => None,
        }
    }

    /// Returns the pointed-to type if this is a pointer or byref.
    #[must_use]
    pub fn pointee_type(&self) -> Option<&SsaType> {
        match self {
            Self::Pointer(inner) | Self::ByRef(inner) => Some(inner),
            _ => None,
        }
    }

    /// Returns the size in bytes for primitive types, if known.
    ///
    /// Returns `None` for reference types and types with platform-dependent sizes.
    #[must_use]
    pub const fn size_bytes(&self) -> Option<u32> {
        match self {
            Self::Bool | Self::I8 | Self::U8 => Some(1),
            Self::I16 | Self::U16 | Self::Char => Some(2),
            Self::I32 | Self::U32 | Self::F32 => Some(4),
            Self::I64 | Self::U64 | Self::F64 => Some(8),
            // NativeInt/NativeUInt and pointers are platform-dependent
            _ => None,
        }
    }

    /// Returns the stack slot type for this SSA type.
    ///
    /// CIL uses a normalized set of types on the evaluation stack:
    /// - All integer types smaller than 32 bits become I32
    /// - Float types stay as-is (F32 becomes F64 in some contexts)
    /// - References stay as references
    #[must_use]
    pub fn stack_type(&self) -> SsaType {
        match self {
            // Small integers promote to I32 on the stack
            Self::Bool | Self::I8 | Self::U8 | Self::I16 | Self::U16 | Self::Char | Self::I32 => {
                Self::I32
            }
            Self::U32 => Self::I32, // Treated as I32 on stack
            Self::I64 | Self::U64 => Self::I64,
            Self::NativeInt | Self::NativeUInt => Self::NativeInt,
            Self::F32 | Self::F64 => Self::F64, // F is typically F64 on stack
            // Reference types stay as-is
            _ => self.clone(),
        }
    }

    /// Returns the storage class of this type.
    ///
    /// Used for local variable coalescing to determine which types can share
    /// the same storage slot without requiring conversion.
    #[must_use]
    pub fn storage_class(&self) -> TypeClass {
        match self {
            // 32-bit integers (all stored as 4 bytes on the CLR stack)
            Self::I32
            | Self::U32
            | Self::Bool
            | Self::Char
            | Self::I16
            | Self::U16
            | Self::I8
            | Self::U8 => TypeClass::Int32,

            // 64-bit integers
            Self::I64 | Self::U64 => TypeClass::Int64,

            // Floating point
            Self::F32 => TypeClass::Float32,
            Self::F64 => TypeClass::Float64,

            // Native integers
            Self::NativeInt | Self::NativeUInt => TypeClass::NativeInt,

            // Reference types and value types (all pointer-sized on stack)
            Self::Object
            | Self::String
            | Self::Class(_)
            | Self::ValueType(_)
            | Self::Array(_, _)
            | Self::ByRef(_)
            | Self::Pointer(_)
            | Self::TypedReference
            | Self::RuntimeTypeHandle
            | Self::RuntimeMethodHandle
            | Self::RuntimeFieldHandle => TypeClass::Reference,

            // Other types
            Self::Void
            | Self::Unknown
            | Self::Varying
            | Self::Null
            | Self::GenericParam(_)
            | Self::MethodGenericParam(_)
            | Self::FnPtr(_) => TypeClass::Other,
        }
    }

    /// Checks if this type can share a local slot with another type.
    ///
    /// Two types are compatible for storage if they have the same size and
    /// alignment requirements, meaning they can be stored in the same local
    /// variable slot without data corruption.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use dotscope::analysis::SsaType;
    ///
    /// // Same types are compatible
    /// assert!(SsaType::I32.is_compatible_for_storage(&SsaType::I32));
    ///
    /// // 32-bit integers can share slots
    /// assert!(SsaType::I32.is_compatible_for_storage(&SsaType::U32));
    /// assert!(SsaType::I32.is_compatible_for_storage(&SsaType::Bool));
    ///
    /// // Reference types can share slots
    /// assert!(SsaType::Object.is_compatible_for_storage(&SsaType::String));
    ///
    /// // Different sizes are incompatible
    /// assert!(!SsaType::I32.is_compatible_for_storage(&SsaType::I64));
    /// ```
    #[must_use]
    pub fn is_compatible_for_storage(&self, other: &SsaType) -> bool {
        if self == other {
            return true;
        }

        // Unknown types are conservatively compatible
        if matches!(self, Self::Unknown) || matches!(other, Self::Unknown) {
            return true;
        }

        // Same storage class means compatible
        matches!(
            (self.storage_class(), other.storage_class()),
            (TypeClass::Int32, TypeClass::Int32)
                | (TypeClass::Int64, TypeClass::Int64)
                | (TypeClass::Float32, TypeClass::Float32)
                | (TypeClass::Float64, TypeClass::Float64)
                | (TypeClass::Reference, TypeClass::Reference)
                | (TypeClass::NativeInt, TypeClass::NativeInt)
        )
    }

    /// Merges two types at a control flow join point.
    ///
    /// Returns the common type if compatible, or `Varying` if incompatible.
    #[must_use]
    pub fn merge(&self, other: &SsaType) -> SsaType {
        if self == other {
            return self.clone();
        }

        // Unknown can be refined
        if matches!(self, Self::Unknown) {
            return other.clone();
        }
        if matches!(other, Self::Unknown) {
            return self.clone();
        }

        // Null can merge with any reference type
        if matches!(self, Self::Null) && other.is_reference() {
            return other.clone();
        }
        if matches!(other, Self::Null) && self.is_reference() {
            return self.clone();
        }

        // If types are incompatible, return Varying
        Self::Varying
    }

    /// Converts this SSA type to a `TypeSignature` for signature encoding.
    ///
    /// This enables generating local variable signatures from SSA type information.
    /// Analysis-only types (`Unknown`, `Null`, `Varying`) are converted to `Object`
    /// as a safe fallback.
    ///
    /// # Returns
    ///
    /// The corresponding `TypeSignature` that can be used for signature encoding.
    #[must_use]
    pub fn to_type_signature(&self) -> TypeSignature {
        match self {
            Self::Void => TypeSignature::Void,
            Self::Bool => TypeSignature::Boolean,
            Self::I8 => TypeSignature::I1,
            Self::U8 => TypeSignature::U1,
            Self::I16 => TypeSignature::I2,
            Self::U16 => TypeSignature::U2,
            Self::I32 => TypeSignature::I4,
            Self::U32 => TypeSignature::U4,
            Self::I64 => TypeSignature::I8,
            Self::U64 => TypeSignature::U8,
            Self::NativeInt => TypeSignature::I,
            Self::NativeUInt => TypeSignature::U,
            Self::F32 => TypeSignature::R4,
            Self::F64 => TypeSignature::R8,
            Self::Char => TypeSignature::Char,
            Self::String => TypeSignature::String,
            Self::Class(type_ref) => TypeSignature::Class(type_ref.token()),
            Self::ValueType(type_ref) => TypeSignature::ValueType(type_ref.token()),
            Self::Array(elem, 1) => TypeSignature::SzArray(SignatureSzArray {
                modifiers: CustomModifiers::default(),
                base: Box::new(elem.to_type_signature()),
            }),
            Self::Array(elem, rank) => TypeSignature::Array(SignatureArray {
                base: Box::new(elem.to_type_signature()),
                rank: *rank,
                dimensions: (0..*rank)
                    .map(|_| ArrayDimensions {
                        size: None,
                        lower_bound: None,
                    })
                    .collect(),
            }),
            Self::Pointer(inner) => TypeSignature::Ptr(SignaturePointer {
                modifiers: CustomModifiers::default(),
                base: Box::new(inner.to_type_signature()),
            }),
            Self::ByRef(inner) => TypeSignature::ByRef(Box::new(inner.to_type_signature())),
            Self::TypedReference => TypeSignature::TypedByRef,
            Self::GenericParam(idx) => TypeSignature::GenericParamType(*idx),
            Self::MethodGenericParam(idx) => TypeSignature::GenericParamMethod(*idx),
            Self::FnPtr(sig) => TypeSignature::FnPtr(Box::new(SignatureMethod {
                has_this: false,
                explicit_this: false,
                default: true,
                vararg: false,
                cdecl: false,
                stdcall: false,
                thiscall: false,
                fastcall: false,
                param_count_generic: 0,
                // Safe: .NET methods have at most ~65535 parameters
                #[allow(clippy::cast_possible_truncation)]
                param_count: sig.params.len() as u32,
                return_type: SignatureParameter {
                    modifiers: CustomModifiers::default(),
                    by_ref: false,
                    base: sig.ret.to_type_signature(),
                },
                params: sig
                    .params
                    .iter()
                    .map(|p| SignatureParameter {
                        modifiers: CustomModifiers::default(),
                        by_ref: false,
                        base: p.to_type_signature(),
                    })
                    .collect(),
                varargs: Vec::new(),
            })),
            // Object and analysis-only types (runtime handles would need BCL resolution)
            Self::Object
            | Self::Null
            | Self::Unknown
            | Self::Varying
            | Self::RuntimeTypeHandle
            | Self::RuntimeMethodHandle
            | Self::RuntimeFieldHandle => TypeSignature::Object,
        }
    }

    /// Creates an `SsaType` from a `CilFlavor`.
    ///
    /// This converts the metadata type flavor to the SSA type representation.
    /// For complex types (arrays, pointers, generic instances), a token is needed
    /// to create a proper type reference.
    ///
    /// # Arguments
    ///
    /// * `flavor` - The CIL type flavor to convert
    /// * `token` - The metadata token for creating type references
    #[must_use]
    pub fn from_cil_flavor(flavor: &CilFlavor, token: Token) -> Self {
        match flavor {
            // Primitive types
            CilFlavor::Void => Self::Void,
            CilFlavor::Boolean => Self::Bool,
            CilFlavor::Char => Self::Char,
            CilFlavor::I1 => Self::I8,
            CilFlavor::U1 => Self::U8,
            CilFlavor::I2 => Self::I16,
            CilFlavor::U2 => Self::U16,
            CilFlavor::I4 => Self::I32,
            CilFlavor::U4 => Self::U32,
            CilFlavor::I8 => Self::I64,
            CilFlavor::U8 => Self::U64,
            CilFlavor::R4 => Self::F32,
            CilFlavor::R8 => Self::F64,
            CilFlavor::I => Self::NativeInt,
            CilFlavor::U => Self::NativeUInt,
            CilFlavor::Object => Self::Object,
            CilFlavor::String => Self::String,

            // Complex types
            CilFlavor::Array { rank, .. } => Self::Array(Box::new(Self::Unknown), *rank),
            CilFlavor::Pointer => Self::Pointer(Box::new(Self::Unknown)),
            CilFlavor::ByRef => Self::ByRef(Box::new(Self::Unknown)),
            CilFlavor::GenericParameter { index, method } => {
                if *method {
                    Self::MethodGenericParam(*index)
                } else {
                    Self::GenericParam(*index)
                }
            }

            // Type categories
            CilFlavor::GenericInstance | CilFlavor::Class | CilFlavor::Interface => {
                Self::Class(TypeRef::new(token))
            }
            // ValueType and TypedReference (which is a value type)
            CilFlavor::ValueType | CilFlavor::TypedRef { .. } => {
                Self::ValueType(TypeRef::new(token))
            }

            CilFlavor::Pinned | CilFlavor::FnPtr { .. } | CilFlavor::Unknown => Self::Unknown,
        }
    }

    /// Creates an `SsaType` from a `TypeSignature`.
    ///
    /// This converts a metadata type signature to the SSA type representation.
    /// For class and value types, the assembly context is used to resolve
    /// type tokens to determine if they are primitives.
    ///
    /// # Arguments
    ///
    /// * `signature` - The type signature to convert
    /// * `assembly` - Assembly context for resolving type tokens
    #[must_use]
    pub fn from_type_signature(signature: &TypeSignature, assembly: &CilObject) -> Self {
        match signature {
            // Primitive types
            TypeSignature::Void => Self::Void,
            TypeSignature::Boolean => Self::Bool,
            TypeSignature::Char => Self::Char,
            TypeSignature::I1 => Self::I8,
            TypeSignature::U1 => Self::U8,
            TypeSignature::I2 => Self::I16,
            TypeSignature::U2 => Self::U16,
            TypeSignature::I4 => Self::I32,
            TypeSignature::U4 => Self::U32,
            TypeSignature::I8 => Self::I64,
            TypeSignature::U8 => Self::U64,
            TypeSignature::R4 => Self::F32,
            TypeSignature::R8 => Self::F64,
            TypeSignature::I => Self::NativeInt,
            TypeSignature::U => Self::NativeUInt,

            // Reference types
            TypeSignature::String => Self::String,
            TypeSignature::Object | TypeSignature::Type | TypeSignature::Boxed => Self::Object,
            TypeSignature::TypedByRef => Self::TypedReference,

            // Class/ValueType with token
            TypeSignature::Class(token) | TypeSignature::ValueType(token) => {
                Self::from_type_token(*token, assembly)
            }

            // Arrays
            TypeSignature::SzArray(sz_array) => {
                let elem_type = Self::from_type_signature(&sz_array.base, assembly);
                Self::Array(Box::new(elem_type), 1)
            }
            TypeSignature::Array(array) => {
                let elem_type = Self::from_type_signature(&array.base, assembly);
                Self::Array(Box::new(elem_type), array.rank)
            }

            // Pointers and references
            TypeSignature::Ptr(ptr) => {
                let inner = Self::from_type_signature(&ptr.base, assembly);
                Self::Pointer(Box::new(inner))
            }
            TypeSignature::ByRef(inner) => {
                let inner_type = Self::from_type_signature(inner, assembly);
                Self::ByRef(Box::new(inner_type))
            }

            // Generic parameters
            TypeSignature::GenericParamType(index) => Self::GenericParam(*index),
            TypeSignature::GenericParamMethod(index) => Self::MethodGenericParam(*index),

            // Generic instantiation
            TypeSignature::GenericInst(base_type, _type_args) => {
                Self::from_type_signature(base_type, assembly)
            }

            // Pinned - unwrap to inner type
            TypeSignature::Pinned(inner) => Self::from_type_signature(inner, assembly),

            // Function pointers, modifiers, and special types - not representable
            TypeSignature::FnPtr(_)
            | TypeSignature::ModifiedRequired(_)
            | TypeSignature::ModifiedOptional(_)
            | TypeSignature::Sentinel
            | TypeSignature::Unknown
            | TypeSignature::Internal
            | TypeSignature::Modifier
            | TypeSignature::Reserved
            | TypeSignature::Field => Self::Unknown,
        }
    }

    /// Creates an `SsaType` from a type token by resolving it in the assembly.
    ///
    /// Handles TypeDef (0x02), TypeRef (0x01), and TypeSpec (0x1B) tokens.
    #[must_use]
    pub fn from_type_token(token: Token, assembly: &CilObject) -> Self {
        let table_id = token.table();

        match table_id {
            // TypeDef or TypeRef - look up in type registry
            0x02 | 0x01 => {
                let Some(cil_type) = assembly.types().get(&token) else {
                    return Self::Unknown;
                };

                let name = &cil_type.name;
                let namespace = &cil_type.namespace;

                // Check for well-known primitive types in System namespace
                if namespace == "System" {
                    if let Some(primitive) = Self::match_system_type(name) {
                        return primitive;
                    }
                }

                // Return Class or ValueType based on flavor
                if cil_type.flavor().is_value_type() {
                    Self::ValueType(TypeRef::new(token))
                } else {
                    Self::Class(TypeRef::new(token))
                }
            }

            // TypeSpec - generic instantiation or complex type
            0x1B => {
                let Some(typespec) = assembly.types().get(&token) else {
                    return Self::Unknown;
                };
                Self::from_cil_flavor(typespec.flavor(), token)
            }

            // Unknown table
            _ => Self::Unknown,
        }
    }

    /// Matches a System namespace type name to its SSA primitive type.
    fn match_system_type(name: &str) -> Option<Self> {
        match name {
            "Void" => Some(Self::Void),
            "Boolean" => Some(Self::Bool),
            "Char" => Some(Self::Char),
            "SByte" => Some(Self::I8),
            "Byte" => Some(Self::U8),
            "Int16" => Some(Self::I16),
            "UInt16" => Some(Self::U16),
            "Int32" => Some(Self::I32),
            "UInt32" => Some(Self::U32),
            "Int64" => Some(Self::I64),
            "UInt64" => Some(Self::U64),
            "Single" => Some(Self::F32),
            "Double" => Some(Self::F64),
            "IntPtr" => Some(Self::NativeInt),
            "UIntPtr" => Some(Self::NativeUInt),
            "String" => Some(Self::String),
            "Object" => Some(Self::Object),
            "TypedReference" => Some(Self::TypedReference),
            _ => None,
        }
    }
}

impl fmt::Display for SsaType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Void => write!(f, "void"),
            Self::Bool => write!(f, "bool"),
            Self::I8 => write!(f, "i8"),
            Self::U8 => write!(f, "u8"),
            Self::I16 => write!(f, "i16"),
            Self::U16 => write!(f, "u16"),
            Self::I32 => write!(f, "i32"),
            Self::U32 => write!(f, "u32"),
            Self::I64 => write!(f, "i64"),
            Self::U64 => write!(f, "u64"),
            Self::NativeInt => write!(f, "nint"),
            Self::NativeUInt => write!(f, "nuint"),
            Self::F32 => write!(f, "f32"),
            Self::F64 => write!(f, "f64"),
            Self::Char => write!(f, "char"),
            Self::Object => write!(f, "object"),
            Self::String => write!(f, "string"),
            Self::Class(t) => write!(f, "class {t}"),
            Self::ValueType(t) => write!(f, "valuetype {t}"),
            Self::Array(elem, 1) => write!(f, "{elem}[]"),
            Self::Array(elem, rank) => write!(f, "{elem}[{rank}d]"),
            Self::Pointer(inner) => write!(f, "{inner}*"),
            Self::ByRef(inner) => write!(f, "{inner}&"),
            Self::TypedReference => write!(f, "typedref"),
            Self::GenericParam(idx) => write!(f, "!{idx}"),
            Self::MethodGenericParam(idx) => write!(f, "!!{idx}"),
            Self::FnPtr(_) => write!(f, "fnptr"),
            Self::RuntimeTypeHandle => write!(f, "RuntimeTypeHandle"),
            Self::RuntimeMethodHandle => write!(f, "RuntimeMethodHandle"),
            Self::RuntimeFieldHandle => write!(f, "RuntimeFieldHandle"),
            Self::Null => write!(f, "null"),
            Self::Unknown => write!(f, "?"),
            Self::Varying => write!(f, "varying"),
        }
    }
}

/// Provides type information during SSA construction.
///
/// This struct holds references to the method being converted and the containing
/// assembly, allowing type lookups for arguments, locals, and call return types
/// without duplicating type information.
///
/// # Usage
///
/// ```rust,ignore
/// let ctx = TypeContext::new(&method, &assembly);
/// let arg0_type = ctx.arg_type(0);
/// let local2_type = ctx.local_type(2);
/// let ret_type = ctx.call_return_type(call_token);
/// ```
pub struct TypeContext<'a> {
    /// The method being converted to SSA form.
    method: &'a Method,
    /// The assembly containing the method.
    assembly: &'a CilObject,
}

impl<'a> TypeContext<'a> {
    /// Creates a new type context for SSA construction.
    #[must_use]
    pub fn new(method: &'a Method, assembly: &'a CilObject) -> Self {
        Self { method, assembly }
    }

    /// Returns the assembly reference.
    #[must_use]
    pub fn assembly(&self) -> &'a CilObject {
        self.assembly
    }

    /// Returns the type of a method argument by index.
    ///
    /// For instance methods, argument 0 is `this`. Parameter indices are offset
    /// by 1 for instance methods.
    #[must_use]
    pub fn arg_type(&self, idx: u16) -> SsaType {
        let idx = idx as usize;

        // For instance methods, arg 0 is 'this'
        if self.method.signature.has_this {
            if idx == 0 {
                // 'this' pointer - get type from declaring class
                return self
                    .method
                    .declaring_type_rc()
                    .map_or(SsaType::Object, |dt| SsaType::Class(TypeRef::new(dt.token)));
            }
            // Adjust for 'this' offset
            if let Some(param) = self.method.signature.params.get(idx - 1) {
                return SsaType::from_type_signature(&param.base, self.assembly);
            }
        } else if let Some(param) = self.method.signature.params.get(idx) {
            return SsaType::from_type_signature(&param.base, self.assembly);
        }

        SsaType::Unknown
    }

    /// Returns the type of a local variable by index.
    #[must_use]
    pub fn local_type(&self, idx: u16) -> SsaType {
        self.method
            .get_local_type_signatures()
            .and_then(|types| types.into_iter().nth(idx as usize))
            .map_or(SsaType::Unknown, |sig| {
                SsaType::from_type_signature(&sig.base, self.assembly)
            })
    }

    /// Returns the return type of a called method.
    ///
    /// Handles MethodDef (0x06), MemberRef (0x0A), and MethodSpec (0x2B) tokens.
    #[must_use]
    pub fn call_return_type(&self, token: Token) -> SsaType {
        let table_id = token.table();

        match table_id {
            // MethodDef - look up in methods
            0x06 => self
                .assembly
                .methods()
                .get(&token)
                .map_or(SsaType::Unknown, |entry| {
                    let method = entry.value();
                    SsaType::from_type_signature(&method.signature.return_type.base, self.assembly)
                }),

            // MemberRef - look up and get signature
            0x0A => self
                .assembly
                .refs_members()
                .get(&token)
                .and_then(|entry| {
                    let member_ref = entry.value();
                    match &member_ref.signature {
                        MemberRefSignature::Method(sig) => Some(SsaType::from_type_signature(
                            &sig.return_type.base,
                            self.assembly,
                        )),
                        MemberRefSignature::Field(_) => None,
                    }
                })
                .unwrap_or(SsaType::Unknown),

            // MethodSpec - resolve to underlying method and get its return type
            0x2B => self
                .assembly
                .method_specs()
                .get(&token)
                .and_then(|entry| entry.value().method.token())
                .map_or(SsaType::Unknown, |method_token| {
                    self.call_return_type(method_token)
                }),

            _ => SsaType::Unknown,
        }
    }

    /// Returns the type of a newly constructed object (for newobj instruction).
    ///
    /// Extracts the declaring type from the constructor method token.
    #[must_use]
    pub fn newobj_type(&self, ctor_token: Token) -> SsaType {
        let table_id = ctor_token.table();

        match table_id {
            // MethodDef - get declaring type
            0x06 => self
                .assembly
                .methods()
                .get(&ctor_token)
                .and_then(|entry| entry.value().declaring_type_rc().map(|dt| dt.token))
                .map_or(SsaType::Object, |type_token| {
                    SsaType::Class(TypeRef::new(type_token))
                }),

            // MemberRef - get declaring type from member ref
            0x0A => self
                .assembly
                .refs_members()
                .get(&ctor_token)
                .and_then(|entry| entry.value().declaredby.token())
                .map_or(SsaType::Object, |class_token| {
                    SsaType::from_type_token(class_token, self.assembly)
                }),

            // MethodSpec - resolve to underlying constructor
            0x2B => self
                .assembly
                .method_specs()
                .get(&ctor_token)
                .and_then(|entry| entry.value().method.token())
                .map_or(SsaType::Object, |method_token| {
                    self.newobj_type(method_token)
                }),

            _ => SsaType::Object,
        }
    }

    /// Returns the type of a field.
    ///
    /// Looks up the field's type signature from the assembly metadata.
    #[must_use]
    pub fn field_type(&self, field_token: Token) -> SsaType {
        let table_id = field_token.table();

        match table_id {
            // Field table (0x04) - look up through type registry
            0x04 => self
                .assembly
                .types()
                .get_field_signature(&field_token)
                .map_or(SsaType::Unknown, |sig| {
                    SsaType::from_type_signature(&sig, self.assembly)
                }),

            // MemberRef table (0x0A) - external field reference
            0x0A => self
                .assembly
                .refs_members()
                .get(&field_token)
                .and_then(|entry| {
                    if let MemberRefSignature::Field(field_sig) = &entry.value().signature {
                        Some(SsaType::from_type_signature(&field_sig.base, self.assembly))
                    } else {
                        None
                    }
                })
                .unwrap_or(SsaType::Unknown),

            _ => SsaType::Unknown,
        }
    }

    /// Returns the return type of a `calli` (indirect call) from its `StandAloneSig` token.
    ///
    /// Resolves the standalone method signature referenced by the token and extracts
    /// the return type. Returns `SsaType::Unknown` on any resolution failure.
    #[must_use]
    pub fn call_indirect_return_type(&self, sig_token: Token) -> SsaType {
        // StandAloneSig table is 0x11
        if sig_token.table() != 0x11 {
            return SsaType::Unknown;
        }
        let Some(tables) = self.assembly.tables() else {
            return SsaType::Unknown;
        };
        let Some(table) = tables.table::<StandAloneSigRaw>() else {
            return SsaType::Unknown;
        };
        let Some(raw) = table.get(sig_token.row()) else {
            return SsaType::Unknown;
        };
        let Some(blob) = self.assembly.blob() else {
            return SsaType::Unknown;
        };
        let Ok(owned) = raw.to_owned(blob) else {
            return SsaType::Unknown;
        };
        match &owned.parsed_signature {
            StandAloneSignature::Method(sig) => {
                SsaType::from_type_signature(&sig.return_type.base, self.assembly)
            }
            _ => SsaType::Unknown,
        }
    }

    /// Returns the original local variable type signatures for code generation.
    ///
    /// This provides the full `SignatureLocalVariable` information (including
    /// pinned and custom modifiers) needed when regenerating CIL code.
    #[must_use]
    pub fn local_type_signatures(&self) -> Option<Vec<SignatureLocalVariable>> {
        self.method.get_local_type_signatures()
    }
}

#[cfg(test)]
mod tests {
    use crate::analysis::ssa::types::{SsaType, TypeClass};

    #[test]
    fn test_primitive_types() {
        assert!(SsaType::I32.is_primitive());
        assert!(SsaType::F64.is_primitive());
        assert!(SsaType::Bool.is_primitive());
        assert!(!SsaType::Object.is_primitive());
        assert!(!SsaType::String.is_primitive());
    }

    #[test]
    fn test_integer_types() {
        assert!(SsaType::I32.is_integer());
        assert!(SsaType::U64.is_integer());
        assert!(SsaType::NativeInt.is_integer());
        assert!(!SsaType::F32.is_integer());
        assert!(!SsaType::Bool.is_integer());
    }

    #[test]
    fn test_float_types() {
        assert!(SsaType::F32.is_float());
        assert!(SsaType::F64.is_float());
        assert!(!SsaType::I32.is_float());
    }

    #[test]
    fn test_reference_types() {
        assert!(SsaType::Object.is_reference());
        assert!(SsaType::String.is_reference());
        assert!(SsaType::Null.is_reference());
        let array_type = SsaType::Array(Box::new(SsaType::I32), 1);
        assert!(array_type.is_reference());
        assert!(!SsaType::I32.is_reference());
    }

    #[test]
    fn test_array_type() {
        let array = SsaType::Array(Box::new(SsaType::I32), 1);
        assert!(array.is_array());
        assert_eq!(array.array_element_type(), Some(&SsaType::I32));
        assert_eq!(array.array_rank(), Some(1));

        let multi_dim = SsaType::Array(Box::new(SsaType::F64), 3);
        assert_eq!(multi_dim.array_rank(), Some(3));
    }

    #[test]
    fn test_pointer_types() {
        let ptr = SsaType::Pointer(Box::new(SsaType::I32));
        assert!(ptr.is_pointer());
        assert_eq!(ptr.pointee_type(), Some(&SsaType::I32));

        let byref = SsaType::ByRef(Box::new(SsaType::Object));
        assert!(byref.is_pointer());
        assert_eq!(byref.pointee_type(), Some(&SsaType::Object));
    }

    #[test]
    fn test_size_bytes() {
        assert_eq!(SsaType::I8.size_bytes(), Some(1));
        assert_eq!(SsaType::I16.size_bytes(), Some(2));
        assert_eq!(SsaType::I32.size_bytes(), Some(4));
        assert_eq!(SsaType::I64.size_bytes(), Some(8));
        assert_eq!(SsaType::F32.size_bytes(), Some(4));
        assert_eq!(SsaType::F64.size_bytes(), Some(8));
        assert_eq!(SsaType::NativeInt.size_bytes(), None); // Platform-dependent
        assert_eq!(SsaType::Object.size_bytes(), None);
    }

    #[test]
    fn test_stack_type() {
        assert_eq!(SsaType::I8.stack_type(), SsaType::I32);
        assert_eq!(SsaType::I16.stack_type(), SsaType::I32);
        assert_eq!(SsaType::I32.stack_type(), SsaType::I32);
        assert_eq!(SsaType::I64.stack_type(), SsaType::I64);
        assert_eq!(SsaType::F32.stack_type(), SsaType::F64);
    }

    #[test]
    fn test_type_merge() {
        // Same types merge to themselves
        assert_eq!(SsaType::I32.merge(&SsaType::I32), SsaType::I32);

        // Unknown merges with anything
        assert_eq!(SsaType::Unknown.merge(&SsaType::I32), SsaType::I32);
        assert_eq!(SsaType::I32.merge(&SsaType::Unknown), SsaType::I32);

        // Null merges with reference types
        assert_eq!(SsaType::Null.merge(&SsaType::Object), SsaType::Object);
        assert_eq!(SsaType::Object.merge(&SsaType::Null), SsaType::Object);

        // Incompatible types become Varying
        assert_eq!(SsaType::I32.merge(&SsaType::I64), SsaType::Varying);
        assert_eq!(SsaType::Object.merge(&SsaType::I32), SsaType::Varying);
    }

    #[test]
    fn test_display() {
        assert_eq!(format!("{}", SsaType::I32), "i32");
        assert_eq!(format!("{}", SsaType::Object), "object");
        assert_eq!(
            format!("{}", SsaType::Array(Box::new(SsaType::I32), 1)),
            "i32[]"
        );
        assert_eq!(
            format!("{}", SsaType::Pointer(Box::new(SsaType::I32))),
            "i32*"
        );
        assert_eq!(
            format!("{}", SsaType::ByRef(Box::new(SsaType::I32))),
            "i32&"
        );
        assert_eq!(format!("{}", SsaType::GenericParam(0)), "!0");
        assert_eq!(format!("{}", SsaType::MethodGenericParam(1)), "!!1");
    }

    #[test]
    fn test_storage_class() {
        // 32-bit integers
        assert_eq!(SsaType::I32.storage_class(), TypeClass::Int32);
        assert_eq!(SsaType::U32.storage_class(), TypeClass::Int32);
        assert_eq!(SsaType::Bool.storage_class(), TypeClass::Int32);
        assert_eq!(SsaType::Char.storage_class(), TypeClass::Int32);
        assert_eq!(SsaType::I8.storage_class(), TypeClass::Int32);

        // 64-bit integers
        assert_eq!(SsaType::I64.storage_class(), TypeClass::Int64);
        assert_eq!(SsaType::U64.storage_class(), TypeClass::Int64);

        // Floats
        assert_eq!(SsaType::F32.storage_class(), TypeClass::Float32);
        assert_eq!(SsaType::F64.storage_class(), TypeClass::Float64);

        // Native integers
        assert_eq!(SsaType::NativeInt.storage_class(), TypeClass::NativeInt);
        assert_eq!(SsaType::NativeUInt.storage_class(), TypeClass::NativeInt);

        // Reference types
        assert_eq!(SsaType::Object.storage_class(), TypeClass::Reference);
        assert_eq!(SsaType::String.storage_class(), TypeClass::Reference);
        assert_eq!(
            SsaType::Array(Box::new(SsaType::I32), 1).storage_class(),
            TypeClass::Reference
        );

        // Other
        assert_eq!(SsaType::Void.storage_class(), TypeClass::Other);
        assert_eq!(SsaType::Unknown.storage_class(), TypeClass::Other);
    }

    #[test]
    fn test_is_compatible_for_storage() {
        // Same types are compatible
        assert!(SsaType::I32.is_compatible_for_storage(&SsaType::I32));

        // 32-bit integers are compatible with each other
        assert!(SsaType::I32.is_compatible_for_storage(&SsaType::U32));
        assert!(SsaType::I32.is_compatible_for_storage(&SsaType::Bool));
        assert!(SsaType::I32.is_compatible_for_storage(&SsaType::Char));

        // 64-bit integers are compatible
        assert!(SsaType::I64.is_compatible_for_storage(&SsaType::U64));

        // Reference types are compatible
        assert!(SsaType::Object.is_compatible_for_storage(&SsaType::String));

        // Unknown is compatible with anything
        assert!(SsaType::Unknown.is_compatible_for_storage(&SsaType::I32));
        assert!(SsaType::I32.is_compatible_for_storage(&SsaType::Unknown));

        // Different classes are not compatible
        assert!(!SsaType::I32.is_compatible_for_storage(&SsaType::I64));
        assert!(!SsaType::I32.is_compatible_for_storage(&SsaType::Object));
        assert!(!SsaType::F32.is_compatible_for_storage(&SsaType::F64));
    }
}