1#![doc = include_str!("readme.md")]
2use core::range::Range;
3
4type SourceSpan = Range<usize>;
6
7#[derive(Debug, Clone, PartialEq)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub struct CppRoot {
11 pub translation_unit: TranslationUnit,
13}
14
15#[derive(Debug, Clone, PartialEq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub struct TranslationUnit {
19 pub external_declarations: Vec<ExternalDeclaration>,
21 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
23 pub span: Range<usize>,
24}
25
26#[derive(Debug, Clone, PartialEq)]
28#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
29pub enum ExternalDeclaration {
30 FunctionDefinition(FunctionDefinition),
32 Declaration(Declaration),
34}
35
36#[derive(Debug, Clone, PartialEq)]
38#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
39pub struct FunctionDefinition {
40 pub declaration_specifiers: Vec<DeclarationSpecifier>,
42 pub declarator: Declarator,
44 pub compound_statement: CompoundStatement,
46 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
48 pub span: SourceSpan,
49}
50
51#[derive(Debug, Clone, PartialEq)]
53#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
54pub struct Declaration {
55 pub declaration_specifiers: Vec<DeclarationSpecifier>,
57 pub init_declarators: Vec<InitDeclarator>,
59 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
61 pub span: SourceSpan,
62}
63
64#[derive(Debug, Clone, PartialEq)]
66#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67pub enum DeclarationSpecifier {
68 StorageClassSpecifier(StorageClassSpecifier),
70 TypeSpecifier(TypeSpecifier),
72 TypeQualifier(TypeQualifier),
74 FunctionSpecifier(FunctionSpecifier),
76}
77
78#[derive(Debug, Clone, PartialEq)]
80#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
81pub enum StorageClassSpecifier {
82 Typedef,
84 Extern,
86 Static,
88 Auto,
90 Register,
92}
93
94#[derive(Debug, Clone, PartialEq)]
96#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
97pub enum TypeSpecifier {
98 Void,
100 Char,
102 Short,
104 Int,
106 Long,
108 Float,
110 Double,
112 Signed,
114 Unsigned,
116 Bool,
118 Complex,
120 Imaginary,
122 StructOrUnion(StructOrUnionSpecifier),
124 Enum(EnumSpecifier),
126 TypedefName(String),
128}
129
130#[derive(Debug, Clone, PartialEq)]
132#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
133pub enum TypeQualifier {
134 Const,
136 Restrict,
138 Volatile,
140}
141
142#[derive(Debug, Clone, PartialEq)]
144#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
145pub enum FunctionSpecifier {
146 Inline,
148 Noreturn,
150}
151
152#[derive(Debug, Clone, PartialEq)]
154#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
155pub struct StructOrUnionSpecifier {
156 pub struct_or_union: StructOrUnion,
158 pub identifier: Option<String>,
160 pub struct_declarations: Option<Vec<StructDeclaration>>,
162 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
164 pub span: SourceSpan,
165}
166
167#[derive(Debug, Clone, PartialEq)]
169#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
170pub enum StructOrUnion {
171 Struct,
173 Union,
175}
176
177#[derive(Debug, Clone, PartialEq)]
179#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
180pub struct StructDeclaration {
181 pub specifier_qualifiers: Vec<SpecifierQualifier>,
183 pub struct_declarators: Vec<StructDeclarator>,
185 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
187 pub span: SourceSpan,
188}
189
190#[derive(Debug, Clone, PartialEq)]
192#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
193pub enum SpecifierQualifier {
194 TypeSpecifier(TypeSpecifier),
196 TypeQualifier(TypeQualifier),
198}
199
200#[derive(Debug, Clone, PartialEq)]
202#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
203pub struct StructDeclarator {
204 pub declarator: Option<Declarator>,
206 pub constant_expression: Option<Expression>,
208 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
210 pub span: SourceSpan,
211}
212
213#[derive(Debug, Clone, PartialEq)]
215#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
216pub struct EnumSpecifier {
217 pub identifier: Option<String>,
219 pub enumerators: Option<Vec<Enumerator>>,
221 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
223 pub span: SourceSpan,
224}
225
226#[derive(Debug, Clone, PartialEq)]
228#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
229pub struct Enumerator {
230 pub identifier: String,
232 pub constant_expression: Option<Expression>,
234 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
236 pub span: SourceSpan,
237}
238
239#[derive(Debug, Clone, PartialEq)]
241#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
242pub struct InitDeclarator {
243 pub declarator: Declarator,
245 pub initializer: Option<Initializer>,
247 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
249 pub span: SourceSpan,
250}
251
252#[derive(Debug, Clone, PartialEq)]
254#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
255pub struct Declarator {
256 pub pointer: Option<Pointer>,
258 pub direct_declarator: DirectDeclarator,
260 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
262 pub span: SourceSpan,
263}
264
265#[derive(Debug, Clone, PartialEq)]
267#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
268pub struct Pointer {
269 pub type_qualifiers: Vec<TypeQualifier>,
271 pub pointer: Option<Box<Pointer>>,
273 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
275 pub span: SourceSpan,
276}
277
278#[derive(Debug, Clone, PartialEq)]
280#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
281pub enum DirectDeclarator {
282 Identifier(String),
284 Declarator(Box<Declarator>),
286 Array {
288 declarator: Box<DirectDeclarator>,
290 assignment_expression: Option<Expression>,
292 },
293 Function {
295 declarator: Box<DirectDeclarator>,
297 parameter_type_list: Option<ParameterTypeList>,
299 identifier_list: Option<Vec<String>>,
301 },
302}
303
304#[derive(Debug, Clone, PartialEq)]
306#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
307pub struct ParameterTypeList {
308 pub parameter_list: Vec<ParameterDeclaration>,
310 pub variadic: bool,
312 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
314 pub span: SourceSpan,
315}
316
317#[derive(Debug, Clone, PartialEq)]
319#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
320pub struct ParameterDeclaration {
321 pub declaration_specifiers: Vec<DeclarationSpecifier>,
323 pub declarator: Option<Declarator>,
325 pub abstract_declarator: Option<AbstractDeclarator>,
327 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
329 pub span: SourceSpan,
330}
331
332#[derive(Debug, Clone, PartialEq)]
334#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
335pub struct AbstractDeclarator {
336 pub pointer: Option<Pointer>,
338 pub direct_abstract_declarator: Option<Box<DirectAbstractDeclarator>>,
340 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
342 pub span: Range<usize>,
343}
344
345#[derive(Debug, Clone, PartialEq)]
347#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
348pub enum DirectAbstractDeclarator {
349 AbstractDeclarator(Box<AbstractDeclarator>),
351 Array {
353 declarator: Option<Box<DirectAbstractDeclarator>>,
355 assignment_expression: Option<Box<Expression>>,
357 },
358 Function {
360 declarator: Option<Box<DirectAbstractDeclarator>>,
362 parameter_type_list: Option<ParameterTypeList>,
364 },
365}
366
367#[derive(Debug, Clone, PartialEq)]
369#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
370pub enum Initializer {
371 AssignmentExpression(Expression),
373 InitializerList(Vec<Initializer>),
375}
376
377#[derive(Debug, Clone, PartialEq)]
379#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
380pub enum Statement {
381 Labeled(LabeledStatement),
383 Compound(CompoundStatement),
385 Expression(ExpressionStatement),
387 Selection(SelectionStatement),
389 Iteration(IterationStatement),
391 Jump(JumpStatement),
393}
394
395#[derive(Debug, Clone, PartialEq)]
397#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
398pub enum LabeledStatement {
399 Label {
401 identifier: String,
403 statement: Box<Statement>,
405 },
406 Case {
408 constant_expression: Expression,
410 statement: Box<Statement>,
412 },
413 Default {
415 statement: Box<Statement>,
417 },
418}
419
420#[derive(Debug, Clone, PartialEq)]
422#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
423pub struct CompoundStatement {
424 pub block_items: Vec<BlockItem>,
426 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
428 pub span: SourceSpan,
429}
430
431#[derive(Debug, Clone, PartialEq)]
433#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
434pub enum BlockItem {
435 Declaration(Declaration),
437 Statement(Statement),
439}
440
441#[derive(Debug, Clone, PartialEq)]
443#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
444pub struct ExpressionStatement {
445 pub expression: Option<Expression>,
447 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
449 pub span: SourceSpan,
450}
451
452#[derive(Debug, Clone, PartialEq)]
454#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
455pub enum SelectionStatement {
456 If {
458 condition: Expression,
460 then_branch: Box<Statement>,
462 else_branch: Option<Box<Statement>>,
464 },
465 Switch {
467 condition: Expression,
469 statement: Box<Statement>,
471 },
472}
473
474#[derive(Debug, Clone, PartialEq)]
476#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
477pub enum IterationStatement {
478 While {
480 condition: Expression,
482 statement: Box<Statement>,
484 },
485 DoWhile {
487 statement: Box<Statement>,
489 condition: Expression,
491 },
492 For {
494 initializer: Option<Box<ForInitializer>>,
496 condition: Option<Expression>,
498 increment: Option<Expression>,
500 statement: Box<Statement>,
502 },
503}
504
505#[derive(Debug, Clone, PartialEq)]
507#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
508pub enum ForInitializer {
509 Expression(Expression),
511 Declaration(Declaration),
513}
514
515#[derive(Debug, Clone, PartialEq)]
517#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
518pub enum JumpStatement {
519 Goto(String),
521 Continue,
523 Break,
525 Return(Option<Expression>),
527}
528
529#[derive(Debug, Clone, PartialEq)]
531#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
532pub struct Expression {
533 pub kind: ExpressionKind,
535 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
537 pub span: SourceSpan,
538}
539
540#[derive(Debug, Clone, PartialEq)]
542#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
543pub enum ExpressionKind {
544 Identifier(String),
546 Constant(String),
548 StringLiteral(String),
550 Parenthesized(Box<Expression>),
552 ArrayAccess {
554 array: Box<Expression>,
556 index: Box<Expression>,
558 },
559 FunctionCall {
561 function: Box<Expression>,
563 arguments: Vec<Expression>,
565 },
566 MemberAccess {
568 object: Box<Expression>,
570 member: String,
572 is_pointer: bool,
574 },
575 Unary {
577 operator: UnaryOperator,
579 operand: Box<Expression>,
581 },
582 Binary {
584 left: Box<Expression>,
586 operator: BinaryOperator,
588 right: Box<Expression>,
590 },
591 Conditional {
593 condition: Box<Expression>,
595 then_branch: Box<Expression>,
597 else_branch: Box<Expression>,
599 },
600 Assignment {
602 left: Box<Expression>,
604 operator: AssignmentOperator,
606 right: Box<Expression>,
608 },
609 Comma(Vec<Expression>),
611}
612
613#[derive(Debug, Clone, Copy, PartialEq)]
615#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
616pub enum UnaryOperator {
617 PostIncrement,
619 PostDecrement,
621 PreIncrement,
623 PreDecrement,
625 AddressOf,
627 Deref,
629 Plus,
631 Minus,
633 BitNot,
635 LogicalNot,
637 Sizeof,
639 AlignOf,
641}
642
643#[derive(Debug, Clone, Copy, PartialEq)]
645#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
646pub enum BinaryOperator {
647 Add,
649 Subtract,
651 Multiply,
653 Divide,
655 Remainder,
657 ShiftLeft,
659 ShiftRight,
661 Less,
663 Greater,
665 LessEqual,
667 GreaterEqual,
669 Equal,
671 NotEqual,
673 BitAnd,
675 BitXor,
677 BitOr,
679 LogicalAnd,
681 LogicalOr,
683}
684
685#[derive(Debug, Clone, Copy, PartialEq)]
687#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
688pub enum AssignmentOperator {
689 Assign,
691 AddAssign,
693 SubAssign,
695 MulAssign,
697 DivAssign,
699 RemAssign,
701 ShlAssign,
703 ShrAssign,
705 AndAssign,
707 XorAssign,
709 OrAssign,
711}
712
713#[derive(Debug, Clone, PartialEq)]
715#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
716pub struct TypeName {
717 pub specifier_qualifiers: Vec<SpecifierQualifier>,
719 pub abstract_declarator: Option<Box<AbstractDeclarator>>,
721 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
723 pub span: Range<usize>,
724}
725
726impl TypeName {
727 pub fn new(specifier_qualifiers: Vec<SpecifierQualifier>, abstract_declarator: Option<Box<AbstractDeclarator>>, span: Range<usize>) -> Self {
729 Self { specifier_qualifiers, abstract_declarator, span }
730 }
731}
732
733impl CppRoot {
734 pub fn new(translation_unit: TranslationUnit) -> Self {
736 Self { translation_unit }
737 }
738}
739
740impl TranslationUnit {
741 pub fn new(external_declarations: Vec<ExternalDeclaration>, span: Range<usize>) -> Self {
743 Self { external_declarations, span }
744 }
745}