Class: RBS::TypeAliasDependency

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/type_alias_dependency.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:) ⇒ TypeAliasDependency

Returns a new instance of TypeAliasDependency.



14
15
16
# File 'lib/rbs/type_alias_dependency.rb', line 14

def initialize(env:)
  @env = env
end

Instance Attribute Details

#dependenciesObject (readonly)

A hash which stores the transitive closure of the directed graph



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

def dependencies
  @dependencies
end

#direct_dependenciesObject (readonly)

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



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

def direct_dependencies
  @direct_dependencies
end

#envObject (readonly)

Returns the value of attribute env.



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

def env
  @env
end

Instance Method Details

#build_dependenciesObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File '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.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

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/rbs/type_alias_dependency.rb', line 19

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

#transitive_closureObject



43
44
45
46
47
48
49
50
# File '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