Skip to content

Commit d58f049

Browse files
committed
AST: Introduce ASTContext::getAnyObjectType()
This replaces a number of usages of KnownProtocolKind::AnyObject, which is soon going away.
1 parent d49f8fb commit d58f049

24 files changed

+67
-94
lines changed

include/swift/AST/ASTContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ class ASTContext {
407407
/// Retrieve the declaration of the "pointee" property of a pointer type.
408408
VarDecl *getPointerPointeePropertyDecl(PointerTypeKind ptrKind) const;
409409

410+
/// Retrieve the type Swift.AnyObject.
411+
CanType getAnyObjectType() const;
412+
410413
/// Retrieve the type Swift.Never.
411414
CanType getNeverType() const;
412415

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,6 @@ WARNING(expr_keypath_swift3_objc_inference,none,
419419
"argument of '#keyPath' refers to property %0 in %1 that depends on "
420420
"'@objc' attribute inference deprecated in Swift 4",
421421
(DeclName, Identifier))
422-
ERROR(stdlib_anyobject_not_found,none,
423-
"broken standard library: cannot find 'AnyObject' protocol", ())
424422
ERROR(expr_keypath_type_of_property,none,
425423
"cannot refer to type member %0 within instance of type %1",
426424
(DeclName, Type))

lib/AST/ASTContext.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ struct ASTContext::Implementation {
112112
/// The declaration of Swift.DefaultPrecedence.
113113
PrecedenceGroupDecl *DefaultPrecedence = nullptr;
114114

115+
/// The AnyObject type.
116+
CanType AnyObjectType;
117+
115118
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
116119
/** The declaration of Swift.NAME. */ \
117120
DECL_CLASS *NAME##Decl = nullptr;
@@ -671,6 +674,29 @@ ASTContext::getPointerPointeePropertyDecl(PointerTypeKind ptrKind) const {
671674
llvm_unreachable("bad pointer kind");
672675
}
673676

677+
CanType ASTContext::getAnyObjectType() const {
678+
if (Impl.AnyObjectType) {
679+
return Impl.AnyObjectType;
680+
}
681+
682+
// Go find 'AnyObject' in the Swift module.
683+
//
684+
// FIXME: This is going away.
685+
SmallVector<ValueDecl *, 1> results;
686+
lookupInSwiftModule("AnyObject", results);
687+
for (auto result : results) {
688+
if (auto proto = dyn_cast<ProtocolDecl>(result)) {
689+
Impl.AnyObjectType = proto->getDeclaredType()->getCanonicalType();
690+
return Impl.AnyObjectType;
691+
}
692+
}
693+
694+
Impl.AnyObjectType = CanType(
695+
ProtocolCompositionType::get(
696+
*this, {}, /*hasExplicitAnyObject=*/true));
697+
return Impl.AnyObjectType;
698+
}
699+
674700
CanType ASTContext::getNeverType() const {
675701
return getNeverDecl()->getDeclaredType()->getCanonicalType();
676702
}

lib/AST/ASTVerifier.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,8 +1069,7 @@ class Verifier : public ASTWalker {
10691069
abort();
10701070
}
10711071

1072-
if (!E->getType()->isEqual(
1073-
Ctx.getProtocol(KnownProtocolKind::AnyObject)->getDeclaredType())){
1072+
if (!E->getType()->isEqual(Ctx.getAnyObjectType())) {
10741073
Out << "ClassMetatypeToObject does not produce AnyObject:\n";
10751074
E->print(Out);
10761075
Out << "\n";
@@ -1100,8 +1099,7 @@ class Verifier : public ASTWalker {
11001099
abort();
11011100
}
11021101

1103-
if (!E->getType()->isEqual(
1104-
Ctx.getProtocol(KnownProtocolKind::AnyObject)->getDeclaredType())){
1102+
if (!E->getType()->isEqual(Ctx.getAnyObjectType())) {
11051103
Out << "ExistentialMetatypeToObject does not produce AnyObject:\n";
11061104
E->print(Out);
11071105
Out << "\n";

lib/AST/ProtocolConformance.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ ProtocolConformanceRef::subst(Type origType,
125125
}
126126

127127
// If that didn't find anything, we can still synthesize AnyObject
128-
// conformances from thin air. FIXME: gross.
128+
// conformances from thin air. FIXME: this is going away soon.
129129
if (proto->isSpecificProtocol(KnownProtocolKind::AnyObject)) {
130130
if (substType->isExistentialType())
131131
return *this;
@@ -889,6 +889,7 @@ void NominalTypeDecl::prepareConformanceTable() const {
889889

890890
// Add any synthesized conformances.
891891
if (isa<ClassDecl>(this)) {
892+
// FIXME: This is going away soon.
892893
if (auto anyObject = getASTContext().getProtocol(
893894
KnownProtocolKind::AnyObject)) {
894895
ConformanceTable->addSynthesizedConformance(mutableThis, anyObject);

lib/AST/Type.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,12 @@ ExistentialLayout CanType::getExistentialLayout() {
308308
}
309309

310310
bool ExistentialLayout::isAnyObject() const {
311-
// FIXME
311+
// New implementation
312312
auto protocols = getProtocols();
313+
if (requiresClass && !requiresClassImplied && protocols.empty())
314+
return true;
315+
316+
// Old implementation -- FIXME: remove this
313317
return protocols.size() == 1 &&
314318
protocols[0]->getDecl()->isSpecificProtocol(KnownProtocolKind::AnyObject);
315319
}

lib/ClangImporter/ImportDecl.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,11 +2172,6 @@ namespace {
21722172
// If the pointee is 'void', 'CFTypeRef', bring it
21732173
// in specifically as AnyObject.
21742174
if (pointee.isVoid()) {
2175-
auto proto = Impl.SwiftContext.getProtocol(
2176-
KnownProtocolKind::AnyObject);
2177-
if (!proto)
2178-
return nullptr;
2179-
21802175
// Create a typealias for this CF typedef.
21812176
TypeAliasDecl *typealias = nullptr;
21822177
typealias = Impl.createDeclWithClangNode<TypeAliasDecl>(
@@ -2186,7 +2181,7 @@ namespace {
21862181
Impl.importSourceLoc(Decl->getLocation()),
21872182
/*genericparams*/nullptr, DC);
21882183
typealias->setUnderlyingType(
2189-
proto->getDeclaredInterfaceType());
2184+
Impl.SwiftContext.getAnyObjectType());
21902185

21912186
Impl.SpecialTypedefNames[Decl->getCanonicalDecl()] =
21922187
MappedTypeNameKind::DefineAndUse;
@@ -6292,13 +6287,8 @@ Optional<GenericParamList *> SwiftDeclConverter::importObjCGenericParams(
62926287
}
62936288
}
62946289
if (inherited.empty()) {
6295-
auto anyObjectProto =
6296-
Impl.SwiftContext.getProtocol(KnownProtocolKind::AnyObject);
6297-
if (!anyObjectProto) {
6298-
return None;
6299-
}
63006290
inherited.push_back(
6301-
TypeLoc::withoutLoc(anyObjectProto->getDeclaredType()));
6291+
TypeLoc::withoutLoc(Impl.SwiftContext.getAnyObjectType()));
63026292
}
63036293
genericParamDecl->setInherited(Impl.SwiftContext.AllocateCopy(inherited));
63046294

lib/ClangImporter/ImportType.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,20 +1043,17 @@ namespace {
10431043
}
10441044

10451045
// Beyond here, we're using AnyObject.
1046-
auto proto = Impl.SwiftContext.getProtocol(KnownProtocolKind::AnyObject);
1047-
if (!proto)
1048-
return Type();
10491046

10501047
// id maps to Any in bridgeable contexts, AnyObject otherwise.
10511048
if (type->isObjCIdType()) {
1052-
return {proto->getDeclaredType(),
1049+
return {Impl.SwiftContext.getAnyObjectType(),
10531050
ImportHint(ImportHint::ObjCBridged,
10541051
Impl.SwiftContext.TheAnyType)};
10551052
}
10561053

10571054
// Class maps to AnyObject.Type.
10581055
assert(type->isObjCClassType());
1059-
return { ExistentialMetatypeType::get(proto->getDeclaredType()),
1056+
return { ExistentialMetatypeType::get(Impl.SwiftContext.getAnyObjectType()),
10601057
ImportHint::ObjCPointer };
10611058
}
10621059
};
@@ -1935,8 +1932,7 @@ Type ClangImporter::Implementation::importMethodType(
19351932

19361933
// Undo 'Any' bridging.
19371934
if (nonOptionalTy->isAny())
1938-
nonOptionalTy = SwiftContext.getProtocol(KnownProtocolKind::AnyObject)
1939-
->getDeclaredType();
1935+
nonOptionalTy = SwiftContext.getAnyObjectType();
19401936

19411937
if (nonOptionalTy->isAnyClassReferenceType()) {
19421938
swiftResultTy = getUnmanagedType(*this, nonOptionalTy);

lib/IDE/CodeCompletion.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4657,9 +4657,7 @@ void CodeCompletionCallbacksImpl::completeNominalMemberBeginning(
46574657
}
46584658

46594659
static bool isDynamicLookup(Type T) {
4660-
if (auto *PT = T->getRValueType()->getAs<ProtocolType>())
4661-
return PT->getDecl()->isSpecificProtocol(KnownProtocolKind::AnyObject);
4662-
return false;
4660+
return T->getRValueType()->isAnyObject();
46634661
}
46644662

46654663
static bool isClangSubModule(ModuleDecl *TheModule) {

lib/IRGen/GenCast.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,11 +788,9 @@ void irgen::emitScalarCheckedCast(IRGenFunction &IGF,
788788
llvm::Value *object =
789789
emitMetatypeToAnyObjectDowncast(IGF, metatypeVal, sourceMetatype, mode);
790790

791-
auto anyObjectProtocol =
792-
IGF.IGM.Context.getProtocol(KnownProtocolKind::AnyObject);
793791
SILType anyObjectType =
794792
SILType::getPrimitiveObjectType(
795-
CanType(anyObjectProtocol->getDeclaredType()));
793+
IGF.IGM.Context.getAnyObjectType());
796794

797795
// Continue, pretending that the source value was an (optional) value.
798796
Explosion newValue;

lib/IRGen/GenReflection.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,7 @@ class SuperclassMetadataBuilder : public ReflectionMetadataBuilder {
334334
auto *M = IGM.getSILModule().getSwiftModule();
335335

336336
addTypeRef(M, Class->getDeclaredType()->getCanonicalType());
337-
338-
auto anyObjectDecl = IGM.Context.getProtocol(KnownProtocolKind::AnyObject);
339-
addTypeRef(M, anyObjectDecl->getDeclaredType()->getCanonicalType());
337+
addTypeRef(M, IGM.Context.getAnyObjectType());
340338

341339
B.addInt32(1);
342340
B.addInt32(AssociatedTypeRecordSize);
@@ -724,8 +722,7 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
724722
SwiftType = SwiftType.transform([&](Type t) -> Type {
725723
if (auto *archetype = t->getAs<ArchetypeType>()) {
726724
assert(archetype->requiresClass() && "don't know what to do");
727-
return IGM.Context.getProtocol(KnownProtocolKind::AnyObject)
728-
->getDeclaredType();
725+
return IGM.Context.getAnyObjectType();
729726
}
730727
return t;
731728
})->getCanonicalType();

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,8 +1440,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
14401440
// an imported type or a collection.
14411441
const StructDecl *SD = ty->getStructOrBoundGenericStruct();
14421442
if (ty->isAny()) {
1443-
ty = ctx.getProtocol(KnownProtocolKind::AnyObject)
1444-
->getDeclaredType();
1443+
ty = ctx.getAnyObjectType();
14451444
} else if (SD != ctx.getArrayDecl() &&
14461445
SD != ctx.getDictionaryDecl() &&
14471446
SD != ctx.getSetDecl() &&

lib/SIL/Bridging.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,8 @@ Type TypeConverter::getLoweredCBridgedType(AbstractionPattern pattern,
174174
}
175175

176176
// `Any` can bridge to `AnyObject` (`id` in ObjC).
177-
if (t->isAny()) {
178-
return Context.getProtocol(KnownProtocolKind::AnyObject)->getDeclaredType();
179-
}
177+
if (t->isAny())
178+
return Context.getAnyObjectType();
180179

181180
if (auto funTy = t->getAs<FunctionType>()) {
182181
switch (funTy->getExtInfo().getSILRepresentation()) {

lib/SIL/SILVerifier.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,8 +2139,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
21392139
// If the method returns Self, substitute AnyObject for the result type.
21402140
if (auto fnDecl = dyn_cast<FuncDecl>(method.getDecl())) {
21412141
if (fnDecl->hasDynamicSelf()) {
2142-
auto anyObjectTy = C.getProtocol(KnownProtocolKind::AnyObject)
2143-
->getDeclaredType();
2142+
auto anyObjectTy = C.getAnyObjectType();
21442143
for (auto &dynResult : dynResults) {
21452144
auto newResultTy
21462145
= dynResult.getType()->replaceCovariantResultType(anyObjectTy, 0);

lib/SILGen/SILGenApply.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ replaceSelfTypeForDynamicLookup(ASTContext &ctx,
7070
newResults.append(fnType->getResults().begin(), fnType->getResults().end());
7171
if (auto fnDecl = dyn_cast<FuncDecl>(methodName.getDecl())) {
7272
if (fnDecl->hasDynamicSelf()) {
73-
auto anyObjectTy = ctx.getProtocol(KnownProtocolKind::AnyObject)
74-
->getDeclaredType();
73+
auto anyObjectTy = ctx.getAnyObjectType();
7574
for (auto &result : newResults) {
7675
auto newResultTy
7776
= result.getType()->replaceCovariantResultType(anyObjectTy, 0);
@@ -2601,10 +2600,7 @@ class ArgEmitter {
26012600
}
26022601

26032602
CanType getAnyObjectType() {
2604-
return SGF.getASTContext()
2605-
.getProtocol(KnownProtocolKind::AnyObject)
2606-
->getDeclaredType()
2607-
->getCanonicalType();
2603+
return SGF.getASTContext().getAnyObjectType();
26082604
}
26092605
bool isAnyObjectType(CanType t) {
26102606
return t == getAnyObjectType();

lib/SILGen/SILGenBridging.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,7 @@ static ManagedValue emitNativeToCBridgedNonoptionalValue(SILGenFunction &SGF,
484484

485485
// Fall back to dynamic Any-to-id bridging.
486486
// The destination type should be AnyObject in this case.
487-
assert(loweredBridgedTy->isEqual(
488-
SGF.getASTContext().getProtocol(KnownProtocolKind::AnyObject)
489-
->getDeclaredType()));
487+
assert(loweredBridgedTy->isEqual(SGF.getASTContext().getAnyObjectType()));
490488

491489
// If the input argument is known to be an existential, save the runtime
492490
// some work by opening it.
@@ -757,10 +755,8 @@ static ManagedValue emitCBridgedToNativeValue(SILGenFunction &SGF,
757755

758756
// id-to-Any bridging.
759757
if (loweredNativeTy->isAny()) {
760-
assert(loweredBridgedTy->isEqual(
761-
SGF.getASTContext().getProtocol(KnownProtocolKind::AnyObject)
762-
->getDeclaredType())
763-
&& "Any should bridge to AnyObject");
758+
assert(loweredBridgedTy->isEqual(SGF.getASTContext().getAnyObjectType())
759+
&& "Any should bridge to AnyObject");
764760

765761
// TODO: Ever need to handle +0 values here?
766762
assert(v.hasCleanup());

lib/SILGen/SILGenConvert.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,7 @@ ManagedValue SILGenFunction::emitExistentialErasure(
641641
// its upper bound.
642642
auto anyObjectProto = getASTContext()
643643
.getProtocol(KnownProtocolKind::AnyObject);
644-
auto anyObjectTy = anyObjectProto
645-
? anyObjectProto->getDeclaredType()->getCanonicalType()
646-
: CanType();
644+
auto anyObjectTy = getASTContext().getAnyObjectType();
647645
auto eraseToAnyObject =
648646
[&, concreteFormalType, F](SGFContext C) -> ManagedValue {
649647
auto concreteValue = F(SGFContext());

lib/SILGen/SILGenFunction.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,7 @@ void SILGenFunction::emitArtificialTopLevel(ClassDecl *mainClass) {
482482
auto mainClassAnyObjectConformance = ProtocolConformanceRef(
483483
*SGM.M.getSwiftModule()->lookupConformance(mainClassTy, anyObjectProtocol,
484484
nullptr));
485-
CanType anyObjectTy = anyObjectProtocol
486-
->getDeclaredInterfaceType()
487-
->getCanonicalType();
485+
CanType anyObjectTy = ctx.getAnyObjectType();
488486
CanType anyObjectMetaTy = CanExistentialMetatypeType::get(anyObjectTy,
489487
MetatypeRepresentation::ObjC);
490488

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,8 +1498,7 @@ namespace {
14981498
auto bridgeAnythingRef = ConcreteDeclRef(tc.Context, bridgeAnything,
14991499
valueSub);
15001500
auto valueParenTy = ParenType::get(tc.Context, cs.getType(value));
1501-
auto bridgeTy = tc.Context.getProtocol(KnownProtocolKind::AnyObject)
1502-
->getDeclaredType();
1501+
auto bridgeTy = tc.Context.getAnyObjectType();
15031502
auto bridgeFnTy = FunctionType::get(valueParenTy, bridgeTy);
15041503
// FIXME: Wrap in ParenExpr to prevent bogus "tuple passed as argument"
15051504
// errors.

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3513,8 +3513,7 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
35133513
if (fromBGT->getDecl() == TC.Context.getArrayDecl()) {
35143514
// [AnyObject]
35153515
addConstraint(ConstraintKind::Bind, fromBGT->getGenericArgs()[0],
3516-
TC.Context.getProtocol(KnownProtocolKind::AnyObject)
3517-
->getDeclaredType(),
3516+
TC.Context.getAnyObjectType(),
35183517
getConstraintLocator(
35193518
locator.withPathElement(
35203519
LocatorPathElt::getGenericArgument(0))));
@@ -3533,8 +3532,7 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
35333532
LocatorPathElt::getGenericArgument(0))));
35343533

35353534
addConstraint(ConstraintKind::Bind, fromBGT->getGenericArgs()[1],
3536-
TC.Context.getProtocol(KnownProtocolKind::AnyObject)
3537-
->getDeclaredType(),
3535+
TC.Context.getAnyObjectType(),
35383536
getConstraintLocator(
35393537
locator.withPathElement(
35403538
LocatorPathElt::getGenericArgument(1))));

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,7 @@ LookupResult &ConstraintSystem::lookupMember(Type base, DeclName name) {
220220
result = std::move(lookup);
221221

222222
// If we aren't performing dynamic lookup, we're done.
223-
auto protoTy = base->getAs<ProtocolType>();
224-
if (!*result ||
225-
!protoTy ||
226-
!protoTy->getDecl()->isSpecificProtocol(
227-
KnownProtocolKind::AnyObject))
223+
if (!*result || !base->isAnyObject())
228224
return *result;
229225

230226
// We are performing dynamic lookup. Filter out redundant results early.
@@ -1203,10 +1199,7 @@ ConstraintSystem::getTypeOfMemberReference(
12031199
// For a dynamic result referring to an instance function through
12041200
// an object of metatype type, replace the 'Self' parameter with
12051201
// a AnyObject member.
1206-
Type anyObjectTy = TC.getProtocol(SourceLoc(),
1207-
KnownProtocolKind::AnyObject)
1208-
->getDeclaredTypeOfContext();
1209-
1202+
auto anyObjectTy = TC.Context.getAnyObjectType();
12101203
type = openedFnType->replaceSelfParameterType(anyObjectTy);
12111204
} else {
12121205
// For an unbound instance method reference, replace the 'Self'

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,9 +1738,7 @@ bool swift::fixItOverrideDeclarationTypes(InFlightDiagnostic &diag,
17381738
// bridging---that doesn't count.
17391739
Type bridged;
17401740
if (normalizedBaseTy->isAny()) {
1741-
const ProtocolDecl *anyObjectProto =
1742-
ctx.getProtocol(KnownProtocolKind::AnyObject);
1743-
bridged = anyObjectProto->getDeclaredType();
1741+
bridged = ctx.getAnyObjectType();
17441742
} else {
17451743
bridged = ctx.getBridgedToObjC(DC, normalizedBaseTy);
17461744
}

0 commit comments

Comments
 (0)