class RBS::TypeAliasDependency

属性

dependencies[R]

一个哈希表,用于存储有向图的传递闭包

direct_dependencies[R]

直接依赖关系对应于一个有向图,其中顶点是类型,方向基于类型的赋值

env[R]

公共类方法

new(env:) 点击以切换源代码
# File rbs-3.8.0/lib/rbs/type_alias_dependency.rb, line 14
def initialize(env:)
  @env = env
end

公共实例方法

build_dependencies() 点击以切换源代码
# File rbs-3.8.0/lib/rbs/type_alias_dependency.rb, line 27
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.type_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) 点击以切换源代码

检查别名类型定义是否循环并被禁止

# File rbs-3.8.0/lib/rbs/type_alias_dependency.rb, line 19
def circular_definition?(alias_name)
  # Construct transitive closure, if not constructed already
  transitive_closure() unless @dependencies

  alias_name = env.normalize_type_name!(alias_name)
  @dependencies[alias_name][alias_name]
end
dependencies_of(name) 点击以切换源代码
# File rbs-3.8.0/lib/rbs/type_alias_dependency.rb, line 57
def dependencies_of(name)
  name = env.normalize_type_name!(name)
  @dependencies[name].each_key.to_set
end
direct_dependencies_of(name) 点击以切换源代码
# File rbs-3.8.0/lib/rbs/type_alias_dependency.rb, line 52
def direct_dependencies_of(name)
  name = env.normalize_type_name!(name)
  @direct_dependencies[name]
end
transitive_closure() 点击以切换源代码
# File rbs-3.8.0/lib/rbs/type_alias_dependency.rb, line 43
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

私有实例方法

dependency(start, vertex, nested = nil) 点击以切换源代码

构造传递闭包的递归函数

# File rbs-3.8.0/lib/rbs/type_alias_dependency.rb, line 81
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 = Set[]) 点击以切换源代码

递归地构造有向图

# File rbs-3.8.0/lib/rbs/type_alias_dependency.rb, line 65
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 << env.normalize_type_name(type.name)
  end

  result
end