-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathapi_availability.dart
More file actions
249 lines (214 loc) · 7.1 KB
/
api_availability.dart
File metadata and controls
249 lines (214 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:meta/meta.dart';
import '../../config_provider/config_types.dart';
import '../../context.dart';
import '../clang_bindings/clang_bindings.dart' as clang_types;
import '../utils.dart';
enum Availability { none, some, all }
class ApiAvailability {
final bool alwaysDeprecated;
final bool alwaysUnavailable;
final PlatformAvailability? ios;
final PlatformAvailability? macos;
final bool swiftUnavailable;
late final Availability availability;
ApiAvailability({
this.alwaysDeprecated = false,
this.alwaysUnavailable = false,
this.ios,
this.macos,
this.swiftUnavailable = false,
required ExternalVersions? externalVersions,
}) {
availability = _getAvailability(externalVersions);
}
static ApiAvailability fromCursor(
clang_types.CXCursor cursor,
Context context,
) {
final platformsLength = clang.clang_getCursorPlatformAvailability(
cursor,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
0,
);
final alwaysDeprecated = calloc<Int>();
final alwaysUnavailable = calloc<Int>();
final platforms = calloc<clang_types.CXPlatformAvailability>(
platformsLength,
);
clang.clang_getCursorPlatformAvailability(
cursor,
alwaysDeprecated,
nullptr,
alwaysUnavailable,
nullptr,
platforms,
platformsLength,
);
PlatformAvailability? ios;
PlatformAvailability? macos;
var swiftIsUnavailable = false;
for (var i = 0; i < platformsLength; ++i) {
final platform = platforms[i];
final platformAvailability = PlatformAvailability(
introduced: platform.Introduced.triple,
deprecated: platform.Deprecated.triple,
obsoleted: platform.Obsoleted.triple,
unavailable: platform.Unavailable != 0,
);
switch (platform.Platform.string()) {
case 'ios':
ios = platformAvailability..name = 'iOS';
break;
case 'macos':
macos = platformAvailability..name = 'macOS';
break;
case 'swift':
if (platformAvailability.unavailable) swiftIsUnavailable = true;
break;
}
}
final api = ApiAvailability(
alwaysDeprecated: alwaysDeprecated.value != 0,
alwaysUnavailable: alwaysUnavailable.value != 0,
ios: ios,
macos: macos,
swiftUnavailable: swiftIsUnavailable,
externalVersions: context.config.objectiveC?.externalVersions,
);
for (var i = 0; i < platformsLength; ++i) {
clang.clang_disposeCXPlatformAvailability(platforms + i);
}
calloc.free(alwaysDeprecated);
calloc.free(alwaysUnavailable);
calloc.free(platforms);
return api;
}
Availability _getAvailability(ExternalVersions? externalVersions) {
final macosVer = _normalizeVersions(externalVersions?.macos);
final iosVer = _normalizeVersions(externalVersions?.ios);
// If no versions are specified, everything is available.
if (iosVer == null && macosVer == null) {
return Availability.all;
}
if (alwaysDeprecated || alwaysUnavailable) {
return Availability.none;
}
Availability? availability_;
for (final (platform, version) in [(ios, iosVer), (macos, macosVer)]) {
// If the user hasn't specified any versions for this platform, defer to
// the other platforms.
if (version == null) {
continue;
}
// If the API is available on any platform, return that it's available.
final platAvailability =
platform?.getAvailability(version) ?? Availability.all;
availability_ = _mergeAvailability(availability_, platAvailability);
}
return availability_ ?? Availability.none;
}
// If the min and max version are null, the versions object should be null.
static Versions? _normalizeVersions(Versions? versions) =>
versions?.min == null && versions?.max == null ? null : versions;
static Availability _mergeAvailability(Availability? x, Availability y) =>
x == null ? y : (x == y ? x : Availability.some);
List<PlatformAvailability> get _platforms => [ios, macos].nonNulls.toList();
String? get dartDoc {
if (availability != Availability.some) return null;
final platforms = _platforms;
if (platforms.isEmpty) return null;
return platforms.map((platform) => platform.dartDoc).join('\n');
}
String? runtimeCheck(String checkOsVersion, String apiName) {
final platforms = _platforms;
if (platforms.isEmpty) return null;
final args = platforms.map((platform) => platform.checkArgs).join(', ');
return "$checkOsVersion('$apiName', $args);";
}
@override
String toString() =>
'''Availability {
alwaysDeprecated: $alwaysDeprecated
alwaysUnavailable: $alwaysUnavailable
ios: $ios
macos: $macos
}''';
}
class PlatformAvailability {
String? name;
Version? introduced;
Version? deprecated;
Version? obsoleted;
bool unavailable;
PlatformAvailability({
this.name,
this.introduced,
this.deprecated,
this.obsoleted,
this.unavailable = false,
});
@visibleForTesting
Version? get deprecatedOrObsoleted {
if (deprecated == null || obsoleted == null) {
return deprecated ?? obsoleted;
}
return deprecated! < obsoleted! ? deprecated : obsoleted;
}
@visibleForTesting
Availability getAvailability(Versions version) {
if (unavailable) {
return Availability.none;
}
// Note: _greaterThan treats null as Version(infinity). For lower bound
// versions, null should be Version(0).
final confMin = version.min ?? Version(0, 0, 0);
final confMax = version.max;
final apiMin = introduced ?? Version(0, 0, 0);
final apiMax = deprecatedOrObsoleted;
if (_lessThanOrEqual(apiMin, confMin) && _greaterThan(apiMax, confMax)) {
return Availability.all;
}
if (_lessThanOrEqual(apiMin, confMax) && _greaterThan(apiMax, confMin)) {
return Availability.some;
}
return Availability.none;
}
static bool _lessThanOrEqual(Version? x, Version? y) => !_greaterThan(x, y);
static bool _greaterThan(Version? x, Version? y) {
if (x == null) return true;
if (y == null) return false;
return x > y;
}
String get dartDoc {
final s = StringBuffer();
s.write('$name: ');
if (unavailable) {
s.write('unavailable');
} else {
s.write(
[
if (introduced != null) 'introduced $introduced',
if (deprecated != null) 'deprecated $deprecated',
if (obsoleted != null) 'obsoleted $obsoleted',
].join(', '),
);
}
return s.toString();
}
String get checkArgs => '$name: ($unavailable, ${_toRecord(introduced)})';
String _toRecord(Version? v) =>
v == null ? 'null' : '(${v.major}, ${v.minor}, ${v.patch})';
@override
String toString() =>
'introduced: $introduced, deprecated: $deprecated, '
'obsoleted: $obsoleted, unavailable: $unavailable';
}