16
16
#include " swift/AST/ASTMangler.h"
17
17
#include " swift/AST/ASTVisitor.h"
18
18
#include " swift/AST/AutoDiff.h"
19
+ #include " swift/AST/DeclExportabilityVisitor.h"
19
20
#include " swift/AST/DiagnosticsCommon.h"
20
21
#include " swift/AST/Expr.h"
21
22
#include " swift/AST/FileSystem.h"
@@ -3276,162 +3277,6 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
3276
3277
return count;
3277
3278
}
3278
3279
3279
- class ExternallyAccessibleDeclVisitor
3280
- : public DeclVisitor<ExternallyAccessibleDeclVisitor, bool > {
3281
- public:
3282
- ExternallyAccessibleDeclVisitor (){};
3283
-
3284
- bool visit (const Decl *D) {
3285
- if (auto value = dyn_cast<ValueDecl>(D)) {
3286
- // A decl is externally accessible if it has a public access level.
3287
- auto accessScope =
3288
- value->getFormalAccessScope (/* useDC=*/ nullptr ,
3289
- /* treatUsableFromInlineAsPublic=*/ true );
3290
- if (accessScope.isPublic () || accessScope.isPackage ())
3291
- return true ;
3292
- }
3293
-
3294
- return DeclVisitor<ExternallyAccessibleDeclVisitor, bool >::visit (
3295
- const_cast <Decl *>(D));
3296
- }
3297
-
3298
- // Force all decl kinds to be handled explicitly.
3299
- bool visitDecl (const Decl *D) = delete;
3300
- bool visitValueDecl (const ValueDecl *valueDecl) = delete;
3301
-
3302
- bool visitExtensionDecl (const ExtensionDecl *ext) {
3303
- // Extensions must extend externally accessible types to be externally
3304
- // accessible.
3305
- auto nominalType = ext->getExtendedNominal ();
3306
- if (!nominalType ||
3307
- !isDeserializationSafe (nominalType))
3308
- return false ;
3309
-
3310
- // If the extension has any externally accessible members, then it is
3311
- // externally accessible.
3312
- auto members = ext->getMembers ();
3313
- auto hasSafeMembers = std::any_of (members.begin (), members.end (),
3314
- [](const Decl *D) -> bool {
3315
- if (auto VD = dyn_cast<ValueDecl>(D))
3316
- return isDeserializationSafe (VD);
3317
- return true ;
3318
- });
3319
- if (hasSafeMembers)
3320
- return true ;
3321
-
3322
- // If the extension has any externally accessible conformances, then it is
3323
- // externally accessible.
3324
- auto protocols = ext->getLocalProtocols (ConformanceLookupKind::All);
3325
- bool hasSafeConformances = std::any_of (
3326
- protocols.begin (), protocols.end (), [this ](ProtocolDecl *protocol) {
3327
- return visit (protocol);
3328
- });
3329
-
3330
- if (hasSafeConformances)
3331
- return true ;
3332
-
3333
- // Truly empty extensions are externally accessible. This can occur in
3334
- // swiftinterfaces, for example.
3335
- if (members.empty () && protocols.size () == 0 )
3336
- return true ;
3337
-
3338
- return false ;
3339
- }
3340
-
3341
- bool visitPatternBindingDecl (const PatternBindingDecl *pbd) {
3342
- // Pattern bindings are externally accessible if any of their var decls
3343
- // are externally accessible.
3344
- for (auto i : range (pbd->getNumPatternEntries ())) {
3345
- if (auto *varDecl = pbd->getAnchoringVarDecl (i)) {
3346
- if (isDeserializationSafe (varDecl))
3347
- return true ;
3348
- }
3349
- }
3350
-
3351
- return false ;
3352
- }
3353
-
3354
- bool visitVarDecl (const VarDecl *var) {
3355
- if (var->isLayoutExposedToClients ())
3356
- return true ;
3357
-
3358
- // Consider all lazy var storage as externally accessible.
3359
- // FIXME: We should keep track of what lazy var is associated to the
3360
- // storage for them to preserve the same accessibility.
3361
- if (var->isLazyStorageProperty ())
3362
- return true ;
3363
-
3364
- // Property wrapper storage is as externally accessible as the wrapped
3365
- // property.
3366
- if (VarDecl *wrapped = var->getOriginalWrappedProperty ())
3367
- if (visit (wrapped))
3368
- return true ;
3369
-
3370
- return false ;
3371
- }
3372
-
3373
- bool visitAccessorDecl (const AccessorDecl *accessor) {
3374
- // Accessors are as externally accessible as the storage.
3375
- return visit (accessor->getStorage ());
3376
- }
3377
-
3378
- // ValueDecls with effectively public access are considered externally
3379
- // accessible and are handled in visit(Decl *) above. Some specific kinds of
3380
- // ValueDecls with additional, unique rules are handled individually above.
3381
- // ValueDecls that are not effectively public and do not have unique rules
3382
- // are by default externally inaccessable.
3383
- #define DEFAULT_TO_ACCESS_LEVEL (KIND ) \
3384
- bool visit##KIND##Decl(const KIND##Decl *D) { \
3385
- static_assert (std::is_convertible<KIND##Decl *, ValueDecl *>::value, \
3386
- " ##KIND##Decl must be a ValueDecl" ); \
3387
- return false ; \
3388
- }
3389
- DEFAULT_TO_ACCESS_LEVEL (NominalType);
3390
- DEFAULT_TO_ACCESS_LEVEL (OpaqueType);
3391
- DEFAULT_TO_ACCESS_LEVEL (TypeAlias);
3392
- DEFAULT_TO_ACCESS_LEVEL (AssociatedType);
3393
- DEFAULT_TO_ACCESS_LEVEL (AbstractStorage);
3394
- DEFAULT_TO_ACCESS_LEVEL (AbstractFunction);
3395
- DEFAULT_TO_ACCESS_LEVEL (Macro);
3396
- DEFAULT_TO_ACCESS_LEVEL (EnumElement);
3397
-
3398
- #undef DEFAULT_TO_ACCESS_LEVEL
3399
-
3400
- // There are several kinds of decls which we never expect to encounter in
3401
- // external accessibility queries.
3402
- #define UNREACHABLE (KIND ) \
3403
- bool visit##KIND##Decl(const KIND##Decl *D) { \
3404
- llvm_unreachable (" unexpected decl kind" ); \
3405
- return true ; \
3406
- }
3407
- UNREACHABLE (Module);
3408
- UNREACHABLE (TopLevelCode);
3409
- UNREACHABLE (Import);
3410
- UNREACHABLE (PoundDiagnostic);
3411
- UNREACHABLE (Missing);
3412
- UNREACHABLE (MissingMember);
3413
- UNREACHABLE (MacroExpansion);
3414
- UNREACHABLE (GenericTypeParam);
3415
- UNREACHABLE (Param);
3416
-
3417
- #undef UNREACHABLE
3418
-
3419
- // Uninteresting decls are always considered external. A kind of decl might
3420
- // always be external if it is declared at the top level and access control
3421
- // does not apply to it. Or, a kind of decl could be considered always
3422
- // external because it is only found nested within other declarations that
3423
- // have their own access level, in which case we assume that the declaration
3424
- // context has already been checked.
3425
- #define UNINTERESTING (KIND ) \
3426
- bool visit##KIND##Decl(const KIND##Decl *D) { return true ; }
3427
- UNINTERESTING (IfConfig);
3428
- UNINTERESTING (PrecedenceGroup);
3429
- UNINTERESTING (EnumCase);
3430
- UNINTERESTING (Operator);
3431
-
3432
- #undef UNINTERESTING
3433
- };
3434
-
3435
3280
public:
3436
3281
// / Determine if \p decl is safe to deserialize when it's public
3437
3282
// / or otherwise needed by the client in normal builds, this should usually
@@ -3441,7 +3286,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
3441
3286
// / it, but at the same time keep the safety checks precise to avoid
3442
3287
// / XRef errors and such.
3443
3288
static bool isDeserializationSafe (const Decl *decl) {
3444
- return ExternallyAccessibleDeclVisitor ().visit (decl);
3289
+ return DeclExportabilityVisitor ().visit (decl);
3445
3290
}
3446
3291
3447
3292
private:
0 commit comments