| 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 backend_ast_emitter; | 5 library backend_ast_emitter; |
| 6 | 6 |
| 7 import 'tree_ir_nodes.dart' as tree; | 7 import 'tree_ir_nodes.dart' as tree; |
| 8 import 'backend_ast_nodes.dart'; | 8 import 'backend_ast_nodes.dart'; |
| 9 import '../constants/expressions.dart'; | 9 import '../constants/expressions.dart'; |
| 10 import '../constants/values.dart'; | 10 import '../constants/values.dart'; |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 void removeTrailingReturn() { | 147 void removeTrailingReturn() { |
| 148 if (statementBuffer.isEmpty) return; | 148 if (statementBuffer.isEmpty) return; |
| 149 if (statementBuffer.last is! Return) return; | 149 if (statementBuffer.last is! Return) return; |
| 150 Return ret = statementBuffer.last; | 150 Return ret = statementBuffer.last; |
| 151 Expression expr = ret.expression; | 151 Expression expr = ret.expression; |
| 152 if (expr is Literal && expr.value.isNull) { | 152 if (expr is Literal && expr.value.isNull) { |
| 153 statementBuffer.removeLast(); | 153 statementBuffer.removeLast(); |
| 154 } | 154 } |
| 155 } | 155 } |
| 156 | 156 |
| 157 Parameter emitParameterFromElement(FormalElement element, [String name]) { | 157 /// TODO(johnniwinther): Remove this when issue 21283 has been resolved. |
| 158 if (name == null) { | 158 int pseudoNameCounter = 0; |
| 159 |
| 160 Parameter emitParameter(DartType type, |
| 161 {String name, |
| 162 Element element, |
| 163 ConstantExpression defaultValue}) { |
| 164 if (name == null && element != null) { |
| 159 name = element.name; | 165 name = element.name; |
| 160 } | 166 } |
| 161 if (element.functionSignature != null) { | 167 if (name == null) { |
| 162 FunctionSignature signature = element.functionSignature; | 168 name = '_${pseudoNameCounter++}'; |
| 163 TypeAnnotation returnType = emitOptionalType(signature.type.returnType); | 169 } |
| 164 Parameters innerParameters = emitParameters(signature); | 170 Parameter parameter; |
| 165 return new Parameter.function(name, returnType, innerParameters) | 171 if (type.isFunctionType) { |
| 166 ..element = element; | 172 FunctionType functionType = type; |
| 173 TypeAnnotation returnType = emitOptionalType(functionType.returnType); |
| 174 Parameters innerParameters = emitParametersFromType(functionType); |
| 175 parameter = new Parameter.function(name, returnType, innerParameters); |
| 167 } else { | 176 } else { |
| 168 TypeAnnotation type = emitOptionalType(element.type); | 177 TypeAnnotation typeAnnotation = emitOptionalType(type); |
| 169 return new Parameter(name, type:type) | 178 parameter = new Parameter(name, type: typeAnnotation); |
| 170 ..element = element; | 179 } |
| 180 parameter.element = element; |
| 181 if (defaultValue != null && !defaultValue.value.isNull) { |
| 182 parameter.defaultValue = emitConstant(defaultValue); |
| 183 } |
| 184 return parameter; |
| 185 } |
| 186 |
| 187 Parameters emitParametersFromType(FunctionType functionType) { |
| 188 if (functionType.namedParameters.isEmpty) { |
| 189 return new Parameters( |
| 190 emitParameters(functionType.parameterTypes), |
| 191 emitParameters(functionType.optionalParameterTypes), |
| 192 false); |
| 193 } else { |
| 194 return new Parameters( |
| 195 emitParameters(functionType.parameterTypes), |
| 196 emitParameters(functionType.namedParameterTypes, |
| 197 names: functionType.namedParameters), |
| 198 true); |
| 171 } | 199 } |
| 172 } | 200 } |
| 173 | 201 |
| 174 Parameters emitParameters(FunctionSignature signature) { | 202 List<Parameter> emitParameters( |
| 175 return new Parameters( | 203 Iterable<DartType> parameterTypes, |
| 176 signature.requiredParameters.mapToList(emitParameterFromElement), | 204 {Iterable<String> names: const <String>[], |
| 177 signature.optionalParameters.mapToList(emitParameterFromElement), | 205 Iterable<ConstantExpression> defaultValues: const <ConstantExpression>[], |
| 178 signature.optionalParametersAreNamed); | 206 Iterable<Element> elements: const <Element>[]}) { |
| 207 Iterator<String> name = names.iterator; |
| 208 Iterator<ConstantExpression> defaultValue = defaultValues.iterator; |
| 209 Iterator<Element> element = elements.iterator; |
| 210 return parameterTypes.map((DartType type) { |
| 211 name.moveNext(); |
| 212 defaultValue.moveNext(); |
| 213 element.moveNext(); |
| 214 return emitParameter(type, |
| 215 name: name.current, |
| 216 defaultValue: defaultValue.current, |
| 217 element: element.current); |
| 218 }).toList(); |
| 179 } | 219 } |
| 180 | 220 |
| 181 /// Emits parameters that are not nested inside other parameters. | 221 /// Emits parameters that are not nested inside other parameters. |
| 182 /// Root parameters can have default values, while inner parameters cannot. | 222 /// Root parameters can have default values, while inner parameters cannot. |
| 183 Parameters emitRootParameters(tree.FunctionDefinition function) { | 223 Parameters emitRootParameters(tree.FunctionDefinition function) { |
| 184 FunctionSignature signature = function.element.functionSignature; | 224 FunctionType functionType = function.element.type; |
| 185 List<ConstantExpression> defaults = function.defaultParameterValues; | 225 List<Parameter> required = emitParameters( |
| 186 List<Parameter> required = | 226 functionType.parameterTypes, |
| 187 signature.requiredParameters.mapToList(emitParameterFromElement); | 227 elements: function.parameters.map((p) => p.element)); |
| 188 List<Parameter> optional = new List<Parameter>(defaults.length); | 228 bool optionalParametersAreNamed = !functionType.namedParameters.isEmpty; |
| 189 for (int i = 0; i < defaults.length; i++) { | 229 List<Parameter> optional = emitParameters( |
| 190 ParameterElement element = signature.orderedOptionalParameters[i]; | 230 optionalParametersAreNamed |
| 191 optional[i] = emitParameterFromElement(element); | 231 ? functionType.namedParameterTypes |
| 192 Expression constant = emitConstant(defaults[i]); | 232 : functionType.optionalParameterTypes, |
| 193 if (!isNullLiteral(constant)) { | 233 defaultValues: function.defaultParameterValues, |
| 194 optional[i].defaultValue = constant; | 234 elements: function.parameters.skip(required.length) |
| 195 } | 235 .map((p) => p.element)); |
| 196 } | 236 return new Parameters(required, optional, optionalParametersAreNamed); |
| 197 return new Parameters(required, optional, | |
| 198 signature.optionalParametersAreNamed); | |
| 199 } | 237 } |
| 200 | 238 |
| 201 /// True if the two expressions are a reference to the same variable. | 239 /// True if the two expressions are a reference to the same variable. |
| 202 bool isSameVariable(Receiver e1, Receiver e2) { | 240 bool isSameVariable(Receiver e1, Receiver e2) { |
| 203 return e1 is Identifier && | 241 return e1 is Identifier && |
| 204 e2 is Identifier && | 242 e2 is Identifier && |
| 205 e1.element is VariableElement && | 243 e1.element is VariableElement && |
| 206 e1.element == e2.element; | 244 e1.element == e2.element; |
| 207 } | 245 } |
| 208 | 246 |
| (...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 852 } | 890 } |
| 853 } | 891 } |
| 854 | 892 |
| 855 visitVariable(tree.Variable variable) { | 893 visitVariable(tree.Variable variable) { |
| 856 if (shadowedParameters.contains(variable)) { | 894 if (shadowedParameters.contains(variable)) { |
| 857 hasShadowedUse.add(variable); | 895 hasShadowedUse.add(variable); |
| 858 } | 896 } |
| 859 } | 897 } |
| 860 | 898 |
| 861 } | 899 } |
| OLD | NEW |