class TypeProf::Core::AST::SigFuncType::SigTyAliasNode::SigTyUnionNode::SigTyIntersectionNode::SigTySingletonNode::SigTyInstanceNode
Attributes
Public Class Methods
Source
# File bundled-src/typeprof-0.31.1/lib/typeprof/core/ast/sig_type.rb, line 609 def initialize(raw_decl, lenv) super(raw_decl, lenv) name = raw_decl.name @cpath = name.namespace.path + [name.name] @toplevel = name.namespace.absolute? # "::Foo" or "Foo" @args = raw_decl.args.map {|arg| AST.create_rbs_type(arg, lenv) } end
Calls superclass method
TypeProf::Core::AST::Node::new
Public Instance Methods
Source
# File bundled-src/typeprof-0.31.1/lib/typeprof/core/ast/sig_type.rb, line 619 def attrs = { cpath:, toplevel: } def define0(genv) @args.each {|arg| arg.define(genv) } const_reads = [] const_read = BaseConstRead.new(genv, @cpath.first, @toplevel ? CRef::Toplevel : @lenv.cref, false) const_reads << const_read unless @cpath.empty? @cpath[1..].each do |cname| const_read = ScopedConstRead.new(cname, const_read, false) const_reads << const_read end end const_reads end def undefine0(genv) @static_ret.each do |const_read| const_read.destroy(genv) end @args.each {|arg| arg.undefine(genv) } end def covariant_vertex0(genv, changes, vtx, subst) changes.add_depended_static_read(@static_ret.last) cpath = @static_ret.last.cpath return unless cpath mod = genv.resolve_cpath(cpath) args = @args.map {|arg| arg.covariant_vertex(genv, changes, subst) } changes.add_edge(genv, Source.new(Type::Instance.new(genv, mod, args)), vtx) end def contravariant_vertex0(genv, changes, vtx, subst) changes.add_depended_static_read(@static_ret.last) cpath = @static_ret.last.cpath return unless cpath mod = genv.resolve_cpath(cpath) # TODO: report error for wrong type arguments # TODO: support default type args args = mod.type_params.zip(@args).map do |_, arg| arg ? arg.contravariant_vertex(genv, changes, subst) : Source.new end changes.add_edge(genv, Source.new(Type::Instance.new(genv, mod, args)), vtx) end def typecheck(genv, changes, vtx, subst) changes.add_depended_static_read(@static_ret.last) cpath = @static_ret.last.cpath return unless cpath f_mod = genv.resolve_cpath(cpath) AST.typecheck_for_module(genv, changes, f_mod, @args, vtx, subst) end def show cpath = @static_ret.last.cpath if cpath s = "#{ cpath.join("::") }" if !@args.empty? s << "[...]" end s else "(unknown instance)" end end end
Source
# File bundled-src/typeprof-0.31.1/lib/typeprof/core/ast/sig_type.rb, line 651 def contravariant_vertex0(genv, changes, vtx, subst) changes.add_depended_static_read(@static_ret.last) cpath = @static_ret.last.cpath return unless cpath mod = genv.resolve_cpath(cpath) # TODO: report error for wrong type arguments # TODO: support default type args args = mod.type_params.zip(@args).map do |_, arg| arg ? arg.contravariant_vertex(genv, changes, subst) : Source.new end changes.add_edge(genv, Source.new(Type::Instance.new(genv, mod, args)), vtx) end
Source
# File bundled-src/typeprof-0.31.1/lib/typeprof/core/ast/sig_type.rb, line 642 def covariant_vertex0(genv, changes, vtx, subst) changes.add_depended_static_read(@static_ret.last) cpath = @static_ret.last.cpath return unless cpath mod = genv.resolve_cpath(cpath) args = @args.map {|arg| arg.covariant_vertex(genv, changes, subst) } changes.add_edge(genv, Source.new(Type::Instance.new(genv, mod, args)), vtx) end
Source
# File bundled-src/typeprof-0.31.1/lib/typeprof/core/ast/sig_type.rb, line 621 def define0(genv) @args.each {|arg| arg.define(genv) } const_reads = [] const_read = BaseConstRead.new(genv, @cpath.first, @toplevel ? CRef::Toplevel : @lenv.cref, false) const_reads << const_read unless @cpath.empty? @cpath[1..].each do |cname| const_read = ScopedConstRead.new(cname, const_read, false) const_reads << const_read end end const_reads end
Source
# File bundled-src/typeprof-0.31.1/lib/typeprof/core/ast/sig_type.rb, line 672 def show cpath = @static_ret.last.cpath if cpath s = "#{ cpath.join("::") }" if !@args.empty? s << "[...]" end s else "(unknown instance)" end end
Source
# File bundled-src/typeprof-0.31.1/lib/typeprof/core/ast/sig_type.rb, line 618 def subnodes = { args: } def attrs = { cpath:, toplevel: } def define0(genv) @args.each {|arg| arg.define(genv) } const_reads = [] const_read = BaseConstRead.new(genv, @cpath.first, @toplevel ? CRef::Toplevel : @lenv.cref, false) const_reads << const_read unless @cpath.empty? @cpath[1..].each do |cname| const_read = ScopedConstRead.new(cname, const_read, false) const_reads << const_read end end const_reads end def undefine0(genv) @static_ret.each do |const_read| const_read.destroy(genv) end @args.each {|arg| arg.undefine(genv) } end def covariant_vertex0(genv, changes, vtx, subst) changes.add_depended_static_read(@static_ret.last) cpath = @static_ret.last.cpath return unless cpath mod = genv.resolve_cpath(cpath) args = @args.map {|arg| arg.covariant_vertex(genv, changes, subst) } changes.add_edge(genv, Source.new(Type::Instance.new(genv, mod, args)), vtx) end def contravariant_vertex0(genv, changes, vtx, subst) changes.add_depended_static_read(@static_ret.last) cpath = @static_ret.last.cpath return unless cpath mod = genv.resolve_cpath(cpath) # TODO: report error for wrong type arguments # TODO: support default type args args = mod.type_params.zip(@args).map do |_, arg| arg ? arg.contravariant_vertex(genv, changes, subst) : Source.new end changes.add_edge(genv, Source.new(Type::Instance.new(genv, mod, args)), vtx) end def typecheck(genv, changes, vtx, subst) changes.add_depended_static_read(@static_ret.last) cpath = @static_ret.last.cpath return unless cpath f_mod = genv.resolve_cpath(cpath) AST.typecheck_for_module(genv, changes, f_mod, @args, vtx, subst) end def show cpath = @static_ret.last.cpath if cpath s = "#{ cpath.join("::") }" if !@args.empty? s << "[...]" end s else "(unknown instance)" end end end class SigTyTupleNode < SigTyNode def initialize(raw_decl, lenv) super(raw_decl, lenv) @types = raw_decl.types.map {|type| AST.create_rbs_type(type, lenv) } end attr_reader :types def subnodes = { types: } def covariant_vertex0(genv, changes, vtx, subst) unified_elem = changes.new_covariant_vertex(genv, [self, :Elem]) # TODO elems = @types.map do |type| nvtx = type.covariant_vertex(genv, changes, subst) changes.add_edge(genv, nvtx, unified_elem) nvtx end changes.add_edge(genv, Source.new(Type::Array.new(genv, elems, genv.gen_ary_type(unified_elem))), vtx) end def contravariant_vertex0(genv, changes, vtx, subst) unified_elem = changes.new_contravariant_vertex(genv, [self, :Elem]) # TODO elems = @types.map do |type| nvtx = type.contravariant_vertex(genv, changes, subst) changes.add_edge(genv, nvtx, unified_elem) nvtx end changes.add_edge(genv, Source.new(Type::Array.new(genv, elems, genv.gen_ary_type(unified_elem))), vtx) end def typecheck(genv, changes, vtx, subst) changes.add_edge(genv, vtx, changes.target) vtx.each_type do |ty| case ty when Type::Array next if ty.elems.size != @types.size @types.zip(ty.elems) do |f_ty, a_ty| return false unless f_ty.typecheck(genv, changes, a_ty, subst) end return true when Type::Instance @types.each do |f_ty| return false unless f_ty.typecheck(genv, changes, vtx, subst) end return true end end false end def show "[#{ @types.map {|ty| ty.show }.join(", ") }]" end end class SigTyRecordNode < SigTyNode def initialize(raw_decl, lenv) super(raw_decl, lenv) @fields = raw_decl.fields.transform_values { |val| AST.create_rbs_type(val, lenv) } end attr_reader :fields def subnodes = { fields: } def covariant_vertex0(genv, changes, vtx, subst) field_vertices = {} @fields.each do |key, field_node| field_vertices[key] = field_node.covariant_vertex(genv, changes, subst) end # Create base Hash type for Record key_vtx = Source.new(genv.symbol_type) # Create union of all field values for the Hash value type val_vtx = changes.new_covariant_vertex(genv, [self, :union]) field_vertices.each_value do |field_vtx| changes.add_edge(genv, field_vtx, val_vtx) end base_hash_type = genv.gen_hash_type(key_vtx, val_vtx) changes.add_edge(genv, Source.new(Type::Record.new(genv, field_vertices, base_hash_type)), vtx) end def contravariant_vertex0(genv, changes, vtx, subst) field_vertices = {} @fields.each do |key, field_node| field_vertices[key] = field_node.contravariant_vertex(genv, changes, subst) end # Create base Hash type for Record key_vtx = Source.new(genv.symbol_type) # Create union of all field values for the Hash value type val_vtx = changes.new_contravariant_vertex(genv, [self, :union]) field_vertices.each_value do |field_vtx| changes.add_edge(genv, field_vtx, val_vtx) end base_hash_type = genv.gen_hash_type(key_vtx, val_vtx) changes.add_edge(genv, Source.new(Type::Record.new(genv, field_vertices, base_hash_type)), vtx) end def typecheck(genv, changes, vtx, subst) changes.add_edge(genv, vtx, changes.target) vtx.each_type do |ty| case ty when Type::Hash @fields.each do |key, field_node| val_vtx = ty.get_value(key) return false unless field_node.typecheck(genv, changes, val_vtx, subst) end return true end end false end def show field_strs = @fields.map do |key, field_node| "#{ key }: #{ field_node.show }" end "{ #{ field_strs.join(", ") } }" end end class SigTyVarNode < SigTyNode def initialize(raw_decl, lenv) super(raw_decl, lenv) @var = raw_decl.name end attr_reader :var def attrs = { var: } def covariant_vertex0(genv, changes, vtx, subst) raise "unknown type variable: #{ @var }" unless subst[@var] changes.add_edge(genv, subst[@var], vtx) end def contravariant_vertex0(genv, changes, vtx, subst) raise "unknown type variable: #{ @var }" unless subst[@var] changes.add_edge(genv, Source.new(Type::Var.new(genv, @var, subst[@var])), vtx) end def typecheck(genv, changes, vtx, subst) changes.add_edge(genv, vtx.new_vertex(genv, self), subst[@var]) unless vtx == subst[@var] true end def show "#{ @var }" end end class SigTyOptionalNode < SigTyNode def initialize(raw_decl, lenv) super(raw_decl, lenv) @type = AST.create_rbs_type(raw_decl.type, lenv) end attr_reader :type def subnodes = { type: } def covariant_vertex0(genv, changes, vtx, subst) @type.covariant_vertex0(genv, changes, vtx, subst) changes.add_edge(genv, Source.new(genv.nil_type), vtx) end def contravariant_vertex0(genv, changes, vtx, subst) @type.contravariant_vertex0(genv, changes, vtx, subst) changes.add_edge(genv, Source.new(genv.nil_type), vtx) end def typecheck(genv, changes, vtx, subst) @type.typecheck(genv, changes, vtx, subst) end def show s = @type.show if @type.is_a?(SigTyIntersectionNode) || @type.is_a?(SigTyUnionNode) s = "(#{ s })" end s + "?" end end class SigTyLiteralNode < SigTyNode def initialize(raw_decl, lenv) super(raw_decl, lenv) @lit = raw_decl.literal end attr_reader :lit def attrs = { lit: } def get_type(genv) case @lit when ::Symbol Type::Symbol.new(genv, @lit) when ::Integer then genv.int_type when ::String then genv.str_type when ::TrueClass then genv.true_type when ::FalseClass then genv.false_type else raise "unknown RBS literal: #{ @lit.inspect }" end end def covariant_vertex0(genv, changes, vtx, subst) changes.add_edge(genv, Source.new(get_type(genv)), vtx) end def contravariant_vertex0(genv, changes, vtx, subst) changes.add_edge(genv, Source.new(get_type(genv)), vtx) end def typecheck(genv, changes, vtx, subst) if @lit.is_a?(::Symbol) changes.add_edge(genv, vtx, changes.target) vtx.each_type do |ty| case ty when Type::Symbol return true if ty.sym == @lit end end return false end f_mod = get_type(genv).mod AST.typecheck_for_module(genv, changes, f_mod, [], vtx, subst) end def show @lit.inspect end end class SigTyProcNode < SigTyNode def initialize(raw_decl, lenv) super(raw_decl, lenv) # raw_decl.type is an RBS::Types::Function, we need to wrap it in a MethodType if raw_decl.type method_type = RBS::MethodType.new( type: raw_decl.type, type_params: [], block: raw_decl.block, location: raw_decl.location ) @type = AST.create_rbs_func_type(method_type, nil, raw_decl.block, lenv) else @type = nil end end attr_reader :type def subnodes = { type: } def covariant_vertex0(genv, changes, vtx, subst) # For now, just return the base Proc type without the function signature details # TODO: Create a proper Type::Proc with the function signature changes.add_edge(genv, Source.new(genv.proc_type), vtx) end def contravariant_vertex0(genv, changes, vtx, subst) # For now, just return the base Proc type without the function signature details # TODO: Create a proper Type::Proc with the function signature changes.add_edge(genv, Source.new(genv.proc_type), vtx) end def typecheck(genv, changes, vtx, subst) # TODO: proper check AST.typecheck_for_module(genv, changes, genv.proc_type.mod, [], vtx, subst) end def show "^(...)" end end class SigTyInterfaceNode < SigTyNode def initialize(raw_decl, lenv) super(raw_decl, lenv) name = raw_decl.name @cpath = name.namespace.path + [name.name] @toplevel = name.namespace.absolute? # "::Foo" or "Foo" @args = raw_decl.args.map {|arg| AST.create_rbs_type(arg, lenv) } end attr_reader :cpath, :toplevel, :args def subnodes = { args: } def attrs = { cpath:, toplevel: } def define0(genv) @args.each {|arg| arg.define(genv) } const_reads = [] const_read = BaseConstRead.new(genv, @cpath.first, @toplevel ? CRef::Toplevel : @lenv.cref, false) const_reads << const_read unless @cpath.empty? @cpath[1..].each do |cname| const_read = ScopedConstRead.new(cname, const_read, false) const_reads << const_read end end const_reads end def undefine0(genv) @static_ret.each do |const_read| const_read.destroy(genv) end @args.each {|arg| arg.undefine(genv) } end def covariant_vertex0(genv, changes, vtx, subst) changes.add_depended_static_read(@static_ret.last) cpath = @static_ret.last.cpath return unless cpath mod = genv.resolve_cpath(cpath) args = @args.map {|arg| arg.covariant_vertex(genv, changes, subst) } changes.add_edge(genv, Source.new(Type::Instance.new(genv, mod, args)), vtx) end def contravariant_vertex0(genv, changes, vtx, subst) changes.add_depended_static_read(@static_ret.last) cpath = @static_ret.last.cpath return unless cpath mod = genv.resolve_cpath(cpath) args = @args.map {|arg| arg.contravariant_vertex(genv, changes, subst) } changes.add_edge(genv, Source.new(Type::Instance.new(genv, mod, args)), vtx) end def typecheck(genv, changes, vtx, subst) changes.add_depended_static_read(@static_ret.last) cpath = @static_ret.last.cpath return unless cpath f_mod = genv.resolve_cpath(cpath) # self/f_mod: formal, vtx: actual AST.typecheck_for_module(genv, changes, f_mod, @args, vtx, subst) end def show s = "::#{ @cpath.join("::") }" if !@args.empty? s << "[...]" end
Source
# File bundled-src/typeprof-0.31.1/lib/typeprof/core/ast/sig_type.rb, line 664 def typecheck(genv, changes, vtx, subst) changes.add_depended_static_read(@static_ret.last) cpath = @static_ret.last.cpath return unless cpath f_mod = genv.resolve_cpath(cpath) AST.typecheck_for_module(genv, changes, f_mod, @args, vtx, subst) end
Source
# File bundled-src/typeprof-0.31.1/lib/typeprof/core/ast/sig_type.rb, line 635 def undefine0(genv) @static_ret.each do |const_read| const_read.destroy(genv) end @args.each {|arg| arg.undefine(genv) } end