Class: Audition::Rewriters::Memoization::SingletonIvars

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/audition/rewriters.rb

Overview

Collects ivar operations that touch the class object: inside def self.x, inside class << self methods, or directly in the class body (recorded with body: true, which disqualifies the group). Instance-method ivars are ignored.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSingletonIvars

Returns a new instance of SingletonIvars.



422
423
424
425
426
427
428
# File 'lib/audition/rewriters.rb', line 422

def initialize
  @groups = Hash.new { |h, k| h[k] = [] }
  @namespace = []
  @sclass_depth = 0
  @def_stack = []
  super
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



420
421
422
# File 'lib/audition/rewriters.rb', line 420

def groups
  @groups
end

Instance Method Details

#visit_class_node(node) ⇒ Object



430
431
432
433
434
435
# File 'lib/audition/rewriters.rb', line 430

def visit_class_node(node)
  @namespace.push(node.constant_path.location.slice)
  super
ensure
  @namespace.pop
end

#visit_def_node(node) ⇒ Object



457
458
459
460
461
462
463
464
465
466
# File 'lib/audition/rewriters.rb', line 457

def visit_def_node(node)
  kind =
    node.receiver.is_a?(Prism::SelfNode) ? :self : :plain
  @def_stack.push(
    {kind: kind, id: node.object_id, name: node.name}
  )
  super
ensure
  @def_stack.pop
end

#visit_if_node(node) ⇒ Object

Matches the guard statement of the second memoization idiom: return @x if defined?(@x). On a match the inner reads are not visited (they belong to the guard, not to the data flow) and the whole statement is recorded so the planner can delete it.



473
474
475
476
477
478
479
480
# File 'lib/audition/rewriters.rb', line 473

def visit_if_node(node)
  name = guard_name(node)
  if name
    record(node, :guard, name)
  else
    super
  end
end

#visit_module_node(node) ⇒ Object



437
438
439
440
441
442
# File 'lib/audition/rewriters.rb', line 437

def visit_module_node(node)
  @namespace.push(node.constant_path.location.slice)
  super
ensure
  @namespace.pop
end

#visit_singleton_class_node(node) ⇒ Object



444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/audition/rewriters.rb', line 444

def visit_singleton_class_node(node)
  if node.expression.is_a?(Prism::SelfNode)
    @sclass_depth += 1
    begin
      super
    ensure
      @sclass_depth -= 1
    end
  else
    super
  end
end