Sema

Struct Sema 

Source
pub struct Sema<'a> { /* private fields */ }
Expand description

Semantic analyzer that converts RIR to AIR.

Implementations§

Source§

impl<'a> Sema<'a>

Source

pub fn analyze_specialized_function( &mut self, infer_ctx: &InferenceContext, return_type: Type, params: &[(Spur, Type, RirParamMode)], body: InstRef, type_subst: &HashMap<Spur, Type>, ) -> CompileResult<(Air, u32, u32, Vec<bool>, Vec<Type>, Vec<CompileWarning>, Vec<String>, HashSet<Spur>, HashSet<(StructId, Spur)>)>

Analyze a specialized function body.

This is similar to analyze_function but for generic function specialization. The type_subst map provides substitutions for type parameters to their concrete types.

For example, when specializing fn identity<T>(x: T) -> T { x } with T = i32, the params will be [(x, i32, Normal)] and return_type will be i32.

Source§

impl<'a> Sema<'a>

Source

pub fn build_inference_context(&self) -> InferenceContext

Build an InferenceContext from the collected type information.

This should be called after the collection phase and builds the pre-computed maps needed for Hindley-Milner type inference. Building this once and reusing for all function analyses avoids the O(n²) cost of rebuilding these maps per function.

§Performance

This converts all function/method signatures to use InferType (which handles arrays structurally rather than by ID). This conversion is done once instead of per-function.

Source§

impl<'a> Sema<'a>

Source

pub fn set_file_paths(&mut self, file_paths: HashMap<FileId, String>)

Set file paths for module resolution in multi-file compilation.

This maps FileIds to their corresponding source file paths, enabling relative import resolution during @import.

Source§

impl Sema<'_>

Source

pub fn evaluate_const_initializers(&mut self) -> CompileResult<()>

Evaluate const initializers to determine their types.

This is Phase 2.5 of semantic analysis, called after declaration gathering but before function body analysis. It handles:

  • const x = @import("module") - evaluates to Type::Module
  • Other const initializers are left with placeholder types for now

This enables module re-exports where a const holds an imported module that can be accessed via dot notation.

Source§

impl<'a> Sema<'a>

Source

pub fn build_sema_context<'s>(&'s self) -> SemaContext<'s>
where 'a: 's,

Build a SemaContext from the current Sema state.

This creates an immutable context that can be shared across threads for parallel function body analysis. The SemaContext contains all type definitions, function signatures, and method signatures.

The returned SemaContext borrows functions and methods from Sema, so Sema must outlive the SemaContext. This is reflected in the lifetime relationship: the returned context lives for 's (the borrow of self), not 'a (the lifetime of the RIR/interner).

§Usage
let mut sema = Sema::new(rir, interner, preview);
sema.inject_builtin_types();
sema.register_type_names()?;
sema.resolve_declarations()?;
let ctx = sema.build_sema_context();
// Now ctx can be shared across threads for parallel analysis
// (while sema is borrowed)
Source§

impl Sema<'_>

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 in match patterns. Checks visibility: private enums are only accessible from the same directory.

Source§

impl<'a> Sema<'a>

Source

pub fn new( rir: &'a Rir, interner: &'a ThreadedRodeo, preview_features: PreviewFeatures, ) -> Self

Create a new semantic analyzer.

Source

pub fn set_suppress_comptime_dbg_print(&mut self, suppress: bool)

Configure whether comptime @dbg prints to stderr on-the-fly. When suppressed, output is still buffered into comptime_dbg_output and warnings are still emitted.

Source

pub fn analyze_all(self) -> MultiErrorResult<SemaOutput>

Perform semantic analysis on the RIR.

This is the main entry point for semantic analysis. It returns analyzed functions, struct definitions, enum definitions, and any warnings.

Source

pub fn analyze_all_bodies(self) -> MultiErrorResult<SemaOutput>

Analyze all function bodies, assuming declarations are already collected.

Auto Trait Implementations§

§

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

§

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

§

impl<'a> Send for Sema<'a>

§

impl<'a> Sync for Sema<'a>

§

impl<'a> Unpin for Sema<'a>

§

impl<'a> !UnwindSafe for Sema<'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