123456789_123456789_123456789_123456789_123456789_

Class: TypeProf::Type::Hash::Elements

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/typeprof/container-type.rb

Overview

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(map_tys) ⇒ Elements

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 613

def initialize(map_tys)
  map_tys.each do |k_ty, v_ty|
    raise unless k_ty.is_a?(Type)
    raise unless v_ty.is_a?(Type)
    raise if k_ty.is_a?(Type::Union)
    raise if k_ty.is_a?(Type::Local)
    raise if k_ty.is_a?(Type::Array)
    raise if k_ty.is_a?(Type::Hash)
  end
  @map_tys = map_tys
end

Class Method Details

.dummy_elements

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 625

def self.dummy_elements
  Elements.new({Type.any => Type.any})
end

Instance Attribute Details

#map_tys (readonly)

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 629

attr_reader :map_tys

Instance Method Details

#[](key_ty)

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 762

def [](key_ty)
  val_ty = Type.bot
  @map_tys.each do |k_ty, v_ty|
    if Type.match?(k_ty, key_ty)
      val_ty = val_ty.union(v_ty)
    end
  end
  val_ty
end

#each_free_type_variable(&blk)

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 727

def each_free_type_variable(&blk)
  @map_tys.each do |k, v|
    k.each_free_type_variable(&blk)
    v.each_free_type_variable(&blk)
  end
end

#globalize(env, visited, depth)

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 635

def globalize(env, visited, depth)
  map_tys = {}
  @map_tys.each do |k_ty, v_ty|
    v_ty = v_ty.globalize(env, visited, depth)
    if map_tys[k_ty]
      map_tys[k_ty] = map_tys[k_ty].union(v_ty)
    else
      map_tys[k_ty] = v_ty
    end
  end
  Elements.new(map_tys)
end

#include_untyped?(scratch) ⇒ Boolean

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 820

def include_untyped?(scratch)
  @map_tys.each do |key, val|
    return true if key.include_untyped?(scratch)
    return true if val.include_untyped?(scratch)
  end
  false
end

#limit_size(limit)

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 657

def limit_size(limit)
  map_tys = {}
  @map_tys.each do |k_ty, v_ty|
    k_ty = k_ty.limit_size(limit)
    v_ty = v_ty.limit_size(limit)
    if map_tys[k_ty]
      map_tys[k_ty] = map_tys[k_ty].union(v_ty)
    else
      map_tys[k_ty] = v_ty
    end
  end
  Elements.new(map_tys)
end

#localize(env, alloc_site, depth)

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 648

def localize(env, alloc_site, depth)
  map_tys = @map_tys.to_h do |k_ty, v_ty|
    alloc_site2 = alloc_site.add_id(k_ty)
    env, v_ty = v_ty.localize(env, alloc_site2, depth)
    [k_ty, v_ty]
  end
  return env, Elements.new(map_tys)
end

#match?(other) ⇒ Boolean

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 707

def match?(other)
  subst = nil
  other.map_tys.each do |k1, v1|
    subst2 = nil
    @map_tys.each do |k0, v0|
      subst3 = Type.match?(k0, k1)
      if subst3
        subst4 = Type.match?(v0, v1)
        if subst4
          subst2 = Type.merge_substitution(subst2, subst3)
          subst2 = Type.merge_substitution(subst2, subst4)
        end
      end
    end
    return nil unless subst2
    subst = Type.merge_substitution(subst, subst2)
  end
  subst
end

#pretty_print(q)

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 692

def pretty_print(q)
  q.group(9, "Elements[", "]") do
    q.seplist(@map_tys) do |k_ty, v_ty|
      q.group do
        q.pp k_ty
        q.text '=>'
        q.group(1) do
          q.breakable ''
          q.pp v_ty
        end
      end
    end
  end
end

#screen_name(scratch)

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 671

def screen_name(scratch)
  if !@map_tys.empty? && @map_tys.all? {|k_ty,| k_ty.is_a?(Type::Symbol) }
    s = @map_tys.map do |k_ty, v_ty|
      v = v_ty.screen_name(scratch)
      "#{ k_ty.sym }: #{ v }"
    end.join(", ")
    "{#{ s }}"
  else
    k_ty = v_ty = Type.bot
    @map_tys.each do |k, v|
      k_ty = k_ty.union(k)
      v_ty = v_ty.union(v)
    end
    k_ty = Type.any if k_ty == Type.bot
    v_ty = Type.any if v_ty == Type.bot
    k_ty = k_ty.screen_name(scratch)
    v_ty = v_ty.screen_name(scratch)
    "Hash[#{ k_ty }, #{ v_ty }]"
  end
end

#squash

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 753

def squash
  all_k_ty, all_v_ty = Type.bot, Type.bot
  @map_tys.each do |k_ty, v_ty|
    all_k_ty = all_k_ty.union(k_ty)
    all_v_ty = all_v_ty.union(v_ty)
  end
  return all_k_ty, all_v_ty
end

#substitute(subst, depth)

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 734

def substitute(subst, depth)
  map_tys = {}
  @map_tys.each do |k_ty_orig, v_ty_orig|
    k_ty = k_ty_orig.substitute(subst, depth)
    v_ty = v_ty_orig.substitute(subst, depth)
    k_ty.each_child_global do |k_ty|
      # This is a temporal hack to mitigate type explosion
      k_ty = Type.any if k_ty.is_a?(Type::Array)
      k_ty = Type.any if k_ty.is_a?(Type::Hash)
      if map_tys[k_ty]
        map_tys[k_ty] = map_tys[k_ty].union(v_ty)
      else
        map_tys[k_ty] = v_ty
      end
    end
  end
  Elements.new(map_tys)
end

#to_keywords

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 804

def to_keywords
  kw_tys = {}
  @map_tys.each do |key_ty, val_ty|
    if key_ty.is_a?(Type::Symbol)
      kw_tys[key_ty.sym] = val_ty
    else
      all_val_ty = Type.bot
      @map_tys.each do |_key_ty, val_ty|
        all_val_ty = all_val_ty.union(val_ty)
      end
      return { nil => all_val_ty }
    end
  end
  kw_tys
end

#to_local_type(id, base_ty)

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 631

def to_local_type(id, base_ty)
  Type::Local.new(Hash, id, base_ty)
end

#union(other)

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 788

def union(other)
  return self if self == other
  raise "Array::Elements merge Hash::Elements" if other.is_a?(Array::Elements)

  map_tys = @map_tys.dup
  other.map_tys.each do |k_ty, v_ty|
    if map_tys[k_ty]
      map_tys[k_ty] = map_tys[k_ty].union(v_ty)
    else
      map_tys[k_ty] = v_ty
    end
  end

  Elements.new(map_tys)
end

#update(idx, ty)

[ GitHub ]

  
# File 'lib/typeprof/container-type.rb', line 772

def update(idx, ty)
  map_tys = @map_tys.dup
  idx.each_child_global do |idx|
    # This is a temporal hack to mitigate type explosion
    idx = Type.any if idx.is_a?(Type::Array)
    idx = Type.any if idx.is_a?(Type::Hash)

    if map_tys[idx]
      map_tys[idx] = map_tys[idx].union(ty)
    else
      map_tys[idx] = ty
    end
  end
  Elements.new(map_tys)
end