Skip to content

Commit 2b2fa22

Browse files
committed
[Type checker] Move 2/3 variants of isRepresentableInObjC() off the TypeChecker class.
1 parent 53d60f9 commit 2b2fa22

File tree

4 files changed

+48
-45
lines changed

4 files changed

+48
-45
lines changed

lib/Sema/TypeCheckDecl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3973,7 +3973,7 @@ void TypeChecker::validateDecl(ValueDecl *D) {
39733973
// Objective-C.
39743974
Optional<ObjCReason> isObjC = shouldMarkAsObjC(*this, VD);
39753975

3976-
if (isObjC && !isRepresentableInObjC(VD, *isObjC))
3976+
if (isObjC && !swift::isRepresentableInObjC(VD, *isObjC))
39773977
isObjC = None;
39783978

39793979
markAsObjC(*this, VD, isObjC);
@@ -4580,7 +4580,7 @@ void TypeChecker::validateDecl(ValueDecl *D) {
45804580
// member of an ObjC-compatible class or protocol.
45814581
Optional<ObjCReason> isObjC = shouldMarkAsObjC(*this, SD);
45824582

4583-
if (isObjC && !isRepresentableInObjC(SD, *isObjC))
4583+
if (isObjC && !swift::isRepresentableInObjC(SD, *isObjC))
45844584
isObjC = None;
45854585
markAsObjC(*this, SD, isObjC);
45864586

lib/Sema/TypeCheckDeclObjC.cpp

+38-41
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,8 @@ bool TypeChecker::isRepresentableInObjC(
412412
errorConvention = None;
413413

414414
// If you change this function, you must add or modify a test in PrintAsObjC.
415-
416-
bool Diagnose = shouldDiagnoseObjCReason(Reason, Context);
415+
ASTContext &ctx = AFD->getASTContext();
416+
bool Diagnose = shouldDiagnoseObjCReason(Reason, ctx);
417417

418418
if (checkObjCInForeignClassContext(AFD, Reason))
419419
return false;
@@ -423,17 +423,16 @@ bool TypeChecker::isRepresentableInObjC(
423423
return false;
424424

425425
if (AFD->isOperator()) {
426-
diagnose(AFD, (isa<ProtocolDecl>(AFD->getDeclContext())
427-
? diag::objc_operator_proto
428-
: diag::objc_operator));
426+
AFD->diagnose((isa<ProtocolDecl>(AFD->getDeclContext())
427+
? diag::objc_operator_proto
428+
: diag::objc_operator));
429429
return false;
430430
}
431431

432432
if (auto accessor = dyn_cast<AccessorDecl>(AFD)) {
433433
// Accessors can only be @objc if the storage declaration is.
434434
// Global computed properties may however @_cdecl their accessors.
435435
auto storage = accessor->getStorage();
436-
validateDecl(storage);
437436
if (!storage->isObjC() && Reason != ObjCReason::ExplicitlyCDecl &&
438437
Reason != ObjCReason::WitnessToObjC) {
439438
if (Diagnose) {
@@ -445,7 +444,7 @@ bool TypeChecker::isRepresentableInObjC(
445444
? diag::objc_setter_for_nonobjc_property
446445
: diag::objc_setter_for_nonobjc_subscript);
447446

448-
diagnose(accessor->getLoc(), error);
447+
accessor->diagnose(error);
449448
describeObjCReason(accessor, Reason);
450449
}
451450
return false;
@@ -457,7 +456,7 @@ bool TypeChecker::isRepresentableInObjC(
457456
// willSet/didSet implementations are never exposed to objc, they are
458457
// always directly dispatched from the synthesized setter.
459458
if (Diagnose) {
460-
diagnose(accessor->getLoc(), diag::objc_observing_accessor);
459+
accessor->diagnose(diag::objc_observing_accessor);
461460
describeObjCReason(accessor, Reason);
462461
}
463462
return false;
@@ -473,7 +472,7 @@ bool TypeChecker::isRepresentableInObjC(
473472
case AccessorKind::Address:
474473
case AccessorKind::MutableAddress:
475474
if (Diagnose) {
476-
diagnose(accessor->getLoc(), diag::objc_addressor);
475+
accessor->diagnose(diag::objc_addressor);
477476
describeObjCReason(accessor, Reason);
478477
}
479478
return false;
@@ -513,8 +512,8 @@ bool TypeChecker::isRepresentableInObjC(
513512
!ResultType->isRepresentableIn(ForeignLanguage::ObjectiveC,
514513
const_cast<FuncDecl *>(FD))) {
515514
if (Diagnose) {
516-
diagnose(AFD->getLoc(), diag::objc_invalid_on_func_result_type,
517-
getObjCDiagnosticAttrKind(Reason));
515+
AFD->diagnose(diag::objc_invalid_on_func_result_type,
516+
getObjCDiagnosticAttrKind(Reason));
518517
SourceRange Range =
519518
FD->getBodyResultTypeLoc().getTypeRepr()->getSourceRange();
520519
diagnoseTypeNotRepresentableInObjC(FD, ResultType, Range);
@@ -549,8 +548,8 @@ bool TypeChecker::isRepresentableInObjC(
549548
// Only non-failing initializers can throw.
550549
if (ctor->getFailability() != OTK_None) {
551550
if (Diagnose) {
552-
diagnose(AFD->getLoc(), diag::objc_invalid_on_failing_init,
553-
getObjCDiagnosticAttrKind(Reason))
551+
AFD->diagnose(diag::objc_invalid_on_failing_init,
552+
getObjCDiagnosticAttrKind(Reason))
554553
.highlight(throwsLoc);
555554
describeObjCReason(AFD, Reason);
556555
}
@@ -561,15 +560,15 @@ bool TypeChecker::isRepresentableInObjC(
561560
// Functions that return nothing (void) can be throwing; they indicate
562561
// failure with a 'false' result.
563562
kind = ForeignErrorConvention::ZeroResult;
564-
NominalTypeDecl *boolDecl = Context.getObjCBoolDecl();
563+
NominalTypeDecl *boolDecl = ctx.getObjCBoolDecl();
565564
// On Linux, we might still run @objc tests even though there's
566565
// no ObjectiveC Foundation, so use Swift.Bool instead of crapping
567566
// out.
568567
if (boolDecl == nullptr)
569-
boolDecl = Context.getBoolDecl();
568+
boolDecl = ctx.getBoolDecl();
570569

571570
if (boolDecl == nullptr) {
572-
diagnose(AFD->getLoc(), diag::broken_bool);
571+
AFD->diagnose(diag::broken_bool);
573572
return false;
574573
}
575574

@@ -584,21 +583,19 @@ bool TypeChecker::isRepresentableInObjC(
584583
// Cannot return an optional bridged type, because 'nil' is reserved
585584
// to indicate failure. Call this out in a separate diagnostic.
586585
if (Diagnose) {
587-
diagnose(AFD->getLoc(),
588-
diag::objc_invalid_on_throwing_optional_result,
589-
getObjCDiagnosticAttrKind(Reason),
590-
resultType)
586+
AFD->diagnose(diag::objc_invalid_on_throwing_optional_result,
587+
getObjCDiagnosticAttrKind(Reason),
588+
resultType)
591589
.highlight(throwsLoc);
592590
describeObjCReason(AFD, Reason);
593591
}
594592
return false;
595593
} else {
596594
// Other result types are not permitted.
597595
if (Diagnose) {
598-
diagnose(AFD->getLoc(),
599-
diag::objc_invalid_on_throwing_result,
600-
getObjCDiagnosticAttrKind(Reason),
601-
resultType)
596+
AFD->diagnose(diag::objc_invalid_on_throwing_result,
597+
getObjCDiagnosticAttrKind(Reason),
598+
resultType)
602599
.highlight(throwsLoc);
603600
describeObjCReason(AFD, Reason);
604601
}
@@ -611,7 +608,7 @@ bool TypeChecker::isRepresentableInObjC(
611608
errorParameterType = OptionalType::get(errorParameterType);
612609
errorParameterType
613610
= BoundGenericType::get(
614-
Context.getAutoreleasingUnsafeMutablePointerDecl(),
611+
ctx.getAutoreleasingUnsafeMutablePointerDecl(),
615612
nullptr,
616613
errorParameterType);
617614
errorParameterType = OptionalType::get(errorParameterType);
@@ -630,7 +627,7 @@ bool TypeChecker::isRepresentableInObjC(
630627
// If the selector piece is "error", this is the location of
631628
// the error parameter.
632629
auto piece = selectorPieces[i-1];
633-
if (piece == Context.Id_error) {
630+
if (piece == ctx.Id_error) {
634631
errorParameterIndex = i-1;
635632
foundErrorParameterIndex = true;
636633
break;
@@ -732,7 +729,7 @@ bool TypeChecker::isRepresentableInObjC(
732729
return true;
733730
}
734731

735-
bool TypeChecker::isRepresentableInObjC(const VarDecl *VD, ObjCReason Reason) {
732+
bool swift::isRepresentableInObjC(const VarDecl *VD, ObjCReason Reason) {
736733
// If you change this function, you must add or modify a test in PrintAsObjC.
737734

738735
if (VD->isInvalid())
@@ -746,9 +743,10 @@ bool TypeChecker::isRepresentableInObjC(const VarDecl *VD, ObjCReason Reason) {
746743
// Because of this, look through @weak and @unowned.
747744
T = RST->getReferentType();
748745
}
746+
ASTContext &ctx = VD->getASTContext();
749747
bool Result = T->isRepresentableIn(ForeignLanguage::ObjectiveC,
750748
VD->getDeclContext());
751-
bool Diagnose = shouldDiagnoseObjCReason(Reason, Context);
749+
bool Diagnose = shouldDiagnoseObjCReason(Reason, ctx);
752750

753751
if (Result && checkObjCInExtensionContext(VD, Diagnose))
754752
return false;
@@ -764,8 +762,7 @@ bool TypeChecker::isRepresentableInObjC(const VarDecl *VD, ObjCReason Reason) {
764762
if (TypeRange.isInvalid())
765763
TypeRange = VD->getNameLoc();
766764

767-
diagnose(VD->getLoc(), diag::objc_invalid_on_var,
768-
getObjCDiagnosticAttrKind(Reason))
765+
VD->diagnose(diag::objc_invalid_on_var, getObjCDiagnosticAttrKind(Reason))
769766
.highlight(TypeRange);
770767
diagnoseTypeNotRepresentableInObjC(VD->getDeclContext(),
771768
VD->getInterfaceType(),
@@ -775,11 +772,10 @@ bool TypeChecker::isRepresentableInObjC(const VarDecl *VD, ObjCReason Reason) {
775772
return Result;
776773
}
777774

778-
bool TypeChecker::isRepresentableInObjC(const SubscriptDecl *SD,
779-
ObjCReason Reason) {
775+
bool swift::isRepresentableInObjC(const SubscriptDecl *SD, ObjCReason Reason) {
780776
// If you change this function, you must add or modify a test in PrintAsObjC.
781-
782-
bool Diagnose = shouldDiagnoseObjCReason(Reason, Context);
777+
ASTContext &ctx = SD->getASTContext();
778+
bool Diagnose = shouldDiagnoseObjCReason(Reason, ctx);
783779

784780
if (checkObjCInForeignClassContext(SD, Reason))
785781
return false;
@@ -805,8 +801,8 @@ bool TypeChecker::isRepresentableInObjC(const SubscriptDecl *SD,
805801
// Make sure we know how to map the selector appropriately.
806802
if (Result && SD->getObjCSubscriptKind() == ObjCSubscriptKind::None) {
807803
SourceRange IndexRange = SD->getIndices()->getSourceRange();
808-
diagnose(SD->getLoc(), diag::objc_invalid_subscript_key_type,
809-
getObjCDiagnosticAttrKind(Reason), IndicesType)
804+
SD->diagnose(diag::objc_invalid_subscript_key_type,
805+
getObjCDiagnosticAttrKind(Reason), IndicesType)
810806
.highlight(IndexRange);
811807
return false;
812808
}
@@ -819,8 +815,8 @@ bool TypeChecker::isRepresentableInObjC(const SubscriptDecl *SD,
819815
TypeRange = SD->getIndices()->getSourceRange();
820816
else
821817
TypeRange = SD->getElementTypeLoc().getSourceRange();
822-
diagnose(SD->getLoc(), diag::objc_invalid_on_subscript,
823-
getObjCDiagnosticAttrKind(Reason))
818+
SD->diagnose(diag::objc_invalid_on_subscript,
819+
getObjCDiagnosticAttrKind(Reason))
824820
.highlight(TypeRange);
825821

826822
diagnoseTypeNotRepresentableInObjC(SD->getDeclContext(),
@@ -843,11 +839,12 @@ bool TypeChecker::canBeRepresentedInObjC(const ValueDecl *decl) {
843839
}
844840

845841
if (auto var = dyn_cast<VarDecl>(decl))
846-
return isRepresentableInObjC(var, ObjCReason::MemberOfObjCMembersClass);
842+
return swift::isRepresentableInObjC(var,
843+
ObjCReason::MemberOfObjCMembersClass);
847844

848845
if (auto subscript = dyn_cast<SubscriptDecl>(decl))
849-
return isRepresentableInObjC(subscript,
850-
ObjCReason::MemberOfObjCMembersClass);
846+
return swift::isRepresentableInObjC(subscript,
847+
ObjCReason::MemberOfObjCMembersClass);
851848

852849
return false;
853850
}

lib/Sema/TypeCheckObjC.h

+8
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
namespace swift {
2424

2525
class ASTContext;
26+
class SubscriptDecl;
2627
class TypeChecker;
2728
class ValueDecl;
29+
class VarDecl;
2830

2931
using llvm::Optional;
3032

@@ -119,6 +121,12 @@ void markAsObjC(TypeChecker &TC, ValueDecl *D,
119121
Optional<ObjCReason> isObjC,
120122
Optional<ForeignErrorConvention> errorConvention = llvm::None);
121123

124+
/// Determine whether the given variable can be represented in Objective-C.
125+
bool isRepresentableInObjC(const VarDecl *VD, ObjCReason Reason);
126+
127+
/// Determine whether the given subscript can be represented in Objective-C.
128+
bool isRepresentableInObjC(const SubscriptDecl *SD, ObjCReason Reason);
129+
122130
} // end namespace swift
123131

124132
#endif // SWIFT_SEMA_TYPE_CHECK_OBJC_H

lib/Sema/TypeChecker.h

-2
Original file line numberDiff line numberDiff line change
@@ -2188,8 +2188,6 @@ class TypeChecker final : public LazyResolver {
21882188
bool isRepresentableInObjC(const AbstractFunctionDecl *AFD,
21892189
ObjCReason Reason,
21902190
Optional<ForeignErrorConvention> &errorConvention);
2191-
bool isRepresentableInObjC(const VarDecl *VD, ObjCReason Reason);
2192-
bool isRepresentableInObjC(const SubscriptDecl *SD, ObjCReason Reason);
21932191

21942192
bool canBeRepresentedInObjC(const ValueDecl *decl);
21952193

0 commit comments

Comments
 (0)