Class: TypeProf::Core::MethodDefBox
- Defined in:
- lib/typeprof/core/graph/box.rb
Instance Attribute Summary collapse
-
#cpath ⇒ Object
readonly
Returns the value of attribute cpath.
-
#f_args ⇒ Object
readonly
Returns the value of attribute f_args.
-
#mid ⇒ Object
readonly
Returns the value of attribute mid.
-
#node ⇒ Object
Returns the value of attribute node.
-
#record_block ⇒ Object
readonly
Returns the value of attribute record_block.
-
#ret ⇒ Object
readonly
Returns the value of attribute ret.
-
#singleton ⇒ Object
readonly
Returns the value of attribute singleton.
Attributes inherited from Box
Instance Method Summary collapse
- #call(changes, genv, a_args, ret) ⇒ Object
- #destroy(genv) ⇒ Object
-
#initialize(node, genv, cpath, singleton, mid, f_args, ret_boxes) ⇒ MethodDefBox
constructor
A new instance of MethodDefBox.
- #normalize_keyword_hash_argument_for_def(a_args) ⇒ Object
- #pass_arguments(changes, genv, a_args) ⇒ Object
- #run0(genv, changes) ⇒ Object
- #show(output_parameter_names) ⇒ Object
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.
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 |
# File 'lib/typeprof/core/graph/box.rb', line 740 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.
768 769 770 |
# File 'lib/typeprof/core/graph/box.rb', line 768 def cpath @cpath end |
#f_args ⇒ Object (readonly)
Returns the value of attribute f_args.
768 769 770 |
# File 'lib/typeprof/core/graph/box.rb', line 768 def f_args @f_args end |
#mid ⇒ Object (readonly)
Returns the value of attribute mid.
768 769 770 |
# File 'lib/typeprof/core/graph/box.rb', line 768 def mid @mid end |
#node ⇒ Object
Returns the value of attribute node.
766 767 768 |
# File 'lib/typeprof/core/graph/box.rb', line 766 def node @node end |
#record_block ⇒ Object (readonly)
Returns the value of attribute record_block.
768 769 770 |
# File 'lib/typeprof/core/graph/box.rb', line 768 def record_block @record_block end |
#ret ⇒ Object (readonly)
Returns the value of attribute ret.
768 769 770 |
# File 'lib/typeprof/core/graph/box.rb', line 768 def ret @ret end |
#singleton ⇒ Object (readonly)
Returns the value of attribute singleton.
768 769 770 |
# File 'lib/typeprof/core/graph/box.rb', line 768 def singleton @singleton end |
Instance Method Details
#call(changes, genv, a_args, ret) ⇒ Object
976 977 978 979 980 981 982 983 984 985 986 |
# File 'lib/typeprof/core/graph/box.rb', line 976 def call(changes, genv, a_args, ret) a_args = normalize_keyword_hash_argument_for_def(a_args) 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
770 771 772 773 774 775 776 |
# File 'lib/typeprof/core/graph/box.rb', line 770 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 |
#normalize_keyword_hash_argument_for_def(a_args) ⇒ Object
967 968 969 970 971 972 973 974 |
# File 'lib/typeprof/core/graph/box.rb', line 967 def normalize_keyword_hash_argument_for_def(a_args) return a_args unless a_args.keywords return a_args if @node.no_keywords return a_args if @node.rest_keywords return a_args unless @node.req_keywords.empty? && @node.opt_keywords.empty? a_args.with_keywords_as_last_positional_hash end |
#pass_arguments(changes, genv, a_args) ⇒ Object
840 841 842 843 844 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 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 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 |
# File 'lib/typeprof/core/graph/box.rb', line 840 def pass_arguments(changes, genv, a_args) if a_args.splat_flags.any? # there is at least one splat actual argument lower = @f_args.req_positionals.size + @f_args.post_positionals.size upper = @f_args.rest_positionals ? nil : lower + @f_args.opt_positionals.size if upper && upper < a_args.positionals.size meth = changes.node.mid_code_range ? :mid_code_range : :code_range err = "#{ a_args.positionals.size } for #{ lower }#{ upper ? lower < upper ? "...#{ upper }" : "" : "+" }" changes.add_diagnostic(meth, "wrong number of arguments (#{ err })") return false end start_rest = [a_args.splat_flags.index(true), @f_args.req_positionals.size + @f_args.opt_positionals.size].min end_rest = [a_args.splat_flags.rindex(true) + 1, a_args.positionals.size - @f_args.post_positionals.size].max rest_vtxs = a_args.get_rest_args(genv, changes, start_rest, end_rest) @f_args.req_positionals.each_with_index do |f_vtx, i| if i < start_rest changes.add_edge(genv, a_args.positionals[i], f_vtx) else rest_vtxs.each do |vtx| changes.add_edge(genv, vtx, f_vtx) end end end @f_args.opt_positionals.each_with_index do |f_vtx, i| i += @f_args.req_positionals.size if i < start_rest changes.add_edge(genv, a_args.positionals[i], f_vtx) else rest_vtxs.each do |vtx| changes.add_edge(genv, vtx, f_vtx) end end end @f_args.post_positionals.each_with_index do |f_vtx, i| i += a_args.positionals.size - @f_args.post_positionals.size if end_rest <= i changes.add_edge(genv, a_args.positionals[i], f_vtx) else rest_vtxs.each do |vtx| changes.add_edge(genv, vtx, f_vtx) end end end if @f_args.rest_positionals rest_vtxs.each do |vtx| @f_args.rest_positionals.each_type do |ty| if ty.is_a?(Type::Instance) && ty.mod == genv.mod_ary && ty.args[0] changes.add_edge(genv, vtx, ty.args[0]) end end end end else # there is no splat actual argument lower = @f_args.req_positionals.size + @f_args.post_positionals.size upper = @f_args.rest_positionals ? nil : lower + @f_args.opt_positionals.size if a_args.positionals.size < lower || (upper && upper < a_args.positionals.size) meth = changes.node.mid_code_range ? :mid_code_range : :code_range err = "#{ a_args.positionals.size } for #{ lower }#{ upper ? lower < upper ? "...#{ upper }" : "" : "+" }" changes.add_diagnostic(meth, "wrong number of arguments (#{ err })") return false end @f_args.req_positionals.each_with_index do |f_vtx, i| changes.add_edge(genv, a_args.positionals[i], f_vtx) end @f_args.post_positionals.each_with_index do |f_vtx, i| i -= @f_args.post_positionals.size changes.add_edge(genv, a_args.positionals[i], f_vtx) end start_rest = @f_args.req_positionals.size end_rest = a_args.positionals.size - @f_args.post_positionals.size i = 0 while i < @f_args.opt_positionals.size && start_rest < end_rest f_arg = @f_args.opt_positionals[i] changes.add_edge(genv, a_args.positionals[start_rest], f_arg) i += 1 start_rest += 1 end if start_rest < end_rest if @f_args.rest_positionals (start_rest..end_rest-1).each do |i| @f_args.rest_positionals.each_type do |ty| if ty.is_a?(Type::Instance) && ty.mod == genv.mod_ary && ty.args[0] changes.add_edge(genv, a_args.positionals[i], ty.args[0]) end end end end end end if a_args.keywords # TODO: support diagnostics @node.req_keywords.zip(@f_args.req_keywords) do |name, f_vtx| changes.add_edge(genv, a_args.get_keyword_arg(genv, changes, name), f_vtx) end @node.opt_keywords.zip(@f_args.opt_keywords).each do |name, f_vtx| changes.add_edge(genv, a_args.get_keyword_arg(genv, changes, name), f_vtx) end if @node.rest_keywords named_keys = @node.req_keywords + @node.opt_keywords a_args.keywords.each_type do |kw_ty| case kw_ty when Type::Record rest_fields = kw_ty.fields.reject {|key, _| named_keys.include?(key) } base = kw_ty.base_type(genv) rest_record = Type::Record.new(genv, rest_fields, base) changes.add_edge(genv, Source.new(rest_record), @f_args.rest_keywords) when Type::Hash, Type::Instance changes.add_edge(genv, Source.new(kw_ty), @f_args.rest_keywords) end end end end return true end |
#run0(genv, changes) ⇒ Object
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 828 829 830 831 832 833 834 835 836 837 838 |
# File 'lib/typeprof/core/graph/box.rb', line 778 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)| Source.new() } # 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) ⇒ Object
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 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 |
# File 'lib/typeprof/core/graph/box.rb', line 988 def show(output_parameter_names) 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 << "-> #{ @mid == :initialize ? "void" : @ret.show }" s.join(" ") end |