Class: CodeTools::AST::IterArguments

Inherits:
Node
  • Object
show all
Defined in:
lib/rubinius/code/ast/sends.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Node

#ascii_graph, #attributes, #children, #defined, match_arguments?, match_send?, #new_block_generator, #new_generator, #node_name, #or_bytecode, #pos, #set_child, #transform, transform, transform_comment, transform_kind, transform_kind=, transform_name, #value_defined, #visit, #walk

Constructor Details

#initialize(line, arguments) ⇒ IterArguments

Returns a new instance of IterArguments.



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
# File 'lib/rubinius/code/ast/sends.rb', line 649

def initialize(line, arguments)
  @line = line
  @optional = 0
  @arguments = nil

  @splat_index = -1
  @block_index = nil
  @kwrest_index = nil
  @required_args = 0
  @splat = nil
  @block = nil
  @prelude = nil

  case arguments
  when Fixnum
    @splat_index = nil
    @arity = 0
    @prelude = nil
  when MultipleAssignment
    arguments.iter_arguments

    if arguments.splat
      case arguments.splat
      when EmptySplat
        @splat_index = -2
        arguments.splat = nil
        @prelude = :empty
      else
        @splat = arguments.splat = arguments.splat.value
      end

      @optional = 1
      if arguments.left
        @prelude = :multi
        size = arguments.left.body.size
        @arity = -(size + 1)
        @required_args = size
      else
        @prelude = :splat unless @prelude
        @arity = -1
      end
    elsif arguments.left
      size = arguments.left.body.size
      @prelude = :multi
      @arity = size
      @required_args = size

      # distinguish { |a, | ... } from { |a| ... }
      @splat_index = nil unless size == 1
    else
      @splat_index = 0
      @prelude = :multi
      @arity = -1
    end

    @block = arguments.block

    @arguments = arguments
  when nil
    @arity = -1
    @splat_index = -2 # -2 means accept the splat, but don't store it anywhere
    @prelude = nil
  when BlockPass
    @arity = -1
    @splat_index = -2
    @prelude = nil
    @block = arguments
  else # Assignment
    @splat_index = nil
    @arguments = arguments
    @arity = 1
    @required_args = 1
    @prelude = :single
  end
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



646
647
648
# File 'lib/rubinius/code/ast/sends.rb', line 646

def arguments
  @arguments
end

#arityObject

Returns the value of attribute arity.



646
647
648
# File 'lib/rubinius/code/ast/sends.rb', line 646

def arity
  @arity
end

#block_indexObject

Returns the value of attribute block_index.



646
647
648
# File 'lib/rubinius/code/ast/sends.rb', line 646

def block_index
  @block_index
end

#keywordsObject

Returns the value of attribute keywords.



647
648
649
# File 'lib/rubinius/code/ast/sends.rb', line 647

def keywords
  @keywords
end

#kwrest_indexObject

Returns the value of attribute kwrest_index.



647
648
649
# File 'lib/rubinius/code/ast/sends.rb', line 647

def kwrest_index
  @kwrest_index
end

#optionalObject

Returns the value of attribute optional.



646
647
648
# File 'lib/rubinius/code/ast/sends.rb', line 646

def optional
  @optional
end

#preludeObject

Returns the value of attribute prelude.



646
647
648
# File 'lib/rubinius/code/ast/sends.rb', line 646

def prelude
  @prelude
end

#required_argsObject Also known as: total_args

Returns the value of attribute required_args.



647
648
649
# File 'lib/rubinius/code/ast/sends.rb', line 647

def required_args
  @required_args
end

#splat_indexObject

Returns the value of attribute splat_index.



646
647
648
# File 'lib/rubinius/code/ast/sends.rb', line 646

def splat_index
  @splat_index
end

Instance Method Details

#arguments_bytecode(g, is_array = false) ⇒ Object



752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
# File 'lib/rubinius/code/ast/sends.rb', line 752

def arguments_bytecode(g, is_array=false)
  g.state.push_masgn

  if @arguments.kind_of? MultipleAssignment
    @arguments.bytecode(g, is_array)
  else
    @arguments.bytecode(g) if @arguments
  end

  g.state.pop_masgn

  if @splat
    @splat_index = @splat.variable.slot
  end
end

#bytecode(g) ⇒ Object



768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
# File 'lib/rubinius/code/ast/sends.rb', line 768

def bytecode(g)
  case @prelude
  when :single
    g.cast_for_single_block_arg
    arguments_bytecode(g)
    g.pop
  when :multi
    g.cast_for_multi_block_arg
    arguments_bytecode(g, true)
    g.pop
  when :splat
    g.cast_for_splat_block_arg
    arguments_bytecode(g)
    g.pop
  when :empty
    g.cast_for_splat_block_arg
    g.pop
  end

  if @block
    @block.assignment_bytecode(g)
  end
end

#namesObject



731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'lib/rubinius/code/ast/sends.rb', line 731

def names
  case @arguments
  when MultipleAssignment
    if arguments = @arguments.left.body
      array = arguments.map { |x| x.name }
    else
      array = []
    end

    if @arguments.splat.kind_of? SplatAssignment
      array << @arguments.splat.name
    end

    array
  when nil
    []
  else
    [@arguments.name]
  end
end

#post_argsObject



727
728
729
# File 'lib/rubinius/code/ast/sends.rb', line 727

def post_args
  0
end

#to_sexpObject



792
793
794
795
796
797
798
799
800
# File 'lib/rubinius/code/ast/sends.rb', line 792

def to_sexp
  if @arguments
    @arguments.to_sexp
  elsif @arity == 0
    0
  else
    nil
  end
end