Class: Steep::Interface::Builder
- Defined in:
- lib/steep/interface/builder.rb
Defined Under Namespace
Classes: Config
Instance Attribute Summary collapse
-
#factory ⇒ Object
readonly
Returns the value of attribute factory.
-
#implicitly_returns_nil ⇒ Object
readonly
Returns the value of attribute implicitly_returns_nil.
-
#object_shape_cache ⇒ Object
readonly
Returns the value of attribute object_shape_cache.
-
#singleton_shape_cache ⇒ Object
readonly
Returns the value of attribute singleton_shape_cache.
-
#union_shape_cache ⇒ Object
readonly
Returns the value of attribute union_shape_cache.
Instance Method Summary collapse
- #add_implicitly_returns_nil(annotations, method_type) ⇒ Object
- #app_subst(type) ⇒ Object
- #class_subst(type) ⇒ Object
- #fetch_cache(cache, key) ⇒ Object
-
#initialize(factory, implicitly_returns_nil:) ⇒ Builder
constructor
A new instance of Builder.
- #interface_subst(type) ⇒ Object
- #intersection_shape(type, shapes) ⇒ Object
- #method_name_for(type_def, name) ⇒ Object
- #object_shape(type_name) ⇒ Object
- #proc_shape(proc, proc_shape) ⇒ Object
- #raw_shape(type, config) ⇒ Object
- #record_shape(record) ⇒ Object
- #replace_kernel_class(method_name, method_def, method_type) ⇒ Object
- #replace_primitive_method(method_name, method_def, method_type) ⇒ Object
- #self_shape(type, config) ⇒ Object
- #shape(type, config) ⇒ Object
- #singleton_shape(type_name) ⇒ Object
- #subtyping ⇒ Object
- #tuple_shape(tuple) ⇒ Object
- #union_shape(shape_type, shapes) ⇒ Object
Constructor Details
#initialize(factory, implicitly_returns_nil:) ⇒ Builder
Returns a new instance of Builder.
59 60 61 62 63 64 65 |
# File 'lib/steep/interface/builder.rb', line 59 def initialize(factory, implicitly_returns_nil:) @factory = factory @object_shape_cache = {} @union_shape_cache = {} @singleton_shape_cache = {} @implicitly_returns_nil = implicitly_returns_nil end |
Instance Attribute Details
#factory ⇒ Object (readonly)
Returns the value of attribute factory.
57 58 59 |
# File 'lib/steep/interface/builder.rb', line 57 def factory @factory end |
#implicitly_returns_nil ⇒ Object (readonly)
Returns the value of attribute implicitly_returns_nil.
57 58 59 |
# File 'lib/steep/interface/builder.rb', line 57 def implicitly_returns_nil @implicitly_returns_nil end |
#object_shape_cache ⇒ Object (readonly)
Returns the value of attribute object_shape_cache.
57 58 59 |
# File 'lib/steep/interface/builder.rb', line 57 def object_shape_cache @object_shape_cache end |
#singleton_shape_cache ⇒ Object (readonly)
Returns the value of attribute singleton_shape_cache.
57 58 59 |
# File 'lib/steep/interface/builder.rb', line 57 def singleton_shape_cache @singleton_shape_cache end |
#union_shape_cache ⇒ Object (readonly)
Returns the value of attribute union_shape_cache.
57 58 59 |
# File 'lib/steep/interface/builder.rb', line 57 def union_shape_cache @union_shape_cache end |
Instance Method Details
#add_implicitly_returns_nil(annotations, method_type) ⇒ Object
862 863 864 865 866 867 868 869 870 871 872 873 |
# File 'lib/steep/interface/builder.rb', line 862 def add_implicitly_returns_nil(annotations, method_type) return method_type unless implicitly_returns_nil if annotations.find { _1.string == "implicitly-returns-nil" } return_type = method_type.type.return_type method_type = method_type.with( type: method_type.type.with(return_type: AST::Types::Union.build(types: [return_type, AST::Builtin.nil_type])) ) else method_type end end |
#app_subst(type) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/steep/interface/builder.rb', line 234 def app_subst(type) if type.args.empty? return Substitution.empty end vars = case type when AST::Types::Name::Instance entry = factory.env.module_class_entry(type.name, normalized: true) or raise entry.primary_decl.type_params.map { _1.name } when AST::Types::Name::Interface entry = factory.env.interface_decls.fetch(type.name) entry.decl.type_params.map { _1.name } when AST::Types::Name::Alias entry = factory.env.type_alias_decls.fetch(type.name) entry.decl.type_params.map { _1.name } end Substitution.build(vars, type.args) end |
#class_subst(type) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/steep/interface/builder.rb', line 255 def class_subst(type) case type when AST::Types::Name::Singleton self_type = type singleton_type = type instance_type = factory.instance_type(type.name) when AST::Types::Name::Instance self_type = type singleton_type = type.to_module instance_type = factory.instance_type(type.name) end Substitution.build([], self_type: self_type, module_type: singleton_type, instance_type: instance_type) end |
#fetch_cache(cache, key) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/steep/interface/builder.rb', line 84 def fetch_cache(cache, key) if cache.key?(key) # steep:ignore ArgumentTypeMismatch return cache.fetch(key) end cache[key] = yield end |
#interface_subst(type) ⇒ Object
270 271 272 |
# File 'lib/steep/interface/builder.rb', line 270 def interface_subst(type) Substitution.build([], self_type: type) end |
#intersection_shape(type, shapes) ⇒ Object
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/steep/interface/builder.rb', line 392 def intersection_shape(type, shapes) shape = Interface::Shape.new(type: type, private: true) shapes.each do |s| shape.methods.merge!(s.methods) do |name, old_entry, new_entry| if old_entry.public_method? && new_entry.private_method? old_entry else new_entry end end end shape end |
#method_name_for(type_def, name) ⇒ Object
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 |
# File 'lib/steep/interface/builder.rb', line 408 def method_name_for(type_def, name) type_name = type_def.implemented_in || type_def.defined_in if name == :new case type_def.member when RBS::AST::Members::MethodDefinition if type_def.member.name == :initialize return SingletonMethodName.new(type_name: type_name, method_name: name) end when RBS::AST::Ruby::Members::DefMember if type_def.member.name == :initialize return SingletonMethodName.new(type_name: type_name, method_name: name) end end end case type_def.member when RBS::AST::Members::Base case type_def.member.kind when :instance InstanceMethodName.new(type_name: type_name, method_name: name) when :singleton SingletonMethodName.new(type_name: type_name, method_name: name) when :singleton_instance # Assume it a instance method, because `module_function` methods are typically defined with `def` InstanceMethodName.new(type_name: type_name, method_name: name) else raise end when RBS::AST::Ruby::Members::DefMember, RBS::AST::Ruby::Members::AttributeMember InstanceMethodName.new(type_name: type_name, method_name: name) end end |
#object_shape(type_name) ⇒ Object
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 |
# File 'lib/steep/interface/builder.rb', line 298 def object_shape(type_name) object_shape_cache[type_name] ||= begin shape = Interface::Shape.new(type: AST::Builtin.bottom_type, private: true) case when type_name.class? definition = factory.definition_builder.build_instance(type_name) when type_name.interface? definition = factory.definition_builder.build_interface(type_name) end definition or raise definition.methods.each do |name, method| Steep.logger.tagged "method = #{type_name}##{name}" do overloads = method.defs.map do |type_def| method_name = method_name_for(type_def, name) method_type = factory.method_type(type_def.type) method_type = replace_primitive_method(method_name, type_def, method_type) if type_name.class? method_type = replace_kernel_class(method_name, type_def, method_type) { AST::Types::Name::Singleton.new(name: type_name) } end method_type = add_implicitly_returns_nil(type_def.each_annotation, method_type) Shape::MethodOverload.new(method_type, [type_def]) end shape.methods[name] = Interface::Shape::Entry.new(method_name: name, private_method: method.private?, overloads: overloads) end end shape end end |
#proc_shape(proc, proc_shape) ⇒ Object
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 |
# File 'lib/steep/interface/builder.rb', line 719 def proc_shape(proc, proc_shape) shape = Shape.new(type: proc, private: true) shape.methods.merge!(proc_shape.methods) overload = Shape::MethodOverload.new( MethodType.new(type_params: [], type: proc.type, block: proc.block), [] ) shape.methods[:[]] = Shape::Entry.new( method_name: :[], private_method: false, overloads: [overload] ) shape.methods[:call] = Shape::Entry.new( method_name: :call, private_method: false, overloads: [overload] ) shape end |
#raw_shape(type, config) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/steep/interface/builder.rb', line 92 def raw_shape(type, config) case type when AST::Types::Self config.validate_self_type self_type = config.self_type or raise self_shape(self_type, config) when AST::Types::Instance config.validate_instance_type instance_type = config.instance_type or raise raw_shape(instance_type, config) when AST::Types::Class config.validate_class_type klass_type = config.class_type or raise raw_shape(klass_type, config) when AST::Types::Name::Singleton singleton_shape(type.name).subst(class_subst(type)) when AST::Types::Name::Instance object_shape(type.name).subst(class_subst(type).merge(app_subst(type)), type: type) when AST::Types::Name::Interface object_shape(type.name).subst(interface_subst(type).merge(app_subst(type)), type: type) when AST::Types::Union groups = type.types.group_by do |type| if type.is_a?(AST::Types::Literal) type.back_type else nil end end shapes = [] #: Array[Shape] groups.each do |name, types| if name union = AST::Types::Union.build(types: types) subst = class_subst(name).update(self_type: union) shapes << object_shape(name.name).subst(subst, type: union) else shapes.concat(types.map {|ty| raw_shape(ty, config) or return }) end end fetch_cache(union_shape_cache, type) do union_shape(type, shapes) end when AST::Types::Intersection shapes = type.types.map do |type| raw_shape(type, config) or return end intersection_shape(type, shapes) when AST::Types::Name::Alias = factory.(type) if shape = raw_shape(, config) shape.update(type: type) end when AST::Types::Literal instance_type = type.back_type subst = class_subst(instance_type).update(self_type: type) object_shape(instance_type.name).subst(subst, type: type) when AST::Types::Boolean true_shape = (object_shape(RBS::BuiltinNames::TrueClass.name)). subst(class_subst(AST::Builtin::TrueClass.instance_type).update(self_type: type)) false_shape = (object_shape(RBS::BuiltinNames::FalseClass.name)). subst(class_subst(AST::Builtin::FalseClass.instance_type).update(self_type: type)) union_shape(type, [true_shape, false_shape]) when AST::Types::Proc shape = object_shape(AST::Builtin::Proc.module_name).subst(class_subst(AST::Builtin::Proc.instance_type).update(self_type: type)) proc_shape(type, shape) when AST::Types::Tuple tuple_shape(type) do |array| object_shape(array.name).subst( class_subst(array).update(self_type: type).merge(app_subst(array)) ) end when AST::Types::Record record_shape(type) do |hash| object_shape(hash.name).subst( class_subst(hash).update(self_type: type).merge(app_subst(hash)) ) end when AST::Types::Var if bound = config.upper_bound(type.name) new_config = Config.new(self_type: bound, variable_bounds: config.variable_bounds) sub = Substitution.build([], self_type: type) # We have to use `self_shape` instead of `raw_shape` here. # Keep the `self` types included in the `bound`'s shape, and replace it to the type variable. self_shape(bound, new_config)&.subst(sub, type: type) end when AST::Types::Nil subst = class_subst(AST::Builtin::NilClass.instance_type).update(self_type: type) object_shape(AST::Builtin::NilClass.module_name).subst(subst, type: type) when AST::Types::Logic::Base true_shape = (object_shape(RBS::BuiltinNames::TrueClass.name)). subst(class_subst(AST::Builtin::TrueClass.instance_type).update(self_type: type)) false_shape = (object_shape(RBS::BuiltinNames::FalseClass.name)). subst(class_subst(AST::Builtin::FalseClass.instance_type).update(self_type: type)) union_shape(type, [true_shape, false_shape]) else nil end end |
#record_shape(record) ⇒ Object
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 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 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 |
# File 'lib/steep/interface/builder.rb', line 604 def record_shape(record) all_key_type = AST::Types::Union.build( types: record.elements.each_key.map {|value| AST::Types::Literal.new(value: value).back_type } ) all_value_type = AST::Types::Union.build(types: record.elements.values) hash_type = AST::Builtin::Hash.instance_type(all_key_type, all_value_type) hash_shape = yield(hash_type) or raise shape = Shape.new(type: record, private: true) shape.methods.merge!(hash_shape.methods) shape.methods[:[]] = hash_shape.methods[:[]].yield_self do |aref| aref or raise Shape::Entry.new( method_name: :[], private_method: false, overloads: record.elements.map do |key_value, value_type| key_type = AST::Types::Literal.new(value: key_value) if record.optional?(key_value) value_type = AST::Builtin.optional(value_type) end Shape::MethodOverload.new( MethodType.new( type_params: [], type: Function.new( params: Function::Params.build(required: [key_type]), return_type: value_type, location: nil ), block: nil ), [] ) end + aref.overloads ) end shape.methods[:[]=] = hash_shape.methods[:[]=].yield_self do |update| update or raise Shape::Entry.new( method_name: :[]=, private_method: false, overloads: record.elements.map do |key_value, value_type| key_type = AST::Types::Literal.new(value: key_value) Shape::MethodOverload.new( MethodType.new( type_params: [], type: Function.new( params: Function::Params.build(required: [key_type, value_type]), return_type: value_type, location: nil), block: nil ), [] ) end + update.overloads ) end shape.methods[:fetch] = hash_shape.methods[:fetch].yield_self do |update| update or raise Shape::Entry.new( method_name: :fetch, private_method: false, overloads: record.elements.flat_map {|key_value, value_type| key_type = AST::Types::Literal.new(value: key_value) [ MethodType.new( type_params: [], type: Function.new( params: Function::Params.build(required: [key_type]), return_type: value_type, location: nil ), block: nil ), MethodType.new( type_params: [TypeParam.new(name: :T, upper_bound: nil, variance: :invariant, unchecked: false, default_type: nil)], type: Function.new( params: Function::Params.build(required: [key_type, AST::Types::Var.new(name: :T)]), return_type: AST::Types::Union.build(types: [value_type, AST::Types::Var.new(name: :T)]), location: nil ), block: nil ), MethodType.new( type_params: [TypeParam.new(name: :T, upper_bound: nil, variance: :invariant, unchecked: false, default_type: nil)], type: Function.new( params: Function::Params.build(required: [key_type]), return_type: AST::Types::Union.build(types: [value_type, AST::Types::Var.new(name: :T)]), location: nil ), block: Block.new( type: Function.new( params: Function::Params.build(required: [all_key_type]), return_type: AST::Types::Var.new(name: :T), location: nil ), optional: false, self_type: nil ) ) ].map { Shape::MethodOverload.new(_1, []) } } + update.overloads ) end shape end |
#replace_kernel_class(method_name, method_def, method_type) ⇒ Object
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 |
# File 'lib/steep/interface/builder.rb', line 845 def replace_kernel_class(method_name, method_def, method_type) defined_in = method_def.defined_in member = method_def.member if member.is_a?(RBS::AST::Members::MethodDefinition) case method_name.method_name when :class case defined_in when AST::Builtin::Kernel.module_name return method_type.with(type: method_type.type.with(return_type: yield)) end end end method_type end |
#replace_primitive_method(method_name, method_def, method_type) ⇒ Object
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 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 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 |
# File 'lib/steep/interface/builder.rb', line 742 def replace_primitive_method(method_name, method_def, method_type) defined_in = method_def.defined_in member = method_def.member if member.is_a?(RBS::AST::Members::MethodDefinition) case method_name.method_name when :is_a?, :kind_of?, :instance_of? case when RBS::BuiltinNames::Object.name, RBS::BuiltinNames::Kernel.name if member.instance? return method_type.with( type: method_type.type.with( return_type: AST::Types::Logic::ReceiverIsArg.instance() ) ) end end when :nil? case defined_in when RBS::BuiltinNames::Object.name, AST::Builtin::NilClass.module_name, RBS::BuiltinNames::Kernel.name if member.instance? return method_type.with( type: method_type.type.with( return_type: AST::Types::Logic::ReceiverIsNil.instance() ) ) end end when :! case defined_in when RBS::BuiltinNames::BasicObject.name, RBS::BuiltinNames::TrueClass.name, RBS::BuiltinNames::FalseClass.name, AST::Builtin::NilClass.module_name return method_type.with( type: method_type.type.with( return_type: AST::Types::Logic::Not.instance() ) ) end when :=== case defined_in when RBS::BuiltinNames::Module.name return method_type.with( type: method_type.type.with( return_type: AST::Types::Logic::ArgIsReceiver.instance() ) ) when RBS::BuiltinNames::BasicObject.name, RBS::BuiltinNames::Object.name, RBS::BuiltinNames::Kernel.name, RBS::BuiltinNames::String.name, RBS::BuiltinNames::Integer.name, RBS::BuiltinNames::Symbol.name, RBS::BuiltinNames::TrueClass.name, RBS::BuiltinNames::FalseClass.name, RBS::TypeName.parse("::NilClass") # Value based type-case works on literal types which is available for String, Integer, Symbol, TrueClass, FalseClass, and NilClass return method_type.with( type: method_type.type.with( return_type: AST::Types::Logic::ArgEqualsReceiver.instance() ) ) end when :== case defined_in when RBS::BuiltinNames::BasicObject.name, RBS::BuiltinNames::Object.name, RBS::BuiltinNames::Kernel.name, RBS::BuiltinNames::String.name, RBS::BuiltinNames::Integer.name, RBS::BuiltinNames::Symbol.name, RBS::BuiltinNames::TrueClass.name, RBS::BuiltinNames::FalseClass.name, RBS::TypeName.parse("::NilClass") # For ==, we use ReceiverIsArg to narrow the receiver based on the argument return method_type.with( type: method_type.type.with( return_type: AST::Types::Logic::ReceiverIsArg.instance() ) ) end when :<, :<= case defined_in when RBS::BuiltinNames::Module.name return method_type.with( type: method_type.type.with( return_type: AST::Types::Logic::ArgIsAncestor.instance() ) ) end end end method_type end |
#self_shape(type, config) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/steep/interface/builder.rb', line 196 def self_shape(type, config) case type when AST::Types::Self, AST::Types::Instance, AST::Types::Class raise when AST::Types::Name::Singleton singleton_shape(type.name).subst(class_subst(type).update(self_type: nil)) when AST::Types::Name::Instance object_shape(type.name) .subst( class_subst(type).update(self_type: nil).merge(app_subst(type)), type: type ) when AST::Types::Name::Interface object_shape(type.name).subst(app_subst(type), type: type) when AST::Types::Literal instance_type = type.back_type subst = class_subst(instance_type).update(self_type: nil) object_shape(instance_type.name).subst(subst, type: type) when AST::Types::Boolean true_shape = (object_shape(RBS::BuiltinNames::TrueClass.name)). subst(class_subst(AST::Builtin::TrueClass.instance_type).update(self_type: nil)) false_shape = (object_shape(RBS::BuiltinNames::FalseClass.name)). subst(class_subst(AST::Builtin::FalseClass.instance_type).update(self_type: nil)) union_shape(type, [true_shape, false_shape]) when AST::Types::Proc shape = object_shape(AST::Builtin::Proc.module_name).subst(class_subst(AST::Builtin::Proc.instance_type).update(self_type: nil)) proc_shape(type, shape) when AST::Types::Var if bound = config.upper_bound(type.name) self_shape(bound, config)&.update(type: type) end else raw_shape(type, config) end end |
#shape(type, config) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/steep/interface/builder.rb', line 67 def shape(type, config) Steep.logger.tagged "shape(#{type})" do if shape = raw_shape(type, config) # Optimization that skips unnecessary substitution if type.free_variables.include?(AST::Types::Self.instance) shape else if s = config.subst shape.subst(s) else shape end end end end end |
#singleton_shape(type_name) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/steep/interface/builder.rb', line 274 def singleton_shape(type_name) singleton_shape_cache[type_name] ||= begin shape = Interface::Shape.new(type: AST::Types::Name::Singleton.new(name: type_name), private: true) definition = factory.definition_builder.build_singleton(type_name) definition.methods.each do |name, method| Steep.logger.tagged "method = #{type_name}.#{name}" do overloads = method.defs.map do |type_def| method_name = method_name_for(type_def, name) method_type = factory.method_type(type_def.type) method_type = replace_primitive_method(method_name, type_def, method_type) method_type = replace_kernel_class(method_name, type_def, method_type) { AST::Builtin::Class.instance_type } method_type = add_implicitly_returns_nil(type_def.each_annotation, method_type) Shape::MethodOverload.new(method_type, [type_def]) end shape.methods[name] = Interface::Shape::Entry.new(method_name: name, private_method: method.private?, overloads: overloads) end end shape end end |
#subtyping ⇒ Object
442 443 444 |
# File 'lib/steep/interface/builder.rb', line 442 def subtyping @subtyping ||= Subtyping::Check.new(builder: self) end |
#tuple_shape(tuple) ⇒ Object
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 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 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 |
# File 'lib/steep/interface/builder.rb', line 446 def tuple_shape(tuple) element_type = AST::Types::Union.build(types: tuple.types) array_type = AST::Builtin::Array.instance_type(element_type) array_shape = yield(array_type) or raise shape = Shape.new(type: tuple, private: true) shape.methods.merge!(array_shape.methods) aref_entry = array_shape.methods[:[]].yield_self do |aref| raise unless aref Shape::Entry.new( method_name: :[], private_method: false, overloads: tuple.types.map.with_index {|elem_type, index| Shape::MethodOverload.new( MethodType.new( type_params: [], type: Function.new( params: Function::Params.build(required: [AST::Types::Literal.new(value: index)]), return_type: elem_type, location: nil ), block: nil ), [] ) } + aref.overloads ) end aref_update_entry = array_shape.methods[:[]=].yield_self do |update| raise unless update Shape::Entry.new( method_name: :[]=, private_method: false, overloads: tuple.types.map.with_index {|elem_type, index| Shape::MethodOverload.new( MethodType.new( type_params: [], type: Function.new( params: Function::Params.build(required: [AST::Types::Literal.new(value: index), elem_type]), return_type: elem_type, location: nil ), block: nil ), [] ) } + update.overloads ) end fetch_entry = array_shape.methods[:fetch].yield_self do |fetch| raise unless fetch Shape::Entry.new( method_name: :fetch, private_method: false, overloads: tuple.types.flat_map.with_index {|elem_type, index| [ MethodType.new( type_params: [], type: Function.new( params: Function::Params.build(required: [AST::Types::Literal.new(value: index)]), return_type: elem_type, location: nil ), block: nil ), MethodType.new( type_params: [TypeParam.new(name: :T, upper_bound: nil, variance: :invariant, unchecked: false, default_type: nil)], type: Function.new( params: Function::Params.build( required: [ AST::Types::Literal.new(value: index), AST::Types::Var.new(name: :T) ] ), return_type: AST::Types::Union.build(types: [elem_type, AST::Types::Var.new(name: :T)]), location: nil ), block: nil ), MethodType.new( type_params: [TypeParam.new(name: :T, upper_bound: nil, variance: :invariant, unchecked: false, default_type: nil)], type: Function.new( params: Function::Params.build(required: [AST::Types::Literal.new(value: index)]), return_type: AST::Types::Union.build(types: [elem_type, AST::Types::Var.new(name: :T)]), location: nil ), block: Block.new( type: Function.new( params: Function::Params.build(required: [AST::Builtin::Integer.instance_type]), return_type: AST::Types::Var.new(name: :T), location: nil ), optional: false, self_type: nil ) ) ].map { Shape::MethodOverload.new(_1, []) } } + fetch.overloads ) end first_entry = array_shape.methods[:first].yield_self do |first| Shape::Entry.new( method_name: :first, private_method: false, overloads: [ Shape::MethodOverload.new( MethodType.new( type_params: [], type: Function.new( params: Function::Params.empty, return_type: tuple.types[0] || AST::Builtin.nil_type, location: nil ), block: nil ), [] ) ] ) end last_entry = array_shape.methods[:last].yield_self do |last| Shape::Entry.new( method_name: :last, private_method: false, overloads: [ Shape::MethodOverload.new( MethodType.new( type_params: [], type: Function.new( params: Function::Params.empty, return_type: tuple.types.last || AST::Builtin.nil_type, location: nil ), block: nil ), [] ) ] ) end shape.methods[:[]] = aref_entry shape.methods[:[]=] = aref_update_entry shape.methods[:fetch] = fetch_entry shape.methods[:first] = first_entry shape.methods[:last] = last_entry shape end |
#union_shape(shape_type, shapes) ⇒ Object
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 |
# File 'lib/steep/interface/builder.rb', line 332 def union_shape(shape_type, shapes) s0, *sx = shapes s0 or raise all_common_methods = Set.new(s0.methods.each_name) sx.each do |shape| all_common_methods &= shape.methods.each_name end shape = Interface::Shape.new(type: shape_type, private: true) all_common_methods.each do |method_name| overloadss = [] #: Array[Array[Shape::MethodOverload]] private_method = false shapes.each do |shape| entry = shape.methods[method_name] || raise overloadss << entry.overloads private_method ||= entry.private_method? end shape.methods[method_name] = Interface::Shape::Entry.new(method_name: method_name, private_method: private_method) do overloadss.inject do |overloads1, overloads2| # @type break: nil types1 = overloads1.map(&:method_type) types2 = overloads2.map(&:method_type) if types1 == types2 defs1 = overloads1.flat_map(&:method_defs) defs2 = overloads2.flat_map(&:method_defs) if defs1 == defs2 next overloads1 end end method_overloads = {} #: Hash[Shape::MethodOverload, bool] overloads1.each do |overload1| overloads2.each do |overload2| if overload1.method_type == overload2.method_type overload = Shape::MethodOverload.new(overload1.method_type, overload1.method_defs + overload2.method_defs) method_overloads[overload] = true else if type = MethodType.union(overload1.method_type, overload2.method_type, subtyping) overload = Shape::MethodOverload.new(type, overload1.method_defs + overload2.method_defs) method_overloads[overload] = true end end end end break nil if method_overloads.empty? method_overloads.keys end end end shape end |