Skip to main content

oak_c/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2/// C language abstract syntax.
3#[derive(Debug, Clone, PartialEq)]
4pub struct CRoot {
5    /// The translation unit containing the source code structure.
6    pub translation_unit: TranslationUnit,
7    /// The source span of the root node.
8    pub span: core::range::Range<usize>,
9}
10
11/// Translation unit (the top-level structure of a C program).
12#[derive(Debug, Clone, PartialEq)]
13pub struct TranslationUnit {
14    /// List of external declarations.
15    pub external_declarations: Vec<ExternalDeclaration>,
16    /// The source span of the translation unit.
17    pub span: core::range::Range<usize>,
18}
19
20/// External declaration.
21#[derive(Debug, Clone, PartialEq)]
22pub enum ExternalDeclaration {
23    /// Function definition.
24    FunctionDefinition(FunctionDefinition),
25    /// Declaration.
26    Declaration(Declaration),
27}
28
29impl ExternalDeclaration {
30    /// Returns the source span of the external declaration.
31    pub fn span(&self) -> core::range::Range<usize> {
32        match self {
33            Self::FunctionDefinition(n) => n.span.clone(),
34            Self::Declaration(n) => n.span.clone(),
35        }
36    }
37}
38
39/// Function definition.
40#[derive(Debug, Clone, PartialEq)]
41pub struct FunctionDefinition {
42    /// Declaration specifiers (e.g., return type, storage class).
43    pub declaration_specifiers: Vec<DeclarationSpecifier>,
44    /// The declarator for the function.
45    pub declarator: Declarator,
46    /// The body of the function.
47    pub compound_statement: CompoundStatement,
48    /// The source span of the function definition.
49    pub span: core::range::Range<usize>,
50}
51
52/// Declaration.
53#[derive(Debug, Clone, PartialEq)]
54pub struct Declaration {
55    /// Declaration specifiers.
56    pub declaration_specifiers: Vec<DeclarationSpecifier>,
57    /// List of declarators being initialized.
58    pub init_declarators: Vec<InitDeclarator>,
59    /// The source span of the declaration.
60    pub span: core::range::Range<usize>,
61}
62
63/// Declaration specifier.
64#[derive(Debug, Clone, PartialEq)]
65pub enum DeclarationSpecifier {
66    /// Storage class specifier (e.g., `static`, `extern`).
67    StorageClassSpecifier(StorageClassSpecifier),
68    /// Type specifier (e.g., `int`, `char`).
69    TypeSpecifier(TypeSpecifier),
70    /// Type qualifier (e.g., `const`, `volatile`).
71    TypeQualifier(TypeQualifier),
72    /// Function specifier (e.g., `inline`).
73    FunctionSpecifier(FunctionSpecifier),
74}
75
76impl DeclarationSpecifier {
77    /// Returns the source span of the declaration specifier.
78    pub fn span(&self) -> core::range::Range<usize> {
79        match self {
80            Self::StorageClassSpecifier(n) => n.span(),
81            Self::TypeSpecifier(n) => n.span(),
82            Self::TypeQualifier(n) => n.span(),
83            Self::FunctionSpecifier(n) => n.span(),
84        }
85    }
86}
87
88/// Storage class specifier.
89#[derive(Debug, Clone, PartialEq)]
90pub enum StorageClassSpecifier {
91    /// `typedef`
92    Typedef {
93        /// Source span.
94        span: core::range::Range<usize>,
95    },
96    /// `extern`
97    Extern {
98        /// Source span.
99        span: core::range::Range<usize>,
100    },
101    /// `static`
102    Static {
103        /// Source span.
104        span: core::range::Range<usize>,
105    },
106    /// `auto`
107    Auto {
108        /// Source span.
109        span: core::range::Range<usize>,
110    },
111    /// `register`
112    Register {
113        /// Source span.
114        span: core::range::Range<usize>,
115    },
116}
117
118impl StorageClassSpecifier {
119    /// Returns the source span of the storage class specifier.
120    pub fn span(&self) -> core::range::Range<usize> {
121        match self {
122            Self::Typedef { span } => span.clone(),
123            Self::Extern { span } => span.clone(),
124            Self::Static { span } => span.clone(),
125            Self::Auto { span } => span.clone(),
126            Self::Register { span } => span.clone(),
127        }
128    }
129}
130
131/// Type specifier.
132#[derive(Debug, Clone, PartialEq)]
133pub enum TypeSpecifier {
134    /// `void`
135    Void {
136        /// Source span.
137        span: core::range::Range<usize>,
138    },
139    /// `char`
140    Char {
141        /// Source span.
142        span: core::range::Range<usize>,
143    },
144    Short {
145        span: core::range::Range<usize>,
146    },
147    Int {
148        span: core::range::Range<usize>,
149    },
150    Long {
151        span: core::range::Range<usize>,
152    },
153    Float {
154        span: core::range::Range<usize>,
155    },
156    Double {
157        span: core::range::Range<usize>,
158    },
159    Signed {
160        span: core::range::Range<usize>,
161    },
162    Unsigned {
163        span: core::range::Range<usize>,
164    },
165    Bool {
166        span: core::range::Range<usize>,
167    },
168    Complex {
169        span: core::range::Range<usize>,
170    },
171    Imaginary {
172        span: core::range::Range<usize>,
173    },
174    StructOrUnion(StructOrUnionSpecifier),
175    Enum(EnumSpecifier),
176    TypedefName(String, core::range::Range<usize>),
177}
178
179impl TypeSpecifier {
180    /// Returns the source span of the type specifier.
181    pub fn span(&self) -> core::range::Range<usize> {
182        match self {
183            Self::Void { span } => span.clone(),
184            Self::Char { span } => span.clone(),
185            Self::Short { span } => span.clone(),
186            Self::Int { span } => span.clone(),
187            Self::Long { span } => span.clone(),
188            Self::Float { span } => span.clone(),
189            Self::Double { span } => span.clone(),
190            Self::Signed { span } => span.clone(),
191            Self::Unsigned { span } => span.clone(),
192            Self::Bool { span } => span.clone(),
193            Self::Complex { span } => span.clone(),
194            Self::Imaginary { span } => span.clone(),
195            Self::StructOrUnion(n) => n.span.clone(),
196            Self::Enum(n) => n.span.clone(),
197            Self::TypedefName(_, span) => span.clone(),
198        }
199    }
200}
201
202/// Type qualifier.
203#[derive(Debug, Clone, PartialEq)]
204pub enum TypeQualifier {
205    /// `const`
206    Const {
207        /// Source span.
208        span: core::range::Range<usize>,
209    },
210    /// `restrict`
211    Restrict {
212        /// Source span.
213        span: core::range::Range<usize>,
214    },
215    /// `volatile`
216    Volatile {
217        /// Source span.
218        span: core::range::Range<usize>,
219    },
220}
221
222impl TypeQualifier {
223    /// Returns the source span of the type qualifier.
224    pub fn span(&self) -> core::range::Range<usize> {
225        match self {
226            Self::Const { span } => span.clone(),
227            Self::Restrict { span } => span.clone(),
228            Self::Volatile { span } => span.clone(),
229        }
230    }
231}
232
233/// Function specifier.
234#[derive(Debug, Clone, PartialEq)]
235pub enum FunctionSpecifier {
236    /// `inline`
237    Inline {
238        /// Source span.
239        span: core::range::Range<usize>,
240    },
241}
242
243impl FunctionSpecifier {
244    /// Returns the source span of the function specifier.
245    pub fn span(&self) -> core::range::Range<usize> {
246        match self {
247            Self::Inline { span } => span.clone(),
248        }
249    }
250}
251
252/// Struct or union specifier.
253#[derive(Debug, Clone, PartialEq)]
254pub struct StructOrUnionSpecifier {
255    /// Whether it's a struct or union.
256    pub kind: StructOrUnion,
257    /// Optional tag identifier.
258    pub identifier: Option<String>,
259    /// List of struct declarations.
260    pub struct_declarations: Vec<StructDeclaration>,
261    /// Source span.
262    pub span: core::range::Range<usize>,
263}
264
265/// Struct or union keyword.
266#[derive(Debug, Clone, PartialEq)]
267pub enum StructOrUnion {
268    /// `struct`
269    Struct {
270        /// Source span.
271        span: core::range::Range<usize>,
272    },
273    /// `union`
274    Union {
275        /// Source span.
276        span: core::range::Range<usize>,
277    },
278}
279
280impl StructOrUnion {
281    pub fn span(&self) -> core::range::Range<usize> {
282        match self {
283            Self::Struct { span } => span.clone(),
284            Self::Union { span } => span.clone(),
285        }
286    }
287}
288
289/// Struct declaration.
290#[derive(Debug, Clone, PartialEq)]
291pub struct StructDeclaration {
292    /// List of specifiers and qualifiers.
293    pub specifier_qualifier_list: Vec<SpecifierQualifier>,
294    /// List of struct declarators.
295    pub struct_declarator_list: Vec<StructDeclarator>,
296    /// Source span.
297    pub span: core::range::Range<usize>,
298}
299
300/// Specifier or qualifier.
301#[derive(Debug, Clone, PartialEq)]
302pub enum SpecifierQualifier {
303    /// Type specifier.
304    TypeSpecifier(TypeSpecifier),
305    /// Type qualifier.
306    TypeQualifier(TypeQualifier),
307}
308
309impl SpecifierQualifier {
310    pub fn span(&self) -> core::range::Range<usize> {
311        match self {
312            Self::TypeSpecifier(n) => n.span(),
313            Self::TypeQualifier(n) => n.span(),
314        }
315    }
316}
317
318/// Struct declarator.
319#[derive(Debug, Clone, PartialEq)]
320pub struct StructDeclarator {
321    /// The declarator.
322    pub declarator: Option<Declarator>,
323    /// Optional bit-field width expression.
324    pub constant_expression: Option<Expression>,
325    /// Source span.
326    pub span: core::range::Range<usize>,
327}
328
329/// Enum specifier.
330#[derive(Debug, Clone, PartialEq)]
331pub struct EnumSpecifier {
332    /// Optional tag identifier.
333    pub identifier: Option<String>,
334    /// List of enumerators.
335    pub enumerators: Vec<Enumerator>,
336    /// Source span.
337    pub span: core::range::Range<usize>,
338}
339
340/// Enumerator.
341#[derive(Debug, Clone, PartialEq)]
342pub struct Enumerator {
343    /// Enumerator identifier.
344    pub identifier: String,
345    /// Optional constant expression value.
346    pub constant_expression: Option<Expression>,
347    /// Source span.
348    pub span: core::range::Range<usize>,
349}
350
351/// Init declarator.
352#[derive(Debug, Clone, PartialEq)]
353pub struct InitDeclarator {
354    /// The declarator.
355    pub declarator: Declarator,
356    /// Optional initializer.
357    pub initializer: Option<Initializer>,
358    /// Source span.
359    pub span: core::range::Range<usize>,
360}
361
362/// Declarator.
363#[derive(Debug, Clone, PartialEq)]
364pub struct Declarator {
365    /// Optional pointer prefix.
366    pub pointer: Option<Pointer>,
367    /// Direct declarator.
368    pub direct_declarator: DirectDeclarator,
369    /// Source span.
370    pub span: core::range::Range<usize>,
371}
372
373/// Pointer.
374#[derive(Debug, Clone, PartialEq)]
375pub struct Pointer {
376    /// List of type qualifiers for this pointer level.
377    pub type_qualifiers: Vec<TypeQualifier>,
378    /// Optional nested pointer (for `**`, etc.).
379    pub pointer: Option<Box<Pointer>>,
380    /// Source span.
381    pub span: core::range::Range<usize>,
382}
383
384/// Direct declarator.
385#[derive(Debug, Clone, PartialEq)]
386pub enum DirectDeclarator {
387    /// Identifier.
388    Identifier(String, core::range::Range<usize>),
389    /// Parenthesized declarator.
390    Declarator(Box<Declarator>, core::range::Range<usize>),
391    /// Array declarator.
392    Array {
393        /// The declarator being declared as an array.
394        direct_declarator: Box<DirectDeclarator>,
395        /// Type qualifiers inside `[]`.
396        type_qualifiers: Vec<TypeQualifier>,
397        /// Optional assignment expression for size.
398        assignment_expression: Option<Box<Expression>>,
399        /// Source span.
400        span: core::range::Range<usize>,
401    },
402    /// Function declarator.
403    Function {
404        /// The declarator being declared as a function.
405        direct_declarator: Box<DirectDeclarator>,
406        /// Parameter list.
407        parameter_list: ParameterList,
408        /// Source span.
409        span: core::range::Range<usize>,
410    },
411}
412
413impl DirectDeclarator {
414    /// Returns the source span of the direct declarator.
415    pub fn span(&self) -> core::range::Range<usize> {
416        match self {
417            Self::Identifier(_, span) => span.clone(),
418            Self::Declarator(n, _) => n.span.clone(),
419            Self::Array { span, .. } => span.clone(),
420            Self::Function { span, .. } => span.clone(),
421        }
422    }
423}
424
425/// Parameter list.
426#[derive(Debug, Clone, PartialEq)]
427pub struct ParameterList {
428    /// List of parameter declarations.
429    pub parameter_declarations: Vec<ParameterDeclaration>,
430    /// Whether the function is variadic (ends with `...`).
431    pub variadic: bool,
432    /// Source span.
433    pub span: core::range::Range<usize>,
434}
435
436/// Parameter declaration.
437#[derive(Debug, Clone, PartialEq)]
438pub struct ParameterDeclaration {
439    /// Declaration specifiers.
440    pub declaration_specifiers: Vec<DeclarationSpecifier>,
441    /// Optional declarator.
442    pub declarator: Option<Declarator>,
443    /// Optional abstract declarator.
444    pub abstract_declarator: Option<AbstractDeclarator>,
445    /// Source span.
446    pub span: core::range::Range<usize>,
447}
448
449/// Abstract declarator.
450#[derive(Debug, Clone, PartialEq)]
451pub struct AbstractDeclarator {
452    /// Optional pointer prefix.
453    pub pointer: Option<Pointer>,
454    /// Direct abstract declarator.
455    pub direct_abstract_declarator: Option<Box<DirectAbstractDeclarator>>,
456    /// Source span.
457    pub span: core::range::Range<usize>,
458}
459
460/// Direct abstract declarator.
461#[derive(Debug, Clone, PartialEq)]
462pub enum DirectAbstractDeclarator {
463    /// Parenthesized abstract declarator.
464    AbstractDeclarator(Box<AbstractDeclarator>),
465    /// Array abstract declarator.
466    Array {
467        /// Optional direct abstract declarator.
468        declarator: Option<Box<DirectAbstractDeclarator>>,
469        /// Optional size expression.
470        assignment_expression: Option<Box<Expression>>,
471        /// Source span.
472        span: core::range::Range<usize>,
473    },
474    /// Function abstract declarator.
475    Function {
476        /// Optional direct abstract declarator.
477        declarator: Option<Box<DirectAbstractDeclarator>>,
478        /// Parameter list.
479        parameter_list: Option<ParameterList>,
480        /// Source span.
481        span: core::range::Range<usize>,
482    },
483}
484
485impl DirectAbstractDeclarator {
486    /// Returns the source span of the direct abstract declarator.
487    pub fn span(&self) -> core::range::Range<usize> {
488        match self {
489            Self::AbstractDeclarator(n) => n.span.clone(),
490            Self::Array { span, .. } => span.clone(),
491            Self::Function { span, .. } => span.clone(),
492        }
493    }
494}
495
496/// Initializer.
497#[derive(Debug, Clone, PartialEq)]
498pub enum Initializer {
499    /// Assignment expression.
500    AssignmentExpression(Expression),
501    /// Initializer list `{ ... }`.
502    InitializerList(Vec<Initializer>, core::range::Range<usize>),
503}
504
505impl Initializer {
506    /// Returns the source span of the initializer.
507    pub fn span(&self) -> core::range::Range<usize> {
508        match self {
509            Self::AssignmentExpression(n) => n.span.clone(),
510            Self::InitializerList(_, span) => span.clone(),
511        }
512    }
513}
514
515/// Statement.
516#[derive(Debug, Clone, PartialEq)]
517pub enum Statement {
518    /// Labeled statement.
519    Labeled(LabeledStatement),
520    /// Compound statement.
521    Compound(CompoundStatement),
522    /// Expression statement.
523    Expression(ExpressionStatement),
524    /// Selection statement (if, switch).
525    Selection(SelectionStatement),
526    /// Iteration statement (while, do, for).
527    Iteration(IterationStatement),
528    /// Jump statement (goto, continue, break, return).
529    Jump(JumpStatement),
530}
531
532impl Statement {
533    /// Returns the source span of the statement.
534    pub fn span(&self) -> core::range::Range<usize> {
535        match self {
536            Self::Labeled(n) => n.span(),
537            Self::Compound(n) => n.span.clone(),
538            Self::Expression(n) => n.span.clone(),
539            Self::Selection(n) => n.span(),
540            Self::Iteration(n) => n.span(),
541            Self::Jump(n) => n.span(),
542        }
543    }
544}
545
546/// Labeled statement.
547#[derive(Debug, Clone, PartialEq)]
548pub enum LabeledStatement {
549    /// `identifier: statement`
550    Label {
551        /// Label name.
552        identifier: String,
553        /// Labeled statement.
554        statement: Box<Statement>,
555        /// Source span.
556        span: core::range::Range<usize>,
557    },
558    /// `case constant-expression: statement`
559    Case {
560        /// Case expression.
561        constant_expression: Expression,
562        /// Statement.
563        statement: Box<Statement>,
564        /// Source span.
565        span: core::range::Range<usize>,
566    },
567    /// `default: statement`
568    Default {
569        /// Statement.
570        statement: Box<Statement>,
571        /// Source span.
572        span: core::range::Range<usize>,
573    },
574}
575
576impl LabeledStatement {
577    /// Returns the source span of the labeled statement.
578    pub fn span(&self) -> core::range::Range<usize> {
579        match self {
580            Self::Label { span, .. } => span.clone(),
581            Self::Case { span, .. } => span.clone(),
582            Self::Default { span, .. } => span.clone(),
583        }
584    }
585}
586
587/// Compound statement (block).
588#[derive(Debug, Clone, PartialEq)]
589pub struct CompoundStatement {
590    /// List of block items (declarations or statements).
591    pub block_items: Vec<BlockItem>,
592    /// Source span.
593    pub span: core::range::Range<usize>,
594}
595
596/// Block item.
597#[derive(Debug, Clone, PartialEq)]
598pub enum BlockItem {
599    /// Declaration.
600    Declaration(Declaration),
601    /// Statement.
602    Statement(Statement),
603}
604
605impl BlockItem {
606    /// Returns the source span of the block item.
607    pub fn span(&self) -> core::range::Range<usize> {
608        match self {
609            Self::Declaration(n) => n.span.clone(),
610            Self::Statement(n) => n.span(),
611        }
612    }
613}
614
615/// Expression statement.
616#[derive(Debug, Clone, PartialEq)]
617pub struct ExpressionStatement {
618    /// Optional expression.
619    pub expression: Option<Expression>,
620    /// Source span.
621    pub span: core::range::Range<usize>,
622}
623
624/// Selection statement.
625#[derive(Debug, Clone, PartialEq)]
626pub enum SelectionStatement {
627    /// `if (condition) then_statement else else_statement?`
628    If {
629        /// Condition.
630        condition: Expression,
631        /// Then branch.
632        then_statement: Box<Statement>,
633        /// Optional else branch.
634        else_statement: Option<Box<Statement>>,
635        /// Source span.
636        span: core::range::Range<usize>,
637    },
638    /// `switch (expression) statement`
639    Switch {
640        /// Switch expression.
641        expression: Expression,
642        /// Switch body.
643        statement: Box<Statement>,
644        /// Source span.
645        span: core::range::Range<usize>,
646    },
647}
648
649impl SelectionStatement {
650    /// Returns the source span of the selection statement.
651    pub fn span(&self) -> core::range::Range<usize> {
652        match self {
653            Self::If { span, .. } => span.clone(),
654            Self::Switch { span, .. } => span.clone(),
655        }
656    }
657}
658
659/// Iteration statement.
660#[derive(Debug, Clone, PartialEq)]
661pub enum IterationStatement {
662    /// `while (condition) statement`
663    While {
664        /// Condition.
665        condition: Expression,
666        /// Loop body.
667        statement: Box<Statement>,
668        /// Source span.
669        span: core::range::Range<usize>,
670    },
671    /// `do statement while (condition);`
672    DoWhile {
673        /// Loop body.
674        statement: Box<Statement>,
675        /// Condition.
676        condition: Expression,
677        /// Source span.
678        span: core::range::Range<usize>,
679    },
680    /// `for (init; condition; update) statement`
681    For {
682        /// Optional initializer expression.
683        init: Option<Expression>,
684        /// Optional condition expression.
685        condition: Option<Expression>,
686        /// Optional update expression.
687        update: Option<Expression>,
688        /// Loop body.
689        statement: Box<Statement>,
690        /// Source span.
691        span: core::range::Range<usize>,
692    },
693}
694
695impl IterationStatement {
696    /// Returns the source span of the iteration statement.
697    pub fn span(&self) -> core::range::Range<usize> {
698        match self {
699            Self::While { span, .. } => span.clone(),
700            Self::DoWhile { span, .. } => span.clone(),
701            Self::For { span, .. } => span.clone(),
702        }
703    }
704}
705
706/// Jump statement.
707#[derive(Debug, Clone, PartialEq)]
708pub enum JumpStatement {
709    /// `goto identifier;`
710    Goto(String, core::range::Range<usize>),
711    /// `continue;`
712    Continue(core::range::Range<usize>),
713    /// `break;`
714    Break(core::range::Range<usize>),
715    /// `return expression?;`
716    Return(Option<Expression>, core::range::Range<usize>),
717}
718
719impl JumpStatement {
720    /// Returns the source span of the jump statement.
721    pub fn span(&self) -> core::range::Range<usize> {
722        match self {
723            Self::Goto(_, span) => span.clone(),
724            Self::Continue(span) => span.clone(),
725            Self::Break(span) => span.clone(),
726            Self::Return(_, span) => span.clone(),
727        }
728    }
729}
730
731/// Expression.
732#[derive(Debug, Clone, PartialEq)]
733pub struct Expression {
734    /// Expression kind.
735    pub kind: Box<ExpressionKind>,
736    /// Source span.
737    pub span: core::range::Range<usize>,
738}
739
740/// Expression kind.
741#[derive(Debug, Clone, PartialEq)]
742pub enum ExpressionKind {
743    /// Identifier.
744    Identifier(String, core::range::Range<usize>),
745    /// Constant (literal).
746    Constant(Constant, core::range::Range<usize>),
747    /// String literal.
748    StringLiteral(String, core::range::Range<usize>),
749    /// Array subscript `array[index]`.
750    ArraySubscript {
751        /// The array expression.
752        array: Box<Expression>,
753        /// The index expression.
754        index: Box<Expression>,
755        /// Source span.
756        span: core::range::Range<usize>,
757    },
758    /// Function call `function(arguments)`.
759    FunctionCall {
760        /// The function expression.
761        function: Box<Expression>,
762        /// List of arguments.
763        arguments: Vec<Expression>,
764        /// Source span.
765        span: core::range::Range<usize>,
766    },
767    /// Member access `object.member` or `object->member`.
768    MemberAccess {
769        /// The object expression.
770        object: Box<Expression>,
771        /// Member name.
772        member: String,
773        /// Whether it's a pointer access (`->`).
774        is_pointer: bool,
775        /// Source span.
776        span: core::range::Range<usize>,
777    },
778    /// Postfix increment or decrement (`++`, `--`).
779    PostfixIncDec {
780        /// The operand.
781        operand: Box<Expression>,
782        /// Whether it's an increment.
783        is_increment: bool,
784        /// Source span.
785        span: core::range::Range<usize>,
786    },
787    /// Prefix increment or decrement (`++`, `--`).
788    PrefixIncDec {
789        /// The operand.
790        operand: Box<Expression>,
791        /// Whether it's an increment.
792        is_increment: bool,
793        /// Source span.
794        span: core::range::Range<usize>,
795    },
796    /// Unary operation.
797    Unary {
798        /// Unary operator.
799        operator: UnaryOperator,
800        /// The operand.
801        operand: Box<Expression>,
802        /// Source span.
803        span: core::range::Range<usize>,
804    },
805    /// Type cast `(type_name) expression`.
806    Cast {
807        /// Target type.
808        type_name: Box<TypeName>,
809        /// The expression to cast.
810        expression: Box<Expression>,
811        /// Source span.
812        span: core::range::Range<usize>,
813    },
814    /// Binary operation.
815    Binary {
816        /// Left operand.
817        left: Box<Expression>,
818        /// Binary operator.
819        operator: BinaryOperator,
820        /// Right operand.
821        right: Box<Expression>,
822        /// Source span.
823        span: core::range::Range<usize>,
824    },
825    /// Conditional expression `condition ? then_expr : else_expr`.
826    Conditional {
827        /// Condition.
828        condition: Box<Expression>,
829        /// Then expression.
830        then_expr: Box<Expression>,
831        /// Else expression.
832        else_expr: Box<Expression>,
833        /// Source span.
834        span: core::range::Range<usize>,
835    },
836    /// Assignment expression.
837    Assignment {
838        /// Left side of assignment.
839        left: Box<Expression>,
840        /// Assignment operator.
841        operator: AssignmentOperator,
842        /// Right side of assignment.
843        right: Box<Expression>,
844        /// Source span.
845        span: core::range::Range<usize>,
846    },
847    /// Comma expression `expr1, expr2, ...`.
848    Comma {
849        /// List of expressions.
850        expressions: Vec<Expression>,
851        /// Source span.
852        span: core::range::Range<usize>,
853    },
854}
855
856impl ExpressionKind {
857    /// Returns the source span of the expression kind.
858    pub fn span(&self) -> core::range::Range<usize> {
859        match self {
860            Self::Identifier(_, span) => span.clone(),
861            Self::Constant(_, span) => span.clone(),
862            Self::StringLiteral(_, span) => span.clone(),
863            Self::ArraySubscript { span, .. } => span.clone(),
864            Self::FunctionCall { span, .. } => span.clone(),
865            Self::MemberAccess { span, .. } => span.clone(),
866            Self::PostfixIncDec { span, .. } => span.clone(),
867            Self::PrefixIncDec { span, .. } => span.clone(),
868            Self::Unary { span, .. } => span.clone(),
869            Self::Cast { span, .. } => span.clone(),
870            Self::Binary { span, .. } => span.clone(),
871            Self::Conditional { span, .. } => span.clone(),
872            Self::Assignment { span, .. } => span.clone(),
873            Self::Comma { span, .. } => span.clone(),
874        }
875    }
876}
877
878/// Constant (literal).
879#[derive(Debug, Clone, PartialEq)]
880pub enum Constant {
881    /// Integer constant.
882    Integer(i64, core::range::Range<usize>),
883    /// Floating-point constant.
884    Float(f64, core::range::Range<usize>),
885    /// Character constant.
886    Character(char, core::range::Range<usize>),
887}
888
889impl Constant {
890    /// Returns the source span of the constant.
891    pub fn span(&self) -> core::range::Range<usize> {
892        match self {
893            Self::Integer(_, span) => span.clone(),
894            Self::Float(_, span) => span.clone(),
895            Self::Character(_, span) => span.clone(),
896        }
897    }
898}
899
900/// Unary operator.
901#[derive(Debug, Clone, Copy, PartialEq, Eq)]
902pub enum UnaryOperator {
903    /// `&`
904    AddressOf,
905    /// `*`
906    Indirection,
907    /// `+`
908    Plus,
909    /// `-`
910    Minus,
911    /// `~`
912    BitNot,
913    /// `!`
914    LogicalNot,
915    /// `sizeof`
916    Sizeof,
917}
918
919/// Binary operator.
920#[derive(Debug, Clone, Copy, PartialEq, Eq)]
921pub enum BinaryOperator {
922    /// `*`
923    Multiply,
924    /// `/`
925    Divide,
926    /// `%`
927    Modulo,
928    /// `+`
929    Add,
930    /// `-`
931    Subtract,
932    /// `<<`
933    ShiftLeft,
934    /// `>>`
935    ShiftRight,
936    /// `<`
937    Less,
938    /// `>`
939    Greater,
940    /// `<=`
941    LessEqual,
942    /// `>=`
943    GreaterEqual,
944    /// `==`
945    Equal,
946    /// `!=`
947    NotEqual,
948    /// `&`
949    BitAnd,
950    /// `^`
951    BitXor,
952    /// `|`
953    BitOr,
954    /// `&&`
955    LogicalAnd,
956    /// `||`
957    LogicalOr,
958}
959
960/// Assignment operator.
961#[derive(Debug, Clone, Copy, PartialEq, Eq)]
962pub enum AssignmentOperator {
963    /// `=`
964    Assign,
965    /// `*=`
966    MulAssign,
967    /// `/=`
968    DivAssign,
969    /// `%=`
970    ModAssign,
971    /// `+=`
972    AddAssign,
973    /// `-=`
974    SubAssign,
975    /// `<<=`
976    ShlAssign,
977    /// `>>=`
978    ShrAssign,
979    /// `&=`
980    AndAssign,
981    /// `^=`
982    XorAssign,
983    /// `|=`
984    OrAssign,
985}
986
987/// Type name.
988#[derive(Debug, Clone, PartialEq)]
989pub struct TypeName {
990    /// List of specifiers and qualifiers.
991    pub specifier_qualifiers: Vec<SpecifierQualifier>,
992    /// Optional abstract declarator.
993    pub abstract_declarator: Option<Box<AbstractDeclarator>>,
994    /// Source span.
995    pub span: core::range::Range<usize>,
996}
997
998impl TypeName {
999    /// Returns the source span of the type name.
1000    pub fn span(&self) -> core::range::Range<usize> {
1001        self.span.clone()
1002    }
1003}
1004
1005impl CRoot {
1006    /// 创建新的 AST
1007    pub fn new(translation_unit: TranslationUnit, span: core::range::Range<usize>) -> Self {
1008        Self { translation_unit, span }
1009    }
1010}
1011
1012impl TranslationUnit {
1013    /// 创建新的翻译单元
1014    pub fn new(external_declarations: Vec<ExternalDeclaration>, span: core::range::Range<usize>) -> Self {
1015        Self { external_declarations, span }
1016    }
1017}