Class: RBS::DefinitionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/definition_builder.rb

Defined Under Namespace

Classes: OneAncestors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:) ⇒ DefinitionBuilder

Returns a new instance of DefinitionBuilder.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rbs/definition_builder.rb', line 90

def initialize(env:)
  @env = env
  @type_name_resolver = TypeNameResolver.from_env(env)

  @instance_cache = {}
  @singleton_cache = {}
  @interface_cache = {}

  @one_instance_cache = {}
  @one_singleton_cache = {}

  @instance_ancestors_cache = {}
  @singleton_ancestor_cache = {}

  @one_instance_ancestors_cache = {}
  @one_singleton_ancestors_cache = {}
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



3
4
5
# File 'lib/rbs/definition_builder.rb', line 3

def env
  @env
end

#instance_ancestors_cacheObject (readonly)

Returns the value of attribute instance_ancestors_cache.



13
14
15
# File 'lib/rbs/definition_builder.rb', line 13

def instance_ancestors_cache
  @instance_ancestors_cache
end

#instance_cacheObject (readonly)

Returns the value of attribute instance_cache.



6
7
8
# File 'lib/rbs/definition_builder.rb', line 6

def instance_cache
  @instance_cache
end

#interface_cacheObject (readonly)

Returns the value of attribute interface_cache.



8
9
10
# File 'lib/rbs/definition_builder.rb', line 8

def interface_cache
  @interface_cache
end

#one_instance_ancestors_cacheObject (readonly)

Returns the value of attribute one_instance_ancestors_cache.



16
17
18
# File 'lib/rbs/definition_builder.rb', line 16

def one_instance_ancestors_cache
  @one_instance_ancestors_cache
end

#one_instance_cacheObject (readonly)

Returns the value of attribute one_instance_cache.



10
11
12
# File 'lib/rbs/definition_builder.rb', line 10

def one_instance_cache
  @one_instance_cache
end

#one_singleton_ancestors_cacheObject (readonly)

Returns the value of attribute one_singleton_ancestors_cache.



17
18
19
# File 'lib/rbs/definition_builder.rb', line 17

def one_singleton_ancestors_cache
  @one_singleton_ancestors_cache
end

#one_singleton_cacheObject (readonly)

Returns the value of attribute one_singleton_cache.



11
12
13
# File 'lib/rbs/definition_builder.rb', line 11

def one_singleton_cache
  @one_singleton_cache
end

#singleton_ancestor_cacheObject (readonly)

Returns the value of attribute singleton_ancestor_cache.



14
15
16
# File 'lib/rbs/definition_builder.rb', line 14

def singleton_ancestor_cache
  @singleton_ancestor_cache
end

#singleton_cacheObject (readonly)

Returns the value of attribute singleton_cache.



7
8
9
# File 'lib/rbs/definition_builder.rb', line 7

def singleton_cache
  @singleton_cache
end

#type_name_resolverObject (readonly)

Returns the value of attribute type_name_resolver.



4
5
6
# File 'lib/rbs/definition_builder.rb', line 4

def type_name_resolver
  @type_name_resolver
end

Instance Method Details

#build_instance(type_name) ⇒ Object



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/rbs/definition_builder.rb', line 402

def build_instance(type_name)
  try_cache type_name, cache: instance_cache do
    entry = env.class_decls[type_name] or raise "Unknown name for build_instance: #{type_name}"
    ensure_namespace!(type_name.namespace, location: entry.decls[0].decl.location)

    case entry
    when Environment::ClassEntry, Environment::ModuleEntry
      ancestors = instance_ancestors(type_name)
      self_type = Types::ClassInstance.new(name: type_name,
                                           args: Types::Variable.build(entry.type_params.each.map(&:name)),
                                           location: nil)

      definition_pairs = ancestors.ancestors.map do |ancestor|
        # @type block: [Definition::Ancestor::t, Definition]
        case ancestor
        when Definition::Ancestor::Instance
          [ancestor, build_one_instance(ancestor.name)]
        when Definition::Ancestor::Singleton
          [ancestor, build_one_singleton(ancestor.name)]
        end
      end

      case entry
      when Environment::ModuleEntry
        entry.self_types.each do |module_self|
          ancestor = Definition::Ancestor::Instance.new(name: module_self.name, args: module_self.args)
          definition_pairs.push(
            [
              ancestor,
              if module_self.name.interface?
                build_interface(module_self.name)
              else
                build_instance(module_self.name)
              end
            ]
          )
        end
      end

      merge_definitions(type_name, definition_pairs, entry: entry, self_type: self_type, ancestors: ancestors)
    end
  end
end

#build_interface(type_name) ⇒ Object



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
1092
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
# File 'lib/rbs/definition_builder.rb', line 1057

def build_interface(type_name)
  try_cache(type_name, cache: interface_cache) do
    entry = env.interface_decls[type_name] or raise "Unknown name for build_interface: #{type_name}"
    declaration = entry.decl
    ensure_namespace!(type_name.namespace, location: declaration.location)

    self_type = Types::Interface.new(
      name: type_name,
      args: Types::Variable.build(declaration.type_params.each.map(&:name)),
      location: nil
    )

    Definition.new(type_name: type_name, entry: entry, self_type: self_type, ancestors: nil).tap do |definition|
      include_members = []
      def_members = []
      alias_members = []

      declaration.members.each do |member|
        case member
        when AST::Members::Include
          include_members << member
        when AST::Members::MethodDefinition
          def_members << member
        when AST::Members::Alias
          alias_members << member
        end
      end

      include_members.each do |member|
        NoMixinFoundError.check!(member.name, env: env, member: member)

        mixin = build_interface(member.name)

        args = member.args
        # @type var interface_entry: Environment::SingleEntry[TypeName, AST::Declarations::Interface]
        interface_entry = _ = mixin.entry
        type_params = interface_entry.decl.type_params

        InvalidTypeApplicationError.check!(
          type_name: type_name,
          args: args,
          params: type_params.each.map(&:name),
          location: member.location
        )

        sub = Substitution.build(type_params.each.map(&:name), args)
        mixin.methods.each do |name, method|
          definition.methods[name] = method.sub(sub)
        end
      end

      def_members.each do |member|
        DuplicatedMethodDefinitionError.check!(
          decl: declaration,
          methods: definition.methods,
          name: member.name,
          location: member.location
        )

        method = Definition::Method.new(
          super_method: nil,
          defs: member.types.map do |method_type|
            Definition::Method::TypeDef.new(
              type: method_type,
              member: member,
              defined_in: type_name,
              implemented_in: nil
            )
          end,
          accessibility: :public
        )
        definition.methods[member.name] = method
      end

      alias_members.each do |member|
        UnknownMethodAliasError.check!(
          methods: definition.methods,
          original_name: member.old_name,
          aliased_name: member.new_name,
          location: member.location
        )

        DuplicatedMethodDefinitionError.check!(
          decl: declaration,
          methods: definition.methods,
          name: member.new_name,
          location: member.location
        )

        # FIXME: may cause a problem if #old_name has super type
        definition.methods[member.new_name] = definition.methods[member.old_name]
      end
    end
  end
end

#build_one_instance(type_name) ⇒ Object



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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/rbs/definition_builder.rb', line 598

def build_one_instance(type_name)
  try_cache(type_name, cache: one_instance_cache) do
    entry = env.class_decls[type_name]

    self_type = Types::ClassInstance.new(name: type_name,
                                         args: Types::Variable.build(entry.type_params.each.map(&:name)),
                                         location: nil)
    ancestors = [Definition::Ancestor::Instance.new(name: type_name, args: self_type.args)]

    Definition.new(type_name: type_name, entry: entry, self_type: self_type, ancestors: ancestors).tap do |definition|
      method_definition_members(type_name, entry, kind: :instance).each do |method_name, array|
        visibility, method_def, primary_member, overload_members = array

        members = if primary_member
          [primary_member, *overload_members]
        else
          overload_members
        end

        m = if method_def
              Definition::Method.new(
                super_method: nil,
                accessibility: visibility,
                defs: method_def.defs.map {|defn| defn.update(implemented_in: type_name) }
              )
            else
              Definition::Method.new(
                super_method: nil,
                accessibility: visibility,
                defs: []
              )
            end

        definition.methods[method_name] = members.inject(m) do |original, member|
          defs = member.types.map do |method_type|
            Definition::Method::TypeDef.new(
              type: method_type,
              member: member,
              implemented_in: type_name,
              defined_in: type_name
            )
          end

          Definition::Method.new(
            super_method: nil,
            defs: defs + original.defs,
            accessibility: original.accessibility
          )
        end
      end

      entry.decls.each do |d|
        each_member_with_accessibility(d.decl.members) do |member, accessibility|
          case member
          when AST::Members::AttrReader, AST::Members::AttrAccessor, AST::Members::AttrWriter
            name = member.name
            type = member.type

            ivar_name = case member.ivar_name
                        when false
                          nil
                        else
                          member.ivar_name || :"@#{member.name}"
                        end

            if member.is_a?(AST::Members::AttrReader) || member.is_a?(AST::Members::AttrAccessor)
              definition.methods[name] = Definition::Method.new(
                super_method: nil,
                defs: [
                  Definition::Method::TypeDef.new(
                    type: MethodType.new(
                      type_params: [],
                      type: Types::Function.empty(type),
                      block: nil,
                      location: nil
                    ),
                    member: member,
                    defined_in: type_name,
                    implemented_in: type_name
                  )
                ],
                accessibility: accessibility
              )
            end

            if member.is_a?(AST::Members::AttrWriter) || member.is_a?(AST::Members::AttrAccessor)
              definition.methods[:"#{name}="] = Definition::Method.new(
                super_method: nil,
                defs: [
                  Definition::Method::TypeDef.new(
                    type: MethodType.new(
                      type_params: [],
                      type: Types::Function.empty(type).update(
                        required_positionals: [Types::Function::Param.new(name: name, type: type)]
                      ),
                      block: nil,
                      location: nil
                    ),
                    member: member,
                    defined_in: type_name,
                    implemented_in: type_name
                  ),
                ],
                accessibility: accessibility
              )
            end

            if ivar_name
              definition.instance_variables[ivar_name] = Definition::Variable.new(
                parent_variable: nil,
                type: type,
                declared_in: type_name
              )
            end

          when AST::Members::InstanceVariable
            definition.instance_variables[member.name] = Definition::Variable.new(
              parent_variable: nil,
              type: member.type,
              declared_in: type_name
            )

          when AST::Members::ClassVariable
            definition.class_variables[member.name] = Definition::Variable.new(
              parent_variable: nil,
              type: member.type,
              declared_in: type_name
            )

          end
        end
      end

      entry.decls.each do |d|
        d.decl.members.each do |member|
          case member
          when AST::Members::Alias
            if member.instance?
              UnknownMethodAliasError.check!(
                methods: definition.methods,
                original_name: member.old_name,
                aliased_name: member.new_name,
                location: member.location
              )

              DuplicatedMethodDefinitionError.check!(
                decl: d.decl,
                methods: definition.methods,
                name: member.new_name,
                location: member.location
              )

              definition.methods[member.new_name] = definition.methods[member.old_name]
            end
          end
        end
      end

      entry.decls.each do |d|
        validate_parameter_variance(
          decl: d.decl,
          methods: definition.methods
        )
      end
    end
  end
end

#build_one_singleton(type_name) ⇒ Object



836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
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
# File 'lib/rbs/definition_builder.rb', line 836

def build_one_singleton(type_name)
  try_cache(type_name, cache: one_singleton_cache) do
    entry = env.class_decls[type_name]

    self_type = Types::ClassSingleton.new(name: type_name, location: nil)
    ancestors = [Definition::Ancestor::Singleton.new(name: type_name)]

    Definition.new(type_name: type_name, entry: entry, self_type: self_type, ancestors: ancestors).tap do |definition|
      method_definition_members(type_name, entry, kind: :singleton).each do |method_name, array|
        visibility, method_def, primary_member, overload_members = array

        members = if primary_member
          [primary_member, *overload_members]
        else
          overload_members
        end

        m = Definition::Method.new(
          super_method: nil,
          defs: method_def&.yield_self do |method_def|
            method_def.defs.map {|defn| defn.update(implemented_in: type_name) }
          end || [],
          accessibility: visibility
        )
        definition.methods[method_name] = members.inject(m) do |original, new|
          defs = new.types.map do |type|
            Definition::Method::TypeDef.new(
              type: type,
              member: new,
              defined_in: type_name,
              implemented_in: type_name
            )
          end
          Definition::Method.new(
            super_method: nil,
            defs: defs + original.defs,
            accessibility: original.accessibility
          )
        end
      end

      entry.decls.each do |d|
        d.decl.members.each do |member|
          case member
          when AST::Members::Alias
            if member.singleton?
              UnknownMethodAliasError.check!(
                methods: definition.methods,
                original_name: member.old_name,
                aliased_name: member.new_name,
                location: member.location
              )

              DuplicatedMethodDefinitionError.check!(
                decl: d.decl,
                methods: definition.methods,
                name: member.new_name,
                location: member.location
              )

              definition.methods[member.new_name] = definition.methods[member.old_name]
            end
          end
        end
      end

      unless definition.methods.key?(:new)
        instance = build_instance(type_name)
        initialize = instance.methods[:initialize]

        if initialize
          class_params = entry.type_params.each.map(&:name)

          initialize_defs = initialize.defs
          definition.methods[:new] = Definition::Method.new(
            super_method: nil,
            defs: initialize_defs.map do |initialize_def|
              method_type = initialize_def.type

              class_type_param_vars = Set.new(class_params)
              method_type_param_vars = Set.new(method_type.type_params)

              if class_type_param_vars.intersect?(method_type_param_vars)
                renamed_method_params = method_type.type_params.map do |name|
                  if class_type_param_vars.include?(name)
                    Types::Variable.fresh(name).name
                  else
                    name
                  end
                end
                method_params = class_params + renamed_method_params

                sub = Substitution.build(method_type.type_params, Types::Variable.build(renamed_method_params))
              else
                method_params = class_params + method_type.type_params
                sub = Substitution.build([], [])
              end

              method_type = method_type.map_type {|ty| ty.sub(sub) }
              method_type = method_type.update(
                type_params: method_params,
                type: method_type.type.with_return_type(Types::Bases::Instance.new(location: nil))
              )

              Definition::Method::TypeDef.new(
                type: method_type,
                member: initialize_def.member,
                defined_in: initialize_def.defined_in,
                implemented_in: initialize_def.implemented_in
              )
            end,
            accessibility: :public,
            annotations: [AST::Annotation.new(location: nil, string: "rbs:test:target")]
          )
        end
      end

      entry.decls.each do |d|
        each_member_with_accessibility(d.decl.members) do |member, _|
          case member
          when AST::Members::ClassInstanceVariable
            definition.instance_variables[member.name] = Definition::Variable.new(
              parent_variable: nil,
              type: member.type,
              declared_in: type_name
            )

          when AST::Members::ClassVariable
            definition.class_variables[member.name] = Definition::Variable.new(
              parent_variable: nil,
              type: member.type,
              declared_in: type_name
            )
          end
        end
      end
    end
  end
end

#build_singleton(type_name) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/rbs/definition_builder.rb', line 446

def build_singleton(type_name)
  try_cache type_name, cache: singleton_cache do
    entry = env.class_decls[type_name] or raise "Unknown name for build_singleton: #{type_name}"
    ensure_namespace!(type_name.namespace, location: entry.decls[0].decl.location)

    case entry
    when Environment::ClassEntry, Environment::ModuleEntry
      ancestors = singleton_ancestors(type_name)
      self_type = Types::ClassSingleton.new(name: type_name, location: nil)
      instance_type = Types::ClassInstance.new(
        name: type_name,
        args: Types::Variable.build(entry.type_params.each.map(&:name)),
        location: nil
      )

      definition_pairs = ancestors.ancestors.map do |ancestor|
        # @type block: [Definition::Ancestor::t, Definition]
        case ancestor
        when Definition::Ancestor::Instance
          [ancestor, build_one_instance(ancestor.name)]
        when Definition::Ancestor::Singleton
          definition = build_one_singleton(ancestor.name)
          definition = definition.sub(Substitution.build([], [], instance_type: instance_type))
          definition = definition.map_method_type do |method_type|
            s = Substitution.build(
              method_type.free_variables.to_a,
              method_type.free_variables.map { Types::Bases::Any.new(location: nil) }
            )
            method_type.sub(s)
          end

          [
            ancestor,
            definition
          ]
        end
      end

      merge_definitions(type_name, definition_pairs, entry: entry, self_type: self_type, ancestors: ancestors)
    end
  end
end

#each_member_with_accessibility(members, accessibility: :public) ⇒ Object



381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/rbs/definition_builder.rb', line 381

def each_member_with_accessibility(members, accessibility: :public)
  members.each do |member|
    case member
    when AST::Members::Public
      accessibility = :public
    when AST::Members::Private
      accessibility = :private
    else
      yield member, accessibility
    end
  end
end

#ensure_namespace!(namespace, location:) ⇒ Object



394
395
396
397
398
399
400
# File 'lib/rbs/definition_builder.rb', line 394

def ensure_namespace!(namespace, location:)
  namespace.ascend do |ns|
    unless ns.empty?
      NoTypeFoundError.check!(ns.to_type_name, env: env, location: location)
    end
  end
end

#expand_alias(type_name) ⇒ Object



1153
1154
1155
1156
1157
# File 'lib/rbs/definition_builder.rb', line 1153

def expand_alias(type_name)
  entry = env.alias_decls[type_name] or raise "Unknown name for expand_alias: #{type_name}"
  ensure_namespace!(type_name.namespace, location: entry.decl.location)
  entry.decl.type
end

#instance_ancestors(type_name, building_ancestors: []) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/rbs/definition_builder.rb', line 269

def instance_ancestors(type_name, building_ancestors: [])
  as = instance_ancestors_cache[type_name] and return as

  entry = env.class_decls[type_name] or raise "Unknown name for instance_ancestors: #{type_name}"
  params = entry.type_params.each.map(&:name)
  args = Types::Variable.build(params)
  self_ancestor = Definition::Ancestor::Instance.new(name: type_name, args: args)

  RecursiveAncestorError.check!(self_ancestor,
                                ancestors: building_ancestors,
                                location: entry.primary.decl.location)
  building_ancestors.push self_ancestor

  one_ancestors = one_instance_ancestors(type_name)

  ancestors = []

  case entry
  when Environment::ClassEntry
    if super_class = one_ancestors.super_class
      # @type var super_class: Definition::Ancestor::Instance
      super_name = super_class.name
      super_args = super_class.args

      super_ancestors = instance_ancestors(super_name, building_ancestors: building_ancestors)
      ancestors.unshift(*super_ancestors.apply(super_args, location: entry.primary.decl.location))
    end
  end

  if included_modules = one_ancestors.included_modules
    included_modules.each do |mod|
      if mod.name.class?
        name = mod.name
        arg_types = mod.args
        mod_ancestors = instance_ancestors(name, building_ancestors: building_ancestors)
        ancestors.unshift(*mod_ancestors.apply(arg_types, location: entry.primary.decl.location))
      end
    end
  end

  ancestors.unshift(self_ancestor)

  if prepended_modules = one_ancestors.prepended_modules
    prepended_modules.each do |mod|
      if mod.name.class?
        name = mod.name
        arg_types = mod.args
        mod_ancestors = instance_ancestors(name, building_ancestors: building_ancestors)
        ancestors.unshift(*mod_ancestors.apply(arg_types, location: entry.primary.decl.location))
      end
    end
  end

  building_ancestors.pop

  instance_ancestors_cache[type_name] = Definition::InstanceAncestors.new(
    type_name: type_name,
    params: params,
    ancestors: ancestors
  )
end

#merge_definitions(type_name, pairs, entry:, self_type:, ancestors:) ⇒ Object



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
# File 'lib/rbs/definition_builder.rb', line 976

def merge_definitions(type_name, pairs, entry:, self_type:, ancestors:)
  Definition.new(type_name: type_name, entry: entry, self_type: self_type, ancestors: ancestors).tap do |definition|
    pairs.reverse_each do |ancestor, current_definition|
      sub = case ancestor
            when Definition::Ancestor::Instance
              Substitution.build(current_definition.type_params, ancestor.args)
            when Definition::Ancestor::Singleton
              Substitution.build([], [])
            end

      # @type var kind: method_kind
      kind = case ancestor
             when Definition::Ancestor::Instance
               :instance
             when Definition::Ancestor::Singleton
               :singleton
             end

      current_definition.methods.each do |name, method|
        merge_method type_name, definition.methods, name, method, sub, kind: kind
      end

      current_definition.instance_variables.each do |name, variable|
        merge_variable definition.instance_variables, name, variable
      end

      current_definition.class_variables.each do |name, variable|
        merge_variable definition.class_variables, name, variable
      end
    end
  end
end

#merge_method(type_name, methods, name, method, sub, kind:) ⇒ Object



1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
# File 'lib/rbs/definition_builder.rb', line 1019

def merge_method(type_name, methods, name, method, sub, kind:)
  super_method = methods[name]

  defs = if method.defs.all? {|d| d.overload? }
           raise InvalidOverloadMethodError.new(type_name: type_name, method_name: name, kind: kind, members: method.members) unless super_method
           method.defs + super_method.defs
         else
           method.defs
         end

  methods[name] = Definition::Method.new(
    super_method: super_method,
    accessibility: method.accessibility,
    defs: sub.mapping.empty? ? defs : defs.map {|defn| defn.update(type: defn.type.sub(sub)) }
  )
end

#merge_variable(variables, name, variable) ⇒ Object



1009
1010
1011
1012
1013
1014
1015
1016
1017
# File 'lib/rbs/definition_builder.rb', line 1009

def merge_variable(variables, name, variable)
  super_variable = variables[name]

  variables[name] = Definition::Variable.new(
    parent_variable: super_variable,
    type: variable.type,
    declared_in: variable.declared_in
  )
end

#method_definition_members(type_name, entry, kind:) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/rbs/definition_builder.rb', line 489

def method_definition_members(type_name, entry, kind:)
  # @type var interface_methods: Hash[Symbol, [Definition::Method, AST::Members::t]]
  interface_methods = {}
  # @type var methods: Hash[Symbol, Array[[AST::Members::MethodDefinition, Definition::accessibility]]]
  methods = {}

  entry.decls.each do |d|
    each_member_with_accessibility(d.decl.members) do |member, accessibility|
      case member
      when AST::Members::MethodDefinition
        case kind
        when :singleton
          next unless member.singleton?
        when :instance
          next unless member.instance?
        end

        methods[member.name] ||= []
        methods[member.name] << [
          member.update(types: member.types),
          accessibility
        ]
      when AST::Members::Include, AST::Members::Extend
        if member.name.interface?
          if (kind == :instance && member.is_a?(AST::Members::Include)) || (kind == :singleton && member.is_a?(AST::Members::Extend))
            NoMixinFoundError.check!(member.name, env: env, member: member)

            interface_name = member.name
            interface_args = member.args

            interface_definition = build_interface(interface_name)

            InvalidTypeApplicationError.check!(
              type_name: interface_name,
              args: interface_args,
              params: interface_definition.type_params_decl,
              location: member.location
            )

            sub = Substitution.build(interface_definition.type_params, interface_args)

            interface_definition.methods.each do |name, method|
              interface_methods[name] = [method.sub(sub), member]
            end
          end
        end
      end
    end
  end

  # @type var result: Hash[Symbol, member_detail]
  result = {}

  interface_methods.each do |name, pair|
    method_definition, _ = pair
    # @type var detail: member_detail
    detail = [:public, method_definition, nil, []]
    result[name] = detail
  end

  methods.each do |method_name, array|
    if result[method_name]
      unless array.all? {|pair| pair[0].overload? }
        raise MethodDefinitionConflictWithInterfaceMixinError.new(
          type_name: type_name,
          method_name: method_name,
          kind: :instance,
          mixin_member: interface_methods[method_name][1],
          entries: array.map(&:first)
        )
      end

      unless array.all? {|pair| pair[1] == :public}
        raise InconsistentMethodVisibilityError.new(
          type_name: type_name,
          method_name: method_name,
          kind: :instance,
          member_pairs: array
        )
      end

      result[method_name][3].push(*array.map(&:first))
    else
      case
      when array.size == 1 && !array[0][0].overload?
        member, visibility = array[0]
        result[method_name] = [visibility, nil, member, []]

      else
        visibilities = array.group_by {|pair| pair[1] }

        if visibilities.size > 1
          raise InconsistentMethodVisibilityError.new(
            type_name: type_name,
            method_name: method_name,
            kind: :instance,
            member_pairs: array
          )
        end

        overloads, primary = array.map(&:first).partition(&:overload?)
        result[method_name] = [array[0][1], nil, primary[0], overloads]
      end
    end
  end

  result
end

#mixin_ancestors(entry, included_modules:, extended_modules:, prepended_modules:) ⇒ Object



224
225
226
227
228
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
# File 'lib/rbs/definition_builder.rb', line 224

def mixin_ancestors(entry, included_modules:, extended_modules:, prepended_modules:)
  entry.decls.each do |d|
    decl = d.decl

    align_params = Substitution.build(
      decl.type_params.each.map(&:name),
      Types::Variable.build(entry.type_params.each.map(&:name))
    )

    decl.each_mixin do |member|
      case member
      when AST::Members::Include
        if included_modules
          NoMixinFoundError.check!(member.name, env: env, member: member)

          module_name = member.name
          module_args = member.args.map {|type| type.sub(align_params) }

          included_modules << Definition::Ancestor::Instance.new(name: module_name, args: module_args)
        end

      when AST::Members::Prepend
        if prepended_modules
          NoMixinFoundError.check!(member.name, env: env, member: member)

          module_name = member.name
          module_args = member.args.map {|type| type.sub(align_params) }

          prepended_modules << Definition::Ancestor::Instance.new(name: module_name, args: module_args)
        end

      when AST::Members::Extend
        if extended_modules
          NoMixinFoundError.check!(member.name, env: env, member: member)

          module_name = member.name
          module_args = member.args

          extended_modules << Definition::Ancestor::Instance.new(name: module_name, args: module_args)
        end
      end
    end
  end
end

#one_instance_ancestors(type_name) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
# File 'lib/rbs/definition_builder.rb', line 125

def one_instance_ancestors(type_name)
  as = one_instance_ancestors_cache[type_name] and return as

  entry = env.class_decls[type_name] or raise "Unknown name for one_instance_ancestors: #{type_name}"
  params = entry.type_params.each.map(&:name)

  case entry
  when Environment::ClassEntry
    validate_super_class!(type_name, entry)
    primary = entry.primary
    super_class = primary.decl.super_class

    if type_name != BuiltinNames::BasicObject.name
      if super_class
        super_name = super_class.name
        super_args = super_class.args
      else
        super_name = BuiltinNames::Object.name
        super_args = []
      end

      NoSuperclassFoundError.check!(super_name, env: env, location: primary.decl.location)

      ancestors = OneAncestors.class_instance(
        type_name: type_name,
        params: params,
        super_class: Definition::Ancestor::Instance.new(name: super_name, args: super_args)
      )
    else
      ancestors = OneAncestors.class_instance(
        type_name: type_name,
        params: params,
        super_class: nil
      )
    end
  when Environment::ModuleEntry
    ancestors = OneAncestors.module_instance(type_name: type_name, params: params)

    entry.self_types.each do |module_self|
      NoSelfTypeFoundError.check!(module_self, env: env)

      self_types = ancestors.self_types or raise
      self_types.push Definition::Ancestor::Instance.new(name: module_self.name, args: module_self.args)
    end
  end

  mixin_ancestors(entry,
                  included_modules: ancestors.included_modules,
                  prepended_modules: ancestors.prepended_modules,
                  extended_modules: nil)

  one_instance_ancestors_cache[type_name] = ancestors
end

#one_singleton_ancestors(type_name) ⇒ Object



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
# File 'lib/rbs/definition_builder.rb', line 179

def one_singleton_ancestors(type_name)
  as = one_singleton_ancestors_cache[type_name] and return as

  entry = env.class_decls[type_name] or raise "Unknown name for one_singleton_ancestors: #{type_name}"

  case entry
  when Environment::ClassEntry
    validate_super_class!(type_name, entry)
    primary = entry.primary
    super_class = primary.decl.super_class

    if type_name != BuiltinNames::BasicObject.name
      if super_class
        super_name = super_class.name
      else
        super_name = BuiltinNames::Object.name
      end

      NoSuperclassFoundError.check!(super_name, env: env, location: primary.decl.location)

      ancestors = OneAncestors.singleton(
        type_name: type_name,
        super_class: Definition::Ancestor::Singleton.new(name: super_name)
      )
    else
      ancestors = OneAncestors.singleton(
        type_name: type_name,
        super_class: Definition::Ancestor::Instance.new(name: BuiltinNames::Class.name, args: [])
      )
    end
  when Environment::ModuleEntry
    ancestors = OneAncestors.singleton(
      type_name: type_name,
      super_class: Definition::Ancestor::Instance.new(name: BuiltinNames::Module.name, args: [])
    )
  end

  mixin_ancestors(entry,
                  included_modules: nil,
                  prepended_modules: nil,
                  extended_modules: ancestors.extended_modules)

  one_singleton_ancestors_cache[type_name] = ancestors
end

#singleton_ancestors(type_name, building_ancestors: []) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/rbs/definition_builder.rb', line 331

def singleton_ancestors(type_name, building_ancestors: [])
  as = singleton_ancestor_cache[type_name] and return as

  entry = env.class_decls[type_name] or raise "Unknown name for singleton_ancestors: #{type_name}"
  self_ancestor = Definition::Ancestor::Singleton.new(name: type_name)

  RecursiveAncestorError.check!(self_ancestor,
                                ancestors: building_ancestors,
                                location: entry.primary.decl.location)
  building_ancestors.push self_ancestor

  one_ancestors = one_singleton_ancestors(type_name)

  ancestors = []

  case super_class = one_ancestors.super_class
  when Definition::Ancestor::Instance
    super_name = super_class.name
    super_args = super_class.args

    super_ancestors = instance_ancestors(super_name, building_ancestors: building_ancestors)
    ancestors.unshift(*super_ancestors.apply(super_args, location: entry.primary.decl.location))

  when Definition::Ancestor::Singleton
    super_name = super_class.name

    super_ancestors = singleton_ancestors(super_name, building_ancestors: [])
    ancestors.unshift(*super_ancestors.ancestors)
  end

  extended_modules = one_ancestors.extended_modules or raise
  extended_modules.each do |mod|
    if mod.name.class?
      name = mod.name
      args = mod.args
      mod_ancestors = instance_ancestors(name, building_ancestors: building_ancestors)
      ancestors.unshift(*mod_ancestors.apply(args, location: entry.primary.decl.location))
    end
  end

  ancestors.unshift(self_ancestor)

  building_ancestors.pop

  singleton_ancestor_cache[type_name] = Definition::SingletonAncestors.new(
    type_name: type_name,
    ancestors: ancestors
  )
end

#try_cache(type_name, cache:) ⇒ Object



1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
# File 'lib/rbs/definition_builder.rb', line 1036

def try_cache(type_name, cache:)
  cached = _ = cache[type_name]

  case cached
  when Definition
    cached
  when false
    raise
  when nil
    cache[type_name] = false
    begin
      cache[type_name] = yield
    rescue => ex
      cache.delete(type_name)
      raise ex
    end
  else
    raise
  end
end

#validate_parameter_variance(decl:, methods:) ⇒ Object



776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
# File 'lib/rbs/definition_builder.rb', line 776

def validate_parameter_variance(decl:, methods:)
  type_params = decl.type_params

  calculator = VarianceCalculator.new(builder: self)
  param_names = type_params.each.map(&:name)

  errors = []

  case decl
  when AST::Declarations::Class
    if super_class = decl.super_class
      result = calculator.in_inherit(name: super_class.name, args: super_class.args, variables: param_names)

      validate_params_with type_params, result: result do |param|
        errors.push InvalidVarianceAnnotationError::InheritanceError.new(
          param: param
        )
      end
    end
  end

  # @type var result: VarianceCalculator::Result

  decl.members.each do |member|
    case member
    when AST::Members::Include
      if member.name.class?
        result = calculator.in_inherit(name: member.name, args: member.args, variables: param_names)

        validate_params_with type_params, result: result do |param|
          errors.push InvalidVarianceAnnotationError::MixinError.new(
            include_member: member,
            param: param
          )
        end
      end
    end
  end

  methods.each do |name, method|
    method.method_types.each do |method_type|
      unless name == :initialize
        result = calculator.in_method_type(method_type: method_type, variables: param_names)

        validate_params_with type_params, result: result do |param|
          errors.push InvalidVarianceAnnotationError::MethodTypeError.new(
            method_name: name,
            method_type: method_type,
            param: param
          )
        end
      end
    end
  end

  unless errors.empty?
    raise InvalidVarianceAnnotationError.new(decl: decl, errors: errors)
  end
end

#validate_params_with(type_params, result:) ⇒ Object



766
767
768
769
770
771
772
773
774
# File 'lib/rbs/definition_builder.rb', line 766

def validate_params_with(type_params, result:)
  type_params.each do |param|
    unless param.skip_validation
      unless result.compatible?(param.name, with_annotation: param.variance)
        yield param
      end
    end
  end
end

#validate_super_class!(type_name, entry) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rbs/definition_builder.rb', line 108

def validate_super_class!(type_name, entry)
  with_super_classes = entry.decls.select {|d| d.decl.super_class }

  return if with_super_classes.size <= 1

  super_types = with_super_classes.map do |d|
    super_class = d.decl.super_class or raise
    Types::ClassInstance.new(name: super_class.name, args: super_class.args, location: nil)
  end

  super_types.uniq!

  return if super_types.size == 1

  raise SuperclassMismatchError.new(name: type_name, super_classes: super_types, entry: entry)
end