@@ -605,54 +605,95 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
605
605
return Replacement.contains (' &' ) || Replacement.contains (" ->" );
606
606
}
607
607
608
+ void handleOverridingTypeChange (AbstractFunctionDecl *AFD,
609
+ CommonDiffItem *DiffItem) {
610
+ assert (AFD);
611
+ assert (DiffItem->isTypeChange ());
612
+ ChildIndexFinder Finder (DiffItem->getChildIndices ());
613
+ auto Result = Finder.findChild (AFD);
614
+ if (!Result.isValid ())
615
+ return ;
616
+
617
+ switch (DiffItem->DiffKind ) {
618
+ case ide::api::NodeAnnotation::WrapOptional:
619
+ if (Result.Suffixable ) {
620
+ Editor.insertAfterToken (Result.TokenRange .End , " ?" );
621
+ } else {
622
+ Editor.insertWrap (" (" , Result.TokenRange , " )?" );
623
+ }
624
+ break ;
625
+ case ide::api::NodeAnnotation::WrapImplicitOptional:
626
+ if (Result.Suffixable ) {
627
+ Editor.insertAfterToken (Result.TokenRange .End , " !" );
628
+ } else {
629
+ Editor.insertWrap (" (" , Result.TokenRange , (" )!" ));
630
+ }
631
+ break ;
632
+ case ide::api::NodeAnnotation::UnwrapOptional:
633
+ if (Result.Optional )
634
+ Editor.remove (Result.TokenRange .End );
635
+ break ;
636
+ case ide::api::NodeAnnotation::ImplicitOptionalToOptional:
637
+ if (Result.Optional )
638
+ Editor.replace (Result.TokenRange .End , " ?" );
639
+ break ;
640
+ case ide::api::NodeAnnotation::TypeRewritten:
641
+ Editor.replace (Result.TokenRange , DiffItem->RightComment );
642
+ if (Result.Suffixed && typeReplacementMayNeedParens (DiffItem->RightComment )) {
643
+ Editor.insertBefore (Result.TokenRange .Start , " (" );
644
+ Editor.insertAfterToken (Result.TokenRange .End , " )" );
645
+ }
646
+ break ;
647
+ default :
648
+ break ;
649
+ }
650
+ }
651
+
652
+ void handleOverridingPropertyChange (AbstractFunctionDecl *AFD,
653
+ CommonDiffItem *DiffItem) {
654
+ assert (AFD);
655
+ assert (DiffItem->isToPropertyChange ());
656
+ auto FD = dyn_cast<FuncDecl>(AFD);
657
+ if (!FD)
658
+ return ;
659
+
660
+ switch (DiffItem->DiffKind ) {
661
+ case NodeAnnotation::GetterToProperty: {
662
+ auto FuncLoc = FD->getFuncLoc ();
663
+ auto ReturnTyLoc = FD->getBodyResultTypeLoc ().getLoc ();
664
+ auto NameLoc = FD->getNameLoc ();
665
+ if (FuncLoc.isInvalid () || ReturnTyLoc.isInvalid () || NameLoc.isInvalid ())
666
+ break ;
667
+
668
+ // Replace "func" with "var"
669
+ Editor.replaceToken (FuncLoc, " var" );
670
+
671
+ // Replace "() -> " with ": "
672
+ Editor.replace (CharSourceRange (SM, Lexer::getLocForEndOfToken (SM, NameLoc),
673
+ ReturnTyLoc), " : " );
674
+
675
+ break ;
676
+ }
677
+ case NodeAnnotation::SetterToProperty: {
678
+ // FIXME: we should migrate this case too.
679
+ break ;
680
+ }
681
+ default :
682
+ llvm_unreachable (" should not be handled here." );
683
+ }
684
+ }
685
+
608
686
bool walkToDeclPre (Decl *D, CharSourceRange Range) override {
609
687
if (D->isImplicit ())
610
688
return true ;
611
689
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
612
690
handleFuncDeclRename (AFD, Range);
613
691
for (auto *Item: getRelatedDiffItems (AFD)) {
614
692
if (auto *DiffItem = dyn_cast<CommonDiffItem>(Item)) {
615
- if (!DiffItem->isTypeChange ())
616
- continue ;
617
-
618
- ChildIndexFinder Finder (DiffItem->getChildIndices ());
619
- auto Result = Finder.findChild (AFD);
620
- if (!Result.isValid ())
621
- return false ;
622
-
623
- switch (DiffItem->DiffKind ) {
624
- case ide::api::NodeAnnotation::WrapOptional:
625
- if (Result.Suffixable ) {
626
- Editor.insertAfterToken (Result.TokenRange .End , " ?" );
627
- } else {
628
- Editor.insertWrap (" (" , Result.TokenRange , " )?" );
629
- }
630
- break ;
631
- case ide::api::NodeAnnotation::WrapImplicitOptional:
632
- if (Result.Suffixable ) {
633
- Editor.insertAfterToken (Result.TokenRange .End , " !" );
634
- } else {
635
- Editor.insertWrap (" (" , Result.TokenRange , (" )!" ));
636
- }
637
- break ;
638
- case ide::api::NodeAnnotation::UnwrapOptional:
639
- if (Result.Optional )
640
- Editor.remove (Result.TokenRange .End );
641
- break ;
642
- case ide::api::NodeAnnotation::ImplicitOptionalToOptional:
643
- if (Result.Optional )
644
- Editor.replace (Result.TokenRange .End , " ?" );
645
- break ;
646
- case ide::api::NodeAnnotation::TypeRewritten:
647
- Editor.replace (Result.TokenRange , DiffItem->RightComment );
648
- if (Result.Suffixed && typeReplacementMayNeedParens (DiffItem->RightComment )) {
649
- Editor.insertBefore (Result.TokenRange .Start , " (" );
650
- Editor.insertAfterToken (Result.TokenRange .End , " )" );
651
- }
652
- break ;
653
- default :
654
- break ;
655
- }
693
+ if (DiffItem->isTypeChange ())
694
+ handleOverridingTypeChange (AFD, DiffItem);
695
+ else if (DiffItem->isToPropertyChange ())
696
+ handleOverridingPropertyChange (AFD, DiffItem);
656
697
}
657
698
}
658
699
}
0 commit comments