refactor(downgrade): MaybeThunk

This commit is contained in:
2026-04-12 16:19:29 +08:00
parent e34cfc7add
commit 8ad4cf7c7a
6 changed files with 240 additions and 230 deletions
+49 -8
View File
@@ -2,7 +2,6 @@ use std::hash::Hash;
use std::ops::Deref;
use bumpalo::Bump;
use bumpalo::boxed::Box;
use bumpalo::collections::Vec;
use fix_builtins::{BUILTINS, BuiltinId};
use fix_common::StringId;
@@ -11,6 +10,8 @@ use num_enum::TryFromPrimitive as _;
use rnix::{TextRange, ast};
use string_interner::DefaultStringInterner;
use crate::downgrade::DowngradeContext;
pub mod downgrade;
pub type HashMap<'ir, K, V> = hashbrown::HashMap<K, V, hashbrown::DefaultHashBuilder, &'ir Bump>;
@@ -62,6 +63,46 @@ impl<'ir> Deref for RawIrRef<'ir> {
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub enum MaybeThunk {
Int(i64),
Float(f64),
Bool(bool),
Null,
Str(StringId),
Path(StringId),
Thunk(ThunkId),
Arg { layer: usize },
Builtin(BuiltinId),
Builtins,
ReplBinding(StringId),
ScopedImportBinding(StringId),
WithLookup(StringId),
}
impl MaybeThunk {
fn to_ir<'id, 'ir>(self, ctx: &mut impl DowngradeContext<'id, 'ir>) -> IrRef<'id, 'ir> {
use MaybeThunk::*;
let ir = match self {
Int(x) => Ir::Int(x),
Float(x) => Ir::Float(x),
Bool(x) => Ir::Bool(x),
Null => Ir::Null,
Str(x) => Ir::Str(x),
Path(x) => Ir::Path(ctx.new_expr(Ir::Str(x))),
Thunk(x) => Ir::Thunk(x),
Arg { layer } => Ir::Arg { layer },
Builtin(x) => Ir::Builtin(x),
Builtins => Ir::Builtins,
ReplBinding(x) => Ir::ReplBinding(x),
ScopedImportBinding(x) => Ir::ScopedImportBinding(x),
WithLookup(x) => Ir::WithLookup(x),
};
ctx.new_expr(ir)
}
}
#[repr(C)]
#[derive(Debug)]
pub enum Ir<'ir, Ref> {
@@ -69,15 +110,15 @@ pub enum Ir<'ir, Ref> {
Float(f64),
Bool(bool),
Null,
Str(Box<'ir, String>),
Str(StringId),
Path(Ref),
AttrSet {
stcs: HashMap<'ir, StringId, (Ref, TextRange)>,
dyns: Vec<'ir, (Ref, Ref, TextRange)>,
stcs: HashMap<'ir, StringId, (MaybeThunk, TextRange)>,
dyns: Vec<'ir, (Ref, MaybeThunk, TextRange)>,
},
List {
items: Vec<'ir, Ref>,
items: Vec<'ir, MaybeThunk>,
},
Path(Ref),
ConcatStrings {
parts: Vec<'ir, Ref>,
force_string: bool,
@@ -118,7 +159,7 @@ pub enum Ir<'ir, Ref> {
},
With {
namespace: Ref,
namespace: MaybeThunk,
body: Ref,
thunks: Vec<'ir, (ThunkId, Ref)>,
},
@@ -135,7 +176,7 @@ pub enum Ir<'ir, Ref> {
},
Call {
func: Ref,
arg: Ref,
arg: MaybeThunk,
span: TextRange,
},