| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library closureToClassMapper; | 5 library closureToClassMapper; |
| 6 | 6 |
| 7 import 'common/names.dart' show Identifiers; | 7 import 'common/names.dart' show Identifiers; |
| 8 import 'common/resolution.dart' show ParsingContext, Resolution; | 8 import 'common/resolution.dart' show ParsingContext, Resolution; |
| 9 import 'common/tasks.dart' show CompilerTask; | 9 import 'common/tasks.dart' show CompilerTask; |
| 10 import 'common.dart'; | 10 import 'common.dart'; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 Compiler compiler; | 35 Compiler compiler; |
| 36 ClosureTask(Compiler compiler) | 36 ClosureTask(Compiler compiler) |
| 37 : compiler = compiler, | 37 : compiler = compiler, |
| 38 super(compiler.measurer); | 38 super(compiler.measurer); |
| 39 | 39 |
| 40 String get name => "Closure Simplifier"; | 40 String get name => "Closure Simplifier"; |
| 41 | 41 |
| 42 DiagnosticReporter get reporter => compiler.reporter; | 42 DiagnosticReporter get reporter => compiler.reporter; |
| 43 | 43 |
| 44 ClosureClassMap getMemberMap(MemberElement member) { | 44 ClosureClassMap getMemberMap(MemberElement member) { |
| 45 return getClosureToClassMapping(member.resolvedAst); | 45 return getClosureToClassMapping(member); |
| 46 } | 46 } |
| 47 | 47 |
| 48 ClosureClassMap getLocalFunctionMap(LocalFunctionElement localFunction) { | 48 ClosureClassMap getLocalFunctionMap(LocalFunctionElement localFunction) { |
| 49 return getClosureToClassMapping(localFunction.resolvedAst); | 49 return getClosureToClassMapping(localFunction); |
| 50 } | 50 } |
| 51 | 51 |
| 52 /// Returns the [ClosureClassMap] computed for [resolvedAst]. | 52 /// Returns the [ClosureClassMap] computed for [resolvedAst]. |
| 53 ClosureClassMap getClosureToClassMapping(ResolvedAst resolvedAst) { | 53 ClosureClassMap getClosureToClassMapping(Element element) { |
| 54 return measure(() { | 54 return measure(() { |
| 55 Element element = resolvedAst.element; | |
| 56 if (element.isGenerativeConstructorBody) { | 55 if (element.isGenerativeConstructorBody) { |
| 57 ConstructorBodyElement constructorBody = element; | 56 ConstructorBodyElement constructorBody = element; |
| 58 element = constructorBody.constructor; | 57 element = constructorBody.constructor; |
| 59 } | 58 } |
| 60 ClosureClassMap closureClassMap = _closureMappingCache[element]; | 59 ClosureClassMap closureClassMap = _closureMappingCache[element]; |
| 61 assert( | 60 assert(closureClassMap != null, |
| 62 closureClassMap != null, | 61 failedAt(element, "No ClosureClassMap computed for ${element}.")); |
| 63 failedAt(resolvedAst.element, | |
| 64 "No ClosureClassMap computed for ${element}.")); | |
| 65 return closureClassMap; | 62 return closureClassMap; |
| 66 }); | 63 }); |
| 67 } | 64 } |
| 68 | 65 |
| 69 /// Create [ClosureClassMap]s for all live members. | 66 /// Create [ClosureClassMap]s for all live members. |
| 70 void createClosureClasses(ClosedWorldRefiner closedWorldRefiner) { | 67 void createClosureClasses(ClosedWorldRefiner closedWorldRefiner) { |
| 71 compiler.enqueuer.resolution.processedEntities | 68 compiler.enqueuer.resolution.processedEntities |
| 72 .forEach((MemberElement element) { | 69 .forEach((MemberElement element) { |
| 73 ResolvedAst resolvedAst = element.resolvedAst; | 70 ResolvedAst resolvedAst = element.resolvedAst; |
| 74 if (element.isAbstract) return; | 71 if (element.isAbstract) return; |
| 75 if (element.isField && | 72 if (element.isField && |
| 76 !element.isInstanceMember && | 73 !element.isInstanceMember && |
| 77 resolvedAst.body == null) { | 74 resolvedAst.body == null) { |
| 78 // Skip top-level/static fields without an initializer. | 75 // Skip top-level/static fields without an initializer. |
| 79 return; | 76 return; |
| 80 } | 77 } |
| 81 computeClosureToClassMapping(resolvedAst, closedWorldRefiner); | 78 computeClosureToClassMapping(element, closedWorldRefiner); |
| 82 }); | 79 }); |
| 83 } | 80 } |
| 84 | 81 |
| 85 ClosureClassMap computeClosureToClassMapping( | 82 ClosureClassMap computeClosureToClassMapping( |
| 86 ResolvedAst resolvedAst, ClosedWorldRefiner closedWorldRefiner) { | 83 AstElement element, ClosedWorldRefiner closedWorldRefiner) { |
| 87 return measure(() { | 84 return measure(() { |
| 88 Element element = resolvedAst.element; | |
| 89 ClosureClassMap cached = _closureMappingCache[element]; | 85 ClosureClassMap cached = _closureMappingCache[element]; |
| 90 if (cached != null) return cached; | 86 if (cached != null) return cached; |
| 91 if (resolvedAst.kind != ResolvedAstKind.PARSED) { | 87 if (element.resolvedAst.kind != ResolvedAstKind.PARSED) { |
| 92 return _closureMappingCache[element] = | 88 return _closureMappingCache[element] = |
| 93 new ClosureClassMap(null, null, null, new ThisLocal(element)); | 89 new ClosureClassMap(null, null, null, new ThisLocal(element)); |
| 94 } | 90 } |
| 95 return reporter.withCurrentElement(element.implementation, () { | 91 return reporter.withCurrentElement(element.implementation, () { |
| 96 Node node = resolvedAst.node; | 92 Node node = element.resolvedAst.node; |
| 97 TreeElements elements = resolvedAst.elements; | 93 TreeElements elements = element.resolvedAst.elements; |
| 98 | 94 |
| 99 ClosureTranslator translator = new ClosureTranslator( | 95 ClosureTranslator translator = new ClosureTranslator( |
| 100 compiler, closedWorldRefiner, elements, _closureMappingCache); | 96 compiler, closedWorldRefiner, elements, _closureMappingCache); |
| 101 | 97 |
| 102 // The translator will store the computed closure-mappings inside the | 98 // The translator will store the computed closure-mappings inside the |
| 103 // cache. One for given node and one for each nested closure. | 99 // cache. One for given node and one for each nested closure. |
| 104 if (node is FunctionExpression) { | 100 if (node is FunctionExpression) { |
| 105 translator.translateFunction(element, node); | 101 translator.translateFunction(element, node); |
| 106 } else if (element.isSynthesized) { | 102 } else if (element.isSynthesized) { |
| 107 reporter.internalError( | 103 reporter.internalError( |
| 108 element, "Unexpected synthesized element: $element"); | 104 element, "Unexpected synthesized element: $element"); |
| 109 _closureMappingCache[element] = | 105 _closureMappingCache[element] = |
| 110 new ClosureClassMap(null, null, null, new ThisLocal(element)); | 106 new ClosureClassMap(null, null, null, new ThisLocal(element)); |
| 111 } else { | 107 } else { |
| 112 assert(element.isField, | 108 assert(element.isField, |
| 113 failedAt(element, "Expected $element to be a field.")); | 109 failedAt(element, "Expected $element to be a field.")); |
| 114 Node initializer = resolvedAst.body; | 110 Node initializer = element.resolvedAst.body; |
| 115 if (initializer != null) { | 111 if (initializer != null) { |
| 116 // The lazy initializer of a static. | 112 // The lazy initializer of a static. |
| 117 translator.translateLazyInitializer(element, node, initializer); | 113 translator.translateLazyInitializer(element, node, initializer); |
| 118 } else { | 114 } else { |
| 119 assert( | 115 assert( |
| 120 element.isInstanceMember, | 116 element.isInstanceMember, |
| 121 failedAt( | 117 failedAt( |
| 122 element, | 118 element, |
| 123 "Expected $element (${element.runtimeType}) " | 119 "Expected $element (${element.runtimeType}) " |
| 124 "to be an instance field.")); | 120 "to be an instance field.")); |
| (...skipping 1007 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1132 ThisLocal thisElement = null; | 1128 ThisLocal thisElement = null; |
| 1133 if (element.isInstanceMember || element.isGenerativeConstructor) { | 1129 if (element.isInstanceMember || element.isGenerativeConstructor) { |
| 1134 thisElement = new ThisLocal(element); | 1130 thisElement = new ThisLocal(element); |
| 1135 } | 1131 } |
| 1136 closureData = new ClosureClassMap(null, null, null, thisElement); | 1132 closureData = new ClosureClassMap(null, null, null, thisElement); |
| 1137 if (element is MethodElement) { | 1133 if (element is MethodElement) { |
| 1138 needsRti = compiler.options.enableTypeAssertions || | 1134 needsRti = compiler.options.enableTypeAssertions || |
| 1139 compiler.backend.rtiNeed.methodNeedsRti(element); | 1135 compiler.backend.rtiNeed.methodNeedsRti(element); |
| 1140 } | 1136 } |
| 1141 } | 1137 } |
| 1138 closureMappingCache[element] = closureData; |
| 1142 closureMappingCache[element.declaration] = closureData; | 1139 closureMappingCache[element.declaration] = closureData; |
| 1143 if (closureData.callElement != null) { | 1140 if (closureData.callElement != null) { |
| 1144 closureMappingCache[closureData.callElement] = closureData; | 1141 closureMappingCache[closureData.callElement] = closureData; |
| 1145 } | 1142 } |
| 1146 | 1143 |
| 1147 inNewScope(node, () { | 1144 inNewScope(node, () { |
| 1148 // If the method needs RTI, or checked mode is set, we need to | 1145 // If the method needs RTI, or checked mode is set, we need to |
| 1149 // escape the potential type variables used in that closure. | 1146 // escape the potential type variables used in that closure. |
| 1150 if (needsRti) { | 1147 if (needsRti) { |
| 1151 analyzeTypeVariables(element.type); | 1148 analyzeTypeVariables(element.type); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1241 /// | 1238 /// |
| 1242 /// Move the below classes to a JS model eventually. | 1239 /// Move the below classes to a JS model eventually. |
| 1243 /// | 1240 /// |
| 1244 abstract class JSEntity implements Entity { | 1241 abstract class JSEntity implements Entity { |
| 1245 Entity get declaredEntity; | 1242 Entity get declaredEntity; |
| 1246 } | 1243 } |
| 1247 | 1244 |
| 1248 abstract class PrivatelyNamedJSEntity implements JSEntity { | 1245 abstract class PrivatelyNamedJSEntity implements JSEntity { |
| 1249 Entity get rootOfScope; | 1246 Entity get rootOfScope; |
| 1250 } | 1247 } |
| OLD | NEW |