Skip to content

Commit ade5c6a

Browse files
authored
Merge pull request swiftlang#82616 from swiftlang/gaborh/attr-hide-swift-decls
[cxx-interop] Add attribute to hide Swift declarations from interop
2 parents d7ca06d + 7ac9a81 commit ade5c6a

File tree

10 files changed

+89
-3
lines changed

10 files changed

+89
-3
lines changed

include/swift/AST/AttrKind.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ enum class ENUM_EXTENSIBILITY_ATTR(closed) EffectsKind : uint8_t {
9999
/// This enum represents the possible values of the @_expose attribute.
100100
enum class ENUM_EXTENSIBILITY_ATTR(closed) ExposureKind : uint8_t {
101101
Cxx SWIFT_NAME("cxx"),
102+
NotCxx SWIFT_NAME("notcxx"),
102103
Wasm SWIFT_NAME("wasm"),
103104
Last_ExposureKind = Wasm
104105
};

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,6 +2174,8 @@ ERROR(expose_only_non_other_attr,none,
21742174
ERROR(expose_inside_unexposed_decl,none,
21752175
"'@_expose' cannot be applied inside of unexposed declaration %0",
21762176
(const ValueDecl *))
2177+
ERROR(expose_redundant_name_provided, none,
2178+
"'@_expose(!Cxx)' does not accept a name argument", ())
21772179
ERROR(expose_invalid_name_pattern_init,none,
21782180
"invalid declaration name '%0' specified in '@_expose'; "
21792181
"exposed initializer name must start with 'init'", (StringRef))

lib/AST/Attr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,9 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
12491249
case ExposureKind::Cxx:
12501250
Printer << "(Cxx";
12511251
break;
1252+
case ExposureKind::NotCxx:
1253+
Printer << "(!Cxx";
1254+
break;
12521255
}
12531256
if (!cast<ExposeAttr>(this)->Name.empty())
12541257
Printer << ", \"" << cast<ExposeAttr>(this)->Name << "\"";

lib/Parse/ParseDecl.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3079,13 +3079,22 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
30793079
"Cxx");
30803080
ParseSymbolName = false;
30813081
};
3082+
bool isNegated = false;
3083+
if (Tok.is(tok::oper_prefix) && Tok.getText() == "!") {
3084+
isNegated = true;
3085+
consumeToken(tok::oper_prefix);
3086+
}
30823087
if (Tok.isNot(tok::identifier)) {
30833088
diagnoseExpectOption();
30843089
return makeParserSuccess();
30853090
}
30863091
if (Tok.getText() == "Cxx") {
3087-
ExpKind = ExposureKind::Cxx;
3092+
ExpKind = isNegated ? ExposureKind::NotCxx : ExposureKind::Cxx;
30883093
} else if (Tok.getText() == "wasm") {
3094+
if (isNegated) {
3095+
diagnoseExpectOption();
3096+
return makeParserSuccess();
3097+
}
30893098
ExpKind = ExposureKind::Wasm;
30903099
} else {
30913100
diagnoseExpectOption();

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/AST/ASTContext.h"
2323
#include "swift/AST/ASTMangler.h"
2424
#include "swift/AST/ASTVisitor.h"
25+
#include "swift/AST/Attr.h"
2526
#include "swift/AST/ClangSwiftTypeCorrespondence.h"
2627
#include "swift/AST/Comment.h"
2728
#include "swift/AST/ConformanceLookup.h"
@@ -2976,6 +2977,17 @@ static bool excludeForObjCImplementation(const ValueDecl *VD) {
29762977
return false;
29772978
}
29782979

2980+
bool swift::hasExposeNotCxxAttr(const ValueDecl *VD) {
2981+
for (const auto *attr : VD->getAttrs().getAttributes<ExposeAttr>())
2982+
if (attr->getExposureKind() == ExposureKind::NotCxx)
2983+
return true;
2984+
if (const auto *NMT = dyn_cast<NominalTypeDecl>(VD->getDeclContext()))
2985+
return hasExposeNotCxxAttr(NMT);
2986+
if (const auto *ED = dyn_cast<ExtensionDecl>(VD->getDeclContext()))
2987+
return hasExposeNotCxxAttr(ED->getExtendedNominal());
2988+
return false;
2989+
}
2990+
29792991
static bool isExposedToThisModule(const ModuleDecl &M, const ValueDecl *VD,
29802992
const llvm::StringSet<> &exposedModules) {
29812993
if (VD->hasClangNode())
@@ -3027,6 +3039,9 @@ bool DeclAndTypePrinter::shouldInclude(const ValueDecl *VD) {
30273039
if (requiresExposedAttribute && !hasExposeAttr(VD))
30283040
return false;
30293041

3042+
if (hasExposeNotCxxAttr(VD))
3043+
return false;
3044+
30303045
if (!isVisible(VD))
30313046
return false;
30323047

lib/PrintAsClang/DeclAndTypePrinter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ class DeclAndTypePrinter {
160160

161161
bool isStringNestedType(const ValueDecl *VD, StringRef Typename);
162162

163+
bool hasExposeNotCxxAttr(const ValueDecl *VD);
164+
163165
} // end namespace swift
164166

165167
#endif

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,8 @@ class ModuleWriter {
10631063
// library. Also skip structs from the standard library, they can cause
10641064
// ambiguities because of the arithmetic types that conflict with types we
10651065
// already have in `swift::` namespace. Also skip `Error` protocol from
1066-
// stdlib, we have experimental support for it.
1066+
// stdlib, we have experimental support for it. Also skip explicitly exluded
1067+
// declarations.
10671068
removedVDList.erase(
10681069
llvm::remove_if(
10691070
removedVDList,
@@ -1073,7 +1074,8 @@ class ModuleWriter {
10731074
vd->getBaseIdentifier().hasUnderscoredNaming()) ||
10741075
(vd->isStdlibDecl() && isa<StructDecl>(vd)) ||
10751076
(vd->isStdlibDecl() &&
1076-
vd->getASTContext().getErrorDecl() == vd);
1077+
vd->getASTContext().getErrorDecl() == vd) ||
1078+
swift::hasExposeNotCxxAttr(vd);
10771079
}),
10781080
removedVDList.end());
10791081
// Sort the unavaiable decls by their name and kind.

lib/Sema/TypeCheckAttr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "TypeChecker.h"
2525
#include "swift/AST/ASTPrinter.h"
2626
#include "swift/AST/ASTVisitor.h"
27+
#include "swift/AST/Attr.h"
28+
#include "swift/AST/AttrKind.h"
2729
#include "swift/AST/AvailabilityInference.h"
2830
#include "swift/AST/ClangModuleLoader.h"
2931
#include "swift/AST/ConformanceLookup.h"
@@ -2462,6 +2464,14 @@ void AttributeChecker::visitExposeAttr(ExposeAttr *attr) {
24622464
diagnose(attr->getLocation(), diag::expose_wasm_not_at_top_level_func);
24632465
break;
24642466
}
2467+
case ExposureKind::NotCxx:
2468+
for (const auto *attr : D->getAttrs().getAttributes<ExposeAttr>())
2469+
if (attr->getExposureKind() == ExposureKind::Cxx)
2470+
diagnose(attr->getLocation(), diag::expose_only_non_other_attr,
2471+
"@_expose(Cxx)");
2472+
if (!attr->Name.empty())
2473+
diagnose(attr->getLocation(), diag::expose_redundant_name_provided);
2474+
break;
24652475
case ExposureKind::Cxx: {
24662476
auto *VD = cast<ValueDecl>(D);
24672477
// Expose cannot be mixed with '@_cdecl' declarations.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -module-name SwiftPrivate -clang-header-expose-decls=all-public -typecheck -verify -emit-clang-header-path %t/swiftprivate.h
3+
// RUN: %FileCheck %s < %t/swiftprivate.h
4+
5+
// RUN: %check-interop-cxx-header-in-clang(%t/swiftprivate.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY)
6+
7+
public struct Exposed {
8+
public var x: Int
9+
@_expose(!Cxx)
10+
public var notExposedField: Int
11+
}
12+
13+
@_expose(!Cxx)
14+
public struct NotExposed {
15+
public var x: Int
16+
}
17+
18+
extension NotExposed {
19+
func notExposed() {}
20+
}
21+
22+
@_expose(!Cxx)
23+
public func NotExposedfunction() {}
24+
25+
@MainActor
26+
@_expose(!Cxx)
27+
public class UnavailableClass {
28+
}
29+
30+
// CHECK-NOT: NotExposed
31+
// CHECK-NOT: notExposed
32+
// CHECK: Exposed
33+
// CHECK-NOT: UnavailableClass

test/attr/attr_expose.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ func incorrectLangSpecifier() {}
1515
@_expose(Cxx) @_cdecl("test") // expected-error {{'@_expose' cannot be applied to an '@_cdecl' declaration}}
1616
func cdeclAndExpose() {}
1717

18+
@_expose(Cxx) @_expose(!Cxx) // expected-error {{'@_expose' cannot be applied to an '@_expose(Cxx)' declaration}}
19+
func contradictingExpose() {}
20+
21+
@_expose(!Cxx) @_expose(Cxx) // expected-error {{'@_expose' cannot be applied to an '@_expose(Cxx)' declaration}}
22+
func contradictingExpose2() {}
23+
24+
@_expose(!Cxx, "name") // expected-error {{'@_expose(!Cxx)' does not accept a name argument}}
25+
func notExposeWithName() {}
26+
1827
func hasNested() {
1928
@_expose(Cxx) // expected-error{{can only be used in a non-local scope}}
2029
func nested() { }

0 commit comments

Comments
 (0)