AST: Introduce primitive AnyObject type

Add a 'hasExplicitAnyObject()' bit to ProtocolCompositionType
to represent canonical composition types containing '& AnyObject'.

Serialize this bit and take it into account when building
ExistentialLayouts.

Rename ProtocolCompositionType::getProtocols() to getMembers()
since it can contain classes now, and update a few usages that
need further attention with FIXMEs or asserts.

For now, nothing actually constructs these types, and they will
trigger arounds asserts. Upcoming patches will introduce support
for this.
This commit is contained in:
Slava Pestov
2017-04-13 01:00:25 -07:00
parent ce3f098021
commit d49f8fb6d9
22 changed files with 178 additions and 104 deletions

View File

@@ -17,6 +17,7 @@
#include "ConformanceLookupTable.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/Module.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/ProtocolConformanceRef.h"
@@ -416,28 +417,6 @@ void ConformanceLookupTable::loadAllConformances(
}
}
namespace {
/// Visit the protocols referenced by the given type, which was
/// uttered at the given location.
template<typename AddProtocolFunc>
void visitProtocols(Type type, SourceLoc loc, AddProtocolFunc addProtocol) {
if (!type) return;
// Protocol types.
if (auto protocol = type->getAs<ProtocolType>()) {
addProtocol(protocol->getDecl(), loc);
return;
}
// Protocol compositions.
if (auto composition = type->getAs<ProtocolCompositionType>()) {
for (auto protocol : composition->getProtocols())
visitProtocols(protocol, loc, addProtocol);
return;
}
}
} // end anonymous namespace
bool ConformanceLookupTable::addProtocol(NominalTypeDecl *nominal,
ProtocolDecl *protocol, SourceLoc loc,
ConformanceSource source) {
@@ -492,10 +471,11 @@ void ConformanceLookupTable::addProtocols(NominalTypeDecl *nominal,
// Visit each of the types in the inheritance list to find
// protocols.
for (const auto &entry : inherited) {
visitProtocols(entry.getType(), entry.getLoc(),
[&](ProtocolDecl *protocol, SourceLoc loc) {
addProtocol(nominal, protocol, loc, source);
});
if (!entry.getType() || !entry.getType()->isExistentialType())
continue;
auto layout = entry.getType()->getExistentialLayout();
for (auto *proto : layout.getProtocols())
addProtocol(nominal, proto->getDecl(), entry.getLoc(), source);
}
}