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:, current_namespace:) ⇒ ConstantEnv

Returns a new instance of ConstantEnv.



8
9
10
11
12
# File 'lib/steep/type_inference/constant_env.rb', line 8

def initialize(builder:, current_namespace:)
  @cache = {}
  @builder = builder
  @current_namespace = current_namespace
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

#current_namespaceObject (readonly)

Returns the value of attribute current_namespace.



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

def current_namespace
  @current_namespace
end

Instance Method Details

#lookup(name) ⇒ Object



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

def lookup(name)
  unless cache.key?(name)
    cache[name] = lookup0(name, namespace: current_namespace)
  end

  cache[name]
end

#lookup0(name, namespace:) ⇒ Object



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
# File 'lib/steep/type_inference/constant_env.rb', line 26

def lookup0(name, namespace:)
  if name.absolute?
    case
    when signatures.module_name?(name)
      AST::Types::Name.new_module(name: name)
    when signatures.class_name?(name)
      AST::Types::Name.new_class(name: name, constructor: true)
    when signatures.const_name?(name)
      builder.absolute_type(signatures.find_const(name).type, current: nil)
    end
  else
    if namespace
      case
      when signatures.module_name?(name, current_module: namespace)
        AST::Types::Name.new_module(name: namespace + name)
      when signatures.class_name?(name, current_module: namespace)
        AST::Types::Name.new_class(name: namespace + name, constructor: true)
      when signatures.const_name?(name, current_module: namespace)
        builder.absolute_type(signatures.find_const(name, current_module: namespace).type, current: nil)
      else
        lookup0(name, namespace: namespace.parent)
      end
    else
      lookup0(name.absolute!, namespace: nil)
    end
  end
end

#signaturesObject



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

def signatures
  builder.signatures
end