naga::front

Struct Typifier

Source
pub struct Typifier { /* private fields */ }
Expand description

A table of types for an Arena<Expression>.

A front end can use a Typifier to get types for an arena’s expressions while it is still contributing expressions to it. At any point, you can call [typifier.grow(expr, arena, ctx)], where expr is a Handle<Expression> referring to something in arena, and the Typifier will resolve the types of all the expressions up to and including expr. Then you can write typifier[handle] to get the type of any handle at or before expr.

Note that Typifier does not build an Arena<Type> as a part of its usual operation. Ideally, a module’s type arena should only contain types actually needed by Handle<Type>s elsewhere in the module — functions, variables, Compose expressions, other types, and so on — so we don’t want every little thing that occurs as the type of some intermediate expression to show up there.

Instead, Typifier accumulates a TypeResolution for each expression, which refers to the Arena<Type> in the ResolveContext passed to grow as needed. TypeResolution is a lightweight representation for intermediate types like this; see its documentation for details.

If you do need to register a Typifier’s conclusion in an Arena<Type> (say, for a LocalVariable whose type you’ve inferred), you can use register_type to do so.

Implementations§

Source§

impl Typifier

Source

pub const fn new() -> Self

Source

pub fn reset(&mut self)

Source

pub fn get<'a>( &'a self, expr_handle: Handle<Expression>, types: &'a UniqueArena<Type>, ) -> &'a TypeInner

Source

pub fn register_type( &self, expr_handle: Handle<Expression>, types: &mut UniqueArena<Type>, ) -> Handle<Type>

Add an expression’s type to an Arena<Type>.

Add the type of expr_handle to types, and return a Handle<Type> referring to it.

§Note

If you just need a TypeInner for expr_handle’s type, consider using typifier[expression].inner_with(types) instead. Calling TypeResolution::inner_with often lets us avoid adding anything to the arena, which can significantly reduce the number of types that end up in the final module.

Source

pub fn grow( &mut self, expr_handle: Handle<Expression>, expressions: &Arena<Expression>, ctx: &ResolveContext<'_>, ) -> Result<(), ResolveError>

Grow this typifier until it contains a type for expr_handle.

Source

pub fn invalidate( &mut self, expr_handle: Handle<Expression>, expressions: &Arena<Expression>, ctx: &ResolveContext<'_>, ) -> Result<(), ResolveError>

Recompute the type resolution for expr_handle.

If the type of expr_handle hasn’t yet been calculated, call grow to ensure it is covered.

In either case, when this returns, self[expr_handle] should be an updated type resolution for expr_handle.

Trait Implementations§

Source§

impl Debug for Typifier

Source§

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

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

impl Default for Typifier

Source§

fn default() -> Typifier

Returns the “default value” for a type. Read more
Source§

impl Index<Handle<Expression>> for Typifier

Source§

type Output = TypeResolution

The returned type after indexing.
Source§

fn index(&self, handle: Handle<Expression>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

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.

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.

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.