Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8966,16 +8966,25 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
}

namespace {
ValueDecl *getKnownSingleDecl(ASTContext &SwiftContext, StringRef DeclName) {
SmallVector<ValueDecl *, 1> decls;
SwiftContext.lookupInSwiftModule(DeclName, decls);
assert(decls.size() < 2);
if (decls.size() != 1) return nullptr;
return decls[0];
}

class SwiftifyInfoPrinter {
public:
static const ssize_t SELF_PARAM_INDEX = -2;
static const ssize_t RETURN_VALUE_INDEX = -1;
clang::ASTContext &ctx;
ASTContext &SwiftContext;
llvm::raw_ostream &out;
MacroDecl &SwiftifyImportDecl;
bool firstParam = true;
SwiftifyInfoPrinter(clang::ASTContext &ctx, ASTContext &SwiftContext, llvm::raw_ostream &out)
: ctx(ctx), SwiftContext(SwiftContext), out(out) {
SwiftifyInfoPrinter(clang::ASTContext &ctx, ASTContext &SwiftContext, llvm::raw_ostream &out, MacroDecl &SwiftifyImportDecl)
: ctx(ctx), SwiftContext(SwiftContext), out(out), SwiftifyImportDecl(SwiftifyImportDecl) {
out << "@_SwiftifyImport(";
}
~SwiftifyInfoPrinter() { out << ")"; }
Expand Down Expand Up @@ -9033,22 +9042,23 @@ class SwiftifyInfoPrinter {
}

void printAvailability() {
if (!hasMacroParameter("spanAvailability"))
return;
printSeparator();
out << "spanAvailability: ";
printAvailabilityOfType("Span");
}

private:
ValueDecl *getDecl(StringRef DeclName) {
SmallVector<ValueDecl *, 1> decls;
SwiftContext.lookupInSwiftModule(DeclName, decls);
assert(decls.size() == 1);
if (decls.size() != 1) return nullptr;
return decls[0];
bool hasMacroParameter(StringRef ParamName) {
for (auto *Param : *SwiftifyImportDecl.parameterList)
if (Param->getArgumentName().str() == ParamName)
return true;
return false;
}

void printAvailabilityOfType(StringRef Name) {
ValueDecl *D = getDecl(Name);
ValueDecl *D = getKnownSingleDecl(SwiftContext, Name);
out << "\"";
llvm::SaveAndRestore<bool> hasAvailbilitySeparatorRestore(firstParam, true);
for (auto attr : D->getSemanticAvailableAttrs(/*includingInactive=*/true)) {
Expand Down Expand Up @@ -9213,6 +9223,10 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
if (ClangDecl->getNumParams() != MappedDecl->getParameters()->size())
return;

MacroDecl *SwiftifyImportDecl = dyn_cast_or_null<MacroDecl>(getKnownSingleDecl(SwiftContext, "_SwiftifyImport"));
if (!SwiftifyImportDecl)
return;

{
UnaliasedInstantiationVisitor visitor;
visitor.TraverseType(ClangDecl->getType());
Expand Down Expand Up @@ -9241,7 +9255,7 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
}
return false;
};
SwiftifyInfoPrinter printer(getClangASTContext(), SwiftContext, out);
SwiftifyInfoPrinter printer(getClangASTContext(), SwiftContext, out, *SwiftifyImportDecl);
Type swiftReturnTy;
if (const auto *funcDecl = dyn_cast<FuncDecl>(MappedDecl))
swiftReturnTy = funcDecl->getResultInterfaceType();
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/SwiftifyImport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ public func _swiftifyOverrideLifetime<
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
// should be expressed by a builtin that is hidden within the function body.
dependent
}
}