@@ -1948,19 +1948,21 @@ class GetSendableType :
19481948
19491949} // anonymous namespace
19501950
1951- Type ClangImporter::Implementation::applyParamAttributes (
1952- const clang::ParmVarDecl *param, Type type, bool sendableByDefault) {
1951+ ImportTypeAttrs swift::getImportTypeAttrs (const clang::Decl *D, bool isParam,
1952+ bool sendableByDefault) {
1953+ ImportTypeAttrs attrs;
1954+
1955+ if (sendableByDefault)
1956+ attrs |= ImportTypeAttr::DefaultsToSendable;
1957+
19531958 bool sendableRequested = sendableByDefault;
19541959 bool sendableDisqualified = false ;
19551960
1956- if (param ->hasAttrs ()) {
1957- for (auto attr : param ->getAttrs ()) {
1961+ if (D ->hasAttrs ()) {
1962+ for (auto attr : D ->getAttrs ()) {
19581963 // Map __attribute__((noescape)) to @noescape.
1959- if (isa<clang::NoEscapeAttr>(attr)) {
1960- type = applyToFunctionType (type, [](ASTExtInfo extInfo) {
1961- return extInfo.withNoEscape ();
1962- });
1963-
1964+ if (isParam && isa<clang::NoEscapeAttr>(attr)) {
1965+ attrs |= ImportTypeAttr::NoEscape;
19641966 continue ;
19651967 }
19661968
@@ -1969,14 +1971,9 @@ Type ClangImporter::Implementation::applyParamAttributes(
19691971 continue ;
19701972
19711973 // Map the main-actor attribute.
1972- if (isMainActorAttr (swiftAttr)) {
1973- if (Type mainActor = SwiftContext.getMainActorType ()) {
1974- type = applyToFunctionType (type, [&](ASTExtInfo extInfo) {
1975- return extInfo.withGlobalActor (mainActor);
1976- });
1977- sendableDisqualified = true ;
1978- }
1979-
1974+ if (isParam && isMainActorAttr (swiftAttr)) {
1975+ attrs |= ImportTypeAttr::MainActor;
1976+ sendableDisqualified = true ;
19801977 continue ;
19811978 }
19821979
@@ -1995,22 +1992,51 @@ Type ClangImporter::Implementation::applyParamAttributes(
19951992 }
19961993
19971994 if (!sendableDisqualified && sendableRequested) {
1995+ attrs |= ImportTypeAttr::Sendable;
1996+ }
1997+
1998+ return attrs;
1999+ }
2000+
2001+ Type ClangImporter::Implementation::applyParamAttributes (
2002+ const clang::ParmVarDecl *param, Type type, bool sendableByDefault) {
2003+ auto parentDecl = cast<clang::Decl>(param->getDeclContext ());
2004+ ImportDiagnosticAdder addDiag (*this , parentDecl, param->getLocation ());
2005+
2006+ auto attrs = getImportTypeAttrs (param, /* isParam=*/ true , sendableByDefault);
2007+ return applyImportTypeAttrs (attrs, type, addDiag);
2008+ }
2009+
2010+ Type ClangImporter::Implementation::
2011+ applyImportTypeAttrs (ImportTypeAttrs attrs, Type type,
2012+ llvm::function_ref<void (Diagnostic &&)> addDiag) {
2013+ if (attrs.contains (ImportTypeAttr::NoEscape)) {
2014+ type = applyToFunctionType (type, [](ASTExtInfo extInfo) {
2015+ return extInfo.withNoEscape ();
2016+ });
2017+ }
2018+
2019+ if (attrs.contains (ImportTypeAttr::MainActor)) {
2020+ if (Type mainActor = SwiftContext.getMainActorType ()) {
2021+ type = applyToFunctionType (type, [&](ASTExtInfo extInfo) {
2022+ return extInfo.withGlobalActor (mainActor);
2023+ });
2024+ } else {
2025+ // If we can't use @MainActor, fall back to at least using @Sendable.
2026+ attrs |= ImportTypeAttr::Sendable;
2027+ }
2028+ }
2029+
2030+ if (attrs.contains (ImportTypeAttr::Sendable)) {
19982031 bool changed;
19992032 std::tie (type, changed) = GetSendableType (SwiftContext).convert (type);
20002033
20012034 // Diagnose if we couldn't find a place to add `Sendable` to the type.
20022035 if (!changed) {
2003- auto parentDecl = cast<clang::Decl>(param->getDeclContext ());
2004-
2005- addImportDiagnostic (parentDecl,
2006- Diagnostic (diag::clang_param_ignored_sendable_attr,
2007- param->getName (), type),
2008- param->getLocation ());
2036+ addDiag (Diagnostic (diag::clang_ignored_sendable_attr, type));
20092037
2010- if (sendableByDefault)
2011- addImportDiagnostic (parentDecl,
2012- Diagnostic (diag::clang_param_should_be_implicitly_sendable),
2013- param->getLocation ());
2038+ if (attrs.contains (ImportTypeAttr::DefaultsToSendable))
2039+ addDiag (Diagnostic (diag::clang_param_should_be_implicitly_sendable));
20142040 }
20152041 }
20162042
0 commit comments