CompilationUnit

Struct CompilationUnit 

Source
pub struct CompilationUnit<'src> { /* private fields */ }
Expand description

A unified compilation unit that owns all artifacts from source to machine code.

The compilation unit progresses through phases:

  1. New: Just source files
  2. Parsed: ASTs and interner from parsing
  3. Lowered: RIR (untyped intermediate representation)
  4. Analyzed: AIR (typed IR) and CFGs for all functions

Each phase builds on the previous one. The unit validates that phases are run in order - you can’t analyze before parsing.

§Thread Safety

The compilation unit uses ThreadedRodeo for string interning, which is thread-safe. Parallel operations (like per-function CFG construction) can safely share the interner.

Implementations§

Source§

impl<'src> CompilationUnit<'src>

Source

pub fn new(sources: Vec<SourceFile<'src>>, options: CompileOptions) -> Self

Create a new compilation unit from source files.

This initializes the unit with source files but does not run any compilation phases. Call parse(), lower(), and analyze() to progress through compilation.

§Arguments
  • sources - Source files to compile
  • options - Compilation options (target, optimization, etc.)
Source

pub fn parse(&mut self) -> MultiErrorResult<()>

Parse all source files.

This runs lexing and parsing on each source file, producing ASTs. The ASTs are then merged into a single program, detecting any duplicate symbol definitions.

§Errors

Returns errors if:

  • Any file fails to lex or parse
  • Duplicate function, struct, or enum definitions are found
Source

pub fn lower(&mut self) -> MultiErrorResult<()>

Generate untyped intermediate representation (RIR).

This transforms the merged AST into RIR, which is a more uniform representation suitable for semantic analysis.

§Panics

Panics if called before parse().

Source

pub fn analyze(&mut self) -> MultiErrorResult<()>

Perform semantic analysis and build control flow graphs.

This runs type checking, symbol resolution, and other semantic checks, then builds CFGs for each function. Optimizations are applied based on the configured optimization level.

§Panics

Panics if called before lower().

Source

pub fn compile(&self) -> MultiErrorResult<CompileOutput>

Generate machine code and link into an executable.

This is the final compilation phase. It generates machine code for all functions and links them into an executable binary.

§Panics

Panics if called before analyze().

Source

pub fn run_frontend(&mut self) -> MultiErrorResult<()>

Run all frontend phases (parse, lower, analyze).

This is a convenience method that runs the complete frontend pipeline. Equivalent to calling parse(), lower(), and analyze() in sequence.

Source

pub fn run_all(&mut self) -> MultiErrorResult<CompileOutput>

Run all phases and produce a compiled binary.

This is a convenience method that runs the complete compilation pipeline. Equivalent to calling run_frontend() followed by compile().

Source

pub fn is_parsed(&self) -> bool

Check if parsing has been completed.

Source

pub fn is_lowered(&self) -> bool

Check if RIR generation has been completed.

Source

pub fn is_analyzed(&self) -> bool

Check if semantic analysis has been completed.

Source

pub fn options(&self) -> &CompileOptions

Get the compilation options.

Source

pub fn ast(&self) -> &Ast

Get the merged AST (after parsing).

§Panics

Panics if called before parse().

Source

pub fn interner(&self) -> &ThreadedRodeo

Get the string interner.

§Panics

Panics if called before parse().

Source

pub fn rir(&self) -> &Rir

Get the RIR (after lowering).

§Panics

Panics if called before lower().

Source

pub fn functions(&self) -> &[FunctionWithCfg]

Get the analyzed functions with CFGs (after analysis).

§Panics

Panics if called before analyze().

Source

pub fn type_pool(&self) -> &TypeInternPool

Get the type pool (after analysis).

§Panics

Panics if called before analyze().

Source

pub fn strings(&self) -> &[String]

Get string literals (after analysis).

§Panics

Panics if called before analyze().

Source

pub fn warnings(&self) -> &[CompileWarning]

Get all warnings collected during compilation.

Source

pub fn file_paths(&self) -> &HashMap<FileId, String>

Get the file paths map.

Source

pub fn take_interner(&mut self) -> ThreadedRodeo

Take the interner out of the compilation unit.

This is useful when you need ownership of the interner (e.g., for code generation).

§Panics

Panics if called before parse() or if the interner has already been taken.

Source

pub fn take_functions(&mut self) -> Vec<FunctionWithCfg>

Take the functions out of the compilation unit.

§Panics

Panics if called before analyze() or if the functions have already been taken.

Source

pub fn take_type_pool(&mut self) -> TypeInternPool

Take the type pool out of the compilation unit.

§Panics

Panics if called before analyze() or if the type pool has already been taken.

Source

pub fn take_strings(&mut self) -> Vec<String>

Take the strings out of the compilation unit.

§Panics

Panics if called before analyze() or if the strings have already been taken.

Source

pub fn take_warnings(&mut self) -> Vec<CompileWarning>

Take the warnings out of the compilation unit.

Trait Implementations§

Source§

impl<'src> Debug for CompilationUnit<'src>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'src> !Freeze for CompilationUnit<'src>

§

impl<'src> !RefUnwindSafe for CompilationUnit<'src>

§

impl<'src> Send for CompilationUnit<'src>

§

impl<'src> Sync for CompilationUnit<'src>

§

impl<'src> Unpin for CompilationUnit<'src>

§

impl<'src> UnwindSafe for CompilationUnit<'src>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<'src, T> IntoMaybe<'src, T> for T
where T: 'src,

§

type Proj<U: 'src> = U

§

fn map_maybe<R>( self, _f: impl FnOnce(&'src T) -> &'src R, g: impl FnOnce(T) -> R, ) -> <T as IntoMaybe<'src, T>>::Proj<R>
where R: 'src,

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more