SemaContext

Struct SemaContext 

Source
pub struct SemaContext<'a> {
Show 20 fields pub rir: &'a Rir, pub interner: &'a ThreadedRodeo, pub structs: HashMap<Spur, StructId>, pub enums: HashMap<Spur, EnumId>, pub functions: &'a HashMap<Spur, FunctionInfo>, pub methods: &'a HashMap<(StructId, Spur), MethodInfo>, pub preview_features: PreviewFeatures, pub builtin_string_id: Option<StructId>, pub builtin_arch_id: Option<EnumId>, pub builtin_os_id: Option<EnumId>, pub builtin_typekind_id: Option<EnumId>, pub target: Target, pub inference_ctx: InferenceContext, pub known: KnownSymbols, pub type_pool: TypeInternPool, pub module_registry: ModuleRegistry, pub source_file_path: Option<String>, pub file_paths: HashMap<FileId, String>, pub param_arena: &'a ParamArena, pub constants: &'a HashMap<Spur, ConstInfo>,
}
Expand description

Context for semantic analysis, designed for parallel function analysis.

This struct contains all type information and declarations needed during function body analysis. It is designed to be Send + Sync so it can be shared across threads during parallel function analysis.

§Contents

  • Struct and enum definitions (immutable)
  • Function and method signatures (references to immutable data in Sema)
  • Type intern pool (thread-safe, allows concurrent array interning)
  • Pre-computed inference context (immutable)
  • Built-in type IDs (immutable)
  • Parameter arena for function/method parameter data (immutable after declaration gathering)

§Thread Safety

SemaContext is Send + Sync because:

  • Most fields are immutable after construction
  • The type intern pool uses RwLock for thread-safe mutations
  • References to RIR and interner are shared immutably
  • References to functions/methods HashMaps are immutable after declaration gathering
  • Reference to param_arena is immutable after declaration gathering
  • ThreadedRodeo is designed to be thread-safe

Fields§

§rir: &'a Rir

Reference to the RIR being analyzed.

§interner: &'a ThreadedRodeo

Reference to the string interner.

§structs: HashMap<Spur, StructId>

Struct lookup: maps struct name symbol to StructId.

§enums: HashMap<Spur, EnumId>

Enum lookup: maps enum name symbol to EnumId.

§functions: &'a HashMap<Spur, FunctionInfo>

Function lookup: reference to Sema’s function map (immutable after declaration gathering).

§methods: &'a HashMap<(StructId, Spur), MethodInfo>

Method lookup: reference to Sema’s method map (immutable after declaration gathering). Uses (StructId, method_name) key to support anonymous struct methods.

§preview_features: PreviewFeatures

Enabled preview features.

§builtin_string_id: Option<StructId>

StructId of the synthetic String type.

§builtin_arch_id: Option<EnumId>

EnumId of the synthetic Arch enum (for @target_arch intrinsic).

§builtin_os_id: Option<EnumId>

EnumId of the synthetic Os enum (for @target_os intrinsic).

§builtin_typekind_id: Option<EnumId>

EnumId of the synthetic TypeKind enum (for @typeInfo intrinsic).

§target: Target

Compilation target (architecture + OS).

§inference_ctx: InferenceContext

Pre-computed inference context for HM type inference.

§known: KnownSymbols

Pre-interned known symbols for fast comparison.

§type_pool: TypeInternPool

Type intern pool for unified type representation (ADR-0024 Phase 1).

During Phase 1, the pool coexists with the existing type registries. It can be used for lookups but the canonical type representation remains the old Type enum. Later phases will migrate to using the pool exclusively.

§module_registry: ModuleRegistry

Thread-safe module registry. Supports concurrent lookups and insertions during parallel analysis.

§source_file_path: Option<String>

Path to the current source file being compiled (single-file mode). Used for resolving relative imports when only one file is compiled.

§file_paths: HashMap<FileId, String>

Maps FileId to source file paths (multi-file mode). Used for resolving relative imports when multiple files are compiled.

§param_arena: &'a ParamArena

Reference to the parameter arena for accessing function/method parameter data. Use param_arena.types(fn_info.params) to get parameter types, etc.

§constants: &'a HashMap<Spur, ConstInfo>

Constant lookup: reference to Sema’s constant map (immutable after declaration gathering). Used for looking up const declarations like const x = @import("...").

Implementations§

Source§

impl<'a> SemaContext<'a>

Source

pub fn builtin_string_type(&self) -> Type

Get the builtin String type as a Type::Struct.

Source

pub fn get_struct(&self, name: Spur) -> Option<StructId>

Look up a struct by name.

Source

pub fn get_struct_def(&self, id: StructId) -> StructDef

Get a struct definition by ID.

Source

pub fn get_enum(&self, name: Spur) -> Option<EnumId>

Look up an enum by name.

Source

pub fn get_enum_def(&self, id: EnumId) -> EnumDef

Get an enum definition by ID.

Source

pub fn get_function(&self, name: Spur) -> Option<&FunctionInfo>

Look up a function by name.

Source

pub fn get_method( &self, struct_id: StructId, method_name: Spur, ) -> Option<&MethodInfo>

Look up a method by struct ID and method name.

Source

pub fn get_constant(&self, name: Spur) -> Option<&ConstInfo>

Look up a constant by name.

Source

pub fn get_array_type_def(&self, id: ArrayTypeId) -> (Type, u64)

Get an array type definition by ID.

Returns (element_type, length) for the array.

Source

pub fn get_array_type( &self, element_type: Type, length: u64, ) -> Option<ArrayTypeId>

Look up an array type by element type and length.

Source

pub fn get_or_create_array_type( &self, element_type: Type, length: u64, ) -> ArrayTypeId

Get or create an array type. Thread-safe.

Source

pub fn get_or_create_ptr_const_type(&self, pointee_type: Type) -> PtrConstTypeId

Get or create a ptr const type. Thread-safe.

Source

pub fn get_or_create_ptr_mut_type(&self, pointee_type: Type) -> PtrMutTypeId

Get or create a ptr mut type. Thread-safe.

Source

pub fn get_module(&self, import_path: &str) -> Option<ModuleId>

Look up a module by import path.

Source

pub fn get_module_def(&self, id: ModuleId) -> ModuleDef

Get a module definition by ID.

Source

pub fn get_or_create_module( &self, import_path: String, file_path: String, ) -> (ModuleId, bool)

Get or create a module for the given import path and file path. Thread-safe.

Returns the ModuleId and whether it was newly created.

Source

pub fn update_module_def(&self, id: ModuleId, def: ModuleDef)

Update a module definition with populated declarations.

Source

pub fn get_source_path(&self, span: Span) -> Option<&str>

Get the source file path for a span.

Looks up the file path using the span’s file_id. Falls back to source_file_path for single-file compilation mode.

Source

pub fn get_file_path(&self, file_id: FileId) -> Option<&str>

Get the file path for a given FileId.

Source

pub fn is_accessible( &self, accessing_file_id: FileId, target_file_id: FileId, is_pub: bool, ) -> bool

Check if the accessing file can see a private item from the target file.

Visibility rules (per ADR-0026):

  • pub items are always accessible
  • Private items are accessible if the files are in the same directory module

Directory module membership includes:

  • Files directly in the directory (e.g., utils/strings.gruel is in utils)
  • Facade files for the directory (e.g., _utils.gruel is in utils module)

Returns true if the item is accessible.

Source

pub fn format_type_name(&self, ty: Type) -> String

Get a human-readable name for a type.

Source

pub fn is_type_copy(&self, ty: Type) -> bool

Check if a type is a Copy type.

Source

pub fn abi_slot_count(&self, ty: Type) -> u32

Get the number of ABI slots required for a type.

Source

pub fn field_slot_offset(&self, struct_id: StructId, field_index: usize) -> u32

Get the slot offset of a field within a struct.

Source

pub fn type_to_infer_type(&self, ty: Type) -> InferType

Convert a concrete Type to InferType for use in constraint generation.

Source

pub fn is_builtin_string(&self, ty: Type) -> bool

Check if a type is the builtin String type.

Source

pub fn get_builtin_type_def( &self, struct_id: StructId, ) -> Option<&'static BuiltinTypeDef>

Get the builtin type definition for a struct if it’s a builtin type.

Source

pub fn is_builtin_mutation_method(&self, method_name: &str) -> bool

Check if a method name is a builtin mutation method.

Source

pub fn builtin_air_type(&self, struct_id: StructId) -> Type

Get the AIR output type for a builtin struct.

Source

pub fn is_type_linear(&self, ty: Type) -> bool

Check if a type is a linear type.

Source

pub fn require_preview( &self, feature: PreviewFeature, what: &str, span: Span, ) -> CompileResult<()>

Check that a preview feature is enabled.

This is used to gate experimental features behind the --preview flag. Returns an error with a helpful message if the feature is not enabled.

Source

pub fn resolve_struct_through_module( &self, _module_ref: InstRef, type_name: Spur, span: Span, ) -> CompileResult<StructId>

Resolve a struct type through a module reference.

Used for qualified struct literals like module.StructName { ... }. The module_ref is an InstRef pointing to the result of an @import. Checks visibility: private structs are only accessible from the same directory.

Source

pub fn resolve_enum_through_module( &self, _module_ref: InstRef, type_name: Spur, span: Span, ) -> CompileResult<EnumId>

Resolve an enum type through a module reference.

Used for qualified enum paths like module.EnumName::Variant. The module_ref is an InstRef pointing to the result of an @import. Checks visibility: private enums are only accessible from the same directory.

Trait Implementations§

Source§

impl<'a> Debug for SemaContext<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Send for SemaContext<'a>

Source§

impl<'a> Sync for SemaContext<'a>

Auto Trait Implementations§

§

impl<'a> !Freeze for SemaContext<'a>

§

impl<'a> !RefUnwindSafe for SemaContext<'a>

§

impl<'a> Unpin for SemaContext<'a>

§

impl<'a> !UnwindSafe for SemaContext<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<'src, T> IntoMaybe<'src, T> for T
where T: 'src,

§

type Proj<U: 'src> = U

§

fn map_maybe<R>( self, _f: impl FnOnce(&'src T) -> &'src R, g: impl FnOnce(T) -> R, ) -> <T as IntoMaybe<'src, T>>::Proj<R>
where R: 'src,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more