OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 import 'package:kernel/ast.dart' as ir; |
| 6 |
| 7 import '../compiler.dart'; |
| 8 |
| 9 import 'builder_kernel.dart'; |
| 10 import 'kernel_ast_adapter.dart'; |
| 11 import 'nodes.dart'; |
| 12 |
| 13 /// Visits and concatenates the expressions in a string concatenation. |
| 14 class KernelStringBuilder extends ir.Visitor { |
| 15 final KernelSsaBuilder builder; |
| 16 Compiler get compiler => builder.compiler; |
| 17 KernelAstAdapter get astAdapter => builder.astAdapter; |
| 18 |
| 19 /// The string value generated so far. |
| 20 HInstruction result = null; |
| 21 |
| 22 KernelStringBuilder(this.builder); |
| 23 |
| 24 @override |
| 25 void defaultNode(ir.Node node) { |
| 26 compiler.reporter |
| 27 .internalError(astAdapter.getNode(node), 'Unexpected node.'); |
| 28 } |
| 29 |
| 30 @override |
| 31 void defaultExpression(ir.Expression node) { |
| 32 node.accept(builder); |
| 33 HInstruction expression = builder.pop(); |
| 34 |
| 35 // We want to use HStringify when: |
| 36 // 1. The value is known to be a primitive type, because it might get |
| 37 // constant-folded and codegen has some tricks with JavaScript |
| 38 // conversions. |
| 39 // 2. The value can be primitive, because the library stringifier has |
| 40 // fast-path code for most primitives. |
| 41 if (expression.canBePrimitive(compiler)) { |
| 42 append(stringify(expression)); |
| 43 return; |
| 44 } |
| 45 |
| 46 // TODO(het): If toString method is guaranteed to return a string, then call |
| 47 // it directly instead of stringify. Or, better yet, do this later in the |
| 48 // optimization phase. |
| 49 |
| 50 append(stringify(expression)); |
| 51 } |
| 52 |
| 53 @override |
| 54 void visitStringConcatenation(ir.StringConcatenation node) { |
| 55 node.visitChildren(this); |
| 56 } |
| 57 |
| 58 void append(HInstruction expression) { |
| 59 result = (result == null) ? expression : concat(result, expression); |
| 60 } |
| 61 |
| 62 HInstruction concat(HInstruction left, HInstruction right) { |
| 63 HInstruction instruction = |
| 64 new HStringConcat(left, right, builder.backend.stringType); |
| 65 builder.add(instruction); |
| 66 return instruction; |
| 67 } |
| 68 |
| 69 HInstruction stringify(HInstruction expression) { |
| 70 HInstruction instruction = |
| 71 new HStringify(expression, builder.backend.stringType); |
| 72 builder.add(instruction); |
| 73 return instruction; |
| 74 } |
| 75 } |
OLD | NEW |