|
| 1 | +//===--- DerivedConformanceEquatableHashable.cpp - Derived Equatable & co -===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 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 implements implicit derivation of the Actor protocol. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | +#include "DerivedConformances.h" |
| 17 | +#include "TypeChecker.h" |
| 18 | +#include "swift/AST/ParameterList.h" |
| 19 | + |
| 20 | +using namespace swift; |
| 21 | + |
| 22 | +bool DerivedConformance::canDeriveActor( |
| 23 | + NominalTypeDecl *nominal, DeclContext *dc) { |
| 24 | + auto classDecl = dyn_cast<ClassDecl>(nominal); |
| 25 | + return classDecl && classDecl->isActor() && dc == nominal; |
| 26 | +} |
| 27 | + |
| 28 | +static DeclName getEnqueuePartialTaskName(ASTContext &ctx) { |
| 29 | + return DeclName(ctx, ctx.Id_enqueue, { ctx.Id_partialTask }); |
| 30 | +} |
| 31 | + |
| 32 | +static Type getPartialAsyncTaskType(ASTContext &ctx) { |
| 33 | + auto concurrencyModule = ctx.getLoadedModule(ctx.Id_Concurrency); |
| 34 | + if (!concurrencyModule) |
| 35 | + return Type(); |
| 36 | + |
| 37 | + SmallVector<ValueDecl *, 2> decls; |
| 38 | + concurrencyModule->lookupQualified( |
| 39 | + concurrencyModule, DeclNameRef(ctx.Id_PartialAsyncTask), |
| 40 | + NL_QualifiedDefault, decls); |
| 41 | + for (auto decl : decls) { |
| 42 | + if (auto typeDecl = dyn_cast<TypeDecl>(decl)) |
| 43 | + return typeDecl->getDeclaredInterfaceType(); |
| 44 | + } |
| 45 | + |
| 46 | + return Type(); |
| 47 | +} |
| 48 | + |
| 49 | +static std::pair<BraceStmt *, bool> |
| 50 | +deriveBodyActor_enqueuePartialTask( |
| 51 | + AbstractFunctionDecl *enqueuePartialTask, void *) { |
| 52 | + ASTContext &ctx = enqueuePartialTask->getASTContext(); |
| 53 | + |
| 54 | + // FIXME: Call into runtime API to enqueue the task, once we figure out |
| 55 | + // what that runtime API should look like. |
| 56 | + |
| 57 | + auto body = BraceStmt::create( |
| 58 | + ctx, SourceLoc(), { }, SourceLoc(), /*implicit=*/true); |
| 59 | + return { body, /*isTypeChecked=*/true }; |
| 60 | +} |
| 61 | + |
| 62 | +/// Derive the declaration of Actor's enqueue(partialTask:). |
| 63 | +static ValueDecl *deriveActor_enqueuePartialTask(DerivedConformance &derived) { |
| 64 | + ASTContext &ctx = derived.Context; |
| 65 | + |
| 66 | + Type partialTaskType = getPartialAsyncTaskType(ctx); |
| 67 | + if (!partialTaskType) { |
| 68 | + derived.Nominal->diagnose(diag::partial_task_type_missing); |
| 69 | + return nullptr; |
| 70 | + } |
| 71 | + |
| 72 | + auto parentDC = derived.getConformanceContext(); |
| 73 | + auto partialTaskParamDecl = new (ctx) ParamDecl( |
| 74 | + SourceLoc(), SourceLoc(), ctx.Id_partialTask, |
| 75 | + SourceLoc(), ctx.Id_partialTask, parentDC); |
| 76 | + partialTaskParamDecl->setInterfaceType(partialTaskType); |
| 77 | + partialTaskParamDecl->setSpecifier(ParamSpecifier::Default); |
| 78 | + |
| 79 | + ParameterList *params = ParameterList::createWithoutLoc(partialTaskParamDecl); |
| 80 | + auto func = FuncDecl::createImplicit( |
| 81 | + ctx, StaticSpellingKind::None, getEnqueuePartialTaskName(ctx), |
| 82 | + SourceLoc(), /*Async=*/false, /*Throws=*/false, /*GenericParams=*/nullptr, |
| 83 | + params, TupleType::getEmpty(ctx), parentDC); |
| 84 | + func->copyFormalAccessFrom(derived.Nominal); |
| 85 | + func->setBodySynthesizer(deriveBodyActor_enqueuePartialTask); |
| 86 | + |
| 87 | + // FIXME: This function should be "actor-unsafe", not "actor-independent", but |
| 88 | + // the latter is all we have at the moment. |
| 89 | + func->getAttrs().add(new (ctx) ActorIndependentAttr(/*IsImplicit=*/true)); |
| 90 | + |
| 91 | + derived.addMembersToConformanceContext({func}); |
| 92 | + return func; |
| 93 | +} |
| 94 | + |
| 95 | +ValueDecl *DerivedConformance::deriveActor(ValueDecl *requirement) { |
| 96 | + auto func = dyn_cast<FuncDecl>(requirement); |
| 97 | + if (!func) |
| 98 | + return nullptr; |
| 99 | + |
| 100 | + if (func->getName() == getEnqueuePartialTaskName(Context)) |
| 101 | + return deriveActor_enqueuePartialTask(*this); |
| 102 | + |
| 103 | + return nullptr; |
| 104 | +} |
0 commit comments