@@ -881,7 +881,9 @@ static bool passCursorInfoForDecl(const ValueDecl *VD,
881
881
}
882
882
883
883
static clang::DeclarationName
884
- getClangDeclarationName (clang::ASTContext &Ctx, NameTranslatingInfo &Info) {
884
+ getClangDeclarationName (const clang::NamedDecl *ND, NameTranslatingInfo &Info) {
885
+ auto &Ctx = ND->getASTContext ();
886
+ auto OrigName = ND->getDeclName ();
885
887
assert (SwiftLangSupport::getNameKindForUID (Info.NameKind ) == NameKind::ObjC);
886
888
if (Info.BaseName .empty () == Info.ArgNames .empty ()) {
887
889
// cannot have both.
@@ -890,27 +892,61 @@ getClangDeclarationName(clang::ASTContext &Ctx, NameTranslatingInfo &Info) {
890
892
if (!Info.BaseName .empty ()) {
891
893
return clang::DeclarationName (&Ctx.Idents .get (Info.BaseName ));
892
894
} else {
895
+ StringRef last = Info.ArgNames .back ();
896
+
897
+ switch (OrigName.getNameKind ()) {
898
+ case clang::DeclarationName::ObjCZeroArgSelector:
899
+ if (last.endswith (" :" ))
900
+ return clang::DeclarationName ();
901
+ break ;
902
+ case clang::DeclarationName::ObjCOneArgSelector:
903
+ case clang::DeclarationName::ObjCMultiArgSelector:
904
+ if (!last.empty () && !last.endswith (" :" ))
905
+ return clang::DeclarationName ();
906
+ break ;
907
+ default :
908
+ return clang::DeclarationName ();
909
+ }
910
+
911
+ auto OrigSel = OrigName.getObjCSelector ();
912
+ unsigned NumPieces = OrigSel.isUnarySelector () ? 1 : OrigSel.getNumArgs ();
913
+ if (Info.ArgNames .size () > NumPieces)
914
+ return clang::DeclarationName ();
915
+
893
916
ArrayRef<StringRef> Args = llvm::makeArrayRef (Info.ArgNames );
894
917
std::vector<clang::IdentifierInfo *> Pieces;
895
- std::transform (Args.begin (), Args.end (), std::back_inserter (Pieces),
896
- [&](StringRef T) { return &Ctx.Idents .get (T.endswith (" :" ) ?
897
- T.drop_back () : T); });
898
- return clang::DeclarationName (Ctx.Selectors .getSelector (
899
- /* Calculate Args*/ std::accumulate (Args.begin (), Args.end (), (unsigned )0 ,
900
- [](unsigned I, StringRef T) { return I + (T.endswith (" :" ) ? 1 : 0 ); }),
901
- Pieces.data ()));
918
+ for (unsigned i = 0 ; i < NumPieces; ++i) {
919
+ if (i >= Info.ArgNames .size () || Info.ArgNames [i].empty ()) {
920
+ Pieces.push_back (OrigSel.getIdentifierInfoForSlot (i));
921
+ } else {
922
+ StringRef T = Args[i];
923
+ Pieces.push_back (&Ctx.Idents .get (T.endswith (" :" ) ? T.drop_back () : T));
924
+ }
925
+ }
926
+ return clang::DeclarationName (
927
+ Ctx.Selectors .getSelector (OrigSel.getNumArgs (), Pieces.data ()));
902
928
}
903
929
}
904
930
905
- static DeclName
906
- getSwiftDeclName (ASTContext &Ctx, NameTranslatingInfo &Info) {
931
+ static DeclName getSwiftDeclName (const ValueDecl *VD,
932
+ NameTranslatingInfo &Info) {
933
+ auto &Ctx = VD->getDeclContext ()->getASTContext ();
907
934
assert (SwiftLangSupport::getNameKindForUID (Info.NameKind ) == NameKind::Swift);
908
- std::vector<Identifier> Args;
909
- std::transform (Info.ArgNames .begin (), Info.ArgNames .end (),
910
- std::back_inserter (Args),
911
- [&](StringRef T) { return Ctx.getIdentifier (T); });
912
- return DeclName (Ctx, Ctx.getIdentifier (Info.BaseName ),
913
- llvm::makeArrayRef (Args));
935
+ DeclName OrigName = VD->getFullName ();
936
+ Identifier BaseName = Info.BaseName .empty ()
937
+ ? OrigName.getBaseName ()
938
+ : Ctx.getIdentifier (Info.BaseName );
939
+ auto OrigArgs = OrigName.getArgumentNames ();
940
+ SmallVector<Identifier, 8 > Args (OrigArgs.begin (), OrigArgs.end ());
941
+ if (Info.ArgNames .size () > OrigArgs.size ())
942
+ return DeclName ();
943
+ for (unsigned i = 0 ; i < OrigArgs.size (); ++i) {
944
+ if (i < Info.ArgNames .size () && !Info.ArgNames [i].empty ()) {
945
+ StringRef Arg = Info.ArgNames [i];
946
+ Args[i] = Ctx.getIdentifier (Arg == " _" ? StringRef () : Arg);
947
+ }
948
+ }
949
+ return DeclName (Ctx, BaseName, llvm::makeArrayRef (Args));
914
950
}
915
951
916
952
// / Returns true for failure to resolve.
@@ -919,9 +955,11 @@ static bool passNameInfoForDecl(const ValueDecl *VD, NameTranslatingInfo &Info,
919
955
switch (SwiftLangSupport::getNameKindForUID (Info.NameKind )) {
920
956
case NameKind::Swift: {
921
957
NameTranslatingInfo Result;
922
- auto &Ctx = VD->getDeclContext ()->getASTContext ();
923
- auto ResultPair = swift::objc_translation::getObjCNameForSwiftDecl (VD,
924
- getSwiftDeclName (Ctx, Info));
958
+ auto DeclName = getSwiftDeclName (VD, Info);
959
+ if (!DeclName)
960
+ return true ;
961
+ auto ResultPair =
962
+ swift::objc_translation::getObjCNameForSwiftDecl (VD, DeclName);
925
963
Identifier Name = ResultPair.first ;
926
964
if (!Name.empty ()) {
927
965
Result.NameKind = SwiftLangSupport::getUIDForNameKind (NameKind::ObjC);
@@ -952,8 +990,11 @@ static bool passNameInfoForDecl(const ValueDecl *VD, NameTranslatingInfo &Info,
952
990
getASTContext ().getClangModuleLoader ());
953
991
954
992
if (auto *Named = dyn_cast_or_null<clang::NamedDecl>(VD->getClangDecl ())) {
955
- DeclName Name = Importer->importName (Named,
956
- getClangDeclarationName (Named->getASTContext (), Info));
993
+ auto ObjCName = getClangDeclarationName (Named, Info);
994
+ if (!ObjCName)
995
+ return true ;
996
+
997
+ DeclName Name = Importer->importName (Named, ObjCName);
957
998
NameTranslatingInfo Result;
958
999
Result.NameKind = SwiftLangSupport::getUIDForNameKind (NameKind::Swift);
959
1000
Result.BaseName = Name.getBaseName ().str ();
0 commit comments