Class: KatakataIrb::TypeAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/katakata_irb/type_analyzer.rb

Defined Under Namespace

Classes: DigTarget

Constant Summary collapse

OBJECT_METHODS =
{
  to_s: KatakataIrb::Types::STRING,
  to_str: KatakataIrb::Types::STRING,
  to_a: KatakataIrb::Types::ARRAY,
  to_ary: KatakataIrb::Types::ARRAY,
  to_h: KatakataIrb::Types::HASH,
  to_hash: KatakataIrb::Types::HASH,
  to_i: KatakataIrb::Types::INTEGER,
  to_int: KatakataIrb::Types::INTEGER,
  to_f: KatakataIrb::Types::FLOAT,
  to_c: KatakataIrb::Types::COMPLEX,
  to_r: KatakataIrb::Types::RATIONAL
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dig_targets) ⇒ TypeAnalyzer

Returns a new instance of TypeAnalyzer.



37
38
39
# File 'lib/katakata_irb/type_analyzer.rb', line 37

def initialize(dig_targets)
  @dig_targets = dig_targets
end

Class Method Details

.calculate_target_type_scope(binding, parents, target) ⇒ Object



1168
1169
1170
1171
1172
1173
1174
1175
1176
# File 'lib/katakata_irb/type_analyzer.rb', line 1168

def self.calculate_target_type_scope(binding, parents, target)
  dig_targets = DigTarget.new(parents, target) do |type, scope|
    return type, scope
  end
  program = parents.first
  scope = KatakataIrb::Scope.from_binding(binding, program.locals)
  new(dig_targets).evaluate_program program, scope
  [KatakataIrb::Types::NIL, scope]
end

Instance Method Details

#assign_numbered_parameters(numbered_parameters, scope, args, _kwargs) ⇒ Object



940
941
942
943
944
945
946
947
948
949
950
951
952
# File 'lib/katakata_irb/type_analyzer.rb', line 940

def assign_numbered_parameters(numbered_parameters, scope, args, _kwargs)
  return if numbered_parameters.empty?
  max_num = numbered_parameters.map { _1[1].to_i }.max
  if max_num == 1
    scope['_1'] = args.first || KatakataIrb::Types::NIL
  else
    args = sized_splat(args.first, :to_ary, max_num) if args.size == 1
    numbered_parameters.each do |name|
      index = name[1].to_i - 1
      scope[name] = args[index] || KatakataIrb::Types::NIL
    end
  end
end

#assign_parameters(node, scope, args, kwargs) ⇒ Object



889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
# File 'lib/katakata_irb/type_analyzer.rb', line 889

def assign_parameters(node, scope, args, kwargs)
  args = args.dup
  kwargs = kwargs.dup
  size = node.requireds.size + node.optionals.size + (node.rest ? 1 : 0) + node.posts.size
  args = sized_splat(args.first, :to_ary, size) if size >= 2 && args.size == 1
  reqs = args.shift node.requireds.size
  if node.rest
    # node.rest is Prism::RestParameterNode
    posts = []
    opts = args.shift node.optionals.size
    rest = args
  else
    posts = args.pop node.posts.size
    opts = args
    rest = []
  end
  node.requireds.zip reqs do |n, v|
    assign_required_parameter n, v, scope
  end
  node.optionals.zip opts do |n, v|
    # n is Prism::OptionalParameterNode
    values = [v]
    values << evaluate(n.value, scope) if n.value
    scope[n.name.to_s] = KatakataIrb::Types::UnionType[*values.compact]
  end
  node.posts.zip posts do |n, v|
    assign_required_parameter n, v, scope
  end
  if node.rest&.name
    # node.rest is Prism::RestParameterNode
    scope[node.rest.name.to_s] = KatakataIrb::Types.array_of(*rest)
  end
  node.keywords.each do |n|
    # n is Prism::KeywordParameterNode (prism = 0.16.0)
    # n is Prism::RequiredKeywordParameterNode | Prism::OptionalKeywordParameterNode (prism > 0.16.0)
    name = n.name.to_s.delete(':')
    values = [kwargs.delete(name)]
    # `respond_to?` is for prism > 0.16.0, `&& n.value` is for prism = 0.16.0
    values << evaluate(n.value, scope) if n.respond_to?(:value) && n.value
    scope[name] = KatakataIrb::Types::UnionType[*values.compact]
  end
  # node.keyword_rest is Prism::KeywordRestParameterNode or Prism::ForwardingParameterNode or Prism::NoKeywordsParameterNode
  if node.keyword_rest.is_a?(Prism::KeywordRestParameterNode) && node.keyword_rest.name
    scope[node.keyword_rest.name.to_s] = KatakataIrb::Types::InstanceType.new(Hash, K: KatakataIrb::Types::SYMBOL, V: KatakataIrb::Types::UnionType[*kwargs.values])
  end
  if node.block&.name
    # node.block is Prism::BlockParameterNode
    scope[node.block.name.to_s] = KatakataIrb::Types::PROC
  end
end

#assign_required_parameter(node, value, scope) ⇒ Object



846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
# File 'lib/katakata_irb/type_analyzer.rb', line 846

def assign_required_parameter(node, value, scope)
  case node.type
  when :required_parameter_node
    scope[node.name.to_s] = value || KatakataIrb::Types::OBJECT
  when :multi_target_node
    parameters = [*node.lefts, *node.rest, *node.rights]
    values = value ? sized_splat(value, :to_ary, parameters.size) : []
    parameters.zip values do |n, v|
      assign_required_parameter n, v, scope
    end
  when :splat_node
    splat_value = value ? KatakataIrb::Types.array_of(value) : KatakataIrb::Types::ARRAY
    assign_required_parameter node.expression, splat_value, scope if node.expression
  end
end

#class_or_value_to_instance(type) ⇒ Object



1022
1023
1024
1025
1026
1027
# File 'lib/katakata_irb/type_analyzer.rb', line 1022

def class_or_value_to_instance(type)
  instance_types = type.types.map do |t|
    t.is_a?(KatakataIrb::Types::SingletonType) ? KatakataIrb::Types::InstanceType.new(t.module_or_class) : t
  end
  KatakataIrb::Types::UnionType[*instance_types]
end

#const_path_write(receiver, name, value, scope) ⇒ Object



837
838
839
840
841
842
843
844
# File 'lib/katakata_irb/type_analyzer.rb', line 837

def const_path_write(receiver, name, value, scope)
  if receiver # receiver::A = value
    singleton_type = receiver.types.find { _1.is_a? KatakataIrb::Types::SingletonType }
    scope.set_const singleton_type.module_or_class, name, value if singleton_type
  else # ::A = value
    scope.set_const Object, name, value
  end
end

#evaluate(node, scope) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/katakata_irb/type_analyzer.rb', line 41

def evaluate(node, scope)
  method = "evaluate_#{node.type}"
  if respond_to? method
    result = send method, node, scope
  else
    KatakataIrb.log_puts
    KatakataIrb.log_puts :NOMATCH
    KatakataIrb.log_puts node.inspect
    result = KatakataIrb::Types::NIL
  end
  @dig_targets.resolve result, scope if @dig_targets.target? node
  result
end

#evaluate_alias_global_variable_node(_node, _scope) ⇒ Object



791
# File 'lib/katakata_irb/type_analyzer.rb', line 791

def evaluate_alias_global_variable_node(_node, _scope) = KatakataIrb::Types::NIL

#evaluate_alias_method_node(_node, _scope) ⇒ Object



790
# File 'lib/katakata_irb/type_analyzer.rb', line 790

def evaluate_alias_method_node(_node, _scope) = KatakataIrb::Types::NIL

#evaluate_and_node(node, scope) ⇒ Object



304
# File 'lib/katakata_irb/type_analyzer.rb', line 304

def evaluate_and_node(node, scope) = evaluate_and_or(node, scope, and_op: true)

#evaluate_and_or(node, scope, and_op:) ⇒ Object



306
307
308
309
310
311
312
313
314
# File 'lib/katakata_irb/type_analyzer.rb', line 306

def evaluate_and_or(node, scope, and_op:)
  left = evaluate node.left, scope
  right = scope.conditional { evaluate node.right, _1 }
  if and_op
    KatakataIrb::Types::UnionType[right, KatakataIrb::Types::NIL, KatakataIrb::Types::FALSE]
  else
    KatakataIrb::Types::UnionType[left, right]
  end
end

#evaluate_array_node(node, scope) ⇒ Object



160
161
162
# File 'lib/katakata_irb/type_analyzer.rb', line 160

def evaluate_array_node(node, scope)
  KatakataIrb::Types.array_of evaluate_list_splat_items(node.elements, scope)
end

#evaluate_back_reference_read_node(_node, _scope) ⇒ Object



221
222
223
# File 'lib/katakata_irb/type_analyzer.rb', line 221

def evaluate_back_reference_read_node(_node, _scope)
  KatakataIrb::Types::UnionType[KatakataIrb::Types::STRING, KatakataIrb::Types::NIL]
end

#evaluate_begin_node(node, scope) ⇒ Object



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/katakata_irb/type_analyzer.rb', line 553

def evaluate_begin_node(node, scope)
  return_type = node.statements ? evaluate(node.statements, scope) : KatakataIrb::Types::NIL
  if node.rescue_clause
    if node.else_clause
      return_types = scope.run_branches(
        ->{ evaluate node.rescue_clause, _1 },
        ->{ evaluate node.else_clause, _1 }
      )
    else
      return_types = [
        return_type,
        scope.conditional { evaluate node.rescue_clause, _1 }
      ]
    end
    return_type = KatakataIrb::Types::UnionType[*return_types]
  end
  if node.ensure_clause&.statements
    # ensure_clause is Prism::EnsureNode
    evaluate node.ensure_clause.statements, scope
  end
  return_type
end

#evaluate_break_node(node, scope) ⇒ Object



503
# File 'lib/katakata_irb/type_analyzer.rb', line 503

def evaluate_break_node(node, scope) = evaluate_jump(node, scope, :break)

#evaluate_call_and_write_node(node, scope) ⇒ Object



317
# File 'lib/katakata_irb/type_analyzer.rb', line 317

def evaluate_call_and_write_node(node, scope) = evaluate_call_write(node, scope, :and, node.write_name)

#evaluate_call_node(node, scope) ⇒ Object



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
# File 'lib/katakata_irb/type_analyzer.rb', line 235

def evaluate_call_node(node, scope)
  is_field_assign = node.name.match?(/[^<>=!\]]=\z/) || (node.name == :[]= && !node.call_operator)
  receiver_type = node.receiver ? evaluate(node.receiver, scope) : scope.self_type
  evaluate_method = lambda do |scope|
    args_types, kwargs_types, block_sym_node, has_block = evaluate_call_node_arguments node, scope

    if block_sym_node
      block_sym = block_sym_node.value
      if @dig_targets.target? block_sym_node
        # method(args, &:completion_target)
        call_block_proc = ->(block_args, _self_type) do
          block_receiver = block_args.first || KatakataIrb::Types::OBJECT
          @dig_targets.resolve block_receiver, scope
          KatakataIrb::Types::OBJECT
        end
      else
        call_block_proc = ->(block_args, _self_type) do
          block_receiver, *rest = block_args
          block_receiver ? method_call(block_receiver || KatakataIrb::Types::OBJECT, block_sym, rest, nil, nil, scope) : KatakataIrb::Types::OBJECT
        end
      end
    elsif node.block.is_a? Prism::BlockNode
      call_block_proc = ->(block_args, block_self_type) do
        scope.conditional do |s|
          numbered_parameters = node.block.locals.grep(/\A_[1-9]/).map(&:to_s)
          params_table = node.block.locals.to_h { [_1.to_s, KatakataIrb::Types::NIL] }
          table = { **params_table, KatakataIrb::Scope::BREAK_RESULT => nil, KatakataIrb::Scope::NEXT_RESULT => nil }
          block_scope = KatakataIrb::Scope.new s, table, self_type: block_self_type, trace_ivar: !block_self_type
          # TODO kwargs
          if node.block.parameters&.parameters
            # node.block.parameters is Prism::BlockParametersNode
            assign_parameters node.block.parameters.parameters, block_scope, block_args, {}
          elsif !numbered_parameters.empty?
            assign_numbered_parameters numbered_parameters, block_scope, block_args, {}
          end
          result = node.block.body ? evaluate(node.block.body, block_scope) : KatakataIrb::Types::NIL
          block_scope.merge_jumps
          s.update block_scope
          nexts = block_scope[KatakataIrb::Scope::NEXT_RESULT]
          breaks = block_scope[KatakataIrb::Scope::BREAK_RESULT]
          if block_scope.terminated?
            [KatakataIrb::Types::UnionType[*nexts], breaks]
          else
            [KatakataIrb::Types::UnionType[result, *nexts], breaks]
          end
        end
      end
    elsif has_block
      call_block_proc = ->(_block_args, _self_type) { KatakataIrb::Types::OBJECT }
    end
    result = method_call receiver_type, node.name, args_types, kwargs_types, call_block_proc, scope
    if is_field_assign
      args_types.last || KatakataIrb::Types::NIL
    else
      result
    end
  end
  if node.call_operator == '&.'
    result = scope.conditional { evaluate_method.call _1 }
    if receiver_type.nillable?
      KatakataIrb::Types::UnionType[result, KatakataIrb::Types::NIL]
    else
      result
    end
  else
    evaluate_method.call scope
  end
end

#evaluate_call_node_arguments(call_node, scope) ⇒ Object



795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
# File 'lib/katakata_irb/type_analyzer.rb', line 795

def evaluate_call_node_arguments(call_node, scope)
  # call_node.arguments is Prism::ArgumentsNode
  arguments = call_node.arguments&.arguments&.dup || []
  block_arg = call_node.block.expression if call_node.block.is_a?(Prism::BlockArgumentNode)
  kwargs = arguments.pop.elements if arguments.last.is_a?(Prism::KeywordHashNode)
  args_types = arguments.map do |arg|
    case arg
    when Prism::ForwardingArgumentsNode
      # `f(a, ...)` treat like splat
      nil
    when Prism::SplatNode
      evaluate arg.expression, scope if arg.expression
      nil # TODO: splat
    else
      evaluate arg, scope
    end
  end
  if kwargs
    kwargs_types = kwargs.map do |arg|
      case arg
      when Prism::AssocNode
        if arg.key.is_a?(Prism::SymbolNode)
          [arg.key.value, evaluate(arg.value, scope)]
        else
          evaluate arg.key, scope
          evaluate arg.value, scope
          nil
        end
      when Prism::AssocSplatNode
        evaluate arg.value, scope if arg.value
        nil
      end
    end.compact.to_h
  end
  if block_arg.is_a? Prism::SymbolNode
    block_sym_node = block_arg
  elsif block_arg
    evaluate block_arg, scope
  end
  [args_types, kwargs_types, block_sym_node, !!block_arg]
end

#evaluate_call_operator_write_node(node, scope) ⇒ Object



316
# File 'lib/katakata_irb/type_analyzer.rb', line 316

def evaluate_call_operator_write_node(node, scope) = evaluate_call_write(node, scope, :operator, node.write_name)

#evaluate_call_or_write_node(node, scope) ⇒ Object



318
# File 'lib/katakata_irb/type_analyzer.rb', line 318

def evaluate_call_or_write_node(node, scope) = evaluate_call_write(node, scope, :or, node.write_name)

#evaluate_call_write(node, scope, operator, write_name) ⇒ Object



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
# File 'lib/katakata_irb/type_analyzer.rb', line 322

def evaluate_call_write(node, scope, operator, write_name)
  receiver_type = evaluate node.receiver, scope
  if write_name == :[]=
    args_types, kwargs_types, block_sym_node, has_block = evaluate_call_node_arguments node, scope
  else
    args_types = []
  end
  if block_sym_node
    block_sym = block_sym_node.value
    call_block_proc = ->(block_args, _self_type) do
      block_receiver, *rest = block_args
      block_receiver ? method_call(block_receiver || KatakataIrb::Types::OBJECT, block_sym, rest, nil, nil, scope) : KatakataIrb::Types::OBJECT
    end
  elsif has_block
    call_block_proc = ->(_block_args, _self_type) { KatakataIrb::Types::OBJECT }
  end
  method = write_name.to_s.delete_suffix('=')
  left = method_call receiver_type, method, args_types, kwargs_types, call_block_proc, scope
  case operator
  when :and
    right = scope.conditional { evaluate node.value, _1 }
    KatakataIrb::Types::UnionType[right, KatakataIrb::Types::NIL, KatakataIrb::Types::FALSE]
  when :or
    right = scope.conditional { evaluate node.value, _1 }
    KatakataIrb::Types::UnionType[left, right]
  else
    right = evaluate node.value, scope
    method_call left, node.operator, [right], nil, nil, scope, name_match: false
  end
end

#evaluate_case_match(target, node, scope) ⇒ Object



954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# File 'lib/katakata_irb/type_analyzer.rb', line 954

def evaluate_case_match(target, node, scope)
  case node
  when Prism::WhenNode
    node.conditions.each { evaluate _1, scope }
    node.statements ? evaluate(node.statements, scope) : KatakataIrb::Types::NIL
  when Prism::InNode
    pattern = node.pattern
    if pattern.is_a?(Prism::IfNode) || pattern.is_a?(Prism::UnlessNode)
      cond_node = pattern.predicate
      pattern = pattern.statements.body.first
    end
    evaluate_match_pattern(target, pattern, scope)
    evaluate cond_node, scope if cond_node # TODO: conditional branch
    node.statements ? evaluate(node.statements, scope) : KatakataIrb::Types::NIL
  end
end

#evaluate_case_node(node, scope) ⇒ Object



706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/katakata_irb/type_analyzer.rb', line 706

def evaluate_case_node(node, scope)
  target = evaluate(node.predicate, scope) if node.predicate
  # TODO
  branches = node.conditions.map do |condition|
    ->(s) { evaluate_case_match target, condition, s }
  end
  if node.consequent
    branches << ->(s) { evaluate node.consequent, s }
  elsif node.conditions.any? { _1.is_a? Prism::WhenNode }
    branches << ->(_s) { KatakataIrb::Types::NIL }
  end
  KatakataIrb::Types::UnionType[*scope.run_branches(*branches)]
end

#evaluate_class_module(node, scope, is_class) ⇒ Object



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'lib/katakata_irb/type_analyzer.rb', line 631

def evaluate_class_module(node, scope, is_class)
  unless node.constant_path.is_a?(Prism::ConstantReadNode) || node.constant_path.is_a?(Prism::ConstantPathNode)
    # Incomplete class/module `class (statement[cursor_here])::Name; end`
    evaluate node.constant_path, scope
    return KatakataIrb::Types::NIL
  end
  const_type, _receiver, parent_module, name = evaluate_constant_node_info node.constant_path, scope
  if is_class
    select_class_type = -> { _1.is_a?(KatakataIrb::Types::SingletonType) && _1.module_or_class.is_a?(Class) }
    module_types = const_type.types.select(&select_class_type)
    module_types += evaluate(node.superclass, scope).types.select(&select_class_type) if node.superclass
    module_types << KatakataIrb::Types::CLASS if module_types.empty?
  else
    module_types = const_type.types.select { _1.is_a?(KatakataIrb::Types::SingletonType) && !_1.module_or_class.is_a?(Class) }
    module_types << KatakataIrb::Types::MODULE if module_types.empty?
  end
  return KatakataIrb::Types::NIL unless node.body

  table = node.locals.to_h { [_1.to_s, KatakataIrb::Types::NIL] }
  if !name.empty? && (parent_module.is_a?(Module) || parent_module.nil?)
    value = parent_module.const_get name if parent_module&.const_defined? name
    unless value
      value_type = scope[name]
      value = value_type.module_or_class if value_type.is_a? KatakataIrb::Types::SingletonType
    end

    if value.is_a? Module
      nesting = [value, []]
    else
      if parent_module
        nesting = [parent_module, [name]]
      else
        parent_nesting, parent_path = scope.module_nesting.first
        nesting = [parent_nesting, parent_path + [name]]
      end
      nesting_key = [nesting[0].__id__, nesting[1]].join('::')
      nesting_value = is_class ? KatakataIrb::Types::CLASS : KatakataIrb::Types::MODULE
    end
  else
    # parent_module == :unknown
    # TODO: dummy module
  end
  module_scope = KatakataIrb::Scope.new(
    scope,
    { **table, KatakataIrb::Scope::BREAK_RESULT => nil, KatakataIrb::Scope::NEXT_RESULT => nil, KatakataIrb::Scope::RETURN_RESULT => nil },
    trace_ivar: false,
    trace_lvar: false,
    self_type: KatakataIrb::Types::UnionType[*module_types],
    nesting: nesting
  )
  module_scope[nesting_key] = nesting_value if nesting_value
  result = evaluate(node.body, module_scope)
  scope.update module_scope
  result
end

#evaluate_class_node(node, scope) ⇒ Object



629
# File 'lib/katakata_irb/type_analyzer.rb', line 629

def evaluate_class_node(node, scope) = evaluate_class_module(node, scope, true)

#evaluate_constant_and_write_node(node, scope) ⇒ Object



388
389
390
391
# File 'lib/katakata_irb/type_analyzer.rb', line 388

def evaluate_constant_and_write_node(node, scope)
  right = scope.conditional { evaluate node.value, scope }
  scope[node.name.to_s] = KatakataIrb::Types::UnionType[right, KatakataIrb::Types::NIL, KatakataIrb::Types::FALSE]
end

#evaluate_constant_node_info(node, scope) ⇒ Object



862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
# File 'lib/katakata_irb/type_analyzer.rb', line 862

def evaluate_constant_node_info(node, scope)
  case node
  when Prism::ConstantPathNode
    name = node.child.name.to_s
    if node.parent
      receiver = evaluate node.parent, scope
      if receiver.is_a? KatakataIrb::Types::SingletonType
        parent_module = receiver.module_or_class
      end
    else
      parent_module = Object
    end
    if parent_module
      type = scope.get_const(parent_module, [name]) || KatakataIrb::Types::NIL
    else
      parent_module = :unknown
      type = KatakataIrb::Types::NIL
    end
  when Prism::ConstantReadNode
    name = node.name.to_s
    type = scope[name]
  end
  @dig_targets.resolve type, scope if @dig_targets.target? node
  [type, receiver, parent_module, name]
end

#evaluate_constant_operator_write_node(node, scope) ⇒ Object



382
383
384
385
386
# File 'lib/katakata_irb/type_analyzer.rb', line 382

def evaluate_constant_operator_write_node(node, scope)
  left = scope[node.name.to_s] || KatakataIrb::Types::OBJECT
  right = evaluate node.value, scope
  scope[node.name.to_s] = method_call left, node.operator, [right], nil, nil, scope, name_match: false
end

#evaluate_constant_or_write_node(node, scope) ⇒ Object



393
394
395
396
397
# File 'lib/katakata_irb/type_analyzer.rb', line 393

def evaluate_constant_or_write_node(node, scope)
  left = scope[node.name.to_s] || KatakataIrb::Types::OBJECT
  right = scope.conditional { evaluate node.value, scope }
  scope[node.name.to_s] = KatakataIrb::Types::UnionType[left, right]
end

#evaluate_constant_path_and_write_node(node, scope) ⇒ Object



407
408
409
410
411
412
413
# File 'lib/katakata_irb/type_analyzer.rb', line 407

def evaluate_constant_path_and_write_node(node, scope)
  _left, receiver, _parent_module, name = evaluate_constant_node_info node.target, scope
  right = scope.conditional { evaluate node.value, scope }
  value = KatakataIrb::Types::UnionType[right, KatakataIrb::Types::NIL, KatakataIrb::Types::FALSE]
  const_path_write receiver, name, value, scope
  value
end

#evaluate_constant_path_node(node, scope) ⇒ Object



198
199
200
201
# File 'lib/katakata_irb/type_analyzer.rb', line 198

def evaluate_constant_path_node(node, scope)
  type, = evaluate_constant_node_info node, scope
  type
end

#evaluate_constant_path_operator_write_node(node, scope) ⇒ Object



399
400
401
402
403
404
405
# File 'lib/katakata_irb/type_analyzer.rb', line 399

def evaluate_constant_path_operator_write_node(node, scope)
  left, receiver, _parent_module, name = evaluate_constant_node_info node.target, scope
  right = evaluate node.value, scope
  value = method_call left, node.operator, [right], nil, nil, scope, name_match: false
  const_path_write receiver, name, value, scope
  value
end

#evaluate_constant_path_or_write_node(node, scope) ⇒ Object



415
416
417
418
419
420
421
# File 'lib/katakata_irb/type_analyzer.rb', line 415

def evaluate_constant_path_or_write_node(node, scope)
  left, receiver, _parent_module, name = evaluate_constant_node_info node.target, scope
  right = scope.conditional { evaluate node.value, scope }
  value = KatakataIrb::Types::UnionType[left, right]
  const_path_write receiver, name, value, scope
  value
end

#evaluate_constant_path_write_node(node, scope) ⇒ Object



423
424
425
426
427
428
# File 'lib/katakata_irb/type_analyzer.rb', line 423

def evaluate_constant_path_write_node(node, scope)
  receiver = evaluate node.target.parent, scope if node.target.parent
  value = evaluate node.value, scope
  const_path_write receiver, node.target.child.name.to_s, value, scope
  value
end

#evaluate_def_node(node, scope) ⇒ Object



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
# File 'lib/katakata_irb/type_analyzer.rb', line 67

def evaluate_def_node(node, scope)
  if node.receiver
    self_type = evaluate node.receiver, scope
  else
    current_self_types = scope.self_type.types
    self_types = current_self_types.map do |type|
      if type.is_a?(KatakataIrb::Types::SingletonType) && type.module_or_class.is_a?(Class)
        KatakataIrb::Types::InstanceType.new type.module_or_class
      else
        type
      end
    end
    self_type = KatakataIrb::Types::UnionType[*self_types]
  end
  if @dig_targets.dig?(node.body) || @dig_targets.dig?(node.parameters)
    params_table = node.locals.to_h { [_1.to_s, KatakataIrb::Types::NIL] }
    method_scope = KatakataIrb::Scope.new(
      scope,
      { **params_table, KatakataIrb::Scope::BREAK_RESULT => nil, KatakataIrb::Scope::NEXT_RESULT => nil, KatakataIrb::Scope::RETURN_RESULT => nil },
      self_type: self_type,
      trace_lvar: false,
      trace_ivar: false
    )
    if node.parameters
      # node.parameters is Prism::ParametersNode
      assign_parameters node.parameters, method_scope, [], {}
    end

    if @dig_targets.dig?(node.body)
      method_scope.conditional do |s|
        evaluate node.body, s
      end
    end
    method_scope.merge_jumps
    scope.update method_scope
  end
  KatakataIrb::Types::SYMBOL
end

#evaluate_defined_node(node, scope) ⇒ Object



739
740
741
742
# File 'lib/katakata_irb/type_analyzer.rb', line 739

def evaluate_defined_node(node, scope)
  scope.conditional { evaluate node.value, _1 }
  KatakataIrb::Types::UnionType[KatakataIrb::Types::STRING, KatakataIrb::Types::NIL]
end

#evaluate_else_node(node, scope) ⇒ Object



483
484
485
# File 'lib/katakata_irb/type_analyzer.rb', line 483

def evaluate_else_node(node, scope)
  node.statements ? evaluate(node.statements, scope) : KatakataIrb::Types::NIL
end

#evaluate_embedded_statements_node(node, scope) ⇒ Object



150
151
152
153
# File 'lib/katakata_irb/type_analyzer.rb', line 150

def evaluate_embedded_statements_node(node, scope)
  node.statements ? evaluate(node.statements, scope) : KatakataIrb::Types::NIL
  KatakataIrb::Types::STRING
end

#evaluate_embedded_variable_node(node, scope) ⇒ Object



155
156
157
158
# File 'lib/katakata_irb/type_analyzer.rb', line 155

def evaluate_embedded_variable_node(node, scope)
  evaluate node.variable, scope
  KatakataIrb::Types::STRING
end

#evaluate_false_node(_node, _scope) ⇒ Object



207
# File 'lib/katakata_irb/type_analyzer.rb', line 207

def evaluate_false_node(_node, _scope) = KatakataIrb::Types::FALSE

#evaluate_flip_flop_node(node, scope) ⇒ Object



744
745
746
747
748
# File 'lib/katakata_irb/type_analyzer.rb', line 744

def evaluate_flip_flop_node(node, scope)
  scope.conditional { evaluate node.left, _1 } if node.left
  scope.conditional { evaluate node.right, _1 } if node.right
  KatakataIrb::Types::BOOLEAN
end

#evaluate_float_node(_node, _scope) ⇒ Object



108
# File 'lib/katakata_irb/type_analyzer.rb', line 108

def evaluate_float_node(_node, _scope) = KatakataIrb::Types::FLOAT

#evaluate_for_node(node, scope) ⇒ Object



687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# File 'lib/katakata_irb/type_analyzer.rb', line 687

def evaluate_for_node(node, scope)
  node.statements
  collection = evaluate node.collection, scope
  inner_scope = KatakataIrb::Scope.new scope, { KatakataIrb::Scope::BREAK_RESULT => nil }
  ary_type = method_call collection, :to_ary, [], nil, nil, nil, name_match: false
  element_types = ary_type.types.filter_map do |ary|
    ary.params[:Elem] if ary.is_a?(KatakataIrb::Types::InstanceType) && ary.klass == Array
  end
  element_type = KatakataIrb::Types::UnionType[*element_types]
  inner_scope.conditional do |s|
    evaluate_write node.index, element_type, s, nil
    evaluate node.statements, s if node.statements
  end
  inner_scope.merge_jumps
  scope.update inner_scope
  breaks = inner_scope[KatakataIrb::Scope::BREAK_RESULT]
  breaks ? KatakataIrb::Types::UnionType[breaks, collection] : collection
end

#evaluate_forwarding_super_node(_node, _scope) ⇒ Object



546
# File 'lib/katakata_irb/type_analyzer.rb', line 546

def evaluate_forwarding_super_node(_node, _scope) = KatakataIrb::Types::OBJECT

#evaluate_hash(node, scope) ⇒ Object



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
# File 'lib/katakata_irb/type_analyzer.rb', line 166

def evaluate_hash(node, scope)
  keys = []
  values = []
  node.elements.each do |assoc|
    case assoc
    when Prism::AssocNode
      keys << evaluate(assoc.key, scope)
      values << evaluate(assoc.value, scope)
    when Prism::AssocSplatNode
      next unless assoc.value # def f(**); {**}

      hash = evaluate assoc.value, scope
      unless hash.is_a?(KatakataIrb::Types::InstanceType) && hash.klass == Hash
        hash = method_call hash, :to_hash, [], nil, nil, scope
      end
      if hash.is_a?(KatakataIrb::Types::InstanceType) && hash.klass == Hash
        keys << hash.params[:K] if hash.params[:K]
        values << hash.params[:V] if hash.params[:V]
      end
    end
  end
  if keys.empty? && values.empty?
    KatakataIrb::Types::InstanceType.new Hash
  else
    KatakataIrb::Types::InstanceType.new Hash, K: KatakataIrb::Types::UnionType[*keys], V: KatakataIrb::Types::UnionType[*values]
  end
end

#evaluate_hash_node(node, scope) ⇒ Object



164
# File 'lib/katakata_irb/type_analyzer.rb', line 164

def evaluate_hash_node(node, scope) = evaluate_hash(node, scope)

#evaluate_if_node(node, scope) ⇒ Object



473
# File 'lib/katakata_irb/type_analyzer.rb', line 473

def evaluate_if_node(node, scope) = evaluate_if_unless(node, scope)

#evaluate_if_unless(node, scope) ⇒ Object



475
476
477
478
479
480
481
# File 'lib/katakata_irb/type_analyzer.rb', line 475

def evaluate_if_unless(node, scope)
  evaluate node.predicate, scope
  KatakataIrb::Types::UnionType[*scope.run_branches(
    -> { node.statements ? evaluate(node.statements, _1) : KatakataIrb::Types::NIL },
    -> { node.consequent ? evaluate(node.consequent, _1) : KatakataIrb::Types::NIL }
  )]
end

#evaluate_imaginary_node(_node, _scope) ⇒ Object



112
# File 'lib/katakata_irb/type_analyzer.rb', line 112

def evaluate_imaginary_node(_node, _scope) = KatakataIrb::Types::COMPLEX

#evaluate_implicit_node(node, scope) ⇒ Object



762
763
764
# File 'lib/katakata_irb/type_analyzer.rb', line 762

def evaluate_implicit_node(node, scope)
  evaluate node.value, scope
end

#evaluate_index_and_write_node(node, scope) ⇒ Object



320
# File 'lib/katakata_irb/type_analyzer.rb', line 320

def evaluate_index_and_write_node(node, scope) = evaluate_call_write(node, scope, :and, :[]=)

#evaluate_index_operator_write_node(node, scope) ⇒ Object



319
# File 'lib/katakata_irb/type_analyzer.rb', line 319

def evaluate_index_operator_write_node(node, scope) = evaluate_call_write(node, scope, :operator, :[]=)

#evaluate_index_or_write_node(node, scope) ⇒ Object



321
# File 'lib/katakata_irb/type_analyzer.rb', line 321

def evaluate_index_or_write_node(node, scope) = evaluate_call_write(node, scope, :or, :[]=)

#evaluate_integer_node(_node, _scope) ⇒ Object



106
# File 'lib/katakata_irb/type_analyzer.rb', line 106

def evaluate_integer_node(_node, _scope) = KatakataIrb::Types::INTEGER

#evaluate_interpolated_match_last_line_node(node, scope) ⇒ Object



777
778
779
780
# File 'lib/katakata_irb/type_analyzer.rb', line 777

def evaluate_interpolated_match_last_line_node(node, scope)
  node.parts.each { evaluate _1, scope }
  KatakataIrb::Types::BOOLEAN
end

#evaluate_interpolated_regular_expression_node(node, scope) ⇒ Object



145
146
147
148
# File 'lib/katakata_irb/type_analyzer.rb', line 145

def evaluate_interpolated_regular_expression_node(node, scope)
  node.parts.each { evaluate _1, scope }
  KatakataIrb::Types::REGEXP
end

#evaluate_interpolated_string_node(node, scope) ⇒ Object



130
131
132
133
# File 'lib/katakata_irb/type_analyzer.rb', line 130

def evaluate_interpolated_string_node(node, scope)
  node.parts.each { evaluate _1, scope }
  KatakataIrb::Types::STRING
end

#evaluate_interpolated_symbol_node(node, scope) ⇒ Object



140
141
142
143
# File 'lib/katakata_irb/type_analyzer.rb', line 140

def evaluate_interpolated_symbol_node(node, scope)
  node.parts.each { evaluate _1, scope }
  KatakataIrb::Types::SYMBOL
end

#evaluate_interpolated_x_string_node(node, scope) ⇒ Object



135
136
137
138
# File 'lib/katakata_irb/type_analyzer.rb', line 135

def evaluate_interpolated_x_string_node(node, scope)
  node.parts.each { evaluate _1, scope }
  KatakataIrb::Types::STRING
end

#evaluate_jump(node, scope, mode) ⇒ Object



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/katakata_irb/type_analyzer.rb', line 506

def evaluate_jump(node, scope, mode)
  internal_key = (
    case mode
    when :break
      KatakataIrb::Scope::BREAK_RESULT
    when :next
      KatakataIrb::Scope::NEXT_RESULT
    when :return
      KatakataIrb::Scope::RETURN_RESULT
    end
  )
  jump_value = (
    arguments = node.arguments&.arguments
    if arguments.nil? || arguments.empty?
      KatakataIrb::Types::NIL
    elsif arguments.size == 1 && !arguments.first.is_a?(Prism::SplatNode)
      evaluate arguments.first, scope
    else
      KatakataIrb::Types.array_of evaluate_list_splat_items(arguments, scope)
    end
  )
  scope.terminate_with internal_key, jump_value
  KatakataIrb::Types::NIL
end

#evaluate_keyword_hash_node(node, scope) ⇒ Object



165
# File 'lib/katakata_irb/type_analyzer.rb', line 165

def evaluate_keyword_hash_node(node, scope) = evaluate_hash(node, scope)

#evaluate_lambda_node(node, scope) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
# File 'lib/katakata_irb/type_analyzer.rb', line 430

def evaluate_lambda_node(node, scope)
  local_table = node.locals.to_h { [_1.to_s, KatakataIrb::Types::OBJECT] }
  block_scope = KatakataIrb::Scope.new scope, { **local_table, KatakataIrb::Scope::BREAK_RESULT => nil, KatakataIrb::Scope::NEXT_RESULT => nil, KatakataIrb::Scope::RETURN_RESULT => nil }
  block_scope.conditional do |s|
    assign_parameters node.parameters.parameters, s, [], {} if node.parameters&.parameters
    evaluate node.body, s if node.body
  end
  block_scope.merge_jumps
  scope.update block_scope
  KatakataIrb::Types::PROC
end

#evaluate_list_splat_items(list, scope) ⇒ Object



1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
# File 'lib/katakata_irb/type_analyzer.rb', line 1084

def evaluate_list_splat_items(list, scope)
  items = list.flat_map do |node|
    if node.is_a? Prism::SplatNode
      next unless node.expression # def f(*); [*]

      splat = evaluate node.expression, scope
      array_elem, non_array = partition_to_array splat.nonnillable, :to_a
      [*array_elem, *non_array]
    else
      evaluate node, scope
    end
  end.compact.uniq
  KatakataIrb::Types::UnionType[*items]
end

#evaluate_match_last_line_node(_node, _scope) ⇒ Object



773
774
775
# File 'lib/katakata_irb/type_analyzer.rb', line 773

def evaluate_match_last_line_node(_node, _scope)
  KatakataIrb::Types::BOOLEAN
end

#evaluate_match_pattern(value, pattern, scope) ⇒ Object



971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# File 'lib/katakata_irb/type_analyzer.rb', line 971

def evaluate_match_pattern(value, pattern, scope)
  # TODO: scope.terminate_with KatakataIrb::Scope::PATTERNMATCH_BREAK, KatakataIrb::Types::NIL
  case pattern
  when Prism::FindPatternNode
    # TODO
    evaluate_match_pattern KatakataIrb::Types::OBJECT, pattern.left, scope
    pattern.requireds.each { evaluate_match_pattern KatakataIrb::Types::OBJECT, _1, scope }
    evaluate_match_pattern KatakataIrb::Types::OBJECT, pattern.right, scope
  when Prism::ArrayPatternNode
    # TODO
    pattern.requireds.each { evaluate_match_pattern KatakataIrb::Types::OBJECT, _1, scope }
    evaluate_match_pattern KatakataIrb::Types::OBJECT, pattern.rest, scope if pattern.rest
    pattern.posts.each { evaluate_match_pattern KatakataIrb::Types::OBJECT, _1, scope }
    KatakataIrb::Types::ARRAY
  when Prism::HashPatternNode
    # TODO
    pattern.elements.each { evaluate_match_pattern KatakataIrb::Types::OBJECT, _1, scope }
    if pattern.respond_to?(:rest) && pattern.rest
      evaluate_match_pattern KatakataIrb::Types::OBJECT, pattern.rest, scope
    end
    KatakataIrb::Types::HASH
  when Prism::AssocNode
    evaluate_match_pattern value, pattern.value, scope if pattern.value
    KatakataIrb::Types::OBJECT
  when Prism::AssocSplatNode
    # TODO
    evaluate_match_pattern KatakataIrb::Types::HASH, pattern.value, scope
    KatakataIrb::Types::OBJECT
  when Prism::PinnedVariableNode
    evaluate pattern.variable, scope
  when Prism::PinnedExpressionNode
    evaluate pattern.expression, scope
  when Prism::LocalVariableTargetNode
    scope[pattern.name.to_s] = value
  when Prism::AlternationPatternNode
    KatakataIrb::Types::UnionType[evaluate_match_pattern(value, pattern.left, scope), evaluate_match_pattern(value, pattern.right, scope)]
  when Prism::CapturePatternNode
    capture_type = class_or_value_to_instance evaluate_match_pattern(value, pattern.value, scope)
    value = capture_type unless capture_type.types.empty? || capture_type.types == [KatakataIrb::Types::OBJECT]
    evaluate_match_pattern value, pattern.target, scope
  when Prism::SplatNode
    value = KatakataIrb::Types.array_of value
    evaluate_match_pattern value, pattern.expression, scope if pattern.expression
    value
  else
    # literal node
    type = evaluate(pattern, scope)
    class_or_value_to_instance(type)
  end
end

#evaluate_match_predicate_node(node, scope) ⇒ Object



726
727
728
729
730
# File 'lib/katakata_irb/type_analyzer.rb', line 726

def evaluate_match_predicate_node(node, scope)
  value_type = evaluate node.value, scope
  scope.conditional { evaluate_match_pattern value_type, node.pattern, _1 }
  KatakataIrb::Types::BOOLEAN
end

#evaluate_match_required_node(node, scope) ⇒ Object



720
721
722
723
724
# File 'lib/katakata_irb/type_analyzer.rb', line 720

def evaluate_match_required_node(node, scope)
  value_type = evaluate node.value, scope
  evaluate_match_pattern value_type, node.pattern, scope
  KatakataIrb::Types::NIL # void value
end

#evaluate_match_write_node(node, scope) ⇒ Object



766
767
768
769
770
771
# File 'lib/katakata_irb/type_analyzer.rb', line 766

def evaluate_match_write_node(node, scope)
  # /(?<a>)(?<b>)/ =~ string
  evaluate node.call, scope
  node.locals.each { scope[_1.to_s] = KatakataIrb::Types::UnionType[KatakataIrb::Types::STRING, KatakataIrb::Types::NIL] }
  KatakataIrb::Types::BOOLEAN
end

#evaluate_missing_node(_node, _scope) ⇒ Object



793
# File 'lib/katakata_irb/type_analyzer.rb', line 793

def evaluate_missing_node(_node, _scope) = KatakataIrb::Types::NIL

#evaluate_module_node(node, scope) ⇒ Object



630
# File 'lib/katakata_irb/type_analyzer.rb', line 630

def evaluate_module_node(node, scope) = evaluate_class_module(node, scope, false)

#evaluate_multi_target_node(node, scope) ⇒ Object



750
751
752
753
754
# File 'lib/katakata_irb/type_analyzer.rb', line 750

def evaluate_multi_target_node(node, scope)
  # Raw MultiTargetNode, incomplete code like `a,b`, `*a`.
  evaluate_multi_write_receiver node, scope, nil
  KatakataIrb::Types::NIL
end

#evaluate_multi_write(node, values, scope, evaluated_receivers) ⇒ Object



1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
# File 'lib/katakata_irb/type_analyzer.rb', line 1046

def evaluate_multi_write(node, values, scope, evaluated_receivers)
  pre_targets = node.lefts
  splat_target = node.rest
  post_targets = node.rights
  size = pre_targets.size + (splat_target ? 1 : 0) + post_targets.size
  values = values.is_a?(Array) ? values.dup : sized_splat(values, :to_ary, size)
  pre_pairs = pre_targets.zip(values.shift(pre_targets.size))
  post_pairs = post_targets.zip(values.pop(post_targets.size))
  splat_pairs = splat_target ? [[splat_target, KatakataIrb::Types::UnionType[*values]]] : []
  (pre_pairs + splat_pairs + post_pairs).each do |target, value|
    evaluate_write target, value || KatakataIrb::Types::NIL, scope, evaluated_receivers
  end
end

#evaluate_multi_write_node(node, scope) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/katakata_irb/type_analyzer.rb', line 451

def evaluate_multi_write_node(node, scope)
  evaluated_receivers = {}
  evaluate_multi_write_receiver node, scope, evaluated_receivers
  value = (
    if node.value.is_a? Prism::ArrayNode
      if node.value.elements.any?(Prism::SplatNode)
        evaluate node.value, scope
      else
        node.value.elements.map do |n|
          evaluate n, scope
        end
      end
    elsif node.value
      evaluate node.value, scope
    else
      KatakataIrb::Types::NIL
    end
  )
  evaluate_multi_write node, value, scope, evaluated_receivers
  value.is_a?(Array) ? KatakataIrb::Types.array_of(*value) : value
end

#evaluate_multi_write_receiver(node, scope, evaluated_receivers) ⇒ Object



1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
# File 'lib/katakata_irb/type_analyzer.rb', line 1060

def evaluate_multi_write_receiver(node, scope, evaluated_receivers)
  case node
  when Prism::MultiWriteNode, Prism::MultiTargetNode
    targets = [*node.lefts, *node.rest, *node.rights]
    targets.each { evaluate_multi_write_receiver _1, scope, evaluated_receivers }
  when Prism::CallNode
    if node.receiver
      receiver = evaluate(node.receiver, scope)
      evaluated_receivers[node.receiver] = receiver if evaluated_receivers
    end
    if node.arguments
      node.arguments.arguments&.each do |arg|
        if arg.is_a? Prism::SplatNode
          evaluate arg.expression, scope
        else
          evaluate arg, scope
        end
      end
    end
  when Prism::SplatNode
    evaluate_multi_write_receiver node.expression, scope, evaluated_receivers if node.expression
  end
end

#evaluate_next_node(node, scope) ⇒ Object



504
# File 'lib/katakata_irb/type_analyzer.rb', line 504

def evaluate_next_node(node, scope) = evaluate_jump(node, scope, :next)

#evaluate_nil_node(_node, _scope) ⇒ Object



209
# File 'lib/katakata_irb/type_analyzer.rb', line 209

def evaluate_nil_node(_node, _scope) = KatakataIrb::Types::NIL

#evaluate_numbered_reference_read_node(_node, _scope) ⇒ Object



217
218
219
# File 'lib/katakata_irb/type_analyzer.rb', line 217

def evaluate_numbered_reference_read_node(_node, _scope)
  KatakataIrb::Types::UnionType[KatakataIrb::Types::STRING, KatakataIrb::Types::NIL]
end

#evaluate_or_node(node, scope) ⇒ Object



305
# File 'lib/katakata_irb/type_analyzer.rb', line 305

def evaluate_or_node(node, scope) = evaluate_and_or(node, scope, and_op: false)

#evaluate_parentheses_node(node, scope) ⇒ Object



194
195
196
# File 'lib/katakata_irb/type_analyzer.rb', line 194

def evaluate_parentheses_node(node, scope)
  node.body ? evaluate(node.body, scope) : KatakataIrb::Types::NIL
end

#evaluate_post_execution_node(node, scope) ⇒ Object



786
787
788
# File 'lib/katakata_irb/type_analyzer.rb', line 786

def evaluate_post_execution_node(node, scope)
  node.statements && @dig_targets.dig?(node.statements) ? evaluate(node.statements, scope) : KatakataIrb::Types::NIL
end

#evaluate_pre_execution_node(node, scope) ⇒ Object



782
783
784
# File 'lib/katakata_irb/type_analyzer.rb', line 782

def evaluate_pre_execution_node(node, scope)
  node.statements ? evaluate(node.statements, scope) : KatakataIrb::Types::NIL
end

#evaluate_program(program, scope) ⇒ Object



1161
1162
1163
1164
1165
1166
# File 'lib/katakata_irb/type_analyzer.rb', line 1161

def evaluate_program(program, scope)
  # statements.body[0] is local variable assign code
  program.statements.body[1..].each do |statement|
    evaluate statement, scope
  end
end

#evaluate_program_node(node, scope) ⇒ Object



55
56
57
# File 'lib/katakata_irb/type_analyzer.rb', line 55

def evaluate_program_node(node, scope)
  evaluate node.statements, scope
end

#evaluate_range_node(node, scope) ⇒ Object



732
733
734
735
736
737
# File 'lib/katakata_irb/type_analyzer.rb', line 732

def evaluate_range_node(node, scope)
  beg_type = evaluate node.left, scope if node.left
  end_type = evaluate node.right, scope if node.right
  elem = (KatakataIrb::Types::UnionType[*[beg_type, end_type].compact]).nonnillable
  KatakataIrb::Types::InstanceType.new Range, Elem: elem
end

#evaluate_rational_node(_node, _scope) ⇒ Object



110
# File 'lib/katakata_irb/type_analyzer.rb', line 110

def evaluate_rational_node(_node, _scope) = KatakataIrb::Types::RATIONAL

#evaluate_redo_node(_node, scope) ⇒ Object



536
537
538
539
# File 'lib/katakata_irb/type_analyzer.rb', line 536

def evaluate_redo_node(_node, scope)
  scope.terminate
  KatakataIrb::Types::NIL
end

#evaluate_reference_read(node, scope) ⇒ Object Also known as: evaluate_constant_read_node, evaluate_global_variable_read_node, evaluate_local_variable_read_node, evaluate_class_variable_read_node, evaluate_instance_variable_read_node



225
226
227
# File 'lib/katakata_irb/type_analyzer.rb', line 225

def evaluate_reference_read(node, scope)
  scope[node.name.to_s] || KatakataIrb::Types::NIL
end

#evaluate_reference_write(node, scope) ⇒ Object Also known as: evaluate_constant_write_node, evaluate_global_variable_write_node, evaluate_local_variable_write_node, evaluate_class_variable_write_node, evaluate_instance_variable_write_node



442
443
444
# File 'lib/katakata_irb/type_analyzer.rb', line 442

def evaluate_reference_write(node, scope)
  scope[node.name.to_s] = evaluate node.value, scope
end

#evaluate_regular_expression_node(_node, _scope) ⇒ Object



122
# File 'lib/katakata_irb/type_analyzer.rb', line 122

def evaluate_regular_expression_node(_node, _scope) = KatakataIrb::Types::REGEXP

#evaluate_rescue_modifier_node(node, scope) ⇒ Object



605
606
607
608
609
# File 'lib/katakata_irb/type_analyzer.rb', line 605

def evaluate_rescue_modifier_node(node, scope)
  a = evaluate node.expression, scope
  b = scope.conditional { evaluate node.rescue_expression, _1 }
  KatakataIrb::Types::UnionType[a, b]
end

#evaluate_rescue_node(node, scope) ⇒ Object



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/katakata_irb/type_analyzer.rb', line 576

def evaluate_rescue_node(node, scope)
  run_rescue = lambda do |s|
    if node.reference
      error_classes_type = evaluate_list_splat_items node.exceptions, s
      error_types = error_classes_type.types.filter_map do
        KatakataIrb::Types::InstanceType.new _1.module_or_class if _1.is_a?(KatakataIrb::Types::SingletonType)
      end
      error_types << KatakataIrb::Types::InstanceType.new(StandardError) if error_types.empty?
      error_type = KatakataIrb::Types::UnionType[*error_types]
      case node.reference
      when Prism::LocalVariableTargetNode, Prism::InstanceVariableTargetNode, Prism::ClassVariableTargetNode, Prism::GlobalVariableTargetNode, Prism::ConstantTargetNode
        s[node.reference.name.to_s] = error_type
      when Prism::CallNode
        evaluate node.reference, s
      end
    end
    node.statements ? evaluate(node.statements, s) : KatakataIrb::Types::NIL
  end
  if node.consequent # begin; rescue A; rescue B; end
    types = scope.run_branches(
      run_rescue,
      -> { evaluate node.consequent, _1 }
    )
    KatakataIrb::Types::UnionType[*types]
  else
    run_rescue.call scope
  end
end

#evaluate_retry_node(_node, scope) ⇒ Object



541
542
543
544
# File 'lib/katakata_irb/type_analyzer.rb', line 541

def evaluate_retry_node(_node, scope)
  scope.terminate
  KatakataIrb::Types::NIL
end

#evaluate_return_node(node, scope) ⇒ Object



505
# File 'lib/katakata_irb/type_analyzer.rb', line 505

def evaluate_return_node(node, scope) = evaluate_jump(node, scope, :return)

#evaluate_self_node(_node, scope) ⇒ Object



203
# File 'lib/katakata_irb/type_analyzer.rb', line 203

def evaluate_self_node(_node, scope) = scope.self_type

#evaluate_singleton_class_node(node, scope) ⇒ Object



611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/katakata_irb/type_analyzer.rb', line 611

def evaluate_singleton_class_node(node, scope)
  klass_types = evaluate(node.expression, scope).types.filter_map do |type|
    KatakataIrb::Types::SingletonType.new type.klass if type.is_a? KatakataIrb::Types::InstanceType
  end
  klass_types = [KatakataIrb::Types::CLASS] if klass_types.empty?
  table = node.locals.to_h { [_1.to_s, KatakataIrb::Types::NIL] }
  sclass_scope = KatakataIrb::Scope.new(
    scope,
    { **table, KatakataIrb::Scope::BREAK_RESULT => nil, KatakataIrb::Scope::NEXT_RESULT => nil, KatakataIrb::Scope::RETURN_RESULT => nil },
    trace_ivar: false,
    trace_lvar: false,
    self_type: KatakataIrb::Types::UnionType[*klass_types]
  )
  result = node.body ? evaluate(node.body, sclass_scope) : KatakataIrb::Types::NIL
  scope.update sclass_scope
  result
end

#evaluate_source_encoding_node(_node, _scope) ⇒ Object



215
# File 'lib/katakata_irb/type_analyzer.rb', line 215

def evaluate_source_encoding_node(_node, _scope) = KatakataIrb::Types::InstanceType.new(Encoding)

#evaluate_source_file_node(_node, _scope) ⇒ Object



211
# File 'lib/katakata_irb/type_analyzer.rb', line 211

def evaluate_source_file_node(_node, _scope) = KatakataIrb::Types::STRING

#evaluate_source_line_node(_node, _scope) ⇒ Object



213
# File 'lib/katakata_irb/type_analyzer.rb', line 213

def evaluate_source_line_node(_node, _scope) = KatakataIrb::Types::INTEGER

#evaluate_splat_node(node, scope) ⇒ Object



756
757
758
759
760
# File 'lib/katakata_irb/type_analyzer.rb', line 756

def evaluate_splat_node(node, scope)
  # Raw SplatNode, incomplete code like `*a.`
  evaluate_multi_write_receiver node.expression, scope, nil if node.expression
  KatakataIrb::Types::NIL
end

#evaluate_statements_node(node, scope) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/katakata_irb/type_analyzer.rb', line 59

def evaluate_statements_node(node, scope)
  if node.body.empty?
    KatakataIrb::Types::NIL
  else
    node.body.map { evaluate _1, scope }.last
  end
end

#evaluate_string_concat_node(node, scope) ⇒ Object



124
125
126
127
128
# File 'lib/katakata_irb/type_analyzer.rb', line 124

def evaluate_string_concat_node(node, scope)
  evaluate node.left, scope
  evaluate node.right, scope
  KatakataIrb::Types::STRING
end

#evaluate_string_node(_node, _scope) ⇒ Object



114
# File 'lib/katakata_irb/type_analyzer.rb', line 114

def evaluate_string_node(_node, _scope) = KatakataIrb::Types::STRING

#evaluate_super_node(node, scope) ⇒ Object



548
549
550
551
# File 'lib/katakata_irb/type_analyzer.rb', line 548

def evaluate_super_node(node, scope)
  evaluate_list_splat_items node.arguments.arguments, scope if node.arguments
  KatakataIrb::Types::OBJECT
end

#evaluate_symbol_node(_node, _scope) ⇒ Object



120
# File 'lib/katakata_irb/type_analyzer.rb', line 120

def evaluate_symbol_node(_node, _scope) = KatakataIrb::Types::SYMBOL

#evaluate_true_node(_node, _scope) ⇒ Object



205
# File 'lib/katakata_irb/type_analyzer.rb', line 205

def evaluate_true_node(_node, _scope) = KatakataIrb::Types::TRUE

#evaluate_undef_node(_node, _scope) ⇒ Object



792
# File 'lib/katakata_irb/type_analyzer.rb', line 792

def evaluate_undef_node(_node, _scope) = KatakataIrb::Types::NIL

#evaluate_unless_node(node, scope) ⇒ Object



474
# File 'lib/katakata_irb/type_analyzer.rb', line 474

def evaluate_unless_node(node, scope) = evaluate_if_unless(node, scope)

#evaluate_variable_and_write(node, scope) ⇒ Object Also known as: evaluate_global_variable_and_write_node, evaluate_local_variable_and_write_node, evaluate_class_variable_and_write_node, evaluate_instance_variable_and_write_node



363
364
365
366
# File 'lib/katakata_irb/type_analyzer.rb', line 363

def evaluate_variable_and_write(node, scope)
  right = scope.conditional { evaluate node.value, scope }
  scope[node.name.to_s] = KatakataIrb::Types::UnionType[right, KatakataIrb::Types::NIL, KatakataIrb::Types::FALSE]
end

#evaluate_variable_operator_write(node, scope) ⇒ Object Also known as: evaluate_global_variable_operator_write_node, evaluate_local_variable_operator_write_node, evaluate_class_variable_operator_write_node, evaluate_instance_variable_operator_write_node



353
354
355
356
357
# File 'lib/katakata_irb/type_analyzer.rb', line 353

def evaluate_variable_operator_write(node, scope)
  left = scope[node.name.to_s] || KatakataIrb::Types::OBJECT
  right = evaluate node.value, scope
  scope[node.name.to_s] = method_call left, node.operator, [right], nil, nil, scope, name_match: false
end

#evaluate_variable_or_write(node, scope) ⇒ Object Also known as: evaluate_global_variable_or_write_node, evaluate_local_variable_or_write_node, evaluate_class_variable_or_write_node, evaluate_instance_variable_or_write_node



372
373
374
375
376
# File 'lib/katakata_irb/type_analyzer.rb', line 372

def evaluate_variable_or_write(node, scope)
  left = scope[node.name.to_s] || KatakataIrb::Types::OBJECT
  right = scope.conditional { evaluate node.value, scope }
  scope[node.name.to_s] = KatakataIrb::Types::UnionType[left, right]
end

#evaluate_while_until(node, scope) ⇒ Object Also known as: evaluate_while_node, evaluate_until_node



487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/katakata_irb/type_analyzer.rb', line 487

def evaluate_while_until(node, scope)
  inner_scope = KatakataIrb::Scope.new scope, { KatakataIrb::Scope::BREAK_RESULT => nil }
  evaluate node.predicate, inner_scope
  if node.statements
    inner_scope.conditional do |s|
      evaluate node.statements, s
    end
  end
  inner_scope.merge_jumps
  scope.update inner_scope
  breaks = inner_scope[KatakataIrb::Scope::BREAK_RESULT]
  breaks ? KatakataIrb::Types::UnionType[breaks, KatakataIrb::Types::NIL] : KatakataIrb::Types::NIL
end

#evaluate_write(node, value, scope, evaluated_receivers) ⇒ Object



1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
# File 'lib/katakata_irb/type_analyzer.rb', line 1029

def evaluate_write(node, value, scope, evaluated_receivers)
  case node
  when Prism::MultiTargetNode
    evaluate_multi_write node, value, scope, evaluated_receivers
  when Prism::CallNode
    evaluated_receivers&.[](node.receiver) || evaluate(node.receiver, scope) if node.receiver
  when Prism::SplatNode
    evaluate_write node.expression, KatakataIrb::Types.array_of(value), scope, evaluated_receivers if node.expression
  when Prism::LocalVariableTargetNode, Prism::GlobalVariableTargetNode, Prism::InstanceVariableTargetNode, Prism::ClassVariableTargetNode, Prism::ConstantTargetNode
    scope[node.name.to_s] = value
  when Prism::ConstantPathTargetNode
    receiver = evaluated_receivers&.[](node.parent) || evaluate(node.parent, scope) if node.parent
    const_path_write receiver, node.child.name.to_s, value, scope
    value
  end
end

#evaluate_x_string_node(_node, _scope) ⇒ Object



116
117
118
# File 'lib/katakata_irb/type_analyzer.rb', line 116

def evaluate_x_string_node(_node, _scope)
  KatakataIrb::Types::UnionType[KatakataIrb::Types::STRING, KatakataIrb::Types::NIL]
end

#evaluate_yield_node(node, scope) ⇒ Object



531
532
533
534
# File 'lib/katakata_irb/type_analyzer.rb', line 531

def evaluate_yield_node(node, scope)
  evaluate_list_splat_items node.arguments.arguments, scope if node.arguments
  KatakataIrb::Types::OBJECT
end

#method_call(receiver, method_name, args, kwargs, block, scope, name_match: true) ⇒ Object



1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
# File 'lib/katakata_irb/type_analyzer.rb', line 1122

def method_call(receiver, method_name, args, kwargs, block, scope, name_match: true)
  methods = KatakataIrb::Types.rbs_methods receiver, method_name.to_sym, args, kwargs, !!block
  block_called = false
  type_breaks = methods.map do |method, given_params, method_params|
    receiver_vars = receiver.is_a?(KatakataIrb::Types::InstanceType) ? receiver.params : {}
    free_vars = method.type.free_variables - receiver_vars.keys.to_set
    vars = receiver_vars.merge KatakataIrb::Types.match_free_variables(free_vars, method_params, given_params)
    if block && method.block
      params_type = method.block.type.required_positionals.map do |func_param|
        KatakataIrb::Types.from_rbs_type func_param.type, receiver, vars
      end
      self_type = KatakataIrb::Types.from_rbs_type method.block.self_type, receiver, vars if method.block.self_type
      block_response, breaks = block.call params_type, self_type
      block_called = true
      vars.merge! KatakataIrb::Types.match_free_variables(free_vars - vars.keys.to_set, [method.block.type.return_type], [block_response])
    end
    if KatakataIrb::Types.method_return_bottom?(method)
      [nil, breaks]
    else
      [KatakataIrb::Types.from_rbs_type(method.type.return_type, receiver, vars || {}), breaks]
    end
  end
  block&.call [], nil unless block_called
  terminates = !type_breaks.empty? && type_breaks.map(&:first).all?(&:nil?)
  types = type_breaks.map(&:first).compact
  breaks = type_breaks.map(&:last).compact
  types << OBJECT_METHODS[method_name.to_sym] if name_match && OBJECT_METHODS.has_key?(method_name.to_sym)

  if method_name.to_sym == :new
    receiver.types.each do |type|
      if type.is_a?(KatakataIrb::Types::SingletonType) && type.module_or_class.is_a?(Class)
        types << KatakataIrb::Types::InstanceType.new(type.module_or_class)
      end
    end
  end
  scope&.terminate if terminates && breaks.empty?
  KatakataIrb::Types::UnionType[*types, *breaks]
end

#partition_to_array(value, method) ⇒ Object



1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
# File 'lib/katakata_irb/type_analyzer.rb', line 1106

def partition_to_array(value, method)
  arrays, non_arrays = value.types.partition { _1.is_a?(KatakataIrb::Types::InstanceType) && _1.klass == Array }
  non_arrays.select! do |type|
    to_array_result = method_call type, method, [], nil, nil, nil, name_match: false
    if to_array_result.is_a?(KatakataIrb::Types::InstanceType) && to_array_result.klass == Array
      arrays << to_array_result
      false
    else
      true
    end
  end
  array_elem = arrays.empty? ? nil : KatakataIrb::Types::UnionType[*arrays.map { _1.params[:Elem] || KatakataIrb::Types::OBJECT }]
  non_array = non_arrays.empty? ? nil : KatakataIrb::Types::UnionType[*non_arrays]
  [array_elem, non_array]
end

#sized_splat(value, method, size) ⇒ Object



1099
1100
1101
1102
1103
1104
# File 'lib/katakata_irb/type_analyzer.rb', line 1099

def sized_splat(value, method, size)
  array_elem, non_array = partition_to_array value, method
  values = [KatakataIrb::Types::UnionType[*array_elem, *non_array]]
  values += [array_elem] * (size - 1) if array_elem && size >= 1
  values
end