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
impl Typifier
pub const fn new() -> Typifier
pub fn reset(&mut self)
pub fn get<'a>( &'a self, expr_handle: Handle<Expression>, types: &'a UniqueArena<Type> ) -> &'a TypeInner
sourcepub fn register_type(
&self,
expr_handle: Handle<Expression>,
types: &mut UniqueArena<Type>
) -> Handle<Type>
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.
sourcepub fn grow(
&mut self,
expr_handle: Handle<Expression>,
expressions: &Arena<Expression>,
ctx: &ResolveContext<'_>
) -> Result<(), ResolveError>
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
.
sourcepub fn invalidate(
&mut self,
expr_handle: Handle<Expression>,
expressions: &Arena<Expression>,
ctx: &ResolveContext<'_>
) -> Result<(), ResolveError>
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
.