Class: Rubinius::ToolSet.current::TS::AST::FormalArguments

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

Direct Known Subclasses

FormalArguments19

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, args, defaults, splat) ⇒ FormalArguments

Returns a new instance of FormalArguments.



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/rubinius/ast/definitions.rb', line 284

def initialize(line, args, defaults, splat)
  @line = line
  @defaults = nil
  @block_arg = nil
  @block_index = nil

  if defaults
    defaults = DefaultArguments.new line, defaults
    @defaults = defaults
    @optional = defaults.names

    stop = defaults.names.first
    last = args.each_with_index { |a, i| break i if a == stop }
    @required = args[0, last]
  else
    @required = args.dup
    @optional = []
  end

  if splat.kind_of? Symbol
    args << splat
  elsif splat
    splat = :*
    args << splat
  end
  @names = args
  @splat = splat
end

Instance Attribute Details

#block_argObject

Returns the value of attribute block_arg.



282
283
284
# File 'lib/rubinius/ast/definitions.rb', line 282

def block_arg
  @block_arg
end

#block_indexObject (readonly)

Returns the value of attribute block_index.



282
283
284
# File 'lib/rubinius/ast/definitions.rb', line 282

def block_index
  @block_index
end

#defaultsObject

Returns the value of attribute defaults.



281
282
283
# File 'lib/rubinius/ast/definitions.rb', line 281

def defaults
  @defaults
end

#namesObject

Returns the value of attribute names.



281
282
283
# File 'lib/rubinius/ast/definitions.rb', line 281

def names
  @names
end

#optionalObject

Returns the value of attribute optional.



281
282
283
# File 'lib/rubinius/ast/definitions.rb', line 281

def optional
  @optional
end

#requiredObject

Returns the value of attribute required.



281
282
283
# File 'lib/rubinius/ast/definitions.rb', line 281

def required
  @required
end

#splatObject

Returns the value of attribute splat.



281
282
283
# File 'lib/rubinius/ast/definitions.rb', line 281

def splat
  @splat
end

Instance Method Details

#bytecode(g) ⇒ Object



320
321
322
323
324
325
326
327
# File 'lib/rubinius/ast/definitions.rb', line 320

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

  @defaults.bytecode(g) if @defaults
  @block_arg.bytecode(g) if @block_arg
  g.state.check_for_locals = true
end

#map_arguments(scope) ⇒ Object



352
353
354
355
356
357
# File 'lib/rubinius/ast/definitions.rb', line 352

def map_arguments(scope)
  @required.each { |arg| scope.new_local arg }
  @defaults.map_arguments scope if @defaults
  scope.new_local @splat if @splat.kind_of? Symbol
  scope.assign_local_reference @block_arg if @block_arg
end

#post_argsObject



335
336
337
# File 'lib/rubinius/ast/definitions.rb', line 335

def post_args
  0
end

#required_argsObject Also known as: arity



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

def required_args
  @required.size
end

#splat_indexObject



343
344
345
346
347
348
349
350
# File 'lib/rubinius/ast/definitions.rb', line 343

def splat_index
  if @splat
    index = @names.size
    index -= 1 if @block_arg
    index -= 1 if @splat.kind_of? Symbol
    index
  end
end

#to_actual(line) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/rubinius/ast/definitions.rb', line 359

def to_actual(line)
  arguments = ActualArguments.new line

  last = -1
  last -= 1 if @block_arg and @block_arg.name == names[last]
  last -= 1 if @splat == names[last]

  arguments.array = @names[0..last].map { |name| LocalVariableAccess.new line, name }

  if @splat.kind_of? Symbol
    arguments.splat = SplatValue.new(line, LocalVariableAccess.new(line, @splat))
  end

  arguments
end

#to_sexpObject



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/rubinius/ast/definitions.rb', line 375

def to_sexp
  sexp = [:args]

  @required.each { |x| sexp << x }
  sexp += @defaults.names if @defaults

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

  sexp += @post if @post

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

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

  sexp
end

#total_argsObject



339
340
341
# File 'lib/rubinius/ast/definitions.rb', line 339

def total_args
  @required.size + @optional.size
end