123456789_123456789_123456789_123456789_123456789_

Class: RBS::TypeAliasDependency

Relationships & Source Files
Inherits: Object
Defined in: lib/rbs/type_alias_dependency.rb

Class Method Summary

Instance Attribute Summary

  • #dependencies readonly

    A hash which stores the transitive closure of the directed graph.

  • #direct_dependencies readonly

    Direct dependencies corresponds to a directed graph with vertices as types and directions based on assignment of types.

  • #env readonly

Instance Method Summary

Constructor Details

.new(env:) ⇒ TypeAliasDependency

[ GitHub ]

  
# File 'lib/rbs/type_alias_dependency.rb', line 12

def initialize(env:)
  @env = env
end

Instance Attribute Details

#dependencies (readonly)

A hash which stores the transitive closure of the directed graph

[ GitHub ]

  
# File 'lib/rbs/type_alias_dependency.rb', line 10

attr_reader :dependencies

#direct_dependencies (readonly)

Direct dependencies corresponds to a directed graph with vertices as types and directions based on assignment of types

[ GitHub ]

  
# File 'lib/rbs/type_alias_dependency.rb', line 7

attr_reader :direct_dependencies

#env (readonly)

[ GitHub ]

  
# File 'lib/rbs/type_alias_dependency.rb', line 3

attr_reader :env

Instance Method Details

#build_dependencies

[ GitHub ]

  
# File 'lib/rbs/type_alias_dependency.rb', line 25

def build_dependencies
  return if @direct_dependencies

  # Initialize hash(a directed graph)
  @direct_dependencies = {}
  # Initialize dependencies as an empty hash
  @dependencies = {}
  # Iterate over alias declarations inserted into environment
  env.alias_decls.each do |name, entry|
    # Construct a directed graph by recursively extracting type aliases
    @direct_dependencies[name] = direct_dependency(entry.decl.type)
    # Initialize dependencies with an empty hash
    @dependencies[name] = {}
  end
end

#circular_definition?(alias_name) ⇒ Boolean

Check if an alias type definition is circular & prohibited

[ GitHub ]

  
# File 'lib/rbs/type_alias_dependency.rb', line 17

def circular_definition?(alias_name)
  # Construct transitive closure, if not constructed already
  transitive_closure() unless @dependencies

  # Check for recursive type alias
  @dependencies[alias_name][alias_name]
end

#dependency(start, vertex, nested = nil) (private)

Recursive function to construct transitive closure

[ GitHub ]

  
# File 'lib/rbs/type_alias_dependency.rb', line 69

def dependency(start, vertex, nested = nil)
  if (start == vertex)
    if (@direct_dependencies[start].include?(vertex) || nested)
      # Mark a vertex as connected to itself
      # if it is connected as an edge || a path(traverse multiple edges)
      @dependencies[start][vertex] = true
    end
  else
    # Mark a pair of vertices as connected while recursively performing DFS
    @dependencies[start][vertex] = true
  end

  # Iterate over the direct dependencies of the vertex
  @direct_dependencies[vertex]&.each do |type_name|
    # Invoke the function unless it is already checked
    dependency(start, type_name, start == type_name) unless @dependencies[start][type_name]
  end
end

#direct_dependency(type, result = ) (private)

Constructs directed graph recursively

[ GitHub ]

  
# File 'lib/rbs/type_alias_dependency.rb', line 53

def direct_dependency(type, result = Set[])
  case type
  when RBS::Types::Union, RBS::Types::Intersection, RBS::Types::Optional
    # Iterate over nested types & extract type aliases recursively
    type.each_type do |nested_type|
      direct_dependency(nested_type, result)
    end
  when RBS::Types::Alias
    # Append type name if the type is an alias
    result << type.name
  end

  result
end

#transitive_closure

[ GitHub ]

  
# File 'lib/rbs/type_alias_dependency.rb', line 41

def transitive_closure
  # Construct a graph of direct dependencies
  build_dependencies()
  # Construct transitive closure by using DFS(recursive technique)
  @direct_dependencies.each_key do |name|
    dependency(name, name)
  end
end