Skip to content

Commit 2b28da6

Browse files
committed
Update ASTPrinting of lifetime dependence
1 parent 815d7a6 commit 2b28da6

File tree

4 files changed

+78
-36
lines changed

4 files changed

+78
-36
lines changed

include/swift/AST/ASTPrinter.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,22 @@ class ASTPrinter {
364364
return printedClangDecl.insert(d).second;
365365
}
366366

367+
void printLifetimeDependence(
368+
std::optional<LifetimeDependenceInfo> lifetimeDependence) {
369+
if (!lifetimeDependence.has_value()) {
370+
return;
371+
}
372+
*this << lifetimeDependence->getString();
373+
}
374+
375+
void printLifetimeDependenceAt(
376+
ArrayRef<LifetimeDependenceInfo> lifetimeDependencies, unsigned index) {
377+
if (auto lifetimeDependence =
378+
getLifetimeDependenceFor(lifetimeDependencies, index)) {
379+
printLifetimeDependence(*lifetimeDependence);
380+
}
381+
}
382+
367383
private:
368384
virtual void anchor();
369385
};

include/swift/AST/Types.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3792,6 +3792,10 @@ class FunctionType final
37923792
targetIndex);
37933793
}
37943794

3795+
std::optional<LifetimeDependenceInfo> getLifetimeDependenceForResult() const {
3796+
return getLifetimeDependenceFor(getNumParams());
3797+
}
3798+
37953799
void Profile(llvm::FoldingSetNodeID &ID) {
37963800
std::optional<ExtInfo> info = std::nullopt;
37973801
if (hasExtInfo())
@@ -4465,8 +4469,12 @@ class SILParameterInfo {
44654469

44664470
SWIFT_DEBUG_DUMP;
44674471
void print(llvm::raw_ostream &out,
4468-
const PrintOptions &options = PrintOptions()) const;
4469-
void print(ASTPrinter &Printer, const PrintOptions &Options) const;
4472+
const PrintOptions &options = PrintOptions(),
4473+
std::optional<LifetimeDependenceInfo> lifetimeDependence =
4474+
std::nullopt) const;
4475+
void print(ASTPrinter &Printer, const PrintOptions &Options,
4476+
std::optional<LifetimeDependenceInfo> lifetimeDependence =
4477+
std::nullopt) const;
44704478
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &out,
44714479
SILParameterInfo type) {
44724480
type.print(out);
@@ -5363,6 +5371,16 @@ class SILFunctionType final
53635371
NumLifetimeDependencies};
53645372
}
53655373

5374+
std::optional<LifetimeDependenceInfo>
5375+
getLifetimeDependenceFor(unsigned targetIndex) const {
5376+
return swift::getLifetimeDependenceFor(getLifetimeDependencies(),
5377+
targetIndex);
5378+
}
5379+
5380+
std::optional<LifetimeDependenceInfo> getLifetimeDependenceForResult() const {
5381+
return getLifetimeDependenceFor(getNumParameters());
5382+
}
5383+
53665384
/// Returns true if the function type stores a Clang type that cannot
53675385
/// be derived from its Swift type. Returns false otherwise, including if
53685386
/// the function type is not @convention(c) or @convention(block).

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4667,6 +4667,7 @@ void Requirement::dump(raw_ostream &out) const {
46674667
}
46684668

46694669
void SILParameterInfo::dump() const {
4670+
// TODO: Fix LifetimeDependenceInfo printing here.
46704671
print(llvm::errs());
46714672
llvm::errs() << '\n';
46724673
}

lib/AST/ASTPrinter.cpp

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6426,8 +6426,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
64266426
}
64276427
}
64286428

6429-
void visitAnyFunctionTypeParams(ArrayRef<AnyFunctionType::Param> Params,
6430-
bool printLabels) {
6429+
void visitAnyFunctionTypeParams(
6430+
ArrayRef<AnyFunctionType::Param> Params, bool printLabels,
6431+
ArrayRef<LifetimeDependenceInfo> lifetimeDependencies) {
64316432
Printer << "(";
64326433

64336434
for (unsigned i = 0, e = Params.size(); i != e; ++i) {
@@ -6461,6 +6462,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
64616462
Printer << ": ";
64626463
}
64636464

6465+
Printer.printLifetimeDependenceAt(lifetimeDependencies, i);
6466+
64646467
auto type = Param.getPlainType();
64656468
if (Param.isVariadic()) {
64666469
visit(type);
@@ -6484,7 +6487,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
64846487
printFunctionExtInfo(T);
64856488

64866489
// If we're stripping argument labels from types, do it when printing.
6487-
visitAnyFunctionTypeParams(T->getParams(), /*printLabels*/false);
6490+
visitAnyFunctionTypeParams(T->getParams(), /*printLabels*/ false,
6491+
T->getLifetimeDependencies());
64886492

64896493
if (T->hasExtInfo()) {
64906494
if (T->isAsync()) {
@@ -6510,11 +6514,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
65106514
Printer.printKeyword("sending ", Options);
65116515
}
65126516

6513-
if (T->hasLifetimeDependenceInfo()) {
6514-
auto lifetimeDependenceInfo = T->getExtInfo().getLifetimeDependenceInfo();
6515-
assert(!lifetimeDependenceInfo.empty());
6516-
Printer << lifetimeDependenceInfo.getString() << " ";
6517-
}
6517+
Printer.printLifetimeDependence(T->getLifetimeDependenceForResult());
65186518

65196519
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
65206520
T->getResult().print(Printer, Options);
@@ -6548,16 +6548,17 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
65486548
PrintAST::defaultGenericRequirementFlags(Options));
65496549
Printer << " ";
65506550

6551-
visitAnyFunctionTypeParams(T->getParams(), /*printLabels*/true);
6551+
visitAnyFunctionTypeParams(T->getParams(), /*printLabels*/ true,
6552+
T->getLifetimeDependencies());
65526553

6553-
if (T->hasExtInfo()) {
6554-
if (T->isAsync()) {
6555-
Printer << " ";
6556-
Printer.printKeyword("async", Options);
6557-
}
6554+
if (T->hasExtInfo()) {
6555+
if (T->isAsync()) {
6556+
Printer << " ";
6557+
Printer.printKeyword("async", Options);
6558+
}
65586559

6559-
if (T->isThrowing()) {
6560-
Printer << " " << tok::kw_throws;
6560+
if (T->isThrowing()) {
6561+
Printer << " " << tok::kw_throws;
65616562

65626563
if (auto thrownError = T->getThrownError()) {
65636564
Printer << "(";
@@ -6569,11 +6570,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
65696570

65706571
Printer << " -> ";
65716572

6572-
if (T->hasLifetimeDependenceInfo()) {
6573-
auto lifetimeDependenceInfo = T->getExtInfo().getLifetimeDependenceInfo();
6574-
assert(!lifetimeDependenceInfo.empty());
6575-
Printer << lifetimeDependenceInfo.getString() << " ";
6576-
}
6573+
Printer.printLifetimeDependenceAt(T->getLifetimeDependencies(),
6574+
T->getParams().size());
65776575

65786576
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
65796577
T->getResult().print(Printer, Options);
@@ -6669,16 +6667,16 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
66696667
[T, sub, &subOptions] {
66706668
sub->Printer << "(";
66716669
bool first = true;
6670+
unsigned paramIndex = 0;
66726671
for (auto param : T->getParameters()) {
66736672
sub->Printer.printSeparator(first, ", ");
6674-
param.print(sub->Printer, subOptions);
6673+
param.print(sub->Printer, subOptions,
6674+
T->getLifetimeDependenceFor(paramIndex));
6675+
paramIndex++;
66756676
}
66766677
sub->Printer << ") -> ";
66776678

6678-
auto lifetimeDependenceInfo = T->getLifetimeDependenceInfo();
6679-
if (!lifetimeDependenceInfo.empty()) {
6680-
sub->Printer << lifetimeDependenceInfo.getString() << " ";
6681-
}
6679+
sub->Printer.printLifetimeDependence(T->getLifetimeDependenceForResult());
66826680

66836681
bool parenthesizeResults = mustParenthesizeResults(T);
66846682
if (parenthesizeResults)
@@ -6689,6 +6687,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
66896687
for (auto yield : T->getYields()) {
66906688
sub->Printer.printSeparator(first, ", ");
66916689
sub->Printer << "@yields ";
6690+
// TODO: Fix lifetime dependence printing here
66926691
yield.print(sub->Printer, subOptions);
66936692
}
66946693

@@ -6708,8 +6707,6 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
67086707
else {
67096708
assert(false && "Should have error, error_indirect, or error_unowned");
67106709
}
6711-
6712-
67136710
T->getErrorResult().getInterfaceType().print(sub->Printer, subOptions);
67146711
}
67156712

@@ -7259,8 +7256,10 @@ void AnyFunctionType::printParams(ArrayRef<AnyFunctionType::Param> Params,
72597256
void AnyFunctionType::printParams(ArrayRef<AnyFunctionType::Param> Params,
72607257
ASTPrinter &Printer,
72617258
const PrintOptions &PO) {
7262-
TypePrinter(Printer, PO).visitAnyFunctionTypeParams(Params,
7263-
/*printLabels*/true);
7259+
// TODO: Handle lifetime dependence printing here
7260+
TypePrinter(Printer, PO)
7261+
.visitAnyFunctionTypeParams(Params,
7262+
/*printLabels*/ true, {});
72647263
}
72657264

72667265
std::string
@@ -7414,12 +7413,16 @@ StringRef swift::getCheckedCastKindName(CheckedCastKind kind) {
74147413
llvm_unreachable("bad checked cast name");
74157414
}
74167415

7417-
void SILParameterInfo::print(raw_ostream &OS, const PrintOptions &Opts) const {
7416+
void SILParameterInfo::print(
7417+
raw_ostream &OS, const PrintOptions &Opts,
7418+
std::optional<LifetimeDependenceInfo> lifetimeDependence) const {
74187419
StreamPrinter Printer(OS);
7419-
print(Printer, Opts);
7420+
print(Printer, Opts, lifetimeDependence);
74207421
}
7421-
void SILParameterInfo::print(ASTPrinter &Printer,
7422-
const PrintOptions &Opts) const {
7422+
7423+
void SILParameterInfo::print(
7424+
ASTPrinter &Printer, const PrintOptions &Opts,
7425+
std::optional<LifetimeDependenceInfo> lifetimeDependence) const {
74237426
auto options = getOptions();
74247427

74257428
if (options.contains(SILParameterInfo::NotDifferentiable)) {
@@ -7437,6 +7440,10 @@ void SILParameterInfo::print(ASTPrinter &Printer,
74377440
Printer << "@sil_isolated ";
74387441
}
74397442

7443+
if (lifetimeDependence) {
7444+
Printer.printLifetimeDependence(*lifetimeDependence);
7445+
}
7446+
74407447
// If we did not handle a case in Options, this code was not updated
74417448
// appropriately.
74427449
assert(!bool(options) && "Code not updated for introduced option");

0 commit comments

Comments
 (0)