Skip to main content

clr_assembler/program/
types.rs

1use gaia_types::{GaiaError, Result, SourceLocation};
2use std::collections::HashMap;
3
4/// CLR version information
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6pub struct ClrVersion {
7    /// Major version
8    pub major: u16,
9    /// Minor version
10    pub minor: u16,
11    /// Build number
12    pub build: u16,
13    /// Revision number
14    pub revision: u16,
15}
16
17impl Default for ClrVersion {
18    fn default() -> Self {
19        Self { major: 0, minor: 0, build: 0, revision: 0 }
20    }
21}
22
23/// CLR access modifiers and flags
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
25pub struct ClrAccessFlags {
26    /// Public access
27    pub is_public: bool,
28    /// Private access
29    pub is_private: bool,
30    /// Security transparent
31    pub is_security_transparent: bool,
32    /// Retargetable
33    pub is_retargetable: bool,
34}
35
36impl Default for ClrAccessFlags {
37    fn default() -> Self {
38        Self { is_public: false, is_private: true, is_security_transparent: false, is_retargetable: false }
39    }
40}
41
42/// CLR method implementation flags
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
44pub struct ClrMethodImplFlags {
45    /// Managed implementation
46    pub is_managed: bool,
47    /// Native implementation
48    pub is_native: bool,
49    /// Runtime implementation
50    pub is_runtime: bool,
51    /// Inline
52    pub is_inline: bool,
53    /// No inline
54    pub is_no_inline: bool,
55    /// Synchronized
56    pub is_synchronized: bool,
57}
58
59impl Default for ClrMethodImplFlags {
60    fn default() -> Self {
61        Self {
62            is_managed: true,
63            is_native: false,
64            is_runtime: false,
65            is_inline: false,
66            is_no_inline: false,
67            is_synchronized: false,
68        }
69    }
70}
71
72/// CLR reference type indicating what kind of member is being referenced.
73#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
74pub enum ClrReferenceType {
75    /// Type reference.
76    Type,
77    /// Method reference.
78    Method,
79    /// Field reference.
80    Field,
81    /// Generic member reference.
82    Member,
83}
84
85/// CLR type reference representing a reference to a type in the CLR type system.
86#[derive(Debug, Clone, PartialEq, Eq, Hash)]
87pub enum ClrTypeReference {
88    /// Primitive type reference (e.g., "int32", "string").
89    Primitive(String),
90    /// User-defined type reference with optional namespace.
91    Type(String, Option<String>),
92    /// Array type reference.
93    Array(Box<ClrTypeReference>),
94    /// Generic type instantiation with type arguments.
95    Generic(Box<ClrTypeReference>, Vec<ClrTypeReference>),
96}
97
98/// CLR external assembly reference representing a reference to an external .NET assembly.
99#[derive(Debug, Clone, PartialEq, Eq, Hash)]
100pub struct ClrExternalAssembly {
101    /// The name of the assembly.
102    pub name: String,
103    /// The version of the assembly.
104    pub version: ClrVersion,
105    /// The public key token for strong-named assemblies.
106    pub public_key_token: Option<Vec<u8>>,
107    /// The culture setting (e.g., "en-US").
108    pub culture: Option<String>,
109    /// The hash value for assembly verification.
110    pub hash_value: Option<Vec<u8>>,
111}
112
113/// CLR module definition representing a module within an assembly.
114#[derive(Debug, Clone, PartialEq, Eq, Hash)]
115pub struct ClrModule {
116    /// The name of the module.
117    pub name: String,
118    /// The module version identifier (GUID).
119    pub mvid: [u8; 16],
120}
121
122/// CLR attribute representing a custom attribute applied to a program element.
123#[derive(Debug, Clone, PartialEq, Eq)]
124pub struct ClrAttribute {
125    /// The full name of the attribute type.
126    pub name: String,
127    /// Positional arguments passed to the attribute constructor.
128    pub constructor_args: Vec<ClrAttributeValue>,
129    /// Named arguments (property or field values).
130    pub named_args: HashMap<String, ClrAttributeValue>,
131}
132
133/// CLR attribute value representing a value that can be passed to an attribute.
134#[derive(Debug, Clone, PartialEq, Eq)]
135pub enum ClrAttributeValue {
136    /// String value.
137    String(String),
138    /// 32-bit integer value.
139    Int32(i32),
140    /// Boolean value.
141    Boolean(bool),
142    /// Enum value with type name and underlying value.
143    Enum(String, i32),
144    /// Array of values.
145    Array(Vec<ClrAttributeValue>),
146}
147
148/// CLR field definition representing a field in a class or struct.
149#[derive(Debug, Clone, PartialEq, Eq)]
150pub struct ClrField {
151    /// The name of the field.
152    pub name: String,
153    /// The type of the field.
154    pub field_type: ClrTypeReference,
155    /// The access modifiers applied to this field.
156    pub access_flags: ClrAccessFlags,
157    /// Custom attributes applied to this field.
158    pub attributes: Vec<ClrAttribute>,
159    /// The initial value for static literal fields.
160    pub initial_value: Option<Vec<u8>>,
161}
162
163/// CLR method parameter definition representing a parameter in a method signature.
164#[derive(Debug, Clone, PartialEq, Eq)]
165pub struct ClrParameter {
166    /// The name of the parameter.
167    pub name: String,
168    /// The type of the parameter.
169    pub parameter_type: ClrTypeReference,
170    /// Custom attributes applied to this parameter.
171    pub attributes: Vec<ClrAttribute>,
172}
173
174/// CLR local variable definition representing a local variable in a method body.
175#[derive(Debug, Clone, PartialEq, Eq)]
176pub struct ClrLocalVariable {
177    /// The index of the local variable in the local variable table.
178    pub index: u32,
179    /// The type of the local variable.
180    pub variable_type: ClrTypeReference,
181    /// The optional name of the local variable (for debugging).
182    pub name: Option<String>,
183}
184
185/// CLR exception handler representing an exception handling clause in a method body.
186#[derive(Debug, Clone, PartialEq, Eq)]
187pub struct ClrExceptionHandler {
188    /// The type of exception handler.
189    pub handler_type: ClrExceptionHandlerType,
190    /// The IL offset where the try block starts.
191    pub try_start: u32,
192    /// The IL offset where the try block ends.
193    pub try_end: u32,
194    /// The IL offset where the handler starts.
195    pub handler_start: u32,
196    /// The IL offset where the handler ends.
197    pub handler_end: u32,
198    /// The exception type to catch (for catch handlers).
199    pub catch_type: Option<ClrTypeReference>,
200    /// The IL offset of the filter block (for filter handlers).
201    pub filter_start: Option<u32>,
202}
203
204/// CLR exception handler type indicating the kind of exception handling.
205#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
206pub enum ClrExceptionHandlerType {
207    /// Catch clause that handles a specific exception type.
208    Catch,
209    /// Filter clause that uses a predicate to determine if it should handle the exception.
210    Filter,
211    /// Finally clause that always executes regardless of exception.
212    Finally,
213    /// Fault clause that executes only when an exception occurs.
214    Fault,
215}