Class: Yoda::Typing::Inferencer::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/typing/inferencer/tracer.rb

Defined Under Namespace

Classes: MaskedMap

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment:, generator:) ⇒ Tracer

Returns a new instance of Tracer.

Parameters:



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/yoda/typing/inferencer/tracer.rb', line 57

def initialize(environment:, generator:)
  @environment = environment
  @generator = generator

  @node_to_kind = MaskedMap.new
  @node_to_type = MaskedMap.new
  @node_to_context = MaskedMap.new
  @node_to_method_candidates = MaskedMap.new
  @node_to_receiver_type = MaskedMap.new
  @node_to_constants = MaskedMap.new
end

Instance Attribute Details

#environmentModel::Environment (readonly)

Returns:



6
7
8
# File 'lib/yoda/typing/inferencer/tracer.rb', line 6

def environment
  @environment
end

#generatorTypes::Generator (readonly)

Returns:



9
10
11
# File 'lib/yoda/typing/inferencer/tracer.rb', line 9

def generator
  @generator
end

#node_to_constantsHash{ AST::Node => Array<Store::Objects::Base> } (readonly)

Returns:



27
28
29
# File 'lib/yoda/typing/inferencer/tracer.rb', line 27

def node_to_constants
  @node_to_constants
end

#node_to_contextHash{ AST::Node => Context } (readonly)

Returns:



18
19
20
# File 'lib/yoda/typing/inferencer/tracer.rb', line 18

def node_to_context
  @node_to_context
end

#node_to_kindHash{ AST::Node => Symbol } (readonly)

Returns:



12
13
14
# File 'lib/yoda/typing/inferencer/tracer.rb', line 12

def node_to_kind
  @node_to_kind
end

#node_to_method_candidatesHash{ AST::Node => Array<FunctionSignatures::Base> } (readonly)

Returns:

  • (Hash{ AST::Node => Array<FunctionSignatures::Base> })


24
25
26
# File 'lib/yoda/typing/inferencer/tracer.rb', line 24

def node_to_method_candidates
  @node_to_method_candidates
end

#node_to_receiver_typeHash{ AST::Node => Types::Type } (readonly)

Returns:



21
22
23
# File 'lib/yoda/typing/inferencer/tracer.rb', line 21

def node_to_receiver_type
  @node_to_receiver_type
end

#node_to_typeHash{ AST::Node => Types::Base } (readonly)

Returns:



15
16
17
# File 'lib/yoda/typing/inferencer/tracer.rb', line 15

def node_to_type
  @node_to_type
end

Instance Method Details

#bind_constants(node:, constants:) ⇒ Object

Parameters:



111
112
113
# File 'lib/yoda/typing/inferencer/tracer.rb', line 111

def bind_constants(node:, constants:)
  node_to_constants[node.identifier] = constants
end

#bind_context(node:, context:) ⇒ Object

Parameters:



78
79
80
# File 'lib/yoda/typing/inferencer/tracer.rb', line 78

def bind_context(node:, context:)
  node_to_context[node.identifier] = context
end

#bind_local_variable(variable:, type:, context:) ⇒ Object

Parameters:



85
86
87
# File 'lib/yoda/typing/inferencer/tracer.rb', line 85

def bind_local_variable(variable:, type:, context:)
  # nop
end

#bind_method_definition(node:, method_candidates:) ⇒ Object

Parameters:



102
103
104
105
106
107
# File 'lib/yoda/typing/inferencer/tracer.rb', line 102

def bind_method_definition(node:, method_candidates:)
  fail TypeError, method_candidates unless method_candidates.all? { |candidate| candidate.is_a?(Model::FunctionSignatures::Wrapper) }

  node_to_kind[node.identifier] = :send
  node_to_method_candidates[node.identifier] = method_candidates
end

#bind_send(node:, receiver_type:, method_candidates:) ⇒ Object

Parameters:



92
93
94
95
96
97
98
# File 'lib/yoda/typing/inferencer/tracer.rb', line 92

def bind_send(node:, receiver_type:, method_candidates:)
  fail TypeError, method_candidates unless method_candidates.all? { |candidate| candidate.is_a?(Model::FunctionSignatures::Wrapper) }

  node_to_kind[node.identifier] = :send
  node_to_receiver_type[node.identifier] = receiver_type
  node_to_method_candidates[node.identifier] = method_candidates
end

#bind_type(node:, type:, context:) ⇒ Object

Parameters:



72
73
74
# File 'lib/yoda/typing/inferencer/tracer.rb', line 72

def bind_type(node:, type:, context:)
  node_to_type[node.identifier] = type
end

#constants(node) ⇒ Array<Store::Objects::Base>

Parameters:

Returns:



153
154
155
# File 'lib/yoda/typing/inferencer/tracer.rb', line 153

def constants(node)
  node_to_constants[node.identifier] || []
end

#context(node) ⇒ Contexts::BaseContext?

Parameters:

Returns:



159
160
161
# File 'lib/yoda/typing/inferencer/tracer.rb', line 159

def context(node)
  node_to_context[node.identifier]
end

#context_variable_objects(node) ⇒ Hash{ Symbol => Store::Objects::Base }

Parameters:

Returns:



171
172
173
# File 'lib/yoda/typing/inferencer/tracer.rb', line 171

def context_variable_objects(node)
  context(node)&.type_binding&.all_variables&.transform_values { |type| type.value.referred_objects } || {}
end

#context_variable_types(node) ⇒ Hash{ Symbol => Types::Base }

Parameters:

Returns:



165
166
167
# File 'lib/yoda/typing/inferencer/tracer.rb', line 165

def context_variable_types(node)
  context(node)&.type_binding&.all_variables || {}
end

#kind(node) ⇒ Symbol?

Parameters:

Returns:

  • (Symbol, nil)


117
118
119
# File 'lib/yoda/typing/inferencer/tracer.rb', line 117

def kind(node)
  node_to_kind[node.identifier]
end

#method_candidates(node) ⇒ Array<FunctionSignatures::Wrapper>

Parameters:

Returns:

  • (Array<FunctionSignatures::Wrapper>)


147
148
149
# File 'lib/yoda/typing/inferencer/tracer.rb', line 147

def method_candidates(node)
  node_to_method_candidates[node.identifier] || []
end

#node_info(node) ⇒ NodeInfo

Parameters:

Returns:



129
130
131
# File 'lib/yoda/typing/inferencer/tracer.rb', line 129

def node_info(node)
  NodeInfo.new(node: node, tracer: self)
end

#objects(node) ⇒ Array<Store::Objects::Base>

Parameters:

Returns:



135
136
137
# File 'lib/yoda/typing/inferencer/tracer.rb', line 135

def objects(node)
  type(node).value.referred_objects
end

#receiver_type(node) ⇒ Types::Type

Parameters:

Returns:



141
142
143
# File 'lib/yoda/typing/inferencer/tracer.rb', line 141

def receiver_type(node)
  node_to_receiver_type[node.identifier] || generator.unknown_type(reason: "not traced")
end

#type(node) ⇒ Types::Type

Parameters:

Returns:



123
124
125
# File 'lib/yoda/typing/inferencer/tracer.rb', line 123

def type(node)
  node_to_type[node.identifier] || generator.unknown_type(reason: "not traced")
end