| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 dart2js.js_backend.patch_resolver; | 5 library dart2js.js_backend.patch_resolver; |
| 6 | 6 |
| 7 import '../common/tasks.dart' show | 7 import '../common/tasks.dart' show |
| 8 CompilerTask; | 8 CompilerTask; |
| 9 import '../compiler.dart' show | 9 import '../compiler.dart' show |
| 10 Compiler; | 10 Compiler; |
| 11 import '../dart_types.dart'; | 11 import '../dart_types.dart'; |
| 12 import '../diagnostics/diagnostic_listener.dart' show |
| 13 DiagnosticMessage; |
| 12 import '../diagnostics/invariant.dart' show | 14 import '../diagnostics/invariant.dart' show |
| 13 invariant; | 15 invariant; |
| 14 import '../diagnostics/messages.dart' show MessageKind; | 16 import '../diagnostics/messages.dart' show |
| 17 MessageKind; |
| 15 import '../elements/elements.dart'; | 18 import '../elements/elements.dart'; |
| 16 import '../elements/modelx.dart'; | 19 import '../elements/modelx.dart'; |
| 17 import '../tree/tree.dart'; | 20 import '../tree/tree.dart'; |
| 18 | 21 |
| 19 class PatchResolverTask extends CompilerTask { | 22 class PatchResolverTask extends CompilerTask { |
| 20 PatchResolverTask(Compiler compiler) : super(compiler); | 23 PatchResolverTask(Compiler compiler) : super(compiler); |
| 21 | 24 |
| 22 String get name => 'JavaScript patch resolver'; | 25 String get name => 'JavaScript patch resolver'; |
| 23 | 26 |
| 24 FunctionElement resolveExternalFunction(FunctionElementX element) { | 27 FunctionElement resolveExternalFunction(FunctionElementX element) { |
| 25 if (element.isPatched) { | 28 if (element.isPatched) { |
| 26 FunctionElementX patch = element.patch; | 29 FunctionElementX patch = element.patch; |
| 27 compiler.withCurrentElement(patch, () { | 30 compiler.withCurrentElement(patch, () { |
| 28 patch.parseNode(compiler); | 31 patch.parseNode(compiler); |
| 29 patch.computeType(compiler); | 32 patch.computeType(compiler); |
| 30 }); | 33 }); |
| 31 checkMatchingPatchSignatures(element, patch); | 34 checkMatchingPatchSignatures(element, patch); |
| 32 element = patch; | 35 element = patch; |
| 33 } else { | 36 } else { |
| 34 compiler.reportError( | 37 compiler.reportErrorMessage( |
| 35 element, MessageKind.PATCH_EXTERNAL_WITHOUT_IMPLEMENTATION); | 38 element, MessageKind.PATCH_EXTERNAL_WITHOUT_IMPLEMENTATION); |
| 36 } | 39 } |
| 37 return element; | 40 return element; |
| 38 } | 41 } |
| 39 | 42 |
| 40 void checkMatchingPatchParameters(FunctionElement origin, | 43 void checkMatchingPatchParameters(FunctionElement origin, |
| 41 List<Element> originParameters, | 44 List<Element> originParameters, |
| 42 List<Element> patchParameters) { | 45 List<Element> patchParameters) { |
| 43 | 46 |
| 44 assert(originParameters.length == patchParameters.length); | 47 assert(originParameters.length == patchParameters.length); |
| 45 for (int index = 0; index < originParameters.length; index++) { | 48 for (int index = 0; index < originParameters.length; index++) { |
| 46 ParameterElementX originParameter = originParameters[index]; | 49 ParameterElementX originParameter = originParameters[index]; |
| 47 ParameterElementX patchParameter = patchParameters[index]; | 50 ParameterElementX patchParameter = patchParameters[index]; |
| 48 // TODO(johnniwinther): Remove the conditional patching when we never | 51 // TODO(johnniwinther): Remove the conditional patching when we never |
| 49 // resolve the same method twice. | 52 // resolve the same method twice. |
| 50 if (!originParameter.isPatched) { | 53 if (!originParameter.isPatched) { |
| 51 originParameter.applyPatch(patchParameter); | 54 originParameter.applyPatch(patchParameter); |
| 52 } else { | 55 } else { |
| 53 assert(invariant(origin, originParameter.patch == patchParameter, | 56 assert(invariant(origin, originParameter.patch == patchParameter, |
| 54 message: "Inconsistent repatch of $originParameter.")); | 57 message: "Inconsistent repatch of $originParameter.")); |
| 55 } | 58 } |
| 56 DartType originParameterType = originParameter.computeType(compiler); | 59 DartType originParameterType = originParameter.computeType(compiler); |
| 57 DartType patchParameterType = patchParameter.computeType(compiler); | 60 DartType patchParameterType = patchParameter.computeType(compiler); |
| 58 if (originParameterType != patchParameterType) { | 61 if (originParameterType != patchParameterType) { |
| 59 compiler.reportError( | 62 compiler.reportError( |
| 60 originParameter.parseNode(compiler), | 63 compiler.createMessage( |
| 61 MessageKind.PATCH_PARAMETER_TYPE_MISMATCH, | 64 originParameter.parseNode(compiler), |
| 62 {'methodName': origin.name, | 65 MessageKind.PATCH_PARAMETER_TYPE_MISMATCH, |
| 63 'parameterName': originParameter.name, | 66 {'methodName': origin.name, |
| 64 'originParameterType': originParameterType, | 67 'parameterName': originParameter.name, |
| 65 'patchParameterType': patchParameterType}); | 68 'originParameterType': originParameterType, |
| 66 compiler.reportInfo(patchParameter, | 69 'patchParameterType': patchParameterType}), |
| 67 MessageKind.PATCH_POINT_TO_PARAMETER, | 70 <DiagnosticMessage>[ |
| 68 {'parameterName': patchParameter.name}); | 71 compiler.createMessage( |
| 72 patchParameter, |
| 73 MessageKind.PATCH_POINT_TO_PARAMETER, |
| 74 {'parameterName': patchParameter.name}), |
| 75 ]); |
| 69 } else { | 76 } else { |
| 70 // Hack: Use unparser to test parameter equality. This only works | 77 // Hack: Use unparser to test parameter equality. This only works |
| 71 // because we are restricting patch uses and the approach cannot be used | 78 // because we are restricting patch uses and the approach cannot be used |
| 72 // elsewhere. | 79 // elsewhere. |
| 73 | 80 |
| 74 // The node contains the type, so there is a potential overlap. | 81 // The node contains the type, so there is a potential overlap. |
| 75 // Therefore we only check the text if the types are identical. | 82 // Therefore we only check the text if the types are identical. |
| 76 String originParameterText = | 83 String originParameterText = |
| 77 originParameter.parseNode(compiler).toString(); | 84 originParameter.parseNode(compiler).toString(); |
| 78 String patchParameterText = | 85 String patchParameterText = |
| 79 patchParameter.parseNode(compiler).toString(); | 86 patchParameter.parseNode(compiler).toString(); |
| 80 if (originParameterText != patchParameterText | 87 if (originParameterText != patchParameterText |
| 81 // We special case the list constructor because of the | 88 // We special case the list constructor because of the |
| 82 // optional parameter. | 89 // optional parameter. |
| 83 && origin != compiler.unnamedListConstructor) { | 90 && origin != compiler.unnamedListConstructor) { |
| 84 compiler.reportError( | 91 compiler.reportError( |
| 85 originParameter.parseNode(compiler), | 92 compiler.createMessage( |
| 86 MessageKind.PATCH_PARAMETER_MISMATCH, | 93 originParameter.parseNode(compiler), |
| 87 {'methodName': origin.name, | 94 MessageKind.PATCH_PARAMETER_MISMATCH, |
| 88 'originParameter': originParameterText, | 95 {'methodName': origin.name, |
| 89 'patchParameter': patchParameterText}); | 96 'originParameter': originParameterText, |
| 90 compiler.reportInfo(patchParameter, | 97 'patchParameter': patchParameterText}), |
| 91 MessageKind.PATCH_POINT_TO_PARAMETER, | 98 <DiagnosticMessage>[ |
| 92 {'parameterName': patchParameter.name}); | 99 compiler.createMessage( |
| 100 patchParameter, |
| 101 MessageKind.PATCH_POINT_TO_PARAMETER, |
| 102 {'parameterName': patchParameter.name}), |
| 103 ]); |
| 104 |
| 93 } | 105 } |
| 94 } | 106 } |
| 95 } | 107 } |
| 96 } | 108 } |
| 97 | 109 |
| 98 void checkMatchingPatchSignatures(FunctionElement origin, | 110 void checkMatchingPatchSignatures(FunctionElement origin, |
| 99 FunctionElement patch) { | 111 FunctionElement patch) { |
| 100 // TODO(johnniwinther): Show both origin and patch locations on errors. | 112 // TODO(johnniwinther): Show both origin and patch locations on errors. |
| 101 FunctionSignature originSignature = origin.functionSignature; | 113 FunctionSignature originSignature = origin.functionSignature; |
| 102 FunctionExpression patchTree = patch.node; | 114 FunctionExpression patchTree = patch.node; |
| 103 FunctionSignature patchSignature = patch.functionSignature; | 115 FunctionSignature patchSignature = patch.functionSignature; |
| 104 | 116 |
| 105 if (originSignature.type.returnType != patchSignature.type.returnType) { | 117 if (originSignature.type.returnType != patchSignature.type.returnType) { |
| 106 compiler.withCurrentElement(patch, () { | 118 compiler.withCurrentElement(patch, () { |
| 107 Node errorNode = | 119 Node errorNode = |
| 108 patchTree.returnType != null ? patchTree.returnType : patchTree; | 120 patchTree.returnType != null ? patchTree.returnType : patchTree; |
| 109 compiler.reportError( | 121 compiler.reportErrorMessage( |
| 110 errorNode, MessageKind.PATCH_RETURN_TYPE_MISMATCH, | 122 errorNode, MessageKind.PATCH_RETURN_TYPE_MISMATCH, |
| 111 {'methodName': origin.name, | 123 {'methodName': origin.name, |
| 112 'originReturnType': originSignature.type.returnType, | 124 'originReturnType': originSignature.type.returnType, |
| 113 'patchReturnType': patchSignature.type.returnType}); | 125 'patchReturnType': patchSignature.type.returnType}); |
| 114 }); | 126 }); |
| 115 } | 127 } |
| 116 if (originSignature.requiredParameterCount != | 128 if (originSignature.requiredParameterCount != |
| 117 patchSignature.requiredParameterCount) { | 129 patchSignature.requiredParameterCount) { |
| 118 compiler.withCurrentElement(patch, () { | 130 compiler.withCurrentElement(patch, () { |
| 119 compiler.reportError( | 131 compiler.reportErrorMessage( |
| 120 patchTree, | 132 patchTree, |
| 121 MessageKind.PATCH_REQUIRED_PARAMETER_COUNT_MISMATCH, | 133 MessageKind.PATCH_REQUIRED_PARAMETER_COUNT_MISMATCH, |
| 122 {'methodName': origin.name, | 134 {'methodName': origin.name, |
| 123 'originParameterCount': originSignature.requiredParameterCount, | 135 'originParameterCount': originSignature.requiredParameterCount, |
| 124 'patchParameterCount': patchSignature.requiredParameterCount}); | 136 'patchParameterCount': patchSignature.requiredParameterCount}); |
| 125 }); | 137 }); |
| 126 } else { | 138 } else { |
| 127 checkMatchingPatchParameters(origin, | 139 checkMatchingPatchParameters(origin, |
| 128 originSignature.requiredParameters, | 140 originSignature.requiredParameters, |
| 129 patchSignature.requiredParameters); | 141 patchSignature.requiredParameters); |
| 130 } | 142 } |
| 131 if (originSignature.optionalParameterCount != 0 && | 143 if (originSignature.optionalParameterCount != 0 && |
| 132 patchSignature.optionalParameterCount != 0) { | 144 patchSignature.optionalParameterCount != 0) { |
| 133 if (originSignature.optionalParametersAreNamed != | 145 if (originSignature.optionalParametersAreNamed != |
| 134 patchSignature.optionalParametersAreNamed) { | 146 patchSignature.optionalParametersAreNamed) { |
| 135 compiler.withCurrentElement(patch, () { | 147 compiler.withCurrentElement(patch, () { |
| 136 compiler.reportError( | 148 compiler.reportErrorMessage( |
| 137 patchTree, | 149 patchTree, |
| 138 MessageKind.PATCH_OPTIONAL_PARAMETER_NAMED_MISMATCH, | 150 MessageKind.PATCH_OPTIONAL_PARAMETER_NAMED_MISMATCH, |
| 139 {'methodName': origin.name}); | 151 {'methodName': origin.name}); |
| 140 }); | 152 }); |
| 141 } | 153 } |
| 142 } | 154 } |
| 143 if (originSignature.optionalParameterCount != | 155 if (originSignature.optionalParameterCount != |
| 144 patchSignature.optionalParameterCount) { | 156 patchSignature.optionalParameterCount) { |
| 145 compiler.withCurrentElement(patch, () { | 157 compiler.withCurrentElement(patch, () { |
| 146 compiler.reportError( | 158 compiler.reportErrorMessage( |
| 147 patchTree, | 159 patchTree, |
| 148 MessageKind.PATCH_OPTIONAL_PARAMETER_COUNT_MISMATCH, | 160 MessageKind.PATCH_OPTIONAL_PARAMETER_COUNT_MISMATCH, |
| 149 {'methodName': origin.name, | 161 {'methodName': origin.name, |
| 150 'originParameterCount': originSignature.optionalParameterCount, | 162 'originParameterCount': originSignature.optionalParameterCount, |
| 151 'patchParameterCount': patchSignature.optionalParameterCount}); | 163 'patchParameterCount': patchSignature.optionalParameterCount}); |
| 152 }); | 164 }); |
| 153 } else { | 165 } else { |
| 154 checkMatchingPatchParameters(origin, | 166 checkMatchingPatchParameters(origin, |
| 155 originSignature.optionalParameters, | 167 originSignature.optionalParameters, |
| 156 patchSignature.optionalParameters); | 168 patchSignature.optionalParameters); |
| 157 } | 169 } |
| 158 } | 170 } |
| 159 | 171 |
| 160 } | 172 } |
| OLD | NEW |