Class: Laser::Analysis::LaserMethod

Inherits:
Object
  • Object
show all
Extended by:
ModuleExtensions
Defined in:
lib/laser/analysis/bootstrap/laser_method.rb

Overview

Laser representation of a method. This name is tweaked so it doesn't collide with ::Method.

Direct Known Subclasses

SpecialMethods::SendMethod

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from ModuleExtensions

attr_accessor_with_default, cattr_accessor, cattr_accessor_with_default, cattr_get_and_setter, cattr_reader, cattr_writer, opposite_method

Constructor Details

- (LaserMethod) initialize(name, base_proc) {|_self| ... }

Returns a new instance of LaserMethod

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 12

def initialize(name, base_proc)
  @name = name
  @type_instantiations = {}
  @proc = base_proc
  @argument_annotations = nil
  @dispatched = LaserMethod.default_dispatched
  if base_proc  # always true except some ugly test cases
    @arglist = base_proc.arguments
    @arity = Arity.for_arglist(@arglist)
  else
    @arglist = []
    @arity = nil
  end  
  yield self if block_given?
end

Instance Attribute Details

- (Object) arglist (readonly) Also known as: arguments

Returns the value of attribute arglist



8
9
10
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 8

def arglist
  @arglist
end

- (Object) arity

Returns the value of attribute arity



10
11
12
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 10

def arity
  @arity
end

- (Object) name (readonly)

Returns the value of attribute name



8
9
10
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 8

def name
  @name
end

- (Object) owner

Returns the value of attribute owner



8
9
10
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 8

def owner
  @owner
end

- (Object) proc (readonly)

Returns the value of attribute proc



8
9
10
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 8

def proc
  @proc
end

Instance Method Details

- (Boolean) arg_types_unify_with_annotations?(arg_types)

Checks if the given argument types correctly unify with any user-requested restrictions.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 160

def arg_types_unify_with_annotations?(arg_types)
  if argument_annotations.any?
    # Match actual arguments against formal arguments
    formal_assignments = assign_formals(arg_types)
    # Check annotations for formals against actual types
    argument_annotations.each do |note_list|
      note_list.each do |type_annotation|
        expected = type_annotation.type
        given = formal_assignments[type_annotation.name]
        # no given -> optional arg that's left out.
        return false if given && !Types.subtype?(given, expected)
      end
    end
  end
  true
end

- (Object) argument_annotations

Gets all annotations with the same name as an argument.



59
60
61
62
63
64
65
66
67
68
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 59

def argument_annotations
  @argument_annotations ||=
    if @proc
      arguments.map do |arg|
        @proc.annotations[arg.name] if @proc.annotations.has_key?(arg.name)
      end.compact
    else
      []
    end
end

- (Object) assign_formals(actual_objs)

Maps a sequence of objects (one per actual argument) to the corresponding formal argument.



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 240

def assign_formals(actual_objs)
  args = arguments
  result_array = []
  num_required = args.count { |arg| arg.kind == :positional }
  num_optional = actual_objs.size - num_required
  current_arg = 0
  current_actual = 0
  rest = []
  while num_required > 0 || num_optional > 0
    next_arg = args[current_arg]
    if next_arg.kind == :positional
      result_array << [next_arg.name, actual_objs[current_actual]]
      num_required -= 1
      current_actual += 1
      current_arg += 1
    elsif next_arg.kind == :optional && num_optional > 0
      result_array << [next_arg.name, actual_objs[current_actual]]
      num_optional -= 1
      current_actual += 1
      current_arg += 1
    elsif next_arg.kind == :optional && num_optional == 0
      current_arg += 1
    elsif next_arg.kind == :rest && num_optional > 0
      rest << actual_objs[current_actual]
      current_actual += 1
      num_optional -= 1
    elsif next_arg.kind == :rest
      result_array << [next_arg.name, rest]
    end
  end
  Hash[*result_array.flatten(1)]
end

- (Object) been_used!



206
207
208
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 206

def been_used!
  @dispatched = true
end

- (Object) cfg_for_types(self_type, arg_types = [], block_type = nil)



216
217
218
219
220
221
222
223
224
225
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 216

def cfg_for_types(self_type, arg_types = [], block_type = nil)
  block_type ||= Types::NILCLASS
  Laser.debug_puts("Calculating CFG(#{owner.name}##{name}, #{self_type.inspect}, #{arg_types.inspect}, #{block_type.inspect})")
  @type_instantiations[[self_type, *arg_types, block_type]] ||= master_cfg.dup.tap do |cfg|
    cfg.bind_self_type(self_type)
    cfg.bind_formal_types(arg_types)
    cfg.bind_block_type(block_type)
    cfg.analyze(method: self)
  end
end

- (Object) check_return_type_against_expectations(return_type)



177
178
179
180
181
182
183
184
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 177

def check_return_type_against_expectations(return_type)
  if (expectation = Types::EXPECTATIONS[self.name]) &&
      !Types.subtype?(return_type, expectation)
    @proc.ast_node.add_error(ImproperOverrideTypeError.new(
      "All methods named #{self.name} should return a subtype of #{expectation.inspect}",
      @proc.ast_node))
  end
end

- (Object) combined_return_type

Combines all known return types for this method into one union type.



71
72
73
74
75
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 71

def combined_return_type
  Types::UnionType.new(@type_instantiations.keys.map do |self_type, *argtypes, blk_type|
    return_type_for_types(self_type, argtypes, blk_type)
  end)
end

- (Boolean) dispatched?



202
203
204
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 202

def dispatched?
  @dispatched
end

- (Object) dup



273
274
275
276
277
278
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 273

def dup
  result = LaserMethod.new(name, proc)
  result.owner = self.owner
  result.arity = self.arity
  result
end

- (Object) master_cfg



210
211
212
213
214
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 210

def master_cfg
  @master_cfg ||= @proc.ssa_cfg.tap do |cfg|
    cfg.bind_self_type(Types::ClassType.new(owner.path, :covariant))
  end
end

- (Object) overload_for_arg_types(arg_types)

Raises:



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 145

def overload_for_arg_types(arg_types)
  overloads.each do |overload_args, proc_type|
    # compatible arg count
    if overload_args.size == arg_types.size
      # all types unify?
      if overload_args.zip(arg_types).all? { |spec, concrete| Types.subtype?(concrete, spec) }
        return proc_type.subtypes[1]
      end
    end
  end
  raise TypeError.new("No overload found for #{self.inspect} with arg types #{arg_types.inspect}")
end

- (Object) overloads



50
51
52
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 50

def overloads
  @overloads || (self.proc ? self.proc.overloads : {})
end

- (Object) predictable



54
55
56
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 54

def predictable
  @predictable || (self.proc ? self.proc.predictable : true)
end

- (Object) raise_frequency_for_types(self_type, arg_types = [], block_type = nil)



94
95
96
97
98
99
100
101
102
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 94

def raise_frequency_for_types(self_type, arg_types = [], block_type = nil)
  block_type ||= Types::NILCLASS
  unless arg_types_unify_with_annotations?(arg_types)
    return Frequency::ALWAYS
  end
  return annotated_raise_frequency if annotated_raise_frequency
  return Frequency::MAYBE if builtin || special
  cfg_for_types(self_type, arg_types, block_type).raise_frequency
end

- (Object) raise_type_for_types(self_type, arg_types = [], block_type = nil)



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 104

def raise_type_for_types(self_type, arg_types = [], block_type = nil)
  block_type ||= Types::NILCLASS
  Laser.debug_puts("Calculating raise type for #{owner.name}##{name} with types "+
           "#{self_type.inspect} #{arg_types.inspect} #{block_type.inspect}")
  unless arg_types_unify_with_annotations?(arg_types)
    return ClassRegistry['TypeError'].as_type  # needs parameterization later
  end
  if raises
    Laser.debug_puts("Raise type is annotated: #{self.raises.inspect}")
    return self.raises 
  end
  if builtin || special
    if annotated_raise_frequency == Frequency::NEVER
      self.raises = Types::EMPTY
      Laser.debug_puts("Raise type is annotated: #{self.raises.inspect}")
      return self.raises
    end
    Laser.debug_puts("Builtin/special: Types::TOP")
    return Types::TOP
  end
  cfg_for_types(self_type, arg_types, block_type).raise_type
end

- (Object) refine_arity(new_arity)



280
281
282
283
284
285
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 280

def refine_arity(new_arity)
  return new_arity if @arity.nil?
  new_begin = [new_arity.begin, @arity.begin].min
  new_end = [new_arity.end, @arity.end].max
  new_begin..new_end
end

- (Object) return_type_for_types(self_type, arg_types = [], block_type = nil)



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 127

def return_type_for_types(self_type, arg_types = [], block_type = nil)
  block_type ||= Types::NILCLASS
  return annotated_return if annotated_return
  unless overloads.empty?
    return overload_for_arg_types(arg_types)
  end
  if builtin || special
    return Types::TOP
  end
  result = cfg_for_types(self_type, arg_types, block_type).return_type
  check_return_type_against_expectations(result)
  result
end

- (Object) simulate_with_args(new_self, args, block, opts)



227
228
229
230
231
232
233
234
235
236
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 227

def simulate_with_args(new_self, args, block, opts)
  self_type = Utilities.type_for(new_self)
  formal_types = args.map { |arg| Utilities.type_for(arg) }
  block_type = Utilities.type_for(block)
  cfg_for_types(self_type, formal_types, block_type).dup.simulate(
      args, opts.merge(self: new_self,
                     method: self,
                      block: block,
                start_block: @proc.start_block))
end

- (Object) super_method

Returns the method this method overrides: what gets called by `super`.



34
35
36
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 34

def super_method
  owner.parent && owner.parent.instance_method(name)
end

- (Boolean) valid_arity?(num_args)



141
142
143
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 141

def valid_arity?(num_args)
  @arity ? @arity.include?(num_args) : true
end

- (Object) verify_override_safety



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 186

def verify_override_safety
  overridden = super_method
  return unless overridden
  if OverrideSafetyInfo.warn_on_any_override?(overridden)
    @proc.ast_node.add_error(DangerousOverrideError.new(
      OverrideSafetyInfo.warning_message(overridden, name),
      @proc.ast_node))
  elsif OverrideSafetyInfo.needs_super_on_override?(overridden)
    unless master_cfg.guaranteed_super_on_success?
      @proc.ast_node.add_error(OverrideWithoutSuperError.new(
        OverrideSafetyInfo.warning_message(overridden, name),
        @proc.ast_node))
    end
  end
end

- (Object) yield_arity



88
89
90
91
92
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 88

def yield_arity
  return @yield_arity if @yield_arity
  master_cfg.analyze(method: self)
  @yield_arity = master_cfg.yield_arity
end

- (Object) yield_type



77
78
79
80
81
82
83
84
85
86
# File 'lib/laser/analysis/bootstrap/laser_method.rb', line 77

def yield_type
  return annotated_yield_usage if annotated_yield_usage
  return @yield_type if @yield_type
  if builtin
    :ignored
  else
    master_cfg.analyze(method: self)
    @yield_type = master_cfg.yield_type
  end
end