visitTypeDefinitionStatement method

  1. @override
Future<void> visitTypeDefinitionStatement(
  1. TypeDefinitionStatement statement
)
override

Implementation

@override
Future<void> visitTypeDefinitionStatement(TypeDefinitionStatement statement) async {
  final environment = _environment;

  final source = ExternalPackage(name: 'LOCAL');

  final definitionType = switch (statement.typeParameters) {
    null || [] => MonomorphicType(
        name: statement.name.lexeme,
        source: source,
      ),
    final elements => PolymorphicType(
        name: statement.name.lexeme,
        source: source,
        arguments: [
          for (final element in elements) TypeParameterType(name: element.identifier.lexeme),
        ],
      ),
  };

  _environment.defineType(definitionType);

  _environment = _environment.fork();

  if (statement.typeParameters case final typeParameters?) {
    for (final typeParameter in typeParameters) {
      final definedType = _environment.getType(typeParameter.identifier.lexeme);

      if (definedType is TypeParameterType) {
        _errorHandler?.emit(
          TypeParameterAlreadyDefinedError(typeParameter.identifier),
        );
      } else {
        final type = TypeParameterType(name: typeParameter.identifier.lexeme);

        _environment.defineType(type);
      }

      typeParameter.accept(this);
    }
  }

  for (final variant in statement.variants) {
    await variant.accept(this);
  }

  _environment = environment;
}