class TypeProf::Core::AST::SigModuleBaseNode::SigDefNode

Attributes

instance[R]
method_types[R]
mid[R]
overloading[R]
singleton[R]

Public Class Methods

new(raw_decl, lenv) click to toggle source
Calls superclass method TypeProf::Core::AST::Node::new
# File typeprof-0.30.1/lib/typeprof/core/ast/sig_decl.rb, line 180
def initialize(raw_decl, lenv)
  super(raw_decl, lenv)
  @mid = raw_decl.name
  @singleton = raw_decl.singleton?
  @instance = raw_decl.instance?
  @method_types = raw_decl.overloads.map do |overload|
    method_type = overload.method_type
    AST.create_rbs_func_type(method_type, method_type.type_params, method_type.block, lenv)
  end
  @overloading = raw_decl.overloading
end

Public Instance Methods

attrs(= { mid:, singleton:, instance:, overloading: }) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/ast/sig_decl.rb, line 195
  def attrs = { mid:, singleton:, instance:, overloading: }

  def install0(genv)
    [[@singleton, true], [@instance, false]].each do |enabled, singleton|
      next unless enabled
      @changes.add_method_decl_box(genv, @lenv.cref.cpath, singleton, @mid, @method_types, @overloading)
    end
    Source.new
  end
end
install0(genv) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/ast/sig_decl.rb, line 197
def install0(genv)
  [[@singleton, true], [@instance, false]].each do |enabled, singleton|
    next unless enabled
    @changes.add_method_decl_box(genv, @lenv.cref.cpath, singleton, @mid, @method_types, @overloading)
  end
  Source.new
end
subnodes(= { method_types: }) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/ast/sig_decl.rb, line 194
      def subnodes = { method_types: }
      def attrs = { mid:, singleton:, instance:, overloading: }

      def install0(genv)
        [[@singleton, true], [@instance, false]].each do |enabled, singleton|
          next unless enabled
          @changes.add_method_decl_box(genv, @lenv.cref.cpath, singleton, @mid, @method_types, @overloading)
        end
        Source.new
      end
    end

    class SigIncludeNode < Node
      def initialize(raw_decl, lenv)
        super(raw_decl, lenv)
        name = raw_decl.name
        @cpath = name.namespace.path + [name.name]
        @toplevel = name.namespace.absolute?
        @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)
        const_reads << const_read
        @cpath[1..].each do |cname|
          const_read = ScopedConstRead.new(cname, const_read)
          const_reads << const_read
        end
        mod = genv.resolve_cpath(@lenv.cref.cpath)
        const_read.followers << mod
        mod.add_include_decl(genv, self)
        const_reads
      end

      def define_copy(genv)
        mod = genv.resolve_cpath(@lenv.cref.cpath)
        mod.add_include_decl(genv, self)
        mod.remove_include_decl(genv, @prev_node)
        super(genv)
      end

      def undefine0(genv)
        mod = genv.resolve_cpath(@lenv.cref.cpath)
        mod.remove_include_decl(genv, self)
        @static_ret.each do |const_read|
          const_read.destroy(genv)
        end
        @args.each {|arg| arg.undefine(genv) }
      end

      def install0(genv)
        Source.new
      end
    end

    class SigAliasNode < Node
      def initialize(raw_decl, lenv)
        super(raw_decl, lenv)
        @new_mid = raw_decl.new_name
        @old_mid = raw_decl.old_name
        @singleton = raw_decl.singleton?
        @instance = raw_decl.instance?
      end

      attr_reader :new_mid, :old_mid, :singleton, :instance
      def attrs = { new_mid:, old_mid:, singleton:, instance: }

      def install0(genv)
        [[@singleton, true], [@instance, false]].each do |enabled, singleton|
          next unless enabled
          @changes.add_method_alias_box(genv, @lenv.cref.cpath, singleton, @new_mid, @old_mid)
        end
        Source.new
      end
    end

    class SigAttrReaderNode < Node
      def initialize(raw_decl, lenv)
        super(raw_decl, lenv)
        @mid = raw_decl.name
        # `eval` is used to prevent TypeProf from failing to parse keyword arguments during dogfooding.
        # TODO: Remove `eval` once TypeProf supports keyword arguments.
        eval <<~RUBY
          rbs_method_type = RBS::MethodType.new(
            type: RBS::Types::Function.empty(raw_decl.type),
            type_params: [],
            block: nil,
            location: raw_decl.type.location,
          )
          @method_type = AST.create_rbs_func_type(rbs_method_type, [], nil, lenv)
        RUBY
      end

      attr_reader :mid, :method_type

      def subnodes = { method_type: }
      def attrs = { mid: }

      def install0(genv)
        @changes.add_method_decl_box(genv, @lenv.cref.cpath, false, @mid, [@method_type], false)
        Source.new
      end
    end

    class SigAttrWriterNode < Node
      def initialize(raw_decl, lenv)
        super(raw_decl, lenv)
        @mid = :"#{raw_decl.name}="

        # `eval` is used to prevent TypeProf from failing to parse keyword arguments during dogfooding.
        # TODO: Remove `eval` once TypeProf supports keyword arguments.
        eval <<~RUBY
          # (raw_decl.type) -> raw_decl.type
          rbs_method_type = RBS::MethodType.new(
            type: RBS::Types::Function.new(
              required_positionals: [RBS::Types::Function::Param.new(name: nil, type: raw_decl.type, location: raw_decl.type.location)],
              optional_positionals: [],
              rest_positionals: nil,
              trailing_positionals: [],
              required_keywords: {},
              optional_keywords: {},
              rest_keywords: nil,
              return_type: raw_decl.type,
            ),
            type_params: [],
            block: nil,
            location: raw_decl.type.location,
          )
          @method_type = AST.create_rbs_func_type(rbs_method_type, [], nil, lenv)
        RUBY
      end

      attr_reader :mid, :method_type

      def subnodes = { method_type: }
      def attrs = { mid: }

      def install0(genv)
        @changes.add_method_decl_box(genv, @lenv.cref.cpath, false, @mid, [@method_type], false)
        Source.new
      end
    end

    class SigAttrAccessorNode < Node
      def initialize(raw_decl, lenv)
        super(raw_decl, lenv)
        @reader = SigAttrReaderNode.new(raw_decl, lenv)
        @writer = SigAttrWriterNode.new(raw_decl, lenv)
      end

      attr_reader :reader, :writer

      def subnodes = { reader:, writer: }

      def install0(genv)
        @reader.install0(genv)
        @writer.install0(genv)
        Source.new
      end
    end

    class SigConstNode < Node
      def initialize(raw_decl, lenv)
        super(raw_decl, lenv)
        @cpath = AST.resolve_rbs_name(raw_decl.name, lenv)
        @type = AST.create_rbs_type(raw_decl.type, lenv)
      end

      attr_reader :cpath, :type
      def subnodes = { type: }
      def attrs = { cpath: }

      def define0(genv)
        @type.define(genv)
        mod = genv.resolve_const(@cpath)
        mod.add_decl(self)
        mod
      end

      def define_copy(genv)
        mod = genv.resolve_const(@cpath)
        mod.add_decl(self)
        mod.remove_decl(@prev_node)
        super(genv)
      end

      def undefine0(genv)
        genv.resolve_const(@cpath).remove_decl(self)
        @type.undefine(genv)
      end

      def install0(genv)
        box = @changes.add_type_read_box(genv, @type)
        @changes.add_edge(genv, box.ret, @static_ret.vtx)
        box.ret
      end
    end

    class SigTypeAliasNode < Node
      def initialize(raw_decl, lenv)
        super(raw_decl, lenv)
        @cpath = AST.resolve_rbs_name(raw_decl.name, lenv)
        @name = @cpath.pop
        @type = AST.create_rbs_type(raw_decl.type, lenv)
        @params = raw_decl.type_params.map {|param| param.name }
      end

      attr_reader :cpath, :name, :type, :params

      def define0(genv)
        @type.define(genv)
        tae = genv.resolve_type_alias(@cpath, @name)
        tae.add_decl(self)
        tae
      end

      def define_copy(genv)
        tae = genv.resolve_type_alias(@cpath, @name)
        tae.add_decl(self)
        tae.remove_decl(@prev_node)
        super(genv)
      end

      def undefine0(genv)
        tae = genv.resolve_type_alias(@cpath, @name)
        tae.remove_decl(self)
        @type.undefine(genv)
      end

      def install0(genv)
        Source.new
      end
    end

    class SigGlobalVariableNode < Node
      def initialize(raw_decl, lenv)
        super(raw_decl, lenv)
        @var = raw_decl.name
        @type = AST.create_rbs_type(raw_decl.type, lenv)
      end

      attr_reader :cpath, :type
      def subnodes = { type: }
      def attrs = { cpath: }

      def define0(genv)
        @type.define(genv)
        mod = genv.resolve_gvar(@var)
        mod.add_decl(self)
        mod
      end

      def define_copy(genv)
        mod = genv.resolve_gvar(@var)
        mod.add_decl(self)
        mod.remove_decl(@prev_node)
        super(genv)
      end

      def undefine0(genv)
        genv.resolve_gvar(@var).remove_decl(self)
        @type.undefine(genv)
      end

      def install0(genv)
        box = @changes.add_type_read_box(genv, @type)
        @changes.add_edge(genv, box.ret