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
log_level, logger
Methods included from Equality
#==, #eql?, #freeze, #hash
Constructor Details
#initialize(links, node = nil, splat = false) ⇒ Chain
Returns a new instance of Chain.
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/solargraph/source/chain.rb', line 54
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
68
69
70
71
|
# File 'lib/solargraph/source/chain.rb', line 68
def base
@base ||= Chain.new(links[0..-2])
end
|
#constant? ⇒ Boolean
194
195
196
|
# File 'lib/solargraph/source/chain.rb', line 194
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
102
103
104
105
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
134
|
# File 'lib/solargraph/source/chain.rb', line 102
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 do
"Chain#define(links=#{links.map(&:desc)}, name_pin=#{name_pin.inspect}, locals=#{locals}) => [] - undefined type from #{link.desc}"
end
return []
end
working_pin = Pin::ProxyType.anonymous(name_pin.context, binder: type, closure: name_pin, source: :chain)
logger.debug do
"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
end
links.last.last_context = working_pin
links.last.resolve(api_map, working_pin, locals)
end
|
#defined? ⇒ Boolean
189
190
191
|
# File 'lib/solargraph/source/chain.rb', line 189
def defined?
!undefined?
end
|
#desc ⇒ String
209
210
211
|
# File 'lib/solargraph/source/chain.rb', line 209
def desc
links.map(&:desc).to_s
end
|
#infer(api_map, name_pin, locals) ⇒ ComplexType
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/solargraph/source/chain.rb', line 141
def infer api_map, name_pin, locals
cache_key = [node, node&.location, links, name_pin&.return_type, name_pin&.binder, 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 do
"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}"
end
@@inference_cache[cache_key] = out
end
|
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/solargraph/source/chain.rb', line 162
def infer_uncached api_map, name_pin, locals
pins = define(api_map, name_pin, locals)
if pins.empty?
logger.debug do
"Chain#infer_uncached(links=#{links.map(&:desc)}, locals=#{locals.map(&:desc)}) => undefined - no pins"
end
return ComplexType::UNDEFINED
end
type = infer_from_definitions(pins, links.last.last_context, api_map, locals)
out = maybe_nil(type)
logger.debug do
"Chain#infer_uncached(links=#{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}"
end
out
end
|
#literal? ⇒ Boolean
181
182
183
|
# File 'lib/solargraph/source/chain.rb', line 181
def literal?
links.last.is_a?(Chain::Literal)
end
|
#nullable? ⇒ Boolean
202
203
204
|
# File 'lib/solargraph/source/chain.rb', line 202
def nullable?
links.any?(&:nullable?)
end
|
#splat? ⇒ Boolean
198
199
200
|
# File 'lib/solargraph/source/chain.rb', line 198
def splat?
@splat
end
|
#to_s ⇒ Object
213
214
215
|
# File 'lib/solargraph/source/chain.rb', line 213
def to_s
desc
end
|
#undefined? ⇒ Boolean
185
186
187
|
# File 'lib/solargraph/source/chain.rb', line 185
def undefined?
links.any?(&:undefined?)
end
|