OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Poor-man's representation of a Closure type. | 5 /// Poor-man's representation of a Closure type. |
6 /// See https://developers.google.com/closure/compiler/docs/js-for-compiler | 6 /// See https://developers.google.com/closure/compiler/docs/js-for-compiler |
7 /// | 7 /// |
8 /// The goal here is not to completely support Closure's type system, but to | 8 /// The goal here is not to completely support Closure's type system, but to |
9 /// be able to generate just the types needed for DDC's JS output. | 9 /// be able to generate just the types needed for DDC's JS output. |
10 /// | 10 /// |
11 /// TODO(ochafik): Consider convergence with TypeScript, which has no nullabilit
y-awareness | 11 /// TODO(ochafik): Consider convergence with TypeScript, which has no nullabilit
y-awareness |
12 /// (see http://www.typescriptlang.org/Handbook). | 12 /// (see http://www.typescriptlang.org/Handbook). |
13 class ClosureType { | 13 class ClosureType { |
14 static const ClosureType _ALL = const ClosureType._("*"); | 14 static const ClosureType _ALL = const ClosureType._("*"); |
15 static const ClosureType _UNKNOWN = const ClosureType._("?"); | 15 static const ClosureType _UNKNOWN = const ClosureType._("?"); |
16 | 16 |
17 final String _representation; | 17 final String _representation; |
18 final bool isNullable; | 18 final bool isNullable; |
19 | 19 |
20 const ClosureType._(this._representation, {this.isNullable: true}); | 20 const ClosureType._(this._representation, {this.isNullable: true}); |
21 | 21 |
22 bool get isAll => _representation == "*"; | 22 bool get isAll => _representation == "*"; |
23 bool get isUnknown => _representation == "?"; | 23 bool get isUnknown => _representation == "?"; |
24 | 24 |
25 @override toString() => _representation; | 25 @override |
| 26 toString() => _representation; |
26 | 27 |
27 factory ClosureType.all() => _ALL; | 28 factory ClosureType.all() => _ALL; |
28 factory ClosureType.unknown() => _UNKNOWN; | 29 factory ClosureType.unknown() => _UNKNOWN; |
29 | 30 |
30 factory ClosureType.record(Map<String, ClosureType> fieldTypes) { | 31 factory ClosureType.record(Map<String, ClosureType> fieldTypes) { |
31 var entries = <String>[]; | 32 var entries = <String>[]; |
32 fieldTypes.forEach((n, t) => entries.add('$n: $t')); | 33 fieldTypes.forEach((n, t) => entries.add('$n: $t')); |
33 return new ClosureType._('{${entries.join(', ')}}'); | 34 return new ClosureType._('{${entries.join(', ')}}'); |
34 } | 35 } |
35 factory ClosureType.function( | 36 factory ClosureType.function( |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 : "!$this", | 78 : "!$this", |
78 isNullable: false); | 79 isNullable: false); |
79 | 80 |
80 /// TODO(ochafik): See which optimizations make sense here (it could be that `
(*|undefined)` | 81 /// TODO(ochafik): See which optimizations make sense here (it could be that `
(*|undefined)` |
81 /// cannot be optimized to `*` when used to model optional record fields). | 82 /// cannot be optimized to `*` when used to model optional record fields). |
82 ClosureType or(ClosureType other) => new ClosureType._("($this|$other)", | 83 ClosureType or(ClosureType other) => new ClosureType._("($this|$other)", |
83 isNullable: isNullable || other.isNullable); | 84 isNullable: isNullable || other.isNullable); |
84 | 85 |
85 ClosureType orUndefined() => or(new ClosureType.undefined()); | 86 ClosureType orUndefined() => or(new ClosureType.undefined()); |
86 } | 87 } |
OLD | NEW |