| 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 code_generator; | 5 library code_generator; |
| 6 | 6 |
| 7 import 'glue.dart'; | 7 import 'glue.dart'; |
| 8 | 8 |
| 9 import '../../tree_ir/tree_ir_nodes.dart' as tree_ir; | 9 import '../../tree_ir/tree_ir_nodes.dart' as tree_ir; |
| 10 import '../../js/js.dart' as js; | 10 import '../../js/js.dart' as js; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 | 44 |
| 45 js.Block body; | 45 js.Block body; |
| 46 | 46 |
| 47 /// Generates JavaScript code for the body of [function]. | 47 /// Generates JavaScript code for the body of [function]. |
| 48 /// The code will be in [body] and the parameters will be in [parameters]. | 48 /// The code will be in [body] and the parameters will be in [parameters]. |
| 49 CodeGenerator(this.glue, this.registry); | 49 CodeGenerator(this.glue, this.registry); |
| 50 | 50 |
| 51 void buildFunction(tree_ir.FunctionDefinition function) { | 51 void buildFunction(tree_ir.FunctionDefinition function) { |
| 52 currentFunction = function.element; | 52 currentFunction = function.element; |
| 53 visitStatement(function.body); | 53 visitStatement(function.body); |
| 54 |
| 55 Set<tree_ir.Variable> parameterSet = new Set<tree_ir.Variable>(); |
| 56 |
| 54 for (tree_ir.Variable parameter in function.parameters) { | 57 for (tree_ir.Variable parameter in function.parameters) { |
| 55 String name = getVariableName(parameter); | 58 String name = getVariableName(parameter); |
| 56 parameters.add(new js.Parameter(name)); | 59 parameters.add(new js.Parameter(name)); |
| 60 parameterSet.add(parameter); |
| 57 } | 61 } |
| 58 | 62 |
| 59 List<js.VariableInitialization> jsVariables = <js.VariableInitialization>[]; | 63 List<js.VariableInitialization> jsVariables = <js.VariableInitialization>[]; |
| 60 | 64 |
| 61 for (tree_ir.Variable variable in variableNames.keys) { | 65 for (tree_ir.Variable variable in variableNames.keys) { |
| 66 if (parameterSet.contains(variable)) continue; |
| 62 String name = getVariableName(variable); | 67 String name = getVariableName(variable); |
| 63 js.VariableInitialization jsVariable = new js.VariableInitialization( | 68 js.VariableInitialization jsVariable = new js.VariableInitialization( |
| 64 new js.VariableDeclaration(name), | 69 new js.VariableDeclaration(name), |
| 65 null); | 70 null); |
| 66 jsVariables.add(jsVariable); | 71 jsVariables.add(jsVariable); |
| 67 } | 72 } |
| 68 | 73 |
| 69 if (jsVariables.length > 0) { | 74 if (jsVariables.length > 0) { |
| 70 // Would be nice to avoid inserting at the beginning of list. | 75 // Would be nice to avoid inserting at the beginning of list. |
| 71 accumulator.insert(0, new js.ExpressionStatement( | 76 accumulator.insert(0, new js.ExpressionStatement( |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 @override | 288 @override |
| 284 void visitReturn(tree_ir.Return node) { | 289 void visitReturn(tree_ir.Return node) { |
| 285 if (node.value != null) { | 290 if (node.value != null) { |
| 286 accumulator.add(new js.Return(visitExpression(node.value))); | 291 accumulator.add(new js.Return(visitExpression(node.value))); |
| 287 } | 292 } |
| 288 } | 293 } |
| 289 | 294 |
| 290 bool isNullLiteral(js.Expression exp) => exp is js.LiteralNull; | 295 bool isNullLiteral(js.Expression exp) => exp is js.LiteralNull; |
| 291 | 296 |
| 292 } | 297 } |
| OLD | NEW |