1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
//===--- Effects.cpp - Effect Checking ASTs -------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements some logic for rethrows and reasync checking.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/ASTContext.h"
#include "swift/AST/Effects.h"
#include "swift/AST/Evaluator.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ProtocolConformanceRef.h"
#include "swift/AST/Type.h"
#include "swift/AST/Types.h"
#include "swift/AST/TypeCheckRequests.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
bool AnyFunctionType::hasEffect(EffectKind kind) const {
switch (kind) {
case EffectKind::Throws: return getExtInfo().isThrowing();
case EffectKind::Async: return getExtInfo().isAsync();
}
llvm_unreachable("Bad effect kind");
}
void swift::simple_display(llvm::raw_ostream &out, const EffectKind kind) {
switch (kind) {
case EffectKind::Throws: out << "throws"; return;
case EffectKind::Async: out << "async"; return;
}
llvm_unreachable("Bad effect kind");
}
void swift::simple_display(llvm::raw_ostream &out,
const PolymorphicEffectRequirementList list) {
for (auto req : list.getRequirements()) {
simple_display(out, req);
out << "\n";
}
for (auto conf : list.getConformances()) {
simple_display(out, conf.first);
out << " : ";
simple_display(out, conf.second);
llvm::errs() << "\n";
}
}
PolymorphicEffectRequirementList
ProtocolDecl::getPolymorphicEffectRequirements(EffectKind kind) const {
return evaluateOrDefault(getASTContext().evaluator,
PolymorphicEffectRequirementsRequest{kind, const_cast<ProtocolDecl *>(this)},
PolymorphicEffectRequirementList());
}
bool ProtocolDecl::hasPolymorphicEffect(EffectKind kind) const {
switch (kind) {
case EffectKind::Throws:
return getAttrs().hasAttribute<swift::AtRethrowsAttr>();
case EffectKind::Async:
return getAttrs().hasAttribute<swift::AtReasyncAttr>();
}
llvm_unreachable("Bad effect kind");
}
bool AbstractFunctionDecl::hasEffect(EffectKind kind) const {
switch (kind) {
case EffectKind::Throws:
return hasThrows();
case EffectKind::Async:
return hasAsync();
}
llvm_unreachable("Bad effect kind");
}
bool AbstractFunctionDecl::hasPolymorphicEffect(EffectKind kind) const {
switch (kind) {
case EffectKind::Throws:
return getAttrs().hasAttribute<swift::RethrowsAttr>();
case EffectKind::Async:
return getAttrs().hasAttribute<swift::ReasyncAttr>();
}
llvm_unreachable("Bad effect kind");
}
PolymorphicEffectKind
AbstractFunctionDecl::getPolymorphicEffectKind(EffectKind kind) const {
return evaluateOrDefault(getASTContext().evaluator,
PolymorphicEffectKindRequest{kind, const_cast<AbstractFunctionDecl *>(this)},
PolymorphicEffectKind::Invalid);
}
void swift::simple_display(llvm::raw_ostream &out,
PolymorphicEffectKind kind) {
switch (kind) {
case PolymorphicEffectKind::None:
out << "none";
break;
case PolymorphicEffectKind::ByClosure:
out << "by closure";
break;
case PolymorphicEffectKind::ByConformance:
out << "by conformance";
break;
case PolymorphicEffectKind::AsyncSequenceRethrows:
out << "by async sequence implicit @rethrows";
break;
case PolymorphicEffectKind::Always:
out << "always";
break;
case PolymorphicEffectKind::Invalid:
out << "invalid";
break;
}
}
bool ProtocolConformanceRef::hasEffect(EffectKind kind) const {
if (!isConcrete()) { return true; }
return evaluateOrDefault(getRequirement()->getASTContext().evaluator,
ConformanceHasEffectRequest{kind, getConcrete()},
true);
}
|