Files
swift-mirror/test/Interop/Cxx/class/access/Inputs/access-inversion.h
John Hui 66c2e2c52b [cxx-interop] Import non-public inherited members (#79348)
This patch is follow-up work from #78942 and imports non-public members,
which were previously not being imported. Those members can be accessed
in a Swift file blessed by the SWIFT_PRIVATE_FILEID annotation.

As a consequence of this patch, we are also now importing inherited members
that are inaccessible from the derived classes, because they were declared
private, or because they were inherited via nested private inheritance. We
import them anyway but mark them unavailable, for better diagnostics and to
(somewhat) simplify the import logic for inheritance.

Because non-public base class members are now imported too, this patch
inflames an existing issue where a 'using' declaration on an inherited member
with a synthesized name (e.g., operators) produces duplicate members, leading
to miscompilation (resulting in a runtime crash). This was not previously noticed
because a 'using' declaration on a public inherited member is not usually
necessary, but is a common way to expose otherwise non-public members.
This patch puts in a workaround to prevent this from affecting the behavior
of MSVC's std::optional implementation, which uses this pattern of 'using'
a private inherited member. That will be fixed in a follow-up patch.

Follow-up work is also needed to correctly diagnose ambiguous overloads
in cases of multiple inheritance, and to account for virtual inheritance.

rdar://137764620
2025-02-25 01:03:16 -08:00

87 lines
2.8 KiB
C++

#ifndef TEST_INTEROP_CXX_CLASS_INPUTS_ACCESS_INVERSION_H
#define TEST_INTEROP_CXX_CLASS_INPUTS_ACCESS_INVERSION_H
/// A record whose public members expose private members
struct Leaky {
public:
Leaky() {
} // Apparently necessary to ensure constructor is unambiguous in Swift
private:
typedef bool PrivateAlias;
struct PrivateRec {
public:
void privateRecMethod() const {}
static const bool PRIVATE_REC_CONST = true;
};
enum PrivateEnum { privateEnumMember };
enum class PrivateEnumClass { privateEnumClassMember };
static const bool PRIVATE_CONST = true;
// These are used as return values in functions that return private types
static PrivateAlias privateAliasVal;
static PrivateRec privateRecVal;
static PrivateEnum privateEnumVal;
static PrivateEnumClass privateEnumClassVal;
public:
typedef PrivateAlias AliasToPrivateAlias;
typedef PrivateRec AliasToPrivateRec;
typedef PrivateEnum AliasToPrivateEnum;
typedef PrivateEnumClass AliasToPrivateEnumClass;
struct RecWithPrivateAlias {
PrivateAlias mem;
};
struct RecWithPrivateRec {
PrivateRec mem;
};
struct RecWithPrivateEnum {
PrivateEnum mem;
};
struct RecWithPrivateEnumClass {
PrivateEnumClass mem;
};
struct RecWithPrivateConst {
const bool mem = PRIVATE_CONST;
};
static PrivateAlias staticReturningPrivateAlias() { return privateAliasVal; }
static PrivateRec staticReturningPrivateRec() { return privateRecVal; }
static PrivateEnum staticReturningPrivateEnum() { return privateEnumVal; }
static PrivateEnumClass staticReturningPrivateEnumClass() {
return privateEnumClassVal;
}
static void staticTakingPrivateAlias(PrivateAlias p) {}
static void staticTakingPrivateRec(PrivateRec p) {}
static void staticTakingPrivateEnum(PrivateEnum p) {}
static void staticTakingPrivateEnumClass(PrivateEnumClass p) {}
PrivateAlias methodReturningPrivateAlias() const { return privateAliasVal; }
PrivateRec methodReturningPrivateRec() const { return privateRecVal; }
PrivateEnum methodReturningPrivateEnum() const { return privateEnumVal; }
PrivateEnumClass methodReturningPrivateEnumClass() const {
return privateEnumClassVal;
}
void methodTakingPrivateAlias(PrivateAlias p) const {}
void methodTakingPrivateRec(PrivateRec p) const {}
void methodTakingPrivateEnum(PrivateEnum p) const {}
void methodTakingPrivateEnumClass(PrivateEnumClass p) const {}
void defaultArgOfPrivateRec(PrivateRec a = privateRecVal) const {}
void defaultArgOfPrivateEnum(PrivateEnum a = privateEnumMember) const {}
void defaultArgOfPrivateEnumClass(
PrivateEnumClass a = PrivateEnumClass::privateEnumClassMember) const {}
void defaultArgOfPrivateConst(bool a = PRIVATE_CONST) const {}
void
defaultArgOfPrivateRecConst(bool a = PrivateRec::PRIVATE_REC_CONST) const {}
};
#endif