Class: TypeProf::Core::MethodDefBox

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

#add_symbol_proc_call_box, #destroy_symbol_proc_call_boxes, #on_type_added, #on_type_removed, #reuse, #run, #to_s

Constructor Details

#initialize(node, genv, cpath, singleton, mid, f_args, ret_boxes) ⇒ MethodDefBox

Returns a new instance of MethodDefBox.



729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
# File 'lib/typeprof/core/graph/box.rb', line 729

def initialize(node, genv, cpath, singleton, mid, f_args, ret_boxes)
  super(node)
  @cpath = cpath
  @singleton = singleton
  @mid = mid
  raise unless f_args
  @f_args = f_args
  raise unless f_args.is_a?(FormalArguments)

  @record_block = RecordBlock.new(@node)
  if @f_args.block
    record_blk_ty = Source.new(Type::Proc.new(genv, @record_block))
    record_blk_ty.add_edge(genv, @f_args.block)
  end

  @ret_boxes = ret_boxes
  @ret = Vertex.new(node)
  ret_boxes.each do |box|
    @changes.add_edge(genv, box.ret, @ret)
  end
  me = genv.resolve_method(@cpath, @singleton, @mid)
  me.add_def(self)
  me.add_run_all_method_call_boxes(genv) if me.decls.empty?
  genv.add_run(self)
end

Instance Attribute Details

#cpath ⇒ Object (readonly)

Returns the value of attribute cpath.



757
758
759
# File 'lib/typeprof/core/graph/box.rb', line 757

def cpath
  @cpath
end

#f_args ⇒ Object (readonly)

Returns the value of attribute f_args.



757
758
759
# File 'lib/typeprof/core/graph/box.rb', line 757

def f_args
  @f_args
end

#mid ⇒ Object (readonly)

Returns the value of attribute mid.



757
758
759
# File 'lib/typeprof/core/graph/box.rb', line 757

def mid
  @mid
end

#node ⇒ Object

Returns the value of attribute node.



755
756
757
# File 'lib/typeprof/core/graph/box.rb', line 755

def node
  @node
end

#record_block ⇒ Object (readonly)

Returns the value of attribute record_block.



757
758
759
# File 'lib/typeprof/core/graph/box.rb', line 757

def record_block
  @record_block
end

#ret ⇒ Object (readonly)

Returns the value of attribute ret.



757
758
759
# File 'lib/typeprof/core/graph/box.rb', line 757

def ret
  @ret
end

#singleton ⇒ Object (readonly)

Returns the value of attribute singleton.



757
758
759
# File 'lib/typeprof/core/graph/box.rb', line 757

def singleton
  @singleton
end

Instance Method Details

#call(changes, genv, a_args, ret) ⇒ Object



833
834
835
836
837
838
839
840
841
842
843
# File 'lib/typeprof/core/graph/box.rb', line 833

def call(changes, genv, a_args, ret)
  a_args = a_args.with_keywords_normalized_for(@node)
  if pass_arguments(changes, genv, a_args)
    if @node.is_a?(AST::DefNode)
      @node.body.lenv.forward_args&.accept_actual_arguments(genv, changes, a_args)
    end
    changes.add_edge(genv, a_args.block, @f_args.block) if @f_args.block && a_args.block

    changes.add_edge(genv, @ret, ret)
  end
end

#destroy(genv) ⇒ Object



759
760
761
762
763
764
765
# File 'lib/typeprof/core/graph/box.rb', line 759

def destroy(genv)
  me = genv.resolve_method(@cpath, @singleton, @mid)
  me.remove_def(self)
  me.add_run_all_method_call_boxes(genv) if me.decls.empty?
  genv.add_run(self)
  super(genv)
end

#pass_arguments(changes, genv, a_args) ⇒ Object



829
830
831
# File 'lib/typeprof/core/graph/box.rb', line 829

def pass_arguments(changes, genv, a_args)
  @f_args.pass_arguments(changes, genv, a_args, @node)
end

#run0(genv, changes) ⇒ Object



767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
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
# File 'lib/typeprof/core/graph/box.rb', line 767

def run0(genv, changes)
  me = genv.resolve_method(@cpath, @singleton, @mid)
  return if me.decls.empty?

  # TODO: support "| ..."
  decl = me.decls.to_a.first
  # TODO: support overload?
  method_type = decl.method_types.first
  _block = method_type.block

  mod = genv.resolve_cpath(@cpath)
  if @singleton
    ty = Type::Singleton.new(genv, mod)
    param_map0 = Type.default_param_map(genv, ty)
  else
    type_params = mod.type_params.map {|(_name, _default_ty)| genv.untyped_arg } # TODO: better support
    ty = Type::Instance.new(genv, mod, type_params)
    param_map0 = Type.default_param_map(genv, ty)
    if ty.is_a?(Type::Instance)
      ty.mod.type_params.zip(ty.args) do |(name, _default_ty), arg|
        param_map0[name] = arg
      end
    end
  end
  method_type.type_params.each do |name, _default_ty|
    param_map0[name] = Source.new()
  end

  positional_args = []
  splat_flags = []

  method_type.req_positionals.each do |a_arg|
    positional_args << a_arg.contravariant_vertex(genv, changes, param_map0)
    splat_flags << false
  end
  method_type.opt_positionals.each do |a_arg|
    positional_args << a_arg.contravariant_vertex(genv, changes, param_map0)
    splat_flags << false
  end
  if method_type.rest_positionals
    elems = method_type.rest_positionals.contravariant_vertex(genv, changes, param_map0)
    positional_args << Source.new(genv.gen_ary_type(elems))
    splat_flags << true
  end
  method_type.post_positionals.each do |a_arg|
    positional_args << a_arg.contravariant_vertex(genv, changes, param_map0)
    splat_flags << false
  end

  a_args = ActualArguments.new(positional_args, splat_flags, nil, nil) # TODO: keywords and block
  if pass_arguments(changes, genv, a_args)
    # TODO: block
    f_ret = method_type.return_type.contravariant_vertex(genv, changes, param_map0)
    changes.add_edge(genv, f_ret, @ret)
    @ret_boxes.each do |ret_box|
      unless method_type.return_type.typecheck(genv, changes, ret_box.a_ret, param_map0)
        ret_box.wrong_return_type(method_type.return_type.show, changes)
      end
    end
  end
end

#show(output_parameter_names, ret: nil) ⇒ Object



845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
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
887
888
889
890
891
892
893
894
895
896
897
898
# File 'lib/typeprof/core/graph/box.rb', line 845

def show(output_parameter_names, ret: nil)
  block_show = []
  if @record_block.used
    blk_f_args = @record_block.f_args.map {|arg| arg.show }.join(", ")
    blk_ret = @record_block.ret.show
    block_show << "{ (#{ blk_f_args }) -> #{ blk_ret } }"
  end
  args = []
  @f_args.req_positionals.each do |f_vtx|
    args << Type.strip_parens(f_vtx.show)
  end
  @f_args.opt_positionals.each do |f_vtx|
    args << ("?" + Type.strip_parens(f_vtx.show))
  end
  if @f_args.rest_positionals
    args << ("*" + Type.strip_array(Type.strip_parens(@f_args.rest_positionals.show)))
  end
  @f_args.post_positionals.each do |var|
    args << Type.strip_parens(var.show)
  end
  if @node.respond_to?(:req_keywords) &&
     @node.req_keywords.size == @f_args.req_keywords.size &&
     @node.opt_keywords.size == @f_args.opt_keywords.size
    @node.req_keywords.zip(@f_args.req_keywords) do |name, f_vtx|
      args << "#{ name }: #{Type.strip_parens(f_vtx.show)}"
    end
    @node.opt_keywords.zip(@f_args.opt_keywords) do |name, f_vtx|
      args << "?#{ name }: #{Type.strip_parens(f_vtx.show)}"
    end
  end
  if @f_args.rest_keywords
    args << "**#{ Type.extract_hash_value_type(Type.strip_parens(@f_args.rest_keywords.show)) }"
  end

  if output_parameter_names && @node.is_a?(AST::DefNode)
    names = []
    names.concat(@node.req_positionals)
    names.concat(@node.opt_positionals)
    names << @node.rest_positionals if @node.rest_positionals
    names.concat(@node.post_positionals)
    names.concat(@node.req_keywords)
    names.concat(@node.opt_keywords)
    names << @node.rest_keywords if @node.rest_keywords
    args = args.zip(names).map do |arg, name|
      name ? "#{ arg } #{ name }" : arg
    end
  end

  args = args.join(", ")
  s = args.empty? ? [] : ["(#{ args })"]
  s << "#{ block_show.sort.join(" | ") }" unless block_show.empty?
  s << "-> #{ ret || (@mid == :initialize ? "void" : @ret.show) }"
  s.join(" ")
end