Skip to content

Commit 384b030

Browse files
committed
Sema: Simplify tracking the foreign language of an @objc decl
Apply review comments from #80744.
1 parent c6b2821 commit 384b030

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
@@ -791,8 +791,7 @@ CaptureInfo CaptureInfoRequest::evaluate(Evaluator &evaluator,
791791
if (!AFD->isObjC() &&
792792
isRepresentableInLanguage(AFD,
793793
ObjCReason::MemberOfObjCMembersClass,
794-
asyncConvention, errorConvention,
795-
ForeignLanguage::ObjectiveC)) {
794+
asyncConvention, errorConvention)) {
796795
AFD->diagnose(
797796
diag::objc_generic_extension_using_type_parameter_try_objc)
798797
.fixItInsert(AFD->getAttributeInsertionLoc(false), "@objc ");

lib/Sema/TypeCheckDeclObjC.cpp

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

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

303303
if (NumParams == 1) {
304304
softenIfAccessNote(AFD, Reason.getAttr(),
305305
AFD->diagnose(diag::objc_invalid_on_func_single_param_type,
306306
AFD, getObjCDiagnosticAttrKind(Reason),
307-
(unsigned)Language)
307+
(unsigned)language)
308308
.limitBehavior(behavior));
309309
} else {
310310
softenIfAccessNote(AFD, Reason.getAttr(),
311311
AFD->diagnose(diag::objc_invalid_on_func_param_type,
312312
AFD, ParamIndex + 1, getObjCDiagnosticAttrKind(Reason),
313-
(unsigned)Language)
313+
(unsigned)language)
314314
.limitBehavior(behavior));
315315
}
316316
SourceRange SR;
@@ -329,12 +329,12 @@ static void diagnoseFunctionParamNotRepresentable(
329329

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

370371
if (param->hasAttachedPropertyWrapper()) {
371372
if (param->getPropertyWrapperBackingPropertyType()->isRepresentableIn(
372-
Language,
373+
language,
373374
const_cast<AbstractFunctionDecl *>(AFD)))
374375
continue;
375376
} else if (param->getTypeInContext()->isRepresentableIn(
376-
Language,
377+
language,
377378
const_cast<AbstractFunctionDecl *>(AFD))) {
378379
continue;
379380
}
@@ -394,7 +395,7 @@ static bool isParamListRepresentableInLanguage(const AbstractFunctionDecl *AFD,
394395

395396
IsObjC = false;
396397
diagnoseFunctionParamNotRepresentable(AFD, NumParams, ParamIndex,
397-
param, Reason, Language);
398+
param, Reason);
398399
}
399400
return IsObjC;
400401
}
@@ -655,13 +656,11 @@ static bool isValidObjectiveCErrorResultType(DeclContext *dc, Type type) {
655656
bool swift::isRepresentableInLanguage(
656657
const AbstractFunctionDecl *AFD, ObjCReason Reason,
657658
std::optional<ForeignAsyncConvention> &asyncConvention,
658-
std::optional<ForeignErrorConvention> &errorConvention,
659-
ForeignLanguage Language) {
659+
std::optional<ForeignErrorConvention> &errorConvention) {
660660
auto abiRole = ABIRoleInfo(AFD);
661661
if (!abiRole.providesAPI() && abiRole.getCounterpart())
662662
return isRepresentableInLanguage(abiRole.getCounterpart(), Reason,
663-
asyncConvention, errorConvention,
664-
Language);
663+
asyncConvention, errorConvention);
665664

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

676675
if (checkObjCInForeignClassContext(AFD, Reason))
677676
return false;
@@ -785,8 +784,7 @@ bool swift::isRepresentableInLanguage(
785784
if (!isSpecialInit &&
786785
!isParamListRepresentableInLanguage(AFD,
787786
AFD->getParameters(),
788-
Reason,
789-
Language)) {
787+
Reason)) {
790788
return false;
791789
}
792790

@@ -796,12 +794,12 @@ bool swift::isRepresentableInLanguage(
796794
!ResultType->hasError() &&
797795
!ResultType->isVoid() &&
798796
!ResultType->isUninhabited() &&
799-
!ResultType->isRepresentableIn(Language,
797+
!ResultType->isRepresentableIn(language,
800798
const_cast<FuncDecl *>(FD))) {
801799
softenIfAccessNote(AFD, Reason.getAttr(),
802800
AFD->diagnose(diag::objc_invalid_on_func_result_type,
803801
FD, getObjCDiagnosticAttrKind(Reason),
804-
(unsigned)Language)
802+
(unsigned)language)
805803
.limitBehavior(behavior));
806804
diagnoseTypeNotRepresentableInObjC(FD, ResultType,
807805
FD->getResultTypeSourceRange(),
@@ -854,11 +852,11 @@ bool swift::isRepresentableInLanguage(
854852
completionHandlerParams.push_back(AnyFunctionType::Param(type));
855853

856854
// Make sure that the parameter type is representable in Objective-C.
857-
if (!type->isRepresentableIn(Language, const_cast<FuncDecl *>(FD))) {
855+
if (!type->isRepresentableIn(language, const_cast<FuncDecl *>(FD))) {
858856
softenIfAccessNote(AFD, Reason.getAttr(),
859857
AFD->diagnose(diag::objc_invalid_on_func_result_type,
860858
FD, getObjCDiagnosticAttrKind(Reason),
861-
(unsigned)Language)
859+
(unsigned)language)
862860
.limitBehavior(behavior));
863861
diagnoseTypeNotRepresentableInObjC(FD, type,
864862
FD->getResultTypeSourceRange(),
@@ -1274,8 +1272,7 @@ bool swift::canBeRepresentedInObjC(const ValueDecl *decl) {
12741272
std::optional<ForeignErrorConvention> errorConvention;
12751273
return isRepresentableInLanguage(func,
12761274
ObjCReason::MemberOfObjCMembersClass,
1277-
asyncConvention, errorConvention,
1278-
ForeignLanguage::ObjectiveC);
1275+
asyncConvention, errorConvention);
12791276
}
12801277

12811278
if (auto var = dyn_cast<VarDecl>(decl))
@@ -1880,8 +1877,7 @@ bool IsObjCRequest::evaluate(Evaluator &evaluator, ValueDecl *VD) const {
18801877
// Destructors need no additional checking.
18811878
} else if (auto func = dyn_cast<AbstractFunctionDecl>(VD)) {
18821879
if (!isRepresentableInLanguage(
1883-
func, *isObjC, asyncConvention, errorConvention,
1884-
ForeignLanguage::ObjectiveC)) {
1880+
func, *isObjC, asyncConvention, errorConvention)) {
18851881
isObjC->setAttrInvalid();
18861882
return false;
18871883
}
@@ -4174,8 +4170,7 @@ TypeCheckCDeclAttributeRequest::evaluate(Evaluator &evaluator,
41744170

41754171
std::optional<ForeignAsyncConvention> asyncConvention;
41764172
std::optional<ForeignErrorConvention> errorConvention;
4177-
if (isRepresentableInLanguage(FD, reason, asyncConvention, errorConvention,
4178-
*lang)) {
4173+
if (isRepresentableInLanguage(FD, reason, asyncConvention, errorConvention)) {
41794174
if (FD->hasAsync()) {
41804175
FD->setForeignAsyncConvention(*asyncConvention);
41814176
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)