Skip to content

[interop][SwiftToC++] print C++ interface for top-level Swift functions #40995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 11, 2022
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
10 changes: 10 additions & 0 deletions include/swift/AST/SwiftNameTranslation.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ namespace objc_translation {
bool checkParent = true);

} // end namespace objc_translation

namespace cxx_translation {

/// Returns true if the given value decl D is visible to C++ of its
/// own accord (i.e. without considering its context)
bool isVisibleToCxx(const ValueDecl *VD, AccessLevel minRequiredAccess,
bool checkParent = true);

} // end namespace cxx_translation

} // end namespace swift

#endif
17 changes: 17 additions & 0 deletions lib/AST/SwiftNameTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,20 @@ isVisibleToObjC(const ValueDecl *VD, AccessLevel minRequiredAccess,
}
return false;
}

bool swift::cxx_translation::isVisibleToCxx(const ValueDecl *VD,
AccessLevel minRequiredAccess,
bool checkParent) {
if (VD->isObjC())
return false;
if (VD->getFormalAccess() >= minRequiredAccess) {
return true;
} else if (checkParent) {
if (auto ctor = dyn_cast<ConstructorDecl>(VD)) {
// Check if we're overriding an initializer that is visible to obj-c
if (auto parent = ctor->getOverriddenDecl())
return isVisibleToCxx(parent, minRequiredAccess, false);
}
}
return false;
}
1 change: 1 addition & 0 deletions lib/PrintAsClang/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

add_swift_host_library(swiftPrintAsClang STATIC
CxxSynthesis.cpp
DeclAndTypePrinter.cpp
ModuleContentsWriter.cpp
PrintAsClang.cpp)
Expand Down
37 changes: 37 additions & 0 deletions lib/PrintAsClang/CxxSynthesis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===--- CxxSynthesis.cpp - Rules for synthesizing C++ code -----*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "CxxSynthesis.h"

using namespace swift;
using namespace cxx_synthesis;

StringRef cxx_synthesis::getCxxImplNamespaceName() { return "_impl"; }

/// Print a C++ namespace declaration with the give name and body.
void CxxPrinter::printNamespace(
llvm::function_ref<void(raw_ostream &OS)> namePrinter,
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter) const {
os << "namespace ";
namePrinter(os);
os << " {\n\n";
bodyPrinter(os);
os << "\n} // namespace ";
namePrinter(os);
os << "\n\n";
}

void CxxPrinter::printNamespace(
StringRef name,
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter) const {
printNamespace([&](raw_ostream &os) { os << name; }, bodyPrinter);
}
49 changes: 49 additions & 0 deletions lib/PrintAsClang/CxxSynthesis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===--- CxxSynthesis.h - Rules for synthesizing C++ code -------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_PRINTASCLANG_CXXSYNTHESIS_H
#define SWIFT_PRINTASCLANG_CXXSYNTHESIS_H

#include "swift/Basic/LLVM.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"

namespace swift {

namespace cxx_synthesis {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's anything wrong with this, but usually, there'd be some object which we put all these functions in. Maybe Implementation?

Copy link
Contributor Author

@hyp hyp Mar 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can pull it into an object class.


/// Return the name of the implementation namespace that is used to hide
/// declarations from the namespace that corresponds to the imported Swift
/// module in C++.
StringRef getCxxImplNamespaceName();

class CxxPrinter {
public:
CxxPrinter(raw_ostream &os) : os(os) {}

/// Print a C++ namespace declaration with the give name and body.
void
printNamespace(llvm::function_ref<void(raw_ostream &OS)> namePrinter,
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter) const;

void
printNamespace(StringRef name,
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter) const;

private:
raw_ostream &os;
};

} // end namespace cxx_synthesis
} // end namespace swift

#endif
Loading