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.



346
347
348
349
350
351
352
353
354
355
356
357
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
# File 'lib/rubinius/code/ast/definitions.rb', line 346

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.



344
345
346
# File 'lib/rubinius/code/ast/definitions.rb', line 344

def block_arg
  @block_arg
end

#block_indexObject (readonly)

Returns the value of attribute block_index.



344
345
346
# File 'lib/rubinius/code/ast/definitions.rb', line 344

def block_index
  @block_index
end

#defaultsObject

Returns the value of attribute defaults.



342
343
344
# File 'lib/rubinius/code/ast/definitions.rb', line 342

def defaults
  @defaults
end

#keywordsObject

Returns the value of attribute keywords.



342
343
344
# File 'lib/rubinius/code/ast/definitions.rb', line 342

def keywords
  @keywords
end

#kwrestObject

Returns the value of attribute kwrest.



342
343
344
# File 'lib/rubinius/code/ast/definitions.rb', line 342

def kwrest
  @kwrest
end

#kwrest_indexObject (readonly)

Returns the value of attribute kwrest_index.



344
345
346
# File 'lib/rubinius/code/ast/definitions.rb', line 344

def kwrest_index
  @kwrest_index
end

#namesObject

Returns the value of attribute names.



342
343
344
# File 'lib/rubinius/code/ast/definitions.rb', line 342

def names
  @names
end

#optionalObject

Returns the value of attribute optional.



342
343
344
# File 'lib/rubinius/code/ast/definitions.rb', line 342

def optional
  @optional
end

#postObject

Returns the value of attribute post.



342
343
344
# File 'lib/rubinius/code/ast/definitions.rb', line 342

def post
  @post
end

#requiredObject

Returns the value of attribute required.



342
343
344
# File 'lib/rubinius/code/ast/definitions.rb', line 342

def required
  @required
end

#splatObject

Returns the value of attribute splat.



342
343
344
# File 'lib/rubinius/code/ast/definitions.rb', line 342

def splat
  @splat
end

Instance Method Details

#arityObject



479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/rubinius/code/ast/definitions.rb', line 479

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



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/rubinius/code/ast/definitions.rb', line 511

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



437
438
439
# File 'lib/rubinius/code/ast/definitions.rb', line 437

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

#map_arguments(scope) ⇒ Object



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/rubinius/code/ast/definitions.rb', line 494

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



463
464
465
# File 'lib/rubinius/code/ast/definitions.rb', line 463

def post_args
  @post.size
end

#process_fixed_arguments(array, arguments, names) ⇒ Object



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/rubinius/code/ast/definitions.rb', line 413

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



459
460
461
# File 'lib/rubinius/code/ast/definitions.rb', line 459

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

#splat_indexObject



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

def splat_index
  return @splat_index if @splat_index

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

#to_sexpObject



544
545
546
547
548
549
550
551
552
553
554
555
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
# File 'lib/rubinius/code/ast/definitions.rb', line 544

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



467
468
469
470
471
# File 'lib/rubinius/code/ast/definitions.rb', line 467

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