Class: Steep::TypeInference::ConstantEnv

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/type_inference/constant_env.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory:, context:) ⇒ ConstantEnv

ConstantEnv receives an Names::Module as a context, not a Namespace, because this is a simulation of Ruby. Any namespace is a module or class.



11
12
13
14
15
16
# File 'lib/steep/type_inference/constant_env.rb', line 11

def initialize(factory:, context:)
  @cache = {}
  @factory = factory
  @context = context
  @table = Ruby::Signature::ConstantTable.new(builder: factory.definition_builder)
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



5
6
7
# File 'lib/steep/type_inference/constant_env.rb', line 5

def cache
  @cache
end

#contextObject (readonly)

Returns the value of attribute context.



4
5
6
# File 'lib/steep/type_inference/constant_env.rb', line 4

def context
  @context
end

#factoryObject (readonly)

Returns the value of attribute factory.



6
7
8
# File 'lib/steep/type_inference/constant_env.rb', line 6

def factory
  @factory
end

#tableObject (readonly)

Returns the value of attribute table.



7
8
9
# File 'lib/steep/type_inference/constant_env.rb', line 7

def table
  @table
end

Instance Method Details

#lookup(name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/steep/type_inference/constant_env.rb', line 26

def lookup(name)
  cache[name] ||= begin
    constant = table.resolve_constant_reference(
      factory.type_name_1(name),
      context: factory.namespace_1(namespace)
    )

    if constant
      factory.type(constant.type)
    end
  rescue => exn
    Steep.logger.error "Looking up a constant failed: name=#{name}, context=#{context}, error=#{exn.inspect}"
    nil
  end
end

#namespaceObject



18
19
20
21
22
23
24
# File 'lib/steep/type_inference/constant_env.rb', line 18

def namespace
  @namespace ||= if context
                   context.namespace.append(context.name)
                 else
                   AST::Namespace.root
                 end
end