cl_typeck/lib.rs
1//! # The Conlang Type Checker
2//!
3//! As a statically typed language, Conlang requires a robust type checker to enforce correctness.
4//!
5//! This crate is a major work-in-progress.
6//!
7//! # The [Table](table::Table)™
8//! A directed graph of nodes and their dependencies.
9//!
10//! Contains [item definitions](handle) and [type expression](type_expression) information.
11//!
12//! *Every* item is itself a module, and can contain arbitrarily nested items
13//! as part of the item graph
14//!
15//! The table, additionally, has some queues for use in external algorithms,
16//! detailed in the [stage] module.
17//!
18//! # Namespaces
19//! Each item in the graph is given its own namespace, which is further separated into
20//! two distinct parts:
21//! - Children of an item are direct descendents (i.e. their `parent` is a handle to the item)
22//! - Imports of an item are indirect descendents created by `use` or `impl` directives. They are
23//! shadowed by Children with the same name.
24//!
25//! # Order of operations:
26//! For order-of-operations information, see the [stage] module.
27#![warn(clippy::all)]
28
29pub(crate) mod format_utils;
30
31pub mod consteval;
32
33pub mod resolved_ast {
34 use cl_ast::types::*;
35 use cl_structures::span::Span;
36
37 macro_rules! ast_types {
38 ($(#[$meta:meta])* type struct $Name:ident {
39 $($member:ident : $t:ty),* $(,)?
40 }) => {
41 $(#[$meta])*
42 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
43 pub struct $Name;
44 impl ::std::fmt::Display for $Name {
45 fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
46 write!(f, stringify!($Name))
47 }
48 }
49 impl ::cl_ast::ast::AstTypes for $Name {
50 $(type $member = $t;)*
51 }
52 };
53 }
54
55 ast_types! {
56 /// The types of a "resolved" AST
57 type struct ResolvedTypes {
58 Annotation: Span,
59 Literal: Literal,
60 MacroId: Symbol,
61 Symbol: usize,
62 Path: usize,
63 }
64 }
65}
66
67pub mod gather {
68
69 pub struct GatherContext {
70 pub vis: Publicity,
71 pub scope: Time,
72 }
73
74 pub enum Publicity {
75 Private,
76 Public,
77 }
78
79 pub enum Time {
80 /// Exists at compile time
81 Const,
82 /// Exists at run time
83 Static,
84 /// Exists at function scope time
85 Local,
86 }
87}
88
89pub mod table;
90
91pub mod handle;
92
93pub mod entry;
94
95pub mod type_kind;
96
97pub mod type_expression;
98
99pub mod stage {
100 //! Type collection, evaluation, checking, and inference passes.
101 //!
102 //! # Order of operations
103 //! 1. [mod@populate]: Populate the graph with nodes for every named item.
104 //! 2. [mod@categorize]: Categorize the nodes according to textual type information.
105 //! - Creates anonymous types (`fn(T) -> U`, `&T`, `[T]`, etc.) as necessary to fill in the
106 //! type graph
107 //! - Creates a new struct type for every enum struct-variant.
108 //! 3. [mod@implement]: Import members of implementation modules into types.
109 //! 4. [mod@infer]: Infer the types of the AST using HM type inference
110
111 pub use populate::Populator;
112 /// Stage 1: Populate the graph with nodes.
113 pub mod populate;
114
115 /// Stage 2: Categorize the nodes according to textual type information.
116 pub mod categorize;
117 pub use categorize::categorize;
118
119 /// Stage 3: Import members of `impl` blocks into their corresponding types.
120 pub mod implement;
121 pub use implement::implement;
122
123 // TODO: Make type inference stage 5
124 // TODO: Use the type information stored in the [table]
125 pub mod infer;
126}