Skip to content

Commit 1a6ca82

Browse files
committed
[NFC] Extract helper for making NSError domains
Avoids having to coordinate changes in two different parts of the compiler.
1 parent 988cf07 commit 1a6ca82

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

include/swift/AST/SwiftNameTranslation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace swift {
2020
class ValueDecl;
21+
class EnumDecl;
2122
class EnumElementDecl;
2223

2324
namespace objc_translation {
@@ -29,6 +30,8 @@ namespace objc_translation {
2930
StringRef getNameForObjC(const ValueDecl *VD,
3031
CustomNamesOnly_t customNamesOnly = Normal);
3132

33+
std::string getErrorDomainStringForObjC(const EnumDecl *ED);
34+
3235
/// Print the ObjC name of an enum element decl to OS, also allowing the client
3336
/// to specify a preferred name other than the decl's original name.
3437
///

lib/AST/SwiftNameTranslation.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "swift/AST/SwiftNameTranslation.h"
1818
#include "swift/AST/ASTContext.h"
19+
#include "swift/AST/Module.h"
1920
#include "swift/AST/Decl.h"
2021
#include "swift/AST/LazyResolver.h"
2122
#include "swift/Basic/StringExtras.h"
@@ -51,6 +52,14 @@ getNameForObjC(const ValueDecl *VD, CustomNamesOnly_t customNamesOnly) {
5152
return VD->getBaseName().getIdentifier().str();
5253
}
5354

55+
std::string swift::objc_translation::
56+
getErrorDomainStringForObjC(const EnumDecl *ED) {
57+
std::string buffer = ED->getParentModule()->getNameStr();
58+
buffer += ".";
59+
buffer += ED->getNameStr();
60+
return buffer;
61+
}
62+
5463
bool swift::objc_translation::
5564
printSwiftEnumElemNameInObjC(const EnumElementDecl *EL, llvm::raw_ostream &OS,
5665
Identifier PreferredName) {

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2511,7 +2511,7 @@ class ModuleWriter {
25112511
});
25122512
if (!hasDomainCase) {
25132513
os << "static NSString * _Nonnull const " << getNameForObjC(ED)
2514-
<< "Domain = @\"" << M.getName() << "." << ED->getName() << "\";\n";
2514+
<< "Domain = @\"" << getErrorDomainStringForObjC(ED) << "\";\n";
25152515
}
25162516
}
25172517

lib/Sema/DerivedConformanceError.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
#include "swift/AST/Expr.h"
2323
#include "swift/AST/Module.h"
2424
#include "swift/AST/Types.h"
25+
#include "swift/AST/SwiftNameTranslation.h"
2526

2627
using namespace swift;
28+
using namespace swift::objc_translation;
2729

2830
static void deriveBodyBridgedNSError_enum_nsErrorDomain(
2931
AbstractFunctionDecl *domainDecl, void *) {
@@ -39,10 +41,7 @@ static void deriveBodyBridgedNSError_enum_nsErrorDomain(
3941
auto TC = domainDecl->getInnermostTypeContext();
4042
auto ED = TC->getSelfEnumDecl();
4143

42-
std::string buffer = M->getNameStr();
43-
buffer += ".";
44-
buffer += ED->getNameStr();
45-
StringRef value(C.AllocateCopy(buffer));
44+
StringRef value(C.AllocateCopy(getErrorDomainStringForObjC(ED)));
4645

4746
auto string = new (C) StringLiteralExpr(value, SourceRange(), /*implicit*/ true);
4847
auto ret = new (C) ReturnStmt(SourceLoc(), string, /*implicit*/ true);
@@ -57,13 +56,10 @@ deriveBridgedNSError_enum_nsErrorDomain(DerivedConformance &derived) {
5756
// enum SomeEnum {
5857
// @derived
5958
// static var _nsErrorDomain: String {
60-
// return "\(self)"
59+
// return "ModuleName.SomeEnum"
6160
// }
6261
// }
6362

64-
// Note that for @objc enums the format is assumed to be "MyModule.SomeEnum".
65-
// If this changes, please change PrintAsObjC as well.
66-
6763
ASTContext &C = derived.TC.Context;
6864

6965
auto stringTy = C.getStringDecl()->getDeclaredType();

0 commit comments

Comments
 (0)