@@ -109,12 +109,14 @@ namespace {
109
109
// / The inferred availability required to access a group of declarations
110
110
// / on a single platform.
111
111
struct InferredAvailability {
112
- PlatformAgnosticAvailabilityKind PlatformAgnostic
113
- = PlatformAgnosticAvailabilityKind::None;
112
+ AvailableAttr::Kind Kind = AvailableAttr::Kind::Default;
114
113
115
114
std::optional<llvm::VersionTuple> Introduced;
116
115
std::optional<llvm::VersionTuple> Deprecated;
117
116
std::optional<llvm::VersionTuple> Obsoleted;
117
+
118
+ StringRef Message;
119
+ StringRef Rename;
118
120
bool IsSPI = false ;
119
121
};
120
122
@@ -148,10 +150,9 @@ mergeIntoInferredVersion(const std::optional<llvm::VersionTuple> &Version,
148
150
static void mergeWithInferredAvailability (SemanticAvailableAttr Attr,
149
151
InferredAvailability &Inferred) {
150
152
auto *ParsedAttr = Attr.getParsedAttr ();
151
- Inferred.PlatformAgnostic = static_cast <PlatformAgnosticAvailabilityKind>(
152
- std::max (static_cast <unsigned >(Inferred.PlatformAgnostic ),
153
- static_cast <unsigned >(
154
- ParsedAttr->getPlatformAgnosticAvailability ())));
153
+ Inferred.Kind = static_cast <AvailableAttr::Kind>(
154
+ std::max (static_cast <unsigned >(Inferred.Kind ),
155
+ static_cast <unsigned >(ParsedAttr->getKind ())));
155
156
156
157
// The merge of two introduction versions is the maximum of the two versions.
157
158
if (mergeIntoInferredVersion (Attr.getIntroduced (), Inferred.Introduced ,
@@ -162,21 +163,24 @@ static void mergeWithInferredAvailability(SemanticAvailableAttr Attr,
162
163
// The merge of deprecated and obsoleted versions takes the minimum.
163
164
mergeIntoInferredVersion (Attr.getDeprecated (), Inferred.Deprecated , std::min);
164
165
mergeIntoInferredVersion (Attr.getObsoleted (), Inferred.Obsoleted , std::min);
166
+
167
+ if (Inferred.Message .empty () && !Attr.getMessage ().empty ())
168
+ Inferred.Message = Attr.getMessage ();
169
+
170
+ if (Inferred.Rename .empty () && !Attr.getRename ().empty ())
171
+ Inferred.Rename = Attr.getRename ();
165
172
}
166
173
167
- // / Create an implicit availability attribute for the given platform
174
+ // / Create an implicit availability attribute for the given domain
168
175
// / and with the inferred availability.
169
- static AvailableAttr *createAvailableAttr (PlatformKind Platform ,
176
+ static AvailableAttr *createAvailableAttr (AvailabilityDomain Domain ,
170
177
const InferredAvailability &Inferred,
171
- StringRef Message,
172
- StringRef Rename,
173
- ValueDecl *RenameDecl,
174
178
ASTContext &Context) {
175
179
// If there is no information that would go into the availability attribute,
176
180
// don't create one.
177
- if (Inferred.PlatformAgnostic == PlatformAgnosticAvailabilityKind::None &&
178
- !Inferred.Introduced && !Inferred.Deprecated && ! Inferred.Obsoleted &&
179
- Message. empty () && Rename.empty () && !RenameDecl )
181
+ if (Inferred.Kind == AvailableAttr::Kind::Default && !Inferred. Introduced &&
182
+ !Inferred.Deprecated && !Inferred.Obsoleted && Inferred.Message . empty () &&
183
+ Inferred. Rename .empty ())
180
184
return nullptr ;
181
185
182
186
llvm::VersionTuple Introduced =
@@ -186,36 +190,26 @@ static AvailableAttr *createAvailableAttr(PlatformKind Platform,
186
190
llvm::VersionTuple Obsoleted =
187
191
Inferred.Obsoleted .value_or (llvm::VersionTuple ());
188
192
189
- return new (Context)
190
- AvailableAttr (SourceLoc (), SourceRange (), Platform, Message, Rename,
191
- Introduced, SourceRange (), Deprecated, SourceRange (),
192
- Obsoleted, SourceRange (), Inferred.PlatformAgnostic ,
193
- /* Implicit=*/ true , Inferred.IsSPI );
193
+ return new (Context) AvailableAttr (
194
+ SourceLoc (), SourceRange (), Domain, Inferred.Kind , Inferred.Message ,
195
+ Inferred.Rename , Introduced, SourceRange (), Deprecated, SourceRange (),
196
+ Obsoleted, SourceRange (), /* Implicit=*/ true , Inferred.IsSPI );
194
197
}
195
198
196
199
void AvailabilityInference::applyInferredAvailableAttrs (
197
200
Decl *ToDecl, ArrayRef<const Decl *> InferredFromDecls) {
198
201
auto &Context = ToDecl->getASTContext ();
199
202
200
- // Let the new AvailabilityAttr inherit the message and rename.
201
- // The first encountered message / rename will win; this matches the
202
- // behaviour of diagnostics for 'non-inherited' AvailabilityAttrs.
203
- StringRef Message;
204
- StringRef Rename;
205
- ValueDecl *RenameDecl = nullptr ;
206
-
207
203
// Iterate over the declarations and infer required availability on
208
204
// a per-platform basis.
209
- // FIXME: [availability] Generalize to AvailabilityDomain.
210
- std::map<PlatformKind, InferredAvailability> Inferred;
205
+ std::map<AvailabilityDomain, InferredAvailability> Inferred;
211
206
for (const Decl *D : InferredFromDecls) {
212
207
llvm::SmallVector<SemanticAvailableAttr, 8 > MergedAttrs;
213
208
214
209
do {
215
210
llvm::SmallVector<SemanticAvailableAttr, 8 > PendingAttrs;
216
211
217
- for (auto AvAttr :
218
- D->getSemanticAvailableAttrs ()) {
212
+ for (auto AvAttr : D->getSemanticAvailableAttrs ()) {
219
213
// Skip an attribute from an outer declaration if it is for a platform
220
214
// that was already handled implicitly by an attribute from an inner
221
215
// declaration.
@@ -226,14 +220,8 @@ void AvailabilityInference::applyInferredAvailableAttrs(
226
220
}))
227
221
continue ;
228
222
229
- mergeWithInferredAvailability (AvAttr, Inferred[AvAttr.getPlatform ()]);
223
+ mergeWithInferredAvailability (AvAttr, Inferred[AvAttr.getDomain ()]);
230
224
PendingAttrs.push_back (AvAttr);
231
-
232
- if (Message.empty () && !AvAttr.getMessage ().empty ())
233
- Message = AvAttr.getMessage ();
234
-
235
- if (Rename.empty () && !AvAttr.getRename ().empty ())
236
- Rename = AvAttr.getRename ();
237
225
}
238
226
239
227
MergedAttrs.append (PendingAttrs);
@@ -245,20 +233,12 @@ void AvailabilityInference::applyInferredAvailableAttrs(
245
233
}
246
234
247
235
DeclAttributes &Attrs = ToDecl->getAttrs ();
248
- auto *ToValueDecl = dyn_cast<ValueDecl>(ToDecl);
249
236
250
237
// Create an availability attribute for each observed platform and add
251
238
// to ToDecl.
252
239
for (auto &Pair : Inferred) {
253
- auto *Attr = createAvailableAttr (Pair.first , Pair.second , Message,
254
- Rename, RenameDecl, Context);
255
-
256
- if (Attr) {
257
- if (RenameDecl && ToValueDecl)
258
- ToValueDecl->setRenamedDecl (Attr, RenameDecl);
259
-
240
+ if (auto Attr = createAvailableAttr (Pair.first , Pair.second , Context))
260
241
Attrs.add (Attr);
261
- }
262
242
}
263
243
}
264
244
0 commit comments