1use core::range::Range;
2use serde::{Deserialize, Serialize};
3
4type SourceSpan = Range<usize>;
6
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9pub struct CppRoot {
10 pub translation_unit: TranslationUnit,
11}
12
13#[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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
23pub enum ExternalDeclaration {
24 FunctionDefinition(FunctionDefinition),
26 Declaration(Declaration),
28}
29
30#[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#[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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
51pub enum DeclarationSpecifier {
52 StorageClassSpecifier(StorageClassSpecifier),
54 TypeSpecifier(TypeSpecifier),
56 TypeQualifier(TypeQualifier),
58 FunctionSpecifier(FunctionSpecifier),
60}
61
62#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
64pub enum StorageClassSpecifier {
65 Typedef,
66 Extern,
67 Static,
68 Auto,
69 Register,
70}
71
72#[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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
94pub enum TypeQualifier {
95 Const,
96 Restrict,
97 Volatile,
98}
99
100#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
102pub enum FunctionSpecifier {
103 Inline,
104 Noreturn,
105}
106
107#[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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
119pub enum StructOrUnion {
120 Struct,
121 Union,
122}
123
124#[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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
135pub enum SpecifierQualifier {
136 TypeSpecifier(TypeSpecifier),
137 TypeQualifier(TypeQualifier),
138}
139
140#[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#[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#[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#[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#[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#[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#[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#[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#[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#[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#[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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
241pub enum Initializer {
242 AssignmentExpression(Expression),
243 InitializerList(Vec<Initializer>),
244}
245
246#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
248pub enum Statement {
249 Labeled(LabeledStatement),
251 Compound(CompoundStatement),
253 Expression(ExpressionStatement),
255 Selection(SelectionStatement),
257 Iteration(IterationStatement),
259 Jump(JumpStatement),
261}
262
263#[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#[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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
281pub enum BlockItem {
282 Declaration(Declaration),
283 Statement(Statement),
284}
285
286#[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#[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#[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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
311pub enum ForInitializer {
312 Expression(Expression),
313 Declaration(Declaration),
314}
315
316#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
318pub enum JumpStatement {
319 Goto(String),
320 Continue,
321 Break,
322 Return(Option<Expression>),
323}
324
325#[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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
335pub enum ExpressionKind {
336 Identifier(String),
338 Constant(String),
340 StringLiteral(String),
342 Parenthesized(Box<Expression>),
344 ArrayAccess { array: Box<Expression>, index: Box<Expression> },
346 FunctionCall { function: Box<Expression>, arguments: Vec<Expression> },
348 MemberAccess { object: Box<Expression>, member: String, is_pointer: bool },
350 Unary { operator: UnaryOperator, operand: Box<Expression> },
352 Binary { left: Box<Expression>, operator: BinaryOperator, right: Box<Expression> },
354 Conditional { condition: Box<Expression>, then_branch: Box<Expression>, else_branch: Box<Expression> },
356 Assignment { left: Box<Expression>, operator: AssignmentOperator, right: Box<Expression> },
358 Comma(Vec<Expression>),
360}
361
362#[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#[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#[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#[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 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 pub fn new(translation_unit: TranslationUnit) -> Self {
436 Self { translation_unit }
437 }
438}
439
440impl TranslationUnit {
441 pub fn new(external_declarations: Vec<ExternalDeclaration>, span: Range<usize>) -> Self {
443 Self { external_declarations, span }
444 }
445}