Architecture
Sun compiles source through a shared analysis pipeline, then uses LLVM to run the program directly or produce a native executable.
Compilation flow
Stage boundaries
- Loading and parsing assemble source and imported declarations into one complete abstract syntax tree (AST).
- Semantic analysis resolves names and types, checks expressions, and prepares concrete generic specializations. Its outputs are an annotated AST and shared declaration and type records.
- Borrow checking validates ownership and lifetime rules before code is emitted.
- Code generation uses those checked results to emit LLVM IR. LLVM optimization and linking produce the executable form.
These boundaries keep file loading out of semantic analysis and name resolution out of code generation. JIT and ahead-of-time compilation share the same frontend and analysis.
Moon libraries
A moon bundle contains compiled code and metadata describing its declarations. Importing a bundle makes those declarations available to analysis; generic templates also retain the syntax needed for new specializations. Stable declaration identities connect references across independently built bundles.
See Semantic Analysis for the analysis flow and shared data, and Code Generation for how those results become executable code.
Implementation layout
The public compiler driver remains declared in include/driver/driver.h.
Its implementation lives in src/driver/pipeline/:
construction.cppcreates drivers and manages JIT exit handlers.inputs.cppresolves, parses, and merges compilation inputs.analysis.cpploads imported declarations and runs semantic analysis.tests.cppcollects tests and synthesizes the test runner and listing.execution.cppruns borrow checking, code generation, optimization, and JIT execution.archives.cppcollects and loads native archives.inspection.cppwrites debug artifacts and LLVM IR views.
The language server starts in src/lsp/main.cpp; its implementation lives in
src/lsp/server/. server.cpp initializes the protocol and dispatches messages,
while protocol.cpp handles framing, URIs, and responses. Configuration,
document updates, cached analysis, semantic tokens, formatting, navigation, and
test requests each have their own implementation file. test_suites.cpp
inventories binaries and checks freshness independently of semantic analysis.
The private server.h declares the interfaces shared by these files. Reusable
language features remain in include/lsp/ and src/lsp/.