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>

source

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.

source

pub(crate) fn enter_scope(&mut self, index: usize)

Enters a child scope.

source

pub(crate) fn create_child_scope(&mut self) -> usize

Creates a new child scope.

source

pub(crate) fn exit_scope(&mut self, index: usize)

Exits the current scope.

source

pub(crate) fn emit_err(&self, err: TypeCheckerError)

Emits a type checker error.

source

pub fn emit_warning(&self, warning: TypeCheckerWarning)

Emits a type checker warning

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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).

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub(crate) fn assert_mapping_type( &self, type_: &Option<Type>, span: Span, ) -> Option<MappingType>

Emits an error if the type is not a mapping.

source

pub(crate) fn assert_array_type(&self, type_: &Option<Type>, span: Span)

Emits an error if the type is not an array.

source

pub(crate) fn check_function_signature(&mut self, function: &Function)

Helper function to check that the input and output of function are valid

source

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.

source

pub(crate) fn assert_future_await( &mut self, future: &Option<&Expression>, span: Span, )

Type checks the awaiting of a future.

source

pub(crate) fn insert_variable( &mut self, inferred_type: Option<Type>, name: &Identifier, type_: Type, index: usize, span: Span, )

Inserts variable to symbol table.

source

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>

§

type AdditionalInput = Option<Type>

§

type Output = Option<Type>

source§

fn visit_expression( &mut self, input: &'a Expression, additional: &Self::AdditionalInput, ) -> Self::Output

source§

fn visit_access( &mut self, input: &'a AccessExpression, expected: &Self::AdditionalInput, ) -> Self::Output

source§

fn visit_array( &mut self, input: &'a ArrayExpression, additional: &Self::AdditionalInput, ) -> Self::Output

source§

fn visit_binary( &mut self, input: &'a BinaryExpression, destination: &Self::AdditionalInput, ) -> Self::Output

source§

fn visit_call( &mut self, input: &'a CallExpression, expected: &Self::AdditionalInput, ) -> Self::Output

source§

fn visit_cast( &mut self, input: &'a CastExpression, expected: &Self::AdditionalInput, ) -> Self::Output

source§

fn visit_struct_init( &mut self, input: &'a StructExpression, additional: &Self::AdditionalInput, ) -> Self::Output

source§

fn visit_err( &mut self, _input: &'a ErrExpression, _additional: &Self::AdditionalInput, ) -> Self::Output

source§

fn visit_identifier( &mut self, input: &'a Identifier, expected: &Self::AdditionalInput, ) -> Self::Output

source§

fn visit_literal( &mut self, input: &'a Literal, expected: &Self::AdditionalInput, ) -> Self::Output

source§

fn visit_locator( &mut self, input: &'a LocatorExpression, expected: &Self::AdditionalInput, ) -> Self::Output

source§

fn visit_ternary( &mut self, input: &'a TernaryExpression, expected: &Self::AdditionalInput, ) -> Self::Output

source§

fn visit_tuple( &mut self, input: &'a TupleExpression, expected: &Self::AdditionalInput, ) -> Self::Output

source§

fn visit_unary( &mut self, input: &'a UnaryExpression, destination: &Self::AdditionalInput, ) -> Self::Output

source§

fn visit_unit( &mut self, input: &'a UnitExpression, _additional: &Self::AdditionalInput, ) -> Self::Output

source§

impl<'a, N: Network> Pass for TypeChecker<'a, N>

§

type Input = (&'a Ast, &'a Handler, SymbolTable, &'a TypeTable, usize, bool)

§

type Output = Result<(SymbolTable, DiGraph<Symbol>, DiGraph<Symbol>), LeoError>

source§

fn do_pass( (ast, handler, st, tt, max_depth, await_checking): Self::Input, ) -> Self::Output

Runs the compiler pass.
source§

impl<'a, N: Network> ProgramVisitor<'a> for TypeChecker<'a, N>

source§

fn visit_program(&mut self, input: &'a Program)

source§

fn visit_program_scope(&mut self, input: &'a ProgramScope)

source§

fn visit_stub(&mut self, input: &'a Stub)

source§

fn visit_struct(&mut self, input: &'a Composite)

source§

fn visit_mapping(&mut self, input: &'a Mapping)

source§

fn visit_function(&mut self, function: &'a Function)

source§

fn visit_function_stub(&mut self, input: &'a FunctionStub)

source§

fn visit_struct_stub(&mut self, input: &'a Composite)

source§

fn visit_import(&mut self, input: &'a Program)

source§

impl<'a, N: Network> StatementVisitor<'a> for TypeChecker<'a, N>

source§

fn visit_statement(&mut self, input: &'a Statement)

source§

fn visit_assert(&mut self, input: &'a AssertStatement)

source§

fn visit_assign(&mut self, input: &'a AssignStatement)

source§

fn visit_block(&mut self, input: &'a Block)

source§

fn visit_conditional(&mut self, input: &'a ConditionalStatement)

source§

fn visit_console(&mut self, _: &'a ConsoleStatement)

source§

fn visit_const(&mut self, input: &'a ConstDeclaration)

source§

fn visit_definition(&mut self, input: &'a DefinitionStatement)

source§

fn visit_expression_statement(&mut self, input: &'a ExpressionStatement)

source§

fn visit_iteration(&mut self, input: &'a IterationStatement)

source§

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> 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.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

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