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