Files
swift-mirror/test/Interop/Cxx/class/access/Inputs/non-public-inheritance.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

39 lines
1.1 KiB
C++

#ifndef NON_PUBLIC_INHERITANCE_H
#define NON_PUBLIC_INHERITANCE_H
#define BLESS \
__attribute__((__swift_attr__("private_fileid:main/blessed.swift")))
const int PUBL_RETURN_VAL = 1;
const int PROT_RETURN_VAL = 2;
const int PRIV_RETURN_VAL = 3;
class BLESS Base {
public:
int publ(void) const { return PUBL_RETURN_VAL; }
protected:
int prot(void) const { return PROT_RETURN_VAL; }
private:
int priv(void) const { return PRIV_RETURN_VAL; }
};
class BLESS PublBase : public Base {};
class BLESS ProtBase : protected Base {};
class BLESS PrivBase : private Base {};
class BLESS PublPublBase : public PublBase {};
class BLESS ProtPublBase : protected PublBase {};
class BLESS PrivPublBase : private PublBase {};
class BLESS PublProtBase : public ProtBase {};
class BLESS ProtProtBase : protected ProtBase {};
class BLESS PrivProtBase : private ProtBase {};
class BLESS PublPrivBase : public PrivBase {};
class BLESS ProtPrivBase : protected PrivBase {};
class BLESS PrivPrivBase : private PrivBase {};
#endif /* NON_PUBLIC_INHERITANCE_H */