Struct leo_passes::type_checking::checker::TypeChecker
source · pub struct TypeChecker<'a, N: Network> {
pub(crate) symbol_table: RefCell<SymbolTable>,
pub(crate) type_table: &'a TypeTable,
pub(crate) struct_graph: StructGraph,
pub(crate) call_graph: CallGraph,
pub(crate) handler: &'a Handler,
pub(crate) scope_state: ScopeState,
pub(crate) await_checker: AwaitChecker,
pub(crate) async_function_input_types: IndexMap<Location, Vec<Type>>,
pub(crate) used_structs: IndexSet<Symbol>,
phantom: PhantomData<N>,
}
Fields§
§symbol_table: RefCell<SymbolTable>
The symbol table for the program.
type_table: &'a TypeTable
A mapping from node IDs to their types.
struct_graph: StructGraph
A dependency graph of the structs in program.
call_graph: CallGraph
The call graph for the program.
handler: &'a Handler
The error handler.
scope_state: ScopeState
The state of the current scope being traversed.
await_checker: AwaitChecker
Struct to store the state relevant to checking all futures are awaited.
async_function_input_types: IndexMap<Location, Vec<Type>>
Mapping from async function name to the inferred input types.
used_structs: IndexSet<Symbol>
The set of used composites.
phantom: PhantomData<N>
Implementations§
source§impl<'a, N: Network> TypeChecker<'a, N>
impl<'a, N: Network> TypeChecker<'a, N>
sourcepub fn new(
symbol_table: SymbolTable,
type_table: &'a TypeTable,
handler: &'a Handler,
max_depth: usize,
disabled: bool,
) -> Self
pub fn new( symbol_table: SymbolTable, type_table: &'a TypeTable, handler: &'a Handler, max_depth: usize, disabled: bool, ) -> Self
Returns a new type checker given a symbol table and error handler.
sourcepub(crate) fn enter_scope(&mut self, index: usize)
pub(crate) fn enter_scope(&mut self, index: usize)
Enters a child scope.
sourcepub(crate) fn create_child_scope(&mut self) -> usize
pub(crate) fn create_child_scope(&mut self) -> usize
Creates a new child scope.
sourcepub(crate) fn exit_scope(&mut self, index: usize)
pub(crate) fn exit_scope(&mut self, index: usize)
Exits the current scope.
sourcepub fn emit_warning(&self, warning: TypeCheckerWarning)
pub fn emit_warning(&self, warning: TypeCheckerWarning)
Emits a type checker warning
sourcefn check_type(
&self,
is_valid: impl Fn(&Type) -> bool,
error_string: String,
type_: &Option<Type>,
span: Span,
)
fn check_type( &self, is_valid: impl Fn(&Type) -> bool, error_string: String, type_: &Option<Type>, span: Span, )
Emits an error to the handler if the given type is invalid.
sourcepub(crate) fn check_eq_types(
&self,
t1: &Option<Type>,
t2: &Option<Type>,
span: Span,
)
pub(crate) fn check_eq_types( &self, t1: &Option<Type>, t2: &Option<Type>, span: Span, )
Emits an error if the two given types are not equal.
sourcepub(crate) fn assert_and_return_type(
&mut self,
actual: Type,
expected: &Option<Type>,
span: Span,
) -> Type
pub(crate) fn assert_and_return_type( &mut self, actual: Type, expected: &Option<Type>, span: Span, ) -> Type
Use this method when you know the actual type.
Emits an error to the handler if the actual
type is not equal to the expected
type.
sourcepub(crate) fn assert_type(
&mut self,
actual: &Option<Type>,
expected: &Type,
span: Span,
)
pub(crate) fn assert_type( &mut self, actual: &Option<Type>, expected: &Type, span: Span, )
Emits an error to the error handler if the actual
type is not equal to the expected
type.
sourcepub(crate) fn assert_address_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_address_type(&self, type_: &Option<Type>, span: Span)
Emits an error to the error handler if the given type is not an address.
sourcepub(crate) fn assert_bool_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_bool_type(&self, type_: &Option<Type>, span: Span)
Emits an error to the handler if the given type is not a boolean.
sourcepub(crate) fn assert_field_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_field_type(&self, type_: &Option<Type>, span: Span)
Emits an error to the handler if the given type is not a field.
sourcepub(crate) fn assert_group_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_group_type(&self, type_: &Option<Type>, span: Span)
Emits an error to the handler if the given type is not a group.
sourcepub(crate) fn assert_scalar_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_scalar_type(&self, type_: &Option<Type>, span: Span)
Emits an error to the handler if the given type is not a scalar.
sourcepub(crate) fn assert_signature_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_signature_type(&self, type_: &Option<Type>, span: Span)
Emits an error to the handler if the given type is not a signature.
sourcepub(crate) fn assert_int_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_int_type(&self, type_: &Option<Type>, span: Span)
Emits an error to the handler if the given type is not an integer.
sourcepub(crate) fn assert_signed_int_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_signed_int_type(&self, type_: &Option<Type>, span: Span)
Emits an error to the handler if the given type is not a signed integer.
sourcepub(crate) fn assert_unsigned_int_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_unsigned_int_type(&self, type_: &Option<Type>, span: Span)
Emits an error to the handler if the given type is not an unsigned integer.
sourcepub(crate) fn assert_magnitude_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_magnitude_type(&self, type_: &Option<Type>, span: Span)
Emits an error to the handler if the given type is not a magnitude (u8, u16, u32).
sourcepub(crate) fn assert_bool_int_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_bool_int_type(&self, type_: &Option<Type>, span: Span)
Emits an error to the handler if the given type is not a boolean or an integer.
sourcepub(crate) fn assert_field_int_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_field_int_type(&self, type_: &Option<Type>, span: Span)
Emits an error to the handler if the given type is not a field or integer.
sourcepub(crate) fn assert_field_group_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_field_group_type(&self, type_: &Option<Type>, span: Span)
Emits an error to the handler if the given type is not a field or group.
sourcepub(crate) fn assert_field_group_int_type(
&self,
type_: &Option<Type>,
span: Span,
)
pub(crate) fn assert_field_group_int_type( &self, type_: &Option<Type>, span: Span, )
Emits an error to the handler if the given type is not a field, group, or integer.
sourcepub(crate) fn assert_field_group_signed_int_type(
&self,
type_: &Option<Type>,
span: Span,
)
pub(crate) fn assert_field_group_signed_int_type( &self, type_: &Option<Type>, span: Span, )
Emits an error to the handler if the given type is not a field, group, or signed integer.
sourcepub(crate) fn assert_field_scalar_int_type(
&self,
type_: &Option<Type>,
span: Span,
)
pub(crate) fn assert_field_scalar_int_type( &self, type_: &Option<Type>, span: Span, )
Emits an error to the handler if the given type is not a field, scalar, or integer.
sourcepub(crate) fn assert_field_group_scalar_int_type(
&self,
type_: &Option<Type>,
span: Span,
)
pub(crate) fn assert_field_group_scalar_int_type( &self, type_: &Option<Type>, span: Span, )
Emits an error to the handler if the given type is not a field, group, scalar, integer, or boolean.
sourcepub(crate) fn assert_castable_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_castable_type(&self, type_: &Option<Type>, span: Span)
Emits an error to the handler if the given type is not a field, group, scalar, integer, boolean, or address.
sourcepub(crate) fn get_core_constant(
&self,
type_: &Type,
constant: &Identifier,
) -> Option<CoreConstant>
pub(crate) fn get_core_constant( &self, type_: &Type, constant: &Identifier, ) -> Option<CoreConstant>
Type checks the inputs to an associated constant and returns the expected output type.
sourcepub(crate) fn get_core_function_call(
&self,
struct_: &Identifier,
function: &Identifier,
) -> Option<CoreFunction>
pub(crate) fn get_core_function_call( &self, struct_: &Identifier, function: &Identifier, ) -> Option<CoreFunction>
Emits an error if the struct
is not a core library struct.
Emits an error if the function
is not supported by the struct.
sourcepub(crate) fn check_core_function_call(
&mut self,
core_function: CoreFunction,
arguments: &[(Option<Type>, Span)],
function_span: Span,
) -> Option<Type>
pub(crate) fn check_core_function_call( &mut self, core_function: CoreFunction, arguments: &[(Option<Type>, Span)], function_span: Span, ) -> Option<Type>
Type checks the inputs to a core function call and returns the expected output type. Emits an error if the correct number of arguments are not provided. Emits an error if the arguments are not of the correct type.
sourcepub(crate) fn check_expected_struct(
&mut self,
struct_: &Composite,
expected: &Option<Type>,
span: Span,
) -> Type
pub(crate) fn check_expected_struct( &mut self, struct_: &Composite, expected: &Option<Type>, span: Span, ) -> Type
Returns the struct
type and emits an error if the expected
type does not match.
sourcepub(crate) fn assert_member_is_not_record(
&mut self,
span: Span,
parent: Symbol,
type_: &Type,
)
pub(crate) fn assert_member_is_not_record( &mut self, span: Span, parent: Symbol, type_: &Type, )
Emits an error if the struct member is a record type.
sourcepub(crate) fn assert_type_is_valid(&mut self, type_: &Type, span: Span) -> bool
pub(crate) fn assert_type_is_valid(&mut self, type_: &Type, span: Span) -> bool
Emits an error if the type or its constituent types is not valid.
sourcepub(crate) fn assert_mapping_type(
&self,
type_: &Option<Type>,
span: Span,
) -> Option<MappingType>
pub(crate) fn assert_mapping_type( &self, type_: &Option<Type>, span: Span, ) -> Option<MappingType>
Emits an error if the type is not a mapping.
sourcepub(crate) fn assert_array_type(&self, type_: &Option<Type>, span: Span)
pub(crate) fn assert_array_type(&self, type_: &Option<Type>, span: Span)
Emits an error if the type is not an array.
sourcepub(crate) fn check_function_signature(&mut self, function: &Function)
pub(crate) fn check_function_signature(&mut self, function: &Function)
Helper function to check that the input and output of function are valid
sourcepub(crate) fn lookup_struct(
&mut self,
program: Option<Symbol>,
name: Symbol,
) -> Option<Composite>
pub(crate) fn lookup_struct( &mut self, program: Option<Symbol>, name: Symbol, ) -> Option<Composite>
Wrapper around lookup_struct that additionally records all structs that are used in the program.
sourcepub(crate) fn assert_future_await(
&mut self,
future: &Option<&Expression>,
span: Span,
)
pub(crate) fn assert_future_await( &mut self, future: &Option<&Expression>, span: Span, )
Type checks the awaiting of a future.
sourcepub(crate) fn insert_variable(
&mut self,
inferred_type: Option<Type>,
name: &Identifier,
type_: Type,
index: usize,
span: Span,
)
pub(crate) fn insert_variable( &mut self, inferred_type: Option<Type>, name: &Identifier, type_: Type, index: usize, span: Span, )
Inserts variable to symbol table.
pub(crate) fn check_access_allowed( &mut self, name: &str, finalize_op: bool, span: Span, )
Trait Implementations§
source§impl<'a, N: Network> ExpressionVisitor<'a> for TypeChecker<'a, N>
impl<'a, N: Network> ExpressionVisitor<'a> for TypeChecker<'a, N>
type AdditionalInput = Option<Type>
type Output = Option<Type>
fn visit_expression( &mut self, input: &'a Expression, additional: &Self::AdditionalInput, ) -> Self::Output
fn visit_access( &mut self, input: &'a AccessExpression, expected: &Self::AdditionalInput, ) -> Self::Output
fn visit_array( &mut self, input: &'a ArrayExpression, additional: &Self::AdditionalInput, ) -> Self::Output
fn visit_binary( &mut self, input: &'a BinaryExpression, destination: &Self::AdditionalInput, ) -> Self::Output
fn visit_call( &mut self, input: &'a CallExpression, expected: &Self::AdditionalInput, ) -> Self::Output
fn visit_cast( &mut self, input: &'a CastExpression, expected: &Self::AdditionalInput, ) -> Self::Output
fn visit_struct_init( &mut self, input: &'a StructExpression, additional: &Self::AdditionalInput, ) -> Self::Output
fn visit_err( &mut self, _input: &'a ErrExpression, _additional: &Self::AdditionalInput, ) -> Self::Output
fn visit_identifier( &mut self, input: &'a Identifier, expected: &Self::AdditionalInput, ) -> Self::Output
fn visit_literal( &mut self, input: &'a Literal, expected: &Self::AdditionalInput, ) -> Self::Output
fn visit_locator( &mut self, input: &'a LocatorExpression, expected: &Self::AdditionalInput, ) -> Self::Output
fn visit_ternary( &mut self, input: &'a TernaryExpression, expected: &Self::AdditionalInput, ) -> Self::Output
fn visit_tuple( &mut self, input: &'a TupleExpression, expected: &Self::AdditionalInput, ) -> Self::Output
fn visit_unary( &mut self, input: &'a UnaryExpression, destination: &Self::AdditionalInput, ) -> Self::Output
fn visit_unit( &mut self, input: &'a UnitExpression, _additional: &Self::AdditionalInput, ) -> Self::Output
source§impl<'a, N: Network> Pass for TypeChecker<'a, N>
impl<'a, N: Network> Pass for TypeChecker<'a, N>
source§impl<'a, N: Network> ProgramVisitor<'a> for TypeChecker<'a, N>
impl<'a, N: Network> ProgramVisitor<'a> for TypeChecker<'a, N>
fn visit_program(&mut self, input: &'a Program)
fn visit_program_scope(&mut self, input: &'a ProgramScope)
fn visit_stub(&mut self, input: &'a Stub)
fn visit_struct(&mut self, input: &'a Composite)
fn visit_mapping(&mut self, input: &'a Mapping)
fn visit_function(&mut self, function: &'a Function)
fn visit_function_stub(&mut self, input: &'a FunctionStub)
fn visit_struct_stub(&mut self, input: &'a Composite)
fn visit_import(&mut self, input: &'a Program)
source§impl<'a, N: Network> StatementVisitor<'a> for TypeChecker<'a, N>
impl<'a, N: Network> StatementVisitor<'a> for TypeChecker<'a, N>
fn visit_statement(&mut self, input: &'a Statement)
fn visit_assert(&mut self, input: &'a AssertStatement)
fn visit_assign(&mut self, input: &'a AssignStatement)
fn visit_block(&mut self, input: &'a Block)
fn visit_conditional(&mut self, input: &'a ConditionalStatement)
fn visit_console(&mut self, _: &'a ConsoleStatement)
fn visit_const(&mut self, input: &'a ConstDeclaration)
fn visit_definition(&mut self, input: &'a DefinitionStatement)
fn visit_expression_statement(&mut self, input: &'a ExpressionStatement)
fn visit_iteration(&mut self, input: &'a IterationStatement)
fn visit_return(&mut self, input: &'a ReturnStatement)
Auto Trait Implementations§
impl<'a, N> !Freeze for TypeChecker<'a, N>
impl<'a, N> !RefUnwindSafe for TypeChecker<'a, N>
impl<'a, N> !Send for TypeChecker<'a, N>
impl<'a, N> !Sync for TypeChecker<'a, N>
impl<'a, N> Unpin for TypeChecker<'a, N>where
N: Unpin,
impl<'a, N> !UnwindSafe for TypeChecker<'a, N>
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
§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