Class: TypeProf::Core::MethodDeclBox

Inherits:
Box
  • Object
show all
Defined in:
lib/typeprof/core/graph/box.rb

Instance Attribute Summary collapse

Attributes inherited from Box

#changes, #destroyed

Instance Method Summary collapse

Methods inherited from Box

#on_type_added, #on_type_removed, #reuse, #run, #run0, #to_s

Constructor Details

#initialize(node, genv, cpath, singleton, mid, method_types, overloading) ⇒ MethodDeclBox



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/typeprof/core/graph/box.rb', line 103

def initialize(node, genv, cpath, singleton, mid, method_types, overloading)
  super(node)
  @cpath = cpath
  @singleton = singleton
  @mid = mid
  @method_types = method_types
  @overloading = overloading
  @ret = Source.new

  me = genv.resolve_method(@cpath, @singleton, @mid)
  me.add_decl(self)
  me.add_run_all_method_call_boxes(genv)
  me.add_run_all_mdefs(genv)
end

Instance Attribute Details

#cpathObject (readonly)

Returns the value of attribute cpath.



120
121
122
# File 'lib/typeprof/core/graph/box.rb', line 120

def cpath
  @cpath
end

#method_typesObject (readonly)

Returns the value of attribute method_types.



120
121
122
# File 'lib/typeprof/core/graph/box.rb', line 120

def method_types
  @method_types
end

#midObject (readonly)

Returns the value of attribute mid.



120
121
122
# File 'lib/typeprof/core/graph/box.rb', line 120

def mid
  @mid
end

#nodeObject

Returns the value of attribute node.



118
119
120
# File 'lib/typeprof/core/graph/box.rb', line 118

def node
  @node
end

#overloadingObject (readonly)

Returns the value of attribute overloading.



120
121
122
# File 'lib/typeprof/core/graph/box.rb', line 120

def overloading
  @overloading
end

#retObject (readonly)

Returns the value of attribute ret.



120
121
122
# File 'lib/typeprof/core/graph/box.rb', line 120

def ret
  @ret
end

#singletonObject (readonly)

Returns the value of attribute singleton.



120
121
122
# File 'lib/typeprof/core/graph/box.rb', line 120

def singleton
  @singleton
end

Instance Method Details

#destroy(genv) ⇒ Object



122
123
124
125
126
# File 'lib/typeprof/core/graph/box.rb', line 122

def destroy(genv)
  me = genv.resolve_method(@cpath, @singleton, @mid)
  me.remove_decl(self)
  me.add_run_all_method_call_boxes(genv)
end

#match_arguments?(genv, changes, param_map, a_args, method_type) ⇒ Boolean



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
# File 'lib/typeprof/core/graph/box.rb', line 128

def match_arguments?(genv, changes, param_map, a_args, method_type)
  # TODO: handle a tuple as a splat argument?
  if a_args.splat_flags.any?
    return false unless method_type.rest_positionals
    method_type.req_positionals.size.times do |i|
      return false if a_args.splat_flags[i]
    end
    method_type.post_positionals.size.times do |i|
      return false if a_args.splat_flags[-i - 1]
    end
  else
    actual = a_args.positionals.size
    required_formal = method_type.req_positionals.size + method_type.post_positionals.size
    if actual < required_formal
      # too few actual arguments
      return false
    end
    if !method_type.rest_positionals && actual > required_formal + method_type.opt_positionals.size
      # too many actual arguments
      return false
    end
  end

  method_type.req_positionals.each_with_index do |ty, i|
    return false unless ty.typecheck(genv, changes, a_args.positionals[i], param_map)
  end
  method_type.post_positionals.each_with_index do |ty, i|
    i -= method_type.post_positionals.size
    return false unless ty.typecheck(genv, changes, a_args.positionals[i], param_map)
  end

  start_rest = method_type.req_positionals.size
  end_rest = a_args.positionals.size - method_type.post_positionals.size

  i = 0
  while i < method_type.opt_positionals.size && start_rest < end_rest
    break if a_args.splat_flags[start_rest]
    return false unless method_type.opt_positionals[i].typecheck(genv, changes, a_args.positionals[start_rest], param_map)
    i += 1
    start_rest += 1
  end

  if start_rest < end_rest
    vtxs = a_args.get_rest_args(genv, changes, start_rest, end_rest)
    while i < method_type.opt_positionals.size
      ty = method_type.opt_positionals[i]
      return false if vtxs.any? {|vtx| !ty.typecheck(genv, changes, vtx, param_map) }
      i += 1
    end
    if method_type.rest_positionals
      return false if vtxs.any? {|vtx| !method_type.rest_positionals.typecheck(genv, changes, vtx, param_map) }
    end
  end

  return true
end

#resolve_overload(changes, genv, method_type, node, param_map, a_args, ret, force) ⇒ Object



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
# File 'lib/typeprof/core/graph/box.rb', line 185

def resolve_overload(changes, genv, method_type, node, param_map, a_args, ret, force)
  param_map0 = param_map.dup
  if method_type.type_params
    method_type.type_params.zip(yield(method_type)) do |var, vtx|
      param_map0[var] = vtx
    end
  end

  unless match_arguments?(genv, changes, param_map0, a_args, method_type)
    if force
      meth = node.mid_code_range ? :mid_code_range : :code_range
      changes.add_diagnostic(meth, "wrong type of arguments") # XXX: more friendly and fine-grained error message
    end
    return false
  end

  rbs_blk = method_type.block
  if method_type.block_required && !a_args.block
    if force
      meth = node.mid_code_range ? :mid_code_range : :code_range
      changes.add_diagnostic(meth, "block is expected") # XXX: more friendly error message
    end
    return false
  end
  if !rbs_blk && a_args.block
    if force
      meth = node.mid_code_range ? :mid_code_range : :code_range
      changes.add_diagnostic(meth, "block is not expected") # XXX: more friendly error message
    end
    return false
  end
  if rbs_blk && a_args.block
    # rbs_blk_func.optional_keywords, ...
    blk_a_args = rbs_blk.req_positionals.map do |blk_a_arg|
      blk_a_arg.covariant_vertex(genv, changes, param_map0)
    end
    a_args.block.each_type do |ty|
      case ty
      when Type::Proc
        ty.block.accept_args(genv, changes, blk_a_args)

        if ty.block.is_a?(Block)
          ty.block.next_boxes.each do |next_box|
            unless rbs_blk.return_type.typecheck(genv, changes, next_box.a_ret, param_map0)
              next_box.wrong_return_type(rbs_blk.return_type.show, changes)
            end
          end
        end
      end
    end
  end

  force = true
  return true

ensure
  if force
    ret_vtx = method_type.return_type.covariant_vertex(genv, changes, param_map0)
    changes.add_edge(genv, ret_vtx, ret)
  end
end

#resolve_overloads(changes, genv, node, param_map, a_args, ret, &blk) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/typeprof/core/graph/box.rb', line 247

def resolve_overloads(changes, genv, node, param_map, a_args, ret, &blk)
  if @method_types.size == 1
    method_type = @method_types.first
    resolve_overload(changes, genv, method_type, node, param_map, a_args, ret, true, &blk)
    return
  end

  match_any_overload = false
  @method_types.each do |method_type|
    if resolve_overload(changes, genv, method_type, node, param_map, a_args, ret, false, &blk)
      match_any_overload = true
    end
  end
  unless match_any_overload
    meth = node.mid_code_range ? :mid_code_range : :code_range
    changes.add_diagnostic(meth, "failed to resolve overloads")
  end
end

#showObject



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
# File 'lib/typeprof/core/graph/box.rb', line 266

def show
  @method_types.map do |method_type|
    args = []
    method_type.req_positionals.each do |arg|
      args << arg.show
    end
    method_type.opt_positionals.each do |arg|
      args << "?#{arg.show}"
    end
    if method_type.rest_positionals
      args << "*#{method_type.rest_positionals.show}"
    end
    method_type.post_positionals.each do |arg|
      args << arg.show
    end

    method_type.req_keywords.each do |key, arg|
      args << "#{ key }: #{arg.show}"
    end
    method_type.opt_keywords.each do |key, arg|
      args << "?#{ key }: #{arg.show}"
    end
    if method_type.rest_keywords
      args << "**#{method_type.rest_keywords.show}"
    end

    s = args.empty? ? "-> " : "(#{ args.join(", ") }) -> "
    s += method_type.return_type.show
  end.join(" | ")
end