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 程序集结构
12#[derive(Debug, Clone)]
13pub struct ClrProgram {
14    /// 程序集名称
15    pub name: String,
16    /// 程序集版本
17    pub version: ClrVersion,
18    /// 访问标志
19    pub access_flags: ClrAccessFlags,
20    /// 外部程序集引用
21    pub external_assemblies: Vec<ClrExternalAssembly>,
22    /// 模块定义
23    pub module: Option<ClrModule>,
24    /// 程序集中的类型
25    pub types: Vec<ClrType>,
26    /// 全局方法
27    pub global_methods: Vec<ClrMethod>,
28    /// 全局字段
29    pub global_fields: Vec<ClrField>,
30    /// 程序集特性
31    pub attributes: Vec<ClrAttribute>,
32    /// 常量池
33    pub constant_pool: ClrConstantPool,
34    /// 源文件路径
35    pub source_file: Option<String>,
36}
37
38impl ClrProgram {
39    /// 创建新的程序集
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    /// 添加类型到程序集
57    pub fn add_type(&mut self, clr_type: ClrType) {
58        self.types.push(clr_type);
59    }
60
61    /// 添加全局方法
62    pub fn add_global_method(&mut self, method: ClrMethod) {
63        self.global_methods.push(method);
64    }
65
66    /// 添加全局字段
67    pub fn add_global_field(&mut self, field: ClrField) {
68        self.global_fields.push(field);
69    }
70
71    /// 添加外部程序集引用
72    pub fn add_external_assembly(&mut self, assembly: ClrExternalAssembly) {
73        self.external_assemblies.push(assembly);
74    }
75
76    /// 验证程序集定义
77    pub fn validate(&self) -> Result<()> {
78        // 验证程序集名称
79        if self.name.is_empty() {
80            return Err(GaiaError::syntax_error("程序集名称不能为空".to_string(), SourceLocation::default()));
81        }
82
83        // 验证所有类型
84        for clr_type in &self.types {
85            clr_type.validate()?;
86        }
87
88        // 验证全局方法
89        for method in &self.global_methods {
90            method.validate()?;
91        }
92
93        Ok(())
94    }
95}
96
97/// CLR 类型定义
98#[derive(Debug, Clone)]
99pub struct ClrType {
100    /// 类型名称
101    pub name: String,
102    /// 命名空间
103    pub namespace: Option<String>,
104    /// 访问标志
105    pub access_flags: ClrAccessFlags,
106    /// 基类
107    pub base_type: Option<ClrTypeReference>,
108    /// 实现的接口
109    pub interfaces: Vec<ClrTypeReference>,
110    /// 类型中的字段
111    pub fields: Vec<ClrField>,
112    /// 类型中的方法
113    pub methods: Vec<ClrMethod>,
114    /// 类型中的属性
115    pub properties: Vec<String>, // 简化处理
116    /// 类型中的事件
117    pub events: Vec<String>, // 简化处理
118    /// 嵌套类型
119    pub nested_types: Vec<ClrType>,
120    /// 类型特性
121    pub attributes: Vec<ClrAttribute>,
122}
123
124impl ClrType {
125    /// 创建新的类型
126    pub fn new(name: String, namespace: Option<String>) -> Self {
127        Self {
128            name,
129            namespace,
130            access_flags: ClrAccessFlags::default(),
131            base_type: None,
132            interfaces: Vec::new(),
133            fields: Vec::new(),
134            methods: Vec::new(),
135            properties: Vec::new(),
136            events: Vec::new(),
137            nested_types: Vec::new(),
138            attributes: Vec::new(),
139        }
140    }
141
142    /// 添加方法
143    pub fn add_method(&mut self, method: ClrMethod) {
144        self.methods.push(method);
145    }
146
147    /// 添加字段
148    pub fn add_field(&mut self, field: ClrField) {
149        self.fields.push(field);
150    }
151
152    /// 验证类型定义
153    pub fn validate(&self) -> Result<()> {
154        // 验证类型名称
155        if self.name.is_empty() {
156            return Err(GaiaError::syntax_error("类型名称不能为空".to_string(), SourceLocation::default()));
157        }
158
159        // 验证方法
160        for method in &self.methods {
161            method.validate()?;
162        }
163
164        Ok(())
165    }
166}
167
168/// CLR 方法定义
169#[derive(Debug, Clone)]
170pub struct ClrMethod {
171    /// 方法名称
172    pub name: String,
173    /// 返回值类型
174    pub return_type: ClrTypeReference,
175    /// 方法参数
176    pub parameters: Vec<ClrParameter>,
177    /// 访问标志
178    pub access_flags: ClrAccessFlags,
179    /// 实现标志
180    pub impl_flags: ClrMethodImplFlags,
181    /// 指令列表
182    pub instructions: Vec<ClrInstruction>,
183    /// 最大栈深度
184    pub max_stack: u32,
185    /// 局部变量
186    pub locals: Vec<ClrLocalVariable>,
187    /// 异常处理器
188    pub exception_handlers: Vec<ClrExceptionHandler>,
189    /// 方法特性
190    pub attributes: Vec<ClrAttribute>,
191    /// 是否为入口点
192    pub is_entry_point: bool,
193}
194
195impl ClrMethod {
196    /// 创建新的方法
197    pub fn new(name: String, return_type: ClrTypeReference) -> Self {
198        Self {
199            name,
200            return_type,
201            parameters: Vec::new(),
202            access_flags: ClrAccessFlags::default(),
203            impl_flags: ClrMethodImplFlags::default(),
204            instructions: Vec::new(),
205            max_stack: 8,
206            locals: Vec::new(),
207            exception_handlers: Vec::new(),
208            attributes: Vec::new(),
209            is_entry_point: false,
210        }
211    }
212
213    /// 添加指令
214    pub fn add_instruction(&mut self, instruction: ClrInstruction) {
215        self.instructions.push(instruction);
216    }
217
218    /// 添加参数
219    pub fn add_parameter(&mut self, parameter: ClrParameter) {
220        self.parameters.push(parameter);
221    }
222
223    /// 验证方法定义
224    pub fn validate(&self) -> Result<()> {
225        // 验证方法名称
226        if self.name.is_empty() {
227            return Err(GaiaError::syntax_error("方法名称不能为空".to_string(), SourceLocation::default()));
228        }
229
230        Ok(())
231    }
232}