typeprof 0.21.11 → 0.30.0

Sign up to get free protection for your applications and to get access to all the features.
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,663 @@
1
+ module TypeProf::Core
2
+ class AST
3
+ class SigFuncType < Node
4
+ def initialize(raw_decl, raw_type_params, raw_block, lenv)
5
+ super(raw_decl, lenv)
6
+ if raw_block
7
+ @block_required = raw_block.required
8
+ @block = AST.create_rbs_func_type(raw_block, nil, nil, lenv)
9
+ else
10
+ @block_required = false
11
+ @block = nil
12
+ end
13
+
14
+ # TODO?: param.variance, param.unchecked, param.upper_bound
15
+ @type_params = raw_type_params ? raw_type_params.map {|param| param.name } : nil
16
+
17
+ if raw_decl.type.is_a?(RBS::Types::Function)
18
+ @req_positionals = raw_decl.type.required_positionals.map do |ty|
19
+ raise "unsupported argument type: #{ ty.class }" if !ty.is_a?(RBS::Types::Function::Param)
20
+ AST.create_rbs_type(ty.type, lenv)
21
+ end
22
+ @post_positionals = raw_decl.type.trailing_positionals.map do |ty|
23
+ raise "unsupported argument type: #{ ty.class }" if !ty.is_a?(RBS::Types::Function::Param)
24
+ AST.create_rbs_type(ty.type, lenv)
25
+ end
26
+ @opt_positionals = raw_decl.type.optional_positionals.map do |ty|
27
+ raise "unsupported argument type: #{ ty.class }" if !ty.is_a?(RBS::Types::Function::Param)
28
+ AST.create_rbs_type(ty.type, lenv)
29
+ end
30
+ param = raw_decl.type.rest_positionals
31
+ @rest_positionals = param ? AST.create_rbs_type(param.type, lenv) : nil
32
+
33
+ @req_keywords = raw_decl.type.required_keywords.to_h do |key, ty|
34
+ raise "unsupported argument type: #{ ty.class }" if !ty.is_a?(RBS::Types::Function::Param)
35
+ [key, AST.create_rbs_type(ty.type, lenv)]
36
+ end
37
+ @opt_keywords = raw_decl.type.optional_keywords.to_h do |key, ty|
38
+ raise "unsupported argument type: #{ ty.class }" if !ty.is_a?(RBS::Types::Function::Param)
39
+ [key, AST.create_rbs_type(ty.type, lenv)]
40
+ end
41
+ param = raw_decl.type.rest_keywords
42
+ @rest_keywords = param ? AST.create_rbs_type(param.type, lenv) : nil
43
+ else
44
+ # RBS::Types::UntypedFunction
45
+ @req_positionals = []
46
+ @post_positionals = []
47
+ @opt_positionals = []
48
+ @rest_positionals = SigTyBaseAnyNode.new(raw_decl, lenv)
49
+ @req_keywords = {}
50
+ @opt_keywords = {}
51
+ @rest_keywords = nil
52
+ end
53
+
54
+ @return_type = AST.create_rbs_type(raw_decl.type.return_type, lenv)
55
+ end
56
+
57
+ attr_reader :type_params, :block, :block_required
58
+ attr_reader :req_positionals
59
+ attr_reader :post_positionals
60
+ attr_reader :opt_positionals
61
+ attr_reader :rest_positionals
62
+ attr_reader :req_keywords
63
+ attr_reader :opt_keywords
64
+ attr_reader :rest_keywords
65
+ attr_reader :return_type
66
+
67
+ def subnodes = {
68
+ block:,
69
+ req_positionals:,
70
+ post_positionals:,
71
+ opt_positionals:,
72
+ rest_positionals:,
73
+ req_keywords:,
74
+ opt_keywords:,
75
+ rest_keywords:,
76
+ return_type:,
77
+ }
78
+ def attrs = { type_params:, block_required: }
79
+ end
80
+
81
+ class SigTyNode < Node
82
+ def covariant_vertex(genv, changes, subst)
83
+ vtx = changes.new_covariant_vertex(genv, self)
84
+ covariant_vertex0(genv, changes, vtx, subst)
85
+ vtx
86
+ end
87
+
88
+ def contravariant_vertex(genv, changes, subst)
89
+ vtx = changes.new_contravariant_vertex(genv, self)
90
+ contravariant_vertex0(genv, changes, vtx, subst)
91
+ vtx
92
+ end
93
+ end
94
+
95
+ class SigTyBaseBoolNode < SigTyNode
96
+ def covariant_vertex0(genv, changes, vtx, subst)
97
+ changes.add_edge(genv, Source.new(genv.true_type, genv.false_type), vtx)
98
+ end
99
+
100
+ def contravariant_vertex0(genv, changes, vtx, subst)
101
+ changes.add_edge(genv, Source.new(genv.true_type, genv.false_type), vtx)
102
+ end
103
+
104
+ def show
105
+ "bool"
106
+ end
107
+ end
108
+
109
+ class SigTyBaseNilNode < SigTyNode
110
+ def covariant_vertex0(genv, changes, vtx, subst)
111
+ changes.add_edge(genv, Source.new(genv.nil_type), vtx)
112
+ end
113
+
114
+ def contravariant_vertex0(genv, changes, vtx, subst)
115
+ changes.add_edge(genv, Source.new(genv.nil_type), vtx)
116
+ end
117
+
118
+ def show
119
+ "nil"
120
+ end
121
+ end
122
+
123
+ class SigTyBaseSelfNode < SigTyNode
124
+ def covariant_vertex0(genv, changes, vtx, subst)
125
+ changes.add_edge(genv, subst[:"*self"], vtx)
126
+ end
127
+
128
+ def contravariant_vertex0(genv, changes, vtx, subst)
129
+ changes.add_edge(genv, subst[:"*self"], vtx)
130
+ end
131
+
132
+ def show
133
+ "self"
134
+ end
135
+ end
136
+
137
+ class SigTyBaseVoidNode < SigTyNode
138
+ def covariant_vertex0(genv, changes, vtx, subst)
139
+ changes.add_edge(genv, Source.new(genv.obj_type), vtx)
140
+ end
141
+
142
+ def contravariant_vertex0(genv, changes, vtx, subst)
143
+ changes.add_edge(genv, Source.new(genv.obj_type), vtx)
144
+ end
145
+
146
+ def show
147
+ "void"
148
+ end
149
+ end
150
+
151
+ class SigTyBaseAnyNode < SigTyNode
152
+ def covariant_vertex0(genv, changes, vtx, subst)
153
+ end
154
+
155
+ def contravariant_vertex0(genv, changes, vtx, subst)
156
+ #Source.new(genv.obj_type).add_edge(genv, vtx) # TODO
157
+ end
158
+
159
+ def show
160
+ "untyped"
161
+ end
162
+ end
163
+
164
+ class SigTyBaseTopNode < SigTyNode
165
+ def covariant_vertex0(genv, changes, vtx, subst)
166
+ # TODO
167
+ end
168
+
169
+ def contravariant_vertex0(genv, changes, vtx, subst)
170
+ # TODO
171
+ end
172
+
173
+ def show
174
+ "top"
175
+ end
176
+ end
177
+
178
+ class SigTyBaseBottomNode < SigTyNode
179
+ def covariant_vertex0(genv, changes, vtx, subst)
180
+ changes.add_edge(genv, Source.new(Type::Bot.new(genv)), vtx)
181
+ end
182
+
183
+ def contravariant_vertex0(genv, changes, vtx, subst)
184
+ changes.add_edge(genv, Source.new(Type::Bot.new(genv)), vtx)
185
+ end
186
+
187
+ def show
188
+ "bot"
189
+ end
190
+ end
191
+
192
+ class SigTyBaseInstanceNode < SigTyNode
193
+ def covariant_vertex0(genv, changes, vtx, subst)
194
+ changes.add_edge(genv, subst[:"*instance"], vtx)
195
+ end
196
+
197
+ def contravariant_vertex0(genv, changes, vtx, subst)
198
+ changes.add_edge(genv, subst[:"*instance"], vtx)
199
+ end
200
+
201
+ def show
202
+ "instance"
203
+ end
204
+ end
205
+
206
+ class SigTyBaseClassNode < SigTyNode
207
+ def covariant_vertex0(genv, changes, vtx, subst)
208
+ changes.add_edge(genv, subst[:"*class"], vtx)
209
+ end
210
+
211
+ def contravariant_vertex0(genv, changes, vtx, subst)
212
+ changes.add_edge(genv, subst[:"*class"], vtx)
213
+ end
214
+
215
+ def show
216
+ "class"
217
+ end
218
+ end
219
+
220
+ class SigTyAliasNode < SigTyNode
221
+ def initialize(raw_decl, lenv)
222
+ super(raw_decl, lenv)
223
+ name = raw_decl.name
224
+ @cpath = name.namespace.path
225
+ @toplevel = name.namespace.absolute?
226
+ @name = name.name
227
+ @args = raw_decl.args.map {|arg| AST.create_rbs_type(arg, lenv) }
228
+ end
229
+
230
+ attr_reader :cpath, :toplevel, :name, :args
231
+ def subnodes = { args: }
232
+ def attrs = { cpath:, toplevel:, name: }
233
+
234
+ def define0(genv)
235
+ @args.each {|arg| arg.define(genv) }
236
+
237
+ static_reads = []
238
+ if @cpath.empty?
239
+ static_reads << BaseTypeAliasRead.new(genv, @name, @toplevel ? CRef::Toplevel : @lenv.cref)
240
+ else
241
+ static_reads << BaseConstRead.new(genv, @cpath.first, @toplevel ? CRef::Toplevel : @lenv.cref)
242
+ @cpath[1..].each do |cname|
243
+ static_reads << ScopedConstRead.new(cname, static_reads.last)
244
+ end
245
+ static_reads << ScopedTypeAliasRead.new(@name, static_reads.last)
246
+ end
247
+ static_reads
248
+ end
249
+
250
+ def undefine0(genv)
251
+ @static_ret.each do |static_read|
252
+ static_read.destroy(genv)
253
+ end
254
+ @args.each {|arg| arg.undefine(genv) }
255
+ end
256
+
257
+ def covariant_vertex0(genv, changes, vtx, subst)
258
+ changes.add_depended_static_read(@static_ret.last)
259
+ tae = @static_ret.last.type_alias_entity
260
+ if tae && tae.exist?
261
+ # need to check tae decls are all consistent?
262
+ decl = tae.decls.each {|decl| break decl }
263
+ subst0 = subst.dup
264
+ # raise if decl.params.size != @args.size # ?
265
+ decl.params.zip(@args) do |param, arg|
266
+ subst0[param] = arg.covariant_vertex(genv, changes, subst0) # passing subst0 is ok?
267
+ end
268
+ tae.type.covariant_vertex0(genv, changes, vtx, subst0)
269
+ end
270
+ end
271
+
272
+ def contravariant_vertex0(genv, changes, vtx, subst)
273
+ changes.add_depended_static_read(@static_ret.last)
274
+ tae = @static_ret.last.type_alias_entity
275
+ if tae && tae.exist?
276
+ # need to check tae decls are all consistent?
277
+ decl = tae.decls.each {|decl| break decl }
278
+ subst0 = subst.dup
279
+ # raise if decl.params.size != @args.size # ?
280
+ decl.params.zip(@args) do |param, arg|
281
+ subst0[param] = arg.contravariant_vertex(genv, changes, subst0)
282
+ end
283
+ tae.type.contravariant_vertex0(genv, changes, vtx, subst0)
284
+ end
285
+ end
286
+
287
+ def show
288
+ "(...alias...)"
289
+ end
290
+ end
291
+
292
+ class SigTyUnionNode < SigTyNode
293
+ def initialize(raw_decl, lenv)
294
+ super(raw_decl, lenv)
295
+ @types = (raw_decl.types || []).map {|type| AST.create_rbs_type(type, lenv) }
296
+ end
297
+
298
+ attr_reader :types
299
+
300
+ def subnodes = { types: }
301
+
302
+ def covariant_vertex0(genv, changes, vtx, subst)
303
+ @types.each do |type|
304
+ type.covariant_vertex0(genv, changes, vtx, subst)
305
+ end
306
+ end
307
+
308
+ def contravariant_vertex0(genv, changes, vtx, subst)
309
+ @types.each do |type|
310
+ type.contravariant_vertex0(genv, changes, vtx, subst)
311
+ end
312
+ end
313
+
314
+ def show
315
+ @types.map {|ty| ty.show }.join(" | ")
316
+ end
317
+ end
318
+
319
+ class SigTyIntersectionNode < SigTyNode
320
+ def covariant_vertex0(genv, changes, vtx, subst)
321
+ #raise NotImplementedError
322
+ end
323
+
324
+ def contravariant_vertex0(genv, changes, vtx, subst)
325
+ #raise NotImplementedError
326
+ end
327
+
328
+ def show
329
+ "(...intersection...)"
330
+ end
331
+ end
332
+
333
+ class SigTySingletonNode < SigTyNode
334
+ def initialize(raw_decl, lenv)
335
+ super(raw_decl, lenv)
336
+ name = raw_decl.name
337
+ @cpath = name.namespace.path + [name.name]
338
+ @toplevel = name.namespace.absolute?
339
+ end
340
+
341
+ attr_reader :cpath, :toplevel
342
+ def attrs = { cpath:, toplevel: }
343
+
344
+ def define0(genv)
345
+ const_reads = []
346
+ const_read = BaseConstRead.new(genv, @cpath.first, @toplevel ? CRef::Toplevel : @lenv.cref)
347
+ const_reads << const_read
348
+ unless @cpath.empty?
349
+ @cpath[1..].each do |cname|
350
+ const_read = ScopedConstRead.new(cname, const_read)
351
+ const_reads << const_read
352
+ end
353
+ end
354
+ const_reads
355
+ end
356
+
357
+ def undefine0(genv)
358
+ @static_ret.each do |const_read|
359
+ const_read.destroy(genv)
360
+ end
361
+ end
362
+
363
+ def covariant_vertex0(genv, changes, vtx, subst)
364
+ # TODO: type.args
365
+ changes.add_depended_static_read(@static_ret.last)
366
+ cpath = @static_ret.last.cpath
367
+ return unless cpath
368
+ mod = genv.resolve_cpath(cpath)
369
+ changes.add_edge(genv, Source.new(Type::Singleton.new(genv, mod)), vtx)
370
+ end
371
+
372
+ def contravariant_vertex0(genv, changes, vtx, subst)
373
+ # TODO: type.args
374
+ changes.add_depended_static_read(@static_ret.last)
375
+ cpath = @static_ret.last.cpath
376
+ return unless cpath
377
+ mod = genv.resolve_cpath(cpath)
378
+ changes.add_edge(genv, Source.new(Type::Singleton.new(genv, mod)), vtx)
379
+ end
380
+
381
+ def show
382
+ s = "::#{ @cpath.join("::") }"
383
+ if !@args.empty?
384
+ s << "[...]"
385
+ end
386
+ "singleton(#{ s })"
387
+ end
388
+ end
389
+
390
+ class SigTyInstanceNode < SigTyNode
391
+ def initialize(raw_decl, lenv)
392
+ super(raw_decl, lenv)
393
+ name = raw_decl.name
394
+ @cpath = name.namespace.path + [name.name]
395
+ @toplevel = name.namespace.absolute? # "::Foo" or "Foo"
396
+ @args = raw_decl.args.map {|arg| AST.create_rbs_type(arg, lenv) }
397
+ end
398
+
399
+ attr_reader :cpath, :toplevel, :args
400
+ def subnodes = { args: }
401
+ def attrs = { cpath:, toplevel: }
402
+
403
+ def define0(genv)
404
+ @args.each {|arg| arg.define(genv) }
405
+ const_reads = []
406
+ const_read = BaseConstRead.new(genv, @cpath.first, @toplevel ? CRef::Toplevel : @lenv.cref)
407
+ const_reads << const_read
408
+ unless @cpath.empty?
409
+ @cpath[1..].each do |cname|
410
+ const_read = ScopedConstRead.new(cname, const_read)
411
+ const_reads << const_read
412
+ end
413
+ end
414
+ const_reads
415
+ end
416
+
417
+ def undefine0(genv)
418
+ @static_ret.each do |const_read|
419
+ const_read.destroy(genv)
420
+ end
421
+ @args.each {|arg| arg.undefine(genv) }
422
+ end
423
+
424
+ def covariant_vertex0(genv, changes, vtx, subst)
425
+ changes.add_depended_static_read(@static_ret.last)
426
+ cpath = @static_ret.last.cpath
427
+ return unless cpath
428
+ mod = genv.resolve_cpath(cpath)
429
+ args = @args.map {|arg| arg.covariant_vertex(genv, changes, subst) }
430
+ changes.add_edge(genv, Source.new(Type::Instance.new(genv, mod, args)), vtx)
431
+ end
432
+
433
+ def contravariant_vertex0(genv, changes, vtx, subst)
434
+ changes.add_depended_static_read(@static_ret.last)
435
+ cpath = @static_ret.last.cpath
436
+ return unless cpath
437
+ mod = genv.resolve_cpath(cpath)
438
+ args = @args.map {|arg| arg.contravariant_vertex(genv, changes, subst) }
439
+ changes.add_edge(genv, Source.new(Type::Instance.new(genv, mod, args)), vtx)
440
+ end
441
+
442
+ def show
443
+ s = "::#{ @cpath.join("::") }"
444
+ if !@args.empty?
445
+ s << "[...]"
446
+ end
447
+ s
448
+ end
449
+ end
450
+
451
+ class SigTyTupleNode < SigTyNode
452
+ def initialize(raw_decl, lenv)
453
+ super(raw_decl, lenv)
454
+ @types = raw_decl.types.map {|type| AST.create_rbs_type(type, lenv) }
455
+ end
456
+
457
+ attr_reader :types
458
+ def subnodes = { types: }
459
+
460
+ def covariant_vertex0(genv, changes, vtx, subst)
461
+ unified_elem = changes.new_covariant_vertex(genv, [self, :Elem]) # TODO
462
+ elems = @types.map do |type|
463
+ nvtx = type.covariant_vertex(genv, changes, subst)
464
+ changes.add_edge(genv, nvtx, unified_elem)
465
+ nvtx
466
+ end
467
+ changes.add_edge(genv, Source.new(Type::Array.new(genv, elems, genv.gen_ary_type(unified_elem))), vtx)
468
+ end
469
+
470
+ def contravariant_vertex0(genv, changes, vtx, subst)
471
+ unified_elem = changes.new_contravariant_vertex(genv, [self, :Elem]) # TODO
472
+ elems = @types.map do |type|
473
+ nvtx = type.contravariant_vertex(genv, changes, subst)
474
+ changes.add_edge(genv, nvtx, unified_elem)
475
+ nvtx
476
+ end
477
+ changes.add_edge(genv, Source.new(Type::Array.new(genv, elems, genv.gen_ary_type(unified_elem))), vtx)
478
+ end
479
+
480
+ def show
481
+ "[#{ @types.map {|ty| ty.show }.join(", ") }]"
482
+ end
483
+ end
484
+
485
+ class SigTyRecordNode < SigTyNode
486
+ def covariant_vertex0(genv, changes, vtx, subst)
487
+ raise NotImplementedError
488
+ end
489
+
490
+ def contravariant_vertex0(genv, changes, vtx, subst)
491
+ raise NotImplementedError
492
+ end
493
+
494
+ def show
495
+ "(...record...)"
496
+ end
497
+ end
498
+
499
+ class SigTyVarNode < SigTyNode
500
+ def initialize(raw_decl, lenv)
501
+ super(raw_decl, lenv)
502
+ @var = raw_decl.name
503
+ end
504
+
505
+ attr_reader :var
506
+
507
+ def attrs = { var: }
508
+
509
+ def covariant_vertex0(genv, changes, vtx, subst)
510
+ raise "unknown type variable: #{ @var }" unless subst[@var]
511
+ changes.add_edge(genv, subst[@var], vtx)
512
+ end
513
+
514
+ def contravariant_vertex0(genv, changes, vtx, subst)
515
+ raise "unknown type variable: #{ @var }" unless subst[@var]
516
+ changes.add_edge(genv, Source.new(Type::Var.new(genv, @var, subst[@var])), vtx)
517
+ end
518
+
519
+ def show
520
+ "#{ @var }"
521
+ end
522
+ end
523
+
524
+ class SigTyOptionalNode < SigTyNode
525
+ def initialize(raw_decl, lenv)
526
+ super(raw_decl, lenv)
527
+ @type = AST.create_rbs_type(raw_decl.type, lenv)
528
+ end
529
+
530
+ attr_reader :type
531
+ def subnodes = { type: }
532
+
533
+ def covariant_vertex0(genv, changes, vtx, subst)
534
+ @type.covariant_vertex0(genv, changes, vtx, subst)
535
+ changes.add_edge(genv, Source.new(genv.nil_type), vtx)
536
+ end
537
+
538
+ def contravariant_vertex0(genv, changes, vtx, subst)
539
+ @type.contravariant_vertex0(genv, changes, vtx, subst)
540
+ changes.add_edge(genv, Source.new(genv.nil_type), vtx)
541
+ end
542
+
543
+ def show
544
+ s = @type.show
545
+ if @type.is_a?(SigTyIntersectionNode) || @type.is_a?(SigTyUnionNode)
546
+ s = "(#{ s })"
547
+ end
548
+ s + "?"
549
+ end
550
+ end
551
+
552
+ class SigTyLiteralNode < SigTyNode
553
+ def initialize(raw_decl, lenv)
554
+ super(raw_decl, lenv)
555
+ @lit = raw_decl.literal
556
+ end
557
+
558
+ attr_reader :lit
559
+ def attrs = { lit: }
560
+
561
+ def get_type(genv)
562
+ case @lit
563
+ when ::Symbol
564
+ Type::Symbol.new(genv, @lit)
565
+ when ::Integer then genv.int_type
566
+ when ::String then genv.str_type
567
+ when ::TrueClass then genv.true_type
568
+ when ::FalseClass then genv.false_type
569
+ else
570
+ raise "unknown RBS literal: #{ @lit.inspect }"
571
+ end
572
+ end
573
+
574
+ def covariant_vertex0(genv, changes, vtx, subst)
575
+ changes.add_edge(genv, Source.new(get_type(genv)), vtx)
576
+ end
577
+
578
+ def contravariant_vertex0(genv, changes, vtx, subst)
579
+ changes.add_edge(genv, Source.new(get_type(genv)), vtx)
580
+ end
581
+
582
+ def show
583
+ @lit.inspect
584
+ end
585
+ end
586
+
587
+ class SigTyProcNode < SigTyNode
588
+ def covariant_vertex0(genv, changes, vtx, subst)
589
+ raise NotImplementedError
590
+ end
591
+
592
+ def contravariant_vertex0(genv, changes, vtx, subst)
593
+ Source.new()
594
+ end
595
+
596
+ def show
597
+ "(...proc...)"
598
+ end
599
+ end
600
+
601
+ class SigTyInterfaceNode < SigTyNode
602
+ def initialize(raw_decl, lenv)
603
+ super(raw_decl, lenv)
604
+ name = raw_decl.name
605
+ @cpath = name.namespace.path + [name.name]
606
+
607
+ @toplevel = name.namespace.absolute? # "::Foo" or "Foo"
608
+ @args = raw_decl.args.map {|arg| AST.create_rbs_type(arg, lenv) }
609
+ end
610
+
611
+ attr_reader :cpath, :toplevel, :args
612
+ def subnodes = { args: }
613
+ def attrs = { cpath:, toplevel: }
614
+
615
+ def define0(genv)
616
+ @args.each {|arg| arg.define(genv) }
617
+ const_reads = []
618
+ const_read = BaseConstRead.new(genv, @cpath.first, @toplevel ? CRef::Toplevel : @lenv.cref)
619
+ const_reads << const_read
620
+ unless @cpath.empty?
621
+ @cpath[1..].each do |cname|
622
+ const_read = ScopedConstRead.new(cname, const_read)
623
+ const_reads << const_read
624
+ end
625
+ end
626
+ const_reads
627
+ end
628
+
629
+ def undefine0(genv)
630
+ @static_ret.each do |const_read|
631
+ const_read.destroy(genv)
632
+ end
633
+ @args.each {|arg| arg.undefine(genv) }
634
+ end
635
+
636
+ def covariant_vertex0(genv, changes, vtx, subst)
637
+ changes.add_depended_static_read(@static_ret.last)
638
+ cpath = @static_ret.last.cpath
639
+ return unless cpath
640
+ mod = genv.resolve_cpath(cpath)
641
+ args = @args.map {|arg| arg.covariant_vertex(genv, changes, subst) }
642
+ changes.add_edge(genv, Source.new(Type::Instance.new(genv, mod, args)), vtx)
643
+ end
644
+
645
+ def contravariant_vertex0(genv, changes, vtx, subst)
646
+ changes.add_depended_static_read(@static_ret.last)
647
+ cpath = @static_ret.last.cpath
648
+ return unless cpath
649
+ mod = genv.resolve_cpath(cpath)
650
+ args = @args.map {|arg| arg.contravariant_vertex(genv, changes, subst) }
651
+ changes.add_edge(genv, Source.new(Type::Instance.new(genv, mod, args)), vtx)
652
+ end
653
+
654
+ def show
655
+ s = "::#{ @cpath.join("::") }"
656
+ if !@args.empty?
657
+ s << "[...]"
658
+ end
659
+ s
660
+ end
661
+ end
662
+ end
663
+ end