Class: TypeProf::Core::BasicVertex

Inherits:
Object
  • Object
show all
Defined in:
lib/typeprof/core/graph/vertex.rb

Direct Known Subclasses

Source, Vertex

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types) ⇒ BasicVertex

Returns a new instance of BasicVertex.



3
4
5
6
# File 'lib/typeprof/core/graph/vertex.rb', line 3

def initialize(types)
  @types = types
  @types_to_be_added = {}
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



8
9
10
# File 'lib/typeprof/core/graph/vertex.rb', line 8

def types
  @types
end

Instance Method Details

#check_match(genv, changes, vtx) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/typeprof/core/graph/vertex.rb', line 23

def check_match(genv, changes, vtx)
  vtx.each_type do |ty|
    if ty.is_a?(Type::Var)
      changes.add_edge(genv, self, ty.vtx) if self != ty.vtx
      return true
    end
  end

  return true if @types.empty?
  return true if vtx.types.empty?

  each_type do |ty|
    return true if vtx.types.include?(ty) # fast path
    if ty.check_match(genv, changes, vtx)
      return true
    end
  end

  return false
end

#each_type(&blk) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/typeprof/core/graph/vertex.rb', line 10

def each_type(&blk)
  @types.each_key(&blk)

  until @types_to_be_added.empty?
    h = @types_to_be_added.dup
    h.each do |ty, source|
      @types[ty] = source
    end
    @types_to_be_added.clear
    h.each_key(&blk)
  end
end

#showObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/typeprof/core/graph/vertex.rb', line 44

def show
  Fiber[:show_rec] ||= Set[]
  if Fiber[:show_rec].include?(self)
    "untyped"
  else
    begin
      Fiber[:show_rec] << self
      types = []
      bot = @types.keys.any? {|ty| ty.is_a?(Type::Bot) }
      optional = true_exist = false_exist = false
      each_type do |ty|
        if ty.is_a?(Type::Instance)
          case ty.mod.cpath
          when [:NilClass] then optional = true
          when [:TrueClass] then true_exist = true
          when [:FalseClass] then false_exist = true
          end
        end
      end
      bool = true_exist && false_exist
      types << "bool" if bool
      each_type do |ty, _source|
        if ty.is_a?(Type::Instance)
          next if ty.mod.cpath == [:NilClass]
          next if bool && (ty.mod.cpath == [:TrueClass] || ty.mod.cpath == [:FalseClass])
        end
        next if ty.is_a?(Type::Bot)
        types << ty.show
      end
      types = types.uniq.sort
      case types.size
      when 0
        optional ? "nil" : bot ? "bot" : "untyped"
      when 1
        types.first + (optional ? "?" : "")
      else
        "(#{ types.join(" | ") })" + (optional ? "?" : "")
      end
    ensure
      Fiber[:show_rec].delete(self) || raise
    end
  end
end