@@ -1948,19 +1948,21 @@ class GetSendableType :
1948
1948
1949
1949
} // anonymous namespace
1950
1950
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
+
1953
1958
bool sendableRequested = sendableByDefault;
1954
1959
bool sendableDisqualified = false ;
1955
1960
1956
- if (param ->hasAttrs ()) {
1957
- for (auto attr : param ->getAttrs ()) {
1961
+ if (D ->hasAttrs ()) {
1962
+ for (auto attr : D ->getAttrs ()) {
1958
1963
// 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;
1964
1966
continue ;
1965
1967
}
1966
1968
@@ -1969,14 +1971,9 @@ Type ClangImporter::Implementation::applyParamAttributes(
1969
1971
continue ;
1970
1972
1971
1973
// 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 ;
1980
1977
continue ;
1981
1978
}
1982
1979
@@ -1995,22 +1992,51 @@ Type ClangImporter::Implementation::applyParamAttributes(
1995
1992
}
1996
1993
1997
1994
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)) {
1998
2031
bool changed;
1999
2032
std::tie (type, changed) = GetSendableType (SwiftContext).convert (type);
2000
2033
2001
2034
// Diagnose if we couldn't find a place to add `Sendable` to the type.
2002
2035
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));
2009
2037
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));
2014
2040
}
2015
2041
}
2016
2042
0 commit comments