Class: RBS::DefinitionBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:) ⇒ DefinitionBuilder

Returns a new instance of DefinitionBuilder.



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

def initialize(env:)
  @env = env
  @instance_cache = {}
  @singleton_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_cacheObject (readonly)

Returns the value of attribute instance_cache.



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

def instance_cache
  @instance_cache
end

#singleton_cacheObject (readonly)

Returns the value of attribute singleton_cache.



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

def singleton_cache
  @singleton_cache
end

Instance Method Details

#absolute_type(type, namespace:) ⇒ Object



903
904
905
906
907
# File 'lib/rbs/definition_builder.rb', line 903

def absolute_type(type, namespace:)
  env.absolute_type(type, namespace: namespace) do |type|
    NoTypeFoundError.check!(type.name.absolute!, env: env, location: type.location)
  end
end

#absolute_type_name(type_name, namespace:, location:) ⇒ Object



909
910
911
912
913
# File 'lib/rbs/definition_builder.rb', line 909

def absolute_type_name(type_name, namespace:, location:)
  env.absolute_type_name(type_name, namespace: namespace) do |type_name|
    NoTypeFoundError.check!(type_name.absolute!, env: env, location: location)
  end
end

#build_ancestors(self_ancestor, ancestors: [], building_ancestors: [], location: nil) ⇒ Object



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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
178
179
180
181
182
183
184
185
186
# File 'lib/rbs/definition_builder.rb', line 13

def build_ancestors(self_ancestor, ancestors: [], building_ancestors: [], location: nil)
  decl = env.find_class(self_ancestor.name)
  namespace = self_ancestor.name.absolute!.to_namespace

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

  case self_ancestor
  when Definition::Ancestor::Instance
    args = self_ancestor.args
    param_names = decl.type_params.each.map(&:name)

    InvalidTypeApplicationError.check!(
      type_name: self_ancestor.name,
      args: args,
      params: decl.type_params,
      location: location || decl.location
    )

    sub = Substitution.build(param_names, args)

    case decl
    when AST::Declarations::Class
      unless self_ancestor.name == BuiltinNames::BasicObject.name
        super_ancestor = decl.super_class&.yield_self do |super_class|
          Definition::Ancestor::Instance.new(
            name: absolute_type_name(super_class.name, namespace: namespace, location: location || decl.location),
            args: super_class.args.map {|ty| absolute_type(ty.sub(sub), namespace: namespace) }
          )
        end || Definition::Ancestor::Instance.new(name: BuiltinNames::Object.name, args: [])

        build_ancestors(super_ancestor, ancestors: ancestors, building_ancestors: building_ancestors)
      end
    end

    decl.members.each do |member|
      case member
      when AST::Members::Include
        if member.name.class?
          ancestor = Definition::Ancestor::Instance.new(
            name: absolute_type_name(member.name, namespace: namespace, location: member.location),
            args: member.args.map {|ty| absolute_type(ty.sub(sub), namespace: namespace) }
          )
          build_ancestors ancestor, ancestors: ancestors, building_ancestors: building_ancestors, location: member.location
        end
      end
    end

    ancestors.unshift(self_ancestor)

    env.each_extension(self_ancestor.name).sort_by {|e| e.extension_name.to_s }.each do |extension|
      InvalidExtensionParameterError.check!(
        type_name: self_ancestor.name,
        extension_name: extension.extension_name,
        extension_params: extension.type_params,
        class_params: self_ancestor.args.map(&:name),
        location: extension.location
      )

      sub = Substitution.build(extension.type_params, self_ancestor.args)

      extension.members.each do |member|
        case member
        when AST::Members::Include
          if member.name.class?
            ancestor = Definition::Ancestor::Instance.new(
              name: absolute_type_name(member.name, namespace: namespace, location: member.location),
              args: member.args.map {|ty| absolute_type(ty.sub(sub), namespace: namespace) }
            )
            build_ancestors ancestor, ancestors: ancestors, building_ancestors: building_ancestors, location: member.location
          end
        end
      end

      extension_ancestor = Definition::Ancestor::ExtensionInstance.new(
        name: extension.name.absolute!,
        extension_name: extension.extension_name,
        args: self_ancestor.args,
      )
      ancestors.unshift(extension_ancestor)

      extension.members.each do |member|
        case member
        when AST::Members::Prepend
          if member.name.class?
            ancestor = Definition::Ancestor::Instance.new(
              name: absolute_type_name(member.name, namespace: namespace, location: member.location),
              args: member.args.map {|ty| absolute_type(ty.sub(sub), namespace: namespace) }
            )
            build_ancestors ancestor, ancestors: ancestors, building_ancestors: building_ancestors, location: member.location
          end
        end
      end
    end

    decl.members.each do |member|
      case member
      when AST::Members::Prepend
        ancestor = Definition::Ancestor::Instance.new(
          name: absolute_type_name(member.name, namespace: namespace, location: member.location),
          args: member.args.map {|ty| absolute_type(ty.sub(sub), namespace: namespace) }
        )
        build_ancestors ancestor, ancestors: ancestors, building_ancestors: building_ancestors, location: member.location
      end
    end
  when Definition::Ancestor::Singleton
    case decl
    when AST::Declarations::Class
      if self_ancestor.name == BuiltinNames::BasicObject.name
        class_ancestor = Definition::Ancestor::Instance.new(
          name: BuiltinNames::Class.name,
          args: []
        )
        build_ancestors class_ancestor, ancestors: ancestors, building_ancestors: building_ancestors
      else
        super_ancestor = decl.super_class&.yield_self do |super_class|
          Definition::Ancestor::Singleton.new(
            name: absolute_type_name(super_class.name, namespace: namespace, location: location || decl.location)
          )
        end || Definition::Ancestor::Singleton.new(name: BuiltinNames::Object.name)

        build_ancestors(super_ancestor, ancestors: ancestors, building_ancestors: building_ancestors)
      end
    when AST::Declarations::Module
      module_ancestor = Definition::Ancestor::Instance.new(
        name: BuiltinNames::Module.name,
        args: []
      )
      build_ancestors module_ancestor, ancestors: ancestors, building_ancestors: building_ancestors
    end

    decl.members.each do |member|
      case member
      when AST::Members::Extend
        if member.name.class?
          ancestor = Definition::Ancestor::Instance.new(
            name: absolute_type_name(member.name, namespace: namespace, location: member.location),
            args: member.args.map {|ty| absolute_type(ty.sub(sub), namespace: namespace) }
          )
          build_ancestors ancestor, ancestors: ancestors, building_ancestors: building_ancestors, location: member.location
        end
      end
    end

    ancestors.unshift(self_ancestor)

    env.each_extension(self_ancestor.name).sort_by {|e| e.extension_name.to_s }.each do |extension|
      extension.members.each do |member|
        case member
        when AST::Members::Extend
          if member.name.class?
            ancestor = Definition::Ancestor::Instance.new(
              name: absolute_type_name(member.name, namespace: namespace, location: member.location),
              args: member.args.map {|ty| absolute_type(ty, namespace: namespace) }
            )
            build_ancestors ancestor, ancestors: ancestors, building_ancestors: building_ancestors, location: member.location
          end
        end
      end

      extension_ancestor = Definition::Ancestor::ExtensionSingleton.new(
        name: extension.name.absolute!,
        extension_name: extension.extension_name
      )
      ancestors.unshift(extension_ancestor)
    end
  end

  building_ancestors.pop

  ancestors
end

#build_instance(type_name) ⇒ Object



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
228
229
230
231
232
233
234
235
236
237
# File 'lib/rbs/definition_builder.rb', line 201

def build_instance(type_name)
  try_cache type_name, cache: instance_cache do
    decl = env.find_class(type_name)
    self_ancestor = Definition::Ancestor::Instance.new(name: type_name,
                                                       args: Types::Variable.build(decl.type_params.each.map(&:name)))
    self_type = Types::ClassInstance.new(name: type_name, args: self_ancestor.args, location: nil)

    case decl
    when AST::Declarations::Class, AST::Declarations::Module
      ancestors = build_ancestors(self_ancestor)
      definition_pairs = ancestors.map do |ancestor|
        case ancestor
        when Definition::Ancestor::Instance
          [ancestor, build_one_instance(ancestor.name)]
        when Definition::Ancestor::Singleton
          [ancestor, build_one_singleton(ancestor.name)]
        when Definition::Ancestor::ExtensionInstance
          [ancestor, build_one_instance(ancestor.name, extension_name: ancestor.extension_name)]
        when Definition::Ancestor::ExtensionSingleton
          [ancestor, build_one_extension_singleton(ancestor.name, extension_name: ancestor.extension_name)]
        end
      end

      if decl.is_a?(AST::Declarations::Module)
        if decl.self_type
          self_interface = absolute_type(decl.self_type, namespace: type_name.to_namespace)
          ancestor = Definition::Ancestor::Instance.new(name: self_interface.name,
                                                        args: self_interface.args)

          definition_pairs.push [ancestor, build_one_instance(ancestor.name)]
        end
      end

      merge_definitions(definition_pairs, decl: decl, self_type: self_type, ancestors: ancestors)
    end
  end
end

#build_interface(type_name, declaration = env.find_class(type_name)) ⇒ Object



819
820
821
822
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
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
# File 'lib/rbs/definition_builder.rb', line 819

def build_interface(type_name, declaration = env.find_class(type_name))
  self_type = Types::Interface.new(
    name: type_name,
    args: declaration.type_params.each.map {|p| Types::Variable.new(name: p.name, location: nil) },
    location: nil
  )

  namespace = type_name.to_namespace

  Definition.new(declaration: declaration, self_type: self_type, ancestors: []).tap do |definition|
    alias_members = []

    declaration.members.each do |member|
      case member
      when AST::Members::Include
        mixin_name = env.absolute_interface_name(member.name, namespace: namespace) || member.name.absolute!
        mixin = build_one_instance(mixin_name)

        args = member.args.map {|type| absolute_type(type, namespace: namespace) }
        type_params = mixin.declaration.type_params

        InvalidTypeApplicationError.check!(
          type_name: type_name,
          args: args,
          params: type_params,
          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
    end

    declaration.members.each do |member|
      case member
      when AST::Members::MethodDefinition
        DuplicatedMethodDefinitionError.check!(
          decl: declaration,
          methods: definition.methods,
          name: member.name,
          location: member.location
        )

        method = Definition::Method.new(
          super_method: nil,
          method_types: member.types.map do |method_type|
            method_type.map_type {|ty| absolute_type(ty, namespace: namespace) }
          end,
          defined_in: declaration,
          implemented_in: nil,
          accessibility: :public,
          attributes: member.attributes,
          annotations: member.annotations,
          comment: member.comment
        )
        definition.methods[member.name] = method
      when AST::Members::Alias
        alias_members << member
      end
    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

#build_one_instance(type_name, extension_name: nil) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
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
445
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
488
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
# File 'lib/rbs/definition_builder.rb', line 316

def build_one_instance(type_name, extension_name: nil)
  decl = if extension_name
           env.each_extension(type_name).find {|ext| ext.extension_name == extension_name } or
             raise "Unknown extension: #{type_name} (#{extension_name})"
         else
           env.find_class(type_name)
         end

  case decl
  when AST::Declarations::Interface
    build_interface type_name, decl
  else
    namespace = type_name.to_namespace

    case decl
    when AST::Declarations::Class, AST::Declarations::Module
      self_type = Types::ClassInstance.new(name: type_name,
                                           args: Types::Variable.build(decl.type_params.each.map(&:name)),
                                           location: nil)
      ancestors = [Definition::Ancestor::Instance.new(name: type_name, args: self_type.args)]
    when AST::Declarations::Extension
      self_type = Types::ClassInstance.new(name: type_name, args: Types::Variable.build(decl.type_params), location: nil)
      ancestors = [Definition::Ancestor::ExtensionInstance.new(name: type_name,
                                                               extension_name: extension_name,
                                                               args: self_type.args)]
    end

    Definition.new(declaration: decl, self_type: self_type, ancestors: ancestors).tap do |definition|
      alias_members = []

      each_member_with_accessibility(decl.members) do |member, accessibility|
        case member
        when AST::Members::MethodDefinition
          if member.instance?
            name = member.name
            method_types = member.types.map do |method_type|
              case method_type
              when MethodType
                method_type.map_type do |type|
                  absolute_type(type, namespace: namespace)
                end
              when :super
                :super
              end
            end

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

            attrs = if name == :initialize
                      (member.attributes + [:incompatible]).uniq
                    else
                      member.attributes
                    end

            definition.methods[name] = Definition::Method.new(super_method: nil,
                                                              method_types: method_types,
                                                              defined_in: decl,
                                                              implemented_in: decl,
                                                              accessibility: accessibility,
                                                              attributes: attrs,
                                                              annotations: member.annotations,
                                                              comment: member.comment)
          end
        when AST::Members::AttrReader, AST::Members::AttrAccessor, AST::Members::AttrWriter
          name = member.name
          type = absolute_type(member.type, namespace: namespace)
          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,
              method_types: [
                MethodType.new(
                  type_params: [],
                  type: Types::Function.empty(type),
                  block: nil,
                  location: nil
                )
              ],
              defined_in: decl,
              implemented_in: decl,
              accessibility: accessibility,
              attributes: [],
              annotations: member.annotations,
              comment: member.comment
            )
          end

          if member.is_a?(AST::Members::AttrWriter) || member.is_a?(AST::Members::AttrAccessor)
            definition.methods[:"#{name}="] = Definition::Method.new(
              super_method: nil,
              method_types: [
                MethodType.new(
                  type_params: [],
                  type: Types::Function.new(required_positionals: [Types::Function::Param.new(name: name, type: type)],
                                            optional_positionals: [],
                                            rest_positionals: nil,
                                            trailing_positionals: [],
                                            required_keywords: {},
                                            optional_keywords: {},
                                            rest_keywords: nil,
                                            return_type: type),
                  block: nil,
                  location: nil
                )
              ],
              defined_in: decl,
              implemented_in: decl,
              accessibility: accessibility,
              attributes: [],
              annotations: member.annotations,
              comment: member.comment
            )
          end

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

        when AST::Members::Alias
          if member.instance?
            alias_members << member
          end
        when AST::Members::Include
          if member.name.interface?
            absolute_name = absolute_type_name(member.name, namespace: namespace, location: member.location)
            interface_definition = build_one_instance(absolute_name)
            absolute_args = member.args.map {|ty| absolute_type(ty, namespace: namespace) }

            InvalidTypeApplicationError.check!(
              type_name: absolute_name,
              args: absolute_args,
              params: interface_definition.type_params_decl,
              location: member.location
            )

            sub = Substitution.build(interface_definition.type_params, absolute_args)
            interface_definition.methods.each do |name, method|
              method_types = method.method_types.map do |method_type|
                method_type.sub(sub).map_type do |type|
                  absolute_type(type, namespace: namespace)
                end
              end

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

              definition.methods[name] = Definition::Method.new(
                super_method: nil,
                method_types: method_types,
                defined_in: method.defined_in,
                implemented_in: decl,
                accessibility: method.accessibility,
                attributes: [],
                annotations: method.annotations,
                comment: member.comment
              )
            end
          end
        when AST::Members::InstanceVariable
          definition.instance_variables[member.name] = Definition::Variable.new(
            type: absolute_type(member.type, namespace: namespace),
            parent_variable: nil,
            declared_in: decl
          )
        when AST::Members::ClassVariable
          definition.class_variables[member.name] = Definition::Variable.new(
            type: absolute_type(member.type, namespace: namespace),
            parent_variable: nil,
            declared_in: decl
          )
        end
      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: decl,
          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

      validate_parameter_variance(
        decl: decl,
        methods: definition.methods
      )
    end
  end
end

#build_one_singleton(type_name, extension_name: nil) ⇒ Object



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

def build_one_singleton(type_name, extension_name: nil)
  decl = if extension_name
           env.each_extension(type_name).find {|ext| ext.extension_name == extension_name } or
             raise "Unknown extension: #{type_name} (#{extension_name})"
         else
           env.find_class(type_name)
         end

  namespace = type_name.to_namespace

  case decl
  when AST::Declarations::Module, AST::Declarations::Class
    self_type = Types::ClassSingleton.new(name: type_name, location: nil)
    ancestors = [Definition::Ancestor::Singleton.new(name: type_name)]
  when AST::Declarations::Extension
    self_type = Types::ClassSingleton.new(name: type_name, location: nil)
    ancestors = [Definition::Ancestor::ExtensionSingleton.new(name: type_name, extension_name: extension_name)]
  end

  Definition.new(declaration: decl, self_type: self_type, ancestors: ancestors).tap do |definition|
    alias_members = []

    each_member_with_accessibility(decl.members) do |member, accessibility|
      case member
      when AST::Members::MethodDefinition
        if member.singleton?
          name = member.name
          method_types = member.types.map do |method_type|
            method_type.map_type do |type|
              absolute_type(type, namespace: namespace)
            end
          end

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

          definition.methods[name] = Definition::Method.new(super_method: nil,
                                                            method_types: method_types,
                                                            defined_in: decl,
                                                            implemented_in: decl,
                                                            accessibility: accessibility,
                                                            attributes: member.attributes,
                                                            annotations: member.annotations,
                                                            comment: member.comment)
        end
      when AST::Members::Alias
        if member.singleton?
          alias_members << member
        end
      when AST::Members::Extend
        if member.name.interface?
          absolute_name = absolute_type_name(member.name, namespace: namespace, location: member.location)
          interface_definition = build_one_instance(absolute_name)
          absolute_args = member.args.map {|ty| absolute_type(ty, namespace: namespace) }

          InvalidTypeApplicationError.check!(
            type_name: absolute_name,
            args: absolute_args,
            params: interface_definition.type_params_decl,
            location: member.location
          )

          sub = Substitution.build(interface_definition.type_params, absolute_args)
          interface_definition.methods.each do |name, method|
            method_types = method.method_types.map do |method_type|
              method_type.sub(sub).map_type do |type|
                absolute_type(type, namespace: namespace)
              end
            end

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

            definition.methods[name] = Definition::Method.new(
              super_method: nil,
              method_types: method_types,
              defined_in: method.defined_in,
              implemented_in: decl,
              accessibility: method.accessibility,
              attributes: method.attributes,
              annotations: method.annotations,
              comment: member.comment
            )
          end
        end
      when AST::Members::ClassInstanceVariable
        definition.instance_variables[member.name] = Definition::Variable.new(
          type: absolute_type(member.type, namespace: namespace),
          parent_variable: nil,
          declared_in: decl
        )
      when AST::Members::ClassVariable
        definition.class_variables[member.name] = Definition::Variable.new(
          type: absolute_type(member.type, namespace: namespace),
          parent_variable: nil,
          declared_in: decl
        )
      end
    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: decl,
        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

#build_singleton(type_name) ⇒ Object



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

def build_singleton(type_name)
  try_cache type_name, cache: singleton_cache do
    decl = env.find_class(type_name)
    self_ancestor = Definition::Ancestor::Singleton.new(name: type_name)
    self_type = Types::ClassSingleton.new(name: type_name, location: nil)

    case decl
    when AST::Declarations::Class, AST::Declarations::Module
      ancestors = build_ancestors(self_ancestor)
      definition_pairs = ancestors.map do |ancestor|
        case ancestor
        when Definition::Ancestor::Instance
          [ancestor, build_one_instance(ancestor.name)]
        when Definition::Ancestor::Singleton
          [ancestor, build_one_singleton(ancestor.name)]
        when Definition::Ancestor::ExtensionInstance
          [ancestor, build_one_instance(ancestor.name, extension_name: ancestor.extension_name)]
        when Definition::Ancestor::ExtensionSingleton
          [ancestor, build_one_singleton(ancestor.name, extension_name: ancestor.extension_name)]
        end
      end

      if decl.is_a?(AST::Declarations::Class)
        definition_pairs.find {|ancestor, _| ancestor == self_ancestor }.tap do |_, definition|
          unless definition.methods[:new]&.implemented_in == decl
            instance_definition = build_instance(type_name)
            class_params = decl.type_params.each.map(&:name)
            initialize_method = instance_definition.methods[:initialize]
            method_types = initialize_method.method_types.map do |method_type|
              case method_type
              when MethodType
                fvs = method_type.free_variables + class_params
                unless fvs.empty?
                  param_name_set = Set.new(class_params)
                  bound_variables = method_type.type_params
                  renamed_types = bound_variables.map do |x|
                    if param_name_set.member?(x)
                      Types::Variable.fresh(x)
                    else
                      Types::Variable.new(name: x, location: nil)
                    end
                  end
                  sub = Substitution.build(bound_variables, renamed_types)
                  method_type_params = renamed_types.unshift(*class_params)
                else
                  sub = Substitution.build([], [])
                  method_type_params = method_type.type_params
                end

                MethodType.new(
                  type_params: method_type_params,
                  type: method_type.type.sub(sub).with_return_type(instance_definition.self_type),
                  block: method_type.block&.yield_self {|ty| ty.sub(sub) },
                  location: method_type.location
                )
              end
            end.compact

            definition.methods[:new] = Definition::Method.new(
              super_method: nil,
              defined_in: nil,
              implemented_in: env.find_class(RBS::BuiltinNames::Class.name),
              method_types: method_types,
              accessibility: :public,
              attributes: [:incompatible],
              annotations: [],
              comment: nil
            )
          end
        end
      end

      merge_definitions(definition_pairs, decl: decl, self_type: self_type, ancestors: ancestors)
    end
  end
end

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



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/rbs/definition_builder.rb', line 188

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

#expand_alias(type_name) ⇒ Object



915
916
917
# File 'lib/rbs/definition_builder.rb', line 915

def expand_alias(type_name)
  absolute_type(env.find_alias(type_name).type, namespace: type_name.namespace)
end

#merge_definitions(pairs, decl:, self_type:, ancestors:) ⇒ Object



742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
# File 'lib/rbs/definition_builder.rb', line 742

def merge_definitions(pairs, decl:, self_type:, ancestors:)
  Definition.new(declaration: decl, self_type: self_type, ancestors: ancestors).tap do |definition|
    pairs.reverse_each do |(ancestor, current_definition)|
      sub = case ancestor
            when Definition::Ancestor::Instance, Definition::Ancestor::ExtensionInstance
              Substitution.build(current_definition.type_params, ancestor.args)
            when Definition::Ancestor::Singleton, Definition::Ancestor::ExtensionSingleton
              Substitution.build([], [])
            end
      namespace = current_definition.name.absolute!.to_namespace

      current_definition.methods.each do |name, method|
        merge_method definition.methods, name, method, sub, namespace
      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(methods, name, method, sub, namespace) ⇒ Object



778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
# File 'lib/rbs/definition_builder.rb', line 778

def merge_method(methods, name, method, sub, namespace)
  super_method = methods[name]

  methods[name] = Definition::Method.new(
    method_types: method.method_types.flat_map do |method_type|
      case method_type
      when MethodType
        [absolute_type(method_type.sub(sub), namespace: namespace)]
      when :super
        super_method.method_types
      end
    end,
    super_method: super_method,
    defined_in: method.defined_in,
    implemented_in: method.implemented_in,
    accessibility: method.accessibility,
    attributes: method.attributes,
    annotations: method.annotations,
    comment: method.comment
  )
end

#merge_variable(variables, name, variable) ⇒ Object



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

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

#try_cache(type_name, cache:) ⇒ Object



800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
# File 'lib/rbs/definition_builder.rb', line 800

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[type_name] = nil
      raise ex
    end
  end
end

#validate_parameter_variance(decl:, methods:) ⇒ Object



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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/rbs/definition_builder.rb', line 545

def validate_parameter_variance(decl:, methods:)
  type_params = case decl
                when AST::Declarations::Extension
                  env.find_class(decl.name.absolute!).type_params.rename_to(decl.type_params)
                else
                  decl.type_params
                end

  namespace = decl.name.absolute!.to_namespace
  calculator = VarianceCalculator.new(builder: self)
  param_names = type_params.each.map(&:name)

  errors = []

  if decl.is_a?(AST::Declarations::Class)
    if decl.super_class
      absolute_super_name = absolute_type_name(decl.super_class.name, namespace: namespace, location: decl.location)
      absolute_args = decl.super_class.args.map {|type| absolute_type(type, namespace: namespace) }
      result = calculator.in_inherit(name: absolute_super_name, args: absolute_args, variables: param_names)

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

  decl.members.each do |member|
    case member
    when AST::Members::Include
      if member.name.class?
        absolute_module_name = absolute_type_name(member.name, namespace: namespace, location: decl.location)
        absolute_args = member.args.map {|type| absolute_type(type, namespace: namespace) }
        result = calculator.in_inherit(name: absolute_module_name, args: absolute_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|
      case method_type
      when MethodType
        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



535
536
537
538
539
540
541
542
543
# File 'lib/rbs/definition_builder.rb', line 535

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