-
Notifications
You must be signed in to change notification settings - Fork 10.5k
ProtocolConformanceAnalysis #17177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ProtocolConformanceAnalysis #17177
Conversation
virtual void invalidateFunctionTables() override {} | ||
|
||
/// Get the nominal types that implement a protocol. | ||
const NominalTypeList &getConformances(ProtocolDecl *P) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this method itself be const?
|
||
/// Get the nominal types that implement a protocol. | ||
const NominalTypeList &getConformances(ProtocolDecl *P) { | ||
return ProtocolConformanceCache[P]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use find() instead?
This looks great, I only have two minor nitpicks. |
2e8fbab
to
aa368ca
Compare
@slavapestov have a look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, just a handful more suggestions.
if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) { | ||
auto Protocols = NTD->getAllProtocols(); | ||
for (auto &Protocol : Protocols) { | ||
ProtocolConformanceCache[Protocol].push_back(NTD); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the 'access level > internal' check up here
|
||
public: | ||
NominalTypeWalker(ProtocolConformanceAnalysis::ProtocolConformanceMap | ||
&ProtocolConformanceCache) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: This should line up with opening (
void ProtocolConformanceAnalysis::init() { | ||
|
||
// We only do this in Whole-Module compilation mode. | ||
if (!(M->isWholeModule())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unnecessary ()
/// or lower in access levels at a whole-module compilation mode. | ||
if (!ProtocolConformanceCache.count(ProtoDecl)) | ||
continue; | ||
if (ProtoDecl->getEffectiveAccess() > AccessLevel::Internal) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this (see above)
const NominalTypeList *getConformances(const ProtocolDecl *P) const { | ||
auto ConformsListIt = ProtocolConformanceCache.find(P); | ||
return ConformsListIt != ProtocolConformanceCache.end() | ||
? reinterpret_cast<const NominalTypeList *>(&*ConformsListIt) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the cast here is necessary
virtual void invalidateFunctionTables() override {} | ||
|
||
/// Get the nominal types that implement a protocol. | ||
const NominalTypeList *getConformances(const ProtocolDecl *P) const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be better to return an ArrayRef<NominalTypeDecl *>
here instead of a pointer to a SmallVector
@swift-ci Please smoke test |
1 similar comment
@swift-ci Please smoke test |
aa368ca
to
9c1817c
Compare
These are excellent suggestions @slavapestov! Please have a look now. |
9c1817c
to
ab95ea2
Compare
|
||
bool walkToDeclPre(Decl *D) override { | ||
if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) { | ||
if (NTD->getEffectiveAccess() <= AccessLevel::Internal) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the right check here for an internal protocol, not an internal conforming type? Public protocols can always have conforming types from outside the module.
… types (classes, structs, and enums) that conform to it. It is performed in the whole-module-compilation mode.
ab95ea2
to
c2aef11
Compare
The check is both ways and is dependent on where you want to add the check.
I have updated it to carry only the non-public protocol information in the
cache.
…On Mon, Jun 18, 2018 at 1:08 PM, Slava Pestov ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In lib/SILOptimizer/Analysis/ProtocolConformanceAnalysis.cpp
<#17177 (comment)>:
> +
+using namespace swift;
+
+namespace {
+/// A helper class to collect all nominal type declarations.
+class NominalTypeWalker : public ASTWalker {
+ ProtocolConformanceAnalysis::ProtocolConformanceMap &ProtocolConformanceCache;
+
+public:
+ NominalTypeWalker(ProtocolConformanceAnalysis::ProtocolConformanceMap
+ &ProtocolConformanceCache)
+ : ProtocolConformanceCache(ProtocolConformanceCache) {}
+
+ bool walkToDeclPre(Decl *D) override {
+ if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {
+ if (NTD->getEffectiveAccess() <= AccessLevel::Internal) {
Isn't the right check here for an internal *protocol*, not an internal
conforming type? Public protocols can always have conforming types from
outside the module.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#17177 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AFBwaZzQIjvB66-61YZqrKvbWRwvnpbDks5t-AjCgaJpZM4Umpc7>
.
|
@swift-ci Please smoke test |
1 similar comment
@swift-ci Please smoke test |
@rajbarik Thanks for making the changes. The PR looks good now, I'm going to merge it. |
ProtocolConformanceAnalysis for a non-public protocol returns all the types (classes, structs, and enums) that conform to it. It is performed in the whole-module-compilation mode.
It is part one (only analysis phase) of the PR at #16739.