Skip to content

Commit ceb4c9c

Browse files
committed
AST: Store captured PackElementExprs in CaptureInfo
1 parent 41dc2be commit ceb4c9c

File tree

2 files changed

+56
-19
lines changed

2 files changed

+56
-19
lines changed

include/swift/AST/CaptureInfo.h

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ template <> struct DenseMapInfo<swift::CapturedValue>;
4141
namespace swift {
4242
class ValueDecl;
4343
class FuncDecl;
44+
class Expr;
4445
class OpaqueValueExpr;
46+
class PackElementExpr;
4547
class VarDecl;
4648
class GenericEnvironment;
49+
class Type;
4750

4851
/// CapturedValue includes both the declaration being captured, along with flags
4952
/// that indicate how it is captured.
@@ -52,7 +55,7 @@ class CapturedValue {
5255

5356
public:
5457
using Storage =
55-
llvm::PointerIntPair<llvm::PointerUnion<ValueDecl*, OpaqueValueExpr*>, 2,
58+
llvm::PointerIntPair<llvm::PointerUnion<ValueDecl *, Expr *>, 2,
5659
unsigned>;
5760

5861
private:
@@ -78,15 +81,7 @@ class CapturedValue {
7881
CapturedValue(ValueDecl *Val, unsigned Flags, SourceLoc Loc)
7982
: Value(Val, Flags), Loc(Loc) {}
8083

81-
private:
82-
// This is only used in TypeLowering when forming Lowered Capture
83-
// Info. OpaqueValueExpr captured value should never show up in the AST
84-
// itself.
85-
//
86-
// NOTE: AbstractClosureExpr::getIsolationCrossing relies upon this and
87-
// asserts that it never sees one of these.
88-
explicit CapturedValue(OpaqueValueExpr *Val, unsigned Flags)
89-
: Value(Val, Flags), Loc(SourceLoc()) {}
84+
CapturedValue(Expr *Val, unsigned Flags);
9085

9186
public:
9287
static CapturedValue getDynamicSelfMetadata() {
@@ -97,10 +92,14 @@ class CapturedValue {
9792
bool isNoEscape() const { return Value.getInt() & IsNoEscape; }
9893

9994
bool isDynamicSelfMetadata() const { return !Value.getPointer(); }
100-
bool isOpaqueValue() const {
101-
return Value.getPointer().is<OpaqueValueExpr *>();
95+
96+
bool isExpr() const {
97+
return Value.getPointer().dyn_cast<Expr *>();
10298
}
10399

100+
bool isPackElement() const;
101+
bool isOpaqueValue() const;
102+
104103
/// Returns true if this captured value is a local capture.
105104
///
106105
/// NOTE: This implies that the value is not dynamic self metadata, since
@@ -116,17 +115,19 @@ class CapturedValue {
116115
}
117116

118117
ValueDecl *getDecl() const {
119-
assert(Value.getPointer() && "dynamic Self metadata capture does not "
120-
"have a value");
121118
return Value.getPointer().dyn_cast<ValueDecl *>();
122119
}
123120

124-
OpaqueValueExpr *getOpaqueValue() const {
125-
assert(Value.getPointer() && "dynamic Self metadata capture does not "
126-
"have a value");
127-
return Value.getPointer().dyn_cast<OpaqueValueExpr *>();
121+
Expr *getExpr() const {
122+
return Value.getPointer().dyn_cast<Expr *>();
128123
}
129124

125+
OpaqueValueExpr *getOpaqueValue() const;
126+
127+
PackElementExpr *getPackElement() const;
128+
129+
Type getPackElementType() const;
130+
130131
SourceLoc getLoc() const { return Loc; }
131132

132133
unsigned getFlags() const { return Value.getInt(); }

lib/AST/CaptureInfo.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,36 @@
1313
#include "swift/AST/CaptureInfo.h"
1414
#include "swift/AST/ASTContext.h"
1515
#include "swift/AST/Decl.h"
16+
#include "swift/AST/Expr.h"
1617
#include "swift/AST/GenericEnvironment.h"
1718
#include "llvm/Support/raw_ostream.h"
1819

1920
using namespace swift;
2021

22+
CapturedValue::CapturedValue(Expr *Val, unsigned Flags)
23+
: Value(Val, Flags), Loc(SourceLoc()) {
24+
assert(isa<OpaqueValueExpr>(Val) || isa<PackElementExpr>(Val));
25+
}
26+
27+
bool CapturedValue::isPackElement() const {
28+
return isExpr() && isa<PackElementExpr>(getExpr());
29+
}
30+
bool CapturedValue::isOpaqueValue() const {
31+
return isExpr() && isa<OpaqueValueExpr>(getExpr());
32+
}
33+
34+
OpaqueValueExpr *CapturedValue::getOpaqueValue() const {
35+
return dyn_cast_or_null<OpaqueValueExpr>(getExpr());
36+
}
37+
38+
PackElementExpr *CapturedValue::getPackElement() const {
39+
return dyn_cast_or_null<PackElementExpr>(getExpr());
40+
}
41+
42+
Type CapturedValue::getPackElementType() const {
43+
return getPackElement()->getType();
44+
}
45+
2146
ArrayRef<CapturedValue>
2247
CaptureInfo::CaptureInfoStorage::getCaptures() const {
2348
return llvm::ArrayRef(this->getTrailingObjects<CapturedValue>(), NumCapturedValues);
@@ -153,7 +178,18 @@ void CaptureInfo::print(raw_ostream &OS) const {
153178

154179
interleave(getCaptures(),
155180
[&](const CapturedValue &capture) {
156-
OS << capture.getDecl()->getBaseName();
181+
if (capture.getDecl())
182+
OS << capture.getDecl()->getBaseName();
183+
else if (capture.isPackElement()) {
184+
OS << "[pack element] ";
185+
capture.getPackElement()->dump(OS);
186+
} else if (capture.isOpaqueValue()) {
187+
OS << "[opaque] ";
188+
capture.getOpaqueValue()->dump(OS);
189+
} else {
190+
OS << "[unknown] ";
191+
assert(false);
192+
}
157193

158194
if (capture.isDirect())
159195
OS << "<direct>";

0 commit comments

Comments
 (0)