Module ast

Module ast 

Source
Expand description

Abstract Syntax Tree types for Gruel.

The AST represents the syntactic structure of the source code. It closely mirrors the source syntax and preserves all information needed for error reporting.

§SmallVec Usage

Some non-recursive Vec fields use SmallVec to avoid heap allocation for common small sizes:

  • Directives (SmallVec<[Directive; 1]>) - most items have 0-1 directives

§Vec Usage (Cannot Use SmallVec)

Vec fields containing recursive types (Expr) cannot use SmallVec because Expr’s size cannot be determined at compile time. These include:

  • Vec<CallArg> - CallArg contains Expr
  • Vec<MatchArm> - contains Expr
  • Vec<FieldInit> - contains Box
  • Vec<IntrinsicArg> - contains Expr
  • Vec<Statement> - Statement contains Expr
  • Vec<Expr> - directly recursive

The IR layers (RIR, AIR, CFG) use index-based references which avoid this issue and are already efficiently allocated.

Structs§

AnonStructField
A field in an anonymous struct type expression.
ArrayLitExpr
An array literal expression (e.g., [1, 2, 3]).
AssignStatement
An assignment statement.
AssocFnCallExpr
An associated function call expression (e.g., Point::origin() or module.Point::origin()).
Ast
A complete source file (list of items).
BinaryExpr
A binary expression.
BlockExpr
A block expression containing statements and a final expression.
BoolLit
A boolean literal.
BreakExpr
A break expression (exits the innermost loop).
CallArg
An argument in a function call.
CallExpr
A function call expression.
CheckedBlockExpr
A checked block expression (e.g., checked { @ptr_read(p) }). Unchecked operations (raw pointer manipulation, calling unchecked functions) are only allowed inside checked blocks.
ComptimeBlockExpr
A comptime block expression (e.g., comptime { 1 + 2 }). The expression inside must be evaluable at compile time.
ComptimeUnrollForExpr
A comptime_unroll for expression. The collection is evaluated at compile time, then the body is unrolled once per element.
ConstDecl
A constant declaration.
ContinueExpr
A continue expression (skips to the next iteration of the innermost loop).
DestructureField
A field binding in a struct destructure pattern.
Directive
A directive that modifies compiler behavior for the following item or statement.
DropFn
A user-defined destructor declaration.
EnumDecl
An enum declaration.
EnumStructLitExpr
An enum struct variant literal expression (e.g., Shape::Circle { radius: 5 }).
EnumVariant
A variant in an enum declaration.
EnumVariantField
A named field in a struct-style enum variant.
FieldDecl
A field declaration in a struct.
FieldExpr
A field access expression (e.g., point.x).
FieldInit
A field initializer in a struct literal.
FloatLit
A floating-point literal, stored as f64 bits for Eq compatibility.
ForExpr
A for-in loop expression (e.g., for x in expr { body }).
Function
A function definition.
Ident
An identifier.
IfExpr
An if expression.
IndexExpr
An array index expression (e.g., arr[0]).
IntLit
An integer literal.
IntrinsicCallExpr
An intrinsic call expression (e.g., @dbg(42) or @size_of(i32)).
LetStatement
A let binding statement.
LoopExpr
An infinite loop expression.
MatchArm
A single arm in a match expression.
MatchExpr
A match expression.
Method
A method definition in an impl block.
MethodCallExpr
A method call expression (e.g., point.distance()).
NegIntLit
A negative integer literal pattern.
NodeData
Fixed-size node data (12 bytes total).
NodeIndex
Node index - references a node in the SOA AST.
Param
A function parameter.
ParenExpr
A parenthesized expression.
PathExpr
A path expression (e.g., Color::Red or module.Color::Red for enum variant).
PathPattern
A path pattern (e.g., Color::Red or module.Color::Red for enum variant matching).
PatternFieldBinding
A named field binding in a struct variant pattern.
ReturnExpr
A return expression (returns a value from the current function).
SelfExpr
A self expression (the self keyword in method bodies).
SelfParam
A self parameter in a method.
SoaAst
Struct-of-Arrays AST representation.
StringLit
A string literal.
StructDecl
A struct declaration.
StructLitExpr
A struct literal expression (e.g., Point { x: 1, y: 2 } or module.Point { x: 1, y: 2 }).
TypeLitExpr
A type literal expression (e.g., i32 used as a value). This represents a type used as a value in expression context, typically as an argument to a generic function with comptime parameters.
UnaryExpr
A unary expression.
UnitLit
A unit literal expression - represents () or implicit unit.
WhileExpr
A while loop expression.

Enums§

ArgMode
Argument passing mode.
AssignTarget
An assignment target.
BinaryOp
Binary operators.
DestructureBinding
How a field is bound in a struct destructure.
DirectiveArg
An argument to a directive.
EnumVariantKind
The kind of an enum variant.
Expr
An expression.
IntrinsicArg
An argument to an intrinsic call (can be an expression or a type).
Item
A top-level item in a source file.
LetPattern
A pattern in a let binding.
NodeTag
Node tag - identifies what kind of node this is.
ParamMode
Parameter passing mode.
Pattern
A pattern in a match arm.
PatternBinding
A binding in a data variant pattern.
Statement
A statement (does not produce a value).
TypeExpr
A type expression in the AST.
UnaryOp
Unary operators.
Visibility
Visibility of an item (function, struct, enum, etc.)

Constants§

NULL_NODE
Sentinel value representing “no node” or “null node”. Used for optional children (e.g., else block in if expression).

Functions§

encode_unary_op
Encode a UnaryOp into a u32 for storage in NodeData.

Type Aliases§

Directives
Type alias for a small vector of directives. Most items have 0-1 directives, so we inline capacity for 1.