Class: Duby::AST::JRubyAst::CallNode

Inherits:
Object
  • Object
show all
Defined in:
lib/duby/transform.rb

Instance Method Summary collapse

Instance Method Details

#annotation(transformer, parent) ⇒ Object



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/duby/transform.rb', line 477

def annotation(transformer, parent)
  if name == '[]'
    annotation = receiver_node.annotation(transformer, parent)
    value = args_node[0]
    case value
    when HashNode
      values = value.list_node
      (0...(values.size / 2)).each do |i|
        name = values.get(2 * i).name
        value = values.get(2 * i + 1)
        annotation[name] = annotation_value(value, annotation)
      end
    when nil
      # ignore an empty argument list
    else
      annotation['value'] = annotation_value(value, annotation)
    end
    annotation
  else
    klass = type_reference(parent)
    Annotation.new(parent, position, klass)
  end
end

#annotation?Boolean

Returns:



501
502
503
504
505
506
507
508
509
# File 'lib/duby/transform.rb', line 501

def annotation?
  if name == '[]'
    receiver = receiver_node
  else
    receiver = self
  end
  receiver = receiver.receiver_node while CallNode === receiver
  GlobalVarNode === receiver
end

#annotation_value(value, annotation) ⇒ Object



511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/duby/transform.rb', line 511

def annotation_value(value, annotation)
  case value
  when StrNode
    java.lang.String.new(value.value)
  when ArrayNode
    value.child_nodes.map {|node| annotation_value(node, annotation)}
  else
    # TODO Support other types
    ref = value.type_reference(annotation)
    desc = BiteScript::Signature.class_id(ref)
    BiteScript::ASM::Type.getType(desc)
  end
end

#transform(transformer, parent) ⇒ Object



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/duby/transform.rb', line 402

def transform(transformer, parent)
  if annotation?
    return transformer.add_annotation(annotation(transformer, parent))
  end

  actual_name = name
  case actual_name
  when '[]'
    # could be array instantiation
    case receiver_node
    when VCallNode
      case receiver_node.name
      when 'boolean', 'byte', 'short', 'char', 'int', 'long', 'float', 'double'
        return EmptyArray.new(parent, position, AST::type(receiver_node.name)) do |array|
          transformer.transform(args_node.get(0), array)
        end
      # TODO look for imported, lower case class names
      end
    when ConstNode
      return EmptyArray.new(parent, position, AST::type(receiver_node.name)) do |array|
        transformer.transform(args_node.get(0), array)
      end
    end
  when /=$/
    if name.size > 2 || name =~ /^\w/
      actual_name = name[0..-2] + '_set'
    end
  end

  Call.new(parent, position, actual_name) do |call|
    [
      transformer.transform(receiver_node, call),
      args_node ? args_node.child_nodes.map {|arg| transformer.transform(arg, call)} : [],
      iter_node ? transformer.transform(iter_node, call) : nil
    ]
  end
end

#type_reference(parent) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/duby/transform.rb', line 440

def type_reference(parent)
  if name == "[]"
    # array type, top should be a constant; find the rest
    array = true
    elements = []
  else
    array = false
    elements = [name]
  end

  receiver = receiver_node

  loop do
    case receiver
    when ConstNode
      elements << receiver_node.name
      break
    when CallNode
      elements.unshift(receiver.name)
      receiver = receiver.receiver_node
    when SymbolNode
      elements.unshift(receiver.name)
      break
    when VCallNode
      elements.unshift(receiver.name)
      break
    when GlobalVarNode
      elements.unshift(receiver.name[1..-1])
      break
    end
  end

  # join and load
  class_name = elements.join(".")
  AST::type(class_name, array)
end