class TypeProf::Core::GlobalEnv

Attributes

cls_type[R]
complex_type[R]
false_type[R]
float_type[R]
int_type[R]
mod_ary[R]
mod_class[R]
mod_hash[R]
mod_object[R]
mod_range[R]
mod_str[R]
mod_type[R]
nil_type[R]
obj_type[R]
proc_type[R]
rational_type[R]
regexp_type[R]
run_count[RW]
set_type[R]
str_type[R]
symbol_type[R]
true_type[R]
type_table[R]

Public Class Methods

new() click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 3
def initialize
  @type_table = {}

  @static_eval_queue = []

  @run_queue = []
  @run_queue_set = Set[]

  @mod_object = ModuleEntity.new([])
  @mod_object.inner_modules[:Object] = @mod_object

  @mod_basic_object = resolve_cpath([:BasicObject])
  @mod_class = resolve_cpath([:Class])
  @mod_module = resolve_cpath([:Module])

  @gvars = {}
  @mod_ary = resolve_cpath([:Array])
  @mod_hash = resolve_cpath([:Hash])
  @mod_range = resolve_cpath([:Range])
  @mod_str = resolve_cpath([:String])

  @cls_type = Type::Instance.new(self, @mod_class, [])
  @mod_type = Type::Instance.new(self, @mod_module, [])
  @obj_type = Type::Instance.new(self, resolve_cpath([:Object]), [])
  @nil_type = Type::Instance.new(self, resolve_cpath([:NilClass]), [])
  @true_type = Type::Instance.new(self, resolve_cpath([:TrueClass]), [])
  @false_type = Type::Instance.new(self, resolve_cpath([:FalseClass]), [])
  @str_type = Type::Instance.new(self, resolve_cpath([:String]), [])
  @int_type = Type::Instance.new(self, resolve_cpath([:Integer]), [])
  @float_type = Type::Instance.new(self, resolve_cpath([:Float]), [])
  @rational_type = Type::Instance.new(self, resolve_cpath([:Rational]), [])
  @complex_type = Type::Instance.new(self, resolve_cpath([:Complex]), [])
  @proc_type = Type::Instance.new(self, resolve_cpath([:Proc]), [])
  @symbol_type = Type::Instance.new(self, resolve_cpath([:Symbol]), [])
  @set_type = Type::Instance.new(self, resolve_cpath([:Set]), [])
  @regexp_type = Type::Instance.new(self, resolve_cpath([:Regexp]), [])

  @run_count = 0
end

Public Instance Methods

add_run(obj) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 162
def add_run(obj)
  unless @run_queue_set.include?(obj)
    @run_queue << obj
    @run_queue_set << obj
  end
end
add_static_eval_queue(change_type, arg) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 135
def add_static_eval_queue(change_type, arg)
  @static_eval_queue << [change_type, arg]
end
define_all() click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 139
def define_all
  until @static_eval_queue.empty?
    change_type, arg = @static_eval_queue.shift
    case change_type
    when :inner_modules_changed
      arg[0].on_inner_modules_changed(self, arg[1])
    when :static_read_changed
      case arg
      when BaseStaticRead
        arg.on_scope_updated(self)
      when ScopedStaticRead
        arg.on_cbase_updated(self)
      else
        raise
      end
    when :parent_modules_changed
      arg.on_parent_modules_changed(self)
    else
      raise change_type.to_s
    end
  end
end
each_direct_superclass(mod, singleton) { |mod, singleton| ... } click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 65
def each_direct_superclass(mod, singleton)
  while mod
    yield mod, singleton
    singleton, mod = get_superclass(singleton, mod)
  end
end
each_included_module(mod) { |inc_mod, false| ... } click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 85
def each_included_module(mod, &blk)
  mod.included_modules.each do |_inc_decl, inc_mod|
    yield inc_mod, false
    each_included_module(inc_mod, &blk)
  end
end
each_superclass(mod, singleton) { |mod, singleton| ... } click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 72
def each_superclass(mod, singleton, &blk)
  while mod
    # TODO: prepended modules
    yield mod, singleton
    if singleton
      # TODO: extended modules
    else
      each_included_module(mod, &blk)
    end
    singleton, mod = get_superclass(singleton, mod)
  end
end
gen_ary_type(elem_vtx) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 51
def gen_ary_type(elem_vtx)
  Type::Instance.new(self, @mod_ary, [elem_vtx])
end
gen_hash_type(key_vtx, val_vtx) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 55
def gen_hash_type(key_vtx, val_vtx)
  Type::Instance.new(self, @mod_hash, [key_vtx, val_vtx])
end
gen_range_type(elem_vtx) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 59
def gen_range_type(elem_vtx)
  Type::Instance.new(self, @mod_range, [elem_vtx])
end
get_instance_type(mod, type_args, changes, base_ty_env, base_ty) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 111
def get_instance_type(mod, type_args, changes, base_ty_env, base_ty)
  ty_env = base_ty_env.dup
  if base_ty.is_a?(Type::Instance)
    base_ty.mod.type_params.zip(base_ty.args) do |param, arg|
      ty_env[param] = arg
    end
  end
  args = mod.type_params.zip(type_args).map do |param, arg|
    arg && changes ? arg.covariant_vertex(self, changes, ty_env) : Source.new
  end
  Type::Instance.new(self, mod, args)
end
get_superclass(singleton, mod) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 92
def get_superclass(singleton, mod)
  super_mod = mod.superclass
  if super_mod
    return [singleton, super_mod]
  else
    if mod == @mod_basic_object
      if singleton
        return [false, @mod_class]
      else
        return nil
      end
    elsif mod == @mod_module && !singleton
      return nil
    else
      return [false, @mod_module]
    end
  end
end
get_superclass_type(ty, changes, base_ty_env) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 124
def get_superclass_type(ty, changes, base_ty_env)
  singleton, super_mod = get_superclass(ty.is_a?(Type::Singleton), ty.mod)
  return unless super_mod

  if singleton
    Type::Singleton.new(self, super_mod)
  else
    get_instance_type(super_mod, ty.mod.superclass_type_args || [], changes, base_ty_env, ty)
  end
end
get_vertexes(vtxs) click to toggle source

just for validation

# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 183
def get_vertexes(vtxs)
  @mod_object.get_vertexes(vtxs)
  @gvars.each_value do |gvar_entity|
    vtxs << gvar_entity.vtx
  end
end
load_core_rbs(raw_decls) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 235
    def load_core_rbs(raw_decls)
      lenv = LocalEnv.new(nil, CRef::Toplevel, {}, [])
      decls = raw_decls.map do |raw_decl|
        AST.create_rbs_decl(raw_decl, lenv)
      end.compact

      decls += AST.parse_rbs("typeprof-rbs-shim.rbs", <<-RBS)
        class Exception
          include _Exception
        end
        class String
          include _ToS
          include _ToStr
        end
        class Array[Elem]
          include _ToAry[Elem]
          include _Each[Elem]
        end
        class Hash[K, V]
          include _Each[[K, V]]
        end
      RBS

      # Loading frequently used modules first will reduces constant resolution
      # which makes loading faster :-)
      critical_modules = [
        decls.find {|decl| decl.cpath == [:Object] },
        decls.find {|decl| decl.cpath == [:Module] },
        decls.find {|decl| decl.cpath == [:Numeric] },
        decls.find {|decl| decl.cpath == [:Integer] },
        decls.find {|decl| decl.cpath == [:String] },
        decls.find {|decl| decl.cpath == [:Array] },
        decls.find {|decl| decl.cpath == [:Hash] },
        decls.find {|decl| decl.cpath == [:Enumerator] },
      ]
      decls = critical_modules + (decls - critical_modules)

      decls.each {|decl| decl.define(self) }
      define_all
      decls.each {|decl| decl.install(self) }
      run_all
    end
resolve_const(cpath) click to toggle source

constants

# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 203
def resolve_const(cpath)
  mod = resolve_cpath(cpath[0..-2])
  mod.get_const(cpath[-1])
end
resolve_cpath(cpath) click to toggle source

classes and modules

# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 192
def resolve_cpath(cpath)
  mod = @mod_object
  raise unless cpath # annotation
  cpath.each do |cname|
    mod = mod.inner_modules[cname] ||= ModuleEntity.new(mod.cpath + [cname], mod)
  end
  mod
end
resolve_cvar(cpath, name) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 223
def resolve_cvar(cpath, name)
  # TODO: include はあとで考える
  mod = resolve_cpath(cpath)
  mod.get_cvar(name)
end
resolve_gvar(name) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 213
def resolve_gvar(name)
  @gvars[name] ||= ValueEntity.new
end
resolve_ivar(cpath, singleton, name) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 217
def resolve_ivar(cpath, singleton, name)
  # TODO: include はあとで考える
  mod = resolve_cpath(cpath)
  mod.get_ivar(singleton, name)
end
resolve_method(cpath, singleton, mid) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 208
def resolve_method(cpath, singleton, mid)
  mod = resolve_cpath(cpath)
  mod.get_method(singleton, mid)
end
resolve_type_alias(cpath, name) click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 229
def resolve_type_alias(cpath, name)
  # TODO: include はあとで考える
  mod = resolve_cpath(cpath)
  mod.get_type_alias(name)
end
run_all() click to toggle source
# File typeprof-0.30.1/lib/typeprof/core/env.rb, line 169
def run_all
  run_count = 0
  until @run_queue.empty?
    obj = @run_queue.shift
    @run_queue_set.delete(obj)
    unless obj.destroyed
      run_count += 1
      obj.run(self)
    end
  end
  @run_count += run_count
end