20
20
21
21
namespace swift {
22
22
23
- // / Used to provide the kind of scope limitation in AccessScope::Value
24
- enum class AccessLimitKind : uint8_t { None = 0 , Private, Package };
25
-
26
23
// / The wrapper around the outermost DeclContext from which
27
24
// / a particular declaration can be accessed.
28
25
class AccessScope {
29
- // / The declaration context along with an enum indicating the level of
30
- // / scope limitation.
31
- // / If the declaration context is set, and the limit kind is Private, the
32
- // / access level is considered 'private'. Whether it's 'internal' or
33
- // / 'fileprivate' is determined by what the declaration context casts to. If
34
- // / the declaration context is null, and the limit kind is None, the access
35
- // / level is considered 'public'. If the limit kind is Private, the access
36
- // / level is considered SPI. If it's Package, the access level is considered
37
- // / 'package'. Below is a table showing the combinations.
26
+ // / To validate access of a decl, access scope check for both decl site
27
+ // / and use site needs to be done. The underlying mechanism uses a
28
+ // / pointer-int pair to determine the scope, as shown in the table below.
38
29
// /
39
- // / AccessLimitKind DC == nullptr DC != nullptr
40
- // / ---------------------------------------------------------------------------
41
- // / None public fileprivate or internal (check DC to tell which)
42
- // / Private `@_spi` public private
43
- // / Package package (unused)
44
-
45
- llvm::PointerIntPair<const DeclContext *, 2 , AccessLimitKind> Value;
30
+ // / AccessScope DeclContext bool AccessLevel
31
+ // / --------------------------------------------
32
+ // / Public nullptr false public or open
33
+ // / Public nullptr true public `@_spi`
34
+ // / Package PackageUnit (unused) package
35
+ // / Module ModuleDecl false internal
36
+ // / FileScope FileUnit false fileprivate
37
+ // / Private FileUnit true private
38
+ // /
39
+ // / For example, if a decl with `public` access level is referenced outside of
40
+ // / its defining module, it will be maped to the <nullptr, false> pair during
41
+ // / the access scope check. This pair is determined based on the decl's access
42
+ // / level in \c getAccessScopeForFormalAccess and passed to
43
+ // / \c checkAccessUsingAccessScope which checks decl context hierarchy
44
+ // / and others.
45
+ // /
46
+ // / Note that the use site always has a non-null DeclContext whether or not
47
+ // / the use site itself is a public type.
48
+ // /
49
+ // / \see AccessScope::getAccessScopeForFormalAccess
50
+ // / \see AccessScope::checkAccessUsingAccessScope
51
+ // / \see ASTHierarchy
52
+ llvm::PointerIntPair<const DeclContext *, 1 , bool > Value;
46
53
47
54
public:
48
- AccessScope (const DeclContext *DC,
49
- AccessLimitKind limitKind = AccessLimitKind::None);
55
+ AccessScope (const DeclContext *DC, bool isPrivate = false );
50
56
51
- static AccessScope getPublic () {
52
- return AccessScope (nullptr , AccessLimitKind::None);
53
- }
54
- static AccessScope getPackage () {
55
- return AccessScope (nullptr , AccessLimitKind::Package);
56
- }
57
+ static AccessScope getPublic () { return AccessScope (nullptr , false ); }
57
58
58
59
// / Check if private access is allowed. This is a lexical scope check in Swift
59
60
// / 3 mode. In Swift 4 mode, declarations and extensions of the same type will
@@ -66,63 +67,58 @@ class AccessScope {
66
67
bool operator ==(AccessScope RHS) const { return Value == RHS.Value ; }
67
68
bool operator !=(AccessScope RHS) const { return !(*this == RHS); }
68
69
bool hasEqualDeclContextWith (AccessScope RHS) const {
69
- if (isPublic ())
70
- return RHS.isPublic ();
71
- if (isPackage ())
72
- return RHS.isPackage ();
73
70
return getDeclContext () == RHS.getDeclContext ();
74
71
}
75
72
76
- bool isPublic () const {
77
- return !Value.getPointer () && Value.getInt () == AccessLimitKind::None;
78
- }
79
- bool isPrivate () const {
80
- return Value.getPointer () && Value.getInt () == AccessLimitKind::Private;
81
- }
73
+ bool isPublic () const { return !Value.getPointer (); }
74
+ bool isPrivate () const { return Value.getPointer () && Value.getInt (); }
82
75
bool isFileScope () const ;
83
76
bool isInternal () const ;
84
- bool isPackage () const {
85
- return !Value.getPointer () && Value.getInt () == AccessLimitKind::Package;
86
- }
87
-
88
- // / Returns true if the context of this (use site) is more restrictive than
89
- // / the argument context (decl site). This function does _not_ check the
90
- // / restrictiveness of the access level between this and the argument. \see
91
- // / AccessScope::isInContext
92
- bool isChildOf (AccessScope AS) const {
93
- if (isInContext ()) {
94
- if (AS.isInContext ())
95
- return allowsPrivateAccess (getDeclContext (), AS.getDeclContext ());
96
- else
97
- return AS.isPackage () || AS.isPublic ();
98
- }
99
- if (isPackage ())
100
- return AS.isPublic ();
101
- // If this is public, it can't be less than access level of AS
102
- // so return false
103
- return false ;
104
- }
77
+ bool isPackage () const ;
105
78
106
- // / Result depends on whether it's called at a use site or a decl site:
79
+ // / Checks if the DeclContext of this access scope (use site) is more
80
+ // / restrictive than that of the argument (decl site) based on the DeclContext
81
+ // / hierarchy (from most to least restrictive):
82
+ // / decl/expr (e.g. ClassDecl) -> FileUnit -> ModuleDecl -> PackageUnit -> null
83
+ // /
84
+ // / A few things to note:
85
+ // / 1. If both have the same DeclContext, returns false as one is _not_ a
86
+ // / child of the other.
87
+ // / 2. This function does _not_ check the restrictiveness of the _access
88
+ // / level_ between two decls.
89
+ // / 3. The DeclContext of this (use site) is is always non-null even if it may
90
+ // / have a `public` access level.
107
91
// /
108
92
// / For example,
109
93
// /
110
94
// / ```
111
- // / public func foo(_ arg: bar) {} // `bar` is a `package` decl in another
112
- // / module
113
- // / ```
95
+ // / import OtherModule
114
96
// /
115
- // / The meaning of \c isInContext changes whether it's at the use site or the
116
- // / decl site.
97
+ // / // `Foo` is a `public` struct defined in `OtherModule`
98
+ // / public func myFunc(_ arg: OtherModule.Foo) {}
99
+ // / ```
117
100
// /
118
- // / The use site of \c bar, i.e. \c foo, is "in context" (decl context is
119
- // / non-null), regardless of the access level of \c foo (\c public in this
120
- // / case).
101
+ // / The use site of `Foo`is a function `myFunc`, and its DeclContext is
102
+ // / non-null (e.g. FileUnit) even though the function decl itself is `public`.
103
+ // / When `isChildOf` is called, the argument passed in is a pair <nullptr,
104
+ // / false> created in \c getAccessScopeForFormalAccess based on the access
105
+ // / level of `Foo`. Since FileUnit is a child of null in the DeclContext
106
+ // / hierarchy described above, it returns true.
121
107
// /
122
- // / The decl site of \c bar is only "in context" if the access level of the
123
- // / decl is \c internal or more restrictive. The context at the decl site is\c
124
- // / FileUnit if the decl is \c fileprivate or \c private; \c ModuleDecl if \c
125
- // / internal, and null if \c package or \c public.
108
+ // / \see AccessScope::getAccessScopeForFormalAccess
109
+ // / \see ASTHierarchy
110
+ bool isChildOf (AccessScope AS) const {
111
+ if (isPackage ()) // This needs to be checked first before isInContext
112
+ return AS.isPublic ();
113
+ else if (isInContext ()) {
114
+ if (AS.isInContext ())
115
+ return allowsPrivateAccess (getDeclContext (), AS.getDeclContext ());
116
+ else
117
+ return AS.isPublic ();
118
+ } else // It's public, so can't be a child of the argument scope
119
+ return false ;
120
+ }
121
+
126
122
bool isInContext () const { return getDeclContext () != nullptr ; }
127
123
128
124
// / Returns the associated access level for diagnostic purposes.
0 commit comments