clr_assembler/formats/msil/ast/mod.rs
1//! MSIL (Microsoft Intermediate Language) 抽象语法树 (AST) 模块
2//!
3//! 这个模块定义了 MSIL 汇编语言的抽象语法树结构,用于表示解析后的 MSIL 代码。
4//! AST 节点对应于 MSIL 汇编语言中的各种构造,如程序集、模块、类、方法等。
5
6/// MSIL 程序的根节点
7///
8/// 表示一个完整的 MSIL 程序,包含程序中的所有语句。
9///
10/// # 示例
11///
12/// ```rust
13/// use clr_msil::ast::{MsilRoot, MsilStatement};
14///
15/// let root = MsilRoot { statements: vec![MsilStatement::Assembly("MyAssembly".to_string())] };
16/// ```
17#[derive(Clone, Debug)]
18pub struct MsilRoot {
19 /// 程序中的所有语句
20 pub statements: Vec<MsilStatement>,
21}
22
23/// MSIL 语句枚举
24///
25/// 表示 MSIL 程序中的各种顶级语句。
26///
27/// # 变体
28///
29/// - `AssemblyExtern`: 外部程序集引用声明
30/// - `Assembly`: 程序集声明
31/// - `Module`: 模块声明
32/// - `Class`: 类声明
33#[derive(Clone, Debug)]
34pub enum MsilStatement {
35 /// 外部程序集引用声明
36 ///
37 /// 例如:`.assembly extern UnityEngine`
38 AssemblyExtern(String),
39 /// 程序集声明
40 ///
41 /// 例如:`.assembly MyAssembly`
42 Assembly(String),
43 /// 模块声明
44 ///
45 /// 例如:`.module MyModule.dll`
46 Module(String),
47 /// 类声明
48 ///
49 /// 包含类的完整定义,包括修饰符、名称、基类和成员方法。
50 Class(MsilClass),
51}
52
53/// MSIL 类定义
54///
55/// 表示一个 MSIL 类,包含类的所有属性和方法。
56///
57/// # 示例
58///
59/// ```rust
60/// use clr_msil::ast::{MsilClass, MsilMethod};
61///
62/// let class = MsilClass {
63/// modifiers: vec!["public".to_string(), "auto".to_string()],
64/// name: "MyClass".to_string(),
65/// extends: Some("System.Object".to_string()),
66/// methods: vec![],
67/// };
68/// ```
69#[derive(Clone, Debug)]
70pub struct MsilClass {
71 /// 类修饰符(如 public, private, auto, ansi 等)
72 pub modifiers: Vec<String>,
73 /// 类名(可能包含命名空间)
74 pub name: String,
75 /// 基类名称(如果存在继承)
76 pub extends: Option<String>,
77 /// 类中包含的方法
78 pub methods: Vec<MsilMethod>,
79}
80
81/// MSIL 方法定义
82///
83/// 表示一个 MSIL 方法,包含方法签名和方法体。
84///
85/// # 示例
86///
87/// ```rust
88/// use clr_msil::ast::{MsilMethod, MsilMethodBody, MsilParameter};
89///
90/// let method = MsilMethod {
91/// modifiers: vec!["public".to_string(), "static".to_string()],
92/// return_type: "void".to_string(),
93/// name: "Main".to_string(),
94/// parameters: vec![MsilParameter {
95/// param_type: "string[]".to_string(),
96/// name: Some("args".to_string()),
97/// }],
98/// body: None,
99/// };
100/// ```
101#[derive(Clone, Debug)]
102pub struct MsilMethod {
103 /// 方法修饰符(如 public, static, virtual 等)
104 pub modifiers: Vec<String>,
105 /// 返回类型
106 pub return_type: String,
107 /// 方法名
108 pub name: String,
109 /// 方法参数列表
110 pub parameters: Vec<MsilParameter>,
111 /// 方法体(包含指令和局部变量)
112 pub body: Option<MsilMethodBody>,
113}
114
115/// MSIL 方法参数定义
116///
117/// 表示方法的一个参数。
118#[derive(Clone, Debug)]
119pub struct MsilParameter {
120 /// 参数类型
121 pub param_type: String,
122 /// 参数名(可选)
123 pub name: Option<String>,
124}
125
126/// MSIL 方法体定义
127///
128/// 包含方法的实现细节,如最大栈大小、局部变量和指令序列。
129///
130/// # 示例
131///
132/// ```rust
133/// use clr_msil::ast::{MsilInstruction, MsilLocal, MsilMethodBody};
134///
135/// let body = MsilMethodBody {
136/// maxstack: Some(8),
137/// locals: vec![MsilLocal {
138/// index: Some(0),
139/// local_type: "bool".to_string(),
140/// name: Some("V_0".to_string()),
141/// }],
142/// instructions: vec![MsilInstruction {
143/// opcode: "ldstr".to_string(),
144/// operands: vec!["Hello World!".to_string()],
145/// label: None,
146/// }],
147/// };
148/// ```
149#[derive(Clone, Debug)]
150pub struct MsilMethodBody {
151 /// 最大栈大小
152 pub maxstack: Option<u32>,
153 /// 局部变量列表
154 pub locals: Vec<MsilLocal>,
155 /// 指令序列
156 pub instructions: Vec<MsilInstruction>,
157}
158
159/// MSIL 局部变量定义
160///
161/// 表示方法中的一个局部变量。
162#[derive(Clone, Debug)]
163pub struct MsilLocal {
164 /// 变量索引(可选)
165 pub index: Option<u32>,
166 /// 变量类型
167 pub local_type: String,
168 /// 变量名(可选)
169 pub name: Option<String>,
170}
171
172/// MSIL 指令定义
173///
174/// 表示一条 MSIL 指令,包含操作码和操作数。
175///
176/// # 示例
177///
178/// ```rust
179/// use clr_msil::ast::MsilInstruction;
180///
181/// let instruction = MsilInstruction {
182/// opcode: "call".to_string(),
183/// operands: vec!["void [UnityEngine]UnityEngine.Debug::Log(object)".to_string()],
184/// label: Some("IL_0006".to_string()),
185/// };
186/// ```
187#[derive(Clone, Debug)]
188pub struct MsilInstruction {
189 /// 指令操作码(如 ldstr, call, ret 等)
190 pub opcode: String,
191 /// 指令操作数
192 pub operands: Vec<String>,
193 /// 指令标签(可选,用于跳转目标)
194 pub label: Option<String>,
195}