Repository navigation
5 1 type checking and monomorphization
Source: https://deepwiki.com/PonvasanthPon/TransCompilator/5-1-type-checking-and-monomorphization
Relevant source files
The following files were used as context for generating this wiki page:
- crates/tc-semantics/src/place.rs
- crates/tc-typeck/Cargo.toml
- crates/tc-typeck/src/ast.rs
- crates/tc-typeck/src/check.rs
- crates/tc-typeck/src/lib.rs
- crates/tc-typeck/src/lower.rs
- crates/tc-typeck/src/mono.rs
- crates/tc-typeck/src/resolve.rs
- crates/tc-typeck/src/suggest.rs
- crates/tc-typeck/tests/phase1_typeck_parity.rs
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]()
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.
-
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].
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>"]
Sources: [crates/tc-typeck/src/lib.rs:33-35](), [crates/tc-typeck/src/resolve.rs:11-196]()
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).
-
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 viachecked_fn[crates/tc-typeck/src/check.rs:148-164].
Sources: [crates/tc-typeck/src/check.rs:1-234]()
Because TransCompilator supports generic structs, unions, and functions written in structured English, templates are instantiated on demand using a worklist algorithm (mono.rs).
-
MAX_GENERIC_DEPTH: Set to64to prevent infinite recursion during recursive generic expansions[crates/tc-typeck/src/mono.rs:10]. -
MAX_INSTANTIATIONS: Capped at4096total 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].
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"]
Sources: [crates/tc-typeck/src/mono.rs:1-187]()
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).
-
lower_program: Converts monomorphized structs, unions, and globals, then lowers function bodies usingFnLowererto 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]()
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]()
The TransCompilator Project