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.



334
335
336
337
338
339
340
341
342
343
344
345
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
# File 'lib/rubinius/code/ast/definitions.rb', line 334

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.



332
333
334
# File 'lib/rubinius/code/ast/definitions.rb', line 332

def block_arg
  @block_arg
end

#block_indexObject (readonly)

Returns the value of attribute block_index.



332
333
334
# File 'lib/rubinius/code/ast/definitions.rb', line 332

def block_index
  @block_index
end

#defaultsObject

Returns the value of attribute defaults.



330
331
332
# File 'lib/rubinius/code/ast/definitions.rb', line 330

def defaults
  @defaults
end

#keywordsObject

Returns the value of attribute keywords.



330
331
332
# File 'lib/rubinius/code/ast/definitions.rb', line 330

def keywords
  @keywords
end

#kwrestObject

Returns the value of attribute kwrest.



330
331
332
# File 'lib/rubinius/code/ast/definitions.rb', line 330

def kwrest
  @kwrest
end

#kwrest_indexObject (readonly)

Returns the value of attribute kwrest_index.



332
333
334
# File 'lib/rubinius/code/ast/definitions.rb', line 332

def kwrest_index
  @kwrest_index
end

#namesObject

Returns the value of attribute names.



330
331
332
# File 'lib/rubinius/code/ast/definitions.rb', line 330

def names
  @names
end

#optionalObject

Returns the value of attribute optional.



330
331
332
# File 'lib/rubinius/code/ast/definitions.rb', line 330

def optional
  @optional
end

#postObject

Returns the value of attribute post.



330
331
332
# File 'lib/rubinius/code/ast/definitions.rb', line 330

def post
  @post
end

#requiredObject

Returns the value of attribute required.



330
331
332
# File 'lib/rubinius/code/ast/definitions.rb', line 330

def required
  @required
end

#splatObject

Returns the value of attribute splat.



330
331
332
# File 'lib/rubinius/code/ast/definitions.rb', line 330

def splat
  @splat
end

Instance Method Details

#arityObject



467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/rubinius/code/ast/definitions.rb', line 467

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



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/rubinius/code/ast/definitions.rb', line 499

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



425
426
427
# File 'lib/rubinius/code/ast/definitions.rb', line 425

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

#map_arguments(scope) ⇒ Object



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/rubinius/code/ast/definitions.rb', line 482

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



451
452
453
# File 'lib/rubinius/code/ast/definitions.rb', line 451

def post_args
  @post.size
end

#process_fixed_arguments(array, arguments, names) ⇒ Object



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 401

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



447
448
449
# File 'lib/rubinius/code/ast/definitions.rb', line 447

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

#splat_indexObject



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

def splat_index
  return @splat_index if @splat_index

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

#to_sexpObject



532
533
534
535
536
537
538
539
540
541
542
543
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
# File 'lib/rubinius/code/ast/definitions.rb', line 532

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



455
456
457
458
459
# File 'lib/rubinius/code/ast/definitions.rb', line 455

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