Class: CodeTools::AST::Parameters

Inherits:
Node
  • Object
show all
Defined in:
lib/rubinius/code/ast/definitions.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, required = nil, optional = nil, splat = nil, post = nil, kwargs = nil, kwrest = nil, block = nil) ⇒ Parameters

Returns a new instance of Parameters.



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/rubinius/code/ast/definitions.rb', line 358

def initialize(line, required=nil, optional=nil, splat=nil,
               post=nil, kwargs=nil, kwrest=nil, block=nil)
  @line = line
  @defaults = nil
  @keywords = nil
  @block_arg = nil
  @splat_index = nil
  @block_index = nil
  @kwrest_index = nil
  @locals = []
  @local_index = 0

  names = []

  process_fixed_arguments Array(required), @required = [], names

  if optional
    @defaults = DefaultArguments.new line, optional
    @optional = @defaults.names
    names.concat @optional
    @locals.concat @defaults.arguments
  else
    @optional = []
  end

  case splat
  when Symbol
    names << splat
    @locals << splat
  when true
    splat = :*
    @locals << splat
  when false
    splat = nil
    @splat_index = -3
  end

  @splat = splat

  process_fixed_arguments post, @post = [], names

  if kwargs || kwrest
    placeholder_var = local_placeholder
    @locals << placeholder_var
  end

  kwrest = placeholder_var if kwrest == true

  if kwargs
    @keywords = KeywordParameters.new line, kwargs, kwrest
    names.concat @keywords.names
  elsif kwrest
    @keywords = KeywordParameters.new line, nil, kwrest
  end

  @keywords.value = LocalVariableAccess.new line, placeholder_var if @keywords

  if kwrest
    @kwrest_index = total_args + @keywords.arguments.length + 1
    @kwrest_index += 1 if block
  end

  @names = names

  self.block_arg = block
end

Instance Attribute Details

#block_argObject

Returns the value of attribute block_arg.



356
357
358
# File 'lib/rubinius/code/ast/definitions.rb', line 356

def block_arg
  @block_arg
end

#block_indexObject (readonly)

Returns the value of attribute block_index.



356
357
358
# File 'lib/rubinius/code/ast/definitions.rb', line 356

def block_index
  @block_index
end

#defaultsObject

Returns the value of attribute defaults.



354
355
356
# File 'lib/rubinius/code/ast/definitions.rb', line 354

def defaults
  @defaults
end

#keywordsObject

Returns the value of attribute keywords.



354
355
356
# File 'lib/rubinius/code/ast/definitions.rb', line 354

def keywords
  @keywords
end

#kwrestObject

Returns the value of attribute kwrest.



354
355
356
# File 'lib/rubinius/code/ast/definitions.rb', line 354

def kwrest
  @kwrest
end

#kwrest_indexObject (readonly)

Returns the value of attribute kwrest_index.



356
357
358
# File 'lib/rubinius/code/ast/definitions.rb', line 356

def kwrest_index
  @kwrest_index
end

#namesObject

Returns the value of attribute names.



354
355
356
# File 'lib/rubinius/code/ast/definitions.rb', line 354

def names
  @names
end

#optionalObject

Returns the value of attribute optional.



354
355
356
# File 'lib/rubinius/code/ast/definitions.rb', line 354

def optional
  @optional
end

#postObject

Returns the value of attribute post.



354
355
356
# File 'lib/rubinius/code/ast/definitions.rb', line 354

def post
  @post
end

#requiredObject

Returns the value of attribute required.



354
355
356
# File 'lib/rubinius/code/ast/definitions.rb', line 354

def required
  @required
end

#splatObject

Returns the value of attribute splat.



354
355
356
# File 'lib/rubinius/code/ast/definitions.rb', line 354

def splat
  @splat
end

Instance Method Details

#arityObject



491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/rubinius/code/ast/definitions.rb', line 491

def arity
  arity = required_args

  if @keywords and @keywords.required?
    arity += 1
  end

  if @splat or not @optional.empty? or
      (@keywords and not @keywords.required?)
    arity = -arity - 1
  end

  arity
end

#bytecode(g) ⇒ Object



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/rubinius/code/ast/definitions.rb', line 523

def bytecode(g)
  g.state.check_for_locals = false
  map_arguments g.state.scope

  @required.each_with_index do |arg, index|
    if arg.kind_of? MultipleAssignment
      g.push_local index
      arg.bytecode(g)
      g.pop
    end
  end

  @defaults.bytecode(g) if @defaults

  index = @required.size + @optional.size
  index += 1 if @splat_index

  @post.each do |arg|
    if arg.kind_of? MultipleAssignment
      g.push_local index
      index += 1
      arg.bytecode(g)
      g.pop
    end
  end

  @keywords.bytecode(g) if @keywords

  @block_arg.bytecode(g) if @block_arg

  g.state.check_for_locals = true
end

#local_placeholderObject



449
450
451
# File 'lib/rubinius/code/ast/definitions.rb', line 449

def local_placeholder
  :"_:#{@local_index += 1}"
end

#map_arguments(scope) ⇒ Object



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/rubinius/code/ast/definitions.rb', line 506

def map_arguments(scope)
  @locals.map do |v|
    case v
    when Symbol
      scope.new_local v
    when MultipleAssignment
      scope.assign_local_reference v.right
    when LocalVariable
      scope.assign_local_reference v
    when nil
      STDERR.puts @locals.inspect, self.inspect
    end
  end

  @keywords.map_arguments(scope) if @keywords
end

#post_argsObject



475
476
477
# File 'lib/rubinius/code/ast/definitions.rb', line 475

def post_args
  @post.size
end

#process_fixed_arguments(array, arguments, names) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/rubinius/code/ast/definitions.rb', line 425

def process_fixed_arguments(array, arguments, names)
  if array
    array.each do |arg|
      case arg
      when :_
        var = names.include?(:_) ? local_placeholder : arg
        names << arg
      when Symbol
        var = arg
        names << arg
      when MultipleAssignment
        var = arg
        var.right = LocalVariableAccess.new line, local_placeholder
      when VariableAssignment
        var = arg
        names << var.name
      end

      arguments << var
      @locals << var
    end
  end
end

#required_argsObject



471
472
473
# File 'lib/rubinius/code/ast/definitions.rb', line 471

def required_args
  @required.size + @post.size
end

#splat_indexObject



485
486
487
488
489
# File 'lib/rubinius/code/ast/definitions.rb', line 485

def splat_index
  return @splat_index if @splat_index

  return @required.size + @optional.size if @splat
end

#to_sexpObject



556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/rubinius/code/ast/definitions.rb', line 556

def to_sexp
  sexp = [:args]

  @required.each do |a|
    case a
    when Symbol
      sexp << a
    when Node
      sexp << a.to_sexp
    end
  end

  sexp += @defaults.names if @defaults

  if @splat == :*
    sexp << :*
  elsif @splat
    sexp << :"*#{@splat}"
  end

  if @post
    @post.each do |a|
      case a
      when Symbol
        sexp << a
      when Node
        sexp << a.to_sexp
      end
    end
  end

  sexp += @keywords.names if @keywords

  sexp << :"&#{@block_arg.name}" if @block_arg

  sexp << [:block] + @defaults.to_sexp if @defaults
  sexp << @keywords.to_sexp if @keywords

  sexp
end

#total_argsObject



479
480
481
482
483
# File 'lib/rubinius/code/ast/definitions.rb', line 479

def total_args
  args = @required.size + @optional.size + @post.size
  args += 1 if @keywords
  args
end