In Files

  • rbs-2.1.0/lib/rbs/environment.rb
  • rbs-2.1.0/lib/rbs/errors.rb

Class/Module Index [+]

Quicksearch

RBS::Environment

Attributes

alias_decls[R]
class_decls[R]
constant_decls[R]
declarations[R]
global_decls[R]
interface_decls[R]

Public Class Methods

from_loader(loader) click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 134
def self.from_loader(loader)
  self.new.tap do |env|
    loader.load(env: env)
  end
end
            
new() click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 112
def initialize
  @buffers = []
  @declarations = []

  @class_decls = {}
  @interface_decls = {}
  @alias_decls = {}
  @constant_decls = {}
  @global_decls = {}
end
            

Public Instance Methods

<<(decl) click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 207
def <<(decl)
  declarations << decl
  insert_decl(decl, outer: [], namespace: Namespace.root)
  self
end
            
absolute_type(resolver, type, context:) click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 460
def absolute_type(resolver, type, context))
  type.map_type_name do |name, _, _|
    absolute_type_name(resolver, name, context: context)
  end
end
            
absolute_type_name(resolver, type_name, context:) click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 456
def absolute_type_name(resolver, type_name, context))
  resolver.resolve(type_name, context: context) || type_name
end
            
buffers() click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 471
def buffers
  buffers_decls.keys.compact
end
            
buffers_decls() click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 475
def buffers_decls
  # @type var hash: Hash[Buffer, Array[AST::Declarations::t]]
  hash = {}

  declarations.each do |decl|
    location = decl.location or next
    (hash[location.buffer] ||= []) << decl
  end

  hash
end
            
cache_name(cache, name:, decl:, outer:) click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 140
def cache_name(cache, name,, decl,, outer))
  if cache.key?(name)
    raise DuplicatedDeclarationError.new(_ = name, _ = decl, _ = cache[name].decl)
  end

  cache[name] = SingleEntry.new(name: name, decl: decl, outer: outer)
end
            
initialize_copy(other) click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 123
def initialize_copy(other)
  @buffers = other.buffers.dup
  @declarations = other.declarations.dup

  @class_decls = other.class_decls.dup
  @interface_decls = other.interface_decls.dup
  @alias_decls = other.alias_decls.dup
  @constant_decls = other.constant_decls.dup
  @global_decls = other.global_decls.dup
end
            
insert_decl(decl, outer:, namespace:) click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 148
def insert_decl(decl, outer,, namespace))
  case decl
  when AST::Declarations::Class, AST::Declarations::Module
    name = decl.name.with_prefix(namespace)

    if constant_decls.key?(name)
      raise DuplicatedDeclarationError.new(name, decl, constant_decls[name].decl)
    end

    unless class_decls.key?(name)
      case decl
      when AST::Declarations::Class
        class_decls[name] ||= ClassEntry.new(name: name)
      when AST::Declarations::Module
        class_decls[name] ||= ModuleEntry.new(name: name)
      end
    end

    existing_entry = class_decls[name]

    case
    when decl.is_a?(AST::Declarations::Module) && existing_entry.is_a?(ModuleEntry)
      # @type var existing_entry: ModuleEntry
      # @type var decl: AST::Declarations::Module
      existing_entry.insert(decl: decl, outer: outer)
    when decl.is_a?(AST::Declarations::Class) && existing_entry.is_a?(ClassEntry)
      # @type var existing_entry: ClassEntry
      # @type var decl: AST::Declarations::Class
      existing_entry.insert(decl: decl, outer: outer)
    else
      raise DuplicatedDeclarationError.new(name, decl, existing_entry.decls[0].decl)
    end

    prefix = outer + [decl]
    ns = name.to_namespace
    decl.each_decl do |d|
      insert_decl(d, outer: prefix, namespace: ns)
    end

  when AST::Declarations::Interface
    cache_name interface_decls, name: decl.name.with_prefix(namespace), decl: decl, outer: outer

  when AST::Declarations::Alias
    cache_name alias_decls, name: decl.name.with_prefix(namespace), decl: decl, outer: outer

  when AST::Declarations::Constant
    name = decl.name.with_prefix(namespace)

    if class_decls.key?(name)
      raise DuplicatedDeclarationError.new(name, decl, class_decls[name].decls[0].decl)
    end

    cache_name constant_decls, name: name, decl: decl, outer: outer

  when AST::Declarations::Global
    cache_name global_decls, name: decl.name, decl: decl, outer: outer
  end
end
            
inspect() click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 466
def inspect
  ivars = %[@declarations @class_decls @interface_decls @alias_decls @constant_decls @global_decls]
  "\#<RBS::Environment #{ivars.map { |iv| "#{iv}=(#{instance_variable_get(iv).size} items)"}.join(' ')}>"
end
            
reject() click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 487
def reject
  env = Environment.new

  declarations.each do |decl|
    unless yield(decl)
      env << decl
    end
  end

  env
end
            
resolve_declaration(resolver, decl, outer:, prefix:) click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 234
def resolve_declaration(resolver, decl, outer,, prefix))
  if decl.is_a?(AST::Declarations::Global)
    # @type var decl: AST::Declarations::Global
    return AST::Declarations::Global.new(
      name: decl.name,
      type: absolute_type(resolver, decl.type, context: [Namespace.root]),
      location: decl.location,
      comment: decl.comment
    )
  end

  context = (outer + [decl]).each.with_object([Namespace.root]) do |decl, array|
    head = array.first or raise
    array.unshift(head + decl.name.to_namespace)
  end

  outer_context = context.drop(1)

  case decl
  when AST::Declarations::Class
    outer_ = outer + [decl]
    prefix_ = prefix + decl.name.to_namespace
    AST::Declarations::Class.new(
      name: decl.name.with_prefix(prefix),
      type_params: resolve_type_params(resolver, decl.type_params, context: context),
      super_class: decl.super_class&.yield_self do |super_class|
        AST::Declarations::Class::Super.new(
          name: absolute_type_name(resolver, super_class.name, context: outer_context),
          args: super_class.args.map {|type| absolute_type(resolver, type, context: outer_context) },
          location: super_class.location
        )
      end,
      members: decl.members.map do |member|
        case member
        when AST::Members::Base
          resolve_member(resolver, member, context: context)
        when AST::Declarations::Base
          resolve_declaration(
            resolver,
            member,
            outer: outer_,
            prefix: prefix_
          )
        else
          raise
        end
      end,
      location: decl.location,
      annotations: decl.annotations,
      comment: decl.comment
    )
  when AST::Declarations::Module
    outer_ = outer + [decl]
    prefix_ = prefix + decl.name.to_namespace
    AST::Declarations::Module.new(
      name: decl.name.with_prefix(prefix),
      type_params: resolve_type_params(resolver, decl.type_params, context: context),
      self_types: decl.self_types.map do |module_self|
        AST::Declarations::Module::Self.new(
          name: absolute_type_name(resolver, module_self.name, context: context),
          args: module_self.args.map {|type| absolute_type(resolver, type, context: context) },
          location: module_self.location
        )
      end,
      members: decl.members.map do |member|
        case member
        when AST::Members::Base
          resolve_member(resolver, member, context: context)
        when AST::Declarations::Base
          resolve_declaration(
            resolver,
            member,
            outer: outer_,
            prefix: prefix_
          )
        else
          raise
        end
      end,
      location: decl.location,
      annotations: decl.annotations,
      comment: decl.comment
    )
  when AST::Declarations::Interface
    AST::Declarations::Interface.new(
      name: decl.name.with_prefix(prefix),
      type_params: resolve_type_params(resolver, decl.type_params, context: context),
      members: decl.members.map do |member|
        resolve_member(resolver, member, context: context)
      end,
      comment: decl.comment,
      location: decl.location,
      annotations: decl.annotations
    )
  when AST::Declarations::Alias
    AST::Declarations::Alias.new(
      name: decl.name.with_prefix(prefix),
      type_params: resolve_type_params(resolver, decl.type_params, context: context),
      type: absolute_type(resolver, decl.type, context: context),
      location: decl.location,
      annotations: decl.annotations,
      comment: decl.comment
    )

  when AST::Declarations::Constant
    AST::Declarations::Constant.new(
      name: decl.name.with_prefix(prefix),
      type: absolute_type(resolver, decl.type, context: context),
      location: decl.location,
      comment: decl.comment
    )
  end
end
            
resolve_member(resolver, member, context:) click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 348
def resolve_member(resolver, member, context))
  case member
  when AST::Members::MethodDefinition
    AST::Members::MethodDefinition.new(
      name: member.name,
      kind: member.kind,
      types: member.types.map do |type|
        resolve_method_type(resolver, type, context: context)
      end,
      comment: member.comment,
      overload: member.overload?,
      annotations: member.annotations,
      location: member.location
    )
  when AST::Members::AttrAccessor
    AST::Members::AttrAccessor.new(
      name: member.name,
      type: absolute_type(resolver, member.type, context: context),
      kind: member.kind,
      annotations: member.annotations,
      comment: member.comment,
      location: member.location,
      ivar_name: member.ivar_name
    )
  when AST::Members::AttrReader
    AST::Members::AttrReader.new(
      name: member.name,
      type: absolute_type(resolver, member.type, context: context),
      kind: member.kind,
      annotations: member.annotations,
      comment: member.comment,
      location: member.location,
      ivar_name: member.ivar_name
    )
  when AST::Members::AttrWriter
    AST::Members::AttrWriter.new(
      name: member.name,
      type: absolute_type(resolver, member.type, context: context),
      kind: member.kind,
      annotations: member.annotations,
      comment: member.comment,
      location: member.location,
      ivar_name: member.ivar_name
    )
  when AST::Members::InstanceVariable
    AST::Members::InstanceVariable.new(
      name: member.name,
      type: absolute_type(resolver, member.type, context: context),
      comment: member.comment,
      location: member.location
    )
  when AST::Members::ClassInstanceVariable
    AST::Members::ClassInstanceVariable.new(
      name: member.name,
      type: absolute_type(resolver, member.type, context: context),
      comment: member.comment,
      location: member.location
    )
  when AST::Members::ClassVariable
    AST::Members::ClassVariable.new(
      name: member.name,
      type: absolute_type(resolver, member.type, context: context),
      comment: member.comment,
      location: member.location
    )
  when AST::Members::Include
    AST::Members::Include.new(
      name: absolute_type_name(resolver, member.name, context: context),
      args: member.args.map {|type| absolute_type(resolver, type, context: context) },
      comment: member.comment,
      location: member.location,
      annotations: member.annotations
    )
  when AST::Members::Extend
    AST::Members::Extend.new(
      name: absolute_type_name(resolver, member.name, context: context),
      args: member.args.map {|type| absolute_type(resolver, type, context: context) },
      comment: member.comment,
      location: member.location,
      annotations: member.annotations
    )
  when AST::Members::Prepend
    AST::Members::Prepend.new(
      name: absolute_type_name(resolver, member.name, context: context),
      args: member.args.map {|type| absolute_type(resolver, type, context: context) },
      comment: member.comment,
      location: member.location,
      annotations: member.annotations
    )
  else
    member
  end
end
            
resolve_method_type(resolver, type, context:) click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 442
def resolve_method_type(resolver, type, context))
  type.map_type do |ty|
    absolute_type(resolver, ty, context: context)
  end.map_type_bound do |bound|
    _ = absolute_type(resolver, bound, context: context)
  end
end
            
resolve_type_names(only: nil) click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 219
def resolve_type_names(only: nil)
  resolver = TypeNameResolver.from_env(self)
  env = Environment.new()

  declarations.each do |decl|
    if only && !only.member?(decl)
      env << decl
    else
      env << resolve_declaration(resolver, decl, outer: [], prefix: Namespace.root)
    end
  end

  env
end
            
resolve_type_params(resolver, params, context:) click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 450
def resolve_type_params(resolver, params, context))
  params.map do |param|
    param.map_type {|type| _ = absolute_type(resolver, type, context: context) }
  end
end
            
validate_type_params() click to toggle source
 
               # File rbs-2.1.0/lib/rbs/environment.rb, line 213
def validate_type_params
  class_decls.each_value do |decl|
    decl.primary
  end
end