rbs 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (132) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ruby.yml +28 -0
  3. data/.gitignore +12 -0
  4. data/.rubocop.yml +15 -0
  5. data/BSDL +22 -0
  6. data/CHANGELOG.md +9 -0
  7. data/COPYING +56 -0
  8. data/Gemfile +6 -0
  9. data/README.md +93 -0
  10. data/Rakefile +142 -0
  11. data/bin/annotate-with-rdoc +157 -0
  12. data/bin/console +14 -0
  13. data/bin/query-rdoc +103 -0
  14. data/bin/setup +10 -0
  15. data/bin/sort +89 -0
  16. data/bin/test_runner.rb +16 -0
  17. data/docs/CONTRIBUTING.md +97 -0
  18. data/docs/sigs.md +148 -0
  19. data/docs/stdlib.md +152 -0
  20. data/docs/syntax.md +528 -0
  21. data/exe/rbs +7 -0
  22. data/lib/rbs.rb +64 -0
  23. data/lib/rbs/ast/annotation.rb +27 -0
  24. data/lib/rbs/ast/comment.rb +27 -0
  25. data/lib/rbs/ast/declarations.rb +395 -0
  26. data/lib/rbs/ast/members.rb +362 -0
  27. data/lib/rbs/buffer.rb +50 -0
  28. data/lib/rbs/builtin_names.rb +55 -0
  29. data/lib/rbs/cli.rb +558 -0
  30. data/lib/rbs/constant.rb +26 -0
  31. data/lib/rbs/constant_table.rb +150 -0
  32. data/lib/rbs/definition.rb +170 -0
  33. data/lib/rbs/definition_builder.rb +919 -0
  34. data/lib/rbs/environment.rb +281 -0
  35. data/lib/rbs/environment_loader.rb +136 -0
  36. data/lib/rbs/environment_walker.rb +124 -0
  37. data/lib/rbs/errors.rb +187 -0
  38. data/lib/rbs/location.rb +102 -0
  39. data/lib/rbs/method_type.rb +123 -0
  40. data/lib/rbs/namespace.rb +91 -0
  41. data/lib/rbs/parser.y +1344 -0
  42. data/lib/rbs/prototype/rb.rb +553 -0
  43. data/lib/rbs/prototype/rbi.rb +587 -0
  44. data/lib/rbs/prototype/runtime.rb +381 -0
  45. data/lib/rbs/substitution.rb +46 -0
  46. data/lib/rbs/test.rb +26 -0
  47. data/lib/rbs/test/errors.rb +61 -0
  48. data/lib/rbs/test/hook.rb +294 -0
  49. data/lib/rbs/test/setup.rb +58 -0
  50. data/lib/rbs/test/spy.rb +325 -0
  51. data/lib/rbs/test/test_helper.rb +183 -0
  52. data/lib/rbs/test/type_check.rb +254 -0
  53. data/lib/rbs/type_name.rb +70 -0
  54. data/lib/rbs/types.rb +936 -0
  55. data/lib/rbs/variance_calculator.rb +138 -0
  56. data/lib/rbs/vendorer.rb +47 -0
  57. data/lib/rbs/version.rb +3 -0
  58. data/lib/rbs/writer.rb +269 -0
  59. data/lib/ruby/signature.rb +7 -0
  60. data/rbs.gemspec +46 -0
  61. data/stdlib/abbrev/abbrev.rbs +60 -0
  62. data/stdlib/base64/base64.rbs +71 -0
  63. data/stdlib/benchmark/benchmark.rbs +372 -0
  64. data/stdlib/builtin/array.rbs +1997 -0
  65. data/stdlib/builtin/basic_object.rbs +280 -0
  66. data/stdlib/builtin/binding.rbs +177 -0
  67. data/stdlib/builtin/builtin.rbs +45 -0
  68. data/stdlib/builtin/class.rbs +145 -0
  69. data/stdlib/builtin/comparable.rbs +116 -0
  70. data/stdlib/builtin/complex.rbs +400 -0
  71. data/stdlib/builtin/constants.rbs +37 -0
  72. data/stdlib/builtin/data.rbs +5 -0
  73. data/stdlib/builtin/deprecated.rbs +2 -0
  74. data/stdlib/builtin/dir.rbs +413 -0
  75. data/stdlib/builtin/encoding.rbs +607 -0
  76. data/stdlib/builtin/enumerable.rbs +404 -0
  77. data/stdlib/builtin/enumerator.rbs +260 -0
  78. data/stdlib/builtin/errno.rbs +781 -0
  79. data/stdlib/builtin/errors.rbs +582 -0
  80. data/stdlib/builtin/exception.rbs +194 -0
  81. data/stdlib/builtin/false_class.rbs +40 -0
  82. data/stdlib/builtin/fiber.rbs +68 -0
  83. data/stdlib/builtin/fiber_error.rbs +12 -0
  84. data/stdlib/builtin/file.rbs +1076 -0
  85. data/stdlib/builtin/file_test.rbs +59 -0
  86. data/stdlib/builtin/float.rbs +696 -0
  87. data/stdlib/builtin/gc.rbs +243 -0
  88. data/stdlib/builtin/hash.rbs +1029 -0
  89. data/stdlib/builtin/integer.rbs +707 -0
  90. data/stdlib/builtin/io.rbs +683 -0
  91. data/stdlib/builtin/kernel.rbs +576 -0
  92. data/stdlib/builtin/marshal.rbs +161 -0
  93. data/stdlib/builtin/match_data.rbs +271 -0
  94. data/stdlib/builtin/math.rbs +369 -0
  95. data/stdlib/builtin/method.rbs +185 -0
  96. data/stdlib/builtin/module.rbs +1104 -0
  97. data/stdlib/builtin/nil_class.rbs +82 -0
  98. data/stdlib/builtin/numeric.rbs +409 -0
  99. data/stdlib/builtin/object.rbs +824 -0
  100. data/stdlib/builtin/proc.rbs +429 -0
  101. data/stdlib/builtin/process.rbs +1227 -0
  102. data/stdlib/builtin/random.rbs +267 -0
  103. data/stdlib/builtin/range.rbs +226 -0
  104. data/stdlib/builtin/rational.rbs +424 -0
  105. data/stdlib/builtin/rb_config.rbs +57 -0
  106. data/stdlib/builtin/regexp.rbs +1083 -0
  107. data/stdlib/builtin/ruby_vm.rbs +14 -0
  108. data/stdlib/builtin/signal.rbs +55 -0
  109. data/stdlib/builtin/string.rbs +1901 -0
  110. data/stdlib/builtin/string_io.rbs +284 -0
  111. data/stdlib/builtin/struct.rbs +40 -0
  112. data/stdlib/builtin/symbol.rbs +228 -0
  113. data/stdlib/builtin/thread.rbs +1108 -0
  114. data/stdlib/builtin/thread_group.rbs +23 -0
  115. data/stdlib/builtin/time.rbs +1047 -0
  116. data/stdlib/builtin/trace_point.rbs +290 -0
  117. data/stdlib/builtin/true_class.rbs +46 -0
  118. data/stdlib/builtin/unbound_method.rbs +153 -0
  119. data/stdlib/builtin/warning.rbs +17 -0
  120. data/stdlib/coverage/coverage.rbs +62 -0
  121. data/stdlib/csv/csv.rbs +773 -0
  122. data/stdlib/erb/erb.rbs +392 -0
  123. data/stdlib/find/find.rbs +40 -0
  124. data/stdlib/ipaddr/ipaddr.rbs +247 -0
  125. data/stdlib/json/json.rbs +335 -0
  126. data/stdlib/pathname/pathname.rbs +1093 -0
  127. data/stdlib/prime/integer-extension.rbs +23 -0
  128. data/stdlib/prime/prime.rbs +188 -0
  129. data/stdlib/securerandom/securerandom.rbs +9 -0
  130. data/stdlib/set/set.rbs +301 -0
  131. data/stdlib/tmpdir/tmpdir.rbs +53 -0
  132. metadata +292 -0
@@ -0,0 +1,26 @@
1
+ module RBS
2
+ class Constant
3
+ attr_reader :name
4
+ attr_reader :type
5
+ attr_reader :declaration
6
+
7
+ def initialize(name:, type:, declaration:)
8
+ @name = name
9
+ @type = type
10
+ @declaration = declaration
11
+ end
12
+
13
+ def ==(other)
14
+ other.is_a?(Constant) &&
15
+ other.name == name &&
16
+ other.type == type &&
17
+ other.declaration == declaration
18
+ end
19
+
20
+ alias eql? ==
21
+
22
+ def hash
23
+ self.class.hash ^ name.hash ^ type.hash ^ declaration.hash
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,150 @@
1
+ module RBS
2
+ class ConstantTable
3
+ attr_reader :definition_builder
4
+ attr_reader :constant_scopes_cache
5
+
6
+ def env
7
+ definition_builder.env
8
+ end
9
+
10
+ def initialize(builder:)
11
+ @definition_builder = builder
12
+ @constant_scopes_cache = {}
13
+ end
14
+
15
+ def name_to_constant(name)
16
+ case
17
+ when env.name_to_constant.key?(name)
18
+ decl = env.name_to_constant[name]
19
+ type = env.absolute_type(decl.type, namespace: name.namespace) {|type| type.name.absolute! }
20
+ Constant.new(name: name, type: type, declaration: decl)
21
+ when env.class?(name)
22
+ decl = env.name_to_decl[name]
23
+ type = Types::ClassSingleton.new(name: name, location: nil)
24
+ Constant.new(name: name, type: type, declaration: decl)
25
+ end
26
+ end
27
+
28
+ def split_name(name)
29
+ name.namespace.path + [name.name]
30
+ end
31
+
32
+ def resolve_constant_reference(name, context:)
33
+ head, *tail = split_name(name)
34
+
35
+ head_constant = case
36
+ when name.absolute?
37
+ name_to_constant(TypeName.new(name: head, namespace: Namespace.root))
38
+ when !context || context.empty?
39
+ name_to_constant(TypeName.new(name: head, namespace: Namespace.root))
40
+ else
41
+ resolve_constant_reference_context(head, context: context) ||
42
+ resolve_constant_reference_inherit(head,
43
+ scopes: constant_scopes(context.to_type_name))
44
+ end
45
+
46
+ if head_constant
47
+ tail.inject(head_constant) do |constant, name|
48
+ resolve_constant_reference_inherit name,
49
+ scopes: constant_scopes(constant.name),
50
+ no_object: constant.name != BuiltinNames::Object.name
51
+ end
52
+ end
53
+ end
54
+
55
+ def resolve_constant_reference_context(name, context:)
56
+ if context.empty?
57
+ nil
58
+ else
59
+ name_to_constant(TypeName.new(name: name, namespace: context)) ||
60
+ resolve_constant_reference_context(name, context: context.parent)
61
+ end
62
+ end
63
+
64
+ def resolve_constant_reference_inherit(name, scopes:, no_object: false)
65
+ scopes.each do |context|
66
+ if context.path == [:Object]
67
+ unless no_object
68
+ constant = name_to_constant(TypeName.new(name: name, namespace: context)) ||
69
+ name_to_constant(TypeName.new(name: name, namespace: Namespace.root))
70
+ end
71
+ else
72
+ constant = name_to_constant(TypeName.new(name: name, namespace: context))
73
+ end
74
+
75
+ return constant if constant
76
+ end
77
+
78
+ nil
79
+ end
80
+
81
+ def constant_scopes(name)
82
+ constant_scopes_cache[name] ||= constant_scopes0(name, scopes: [])
83
+ end
84
+
85
+ def constant_scopes_module(name, scopes:)
86
+ decl = env.find_class(name)
87
+ namespace = name.to_namespace
88
+
89
+ decl.members.each do |member|
90
+ case member
91
+ when AST::Members::Include
92
+ constant_scopes_module absolute_type_name(member.name, namespace: namespace),
93
+ scopes: scopes
94
+ end
95
+ end
96
+
97
+ scopes.unshift namespace
98
+ end
99
+
100
+ def constant_scopes0(name, scopes: [])
101
+ decl = env.find_class(name)
102
+ namespace = name.to_namespace
103
+
104
+ case decl
105
+ when AST::Declarations::Module
106
+ constant_scopes0 BuiltinNames::Module.name, scopes: scopes
107
+ constant_scopes_module name, scopes: scopes
108
+
109
+ when AST::Declarations::Class
110
+ unless name == BuiltinNames::BasicObject.name
111
+ super_name = decl.super_class&.yield_self {|super_class|
112
+ absolute_type_name(super_class.name, namespace: namespace)
113
+ } || BuiltinNames::Object.name
114
+
115
+ constant_scopes0 super_name, scopes: scopes
116
+ end
117
+
118
+ decl.members.each do |member|
119
+ case member
120
+ when AST::Members::Include
121
+ constant_scopes_module absolute_type_name(member.name, namespace: namespace),
122
+ scopes: scopes
123
+ end
124
+ end
125
+
126
+ scopes.unshift namespace
127
+ else
128
+ raise "Unexpected declaration: #{name}"
129
+ end
130
+
131
+ env.each_extension(name).sort_by {|e| e.extension_name.to_s }.each do |extension|
132
+ extension.members.each do |member|
133
+ case member
134
+ when AST::Members::Include
135
+ constant_scopes_module absolute_type_name(member.name, namespace: namespace),
136
+ scopes: []
137
+ end
138
+ end
139
+ end
140
+
141
+ scopes
142
+ end
143
+
144
+ def absolute_type_name(name, namespace:)
145
+ env.absolute_type_name(name, namespace: namespace) do
146
+ raise
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,170 @@
1
+ module RBS
2
+ class Definition
3
+ class Variable
4
+ attr_reader :parent_variable
5
+ attr_reader :type
6
+ attr_reader :declared_in
7
+
8
+ def initialize(parent_variable:, type:, declared_in:)
9
+ @parent_variable = parent_variable
10
+ @type = type
11
+ @declared_in = declared_in
12
+ end
13
+ end
14
+
15
+ class Method
16
+ attr_reader :super_method
17
+ attr_reader :method_types
18
+ attr_reader :defined_in
19
+ attr_reader :implemented_in
20
+ attr_reader :accessibility
21
+ attr_reader :attributes
22
+ attr_reader :annotations
23
+ attr_reader :comment
24
+
25
+ def initialize(super_method:, method_types:, defined_in:, implemented_in:, accessibility:, attributes:, annotations:, comment:)
26
+ @super_method = super_method
27
+ @method_types = method_types
28
+ @defined_in = defined_in
29
+ @implemented_in = implemented_in
30
+ @accessibility = accessibility
31
+ @attributes = attributes
32
+ @annotations = annotations
33
+ @comment = comment
34
+ end
35
+
36
+ def public?
37
+ @accessibility == :public
38
+ end
39
+
40
+ def private?
41
+ @accessibility == :private
42
+ end
43
+
44
+ def sub(s)
45
+ self.class.new(
46
+ super_method: super_method&.sub(s),
47
+ method_types: method_types.map {|ty| ty.sub(s) },
48
+ defined_in: defined_in,
49
+ implemented_in: implemented_in,
50
+ accessibility: @accessibility,
51
+ attributes: attributes,
52
+ annotations: annotations,
53
+ comment: comment
54
+ )
55
+ end
56
+
57
+ def map_type(&block)
58
+ self.class.new(
59
+ super_method: super_method&.map_type(&block),
60
+ method_types: method_types.map do |ty|
61
+ ty.map_type(&block)
62
+ end,
63
+ defined_in: defined_in,
64
+ implemented_in: implemented_in,
65
+ accessibility: @accessibility,
66
+ attributes: attributes,
67
+ annotations: annotations,
68
+ comment: comment
69
+ )
70
+ end
71
+ end
72
+
73
+ module Ancestor
74
+ Instance = Struct.new(:name, :args, keyword_init: true)
75
+ Singleton = Struct.new(:name, keyword_init: true)
76
+ ExtensionInstance = Struct.new(:name, :extension_name, :args, keyword_init: true)
77
+ ExtensionSingleton = Struct.new(:name, :extension_name, keyword_init: true)
78
+ end
79
+
80
+ attr_reader :declaration
81
+ attr_reader :self_type
82
+ attr_reader :methods
83
+ attr_reader :instance_variables
84
+ attr_reader :class_variables
85
+ attr_reader :ancestors
86
+
87
+ def initialize(declaration:, self_type:, ancestors:)
88
+ unless declaration.is_a?(AST::Declarations::Class) ||
89
+ declaration.is_a?(AST::Declarations::Module) ||
90
+ declaration.is_a?(AST::Declarations::Interface) ||
91
+ declaration.is_a?(AST::Declarations::Extension)
92
+ raise "Declaration should be a class, module, or interface: #{declaration.name}"
93
+ end
94
+
95
+ unless (self_type.is_a?(Types::ClassSingleton) || self_type.is_a?(Types::Interface) || self_type.is_a?(Types::ClassInstance)) && self_type.name == declaration.name.absolute!
96
+ raise "self_type should be the type of declaration: #{self_type}"
97
+ end
98
+
99
+ @self_type = self_type
100
+ @declaration = declaration
101
+ @methods = {}
102
+ @instance_variables = {}
103
+ @class_variables = {}
104
+ @ancestors = ancestors
105
+ end
106
+
107
+ def name
108
+ declaration.name
109
+ end
110
+
111
+ def class?
112
+ declaration.is_a?(AST::Declarations::Class)
113
+ end
114
+
115
+ def module?
116
+ declaration.is_a?(AST::Declarations::Module)
117
+ end
118
+
119
+ def class_type?
120
+ @self_type.is_a?(Types::ClassSingleton)
121
+ end
122
+
123
+ def instance_type?
124
+ @self_type.is_a?(Types::ClassInstance)
125
+ end
126
+
127
+ def interface_type?
128
+ @self_type.is_a?(Types::Interface)
129
+ end
130
+
131
+ def type_params
132
+ @self_type.args.map(&:name)
133
+ end
134
+
135
+ def type_params_decl
136
+ case declaration
137
+ when AST::Declarations::Extension
138
+ nil
139
+ else
140
+ declaration.type_params
141
+ end
142
+ end
143
+
144
+ def each_type(&block)
145
+ if block_given?
146
+ methods.each_value do |method|
147
+ if method.defined_in == self.declaration
148
+ method.method_types.each do |method_type|
149
+ method_type.each_type(&block)
150
+ end
151
+ end
152
+ end
153
+
154
+ instance_variables.each_value do |var|
155
+ if var.declared_in == self.declaration
156
+ yield var.type
157
+ end
158
+ end
159
+
160
+ class_variables.each_value do |var|
161
+ if var.declared_in == self.declaration
162
+ yield var.type
163
+ end
164
+ end
165
+ else
166
+ enum_for :each_type
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,919 @@
1
+ module RBS
2
+ class DefinitionBuilder
3
+ attr_reader :env
4
+ attr_reader :instance_cache
5
+ attr_reader :singleton_cache
6
+
7
+ def initialize(env:)
8
+ @env = env
9
+ @instance_cache = {}
10
+ @singleton_cache = {}
11
+ end
12
+
13
+ def build_ancestors(self_ancestor, ancestors: [], building_ancestors: [], location: nil)
14
+ decl = env.find_class(self_ancestor.name)
15
+ namespace = self_ancestor.name.absolute!.to_namespace
16
+
17
+ RecursiveAncestorError.check!(self_ancestor,
18
+ ancestors: building_ancestors,
19
+ location: location || decl.location)
20
+ building_ancestors.push self_ancestor
21
+
22
+ case self_ancestor
23
+ when Definition::Ancestor::Instance
24
+ args = self_ancestor.args
25
+ param_names = decl.type_params.each.map(&:name)
26
+
27
+ InvalidTypeApplicationError.check!(
28
+ type_name: self_ancestor.name,
29
+ args: args,
30
+ params: decl.type_params,
31
+ location: location || decl.location
32
+ )
33
+
34
+ sub = Substitution.build(param_names, args)
35
+
36
+ case decl
37
+ when AST::Declarations::Class
38
+ unless self_ancestor.name == BuiltinNames::BasicObject.name
39
+ super_ancestor = decl.super_class&.yield_self do |super_class|
40
+ Definition::Ancestor::Instance.new(
41
+ name: absolute_type_name(super_class.name, namespace: namespace, location: location || decl.location),
42
+ args: super_class.args.map {|ty| absolute_type(ty.sub(sub), namespace: namespace) }
43
+ )
44
+ end || Definition::Ancestor::Instance.new(name: BuiltinNames::Object.name, args: [])
45
+
46
+ build_ancestors(super_ancestor, ancestors: ancestors, building_ancestors: building_ancestors)
47
+ end
48
+ end
49
+
50
+ decl.members.each do |member|
51
+ case member
52
+ when AST::Members::Include
53
+ if member.name.class?
54
+ ancestor = Definition::Ancestor::Instance.new(
55
+ name: absolute_type_name(member.name, namespace: namespace, location: member.location),
56
+ args: member.args.map {|ty| absolute_type(ty.sub(sub), namespace: namespace) }
57
+ )
58
+ build_ancestors ancestor, ancestors: ancestors, building_ancestors: building_ancestors, location: member.location
59
+ end
60
+ end
61
+ end
62
+
63
+ ancestors.unshift(self_ancestor)
64
+
65
+ env.each_extension(self_ancestor.name).sort_by {|e| e.extension_name.to_s }.each do |extension|
66
+ InvalidExtensionParameterError.check!(
67
+ type_name: self_ancestor.name,
68
+ extension_name: extension.extension_name,
69
+ extension_params: extension.type_params,
70
+ class_params: self_ancestor.args.map(&:name),
71
+ location: extension.location
72
+ )
73
+
74
+ sub = Substitution.build(extension.type_params, self_ancestor.args)
75
+
76
+ extension.members.each do |member|
77
+ case member
78
+ when AST::Members::Include
79
+ if member.name.class?
80
+ ancestor = Definition::Ancestor::Instance.new(
81
+ name: absolute_type_name(member.name, namespace: namespace, location: member.location),
82
+ args: member.args.map {|ty| absolute_type(ty.sub(sub), namespace: namespace) }
83
+ )
84
+ build_ancestors ancestor, ancestors: ancestors, building_ancestors: building_ancestors, location: member.location
85
+ end
86
+ end
87
+ end
88
+
89
+ extension_ancestor = Definition::Ancestor::ExtensionInstance.new(
90
+ name: extension.name.absolute!,
91
+ extension_name: extension.extension_name,
92
+ args: self_ancestor.args,
93
+ )
94
+ ancestors.unshift(extension_ancestor)
95
+
96
+ extension.members.each do |member|
97
+ case member
98
+ when AST::Members::Prepend
99
+ if member.name.class?
100
+ ancestor = Definition::Ancestor::Instance.new(
101
+ name: absolute_type_name(member.name, namespace: namespace, location: member.location),
102
+ args: member.args.map {|ty| absolute_type(ty.sub(sub), namespace: namespace) }
103
+ )
104
+ build_ancestors ancestor, ancestors: ancestors, building_ancestors: building_ancestors, location: member.location
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ decl.members.each do |member|
111
+ case member
112
+ when AST::Members::Prepend
113
+ ancestor = Definition::Ancestor::Instance.new(
114
+ name: absolute_type_name(member.name, namespace: namespace, location: member.location),
115
+ args: member.args.map {|ty| absolute_type(ty.sub(sub), namespace: namespace) }
116
+ )
117
+ build_ancestors ancestor, ancestors: ancestors, building_ancestors: building_ancestors, location: member.location
118
+ end
119
+ end
120
+ when Definition::Ancestor::Singleton
121
+ case decl
122
+ when AST::Declarations::Class
123
+ if self_ancestor.name == BuiltinNames::BasicObject.name
124
+ class_ancestor = Definition::Ancestor::Instance.new(
125
+ name: BuiltinNames::Class.name,
126
+ args: []
127
+ )
128
+ build_ancestors class_ancestor, ancestors: ancestors, building_ancestors: building_ancestors
129
+ else
130
+ super_ancestor = decl.super_class&.yield_self do |super_class|
131
+ Definition::Ancestor::Singleton.new(
132
+ name: absolute_type_name(super_class.name, namespace: namespace, location: location || decl.location)
133
+ )
134
+ end || Definition::Ancestor::Singleton.new(name: BuiltinNames::Object.name)
135
+
136
+ build_ancestors(super_ancestor, ancestors: ancestors, building_ancestors: building_ancestors)
137
+ end
138
+ when AST::Declarations::Module
139
+ module_ancestor = Definition::Ancestor::Instance.new(
140
+ name: BuiltinNames::Module.name,
141
+ args: []
142
+ )
143
+ build_ancestors module_ancestor, ancestors: ancestors, building_ancestors: building_ancestors
144
+ end
145
+
146
+ decl.members.each do |member|
147
+ case member
148
+ when AST::Members::Extend
149
+ if member.name.class?
150
+ ancestor = Definition::Ancestor::Instance.new(
151
+ name: absolute_type_name(member.name, namespace: namespace, location: member.location),
152
+ args: member.args.map {|ty| absolute_type(ty.sub(sub), namespace: namespace) }
153
+ )
154
+ build_ancestors ancestor, ancestors: ancestors, building_ancestors: building_ancestors, location: member.location
155
+ end
156
+ end
157
+ end
158
+
159
+ ancestors.unshift(self_ancestor)
160
+
161
+ env.each_extension(self_ancestor.name).sort_by {|e| e.extension_name.to_s }.each do |extension|
162
+ extension.members.each do |member|
163
+ case member
164
+ when AST::Members::Extend
165
+ if member.name.class?
166
+ ancestor = Definition::Ancestor::Instance.new(
167
+ name: absolute_type_name(member.name, namespace: namespace, location: member.location),
168
+ args: member.args.map {|ty| absolute_type(ty, namespace: namespace) }
169
+ )
170
+ build_ancestors ancestor, ancestors: ancestors, building_ancestors: building_ancestors, location: member.location
171
+ end
172
+ end
173
+ end
174
+
175
+ extension_ancestor = Definition::Ancestor::ExtensionSingleton.new(
176
+ name: extension.name.absolute!,
177
+ extension_name: extension.extension_name
178
+ )
179
+ ancestors.unshift(extension_ancestor)
180
+ end
181
+ end
182
+
183
+ building_ancestors.pop
184
+
185
+ ancestors
186
+ end
187
+
188
+ def each_member_with_accessibility(members, accessibility: :public)
189
+ members.each do |member|
190
+ case member
191
+ when AST::Members::Public
192
+ accessibility = :public
193
+ when AST::Members::Private
194
+ accessibility = :private
195
+ else
196
+ yield member, accessibility
197
+ end
198
+ end
199
+ end
200
+
201
+ def build_instance(type_name)
202
+ try_cache type_name, cache: instance_cache do
203
+ decl = env.find_class(type_name)
204
+ self_ancestor = Definition::Ancestor::Instance.new(name: type_name,
205
+ args: Types::Variable.build(decl.type_params.each.map(&:name)))
206
+ self_type = Types::ClassInstance.new(name: type_name, args: self_ancestor.args, location: nil)
207
+
208
+ case decl
209
+ when AST::Declarations::Class, AST::Declarations::Module
210
+ ancestors = build_ancestors(self_ancestor)
211
+ definition_pairs = ancestors.map do |ancestor|
212
+ case ancestor
213
+ when Definition::Ancestor::Instance
214
+ [ancestor, build_one_instance(ancestor.name)]
215
+ when Definition::Ancestor::Singleton
216
+ [ancestor, build_one_singleton(ancestor.name)]
217
+ when Definition::Ancestor::ExtensionInstance
218
+ [ancestor, build_one_instance(ancestor.name, extension_name: ancestor.extension_name)]
219
+ when Definition::Ancestor::ExtensionSingleton
220
+ [ancestor, build_one_extension_singleton(ancestor.name, extension_name: ancestor.extension_name)]
221
+ end
222
+ end
223
+
224
+ if decl.is_a?(AST::Declarations::Module)
225
+ if decl.self_type
226
+ self_interface = absolute_type(decl.self_type, namespace: type_name.to_namespace)
227
+ ancestor = Definition::Ancestor::Instance.new(name: self_interface.name,
228
+ args: self_interface.args)
229
+
230
+ definition_pairs.push [ancestor, build_one_instance(ancestor.name)]
231
+ end
232
+ end
233
+
234
+ merge_definitions(definition_pairs, decl: decl, self_type: self_type, ancestors: ancestors)
235
+ end
236
+ end
237
+ end
238
+
239
+ def build_singleton(type_name)
240
+ try_cache type_name, cache: singleton_cache do
241
+ decl = env.find_class(type_name)
242
+ self_ancestor = Definition::Ancestor::Singleton.new(name: type_name)
243
+ self_type = Types::ClassSingleton.new(name: type_name, location: nil)
244
+
245
+ case decl
246
+ when AST::Declarations::Class, AST::Declarations::Module
247
+ ancestors = build_ancestors(self_ancestor)
248
+ definition_pairs = ancestors.map do |ancestor|
249
+ case ancestor
250
+ when Definition::Ancestor::Instance
251
+ [ancestor, build_one_instance(ancestor.name)]
252
+ when Definition::Ancestor::Singleton
253
+ [ancestor, build_one_singleton(ancestor.name)]
254
+ when Definition::Ancestor::ExtensionInstance
255
+ [ancestor, build_one_instance(ancestor.name, extension_name: ancestor.extension_name)]
256
+ when Definition::Ancestor::ExtensionSingleton
257
+ [ancestor, build_one_singleton(ancestor.name, extension_name: ancestor.extension_name)]
258
+ end
259
+ end
260
+
261
+ if decl.is_a?(AST::Declarations::Class)
262
+ definition_pairs.find {|ancestor, _| ancestor == self_ancestor }.tap do |_, definition|
263
+ unless definition.methods[:new]&.implemented_in == decl
264
+ instance_definition = build_instance(type_name)
265
+ class_params = decl.type_params.each.map(&:name)
266
+ initialize_method = instance_definition.methods[:initialize]
267
+ method_types = initialize_method.method_types.map do |method_type|
268
+ case method_type
269
+ when MethodType
270
+ fvs = method_type.free_variables + class_params
271
+ unless fvs.empty?
272
+ param_name_set = Set.new(class_params)
273
+ bound_variables = method_type.type_params
274
+ renamed_types = bound_variables.map do |x|
275
+ if param_name_set.member?(x)
276
+ Types::Variable.fresh(x)
277
+ else
278
+ Types::Variable.new(name: x, location: nil)
279
+ end
280
+ end
281
+ sub = Substitution.build(bound_variables, renamed_types)
282
+ method_type_params = renamed_types.unshift(*class_params)
283
+ else
284
+ sub = Substitution.build([], [])
285
+ method_type_params = method_type.type_params
286
+ end
287
+
288
+ MethodType.new(
289
+ type_params: method_type_params,
290
+ type: method_type.type.sub(sub).with_return_type(instance_definition.self_type),
291
+ block: method_type.block&.yield_self {|ty| ty.sub(sub) },
292
+ location: method_type.location
293
+ )
294
+ end
295
+ end.compact
296
+
297
+ definition.methods[:new] = Definition::Method.new(
298
+ super_method: nil,
299
+ defined_in: nil,
300
+ implemented_in: env.find_class(RBS::BuiltinNames::Class.name),
301
+ method_types: method_types,
302
+ accessibility: :public,
303
+ attributes: [:incompatible],
304
+ annotations: [],
305
+ comment: nil
306
+ )
307
+ end
308
+ end
309
+ end
310
+
311
+ merge_definitions(definition_pairs, decl: decl, self_type: self_type, ancestors: ancestors)
312
+ end
313
+ end
314
+ end
315
+
316
+ def build_one_instance(type_name, extension_name: nil)
317
+ decl = if extension_name
318
+ env.each_extension(type_name).find {|ext| ext.extension_name == extension_name } or
319
+ raise "Unknown extension: #{type_name} (#{extension_name})"
320
+ else
321
+ env.find_class(type_name)
322
+ end
323
+
324
+ case decl
325
+ when AST::Declarations::Interface
326
+ build_interface type_name, decl
327
+ else
328
+ namespace = type_name.to_namespace
329
+
330
+ case decl
331
+ when AST::Declarations::Class, AST::Declarations::Module
332
+ self_type = Types::ClassInstance.new(name: type_name,
333
+ args: Types::Variable.build(decl.type_params.each.map(&:name)),
334
+ location: nil)
335
+ ancestors = [Definition::Ancestor::Instance.new(name: type_name, args: self_type.args)]
336
+ when AST::Declarations::Extension
337
+ self_type = Types::ClassInstance.new(name: type_name, args: Types::Variable.build(decl.type_params), location: nil)
338
+ ancestors = [Definition::Ancestor::ExtensionInstance.new(name: type_name,
339
+ extension_name: extension_name,
340
+ args: self_type.args)]
341
+ end
342
+
343
+ Definition.new(declaration: decl, self_type: self_type, ancestors: ancestors).tap do |definition|
344
+ alias_members = []
345
+
346
+ each_member_with_accessibility(decl.members) do |member, accessibility|
347
+ case member
348
+ when AST::Members::MethodDefinition
349
+ if member.instance?
350
+ name = member.name
351
+ method_types = member.types.map do |method_type|
352
+ case method_type
353
+ when MethodType
354
+ method_type.map_type do |type|
355
+ absolute_type(type, namespace: namespace)
356
+ end
357
+ when :super
358
+ :super
359
+ end
360
+ end
361
+
362
+ DuplicatedMethodDefinitionError.check!(
363
+ decl: decl,
364
+ methods: definition.methods,
365
+ name: name,
366
+ location: member.location
367
+ )
368
+
369
+ attrs = if name == :initialize
370
+ (member.attributes + [:incompatible]).uniq
371
+ else
372
+ member.attributes
373
+ end
374
+
375
+ definition.methods[name] = Definition::Method.new(super_method: nil,
376
+ method_types: method_types,
377
+ defined_in: decl,
378
+ implemented_in: decl,
379
+ accessibility: accessibility,
380
+ attributes: attrs,
381
+ annotations: member.annotations,
382
+ comment: member.comment)
383
+ end
384
+ when AST::Members::AttrReader, AST::Members::AttrAccessor, AST::Members::AttrWriter
385
+ name = member.name
386
+ type = absolute_type(member.type, namespace: namespace)
387
+ ivar_name = case member.ivar_name
388
+ when false
389
+ nil
390
+ else
391
+ member.ivar_name || :"@#{member.name}"
392
+ end
393
+
394
+ if member.is_a?(AST::Members::AttrReader) || member.is_a?(AST::Members::AttrAccessor)
395
+ definition.methods[name] = Definition::Method.new(
396
+ super_method: nil,
397
+ method_types: [
398
+ MethodType.new(
399
+ type_params: [],
400
+ type: Types::Function.empty(type),
401
+ block: nil,
402
+ location: nil
403
+ )
404
+ ],
405
+ defined_in: decl,
406
+ implemented_in: decl,
407
+ accessibility: accessibility,
408
+ attributes: [],
409
+ annotations: member.annotations,
410
+ comment: member.comment
411
+ )
412
+ end
413
+
414
+ if member.is_a?(AST::Members::AttrWriter) || member.is_a?(AST::Members::AttrAccessor)
415
+ definition.methods[:"#{name}="] = Definition::Method.new(
416
+ super_method: nil,
417
+ method_types: [
418
+ MethodType.new(
419
+ type_params: [],
420
+ type: Types::Function.new(required_positionals: [Types::Function::Param.new(name: name, type: type)],
421
+ optional_positionals: [],
422
+ rest_positionals: nil,
423
+ trailing_positionals: [],
424
+ required_keywords: {},
425
+ optional_keywords: {},
426
+ rest_keywords: nil,
427
+ return_type: type),
428
+ block: nil,
429
+ location: nil
430
+ )
431
+ ],
432
+ defined_in: decl,
433
+ implemented_in: decl,
434
+ accessibility: accessibility,
435
+ attributes: [],
436
+ annotations: member.annotations,
437
+ comment: member.comment
438
+ )
439
+ end
440
+
441
+ if ivar_name
442
+ definition.instance_variables[ivar_name] = Definition::Variable.new(
443
+ parent_variable: nil,
444
+ type: type,
445
+ declared_in: decl
446
+ )
447
+ end
448
+
449
+ when AST::Members::Alias
450
+ if member.instance?
451
+ alias_members << member
452
+ end
453
+ when AST::Members::Include
454
+ if member.name.interface?
455
+ absolute_name = absolute_type_name(member.name, namespace: namespace, location: member.location)
456
+ interface_definition = build_one_instance(absolute_name)
457
+ absolute_args = member.args.map {|ty| absolute_type(ty, namespace: namespace) }
458
+
459
+ InvalidTypeApplicationError.check!(
460
+ type_name: absolute_name,
461
+ args: absolute_args,
462
+ params: interface_definition.type_params_decl,
463
+ location: member.location
464
+ )
465
+
466
+ sub = Substitution.build(interface_definition.type_params, absolute_args)
467
+ interface_definition.methods.each do |name, method|
468
+ method_types = method.method_types.map do |method_type|
469
+ method_type.sub(sub).map_type do |type|
470
+ absolute_type(type, namespace: namespace)
471
+ end
472
+ end
473
+
474
+ DuplicatedMethodDefinitionError.check!(
475
+ decl: decl,
476
+ methods: definition.methods,
477
+ name: name,
478
+ location: member.location
479
+ )
480
+
481
+ definition.methods[name] = Definition::Method.new(
482
+ super_method: nil,
483
+ method_types: method_types,
484
+ defined_in: method.defined_in,
485
+ implemented_in: decl,
486
+ accessibility: method.accessibility,
487
+ attributes: [],
488
+ annotations: method.annotations,
489
+ comment: member.comment
490
+ )
491
+ end
492
+ end
493
+ when AST::Members::InstanceVariable
494
+ definition.instance_variables[member.name] = Definition::Variable.new(
495
+ type: absolute_type(member.type, namespace: namespace),
496
+ parent_variable: nil,
497
+ declared_in: decl
498
+ )
499
+ when AST::Members::ClassVariable
500
+ definition.class_variables[member.name] = Definition::Variable.new(
501
+ type: absolute_type(member.type, namespace: namespace),
502
+ parent_variable: nil,
503
+ declared_in: decl
504
+ )
505
+ end
506
+ end
507
+
508
+ alias_members.each do |member|
509
+ UnknownMethodAliasError.check!(
510
+ methods: definition.methods,
511
+ original_name: member.old_name,
512
+ aliased_name: member.new_name,
513
+ location: member.location
514
+ )
515
+
516
+ DuplicatedMethodDefinitionError.check!(
517
+ decl: decl,
518
+ methods: definition.methods,
519
+ name: member.new_name,
520
+ location: member.location
521
+ )
522
+
523
+ # FIXME: may cause a problem if #old_name has super type
524
+ definition.methods[member.new_name] = definition.methods[member.old_name]
525
+ end
526
+
527
+ validate_parameter_variance(
528
+ decl: decl,
529
+ methods: definition.methods
530
+ )
531
+ end
532
+ end
533
+ end
534
+
535
+ def validate_params_with(type_params, result:)
536
+ type_params.each do |param|
537
+ unless param.skip_validation
538
+ unless result.compatible?(param.name, with_annotation: param.variance)
539
+ yield param
540
+ end
541
+ end
542
+ end
543
+ end
544
+
545
+ def validate_parameter_variance(decl:, methods:)
546
+ type_params = case decl
547
+ when AST::Declarations::Extension
548
+ env.find_class(decl.name.absolute!).type_params.rename_to(decl.type_params)
549
+ else
550
+ decl.type_params
551
+ end
552
+
553
+ namespace = decl.name.absolute!.to_namespace
554
+ calculator = VarianceCalculator.new(builder: self)
555
+ param_names = type_params.each.map(&:name)
556
+
557
+ errors = []
558
+
559
+ if decl.is_a?(AST::Declarations::Class)
560
+ if decl.super_class
561
+ absolute_super_name = absolute_type_name(decl.super_class.name, namespace: namespace, location: decl.location)
562
+ absolute_args = decl.super_class.args.map {|type| absolute_type(type, namespace: namespace) }
563
+ result = calculator.in_inherit(name: absolute_super_name, args: absolute_args, variables: param_names)
564
+
565
+ validate_params_with type_params, result: result do |param|
566
+ errors.push InvalidVarianceAnnotationError::InheritanceError.new(
567
+ param: param
568
+ )
569
+ end
570
+ end
571
+ end
572
+
573
+ decl.members.each do |member|
574
+ case member
575
+ when AST::Members::Include
576
+ if member.name.class?
577
+ absolute_module_name = absolute_type_name(member.name, namespace: namespace, location: decl.location)
578
+ absolute_args = member.args.map {|type| absolute_type(type, namespace: namespace) }
579
+ result = calculator.in_inherit(name: absolute_module_name, args: absolute_args, variables: param_names)
580
+
581
+ validate_params_with type_params, result: result do |param|
582
+ errors.push InvalidVarianceAnnotationError::MixinError.new(
583
+ include_member: member,
584
+ param: param
585
+ )
586
+ end
587
+ end
588
+ end
589
+ end
590
+
591
+ methods.each do |name, method|
592
+ method.method_types.each do |method_type|
593
+ case method_type
594
+ when MethodType
595
+ result = calculator.in_method_type(method_type: method_type, variables: param_names)
596
+
597
+ validate_params_with type_params, result: result do |param|
598
+ errors.push InvalidVarianceAnnotationError::MethodTypeError.new(
599
+ method_name: name,
600
+ method_type: method_type,
601
+ param: param
602
+ )
603
+ end
604
+ end
605
+ end
606
+ end
607
+
608
+ unless errors.empty?
609
+ raise InvalidVarianceAnnotationError.new(decl: decl, errors: errors)
610
+ end
611
+ end
612
+
613
+ def build_one_singleton(type_name, extension_name: nil)
614
+ decl = if extension_name
615
+ env.each_extension(type_name).find {|ext| ext.extension_name == extension_name } or
616
+ raise "Unknown extension: #{type_name} (#{extension_name})"
617
+ else
618
+ env.find_class(type_name)
619
+ end
620
+
621
+ namespace = type_name.to_namespace
622
+
623
+ case decl
624
+ when AST::Declarations::Module, AST::Declarations::Class
625
+ self_type = Types::ClassSingleton.new(name: type_name, location: nil)
626
+ ancestors = [Definition::Ancestor::Singleton.new(name: type_name)]
627
+ when AST::Declarations::Extension
628
+ self_type = Types::ClassSingleton.new(name: type_name, location: nil)
629
+ ancestors = [Definition::Ancestor::ExtensionSingleton.new(name: type_name, extension_name: extension_name)]
630
+ end
631
+
632
+ Definition.new(declaration: decl, self_type: self_type, ancestors: ancestors).tap do |definition|
633
+ alias_members = []
634
+
635
+ each_member_with_accessibility(decl.members) do |member, accessibility|
636
+ case member
637
+ when AST::Members::MethodDefinition
638
+ if member.singleton?
639
+ name = member.name
640
+ method_types = member.types.map do |method_type|
641
+ method_type.map_type do |type|
642
+ absolute_type(type, namespace: namespace)
643
+ end
644
+ end
645
+
646
+ DuplicatedMethodDefinitionError.check!(
647
+ decl: decl,
648
+ methods: definition.methods,
649
+ name: name,
650
+ location: member.location
651
+ )
652
+
653
+ definition.methods[name] = Definition::Method.new(super_method: nil,
654
+ method_types: method_types,
655
+ defined_in: decl,
656
+ implemented_in: decl,
657
+ accessibility: accessibility,
658
+ attributes: member.attributes,
659
+ annotations: member.annotations,
660
+ comment: member.comment)
661
+ end
662
+ when AST::Members::Alias
663
+ if member.singleton?
664
+ alias_members << member
665
+ end
666
+ when AST::Members::Extend
667
+ if member.name.interface?
668
+ absolute_name = absolute_type_name(member.name, namespace: namespace, location: member.location)
669
+ interface_definition = build_one_instance(absolute_name)
670
+ absolute_args = member.args.map {|ty| absolute_type(ty, namespace: namespace) }
671
+
672
+ InvalidTypeApplicationError.check!(
673
+ type_name: absolute_name,
674
+ args: absolute_args,
675
+ params: interface_definition.type_params_decl,
676
+ location: member.location
677
+ )
678
+
679
+ sub = Substitution.build(interface_definition.type_params, absolute_args)
680
+ interface_definition.methods.each do |name, method|
681
+ method_types = method.method_types.map do |method_type|
682
+ method_type.sub(sub).map_type do |type|
683
+ absolute_type(type, namespace: namespace)
684
+ end
685
+ end
686
+
687
+ DuplicatedMethodDefinitionError.check!(
688
+ decl: decl,
689
+ methods: definition.methods,
690
+ name: name,
691
+ location: member.location
692
+ )
693
+
694
+ definition.methods[name] = Definition::Method.new(
695
+ super_method: nil,
696
+ method_types: method_types,
697
+ defined_in: method.defined_in,
698
+ implemented_in: decl,
699
+ accessibility: method.accessibility,
700
+ attributes: method.attributes,
701
+ annotations: method.annotations,
702
+ comment: member.comment
703
+ )
704
+ end
705
+ end
706
+ when AST::Members::ClassInstanceVariable
707
+ definition.instance_variables[member.name] = Definition::Variable.new(
708
+ type: absolute_type(member.type, namespace: namespace),
709
+ parent_variable: nil,
710
+ declared_in: decl
711
+ )
712
+ when AST::Members::ClassVariable
713
+ definition.class_variables[member.name] = Definition::Variable.new(
714
+ type: absolute_type(member.type, namespace: namespace),
715
+ parent_variable: nil,
716
+ declared_in: decl
717
+ )
718
+ end
719
+ end
720
+
721
+ alias_members.each do |member|
722
+ UnknownMethodAliasError.check!(
723
+ methods: definition.methods,
724
+ original_name: member.old_name,
725
+ aliased_name: member.new_name,
726
+ location: member.location
727
+ )
728
+
729
+ DuplicatedMethodDefinitionError.check!(
730
+ decl: decl,
731
+ methods: definition.methods,
732
+ name: member.new_name,
733
+ location: member.location
734
+ )
735
+
736
+ # FIXME: may cause a problem if #old_name has super type
737
+ definition.methods[member.new_name] = definition.methods[member.old_name]
738
+ end
739
+ end
740
+ end
741
+
742
+ def merge_definitions(pairs, decl:, self_type:, ancestors:)
743
+ Definition.new(declaration: decl, self_type: self_type, ancestors: ancestors).tap do |definition|
744
+ pairs.reverse_each do |(ancestor, current_definition)|
745
+ sub = case ancestor
746
+ when Definition::Ancestor::Instance, Definition::Ancestor::ExtensionInstance
747
+ Substitution.build(current_definition.type_params, ancestor.args)
748
+ when Definition::Ancestor::Singleton, Definition::Ancestor::ExtensionSingleton
749
+ Substitution.build([], [])
750
+ end
751
+ namespace = current_definition.name.absolute!.to_namespace
752
+
753
+ current_definition.methods.each do |name, method|
754
+ merge_method definition.methods, name, method, sub, namespace
755
+ end
756
+
757
+ current_definition.instance_variables.each do |name, variable|
758
+ merge_variable definition.instance_variables, name, variable
759
+ end
760
+
761
+ current_definition.class_variables.each do |name, variable|
762
+ merge_variable definition.class_variables, name, variable
763
+ end
764
+ end
765
+ end
766
+ end
767
+
768
+ def merge_variable(variables, name, variable)
769
+ super_variable = variables[name]
770
+
771
+ variables[name] = Definition::Variable.new(
772
+ parent_variable: super_variable,
773
+ type: variable.type,
774
+ declared_in: variable.declared_in
775
+ )
776
+ end
777
+
778
+ def merge_method(methods, name, method, sub, namespace)
779
+ super_method = methods[name]
780
+
781
+ methods[name] = Definition::Method.new(
782
+ method_types: method.method_types.flat_map do |method_type|
783
+ case method_type
784
+ when MethodType
785
+ [absolute_type(method_type.sub(sub), namespace: namespace)]
786
+ when :super
787
+ super_method.method_types
788
+ end
789
+ end,
790
+ super_method: super_method,
791
+ defined_in: method.defined_in,
792
+ implemented_in: method.implemented_in,
793
+ accessibility: method.accessibility,
794
+ attributes: method.attributes,
795
+ annotations: method.annotations,
796
+ comment: method.comment
797
+ )
798
+ end
799
+
800
+ def try_cache(type_name, cache:)
801
+ cached = cache[type_name]
802
+
803
+ case cached
804
+ when Definition
805
+ cached
806
+ when false
807
+ raise
808
+ when nil
809
+ cache[type_name] = false
810
+ begin
811
+ cache[type_name] = yield
812
+ rescue => ex
813
+ cache[type_name] = nil
814
+ raise ex
815
+ end
816
+ end
817
+ end
818
+
819
+ def build_interface(type_name, declaration = env.find_class(type_name))
820
+ self_type = Types::Interface.new(
821
+ name: type_name,
822
+ args: declaration.type_params.each.map {|p| Types::Variable.new(name: p.name, location: nil) },
823
+ location: nil
824
+ )
825
+
826
+ namespace = type_name.to_namespace
827
+
828
+ Definition.new(declaration: declaration, self_type: self_type, ancestors: []).tap do |definition|
829
+ alias_members = []
830
+
831
+ declaration.members.each do |member|
832
+ case member
833
+ when AST::Members::Include
834
+ mixin_name = env.absolute_interface_name(member.name, namespace: namespace) || member.name.absolute!
835
+ mixin = build_one_instance(mixin_name)
836
+
837
+ args = member.args.map {|type| absolute_type(type, namespace: namespace) }
838
+ type_params = mixin.declaration.type_params
839
+
840
+ InvalidTypeApplicationError.check!(
841
+ type_name: type_name,
842
+ args: args,
843
+ params: type_params,
844
+ location: member.location
845
+ )
846
+
847
+ sub = Substitution.build(type_params.each.map(&:name), args)
848
+ mixin.methods.each do |name, method|
849
+ definition.methods[name] = method.sub(sub)
850
+ end
851
+ end
852
+ end
853
+
854
+ declaration.members.each do |member|
855
+ case member
856
+ when AST::Members::MethodDefinition
857
+ DuplicatedMethodDefinitionError.check!(
858
+ decl: declaration,
859
+ methods: definition.methods,
860
+ name: member.name,
861
+ location: member.location
862
+ )
863
+
864
+ method = Definition::Method.new(
865
+ super_method: nil,
866
+ method_types: member.types.map do |method_type|
867
+ method_type.map_type {|ty| absolute_type(ty, namespace: namespace) }
868
+ end,
869
+ defined_in: declaration,
870
+ implemented_in: nil,
871
+ accessibility: :public,
872
+ attributes: member.attributes,
873
+ annotations: member.annotations,
874
+ comment: member.comment
875
+ )
876
+ definition.methods[member.name] = method
877
+ when AST::Members::Alias
878
+ alias_members << member
879
+ end
880
+ end
881
+
882
+ alias_members.each do |member|
883
+ UnknownMethodAliasError.check!(
884
+ methods: definition.methods,
885
+ original_name: member.old_name,
886
+ aliased_name: member.new_name,
887
+ location: member.location
888
+ )
889
+
890
+ DuplicatedMethodDefinitionError.check!(
891
+ decl: declaration,
892
+ methods: definition.methods,
893
+ name: member.new_name,
894
+ location: member.location
895
+ )
896
+
897
+ # FIXME: may cause a problem if #old_name has super type
898
+ definition.methods[member.new_name] = definition.methods[member.old_name]
899
+ end
900
+ end
901
+ end
902
+
903
+ def absolute_type(type, namespace:)
904
+ env.absolute_type(type, namespace: namespace) do |type|
905
+ NoTypeFoundError.check!(type.name.absolute!, env: env, location: type.location)
906
+ end
907
+ end
908
+
909
+ def absolute_type_name(type_name, namespace:, location:)
910
+ env.absolute_type_name(type_name, namespace: namespace) do |type_name|
911
+ NoTypeFoundError.check!(type_name.absolute!, env: env, location: location)
912
+ end
913
+ end
914
+
915
+ def expand_alias(type_name)
916
+ absolute_type(env.find_alias(type_name).type, namespace: type_name.namespace)
917
+ end
918
+ end
919
+ end