mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[Type checker/runtime] Move default implementation of Error._code into the runtime.
Rather than having Sema provide a default implementation of Error._code when needed, introduce a runtime function to extract the default code, so that we can provide a default implementation via a protocol extension in the standard library.
This commit is contained in:
@@ -26,167 +26,6 @@
|
||||
using namespace swift;
|
||||
using namespace DerivedConformance;
|
||||
|
||||
static void deriveBodyError_enum_code(AbstractFunctionDecl *codeDecl) {
|
||||
// enum SomeEnum {
|
||||
// case A,B,C,D
|
||||
//
|
||||
// @derived
|
||||
// var code: Int {
|
||||
// switch self {
|
||||
// case A: return 0
|
||||
// case B: return 1
|
||||
// case C: return 2
|
||||
// ...
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// TODO: Some convenient way to override the code if that's desired.
|
||||
|
||||
auto parentDC = codeDecl->getDeclContext();
|
||||
ASTContext &C = parentDC->getASTContext();
|
||||
|
||||
auto enumDecl = parentDC->getAsEnumOrEnumExtensionContext();
|
||||
Type enumType = parentDC->getDeclaredTypeInContext();
|
||||
|
||||
SmallVector<CaseStmt*, 4> cases;
|
||||
SmallString<11> strBuf;
|
||||
|
||||
unsigned code = 0;
|
||||
for (auto elt : enumDecl->getAllElements()) {
|
||||
auto pat = new (C) EnumElementPattern(TypeLoc::withoutLoc(enumType),
|
||||
SourceLoc(), SourceLoc(),
|
||||
Identifier(), elt, nullptr);
|
||||
pat->setImplicit();
|
||||
|
||||
auto labelItem =
|
||||
CaseLabelItem(/*IsDefault=*/false, pat, SourceLoc(), nullptr);
|
||||
|
||||
{
|
||||
strBuf.clear();
|
||||
llvm::raw_svector_ostream os(strBuf);
|
||||
os << code;
|
||||
}
|
||||
|
||||
auto codeStr = C.AllocateCopy(StringRef(strBuf));
|
||||
|
||||
auto returnExpr = new (C) IntegerLiteralExpr(codeStr, SourceLoc(),
|
||||
/*implicit*/ true);
|
||||
auto returnStmt = new (C) ReturnStmt(SourceLoc(), returnExpr,
|
||||
/*implicit*/ true);
|
||||
|
||||
auto body = BraceStmt::create(C, SourceLoc(),
|
||||
ASTNode(returnStmt), SourceLoc());
|
||||
|
||||
cases.push_back(CaseStmt::create(C, SourceLoc(), labelItem,
|
||||
/*HasBoundDecls=*/false, SourceLoc(),
|
||||
body));
|
||||
|
||||
++code;
|
||||
}
|
||||
|
||||
Stmt *bodyStmt;
|
||||
// If the enum is empty, simply return zero. (It doesn't really matter, since
|
||||
// the enum can't be instantiated regardless.)
|
||||
if (cases.empty()) {
|
||||
static const char zero[] = "0";
|
||||
auto returnExpr = new (C) IntegerLiteralExpr(zero, SourceLoc(),
|
||||
/*implicit*/ true);
|
||||
bodyStmt = new (C) ReturnStmt(SourceLoc(), returnExpr,
|
||||
/*implicit*/ true);
|
||||
} else {
|
||||
auto selfRef = createSelfDeclRef(codeDecl);
|
||||
bodyStmt = SwitchStmt::create(LabeledStmtInfo(), SourceLoc(), selfRef,
|
||||
SourceLoc(), cases, SourceLoc(), C);
|
||||
}
|
||||
auto body = BraceStmt::create(C, SourceLoc(),
|
||||
ASTNode(bodyStmt),
|
||||
SourceLoc());
|
||||
|
||||
codeDecl->setBody(body);
|
||||
}
|
||||
|
||||
static void deriveBodyError_zero_code(AbstractFunctionDecl *codeDecl) {
|
||||
// struct SomeStruct {
|
||||
// @derived
|
||||
// var code: Int { return 0 }
|
||||
// }
|
||||
//
|
||||
// TODO: Some convenient way to override the code if that's desired.
|
||||
|
||||
auto parentDC = codeDecl->getDeclContext();
|
||||
ASTContext &C = parentDC->getASTContext();
|
||||
|
||||
auto returnExpr = new (C) IntegerLiteralExpr("1", SourceLoc(),
|
||||
/*implicit*/ true);
|
||||
auto returnStmt = new (C) ReturnStmt(SourceLoc(), returnExpr,
|
||||
/*implicit*/ true);
|
||||
|
||||
auto body = BraceStmt::create(C, SourceLoc(),
|
||||
ASTNode(returnStmt), SourceLoc());
|
||||
|
||||
codeDecl->setBody(body);
|
||||
}
|
||||
|
||||
static ValueDecl *deriveError_code(TypeChecker &tc, Decl *parentDecl,
|
||||
NominalTypeDecl *nominal) {
|
||||
// enum SomeEnum {
|
||||
// case A,B,C,D
|
||||
//
|
||||
// @derived
|
||||
// var code: Int {
|
||||
// switch self {
|
||||
// case A: return 0
|
||||
// case B: return 1
|
||||
// case C: return 2
|
||||
// ...
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
ASTContext &C = tc.Context;
|
||||
|
||||
auto intTy = C.getIntDecl()->getDeclaredType();
|
||||
|
||||
// Define the getter.
|
||||
auto getterDecl = declareDerivedPropertyGetter(tc, parentDecl, nominal,
|
||||
intTy, intTy,
|
||||
/*isStatic=*/false,
|
||||
/*isFinal=*/true);
|
||||
if (isa<EnumDecl>(nominal))
|
||||
getterDecl->setBodySynthesizer(&deriveBodyError_enum_code);
|
||||
else
|
||||
getterDecl->setBodySynthesizer(&deriveBodyError_zero_code);
|
||||
|
||||
// Define the property.
|
||||
VarDecl *propDecl;
|
||||
PatternBindingDecl *pbDecl;
|
||||
std::tie(propDecl, pbDecl)
|
||||
= declareDerivedReadOnlyProperty(tc, parentDecl, nominal, C.Id_code_,
|
||||
intTy, intTy, getterDecl,
|
||||
/*isStatic=*/false, /*isFinal=*/true);
|
||||
|
||||
auto dc = cast<IterableDeclContext>(parentDecl);
|
||||
dc->addMember(getterDecl);
|
||||
dc->addMember(propDecl);
|
||||
dc->addMember(pbDecl);
|
||||
|
||||
return propDecl;
|
||||
|
||||
}
|
||||
|
||||
ValueDecl *DerivedConformance::deriveError(TypeChecker &tc,
|
||||
Decl *parentDecl,
|
||||
NominalTypeDecl *type,
|
||||
ValueDecl *requirement) {
|
||||
if (requirement->getName() == tc.Context.Id_code_)
|
||||
return deriveError_code(tc, parentDecl, type);
|
||||
|
||||
tc.diagnose(requirement->getLoc(),
|
||||
diag::broken_errortype_requirement);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void deriveBodyBridgedNSError_enum_nsErrorDomain(
|
||||
AbstractFunctionDecl *domainDecl) {
|
||||
// enum SomeEnum {
|
||||
|
||||
Reference in New Issue
Block a user