Class: Objc2swiftAssistant::ObjC2SwiftMethodConverter
- Inherits:
-
ObjC2SwiftCodeConverter
- Object
- FailableProcessingElement
- ObjC2SwiftCodeConverter
- Objc2swiftAssistant::ObjC2SwiftMethodConverter
- Defined in:
- lib/objc2swift_assistant/objc_2_swift.rb
Instance Attribute Summary collapse
-
#access_control_state ⇒ Object
Returns the value of attribute access_control_state.
-
#class_converter ⇒ Object
Returns the value of attribute class_converter.
-
#declaration_region ⇒ Object
Returns the value of attribute declaration_region.
-
#implementation_region ⇒ Object
Returns the value of attribute implementation_region.
-
#is_initializer ⇒ Object
Returns the value of attribute is_initializer.
-
#objc_signature ⇒ Object
Returns the value of attribute objc_signature.
-
#owning_property ⇒ Object
Returns the value of attribute owning_property.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#protocol_optional_state ⇒ Object
Returns the value of attribute protocol_optional_state.
-
#swift_name ⇒ Object
Returns the value of attribute swift_name.
Attributes inherited from ObjC2SwiftCodeConverter
#configuration, #file_converter, #marks
Attributes inherited from FailableProcessingElement
Instance Method Summary collapse
- #arg_list(configuration) ⇒ Object
- #arg_string(param, configuration) ⇒ Object
- #body_lines_to_emit ⇒ Object
- #compute_swift_name ⇒ Object
- #generate(generated_class, configuration, dry_run) ⇒ Object
-
#initialize(class_converter, objc_signature, configuration) ⇒ ObjC2SwiftMethodConverter
constructor
A new instance of ObjC2SwiftMethodConverter.
- #nullable_qualifier(null_qualifier, pointer_level) ⇒ Object
- #prepare ⇒ Object
- #split_first_param_name ⇒ Object
- #swift_type_string(configuration, param_type, pointer_level, null_qualifier, is_weak = false) ⇒ Object
Methods inherited from ObjC2SwiftCodeConverter
#add_mark_region, #cannonical_source_file_path, #company_name, #emit_original_bodies, #emit_original_signatures, #emit_unconverted_content, #omit_file
Methods inherited from FailableProcessingElement
#add_error, #add_warning, #has_errors, #initiailize
Constructor Details
#initialize(class_converter, objc_signature, configuration) ⇒ ObjC2SwiftMethodConverter
Returns a new instance of ObjC2SwiftMethodConverter.
504 505 506 507 508 509 510 511 512 513 |
# 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 |
Instance Attribute Details
#access_control_state ⇒ Object
Returns the value of attribute access_control_state.
500 501 502 |
# File 'lib/objc2swift_assistant/objc_2_swift.rb', line 500 def access_control_state @access_control_state end |
#class_converter ⇒ Object
Returns the value of attribute class_converter.
491 492 493 |
# File 'lib/objc2swift_assistant/objc_2_swift.rb', line 491 def class_converter @class_converter end |
#declaration_region ⇒ Object
Returns the value of attribute declaration_region.
494 495 496 |
# File 'lib/objc2swift_assistant/objc_2_swift.rb', line 494 def declaration_region @declaration_region end |
#implementation_region ⇒ Object
Returns the value of attribute implementation_region.
495 496 497 |
# File 'lib/objc2swift_assistant/objc_2_swift.rb', line 495 def implementation_region @implementation_region end |
#is_initializer ⇒ Object
Returns the value of attribute is_initializer.
498 499 500 |
# File 'lib/objc2swift_assistant/objc_2_swift.rb', line 498 def is_initializer @is_initializer end |
#objc_signature ⇒ Object
Returns the value of attribute objc_signature.
492 493 494 |
# File 'lib/objc2swift_assistant/objc_2_swift.rb', line 492 def objc_signature @objc_signature end |
#owning_property ⇒ Object
Returns the value of attribute owning_property.
496 497 498 |
# File 'lib/objc2swift_assistant/objc_2_swift.rb', line 496 def owning_property @owning_property end |
#parameters ⇒ Object
Returns the value of attribute parameters.
497 498 499 |
# File 'lib/objc2swift_assistant/objc_2_swift.rb', line 497 def parameters @parameters end |
#protocol_optional_state ⇒ Object
Returns the value of attribute protocol_optional_state.
501 502 503 |
# File 'lib/objc2swift_assistant/objc_2_swift.rb', line 501 def protocol_optional_state @protocol_optional_state end |
#swift_name ⇒ Object
Returns the value of attribute swift_name.
493 494 495 |
# File 'lib/objc2swift_assistant/objc_2_swift.rb', line 493 def swift_name @swift_name end |
Instance Method Details
#arg_list(configuration) ⇒ Object
586 587 588 589 590 591 592 |
# 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) ⇒ Object
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# 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 ⇒ Object
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 |
# 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 ⇒ Object
515 516 517 518 519 520 |
# 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) ⇒ Object
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 |
# 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) ⇒ Object
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 |
# 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 ⇒ Object
522 523 |
# File 'lib/objc2swift_assistant/objc_2_swift.rb', line 522 def prepare end |
#split_first_param_name ⇒ Object
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
# 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) ⇒ Object
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 |
# 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 |