Skip to main content

clr_assembler/program/
entities.rs

1use super::{
2    instructions::ClrInstruction,
3    pool::ClrConstantPool,
4    types::{
5        ClrAccessFlags, ClrAttribute, ClrExceptionHandler, ClrExternalAssembly, ClrField, ClrLocalVariable, ClrMethodImplFlags,
6        ClrModule, ClrParameter, ClrTypeReference, ClrVersion,
7    },
8};
9use gaia_types::{GaiaError, Result, SourceLocation};
10
11/// CLR assembly structure representing a complete .NET assembly.
12#[derive(Debug, Clone)]
13pub struct ClrProgram {
14    /// The name of the assembly.
15    pub name: String,
16    /// The version of the assembly.
17    pub version: ClrVersion,
18    /// Access flags for the assembly.
19    pub access_flags: ClrAccessFlags,
20    /// External assembly references.
21    pub external_assemblies: Vec<ClrExternalAssembly>,
22    /// Module definition.
23    pub module: Option<ClrModule>,
24    /// Types defined in the assembly.
25    pub types: Vec<ClrType>,
26    /// Global methods in the assembly.
27    pub global_methods: Vec<ClrMethod>,
28    /// Global fields in the assembly.
29    pub global_fields: Vec<ClrField>,
30    /// Assembly-level attributes.
31    pub attributes: Vec<ClrAttribute>,
32    /// Constant pool for string and type references.
33    pub constant_pool: ClrConstantPool,
34    /// Source file path.
35    pub source_file: Option<String>,
36}
37
38impl ClrProgram {
39    /// Creates a new assembly with the specified name.
40    pub fn new(name: impl Into<String>) -> Self {
41        Self {
42            name: name.into(),
43            version: ClrVersion::default(),
44            access_flags: ClrAccessFlags::default(),
45            external_assemblies: Vec::new(),
46            module: None,
47            types: Vec::new(),
48            global_methods: Vec::new(),
49            global_fields: Vec::new(),
50            attributes: Vec::new(),
51            constant_pool: ClrConstantPool::new(),
52            source_file: None,
53        }
54    }
55
56    /// Adds a type to the assembly.
57    pub fn add_type(&mut self, clr_type: ClrType) {
58        self.types.push(clr_type);
59    }
60
61    /// Adds a global method to the assembly.
62    pub fn add_global_method(&mut self, method: ClrMethod) {
63        self.global_methods.push(method);
64    }
65
66    /// Adds a global field to the assembly.
67    pub fn add_global_field(&mut self, field: ClrField) {
68        self.global_fields.push(field);
69    }
70
71    /// Adds an external assembly reference.
72    pub fn add_external_assembly(&mut self, assembly: ClrExternalAssembly) {
73        self.external_assemblies.push(assembly);
74    }
75
76    /// Validates the assembly definition.
77    pub fn validate(&self) -> Result<()> {
78        if self.name.is_empty() {
79            return Err(GaiaError::syntax_error("Assembly name cannot be empty".to_string(), SourceLocation::default()));
80        }
81
82        for clr_type in &self.types {
83            clr_type.validate()?;
84        }
85
86        for method in &self.global_methods {
87            method.validate()?;
88        }
89
90        Ok(())
91    }
92}
93
94/// CLR type definition representing a class, struct, or interface.
95#[derive(Debug, Clone)]
96pub struct ClrType {
97    /// The name of the type.
98    pub name: String,
99    /// The namespace containing the type.
100    pub namespace: Option<String>,
101    /// Access flags for the type.
102    pub access_flags: ClrAccessFlags,
103    /// The base type this type inherits from.
104    pub base_type: Option<ClrTypeReference>,
105    /// Interfaces implemented by this type.
106    pub interfaces: Vec<ClrTypeReference>,
107    /// Fields defined in this type.
108    pub fields: Vec<ClrField>,
109    /// Methods defined in this type.
110    pub methods: Vec<ClrMethod>,
111    /// Properties defined in this type.
112    pub properties: Vec<String>,
113    /// Events defined in this type.
114    pub events: Vec<String>,
115    /// Nested types defined within this type.
116    pub nested_types: Vec<ClrType>,
117    /// Custom attributes applied to this type.
118    pub attributes: Vec<ClrAttribute>,
119}
120
121impl ClrType {
122    /// Creates a new type with the specified name and optional namespace.
123    pub fn new(name: String, namespace: Option<String>) -> Self {
124        Self {
125            name,
126            namespace,
127            access_flags: ClrAccessFlags::default(),
128            base_type: None,
129            interfaces: Vec::new(),
130            fields: Vec::new(),
131            methods: Vec::new(),
132            properties: Vec::new(),
133            events: Vec::new(),
134            nested_types: Vec::new(),
135            attributes: Vec::new(),
136        }
137    }
138
139    /// Adds a method to the type.
140    pub fn add_method(&mut self, method: ClrMethod) {
141        self.methods.push(method);
142    }
143
144    /// Adds a field to the type.
145    pub fn add_field(&mut self, field: ClrField) {
146        self.fields.push(field);
147    }
148
149    /// Validates the type definition.
150    pub fn validate(&self) -> Result<()> {
151        if self.name.is_empty() {
152            return Err(GaiaError::syntax_error("Type name cannot be empty".to_string(), SourceLocation::default()));
153        }
154
155        for method in &self.methods {
156            method.validate()?;
157        }
158
159        Ok(())
160    }
161}
162
163/// CLR method definition representing a method in a type.
164#[derive(Debug, Clone)]
165pub struct ClrMethod {
166    /// The name of the method.
167    pub name: String,
168    /// The return type of the method.
169    pub return_type: ClrTypeReference,
170    /// The parameters of the method.
171    pub parameters: Vec<ClrParameter>,
172    /// Access flags for the method.
173    pub access_flags: ClrAccessFlags,
174    /// Implementation flags for the method.
175    pub impl_flags: ClrMethodImplFlags,
176    /// The IL instructions in the method body.
177    pub instructions: Vec<ClrInstruction>,
178    /// The maximum stack depth for the method.
179    pub max_stack: u32,
180    /// Local variables defined in the method.
181    pub locals: Vec<ClrLocalVariable>,
182    /// Exception handlers defined in the method.
183    pub exception_handlers: Vec<ClrExceptionHandler>,
184    /// Custom attributes applied to this method.
185    pub attributes: Vec<ClrAttribute>,
186    /// Whether this method is the assembly entry point.
187    pub is_entry_point: bool,
188}
189
190impl ClrMethod {
191    /// Creates a new method with the specified name and return type.
192    pub fn new(name: String, return_type: ClrTypeReference) -> Self {
193        Self {
194            name,
195            return_type,
196            parameters: Vec::new(),
197            access_flags: ClrAccessFlags::default(),
198            impl_flags: ClrMethodImplFlags::default(),
199            instructions: Vec::new(),
200            max_stack: 8,
201            locals: Vec::new(),
202            exception_handlers: Vec::new(),
203            attributes: Vec::new(),
204            is_entry_point: false,
205        }
206    }
207
208    /// Adds an instruction to the method body.
209    pub fn add_instruction(&mut self, instruction: ClrInstruction) {
210        self.instructions.push(instruction);
211    }
212
213    /// Adds a parameter to the method.
214    pub fn add_parameter(&mut self, parameter: ClrParameter) {
215        self.parameters.push(parameter);
216    }
217
218    /// Validates the method definition.
219    pub fn validate(&self) -> Result<()> {
220        if self.name.is_empty() {
221            return Err(GaiaError::syntax_error("Method name cannot be empty".to_string(), SourceLocation::default()));
222        }
223
224        Ok(())
225    }
226}