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.



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
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
# File 'lib/rubinius/code/ast/definitions.rb', line 307

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
  @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

  @names = names

  self.block_arg = block
end

Instance Attribute Details

#block_argObject

Returns the value of attribute block_arg.



305
306
307
# File 'lib/rubinius/code/ast/definitions.rb', line 305

def block_arg
  @block_arg
end

#block_indexObject (readonly)

Returns the value of attribute block_index.



305
306
307
# File 'lib/rubinius/code/ast/definitions.rb', line 305

def block_index
  @block_index
end

#defaultsObject

Returns the value of attribute defaults.



303
304
305
# File 'lib/rubinius/code/ast/definitions.rb', line 303

def defaults
  @defaults
end

#keywordsObject

Returns the value of attribute keywords.



303
304
305
# File 'lib/rubinius/code/ast/definitions.rb', line 303

def keywords
  @keywords
end

#kwrestObject

Returns the value of attribute kwrest.



303
304
305
# File 'lib/rubinius/code/ast/definitions.rb', line 303

def kwrest
  @kwrest
end

#namesObject

Returns the value of attribute names.



303
304
305
# File 'lib/rubinius/code/ast/definitions.rb', line 303

def names
  @names
end

#optionalObject

Returns the value of attribute optional.



303
304
305
# File 'lib/rubinius/code/ast/definitions.rb', line 303

def optional
  @optional
end

#postObject

Returns the value of attribute post.



303
304
305
# File 'lib/rubinius/code/ast/definitions.rb', line 303

def post
  @post
end

#requiredObject

Returns the value of attribute required.



303
304
305
# File 'lib/rubinius/code/ast/definitions.rb', line 303

def required
  @required
end

#splatObject

Returns the value of attribute splat.



303
304
305
# File 'lib/rubinius/code/ast/definitions.rb', line 303

def splat
  @splat
end

Instance Method Details

#arityObject



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/rubinius/code/ast/definitions.rb', line 437

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 += 1
  end

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

  arity
end

#bytecode(g) ⇒ Object



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/rubinius/code/ast/definitions.rb', line 474

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



392
393
394
# File 'lib/rubinius/code/ast/definitions.rb', line 392

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

#map_arguments(scope) ⇒ Object



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/rubinius/code/ast/definitions.rb', line 457

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



421
422
423
# File 'lib/rubinius/code/ast/definitions.rb', line 421

def post_args
  @post.size
end

#process_fixed_arguments(array, arguments, names) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/rubinius/code/ast/definitions.rb', line 368

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



417
418
419
# File 'lib/rubinius/code/ast/definitions.rb', line 417

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

#splat_indexObject



431
432
433
434
435
# File 'lib/rubinius/code/ast/definitions.rb', line 431

def splat_index
  return @splat_index if @splat_index

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

#to_sexpObject



507
508
509
510
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
543
544
545
546
# File 'lib/rubinius/code/ast/definitions.rb', line 507

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



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

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