|
| 1 | +//===--- SwiftNameTranslation.cpp - Swift to ObjC Name Translation APIs ---===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file contains utilities for translating Swift names to ObjC. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#include "swift/AST/SwiftNameTranslation.h" |
| 18 | +#include "swift/AST/ASTContext.h" |
| 19 | +#include "swift/AST/Decl.h" |
| 20 | +#include "swift/AST/LazyResolver.h" |
| 21 | +#include "swift/Basic/StringExtras.h" |
| 22 | + |
| 23 | +#include "clang/AST/DeclObjC.h" |
| 24 | +#include "llvm/ADT/SmallString.h" |
| 25 | + |
| 26 | +using namespace swift; |
| 27 | + |
| 28 | +StringRef swift::objc_translation:: |
| 29 | +getNameForObjC(const ValueDecl *VD, CustomNamesOnly_t customNamesOnly) { |
| 30 | + assert(isa<ClassDecl>(VD) || isa<ProtocolDecl>(VD) || isa<StructDecl>(VD) || |
| 31 | + isa<EnumDecl>(VD) || isa<EnumElementDecl>(VD)); |
| 32 | + if (auto objc = VD->getAttrs().getAttribute<ObjCAttr>()) { |
| 33 | + if (auto name = objc->getName()) { |
| 34 | + assert(name->getNumSelectorPieces() == 1); |
| 35 | + return name->getSelectorPieces().front().str(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (customNamesOnly) |
| 40 | + return StringRef(); |
| 41 | + |
| 42 | + if (auto clangDecl = dyn_cast_or_null<clang::NamedDecl>(VD->getClangDecl())) { |
| 43 | + if (const clang::IdentifierInfo *II = clangDecl->getIdentifier()) |
| 44 | + return II->getName(); |
| 45 | + if (auto *anonDecl = dyn_cast<clang::TagDecl>(clangDecl)) |
| 46 | + if (auto *anonTypedef = anonDecl->getTypedefNameForAnonDecl()) |
| 47 | + return anonTypedef->getIdentifier()->getName(); |
| 48 | + } |
| 49 | + |
| 50 | + return VD->getName().str(); |
| 51 | +} |
| 52 | + |
| 53 | +bool swift::objc_translation:: |
| 54 | +printSwiftEnumElemNameInObjC(const EnumElementDecl *EL, llvm::raw_ostream &OS, |
| 55 | + Identifier PreferredName) { |
| 56 | + StringRef ElemName = getNameForObjC(EL, CustomNamesOnly); |
| 57 | + if (!ElemName.empty()) { |
| 58 | + OS << ElemName; |
| 59 | + return true; |
| 60 | + } |
| 61 | + OS << getNameForObjC(EL->getDeclContext()->getAsEnumOrEnumExtensionContext()); |
| 62 | + if (PreferredName.empty()) |
| 63 | + ElemName = EL->getName().str(); |
| 64 | + else |
| 65 | + ElemName = PreferredName.str(); |
| 66 | + |
| 67 | + SmallString<64> Scratch; |
| 68 | + OS << camel_case::toSentencecase(ElemName, Scratch); |
| 69 | + return false; |
| 70 | +} |
| 71 | + |
| 72 | +std::pair<Identifier, ObjCSelector> swift::objc_translation:: |
| 73 | +getObjCNameForSwiftDecl(const ValueDecl *VD, DeclName PreferredName){ |
| 74 | + ASTContext &Ctx = VD->getASTContext(); |
| 75 | + LazyResolver *Resolver = Ctx.getLazyResolver(); |
| 76 | + if (auto *FD = dyn_cast<AbstractFunctionDecl>(VD)) { |
| 77 | + return {Identifier(), FD->getObjCSelector(Resolver, PreferredName)}; |
| 78 | + } else if (auto *VAD = dyn_cast<VarDecl>(VD)) { |
| 79 | + if (PreferredName) |
| 80 | + return {PreferredName.getBaseName(), ObjCSelector()}; |
| 81 | + return {VAD->getObjCPropertyName(), ObjCSelector()}; |
| 82 | + } else if (auto *SD = dyn_cast<SubscriptDecl>(VD)) { |
| 83 | + return getObjCNameForSwiftDecl(SD->getGetter(), PreferredName); |
| 84 | + } else if (auto *EL = dyn_cast<EnumElementDecl>(VD)) { |
| 85 | + SmallString<64> Buffer; |
| 86 | + { |
| 87 | + llvm::raw_svector_ostream OS(Buffer); |
| 88 | + printSwiftEnumElemNameInObjC(EL, OS, PreferredName.getBaseName()); |
| 89 | + } |
| 90 | + return {Ctx.getIdentifier(Buffer.str()), ObjCSelector()}; |
| 91 | + } else { |
| 92 | + // @objc(ExplicitName) > PreferredName > Swift name. |
| 93 | + StringRef Name = getNameForObjC(VD, CustomNamesOnly); |
| 94 | + if (!Name.empty()) |
| 95 | + return {Ctx.getIdentifier(Name), ObjCSelector()}; |
| 96 | + if (!PreferredName.getBaseName().empty()) |
| 97 | + return {PreferredName.getBaseName(), ObjCSelector()}; |
| 98 | + return {Ctx.getIdentifier(getNameForObjC(VD)), ObjCSelector()}; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +bool swift::objc_translation:: |
| 103 | +isVisibleToObjC(const ValueDecl *VD, Accessibility minRequiredAccess, |
| 104 | + bool checkParent) { |
| 105 | + if (!(VD->isObjC() || VD->getAttrs().hasAttribute<CDeclAttr>())) |
| 106 | + return false; |
| 107 | + if (VD->hasAccessibility() && VD->getFormalAccess() >= minRequiredAccess) { |
| 108 | + return true; |
| 109 | + } else if (checkParent) { |
| 110 | + if (auto ctor = dyn_cast<ConstructorDecl>(VD)) { |
| 111 | + // Check if we're overriding an initializer that is visible to obj-c |
| 112 | + if (auto parent = ctor->getOverriddenDecl()) |
| 113 | + return isVisibleToObjC(parent, minRequiredAccess, false); |
| 114 | + } |
| 115 | + } |
| 116 | + return false; |
| 117 | +} |
0 commit comments