oak_cpp/ast/
mod.rs

1use core::range::Range;
2use serde::{Deserialize, Serialize};
3
4/// Type alias for source span
5type SourceSpan = Range<usize>;
6
7/// Abstract syntax tree for C++ language
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9pub struct CppRoot {
10    pub translation_unit: TranslationUnit,
11}
12
13/// Translation unit (top-level structure of a C++ program)
14#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
15pub struct TranslationUnit {
16    pub external_declarations: Vec<ExternalDeclaration>,
17    #[serde(with = "oak_core::serde_range")]
18    pub span: Range<usize>,
19}
20
21/// External declaration
22#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
23pub enum ExternalDeclaration {
24    /// Function definition
25    FunctionDefinition(FunctionDefinition),
26    /// Declaration
27    Declaration(Declaration),
28}
29
30/// Function definition
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32pub struct FunctionDefinition {
33    pub declaration_specifiers: Vec<DeclarationSpecifier>,
34    pub declarator: Declarator,
35    pub compound_statement: CompoundStatement,
36    #[serde(with = "oak_core::serde_range")]
37    pub span: SourceSpan,
38}
39
40/// Declaration
41#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
42pub struct Declaration {
43    pub declaration_specifiers: Vec<DeclarationSpecifier>,
44    pub init_declarators: Vec<InitDeclarator>,
45    #[serde(with = "oak_core::serde_range")]
46    pub span: SourceSpan,
47}
48
49/// Declaration specifier
50#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
51pub enum DeclarationSpecifier {
52    /// Storage class specifier
53    StorageClassSpecifier(StorageClassSpecifier),
54    /// Type specifier
55    TypeSpecifier(TypeSpecifier),
56    /// Type qualifier
57    TypeQualifier(TypeQualifier),
58    /// Function specifier
59    FunctionSpecifier(FunctionSpecifier),
60}
61
62/// Storage class specifier
63#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
64pub enum StorageClassSpecifier {
65    Typedef,
66    Extern,
67    Static,
68    Auto,
69    Register,
70}
71
72/// Type specifier
73#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
74pub enum TypeSpecifier {
75    Void,
76    Char,
77    Short,
78    Int,
79    Long,
80    Float,
81    Double,
82    Signed,
83    Unsigned,
84    Bool,
85    Complex,
86    Imaginary,
87    StructOrUnion(StructOrUnionSpecifier),
88    Enum(EnumSpecifier),
89    TypedefName(String),
90}
91
92/// Type qualifier
93#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
94pub enum TypeQualifier {
95    Const,
96    Restrict,
97    Volatile,
98}
99
100/// Function specifier
101#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
102pub enum FunctionSpecifier {
103    Inline,
104    Noreturn,
105}
106
107/// Struct or union specifier
108#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
109pub struct StructOrUnionSpecifier {
110    pub struct_or_union: StructOrUnion,
111    pub identifier: Option<String>,
112    pub struct_declarations: Option<Vec<StructDeclaration>>,
113    #[serde(with = "oak_core::serde_range")]
114    pub span: SourceSpan,
115}
116
117/// Struct or union
118#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
119pub enum StructOrUnion {
120    Struct,
121    Union,
122}
123
124/// Struct declaration
125#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
126pub struct StructDeclaration {
127    pub specifier_qualifiers: Vec<SpecifierQualifier>,
128    pub struct_declarators: Vec<StructDeclarator>,
129    #[serde(with = "oak_core::serde_range")]
130    pub span: SourceSpan,
131}
132
133/// Specifier qualifier
134#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
135pub enum SpecifierQualifier {
136    TypeSpecifier(TypeSpecifier),
137    TypeQualifier(TypeQualifier),
138}
139
140/// Struct declarator
141#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
142pub struct StructDeclarator {
143    pub declarator: Option<Declarator>,
144    pub constant_expression: Option<Expression>,
145    #[serde(with = "oak_core::serde_range")]
146    pub span: SourceSpan,
147}
148
149/// Enum specifier
150#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
151pub struct EnumSpecifier {
152    pub identifier: Option<String>,
153    pub enumerators: Option<Vec<Enumerator>>,
154    #[serde(with = "oak_core::serde_range")]
155    pub span: SourceSpan,
156}
157
158/// Enumerator
159#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
160pub struct Enumerator {
161    pub identifier: String,
162    pub constant_expression: Option<Expression>,
163    #[serde(with = "oak_core::serde_range")]
164    pub span: SourceSpan,
165}
166
167/// Init declarator
168#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
169pub struct InitDeclarator {
170    pub declarator: Declarator,
171    pub initializer: Option<Initializer>,
172    #[serde(with = "oak_core::serde_range")]
173    pub span: SourceSpan,
174}
175
176/// Declarator
177#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
178pub struct Declarator {
179    pub pointer: Option<Pointer>,
180    pub direct_declarator: DirectDeclarator,
181    #[serde(with = "oak_core::serde_range")]
182    pub span: SourceSpan,
183}
184
185/// Pointer
186#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
187pub struct Pointer {
188    pub type_qualifiers: Vec<TypeQualifier>,
189    pub pointer: Option<Box<Pointer>>,
190    #[serde(with = "oak_core::serde_range")]
191    pub span: SourceSpan,
192}
193
194/// Direct declarator
195#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
196pub enum DirectDeclarator {
197    Identifier(String),
198    Declarator(Box<Declarator>),
199    Array { declarator: Box<DirectDeclarator>, assignment_expression: Option<Expression> },
200    Function { declarator: Box<DirectDeclarator>, parameter_type_list: Option<ParameterTypeList>, identifier_list: Option<Vec<String>> },
201}
202
203/// Parameter type list
204#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
205pub struct ParameterTypeList {
206    pub parameter_list: Vec<ParameterDeclaration>,
207    pub variadic: bool,
208    #[serde(with = "oak_core::serde_range")]
209    pub span: SourceSpan,
210}
211
212/// Parameter declaration
213#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
214pub struct ParameterDeclaration {
215    pub declaration_specifiers: Vec<DeclarationSpecifier>,
216    pub declarator: Option<Declarator>,
217    pub abstract_declarator: Option<AbstractDeclarator>,
218    #[serde(with = "oak_core::serde_range")]
219    pub span: SourceSpan,
220}
221
222/// Abstract declarator
223#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
224pub struct AbstractDeclarator {
225    pub pointer: Option<Pointer>,
226    pub direct_abstract_declarator: Option<Box<DirectAbstractDeclarator>>,
227    #[serde(with = "oak_core::serde_range")]
228    pub span: Range<usize>,
229}
230
231/// Direct abstract declarator
232#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
233pub enum DirectAbstractDeclarator {
234    AbstractDeclarator(Box<AbstractDeclarator>),
235    Array { declarator: Option<Box<DirectAbstractDeclarator>>, assignment_expression: Option<Box<Expression>> },
236    Function { declarator: Option<Box<DirectAbstractDeclarator>>, parameter_type_list: Option<ParameterTypeList> },
237}
238
239/// Initializer
240#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
241pub enum Initializer {
242    AssignmentExpression(Expression),
243    InitializerList(Vec<Initializer>),
244}
245
246/// Statement
247#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
248pub enum Statement {
249    /// Labeled statement
250    Labeled(LabeledStatement),
251    /// Compound statement
252    Compound(CompoundStatement),
253    /// Expression statement
254    Expression(ExpressionStatement),
255    /// Selection statement
256    Selection(SelectionStatement),
257    /// Iteration statement
258    Iteration(IterationStatement),
259    /// Jump statement
260    Jump(JumpStatement),
261}
262
263/// Labeled statement
264#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
265pub enum LabeledStatement {
266    Label { identifier: String, statement: Box<Statement> },
267    Case { constant_expression: Expression, statement: Box<Statement> },
268    Default { statement: Box<Statement> },
269}
270
271/// Compound statement
272#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
273pub struct CompoundStatement {
274    pub block_items: Vec<BlockItem>,
275    #[serde(with = "oak_core::serde_range")]
276    pub span: SourceSpan,
277}
278
279/// Block item
280#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
281pub enum BlockItem {
282    Declaration(Declaration),
283    Statement(Statement),
284}
285
286/// Expression statement
287#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
288pub struct ExpressionStatement {
289    pub expression: Option<Expression>,
290    #[serde(with = "oak_core::serde_range")]
291    pub span: SourceSpan,
292}
293
294/// Selection statement
295#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
296pub enum SelectionStatement {
297    If { condition: Expression, then_branch: Box<Statement>, else_branch: Option<Box<Statement>> },
298    Switch { condition: Expression, statement: Box<Statement> },
299}
300
301/// Iteration statement
302#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
303pub enum IterationStatement {
304    While { condition: Expression, statement: Box<Statement> },
305    DoWhile { statement: Box<Statement>, condition: Expression },
306    For { initializer: Option<Box<ForInitializer>>, condition: Option<Expression>, increment: Option<Expression>, statement: Box<Statement> },
307}
308
309/// For initializer
310#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
311pub enum ForInitializer {
312    Expression(Expression),
313    Declaration(Declaration),
314}
315
316/// Jump statement
317#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
318pub enum JumpStatement {
319    Goto(String),
320    Continue,
321    Break,
322    Return(Option<Expression>),
323}
324
325/// Expression
326#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
327pub struct Expression {
328    pub kind: ExpressionKind,
329    #[serde(with = "oak_core::serde_range")]
330    pub span: SourceSpan,
331}
332
333/// Expression kind
334#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
335pub enum ExpressionKind {
336    /// Identifier
337    Identifier(String),
338    /// Constant
339    Constant(String),
340    /// String literal
341    StringLiteral(String),
342    /// Parenthesized expression
343    Parenthesized(Box<Expression>),
344    /// Array access
345    ArrayAccess { array: Box<Expression>, index: Box<Expression> },
346    /// Function call
347    FunctionCall { function: Box<Expression>, arguments: Vec<Expression> },
348    /// Member access
349    MemberAccess { object: Box<Expression>, member: String, is_pointer: bool },
350    /// Unary operation
351    Unary { operator: UnaryOperator, operand: Box<Expression> },
352    /// Binary operation
353    Binary { left: Box<Expression>, operator: BinaryOperator, right: Box<Expression> },
354    /// Conditional expression
355    Conditional { condition: Box<Expression>, then_branch: Box<Expression>, else_branch: Box<Expression> },
356    /// Assignment
357    Assignment { left: Box<Expression>, operator: AssignmentOperator, right: Box<Expression> },
358    /// Comma expression
359    Comma(Vec<Expression>),
360}
361
362/// Unary operator
363#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
364pub enum UnaryOperator {
365    PostIncrement,
366    PostDecrement,
367    PreIncrement,
368    PreDecrement,
369    AddressOf,
370    Deref,
371    Plus,
372    Minus,
373    BitNot,
374    LogicalNot,
375    Sizeof,
376}
377
378/// Binary operator
379#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
380pub enum BinaryOperator {
381    Multiply,
382    Divide,
383    Remainder,
384    Add,
385    Subtract,
386    ShiftLeft,
387    ShiftRight,
388    Less,
389    Greater,
390    LessEqual,
391    GreaterEqual,
392    Equal,
393    NotEqual,
394    BitAnd,
395    BitXor,
396    BitOr,
397    LogicalAnd,
398    LogicalOr,
399}
400
401/// Assignment operator
402#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
403pub enum AssignmentOperator {
404    Assign,
405    MulAssign,
406    DivAssign,
407    RemAssign,
408    AddAssign,
409    SubAssign,
410    ShlAssign,
411    ShrAssign,
412    AndAssign,
413    XorAssign,
414    OrAssign,
415}
416
417/// Type name
418#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
419pub struct TypeName {
420    pub specifier_qualifiers: Vec<SpecifierQualifier>,
421    pub abstract_declarator: Option<Box<AbstractDeclarator>>,
422    #[serde(with = "oak_core::serde_range")]
423    pub span: Range<usize>,
424}
425
426impl TypeName {
427    /// Create a new type name
428    pub fn new(specifier_qualifiers: Vec<SpecifierQualifier>, abstract_declarator: Option<Box<AbstractDeclarator>>, span: Range<usize>) -> Self {
429        Self { specifier_qualifiers, abstract_declarator, span }
430    }
431}
432
433impl CppRoot {
434    /// Create a new AST
435    pub fn new(translation_unit: TranslationUnit) -> Self {
436        Self { translation_unit }
437    }
438}
439
440impl TranslationUnit {
441    /// Create a new translation unit
442    pub fn new(external_declarations: Vec<ExternalDeclaration>, span: Range<usize>) -> Self {
443        Self { external_declarations, span }
444    }
445}