# cod-e-codes/ion-lang architecture

> Architecture diagram of the GitHub repository cod-e-codes/ion-lang, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cod-e-codes/ion-lang
- Repository: https://github.com/cod-e-codes/ion-lang

## Overview

#### Purpose and binaries
Ion is a Rust-built systems-language toolchain that enforces move ownership, non-escaping references, and channel-based concurrency, then emits C for GCC/Clang compilation. The compiler executable is rooted at `src/main.rs`; project build orchestration and language-server binaries are `src/bin/ion-build.rs` and `src/bin/ion-lsp.rs`. Shared library wiring is in `src/lib.rs`.

#### Front-end: source to checked program
The compiler pipeline tokenizes Ion, constructs its AST, resolves modules/imports, and type-checks semantic and safety rules:
- Lexer: `src/lexer/mod.rs`
- Parser and AST: `src/parser/mod.rs`, `src/ast/mod.rs`
- Module/import compilation: `src/compiler/mod.rs`
- Type checking: `src/tc/mod.rs`, `src/tc/ownership.rs`, `src/tc/trait_bounds.rs`

The type-checking boundary is architecture-critical: it validates ownership/borrowing, structural `Send`, visibility, generics, and trait bounds before lowering/code generation.

#### Lowering and C backend
Checked programs lower through an intermediate representation and generate readable C, including type definitions, builtins, and destruction/drop behavior:
- IR: `src/ir/mod.rs`
- C generation: `src/cgen/mod.rs`, `src/cgen/types.rs`, `src/cgen/drop.rs`

The generated C is the interoperability boundary: Ion FFI uses C-compatible declarations/raw pointers and requires unsafe contexts. Generated programs compile outside Rust using a platform C toolchain.

#### Build driver and native linking
`ion-build` discovers and reads `ion.toml`, chooses single- or multi-module compilation, invokes the compiler, and calls GCC or Clang to compile/link generated C:
- Build driver: `src/build/driver.rs`
- Manifest and path discovery: `src/build/manifest.rs`, `src/build/paths.rs`
- C toolchain integration: `src/build/c_toolchain.rs`

Artifacts normally go to `target/`. Multi-file projects produce C/header units and native objects before final linking. Material dependencies are Rust 1.96, GCC or Clang, and pthread-compatible native threading; Windows uses MinGW as the primary target.

#### Runtime and standard library
Generated C links the Ion C runtime, which provides runtime-level facilities needed by emitted code, including lifecycle and concurrency support:
- Runtime ABI/source: `runtime/ion_runtime.h`, `runtime/ion_runtime.c`
- Ion standard-library modules: `stdlib/io.ion`, `stdlib/fmt.ion`, `stdlib/fs.ion`, `stdlib/result.ion`
- ABI contract: `docs/ABI.md`

The stdlib is Ion source consumed through module resolution; the runtime is C linked into each executable. OS-level threads underpin Ion `spawn` and channels.

#### Editor protocol integration
The LSP reuses compiler-facing analysis to serve diagnostics and language intelligence over the Language Server Protocol:
- Server: `src/lsp/server.rs`, `src/lsp/mod.rs`
- LSP binary: `src/bin/ion-lsp.rs`
- VS Code/Cursor client: `ion-vscode/src/extension.ts`, `ion-vscode/package.json`

The TypeScript extension launches/configures the Rust LSP, provides syntax configuration, and watches workspace Ion dependencies.

#### Primary execution flows
1. **Direct compile:** `ion-compiler` takes an Ion entry file, runs front-end checking → IR → C generation; users manually compile generated C with the runtime.
2. **Project build:** `ion-build` discovers `ion.toml` → plans sources/output → invokes compiler generation → invokes C compiler/linker with runtime, flags, and platform libraries → writes executable.
3. **IDE analysis:** editor extension starts `ion-lsp` → LSP parses/checks workspace documents and imports → returns diagnostics, symbols, navigation, completion, and hover data.

Language semantics are specified in `ION_SPEC.md`; project-level build behavior and the primary sample manifest are represented by `ion.toml`.

Main entry points: [`src/compiler/mod.rs`](https://github.com/cod-e-codes/ion-lang/blob/main/src/compiler/mod.rs) (Module resolver), [`src/build/c_toolchain.rs`](https://github.com/cod-e-codes/ion-lang/blob/main/src/build/c_toolchain.rs) (GCC / Clang integration), [`src/build/driver.rs`](https://github.com/cod-e-codes/ion-lang/blob/main/src/build/driver.rs) (Build driver), [`src/lsp/server.rs`](https://github.com/cod-e-codes/ion-lang/blob/main/src/lsp/server.rs) (LSP server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_compiler["Compiler pipeline"]
  node_compiler_bin["ion-compiler<br/>Rust executable<br/>[main.rs]"]
  node_lexer["Lexer<br/>front-end stage<br/>[mod.rs]"]
  node_parser_ast["Parser &amp; AST<br/>front-end stage<br/>[mod.rs]"]
  node_module_compiler["Module resolver<br/>compilation stage<br/>[mod.rs]"]
  node_type_checker{{"Type &amp; safety checker<br/>semantic boundary<br/>[mod.rs]"}}
  node_ir["Intermediate representation<br/>IR stage<br/>[mod.rs]"]
  node_c_generator["C generator<br/>backend stage<br/>[mod.rs]"]
end

subgraph group_build["Project build &amp; native output"]
  node_build_bin["ion-build<br/>Rust executable<br/>[ion-build.rs]"]
  node_manifest_paths["Manifest &amp; path discovery<br/>build planning<br/>[manifest.rs]"]
  node_build_driver["Build driver<br/>build orchestration<br/>[driver.rs]"]
  node_c_toolchain{{"GCC / Clang integration<br/>native toolchain<br/>[c_toolchain.rs]"}}
  node_native_artifact["Native executable<br/>build artifact"]
end

subgraph group_language["Runtime &amp; language libraries"]
  node_stdlib["Ion standard library<br/>Ion source modules<br/>[io.ion]"]
  node_runtime["Ion C runtime<br/>runtime ABI<br/>[ion_runtime.c]"]
  node_os_threads["Native threads<br/>platform service"]
end

subgraph group_ide["Editor integration"]
  node_lsp_bin["ion-lsp<br/>Rust executable<br/>[ion-lsp.rs]"]
  node_lsp_server["LSP server<br/>workspace analysis<br/>[server.rs]"]
  node_editor_extension["VS Code/Cursor extension<br/>TypeScript client<br/>[extension.ts]"]
  node_ide_features["Editor diagnostics &amp; navigation<br/>LSP responses"]
end

node_ion_source["Ion entry &amp; modules<br/>source input"]

node_ion_source -->|"direct compile"| node_compiler_bin
node_compiler_bin -->|"tokenizes"| node_lexer
node_lexer -->|"tokens"| node_parser_ast
node_parser_ast -->|"AST"| node_module_compiler
node_module_compiler -->|"resolves imports"| node_stdlib
node_module_compiler -->|"resolved program"| node_type_checker
node_type_checker -->|"checked program"| node_ir
node_ir -->|"lowers"| node_c_generator
node_build_bin -->|"discovers"| node_manifest_paths
node_manifest_paths -->|"project plan"| node_build_driver
node_build_driver -->|"invokes C generation"| node_compiler_bin
node_c_generator -->|"generated C units"| node_c_toolchain
node_build_driver -->|"link plan and flags"| node_c_toolchain
node_c_toolchain -->|"links"| node_runtime
node_c_toolchain -->|"compiles and links"| node_native_artifact
node_runtime -->|"uses"| node_os_threads
node_editor_extension -->|"starts"| node_lsp_bin
node_lsp_bin -->|"hosts"| node_lsp_server
node_lsp_server -.->|"analyzes documents and imports"| node_module_compiler
node_lsp_server -->|"returns"| node_ide_features

click node_compiler_bin "https://github.com/cod-e-codes/ion-lang/blob/main/src/main.rs"
click node_lexer "https://github.com/cod-e-codes/ion-lang/blob/main/src/lexer/mod.rs"
click node_parser_ast "https://github.com/cod-e-codes/ion-lang/blob/main/src/parser/mod.rs"
click node_module_compiler "https://github.com/cod-e-codes/ion-lang/blob/main/src/compiler/mod.rs"
click node_type_checker "https://github.com/cod-e-codes/ion-lang/blob/main/src/tc/mod.rs"
click node_ir "https://github.com/cod-e-codes/ion-lang/blob/main/src/ir/mod.rs"
click node_c_generator "https://github.com/cod-e-codes/ion-lang/blob/main/src/cgen/mod.rs"
click node_stdlib "https://github.com/cod-e-codes/ion-lang/blob/main/stdlib/io.ion"
click node_build_bin "https://github.com/cod-e-codes/ion-lang/blob/main/src/bin/ion-build.rs"
click node_manifest_paths "https://github.com/cod-e-codes/ion-lang/blob/main/src/build/manifest.rs"
click node_build_driver "https://github.com/cod-e-codes/ion-lang/blob/main/src/build/driver.rs"
click node_c_toolchain "https://github.com/cod-e-codes/ion-lang/blob/main/src/build/c_toolchain.rs"
click node_runtime "https://github.com/cod-e-codes/ion-lang/blob/main/runtime/ion_runtime.c"
click node_lsp_bin "https://github.com/cod-e-codes/ion-lang/blob/main/src/bin/ion-lsp.rs"
click node_lsp_server "https://github.com/cod-e-codes/ion-lang/blob/main/src/lsp/server.rs"
click node_editor_extension "https://github.com/cod-e-codes/ion-lang/blob/main/ion-vscode/src/extension.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_compiler_bin,node_lexer,node_parser_ast,node_module_compiler,node_type_checker,node_ir,node_c_generator toneBlue
class node_build_bin,node_manifest_paths,node_build_driver,node_c_toolchain,node_native_artifact toneAmber
class node_stdlib,node_runtime,node_os_threads toneMint
class node_lsp_bin,node_lsp_server,node_editor_extension,node_ide_features toneRose
class node_ion_source toneNeutral
```

## Components

### Compiler pipeline

Rust compiler transforms checked Ion into C.

- [ion-compiler](https://github.com/cod-e-codes/ion-lang/blob/main/src/main.rs): Rust executable · Direct compiler entry point. · `src/main.rs`
- [Lexer](https://github.com/cod-e-codes/ion-lang/blob/main/src/lexer/mod.rs): front-end stage · `src/lexer/mod.rs`
- [Parser & AST](https://github.com/cod-e-codes/ion-lang/blob/main/src/parser/mod.rs): front-end stage · `src/parser/mod.rs`
- [Module resolver](https://github.com/cod-e-codes/ion-lang/blob/main/src/compiler/mod.rs): compilation stage · Resolves imports and compiles modules. · `src/compiler/mod.rs`
- [Type & safety checker](https://github.com/cod-e-codes/ion-lang/blob/main/src/tc/mod.rs): semantic boundary · Enforces ownership, borrowing, Send, visibility, generics, and trait bounds. · `src/tc/mod.rs`
- [Intermediate representation](https://github.com/cod-e-codes/ion-lang/blob/main/src/ir/mod.rs): IR stage · `src/ir/mod.rs`
- [C generator](https://github.com/cod-e-codes/ion-lang/blob/main/src/cgen/mod.rs): backend stage · Emits C types, builtins, and drop behavior. · `src/cgen/mod.rs`

### Project build & native output

Build driver coordinates manifests, compiler output, and native linking.

- [ion-build](https://github.com/cod-e-codes/ion-lang/blob/main/src/bin/ion-build.rs): Rust executable · Project build orchestration entry point. · `src/bin/ion-build.rs`
- [Manifest & path discovery](https://github.com/cod-e-codes/ion-lang/blob/main/src/build/manifest.rs): build planning · Reads ion.toml and selects project sources and outputs. · `src/build/manifest.rs`
- [Build driver](https://github.com/cod-e-codes/ion-lang/blob/main/src/build/driver.rs): build orchestration · Chooses single- or multi-module compilation and native build steps. · `src/build/driver.rs`
- [GCC / Clang integration](https://github.com/cod-e-codes/ion-lang/blob/main/src/build/c_toolchain.rs): native toolchain · Compiles generated C and links platform libraries. · `src/build/c_toolchain.rs`
- **Native executable**: build artifact · Written under target/.

### Runtime & language libraries

Generated programs use the C runtime while source imports consume the Ion stdlib.

- [Ion standard library](https://github.com/cod-e-codes/ion-lang/blob/main/stdlib/io.ion): Ion source modules · Imported through module resolution. · `stdlib/io.ion`
- [Ion C runtime](https://github.com/cod-e-codes/ion-lang/blob/main/runtime/ion_runtime.c): runtime ABI · Lifecycle and concurrency support linked into generated executables. · `runtime/ion_runtime.c`
- **Native threads**: platform service · pthread-compatible threads underpin spawn and channels.

### Editor integration

VS Code/Cursor launches the Rust language server.

- [ion-lsp](https://github.com/cod-e-codes/ion-lang/blob/main/src/bin/ion-lsp.rs): Rust executable · Language Server Protocol binary. · `src/bin/ion-lsp.rs`
- [LSP server](https://github.com/cod-e-codes/ion-lang/blob/main/src/lsp/server.rs): workspace analysis · Reuses compiler-facing parsing and checking for language intelligence. · `src/lsp/server.rs`
- [VS Code/Cursor extension](https://github.com/cod-e-codes/ion-lang/blob/main/ion-vscode/src/extension.ts): TypeScript client · Launches and configures ion-lsp and watches workspace dependencies. · `ion-vscode/src/extension.ts`
- **Editor diagnostics & navigation**: LSP responses · Diagnostics, symbols, navigation, completion, and hover.

### Other components

- **Ion entry & modules**: source input

## Connections

- Ion entry & modules → ion-compiler: direct compile
- ion-compiler → Lexer: tokenizes
- Lexer → Parser & AST: tokens
- Parser & AST → Module resolver: AST
- Module resolver → Ion standard library: resolves imports
- Module resolver → Type & safety checker: resolved program
- Type & safety checker → Intermediate representation: checked program
- Intermediate representation → C generator: lowers
- ion-build → Manifest & path discovery: discovers
- Manifest & path discovery → Build driver: project plan
- Build driver → ion-compiler: invokes C generation
- C generator → GCC / Clang integration: generated C units
- Build driver → GCC / Clang integration: link plan and flags
- GCC / Clang integration → Ion C runtime: links
- GCC / Clang integration → Native executable: compiles and links
- Ion C runtime → Native threads: uses
- VS Code/Cursor extension → ion-lsp: starts
- ion-lsp → LSP server: hosts
- LSP server → Module resolver: analyzes documents and imports
- LSP server → Editor diagnostics & navigation: returns
