Class: MilkTea::ControlFlow::NullabilityFlow

Inherits:
Object
  • Object
show all
Defined in:
lib/milk_tea/core/control_flow/nullability_flow.rb

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.solve(graph) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/milk_tea/core/control_flow/nullability_flow.rb', line 15

def self.solve(graph)
  stmt_to_node_id = {}
  graph.each_node { |n| stmt_to_node_id[n.statement.object_id] = n.id if n.statement }

  result = Dataflow.solve(
    graph,
    direction: :forward,
    initial: -> { Set.new },
    join: lambda do |states|
      return Set.new if states.empty?

      states.reduce { |acc, s| acc & s }
    end,
    edge_transfer: lambda do |node, in_state, succ_id, _edge_label|
      state = in_state - node.writes
      refs  = graph.edge_refinement(node.id, succ_id) || {}
      refs.each do |key, ref|
        ref == :non_null ? (state = state | Set[key]) : (state = state - Set[key])
      end
      state
    end,
    boundary_in: { graph.entry_id => Set.new }
  )

  Result.new(in_states: result.in_states, out_states: result.out_states, stmt_to_node_id:)
end