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 '../../tree_ir/tree_ir_nodes.dart' show BuiltinOperator; | 10 import '../../tree_ir/tree_ir_nodes.dart' show BuiltinOperator; |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 (int index) => visitExpression(expressions[index]), | 164 (int index) => visitExpression(expressions[index]), |
165 growable: false); | 165 growable: false); |
166 } | 166 } |
167 | 167 |
168 giveup(tree_ir.Node node, | 168 giveup(tree_ir.Node node, |
169 [String reason = 'unimplemented in CodeGenerator']) { | 169 [String reason = 'unimplemented in CodeGenerator']) { |
170 throw new CodegenBailout(node, reason); | 170 throw new CodegenBailout(node, reason); |
171 } | 171 } |
172 | 172 |
173 @override | 173 @override |
174 js.Expression visitConcatenateStrings(tree_ir.ConcatenateStrings node) { | |
175 js.Expression addStrings(js.Expression left, js.Expression right) { | |
176 return new js.Binary('+', left, right); | |
177 } | |
178 | |
179 js.Expression toString(tree_ir.Expression input) { | |
180 bool useDirectly = input is tree_ir.Constant && | |
181 (input.value.isString || | |
182 input.value.isInt || | |
183 input.value.isBool); | |
184 js.Expression value = visitExpression(input); | |
185 if (useDirectly) { | |
186 return value; | |
187 } else { | |
188 Element convertToString = glue.getStringConversion(); | |
189 registry.registerStaticUse(convertToString); | |
190 js.Expression access = glue.staticFunctionAccess(convertToString); | |
191 return (new js.Call(access, <js.Expression>[value])); | |
192 } | |
193 } | |
194 | |
195 return node.arguments.map(toString).reduce(addStrings); | |
196 } | |
197 | |
198 @override | |
199 js.Expression visitConditional(tree_ir.Conditional node) { | 174 js.Expression visitConditional(tree_ir.Conditional node) { |
200 return new js.Conditional( | 175 return new js.Conditional( |
201 visitExpression(node.condition), | 176 visitExpression(node.condition), |
202 visitExpression(node.thenExpression), | 177 visitExpression(node.thenExpression), |
203 visitExpression(node.elseExpression)); | 178 visitExpression(node.elseExpression)); |
204 } | 179 } |
205 | 180 |
206 js.Expression buildConstant(ConstantValue constant) { | 181 js.Expression buildConstant(ConstantValue constant) { |
207 registry.registerCompileTimeConstant(constant); | 182 registry.registerCompileTimeConstant(constant); |
208 return glue.constantReference(constant); | 183 return glue.constantReference(constant); |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 case BuiltinOperator.NumXor: | 707 case BuiltinOperator.NumXor: |
733 return js.js('(# ^ #) >>> 0', args); | 708 return js.js('(# ^ #) >>> 0', args); |
734 case BuiltinOperator.NumLt: | 709 case BuiltinOperator.NumLt: |
735 return new js.Binary('<', args[0], args[1]); | 710 return new js.Binary('<', args[0], args[1]); |
736 case BuiltinOperator.NumLe: | 711 case BuiltinOperator.NumLe: |
737 return new js.Binary('<=', args[0], args[1]); | 712 return new js.Binary('<=', args[0], args[1]); |
738 case BuiltinOperator.NumGt: | 713 case BuiltinOperator.NumGt: |
739 return new js.Binary('>', args[0], args[1]); | 714 return new js.Binary('>', args[0], args[1]); |
740 case BuiltinOperator.NumGe: | 715 case BuiltinOperator.NumGe: |
741 return new js.Binary('>=', args[0], args[1]); | 716 return new js.Binary('>=', args[0], args[1]); |
| 717 case BuiltinOperator.StringConcatenate: |
| 718 if (args.isEmpty) return js.string(''); |
| 719 return args.reduce((e1,e2) => new js.Binary('+', e1, e2)); |
742 case BuiltinOperator.StrictEq: | 720 case BuiltinOperator.StrictEq: |
743 return new js.Binary('===', args[0], args[1]); | 721 return new js.Binary('===', args[0], args[1]); |
744 case BuiltinOperator.StrictNeq: | 722 case BuiltinOperator.StrictNeq: |
745 return new js.Binary('!==', args[0], args[1]); | 723 return new js.Binary('!==', args[0], args[1]); |
746 case BuiltinOperator.LooseEq: | 724 case BuiltinOperator.LooseEq: |
747 return new js.Binary('==', args[0], args[1]); | 725 return new js.Binary('==', args[0], args[1]); |
748 case BuiltinOperator.LooseNeq: | 726 case BuiltinOperator.LooseNeq: |
749 return new js.Binary('!=', args[0], args[1]); | 727 return new js.Binary('!=', args[0], args[1]); |
750 case BuiltinOperator.IsFalsy: | 728 case BuiltinOperator.IsFalsy: |
751 return new js.Prefix('!', args[0]); | 729 return new js.Prefix('!', args[0]); |
752 case BuiltinOperator.IsNumber: | 730 case BuiltinOperator.IsNumber: |
753 return js.js("typeof # === 'number'", args); | 731 return js.js("typeof # === 'number'", args); |
754 case BuiltinOperator.IsNotNumber: | 732 case BuiltinOperator.IsNotNumber: |
755 return js.js("typeof # !== 'number'", args); | 733 return js.js("typeof # !== 'number'", args); |
756 case BuiltinOperator.IsFloor: | 734 case BuiltinOperator.IsFloor: |
757 return js.js("Math.floor(#) === #", args); | 735 return js.js("Math.floor(#) === #", args); |
758 case BuiltinOperator.IsNumberAndFloor: | 736 case BuiltinOperator.IsNumberAndFloor: |
759 return js.js("typeof # === 'number' && Math.floor(#) === #", args); | 737 return js.js("typeof # === 'number' && Math.floor(#) === #", args); |
760 } | 738 } |
761 } | 739 } |
762 | 740 |
763 visitFunctionExpression(tree_ir.FunctionExpression node) { | 741 visitFunctionExpression(tree_ir.FunctionExpression node) { |
764 // FunctionExpressions are currently unused. | 742 // FunctionExpressions are currently unused. |
765 // We might need them if we want to emit raw JS nested functions. | 743 // We might need them if we want to emit raw JS nested functions. |
766 throw 'FunctionExpressions should not be used'; | 744 throw 'FunctionExpressions should not be used'; |
767 } | 745 } |
768 } | 746 } |
OLD | NEW |