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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
//===--- CaptureInfo.cpp - Data Structure for Capture Lists ---------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
#include "swift/AST/CaptureInfo.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/GenericEnvironment.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
CapturedValue::CapturedValue(Expr *Val, unsigned Flags)
: Value(Val, Flags), Loc(SourceLoc()) {
assert(isa<OpaqueValueExpr>(Val) || isa<PackElementExpr>(Val));
}
bool CapturedValue::isPackElement() const {
return isExpr() && isa<PackElementExpr>(getExpr());
}
bool CapturedValue::isOpaqueValue() const {
return isExpr() && isa<OpaqueValueExpr>(getExpr());
}
OpaqueValueExpr *CapturedValue::getOpaqueValue() const {
return dyn_cast_or_null<OpaqueValueExpr>(getExpr());
}
PackElementExpr *CapturedValue::getPackElement() const {
return dyn_cast_or_null<PackElementExpr>(getExpr());
}
Type CapturedValue::getPackElementType() const {
return getPackElement()->getType();
}
ArrayRef<CapturedValue>
CaptureInfo::CaptureInfoStorage::getCaptures() const {
return llvm::ArrayRef(this->getTrailingObjects<CapturedValue>(), NumCapturedValues);
}
ArrayRef<GenericEnvironment *>
CaptureInfo::CaptureInfoStorage::getGenericEnvironments() const {
return llvm::ArrayRef(this->getTrailingObjects<GenericEnvironment *>(), NumGenericEnvironments);
}
//===----------------------------------------------------------------------===//
// MARK: CaptureInfo
//===----------------------------------------------------------------------===//
CaptureInfo::CaptureInfo(ASTContext &ctx, ArrayRef<CapturedValue> captures,
DynamicSelfType *dynamicSelf,
OpaqueValueExpr *opaqueValue,
bool genericParamCaptures,
ArrayRef<GenericEnvironment *> genericEnv) {
static_assert(IsTriviallyDestructible<CapturedValue>::value,
"Capture info is alloc'd on the ASTContext and not destroyed");
static_assert(IsTriviallyDestructible<CaptureInfo::CaptureInfoStorage>::value,
"Capture info is alloc'd on the ASTContext and not destroyed");
// This is the only kind of local generic environment we can capture right now.
#ifndef NDEBUG
for (auto *env : genericEnv) {
assert(env->getKind() == GenericEnvironment::Kind::OpenedElement);
}
#endif
OptionSet<Flags> flags;
if (genericParamCaptures)
flags |= Flags::HasGenericParamCaptures;
if (captures.empty() && genericEnv.empty() && !dynamicSelf && !opaqueValue) {
*this = CaptureInfo::empty();
StorageAndFlags.setInt(flags);
return;
}
size_t storageToAlloc =
CaptureInfoStorage::totalSizeToAlloc<CapturedValue,
GenericEnvironment *>(captures.size(),
genericEnv.size());
void *storageBuf = ctx.Allocate(storageToAlloc, alignof(CaptureInfoStorage));
auto *storage = new (storageBuf) CaptureInfoStorage(dynamicSelf,
opaqueValue,
captures.size(),
genericEnv.size());
StorageAndFlags.setPointerAndInt(storage, flags);
std::uninitialized_copy(captures.begin(), captures.end(),
storage->getTrailingObjects<CapturedValue>());
std::uninitialized_copy(genericEnv.begin(), genericEnv.end(),
storage->getTrailingObjects<GenericEnvironment *>());
}
CaptureInfo CaptureInfo::empty() {
static const CaptureInfoStorage empty{/*dynamicSelf*/nullptr,
/*opaqueValue*/nullptr,
0, 0};
CaptureInfo result;
result.StorageAndFlags.setPointer(&empty);
return result;
}
bool CaptureInfo::hasLocalCaptures() const {
for (auto capture : getCaptures()) {
if (capture.isLocalCapture())
return true;
}
return false;
}
void CaptureInfo::
getLocalCaptures(SmallVectorImpl<CapturedValue> &Result) const {
if (!hasLocalCaptures()) return;
Result.reserve(getCaptures().size());
// Filter out global variables.
for (auto capture : getCaptures()) {
if (!capture.isLocalCapture())
continue;
Result.push_back(capture);
}
}
VarDecl *CaptureInfo::getIsolatedParamCapture() const {
if (!hasLocalCaptures())
return nullptr;
for (const auto &capture : getCaptures()) {
// NOTE: isLocalCapture() returns false if we have dynamic self metadata
// since dynamic self metadata is never an isolated capture. So we can just
// call isLocalCapture without checking for dynamic self metadata.
if (!capture.isLocalCapture())
continue;
// If we captured an isolated parameter, return it.
if (auto param = dyn_cast_or_null<ParamDecl>(capture.getDecl())) {
// If we have captured an isolated parameter, return it.
if (param->isIsolated())
return param;
}
// If we captured 'self', check whether it is (still) isolated.
if (auto var = dyn_cast_or_null<VarDecl>(capture.getDecl())) {
if (var->isSelfParamCapture() && var->isSelfParamCaptureIsolated())
return var;
}
}
return nullptr;
}
LLVM_ATTRIBUTE_USED void CaptureInfo::dump() const {
print(llvm::errs());
llvm::errs() << '\n';
}
void CaptureInfo::print(raw_ostream &OS) const {
OS << "captures=(";
if (hasGenericParamCaptures())
OS << "<generic> ";
if (hasDynamicSelfCapture())
OS << "<dynamic_self> ";
if (hasOpaqueValueCapture())
OS << "<opaque_value> ";
interleave(getCaptures(),
[&](const CapturedValue &capture) {
if (capture.getDecl())
OS << capture.getDecl()->getBaseName();
else if (capture.isPackElement()) {
OS << "[pack element] ";
capture.getPackElement()->dump(OS);
} else if (capture.isOpaqueValue()) {
OS << "[opaque] ";
capture.getOpaqueValue()->dump(OS);
} else {
OS << "[unknown] ";
assert(false);
}
if (capture.isDirect())
OS << "<direct>";
if (capture.isNoEscape())
OS << "<noescape>";
},
[&] { OS << ", "; });
interleave(getGenericEnvironments(),
[&](GenericEnvironment *genericEnv) {
OS << " shape_class=";
OS << genericEnv->getOpenedElementShapeClass();
},
[&] { OS << ","; });
OS << ')';
}
//===----------------------------------------------------------------------===//
// MARK: CapturedValue
//===----------------------------------------------------------------------===//
bool CapturedValue::isLocalCapture() const {
auto *decl = Value.getPointer().dyn_cast<ValueDecl *>();
return decl && decl->isLocalCapture();
}
|