|
1 | | -// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file |
| 1 | +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | 3 | // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
5 | | -// NOTE: THIS FILE IS GENERATED. DO NOT EDIT. |
6 | | -// |
7 | | -// Instead modify 'tools/experimental_features.yaml' and run |
8 | | -// 'dart pkg/front_end/tool/fasta.dart generate-experimental-flags' to update. |
9 | | - |
10 | | -import 'package:_fe_analyzer_shared/src/sdk/allowed_experiments.dart'; |
11 | 5 | import 'package:kernel/kernel.dart' show Version; |
12 | 6 |
|
13 | | -enum ExperimentalFlag { |
14 | | - constantUpdate2018, |
15 | | - controlFlowCollections, |
16 | | - extensionMethods, |
17 | | - nonNullable, |
18 | | - nonfunctionTypeAliases, |
19 | | - setLiterals, |
20 | | - spreadCollections, |
21 | | - tripleShift, |
22 | | - variance, |
23 | | -} |
| 7 | +part 'experimental_flags_generated.dart'; |
| 8 | + |
| 9 | +/// The set of experiments enabled for SDK and packages. |
| 10 | +/// |
| 11 | +/// This are derived from an `allowed_experiments.json` file whose default is |
| 12 | +/// located in `sdk_nnbd/lib/_internal/allowed_experiments.json`. |
| 13 | +class AllowedExperimentalFlags { |
| 14 | + /// The set of experiments that are enabled for all SDK libraries other than |
| 15 | + /// for those which are specified in [sdkLibraryExperiments]. |
| 16 | + final Set<ExperimentalFlag> sdkDefaultExperiments; |
| 17 | + |
| 18 | + /// Mapping from individual SDK libraries, e.g. 'core', to the set of |
| 19 | + /// experiments that are enabled for this library. |
| 20 | + final Map<String, Set<ExperimentalFlag>> sdkLibraryExperiments; |
24 | 21 |
|
25 | | -const Version enableConstantUpdate2018Version = const Version(2, 4); |
26 | | -const Version enableControlFlowCollectionsVersion = const Version(2, 2); |
27 | | -const Version enableExtensionMethodsVersion = const Version(2, 6); |
28 | | -const Version enableNonNullableVersion = const Version(2, 9); |
29 | | -const Version enableNonfunctionTypeAliasesVersion = const Version(2, 9); |
30 | | -const Version enableSetLiteralsVersion = const Version(2, 2); |
31 | | -const Version enableSpreadCollectionsVersion = const Version(2, 2); |
32 | | -const Version enableTripleShiftVersion = const Version(2, 9); |
33 | | -const Version enableVarianceVersion = const Version(2, 9); |
| 22 | + /// Mapping from package names, e.g. 'path', to the set of experiments that |
| 23 | + /// are enabled for all files of this package. |
| 24 | + final Map<String, Set<ExperimentalFlag>> packageExperiments; |
34 | 25 |
|
35 | | -ExperimentalFlag parseExperimentalFlag(String flag) { |
36 | | - switch (flag) { |
37 | | - case "constant-update-2018": |
38 | | - return ExperimentalFlag.constantUpdate2018; |
39 | | - case "control-flow-collections": |
40 | | - return ExperimentalFlag.controlFlowCollections; |
41 | | - case "extension-methods": |
42 | | - return ExperimentalFlag.extensionMethods; |
43 | | - case "non-nullable": |
44 | | - return ExperimentalFlag.nonNullable; |
45 | | - case "nonfunction-type-aliases": |
46 | | - return ExperimentalFlag.nonfunctionTypeAliases; |
47 | | - case "set-literals": |
48 | | - return ExperimentalFlag.setLiterals; |
49 | | - case "spread-collections": |
50 | | - return ExperimentalFlag.spreadCollections; |
51 | | - case "triple-shift": |
52 | | - return ExperimentalFlag.tripleShift; |
53 | | - case "variance": |
54 | | - return ExperimentalFlag.variance; |
| 26 | + const AllowedExperimentalFlags({ |
| 27 | + this.sdkDefaultExperiments: const {}, |
| 28 | + this.sdkLibraryExperiments: const {}, |
| 29 | + this.packageExperiments: const {}, |
| 30 | + }); |
| 31 | + |
| 32 | + /// Return the set of enabled experiments for the package with the [name], |
| 33 | + /// e.g. "path", possibly `null`. |
| 34 | + Set<ExperimentalFlag> forPackage(String name) { |
| 35 | + return packageExperiments[name]; |
55 | 36 | } |
56 | | - return null; |
57 | | -} |
58 | 37 |
|
59 | | -const Map<ExperimentalFlag, bool> defaultExperimentalFlags = { |
60 | | - ExperimentalFlag.constantUpdate2018: true, |
61 | | - ExperimentalFlag.controlFlowCollections: true, |
62 | | - ExperimentalFlag.extensionMethods: true, |
63 | | - ExperimentalFlag.nonNullable: false, |
64 | | - ExperimentalFlag.nonfunctionTypeAliases: false, |
65 | | - ExperimentalFlag.setLiterals: true, |
66 | | - ExperimentalFlag.spreadCollections: true, |
67 | | - ExperimentalFlag.tripleShift: false, |
68 | | - ExperimentalFlag.variance: false, |
69 | | -}; |
| 38 | + /// Return the set of enabled experiments for the library with the [name], |
| 39 | + /// e.g. "core". |
| 40 | + Set<ExperimentalFlag> forSdkLibrary(String name) { |
| 41 | + return sdkLibraryExperiments[name] ?? sdkDefaultExperiments; |
| 42 | + } |
| 43 | +} |
70 | 44 |
|
71 | | -const Map<ExperimentalFlag, bool> expiredExperimentalFlags = { |
72 | | - ExperimentalFlag.constantUpdate2018: true, |
73 | | - ExperimentalFlag.controlFlowCollections: true, |
74 | | - ExperimentalFlag.extensionMethods: false, |
75 | | - ExperimentalFlag.nonNullable: false, |
76 | | - ExperimentalFlag.nonfunctionTypeAliases: false, |
77 | | - ExperimentalFlag.setLiterals: true, |
78 | | - ExperimentalFlag.spreadCollections: true, |
79 | | - ExperimentalFlag.tripleShift: false, |
80 | | - ExperimentalFlag.variance: false, |
81 | | -}; |
| 45 | +/// Returns `true` if [flag] is enabled using global [experimentalFlags]. |
| 46 | +/// |
| 47 | +/// If [experimentalFlags] is `null` or doesn't contain [flag], the default |
| 48 | +/// value from [defaultExperimentalFlags] is returned. |
| 49 | +/// |
| 50 | +/// If [flag] is marked as expired in [expiredExperimentalFlags], the value from |
| 51 | +/// [defaultExperimentalFlags] is always returned. |
| 52 | +bool isExperimentEnabled(ExperimentalFlag flag, |
| 53 | + {Map<ExperimentalFlag, bool> experimentalFlags}) { |
| 54 | + assert(defaultExperimentalFlags.containsKey(flag), |
| 55 | + "No default value for $flag."); |
| 56 | + assert(expiredExperimentalFlags.containsKey(flag), |
| 57 | + "No expired value for $flag."); |
| 58 | + if (expiredExperimentalFlags[flag]) { |
| 59 | + return defaultExperimentalFlags[flag]; |
| 60 | + } |
| 61 | + bool enabled; |
| 62 | + if (experimentalFlags != null) { |
| 63 | + enabled = experimentalFlags[flag]; |
| 64 | + } |
| 65 | + enabled ??= defaultExperimentalFlags[flag]; |
| 66 | + return enabled; |
| 67 | +} |
82 | 68 |
|
83 | | -const AllowedExperiments allowedExperiments = |
84 | | - const AllowedExperiments(sdkDefaultExperiments: [ |
85 | | - "non-nullable", |
86 | | -], sdkLibraryExperiments: {}, packageExperiments: {}); |
| 69 | +/// Returns `true` if [flag] is enabled in the library with the [canonicalUri] |
| 70 | +/// either globally using [experimentalFlags] or per library using |
| 71 | +/// [allowedExperimentalFlags]. |
| 72 | +/// |
| 73 | +/// If [experimentalFlags] is `null` or doesn't contain [flag], the default |
| 74 | +/// value from [defaultExperimentalFlags] used as the global flag state. |
| 75 | +/// |
| 76 | +/// If [allowedExperimentalFlags] is `null` [defaultAllowedExperimentalFlags] is |
| 77 | +/// used for the per library flag state. |
| 78 | +/// |
| 79 | +/// If [flag] is marked as expired in [expiredExperimentalFlags], the value from |
| 80 | +/// [defaultExperimentalFlags] is always returned. |
| 81 | +/// |
| 82 | +/// The canonical uri, also known as the import uri, is the absolute uri that |
| 83 | +/// defines the identity of a library, for instance `dart:core`, `package:foo`, |
| 84 | +/// or `file:///path/dir/file.dart`. |
| 85 | +bool isExperimentEnabledInLibrary(ExperimentalFlag flag, Uri canonicalUri, |
| 86 | + {Map<ExperimentalFlag, bool> experimentalFlags, |
| 87 | + AllowedExperimentalFlags allowedExperimentalFlags}) { |
| 88 | + assert(defaultExperimentalFlags.containsKey(flag), |
| 89 | + "No default value for $flag."); |
| 90 | + assert(expiredExperimentalFlags.containsKey(flag), |
| 91 | + "No expired value for $flag."); |
| 92 | + if (expiredExperimentalFlags[flag]) { |
| 93 | + return defaultExperimentalFlags[flag]; |
| 94 | + } |
| 95 | + bool enabled; |
| 96 | + if (experimentalFlags != null) { |
| 97 | + enabled = experimentalFlags[flag]; |
| 98 | + } |
| 99 | + enabled ??= defaultExperimentalFlags[flag]; |
| 100 | + if (!enabled) { |
| 101 | + allowedExperimentalFlags ??= defaultAllowedExperimentalFlags; |
| 102 | + Set<ExperimentalFlag> allowedFlags; |
| 103 | + if (canonicalUri.scheme == 'dart') { |
| 104 | + allowedFlags = allowedExperimentalFlags.forSdkLibrary(canonicalUri.path); |
| 105 | + } else if (canonicalUri.scheme == 'package') { |
| 106 | + int index = canonicalUri.path.indexOf('/'); |
| 107 | + if (index >= 0) { |
| 108 | + String packageName = canonicalUri.path.substring(0, index); |
| 109 | + allowedFlags = allowedExperimentalFlags.forPackage(packageName); |
| 110 | + } |
| 111 | + } |
| 112 | + if (allowedFlags != null) { |
| 113 | + enabled = allowedFlags.contains(flag); |
| 114 | + } |
| 115 | + } |
| 116 | + return enabled; |
| 117 | +} |
0 commit comments