/
block.rb
176 lines (143 loc) · 4.63 KB
/
block.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
module TypeProf
class Block
include Utils::StructuralEquality
end
class ISeqBlock < Block
def initialize(iseq, ep)
@iseq = iseq
@outer_ep = ep
end
attr_reader :iseq, :outer_ep
def inspect
"#<ISeqBlock: #{ @outer_ep.source_location }>"
end
def consistent?(other)
if other.is_a?(ISeqBlock)
self == other
else
true # XXX
end
end
def substitute(_subst, _depth)
self
end
def do_call(aargs, caller_ep, caller_env, scratch, replace_recv_ty:, replace_cref:, &ctn)
blk_env = scratch.return_envs[@outer_ep]
if replace_recv_ty
replace_recv_ty = scratch.globalize_type(replace_recv_ty, caller_env, caller_ep)
blk_env = blk_env.replace_recv_ty(replace_recv_ty)
end
aargs = scratch.globalize_type(aargs, caller_env, caller_ep)
scratch.add_block_signature!(self, aargs.to_block_signature)
locals = [Type.nil] * @iseq.locals.size
blk_ty, start_pcs = aargs.setup_formal_arguments(:block, locals, @iseq.fargs_format)
if blk_ty.is_a?(String)
scratch.error(caller_ep, blk_ty)
ctn[Type.any, caller_ep, caller_env]
return
end
cref = replace_cref || @outer_ep.ctx.cref
nctx = Context.new(@iseq, cref, nil)
callee_ep = ExecutionPoint.new(nctx, 0, @outer_ep)
nenv = Env.new(blk_env.static_env, locals, [], nil)
alloc_site = AllocationSite.new(callee_ep)
locals.each_with_index do |ty, i|
alloc_site2 = alloc_site.add_id(i)
nenv, ty = scratch.localize_type(ty, nenv, callee_ep, alloc_site2)
nenv = nenv.local_update(i, ty)
end
start_pcs.each do |start_pc|
scratch.merge_env(ExecutionPoint.new(nctx, start_pc, @outer_ep), nenv)
end
scratch.add_block_to_ctx!(self, callee_ep.ctx)
scratch.add_callsite!(callee_ep.ctx, caller_ep, caller_env, &ctn)
end
end
class TypedBlock < Block
def initialize(msig, ret_ty)
@msig = msig
@ret_ty = ret_ty
end
attr_reader :msig, :ret_ty
def consistent?(other)
if other.is_a?(ISeqBlock)
raise "assert false"
else
self == other
end
end
def substitute(subst, depth)
msig = @msig.substitute(subst, depth)
ret_ty = @ret_ty.substitute(subst, depth)
TypedBlock.new(msig, ret_ty)
end
def do_call(aargs, caller_ep, caller_env, scratch, replace_recv_ty:, replace_cref:, &ctn)
aargs = scratch.globalize_type(aargs, caller_env, caller_ep)
subst = aargs.consistent_with_method_signature?(@msig)
unless subst
scratch.warn(caller_ep, "The arguments is not compatible to RBS block")
end
# check?
#subst = { Type::Var.new(:self) => caller_env.static_env.recv_ty }
# XXX: Update type vars
ret_ty = @ret_ty.remove_type_vars
ctn[ret_ty, caller_ep, caller_env]
end
end
class SymbolBlock < Block
def initialize(sym)
@sym = sym
end
attr_reader :iseq, :outer_ep
def inspect
"#<SymbolBlock: #{ @sym }>"
end
def consistent?(other)
true # XXX
end
def substitute(_subst, _depth)
self
end
def do_call(aargs, caller_ep, caller_env, scratch, replace_recv_ty:, replace_cref:, &ctn)
if aargs.lead_tys.size >= 1
recv = aargs.lead_tys[0]
recv = Type.any if recv == Type.bot
aargs = ActualArguments.new(aargs.lead_tys[1..], aargs.rest_ty, aargs.kw_tys, aargs.blk_ty)
elsif aargs.rest_ty
recv = aargs.rest_ty.elems.squash_or_any # XXX: need to shift
else
recv = Type.any
end
scratch.add_block_signature!(self, aargs.to_block_signature)
recv.each_child do |recv|
scratch.do_send(recv, @sym, aargs, caller_ep, caller_env, &ctn)
end
end
end
class CustomBlock < Block
def initialize(caller_ep, mid, &blk)
@caller_ep = caller_ep
@mid = mid
@blk = blk
end
def inspect
"#<CustomBlock>"
end
def consistent?(other)
true # XXX
end
def substitute(_subst, _depth)
self
end
def do_call(aargs, caller_ep, caller_env, scratch, replace_recv_ty:, replace_cref:, &ctn)
aargs = scratch.globalize_type(aargs, caller_env, caller_ep)
dummy_ctx = TypedContext.new(@caller_ep, @mid)
scratch.add_block_signature!(self, aargs.to_block_signature)
scratch.add_block_to_ctx!(self, dummy_ctx)
@blk.call(aargs, caller_ep, caller_env, scratch, replace_recv_ty: replace_recv_ty, replace_cref: replace_cref) do |ret_ty, ep, env|
scratch.add_return_value!(dummy_ctx, ret_ty)
ctn[ret_ty, ep, env]
end
end
end
end