typeprof 0.21.11 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +15 -31
  3. data/bin/typeprof +5 -0
  4. data/doc/doc.ja.md +134 -0
  5. data/doc/doc.md +136 -0
  6. data/lib/typeprof/cli/cli.rb +180 -0
  7. data/lib/typeprof/cli.rb +2 -133
  8. data/lib/typeprof/code_range.rb +112 -0
  9. data/lib/typeprof/core/ast/base.rb +263 -0
  10. data/lib/typeprof/core/ast/call.rb +251 -0
  11. data/lib/typeprof/core/ast/const.rb +126 -0
  12. data/lib/typeprof/core/ast/control.rb +432 -0
  13. data/lib/typeprof/core/ast/meta.rb +150 -0
  14. data/lib/typeprof/core/ast/method.rb +335 -0
  15. data/lib/typeprof/core/ast/misc.rb +263 -0
  16. data/lib/typeprof/core/ast/module.rb +123 -0
  17. data/lib/typeprof/core/ast/pattern.rb +140 -0
  18. data/lib/typeprof/core/ast/sig_decl.rb +471 -0
  19. data/lib/typeprof/core/ast/sig_type.rb +663 -0
  20. data/lib/typeprof/core/ast/value.rb +319 -0
  21. data/lib/typeprof/core/ast/variable.rb +315 -0
  22. data/lib/typeprof/core/ast.rb +472 -0
  23. data/lib/typeprof/core/builtin.rb +146 -0
  24. data/lib/typeprof/core/env/method.rb +137 -0
  25. data/lib/typeprof/core/env/method_entity.rb +55 -0
  26. data/lib/typeprof/core/env/module_entity.rb +408 -0
  27. data/lib/typeprof/core/env/static_read.rb +155 -0
  28. data/lib/typeprof/core/env/type_alias_entity.rb +27 -0
  29. data/lib/typeprof/core/env/value_entity.rb +32 -0
  30. data/lib/typeprof/core/env.rb +360 -0
  31. data/lib/typeprof/core/graph/box.rb +991 -0
  32. data/lib/typeprof/core/graph/change_set.rb +224 -0
  33. data/lib/typeprof/core/graph/filter.rb +155 -0
  34. data/lib/typeprof/core/graph/vertex.rb +222 -0
  35. data/lib/typeprof/core/graph.rb +3 -0
  36. data/lib/typeprof/core/service.rb +522 -0
  37. data/lib/typeprof/core/type.rb +348 -0
  38. data/lib/typeprof/core/util.rb +81 -0
  39. data/lib/typeprof/core.rb +32 -0
  40. data/lib/typeprof/diagnostic.rb +35 -0
  41. data/lib/typeprof/lsp/messages.rb +430 -0
  42. data/lib/typeprof/lsp/server.rb +177 -0
  43. data/lib/typeprof/lsp/text.rb +69 -0
  44. data/lib/typeprof/lsp/util.rb +61 -0
  45. data/lib/typeprof/lsp.rb +4 -907
  46. data/lib/typeprof/version.rb +1 -1
  47. data/lib/typeprof.rb +4 -18
  48. data/typeprof.gemspec +5 -7
  49. metadata +48 -35
  50. data/.github/dependabot.yml +0 -6
  51. data/.github/workflows/main.yml +0 -39
  52. data/.gitignore +0 -9
  53. data/Gemfile +0 -17
  54. data/Gemfile.lock +0 -41
  55. data/Rakefile +0 -10
  56. data/exe/typeprof +0 -10
  57. data/lib/typeprof/analyzer.rb +0 -2598
  58. data/lib/typeprof/arguments.rb +0 -414
  59. data/lib/typeprof/block.rb +0 -176
  60. data/lib/typeprof/builtin.rb +0 -893
  61. data/lib/typeprof/code-range.rb +0 -177
  62. data/lib/typeprof/config.rb +0 -158
  63. data/lib/typeprof/container-type.rb +0 -912
  64. data/lib/typeprof/export.rb +0 -589
  65. data/lib/typeprof/import.rb +0 -852
  66. data/lib/typeprof/insns-def.rb +0 -65
  67. data/lib/typeprof/iseq.rb +0 -864
  68. data/lib/typeprof/method.rb +0 -355
  69. data/lib/typeprof/type.rb +0 -1140
  70. data/lib/typeprof/utils.rb +0 -212
  71. data/tools/coverage.rb +0 -14
  72. data/tools/setup-insns-def.rb +0 -30
  73. data/typeprof-lsp +0 -3
@@ -0,0 +1,472 @@
1
+ module TypeProf::Core
2
+ class AST
3
+ def self.parse_rb(path, src)
4
+ result = Prism.parse(src)
5
+
6
+ return nil unless result.errors.empty?
7
+
8
+ # comments, errors, magic_comments
9
+ raw_scope = result.value
10
+
11
+ raise unless raw_scope.type == :program_node
12
+
13
+ Fiber[:comments] = result.comments
14
+
15
+ cref = CRef::Toplevel
16
+ lenv = LocalEnv.new(path, cref, {}, [])
17
+
18
+ ProgramNode.new(raw_scope, lenv)
19
+ end
20
+
21
+ #: (untyped, TypeProf::Core::LocalEnv, ?bool) -> TypeProf::Core::AST::Node
22
+ def self.create_node(raw_node, lenv, use_result = true)
23
+ while true
24
+ case raw_node.type
25
+ when :parentheses_node
26
+ raw_node = raw_node.body
27
+ when :implicit_node
28
+ raw_node = raw_node.value
29
+ else
30
+ break
31
+ end
32
+ end
33
+
34
+ case raw_node.type
35
+
36
+ # definition
37
+ when :statements_node then StatementsNode.new(raw_node, lenv, use_result)
38
+ when :module_node then ModuleNode.new(raw_node, lenv, use_result)
39
+ when :class_node then ClassNode.new(raw_node, lenv, use_result)
40
+ when :singleton_class_node then SingletonClassNode.new(raw_node, lenv, use_result)
41
+ when :def_node then DefNode.new(raw_node, lenv, use_result)
42
+ when :alias_method_node then AliasNode.new(raw_node, lenv)
43
+ when :undef_node then UndefNode.new(raw_node, lenv)
44
+
45
+ # control
46
+ when :and_node then AndNode.new(raw_node, lenv)
47
+ when :or_node then OrNode.new(raw_node, lenv)
48
+ when :if_node then IfNode.new(raw_node, lenv)
49
+ when :unless_node then UnlessNode.new(raw_node, lenv)
50
+ when :case_node then CaseNode.new(raw_node, lenv)
51
+ when :case_match_node then CaseMatchNode.new(raw_node, lenv)
52
+ when :while_node then WhileNode.new(raw_node, lenv)
53
+ when :until_node then UntilNode.new(raw_node, lenv)
54
+ when :break_node then BreakNode.new(raw_node, lenv)
55
+ when :next_node then NextNode.new(raw_node, lenv)
56
+ when :redo_node then RedoNode.new(raw_node, lenv)
57
+ when :return_node then ReturnNode.new(raw_node, lenv)
58
+ when :begin_node then BeginNode.new(raw_node, lenv)
59
+ when :retry_node then RetryNode.new(raw_node, lenv)
60
+ when :rescue_modifier_node then RescueModifierNode.new(raw_node, lenv)
61
+
62
+ # constants
63
+ when :constant_read_node, :constant_path_node
64
+ ConstantReadNode.new(raw_node, lenv)
65
+ when :constant_write_node, :constant_path_write_node
66
+ ConstantWriteNode.new(raw_node, AST.create_node(raw_node.value, lenv), lenv)
67
+ when :constant_operator_write_node
68
+ read = ConstantReadNode.new(raw_node, lenv)
69
+ rhs = OperatorNode.new(raw_node, read, lenv)
70
+ ConstantWriteNode.new(raw_node, rhs, lenv)
71
+ when :constant_or_write_node
72
+ read = ConstantReadNode.new(raw_node, lenv)
73
+ rhs = OrNode.new(raw_node, read, raw_node.value, lenv)
74
+ ConstantWriteNode.new(raw_node, rhs, lenv)
75
+ when :constant_and_write_node
76
+ read = ConstantReadNode.new(raw_node, lenv)
77
+ rhs = AndNode.new(raw_node, read, raw_node.value, lenv)
78
+ ConstantWriteNode.new(raw_node, rhs, lenv)
79
+ when :constant_path_operator_write_node
80
+ read = ConstantReadNode.new(raw_node.target, lenv)
81
+ rhs = OperatorNode.new(raw_node, read, lenv)
82
+ ConstantWriteNode.new(raw_node, rhs, lenv)
83
+ when :constant_path_or_write_node
84
+ read = ConstantReadNode.new(raw_node.target, lenv)
85
+ rhs = OrNode.new(raw_node, read, raw_node.value, lenv)
86
+ ConstantWriteNode.new(raw_node, rhs, lenv)
87
+ when :constant_path_and_write_node
88
+ read = ConstantReadNode.new(raw_node.target, lenv)
89
+ rhs = AndNode.new(raw_node, read, raw_node.value, lenv)
90
+ ConstantWriteNode.new(raw_node, rhs, lenv)
91
+
92
+ # variables
93
+ when :local_variable_read_node
94
+ LocalVariableReadNode.new(raw_node, lenv)
95
+ when :local_variable_write_node
96
+ LocalVariableWriteNode.new(raw_node, AST.create_node(raw_node.value, lenv), lenv)
97
+ when :local_variable_operator_write_node
98
+ read = LocalVariableReadNode.new(raw_node, lenv)
99
+ rhs = OperatorNode.new(raw_node, read, lenv)
100
+ LocalVariableWriteNode.new(raw_node, rhs, lenv)
101
+ when :local_variable_or_write_node
102
+ read = LocalVariableReadNode.new(raw_node, lenv)
103
+ rhs = OrNode.new(raw_node, read, raw_node.value, lenv)
104
+ LocalVariableWriteNode.new(raw_node, rhs, lenv)
105
+ when :local_variable_and_write_node
106
+ read = LocalVariableReadNode.new(raw_node, lenv)
107
+ rhs = AndNode.new(raw_node, read, raw_node.value, lenv)
108
+ LocalVariableWriteNode.new(raw_node, rhs, lenv)
109
+ when :instance_variable_read_node
110
+ InstanceVariableReadNode.new(raw_node, lenv)
111
+ when :instance_variable_write_node
112
+ InstanceVariableWriteNode.new(raw_node, AST.create_node(raw_node.value, lenv), lenv)
113
+ when :instance_variable_operator_write_node
114
+ read = InstanceVariableReadNode.new(raw_node, lenv)
115
+ rhs = OperatorNode.new(raw_node, read, lenv)
116
+ InstanceVariableWriteNode.new(raw_node, rhs, lenv)
117
+ when :instance_variable_or_write_node
118
+ read = InstanceVariableReadNode.new(raw_node, lenv)
119
+ rhs = OrNode.new(raw_node, read, raw_node.value, lenv)
120
+ InstanceVariableWriteNode.new(raw_node, rhs, lenv)
121
+ when :instance_variable_and_write_node
122
+ read = InstanceVariableReadNode.new(raw_node, lenv)
123
+ rhs = AndNode.new(raw_node, read, raw_node.value, lenv)
124
+ InstanceVariableWriteNode.new(raw_node, rhs, lenv)
125
+ when :class_variable_read_node
126
+ ClassVariableReadNode.new(raw_node, lenv)
127
+ when :class_variable_write_node
128
+ ClassVariableWriteNode.new(raw_node, AST.create_node(raw_node.value, lenv), lenv)
129
+ when :class_variable_operator_write_node
130
+ read = ClassVariableReadNode.new(raw_node, lenv)
131
+ rhs = OperatorNode.new(raw_node, read, lenv)
132
+ when :class_variable_or_write_node
133
+ read = ClassVariableReadNode.new(raw_node, lenv)
134
+ rhs = OrNode.new(raw_node, read, raw_node.value, lenv)
135
+ ClassVariableWriteNode.new(raw_node, rhs, lenv)
136
+ when :class_variable_and_write_node
137
+ read = ClassVariableReadNode.new(raw_node, lenv)
138
+ rhs = AndNode.new(raw_node, read, raw_node.value, lenv)
139
+ ClassVariableWriteNode.new(raw_node, rhs, lenv)
140
+ when :global_variable_read_node
141
+ GlobalVariableReadNode.new(raw_node, lenv)
142
+ when :global_variable_write_node
143
+ GlobalVariableWriteNode.new(raw_node, AST.create_node(raw_node.value, lenv), lenv)
144
+ when :global_variable_operator_write_node
145
+ read = GlobalVariableReadNode.new(raw_node, lenv)
146
+ rhs = OperatorNode.new(raw_node, read, lenv)
147
+ GlobalVariableWriteNode.new(raw_node, rhs, lenv)
148
+ when :global_variable_or_write_node
149
+ read = GlobalVariableReadNode.new(raw_node, lenv)
150
+ rhs = OrNode.new(raw_node, read, raw_node.value, lenv)
151
+ GlobalVariableWriteNode.new(raw_node, rhs, lenv)
152
+ when :global_variable_and_write_node
153
+ read = GlobalVariableReadNode.new(raw_node, lenv)
154
+ rhs = AndNode.new(raw_node, read, raw_node.value, lenv)
155
+ GlobalVariableWriteNode.new(raw_node, rhs, lenv)
156
+ when :numbered_reference_read_node
157
+ RegexpReferenceReadNode.new(raw_node, lenv)
158
+ when :back_reference_read_node
159
+ RegexpReferenceReadNode.new(raw_node, lenv)
160
+
161
+ # assignment targets
162
+ when :index_operator_write_node
163
+ read = IndexReadNode.new(raw_node, lenv)
164
+ rhs = OperatorNode.new(raw_node, read, lenv)
165
+ IndexWriteNode.new(raw_node, rhs, lenv)
166
+ when :index_or_write_node
167
+ read = IndexReadNode.new(raw_node, lenv)
168
+ rhs = OrNode.new(raw_node, read, raw_node.value, lenv)
169
+ IndexWriteNode.new(raw_node, rhs, lenv)
170
+ when :index_and_write_node
171
+ read = IndexReadNode.new(raw_node, lenv)
172
+ rhs = AndNode.new(raw_node, read, raw_node.value, lenv)
173
+ IndexWriteNode.new(raw_node, rhs, lenv)
174
+ when :call_operator_write_node
175
+ read = CallReadNode.new(raw_node, lenv)
176
+ rhs = OperatorNode.new(raw_node, read, lenv)
177
+ CallWriteNode.new(raw_node, rhs, lenv)
178
+ when :call_or_write_node
179
+ read = CallReadNode.new(raw_node, lenv)
180
+ rhs = OrNode.new(raw_node, read, raw_node.value, lenv)
181
+ CallWriteNode.new(raw_node, rhs, lenv)
182
+ when :call_and_write_node
183
+ read = CallReadNode.new(raw_node, lenv)
184
+ rhs = AndNode.new(raw_node, read, raw_node.value, lenv)
185
+ CallWriteNode.new(raw_node, rhs, lenv)
186
+ when :multi_write_node then MultiWriteNode.new(raw_node, lenv)
187
+ when :match_write_node then MatchWriteNode.new(raw_node, lenv)
188
+
189
+ # value
190
+ when :self_node then SelfNode.new(raw_node, lenv)
191
+ when :nil_node then NilNode.new(raw_node, lenv)
192
+ when :true_node then TrueNode.new(raw_node, lenv)
193
+ when :false_node then FalseNode.new(raw_node, lenv)
194
+ when :integer_node then IntegerNode.new(raw_node, lenv)
195
+ when :float_node then FloatNode.new(raw_node, lenv)
196
+ when :rational_node then RationalNode.new(raw_node, lenv)
197
+ when :imaginary_node then ComplexNode.new(raw_node, lenv)
198
+ when :source_file_node then StringNode.new(raw_node, lenv, "")
199
+ when :source_line_node then IntegerNode.new(raw_node, lenv, 0)
200
+ when :source_encoding_node then SourceEncodingNode.new(raw_node, lenv)
201
+ when :symbol_node then SymbolNode.new(raw_node, lenv)
202
+ when :interpolated_symbol_node then InterpolatedSymbolNode.new(raw_node, lenv)
203
+ when :string_node then StringNode.new(raw_node, lenv, raw_node.content)
204
+ when :interpolated_string_node then InterpolatedStringNode.new(raw_node, lenv)
205
+ when :x_string_node then StringNode.new(raw_node, lenv, "")
206
+ when :interpolated_x_string_node then InterpolatedStringNode.new(raw_node, lenv)
207
+ when :regular_expression_node then RegexpNode.new(raw_node, lenv)
208
+ when :interpolated_regular_expression_node then InterpolatedRegexpNode.new(raw_node, lenv)
209
+ when :match_last_line_node then MatchLastLineNode.new(raw_node, lenv)
210
+ when :interpolated_match_last_line_node then InterpolatedMatchLastLineNode.new(raw_node, lenv)
211
+ when :range_node then RangeNode.new(raw_node, lenv)
212
+ when :array_node then ArrayNode.new(raw_node, lenv)
213
+ when :hash_node then HashNode.new(raw_node, lenv, false)
214
+ when :keyword_hash_node then HashNode.new(raw_node, lenv, true)
215
+ when :lambda_node then LambdaNode.new(raw_node, lenv)
216
+
217
+ # misc
218
+ when :defined_node then DefinedNode.new(raw_node, lenv)
219
+ when :splat_node then SplatNode.new(raw_node, lenv)
220
+ when :for_node then ForNode.new(raw_node, lenv)
221
+ when :alias_global_variable_node then AliasGlobalVariableNode.new(raw_node, lenv)
222
+ when :post_execution_node then PostExecutionNode.new(raw_node, lenv)
223
+ when :flip_flop_node then FlipFlopNode.new(raw_node, lenv)
224
+ when :shareable_constant_node then create_node(raw_node.write, lenv)
225
+ when :match_required_node then MatchRequiredNode.new(raw_node, lenv)
226
+ when :match_predicate_node then MatchPreidcateNode.new(raw_node, lenv)
227
+
228
+ # call
229
+ when :super_node then SuperNode.new(raw_node, lenv)
230
+ when :forwarding_super_node then ForwardingSuperNode.new(raw_node, lenv)
231
+ when :yield_node then YieldNode.new(raw_node, lenv)
232
+ when :call_node
233
+ if !raw_node.receiver
234
+ # TODO: handle them only when it is directly under class or module
235
+ case raw_node.name
236
+ when :include
237
+ return IncludeMetaNode.new(raw_node, lenv)
238
+ when :attr_reader
239
+ return AttrReaderMetaNode.new(raw_node, lenv)
240
+ when :attr_accessor
241
+ return AttrAccessorMetaNode.new(raw_node, lenv)
242
+ end
243
+ end
244
+ CallNode.new(raw_node, lenv)
245
+ else
246
+ pp raw_node
247
+ raise "not supported yet: #{ raw_node.type }"
248
+ end
249
+ end
250
+
251
+ def self.create_target_node(raw_node, lenv)
252
+ dummy_node = DummyRHSNode.new(TypeProf::CodeRange.from_node(raw_node.location), lenv)
253
+ case raw_node.type
254
+ when :local_variable_target_node
255
+ LocalVariableWriteNode.new(raw_node, dummy_node, lenv)
256
+ when :instance_variable_target_node
257
+ InstanceVariableWriteNode.new(raw_node, dummy_node, lenv)
258
+ when :class_variable_target_node
259
+ ClassVariableWriteNode.new(raw_node, dummy_node, lenv)
260
+ when :global_variable_target_node
261
+ GlobalVariableWriteNode.new(raw_node, dummy_node, lenv)
262
+ when :constant_target_node
263
+ ConstantWriteNode.new(raw_node, dummy_node, lenv)
264
+ when :constant_path_target_node
265
+ ConstantWriteNode.new(raw_node, dummy_node, lenv)
266
+ when :index_target_node
267
+ IndexWriteNode.new(raw_node, dummy_node, lenv)
268
+ when :call_target_node
269
+ CallWriteNode.new(raw_node, dummy_node, lenv)
270
+ else
271
+ pp raw_node
272
+ raise "not supported yet: #{ raw_node.type }"
273
+ end
274
+ end
275
+
276
+ def self.create_pattern_node(raw_node, lenv)
277
+ while true
278
+ case raw_node.type
279
+ when :parentheses_node
280
+ raw_node = raw_node.body
281
+ when :implicit_node
282
+ raw_node = raw_node.value
283
+ else
284
+ break
285
+ end
286
+ end
287
+
288
+ case raw_node.type
289
+ when :array_pattern_node then ArrayPatternNode.new(raw_node, lenv)
290
+ when :hash_pattern_node then HashPatternNode.new(raw_node, lenv)
291
+ when :find_pattern_node then FindPatternNode.new(raw_node, lenv)
292
+
293
+ when :alternation_pattern_node then AltPatternNode.new(raw_node, lenv)
294
+
295
+ when :capture_pattern_node then CapturePatternNode.new(raw_node, lenv)
296
+
297
+ when :if_node then IfPatternNode.new(raw_node, lenv)
298
+
299
+ when :pinned_variable_node then PinnedPatternNode.new(raw_node, lenv)
300
+ when :pinned_expression_node then PinnedPatternNode.new(raw_node, lenv)
301
+
302
+ when :local_variable_target_node
303
+ dummy_node = DummyRHSNode.new(TypeProf::CodeRange.from_node(raw_node.location), lenv)
304
+ LocalVariableWriteNode.new(raw_node, dummy_node, lenv)
305
+
306
+ when :constant_read_node, :constant_path_node
307
+ ConstantReadNode.new(raw_node, lenv)
308
+
309
+ when :self_node then SelfNode.new(raw_node, lenv)
310
+ when :nil_node then NilNode.new(raw_node, lenv)
311
+ when :true_node then TrueNode.new(raw_node, lenv)
312
+ when :false_node then FalseNode.new(raw_node, lenv)
313
+ when :integer_node then IntegerNode.new(raw_node, lenv)
314
+ when :float_node then FloatNode.new(raw_node, lenv)
315
+ when :rational_node then RationalNode.new(raw_node, lenv)
316
+ when :imaginary_node then ComplexNode.new(raw_node, lenv)
317
+ when :source_file_node then StringNode.new(raw_node, lenv, "")
318
+ when :source_line_node then IntegerNode.new(raw_node, lenv, 0)
319
+ when :source_encoding_node then SourceEncodingNode.new(raw_node, lenv)
320
+ when :symbol_node then SymbolNode.new(raw_node, lenv)
321
+ when :interpolated_symbol_node then InterpolatedSymbolNode.new(raw_node, lenv)
322
+ when :string_node then StringNode.new(raw_node, lenv, raw_node.content)
323
+ when :interpolated_string_node then InterpolatedStringNode.new(raw_node, lenv)
324
+ when :x_string_node then StringNode.new(raw_node, lenv, "")
325
+ when :interpolated_x_string_node then InterpolatedStringNode.new(raw_node, lenv)
326
+ when :regular_expression_node then RegexpNode.new(raw_node, lenv)
327
+ when :interpolated_regular_expression_node then InterpolatedRegexpNode.new(raw_node, lenv)
328
+
329
+ when :array_node then ArrayNode.new(raw_node, lenv) # for %w[foo bar]
330
+ when :range_node then RangeNode.new(raw_node, lenv) # TODO: support range pattern correctly
331
+
332
+ else
333
+ raise "unknown pattern node type: #{ raw_node.type }"
334
+ end
335
+ end
336
+
337
+ def self.parse_cpath(raw_node, cref)
338
+ names = []
339
+ while raw_node
340
+ case raw_node.type
341
+ when :constant_read_node
342
+ names << raw_node.name
343
+ break
344
+ when :constant_path_node, :constant_path_target_node
345
+ if raw_node.parent
346
+ # temporarily support old Prism https://bugs.ruby-lang.org/issues/20467
347
+ names << (raw_node.respond_to?(:name) ? raw_node.name : raw_node.child.name)
348
+ raw_node = raw_node.parent
349
+ else
350
+ return names.reverse
351
+ end
352
+ when :self_node
353
+ break if cref.scope_level == :class
354
+ return nil
355
+ else
356
+ return nil
357
+ end
358
+ end
359
+ return cref.cpath + names.reverse
360
+ end
361
+
362
+ def self.parse_rbs(path, src)
363
+ _buffer, _directives, raw_decls = RBS::Parser.parse_signature(src)
364
+
365
+ cref = CRef::Toplevel
366
+ lenv = LocalEnv.new(path, cref, {}, [])
367
+
368
+ raw_decls.map do |raw_decl|
369
+ AST.create_rbs_decl(raw_decl, lenv)
370
+ end
371
+ end
372
+
373
+ def self.create_rbs_decl(raw_decl, lenv)
374
+ case raw_decl
375
+ when RBS::AST::Declarations::Class
376
+ SigClassNode.new(raw_decl, lenv)
377
+ when RBS::AST::Declarations::Module
378
+ SigModuleNode.new(raw_decl, lenv)
379
+ when RBS::AST::Declarations::Interface
380
+ SigInterfaceNode.new(raw_decl, lenv)
381
+ when RBS::AST::Declarations::Constant
382
+ SigConstNode.new(raw_decl, lenv)
383
+ when RBS::AST::Declarations::AliasDecl
384
+ when RBS::AST::Declarations::TypeAlias
385
+ SigTypeAliasNode.new(raw_decl, lenv)
386
+ # TODO: check
387
+ when RBS::AST::Declarations::Global
388
+ SigGlobalVariableNode.new(raw_decl, lenv)
389
+ else
390
+ raise "unsupported: #{ raw_decl.class }"
391
+ end
392
+ end
393
+
394
+ def self.create_rbs_member(raw_decl, lenv)
395
+ case raw_decl
396
+ when RBS::AST::Members::MethodDefinition
397
+ SigDefNode.new(raw_decl, lenv)
398
+ when RBS::AST::Members::Include
399
+ SigIncludeNode.new(raw_decl, lenv)
400
+ when RBS::AST::Members::Extend
401
+ when RBS::AST::Members::Public
402
+ when RBS::AST::Members::Private
403
+ when RBS::AST::Members::Alias
404
+ SigAliasNode.new(raw_decl, lenv)
405
+ when RBS::AST::Members::AttrReader
406
+ SigAttrReaderNode.new(raw_decl, lenv)
407
+ when RBS::AST::Members::AttrWriter
408
+ SigAttrWriterNode.new(raw_decl, lenv)
409
+ when RBS::AST::Members::AttrAccessor
410
+ SigAttrAccessorNode.new(raw_decl, lenv)
411
+ when RBS::AST::Declarations::Base
412
+ self.create_rbs_decl(raw_decl, lenv)
413
+ else
414
+ raise "unsupported: #{ raw_decl.class }"
415
+ end
416
+ end
417
+
418
+ def self.create_rbs_func_type(raw_decl, raw_type_params, raw_block, lenv)
419
+ SigFuncType.new(raw_decl, raw_type_params, raw_block, lenv)
420
+ end
421
+
422
+ def self.create_rbs_type(raw_decl, lenv)
423
+ case raw_decl
424
+ when RBS::Types::Bases::Nil
425
+ SigTyBaseNilNode.new(raw_decl, lenv)
426
+ when RBS::Types::Bases::Bool
427
+ SigTyBaseBoolNode.new(raw_decl, lenv)
428
+ when RBS::Types::Bases::Self
429
+ SigTyBaseSelfNode.new(raw_decl, lenv)
430
+ when RBS::Types::Bases::Void
431
+ SigTyBaseVoidNode.new(raw_decl, lenv)
432
+ when RBS::Types::Bases::Any
433
+ SigTyBaseAnyNode.new(raw_decl, lenv)
434
+ when RBS::Types::Bases::Top
435
+ SigTyBaseTopNode.new(raw_decl, lenv)
436
+ when RBS::Types::Bases::Bottom
437
+ SigTyBaseBottomNode.new(raw_decl, lenv)
438
+ when RBS::Types::Bases::Instance
439
+ SigTyBaseInstanceNode.new(raw_decl, lenv)
440
+ when RBS::Types::Bases::Class
441
+ SigTyBaseClassNode.new(raw_decl, lenv)
442
+
443
+ when RBS::Types::Alias
444
+ SigTyAliasNode.new(raw_decl, lenv)
445
+ when RBS::Types::Union
446
+ SigTyUnionNode.new(raw_decl, lenv)
447
+ when RBS::Types::Intersection
448
+ SigTyIntersectionNode.new(raw_decl, lenv)
449
+ when RBS::Types::ClassSingleton
450
+ SigTySingletonNode.new(raw_decl, lenv)
451
+ when RBS::Types::ClassInstance
452
+ SigTyInstanceNode.new(raw_decl, lenv)
453
+ when RBS::Types::Tuple
454
+ SigTyTupleNode.new(raw_decl, lenv)
455
+ when RBS::Types::Record
456
+ SigTyRecordNode.new(raw_decl, lenv)
457
+ when RBS::Types::Interface
458
+ SigTyInterfaceNode.new(raw_decl, lenv)
459
+ when RBS::Types::Proc
460
+ SigTyProcNode.new(raw_decl, lenv)
461
+ when RBS::Types::Variable
462
+ SigTyVarNode.new(raw_decl, lenv)
463
+ when RBS::Types::Optional
464
+ SigTyOptionalNode.new(raw_decl, lenv)
465
+ when RBS::Types::Literal
466
+ SigTyLiteralNode.new(raw_decl, lenv)
467
+ else
468
+ raise "unknown RBS type: #{ raw_decl.class }"
469
+ end
470
+ end
471
+ end
472
+ end
@@ -0,0 +1,146 @@
1
+ module TypeProf::Core
2
+ class Builtin
3
+ def initialize(genv)
4
+ @genv = genv
5
+ end
6
+
7
+ def class_new(changes, node, ty, a_args, ret)
8
+ if ty.is_a?(Type::Singleton)
9
+ ty = ty.get_instance_type(@genv)
10
+ recv = Source.new(ty)
11
+ changes.add_method_call_box(@genv, recv, :initialize, a_args, false)
12
+ changes.add_edge(@genv, Source.new(ty), ret)
13
+ end
14
+ true
15
+ end
16
+
17
+ def object_class(changes, node, ty, a_args, ret)
18
+ ty = ty.base_type(@genv)
19
+ mod = ty.is_a?(Type::Instance) ? ty.mod : @genv.mod_class
20
+ ty = Type::Singleton.new(@genv, mod)
21
+ vtx = Source.new(ty)
22
+ changes.add_edge(@genv, vtx, ret)
23
+ true
24
+ end
25
+
26
+ def proc_call(changes, node, ty, a_args, ret)
27
+ case ty
28
+ when Type::Proc
29
+ ty.block.accept_args(@genv, changes, a_args.positionals, ret, false)
30
+ true
31
+ else
32
+ false
33
+ end
34
+ end
35
+
36
+ def array_aref(changes, node, ty, a_args, ret)
37
+ if a_args.positionals.size == 1
38
+ case ty
39
+ when Type::Array
40
+ idx = node.positional_args[0]
41
+ if idx.is_a?(AST::IntegerNode)
42
+ idx = idx.lit
43
+ else
44
+ idx = nil
45
+ end
46
+ vtx = ty.get_elem(@genv, idx)
47
+ changes.add_edge(@genv, vtx, ret)
48
+ true
49
+ else
50
+ false
51
+ end
52
+ else
53
+ false
54
+ end
55
+ end
56
+
57
+ def array_aset(changes, node, ty, a_args, ret)
58
+ if a_args.positionals.size == 2
59
+ case ty
60
+ when Type::Array
61
+ val = a_args.positionals[1]
62
+ idx = node.positional_args[0]
63
+ if idx.is_a?(AST::IntegerNode) && ty.get_elem(@genv, idx.lit)
64
+ changes.add_edge(@genv, val, ty.get_elem(@genv, idx.lit))
65
+ else
66
+ changes.add_edge(@genv, val, ty.get_elem(@genv))
67
+ end
68
+ true
69
+ else
70
+ false
71
+ end
72
+ else
73
+ false
74
+ end
75
+ end
76
+
77
+ def array_push(changes, node, ty, a_args, ret)
78
+ if a_args.positionals.size == 1
79
+ if ty.is_a?(Type::Array)
80
+ val = a_args.positionals[0]
81
+ changes.add_edge(@genv, val, ty.get_elem(@genv))
82
+ end
83
+ recv = Source.new(ty)
84
+ changes.add_edge(@genv, recv, ret)
85
+ else
86
+ false
87
+ end
88
+ end
89
+
90
+ def hash_aref(changes, node, ty, a_args, ret)
91
+ if a_args.positionals.size == 1
92
+ case ty
93
+ when Type::Hash
94
+ idx = node.positional_args[0]
95
+ idx = idx.is_a?(AST::SymbolNode) ? idx.lit : nil
96
+ changes.add_edge(@genv, ty.get_value(idx), ret)
97
+ true
98
+ else
99
+ false
100
+ end
101
+ else
102
+ false
103
+ end
104
+ end
105
+
106
+ def hash_aset(changes, node, ty, a_args, ret)
107
+ if a_args.positionals.size == 2
108
+ case ty
109
+ when Type::Hash
110
+ val = a_args.positionals[1]
111
+ idx = node.positional_args[0]
112
+ if idx.is_a?(AST::SymbolNode) && ty.get_value(idx.lit)
113
+ # TODO: how to handle new key?
114
+ changes.add_edge(@genv, val, ty.get_value(idx.lit))
115
+ else
116
+ # TODO: literal_pairs will not be updated
117
+ changes.add_edge(@genv, a_args.positionals[0], ty.get_key)
118
+ changes.add_edge(@genv, val, ty.get_value)
119
+ end
120
+ changes.add_edge(@genv, val, ret)
121
+ true
122
+ else
123
+ false
124
+ end
125
+ else
126
+ false
127
+ end
128
+ end
129
+
130
+ def deploy
131
+ {
132
+ class_new: [[:Class], false, :new],
133
+ object_class: [[:Object], false, :class],
134
+ proc_call: [[:Proc], false, :call],
135
+ array_aref: [[:Array], false, :[]],
136
+ array_aset: [[:Array], false, :[]=],
137
+ array_push: [[:Array], false, :<<],
138
+ hash_aref: [[:Hash], false, :[]],
139
+ hash_aset: [[:Hash], false, :[]=],
140
+ }.each do |key, (cpath, singleton, mid)|
141
+ me = @genv.resolve_method(cpath, singleton, mid)
142
+ me.builtin = method(key)
143
+ end
144
+ end
145
+ end
146
+ end