123456789_123456789_123456789_123456789_123456789_

Class: RBS::Resolver::ConstantResolver

Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: lib/rbs/resolver/constant_resolver.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(builder:) ⇒ ConstantResolver

[ GitHub ]

  
# File 'lib/rbs/resolver/constant_resolver.rb', line 75

def initialize(builder:)
  @builder = builder
  @table = Table.new(builder.env)
  @context_constants_cache = {}
  @child_constants_cache = {}
end

Instance Attribute Details

#builder (readonly)

[ GitHub ]

  
# File 'lib/rbs/resolver/constant_resolver.rb', line 72

attr_reader :builder, :table

#child_constants_cache (readonly)

[ GitHub ]

  
# File 'lib/rbs/resolver/constant_resolver.rb', line 73

attr_reader :context_constants_cache, :child_constants_cache

#context_constants_cache (readonly)

[ GitHub ]

  
# File 'lib/rbs/resolver/constant_resolver.rb', line 73

attr_reader :context_constants_cache, :child_constants_cache

#table (readonly)

[ GitHub ]

  
# File 'lib/rbs/resolver/constant_resolver.rb', line 72

attr_reader :builder, :table

Instance Method Details

#children(module_name)

[ GitHub ]

  
# File 'lib/rbs/resolver/constant_resolver.rb', line 99

def children(module_name)
  unless child_constants_cache.key?(module_name)
    load_child_constants(module_name)
  end

  child_constants_cache[module_name] or raise
end

#constants(context)

[ GitHub ]

  
# File 'lib/rbs/resolver/constant_resolver.rb', line 87

def constants(context)
  unless context_constants_cache.key?(context)
    load_context_constants(context)
  end

  context_constants_cache[context]
end

#constants_from_ancestors(module_name, constants:)

[ GitHub ]

  
# File 'lib/rbs/resolver/constant_resolver.rb', line 162

def constants_from_ancestors(module_name, constants:)
  entry = builder.env.class_decls[module_name]

  if entry.is_a?(Environment::ModuleEntry)
    constants.merge!(table.children(BuiltinNames::Object.name) || raise)
    constants.merge!(table.toplevel)
  end

  builder.ancestor_builder.instance_ancestors(module_name).ancestors.reverse_each do |ancestor|
    if ancestor.is_a?(Definition::Ancestor::Instance)
      case ancestor.source
      when AST::Members::Include, :super, nil
        consts = table.children(ancestor.name) or raise
        if ancestor.name == BuiltinNames::Object.name && entry.is_a?(Environment::ClassEntry)
          # Insert toplevel constants as ::Object's constants
          consts.merge!(table.toplevel)
        end
        constants.merge!(consts)
      end
    end
  end
end

#constants_from_context(context, constants:)

[ GitHub ]

  
# File 'lib/rbs/resolver/constant_resolver.rb', line 147

def constants_from_context(context, constants:)
  if context
    parent, last = context

    constants_from_context(parent, constants: constants) or return false

    if last
      consts = table.children(last) or return false
      constants.merge!(consts)
    end
  end

  true
end

#constants_itself(context, constants:)

[ GitHub ]

  
# File 'lib/rbs/resolver/constant_resolver.rb', line 185

def constants_itself(context, constants:)
  if context
    _, typename = context

    if typename
      if (ns = typename.namespace).empty?
        constant = table.toplevel[typename.name] or raise
      else
        hash = table.children(ns.to_type_name) or raise
        constant = hash[typename.name]
      end

      constants[typename.name] = constant
    end
  end
end

#load_child_constants(name)

[ GitHub ]

  
# File 'lib/rbs/resolver/constant_resolver.rb', line 122

def load_child_constants(name)
  # @type var constants: Hash[Symbol, Constant]
  constants = {}

  if table.children(name)
    builder.ancestor_builder.instance_ancestors(name).ancestors.reverse_each do |ancestor|
      if ancestor.is_a?(Definition::Ancestor::Instance)
        if ancestor.name == BuiltinNames::Object.name
          if name != BuiltinNames::Object.name
            next
          end
        end

        case ancestor.source
        when AST::Members::Include, :super, nil
          consts = table.children(ancestor.name) or raise
          constants.merge!(consts)
        end
      end
    end
  end

  child_constants_cache[name] = constants
end

#load_context_constants(context)

[ GitHub ]

  
# File 'lib/rbs/resolver/constant_resolver.rb', line 107

def load_context_constants(context)
  # @type var consts: Hash[Symbol, Constant]
  consts = {}

  if last = context&.[](1)
    constants_from_ancestors(last, constants: consts)
  else
    constants_from_ancestors(BuiltinNames::Object.name, constants: consts)
  end
  constants_from_context(context, constants: consts) or return
  constants_itself(context, constants: consts)

  context_constants_cache[context] = consts
end

#resolve(name, context:)

[ GitHub ]

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

def resolve(name, context:)
  cs = constants(context) or raise "Broken context is given"
  cs[name]
end

#resolve_child(module_name, name)

[ GitHub ]

  
# File 'lib/rbs/resolver/constant_resolver.rb', line 95

def resolve_child(module_name, name)
  children(module_name)[name]
end