SPPCompiler.SemanticAnalysis.AstUtils.AstTypeUtils module

class SPPCompiler.SemanticAnalysis.AstUtils.AstTypeUtils.AstTypeUtils

Bases: object

! Type utility methods that mostly revolve around generic types, creating new scopes and synbols for them, and# organising the super-scopes for generic substitutions. This is one of the most complex set of helper functions in the compiler, and uses detailed scoping and symbol checks.

static is_type_indexable(type, scope)
static is_type_functional(type, scope)
static is_type_generator(type, scope)
static is_type_tuple(type, scope)
static is_type_array(type, scope)
static is_type_variant(type, scope)
static is_index_within_type_bound(index, type, scope)
static get_nth_type_of_indexable_type(sm, index, type)
Return type:

Asts.TypeAst

static get_namespaced_scope_with_error(sm, namespace)
Return type:

Scope

static get_type_part_symbol_with_error(scope, sm, type_part, ignore_alias=False, **kwargs)
Return type:

TypeSymbol | AliasSymbol

static create_generic_cls_scope(sm, type_part, old_cls_symbol, external_generic_symbols, is_tuple, **kwargs)
Return type:

Scope

static create_generic_fun_scope(sm, old_fun_scope, generic_arguments, external_generic_symbols, **kwargs)
Return type:

Scope

static create_generic_sup_scope(sm, old_sup_scope, new_cls_scope, generic_arguments, external_generic_symbols, **kwargs)
Return type:

tuple[Scope, Scope]

static create_generic_symbol(sm, generic_argument, tm=None)
Return type:

TypeSymbol | VariableSymbol

static generic_convert_sup_scope_name(name, generics, pos)
static is_type_recursive(type, sm)
static get_generator_and_yielded_type(type, sm, expr, what)

Get the generator type, and the yielded type from a type. Search the super types of the input type for a generator type, and return it along with the extracted Yield type.

static deduplicate_composite_types(type, scope)

Given a variant type, such as “Str or Bool or Bool or U32”, create a list of the composite types, including nested variants. This example will return [Str, Bool, Bool, U32]. Semantic analysis will collapse any duplicates.

Parameters:
  • type – The incoming variant type.

  • scope – The scope for comparing the type against “Var”.

Returns:

The list of composite types.

static symbolic_eq(lhs_type, rhs_type, lhs_scope, rhs_scope, check_variant=True, lhs_ignore_alias=False)

Compare the two types for symbolic equality. This ensures that they are the exact same type, and not just textually the same.

Parameters:
  • lhs_type – The left-hand side type to compare.

  • rhs_type – The right-hand side type to compare.

  • lhs_scope – The scope to get the left-hand side type from.

  • rhs_scope – The scope to get the right-hand side type from.

  • check_variant – Whether to allow variant type matching.

  • lhs_ignore_alias – Whether to ignore alias types on the left-hand side.

Returns:

Whether the two types are equal.

TODO: Need to add a function checker. So if the lhs_type is like FunRef[…], and the RHS is a collection of

function overloads,then as long as one overload matches, it should be accepted. Something like iterate the function overloads for the $Func type, and use AstFunctionUtils to check for an overload conflict => complete match?

static relaxed_symbolic_eq(lhs_type, rhs_type, lhs_scope, rhs_scope, generics=None)

The relaxed version of the symbolic equality check is the same as normal symbolic matching, but it allows a generic to be matched against any type. For example, Vec[Str] will match [T] Vec[T]. This is required in superimpositions, for fallthrough generic types to have the correct sup-ext blocks applied. Similarly, Vec[Str] will also match against [T] T (blanket superimpositions), because T itself would be a generic.

Notably, check_variant is not present in this function, because “Str | Bool” cannot have the methods of Str and Bool (use pattern matching to extract), and both Str and Bool can’t have methods attached to the variant, as they are different types (Str vs Var[Str, Bool] etc).

Parameters:
  • lhs_type – The left-hand side type to compare.

  • rhs_type – The right-hand side type to compare.

  • lhs_scope – The scope to get the left-hand side type from.

  • rhs_scope – The scope to get the right-hand side type from.

  • generics – The generics that have been accepted as relaxed comparative (value = generic).

Returns:

Whether the two types are equal.