Class: RBS::Resolver::ConstantResolver::Table

Inherits:
Object
  • Object
show all
Defined in:
sig/resolver/constant_resolver.rbs,
lib/rbs/resolver/constant_resolver.rb

Overview

Table stores the set of immediate child constants of a module.

table = RBS::ConstantResolver::Table.new(env)

table.children(TypeName.parse("::Object"))                # -> { ... }  Returns a hash of name and constants.
table.children(TypeName.parse("::File::PATH_SEPARATOR"))  # -> nil      Returns nil because the constant is not a module.

table.toplevel                                      # -> { ... }  Returns a hash of top level constants.

The #toplevel is incompatible with Ruby. All constants in Ruby are defined under Object, and they are accessed with :: (Colon3) operator. RBS is different. :: constants are toplevel constants, and they are not defined under ::Object.

The behavior is simulated in ConstantResolver.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment) ⇒ Table

Returns a new instance of Table.

Parameters:



10
11
12
13
14
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rbs/resolver/constant_resolver.rb', line 10

def initialize(environment)
  @children_table = {}
  @toplevel = {}

  @constants_table = {}

  environment.class_decls.each_key do |name|
    children_table[name] = {}
  end

  environment.class_decls.each do |name, entry|
    constant = constant_of_module(name, entry)

    unless name.namespace.empty?
      parent = name.namespace.to_type_name
      table = children_table[parent] or raise "#{parent} not found by #{name}"
    else
      table = toplevel
    end

    table[name.name] = constant
    constants_table[name] = constant
  end

  environment.class_alias_decls.each do |name, entry|
    normalized_entry = environment.module_class_entry(name, normalized: true) or next
    constant = constant_of_module(name, normalized_entry)

    # Insert class/module aliases into `children_table` and `toplevel` table
    unless name.namespace.empty?
      normalized_parent = environment.normalize_module_name?(name.namespace.to_type_name) or raise
      table = children_table[normalized_parent] or raise
      table[name.name] = constant
    else
      toplevel[name.name] = constant
    end
  end

  environment.constant_decls.each do |name, entry|
    unless name.namespace.empty?
      parent = name.namespace.to_type_name

      table = children_table[parent] or raise
      constant = constant_of_constant(name, entry)
    else
      table = toplevel
      constant = constant_of_constant(name, entry)
    end

    table[name.name] = constant
  end
end

Instance Attribute Details

#children_tableHash[TypeName, Hash[Symbol, Constant]?] (readonly)

Returns the value of attribute children_table.

Returns:



7
8
9
# File 'lib/rbs/resolver/constant_resolver.rb', line 7

def children_table
  @children_table
end

#constants_tableHash[TypeName, Constant] (readonly)

Returns the value of attribute constants_table.

Returns:



8
9
10
# File 'lib/rbs/resolver/constant_resolver.rb', line 8

def constants_table
  @constants_table
end

#toplevelHash[Symbol, Constant] (readonly)

Returns the value of attribute toplevel.

Returns:



7
8
9
# File 'lib/rbs/resolver/constant_resolver.rb', line 7

def toplevel
  @toplevel
end

Instance Method Details

#children(name) ⇒ Hash[Symbol, Constant]?

Returns a set of constants defined under module_name. Returns nil if there is no module with given module_name.

Parameters:

Returns:



34
35
36
# File 'sig/resolver/constant_resolver.rbs', line 34

def children(name)
  children_table[name]
end

#constant(name) ⇒ Constant?

Parameters:

Returns:



67
68
69
# File 'lib/rbs/resolver/constant_resolver.rb', line 67

def constant(name)
  constants_table[name]
end

#constant_of_constant(name, entry) ⇒ Constant

Parameters:

Returns:



80
81
82
# File 'lib/rbs/resolver/constant_resolver.rb', line 80

def constant_of_constant(name, entry)
  Constant.new(name: name, type: entry.decl.type, entry: entry)
end

#constant_of_module(name, entry) ⇒ Constant



71
72
73
74
75
76
77
78
# File 'lib/rbs/resolver/constant_resolver.rb', line 71

def constant_of_module(name, entry)
  type = Types::ClassSingleton.new(
    name: name,
    location: nil
  )

  Constant.new(name: name, type: type, entry: entry)
end