formalang 0.0.4-beta

FormaLang compiler frontend: lexer, parser, semantic analyzer, and IR lowering.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! Phase 3: compact the module by dropping the original generic structs,
//! enums, traits, and impls (which were specialised in earlier phases),
//! then remap every surviving id to its new post-compaction position.

use crate::error::CompilerError;
use crate::ir::{EnumId, GenericBase, IrExpr, IrModule, ResolvedType, StructId, TraitId};
use crate::location::Span;

use super::expr_walk::iter_expr_children_mut;
use super::walkers::walk_module_types_mut;

/// True for the prelude-shipped generic carriers (`Array`, `Dictionary`,
/// `Range`). They have non-empty `generic_params` like every other
/// generic, but `mod.rs` keeps them in `module.structs` after compaction
/// because `Generic { base, args }` is their canonical post-pass shape.
/// The remap must keep their slots in lockstep with that retain — naively
/// dropping them would let surviving structs renumber onto prelude
/// positions.
fn is_prelude_struct_name(name: &str) -> bool {
    matches!(name, "Array" | "Dictionary" | "Range")
}

fn is_prelude_enum_name(name: &str) -> bool {
    name == "Optional"
}

/// Build an old-id → new-id remap table for structs. Structs with non-empty
/// `generic_params` become `None` (they will be dropped on compaction);
/// surviving structs (including the prelude-shipped generic carriers, which
/// survive despite carrying `generic_params`) map to their new
/// post-compaction position.
pub(super) fn build_struct_remap(module: &IrModule) -> Vec<Option<StructId>> {
    let mut out = Vec::with_capacity(module.structs.len());
    let mut next: u32 = 0;
    for s in &module.structs {
        if s.generic_params.is_empty() || is_prelude_struct_name(&s.name) {
            out.push(Some(StructId(next)));
            next = next.saturating_add(1);
        } else {
            out.push(None);
        }
    }
    out
}

/// Matching remap for enums. Keeps `Optional` in lockstep with the retain
/// in `mod.rs`, same rationale as [`build_struct_remap`].
pub(super) fn build_enum_remap(module: &IrModule) -> Vec<Option<EnumId>> {
    let mut out = Vec::with_capacity(module.enums.len());
    let mut next: u32 = 0;
    for e in &module.enums {
        if e.generic_params.is_empty() || is_prelude_enum_name(&e.name) {
            out.push(Some(EnumId(next)));
            next = next.saturating_add(1);
        } else {
            out.push(None);
        }
    }
    out
}

/// Phase F: matching remap for traits. Generic traits are dropped
/// post-specialisation (every reference to them was rewritten to
/// the specialised clone in `rewrite_trait_refs`); surviving traits
/// shift down to fill the gaps.
pub(super) fn build_trait_remap(module: &IrModule) -> Vec<Option<TraitId>> {
    let mut out = Vec::with_capacity(module.traits.len());
    let mut next: u32 = 0;
    for t in &module.traits {
        if t.generic_params.is_empty() {
            out.push(Some(TraitId(next)));
            next = next.saturating_add(1);
        } else {
            out.push(None);
        }
    }
    out
}

/// Drop impls whose target is a generic struct or enum that got specialised
/// (and therefore survives in `module.impls` only through its Phase-2b
/// clones). Returns the old-index → new-index mapping for surviving
/// impls so callers can rewrite `DispatchKind::Static { impl_id }`
/// references to match the compacted vector.
pub(super) fn drop_specialised_generic_impls(
    module: &mut IrModule,
    struct_remap: &[Option<StructId>],
    enum_remap: &[Option<EnumId>],
) -> Vec<Option<usize>> {
    let keep: Vec<bool> = module
        .impls
        .iter()
        .map(|imp| match imp.target {
            crate::ir::ImplTarget::Struct(id) => struct_remap
                .get(id.0 as usize)
                .copied()
                .is_none_or(|slot| slot.is_some()),
            crate::ir::ImplTarget::Enum(id) => enum_remap
                .get(id.0 as usize)
                .copied()
                .is_none_or(|slot| slot.is_some()),
            // Primitive impls don't carry an id-remappable target.
            crate::ir::ImplTarget::Primitive(_) => true,
        })
        .collect();
    let mut new_index: Vec<Option<usize>> = Vec::with_capacity(keep.len());
    let mut next: usize = 0;
    for &k in &keep {
        if k {
            new_index.push(Some(next));
            next = next.saturating_add(1);
        } else {
            new_index.push(None);
        }
    }
    let mut idx = 0;
    module.impls.retain(|_| {
        let k = keep.get(idx).copied().unwrap_or(false);
        idx = idx.saturating_add(1);
        k
    });
    new_index
}

/// Rewrite every `DispatchKind::Static { impl_id }` so it points at the
/// compacted impl index. Called after `drop_specialised_generic_impls`.
fn impl_index_rewrite_expr(expr: &mut IrExpr, remap: &[Option<usize>]) {
    use crate::ir::{DispatchKind, ImplId};
    for child in iter_expr_children_mut(expr) {
        impl_index_rewrite_expr(child, remap);
    }
    if let IrExpr::MethodCall {
        dispatch: DispatchKind::Static { impl_id },
        ..
    } = expr
    {
        if let Some(Some(new)) = remap.get(impl_id.0 as usize).copied() {
            *impl_id = ImplId(u32::try_from(new).unwrap_or(u32::MAX));
        }
    }
}

pub(super) fn apply_impl_index_remap(module: &mut IrModule, remap: &[Option<usize>]) {
    let identity = remap
        .iter()
        .enumerate()
        .all(|(i, s)| matches!(s, Some(j) if *j == i));
    if identity {
        return;
    }
    for func in &mut module.functions {
        if let Some(body) = &mut func.body {
            impl_index_rewrite_expr(body, remap);
        }
    }
    for imp in &mut module.impls {
        for func in &mut imp.functions {
            if let Some(body) = &mut func.body {
                impl_index_rewrite_expr(body, remap);
            }
        }
    }
    for s in &mut module.structs {
        for field in &mut s.fields {
            if let Some(default) = &mut field.default {
                impl_index_rewrite_expr(default, remap);
            }
        }
    }
    for l in &mut module.lets {
        impl_index_rewrite_expr(&mut l.value, remap);
    }
}

/// Remap struct/enum IDs across the module after compaction.
///
/// Returns `Err` on out-of-bounds or dropped-slot impl targets — silently
/// no-op'ing them would leave dangling target IDs in the IR.
#[expect(
    clippy::too_many_lines,
    reason = "linear walk over every TraitId-bearing slot in the module"
)]
pub(super) fn apply_remaps(
    module: &mut IrModule,
    struct_remap: &[Option<StructId>],
    enum_remap: &[Option<EnumId>],
    trait_remap: &[Option<TraitId>],
) -> Result<(), Vec<CompilerError>> {
    walk_module_types_mut(module, |ty| {
        remap_type(ty, struct_remap, enum_remap, trait_remap);
    });
    // Phase F: walk every other slot that holds a TraitId outside
    // ResolvedType. Constraints, composed-trait lists, impl-trait
    // refs, and DispatchKind::Virtual all need their TraitIds
    // remapped (or dropped, if a generic-trait id slipped through —
    // by the time we reach apply_remaps, every constraint should
    // already point at a specialised, non-generic id, but we tolerate
    // None defensively).
    let mut errors: Vec<CompilerError> = Vec::new();
    let remap_trait_id_in_place = |id: &mut TraitId, errors: &mut Vec<CompilerError>| {
        match trait_remap.get(id.0 as usize).copied() {
            Some(Some(new)) => *id = new,
            Some(None) => errors.push(CompilerError::InternalError {
                detail: format!(
                    "monomorphise: stale TraitId({}) survived rewrite_trait_refs (generic trait dropped during compaction)",
                    id.0
                ),
                span: Span::default(),
            }),
            None => errors.push(CompilerError::InternalError {
                detail: format!(
                    "monomorphise: TraitId({}) out of bounds for trait remap table (len {})",
                    id.0,
                    trait_remap.len()
                ),
                span: Span::default(),
            }),
        }
    };
    for s in &mut module.structs {
        // Drop traits entries that point at dropped generic traits.
        // The symbol-table-driven `s.traits` index only ever held the
        // unqualified trait id (no args), so a generic-trait impl
        // (`impl Eq<I32> for Foo`) used to register both Eq AND
        // the relevant args at the impl level — but the index slot
        // can't tell them apart and ends up listing the generic id.
        // After rewrite_trait_refs, the impl's trait_ref points at
        // the specialised id; the struct.traits entry for the
        // generic id is stale and gets dropped here.
        s.traits.retain_mut(
            |tr| match trait_remap.get(tr.trait_id.0 as usize).copied() {
                Some(Some(new)) => {
                    tr.trait_id = new;
                    true
                }
                Some(None) | None => false,
            },
        );
        for gp in &mut s.generic_params {
            for c in &mut gp.constraints {
                remap_trait_id_in_place(&mut c.trait_id, &mut errors);
            }
        }
    }
    for t in &mut module.traits {
        for id in &mut t.composed_traits {
            remap_trait_id_in_place(id, &mut errors);
        }
        for gp in &mut t.generic_params {
            for c in &mut gp.constraints {
                remap_trait_id_in_place(&mut c.trait_id, &mut errors);
            }
        }
    }
    for e in &mut module.enums {
        for gp in &mut e.generic_params {
            for c in &mut gp.constraints {
                remap_trait_id_in_place(&mut c.trait_id, &mut errors);
            }
        }
    }
    for f in &mut module.functions {
        for gp in &mut f.generic_params {
            for c in &mut gp.constraints {
                remap_trait_id_in_place(&mut c.trait_id, &mut errors);
            }
        }
    }
    for imp in &mut module.impls {
        match &mut imp.target {
            crate::ir::ImplTarget::Struct(id) => match struct_remap.get(id.0 as usize).copied() {
                Some(Some(new)) => *id = new,
                Some(None) => errors.push(CompilerError::InternalError {
                    detail: format!(
                        "monomorphise: impl block targets struct id {} which was dropped during compaction (drop_specialised_generic_impls missed it)",
                        id.0
                    ),
                    span: Span::default(),
                }),
                None => errors.push(CompilerError::InternalError {
                    detail: format!(
                        "monomorphise: impl block targets struct id {} which is out of bounds for the remap table (len {})",
                        id.0,
                        struct_remap.len()
                    ),
                    span: Span::default(),
                }),
            },
            crate::ir::ImplTarget::Enum(id) => match enum_remap.get(id.0 as usize).copied() {
                Some(Some(new)) => *id = new,
                Some(None) => errors.push(CompilerError::InternalError {
                    detail: format!(
                        "monomorphise: impl block targets enum id {} which was dropped during compaction (drop_specialised_generic_impls missed it)",
                        id.0
                    ),
                    span: Span::default(),
                }),
                None => errors.push(CompilerError::InternalError {
                    detail: format!(
                        "monomorphise: impl block targets enum id {} which is out of bounds for the remap table (len {})",
                        id.0,
                        enum_remap.len()
                    ),
                    span: Span::default(),
                }),
            },
            // Primitive impls have no struct/enum id to remap.
            crate::ir::ImplTarget::Primitive(_) => {}
        }
        if let Some(tr) = &mut imp.trait_ref {
            remap_trait_id_in_place(&mut tr.trait_id, &mut errors);
        }
        for gp in &mut imp.generic_params {
            for c in &mut gp.constraints {
                remap_trait_id_in_place(&mut c.trait_id, &mut errors);
            }
        }
    }
    // DispatchKind::Virtual call sites carry a trait id too. Walk
    // every expression in the module.
    for f in &mut module.functions {
        if let Some(body) = &mut f.body {
            walk_dispatch(body, trait_remap, &mut errors);
        }
    }
    for imp in &mut module.impls {
        for f in &mut imp.functions {
            if let Some(body) = &mut f.body {
                walk_dispatch(body, trait_remap, &mut errors);
            }
        }
    }
    for l in &mut module.lets {
        walk_dispatch(&mut l.value, trait_remap, &mut errors);
    }
    if errors.is_empty() {
        Ok(())
    } else {
        Err(errors)
    }
}

fn walk_dispatch(
    expr: &mut IrExpr,
    trait_remap: &[Option<TraitId>],
    errors: &mut Vec<CompilerError>,
) {
    for child in iter_expr_children_mut(expr) {
        walk_dispatch(child, trait_remap, errors);
    }
    if let IrExpr::MethodCall {
        dispatch: crate::ir::DispatchKind::Virtual { trait_id, .. },
        ..
    } = expr
    {
        match trait_remap.get(trait_id.0 as usize).copied() {
            Some(Some(new)) => *trait_id = new,
            Some(None) => errors.push(CompilerError::InternalError {
                detail: format!(
                    "monomorphise: Virtual dispatch references generic-trait id {} that was dropped",
                    trait_id.0
                ),
                span: Span::default(),
            }),
            None => errors.push(CompilerError::InternalError {
                detail: format!(
                    "monomorphise: Virtual dispatch trait id {} out of bounds for trait remap (len {})",
                    trait_id.0,
                    trait_remap.len()
                ),
                span: Span::default(),
            }),
        }
    }
}

fn remap_type(
    ty: &mut ResolvedType,
    struct_remap: &[Option<StructId>],
    enum_remap: &[Option<EnumId>],
    trait_remap: &[Option<TraitId>],
) {
    match ty {
        ResolvedType::Struct(id) => {
            if let Some(Some(new)) = struct_remap.get(id.0 as usize).copied() {
                *id = new;
            }
        }
        ResolvedType::Enum(id) => {
            if let Some(Some(new)) = enum_remap.get(id.0 as usize).copied() {
                *id = new;
            }
        }
        ResolvedType::Trait(id) => {
            if let Some(Some(new)) = trait_remap.get(id.0 as usize).copied() {
                *id = new;
            }
        }
        ResolvedType::Tuple(fields) => {
            for (_, t) in fields {
                remap_type(t, struct_remap, enum_remap, trait_remap);
            }
        }
        ResolvedType::Closure {
            param_tys,
            return_ty,
        } => {
            for (_, t) in param_tys {
                remap_type(t, struct_remap, enum_remap, trait_remap);
            }
            remap_type(return_ty, struct_remap, enum_remap, trait_remap);
        }
        ResolvedType::Generic { base, args } => {
            // Defensive: by Phase 3 every Generic should have been
            // rewritten to a concrete Struct/Enum/Trait base, but
            // remap just in case a caller is inspecting mid-pass.
            match base {
                GenericBase::Struct(id) => {
                    if let Some(Some(new)) = struct_remap.get(id.0 as usize).copied() {
                        *id = new;
                    }
                }
                GenericBase::Enum(id) => {
                    if let Some(Some(new)) = enum_remap.get(id.0 as usize).copied() {
                        *id = new;
                    }
                }
                GenericBase::Trait(id) => {
                    if let Some(Some(new)) = trait_remap.get(id.0 as usize).copied() {
                        *id = new;
                    }
                }
            }
            for a in args {
                remap_type(a, struct_remap, enum_remap, trait_remap);
            }
        }
        ResolvedType::External { type_args, .. } => {
            for a in type_args {
                remap_type(a, struct_remap, enum_remap, trait_remap);
            }
        }
        ResolvedType::Primitive(_) | ResolvedType::TypeParam(_) | ResolvedType::Error => {}
    }
}