Skip to content

Commit d7bd76e

Browse files
authored
[Swiftify] Add return pointer support (#78571)
* Import __counted_by for function return values Instead of simply passing a parameter index to _SwiftifyInfo, the _SwiftifyExpr enum is introduced. It currently has two cases: - .param(index: Int), corresponding to the previous parameter index - .return, corresponding to the function's return value. ClangImporter is also updated to pass this new information along to _SwiftifyImport, allowing overloads with buffer pointer return types to be generated. The swiftified return values currently return Span when the return value is marked as nonescaping, despite this not being sound. This is a bug that will be fixed in the next commit, as the issue is greater than just for return values. * Fix Span variant selection There was an assumption that all converted pointers were either converted to Span-family pointers, or UnsafeBufferPointer-family pointers. This was not consistently handled, resulting in violating the `assert(nonescaping)` assert when the two were mixed. This patch removes the Variant struct, and instead each swiftified pointer separately tracks whether it should map to Span or UnsafeBufferPointer. This also fixes return pointers being incorrectly mapped to Span when marked as nonescaping.
1 parent 0cdcfad commit d7bd76e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+412
-237
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8679,7 +8679,7 @@ class SwiftifyInfoPrinter {
86798679
~SwiftifyInfoPrinter() { out << ")"; }
86808680

86818681
void printCountedBy(const clang::CountAttributedType *CAT,
8682-
size_t pointerIndex) {
8682+
ssize_t pointerIndex) {
86838683
printSeparator();
86848684
clang::Expr *countExpr = CAT->getCountExpr();
86858685
bool isSizedBy = CAT->isCountInBytes();
@@ -8688,7 +8688,9 @@ class SwiftifyInfoPrinter {
86888688
out << "sizedBy";
86898689
else
86908690
out << "countedBy";
8691-
out << "(pointer: " << pointerIndex + 1 << ", ";
8691+
out << "(pointer: ";
8692+
printParamOrReturn(pointerIndex);
8693+
out << ", ";
86928694
if (isSizedBy)
86938695
out << "size";
86948696
else
@@ -8701,7 +8703,9 @@ class SwiftifyInfoPrinter {
87018703

87028704
void printNonEscaping(int idx) {
87038705
printSeparator();
8704-
out << ".nonescaping(pointer: " << idx << ")";
8706+
out << ".nonescaping(pointer: ";
8707+
printParamOrReturn(idx);
8708+
out << ")";
87058709
}
87068710

87078711
void printTypeMapping(const llvm::StringMap<std::string> &mapping) {
@@ -8725,6 +8729,13 @@ class SwiftifyInfoPrinter {
87258729
firstParam = false;
87268730
}
87278731
}
8732+
8733+
void printParamOrReturn(ssize_t pointerIndex) {
8734+
if (pointerIndex == -1)
8735+
out << ".return";
8736+
else
8737+
out << ".param(" << pointerIndex + 1 << ")";
8738+
}
87288739
};
87298740
} // namespace
87308741

@@ -8751,7 +8762,7 @@ void ClangImporter::Implementation::importSpanAttributes(FuncDecl *MappedDecl) {
87518762
if (decl->getName() != "span")
87528763
continue;
87538764
if (param->hasAttr<clang::NoEscapeAttr>()) {
8754-
printer.printNonEscaping(index + 1);
8765+
printer.printNonEscaping(index);
87558766
clang::PrintingPolicy policy(param->getASTContext().getLangOpts());
87568767
policy.SuppressTagKeyword = true;
87578768
auto param = MappedDecl->getParameters()->get(index);
@@ -8788,6 +8799,10 @@ void ClangImporter::Implementation::importBoundsAttributes(
87888799
printer.printCountedBy(CAT, index);
87898800
}
87908801
}
8802+
if (auto CAT =
8803+
ClangDecl->getReturnType()->getAs<clang::CountAttributedType>()) {
8804+
printer.printCountedBy(CAT, -1);
8805+
}
87918806
}
87928807

87938808
importNontrivialAttribute(MappedDecl, MacroString);

lib/ClangImporter/ImportType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2330,7 +2330,7 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
23302330
: ImportTypeKind::Result),
23312331
ImportDiagnosticAdder(*this, clangDecl, clangDecl->getLocation()),
23322332
allowNSUIntegerAsInt, Bridgeability::Full, getImportTypeAttrs(clangDecl),
2333-
OptionalityOfReturn, isBoundsAnnotated);
2333+
OptionalityOfReturn, true, std::nullopt, isBoundsAnnotated);
23342334
}
23352335

23362336
static Type

0 commit comments

Comments
 (0)