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(builder:, context:) ⇒ ConstantEnv

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



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

def initialize(builder:, context:)
  @cache = {}
  @builder = builder
  @context = context
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



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

def builder
  @builder
end

#cacheObject (readonly)

Returns the value of attribute cache.



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

def cache
  @cache
end

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

Instance Method Details

#lookup(name) ⇒ Object



28
29
30
# File 'lib/steep/type_inference/constant_env.rb', line 28

def lookup(name)
  cache[name] ||= lookup0(name, namespace: namespace)
end

#lookup0(name, namespace:) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/steep/type_inference/constant_env.rb', line 33

def lookup0(name, namespace:)
  full_name = name.in_namespace(namespace)
  case
  when signatures.module_name?(full_name)
    AST::Types::Name::Module.new(name: full_name)
  when signatures.class_name?(full_name)
    AST::Types::Name::Class.new(name: full_name, constructor: true)
  when signatures.const_name?(full_name)
    builder.absolute_type(signatures.find_const(name, current_module: namespace).type,
                          current: namespace)
  else
    unless namespace.empty?
      lookup0(name, namespace: namespace.parent)
    end
  end
end

#namespaceObject



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

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

#signaturesObject



16
17
18
# File 'lib/steep/type_inference/constant_env.rb', line 16

def signatures
  builder.signatures
end