Class: TypeProf::Core::Vertex

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BasicVertex

#each_type, #show

Constructor Details

#initialize(origin) ⇒ Vertex

Returns a new instance of Vertex.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/typeprof/core/graph/vertex.rb', line 126

def initialize(origin)
  # Note that origin is just for debug.
  # When an AST node is reused, the value of the origin will be invalid.
  case origin
  when AST::Node
  when RBS::AST::Declarations::Base
  when ValueEntity
  when ActualArguments
  when Array
  when Symbol
  else
    raise "unknown class: #{ origin.class }"
  end
  @next_vtxs = Set.empty
  super({})
end

Instance Attribute Details

#next_vtxsObject (readonly)

Returns the value of attribute next_vtxs.



143
144
145
# File 'lib/typeprof/core/graph/vertex.rb', line 143

def next_vtxs
  @next_vtxs
end

#typesObject (readonly)

Returns the value of attribute types.



143
144
145
# File 'lib/typeprof/core/graph/vertex.rb', line 143

def types
  @types
end

Instance Method Details

#add_edge(genv, nvtx) ⇒ Object



191
192
193
194
# File 'lib/typeprof/core/graph/vertex.rb', line 191

def add_edge(genv, nvtx)
  @next_vtxs << nvtx
  nvtx.on_type_added(genv, self, @types.keys) unless @types.empty?
end

#new_vertex(genv, origin) ⇒ Object



185
186
187
188
189
# File 'lib/typeprof/core/graph/vertex.rb', line 185

def new_vertex(genv, origin)
  nvtx = Vertex.new(origin)
  add_edge(genv, nvtx)
  nvtx
end

#on_type_added(genv, src_var, added_types) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/typeprof/core/graph/vertex.rb', line 145

def on_type_added(genv, src_var, added_types)
  new_added_types = nil
  added_types.each do |ty|
    if @types[ty]
      @types[ty] << src_var
    else
      set = Set.empty
      begin
        @types[ty] = set
      rescue
        (@types_to_be_added ||= {})[ty] = set
      end
      set << src_var
      (new_added_types ||= []) << ty
    end
  end
  if new_added_types
    @next_vtxs.each do |nvtx|
      nvtx.on_type_added(genv, self, new_added_types)
    end
  end
end

#on_type_removed(genv, src_var, removed_types) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/typeprof/core/graph/vertex.rb', line 168

def on_type_removed(genv, src_var, removed_types)
  new_removed_types = nil
  removed_types.each do |ty|
    raise "!!! not implemented" if @types_to_be_added&.[](ty)
    @types[ty].delete(src_var) || raise
    if @types[ty].empty?
      @types.delete(ty) || raise
      (new_removed_types ||= []) << ty
    end
  end
  if new_removed_types
    @next_vtxs.each do |nvtx|
      nvtx.on_type_removed(genv, self, new_removed_types)
    end
  end
end

#remove_edge(genv, nvtx) ⇒ Object



196
197
198
199
# File 'lib/typeprof/core/graph/vertex.rb', line 196

def remove_edge(genv, nvtx)
  @next_vtxs.delete(nvtx) || raise
  nvtx.on_type_removed(genv, self, @types.keys) unless @types.empty?
end

#to_sObject Also known as: inspect



203
204
205
# File 'lib/typeprof/core/graph/vertex.rb', line 203

def to_s
  "v#{ @id ||= $new_id += 1 }"
end