2024-12-01 07:01:44 -06:00
|
|
|
use {
|
|
|
|
crate::{
|
|
|
|
ctx_map,
|
|
|
|
lexer::TokenKind,
|
2024-12-21 07:21:58 -06:00
|
|
|
parser::{self, CapturedIdent, CommentOr, Expr, ExprRef, Pos},
|
2024-12-01 12:04:27 -06:00
|
|
|
utils::{self, Ent, EntSlice, EntVec},
|
2024-12-01 07:01:44 -06:00
|
|
|
Ident,
|
|
|
|
},
|
|
|
|
alloc::{string::String, vec::Vec},
|
|
|
|
core::{
|
|
|
|
cell::Cell,
|
|
|
|
num::NonZeroU32,
|
|
|
|
ops::{Deref, DerefMut, Range},
|
|
|
|
},
|
|
|
|
hashbrown::hash_map,
|
|
|
|
};
|
2024-12-01 08:11:38 -06:00
|
|
|
|
2024-12-01 07:01:44 -06:00
|
|
|
macro_rules! impl_deref {
|
|
|
|
($for:ty { $name:ident: $base:ty }) => {
|
|
|
|
impl Deref for $for {
|
|
|
|
type Target = $base;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.$name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for $for {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.$name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type ArrayLen = u32;
|
2024-12-01 08:11:38 -06:00
|
|
|
pub type Offset = u32;
|
|
|
|
pub type Size = u32;
|
2024-12-01 07:01:44 -06:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, PartialOrd, Ord)]
|
2024-12-19 16:08:36 -06:00
|
|
|
pub struct List(pub u32);
|
2024-12-01 07:01:44 -06:00
|
|
|
|
2024-12-19 16:08:36 -06:00
|
|
|
impl List {
|
2024-12-01 07:01:44 -06:00
|
|
|
const LEN_BITS: u32 = 5;
|
|
|
|
const LEN_MASK: usize = Self::MAX_LEN - 1;
|
|
|
|
const MAX_LEN: usize = 1 << Self::LEN_BITS;
|
|
|
|
|
|
|
|
pub fn new(pos: usize, len: usize) -> Option<Self> {
|
|
|
|
if len >= Self::MAX_LEN {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Self((pos << Self::LEN_BITS | len) as u32))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn range(self) -> Range<usize> {
|
|
|
|
let start = self.0 as usize >> Self::LEN_BITS;
|
|
|
|
start..start + self.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(self) -> usize {
|
|
|
|
self.0 as usize & Self::LEN_MASK
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
Self(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn args(self) -> ArgIter {
|
|
|
|
ArgIter(self.range())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ArgIter(Range<usize>);
|
|
|
|
|
|
|
|
pub enum Arg {
|
|
|
|
Type(Id),
|
|
|
|
Value(Id),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ArgIter {
|
|
|
|
pub(crate) fn next(&mut self, tys: &Types) -> Option<Arg> {
|
|
|
|
let ty = tys.ins.args[self.0.next()?];
|
|
|
|
if ty == Id::TYPE {
|
|
|
|
return Some(Arg::Type(tys.ins.args[self.0.next().unwrap()]));
|
|
|
|
}
|
|
|
|
Some(Arg::Value(ty))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn next_value(&mut self, tys: &Types) -> Option<Id> {
|
|
|
|
loop {
|
|
|
|
match self.next(tys)? {
|
|
|
|
Arg::Type(_) => continue,
|
|
|
|
Arg::Value(id) => break Some(id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
|
|
|
|
pub struct Id(NonZeroU32);
|
|
|
|
|
2024-12-19 16:08:36 -06:00
|
|
|
impl AsRef<Id> for Id {
|
|
|
|
fn as_ref(&self) -> &Id {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-01 07:01:44 -06:00
|
|
|
impl From<Id> for i64 {
|
|
|
|
fn from(value: Id) -> Self {
|
|
|
|
value.0.get() as _
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl crate::ctx_map::CtxEntry for Id {
|
|
|
|
type Ctx = TypeIns;
|
|
|
|
type Key<'a> = SymKey<'a>;
|
|
|
|
|
|
|
|
fn key<'a>(&self, ctx: &'a Self::Ctx) -> Self::Key<'a> {
|
|
|
|
match self.expand() {
|
|
|
|
Kind::Struct(s) => {
|
|
|
|
let st = &ctx.structs[s];
|
|
|
|
debug_assert_ne!(st.pos, Pos::MAX);
|
2024-12-02 08:51:12 -06:00
|
|
|
SymKey::Type(st.parent, st.pos, st.captured)
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
Kind::Enum(e) => {
|
|
|
|
let en = &ctx.enums[e];
|
|
|
|
debug_assert_ne!(en.pos, Pos::MAX);
|
2024-12-02 08:51:12 -06:00
|
|
|
SymKey::Type(en.parent, en.pos, en.captured)
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
2024-12-01 08:11:38 -06:00
|
|
|
Kind::Union(e) => {
|
|
|
|
let en = &ctx.unions[e];
|
|
|
|
debug_assert_ne!(en.pos, Pos::MAX);
|
2024-12-02 08:51:12 -06:00
|
|
|
SymKey::Type(en.parent, en.pos, en.captured)
|
2024-12-01 08:11:38 -06:00
|
|
|
}
|
2024-12-01 07:01:44 -06:00
|
|
|
Kind::Ptr(p) => SymKey::Pointer(&ctx.ptrs[p]),
|
|
|
|
Kind::Opt(p) => SymKey::Optional(&ctx.opts[p]),
|
|
|
|
Kind::Func(f) => {
|
|
|
|
let fc = &ctx.funcs[f];
|
2024-12-02 08:51:12 -06:00
|
|
|
if fc.is_generic {
|
|
|
|
SymKey::Type(fc.parent, fc.pos, fc.sig.args)
|
2024-12-01 07:01:44 -06:00
|
|
|
} else {
|
|
|
|
SymKey::Decl(fc.parent, fc.name)
|
|
|
|
}
|
|
|
|
}
|
2024-12-02 08:51:12 -06:00
|
|
|
Kind::Template(t) => {
|
|
|
|
let tc = &ctx.templates[t];
|
|
|
|
SymKey::Decl(tc.parent, tc.name)
|
|
|
|
}
|
2024-12-01 07:01:44 -06:00
|
|
|
Kind::Global(g) => {
|
|
|
|
let gb = &ctx.globals[g];
|
|
|
|
SymKey::Decl(gb.file.into(), gb.name)
|
|
|
|
}
|
|
|
|
Kind::Slice(s) => SymKey::Array(&ctx.slices[s]),
|
2024-12-19 16:08:36 -06:00
|
|
|
Kind::Tuple(t) => SymKey::Tuple(ctx.tuples[t].fields),
|
2024-12-01 07:01:44 -06:00
|
|
|
Kind::Module(_) | Kind::Builtin(_) => {
|
|
|
|
SymKey::Decl(Module::default().into(), Ident::INVALID)
|
|
|
|
}
|
|
|
|
Kind::Const(c) => SymKey::Constant(&ctx.consts[c]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Id {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(unsafe { NonZeroU32::new_unchecked(UNDECLARED) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Id {
|
|
|
|
pub const DINT: Self = Self::UINT;
|
|
|
|
|
|
|
|
pub fn bin_ret(self, op: TokenKind) -> Id {
|
|
|
|
if op.is_compatison() {
|
|
|
|
Self::BOOL
|
|
|
|
} else {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_float(self) -> bool {
|
|
|
|
matches!(self.repr(), F32 | F64) || self.is_never()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_signed(self) -> bool {
|
|
|
|
matches!(self.repr(), I8..=INT) || self.is_never()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_unsigned(self) -> bool {
|
2024-12-19 12:43:30 -06:00
|
|
|
matches!(self.repr(), U8..=UINT)
|
|
|
|
|| self.is_never()
|
|
|
|
|| matches!(self.expand(), Kind::Enum(_))
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_integer(self) -> bool {
|
2024-12-19 12:43:30 -06:00
|
|
|
self.is_signed() || self.is_unsigned()
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_never(self) -> bool {
|
|
|
|
self == Self::NEVER
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn strip_pointer(self) -> Self {
|
|
|
|
match self.expand() {
|
|
|
|
Kind::Ptr(_) => Id::UINT,
|
|
|
|
_ => self,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_pointer(self) -> bool {
|
|
|
|
matches!(self.expand(), Kind::Ptr(_)) || self.is_never()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_optional(self) -> bool {
|
|
|
|
matches!(self.expand(), Kind::Opt(_)) || self.is_never()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_upcast(self, ob: Self) -> Option<Self> {
|
|
|
|
self.try_upcast_low(ob, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_upcast_low(self, ob: Self, coerce_pointer: bool) -> Option<Self> {
|
|
|
|
let (oa, ob) = (Self(self.0.min(ob.0)), Self(self.0.max(ob.0)));
|
|
|
|
let (a, b) = (oa.strip_pointer(), ob.strip_pointer());
|
|
|
|
Some(match () {
|
|
|
|
_ if oa == Id::NEVER => ob,
|
|
|
|
_ if ob == Id::NEVER => oa,
|
|
|
|
_ if oa == ob => oa,
|
|
|
|
_ if ob.is_optional() => ob,
|
|
|
|
_ if oa.is_pointer() && ob.is_pointer() => return None,
|
2024-12-16 06:49:20 -06:00
|
|
|
_ if oa == Id::BOOL && ob.is_integer() => ob,
|
2024-12-01 07:01:44 -06:00
|
|
|
_ if a.is_signed() && b.is_signed() || a.is_unsigned() && b.is_unsigned() => ob,
|
|
|
|
_ if a.is_unsigned() && b.is_signed() && a.repr() - U8 < b.repr() - I8 => ob,
|
|
|
|
_ if a.is_unsigned() && b.is_signed() && a.repr() - U8 > b.repr() - I8 => oa,
|
|
|
|
_ if oa.is_integer() && ob.is_pointer() && coerce_pointer => ob,
|
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expand(self) -> Kind {
|
|
|
|
Kind::from_ty(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn repr(self) -> u32 {
|
|
|
|
self.0.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn simple_size(&self) -> Option<Size> {
|
|
|
|
Some(match self.expand() {
|
|
|
|
Kind::Ptr(_) => 8,
|
|
|
|
Kind::Builtin(Builtin(VOID)) => 0,
|
|
|
|
Kind::Builtin(Builtin(NEVER)) => 0,
|
|
|
|
Kind::Builtin(Builtin(INT | UINT | F64)) => 8,
|
|
|
|
Kind::Builtin(Builtin(I32 | U32 | TYPE | F32)) => 4,
|
|
|
|
Kind::Builtin(Builtin(I16 | U16)) => 2,
|
|
|
|
Kind::Builtin(Builtin(I8 | U8 | BOOL)) => 1,
|
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn extend(self) -> Self {
|
|
|
|
if self.is_signed() {
|
|
|
|
Self::INT
|
|
|
|
} else if self.is_pointer() {
|
|
|
|
self
|
|
|
|
} else {
|
|
|
|
Self::UINT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn loc(&self, tys: &Types) -> Loc {
|
2024-12-19 16:08:36 -06:00
|
|
|
use Kind as K;
|
2024-12-01 07:01:44 -06:00
|
|
|
match self.expand() {
|
2024-12-19 16:08:36 -06:00
|
|
|
K::Opt(o)
|
2024-12-01 07:01:44 -06:00
|
|
|
if let ty = tys.ins.opts[o].base
|
|
|
|
&& ty.loc(tys) == Loc::Reg
|
|
|
|
&& (ty.is_pointer() || tys.size_of(ty) < 8) =>
|
|
|
|
{
|
|
|
|
Loc::Reg
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
K::Ptr(_) | K::Enum(_) | K::Builtin(_) => Loc::Reg,
|
|
|
|
K::Struct(_) | K::Tuple(_) | K::Union(_) if tys.size_of(*self) == 0 => Loc::Reg,
|
|
|
|
K::Struct(_) | K::Tuple(_) | K::Union(_) | K::Slice(_) | K::Opt(_) => Loc::Stack,
|
|
|
|
c @ (K::Func(_) | K::Global(_) | K::Module(_) | K::Const(_) | K::Template(_)) => {
|
2024-12-01 07:01:44 -06:00
|
|
|
unreachable!("{c:?}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn has_pointers(&self, tys: &Types) -> bool {
|
|
|
|
match self.expand() {
|
|
|
|
Kind::Struct(s) => tys.struct_fields(s).iter().any(|f| f.ty.has_pointers(tys)),
|
|
|
|
Kind::Ptr(_) => true,
|
|
|
|
Kind::Slice(s) => tys.ins.slices[s].len == ArrayLen::MAX,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub enum Loc {
|
|
|
|
Reg,
|
|
|
|
Stack,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u64> for Id {
|
|
|
|
fn from(id: u64) -> Self {
|
|
|
|
Self(unsafe { NonZeroU32::new_unchecked(id as _) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn array_to_lower_case<const N: usize>(array: [u8; N]) -> [u8; N] {
|
|
|
|
let mut result = [0; N];
|
|
|
|
let mut i = 0;
|
|
|
|
while i < N {
|
|
|
|
result[i] = array[i].to_ascii_lowercase();
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
// const string to lower case
|
|
|
|
|
|
|
|
macro_rules! builtin_type {
|
|
|
|
($($name:ident;)*) => {
|
|
|
|
$(const $name: u32 = ${index(0)} + 1;)*
|
|
|
|
|
|
|
|
mod __lc_names {
|
|
|
|
use super::*;
|
|
|
|
$(pub const $name: &str = unsafe {
|
|
|
|
const LCL: &[u8] = unsafe {
|
|
|
|
&array_to_lower_case(
|
|
|
|
*(stringify!($name).as_ptr() as *const [u8; stringify!($name).len()])
|
|
|
|
)
|
|
|
|
};
|
|
|
|
core::str::from_utf8_unchecked(LCL)
|
|
|
|
};)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Builtin {
|
|
|
|
$(pub const $name: Self = Builtin($name);)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Id {
|
|
|
|
$(pub const $name: Self = Kind::Builtin(Builtin($name)).compress();)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Kind {
|
|
|
|
$(pub const $name: Self = Kind::Builtin(Builtin($name));)*
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_str(name: &str) -> Option<Builtin> {
|
|
|
|
match name {
|
|
|
|
$(__lc_names::$name => Some(Builtin($name)),)*
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_str(ty: Builtin) -> &'static str {
|
|
|
|
match ty.0 {
|
|
|
|
$($name => __lc_names::$name,)*
|
|
|
|
v => unreachable!("invalid type: {}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
builtin_type! {
|
|
|
|
UNDECLARED;
|
|
|
|
LEFT_UNREACHABLE;
|
|
|
|
RIGHT_UNREACHABLE;
|
|
|
|
NEVER;
|
|
|
|
VOID;
|
|
|
|
TYPE;
|
|
|
|
BOOL;
|
|
|
|
U8;
|
|
|
|
U16;
|
|
|
|
U32;
|
|
|
|
UINT;
|
|
|
|
I8;
|
|
|
|
I16;
|
|
|
|
I32;
|
|
|
|
INT;
|
|
|
|
F32;
|
|
|
|
F64;
|
2024-12-17 12:01:01 -06:00
|
|
|
ANY_TYPE;
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! type_kind {
|
|
|
|
($(#[$meta:meta])* $vis:vis enum $name:ident {$( $variant:ident, )*}) => {
|
|
|
|
crate::utils::decl_ent! {
|
|
|
|
$(pub struct $variant(u32);)*
|
|
|
|
}
|
|
|
|
|
|
|
|
$(#[$meta])*
|
|
|
|
$vis enum $name {
|
|
|
|
$($variant($variant),)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $name {
|
|
|
|
const FLAG_BITS: u32 = (${count($variant)} as u32).next_power_of_two().ilog2();
|
|
|
|
const FLAG_OFFSET: u32 = core::mem::size_of::<Id>() as u32 * 8 - Self::FLAG_BITS;
|
|
|
|
const INDEX_MASK: u32 = (1 << (32 - Self::FLAG_BITS)) - 1;
|
|
|
|
|
|
|
|
$vis fn from_ty(ty: Id) -> Self {
|
|
|
|
let (flag, index) = (ty.repr() >> Self::FLAG_OFFSET, ty.repr() & Self::INDEX_MASK);
|
|
|
|
match flag {
|
|
|
|
$(${index(0)} => Self::$variant($variant(index)),)*
|
|
|
|
i => unreachable!("{i}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$vis const fn compress(self) -> Id {
|
|
|
|
let (index, flag) = match self {
|
|
|
|
$(Self::$variant(index) => (index.0, ${index(0)}),)*
|
|
|
|
};
|
|
|
|
Id(unsafe { NonZeroU32::new_unchecked((flag << Self::FLAG_OFFSET) | index) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-17 10:51:14 -06:00
|
|
|
impl Id {
|
|
|
|
pub fn kind(self) -> u8 {
|
|
|
|
(self.repr() >> $name::FLAG_OFFSET) as _
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-01 07:01:44 -06:00
|
|
|
$(
|
|
|
|
impl From<$variant> for $name {
|
|
|
|
fn from(value: $variant) -> Self {
|
|
|
|
Self::$variant(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<$variant> for i64 {
|
|
|
|
fn from(value: $variant) -> Self {
|
|
|
|
Id::from(value).into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<$variant> for Id {
|
|
|
|
fn from(value: $variant) -> Self {
|
|
|
|
$name::$variant(value).compress()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
type_kind! {
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum Kind {
|
|
|
|
Builtin,
|
|
|
|
Struct,
|
2024-12-19 16:08:36 -06:00
|
|
|
Tuple,
|
2024-12-01 07:01:44 -06:00
|
|
|
Enum,
|
2024-12-01 08:11:38 -06:00
|
|
|
Union,
|
2024-12-01 07:01:44 -06:00
|
|
|
Ptr,
|
|
|
|
Slice,
|
|
|
|
Opt,
|
|
|
|
Func,
|
2024-12-02 08:51:12 -06:00
|
|
|
Template,
|
2024-12-01 07:01:44 -06:00
|
|
|
Global,
|
|
|
|
Const,
|
2024-12-17 10:51:14 -06:00
|
|
|
Module,
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-01 08:11:38 -06:00
|
|
|
impl Func {
|
|
|
|
pub const ECA: Func = Func(u32::MAX);
|
|
|
|
pub const MAIN: Func = Func(u32::MIN);
|
|
|
|
}
|
|
|
|
|
2024-12-01 07:01:44 -06:00
|
|
|
impl Module {
|
|
|
|
pub const MAIN: Self = Self(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Module {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(u32::MAX)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Ident> for Builtin {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn try_from(value: Ident) -> Result<Self, Self::Error> {
|
|
|
|
if value.is_null() {
|
|
|
|
Ok(Self(value.len()))
|
|
|
|
} else {
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Kind {
|
|
|
|
fn default() -> Self {
|
|
|
|
Id::UNDECLARED.expand()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Display<'a> {
|
|
|
|
tys: &'a Types,
|
2024-12-01 12:04:27 -06:00
|
|
|
files: &'a EntSlice<Module, parser::Ast>,
|
2024-12-01 07:01:44 -06:00
|
|
|
ty: Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Display<'a> {
|
2024-12-01 12:04:27 -06:00
|
|
|
pub fn new(tys: &'a Types, files: &'a EntSlice<Module, parser::Ast>, ty: Id) -> Self {
|
2024-12-01 07:01:44 -06:00
|
|
|
Self { tys, files, ty }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rety(&self, ty: Id) -> Self {
|
|
|
|
Self::new(self.tys, self.files, ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl core::fmt::Display for Display<'_> {
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
2024-12-19 16:08:36 -06:00
|
|
|
use Kind as K;
|
|
|
|
match K::from_ty(self.ty) {
|
|
|
|
K::Module(idx) => {
|
2024-12-01 07:01:44 -06:00
|
|
|
f.write_str("@use(\"")?;
|
2024-12-01 12:04:27 -06:00
|
|
|
self.files[idx].path.fmt(f)?;
|
2024-12-01 07:01:44 -06:00
|
|
|
f.write_str(")[")?;
|
|
|
|
idx.fmt(f)?;
|
|
|
|
f.write_str("]")
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
K::Builtin(ty) => f.write_str(to_str(ty)),
|
|
|
|
K::Opt(ty) => {
|
2024-12-01 07:01:44 -06:00
|
|
|
f.write_str("?")?;
|
|
|
|
self.rety(self.tys.ins.opts[ty].base).fmt(f)
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
K::Ptr(ty) => {
|
2024-12-01 07:01:44 -06:00
|
|
|
f.write_str("^")?;
|
|
|
|
self.rety(self.tys.ins.ptrs[ty].base).fmt(f)
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
K::Struct(idx) => {
|
2024-12-01 07:01:44 -06:00
|
|
|
let record = &self.tys.ins.structs[idx];
|
|
|
|
if record.name.is_null() {
|
|
|
|
f.write_str("[")?;
|
|
|
|
idx.fmt(f)?;
|
|
|
|
f.write_str("]{")?;
|
2024-12-17 10:51:14 -06:00
|
|
|
for (i, &StructField { name, ty, .. }) in
|
2024-12-01 07:01:44 -06:00
|
|
|
self.tys.struct_fields(idx).iter().enumerate()
|
|
|
|
{
|
|
|
|
if i != 0 {
|
|
|
|
f.write_str(", ")?;
|
|
|
|
}
|
|
|
|
f.write_str(self.tys.names.ident_str(name))?;
|
|
|
|
f.write_str(": ")?;
|
|
|
|
self.rety(ty).fmt(f)?;
|
|
|
|
}
|
|
|
|
f.write_str("}")
|
|
|
|
} else {
|
2024-12-01 12:04:27 -06:00
|
|
|
let file = &self.files[record.file];
|
2024-12-01 07:01:44 -06:00
|
|
|
f.write_str(file.ident_str(record.name))
|
|
|
|
}
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
K::Tuple(idx) => {
|
|
|
|
f.write_str(".(")?;
|
|
|
|
for (i, &ty) in
|
|
|
|
self.tys.ins.args[self.tys.ins.tuples[idx].fields.range()].iter().enumerate()
|
|
|
|
{
|
|
|
|
if i != 0 {
|
|
|
|
f.write_str(", ")?;
|
|
|
|
}
|
|
|
|
self.rety(ty).fmt(f)?;
|
|
|
|
}
|
|
|
|
f.write_str(")")
|
|
|
|
}
|
|
|
|
K::Union(idx) => {
|
2024-12-01 08:11:38 -06:00
|
|
|
let record = &self.tys.ins.unions[idx];
|
|
|
|
if record.name.is_null() {
|
|
|
|
f.write_str("[")?;
|
|
|
|
idx.fmt(f)?;
|
|
|
|
f.write_str("]{")?;
|
2024-12-17 10:51:14 -06:00
|
|
|
for (i, &UnionField { name, ty }) in
|
2024-12-01 08:11:38 -06:00
|
|
|
self.tys.union_fields(idx).iter().enumerate()
|
|
|
|
{
|
|
|
|
if i != 0 {
|
|
|
|
f.write_str(", ")?;
|
|
|
|
}
|
|
|
|
f.write_str(self.tys.names.ident_str(name))?;
|
|
|
|
f.write_str(": ")?;
|
|
|
|
self.rety(ty).fmt(f)?;
|
|
|
|
}
|
|
|
|
f.write_str("}")
|
|
|
|
} else {
|
2024-12-01 12:04:27 -06:00
|
|
|
let file = &self.files[record.file];
|
2024-12-01 08:11:38 -06:00
|
|
|
f.write_str(file.ident_str(record.name))
|
|
|
|
}
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
K::Enum(idx) => {
|
2024-12-01 07:01:44 -06:00
|
|
|
let enm = &self.tys.ins.enums[idx];
|
|
|
|
debug_assert!(!enm.name.is_null());
|
2024-12-01 12:04:27 -06:00
|
|
|
let file = &self.files[enm.file];
|
2024-12-01 07:01:44 -06:00
|
|
|
f.write_str(file.ident_str(enm.name))
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
K::Func(idx) => {
|
2024-12-01 07:01:44 -06:00
|
|
|
f.write_str("fn")?;
|
|
|
|
idx.fmt(f)
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
K::Template(idx) => {
|
2024-12-02 08:51:12 -06:00
|
|
|
f.write_str("fn")?;
|
|
|
|
idx.fmt(f)
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
K::Global(idx) => {
|
2024-12-01 07:01:44 -06:00
|
|
|
let global = &self.tys.ins.globals[idx];
|
2024-12-01 12:04:27 -06:00
|
|
|
let file = &self.files[global.file];
|
2024-12-01 07:01:44 -06:00
|
|
|
f.write_str(file.ident_str(global.name))?;
|
|
|
|
f.write_str(" (global)")
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
K::Slice(idx) => {
|
2024-12-01 07:01:44 -06:00
|
|
|
let array = self.tys.ins.slices[idx];
|
|
|
|
f.write_str("[")?;
|
2024-12-20 04:32:18 -06:00
|
|
|
if let Some(len) = array.len() {
|
|
|
|
len.fmt(f)?;
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
2024-12-20 04:32:18 -06:00
|
|
|
f.write_str("]")?;
|
|
|
|
self.rety(array.elem).fmt(f)
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
K::Const(idx) => {
|
2024-12-01 07:01:44 -06:00
|
|
|
let cnst = &self.tys.ins.consts[idx];
|
2024-12-01 12:04:27 -06:00
|
|
|
let file = &self.files[cnst.file];
|
2024-12-01 07:01:44 -06:00
|
|
|
f.write_str(file.ident_str(cnst.name))?;
|
|
|
|
f.write_str(" (const)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
|
|
|
|
pub enum SymKey<'a> {
|
2024-12-19 16:08:36 -06:00
|
|
|
Tuple(List),
|
2024-12-01 07:01:44 -06:00
|
|
|
Pointer(&'a PtrData),
|
|
|
|
Optional(&'a OptData),
|
2024-12-19 16:08:36 -06:00
|
|
|
Type(Id, Pos, List),
|
2024-12-01 07:01:44 -06:00
|
|
|
Decl(Id, Ident),
|
|
|
|
Array(&'a ArrayData),
|
|
|
|
Constant(&'a ConstData),
|
|
|
|
}
|
|
|
|
|
2024-12-02 08:51:12 -06:00
|
|
|
#[derive(Clone, Copy, Default)]
|
2024-12-01 07:01:44 -06:00
|
|
|
pub struct Sig {
|
2024-12-19 16:08:36 -06:00
|
|
|
pub args: List,
|
2024-12-01 07:01:44 -06:00
|
|
|
pub ret: Id,
|
|
|
|
}
|
|
|
|
|
2024-12-02 08:51:12 -06:00
|
|
|
pub struct TemplateData {
|
|
|
|
pub file: Module,
|
|
|
|
pub parent: Id,
|
|
|
|
pub name: Ident,
|
|
|
|
pub expr: ExprRef,
|
|
|
|
pub is_inline: bool,
|
|
|
|
}
|
|
|
|
|
2024-12-01 07:01:44 -06:00
|
|
|
#[derive(Default, Clone, Copy)]
|
|
|
|
pub struct FuncData {
|
|
|
|
pub file: Module,
|
|
|
|
pub parent: Id,
|
|
|
|
pub name: Ident,
|
2024-12-02 08:51:12 -06:00
|
|
|
pub pos: Pos,
|
2024-12-01 07:01:44 -06:00
|
|
|
pub expr: ExprRef,
|
2024-12-02 08:51:12 -06:00
|
|
|
pub sig: Sig,
|
2024-12-01 07:01:44 -06:00
|
|
|
pub is_inline: bool,
|
2024-12-02 08:51:12 -06:00
|
|
|
pub is_generic: bool,
|
|
|
|
pub comp_state: [PackedCompState; 2],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub struct PackedCompState(u16);
|
|
|
|
|
|
|
|
impl Default for PackedCompState {
|
|
|
|
fn default() -> Self {
|
|
|
|
CompState::default().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PackedCompState {
|
|
|
|
const COMPILED: u16 = u16::MAX - 1;
|
|
|
|
const DEAD: u16 = u16::MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PackedCompState> for CompState {
|
|
|
|
fn from(value: PackedCompState) -> Self {
|
|
|
|
match value.0 {
|
|
|
|
PackedCompState::DEAD => CompState::Dead,
|
|
|
|
PackedCompState::COMPILED => CompState::Compiled,
|
|
|
|
v => CompState::Queued(v as _),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<CompState> for PackedCompState {
|
|
|
|
fn from(value: CompState) -> Self {
|
|
|
|
Self(match value {
|
|
|
|
CompState::Dead => Self::DEAD,
|
|
|
|
CompState::Queued(v) => v.try_into().unwrap(),
|
|
|
|
CompState::Compiled => Self::COMPILED,
|
|
|
|
})
|
|
|
|
}
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub enum CompState {
|
|
|
|
#[default]
|
|
|
|
Dead,
|
|
|
|
Queued(usize),
|
|
|
|
Compiled,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct GlobalData {
|
|
|
|
pub file: Module,
|
|
|
|
pub name: Ident,
|
|
|
|
pub ty: Id,
|
|
|
|
pub data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Hash)]
|
|
|
|
pub struct ConstData {
|
|
|
|
pub ast: ExprRef,
|
|
|
|
pub name: Ident,
|
|
|
|
pub file: Module,
|
|
|
|
pub parent: Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct EnumField {
|
|
|
|
pub name: Ident,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct TypeBase {
|
|
|
|
pub file: Module,
|
|
|
|
pub parent: Id,
|
|
|
|
pub pos: Pos,
|
|
|
|
pub name: Ident,
|
|
|
|
pub field_start: u32,
|
2024-12-19 16:08:36 -06:00
|
|
|
pub captured: List,
|
2024-12-01 07:01:44 -06:00
|
|
|
pub ast: ExprRef,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct EnumData {
|
|
|
|
pub base: TypeBase,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_deref!(EnumData { base: TypeBase });
|
|
|
|
|
2024-12-17 10:51:14 -06:00
|
|
|
pub struct UnionField {
|
|
|
|
pub name: Ident,
|
|
|
|
pub ty: Id,
|
|
|
|
}
|
|
|
|
|
2024-12-01 08:11:38 -06:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct UnionData {
|
|
|
|
pub base: TypeBase,
|
|
|
|
pub size: Cell<Size>,
|
|
|
|
pub align: Cell<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_deref!(UnionData { base: TypeBase });
|
|
|
|
|
2024-12-01 07:01:44 -06:00
|
|
|
pub struct StructField {
|
|
|
|
pub name: Ident,
|
|
|
|
pub ty: Id,
|
2024-12-17 10:51:14 -06:00
|
|
|
pub default_value: Option<Const>,
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct StructData {
|
|
|
|
pub base: TypeBase,
|
|
|
|
pub size: Cell<Size>,
|
|
|
|
pub align: Cell<u8>,
|
|
|
|
// TODO: make this compact
|
|
|
|
pub explicit_alignment: Option<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_deref!(StructData { base: TypeBase });
|
|
|
|
|
2024-12-19 16:08:36 -06:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct TupleData {
|
|
|
|
pub fields: List,
|
|
|
|
pub size: Cell<Size>,
|
|
|
|
pub align: Cell<u8>,
|
|
|
|
}
|
|
|
|
|
2024-12-01 07:01:44 -06:00
|
|
|
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
|
|
|
|
pub struct OptData {
|
|
|
|
pub base: Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
|
|
|
|
pub struct PtrData {
|
|
|
|
pub base: Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub struct ArrayData {
|
|
|
|
pub elem: Id,
|
|
|
|
pub len: ArrayLen,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ArrayData {
|
|
|
|
#[expect(clippy::len_without_is_empty)]
|
|
|
|
pub fn len(&self) -> Option<usize> {
|
|
|
|
(self.len != ArrayLen::MAX).then_some(self.len as usize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ctx_map::CtxEntry for Ident {
|
|
|
|
type Ctx = str;
|
|
|
|
type Key<'a> = &'a str;
|
|
|
|
|
|
|
|
fn key<'a>(&self, ctx: &'a Self::Ctx) -> Self::Key<'a> {
|
|
|
|
unsafe { ctx.get_unchecked(self.range()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct IdentInterner {
|
|
|
|
lookup: ctx_map::CtxMap<Ident>,
|
|
|
|
strings: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IdentInterner {
|
|
|
|
pub fn intern(&mut self, ident: &str) -> Ident {
|
|
|
|
let (entry, hash) = self.lookup.entry(ident, &self.strings);
|
|
|
|
match entry {
|
|
|
|
hash_map::RawEntryMut::Occupied(o) => o.get_key_value().0.value,
|
|
|
|
hash_map::RawEntryMut::Vacant(v) => {
|
|
|
|
let id = Ident::new(self.strings.len() as _, ident.len() as _).unwrap();
|
|
|
|
self.strings.push_str(ident);
|
|
|
|
v.insert(ctx_map::Key { hash, value: id }, ());
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ident_str(&self, ident: Ident) -> &str {
|
|
|
|
&self.strings[ident.range()]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn project(&self, ident: &str) -> Option<Ident> {
|
|
|
|
self.lookup.get(ident, &self.strings).copied()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear(&mut self) {
|
|
|
|
self.lookup.clear();
|
|
|
|
self.strings.clear()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct TypesTmp {
|
|
|
|
pub struct_fields: Vec<StructField>,
|
2024-12-17 10:51:14 -06:00
|
|
|
pub union_fields: Vec<UnionField>,
|
2024-12-01 07:01:44 -06:00
|
|
|
pub enum_fields: Vec<EnumField>,
|
|
|
|
pub args: Vec<Id>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct TypeIns {
|
|
|
|
pub args: Vec<Id>,
|
|
|
|
pub struct_fields: Vec<StructField>,
|
2024-12-17 10:51:14 -06:00
|
|
|
pub union_fields: Vec<UnionField>,
|
2024-12-01 07:01:44 -06:00
|
|
|
pub enum_fields: Vec<EnumField>,
|
|
|
|
pub funcs: EntVec<Func, FuncData>,
|
2024-12-02 08:51:12 -06:00
|
|
|
pub templates: EntVec<Template, TemplateData>,
|
2024-12-01 07:01:44 -06:00
|
|
|
pub globals: EntVec<Global, GlobalData>,
|
|
|
|
pub consts: EntVec<Const, ConstData>,
|
|
|
|
pub structs: EntVec<Struct, StructData>,
|
|
|
|
pub enums: EntVec<Enum, EnumData>,
|
2024-12-01 08:11:38 -06:00
|
|
|
pub unions: EntVec<Union, UnionData>,
|
2024-12-01 07:01:44 -06:00
|
|
|
pub ptrs: EntVec<Ptr, PtrData>,
|
|
|
|
pub opts: EntVec<Opt, OptData>,
|
|
|
|
pub slices: EntVec<Slice, ArrayData>,
|
2024-12-19 16:08:36 -06:00
|
|
|
pub tuples: EntVec<Tuple, TupleData>,
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct FTask {
|
|
|
|
pub file: Module,
|
|
|
|
pub id: Func,
|
|
|
|
pub ct: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StringRef(pub Global);
|
|
|
|
|
|
|
|
impl ctx_map::CtxEntry for StringRef {
|
|
|
|
type Ctx = EntVec<Global, GlobalData>;
|
|
|
|
type Key<'a> = &'a [u8];
|
|
|
|
|
|
|
|
fn key<'a>(&self, ctx: &'a Self::Ctx) -> Self::Key<'a> {
|
|
|
|
&ctx[self.0].data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Types {
|
|
|
|
pub syms: ctx_map::CtxMap<Id>,
|
|
|
|
pub names: IdentInterner,
|
|
|
|
pub strings: ctx_map::CtxMap<StringRef>,
|
|
|
|
pub ins: TypeIns,
|
|
|
|
pub tmp: TypesTmp,
|
|
|
|
pub tasks: Vec<Option<FTask>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Types {
|
2024-12-02 08:51:12 -06:00
|
|
|
pub fn case(
|
|
|
|
&self,
|
|
|
|
ty: Id,
|
|
|
|
files: &EntSlice<Module, parser::Ast>,
|
|
|
|
) -> fn(&str) -> Result<(), &'static str> {
|
2024-12-01 07:01:44 -06:00
|
|
|
match ty.expand() {
|
|
|
|
Kind::NEVER => |_| Ok(()),
|
|
|
|
Kind::Enum(_)
|
|
|
|
| Kind::Struct(_)
|
2024-12-01 08:11:38 -06:00
|
|
|
| Kind::Union(_)
|
2024-12-01 07:01:44 -06:00
|
|
|
| Kind::Builtin(_)
|
|
|
|
| Kind::Ptr(_)
|
|
|
|
| Kind::Slice(_)
|
2024-12-19 16:08:36 -06:00
|
|
|
| Kind::Tuple(_)
|
2024-12-01 07:01:44 -06:00
|
|
|
| Kind::Opt(_) => utils::is_pascal_case,
|
2024-12-02 08:51:12 -06:00
|
|
|
Kind::Func(f)
|
|
|
|
if let &Expr::Closure { ret: &Expr::Ident { id, .. }, .. } =
|
|
|
|
self.ins.funcs[f].expr.get(&files[self.ins.funcs[f].file])
|
|
|
|
&& id.is_type() =>
|
|
|
|
{
|
|
|
|
utils::is_pascal_case
|
|
|
|
}
|
|
|
|
Kind::Template(f)
|
|
|
|
if let &Expr::Closure { ret: &Expr::Ident { id, .. }, .. } =
|
|
|
|
self.ins.templates[f].expr.get(&files[self.ins.templates[f].file])
|
|
|
|
&& id.is_type() =>
|
|
|
|
{
|
|
|
|
utils::is_pascal_case
|
|
|
|
}
|
|
|
|
Kind::Func(_) | Kind::Template(_) | Kind::Global(_) | Kind::Module(_) => {
|
|
|
|
utils::is_snake_case
|
|
|
|
}
|
2024-12-01 07:01:44 -06:00
|
|
|
Kind::Const(_) => utils::is_screaming_case,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-19 16:08:36 -06:00
|
|
|
pub fn pack_args(&mut self, arg_base: usize) -> Option<List> {
|
2024-12-01 07:01:44 -06:00
|
|
|
let base = self.ins.args.len();
|
|
|
|
self.ins.args.extend(self.tmp.args.drain(arg_base..));
|
|
|
|
let needle = &self.ins.args[base..];
|
|
|
|
if needle.is_empty() {
|
2024-12-19 16:08:36 -06:00
|
|
|
return Some(List::empty());
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
let len = needle.len();
|
|
|
|
// FIXME: maybe later when this becomes a bottleneck we use more
|
|
|
|
// efficient search (SIMD?, indexing?)
|
|
|
|
let sp = self.ins.args.windows(needle.len()).position(|val| val == needle).unwrap();
|
|
|
|
self.ins.args.truncate((sp + needle.len()).max(base));
|
2024-12-19 16:08:36 -06:00
|
|
|
List::new(sp, len)
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
|
2024-12-17 10:51:14 -06:00
|
|
|
pub fn union_fields(&self, union: Union) -> &[UnionField] {
|
|
|
|
&self.ins.union_fields[self.union_field_range(union)]
|
2024-12-01 08:11:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn union_field_range(&self, union: Union) -> Range<usize> {
|
|
|
|
let start = self.ins.unions[union].field_start as usize;
|
|
|
|
let end = self
|
|
|
|
.ins
|
|
|
|
.unions
|
|
|
|
.next(union)
|
2024-12-17 10:51:14 -06:00
|
|
|
.map_or(self.ins.union_fields.len(), |s| s.field_start as usize);
|
2024-12-01 08:11:38 -06:00
|
|
|
start..end
|
|
|
|
}
|
|
|
|
|
2024-12-01 07:01:44 -06:00
|
|
|
pub fn struct_fields(&self, strct: Struct) -> &[StructField] {
|
|
|
|
&self.ins.struct_fields[self.struct_field_range(strct)]
|
|
|
|
}
|
|
|
|
|
2024-12-17 10:51:14 -06:00
|
|
|
pub fn struct_field_range(&self, strct: Struct) -> Range<usize> {
|
2024-12-01 07:01:44 -06:00
|
|
|
let start = self.ins.structs[strct].field_start as usize;
|
|
|
|
let end = self
|
|
|
|
.ins
|
|
|
|
.structs
|
|
|
|
.next(strct)
|
|
|
|
.map_or(self.ins.struct_fields.len(), |s| s.field_start as usize);
|
|
|
|
start..end
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enum_fields(&self, enm: Enum) -> &[EnumField] {
|
|
|
|
&self.ins.enum_fields[self.enum_field_range(enm)]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enum_field_range(&self, enm: Enum) -> Range<usize> {
|
|
|
|
let start = self.ins.enums[enm].field_start as usize;
|
|
|
|
let end =
|
|
|
|
self.ins.enums.next(enm).map_or(self.ins.enum_fields.len(), |s| s.field_start as usize);
|
|
|
|
start..end
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn make_opt(&mut self, base: Id) -> Id {
|
|
|
|
self.make_generic_ty(OptData { base }, |ins| &mut ins.opts, |e| SymKey::Optional(e))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn make_ptr(&mut self, base: Id) -> Id {
|
|
|
|
self.make_generic_ty(PtrData { base }, |ins| &mut ins.ptrs, |e| SymKey::Pointer(e))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn make_array(&mut self, elem: Id, len: ArrayLen) -> Id {
|
|
|
|
self.make_generic_ty(ArrayData { elem, len }, |ins| &mut ins.slices, |e| SymKey::Array(e))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_generic_ty<K: Ent + Into<Id>, T: Copy>(
|
|
|
|
&mut self,
|
|
|
|
ty: T,
|
|
|
|
get_col: fn(&mut TypeIns) -> &mut EntVec<K, T>,
|
|
|
|
key: fn(&T) -> SymKey,
|
|
|
|
) -> Id {
|
|
|
|
*self.syms.get_or_insert(key(&{ ty }), &mut self.ins, |ins| get_col(ins).push(ty).into())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn size_of(&self, ty: Id) -> Size {
|
|
|
|
match ty.expand() {
|
|
|
|
Kind::Slice(arr) => {
|
|
|
|
let arr = &self.ins.slices[arr];
|
|
|
|
match arr.len {
|
|
|
|
0 => 0,
|
|
|
|
ArrayLen::MAX => 16,
|
|
|
|
len => self.size_of(arr.elem) * len,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Kind::Struct(stru) => {
|
|
|
|
if self.ins.structs[stru].size.get() != 0 {
|
|
|
|
return self.ins.structs[stru].size.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut oiter = OffsetIter::new(stru, self);
|
|
|
|
while oiter.next(self).is_some() {}
|
|
|
|
self.ins.structs[stru].size.set(oiter.offset);
|
|
|
|
oiter.offset
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
Kind::Tuple(tuple) => {
|
|
|
|
if self.ins.tuples[tuple].size.get() != 0 {
|
|
|
|
return self.ins.tuples[tuple].size.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut oiter = OffsetIter::new(tuple, self);
|
|
|
|
while oiter.next(self).is_some() {}
|
|
|
|
self.ins.tuples[tuple].size.set(oiter.offset);
|
|
|
|
oiter.offset
|
|
|
|
}
|
2024-12-01 08:11:38 -06:00
|
|
|
Kind::Union(union) => {
|
|
|
|
if self.ins.unions[union].size.get() != 0 {
|
|
|
|
return self.ins.unions[union].size.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
let size =
|
|
|
|
self.union_fields(union).iter().map(|f| self.size_of(f.ty)).max().unwrap_or(0);
|
|
|
|
self.ins.unions[union].size.set(size);
|
|
|
|
size
|
|
|
|
}
|
2024-12-01 07:01:44 -06:00
|
|
|
Kind::Enum(enm) => (self.enum_field_range(enm).len().ilog2() + 7) / 8,
|
|
|
|
Kind::Opt(opt) => {
|
|
|
|
let base = self.ins.opts[opt].base;
|
|
|
|
if self.nieche_of(base).is_some() {
|
|
|
|
self.size_of(base)
|
|
|
|
} else {
|
|
|
|
self.size_of(base) + self.align_of(base)
|
|
|
|
}
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
Kind::Ptr(_) | Kind::Builtin(_) => ty.simple_size().unwrap(),
|
|
|
|
Kind::Func(_)
|
|
|
|
| Kind::Template(_)
|
|
|
|
| Kind::Global(_)
|
|
|
|
| Kind::Const(_)
|
|
|
|
| Kind::Module(_) => unreachable!(),
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn align_of(&self, ty: Id) -> Size {
|
|
|
|
match ty.expand() {
|
2024-12-01 08:11:38 -06:00
|
|
|
Kind::Union(union) => {
|
|
|
|
if self.ins.unions[union].align.get() != 0 {
|
|
|
|
return self.ins.unions[union].align.get() as _;
|
|
|
|
}
|
|
|
|
let align =
|
|
|
|
self.union_fields(union).iter().map(|f| self.align_of(f.ty)).max().unwrap_or(1);
|
|
|
|
self.ins.unions[union].align.set(align.try_into().unwrap());
|
|
|
|
align
|
|
|
|
}
|
2024-12-01 07:01:44 -06:00
|
|
|
Kind::Struct(stru) => {
|
|
|
|
if self.ins.structs[stru].align.get() != 0 {
|
|
|
|
return self.ins.structs[stru].align.get() as _;
|
|
|
|
}
|
|
|
|
let align = self.ins.structs[stru].explicit_alignment.map_or_else(
|
|
|
|
|| {
|
|
|
|
self.struct_fields(stru)
|
|
|
|
.iter()
|
|
|
|
.map(|&StructField { ty, .. }| self.align_of(ty))
|
|
|
|
.max()
|
|
|
|
.unwrap_or(1)
|
|
|
|
},
|
|
|
|
|a| a as _,
|
|
|
|
);
|
|
|
|
self.ins.structs[stru].align.set(align.try_into().unwrap());
|
|
|
|
align
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
Kind::Tuple(tuple) => {
|
|
|
|
if self.ins.tuples[tuple].align.get() != 0 {
|
|
|
|
return self.ins.tuples[tuple].align.get() as _;
|
|
|
|
}
|
|
|
|
let align =
|
|
|
|
self.tuple_fields(tuple).iter().map(|&f| self.align_of(f)).max().unwrap_or(1);
|
|
|
|
self.ins.tuples[tuple].align.set(align.try_into().unwrap());
|
|
|
|
align
|
|
|
|
}
|
2024-12-01 07:01:44 -06:00
|
|
|
Kind::Slice(arr) => {
|
|
|
|
let arr = &self.ins.slices[arr];
|
|
|
|
match arr.len {
|
|
|
|
ArrayLen::MAX => 8,
|
|
|
|
_ => self.align_of(arr.elem),
|
|
|
|
}
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
Kind::Opt(opt) => self.align_of(self.ins.opts[opt].base),
|
|
|
|
Kind::Builtin(_) | Kind::Enum(_) | Kind::Ptr(_) => self.size_of(ty),
|
|
|
|
Kind::Func(_)
|
|
|
|
| Kind::Template(_)
|
|
|
|
| Kind::Global(_)
|
|
|
|
| Kind::Const(_)
|
|
|
|
| Kind::Module(_) => unreachable!(),
|
|
|
|
//_ => self.size_of(ty).max(1),
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn base_of(&self, ty: Id) -> Option<Id> {
|
|
|
|
match ty.expand() {
|
|
|
|
Kind::Ptr(p) => Some(self.ins.ptrs[p].base),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn inner_of(&self, ty: Id) -> Option<Id> {
|
|
|
|
match ty.expand() {
|
|
|
|
Kind::Opt(o) => Some(self.ins.opts[o].base),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn opt_layout(&self, inner_ty: Id) -> OptLayout {
|
|
|
|
match self.nieche_of(inner_ty) {
|
|
|
|
Some((_, flag_offset, flag_ty)) => {
|
|
|
|
OptLayout { flag_ty, flag_offset, payload_offset: 0 }
|
|
|
|
}
|
|
|
|
None => OptLayout {
|
|
|
|
flag_ty: Id::BOOL,
|
|
|
|
flag_offset: 0,
|
|
|
|
payload_offset: self.align_of(inner_ty),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nieche_of(&self, ty: Id) -> Option<(bool, Offset, Id)> {
|
|
|
|
match ty.expand() {
|
|
|
|
Kind::Ptr(_) => Some((false, 0, Id::UINT)),
|
|
|
|
// TODO: cache this
|
|
|
|
Kind::Struct(s) => OffsetIter::new(s, self).into_iter(self).find_map(|(f, off)| {
|
|
|
|
self.nieche_of(f.ty).map(|(uninit, o, ty)| (uninit, o + off, ty))
|
|
|
|
}),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_struct_field(&self, s: Struct, name: &str) -> Option<usize> {
|
|
|
|
let name = self.names.project(name)?;
|
|
|
|
self.struct_fields(s).iter().position(|f| f.name == name)
|
|
|
|
}
|
|
|
|
|
2024-12-17 10:51:14 -06:00
|
|
|
pub fn find_union_field(&self, u: Union, name: &str) -> Option<(usize, &UnionField)> {
|
2024-12-01 08:11:38 -06:00
|
|
|
let name = self.names.project(name)?;
|
2024-12-01 12:04:27 -06:00
|
|
|
self.union_fields(u).iter().enumerate().find(|(_, f)| f.name == name)
|
2024-12-01 08:11:38 -06:00
|
|
|
}
|
|
|
|
|
2024-12-01 07:01:44 -06:00
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.syms.clear();
|
|
|
|
self.names.clear();
|
|
|
|
self.strings.clear();
|
|
|
|
|
|
|
|
self.ins.funcs.clear();
|
|
|
|
self.ins.args.clear();
|
|
|
|
self.ins.globals.clear();
|
|
|
|
self.ins.structs.clear();
|
|
|
|
self.ins.struct_fields.clear();
|
2024-12-17 10:51:14 -06:00
|
|
|
self.ins.union_fields.clear();
|
|
|
|
self.ins.enum_fields.clear();
|
2024-12-01 07:01:44 -06:00
|
|
|
self.ins.ptrs.clear();
|
|
|
|
self.ins.slices.clear();
|
|
|
|
|
|
|
|
debug_assert_eq!(self.tmp.struct_fields.len(), 0);
|
2024-12-17 10:51:14 -06:00
|
|
|
debug_assert_eq!(self.tmp.union_fields.len(), 0);
|
|
|
|
debug_assert_eq!(self.tmp.enum_fields.len(), 0);
|
2024-12-01 07:01:44 -06:00
|
|
|
debug_assert_eq!(self.tmp.args.len(), 0);
|
|
|
|
|
|
|
|
debug_assert_eq!(self.tasks.len(), 0);
|
|
|
|
}
|
|
|
|
|
2024-12-01 12:04:27 -06:00
|
|
|
pub fn type_base_of(&self, id: Id) -> Option<&TypeBase> {
|
2024-12-01 08:11:38 -06:00
|
|
|
Some(match id.expand() {
|
|
|
|
Kind::Struct(s) => &*self.ins.structs[s],
|
|
|
|
Kind::Enum(e) => &*self.ins.enums[e],
|
|
|
|
Kind::Union(e) => &*self.ins.unions[e],
|
|
|
|
Kind::Builtin(_)
|
|
|
|
| Kind::Ptr(_)
|
|
|
|
| Kind::Slice(_)
|
|
|
|
| Kind::Opt(_)
|
|
|
|
| Kind::Func(_)
|
2024-12-02 08:51:12 -06:00
|
|
|
| Kind::Template(_)
|
2024-12-01 08:11:38 -06:00
|
|
|
| Kind::Global(_)
|
|
|
|
| Kind::Module(_)
|
2024-12-19 16:08:36 -06:00
|
|
|
| Kind::Tuple(_)
|
2024-12-01 08:11:38 -06:00
|
|
|
| Kind::Const(_) => return None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-01 07:01:44 -06:00
|
|
|
pub fn scope_of<'a>(&self, parent: Id, file: &'a parser::Ast) -> Option<&'a [Expr<'a>]> {
|
2024-12-01 08:11:38 -06:00
|
|
|
let base = match parent.expand() {
|
|
|
|
_ if let Some(base) = self.type_base_of(parent) => base,
|
|
|
|
Kind::Module(_) => return Some(file.exprs()),
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Expr::Struct { fields: [.., CommentOr::Or(Err(scope))], .. }
|
|
|
|
| Expr::Union { fields: [.., CommentOr::Or(Err(scope))], .. }
|
|
|
|
| Expr::Enum { variants: [.., CommentOr::Or(Err(scope))], .. } = base.ast.get(file)
|
|
|
|
{
|
|
|
|
Some(scope)
|
|
|
|
} else {
|
|
|
|
Some(&[])
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parent_of(&self, ty: Id) -> Option<Id> {
|
2024-12-01 08:11:38 -06:00
|
|
|
self.type_base_of(ty).map(|b| b.parent)
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
|
2024-12-21 07:21:58 -06:00
|
|
|
pub fn captures_of<'a>(
|
|
|
|
&self,
|
|
|
|
ty: Id,
|
|
|
|
file: &'a parser::Ast,
|
|
|
|
) -> Option<(&'a [CapturedIdent], List)> {
|
2024-12-01 08:11:38 -06:00
|
|
|
let base = self.type_base_of(ty)?;
|
|
|
|
|
|
|
|
let (Expr::Struct { captured, .. }
|
|
|
|
| Expr::Enum { captured, .. }
|
|
|
|
| Expr::Union { captured, .. }) = *base.ast.get(file)
|
|
|
|
else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
debug_assert_eq!(captured.len(), base.captured.len());
|
|
|
|
Some((captured, base.captured))
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
2024-12-16 06:20:47 -06:00
|
|
|
|
|
|
|
pub fn len_of(&self, ty: Id) -> Option<u32> {
|
|
|
|
Some(match ty.expand() {
|
|
|
|
Kind::Struct(s) => self.struct_field_range(s).len() as _,
|
2024-12-20 04:32:18 -06:00
|
|
|
Kind::Tuple(s) => self.ins.tuples[s].fields.len() as _,
|
2024-12-16 06:20:47 -06:00
|
|
|
Kind::Slice(s) => self.ins.slices[s].len()? as _,
|
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
2024-12-17 09:46:43 -06:00
|
|
|
|
|
|
|
pub fn name_of(&self, ty: Id, files: &EntSlice<Module, parser::Ast>, data: &mut Vec<u8>) {
|
|
|
|
use core::fmt::Write;
|
|
|
|
let str = unsafe { core::mem::transmute::<&mut Vec<u8>, &mut String>(data) };
|
|
|
|
write!(str, "{}", Display::new(self, files, ty)).unwrap();
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
|
|
|
|
pub fn tuple_fields(&self, tuple: Tuple) -> &[Id] {
|
|
|
|
&self.ins.args[self.ins.tuples[tuple].fields.range()]
|
|
|
|
}
|
2024-12-20 04:32:18 -06:00
|
|
|
|
|
|
|
pub fn elem_of(&self, ty: Id) -> Option<Id> {
|
|
|
|
match ty.expand() {
|
|
|
|
Kind::Slice(s) => Some(self.ins.slices[s].elem),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct OptLayout {
|
|
|
|
pub flag_ty: Id,
|
|
|
|
pub flag_offset: Offset,
|
|
|
|
pub payload_offset: Offset,
|
|
|
|
}
|
|
|
|
|
2024-12-19 16:08:36 -06:00
|
|
|
pub trait Agregate: Copy {
|
|
|
|
type Field: AsRef<Id> + 'static;
|
|
|
|
|
|
|
|
fn fields(self, tys: &Types) -> Range<usize>;
|
|
|
|
fn field_by_idx(tys: &Types, index: usize) -> &Self::Field;
|
|
|
|
fn align_override(self, _: &Types) -> Option<u8> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Agregate for Tuple {
|
|
|
|
type Field = Id;
|
|
|
|
|
|
|
|
fn fields(self, tys: &Types) -> Range<usize> {
|
|
|
|
tys.ins.tuples[self].fields.range()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn field_by_idx(tys: &Types, index: usize) -> &Self::Field {
|
|
|
|
&tys.ins.args[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Agregate for Struct {
|
|
|
|
type Field = StructField;
|
|
|
|
|
|
|
|
fn fields(self, tys: &Types) -> Range<usize> {
|
|
|
|
tys.struct_field_range(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn field_by_idx(tys: &Types, index: usize) -> &Self::Field {
|
|
|
|
&tys.ins.struct_fields[index]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn align_override(self, tys: &Types) -> Option<u8> {
|
|
|
|
tys.ins.structs[self].explicit_alignment
|
|
|
|
}
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
|
2024-12-19 16:08:36 -06:00
|
|
|
impl AsRef<Id> for StructField {
|
|
|
|
fn as_ref(&self) -> &Id {
|
|
|
|
&self.ty
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
}
|
2024-12-01 07:01:44 -06:00
|
|
|
|
2024-12-19 16:08:36 -06:00
|
|
|
pub struct OffsetIter<T> {
|
|
|
|
strct: T,
|
|
|
|
offset: Offset,
|
|
|
|
fields: Range<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OffsetIter<Struct> {
|
2024-12-01 07:01:44 -06:00
|
|
|
pub fn offset_of(tys: &Types, idx: Struct, field: &str) -> Option<(Offset, Id)> {
|
|
|
|
let field_id = tys.names.project(field)?;
|
|
|
|
OffsetIter::new(idx, tys)
|
|
|
|
.into_iter(tys)
|
|
|
|
.find(|(f, _)| f.name == field_id)
|
|
|
|
.map(|(f, off)| (off, f.ty))
|
|
|
|
}
|
2024-12-19 16:08:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Agregate> OffsetIter<T> {
|
|
|
|
pub fn new(strct: T, tys: &Types) -> Self {
|
|
|
|
Self { strct, offset: 0, fields: strct.fields(tys) }
|
|
|
|
}
|
2024-12-01 07:01:44 -06:00
|
|
|
|
2024-12-19 16:08:36 -06:00
|
|
|
fn next<'a>(&mut self, tys: &'a Types) -> Option<(&'a T::Field, Offset)> {
|
|
|
|
let field = &T::field_by_idx(tys, self.fields.next()?);
|
2024-12-01 07:01:44 -06:00
|
|
|
|
2024-12-19 16:08:36 -06:00
|
|
|
let align = self
|
|
|
|
.strct
|
|
|
|
.align_override(tys)
|
|
|
|
.map_or_else(|| tys.align_of(*field.as_ref()), |a| a as u32);
|
2024-12-01 07:01:44 -06:00
|
|
|
self.offset = (self.offset + align - 1) & !(align - 1);
|
|
|
|
|
|
|
|
let off = self.offset;
|
2024-12-19 16:08:36 -06:00
|
|
|
self.offset += tys.size_of(*field.as_ref());
|
2024-12-01 07:01:44 -06:00
|
|
|
Some((field, off))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn next_ty(&mut self, tys: &Types) -> Option<(Id, Offset)> {
|
|
|
|
let (field, off) = self.next(tys)?;
|
2024-12-19 16:08:36 -06:00
|
|
|
Some((*field.as_ref(), off))
|
2024-12-01 07:01:44 -06:00
|
|
|
}
|
|
|
|
|
2024-12-19 16:08:36 -06:00
|
|
|
pub fn into_iter(mut self, tys: &Types) -> impl Iterator<Item = (&T::Field, Offset)> {
|
2024-12-01 07:01:44 -06:00
|
|
|
core::iter::from_fn(move || self.next(tys))
|
|
|
|
}
|
|
|
|
}
|