Class: Steep::TypeInference::TypeEnv

Inherits:
Object
  • Object
show all
Includes:
NodeHelper
Defined in:
lib/steep/type_inference/type_env.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NodeHelper

#clone_node, #deconstruct_case_node, #deconstruct_case_node!, #deconstruct_if_node, #deconstruct_if_node!, #deconstruct_resbody_node, #deconstruct_resbody_node!, #deconstruct_rescue_node, #deconstruct_rescue_node!, #deconstruct_send_node, #deconstruct_send_node!, #deconstruct_sendish_and_block_nodes, #deconstruct_when_node, #deconstruct_when_node!, #deconstruct_whileish_node, #deconstruct_whileish_node!, #each_child_node, #each_descendant_node, #private_send?, #test_case_node, #test_if_node, #test_resbody_node, #test_rescue_node, #test_send_node, #test_when_node, #test_whileish_node, #value_node?

Constructor Details

#initialize(constant_env, local_variable_types: {}, instance_variable_types: {}, global_types: {}, constant_types: {}, pure_method_calls: {}) ⇒ TypeEnv

Returns a new instance of TypeEnv.



42
43
44
45
46
47
48
49
50
51
# File 'lib/steep/type_inference/type_env.rb', line 42

def initialize(constant_env, local_variable_types: {}, instance_variable_types: {}, global_types: {}, constant_types: {}, pure_method_calls: {})
  @constant_env = constant_env
  @local_variable_types = local_variable_types
  @instance_variable_types = instance_variable_types
  @global_types = global_types
  @constant_types = constant_types
  @pure_method_calls = pure_method_calls

  @pure_node_descendants = {}
end

Instance Attribute Details

#constant_envObject (readonly)

Returns the value of attribute constant_env.



8
9
10
# File 'lib/steep/type_inference/type_env.rb', line 8

def constant_env
  @constant_env
end

#constant_typesObject (readonly)

Returns the value of attribute constant_types.



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

def constant_types
  @constant_types
end

#global_typesObject (readonly)

Returns the value of attribute global_types.



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

def global_types
  @global_types
end

#instance_variable_typesObject (readonly)

Returns the value of attribute instance_variable_types.



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

def instance_variable_types
  @instance_variable_types
end

#local_variable_typesObject (readonly)

Returns the value of attribute local_variable_types.



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

def local_variable_types
  @local_variable_types
end

#pure_method_callsObject (readonly)

Returns the value of attribute pure_method_calls.



9
10
11
# File 'lib/steep/type_inference/type_env.rb', line 9

def pure_method_calls
  @pure_method_calls
end

Instance Method Details

#[](name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/steep/type_inference/type_env.rb', line 81

def [](name)
  case name
  when Symbol
    case
    when local_variable_name?(name)
      local_variable_types[name]&.[](0)
    when instance_variable_name?(name)
      instance_variable_types[name]
    when global_name?(name)
      global_types[name]
    else
      raise "Unexpected variable name: #{name}"
    end
  when Parser::AST::Node
    case name.type
    when :lvar
      self[name.children[0]]
    when :send
      if (call, type = pure_method_calls[name])
        type || call.return_type
      end
    end
  end
end

#add_pure_call(node, call, type) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
# File 'lib/steep/type_inference/type_env.rb', line 260

def add_pure_call(node, call, type)
  if (c, _ = pure_method_calls[node]) && c == call
    return self
  end

  update =
    pure_node_invalidation(invalidated_pure_nodes(node))
      .merge!({ node => [call, type] })

  merge(pure_method_calls: update)
end

#annotated_constant(name) ⇒ Object



172
173
174
# File 'lib/steep/type_inference/type_env.rb', line 172

def annotated_constant(name)
  constant_types[name]
end

#assign_local_variable(name, var_type, enforced_type) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/steep/type_inference/type_env.rb', line 129

def assign_local_variable(name, var_type, enforced_type)
  local_variable_name!(name)
  merge(
    local_variable_types: { name => [enforced_type || var_type, enforced_type] },
    pure_method_calls: pure_node_invalidation(invalidated_pure_nodes(::Parser::AST::Node.new(:lvar, [name])))
  )
end

#assign_local_variables(assignments) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/steep/type_inference/type_env.rb', line 110

def assign_local_variables(assignments)
  local_variable_types = {} #: Hash[Symbol, local_variable_entry]
  invalidated_nodes = Set[]

  assignments.each do |name, new_type|
    local_variable_name!(name)

    local_variable_types[name] = [new_type, enforced_type(name)]
    invalidated_nodes.merge(invalidated_pure_nodes(::Parser::AST::Node.new(:lvar, [name])))
  end

  invalidation = pure_node_invalidation(invalidated_nodes)

  merge(
    local_variable_types: local_variable_types,
    pure_method_calls: invalidation
  )
end

#constant(arg1, arg2) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/steep/type_inference/type_env.rb', line 160

def constant(arg1, arg2)
  if arg1.is_a?(RBS::TypeName) && arg2.is_a?(Symbol)
    constant_env.resolve_child(arg1, arg2)
  elsif arg1.is_a?(Symbol)
    if arg2
      constant_env.toplevel(arg1)
    else
      constant_env.resolve(arg1)
    end
  end
end

#enforced_type(name) ⇒ Object



106
107
108
# File 'lib/steep/type_inference/type_env.rb', line 106

def enforced_type(name)
  local_variable_types[name]&.[](1)
end

#global_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


330
331
332
# File 'lib/steep/type_inference/type_env.rb', line 330

def global_name?(name)
  name.start_with?('$')
end

#inspectObject



334
335
336
337
338
# File 'lib/steep/type_inference/type_env.rb', line 334

def inspect
  s = "#<%s:%#018x " % [self.class, object_id]
  s << instance_variables.map(&:to_s).sort.map {|name| "#{name}=..." }.join(", ")
  s + ">"
end

#instance_variable_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


326
327
328
# File 'lib/steep/type_inference/type_env.rb', line 326

def instance_variable_name?(name)
  name.start_with?(/@[^@]/)
end

#invalidate_pure_node(node) ⇒ Object



282
283
284
# File 'lib/steep/type_inference/type_env.rb', line 282

def invalidate_pure_node(node)
  merge(pure_method_calls: pure_node_invalidation(invalidated_pure_nodes(node)))
end

#invalidated_pure_nodes(invalidated_node) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/steep/type_inference/type_env.rb', line 299

def invalidated_pure_nodes(invalidated_node)
  invalidated_nodes = Set[]

  pure_method_calls.each_key do |pure_node|
    descendants = @pure_node_descendants[pure_node] ||= each_descendant_node(pure_node).to_set
    if descendants.member?(invalidated_node)
      invalidated_nodes << pure_node
    end
  end

  invalidated_nodes
end

#join(*envs) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/steep/type_inference/type_env.rb', line 224

def join(*envs)
  # @type var all_lvar_types: Hash[Symbol, Array[AST::Types::t]]
  all_lvar_types = envs.each_with_object({}) do |env, hash| #$ Hash[Symbol, Array[AST::Types::t]]
    env.local_variable_types.each_key do |name|
      hash[name] = []
    end
  end

  envs.each do |env|
    all_lvar_types.each_key do |name|
      all_lvar_types.fetch(name) << (env[name] || AST::Builtin.nil_type)
    end
  end

  assignments =
    all_lvar_types
      .transform_values {|types| AST::Types::Union.build(types: types) }
      .reject {|var, type| self[var] == type }

  common_pure_nodes = envs
    .map {|env| Set.new(env.pure_method_calls.each_key) }
    .inject {|s1, s2| s1.intersection(s2) } || Set[]

  pure_call_updates = common_pure_nodes.each_with_object({}) do |node, hash| #$ Hash[Parser::AST::Node, [MethodCall::Typed, AST::Types::t]]
    pairs = envs.map {|env| env.pure_method_calls.fetch(node) }
    refined_type = AST::Types::Union.build(types: pairs.map {|call, type| type || call.return_type })

    # Any *pure_method_call* can be used because it's *pure*
    (call, _ = envs.fetch(0).pure_method_calls[node]) or raise

    hash[node] = [call, refined_type]
  end

  assign_local_variables(assignments).merge(pure_method_calls: pure_call_updates)
end

#local_variable_name!(name) ⇒ Object



322
323
324
# File 'lib/steep/type_inference/type_env.rb', line 322

def local_variable_name!(name)
  local_variable_name?(name) || raise("#{name} is not a local variable")
end

#local_variable_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


312
313
314
315
316
317
318
319
320
# File 'lib/steep/type_inference/type_env.rb', line 312

def local_variable_name?(name)
  # Ruby constants start with Uppercase_Letter or Titlecase_Letter in the unicode property.
  # If name start with `@`, it is instance variable or class instance variable.
  # If name start with `$`, it is global variable.
  return false if name.start_with?(/[\p{Uppercase_Letter}\p{Titlecase_Letter}@$]/)
  return false if TypeConstruction::SPECIAL_LVAR_NAMES.include?(name)

  true
end

#merge(local_variable_types: {}, instance_variable_types: {}, global_types: {}, constant_types: {}, pure_method_calls: {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/steep/type_inference/type_env.rb', line 64

def merge(local_variable_types: {}, instance_variable_types: {}, global_types: {}, constant_types: {}, pure_method_calls: {})
  local_variable_types = self.local_variable_types.merge(local_variable_types)
  instance_variable_types = self.instance_variable_types.merge(instance_variable_types)
  global_types = self.global_types.merge(global_types)
  constant_types = self.constant_types.merge(constant_types)
  pure_method_calls = self.pure_method_calls.merge(pure_method_calls)

  TypeEnv.new(
    constant_env,
    local_variable_types: local_variable_types,
    instance_variable_types: instance_variable_types,
    global_types:  global_types,
    constant_types: constant_types,
    pure_method_calls: pure_method_calls
  )
end

#pin_local_variables(names) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/steep/type_inference/type_env.rb', line 176

def pin_local_variables(names)
  names = Set.new(names) if names

  local_variable_types.each.with_object({}) do |pair, hash| #$ Hash[Symbol, [AST::Types::t, AST::Types::t?]]
    name, entry = pair

    local_variable_name!(name)

    if names.nil? || names.include?(name)
      type, enforced_type = entry
      unless enforced_type
        hash[name] = [type, type]
      end
    end
  end
end

#pure_node_invalidation(invalidated_nodes) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/steep/type_inference/type_env.rb', line 286

def pure_node_invalidation(invalidated_nodes)
  # @type var invalidation: Hash[Parser::AST::Node, [MethodCall::Typed, AST::Types::t?]]
  invalidation = {}

  invalidated_nodes.each do |node|
    if (call, _ = pure_method_calls[node])
      invalidation[node] = [call, nil]
    end
  end

  invalidation
end

#refine_types(local_variable_types: {}, pure_call_types: {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/steep/type_inference/type_env.rb', line 137

def refine_types(local_variable_types: {}, pure_call_types: {})
  local_variable_updates = {} #: Hash[Symbol, local_variable_entry]

  local_variable_types.each do |name, type|
    local_variable_name!(name)
    local_variable_updates[name] = [type, enforced_type(name)]
  end

  invalidated_nodes = Set.new(pure_call_types.each_key)
  local_variable_types.each_key do |name|
    invalidated_nodes.merge(invalidated_pure_nodes(Parser::AST::Node.new(:lvar, [name])))
  end

  pure_call_updates = pure_node_invalidation(invalidated_nodes)

  pure_call_types.each do |node, type|
    call, _ = pure_call_updates.fetch(node)
    pure_call_updates[node] = [call, type]
  end

  merge(local_variable_types: local_variable_updates, pure_method_calls: pure_call_updates)
end

#replace_pure_call_type(node, type) ⇒ Object



272
273
274
275
276
277
278
279
280
# File 'lib/steep/type_inference/type_env.rb', line 272

def replace_pure_call_type(node, type)
  if (call, _ = pure_method_calls[node])
    calls = pure_method_calls.dup
    calls[node] = [call, type]
    update(pure_method_calls: calls)
  else
    raise
  end
end

#subst(s) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/steep/type_inference/type_env.rb', line 210

def subst(s)
  update(
    local_variable_types: local_variable_types.transform_values do |entry|
      # @type block: local_variable_entry

      type, enforced_type = entry
      [
        type.subst(s),
        enforced_type&.yield_self {|ty| ty.subst(s) }
      ]
    end
  )
end

#to_sObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/steep/type_inference/type_env.rb', line 11

def to_s
  array = [] #: Array[String]

  local_variable_types.each do |name, entry|
    if enforced_type = entry[1]
      array << "#{name}: #{entry[0].to_s} <#{enforced_type.to_s}>"
    else
      array << "#{name}: #{entry[0].to_s}"
    end
  end

  instance_variable_types.each do |name, type|
    array << "#{name}: #{type.to_s}"
  end

  global_types.each do |name, type|
    array << "#{name}: #{type.to_s}"
  end

  constant_types.each do |name, type|
    array << "#{name}: #{type.to_s}"
  end

  pure_method_calls.each do |node, pair|
    call, type = pair
    array << "`#{node.loc.expression.source.lines[0]}`: #{type || call.return_type}"
  end

  "{ #{array.join(", ")} }"
end

#unpin_local_variables(names) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/steep/type_inference/type_env.rb', line 193

def unpin_local_variables(names)
  names = Set.new(names) if names

  local_var_types = local_variable_types.each.with_object({}) do |pair, hash| #$ Hash[Symbol, [AST::Types::t, AST::Types::t?]]
    name, entry = pair

    local_variable_name!(name)

    if names.nil? || names.include?(name)
      type, _ = entry
      hash[name] = [type, nil]
    end
  end

  merge(local_variable_types: local_var_types)
end

#update(local_variable_types: self.local_variable_types, instance_variable_types: self.instance_variable_types, global_types: self.global_types, constant_types: self.constant_types, pure_method_calls: self.pure_method_calls) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/steep/type_inference/type_env.rb', line 53

def update(local_variable_types: self.local_variable_types, instance_variable_types: self.instance_variable_types, global_types: self.global_types, constant_types: self.constant_types, pure_method_calls: self.pure_method_calls)
  TypeEnv.new(
    constant_env,
    local_variable_types: local_variable_types,
    instance_variable_types: instance_variable_types,
    global_types: global_types,
    constant_types: constant_types,
    pure_method_calls: pure_method_calls
  )
end