-
Notifications
You must be signed in to change notification settings - Fork 83
/
export.rb
589 lines (532 loc) · 20.8 KB
/
export.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
module TypeProf
module Reporters
module_function
def generate_analysis_trace(state, visited, backward_edge)
return nil if visited[state]
visited[state] = true
prev_states = backward_edges[state]
if prev_states
prev_states.each_key do |pstate|
trace = generate_analysis_trace(pstate, visited, backward_edge)
return [state] + trace if trace
end
nil
else
[]
end
end
def filter_backtrace(trace)
ntrace = [trace.first]
trace.each_cons(2) do |ep1, ep2|
ntrace << ep2 if ep1.ctx != ep2.ctx
end
ntrace
end
def show_message(terminated, output)
if Config.current.options[:show_typeprof_version]
output.puts "# TypeProf #{ VERSION }"
output.puts
end
if terminated
output.puts "# CAUTION: Type profiling was terminated prematurely because of the limitation"
output.puts
end
end
def show_error(errors, backward_edge, output)
return if errors.empty?
return unless Config.current.options[:show_errors]
output.puts "# Errors"
errors.each do |ep, msg|
if ENV["TP_DETAIL"]
backtrace = filter_backtrace(generate_analysis_trace(ep, {}, backward_edge))
else
backtrace = [ep]
end
loc, *backtrace = backtrace.map do |ep|
ep&.source_location
end
output.puts "#{ loc }: #{ msg }"
backtrace.each do |loc|
output.puts " from #{ loc }"
end
end
output.puts
end
def show_reveal_types(scratch, reveal_types, output)
return if reveal_types.empty?
output.puts "# Revealed types"
reveal_types.each do |source_location, ty|
output.puts "# #{ source_location } #=> #{ ty.screen_name(scratch) }"
end
output.puts
end
def show_gvars(scratch, gvars, output)
gvars = gvars.dump.filter_map do |gvar_name, entry|
if entry.type != Type.bot && !entry.rbs_declared
[gvar_name, entry]
end
end
# A signature for global variables is not supported in RBS
return if gvars.empty?
output.puts "# Global variables"
gvars.each do |gvar_name, entry|
output.puts "#{ gvar_name }: #{ entry.type.screen_name(scratch) }"
end
output.puts
end
end
class RubySignatureExporter
def initialize(
scratch,
class_defs, iseq_method_to_ctxs
)
@scratch = scratch
@class_defs = class_defs
@iseq_method_to_ctxs = iseq_method_to_ctxs
end
def conv_class(namespace, class_def, inner_classes)
@scratch.namespace = namespace
if class_def.klass_obj.superclass != :__root__ && class_def.klass_obj.superclass
omit = class_def.klass_obj.superclass == Type::Builtin[:obj] || class_def.klass_obj == Type::Builtin[:obj]
superclass = omit ? nil : @scratch.get_class_name(class_def.klass_obj.superclass)
type_args = class_def.klass_obj.superclass_type_args
if type_args && !type_args.empty?
superclass += "[#{ type_args.map {|ty| ty.screen_name(@scratch) }.join(", ") }]"
end
end
@scratch.namespace = class_def.name
consts = {}
class_def.consts.each do |name, (ty, loc)|
next unless loc
next if ty.is_a?(Type::Class)
next if Config.current.check_dir_filter(loc[0]) == :exclude
consts[name] = ty.screen_name(@scratch)
end
modules = class_def.modules.to_h do |kind, mods|
mods = mods.to_h do |singleton, mods|
mods = mods.filter_map do |mod_def, _type_args, absolute_paths|
next if absolute_paths.all? {|path| !path || Config.current.check_dir_filter(path) == :exclude }
Type::Instance.new(mod_def.klass_obj).screen_name(@scratch)
end
[singleton, mods]
end
[kind, mods]
end
visibilities = {}
source_locations = {}
methods = {}
ivars = class_def.ivars.dump
cvars = class_def.cvars.dump
class_def.methods.each do |(singleton, mid), mdefs|
mdefs.each do |mdef|
case mdef
when ISeqMethodDef
ctxs = @iseq_method_to_ctxs[mdef]
next unless ctxs
ctx = ctxs.find {|ctx| ctx.mid == mid } || ctxs.first
next if Config.current.check_dir_filter(ctx.iseq.absolute_path) == :exclude
method_name = mid
method_name = "self.#{ method_name }" if singleton
key = [:iseq, method_name]
visibilities[key] ||= mdef.pub_meth
source_locations[key] ||= ctx.iseq.source_location(0)
(methods[key] ||= []) << @scratch.show_method_signature(ctx)
when AliasMethodDef
next if mdef.def_ep && Config.current.check_dir_filter(mdef.def_ep.source_location) == :exclude
alias_name, orig_name = mid, mdef.orig_mid
if singleton
alias_name = "self.#{ alias_name }"
orig_name = "self.#{ orig_name }"
end
key = [:alias, alias_name]
visibilities[key] ||= mdef.pub_meth
source_locations[key] ||= mdef.def_ep&.source_location
methods[key] = orig_name
when ExecutedAttrMethodDef
absolute_path = mdef.def_ep.ctx.iseq.absolute_path
next if !absolute_path || Config.current.check_dir_filter(absolute_path) == :exclude
mid = mid.to_s[0..-2].to_sym if mid.to_s.end_with?("=")
method_name = mid
method_name = "self.#{ mid }" if singleton
method_name = [method_name, :"@#{ mid }" != mdef.ivar]
key = [:attr, method_name]
visibilities[key] ||= mdef.pub_meth
source_locations[key] ||= mdef.def_ep.source_location
if methods[key]
if methods[key][0] != mdef.kind
methods[key][0] = :accessor
end
else
entry = ivars[[singleton, mdef.ivar]]
ty = entry ? entry.type : Type.any
methods[key] = [mdef.kind, ty.screen_name(@scratch), ty.include_untyped?(@scratch)]
end
when TypedMethodDef
if mdef.rbs_source
method_name, sigs = mdef.rbs_source
key = [:rbs, method_name]
methods[key] = sigs
visibilities[key] ||= mdef.pub_meth
source_locations[key] ||= mdef.iseq&.source_location(0)
end
when TypedAttrMethodDef
if mdef.rbs_source
mid = mid.to_s[0..-2].to_sym if mid.to_s.end_with?("=")
method_name = mid
method_name = [method_name, :"@#{ mid }" != mdef.ivar]
key = [:rbs_attr, method_name]
visibilities[key] ||= mdef.pub_meth
if methods[key]
if methods[key][0] != mdef.kind
methods[key][0] = :accessor
end
else
entry = ivars[[singleton, mdef.ivar]]
ty = entry ? entry.type : Type.any
methods[key] = [mdef.kind, ty.screen_name(@scratch), ty.include_untyped?(@scratch)]
end
end
end
end
end
superclass_ivars = {}
while (superclass_def = (superclass_def || class_def).superclass)
superclass_ivars.merge!(superclass_def.ivars.dump)
end
ivars = ivars.map do |(singleton, var), entry|
next if entry.absolute_paths.all? {|path| Config.current.check_dir_filter(path) == :exclude }
ty = entry.type
next unless var.to_s.start_with?("@")
if (_, existing = superclass_ivars.find {|((s, v), _)| s == singleton && v == var })
existing_types = existing.type.is_a?(Type::Union) ? existing.type.types : [existing.type]
entry_types = entry.type.is_a?(Type::Union) ? entry.type.types : [entry.type]
if entry_types.all? { |t| existing_types.include?(t) }
# This type is a subset of the parent type
next
end
end
var = "self.#{ var }" if singleton
next if methods[[:attr, [singleton ? "self.#{ var.to_s[1..] }" : var.to_s[1..].to_sym, false]]]
next if entry.rbs_declared
[var, ty.screen_name(@scratch)]
end.compact
cvars = cvars.map do |var, entry|
next if entry.absolute_paths.all? {|path| Config.current.check_dir_filter(path) == :exclude }
next if entry.rbs_declared
[var, entry.type.screen_name(@scratch)]
end.compact
if !class_def.absolute_path || Config.current.check_dir_filter(class_def.absolute_path) == :exclude
if methods.keys.all? {|type,| type == :rbs }
return nil if consts.empty? && modules[:before][true].empty? && modules[:before][false].empty? && modules[:after][true].empty? && modules[:after][false].empty? && ivars.empty? && cvars.empty? && inner_classes.empty?
end
end
@scratch.namespace = nil
ClassData.new(
kind: class_def.kind,
name: class_def.name,
superclass: superclass,
consts: consts,
modules: modules,
ivars: ivars,
cvars: cvars,
methods: methods,
visibilities: visibilities,
source_locations: source_locations,
inner_classes: inner_classes,
)
end
def conv_class_lsp(namespace, class_def)
@scratch.namespace = namespace
if class_def.klass_obj.superclass != :__root__ && class_def.klass_obj.superclass
omit = class_def.klass_obj.superclass == Type::Builtin[:obj] || class_def.klass_obj == Type::Builtin[:obj]
superclass = omit ? nil : @scratch.get_class_name(class_def.klass_obj.superclass)
type_args = class_def.klass_obj.superclass_type_args
if type_args && !type_args.empty?
superclass += "[#{ type_args.map {|ty| ty.screen_name(@scratch) }.join(", ") }]"
end
end
@scratch.namespace = class_def.name
consts = {}
class_def.consts.each do |name, (ty, loc)|
next unless loc
next if ty.is_a?(Type::Class)
next if Config.current.check_dir_filter(loc[0]) == :exclude
consts[name] = ty.screen_name(@scratch)
end
modules = class_def.modules.to_h do |kind, mods|
mods = mods.to_h do |singleton, mods|
mods = mods.filter_map do |mod_def, _type_args, absolute_paths|
next if absolute_paths.all? {|path| !path || Config.current.check_dir_filter(path) == :exclude }
Type::Instance.new(mod_def.klass_obj).screen_name(@scratch)
end
[singleton, mods]
end
[kind, mods]
end
visibilities = {}
source_locations = {}
methods = {}
ivars = class_def.ivars.dump
cvars = class_def.cvars.dump
class_def.methods.each do |(singleton, mid), mdefs|
mdefs.each do |mdef|
case mdef
when ISeqMethodDef
ctxs = @iseq_method_to_ctxs[mdef]
next unless ctxs
ctx = ctxs.find {|ctx| ctx.mid == mid } || ctxs.first
next if Config.current.check_dir_filter(ctx.iseq.absolute_path) == :exclude
method_name = mid
method_name = "self.#{ method_name }" if singleton
key = [:iseq, method_name]
visibilities[key] ||= mdef.pub_meth
source_locations[key] ||= [ctx.iseq.source_location(0)]
sig = @scratch.show_method_signature(ctx)
(methods[key] ||= []) << sig if sig
when AliasMethodDef
alias_name, orig_name = mid, mdef.orig_mid
if singleton
alias_name = "self.#{ alias_name }"
orig_name = "self.#{ orig_name }"
end
key = [:alias, alias_name]
visibilities[key] ||= mdef.pub_meth
source_locations[key] ||= [mdef.def_ep&.source_location]
methods[key] = orig_name
when ExecutedAttrMethodDef
next if !mdef.def_ep
absolute_path = mdef.def_ep.ctx.iseq.absolute_path
next if !absolute_path || Config.current.check_dir_filter(absolute_path) == :exclude
mid = mid.to_s[0..-2].to_sym if mid.to_s.end_with?("=")
method_name = mid
method_name = "self.#{ mid }" if singleton
method_name = [method_name, :"@#{ mid }" != mdef.ivar]
key = [:attr, method_name]
visibilities[key] ||= mdef.pub_meth
source_locations[key] ||= [mdef.def_ep.source_location]
if methods[key]
if methods[key][0] != mdef.kind
methods[key][0] = :accessor
end
else
entry = ivars[[singleton, mdef.ivar]]
ty = entry ? entry.type : Type.any
methods[key] = [mdef.kind, ty.screen_name(@scratch), ty.include_untyped?(@scratch)]
end
when TypedMethodDef
if mdef.rbs_source
method_name, sigs, rbs_code_range = mdef.rbs_source
key = [:rbs, method_name]
methods[key] = sigs
visibilities[key] ||= mdef.pub_meth
source_locations[key] ||= [mdef.iseq&.source_location(0), rbs_code_range]
end
end
end
end
ivars = ivars.map do |(singleton, var), entry|
next if entry.absolute_paths.all? {|path| Config.current.check_dir_filter(path) == :exclude }
ty = entry.type
next unless var.to_s.start_with?("@")
var = "self.#{ var }" if singleton
next if methods[[:attr, [singleton ? "self.#{ var.to_s[1..] }" : var.to_s[1..].to_sym, false]]]
next if entry.rbs_declared
[var, ty.screen_name(@scratch)]
end.compact
cvars = cvars.map do |var, entry|
next if entry.absolute_paths.all? {|path| Config.current.check_dir_filter(path) == :exclude }
next if entry.rbs_declared
[var, entry.type.screen_name(@scratch)]
end.compact
if !class_def.absolute_path || Config.current.check_dir_filter(class_def.absolute_path) == :exclude
if methods.keys.all? {|type,| type == :rbs }
return nil if consts.empty? && modules[:before][true].empty? && modules[:before][false].empty? && modules[:after][true].empty? && modules[:after][false].empty? && ivars.empty? && cvars.empty?
end
end
@scratch.namespace = nil
ClassData.new(
kind: class_def.kind,
name: class_def.name,
superclass: superclass,
consts: consts,
modules: modules,
ivars: ivars,
cvars: cvars,
methods: methods,
visibilities: visibilities,
source_locations: source_locations,
)
end
ClassData = Struct.new(:kind, :name, :superclass, :consts, :modules, :ivars, :cvars, :methods, :visibilities, :source_locations, :inner_classes, keyword_init: true)
def show_lsp
res = []
@class_defs.each_value do |class_def|
class_data = conv_class_lsp([], class_def)
next unless class_data
class_data.methods.each do |key, arg|
source_location, rbs_code_range = class_data.source_locations[key]
type, (method_name, hidden) = key
case type
when :attr, :rbs_attr
kind, ty, untyped = *arg
line = "attr_#{ kind } #{ method_name }#{ hidden ? "()" : "" }: #{ ty }"
when :rbs
sigs = arg.sort.join(" | ")
line = "# def #{ method_name }: #{ sigs }"
when :iseq
sigs = []
untyped = false
arg.each do |sig, untyped0|
sigs << sig
untyped ||= untyped0
end
sigs = sigs.sort.join(" | ")
line = "def #{ method_name }: #{ sigs }"
when :alias
orig_name = arg
line = "alias #{ method_name } #{ orig_name }"
end
if source_location =~ /:(\d+)$/
res << [$`, $1.to_i, line, rbs_code_range, class_data.kind, class_data.name]
end
end
end
res
end
def show(stat_eps, output)
# make the class hierarchy
root = {}
@class_defs.each_value do |class_def|
h = root
class_def.name.each do |name|
h = h[name] ||= {}
end
h[:class_def] = class_def
end
hierarchy = build_class_hierarchy([], root)
output.puts "# Classes" # and Modules
prev_nil = true
show_class_hierarchy(0, hierarchy).each do |line|
if line == nil
output.puts line unless prev_nil
prev_nil = true
else
output.puts line
prev_nil = false
end
end
if ENV["TP_STAT"]
output.puts ""
output.puts "# TypeProf statistics:"
output.puts "# %d execution points" % stat_eps.size
end
if ENV["TP_COVERAGE"]
coverage = {}
stat_eps.each do |ep|
path = ep.ctx.iseq.path
lineno = ep.ctx.iseq.insns[ep.pc].lineno - 1
(coverage[path] ||= [])[lineno] ||= 0
(coverage[path] ||= [])[lineno] += 1
end
File.binwrite("typeprof-analysis-coverage.dump", Marshal.dump(coverage))
end
end
def build_class_hierarchy(namespace, hierarchy)
hierarchy.map do |name, h|
class_def = h.delete(:class_def)
class_data = conv_class(namespace, class_def, build_class_hierarchy(namespace + [name], h))
class_data
end.compact
end
def show_class_hierarchy(depth, hierarchy)
lines = []
hierarchy.each do |class_data|
lines << nil
lines.concat show_class_data(depth, class_data)
end
lines
end
def show_const(namespace, path)
return path.last.to_s if namespace == path
i = 0
i += 1 while namespace[i] && namespace[i] == path[i]
path[i..].join("::")
end
def show_class_data(depth, class_data)
indent = " " * depth
name = class_data.name.last
superclass = " < " + class_data.superclass if class_data.superclass
first_line = indent + "#{ class_data.kind } #{ name }#{ superclass }"
lines = []
class_data.consts.each do |name, ty|
lines << (indent + " #{ name }: #{ ty }")
end
class_data.modules.each do |kind, mods|
mods.each do |singleton, mods|
case
when kind == :before && singleton then directive = nil
when kind == :before && !singleton then directive = "prepend"
when kind == :after && singleton then directive = "extend"
when kind == :after && !singleton then directive = "include"
end
mods.each do |mod|
lines << (indent + " #{ directive } #{ mod }") if directive
end
end
end
class_data.ivars.each do |var, ty|
lines << (indent + " #{ var }: #{ ty }") unless var.start_with?("_")
end
class_data.cvars.each do |var, ty|
lines << (indent + " #{ var }: #{ ty }")
end
lines << nil
prev_vis = true
class_data.methods.each do |key, arg|
vis = class_data.visibilities[key]
if prev_vis != vis
lines << nil
lines << (indent + " #{ vis ? "public" : "private" }")
prev_vis = vis
end
source_location = class_data.source_locations[key]
if Config.current.options[:show_source_locations] && source_location
lines << nil
lines << (indent + " # #{ source_location }")
end
type, (method_name, hidden) = key
case type
when :rbs_attr
kind, ty, untyped = *arg
lines << (indent + "# attr_#{ kind } #{ method_name }#{ hidden ? "()" : "" }: #{ ty }")
when :attr
kind, ty, untyped = *arg
exclude = Config.current.options[:exclude_untyped] && untyped ? "#" : " " # XXX
lines << (indent + "#{ exclude } attr_#{ kind } #{ method_name }#{ hidden ? "()" : "" }: #{ ty }")
when :rbs
arg = arg.map { |a| a.split("\n").join("\n" + indent + "#" + " " * (method_name.size + 5)) }
sigs = arg.sort.join("\n" + indent + "#" + " " * (method_name.size + 5) + "| ")
lines << (indent + "# def #{ method_name }: #{ sigs }")
when :iseq
sigs = []
untyped = false
arg.each do |sig, untyped0|
sigs << sig
untyped ||= untyped0
end
sigs = sigs.sort.join("\n" + indent + " " * (method_name.size + 6) + "| ")
exclude = Config.current.options[:exclude_untyped] && untyped ? "#" : " " # XXX
lines << (indent + "#{ exclude } def #{ method_name }: #{ sigs }")
when :alias
orig_name = arg
lines << (indent + " alias #{ method_name } #{ orig_name }")
end
end
lines.concat show_class_hierarchy(depth + 1, class_data.inner_classes)
lines.shift until lines.empty? || lines.first
lines.pop until lines.empty? || lines.last
lines.unshift first_line
lines << (indent + "end")
end
end
end