Class: Steep::TypeInference::MethodParams

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/type_inference/method_params.rb

Defined Under Namespace

Classes: BaseParameter, BaseRestParameter, BlockParameter, KeywordParameter, KeywordRestParameter, PositionalParameter, PositionalRestParameter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args:, method_type:, forward_arg_type:) ⇒ MethodParams

Returns a new instance of MethodParams.



148
149
150
151
152
153
154
# File 'lib/steep/type_inference/method_params.rb', line 148

def initialize(args:, method_type:, forward_arg_type:)
  @args = args
  @method_type = method_type
  @params = {}
  @errors = []
  @forward_arg_type = forward_arg_type
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



142
143
144
# File 'lib/steep/type_inference/method_params.rb', line 142

def args
  @args
end

#errorsObject (readonly)

Returns the value of attribute errors.



145
146
147
# File 'lib/steep/type_inference/method_params.rb', line 145

def errors
  @errors
end

#forward_arg_typeObject (readonly)

Returns the value of attribute forward_arg_type.



146
147
148
# File 'lib/steep/type_inference/method_params.rb', line 146

def forward_arg_type
  @forward_arg_type
end

#method_typeObject (readonly)

Returns the value of attribute method_type.



143
144
145
# File 'lib/steep/type_inference/method_params.rb', line 143

def method_type
  @method_type
end

#paramsObject (readonly)

Returns the value of attribute params.



144
145
146
# File 'lib/steep/type_inference/method_params.rb', line 144

def params
  @params
end

Class Method Details

.build(node:, method_type:) ⇒ Object



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
# File 'lib/steep/type_inference/method_params.rb', line 228

def self.build(node:, method_type:)
  # @type var args_node: ::Parser::AST::Node
  args_node =
    case node.type
    when :def
      node.children[1]
    when :defs
      node.children[2]
    else
      raise
    end
  original = args_node.children #: Array[Parser::AST::Node]
  args = original.dup

  instance = new(args: original, method_type: method_type, forward_arg_type: nil)

  unless method_type.type.params
    args.each do |arg|
      case arg.type
      when :arg
        name = arg.children[0]
        instance.params[name] = PositionalParameter.new(name: name, type: AST::Builtin.any_type, node: arg)
      when :optarg
        name = arg.children[0]
        instance.params[name] = PositionalParameter.new(name: name, type: AST::Builtin.any_type, node: arg)
      when :forward_arg
        return instance.update(forward_arg_type: true)
      end
    end
    return instance
  end

  positional_params = method_type.type.params.positional_params

  loop do
    arg = args.first or break

    case arg.type
    when :arg
      name = arg.children[0]
      param = positional_params&.head

      case param
      when Interface::Function::Params::PositionalParams::Required
        instance.params[name] = PositionalParameter.new(name: name, type: param.type, node: arg)
      when Interface::Function::Params::PositionalParams::Optional
        method_param = PositionalParameter.new(name: name, type: param.type, node: arg)
        instance.params[name] = method_param
        instance.errors << Diagnostic::Ruby::MethodParameterMismatch.new(
          method_param: method_param,
          method_type: method_type
        )
      when Interface::Function::Params::PositionalParams::Rest
        method_param = PositionalParameter.new(name: name, type: param.type, node: arg)
        instance.params[name] = method_param
        instance.errors << Diagnostic::Ruby::MethodParameterMismatch.new(
          method_param: method_param,
          method_type: method_type
        )
      when nil
        method_param = PositionalParameter.new(name: name, type: nil, node: arg)
        instance.params[name] = method_param
        instance.errors << Diagnostic::Ruby::MethodParameterMismatch.new(
          method_param: method_param,
          method_type: method_type
        )
      end

      positional_params = positional_params&.tail

    when :optarg
      name = arg.children[0]
      param = positional_params&.head

      case param
      when Interface::Function::Params::PositionalParams::Required
        method_param = PositionalParameter.new(name: name, type: param.type, node: arg)
        instance.params[name] = method_param
        instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
          method_param: method_param,
          method_type: method_type
        )
      when Interface::Function::Params::PositionalParams::Optional
        instance.params[name] = PositionalParameter.new(name: name, type: param.type, node: arg)
      when Interface::Function::Params::PositionalParams::Rest
        method_param = PositionalParameter.new(name: name, type: param.type, node: arg)
        instance.params[name] = method_param
        instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
          method_param: method_param,
          method_type: method_type
        )
      when nil
        method_param = PositionalParameter.new(name: name, type: nil, node: arg)
        instance.params[name] = method_param
        instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
          method_param: method_param,
          method_type: method_type
        )
      end

      positional_params = positional_params&.tail
    else
      break
    end

    args.shift
  end

  if (arg = args.first) && arg.type == :forward_arg
    forward_params = method_type.type.params.update(positional_params: positional_params)
    return instance.update(forward_arg_type: [forward_params, method_type.block])
  end

  if (arg = args.first) && arg.type == :restarg
    name = arg.children[0]
    rest_types = [] #: Array[AST::Types::t]
    has_error = false

    loop do
      param = positional_params&.head

      case param
      when Interface::Function::Params::PositionalParams::Required
        rest_types << param.type
        has_error = true
      when Interface::Function::Params::PositionalParams::Optional
        rest_types << param.type
        has_error = true
      when Interface::Function::Params::PositionalParams::Rest
        rest_types << param.type
        positional_params = nil
        args.shift
        break
      when nil
        has_error = true
        break
      end

      if positional_params
        positional_params = positional_params.tail
      else
        raise "Fatal error"
      end
    end

    type = rest_types.empty? ? nil : AST::Types::Union.build(types: rest_types)

    method_param = PositionalRestParameter.new(name: name, type: type, node: arg)
    instance.params[name] = method_param
    if has_error
      instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
        method_param: method_param,
        method_type: method_type
      )
    end
  end

  if positional_params
    instance.errors << Diagnostic::Ruby::MethodArityMismatch.new(node: node, method_type: method_type)
  end

  keyword_params = method_type.type.params.keyword_params
  keywords = keyword_params.keywords

  loop do
    arg = args.first or break

    case arg.type
    when :kwarg
      name = arg.children[0]

      case
      when type = keyword_params.requireds[name]
        instance.params[name] = KeywordParameter.new(name: name, type: type, node: arg)
        keywords.delete(name)
      when type = keyword_params.optionals[name]
        method_param = KeywordParameter.new(name: name, type: type, node: arg)
        instance.params[name] = method_param
        instance.errors << Diagnostic::Ruby::MethodParameterMismatch.new(
          method_param: method_param,
          method_type: method_type
        )
        keywords.delete(name)
      when type = keyword_params.rest
        method_param = KeywordParameter.new(name: name, type: type, node: arg)
        instance.params[name] = method_param
        instance.errors << Diagnostic::Ruby::MethodParameterMismatch.new(
          method_param: method_param,
          method_type: method_type
        )
        keywords.delete(name)
      else
        method_param = KeywordParameter.new(name: name, type: nil, node: arg)
        instance.params[name] = method_param
        instance.errors << Diagnostic::Ruby::MethodParameterMismatch.new(
          method_param: method_param,
          method_type: method_type
        )
      end
    when :kwoptarg
      name = arg.children[0]

      case
      when type = keyword_params.requireds[name]
        method_param = KeywordParameter.new(name: name, type: type, node: arg)
        instance.params[name] = method_param
        instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
          method_param: method_param,
          method_type: method_type
        )
        keywords.delete(name)
      when type = keyword_params.optionals[name]
        method_param = KeywordParameter.new(name: name, type: type, node: arg)
        instance.params[name] = method_param
        keywords.delete(name)
      when type = keyword_params.rest
        method_param = KeywordParameter.new(name: name, type: type, node: arg)
        instance.params[name] = method_param
        instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
          method_param: method_param,
          method_type: method_type
        )
        keywords.delete(name)
      else
        method_param = KeywordParameter.new(name: name, type: nil, node: arg)
        instance.params[name] = method_param
        instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
          method_param: method_param,
          method_type: method_type
        )
      end
    else
      break
    end

    args.shift
  end

  if (arg = args.first) && arg.type == :kwrestarg
    name = arg.children[0]
    rest_types = [] #: Array[AST::Types::t]
    has_error = false

    keywords.each do |keyword|
      rest_types << (keyword_params.requireds[keyword] || keyword_params.optionals.fetch(keyword))
      has_error = true
    end
    keywords.clear

    if keyword_params.rest
      rest_types << keyword_params.rest
    else
      has_error = true
    end

    type = rest_types.empty? ? nil : AST::Types::Union.build(types: rest_types)

    method_param = KeywordRestParameter.new(name: name, type: type, node: arg)
    instance.params[name] = method_param

    if has_error
      instance.errors << Diagnostic::Ruby::DifferentMethodParameterKind.new(
        method_param: method_param,
        method_type: method_type
      )
    end

    args.shift
  else
    if !keywords.empty? || keyword_params.rest
      instance.errors << Diagnostic::Ruby::MethodArityMismatch.new(
        node: node,
        method_type: method_type
      )
    end
  end

  if (arg = args.first) && arg.type == :blockarg
    name = arg.children[0] #: Symbol

    if method_type.block
      instance.params[name] = BlockParameter.new(
        name: name,
        type: method_type.block.type,
        optional: method_type.block.optional?,
        node: arg,
        self_type: method_type.block.self_type
      )
    else
      instance.params[name] = BlockParameter.new(
        name: name,
        type: nil,
        optional: nil,
        node: arg,
        self_type: nil
      )
    end
  end

  instance
end

.empty(node:) ⇒ Object



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
# File 'lib/steep/type_inference/method_params.rb', line 190

def self.empty(node:)
  # @type var args_node: ::Parser::AST::Node
  args_node =
    case node.type
    when :def
      node.children[1]
    when :defs
      node.children[2]
    else
      raise
    end

  params = new(args: args_node.children, method_type: nil, forward_arg_type: nil)

  args_node.children.each do |arg|
    # @type var arg: ::Parser::AST::Node
    case arg.type
    when :arg, :optarg
      name = arg.children[0]
      params.params[name] = PositionalParameter.new(name: name, type: nil, node: arg)
    when :kwarg, :kwoptarg
      name = arg.children[0]
      params.params[name] = KeywordParameter.new(name: name, type: nil, node: arg)
    when :restarg
      name = arg.children[0]
      params.params[name] = PositionalRestParameter.new(name: name, type: nil, node: arg)
    when :kwrestarg
      name = arg.children[0]
      params.params[name] = KeywordRestParameter.new(name: name, type: nil, node: arg)
    when :blockarg
      name = arg.children[0]
      params.params[name] = BlockParameter.new(name: name, type: nil, optional: nil, node: arg, self_type: nil)
    end
  end

  params
end

Instance Method Details

#[](name) ⇒ Object



156
157
158
# File 'lib/steep/type_inference/method_params.rb', line 156

def [](name)
  params[name] or raise "Unknown variable name: #{name}"
end

#eachObject



176
177
178
179
180
181
182
183
184
# File 'lib/steep/type_inference/method_params.rb', line 176

def each
  if block_given?
    each_param do |param|
      yield param.name, param.var_type
    end
  else
    enum_for :each
  end
end

#each_param(&block) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/steep/type_inference/method_params.rb', line 168

def each_param(&block)
  if block
    params.each_value(&block)
  else
    params.each_value
  end
end

#param?(name) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/steep/type_inference/method_params.rb', line 160

def param?(name)
  params.key?(name)
end

#sizeObject



164
165
166
# File 'lib/steep/type_inference/method_params.rb', line 164

def size
  params.size
end

#update(forward_arg_type: self.forward_arg_type) ⇒ Object



186
187
188
# File 'lib/steep/type_inference/method_params.rb', line 186

def update(forward_arg_type: self.forward_arg_type)
  MethodParams.new(args: args, method_type: method_type, forward_arg_type: forward_arg_type)
end