Skip to content

Commit e55c9bf

Browse files
authored
Merge pull request #80900 from xymus/cdecl-reason-language
Sema: Simplify tracking the foreign language for @cdecl
2 parents b3e48b4 + 384b030 commit e55c9bf

File tree

4 files changed

+32
-33
lines changed

4 files changed

+32
-33
lines changed

lib/Sema/CodeSynthesis.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,7 @@ configureInheritedDesignatedInitAttributes(ClassDecl *classDecl,
638638
std::optional<ForeignErrorConvention> errorConvention;
639639
if (superclassCtor->isObjC() &&
640640
!isRepresentableInLanguage(ctor, ObjCReason::MemberOfObjCSubclass,
641-
asyncConvention, errorConvention,
642-
ForeignLanguage::ObjectiveC))
641+
asyncConvention, errorConvention))
643642
ctor->getAttrs().add(new (ctx) NonObjCAttr(/*isImplicit=*/true));
644643
}
645644

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,7 @@ CaptureInfo CaptureInfoRequest::evaluate(Evaluator &evaluator,
789789
if (!AFD->isObjC() &&
790790
isRepresentableInLanguage(AFD,
791791
ObjCReason::MemberOfObjCMembersClass,
792-
asyncConvention, errorConvention,
793-
ForeignLanguage::ObjectiveC)) {
792+
asyncConvention, errorConvention)) {
794793
AFD->diagnose(
795794
diag::objc_generic_extension_using_type_parameter_try_objc)
796795
.fixItInsert(AFD->getAttributeInsertionLoc(false), "@objc ");

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -297,21 +297,21 @@ static void diagnoseTypeNotRepresentableInObjC(const DeclContext *DC,
297297

298298
static void diagnoseFunctionParamNotRepresentable(
299299
const AbstractFunctionDecl *AFD, unsigned NumParams,
300-
unsigned ParamIndex, const ParamDecl *P, ObjCReason Reason,
301-
ForeignLanguage Language) {
300+
unsigned ParamIndex, const ParamDecl *P, ObjCReason Reason) {
302301
auto behavior = behaviorLimitForObjCReason(Reason, AFD->getASTContext());
302+
auto language = Reason.getForeignLanguage();
303303

304304
if (NumParams == 1) {
305305
softenIfAccessNote(AFD, Reason.getAttr(),
306306
AFD->diagnose(diag::objc_invalid_on_func_single_param_type,
307307
AFD, getObjCDiagnosticAttrKind(Reason),
308-
(unsigned)Language)
308+
(unsigned)language)
309309
.limitBehavior(behavior));
310310
} else {
311311
softenIfAccessNote(AFD, Reason.getAttr(),
312312
AFD->diagnose(diag::objc_invalid_on_func_param_type,
313313
AFD, ParamIndex + 1, getObjCDiagnosticAttrKind(Reason),
314-
(unsigned)Language)
314+
(unsigned)language)
315315
.limitBehavior(behavior));
316316
}
317317
SourceRange SR;
@@ -330,12 +330,12 @@ static void diagnoseFunctionParamNotRepresentable(
330330

331331
static bool isParamListRepresentableInLanguage(const AbstractFunctionDecl *AFD,
332332
const ParameterList *PL,
333-
ObjCReason Reason,
334-
ForeignLanguage Language) {
333+
ObjCReason Reason) {
335334
// If you change this function, you must add or modify a test in PrintAsClang.
336335
ASTContext &ctx = AFD->getASTContext();
337336
auto &diags = ctx.Diags;
338337
auto behavior = behaviorLimitForObjCReason(Reason, ctx);
338+
auto language = Reason.getForeignLanguage();
339339
bool IsObjC = true;
340340
unsigned NumParams = PL->size();
341341
for (unsigned ParamIndex = 0; ParamIndex != NumParams; ++ParamIndex) {
@@ -357,7 +357,8 @@ static bool isParamListRepresentableInLanguage(const AbstractFunctionDecl *AFD,
357357
if (param->isInOut()) {
358358
softenIfAccessNote(AFD, Reason.getAttr(),
359359
diags.diagnose(param->getStartLoc(), diag::objc_invalid_on_func_inout,
360-
AFD, getObjCDiagnosticAttrKind(Reason), (unsigned)Language)
360+
AFD, getObjCDiagnosticAttrKind(Reason),
361+
(unsigned)language)
361362
.highlight(param->getSourceRange())
362363
.limitBehavior(behavior));
363364
Reason.describe(AFD);
@@ -370,11 +371,11 @@ static bool isParamListRepresentableInLanguage(const AbstractFunctionDecl *AFD,
370371

371372
if (param->hasAttachedPropertyWrapper()) {
372373
if (param->getPropertyWrapperBackingPropertyType()->isRepresentableIn(
373-
Language,
374+
language,
374375
const_cast<AbstractFunctionDecl *>(AFD)))
375376
continue;
376377
} else if (param->getTypeInContext()->isRepresentableIn(
377-
Language,
378+
language,
378379
const_cast<AbstractFunctionDecl *>(AFD))) {
379380
continue;
380381
}
@@ -395,7 +396,7 @@ static bool isParamListRepresentableInLanguage(const AbstractFunctionDecl *AFD,
395396

396397
IsObjC = false;
397398
diagnoseFunctionParamNotRepresentable(AFD, NumParams, ParamIndex,
398-
param, Reason, Language);
399+
param, Reason);
399400
}
400401
return IsObjC;
401402
}
@@ -656,13 +657,11 @@ static bool isValidObjectiveCErrorResultType(DeclContext *dc, Type type) {
656657
bool swift::isRepresentableInLanguage(
657658
const AbstractFunctionDecl *AFD, ObjCReason Reason,
658659
std::optional<ForeignAsyncConvention> &asyncConvention,
659-
std::optional<ForeignErrorConvention> &errorConvention,
660-
ForeignLanguage Language) {
660+
std::optional<ForeignErrorConvention> &errorConvention) {
661661
auto abiRole = ABIRoleInfo(AFD);
662662
if (!abiRole.providesAPI() && abiRole.getCounterpart())
663663
return isRepresentableInLanguage(abiRole.getCounterpart(), Reason,
664-
asyncConvention, errorConvention,
665-
Language);
664+
asyncConvention, errorConvention);
666665

667666
// Clear out the async and error conventions. They will be added later if
668667
// needed.
@@ -672,7 +671,7 @@ bool swift::isRepresentableInLanguage(
672671
// If you change this function, you must add or modify a test in PrintAsClang.
673672
ASTContext &ctx = AFD->getASTContext();
674673
DiagnosticStateRAII diagState(ctx.Diags);
675-
674+
auto language = Reason.getForeignLanguage();
676675

677676
if (checkObjCInForeignClassContext(AFD, Reason))
678677
return false;
@@ -786,8 +785,7 @@ bool swift::isRepresentableInLanguage(
786785
if (!isSpecialInit &&
787786
!isParamListRepresentableInLanguage(AFD,
788787
AFD->getParameters(),
789-
Reason,
790-
Language)) {
788+
Reason)) {
791789
return false;
792790
}
793791

@@ -797,12 +795,12 @@ bool swift::isRepresentableInLanguage(
797795
!ResultType->hasError() &&
798796
!ResultType->isVoid() &&
799797
!ResultType->isUninhabited() &&
800-
!ResultType->isRepresentableIn(Language,
798+
!ResultType->isRepresentableIn(language,
801799
const_cast<FuncDecl *>(FD))) {
802800
softenIfAccessNote(AFD, Reason.getAttr(),
803801
AFD->diagnose(diag::objc_invalid_on_func_result_type,
804802
FD, getObjCDiagnosticAttrKind(Reason),
805-
(unsigned)Language)
803+
(unsigned)language)
806804
.limitBehavior(behavior));
807805
diagnoseTypeNotRepresentableInObjC(FD, ResultType,
808806
FD->getResultTypeSourceRange(),
@@ -855,11 +853,11 @@ bool swift::isRepresentableInLanguage(
855853
completionHandlerParams.push_back(AnyFunctionType::Param(type));
856854

857855
// Make sure that the parameter type is representable in Objective-C.
858-
if (!type->isRepresentableIn(Language, const_cast<FuncDecl *>(FD))) {
856+
if (!type->isRepresentableIn(language, const_cast<FuncDecl *>(FD))) {
859857
softenIfAccessNote(AFD, Reason.getAttr(),
860858
AFD->diagnose(diag::objc_invalid_on_func_result_type,
861859
FD, getObjCDiagnosticAttrKind(Reason),
862-
(unsigned)Language)
860+
(unsigned)language)
863861
.limitBehavior(behavior));
864862
diagnoseTypeNotRepresentableInObjC(FD, type,
865863
FD->getResultTypeSourceRange(),
@@ -1275,8 +1273,7 @@ bool swift::canBeRepresentedInObjC(const ValueDecl *decl) {
12751273
std::optional<ForeignErrorConvention> errorConvention;
12761274
return isRepresentableInLanguage(func,
12771275
ObjCReason::MemberOfObjCMembersClass,
1278-
asyncConvention, errorConvention,
1279-
ForeignLanguage::ObjectiveC);
1276+
asyncConvention, errorConvention);
12801277
}
12811278

12821279
if (auto var = dyn_cast<VarDecl>(decl))
@@ -1881,8 +1878,7 @@ bool IsObjCRequest::evaluate(Evaluator &evaluator, ValueDecl *VD) const {
18811878
// Destructors need no additional checking.
18821879
} else if (auto func = dyn_cast<AbstractFunctionDecl>(VD)) {
18831880
if (!isRepresentableInLanguage(
1884-
func, *isObjC, asyncConvention, errorConvention,
1885-
ForeignLanguage::ObjectiveC)) {
1881+
func, *isObjC, asyncConvention, errorConvention)) {
18861882
isObjC->setAttrInvalid();
18871883
return false;
18881884
}
@@ -4203,8 +4199,7 @@ TypeCheckCDeclAttributeRequest::evaluate(Evaluator &evaluator,
42034199

42044200
std::optional<ForeignAsyncConvention> asyncConvention;
42054201
std::optional<ForeignErrorConvention> errorConvention;
4206-
if (isRepresentableInLanguage(FD, reason, asyncConvention, errorConvention,
4207-
*lang)) {
4202+
if (isRepresentableInLanguage(FD, reason, asyncConvention, errorConvention)) {
42084203
if (FD->hasAsync()) {
42094204
FD->setForeignAsyncConvention(*asyncConvention);
42104205
ctx.Diags.diagnose(attr->getLocation(), diag::attr_decl_async,

lib/Sema/TypeCheckObjC.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ class ObjCReason {
165165
return declOrAttr.get<DeclAttribute *>();
166166
}
167167

168+
// The foreign language targeted by the context.
169+
ForeignLanguage getForeignLanguage() const {
170+
if (kind == ExplicitlyCDecl)
171+
return ForeignLanguage::C;
172+
return ForeignLanguage::ObjectiveC;
173+
}
174+
168175
void setAttrInvalid() const;
169176

170177
/// Emit an additional diagnostic describing why we are applying @objc to the
@@ -189,8 +196,7 @@ unsigned getObjCDiagnosticAttrKind(ObjCReason reason);
189196
bool isRepresentableInLanguage(
190197
const AbstractFunctionDecl *AFD, ObjCReason Reason,
191198
std::optional<ForeignAsyncConvention> &asyncConvention,
192-
std::optional<ForeignErrorConvention> &errorConvention,
193-
ForeignLanguage lang);
199+
std::optional<ForeignErrorConvention> &errorConvention);
194200

195201
/// Determine whether the given variable can be represented in Objective-C.
196202
bool isRepresentableInObjC(const VarDecl *VD, ObjCReason Reason);

0 commit comments

Comments
 (0)