Files
swift-mirror/lib/AST/CaptureInfo.cpp
Jordan Rose 772a124b63 Compute captures after all bodies have been type-checked, as a separate pass.
We already have the restriction that captures can't be computed until
everything is type-checked, but previously we tried to compute captures
/immediately/ after a closure was type-checked. Unfortunately, we either
type-checked too early (before @noescape was propagated onto multi-statement
closures) or too late (trying to compute autoclosure captures at the point
the autoclosure was introduced).

Now, all closure captures are computed after type-checking, and local
function captures as well. They also more consistently reuse the capture
list of nested closures/functions. Because captures can be computed on
demand, there is now a flag on CaptureInfo for whether we've computed
captures yet. Note that some functions will never have captures computed,
namely those that are not in a local context.

rdar://problem/19956242

Swift SVN r25776
2015-03-05 02:33:37 +00:00

66 lines
1.6 KiB
C++

//===--- CaptureInfo.cpp - Data Structure for Capture Lists ---------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/AST/CaptureInfo.h"
#include "swift/AST/Decl.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
bool CaptureInfo::hasLocalCaptures() const {
for (auto capture : getCaptures())
if (capture.getDecl()->getDeclContext()->isLocalContext())
return true;
return false;
}
void CaptureInfo::
getLocalCaptures(SmallVectorImpl<CapturedValue> &Result) const {
if (!hasLocalCaptures()) return;
Result.reserve(Count);
// Filter out global variables.
for (auto capture : getCaptures()) {
if (!capture.getDecl()->getDeclContext()->isLocalContext())
continue;
Result.push_back(capture);
}
}
void CaptureInfo::dump() const {
print(llvm::errs());
llvm::errs() << '\n';
}
void CaptureInfo::print(raw_ostream &OS) const {
OS << "captures=(";
bool isFirst = true;
for (auto capture : getCaptures()) {
if (isFirst)
isFirst = false;
else
OS << ", ";
OS << capture.getDecl()->getName();
if (capture.isDirect())
OS << "<direct>";
if (capture.isNoEscape())
OS << "<noescape>";
}
OS << ')';
}