class Objc2swiftAssistant::ObjC2SwiftMethodConverter

Attributes

access_control_state[RW]
class_converter[RW]
declaration_region[RW]
implementation_region[RW]
is_initializer[RW]
objc_signature[RW]
owning_property[RW]
parameters[RW]
protocol_optional_state[RW]
swift_name[RW]

Public Class Methods

new( class_converter, objc_signature, configuration ) click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 504
def initialize( class_converter, objc_signature, configuration )
  super( class_converter.file_converter, configuration )

  @class_converter = class_converter
  @objc_signature = objc_signature
  @is_initializer = false
  @parameters = nil
  @access_control_state = nil
  @protocol_optional_state = nil
end

Public Instance Methods

arg_list( configuration ) click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 586
def arg_list( configuration )
  param_strings = @parameters.map do |parameter|
    arg_string( parameter, configuration )
  end

  param_strings.join( ", " )
end
arg_string( param, configuration ) click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 594
def arg_string( param, configuration )
  if param.is_block_type
    block_signature = configuration.block_converter.block_sig_for_method_arg( param.param_type )
    type_string = block_signature.swift_declaration
  else
    type_string = swift_type_string( configuration, param.param_type, param.pointer_level, param.null_qualifier, param.is_weak )
  end

  if param.param_name.nil? || param.param_name == param.param_label
    sig = param.param_label
  else
    sig = "#{param.param_label} #{param.param_name}"
  end

  sig ||= '/* Unknown */'
  sig += ": #{type_string}"
  sig
end
body_lines_to_emit() click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 649
def body_lines_to_emit()
  if @implementation_region.nil?
    return []
  end

  lines = []
  if emit_original_bodies
    if emit_original_signatures
      lines = implementation_region.all_lines
    else
      lines = implementation_region.unmatched_lines
    end
  end

  return  Objc2swiftAssistant::prepare_method_body_lines( lines )
end
compute_swift_name() click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 515
def compute_swift_name
  #Copy the parameters from the method region
  @parameters = Marshal.load(Marshal.dump(@declaration_region.parameters)) unless @declaration_region.nil?
  @parameters= Marshal.load(Marshal.dump(@implementation_region.parameters)) if @parameters.nil? unless @implementation_region.nil?
  split_first_param_name
end
generate( generated_class, configuration, dry_run ) click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 554
def generate( generated_class, configuration, dry_run )
  return unless @owning_property.nil?

  # type_symbol = @declaration_region.type_name.to_sym
  # mutability = @declaration_region.is_pointer ? :var : :let

  method_region = @declaration_region || @implementation_region

  if @is_initializer
    generated_method = SwiftGenerator::SwiftInitializer.new( generated_class, @swift_name, arg_list( configuration ), override: false, comment: nil )
  else
    returns_str = swift_type_string( configuration, method_region.return_type, method_region.return_pointer_level, method_region.return_nillable_qualifier )
    # returns = configuration.type_mapper.swift_type_for_objc_type( returns_str )
    # TODO: handle block returns
    returns = nil if returns == 'void'
    generated_method = SwiftGenerator::SwiftMethod.new( generated_class, @swift_name, arg_list( configuration ), @is_initializer ? nil : returns, override: false, comment: nil )
  end

  generated_method.access_control_modifiers = class_converter.make_modifiers( access_control:@access_control_state,
                                                                             optional:@protocol_optional_state )
  generated_method << body_lines_to_emit

  unless method_region.return_type.nil?

    # TODO: Dummy Return Statements
    # @return_nillable_qualifier = ''    # Not yet implemented
    # @return_type = m[ 'return_type' ]
    # @return_pointer_level = m[ 'return_pointer' ].nil? ? 0 : m[ 'return_pointer' ].length

  end
end
nullable_qualifier( null_qualifier, pointer_level ) click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 630
def nullable_qualifier( null_qualifier, pointer_level )
  pointer_chars = ''
  if( pointer_level > 1)
    pointer_chars = '**********'[0..pointer_level]    # Almost sure to be incorrect, but reflects original code
  end

  if null_qualifier == '__nonnull' && pointer_level > 0
    return pointer_chars + '!'
  elsif null_qualifier ==  '__nullable'
    return pointer_chars + '?'
  elsif null_qualifier ==  '__null_unspecified'
    return pointer_chars + '!'
  elsif pointer_level > 0
    return pointer_chars + '!'
  else
    return pointer_chars
  end
end
prepare() click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 522
def prepare
end
split_first_param_name() click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 525
def split_first_param_name()
  # Note parameters are instances of MethodParameter
  # return if @parameters.length == 0

  p1 = @parameters[ 0 ]

  m = p1.param_label.match( /(initWith|init)(?<arg_name>\w*)/ )
  unless m.nil?
    arg_name = m[ 'arg_name' ]
    if arg_name.nil?
      p1.param_label = nil
    elsif arg_name.length < 3
      p1.param_label = arg_name.downcase
    else
      p1.param_label = arg_name[0, 1].downcase + arg_name[1..-1]
    end

    @swift_name = 'init'
    @is_initializer = true
  else
    @swift_name = @parameters[ 0 ].param_label
    # p1.param_label = nil
  end

  if @parameters.length == 1 && (p1.param_type.nil? || p1.param_type.length == 0)
    @parameters = []
  end
end
swift_type_string( configuration, param_type, pointer_level, null_qualifier, is_weak=false ) click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 614
def swift_type_string( configuration, param_type, pointer_level, null_qualifier, is_weak=false  )
  mapped_param_type = configuration.type_mapper.swift_type_for_objc_type( param_type )

  mapped_param_type = "/*__weak*/ #{mapped_param_type}" if is_weak

  if( pointer_level == 2 )
    if mapped_param_type == "NSError"
        return 'NSErrorPointer'
    else
      return "AutoreleasingUnsafeMutablePointer<#{mapped_param_type}?>"
    end
  else
    return "#{mapped_param_type}#{nullable_qualifier(null_qualifier, pointer_level)}" unless mapped_param_type.nil?
  end
end