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 | 2 |
3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 import 'dart:collection' show HashMap, HashSet; | 6 import 'dart:collection' show HashMap, HashSet; |
7 import 'dart:math' show min, max; | 7 import 'dart:math' show min, max; |
8 | 8 |
9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; | 9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; |
10 import 'package:analyzer/dart/ast/ast.dart'; | 10 import 'package:analyzer/dart/ast/ast.dart'; |
(...skipping 1078 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1089 // Generate a class per section 13 of the spec. | 1089 // Generate a class per section 13 of the spec. |
1090 // TODO(vsm): Generate any accompanying metadata | 1090 // TODO(vsm): Generate any accompanying metadata |
1091 | 1091 |
1092 // Create constructor and initialize index | 1092 // Create constructor and initialize index |
1093 var constructor = new JS.Method(_propertyName('new'), | 1093 var constructor = new JS.Method(_propertyName('new'), |
1094 js.call('function(index) { this.index = index; }') as JS.Fun); | 1094 js.call('function(index) { this.index = index; }') as JS.Fun); |
1095 var fields = new List<FieldElement>.from( | 1095 var fields = new List<FieldElement>.from( |
1096 element.fields.where((f) => f.type == type)); | 1096 element.fields.where((f) => f.type == type)); |
1097 | 1097 |
1098 // Create toString() method | 1098 // Create toString() method |
1099 var properties = new List<JS.Property>(); | 1099 var nameProperties = new List<JS.Property>(fields.length); |
1100 for (var i = 0; i < fields.length; ++i) { | 1100 for (var i = 0; i < fields.length; ++i) { |
1101 properties.add(new JS.Property( | 1101 nameProperties[i] = new JS.Property( |
1102 js.number(i), js.string('${type.name}.${fields[i].name}'))); | 1102 js.number(i), js.string('${type.name}.${fields[i].name}')); |
1103 } | 1103 } |
1104 var nameMap = new JS.ObjectInitializer(properties, multiline: true); | 1104 var nameMap = new JS.ObjectInitializer(nameProperties, multiline: true); |
1105 var toStringF = new JS.Method(js.string('toString'), | 1105 var toStringF = new JS.Method(js.string('toString'), |
1106 js.call('function() { return #[this.index]; }', nameMap) as JS.Fun); | 1106 js.call('function() { return #[this.index]; }', nameMap) as JS.Fun); |
1107 | 1107 |
1108 // Create enum class | 1108 // Create enum class |
1109 var classExpr = new JS.ClassExpression(new JS.Identifier(type.name), | 1109 var classExpr = new JS.ClassExpression(new JS.Identifier(type.name), |
1110 _emitClassHeritage(element), [constructor, toStringF]); | 1110 _emitClassHeritage(element), [constructor, toStringF]); |
1111 var id = _emitTopLevelName(element); | 1111 var id = _emitTopLevelName(element); |
1112 var result = [ | 1112 var result = [ |
1113 js.statement('# = #', [id, classExpr]) | 1113 js.statement('# = #', [id, classExpr]) |
1114 ]; | 1114 ]; |
1115 | 1115 |
1116 // Create static fields for each enum value | 1116 // defineEnumValues internally depends on dart.constList which uses |
1117 for (var i = 0; i < fields.length; ++i) { | 1117 // _interceptors.JSArray. |
1118 result.add(js.statement('#.# = #.const(new #(#));', | |
1119 [id, fields[i].name, _runtimeModule, id, js.number(i)])); | |
1120 } | |
1121 | |
1122 // Create static values list | |
1123 var values = new JS.ArrayInitializer(new List<JS.Expression>.from( | |
1124 fields.map((f) => js.call('#.#', [id, f.name])))); | |
1125 | |
1126 // dart.constList helper internally depends on _interceptors.JSArray. | |
1127 _declareBeforeUse(_jsArray); | 1118 _declareBeforeUse(_jsArray); |
1128 | 1119 |
1129 result.add(js.statement('#.values = #.constList(#, #);', | 1120 // Create static fields for each enum value, and the "values" getter |
1130 [id, _runtimeModule, values, _emitType(type)])); | 1121 result.add(_callHelperStatement('defineEnumValues(#, #);', [ |
| 1122 id, |
| 1123 new JS.ArrayInitializer(fields.map((f) => _propertyName(f.name)).toList(), |
| 1124 multiline: true) |
| 1125 ])); |
1131 | 1126 |
1132 return _statement(result); | 1127 return _statement(result); |
1133 } | 1128 } |
1134 | 1129 |
1135 /// Wraps a possibly generic class in its type arguments. | 1130 /// Wraps a possibly generic class in its type arguments. |
1136 JS.Statement _defineClassTypeArguments(TypeDefiningElement element, | 1131 JS.Statement _defineClassTypeArguments(TypeDefiningElement element, |
1137 List<TypeParameterElement> formals, JS.Statement body) { | 1132 List<TypeParameterElement> formals, JS.Statement body) { |
1138 assert(formals.isNotEmpty); | 1133 assert(formals.isNotEmpty); |
1139 var genericCall = _callHelper('generic((#) => { #; #; return #; })', [ | 1134 var genericCall = _callHelper('generic((#) => { #; #; return #; })', [ |
1140 _emitTypeFormals(formals), | 1135 _emitTypeFormals(formals), |
(...skipping 4460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5601 var targetIdentifier = target as SimpleIdentifier; | 5596 var targetIdentifier = target as SimpleIdentifier; |
5602 | 5597 |
5603 if (targetIdentifier.staticElement is! PrefixElement) return false; | 5598 if (targetIdentifier.staticElement is! PrefixElement) return false; |
5604 var prefix = targetIdentifier.staticElement as PrefixElement; | 5599 var prefix = targetIdentifier.staticElement as PrefixElement; |
5605 | 5600 |
5606 // The library the prefix is referring to must come from a deferred import. | 5601 // The library the prefix is referring to must come from a deferred import. |
5607 var containingLibrary = (target.root as CompilationUnit).element.library; | 5602 var containingLibrary = (target.root as CompilationUnit).element.library; |
5608 var imports = containingLibrary.getImportsWithPrefix(prefix); | 5603 var imports = containingLibrary.getImportsWithPrefix(prefix); |
5609 return imports.length == 1 && imports[0].isDeferred; | 5604 return imports.length == 1 && imports[0].isDeferred; |
5610 } | 5605 } |
OLD | NEW |