|
| 1 | +//===- DeclExportabilityVisitor.h - Swift Language Context ASTs -*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file defines the DeclExportabilityVisitor class. "Exportability" refers |
| 14 | +// to whether a declaration may be referenced from outside of the module it |
| 15 | +// is defined in. |
| 16 | +// |
| 17 | +//===----------------------------------------------------------------------===// |
| 18 | + |
| 19 | +#ifndef SWIFT_DECLEXPORTABILITYVISITOR_H |
| 20 | +#define SWIFT_DECLEXPORTABILITYVISITOR_H |
| 21 | + |
| 22 | +#include "swift/AST/ASTVisitor.h" |
| 23 | +#include "swift/AST/Decl.h" |
| 24 | + |
| 25 | +namespace swift { |
| 26 | + |
| 27 | +/// This visitor determines whether a declaration is "exportable", meaning whether |
| 28 | +/// it can be referenced by other modules. For example, a function with a public |
| 29 | +/// access level or with the `@usableFromInline` attribute is exportable. |
| 30 | +class DeclExportabilityVisitor |
| 31 | + : public DeclVisitor<DeclExportabilityVisitor, bool> { |
| 32 | +public: |
| 33 | + DeclExportabilityVisitor(){}; |
| 34 | + |
| 35 | + bool visit(const Decl *D) { |
| 36 | + if (auto value = dyn_cast<ValueDecl>(D)) { |
| 37 | + // A decl is exportable if it has a public access level. |
| 38 | + auto accessScope = |
| 39 | + value->getFormalAccessScope(/*useDC=*/nullptr, |
| 40 | + /*treatUsableFromInlineAsPublic=*/true); |
| 41 | + if (accessScope.isPublic() || accessScope.isPackage()) |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + return DeclVisitor<DeclExportabilityVisitor, bool>::visit( |
| 46 | + const_cast<Decl *>(D)); |
| 47 | + } |
| 48 | + |
| 49 | + // Force all decl kinds to be handled explicitly. |
| 50 | + bool visitDecl(const Decl *D) = delete; |
| 51 | + bool visitValueDecl(const ValueDecl *valueDecl) = delete; |
| 52 | + |
| 53 | + bool visitExtensionDecl(const ExtensionDecl *ext) { |
| 54 | + // Extensions must extend exportable types to be exportable. |
| 55 | + auto nominalType = ext->getExtendedNominal(); |
| 56 | + if (!nominalType || !visit(nominalType)) |
| 57 | + return false; |
| 58 | + |
| 59 | + // If the extension has any exportable members, then it is exportable. |
| 60 | + auto members = ext->getMembers(); |
| 61 | + auto hasSafeMembers = |
| 62 | + std::any_of(members.begin(), members.end(), [&](const Decl *D) -> bool { |
| 63 | + if (auto VD = dyn_cast<ValueDecl>(D)) |
| 64 | + return visit(VD); |
| 65 | + return true; |
| 66 | + }); |
| 67 | + if (hasSafeMembers) |
| 68 | + return true; |
| 69 | + |
| 70 | + // If the extension has any exportable conformances, then it is exportable. |
| 71 | + auto protocols = ext->getLocalProtocols(ConformanceLookupKind::All); |
| 72 | + bool hasSafeConformances = |
| 73 | + std::any_of(protocols.begin(), protocols.end(), |
| 74 | + [this](ProtocolDecl *protocol) { return visit(protocol); }); |
| 75 | + |
| 76 | + if (hasSafeConformances) |
| 77 | + return true; |
| 78 | + |
| 79 | + // Truly empty extensions are exportable. This can occur in swiftinterfaces, |
| 80 | + // for example. |
| 81 | + if (members.empty() && protocols.size() == 0) |
| 82 | + return true; |
| 83 | + |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + bool visitPatternBindingDecl(const PatternBindingDecl *pbd) { |
| 88 | + // Pattern bindings are exportable if any of their var decls are exportable. |
| 89 | + for (auto i : range(pbd->getNumPatternEntries())) { |
| 90 | + if (auto *varDecl = pbd->getAnchoringVarDecl(i)) { |
| 91 | + if (visit(varDecl)) |
| 92 | + return true; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + bool visitVarDecl(const VarDecl *var) { |
| 100 | + if (var->isLayoutExposedToClients()) |
| 101 | + return true; |
| 102 | + |
| 103 | + // Consider all lazy var storage as exportable. |
| 104 | + // FIXME: We should keep track of what lazy var is associated to the |
| 105 | + // storage for them to preserve the same accessibility. |
| 106 | + if (var->isLazyStorageProperty()) |
| 107 | + return true; |
| 108 | + |
| 109 | + // Property wrapper storage is as exportable as the wrapped property. |
| 110 | + if (VarDecl *wrapped = var->getOriginalWrappedProperty()) |
| 111 | + if (visit(wrapped)) |
| 112 | + return true; |
| 113 | + |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + bool visitAccessorDecl(const AccessorDecl *accessor) { |
| 118 | + // Accessors are as exportable as their storage. |
| 119 | + return visit(accessor->getStorage()); |
| 120 | + } |
| 121 | + |
| 122 | + // ValueDecls with effectively public access are considered exportable and are |
| 123 | + // handled in visit(Decl *) above. Some specific kinds of ValueDecls with |
| 124 | + // additional, unique rules are handled individually above. ValueDecls that |
| 125 | + // are not effectively public and do not have unique rules are by default not |
| 126 | + // exportable. |
| 127 | +#define DEFAULT_TO_ACCESS_LEVEL(KIND) \ |
| 128 | + bool visit##KIND##Decl(const KIND##Decl *D) { \ |
| 129 | + static_assert(std::is_convertible<KIND##Decl *, ValueDecl *>::value, \ |
| 130 | + "##KIND##Decl must be a ValueDecl"); \ |
| 131 | + return false; \ |
| 132 | + } |
| 133 | + DEFAULT_TO_ACCESS_LEVEL(NominalType); |
| 134 | + DEFAULT_TO_ACCESS_LEVEL(OpaqueType); |
| 135 | + DEFAULT_TO_ACCESS_LEVEL(TypeAlias); |
| 136 | + DEFAULT_TO_ACCESS_LEVEL(AssociatedType); |
| 137 | + DEFAULT_TO_ACCESS_LEVEL(AbstractStorage); |
| 138 | + DEFAULT_TO_ACCESS_LEVEL(AbstractFunction); |
| 139 | + DEFAULT_TO_ACCESS_LEVEL(Macro); |
| 140 | + DEFAULT_TO_ACCESS_LEVEL(EnumElement); |
| 141 | + |
| 142 | +#undef DEFAULT_TO_ACCESS_LEVEL |
| 143 | + |
| 144 | + // There are several kinds of decls which we never expect to encounter in |
| 145 | + // exportability queries. |
| 146 | +#define UNREACHABLE(KIND) \ |
| 147 | + bool visit##KIND##Decl(const KIND##Decl *D) { \ |
| 148 | + llvm_unreachable("unexpected decl kind"); \ |
| 149 | + return true; \ |
| 150 | + } |
| 151 | + UNREACHABLE(Module); |
| 152 | + UNREACHABLE(TopLevelCode); |
| 153 | + UNREACHABLE(Import); |
| 154 | + UNREACHABLE(PoundDiagnostic); |
| 155 | + UNREACHABLE(Missing); |
| 156 | + UNREACHABLE(MissingMember); |
| 157 | + UNREACHABLE(MacroExpansion); |
| 158 | + UNREACHABLE(GenericTypeParam); |
| 159 | + UNREACHABLE(Param); |
| 160 | + |
| 161 | +#undef UNREACHABLE |
| 162 | + |
| 163 | + // Uninteresting decls are always considered exportable. A kind of decl might |
| 164 | + // always be exportable if it is declared at the top level and access control |
| 165 | + // does not apply to it. Or, a kind of decl could be considered always |
| 166 | + // exportable because it is only found nested within other declarations that |
| 167 | + // have their own access level, in which case we assume that the declaration |
| 168 | + // context has already been checked. |
| 169 | +#define UNINTERESTING(KIND) \ |
| 170 | + bool visit##KIND##Decl(const KIND##Decl *D) { return true; } |
| 171 | + UNINTERESTING(IfConfig); |
| 172 | + UNINTERESTING(PrecedenceGroup); |
| 173 | + UNINTERESTING(EnumCase); |
| 174 | + UNINTERESTING(Operator); |
| 175 | + |
| 176 | +#undef UNINTERESTING |
| 177 | +}; |
| 178 | +} // end namespace swift |
| 179 | + |
| 180 | +#endif |
0 commit comments