oak_c/ast/
mod.rs

1/// C 语言抽象语法
2#[derive(Debug, Clone, PartialEq)]
3pub struct CRoot {
4    pub translation_unit: TranslationUnit,
5}
6
7/// 翻译单元(C 程序的顶层结构)
8#[derive(Debug, Clone, PartialEq)]
9pub struct TranslationUnit {
10    pub external_declarations: Vec<ExternalDeclaration>,
11    pub span: core::range::Range<usize>,
12}
13
14/// 外部声明
15#[derive(Debug, Clone, PartialEq)]
16pub enum ExternalDeclaration {
17    /// 函数定义
18    FunctionDefinition(FunctionDefinition),
19    /// 声明
20    Declaration(Declaration),
21}
22
23/// 函数定义
24#[derive(Debug, Clone, PartialEq)]
25pub struct FunctionDefinition {
26    pub declaration_specifiers: Vec<DeclarationSpecifier>,
27    pub declarator: Declarator,
28    pub compound_statement: CompoundStatement,
29    pub span: core::range::Range<usize>,
30}
31
32/// 声明
33#[derive(Debug, Clone, PartialEq)]
34pub struct Declaration {
35    pub declaration_specifiers: Vec<DeclarationSpecifier>,
36    pub init_declarators: Vec<InitDeclarator>,
37    pub span: core::range::Range<usize>,
38}
39
40/// 声明说明
41#[derive(Debug, Clone, PartialEq)]
42pub enum DeclarationSpecifier {
43    /// 存储类说明符
44    StorageClassSpecifier(StorageClassSpecifier),
45    /// 类型说明
46    TypeSpecifier(TypeSpecifier),
47    /// 类型限定
48    TypeQualifier(TypeQualifier),
49    /// 函数说明
50    FunctionSpecifier(FunctionSpecifier),
51}
52
53/// 存储类说明符
54#[derive(Debug, Clone, PartialEq)]
55pub enum StorageClassSpecifier {
56    Typedef,
57    Extern,
58    Static,
59    Auto,
60    Register,
61}
62
63/// 类型说明
64#[derive(Debug, Clone, PartialEq)]
65pub enum TypeSpecifier {
66    Void,
67    Char,
68    Short,
69    Int,
70    Long,
71    Float,
72    Double,
73    Signed,
74    Unsigned,
75    Bool,
76    Complex,
77    Imaginary,
78    StructOrUnion(StructOrUnionSpecifier),
79    Enum(EnumSpecifier),
80    TypedefName(String),
81}
82
83/// 类型限定
84#[derive(Debug, Clone, PartialEq)]
85pub enum TypeQualifier {
86    Const,
87    Restrict,
88    Volatile,
89}
90
91/// 函数说明
92#[derive(Debug, Clone, PartialEq)]
93pub enum FunctionSpecifier {
94    Inline,
95    Noreturn,
96}
97
98/// 结构体或联合体说明符
99#[derive(Debug, Clone, PartialEq)]
100pub struct StructOrUnionSpecifier {
101    pub struct_or_union: StructOrUnion,
102    pub identifier: Option<String>,
103    pub struct_declarations: Option<Vec<StructDeclaration>>,
104    pub span: core::range::Range<usize>,
105}
106
107/// 结构体或联合
108#[derive(Debug, Clone, PartialEq)]
109pub enum StructOrUnion {
110    Struct,
111    Union,
112}
113
114/// 结构体声
115#[derive(Debug, Clone, PartialEq)]
116pub struct StructDeclaration {
117    pub specifier_qualifiers: Vec<SpecifierQualifier>,
118    pub struct_declarators: Vec<StructDeclarator>,
119    pub span: core::range::Range<usize>,
120}
121
122/// 说明符限定符
123#[derive(Debug, Clone, PartialEq)]
124pub enum SpecifierQualifier {
125    TypeSpecifier(TypeSpecifier),
126    TypeQualifier(TypeQualifier),
127}
128
129/// 结构体声明符
130#[derive(Debug, Clone, PartialEq)]
131pub struct StructDeclarator {
132    pub declarator: Option<Declarator>,
133    pub constant_expression: Option<Expression>,
134    pub span: core::range::Range<usize>,
135}
136
137/// 枚举说明
138#[derive(Debug, Clone, PartialEq)]
139pub struct EnumSpecifier {
140    pub identifier: Option<String>,
141    pub enumerators: Option<Vec<Enumerator>>,
142    pub span: core::range::Range<usize>,
143}
144
145/// 枚举
146#[derive(Debug, Clone, PartialEq)]
147pub struct Enumerator {
148    pub identifier: String,
149    pub constant_expression: Option<Expression>,
150    pub span: core::range::Range<usize>,
151}
152
153/// 初始化声明符
154#[derive(Debug, Clone, PartialEq)]
155pub struct InitDeclarator {
156    pub declarator: Declarator,
157    pub initializer: Option<Initializer>,
158    pub span: core::range::Range<usize>,
159}
160
161/// 声明
162#[derive(Debug, Clone, PartialEq)]
163pub struct Declarator {
164    pub pointer: Option<Pointer>,
165    pub direct_declarator: DirectDeclarator,
166    pub span: core::range::Range<usize>,
167}
168
169/// 指针
170#[derive(Debug, Clone, PartialEq)]
171pub struct Pointer {
172    pub type_qualifiers: Vec<TypeQualifier>,
173    pub pointer: Option<Box<Pointer>>,
174    pub span: core::range::Range<usize>,
175}
176
177/// 直接声明
178#[derive(Debug, Clone, PartialEq)]
179pub enum DirectDeclarator {
180    Identifier(String),
181    Declarator(Box<Declarator>),
182    Array {
183        declarator: Box<DirectDeclarator>,
184        assignment_expression: Option<Expression>,
185    },
186    Function {
187        declarator: Box<DirectDeclarator>,
188        parameter_type_list: Option<ParameterTypeList>,
189        identifier_list: Option<Vec<String>>,
190    },
191}
192
193/// 参数类型列表
194#[derive(Debug, Clone, PartialEq)]
195pub struct ParameterTypeList {
196    pub parameter_list: Vec<ParameterDeclaration>,
197    pub variadic: bool,
198    pub span: core::range::Range<usize>,
199}
200
201/// 参数声明
202#[derive(Debug, Clone, PartialEq)]
203pub struct ParameterDeclaration {
204    pub declaration_specifiers: Vec<DeclarationSpecifier>,
205    pub declarator: Option<Declarator>,
206    pub abstract_declarator: Option<AbstractDeclarator>,
207    pub span: core::range::Range<usize>,
208}
209
210/// 抽象声明
211#[derive(Debug, Clone, PartialEq)]
212pub struct AbstractDeclarator {
213    pub pointer: Option<Pointer>,
214    pub direct_abstract_declarator: Option<Box<DirectAbstractDeclarator>>,
215    pub span: core::range::Range<usize>,
216}
217
218/// 直接抽象声明
219#[derive(Debug, Clone, PartialEq)]
220pub enum DirectAbstractDeclarator {
221    AbstractDeclarator(Box<AbstractDeclarator>),
222    Array { declarator: Option<Box<DirectAbstractDeclarator>>, assignment_expression: Option<Box<Expression>> },
223    Function { declarator: Option<Box<DirectAbstractDeclarator>>, parameter_type_list: Option<ParameterTypeList> },
224}
225
226/// 初始化器
227#[derive(Debug, Clone, PartialEq)]
228pub enum Initializer {
229    AssignmentExpression(Expression),
230    InitializerList(Vec<Initializer>),
231}
232
233/// 语句
234#[derive(Debug, Clone, PartialEq)]
235pub enum Statement {
236    /// 标号语句
237    Labeled(LabeledStatement),
238    /// 复合语句
239    Compound(CompoundStatement),
240    /// 表达式语
241    Expression(ExpressionStatement),
242    /// 选择语句
243    Selection(SelectionStatement),
244    /// 迭代语句
245    Iteration(IterationStatement),
246    /// 跳转语句
247    Jump(JumpStatement),
248}
249
250/// 标号语句
251#[derive(Debug, Clone, PartialEq)]
252pub enum LabeledStatement {
253    Label { identifier: String, statement: Box<Statement> },
254    Case { constant_expression: Expression, statement: Box<Statement> },
255    Default { statement: Box<Statement> },
256}
257
258/// 复合语句
259#[derive(Debug, Clone, PartialEq)]
260pub struct CompoundStatement {
261    pub block_items: Vec<BlockItem>,
262    pub span: core::range::Range<usize>,
263}
264
265/// 块项
266#[derive(Debug, Clone, PartialEq)]
267pub enum BlockItem {
268    Declaration(Declaration),
269    Statement(Statement),
270}
271
272/// 表达式语
273#[derive(Debug, Clone, PartialEq)]
274pub struct ExpressionStatement {
275    pub expression: Option<Expression>,
276    pub span: core::range::Range<usize>,
277}
278
279/// 选择语句
280#[derive(Debug, Clone, PartialEq)]
281pub enum SelectionStatement {
282    If { condition: Expression, then_statement: Box<Statement>, else_statement: Option<Box<Statement>> },
283    Switch { expression: Expression, statement: Box<Statement> },
284}
285
286/// 迭代语句
287#[derive(Debug, Clone, PartialEq)]
288pub enum IterationStatement {
289    While { condition: Expression, statement: Box<Statement> },
290    DoWhile { statement: Box<Statement>, condition: Expression },
291    For { init: Option<Expression>, condition: Option<Expression>, update: Option<Expression>, statement: Box<Statement> },
292}
293
294/// 跳转语句
295#[derive(Debug, Clone, PartialEq)]
296pub enum JumpStatement {
297    Goto(String),
298    Continue,
299    Break,
300    Return(Option<Expression>),
301}
302
303/// 表达
304#[derive(Debug, Clone, PartialEq)]
305pub struct Expression {
306    pub kind: Box<ExpressionKind>,
307    pub span: core::range::Range<usize>,
308}
309
310/// 表达式种
311#[derive(Debug, Clone, PartialEq)]
312pub enum ExpressionKind {
313    /// 标识
314    Identifier(String),
315    /// 常量
316    Constant(Constant),
317    /// 字符串字面量
318    StringLiteral(String),
319    /// 数组下标
320    ArraySubscript { array: Box<Expression>, index: Box<Expression> },
321    /// 函数调用
322    FunctionCall { function: Box<Expression>, arguments: Vec<Expression> },
323    /// 成员访问
324    MemberAccess {
325        object: Box<Expression>,
326        member: String,
327        is_pointer: bool, // true for ->, false for .
328    },
329    /// 后缀递增/递减
330    PostfixIncDec { operand: Box<Expression>, is_increment: bool },
331    /// 前缀递增/递减
332    PrefixIncDec { operand: Box<Expression>, is_increment: bool },
333    /// 一元操作符
334    Unary { operator: UnaryOperator, operand: Box<Expression> },
335    /// 类型转换
336    Cast { type_name: Box<TypeName>, expression: Box<Expression> },
337    /// 二元操作
338    Binary { left: Box<Expression>, operator: BinaryOperator, right: Box<Expression> },
339    /// 条件表达
340    Conditional { condition: Box<Expression>, then_expr: Box<Expression>, else_expr: Box<Expression> },
341    /// 赋值表达式
342    Assignment { left: Box<Expression>, operator: AssignmentOperator, right: Box<Expression> },
343    /// 逗号表达
344    Comma { expressions: Vec<Expression> },
345}
346
347/// 常量
348#[derive(Debug, Clone, PartialEq)]
349pub enum Constant {
350    Integer(i64),
351    Float(f64),
352    Character(char),
353}
354
355/// 一元操作符
356#[derive(Debug, Clone, PartialEq)]
357pub enum UnaryOperator {
358    AddressOf,   // &
359    Dereference, // *
360    Plus,        // +
361    Minus,       // -
362    BitwiseNot,  // ~
363    LogicalNot,  // !
364    Sizeof,      // sizeof
365}
366
367/// 二元操作
368#[derive(Debug, Clone, PartialEq)]
369pub enum BinaryOperator {
370    // 算术操作
371    Add,      // +
372    Subtract, // -
373    Multiply, // *
374    Divide,   // /
375    Modulo,   // %
376
377    // 位操作符
378    BitwiseAnd, // &
379    BitwiseOr,  // |
380    BitwiseXor, // ^
381    LeftShift,  // <<
382    RightShift, // >>
383
384    // 比较操作
385    Equal,        // ==
386    NotEqual,     // !=
387    Less,         // <
388    Greater,      // >
389    LessEqual,    // <=
390    GreaterEqual, // >=
391
392    // 逻辑操作
393    LogicalAnd, // &&
394    LogicalOr,  // ||
395}
396
397/// 赋值操作符
398#[derive(Debug, Clone, PartialEq)]
399pub enum AssignmentOperator {
400    Assign,           // =
401    AddAssign,        // +=
402    SubAssign,        // -=
403    MulAssign,        // *=
404    DivAssign,        // /=
405    ModAssign,        // %=
406    AndAssign,        // &=
407    OrAssign,         // |=
408    XorAssign,        // ^=
409    LeftShiftAssign,  // <<=
410    RightShiftAssign, // >>=
411}
412
413/// 类型
414#[derive(Debug, Clone, PartialEq)]
415pub struct TypeName {
416    pub specifier_qualifiers: Vec<SpecifierQualifier>,
417    pub abstract_declarator: Option<Box<AbstractDeclarator>>,
418    pub span: core::range::Range<usize>,
419}
420
421impl CRoot {
422    /// 创建新的 AST
423    pub fn new(translation_unit: TranslationUnit) -> Self {
424        Self { translation_unit }
425    }
426}
427
428impl TranslationUnit {
429    /// 创建新的翻译单元
430    pub fn new(external_declarations: Vec<ExternalDeclaration>, span: core::range::Range<usize>) -> Self {
431        Self { external_declarations, span }
432    }
433}