define_type.rb 50.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
# frozen_string_literal: true

module Inkoc
  module Pass
    # rubocop: disable Metrics/ClassLength
    class DefineType
      include VisitorMethods
      include TypePass

      DeferredMethod = Struct.new(:node, :scope)

      def initialize(mod, state)
        super

        @constant_resolver = ConstantResolver.new(diagnostics)
        @deferred_methods = []
      end

      def process_deferred_methods
        @deferred_methods.each do |method|
          on_deferred_method(method.node, method.scope)
        end
      end

      def define_type_instance(node, scope, *extra)
        type = define_type(node, scope, *extra)

        unless type.type_instance?
          type = type.new_instance
          node.type = type
        end

        type
      end

      def on_module_body(node, scope)
        type = define_type(node, scope)

        process_deferred_methods

        type
      end

      def on_integer(*)
        typedb.integer_type.new_instance
      end

      def on_float(*)
        typedb.float_type.new_instance
      end

      def on_string(*)
        typedb.string_type.new_instance
      end

      def on_constant(node, scope)
        @constant_resolver.resolve(node, scope)
      end

      def on_type_name_reference(node, scope)
        type = define_type(node.name, scope)

        return type if type.error?

        if same_type_parameters?(node, type)
          wrap_optional_type(node, type)
        else
          TypeSystem::Error.new
        end
      end

      def same_type_parameters?(node, type)
        node_names = node.type_parameters.map(&:type_name)
        type_names = type.type_parameters.map(&:name)

        if node_names == type_names
          true
        else
          diagnostics.invalid_type_parameters(type, node_names, node.location)
          false
        end
      end

      def on_block_type(node, scope)
        proto = @state.typedb.block_type
        type =
          if node.lambda_type?
            TypeSystem::Block.lambda(proto)
          else
            TypeSystem::Block.closure(proto)
          end

93
        type.self_type = scope.self_type
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        type.define_call_method

        arg_types = node.arguments.map do |arg|
          define_type_instance(arg, scope)
        end

        type.define_arguments(arg_types)

        if node.returns
          type.return_type = define_type_instance(node.returns, scope)
        end

        if node.throws
          type.throw_type = define_type_instance(node.throws, scope)
        end

        wrap_optional_type(node, type)
      end
      alias on_lambda_type on_block_type

114 115 116 117
      def on_self_type_with_late_binding(node, _)
        wrap_optional_type(node, TypeSystem::SelfType.new)
      end

118
      def on_self_type(node, scope)
119 120 121 122 123 124 125 126 127 128
        self_type = scope.self_type

        # When "Self" translates to a generic type, e.g. Array!(T), we want to
        # return a type in the form of `Array!(T -> T)`, and not just `Array`.
        # This ensures that any arguments passed to a method returning "Self"
        # can properly initialise the type.
        type_arguments =
          self_type.generic_type? ? self_type.type_parameters.to_a : []

        wrap_optional_type(node, self_type.new_instance(type_arguments))
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
      end

      def on_dynamic_type(node, _)
        wrap_optional_type(node, TypeSystem::Dynamic.new)
      end

      def on_void_type(node, _)
        wrap_optional_type(node, TypeSystem::Void.new)
      end

      def on_type_name(node, scope)
        type = define_type(node.name, scope)

        return type if type.error?
        return wrap_optional_type(node, type) unless type.generic_type?

        # When our type is a generic type we need to initialise it according to
        # the passed type parameters.
        type_arguments = node
          .type_parameters
          .zip(type.type_parameters)
          .map do |param_node, param|
151
            param_instance = define_type_instance(param_node, scope)
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

            if param && !param_instance.type_compatible?(param, @state)
              return diagnostics
                  .type_error(param, param_instance, param_node.location)
            end

            param_instance
          end

        num_given = type_arguments.length
        num_expected = type.type_parameters.length

        if num_given != num_expected
          return diagnostics.type_parameter_count_error(
            num_given,
            num_expected,
            node.location
          )
        end

        # Simply referencing a constant should not lead to it being initialised,
        # unless there are any type parameters to initialise.
        wrap_optional_type(
          node,
          type.new_instance_for_reference(type_arguments)
        )
      end

      def on_attribute(node, scope)
        name = node.name
        symbol = scope.self_type.lookup_attribute(name)

        if symbol.nil?
          diagnostics
            .undefined_attribute_error(scope.self_type, name, node.location)

          TypeSystem::Error.new
        else
          symbol.type
        end
      end

      def on_identifier(node, scope)
        name = node.name
        loc = node.location
        self_type = scope.self_type

        if (depth_sym = scope.depth_and_symbol_for_local(name))
          node.depth = depth_sym[0]
          node.symbol = depth_sym[1]

          remap_send_return_type(node.symbol.type, scope)
        elsif self_type.responds_to_message?(name)
          identifier_send(node, scope.self_type, name, scope)
        elsif scope.module_type.responds_to_message?(name)
          identifier_send(node, scope.module_type, name, scope)
        elsif (global = @module.lookup_global(name))
          global
        else
          diagnostics.undefined_method_error(self_type, name, loc)
          TypeSystem::Error.new
        end
      end

      def identifier_send(node, source, name, scope)
        node.block_type = source.lookup_method(name).type
        return_type = source.message_return_type(name, scope.self_type)

        remap_send_return_type(return_type, scope)
      end

      def on_self(_, scope)
        scope.self_type.new_instance
      end

      def on_send(node, scope)
228
        node.receiver_type = source = type_of_receiver(node, scope)
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

        if source.dynamic?
          send_to_dynamic_type(node, scope)
        elsif source.error?
          source
        elsif source.optional?
          send_to_optional_type(node, source, scope)
        else
          send_to_known_type(node, source, scope)
        end
      end

      def send_to_dynamic_type(node, scope)
        define_types(node.arguments, scope)

        TypeSystem::Dynamic.new
      end

      def send_to_optional_type(node, source, scope)
        nil_type = typedb.nil_type
        rtype = send_to_known_type(node, source, scope)
        name = node.name

        return rtype if rtype.error?

        if (nil_impl = nil_type.lookup_method(name)) && nil_impl.any?
          rec_impl = source.lookup_method(name).type

          # Only if the receiver and Nil implement the message in a compatible
          # way can we return the message's return type directly.
          if rec_impl.type_compatible?(nil_impl.type, @state)
            rtype
          else
            # Nil and the receiver have different implementations of the same
            # message. For example, type T implements "foo -> Integer" while Nil
            # implements it as "foo -> String". In this case the compiler can
            # not make a reasonable guess as to what the type will be, so we
            # just error instead.
            diagnostics.incompatible_optional_method(
              source,
              nil_type,
              name,
              node.location
            )
          end
        else
          TypeSystem::Optional.wrap(rtype)
        end
      end

      def send_to_known_type(node, source, scope)
        name = node.name
        method = source.lookup_method(name).type_or_else do
282 283
          unknown_method = source.lookup_unknown_message(@state)

284
          if unknown_method.nil?
285 286 287 288 289 290 291
            return diagnostics
                .undefined_method_error(source, name, node.location)
          end

          # If the method is not defined, but the receiver _does_ implement
          # "unknown_message", just return the type of that implementation.
          return unknown_method.type.resolved_return_type(source)
292 293
        end

294 295 296 297
        unless verify_method_bounds(source, method, node.location)
          return TypeSystem::Error.new
        end

298 299 300 301 302 303 304 305 306 307
        exp_args = method.argument_count_range

        unless exp_args.cover?(node.arguments.length)
          return diagnostics.argument_count_error(
            node.arguments.length,
            exp_args,
            node.location
          )
        end

308 309 310
        method = initialize_method_for_send(node, method, scope)

        return method if method.error?
311 312 313 314

        verify_argument_types_and_initialize(node, source, method, scope)
      end

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
      def verify_method_bounds(receiver, method, loc)
        method.method_bounds.all? do |bound|
          param = receiver.lookup_type_parameter(bound.name)
          instance = receiver.lookup_type_parameter_instance(param)

          if !instance || instance.type_compatible?(bound, @state)
            true
          else
            diagnostics
              .method_requirement_error(receiver, method, instance, bound, loc)

            false
          end
        end
      end

331 332 333
      # rubocop: disable Metrics/CyclomaticComplexity
      # rubocop: disable Metrics/PerceivedComplexity
      # rubocop: disable Metrics/BlockLength
334
      # rubocop: disable Metrics/AbcSize
335 336 337 338 339 340 341 342 343 344
      def verify_argument_types_and_initialize(node, source, method, scope)
        node.arguments.each_with_index do |arg_node, index|
          rest = false

          if arg_node.keyword_argument?
            keyword_type = method.keyword_argument_type(arg_node.name, source)

            unless keyword_type
              return diagnostics.undefined_keyword_argument_error(
                arg_node.name,
345
                source,
346 347 348 349 350 351 352 353 354 355
                method,
                arg_node.location
              )
            end

            exp_arg = keyword_type
          else
            exp_arg, rest = method.argument_type_at(index, source)
          end

356 357
          exp_arg = exp_arg.resolve_type_parameters(source, method)

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
          given_arg =
            if arg_node.closure? && exp_arg.block?
              # When passing a closure to a closure we want to infer the
              # arguments of our given closure according to the arguments of the
              # expected closure.
              #
              # Before we do this, we create a copy of the expected closure and
              # make sure any instance type parameters are initialised. This
              # ensures that if the expected argument of a closure is "T", we
              # use any corresponding type parameter instances if available,
              # instead of just using "T" as-is.
              #
              # In other words, if the expected block is defined like this:
              #
              #     do (T)
              #
              # And our given block is defined like this:
              #
              #     do (thing) { ... }
              #
              # Then "thing" will be whatever instance is bound to type
              # parameter "T", or "T" itself is no instance was bound.
380 381
              exp_arg =
                exp_arg.with_type_parameter_instances_from([source, method])
382 383 384 385 386 387 388 389 390 391 392 393 394 395

              # When passing a block without a signature (e.g. `foo { 10 }`) we
              # want to infer this as a lambda, if the expected block is also a
              # lambda. This allows one to write code such as the following:
              #
              #     process.spawn {
              #       ...
              #     }
              #
              # Instead of having to write this:
              #
              #     process.spawn lambda {
              #       ...
              #     }
396
              if arg_node.block_without_signature? && exp_arg.lambda?
397 398 399
                arg_node.infer_as_lambda
              end

400
              define_type(arg_node, scope, exp_arg)
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
            else
              define_type(arg_node, scope)
            end

          # When the expected argument is a rest type we need to compare
          # with/initialise the type of the individual rest values. For example,
          # for rest argument `*foo: X` the actual type of `foo` is `Array!(X)`,
          # but we want to compare with/initialise _just_ `X`.
          compare_with = rest ? type_of_rest_argument_value(exp_arg) : exp_arg

          unless given_arg.type_compatible?(compare_with, @state)
            return diagnostics.type_error(exp_arg, given_arg, arg_node.location)
          end

          compare_with.initialize_as(given_arg, method, source)
        end

        node.block_type = method
        return_type = method.resolved_return_type(source)

        remap_send_return_type(return_type, scope)
      end
423
      # rubocop: enable Metrics/AbcSize
424 425 426 427
      # rubocop: enable Metrics/BlockLength
      # rubocop: enable Metrics/PerceivedComplexity
      # rubocop: enable Metrics/CyclomaticComplexity

428 429 430 431 432 433 434 435 436
      def initialize_method_for_send(node, method, scope)
        given = node.type_arguments.length
        max = method.type_parameters.length

        if given > max
          return diagnostics.too_many_type_parameters(max, given, node.location)
        end

        type_args = node.type_arguments.map do |type_arg_node|
437
          define_type_instance(type_arg_node, scope)
438 439 440 441 442
        end

        method.new_instance_for_send(type_args)
      end

443
      def remap_send_return_type(type, scope)
444
        if (surrounding_method = scope.enclosing_method)
445 446 447 448 449 450 451 452
          type.remap_using_method_bounds(surrounding_method)
        else
          type
        end
      end

      def type_of_receiver(node, scope)
        if node.receiver
453
          receiver_type_for_send_with_receiver(node, scope)
454 455 456 457 458 459 460
        elsif scope.self_type.responds_to_message?(node.name)
          scope.self_type
        else
          scope.module_type
        end
      end

461 462 463
      def receiver_type_for_send_with_receiver(node, scope)
        if node.name == Config::NEW_MESSAGE
          define_type_instance(node.receiver, scope)
464 465
        elsif node.hash_map_literal?
          @state.module(Config::HASH_MAP_MODULE).type
466 467 468 469 470
        else
          define_type(node.receiver, scope)
        end
      end

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
      def type_of_rest_argument_value(type)
        param = type
          .lookup_type_parameter(Config::ARRAY_TYPE_PARAMETER)

        type.lookup_type_parameter_instance(param)
      end

      def on_body(node, scope)
        type =
          define_types(node.expressions, scope).last ||
          typedb.nil_type.new_instance

        block_type = scope.block_type

        block_type.return_type = type if block_type.infer_return_type
486 487
        expected_type =
          block_type.return_type.resolve_self_type(scope.self_type)
488

489
        if !type.void? && !type.type_compatible?(expected_type, @state)
490 491
          loc = node.location_of_last_expression

492
          diagnostics.return_type_error(expected_type, type, loc)
493 494 495 496 497 498 499 500 501 502 503 504 505
        end

        type
      end

      def on_return(node, scope)
        rtype =
          if node.value
            define_type(node.value, scope)
          else
            typedb.nil_type.new_instance
          end

506
        if (method = scope.enclosing_method)
507
          expected = method.return_type.resolve_self_type(scope.self_type)
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557

          unless rtype.type_compatible?(expected, @state)
            diagnostics
              .return_type_error(expected, rtype, node.value_location)
          end
        else
          diagnostics.return_outside_of_method_error(node.location)
        end

        # A "return" statement itself will never return a value. For example,
        # `let x = return 10` would never assign a value to `x`.
        TypeSystem::Void.new
      end

      def on_try(node, scope)
        define_type(node.expression, scope)

        if node.empty_else?
          on_try_without_else(node, scope)
        else
          on_try_with_else(node, scope)
        end
      end

      def on_try_without_else(node, scope)
        ret_type = node.expression.type
        curr_block = scope.block_type

        if (throw_type = node.throw_type)
          curr_block.throw_type = throw_type if curr_block.infer_throw_type?
        else
          diagnostics.redundant_try_warning(node.location)
        end

        ret_type
      end

      def on_try_with_else(node, scope)
        try_type = node.expression.type
        throw_type = node.throw_type || TypeSystem::Dynamic.new

        node.else_block_type = TypeSystem::Block.new(
          name: Config::ELSE_BLOCK_NAME,
          prototype: @state.typedb.block_type
        )

        else_scope = TypeScope.new(
          scope.self_type,
          node.else_block_type,
          @module,
558 559
          locals: node.else_body.locals,
          parent: scope
560 561
        )

562
        else_scope.define_receiver_type
563

564 565 566
        if (else_arg_name = node.else_argument_name)
          node.else_block_type.arguments.define(else_arg_name, throw_type)
          else_scope.locals.define(else_arg_name, throw_type)
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
        end

        else_type = define_type(node.else_body, else_scope)

        # If "try" returns X and "else" returns Nil then we want to infer the
        # type to a `?X`.
        if infer_try_as_optional?(try_type, else_type)
          node.type = try_type = TypeSystem::Optional.wrap(try_type)
        end

        if else_type.type_compatible?(try_type, @state)
          try_type
        else
          diagnostics.type_error(try_type, else_type, node.else_body.location)
        end
      end

      def infer_try_as_optional?(try_type, else_type)
585 586 587 588 589 590 591 592
        nil_type = typedb.nil_type

        if try_type.type_instance_of?(nil_type) &&
           else_type.type_instance_of?(nil_type)
          return false
        end

        !try_type.optional? && else_type.type_instance_of?(nil_type)
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
      end

      def on_throw(node, scope)
        type = define_type(node.value, scope)

        scope.block_type.throw_type = type if scope.block_type.infer_throw_type?

        TypeSystem::Void.new
      end

      def on_object(node, scope)
        type = typedb.new_object_type(node.name)

        define_object_name_attribute(type)
        define_named_type(node, type, scope)
      end

      def on_trait(node, scope)
        if (existing = scope.lookup_type(node.name))
          extend_trait(existing, node, scope)
        else
          type = typedb.new_trait_type(node.name)

          define_object_name_attribute(type)
          define_required_traits(node, type, scope)
          define_named_type(node, type, scope)
        end
      end

      def extend_trait(trait, node, scope)
        unless trait.empty?
          return diagnostics.extend_trait_error(trait, node.location)
        end

        return TypeSystem::Error.new unless same_type_parameters?(node, trait)

        node.redefines = true

        define_required_traits(node, trait, scope)

633
        body_type = TypeSystem::Block.closure(typedb.block_type)
634

635 636
        body_scope = TypeScope
          .new(trait, body_type, @module, locals: node.body.locals)
637

638
        body_scope.define_receiver_type
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662

        node.block_type = body_type

        define_type(node.body, body_scope)

        trait
      end

      def define_object_name_attribute(type)
        type.define_attribute(
          Config::OBJECT_NAME_INSTANCE_ATTRIBUTE,
          typedb.string_type.new_instance
        )
      end

      def define_required_traits(node, trait, scope)
        node.required_traits.each do |req_node|
          req = define_type_instance(req_node, scope)

          trait.add_required_trait(req) unless req.error?
        end
      end

      def define_named_type(node, new_type, scope)
663
        body_type = TypeSystem::Block.closure(typedb.block_type)
664 665

        body_scope = TypeScope
666
          .new(new_type, body_type, @module, locals: node.body.locals)
667

668
        body_scope.define_receiver_type
669 670 671 672 673

        node.block_type = body_type

        define_types(node.type_parameters, body_scope)
        store_type(new_type, scope, node.location)
674 675 676 677 678

        if node.object?
          add_object_trait_implementations(new_type, node, body_scope)
        end

679 680
        define_type(node.body, body_scope)

681 682
        verify_object_trait_implementations(new_type, node) if node.object?

683 684 685
        new_type
      end

686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
      def add_object_trait_implementations(object, node, body_scope)
        node.trait_implementations.each do |trait_name_node|
          trait_type = define_type(trait_name_node, body_scope)

          object.implement_trait(trait_type) unless trait_type.error?
        end
      end

      def verify_object_trait_implementations(object, node)
        node.trait_implementations.each do |trait_name|
          trait = trait_name.type

          next if trait.error?
          next if trait_requirements_met?(object, trait, trait_name.location)

          object.remove_trait_implementation(trait)
        end
      end

705 706 707 708 709 710 711 712 713 714 715 716
      def on_reopen_object(node, scope)
        type = on_type_name_reference(node.name, scope)

        return type if type.error?

        unless type.object?
          return diagnostics.reopen_invalid_object_error(
            node.name.qualified_name,
            node.location
          )
        end

717
        block_type = TypeSystem::Block.closure(typedb.block_type)
718 719

        new_scope = TypeScope
720
          .new(type, block_type, @module, locals: node.body.locals)
721

722
        new_scope.define_receiver_type
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741

        node.block_type = block_type

        define_type(node.body, new_scope)

        type
      end

      def on_trait_implementation(node, scope)
        object = on_type_name_reference(node.object_name, scope)

        return object if object.error?

        # The trait name has to be looked up in the context of the
        # implementation. This ensures that a Self type refers to the type
        # that the trait is implemented for, instead of referring to the type of
        # the outer scope.
        impl_block = TypeSystem::Block.closure(typedb.block_type)
        impl_scope = TypeScope
742
          .new(object, impl_block, @module, locals: node.body.locals)
743

744
        impl_scope.define_receiver_type
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784

        trait = define_type(node.trait_name, impl_scope)

        return trait if trait.error?

        object.implement_trait(trait)

        node.block_type = impl_block

        define_type(node.body, impl_scope)

        if trait_requirements_met?(object, trait, node.location)
          trait
        else
          object.remove_trait_implementation(trait)

          TypeSystem::Error.new
        end
      end

      def trait_requirements_met?(object, trait, location)
        required_traits_implemented?(object, trait, location) &&
          required_methods_implemented?(object, trait, location)
      end

      def required_traits_implemented?(object, trait, location)
        trait.required_trait_types.all? do |required|
          if object.implements_trait?(required)
            true
          else
            diagnostics
              .uninplemented_trait_error(trait, object, required, location)

            false
          end
        end
      end

      def required_methods_implemented?(object, trait, location)
        trait.required_methods.all? do |required|
785
          req_method = required.type.with_type_parameter_instances_from([trait])
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800

          if object.implements_method?(req_method, @state)
            true
          else
            diagnostics
              .unimplemented_method_error(req_method, object, location)

            false
          end
        end
      end

      def on_method(node, scope)
        type = TypeSystem::Block.named_method(node.name, typedb.block_type)

801 802 803 804 805 806
        new_scope = TypeScope.new(
          scope.self_type.new_instance,
          type,
          @module,
          locals: node.body.locals
        )
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

        define_method_bounds(node, new_scope)
        define_block_signature(node, new_scope)

        store_type(type, new_scope, node.location)

        @deferred_methods << DeferredMethod.new(node, new_scope)

        type
      end

      def on_required_method(node, scope)
        type = TypeSystem::Block.named_method(node.name, typedb.block_type)

        new_scope = TypeScope
822
          .new(scope.self_type, type, @module, locals: node.body.locals)
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855

        define_block_signature(node, new_scope)

        if scope.self_type.trait?
          scope.self_type.define_required_method(type)
        else
          diagnostics.define_required_method_on_non_trait_error(node.location)
        end

        type
      end

      def on_deferred_method(node, scope)
        define_type(node.body, scope)
      end

      def store_type(type, scope, location)
        scope.self_type.define_attribute(type.name, type)

        store_type_as_global(type.name, type, scope, location)
      end

      def store_type_as_global(name, type, scope, location)
        if Config::RESERVED_CONSTANTS.include?(name)
          diagnostics.redefine_reserved_constant_error(name, location)
        elsif scope.module_scope?
          @module.globals.define(name, type)
        end
      end

      def on_block(node, scope, expected_block = nil)
        block_type = TypeSystem::Block.closure(typedb.block_type)
        locals = node.body.locals
856 857 858 859 860 861 862 863

        new_scope = TypeScope.new(
          scope.self_type,
          block_type,
          @module,
          locals: locals,
          parent: scope
        )
864 865 866 867 868 869 870 871 872

        define_block_signature(node, new_scope, expected_block)
        define_type(node.body, new_scope)

        block_type
      end

      def on_lambda(node, scope, expected_block = nil)
        block_type = TypeSystem::Block.lambda(typedb.block_type)
873
        new_scope = TypeScope.new(
874
          @module.type,
875 876 877 878 879
          block_type,
          @module,
          locals: node.body.locals,
          enclosing_method: scope.enclosing_method
        )
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895

        define_block_signature(node, new_scope, expected_block)
        define_type(node.body, new_scope)

        block_type
      end

      def on_define_variable(node, scope)
        vtype = define_type(node.value, scope)
        callback = node.variable.define_variable_visitor_method

        public_send(callback, node.variable, vtype, scope, node.mutable?)
      end

      def on_define_variable_with_explicit_type(node, scope)
        vtype = define_type(node.value, scope)
896
        exp_type = define_type_instance(node.value_type, scope)
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
        callback = node.variable.define_variable_visitor_method

        vtype =
          if vtype.type_compatible?(exp_type, @state)
            exp_type
          else
            diagnostics.type_error(exp_type, vtype, node.location)
          end

        public_send(callback, node.variable, vtype, scope, node.mutable?)
      end

      def on_define_local(node, value_type, scope, mutable = false)
        name = node.name

        if scope.locals.defined?(name)
          value_type = diagnostics
            .redefine_existing_local_error(name, node.location)
        else
          scope.locals.define(name, value_type, mutable)
        end

        value_type
      end

      def on_define_attribute(node, value_type, scope, mutable = false)
        name = node.name

        unless scope.constructor?
          value_type = diagnostics
            .define_instance_attribute_error(name, node.location)
        end

        if scope.self_type.lookup_attribute(name).any?
          value_type = diagnostics
            .redefine_existing_attribute_error(name, node.location)
        else
          scope.self_type.define_attribute(name, value_type, mutable)
        end

        value_type
      end

      def on_define_constant(node, value_type, scope, _)
        name = node.name

        if scope.self_type.lookup_attribute(name).any?
          value_type = diagnostics
            .redefine_existing_constant_error(name, node.location)
        else
          scope.self_type.define_attribute(name, value_type)
        end

        store_type_as_global(name, value_type, scope, node.location)

        value_type
      end

      def on_reassign_variable(node, scope)
        callback = node.variable.reassign_variable_visitor_method
        value_type = define_type(node.value, scope)

        public_send(callback, node.variable, value_type, scope)
      end

      def on_reassign_local(node, value_type, scope)
        name = node.name
        _, existing = scope.locals.lookup_with_parent(name)

        unless existing.any?
          return diagnostics.reassign_undefined_local_error(name, node.location)
        end

        unless existing.mutable?
          diagnostics.reassign_immutable_local_error(name, node.location)
          return existing.type
        end

        unless value_type.type_compatible?(existing.type, @state)
          diagnostics.type_error(existing.type, value_type, node.location)
        end

        existing.type
      end

      def on_reassign_attribute(node, value_type, scope)
        name = node.name
        existing = scope.self_type.lookup_attribute(name)

        unless existing.any?
          return diagnostics
              .reassign_undefined_attribute_error(name, node.location)
        end

        unless existing.mutable?
          diagnostics.reassign_immutable_attribute_error(name, node.location)
          return existing.type
        end

        if value_type.type_compatible?(existing.type, @state)
          scope.self_type.reassign_attribute(name, value_type)
        else
          diagnostics.type_error(existing.type, value_type, node.location)
          existing.type
        end
      end

      def on_define_argument(arg_node, scope, default_type = nil)
        block_type = scope.block_type
        name = arg_node.name
        mutable = arg_node.mutable?

        vtype = type_for_argument_value(arg_node, scope)
        def_type = defined_type_for_argument(arg_node, scope)
        arg_type = determine_argument_type(
          arg_node,
          def_type,
          vtype,
          scope.block_type,
          default_type
        )

        symbol =
          if arg_node.default
            block_type.define_optional_argument(name, arg_type, mutable)
          elsif arg_node.rest?
            block_type.define_rest_argument(
              name,
              @state.typedb.new_array_of_type(arg_type),
              mutable
            )
          else
            block_type.define_required_argument(name, arg_type, mutable)
          end

        scope.locals.add_symbol(symbol)

        arg_type
      end

      def on_define_type_parameter(node, scope)
        traits = define_types(node.required_traits, scope)

        scope.self_type.define_type_parameter(node.name, traits)
      end

      def on_keyword_argument(node, scope)
        define_type(node.value, scope)
      end

      def on_type_cast(node, scope)
        to_cast = define_type(node.expression, scope)
        cast_to = define_type_instance(node.cast_to, scope)

        if to_cast.cast_to?(cast_to, @state)
          cast_to
        else
          diagnostics.invalid_cast_error(to_cast, cast_to, node.location)
        end
      end

      def on_global(node, _)
        if (symbol = @module.globals[node.name]) && symbol.any?
          symbol.type
        else
          diagnostics.undefined_constant_error(node.name, node.location)
        end
      end

      def on_dereference(node, scope)
        type = define_type(node.expression, scope)

        if type.dereference?
          type.dereferenced_type
        else
          diagnostics.dereference_error(type, node.location)
          type
        end
      end

      def on_raw_instruction(node, scope)
        callback = node.raw_instruction_visitor_method

        define_types(node.arguments, scope)

        if respond_to?(callback)
          public_send(callback, node, scope)
        else
          diagnostics.unknown_raw_instruction_error(node.name, node.location)

          TypeSystem::Error.new
        end
      end

      def on_raw_get_toplevel(*)
1092
        typedb.top_level.new_instance
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
      end

      def on_raw_set_prototype(node, _)
        node.arguments.fetch(1).type
      end

      def on_raw_set_attribute(node, *)
        node.arguments.fetch(2).type
      end

      def on_raw_set_attribute_to_object(*)
        typedb.new_empty_object.new_instance
      end

      def on_raw_get_attribute(node, *)
        object = node.arguments.fetch(0).type
        name = node.arguments.fetch(1)

        if name.string?
          object.lookup_attribute(name.value).type
        else
          TypeSystem::Dynamic.new
        end
      end

      def on_raw_set_object(node, *)
        if (proto = node.arguments[1]&.type)
          proto = proto.type if proto.optional?

          proto.new_instance
        else
          typedb.new_empty_object
        end
      end

      def on_raw_object_equals(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_copy_blocks(*)
        TypeSystem::Void.new
      end

      def on_raw_integer_to_string(*)
        typedb.string_type.new_instance
      end

      def on_raw_integer_to_float(*)
        typedb.float_type.new_instance
      end

      def on_raw_integer_add(*)
        typedb.integer_type.new_instance
      end

      def on_raw_integer_div(*)
        typedb.integer_type.new_instance
      end

      def on_raw_integer_mul(*)
        typedb.integer_type.new_instance
      end

      def on_raw_integer_sub(*)
        typedb.integer_type.new_instance
      end

      def on_raw_integer_mod(*)
        typedb.integer_type.new_instance
      end

      def on_raw_integer_bitwise_and(*)
        typedb.integer_type.new_instance
      end

      def on_raw_integer_bitwise_or(*)
        typedb.integer_type.new_instance
      end

      def on_raw_integer_bitwise_xor(*)
        typedb.integer_type.new_instance
      end

      def on_raw_integer_shift_left(*)
        typedb.integer_type.new_instance
      end

      def on_raw_integer_shift_right(*)
        typedb.integer_type.new_instance
      end

      def on_raw_integer_smaller(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_integer_greater(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_integer_equals(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_integer_greater_or_equal(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_integer_smaller_or_equal(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_float_to_string(*)
        typedb.string_type.new_instance
      end

      def on_raw_float_to_integer(*)
        typedb.integer_type.new_instance
      end

      def on_raw_float_add(*)
        typedb.float_type.new_instance
      end

      def on_raw_float_div(*)
        typedb.float_type.new_instance
      end

      def on_raw_float_mul(*)
        typedb.float_type.new_instance
      end

      def on_raw_float_sub(*)
        typedb.float_type.new_instance
      end

      def on_raw_float_mod(*)
        typedb.float_type.new_instance
      end

      def on_raw_float_smaller(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_float_greater(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_float_equals(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_float_greater_or_equal(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_float_smaller_or_equal(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_float_is_nan(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_float_is_infinite(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_float_ceil(*)
        typedb.float_type.new_instance
      end

      def on_raw_float_floor(*)
        typedb.float_type.new_instance
      end

      def on_raw_float_round(*)
        typedb.float_type.new_instance
      end

      def on_raw_stdout_write(*)
        typedb.integer_type.new_instance
      end

      def on_raw_get_true(*)
        typedb.true_type.new_instance
      end

      def on_raw_get_false(*)
        typedb.false_type.new_instance
      end

      def on_raw_get_nil(*)
        typedb.nil_type.new_instance
      end

      def on_raw_run_block(*)
        TypeSystem::Dynamic.new
      end

      def on_raw_get_string_prototype(*)
        typedb.string_type.new_instance
      end

      def on_raw_get_integer_prototype(*)
        typedb.integer_type.new_instance
      end

      def on_raw_get_float_prototype(*)
        typedb.float_type.new_instance
      end

      def on_raw_get_object_prototype(*)
        typedb.object_type.new_instance
      end

      def on_raw_get_array_prototype(*)
        typedb.array_type.new_instance
      end

      def on_raw_get_block_prototype(*)
        typedb.block_type.new_instance
      end

      def optional_array_element_value(array)
        param = array.lookup_type_parameter(Config::ARRAY_TYPE_PARAMETER)
        type = array.lookup_type_parameter_instance(param) || param

        TypeSystem::Optional.wrap(type)
      end

      def on_raw_array_length(*)
        typedb.integer_type.new_instance
      end

      def on_raw_array_at(node, _)
        optional_array_element_value(node.arguments.fetch(0).type)
      end

      def on_raw_array_set(node, _)
        node.arguments.fetch(2).type
      end

      def on_raw_array_clear(*)
        TypeSystem::Void.new
      end

      def on_raw_array_remove(node, _)
        optional_array_element_value(node.arguments.fetch(0).type)
      end

      def on_raw_time_monotonic(*)
        typedb.float_type.new_instance
      end

      def on_raw_time_system(*)
        typedb.float_type.new_instance
      end

      def on_raw_time_system_offset(*)
        typedb.integer_type.new_instance
      end

      def on_raw_time_system_dst(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_string_to_upper(*)
        typedb.string_type.new_instance
      end

      def on_raw_string_to_lower(*)
        typedb.string_type.new_instance
      end

1367 1368
      def on_raw_string_to_byte_array(*)
        TypeSystem::Dynamic.new
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
      end

      def on_raw_string_size(*)
        typedb.integer_type.new_instance
      end

      def on_raw_string_length(*)
        typedb.integer_type.new_instance
      end

      def on_raw_string_equals(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_string_concat(*)
        typedb.string_type.new_instance
      end

      def on_raw_string_slice(*)
        typedb.string_type.new_instance
      end

      def on_raw_stdin_read(*)
1392
        typedb.integer_type.new_instance
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
      end

      def on_raw_stderr_write(*)
        typedb.integer_type.new_instance
      end

      def on_raw_process_spawn(*)
        typedb.integer_type.new_instance
      end

      def on_raw_process_send_message(node, _)
        node.arguments.fetch(1).type
      end

      def on_raw_process_receive_message(*)
        TypeSystem::Dynamic.new
      end

      def on_raw_process_current_pid(*)
        typedb.integer_type.new_instance
      end

      def on_raw_process_suspend_current(*)
        TypeSystem::Void.new
      end

      def on_raw_process_terminate_current(*)
        TypeSystem::Void.new
      end

      def on_raw_remove_attribute(node, _)
        object = node.arguments.fetch(0).type
        name = node.arguments.fetch(1)

        if name.string?
          object.lookup_attribute(name.value).type
        else
          TypeSystem::Dynamic.new
        end
      end

      def on_raw_get_prototype(*)
1435
        typedb.object_type.new_instance
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
      end

      def on_raw_get_attribute_names(*)
        typedb.new_array_of_type(typedb.string_type.new_instance)
      end

      def on_raw_attribute_exists(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_file_flush(*)
        typedb.nil_type.new_instance
      end

      def on_raw_stdout_flush(*)
        typedb.nil_type.new_instance
      end

      def on_raw_stderr_flush(*)
        typedb.nil_type.new_instance
      end

      def on_raw_file_open(*)
1459
        TypeSystem::Dynamic.new
1460 1461 1462
      end

      def on_raw_file_read(*)
1463
        typedb.integer_type.new_instance
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
      end

      def on_raw_file_seek(*)
        typedb.integer_type.new_instance
      end

      def on_raw_file_size(*)
        typedb.integer_type.new_instance
      end

      def on_raw_file_write(*)
        typedb.integer_type.new_instance
      end

      def on_raw_file_remove(*)
        typedb.nil_type.new_instance
      end

      def on_raw_file_copy(*)
        typedb.integer_type.new_instance
      end

      def on_raw_file_type(*)
        typedb.integer_type.new_instance
      end

      def on_raw_file_time(*)
        typedb.integer_type.new_instance
      end

      def on_raw_directory_create(*)
        typedb.nil_type.new_instance
      end

      def on_raw_directory_remove(*)
        typedb.nil_type.new_instance
      end

      def on_raw_directory_list(*)
        typedb.new_array_of_type(typedb.string_type.new_instance)
      end

      def on_raw_drop(*)
        typedb.nil_type.new_instance
      end

1510
      def on_raw_set_blocking(*)
1511
        typedb.boolean_type.new_instance
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
      end

      def on_raw_panic(*)
        TypeSystem::Void.new
      end

      def on_raw_exit(*)
        TypeSystem::Void.new
      end

      def on_raw_platform(*)
1523
        typedb.string_type.new_instance
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
      end

      def on_raw_hasher_new(*)
        typedb.hasher_type.new_instance
      end

      def on_raw_hasher_write(*)
        typedb.nil_type.new_instance
      end

      def on_raw_hasher_finish(*)
        typedb.integer_type.new_instance
      end

      def on_raw_stacktrace(*)
        tuple = typedb.new_array_of_type(TypeSystem::Dynamic.new)

        typedb.new_array_of_type(tuple)
      end

1544 1545 1546 1547
      def on_raw_block_metadata(*)
        TypeSystem::Dynamic.new
      end

Yorick Peterse's avatar
Yorick Peterse committed
1548 1549 1550 1551
      def on_raw_string_format_debug(*)
        typedb.string_type.new_instance
      end

1552 1553 1554 1555
      def on_raw_string_concat_multiple(*)
        typedb.string_type.new_instance
      end

1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
      def on_raw_byte_array_from_array(*)
        TypeSystem::Dynamic.new
      end

      def on_raw_byte_array_set(*)
        typedb.integer_type.new_instance
      end

      def on_raw_byte_array_at(*)
        typedb.integer_type.new_instance
      end

      def on_raw_byte_array_remove(*)
        typedb.integer_type.new_instance
      end

      def on_raw_byte_array_length(*)
        typedb.integer_type.new_instance
      end

      def on_raw_byte_array_clear(*)
        TypeSystem::Void.new
      end

      def on_raw_byte_array_equals(*)
        typedb.boolean_type.new_instance
      end

      def on_raw_byte_array_to_string(*)
        typedb.string_type.new_instance
      end

1588 1589 1590 1591
      def on_raw_get_boolean_prototype(*)
        typedb.boolean_type.new_instance
      end

1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
      def on_raw_get_read_only_file_prototype(*)
        typedb.read_only_file_type.new_instance
      end

      def on_raw_get_write_only_file_prototype(*)
        typedb.write_only_file_type.new_instance
      end

      def on_raw_get_read_write_file_prototype(*)
        typedb.read_write_file_type.new_instance
      end

      def on_raw_get_byte_array_prototype(*)
        typedb.byte_array_type.new_instance
      end

      def on_raw_get_hasher_prototype(*)
        typedb.hasher_type.new_instance
      end

1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
      def on_raw_get_library_prototype(*)
        typedb.library_type.new_instance
      end

      def on_raw_get_function_prototype(*)
        typedb.function_type.new_instance
      end

      def on_raw_get_pointer_prototype(*)
        typedb.pointer_type.new_instance
      end

1624 1625 1626 1627
      def on_raw_set_object_name(*)
        typedb.string_type.new_instance
      end

1628 1629 1630 1631
      def on_raw_current_file_path(*)
        typedb.string_type.new_instance
      end

1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
      def on_raw_env_get(*)
        TypeSystem::Optional.new(typedb.string_type.new_instance)
      end

      def on_raw_env_set(*)
        typedb.string_type.new_instance
      end

      def on_raw_env_remove(*)
        typedb.nil_type.new_instance
      end

      def on_raw_env_variables(*)
        typedb.new_array_of_type(typedb.string_type.new_instance)
      end

      def on_raw_env_home_directory(*)
        TypeSystem::Optional.new(typedb.string_type.new_instance)
      end

      def on_raw_env_temp_directory(*)
        typedb.string_type.new_instance
      end

      def on_raw_env_get_working_directory(*)
        typedb.string_type.new_instance
      end

      def on_raw_env_set_working_directory(*)
        typedb.string_type.new_instance
      end

      def on_raw_env_arguments(*)
        typedb.new_array_of_type(typedb.string_type.new_instance)
      end

1668 1669 1670 1671
      def on_raw_process_set_panic_handler(*)
        typedb.block_type.new_instance
      end

1672 1673 1674 1675 1676 1677 1678 1679
      def on_raw_process_add_defer_to_caller(*)
        TypeSystem::Block.closure(typedb.block_type)
      end

      def on_raw_set_default_panic_handler(*)
        TypeSystem::Block.lambda(typedb.block_type)
      end

1680
      def on_raw_process_pin_thread(*)
1681
        typedb.boolean_type.new_instance
1682 1683 1684 1685 1686 1687
      end

      def on_raw_process_unpin_thread(*)
        typedb.nil_type.new_instance
      end

1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
      def on_raw_library_open(*)
        typedb.library_type.new_instance
      end

      def on_raw_function_attach(*)
        typedb.function_type.new_instance
      end

      def on_raw_function_call(*)
        TypeSystem::Dynamic.new
      end

      def on_raw_pointer_attach(*)
        typedb.pointer_type.new_instance
      end

      def on_raw_pointer_read(*)
        TypeSystem::Dynamic.new
      end

      def on_raw_pointer_write(*)
        TypeSystem::Dynamic.new
      end

      def on_raw_pointer_from_address(*)
        typedb.pointer_type.new_instance
      end

      def on_raw_pointer_address(*)
        typedb.integer_type.new_instance
      end

      def on_raw_foreign_type_size(*)
        typedb.integer_type.new_instance
      end

      def on_raw_foreign_type_alignment(*)
        typedb.integer_type.new_instance
      end

1728 1729 1730 1731 1732 1733 1734 1735
      def on_raw_string_to_integer(*)
        typedb.integer_type.new_instance
      end

      def on_raw_string_to_float(*)
        typedb.float_type.new_instance
      end

1736 1737 1738 1739
      def on_raw_float_to_bits(*)
        typedb.integer_type.new_instance
      end

1740 1741 1742 1743 1744 1745
      def define_block_signature(node, scope, expected_block = nil)
        define_type_parameters(node, scope)
        define_argument_types(node, scope, expected_block)
        define_throw_type(node, scope)
        define_return_type(node, scope)

1746
        scope.define_receiver_type
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
        scope.block_type.define_call_method
      end

      def define_method_bounds(node, scope)
        stype = scope.self_type

        node.method_bounds.each do |bound|
          name = bound.name

          if (param = stype.lookup_type_parameter(name))
            required_traits =
              param.required_traits +
              define_types(bound.required_traits, scope)

            scope
              .block_type
              .method_bounds
              .define(name, required_traits)
          else
            diagnostics
              .undefined_type_parameter_error(stype, name, bound.location)
          end
        end
      end

      def define_type_parameters(node, scope)
        node.type_parameters.each do |param_node|
          requirements = required_traits_for_type_parameter(param_node, scope)

          scope.block_type.define_type_parameter(param_node.name, requirements)
        end
      end

      # Returns an Array containing the traits required by a type parameter.
      def required_traits_for_type_parameter(node, scope)
        requirements = []

        node.required_traits.each do |req_node|
          type = define_type(req_node, scope)

          if type&.trait?
            requirements << type
          elsif type
            diagnostics
              .invalid_type_parameter_requirement(type, req_node.location)
          else
            diagnostics.undefined_constant_error(name, req_node.location)
          end
        end

        requirements
      end

      def define_argument_types(node, scope, expected_block = nil)
        if expected_block
          define_argument_types_with_expected_block(node, scope, expected_block)
        else
          define_argument_types_without_expected_block(node, scope)
        end
      end

      def define_argument_types_without_expected_block(node, scope)
        define_types(node.arguments, scope)
      end

      def define_argument_types_with_expected_block(node, scope, expected_block)
1813
        expected_args = expected_block.arguments
1814 1815

        node.arguments.zip(expected_args) do |arg_node, exp_arg|
1816 1817 1818
          expected_type = exp_arg
            .type
            .resolve_type_parameters(scope.self_type, expected_block)
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834

          define_type(arg_node, scope, expected_type)
        end
      end

      def define_throw_type(node, scope)
        return unless node.throws

        scope.block_type.throw_type = define_type_instance(node.throws, scope)
      end

      def define_return_type(node, scope)
        return unless node.returns

        scope.block_type.infer_return_type = false

1835 1836
        node.returns.late_binding = true

1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
        scope.block_type.return_type =
          define_type_instance(node.returns, scope)
      end

      # Returns the type of an argument's default value, if any.
      def type_for_argument_value(arg_node, scope)
        define_type_instance(arg_node.default, scope) if arg_node.default
      end

      # Returns the type for an explicitly defined argument type, if any.
      def defined_type_for_argument(arg_node, scope)
        define_type_instance(arg_node.value_type, scope) if arg_node.value_type
      end

      # Determines which type to use for an argument.
      #
      # Given the explicitly defined type (if any) and the type of the default
      # value (if any) this method will determine which of the two should be
      # used. If neither are given the Dynamic type is used.
      #
      # rubocop: disable Metrics/CyclomaticComplexity
      # rubocop: disable Metrics/PerceivedComplexity
      def determine_argument_type(
        node,
        defined_type,
        value_type,
        block_type,
        default_type = nil
      )
        type =
          if defined_type && value_type
            unless value_type.type_compatible?(defined_type, @state)
              diagnostics
                .type_error(defined_type, value_type, node.default.location)
            end

            defined_type
          elsif defined_type
            defined_type
          elsif value_type
            value_type
          else
            default_type || TypeSystem::Dynamic.new
          end

        type.remap_using_method_bounds(block_type)
      end
      # rubocop: enable Metrics/PerceivedComplexity
      # rubocop: enable Metrics/CyclomaticComplexity

      def wrap_optional_type(node, type)
        if node.optional?
          TypeSystem::Optional.wrap(type)
        else
          type
        end
      end
    end
    # rubocop: enable Metrics/ClassLength
  end
end