@@ -34,6 +34,7 @@ namespace swift {
3434 class Type ;
3535 class TypeRepr ;
3636 class ValueDecl ;
37+
3738enum class DeclAvailabilityFlag : uint8_t {
3839 // / Do not diagnose uses of protocols in versions before they were introduced.
3940 // / Used when type-checking protocol conformances, since conforming to a
@@ -68,6 +69,29 @@ enum class ExportabilityReason : unsigned {
6869 ExtensionWithConditionalConformances
6970};
7071
72+ // / A description of the restrictions on what declarations can be referenced
73+ // / from a the signature or body of a declaration.
74+ // /
75+ // / We say a declaration is "exported" if it is `public` or
76+ // / `@usableFromInline`, not `_@spi`, and not visible via an
77+ // / `@_implementationOnly` import.
78+ // /
79+ // / The "signature" of a declaration is the set of all types written in the
80+ // / declaration (such as function parameter and return types), but not
81+ // / including the function body.
82+ // /
83+ // / The signature of an exported declaration can only reference other
84+ // / exported types.
85+ // /
86+ // / The body of an inlinable function can only reference other `public` and
87+ // / `@usableFromInline` declarations; furthermore, if the inlinable
88+ // / function is also exported, its body is restricted to referencing other
89+ // / exported declarations.
90+ // /
91+ // / The ExportContext also stores if the location in the program is inside
92+ // / of a function or type body with deprecated or unavailable availability.
93+ // / This allows referencing other deprecated and unavailable declarations,
94+ // / without producing a warning or error, respectively.
7195class ExportContext {
7296 DeclContext *DC;
7397 FragileFunctionKind FragileKind;
@@ -78,20 +102,54 @@ class ExportContext {
78102 ExportContext (DeclContext *DC, FragileFunctionKind kind, bool spi, bool exported);
79103
80104public:
105+
106+ // / Create an instance describing the types that can be referenced from the
107+ // / given declaration's signature.
108+ // /
109+ // / If the declaration is exported, the resulting context is restricted to
110+ // / referencing exported types only. Otherwise it can reference anything.
81111 static ExportContext forDeclSignature (Decl *D);
112+
113+ // / Create an instance describing the declarations that can be referenced
114+ // / from the given function's body.
115+ // /
116+ // / If the function is inlinable, the resulting context is restricted to
117+ // / referencing ABI-public declarations only. Furthermore, if the function
118+ // / is exported, referenced declarations must also be exported. Otherwise
119+ // / it can reference anything.
82120 static ExportContext forFunctionBody (DeclContext *DC);
83121
122+ // / Produce a new context with the same properties as this one, except
123+ // / changing the ExportabilityReason. This only affects diagnostics.
84124 ExportContext forReason (ExportabilityReason reason) const ;
125+
126+ // / Produce a new context with the same properties as this one, except
127+ // / that if 'exported' is false, the resulting context can reference
128+ // / declarations that are not exported. If 'exported' is true, the
129+ // / resulting context is indentical to this one.
130+ // /
131+ // / That is, this will perform a 'bitwise and' on the 'exported' bit.
85132 ExportContext forExported (bool exported) const ;
86133
87134 DeclContext *getDeclContext () const { return DC; }
135+
136+ // / If not 'None', the context has the inlinable function body restriction.
88137 FragileFunctionKind getFragileFunctionKind () const { return FragileKind; }
89138
139+ // / If true, the context is SPI and can reference SPI declarations.
90140 bool isSPI () const { return SPI; }
141+
142+ // / If true, the context is exported and cannot reference SPI declarations
143+ // / or declarations from `@_implementationOnly` imports.
91144 bool isExported () const { return Exported; }
92145
146+ // / If true, the context can only reference exported declarations, either
147+ // / because it is the signature context of an exported declaration, or
148+ // / because it is the function body context of an inlinable function.
93149 bool mustOnlyReferenceExportedDecls () const ;
94150
151+ // / Get the ExportabilityReason for diagnostics. If this is 'None', there
152+ // / are no restrictions on referencing unexported declarations.
95153 Optional<ExportabilityReason> getExportabilityReason () const ;
96154};
97155
0 commit comments