Class: Solargraph::Source::Chain
- Inherits:
-
Object
- Object
- Solargraph::Source::Chain
show all
- Includes:
- Equality, Logging
- Defined in:
- lib/solargraph/source/chain.rb,
lib/solargraph/source/chain/if.rb,
lib/solargraph/source/chain/or.rb,
lib/solargraph/source/chain/call.rb,
lib/solargraph/source/chain/hash.rb,
lib/solargraph/source/chain/head.rb,
lib/solargraph/source/chain/link.rb,
lib/solargraph/source/chain/array.rb,
lib/solargraph/source/chain/q_call.rb,
lib/solargraph/source/chain/literal.rb,
lib/solargraph/source/chain/z_super.rb,
lib/solargraph/source/chain/constant.rb,
lib/solargraph/source/chain/variable.rb,
lib/solargraph/source/chain/block_symbol.rb,
lib/solargraph/source/chain/block_variable.rb,
lib/solargraph/source/chain/class_variable.rb,
lib/solargraph/source/chain/global_variable.rb,
lib/solargraph/source/chain/instance_variable.rb
Overview
Represents an expression as a single call chain at the parse tree level, made up of constants, variables, and method calls, each represented as a Link object.
Computes Pins and/or ComplexTypes representing the interpreted expression.
Defined Under Namespace
Classes: Array, BlockSymbol, BlockVariable, Call, ClassVariable, Constant, GlobalVariable, Hash, Head, If, InstanceVariable, Link, Literal, Or, QCall, Variable, ZSuper
Constant Summary
collapse
- UNDEFINED_CALL =
Chain::Call.new('<undefined>', nil)
- UNDEFINED_CONSTANT =
Chain::Constant.new('<undefined>')
- @@inference_stack =
[]
- @@inference_depth =
0
- @@inference_invalidation_key =
nil
- @@inference_cache =
{}
Constants included
from Logging
Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Logging
logger
Methods included from Equality
#==, #eql?, #freeze, #hash
Constructor Details
#initialize(links, node = nil, splat = false) ⇒ Chain
Returns a new instance of Chain.
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/solargraph/source/chain.rb', line 59
def initialize links, node = nil, splat = false
@links = links.clone
@links.push UNDEFINED_CALL if @links.empty?
head = true
@links.map! do |link|
result = (head ? link.clone_head : link.clone_body)
head = false
result
end
@node = node
@splat = splat
end
|
Instance Attribute Details
47
48
49
|
# File 'lib/solargraph/source/chain.rb', line 47
def links
@links
end
|
#node ⇒ Object
Returns the value of attribute node.
49
50
51
|
# File 'lib/solargraph/source/chain.rb', line 49
def node
@node
end
|
Instance Method Details
73
74
75
|
# File 'lib/solargraph/source/chain.rb', line 73
def base
@base ||= Chain.new(links[0..-2])
end
|
#constant? ⇒ Boolean
184
185
186
|
# File 'lib/solargraph/source/chain.rb', line 184
def constant?
links.last.is_a?(Chain::Constant)
end
|
#define(api_map, name_pin, locals) ⇒ ::Array<Pin::Base>
Determine potential Pins returned by this chain of words
representing the place in which expression is evaluated (e.g., a Method pin, or a Module or Class pin if not run within a method - both in terms of the closure around the chain, as well as the self type used for any method calls in head position.
Requirements for name_pin:
* name_pin.context: This should be a type representing the
namespace where we can look up non-local variables and
method names. If it is a Class<X>, we will look up
:class scoped methods/variables.
* name_pin.binder: Used for method call lookups only
(Chain::Call links). For method calls as the first
element in the chain, 'name_pin.binder' should be the
same as name_pin.context above. For method calls later
in the chain (e.g., 'b' in a.b.c), it should represent
'a'.
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/solargraph/source/chain.rb', line 106
def define api_map, name_pin, locals
return [] if undefined?
working_pin = name_pin
links[0..-2].each do |link|
pins = link.resolve(api_map, working_pin, locals)
type = infer_from_definitions(pins, working_pin, api_map, locals)
if type.undefined?
logger.debug { "Chain#define(links=#{links.map(&:desc)}, name_pin=#{name_pin.inspect}, locals=#{locals}) => [] - undefined type from #{link.desc}" }
return []
end
working_pin = Pin::ProxyType.anonymous(name_pin.context, binder: type, closure: name_pin, source: :chain)
logger.debug { "Chain#define(links=#{links.map(&:desc)}, name_pin=#{name_pin.inspect}, locals=#{locals}) - after processing #{link.desc}, new working_pin=#{working_pin} with binder #{working_pin.binder}" }
end
links.last.last_context = working_pin
links.last.resolve(api_map, working_pin, locals)
end
|
#defined? ⇒ Boolean
179
180
181
|
# File 'lib/solargraph/source/chain.rb', line 179
def defined?
!undefined?
end
|
#desc ⇒ String
199
200
201
|
# File 'lib/solargraph/source/chain.rb', line 199
def desc
links.map(&:desc).to_s
end
|
#infer(api_map, name_pin, locals) ⇒ ComplexType
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/solargraph/source/chain.rb', line 140
def infer api_map, name_pin, locals
cache_key = [node, node&.location, links, name_pin&.return_type, locals]
if @@inference_invalidation_key == api_map.hash
cached = @@inference_cache[cache_key]
return cached if cached
else
@@inference_invalidation_key = api_map.hash
@@inference_cache = {}
end
out = infer_uncached(api_map, name_pin, locals).downcast_to_literal_if_possible
logger.debug { "Chain#infer() - caching result - cache_key_hash=#{cache_key.hash}, links.map(&:hash)=#{links.map(&:hash)}, links=#{links}, cache_key.map(&:hash) = #{cache_key.map(&:hash)}, cache_key=#{cache_key}" }
@@inference_cache[cache_key] = out
end
|
#infer_uncached(api_map, name_pin, locals) ⇒ ComplexType
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/solargraph/source/chain.rb', line 158
def infer_uncached api_map, name_pin, locals
pins = define(api_map, name_pin, locals)
if pins.empty?
logger.debug { "Chain#infer_uncached(links=#{links.map(&:desc)}, locals=#{locals.map(&:desc)}) => undefined - no pins" }
return ComplexType::UNDEFINED
end
type = infer_from_definitions(pins, links.last.last_context, api_map, locals)
out = maybe_nil(type)
logger.debug { "Chain#infer_uncached(links=#{self.links.map(&:desc)}, locals=#{locals.map(&:desc)}, name_pin=#{name_pin}, name_pin.closure=#{name_pin.closure.inspect}, name_pin.binder=#{name_pin.binder}) => #{out.rooted_tags.inspect}" }
out
end
|
#literal? ⇒ Boolean
171
172
173
|
# File 'lib/solargraph/source/chain.rb', line 171
def literal?
links.last.is_a?(Chain::Literal)
end
|
#nullable? ⇒ Boolean
192
193
194
|
# File 'lib/solargraph/source/chain.rb', line 192
def nullable?
links.any?(&:nullable?)
end
|
#splat? ⇒ Boolean
188
189
190
|
# File 'lib/solargraph/source/chain.rb', line 188
def splat?
@splat
end
|
#to_s ⇒ Object
203
204
205
|
# File 'lib/solargraph/source/chain.rb', line 203
def to_s
desc
end
|
#undefined? ⇒ Boolean
175
176
177
|
# File 'lib/solargraph/source/chain.rb', line 175
def undefined?
links.any?(&:undefined?)
end
|