pub struct TypeInternPool { /* private fields */ }Expand description
Thread-safe intern pool for all composite types.
The pool is designed to be built during declaration gathering (sequential) and then queried during function body analysis (potentially parallel).
§Thread Safety
Uses RwLock for interior mutability:
- Read lock for lookups (most common)
- Write lock for insertions (only during declaration gathering)
§Usage
let pool = TypeInternPool::new();
// Register nominal types (structs/enums)
let (struct_type, is_new) = pool.register_struct(name_spur, struct_def);
// Intern structural types (arrays)
let array_type = pool.intern_array(element_type, 10);
// Look up type data
if let Some(data) = pool.try_get(some_type) {
match data {
TypeData::Struct(s) => println!("struct {}", s.def.name),
TypeData::Enum(e) => println!("enum {}", e.def.name),
TypeData::Array { element, len } => println!("array of {:?}; {}", element, len),
}
}Implementations§
Source§impl TypeInternPool
impl TypeInternPool
Sourcepub fn new() -> TypeInternPool
pub fn new() -> TypeInternPool
Create a new empty pool.
Sourcepub fn register_struct(&self, name: Spur, def: StructDef) -> (StructId, bool)
pub fn register_struct(&self, name: Spur, def: StructDef) -> (StructId, bool)
Register a new struct (nominal - no deduplication).
Returns the StructId (containing the pool index) and whether it was newly inserted.
If a struct with this name already exists, returns the existing StructId.
Sourcepub fn reserve_struct_id(&self) -> StructId
pub fn reserve_struct_id(&self) -> StructId
Reserve a struct ID without registering the full definition yet.
This is used for anonymous structs where we need to know the ID before
we can construct the name (which includes the ID). Call complete_struct_registration
with the reserved ID to finish registration.
§Returns
Returns the reserved StructId. The caller MUST call complete_struct_registration
with this ID before any other pool operations that might read this entry.
§Example
let struct_id = pool.reserve_struct_id();
let name = format!("__anon_struct_{}", struct_id.0);
let name_spur = interner.get_or_intern(&name);
let def = StructDef { name: name.clone(), ... };
pool.complete_struct_registration(struct_id, name_spur, def);Sourcepub fn complete_struct_registration(
&self,
struct_id: StructId,
name: Spur,
def: StructDef,
)
pub fn complete_struct_registration( &self, struct_id: StructId, name: Spur, def: StructDef, )
Complete the registration of a previously reserved struct ID.
This must be called after reserve_struct_id to fill in the actual struct data.
The struct will be registered with the provided name for lookup purposes.
§Panics
Panics if:
- The struct_id wasn’t created by
reserve_struct_id - The slot at struct_id doesn’t contain a placeholder struct
- A struct with the given name already exists
Sourcepub fn register_enum(&self, name: Spur, def: EnumDef) -> (EnumId, bool)
pub fn register_enum(&self, name: Spur, def: EnumDef) -> (EnumId, bool)
Register a new enum (nominal - no deduplication).
Returns the EnumId (containing the pool index) and whether it was newly inserted.
If an enum with this name already exists, returns the existing EnumId.
Sourcepub fn intern_array(&self, element: InternedType, len: u64) -> InternedType
pub fn intern_array(&self, element: InternedType, len: u64) -> InternedType
Intern an array type (structural - deduplicates).
Returns the canonical InternedType for arrays with this element type and length.
If an identical array type already exists, returns the existing type.
Sourcepub fn intern_ptr_const(&self, pointee: InternedType) -> InternedType
pub fn intern_ptr_const(&self, pointee: InternedType) -> InternedType
Intern a ptr const type (structural - deduplicates).
Returns the canonical InternedType for pointers to this pointee type.
If an identical pointer type already exists, returns the existing type.
Sourcepub fn intern_ptr_mut(&self, pointee: InternedType) -> InternedType
pub fn intern_ptr_mut(&self, pointee: InternedType) -> InternedType
Intern a ptr mut type (structural - deduplicates).
Returns the canonical InternedType for mutable pointers to this pointee type.
If an identical pointer type already exists, returns the existing type.
Sourcepub fn get_struct_by_name(&self, name: Spur) -> Option<InternedType>
pub fn get_struct_by_name(&self, name: Spur) -> Option<InternedType>
Look up a struct by name.
Sourcepub fn get_enum_by_name(&self, name: Spur) -> Option<InternedType>
pub fn get_enum_by_name(&self, name: Spur) -> Option<InternedType>
Look up an enum by name.
Sourcepub fn get_array(&self, element: InternedType, len: u64) -> Option<InternedType>
pub fn get_array(&self, element: InternedType, len: u64) -> Option<InternedType>
Look up an array type by element and length.
Sourcepub fn get(&self, ty: InternedType) -> Option<TypeData>
pub fn get(&self, ty: InternedType) -> Option<TypeData>
Get type data for a composite type.
Returns None for primitive types (use InternedType::is_primitive() first).
§Panics
Panics if the index is invalid.
Sourcepub fn is_struct(&self, ty: InternedType) -> bool
pub fn is_struct(&self, ty: InternedType) -> bool
Check if this is a struct type.
Sourcepub fn is_enum(&self, ty: InternedType) -> bool
pub fn is_enum(&self, ty: InternedType) -> bool
Check if this is an enum type.
Sourcepub fn is_array(&self, ty: InternedType) -> bool
pub fn is_array(&self, ty: InternedType) -> bool
Check if this is an array type.
Sourcepub fn get_struct_def(&self, ty: InternedType) -> Option<StructDef>
pub fn get_struct_def(&self, ty: InternedType) -> Option<StructDef>
Get the struct definition if this is a struct type.
Sourcepub fn get_enum_def(&self, ty: InternedType) -> Option<EnumDef>
pub fn get_enum_def(&self, ty: InternedType) -> Option<EnumDef>
Get the enum definition if this is an enum type.
Sourcepub fn get_array_info(&self, ty: InternedType) -> Option<(InternedType, u64)>
pub fn get_array_info(&self, ty: InternedType) -> Option<(InternedType, u64)>
Get array info (element type, length) if this is an array type.
Sourcepub fn struct_def(&self, struct_id: StructId) -> StructDef
pub fn struct_def(&self, struct_id: StructId) -> StructDef
Get a struct definition by StructId.
The StructId contains a pool index. This method looks up the struct in the pool and returns a clone of its definition.
§Panics
Panics if the StructId doesn’t correspond to a struct in the pool.
Sourcepub fn enum_def(&self, enum_id: EnumId) -> EnumDef
pub fn enum_def(&self, enum_id: EnumId) -> EnumDef
Get an enum definition by EnumId.
The EnumId contains a pool index. This method looks up the enum in the pool and returns a clone of its definition.
§Panics
Panics if the EnumId doesn’t correspond to an enum in the pool.
Sourcepub fn update_struct_def(&self, struct_id: StructId, new_def: StructDef)
pub fn update_struct_def(&self, struct_id: StructId, new_def: StructDef)
Update a struct definition in the pool.
This is used during semantic analysis when struct fields are resolved after the struct is initially registered.
§Panics
Panics if the StructId doesn’t correspond to a struct in the pool.
Sourcepub fn update_enum_def(&self, enum_id: EnumId, new_def: EnumDef)
pub fn update_enum_def(&self, enum_id: EnumId, new_def: EnumDef)
Update an enum definition in the pool.
This is used during semantic analysis when enum variants are resolved after the enum is initially registered.
§Panics
Panics if the EnumId doesn’t correspond to an enum in the pool.
Sourcepub fn struct_id_to_interned(&self, struct_id: StructId) -> InternedType
pub fn struct_id_to_interned(&self, struct_id: StructId) -> InternedType
Convert a StructId to an InternedType.
Since StructId now contains a pool index, we just add the primitive offset.
Sourcepub fn enum_id_to_interned(&self, enum_id: EnumId) -> InternedType
pub fn enum_id_to_interned(&self, enum_id: EnumId) -> InternedType
Convert an EnumId to an InternedType.
Since EnumId now contains a pool index, we just add the primitive offset.
Sourcepub fn array_def(&self, array_id: ArrayTypeId) -> (Type, u64)
pub fn array_def(&self, array_id: ArrayTypeId) -> (Type, u64)
Get an array type definition by ArrayTypeId.
The ArrayTypeId contains a pool index. This method looks up the array in the pool and returns its element type and length as a tuple.
§Returns
Returns (element_type, length) where element_type is the array’s element type
and length is the array’s fixed size.
§Panics
Panics if the ArrayTypeId doesn’t correspond to an array in the pool.
Sourcepub fn intern_array_from_type(
&self,
element_type: Type,
len: u64,
) -> ArrayTypeId
pub fn intern_array_from_type( &self, element_type: Type, len: u64, ) -> ArrayTypeId
Intern an array type from a Type element.
This is a helper method that converts the Type to InternedType and then interns the array.
§Panics
Panics if the element type contains a struct/enum that isn’t in the pool.
Sourcepub fn get_array_by_type(
&self,
element_type: Type,
len: u64,
) -> Option<ArrayTypeId>
pub fn get_array_by_type( &self, element_type: Type, len: u64, ) -> Option<ArrayTypeId>
Look up an array type by Type element and length.
Returns None if no such array exists in the pool.
Sourcepub fn intern_ptr_const_from_type(&self, pointee_type: Type) -> PtrConstTypeId
pub fn intern_ptr_const_from_type(&self, pointee_type: Type) -> PtrConstTypeId
Intern a ptr const type from a Type pointee.
§Panics
Panics if the pointee type contains a struct/enum that isn’t in the pool.
Sourcepub fn intern_ptr_mut_from_type(&self, pointee_type: Type) -> PtrMutTypeId
pub fn intern_ptr_mut_from_type(&self, pointee_type: Type) -> PtrMutTypeId
Intern a ptr mut type from a Type pointee.
§Panics
Panics if the pointee type contains a struct/enum that isn’t in the pool.
Sourcepub fn ptr_const_def(&self, ptr_id: PtrConstTypeId) -> Type
pub fn ptr_const_def(&self, ptr_id: PtrConstTypeId) -> Type
Get ptr const pointee type if this is a ptr const type.
Sourcepub fn ptr_mut_def(&self, ptr_id: PtrMutTypeId) -> Type
pub fn ptr_mut_def(&self, ptr_id: PtrMutTypeId) -> Type
Get ptr mut pointee type if this is a ptr mut type.
Sourcepub fn array_id_to_interned(&self, array_id: ArrayTypeId) -> InternedType
pub fn array_id_to_interned(&self, array_id: ArrayTypeId) -> InternedType
Convert an ArrayTypeId to an InternedType.
Since ArrayTypeId now contains a pool index, we just add the primitive offset.
Sourcepub fn all_struct_ids(&self) -> Vec<StructId>
pub fn all_struct_ids(&self) -> Vec<StructId>
Get all struct IDs registered in the pool.
Returns a vector of all StructId values, useful for iterating over all structs (e.g., for drop glue synthesis).
Sourcepub fn all_enum_ids(&self) -> Vec<EnumId>
pub fn all_enum_ids(&self) -> Vec<EnumId>
Get all enum IDs registered in the pool.
Returns a vector of all EnumId values, useful for iterating over all enums.
Sourcepub fn all_array_ids(&self) -> Vec<ArrayTypeId>
pub fn all_array_ids(&self) -> Vec<ArrayTypeId>
Get all array IDs registered in the pool.
Returns a vector of all ArrayTypeId values, useful for iterating over all arrays (e.g., for drop glue synthesis).
Sourcepub fn stats(&self) -> TypeInternPoolStats
pub fn stats(&self) -> TypeInternPoolStats
Get statistics about the pool contents.
Sourcepub fn type_to_interned(&self, ty: Type) -> Option<InternedType>
pub fn type_to_interned(&self, ty: Type) -> Option<InternedType>
Convert an old-style Type to an InternedType.
This is a temporary helper for Phase 1 migration. It converts the
existing Type enum to the new interned representation.
§Note
For struct/enum types, the corresponding type must already be registered in the pool. For array types, this returns an error since array interning requires the pool to already have the element type interned.
Sourcepub fn interned_to_type(&self, ty: InternedType) -> Option<Type>
pub fn interned_to_type(&self, ty: InternedType) -> Option<Type>
Convert an InternedType back to the old-style Type.
This is a temporary helper for Phase 1 migration to verify correctness.
Returns None for composite types since we need the old IDs.
Sourcepub fn abi_slot_count(&self, ty: Type) -> u32
pub fn abi_slot_count(&self, ty: Type) -> u32
Return the number of ABI slots that ty occupies when passed as a function argument.
- Scalars (integers, bool, enum, pointer) → 1
- Struct → sum of field slot counts (flattened)
- Array → element slot count × length
- Zero-sized types (unit, never, comptime-only, module) → 0
Sourcepub fn format_type_name(&self, ty: Type) -> String
pub fn format_type_name(&self, ty: Type) -> String
Return the human-readable name of ty, suitable for error messages.
Examples: "i32", "bool", "MyStruct", "[i32; 4]", "ptr const i32".
Trait Implementations§
Source§impl Clone for TypeInternPool
impl Clone for TypeInternPool
Source§fn clone(&self) -> TypeInternPool
fn clone(&self) -> TypeInternPool
Clone the pool by copying all type data into a new pool.
This is used when building SemaContext from Sema, as the context
needs its own copy of the pool for thread-safe sharing.
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TypeInternPool
impl Debug for TypeInternPool
Source§impl Default for TypeInternPool
impl Default for TypeInternPool
Source§fn default() -> TypeInternPool
fn default() -> TypeInternPool
Auto Trait Implementations§
impl !Freeze for TypeInternPool
impl RefUnwindSafe for TypeInternPool
impl Send for TypeInternPool
impl Sync for TypeInternPool
impl Unpin for TypeInternPool
impl UnwindSafe for TypeInternPool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<'src, T> IntoMaybe<'src, T> for Twhere
T: 'src,
impl<'src, T> IntoMaybe<'src, T> for Twhere
T: 'src,
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<'p, T> Seq<'p, T> for Twhere
T: Clone,
impl<'p, T> Seq<'p, T> for Twhere
T: Clone,
§type Iter<'a> = Once<&'a T>
where
T: 'a
type Iter<'a> = Once<&'a T> where T: 'a
§fn contains(&self, val: &T) -> boolwhere
T: PartialEq,
fn contains(&self, val: &T) -> boolwhere
T: PartialEq,
§fn to_maybe_ref<'b>(item: <T as Seq<'p, T>>::Item<'b>) -> Maybe<T, &'p T>where
'p: 'b,
fn to_maybe_ref<'b>(item: <T as Seq<'p, T>>::Item<'b>) -> Maybe<T, &'p T>where
'p: 'b,
MaybeRef].