Skip to content

5 1 type checking and monomorphization

TransCompilator edited this page Nov 26, 2026 · 1 revision

5.1 Type Checking and Monomorphization

Source: https://deepwiki.com/PonvasanthPon/TransCompilator/5-1-type-checking-and-monomorphization

Type Checking and Monomorphization

Relevant source files

The following files were used as context for generating this wiki page:

Purpose and Scope

Section 5.1 covers the implementation of tc-typeck, the language-agnostic typed middle-end of the TransCompilator (tc_r1) pipeline. Operating between semantic phrase lowering and Core-7 IR verification, tc-typeck is responsible for module graph construction, symbol and import resolution, strict static type checking, overload handling, demand-driven compile-time monomorphization of generics, and lowering into verified Core-7 IR.

Sources: [crates/tc-typeck/src/lib.rs:1-38]()


1. Module Graph Construction and Scope Resolution

The resolution stage (resolve.rs) takes the multi-module high-level AST (Program) and constructs a unified symbol table, verifying visibility constraints (Visibility::Public vs Private), struct/union declarations, and module-level imports.

Core Data Structures

  • ConcreteType: Represents fully resolved types used during type checking and monomorphization, supporting primitive integer and floating-point widths, pointers, fixed-size arrays, nominal structs and unions, function pointers, and generic parameters [crates/tc-typeck/src/resolve.rs:11-42].
  • ResolvedModule: Captures local symbol maps for imports, structs (HashMap<String, usize>), unions, globals, and overloaded functions (HashMap<String, Vec<usize>>) [crates/tc-typeck/src/resolve.rs:185-196].

Diagram: Resolution Flow

graph TD
    Program["Program"] -->|resolve_program| ResolvedProgram["ResolvedProgram"]
    ResolvedProgram --> ResolvedModule["ResolvedModule"]
    ResolvedModule --> StructMap["structs: HashMap<String, usize>"]
    ResolvedModule --> FnMap["functions: HashMap<String, Vec<usize>>"]
    ResolvedModule --> ImportMap["import_map: HashMap<String, usize>"]
Loading

Sources: [crates/tc-typeck/src/lib.rs:33-35](), [crates/tc-typeck/src/resolve.rs:11-196]()


2. Strict Type Checking and Overload Resolution

The checking engine (check.rs) traverses every resolved module, statement, and expression to verify types, enforce lexical scoping, and produce a CheckedProgram containing typed expressions (TExpr) and statements (TStmt).

Key Functions

  • check_program: Validates aggregate declarations, computes global types, validates phrase patterns, and checks every function body against declared parameter and return signatures [crates/tc-typeck/src/check.rs:167-234].
  • CheckedProgram: Stores resolved modules alongside checked functions and globals, providing indexed lookup via checked_fn [crates/tc-typeck/src/check.rs:148-164].

Sources: [crates/tc-typeck/src/check.rs:1-234]()


3. Demand-Driven Monomorphization

Because TransCompilator supports generic structs, unions, and functions written in structured English, templates are instantiated on demand using a worklist algorithm (mono.rs).

Instantiation Rules and Limits

  • MAX_GENERIC_DEPTH: Set to 64 to prevent infinite recursion during recursive generic expansions [crates/tc-typeck/src/mono.rs:10].
  • MAX_INSTANTIATIONS: Capped at 4096 total specialized items per compilation unit [crates/tc-typeck/src/mono.rs:11].
  • FnKey, StructKey, UnionKey: Hashing keys combining defining module index, item index, and concrete type arguments [crates/tc-typeck/src/mono.rs:14-35].

Diagram: Demand-Driven Monomorphization Loop

graph TD
    Entry["EntryFn"] -->|enqueue_fn| MonoCtx["MonoCtx"]
    MonoCtx -->|fn_queue.pop_front| InstFn["instantiate_fn"]
    InstFn -->|discover types/calls| Enqueue["enqueue_fn / StructKey"]
    Enqueue -->|queue not empty| MonoCtx
    MonoCtx -->|queue empty| MonoProgram["MonoProgram"]
Loading

Sources: [crates/tc-typeck/src/mono.rs:1-187]()


4. Lowering to Core-7 IR

Once monomorphization completes, the lower.rs engine translates the typed, concrete AST structures into low-level Core-7 IR primitives (Module, Function, StructDef, UnionDef, GlobalDef).

Lowering Mechanics

  • lower_program: Converts monomorphized structs, unions, and globals, then lowers function bodies using FnLowerer to manage local variable allocation, temporary variables, and memory copies for aggregate types [crates/tc-typeck/src/lower.rs:17-85].
  • verify_module: Invoked immediately after lowering to guarantee that the generated Core-7 IR complies with all semantic and structural rules before codegen or interpretation [crates/tc-typeck/src/lower.rs:68-83].

Sources: [crates/tc-typeck/src/lower.rs:1-85]()


5. Suggestion Machinery and Diagnostics

To maintain deterministic, user-friendly error recovery, suggest.rs provides Levenshtein distance-based typo correction and identifier humanization.

  • closest_match: Normalizes query strings (lowercasing and replacing underscores with spaces) and ranks candidate identifiers within a maximum edit distance [crates/tc-typeck/src/suggest.rs:28-52].
  • humanize: Converts canonical snake_case identifiers into natural space-separated terms for diagnostic output [crates/tc-typeck/src/suggest.rs:56-58].

Sources: [crates/tc-typeck/src/suggest.rs:1-102]()


Clone this wiki locally